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.38 2002/07/10 11:32:33 guus Exp $
31 #include <netinet/in.h>
45 /* lists type of subnet */
47 avl_tree_t *subnet_tree;
49 /* Subnet comparison */
51 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 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 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 int subnet_compare(subnet_t *a, subnet_t *b)
101 result = a->type - b->type;
109 return subnet_compare_mac(a, b);
111 return subnet_compare_ipv4(a, b);
113 return subnet_compare_ipv6(a, b);
115 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"), a->type);
123 /* Initialising trees */
125 void init_subnets(void)
128 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
132 void exit_subnets(void)
135 avl_delete_tree(subnet_tree);
139 avl_tree_t *new_subnet_tree(void)
142 return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
146 void free_subnet_tree(avl_tree_t *subnet_tree)
149 avl_delete_tree(subnet_tree);
153 /* Allocating and freeing space for subnets */
155 subnet_t *new_subnet(void)
158 return (subnet_t *)xmalloc(sizeof(subnet_t));
161 void free_subnet(subnet_t *subnet)
167 /* Adding and removing subnets */
169 void subnet_add(node_t *n, subnet_t *subnet)
174 avl_insert(subnet_tree, subnet);
176 avl_insert(n->subnet_tree, subnet);
180 void subnet_del(node_t *n, subnet_t *subnet)
183 avl_delete(n->subnet_tree, subnet);
185 avl_delete(subnet_tree, subnet);
189 /* Ascii representation of subnets */
191 subnet_t *str2net(char *subnetstr)
197 subnet = new_subnet();
199 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
200 &x[0], &x[1], &x[2], &x[3],
203 subnet->type = SUBNET_IPV4;
204 subnet->net.ipv4.prefixlength = l;
205 for(i = 0; i < 4; i++)
206 subnet->net.ipv4.address.x[i] = x[i];
210 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
211 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
214 subnet->type = SUBNET_IPV6;
215 subnet->net.ipv6.prefixlength = l;
216 for(i = 0; i < 8; i++)
217 subnet->net.ipv6.address.x[i] = htons(x[i]);
221 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu",
222 &x[0], &x[1], &x[2], &x[3]) == 4)
224 subnet->type = SUBNET_IPV4;
225 subnet->net.ipv4.prefixlength = 32;
226 for(i = 0; i < 4; i++)
227 subnet->net.ipv4.address.x[i] = x[i];
231 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
232 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8)
234 subnet->type = SUBNET_IPV6;
235 subnet->net.ipv6.prefixlength = 128;
236 for(i = 0; i < 8; i++)
237 subnet->net.ipv6.address.x[i] = htons(x[i]);
241 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
242 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6)
244 subnet->type = SUBNET_MAC;
245 for(i = 0; i < 6; i++)
246 subnet->net.mac.address.x[i] = x[i];
254 char *net2str(subnet_t *subnet)
261 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
262 subnet->net.mac.address.x[0],
263 subnet->net.mac.address.x[1],
264 subnet->net.mac.address.x[2],
265 subnet->net.mac.address.x[3],
266 subnet->net.mac.address.x[4],
267 subnet->net.mac.address.x[5]);
270 asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
271 subnet->net.ipv4.address.x[0],
272 subnet->net.ipv4.address.x[1],
273 subnet->net.ipv4.address.x[2],
274 subnet->net.ipv4.address.x[3],
275 subnet->net.ipv4.prefixlength);
278 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
279 ntohs(subnet->net.ipv6.address.x[0]),
280 ntohs(subnet->net.ipv6.address.x[1]),
281 ntohs(subnet->net.ipv6.address.x[2]),
282 ntohs(subnet->net.ipv6.address.x[3]),
283 ntohs(subnet->net.ipv6.address.x[4]),
284 ntohs(subnet->net.ipv6.address.x[5]),
285 ntohs(subnet->net.ipv6.address.x[6]),
286 ntohs(subnet->net.ipv6.address.x[7]),
287 subnet->net.ipv6.prefixlength);
290 syslog(LOG_ERR, _("net2str() was called with unknown subnet type %d, exiting!"), subnet->type);
298 /* Subnet lookup routines */
300 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
303 return avl_search(owner->subnet_tree, subnet);
306 subnet_t *lookup_subnet_mac(mac_t *address)
310 subnet.type = SUBNET_MAC;
311 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
314 p = (subnet_t *)avl_search(subnet_tree, &subnet);
319 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
323 subnet.type = SUBNET_IPV4;
324 memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
325 subnet.net.ipv4.prefixlength = 32;
332 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
334 /* Check if the found subnet REALLY matches */
338 if(p->type != SUBNET_IPV4)
344 if (!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
348 /* Otherwise, see if there is a bigger enclosing subnet */
350 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
351 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
359 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
363 subnet.type = SUBNET_IPV6;
364 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
365 subnet.net.ipv6.prefixlength = 128;
372 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
374 /* Check if the found subnet REALLY matches */
379 if(p->type != SUBNET_IPV6)
382 if (!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
386 /* Otherwise, see if there is a bigger enclosing subnet */
388 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
389 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
397 void dump_subnets(void)
403 syslog(LOG_DEBUG, _("Subnet list:"));
404 for(node = subnet_tree->head; node; node = node->next)
406 subnet = (subnet_t *)node->data;
407 netstr = net2str(subnet);
408 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
411 syslog(LOG_DEBUG, _("End of subnet list."));