2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
4 2000-2002 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.30 2002/02/18 16:25:19 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)
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)
61 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
66 return a->net.ipv4.masklength - b->net.ipv4.masklength;
69 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
73 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
78 return a->net.ipv6.masklength - b->net.ipv6.masklength;
81 int subnet_compare(subnet_t *a, subnet_t *b)
85 result = a->type - b->type;
93 return subnet_compare_mac(a, b);
95 return subnet_compare_ipv4(a, b);
97 return subnet_compare_ipv6(a, b);
99 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"), a->type);
107 /* Initialising trees */
109 void init_subnets(void)
112 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
116 void exit_subnets(void)
119 avl_delete_tree(subnet_tree);
123 avl_tree_t *new_subnet_tree(void)
126 return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
130 void free_subnet_tree(avl_tree_t *subnet_tree)
133 avl_delete_tree(subnet_tree);
137 /* Allocating and freeing space for subnets */
139 subnet_t *new_subnet(void)
142 return (subnet_t *)xmalloc(sizeof(subnet_t));
145 void free_subnet(subnet_t *subnet)
151 /* Adding and removing subnets */
153 void subnet_add(node_t *n, subnet_t *subnet)
158 avl_insert(subnet_tree, subnet);
160 avl_insert(n->subnet_tree, subnet);
164 void subnet_del(node_t *n, subnet_t *subnet)
167 avl_delete(n->subnet_tree, subnet);
169 avl_delete(subnet_tree, subnet);
173 /* Ascii representation of subnets */
175 subnet_t *str2net(char *subnetstr)
179 unsigned short int x[8];
181 subnet = new_subnet();
183 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
184 &x[0], &x[1], &x[2], &x[3],
187 subnet->type = SUBNET_IPV4;
188 subnet->net.ipv4.masklength = l;
189 for(i = 0; i < 4; i++)
190 subnet->net.ipv4.address.x[i] = x[i];
194 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
195 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
198 subnet->type = SUBNET_IPV6;
199 subnet->net.ipv6.masklength = l;
200 for(i = 0; i < 8; i++)
201 subnet->net.ipv6.address.x[i] = htons(x[i]);
205 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu",
206 &x[0], &x[1], &x[2], &x[3]) == 4)
208 subnet->type = SUBNET_IPV4;
209 subnet->net.ipv4.masklength = 32;
210 for(i = 0; i < 4; i++)
211 subnet->net.ipv4.address.x[i] = x[i];
215 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
216 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8)
218 subnet->type = SUBNET_IPV6;
219 subnet->net.ipv6.masklength = 128;
220 for(i = 0; i < 8; i++)
221 subnet->net.ipv6.address.x[i] = htons(x[i]);
225 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
226 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6)
228 subnet->type = SUBNET_MAC;
229 for(i = 0; i < 6; i++)
230 subnet->net.mac.address.x[i] = x[i];
238 char *net2str(subnet_t *subnet)
245 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
246 subnet->net.mac.address.x[0],
247 subnet->net.mac.address.x[1],
248 subnet->net.mac.address.x[2],
249 subnet->net.mac.address.x[3],
250 subnet->net.mac.address.x[4],
251 subnet->net.mac.address.x[5]);
254 asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
255 subnet->net.ipv4.address.x[0],
256 subnet->net.ipv4.address.x[1],
257 subnet->net.ipv4.address.x[2],
258 subnet->net.ipv4.address.x[3],
259 subnet->net.ipv4.masklength);
262 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
263 ntohs(subnet->net.ipv6.address.x[0]),
264 ntohs(subnet->net.ipv6.address.x[1]),
265 ntohs(subnet->net.ipv6.address.x[2]),
266 ntohs(subnet->net.ipv6.address.x[3]),
267 ntohs(subnet->net.ipv6.address.x[4]),
268 ntohs(subnet->net.ipv6.address.x[5]),
269 ntohs(subnet->net.ipv6.address.x[6]),
270 ntohs(subnet->net.ipv6.address.x[7]),
271 subnet->net.ipv6.masklength);
274 syslog(LOG_ERR, _("net2str() was called with unknown subnet type %d, exitting!"), subnet->type);
282 /* Subnet lookup routines */
284 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
287 return avl_search(owner->subnet_tree, subnet);
290 subnet_t *lookup_subnet_mac(mac_t *address)
294 subnet.type = SUBNET_MAC;
295 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
297 p = (subnet_t *)avl_search(subnet_tree, &subnet);
302 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
306 subnet.type = SUBNET_IPV4;
307 memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
308 subnet.net.ipv4.masklength = 32;
314 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
316 /* Check if the found subnet REALLY matches */
320 if(p->type != SUBNET_IPV4)
323 if (!maskcmp((char *)address, (char *)&p->net.ipv4.address, p->net.ipv4.masklength, sizeof(ipv4_t)))
327 /* Otherwise, see if there is a bigger enclosing subnet */
329 subnet.net.ipv4.masklength = p->net.ipv4.masklength - 1;
330 maskcpy((char *)&subnet.net.ipv4.address, (char *)&p->net.ipv4.address, subnet.net.ipv4.masklength, sizeof(ipv4_t));
338 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
342 subnet.type = SUBNET_IPV6;
343 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
344 subnet.net.ipv6.masklength = 128;
350 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
352 /* Check if the found subnet REALLY matches */
357 if(p->type != SUBNET_IPV6)
360 if (!maskcmp((char *)address, (char *)&p->net.ipv6.address, p->net.ipv6.masklength, sizeof(ipv6_t)))
364 /* Otherwise, see if there is a bigger enclosing subnet */
366 subnet.net.ipv6.masklength = p->net.ipv6.masklength - 1;
367 maskcpy((char *)&subnet.net.ipv6.address, (char *)&p->net.ipv6.address, subnet.net.ipv6.masklength, sizeof(ipv6_t));
375 void dump_subnets(void)
381 syslog(LOG_DEBUG, _("Subnet list:"));
382 for(node = subnet_tree->head; node; node = node->next)
384 subnet = (subnet_t *)node->data;
385 netstr = net2str(subnet);
386 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
389 syslog(LOG_DEBUG, _("End of subnet list."));