X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fsubnet.c;h=537be117520913413a295a7200e4a7dc23ad5415;hp=28a203dc21ca66b2e2f91a4db3c7aef49a3d44e5;hb=ce6c8e6d089abac81520c517185c6ef81b09f051;hpb=e924096f62655d711cd2d114a8d1ef0fecbb593b diff --git a/src/subnet.c b/src/subnet.c index 28a203dc..537be117 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -1,7 +1,7 @@ /* subnet.c -- handle subnet lookups and lists - Copyright (C) 2000 Guus Sliepen , - 2000 Ivo Timmermans + Copyright (C) 2000,2001 Guus Sliepen , + 2000,2001 Ivo Timmermans This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: subnet.c,v 1.1.2.16 2001/01/05 23:53:53 guus Exp $ + $Id: subnet.c,v 1.1.2.22 2001/06/06 19:11:16 guus Exp $ */ #include "config.h" @@ -57,16 +57,12 @@ cp int subnet_compare_ipv4(subnet_t *a, subnet_t *b) { cp - /* If the subnet of a falls within the range of subnet b, - then we consider a smaller then b. - Otherwise, the addresses alone (and not the subnet masks) will be compared. - */ + /* We compare as if a subnet is a number that equals (address << 32 + netmask). */ - if(a->net.ipv4.mask > b->net.ipv4.mask) - if((a->net.ipv4.address & b->net.ipv4.mask) == b->net.ipv4.address) - return -1; - - return a->net.ipv4.address - b->net.ipv4.address; + if(a->net.ipv4.address == b->net.ipv4.address) + return a->net.ipv4.mask - b->net.ipv4.mask; + else + return a->net.ipv4.address - b->net.ipv4.address; } int subnet_compare_ipv6(subnet_t *a, subnet_t *b) @@ -131,7 +127,25 @@ void subnet_add(connection_t *cl, subnet_t *subnet) { cp subnet->owner = cl; - avl_insert(subnet_tree, subnet); + + while(!avl_insert(subnet_tree, subnet)) + { + subnet_t *old; + + old = (subnet_t *)avl_search(subnet_tree, subnet); + + if(debug_lvl >= DEBUG_PROTOCOL) + { + char *subnetstr; + subnetstr = net2str(subnet); + syslog(LOG_WARNING, _("Duplicate subnet %s for %s (%s), previous owner %s (%s)!"), + subnetstr, cl->name, cl->hostname, old->owner->name, old->owner->hostname); + free(subnetstr); + } + + subnet_del(old); + } + avl_insert(cl->subnet_tree, subnet); cp } @@ -248,7 +262,7 @@ cp subnet->net.ipv6.mask.x[7]); break; default: - asprintf(&netstr, _("unknown")); + asprintf(&netstr, _("unknown subnet type")); } cp return netstr; @@ -276,27 +290,48 @@ cp subnet.net.ipv4.address = *address; subnet.net.ipv4.mask = 0xFFFFFFFF; - p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet); + do + { + /* Go find subnet */ + + p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet); /* Check if the found subnet REALLY matches */ cp - if(p && ((*address & p->net.ipv4.mask) == p->net.ipv4.address)) - return p; - else - return NULL; + if(p) + { + if ((*address & p->net.ipv4.mask) == p->net.ipv4.address) + break; + else + { + /* Otherwise, see if there is a bigger enclosing subnet */ + + subnet.net.ipv4.mask = p->net.ipv4.mask << 1; + subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask; + } + } + } while (p); + + return p; } subnet_t *lookup_subnet_ipv6(ipv6_t *address) { - subnet_t subnet; + subnet_t subnet, *p; + int i; cp subnet.type = SUBNET_IPV6; memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t)); memset(&subnet.net.ipv6.mask, 0xFF, 16); -/* FIXME: check if it REALLY matches */ + p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet); + + if(p) + for(i=0; i<8; i++) + if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i]) + return NULL; - return (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet); + return p; } void dump_subnet_list(void)