2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2006 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
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.
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.
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.
25 #include "splay_tree.h"
36 /* lists type of subnet */
38 splay_tree_t *subnet_tree;
40 /* Subnet comparison */
42 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b)
46 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof a->net.mac.address);
48 if(result || !a->owner || !b->owner)
51 return strcmp(a->owner->name, b->owner->name);
54 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b)
58 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof a->net.ipv4.address);
63 result = a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
65 if(result || !a->owner || !b->owner)
68 return strcmp(a->owner->name, b->owner->name);
71 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b)
75 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof a->net.ipv6.address);
80 result = a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
82 if(result || !a->owner || !b->owner)
85 return strcmp(a->owner->name, b->owner->name);
88 int subnet_compare(const subnet_t *a, const subnet_t *b)
92 result = a->type - b->type;
99 return subnet_compare_mac(a, b);
101 return subnet_compare_ipv4(a, b);
103 return subnet_compare_ipv6(a, b);
105 logger(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
114 /* Initialising trees */
116 void init_subnets(void)
120 subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
123 void exit_subnets(void)
127 splay_delete_tree(subnet_tree);
130 splay_tree_t *new_subnet_tree(void)
134 return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
137 void free_subnet_tree(splay_tree_t *subnet_tree)
141 splay_delete_tree(subnet_tree);
144 /* Allocating and freeing space for subnets */
146 subnet_t *new_subnet(void)
150 return xmalloc_and_zero(sizeof(subnet_t));
153 void free_subnet(subnet_t *subnet)
160 /* Adding and removing subnets */
162 void subnet_add(node_t *n, subnet_t *subnet)
168 splay_insert(subnet_tree, subnet);
169 splay_insert(n->subnet_tree, subnet);
172 void subnet_del(node_t *n, subnet_t *subnet)
176 splay_delete(n->subnet_tree, subnet);
177 splay_delete(subnet_tree, subnet);
180 /* Ascii representation of subnets */
182 bool str2net(subnet_t *subnet, const char *subnetstr)
189 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
190 &x[0], &x[1], &x[2], &x[3], &l) == 5) {
194 subnet->type = SUBNET_IPV4;
195 subnet->net.ipv4.prefixlength = l;
197 for(i = 0; i < 4; i++) {
200 subnet->net.ipv4.address.x[i] = x[i];
206 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
207 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
212 subnet->type = SUBNET_IPV6;
213 subnet->net.ipv6.prefixlength = l;
215 for(i = 0; i < 8; i++)
216 subnet->net.ipv6.address.x[i] = htons(x[i]);
221 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;
228 for(i = 0; i < 4; i++) {
231 subnet->net.ipv4.address.x[i] = x[i];
237 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
238 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
242 subnet->type = SUBNET_IPV6;
243 subnet->net.ipv6.prefixlength = 128;
245 for(i = 0; i < 8; i++)
246 subnet->net.ipv6.address.x[i] = htons(x[i]);
251 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
252 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
253 subnet->type = SUBNET_MAC;
255 for(i = 0; i < 6; i++)
256 subnet->net.mac.address.x[i] = x[i];
264 bool net2str(char *netstr, int len, const subnet_t *subnet)
268 if(!netstr || !subnet) {
269 logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet);
273 switch (subnet->type) {
275 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx",
276 subnet->net.mac.address.x[0],
277 subnet->net.mac.address.x[1],
278 subnet->net.mac.address.x[2],
279 subnet->net.mac.address.x[3],
280 subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
284 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d",
285 subnet->net.ipv4.address.x[0],
286 subnet->net.ipv4.address.x[1],
287 subnet->net.ipv4.address.x[2],
288 subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
292 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
293 ntohs(subnet->net.ipv6.address.x[0]),
294 ntohs(subnet->net.ipv6.address.x[1]),
295 ntohs(subnet->net.ipv6.address.x[2]),
296 ntohs(subnet->net.ipv6.address.x[3]),
297 ntohs(subnet->net.ipv6.address.x[4]),
298 ntohs(subnet->net.ipv6.address.x[5]),
299 ntohs(subnet->net.ipv6.address.x[6]),
300 ntohs(subnet->net.ipv6.address.x[7]),
301 subnet->net.ipv6.prefixlength);
306 _("net2str() was called with unknown subnet type %d, exiting!"),
315 /* Subnet lookup routines */
317 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
321 return splay_search(owner->subnet_tree, subnet);
324 subnet_t *lookup_subnet_mac(const mac_t *address)
326 subnet_t *p, subnet = {0};
330 subnet.type = SUBNET_MAC;
331 subnet.net.mac.address = *address;
334 p = splay_search(subnet_tree, &subnet);
339 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
341 subnet_t *p, subnet = {0};
345 subnet.type = SUBNET_IPV4;
346 subnet.net.ipv4.address = *address;
347 subnet.net.ipv4.prefixlength = 32;
353 p = splay_search_closest_smaller(subnet_tree, &subnet);
355 /* Check if the found subnet REALLY matches */
358 if(p->type != SUBNET_IPV4) {
363 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength))
366 /* Otherwise, see if there is a bigger enclosing subnet */
368 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
369 if(subnet.net.ipv4.prefixlength < 0 || subnet.net.ipv4.prefixlength > 32)
371 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address);
379 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
381 subnet_t *p, subnet = {0};
385 subnet.type = SUBNET_IPV6;
386 subnet.net.ipv6.address = *address;
387 subnet.net.ipv6.prefixlength = 128;
393 p = splay_search_closest_smaller(subnet_tree, &subnet);
395 /* Check if the found subnet REALLY matches */
398 if(p->type != SUBNET_IPV6)
401 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength))
404 /* Otherwise, see if there is a bigger enclosing subnet */
406 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
407 if(subnet.net.ipv6.prefixlength < 0 || subnet.net.ipv6.prefixlength > 128)
409 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address);
417 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
421 char netstr[MAXNETSTR + 7] = "SUBNET=";
422 char *name, *address, *port;
424 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
425 asprintf(&envp[1], "DEVICE=%s", device ? : "");
426 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
427 asprintf(&envp[3], "NODE=%s", owner->name);
429 if(owner != myself) {
430 sockaddr2str(&owner->address, &address, &port);
431 asprintf(&envp[4], "REMOTEADDRESS=%s", address);
432 asprintf(&envp[5], "REMOTEPORT=%s", port);
440 name = up ? "subnet-up" : "subnet-down";
443 for(node = owner->subnet_tree->head; node; node = node->next) {
445 if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
447 execute_script(name, envp);
450 if(net2str(netstr + 7, sizeof netstr - 7, subnet))
451 execute_script(name, envp);
454 for(i = 0; i < (owner != myself ? 6 : 4); i++)
457 if(owner != myself) {
463 int dump_subnets(struct evbuffer *out)
465 char netstr[MAXNETSTR];
471 for(node = subnet_tree->head; node; node = node->next) {
473 if(!net2str(netstr, sizeof netstr, subnet))
475 if(evbuffer_add_printf(out, _(" %s owner %s\n"),
476 netstr, subnet->owner->name) == -1)