2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
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.
20 $Id: subnet.c,v 1.1.2.22 2001/06/06 19:11:16 guus Exp $
30 #include "connection.h"
38 /* lists type of subnet */
40 avl_tree_t *subnet_tree;
42 void init_subnets(void)
45 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
49 /* Subnet comparison */
51 int subnet_compare_mac(subnet_t *a, subnet_t *b)
54 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
57 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
60 /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
62 if(a->net.ipv4.address == b->net.ipv4.address)
63 return a->net.ipv4.mask - b->net.ipv4.mask;
65 return a->net.ipv4.address - b->net.ipv4.address;
68 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
71 /* Same as ipv4 case, but with nasty 128 bit addresses */
73 if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
74 if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
75 (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
76 (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
77 (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
78 (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
79 (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
80 (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
81 (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
84 return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
87 int subnet_compare(subnet_t *a, subnet_t *b)
91 x = a->type - b->type;
98 return subnet_compare_mac(a, b);
100 return subnet_compare_ipv4(a, b);
102 return subnet_compare_ipv6(a, b);
104 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
110 /* Allocating and freeing space for subnets */
112 subnet_t *new_subnet(void)
115 return (subnet_t *)xmalloc(sizeof(subnet_t));
118 void free_subnet(subnet_t *subnet)
124 /* Linked list management */
126 void subnet_add(connection_t *cl, subnet_t *subnet)
131 while(!avl_insert(subnet_tree, subnet))
135 old = (subnet_t *)avl_search(subnet_tree, subnet);
137 if(debug_lvl >= DEBUG_PROTOCOL)
140 subnetstr = net2str(subnet);
141 syslog(LOG_WARNING, _("Duplicate subnet %s for %s (%s), previous owner %s (%s)!"),
142 subnetstr, cl->name, cl->hostname, old->owner->name, old->owner->hostname);
149 avl_insert(cl->subnet_tree, subnet);
153 void subnet_del(subnet_t *subnet)
156 avl_delete(subnet->owner->subnet_tree, subnet);
158 avl_delete(subnet_tree, subnet);
162 /* Ascii representation of subnets */
164 subnet_t *str2net(char *subnetstr)
169 if(sscanf(subnetstr, "%d,", &type) != 1)
172 subnet = new_subnet();
177 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
178 &subnet->net.mac.address.x[0],
179 &subnet->net.mac.address.x[1],
180 &subnet->net.mac.address.x[2],
181 &subnet->net.mac.address.x[3],
182 &subnet->net.mac.address.x[4],
183 &subnet->net.mac.address.x[5]) != 7)
190 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
197 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
198 &subnet->net.ipv6.address.x[0],
199 &subnet->net.ipv6.address.x[1],
200 &subnet->net.ipv6.address.x[2],
201 &subnet->net.ipv6.address.x[3],
202 &subnet->net.ipv6.address.x[4],
203 &subnet->net.ipv6.address.x[5],
204 &subnet->net.ipv6.address.x[6],
205 &subnet->net.ipv6.address.x[7],
206 &subnet->net.ipv6.mask.x[0],
207 &subnet->net.ipv6.mask.x[1],
208 &subnet->net.ipv6.mask.x[2],
209 &subnet->net.ipv6.mask.x[3],
210 &subnet->net.ipv6.mask.x[4],
211 &subnet->net.ipv6.mask.x[5],
212 &subnet->net.ipv6.mask.x[6],
213 &subnet->net.ipv6.mask.x[7]) != 17)
227 char *net2str(subnet_t *subnet)
234 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
235 subnet->net.mac.address.x[0],
236 subnet->net.mac.address.x[1],
237 subnet->net.mac.address.x[2],
238 subnet->net.mac.address.x[3],
239 subnet->net.mac.address.x[4],
240 subnet->net.mac.address.x[5]);
243 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
246 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
247 subnet->net.ipv6.address.x[0],
248 subnet->net.ipv6.address.x[1],
249 subnet->net.ipv6.address.x[2],
250 subnet->net.ipv6.address.x[3],
251 subnet->net.ipv6.address.x[4],
252 subnet->net.ipv6.address.x[5],
253 subnet->net.ipv6.address.x[6],
254 subnet->net.ipv6.address.x[7],
255 subnet->net.ipv6.mask.x[0],
256 subnet->net.ipv6.mask.x[1],
257 subnet->net.ipv6.mask.x[2],
258 subnet->net.ipv6.mask.x[3],
259 subnet->net.ipv6.mask.x[4],
260 subnet->net.ipv6.mask.x[5],
261 subnet->net.ipv6.mask.x[6],
262 subnet->net.ipv6.mask.x[7]);
265 asprintf(&netstr, _("unknown subnet type"));
271 /* Subnet lookup routines */
273 subnet_t *lookup_subnet_mac(mac_t *address)
277 subnet.type = SUBNET_MAC;
278 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
280 p = (subnet_t *)avl_search(subnet_tree, &subnet);
285 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
289 subnet.type = SUBNET_IPV4;
290 subnet.net.ipv4.address = *address;
291 subnet.net.ipv4.mask = 0xFFFFFFFF;
297 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
299 /* Check if the found subnet REALLY matches */
303 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
307 /* Otherwise, see if there is a bigger enclosing subnet */
309 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
310 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
318 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
323 subnet.type = SUBNET_IPV6;
324 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
325 memset(&subnet.net.ipv6.mask, 0xFF, 16);
327 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
331 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
337 void dump_subnet_list(void)
343 syslog(LOG_DEBUG, _("Subnet list:"));
344 for(node = subnet_tree->head; node; node = node->next)
346 subnet = (subnet_t *)node->data;
347 netstr = net2str(subnet);
348 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
351 syslog(LOG_DEBUG, _("End of subnet list."));