2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.eu.org>,
4 2000-2002 Ivo Timmermans <ivo@o2w.nl>
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.45 2003/07/06 23:16:29 guus Exp $
30 #include <netinet/in.h>
45 /* lists type of subnet */
47 avl_tree_t *subnet_tree;
49 /* Subnet comparison */
51 static int subnet_compare_mac(subnet_t *a, subnet_t *b)
55 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
57 if(result || !a->owner || !b->owner)
60 return strcmp(a->owner->name, b->owner->name);
63 static int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
67 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
72 result = a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
74 if(result || !a->owner || !b->owner)
77 return strcmp(a->owner->name, b->owner->name);
80 static int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
84 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
89 result = a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
91 if(result || !a->owner || !b->owner)
94 return strcmp(a->owner->name, b->owner->name);
97 static int subnet_compare(subnet_t *a, subnet_t *b)
101 result = a->type - b->type;
108 return subnet_compare_mac(a, b);
110 return subnet_compare_ipv4(a, b);
112 return subnet_compare_ipv6(a, b);
114 logger(DEBUG_ALWAYS, LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
123 /* Initialising trees */
125 void init_subnets(void)
129 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
132 void exit_subnets(void)
136 avl_delete_tree(subnet_tree);
139 avl_tree_t *new_subnet_tree(void)
143 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
146 void free_subnet_tree(avl_tree_t *subnet_tree)
150 avl_delete_tree(subnet_tree);
153 /* Allocating and freeing space for subnets */
155 subnet_t *new_subnet(void)
159 return (subnet_t *) xmalloc_and_zero(sizeof(subnet_t));
162 void free_subnet(subnet_t *subnet)
169 /* Adding and removing subnets */
171 void subnet_add(node_t *n, subnet_t *subnet)
177 avl_insert(subnet_tree, subnet);
178 avl_insert(n->subnet_tree, subnet);
181 void subnet_del(node_t *n, subnet_t *subnet)
185 avl_delete(n->subnet_tree, subnet);
186 avl_delete(subnet_tree, subnet);
189 /* Ascii representation of subnets */
191 subnet_t *str2net(char *subnetstr)
199 subnet = new_subnet();
201 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
202 &x[0], &x[1], &x[2], &x[3], &l) == 5) {
203 subnet->type = SUBNET_IPV4;
204 subnet->net.ipv4.prefixlength = l;
206 for(i = 0; i < 4; i++)
207 subnet->net.ipv4.address.x[i] = x[i];
212 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
213 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
215 subnet->type = SUBNET_IPV6;
216 subnet->net.ipv6.prefixlength = l;
218 for(i = 0; i < 8; i++)
219 subnet->net.ipv6.address.x[i] = htons(x[i]);
224 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu", &x[0], &x[1], &x[2], &x[3]) == 4) {
225 subnet->type = SUBNET_IPV4;
226 subnet->net.ipv4.prefixlength = 32;
228 for(i = 0; i < 4; i++)
229 subnet->net.ipv4.address.x[i] = x[i];
234 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
235 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
236 subnet->type = SUBNET_IPV6;
237 subnet->net.ipv6.prefixlength = 128;
239 for(i = 0; i < 8; i++)
240 subnet->net.ipv6.address.x[i] = htons(x[i]);
245 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
246 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
247 subnet->type = SUBNET_MAC;
249 for(i = 0; i < 6; i++)
250 subnet->net.mac.address.x[i] = x[i];
260 char *net2str(subnet_t *subnet)
266 switch (subnet->type) {
268 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
269 subnet->net.mac.address.x[0],
270 subnet->net.mac.address.x[1],
271 subnet->net.mac.address.x[2],
272 subnet->net.mac.address.x[3],
273 subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
277 asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
278 subnet->net.ipv4.address.x[0],
279 subnet->net.ipv4.address.x[1],
280 subnet->net.ipv4.address.x[2],
281 subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
285 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
286 ntohs(subnet->net.ipv6.address.x[0]),
287 ntohs(subnet->net.ipv6.address.x[1]),
288 ntohs(subnet->net.ipv6.address.x[2]),
289 ntohs(subnet->net.ipv6.address.x[3]),
290 ntohs(subnet->net.ipv6.address.x[4]),
291 ntohs(subnet->net.ipv6.address.x[5]),
292 ntohs(subnet->net.ipv6.address.x[6]),
293 ntohs(subnet->net.ipv6.address.x[7]),
294 subnet->net.ipv6.prefixlength);
298 logger(DEBUG_ALWAYS, LOG_ERR,
299 _("net2str() was called with unknown subnet type %d, exiting!"),
308 /* Subnet lookup routines */
310 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
314 return avl_search(owner->subnet_tree, subnet);
317 subnet_t *lookup_subnet_mac(mac_t *address)
323 subnet.type = SUBNET_MAC;
324 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
327 p = (subnet_t *) avl_search(subnet_tree, &subnet);
332 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
338 subnet.type = SUBNET_IPV4;
339 memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
340 subnet.net.ipv4.prefixlength = 32;
346 p = (subnet_t *) avl_search_closest_smaller(subnet_tree, &subnet);
348 /* Check if the found subnet REALLY matches */
351 if(p->type != SUBNET_IPV4) {
356 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
359 /* Otherwise, see if there is a bigger enclosing subnet */
361 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
362 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
370 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
376 subnet.type = SUBNET_IPV6;
377 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
378 subnet.net.ipv6.prefixlength = 128;
384 p = (subnet_t *) avl_search_closest_smaller(subnet_tree, &subnet);
386 /* Check if the found subnet REALLY matches */
389 if(p->type != SUBNET_IPV6)
392 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
395 /* Otherwise, see if there is a bigger enclosing subnet */
397 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
398 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
406 void dump_subnets(void)
414 logger(DEBUG_ALWAYS, LOG_DEBUG, _("Subnet list:"));
416 for(node = subnet_tree->head; node; node = node->next) {
417 subnet = (subnet_t *) node->data;
418 netstr = net2str(subnet);
419 logger(DEBUG_ALWAYS, LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
423 logger(DEBUG_ALWAYS, LOG_DEBUG, _("End of subnet list."));