]> git.meshlink.io Git - meshlink/blob - src/subnet.c
Always close all sockets in terminate_connection().
[meshlink] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
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: subnet.c,v 1.1.2.23 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 "conf.h"
30 #include "net.h"
31 #include "connection.h"
32 #include "subnet.h"
33 #include "system.h"
34
35 #include <utils.h>
36 #include <xalloc.h>
37 #include <avl_tree.h>
38
39 /* lists type of subnet */
40
41 avl_tree_t *subnet_tree;
42
43 void init_subnets(void)
44 {
45 cp
46   subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
47 cp
48 }
49
50 /* Subnet comparison */
51
52 int subnet_compare_mac(subnet_t *a, subnet_t *b)
53 {
54 cp
55   return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
56 }
57
58 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
59 {
60 cp
61   /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
62    
63   if(a->net.ipv4.address == b->net.ipv4.address)
64     return a->net.ipv4.mask - b->net.ipv4.mask;
65   else
66     return a->net.ipv4.address - b->net.ipv4.address;
67 }
68
69 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
70 {
71 cp
72   /* Same as ipv4 case, but with nasty 128 bit addresses */
73   
74   if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
75     if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
76        (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
77        (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
78        (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
79        (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
80        (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
81        (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
82        (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
83       return -1;
84   
85   return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
86 }
87
88 int subnet_compare(subnet_t *a, subnet_t *b)
89 {
90   int x;
91 cp  
92   x = a->type - b->type;
93   if(x)
94     return x;
95     
96   switch(a->type)
97     {
98       case SUBNET_MAC:
99         return subnet_compare_mac(a, b);
100       case SUBNET_IPV4:
101         return subnet_compare_ipv4(a, b);
102       case SUBNET_IPV6:
103         return subnet_compare_ipv6(a, b);
104       default:
105         syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
106         sighup = 1;
107         return 0;
108     }
109 }
110
111 /* Allocating and freeing space for subnets */
112
113 subnet_t *new_subnet(void)
114 {
115 cp
116   return (subnet_t *)xmalloc(sizeof(subnet_t));
117 }
118
119 void free_subnet(subnet_t *subnet)
120 {
121 cp
122   free(subnet);
123 }
124
125 /* Linked list management */
126
127 void subnet_add(connection_t *cl, subnet_t *subnet)
128 {
129 cp
130   subnet->owner = cl;
131
132   while(!avl_insert(subnet_tree, subnet))
133     {
134       subnet_t *old;
135       
136       old = (subnet_t *)avl_search(subnet_tree, subnet);
137
138       if(debug_lvl >= DEBUG_PROTOCOL)
139         {
140           char *subnetstr;
141           subnetstr = net2str(subnet);
142           syslog(LOG_WARNING, _("Duplicate subnet %s for %s (%s), previous owner %s (%s)!"),
143                  subnetstr, cl->name, cl->hostname, old->owner->name, old->owner->hostname);
144           free(subnetstr);
145         }
146
147       subnet_del(old);
148     }
149
150   avl_insert(cl->subnet_tree, subnet);
151 cp
152 }
153
154 void subnet_del(subnet_t *subnet)
155 {
156 cp
157   avl_delete(subnet->owner->subnet_tree, subnet);
158 cp
159   avl_delete(subnet_tree, subnet);
160 cp
161 }
162
163 /* Ascii representation of subnets */
164
165 subnet_t *str2net(char *subnetstr)
166 {
167   int type;
168   subnet_t *subnet;
169 cp
170   if(sscanf(subnetstr, "%d,", &type) != 1)
171     return NULL;
172 cp
173   subnet = new_subnet();
174 cp
175   switch(type)
176     {
177       case SUBNET_MAC:
178         if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
179                    &subnet->net.mac.address.x[0],
180                    &subnet->net.mac.address.x[1],
181                    &subnet->net.mac.address.x[2],
182                    &subnet->net.mac.address.x[3],
183                    &subnet->net.mac.address.x[4],
184                    &subnet->net.mac.address.x[5]) != 7)
185           {
186             free_subnet(subnet);
187             return NULL;
188           }
189         break;
190       case SUBNET_IPV4:
191         if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
192           {
193             free_subnet(subnet);
194             return NULL;
195           }
196         break;
197       case SUBNET_IPV6:
198         if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
199                    &subnet->net.ipv6.address.x[0],
200                    &subnet->net.ipv6.address.x[1],
201                    &subnet->net.ipv6.address.x[2],
202                    &subnet->net.ipv6.address.x[3],
203                    &subnet->net.ipv6.address.x[4],
204                    &subnet->net.ipv6.address.x[5],
205                    &subnet->net.ipv6.address.x[6],
206                    &subnet->net.ipv6.address.x[7],
207                    &subnet->net.ipv6.mask.x[0],
208                    &subnet->net.ipv6.mask.x[1],
209                    &subnet->net.ipv6.mask.x[2],
210                    &subnet->net.ipv6.mask.x[3],
211                    &subnet->net.ipv6.mask.x[4],
212                    &subnet->net.ipv6.mask.x[5],
213                    &subnet->net.ipv6.mask.x[6],
214                    &subnet->net.ipv6.mask.x[7]) != 17)
215           {
216             free_subnet(subnet);
217             return NULL;
218           }
219         break;
220       default:
221         free_subnet(subnet);
222         return NULL;
223     }
224 cp
225   return subnet;
226 }
227
228 char *net2str(subnet_t *subnet)
229 {
230   char *netstr;
231 cp
232   switch(subnet->type)
233     {
234       case SUBNET_MAC:
235         asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
236                    subnet->net.mac.address.x[0],
237                    subnet->net.mac.address.x[1],
238                    subnet->net.mac.address.x[2],
239                    subnet->net.mac.address.x[3],
240                    subnet->net.mac.address.x[4],
241                    subnet->net.mac.address.x[5]);
242         break;
243       case SUBNET_IPV4:
244         asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
245         break;
246       case SUBNET_IPV6:
247         asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
248                    subnet->net.ipv6.address.x[0],
249                    subnet->net.ipv6.address.x[1],
250                    subnet->net.ipv6.address.x[2],
251                    subnet->net.ipv6.address.x[3],
252                    subnet->net.ipv6.address.x[4],
253                    subnet->net.ipv6.address.x[5],
254                    subnet->net.ipv6.address.x[6],
255                    subnet->net.ipv6.address.x[7],
256                    subnet->net.ipv6.mask.x[0],
257                    subnet->net.ipv6.mask.x[1],
258                    subnet->net.ipv6.mask.x[2],
259                    subnet->net.ipv6.mask.x[3],
260                    subnet->net.ipv6.mask.x[4],
261                    subnet->net.ipv6.mask.x[5],
262                    subnet->net.ipv6.mask.x[6],
263                    subnet->net.ipv6.mask.x[7]);
264         break;
265       default:
266         asprintf(&netstr, _("unknown subnet type"));
267     }
268 cp
269   return netstr;
270 }
271
272 /* Subnet lookup routines */
273
274 subnet_t *lookup_subnet_mac(mac_t *address)
275 {
276   subnet_t subnet, *p;
277 cp
278   subnet.type = SUBNET_MAC;
279   memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
280
281   p = (subnet_t *)avl_search(subnet_tree, &subnet);
282 cp
283   return p;
284 }
285
286 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
287 {
288   subnet_t subnet, *p;
289 cp
290   subnet.type = SUBNET_IPV4;
291   subnet.net.ipv4.address = *address;
292   subnet.net.ipv4.mask = 0xFFFFFFFF;
293
294   do
295   {
296     /* Go find subnet */
297   
298     p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
299
300   /* Check if the found subnet REALLY matches */
301 cp
302     if(p)
303       {
304         if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
305           break;
306         else
307           {
308             /* Otherwise, see if there is a bigger enclosing subnet */
309
310             subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
311             subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
312           }
313       }
314    } while (p);
315    
316    return p;
317 }
318
319 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
320 {
321   subnet_t subnet, *p;
322   int i;
323 cp
324   subnet.type = SUBNET_IPV6;
325   memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
326   memset(&subnet.net.ipv6.mask, 0xFF, 16);
327   
328   p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
329   
330   if(p)
331     for(i=0; i<8; i++)
332       if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
333         return NULL;
334
335   return p;
336 }
337
338 void dump_subnet_list(void)
339 {
340   char *netstr;
341   subnet_t *subnet;
342   avl_node_t *node;
343 cp
344   syslog(LOG_DEBUG, _("Subnet list:"));
345   for(node = subnet_tree->head; node; node = node->next)
346     {
347       subnet = (subnet_t *)node->data;
348       netstr = net2str(subnet);
349       syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
350       free(netstr);
351     }
352   syslog(LOG_DEBUG, _("End of subnet list."));
353 cp
354 }