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.27 2001/10/28 22:42:49 guus Exp $
39 /* lists type of subnet */
41 avl_tree_t *subnet_tree;
43 /* Subnet comparison */
45 int subnet_compare_mac(subnet_t *a, subnet_t *b)
49 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
54 return strcmp(a->owner->name, b->owner->name);
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)
64 else if(a->net.ipv4.address > b->net.ipv4.address)
67 if(a->net.ipv4.mask < b->net.ipv4.mask)
69 else if(a->net.ipv4.mask > b->net.ipv4.mask)
72 return strcmp(a->owner->name, b->owner->name);
75 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
79 /* Same as ipv4 case, but with nasty 128 bit addresses */
81 result = memcmp(a->net.ipv6.address.x, b->net.ipv6.address.x, sizeof(ipv6_t));
86 result = memcmp(a->net.ipv6.mask.x, b->net.ipv6.mask.x, sizeof(ipv6_t));
91 return strcmp(a->owner->name, b->owner->name);
94 int subnet_compare(subnet_t *a, subnet_t *b)
98 x = a->type - b->type;
105 return subnet_compare_mac(a, b);
107 return subnet_compare_ipv4(a, b);
109 return subnet_compare_ipv6(a, b);
111 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
117 /* Initialising trees */
119 void init_subnets(void)
122 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
126 void exit_subnets(void)
129 avl_delete_tree(subnet_tree);
133 avl_tree_t *new_subnet_tree(void)
136 return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
140 void free_subnet_tree(avl_tree_t *subnet_tree)
143 avl_delete_tree(subnet_tree);
147 /* Allocating and freeing space for subnets */
149 subnet_t *new_subnet(void)
152 return (subnet_t *)xmalloc(sizeof(subnet_t));
155 void free_subnet(subnet_t *subnet)
161 /* Linked list management */
163 void subnet_add(node_t *n, subnet_t *subnet)
168 avl_insert(subnet_tree, subnet);
170 avl_insert(n->subnet_tree, subnet);
174 void subnet_del(node_t *n, subnet_t *subnet)
177 avl_delete(n->subnet_tree, subnet);
179 avl_delete(subnet_tree, subnet);
183 /* Ascii representation of subnets */
185 subnet_t *str2net(char *subnetstr)
190 if(sscanf(subnetstr, "%d,", &type) != 1)
193 subnet = new_subnet();
198 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
199 &subnet->net.mac.address.x[0],
200 &subnet->net.mac.address.x[1],
201 &subnet->net.mac.address.x[2],
202 &subnet->net.mac.address.x[3],
203 &subnet->net.mac.address.x[4],
204 &subnet->net.mac.address.x[5]) != 7)
211 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
218 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
219 &subnet->net.ipv6.address.x[0],
220 &subnet->net.ipv6.address.x[1],
221 &subnet->net.ipv6.address.x[2],
222 &subnet->net.ipv6.address.x[3],
223 &subnet->net.ipv6.address.x[4],
224 &subnet->net.ipv6.address.x[5],
225 &subnet->net.ipv6.address.x[6],
226 &subnet->net.ipv6.address.x[7],
227 &subnet->net.ipv6.mask.x[0],
228 &subnet->net.ipv6.mask.x[1],
229 &subnet->net.ipv6.mask.x[2],
230 &subnet->net.ipv6.mask.x[3],
231 &subnet->net.ipv6.mask.x[4],
232 &subnet->net.ipv6.mask.x[5],
233 &subnet->net.ipv6.mask.x[6],
234 &subnet->net.ipv6.mask.x[7]) != 17)
248 char *net2str(subnet_t *subnet)
255 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
256 subnet->net.mac.address.x[0],
257 subnet->net.mac.address.x[1],
258 subnet->net.mac.address.x[2],
259 subnet->net.mac.address.x[3],
260 subnet->net.mac.address.x[4],
261 subnet->net.mac.address.x[5]);
264 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
267 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
268 subnet->net.ipv6.address.x[0],
269 subnet->net.ipv6.address.x[1],
270 subnet->net.ipv6.address.x[2],
271 subnet->net.ipv6.address.x[3],
272 subnet->net.ipv6.address.x[4],
273 subnet->net.ipv6.address.x[5],
274 subnet->net.ipv6.address.x[6],
275 subnet->net.ipv6.address.x[7],
276 subnet->net.ipv6.mask.x[0],
277 subnet->net.ipv6.mask.x[1],
278 subnet->net.ipv6.mask.x[2],
279 subnet->net.ipv6.mask.x[3],
280 subnet->net.ipv6.mask.x[4],
281 subnet->net.ipv6.mask.x[5],
282 subnet->net.ipv6.mask.x[6],
283 subnet->net.ipv6.mask.x[7]);
286 asprintf(&netstr, _("unknown subnet type"));
292 /* Subnet lookup routines */
294 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
297 return avl_search(owner->subnet_tree, subnet);
300 subnet_t *lookup_subnet_mac(mac_t *address)
304 subnet.type = SUBNET_MAC;
305 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
307 p = (subnet_t *)avl_search(subnet_tree, &subnet);
312 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
316 subnet.type = SUBNET_IPV4;
317 subnet.net.ipv4.address = *address;
318 subnet.net.ipv4.mask = 0xFFFFFFFF;
324 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
326 /* Check if the found subnet REALLY matches */
330 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
334 /* Otherwise, see if there is a bigger enclosing subnet */
336 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
337 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
345 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
350 subnet.type = SUBNET_IPV6;
351 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
352 memset(&subnet.net.ipv6.mask, 0xFF, 16);
354 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
358 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
364 void dump_subnets(void)
370 syslog(LOG_DEBUG, _("Subnet list:"));
371 for(node = subnet_tree->head; node; node = node->next)
373 subnet = (subnet_t *)node->data;
374 netstr = net2str(subnet);
375 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
378 syslog(LOG_DEBUG, _("End of subnet list."));