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