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.31 2002/02/20 17:16:15 guus Exp $
31 #include <netinet/in.h>
45 int cache_mac_valid = 0;
46 int cache_ipv4_valid = 0;
47 int cache_ipv6_valid = 0;
49 /* lists type of subnet */
51 avl_tree_t *subnet_tree;
53 /* Subnet comparison */
55 int subnet_compare_mac(subnet_t *a, subnet_t *b)
58 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
61 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
65 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
70 return a->net.ipv4.masklength - b->net.ipv4.masklength;
73 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
77 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
82 return a->net.ipv6.masklength - b->net.ipv6.masklength;
85 int subnet_compare(subnet_t *a, subnet_t *b)
89 result = a->type - b->type;
97 return subnet_compare_mac(a, b);
99 return subnet_compare_ipv4(a, b);
101 return subnet_compare_ipv6(a, b);
103 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"), a->type);
111 /* Initialising trees */
113 void init_subnets(void)
116 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
120 void exit_subnets(void)
123 avl_delete_tree(subnet_tree);
127 avl_tree_t *new_subnet_tree(void)
130 return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
134 void free_subnet_tree(avl_tree_t *subnet_tree)
137 avl_delete_tree(subnet_tree);
141 /* Allocating and freeing space for subnets */
143 subnet_t *new_subnet(void)
146 return (subnet_t *)xmalloc(sizeof(subnet_t));
149 void free_subnet(subnet_t *subnet)
155 /* Adding and removing subnets */
157 void cache_invalidate(void)
160 cache_ipv4_valid = 0;
161 cache_ipv6_valid = 0;
164 void subnet_add(node_t *n, subnet_t *subnet)
171 avl_insert(subnet_tree, subnet);
173 avl_insert(n->subnet_tree, subnet);
177 void subnet_del(node_t *n, subnet_t *subnet)
182 avl_delete(n->subnet_tree, subnet);
184 avl_delete(subnet_tree, subnet);
188 /* Ascii representation of subnets */
190 subnet_t *str2net(char *subnetstr)
194 unsigned short int x[8];
196 subnet = new_subnet();
198 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
199 &x[0], &x[1], &x[2], &x[3],
202 subnet->type = SUBNET_IPV4;
203 subnet->net.ipv4.masklength = l;
204 for(i = 0; i < 4; i++)
205 subnet->net.ipv4.address.x[i] = x[i];
209 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
210 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
213 subnet->type = SUBNET_IPV6;
214 subnet->net.ipv6.masklength = l;
215 for(i = 0; i < 8; i++)
216 subnet->net.ipv6.address.x[i] = htons(x[i]);
220 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu",
221 &x[0], &x[1], &x[2], &x[3]) == 4)
223 subnet->type = SUBNET_IPV4;
224 subnet->net.ipv4.masklength = 32;
225 for(i = 0; i < 4; i++)
226 subnet->net.ipv4.address.x[i] = x[i];
230 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
231 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8)
233 subnet->type = SUBNET_IPV6;
234 subnet->net.ipv6.masklength = 128;
235 for(i = 0; i < 8; i++)
236 subnet->net.ipv6.address.x[i] = htons(x[i]);
240 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
241 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6)
243 subnet->type = SUBNET_MAC;
244 for(i = 0; i < 6; i++)
245 subnet->net.mac.address.x[i] = x[i];
253 char *net2str(subnet_t *subnet)
260 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
261 subnet->net.mac.address.x[0],
262 subnet->net.mac.address.x[1],
263 subnet->net.mac.address.x[2],
264 subnet->net.mac.address.x[3],
265 subnet->net.mac.address.x[4],
266 subnet->net.mac.address.x[5]);
269 asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
270 subnet->net.ipv4.address.x[0],
271 subnet->net.ipv4.address.x[1],
272 subnet->net.ipv4.address.x[2],
273 subnet->net.ipv4.address.x[3],
274 subnet->net.ipv4.masklength);
277 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
278 ntohs(subnet->net.ipv6.address.x[0]),
279 ntohs(subnet->net.ipv6.address.x[1]),
280 ntohs(subnet->net.ipv6.address.x[2]),
281 ntohs(subnet->net.ipv6.address.x[3]),
282 ntohs(subnet->net.ipv6.address.x[4]),
283 ntohs(subnet->net.ipv6.address.x[5]),
284 ntohs(subnet->net.ipv6.address.x[6]),
285 ntohs(subnet->net.ipv6.address.x[7]),
286 subnet->net.ipv6.masklength);
289 syslog(LOG_ERR, _("net2str() was called with unknown subnet type %d, exitting!"), subnet->type);
297 /* Subnet lookup routines */
299 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
302 return avl_search(owner->subnet_tree, subnet);
305 subnet_t *lookup_subnet_mac(mac_t *address)
307 static subnet_t subnet, *p;
309 if(cache_mac_valid && !memcmp(&subnet.net.mac.address, address, sizeof(mac_t)))
312 subnet.type = SUBNET_MAC;
313 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
315 p = (subnet_t *)avl_search(subnet_tree, &subnet);
321 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
323 static subnet_t subnet, *p;
325 if(cache_ipv4_valid && !memcmp(&subnet.net.ipv4.address, address, sizeof(ipv4_t)))
328 subnet.type = SUBNET_IPV4;
329 memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
330 subnet.net.ipv4.masklength = 32;
336 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
338 /* Check if the found subnet REALLY matches */
342 if(p->type != SUBNET_IPV4)
348 if (!maskcmp((char *)address, (char *)&p->net.ipv4.address, p->net.ipv4.masklength, sizeof(ipv4_t)))
352 /* Otherwise, see if there is a bigger enclosing subnet */
354 subnet.net.ipv4.masklength = p->net.ipv4.masklength - 1;
355 maskcpy((char *)&subnet.net.ipv4.address, (char *)&p->net.ipv4.address, subnet.net.ipv4.masklength, sizeof(ipv4_t));
360 memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
361 cache_ipv4_valid = 1;
366 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
368 static subnet_t subnet, *p;
370 if(cache_ipv6_valid && !memcmp(&subnet.net.ipv6.address, address, sizeof(ipv6_t)))
373 subnet.type = SUBNET_IPV6;
374 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
375 subnet.net.ipv6.masklength = 128;
381 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
383 /* Check if the found subnet REALLY matches */
388 if(p->type != SUBNET_IPV6)
391 if (!maskcmp((char *)address, (char *)&p->net.ipv6.address, p->net.ipv6.masklength, sizeof(ipv6_t)))
395 /* Otherwise, see if there is a bigger enclosing subnet */
397 subnet.net.ipv6.masklength = p->net.ipv6.masklength - 1;
398 maskcpy((char *)&subnet.net.ipv6.address, (char *)&p->net.ipv6.address, subnet.net.ipv6.masklength, sizeof(ipv6_t));
403 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
404 cache_ipv6_valid = 1;
409 void dump_subnets(void)
415 syslog(LOG_DEBUG, _("Subnet list:"));
416 for(node = subnet_tree->head; node; node = node->next)
418 subnet = (subnet_t *)node->data;
419 netstr = net2str(subnet);
420 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
423 syslog(LOG_DEBUG, _("End of subnet list."));