+
+subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
+{
+ cp();
+
+ return avl_search(owner->subnet_tree, subnet);
+}
+
+subnet_t *lookup_subnet_mac(const mac_t *address)
+{
+ subnet_t *p, subnet = {0};
+
+ cp();
+
+ subnet.type = SUBNET_MAC;
+ subnet.net.mac.address = *address;
+ subnet.owner = NULL;
+
+ p = avl_search(subnet_tree, &subnet);
+
+ return p;
+}
+
+subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
+{
+ subnet_t *p, subnet = {0};
+
+ cp();
+
+ subnet.type = SUBNET_IPV4;
+ subnet.net.ipv4.address = *address;
+ subnet.net.ipv4.prefixlength = 32;
+ subnet.owner = NULL;
+
+ do {
+ /* Go find subnet */
+
+ p = avl_search_closest_smaller(subnet_tree, &subnet);
+
+ /* Check if the found subnet REALLY matches */
+
+ if(p) {
+ if(p->type != SUBNET_IPV4) {
+ p = NULL;
+ break;
+ }
+
+ if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
+ break;
+ else {
+ /* Otherwise, see if there is a bigger enclosing subnet */
+
+ subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
+ maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
+ }
+ }
+ } while(p);
+
+ return p;
+}
+
+subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
+{
+ subnet_t *p, subnet = {0};
+
+ cp();
+
+ subnet.type = SUBNET_IPV6;
+ subnet.net.ipv6.address = *address;
+ subnet.net.ipv6.prefixlength = 128;
+ subnet.owner = NULL;
+
+ do {
+ /* Go find subnet */
+
+ p = avl_search_closest_smaller(subnet_tree, &subnet);
+
+ /* Check if the found subnet REALLY matches */
+
+ if(p) {
+ if(p->type != SUBNET_IPV6)
+ return NULL;
+
+ if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
+ break;
+ else {
+ /* Otherwise, see if there is a bigger enclosing subnet */
+
+ subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
+ maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
+ }
+ }
+ } while(p);
+
+ return p;
+}
+
+void dump_subnets(void)
+{
+ char netstr[MAXNETSTR];
+ subnet_t *subnet;
+ avl_node_t *node;
+
+ cp();
+
+ logger(LOG_DEBUG, _("Subnet list:"));
+
+ for(node = subnet_tree->head; node; node = node->next) {
+ subnet = node->data;
+ if(!net2str(netstr, sizeof netstr, subnet))
+ continue;
+ logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
+ }
+
+ logger(LOG_DEBUG, _("End of subnet list."));
+}