Another big & bad commit:
[tinc] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000 Ivo Timmermans <itimmermans@bigfoot.com>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: connection.c,v 1.1.2.6 2000/11/24 23:13:01 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27
28 #include <rbl.h>
29
30 #include "net.h"        /* Don't ask. */
31 #include "netutl.h"
32 #include "config.h"
33 #include "conf.h"
34 #include <utils.h>
35 #include "subnet.h"
36
37 #include "xalloc.h"
38 #include "system.h"
39
40 /* Root of the connection list */
41
42 rbltree_t *connection_tree;
43 rbltree_t *id_tree;
44
45 connection_t *myself = NULL;
46
47 /* Initialization and callbacks */
48
49 int connection_compare(connection_t *a, connection_t *b)
50 {
51   ipv4_t result;
52   result = a->address - b->address;
53   if(result)
54     return result;
55   else
56     return a->port - b->port;
57 }
58
59 int id_compare(connection_t *a, connection_t *b)
60 {
61   return strcmp(a->name, b->name);
62 }
63
64 void init_connections(void)
65 {
66   connection_tree = new_rbltree((rbl_compare_t)connection_compare, (rbl_action_t)free_connection);
67   id_tree = new_rbltree((rbl_compare_t)id_compare, NULL);
68 }
69
70 /* Creation and deletion of connection elements */
71
72 connection_t *new_connection(void)
73 {
74   connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
75 cp
76   p->subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, NULL);
77 cp
78   return p;
79 }
80
81 void free_connection(connection_t *p)
82 {
83 cp
84   if(p->sq)
85     destroy_queue(p->sq);
86   if(p->rq)
87     destroy_queue(p->rq);
88   if(p->name && p->name!=unknown)
89     free(p->name);
90   if(p->hostname)
91     free(p->hostname);
92   if(p->rsa_key)
93     RSA_free(p->rsa_key);
94   if(p->cipher_pktkey)
95     free(p->cipher_pktkey);
96   if(p->buffer)
97     free(p->buffer);
98   if(p->config)
99     clear_config(&p->config);
100   free(p);
101 cp
102 }
103
104 /*
105   remove all marked connections
106 */
107 void prune_connection_tree(void)
108 {
109   rbl_t *rbl;
110   connection_t *cl;
111 cp
112   RBL_FOREACH(connection_tree, rbl)
113     {
114       cl = (connection_t *) rbl->data;
115       if(cl->status.remove)
116         connection_del(cl);
117     }
118 cp
119 }
120
121 /*
122   free all elements of connection
123 */
124 void destroy_connection_tree(void)
125 {
126 cp
127   rbl_delete_rbltree(id_tree);
128   rbl_delete_rbltree(connection_tree);
129 cp
130 }
131
132 /* Linked list management */
133
134 void connection_add(connection_t *cl)
135 {
136 cp
137   rbl_insert(connection_tree, cl);
138 cp
139 }
140
141 void id_add(connection_t *cl)
142 {
143 cp
144   rbl_insert(id_tree, cl);
145 cp
146 }
147
148 void connection_del(connection_t *cl)
149 {
150 cp
151   rbl_delete(id_tree, cl);
152   rbl_delete(connection_tree, cl);
153 cp
154 }
155
156 /* Lookup functions */
157
158 connection_t *lookup_connection(ipv4_t address, short unsigned int port)
159 {
160   connection_t cl;
161 cp
162   cl.address = address;
163   cl.port = port;
164
165   return rbl_search(connection_tree, &cl);
166 }
167
168 connection_t *lookup_id(char *name)
169 {
170   connection_t cl, *p;
171 cp
172   cl.name = name;
173   p = rbl_search(id_tree, &cl);
174   if(p && p->status.active)
175     return p;
176   else
177     return NULL;
178 }
179
180 /* Debugging */
181
182 void dump_connection_list(void)
183 {
184   rbl_t *rbl;
185   connection_t *cl;
186 cp
187   syslog(LOG_DEBUG, _("Connection list:"));
188
189   syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
190          myself->name, myself->hostname, myself->port, myself->flags,
191          myself->socket, myself->meta_socket, myself->status);
192
193   RBL_FOREACH(connection_tree, rbl)
194     {
195       cl = (connection_t *)rbl->data;
196       syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
197              cl->name, cl->hostname, cl->port, cl->flags,
198              cl->socket, cl->meta_socket, cl->status);
199     }
200     
201   syslog(LOG_DEBUG, _("End of connection list."));
202 cp
203 }
204
205 int read_host_config(connection_t *cl)
206 {
207   char *fname;
208   int x;
209 cp
210   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
211   x = read_config_file(&cl->config, fname);
212   free(fname);
213 cp
214   return x;
215 }