Fix gcc 3.0 warnings.
[tinc] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000,2001 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.12 2001/06/29 13:09:55 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28
29 #include <avl_tree.h>
30 #include <list.h>
31
32 #include "net.h"        /* Don't ask. */
33 #include "netutl.h"
34 #include "config.h"
35 #include "conf.h"
36 #include <utils.h>
37 #include "subnet.h"
38
39 #include "xalloc.h"
40 #include "system.h"
41
42 /* Root of the connection list */
43
44 avl_tree_t *connection_tree;
45 avl_tree_t *id_tree;
46
47 /* Pointer to connection describing myself */
48
49 connection_t *myself = NULL;
50
51 /* Initialization and callbacks */
52
53 int connection_compare(connection_t *a, connection_t *b)
54 {
55   ipv4_t result;
56
57   result = a->address - b->address;
58   if(result)
59     return result;
60   else
61     return a->port - b->port;
62 }
63
64 int id_compare(connection_t *a, connection_t *b)
65 {
66   return strcmp(a->name, b->name);
67 }
68
69 void init_connections(void)
70 {
71   connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, (avl_action_t)free_connection);
72   id_tree = avl_alloc_tree((avl_compare_t)id_compare, NULL);
73 }
74
75 /* Creation and deletion of connection elements */
76
77 connection_t *new_connection(void)
78 {
79   connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
80 cp
81   p->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
82   p->queue = list_alloc((list_action_t)free);
83 cp
84   return p;
85 }
86
87 void free_connection(connection_t *p)
88 {
89 cp
90   if(p->queue)
91     list_delete_list(p->queue);
92   if(p->name)
93     free(p->name);
94   if(p->hostname)
95     free(p->hostname);
96   if(p->rsa_key)
97     RSA_free(p->rsa_key);
98   if(p->cipher_pktkey)
99     free(p->cipher_pktkey);
100   if(p->buffer)
101     free(p->buffer);
102   if(p->config)
103     clear_config(&p->config);
104   free(p);
105 cp
106 }
107
108 /*
109   remove all marked connections
110 */
111 void prune_connection_tree(void)
112 {
113   avl_node_t *node, *next;
114   connection_t *cl;
115 cp
116   for(node = connection_tree->head; node; node = next)
117     {
118       next = node->next;
119       cl = (connection_t *)node->data;
120       if(cl->status.remove)
121         connection_del(cl);
122     }
123 cp
124 }
125
126 /*
127   free all elements of connection
128 */
129 void destroy_connection_tree(void)
130 {
131 cp
132   avl_delete_tree(id_tree);
133   avl_delete_tree(connection_tree);
134 cp
135 }
136
137 /* Linked list management */
138
139 void connection_add(connection_t *cl)
140 {
141 cp
142   avl_insert(connection_tree, cl);
143 cp
144 }
145
146 void id_add(connection_t *cl)
147 {
148 cp
149   avl_insert(id_tree, cl);
150 cp
151 }
152
153 void connection_del(connection_t *cl)
154 {
155 cp
156   avl_delete(id_tree, cl);
157   avl_delete(connection_tree, cl);
158 cp
159 }
160
161 /* Lookup functions */
162
163 connection_t *lookup_connection(ipv4_t address, short unsigned int port)
164 {
165   connection_t cl;
166 cp
167   cl.address = address;
168   cl.port = port;
169
170   return avl_search(connection_tree, &cl);
171 }
172
173 connection_t *lookup_id(char *name)
174 {
175   connection_t cl, *p;
176 cp
177   cl.name = name;
178   p = avl_search(id_tree, &cl);
179   if(p && p->status.active)
180     return p;
181   else
182     return NULL;
183 }
184
185 /* Debugging */
186
187 void dump_connection_list(void)
188 {
189   avl_node_t *node;
190   connection_t *cl;
191 cp
192   syslog(LOG_DEBUG, _("Connection list:"));
193
194   syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
195          myself->name, myself->hostname, myself->port, myself->options,
196          myself->socket, myself->meta_socket, myself->status);
197
198   for(node = connection_tree->head; node; node = node->next)
199     {
200       cl = (connection_t *)node->data;
201       syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
202              cl->name, cl->hostname, cl->port, cl->options,
203              cl->socket, cl->meta_socket, cl->status);
204     }
205     
206   syslog(LOG_DEBUG, _("End of connection list."));
207 cp
208 }
209
210 int read_host_config(connection_t *cl)
211 {
212   char *fname;
213   int x;
214 cp
215   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
216   x = read_config_file(&cl->config, fname);
217   free(fname);
218 cp
219   return x;
220 }