- Cleaned up and checked for some more NULL pointers in rbl.c
[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.4 2000/11/22 18:54:07 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(sizeof(*p));
75 cp
76   /* initialise all those stupid pointers at once */
77   memset(p, '\0', sizeof(*p));
78
79   p->subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, NULL);
80 cp
81   return p;
82 }
83
84 void free_connection(connection_t *p)
85 {
86 cp
87   if(p->sq)
88     destroy_queue(p->sq);
89   if(p->rq)
90     destroy_queue(p->rq);
91   if(p->name && p->name!=unknown)
92     free(p->name);
93   if(p->hostname)
94     free(p->hostname);
95   if(p->rsa_key)
96     RSA_free(p->rsa_key);
97   if(p->cipher_pktkey)
98     free(p->cipher_pktkey);
99   if(p->buffer)
100     free(p->buffer);
101   if(p->config)
102     clear_config(&p->config);
103   free(p);
104 cp
105 }
106
107 /*
108   remove all marked connections
109 */
110 void prune_connection_tree(void)
111 {
112   rbl_t *rbl;
113   connection_t *cl;
114 cp
115   RBL_FOREACH(connection_tree, rbl)
116     {
117       cl = (connection_t *) rbl->data;
118       if(cl->status.remove)
119         connection_del(cl);
120     }
121 cp
122 }
123
124 /*
125   free all elements of connection
126 */
127 void destroy_connection_tree(void)
128 {
129 cp
130   rbl_delete_rbltree(id_tree);
131   rbl_delete_rbltree(connection_tree);
132 cp
133 }
134
135 /* Linked list management */
136
137 void connection_add(connection_t *cl)
138 {
139 cp
140   rbl_insert(connection_tree, cl);
141 cp
142 }
143
144 void id_add(connection_t *cl)
145 {
146 cp
147   rbl_insert(id_tree, cl);
148 cp
149 }
150
151 void connection_del(connection_t *cl)
152 {
153 cp
154   rbl_delete(id_tree, cl);
155   rbl_delete(connection_tree, cl);
156 cp
157 }
158
159 /* Lookup functions */
160
161 connection_t *lookup_connection(ipv4_t address, short unsigned int port)
162 {
163   connection_t cl, *p;
164 cp
165   cl.address = address;
166   cl.port = port;
167
168   return rbl_search(connection_tree, &cl);
169 }
170
171 connection_t *lookup_id(char *name)
172 {
173   connection_t cl, *p;
174 cp
175   cl.name = name;
176   p = rbl_search(id_tree, &cl);
177   if(p && p->status.active)
178     return p;
179   else
180     return NULL;
181 }
182
183 /* Debugging */
184
185 void dump_connection_list(void)
186 {
187   rbl_t *rbl;
188   connection_t *cl;
189 cp
190   syslog(LOG_DEBUG, _("Connection list:"));
191
192   syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
193          myself->name, myself->hostname, myself->port, myself->flags,
194          myself->socket, myself->meta_socket, myself->status);
195
196   RBL_FOREACH(connection_tree, rbl)
197     {
198       cl = (connection_t *)rbl->data;
199       syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
200              cl->name, cl->hostname, cl->port, cl->flags,
201              cl->socket, cl->meta_socket, cl->status);
202     }
203     
204   syslog(LOG_DEBUG, _("End of connection list."));
205 cp
206 }
207
208 int read_host_config(connection_t *cl)
209 {
210   char *fname;
211   int x;
212 cp
213   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
214   x = read_config_file(&cl->config, fname);
215   free(fname);
216 cp
217   return x;
218 }