X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fsubnet.c;h=2e1a24af6e218305f62ce4667f60fa97ced929b3;hp=b2ced415934514b3636b3dbf60c91fe111f2ede8;hb=edd6734faa37d043b8a2cc75b125db3b1c2130fa;hpb=408ca91766088b6c2d38e198b0692bf394b41248 diff --git a/src/subnet.c b/src/subnet.c index b2ced415..2e1a24af 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.12 2000/11/20 19:12:17 guus Exp $ + $Id: subnet.c,v 1.1.2.21 2001/06/05 18:07:14 guus Exp $ */ #include "config.h" @@ -33,12 +33,18 @@ #include #include -#include +#include /* lists type of subnet */ -rbltree_t _subnet_tree = { NULL }; -rbltree_t *subnet_tree = &_subnet_tree; +avl_tree_t *subnet_tree; + +void init_subnets(void) +{ +cp + subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet); +cp +} /* Subnet comparison */ @@ -51,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) @@ -124,16 +126,18 @@ cp void subnet_add(connection_t *cl, subnet_t *subnet) { cp - rbl_insert(subnet_tree, subnet); - rbl_insert(cl->subnet_tree, subnet); + subnet->owner = cl; + avl_insert(subnet_tree, subnet); + avl_insert(cl->subnet_tree, subnet); cp } void subnet_del(subnet_t *subnet) { cp - free_rbl(rbl_unlink(subnet->owner->subnet_tree, subnet)); - rbl_delete(subnet_tree, subnet); + avl_delete(subnet->owner->subnet_tree, subnet); +cp + avl_delete(subnet_tree, subnet); cp } @@ -240,7 +244,7 @@ cp subnet->net.ipv6.mask.x[7]); break; default: - asprintf(&netstr, _("unknown")); + asprintf(&netstr, _("unknown subnet type")); } cp return netstr; @@ -248,45 +252,80 @@ cp /* Subnet lookup routines */ -subnet_t *lookup_subnet_mac(mac_t address) +subnet_t *lookup_subnet_mac(mac_t *address) { - subnet_t subnet; + subnet_t subnet, *p; cp subnet.type = SUBNET_MAC; - subnet.net.mac.address = address; - return (subnet_t *)rbl_search_closest(subnet_tree, &subnet); + memcpy(&subnet.net.mac.address, address, sizeof(mac_t)); + + p = (subnet_t *)avl_search(subnet_tree, &subnet); +cp + return p; } -subnet_t *lookup_subnet_ipv4(ipv4_t address) +subnet_t *lookup_subnet_ipv4(ipv4_t *address) { - subnet_t subnet; + subnet_t subnet, *p; cp subnet.type = SUBNET_IPV4; - subnet.net.ipv4.address = address; + subnet.net.ipv4.address = *address; subnet.net.ipv4.mask = 0xFFFFFFFF; - return (subnet_t *)rbl_search_closest(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) + { + 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 *lookup_subnet_ipv6(ipv6_t *address) { - subnet_t subnet; + subnet_t subnet, *p; + int i; cp subnet.type = SUBNET_IPV6; - subnet.net.ipv6.address = address; + memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t)); memset(&subnet.net.ipv6.mask, 0xFF, 16); - return (subnet_t *)rbl_search_closest(subnet_tree, &subnet); + + 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 p; } void dump_subnet_list(void) { char *netstr; subnet_t *subnet; - rbl_t *rbl; + avl_node_t *node; cp syslog(LOG_DEBUG, _("Subnet list:")); - RBL_FOREACH(subnet_tree, rbl) + for(node = subnet_tree->head; node; node = node->next) { - subnet = (subnet_t *)rbl->data; + subnet = (subnet_t *)node->data; netstr = net2str(subnet); syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name); free(netstr);