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 2002/04/28 12:46:26 zarq Exp $
30 #include <netinet/in.h>
46 /* lists type of subnet */
48 avl_tree_t *subnet_tree;
50 /* Subnet comparison */
52 int subnet_compare_mac(subnet_t *a, subnet_t *b)
55 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
58 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
62 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
67 return a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
70 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
74 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
79 return a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
82 int subnet_compare(subnet_t *a, subnet_t *b)
86 result = a->type - b->type;
94 return subnet_compare_mac(a, b);
96 return subnet_compare_ipv4(a, b);
98 return subnet_compare_ipv6(a, b);
100 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"), a->type);
108 /* Initialising trees */
110 void init_subnets(void)
113 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
117 void exit_subnets(void)
120 avl_delete_tree(subnet_tree);
124 avl_tree_t *new_subnet_tree(void)
127 return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
131 void free_subnet_tree(avl_tree_t *subnet_tree)
134 avl_delete_tree(subnet_tree);
138 /* Allocating and freeing space for subnets */
140 subnet_t *new_subnet(void)
143 return (subnet_t *)xmalloc(sizeof(subnet_t));
146 void free_subnet(subnet_t *subnet)
152 /* Adding and removing subnets */
154 void subnet_add(node_t *n, subnet_t *subnet)
159 avl_insert(subnet_tree, subnet);
161 avl_insert(n->subnet_tree, subnet);
163 run_hooks("subnet-add", subnet);
167 void subnet_del(node_t *n, subnet_t *subnet)
170 avl_delete(n->subnet_tree, subnet);
172 avl_delete(subnet_tree, subnet);
174 run_hooks("subnet-del", subnet);
178 /* Ascii representation of subnets */
180 subnet_t *str2net(char *subnetstr)
184 unsigned short int x[8];
186 subnet = new_subnet();
188 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
189 &x[0], &x[1], &x[2], &x[3],
192 subnet->type = SUBNET_IPV4;
193 subnet->net.ipv4.prefixlength = l;
194 for(i = 0; i < 4; i++)
195 subnet->net.ipv4.address.x[i] = x[i];
199 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
200 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
203 subnet->type = SUBNET_IPV6;
204 subnet->net.ipv6.prefixlength = l;
205 for(i = 0; i < 8; i++)
206 subnet->net.ipv6.address.x[i] = htons(x[i]);
210 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu",
211 &x[0], &x[1], &x[2], &x[3]) == 4)
213 subnet->type = SUBNET_IPV4;
214 subnet->net.ipv4.prefixlength = 32;
215 for(i = 0; i < 4; i++)
216 subnet->net.ipv4.address.x[i] = x[i];
220 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
221 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8)
223 subnet->type = SUBNET_IPV6;
224 subnet->net.ipv6.prefixlength = 128;
225 for(i = 0; i < 8; i++)
226 subnet->net.ipv6.address.x[i] = htons(x[i]);
230 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
231 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6)
233 subnet->type = SUBNET_MAC;
234 for(i = 0; i < 6; i++)
235 subnet->net.mac.address.x[i] = x[i];
243 char *net2str(subnet_t *subnet)
250 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
251 subnet->net.mac.address.x[0],
252 subnet->net.mac.address.x[1],
253 subnet->net.mac.address.x[2],
254 subnet->net.mac.address.x[3],
255 subnet->net.mac.address.x[4],
256 subnet->net.mac.address.x[5]);
259 asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
260 subnet->net.ipv4.address.x[0],
261 subnet->net.ipv4.address.x[1],
262 subnet->net.ipv4.address.x[2],
263 subnet->net.ipv4.address.x[3],
264 subnet->net.ipv4.prefixlength);
267 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
268 ntohs(subnet->net.ipv6.address.x[0]),
269 ntohs(subnet->net.ipv6.address.x[1]),
270 ntohs(subnet->net.ipv6.address.x[2]),
271 ntohs(subnet->net.ipv6.address.x[3]),
272 ntohs(subnet->net.ipv6.address.x[4]),
273 ntohs(subnet->net.ipv6.address.x[5]),
274 ntohs(subnet->net.ipv6.address.x[6]),
275 ntohs(subnet->net.ipv6.address.x[7]),
276 subnet->net.ipv6.prefixlength);
279 syslog(LOG_ERR, _("net2str() was called with unknown subnet type %d, exitting!"), subnet->type);
287 /* Subnet lookup routines */
289 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
292 return avl_search(owner->subnet_tree, subnet);
295 subnet_t *lookup_subnet_mac(mac_t *address)
299 subnet.type = SUBNET_MAC;
300 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
302 p = (subnet_t *)avl_search(subnet_tree, &subnet);
307 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
311 subnet.type = SUBNET_IPV4;
312 memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
313 subnet.net.ipv4.prefixlength = 32;
319 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
321 /* Check if the found subnet REALLY matches */
325 if(p->type != SUBNET_IPV4)
331 if (!maskcmp((char *)address, (char *)&p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
335 /* Otherwise, see if there is a bigger enclosing subnet */
337 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
338 maskcpy((char *)&subnet.net.ipv4.address, (char *)&p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
346 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
350 subnet.type = SUBNET_IPV6;
351 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
352 subnet.net.ipv6.prefixlength = 128;
358 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
360 /* Check if the found subnet REALLY matches */
365 if(p->type != SUBNET_IPV6)
368 if (!maskcmp((char *)address, (char *)&p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
372 /* Otherwise, see if there is a bigger enclosing subnet */
374 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
375 maskcpy((char *)&subnet.net.ipv6.address, (char *)&p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
383 void dump_subnets(void)
389 syslog(LOG_DEBUG, _("Subnet list:"));
390 for(node = subnet_tree->head; node; node = node->next)
392 subnet = (subnet_t *)node->data;
393 netstr = net2str(subnet);
394 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
397 syslog(LOG_DEBUG, _("End of subnet list."));