Small fixes so tinc compiles out of the box on SunOS 5.8
[oweals/tinc.git] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.eu.org>,
4                   2000-2002 Ivo Timmermans <ivo@o2w.nl>
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.43 2002/09/15 14:55:54 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <netdb.h>
31 #include <netinet/in.h>
32
33 #include <utils.h>
34 #include <xalloc.h>
35 #include <avl_tree.h>
36
37 #include "conf.h"
38 #include "net.h"
39 #include "node.h"
40 #include "subnet.h"
41 #include "netutl.h"
42
43 #include "system.h"
44
45 /* lists type of subnet */
46
47 avl_tree_t *subnet_tree;
48
49 /* Subnet comparison */
50
51 int subnet_compare_mac(subnet_t *a, subnet_t *b)
52 {
53         int result;
54
55         result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
56
57         if(result || !a->owner || !b->owner)
58                 return result;
59
60         return strcmp(a->owner->name, b->owner->name);
61 }
62
63 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
64 {
65         int result;
66
67         result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
68
69         if(result)
70                 return result;
71
72         result = a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
73
74         if(result || !a->owner || !b->owner)
75                 return result;
76
77         return strcmp(a->owner->name, b->owner->name);
78 }
79
80 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
81 {
82         int result;
83
84         result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
85
86         if(result)
87                 return result;
88
89         result = a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
90
91         if(result || !a->owner || !b->owner)
92                 return result;
93
94         return strcmp(a->owner->name, b->owner->name);
95 }
96
97 int subnet_compare(subnet_t *a, subnet_t *b)
98 {
99         int result;
100
101         result = a->type - b->type;
102
103         if(result)
104                 return result;
105
106         switch (a->type) {
107         case SUBNET_MAC:
108                 return subnet_compare_mac(a, b);
109         case SUBNET_IPV4:
110                 return subnet_compare_ipv4(a, b);
111         case SUBNET_IPV6:
112                 return subnet_compare_ipv6(a, b);
113         default:
114                 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
115                            a->type);
116                 cp_trace();
117                 exit(0);
118         }
119
120         return 0;
121 }
122
123 /* Initialising trees */
124
125 void init_subnets(void)
126 {
127         cp();
128
129         subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
130 }
131
132 void exit_subnets(void)
133 {
134         cp();
135
136         avl_delete_tree(subnet_tree);
137 }
138
139 avl_tree_t *new_subnet_tree(void)
140 {
141         cp();
142
143         return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
144 }
145
146 void free_subnet_tree(avl_tree_t *subnet_tree)
147 {
148         cp();
149
150         avl_delete_tree(subnet_tree);
151 }
152
153 /* Allocating and freeing space for subnets */
154
155 subnet_t *new_subnet(void)
156 {
157         cp();
158
159         return (subnet_t *) xmalloc_and_zero(sizeof(subnet_t));
160 }
161
162 void free_subnet(subnet_t *subnet)
163 {
164         cp();
165
166         free(subnet);
167 }
168
169 /* Adding and removing subnets */
170
171 void subnet_add(node_t *n, subnet_t *subnet)
172 {
173         cp();
174
175         subnet->owner = n;
176
177         avl_insert(subnet_tree, subnet);
178         avl_insert(n->subnet_tree, subnet);
179 }
180
181 void subnet_del(node_t *n, subnet_t *subnet)
182 {
183         cp();
184
185         avl_delete(n->subnet_tree, subnet);
186         avl_delete(subnet_tree, subnet);
187 }
188
189 /* Ascii representation of subnets */
190
191 subnet_t *str2net(char *subnetstr)
192 {
193         int i, l;
194         subnet_t *subnet;
195         uint16_t x[8];
196
197         cp();
198
199         subnet = new_subnet();
200
201         if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
202                           &x[0], &x[1], &x[2], &x[3], &l) == 5) {
203                 subnet->type = SUBNET_IPV4;
204                 subnet->net.ipv4.prefixlength = l;
205
206                 for(i = 0; i < 4; i++)
207                         subnet->net.ipv4.address.x[i] = x[i];
208
209                 return subnet;
210         }
211
212         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
213                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
214                           &l) == 9) {
215                 subnet->type = SUBNET_IPV6;
216                 subnet->net.ipv6.prefixlength = l;
217
218                 for(i = 0; i < 8; i++)
219                         subnet->net.ipv6.address.x[i] = htons(x[i]);
220
221                 return subnet;
222         }
223
224         if(sscanf(subnetstr, "%hu.%hu.%hu.%hu", &x[0], &x[1], &x[2], &x[3]) == 4) {
225                 subnet->type = SUBNET_IPV4;
226                 subnet->net.ipv4.prefixlength = 32;
227
228                 for(i = 0; i < 4; i++)
229                         subnet->net.ipv4.address.x[i] = x[i];
230
231                 return subnet;
232         }
233
234         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
235                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
236                 subnet->type = SUBNET_IPV6;
237                 subnet->net.ipv6.prefixlength = 128;
238
239                 for(i = 0; i < 8; i++)
240                         subnet->net.ipv6.address.x[i] = htons(x[i]);
241
242                 return subnet;
243         }
244
245         if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
246                           &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
247                 subnet->type = SUBNET_MAC;
248
249                 for(i = 0; i < 6; i++)
250                         subnet->net.mac.address.x[i] = x[i];
251
252                 return subnet;
253         }
254
255         free(subnet);
256
257         return NULL;
258 }
259
260 char *net2str(subnet_t *subnet)
261 {
262         char *netstr;
263
264         cp();
265
266         switch (subnet->type) {
267                 case SUBNET_MAC:
268                         asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
269                                          subnet->net.mac.address.x[0],
270                                          subnet->net.mac.address.x[1],
271                                          subnet->net.mac.address.x[2],
272                                          subnet->net.mac.address.x[3],
273                                          subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
274                         break;
275
276                 case SUBNET_IPV4:
277                         asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
278                                          subnet->net.ipv4.address.x[0],
279                                          subnet->net.ipv4.address.x[1],
280                                          subnet->net.ipv4.address.x[2],
281                                          subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
282                         break;
283
284                 case SUBNET_IPV6:
285                         asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
286                                          ntohs(subnet->net.ipv6.address.x[0]),
287                                          ntohs(subnet->net.ipv6.address.x[1]),
288                                          ntohs(subnet->net.ipv6.address.x[2]),
289                                          ntohs(subnet->net.ipv6.address.x[3]),
290                                          ntohs(subnet->net.ipv6.address.x[4]),
291                                          ntohs(subnet->net.ipv6.address.x[5]),
292                                          ntohs(subnet->net.ipv6.address.x[6]),
293                                          ntohs(subnet->net.ipv6.address.x[7]),
294                                          subnet->net.ipv6.prefixlength);
295                         break;
296
297                 default:
298                         syslog(LOG_ERR,
299                                    _("net2str() was called with unknown subnet type %d, exiting!"),
300                                    subnet->type);
301                         cp_trace();
302                         exit(0);
303         }
304
305         return netstr;
306 }
307
308 /* Subnet lookup routines */
309
310 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
311 {
312         cp();
313
314         return avl_search(owner->subnet_tree, subnet);
315 }
316
317 subnet_t *lookup_subnet_mac(mac_t *address)
318 {
319         subnet_t subnet, *p;
320
321         cp();
322
323         subnet.type = SUBNET_MAC;
324         memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
325         subnet.owner = NULL;
326
327         p = (subnet_t *) avl_search(subnet_tree, &subnet);
328
329         return p;
330 }
331
332 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
333 {
334         subnet_t subnet, *p;
335
336         cp();
337
338         subnet.type = SUBNET_IPV4;
339         memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
340         subnet.net.ipv4.prefixlength = 32;
341         subnet.owner = NULL;
342
343         do {
344                 /* Go find subnet */
345
346                 p = (subnet_t *) avl_search_closest_smaller(subnet_tree, &subnet);
347
348                 /* Check if the found subnet REALLY matches */
349
350                 if(p) {
351                         if(p->type != SUBNET_IPV4) {
352                                 p = NULL;
353                                 break;
354                         }
355
356                         if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
357                                 break;
358                         else {
359                                 /* Otherwise, see if there is a bigger enclosing subnet */
360
361                                 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
362                                 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
363                         }
364                 }
365         } while(p);
366
367         return p;
368 }
369
370 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
371 {
372         subnet_t subnet, *p;
373
374         cp();
375
376         subnet.type = SUBNET_IPV6;
377         memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
378         subnet.net.ipv6.prefixlength = 128;
379         subnet.owner = NULL;
380
381         do {
382                 /* Go find subnet */
383
384                 p = (subnet_t *) avl_search_closest_smaller(subnet_tree, &subnet);
385
386                 /* Check if the found subnet REALLY matches */
387
388                 if(p) {
389                         if(p->type != SUBNET_IPV6)
390                                 return NULL;
391
392                         if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
393                                 break;
394                         else {
395                                 /* Otherwise, see if there is a bigger enclosing subnet */
396
397                                 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
398                                 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
399                         }
400                 }
401         } while(p);
402
403         return p;
404 }
405
406 void dump_subnets(void)
407 {
408         char *netstr;
409         subnet_t *subnet;
410         avl_node_t *node;
411
412         cp();
413
414         syslog(LOG_DEBUG, _("Subnet list:"));
415
416         for(node = subnet_tree->head; node; node = node->next) {
417                 subnet = (subnet_t *) node->data;
418                 netstr = net2str(subnet);
419                 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
420                 free(netstr);
421         }
422
423         syslog(LOG_DEBUG, _("End of subnet list."));
424 }