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.36 2002/06/08 12:57:10 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.prefixlength - b->net.ipv4.prefixlength;
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.prefixlength - b->net.ipv6.prefixlength;
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)
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.prefixlength = 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.prefixlength = 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.prefixlength = 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.prefixlength = 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.prefixlength);
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.prefixlength);
274 syslog(LOG_ERR, _("net2str() was called with unknown subnet type %d, exiting!"), 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.prefixlength = 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)
326 if (!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
330 /* Otherwise, see if there is a bigger enclosing subnet */
332 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
333 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
341 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
345 subnet.type = SUBNET_IPV6;
346 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
347 subnet.net.ipv6.prefixlength = 128;
353 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
355 /* Check if the found subnet REALLY matches */
360 if(p->type != SUBNET_IPV6)
363 if (!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
367 /* Otherwise, see if there is a bigger enclosing subnet */
369 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
370 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
378 void dump_subnets(void)
384 syslog(LOG_DEBUG, _("Subnet list:"));
385 for(node = subnet_tree->head; node; node = node->next)
387 subnet = (subnet_t *)node->data;
388 netstr = net2str(subnet);
389 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
392 syslog(LOG_DEBUG, _("End of subnet list."));