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.29 2002/02/10 21:57:54 guus Exp $
31 #include <netinet/in.h>
43 /* lists type of subnet */
45 avl_tree_t *subnet_tree;
47 /* Subnet comparison */
49 int subnet_compare_mac(subnet_t *a, subnet_t *b)
52 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
55 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
58 /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
60 if(a->net.ipv4.address < b->net.ipv4.address)
62 else if(a->net.ipv4.address > b->net.ipv4.address)
65 if(a->net.ipv4.mask < b->net.ipv4.mask)
67 else if(a->net.ipv4.mask > b->net.ipv4.mask)
73 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
77 /* Same as ipv4 case, but with nasty 128 bit addresses */
79 result = memcmp(a->net.ipv6.address.x, b->net.ipv6.address.x, sizeof(ipv6_t));
84 result = memcmp(a->net.ipv6.mask.x, b->net.ipv6.mask.x, sizeof(ipv6_t));
92 int subnet_compare(subnet_t *a, subnet_t *b)
96 x = a->type - b->type;
103 return subnet_compare_mac(a, b);
105 return subnet_compare_ipv4(a, b);
107 return subnet_compare_ipv6(a, b);
109 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"), a->type);
117 /* Initialising trees */
119 void init_subnets(void)
122 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
126 void exit_subnets(void)
129 avl_delete_tree(subnet_tree);
133 avl_tree_t *new_subnet_tree(void)
136 return avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
140 void free_subnet_tree(avl_tree_t *subnet_tree)
143 avl_delete_tree(subnet_tree);
147 /* Allocating and freeing space for subnets */
149 subnet_t *new_subnet(void)
152 return (subnet_t *)xmalloc(sizeof(subnet_t));
155 void free_subnet(subnet_t *subnet)
161 /* Adding and removing subnets */
163 void subnet_add(node_t *n, subnet_t *subnet)
168 avl_insert(subnet_tree, subnet);
170 avl_insert(n->subnet_tree, subnet);
174 void subnet_del(node_t *n, subnet_t *subnet)
177 avl_delete(n->subnet_tree, subnet);
179 avl_delete(subnet_tree, subnet);
183 /* Ascii representation of subnets */
185 subnet_t *str2net(char *subnetstr)
189 unsigned short int x[6];
191 subnet = new_subnet();
193 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
198 &subnet->net.ipv4.masklength) == 5)
200 subnet->type = SUBNET_IPV4;
201 subnet->net.ipv4.address = (((((x[0] << 8) + x[1]) << 8) + x[2]) << 8) + x[3];
202 subnet->net.ipv4.mask = ~((1 << (32 - subnet->net.ipv4.masklength)) - 1);
206 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
207 &subnet->net.ipv6.address.x[0],
208 &subnet->net.ipv6.address.x[1],
209 &subnet->net.ipv6.address.x[2],
210 &subnet->net.ipv6.address.x[3],
211 &subnet->net.ipv6.address.x[4],
212 &subnet->net.ipv6.address.x[5],
213 &subnet->net.ipv6.address.x[6],
214 &subnet->net.ipv6.address.x[7],
215 &subnet->net.ipv6.masklength) == 9)
217 subnet->type = SUBNET_IPV6;
218 for(l = subnet->net.ipv6.masklength, i = 0; i < 8; l -= 16, i++)
220 subnet->net.ipv6.address.x[i] = htons(subnet->net.ipv6.address.x[i]);
222 subnet->net.ipv6.mask.x[i] = 65535;
224 subnet->net.ipv6.mask.x[i] = htons(65536 - (1 << l));
226 subnet->net.ipv6.mask.x[i] = 0;
231 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu",
237 subnet->type = SUBNET_IPV4;
238 subnet->net.ipv4.address = (((((x[0] << 8) + x[1]) << 8) + x[2]) << 8) + x[3];
239 subnet->net.ipv4.mask = ~0;
240 subnet->net.ipv4.masklength = 32;
244 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
245 &subnet->net.ipv6.address.x[0],
246 &subnet->net.ipv6.address.x[1],
247 &subnet->net.ipv6.address.x[2],
248 &subnet->net.ipv6.address.x[3],
249 &subnet->net.ipv6.address.x[4],
250 &subnet->net.ipv6.address.x[5],
251 &subnet->net.ipv6.address.x[6],
252 &subnet->net.ipv6.address.x[7]) == 8)
254 subnet->type = SUBNET_IPV6;
255 subnet->net.ipv6.masklength = 128;
256 for(l = subnet->net.ipv6.masklength, i = 0; i < 8; l -= 16, i++)
258 subnet->net.ipv6.address.x[i] = htons(subnet->net.ipv6.address.x[i]);
260 subnet->net.ipv6.mask.x[i] = 65535;
262 subnet->net.ipv6.mask.x[i] = htons(65536 - (1 << l));
264 subnet->net.ipv6.mask.x[i] = 0;
269 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
277 subnet->type = SUBNET_MAC;
278 subnet->net.mac.address.x[0] = x[0];
279 subnet->net.mac.address.x[1] = x[1];
280 subnet->net.mac.address.x[2] = x[2];
281 subnet->net.mac.address.x[3] = x[3];
282 subnet->net.mac.address.x[4] = x[4];
283 subnet->net.mac.address.x[5] = x[5];
291 char *net2str(subnet_t *subnet)
298 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
299 subnet->net.mac.address.x[0],
300 subnet->net.mac.address.x[1],
301 subnet->net.mac.address.x[2],
302 subnet->net.mac.address.x[3],
303 subnet->net.mac.address.x[4],
304 subnet->net.mac.address.x[5]);
307 asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
308 (unsigned short int)((subnet->net.ipv4.address >> 24) & 255),
309 (unsigned short int)((subnet->net.ipv4.address >> 16) & 255),
310 (unsigned short int)((subnet->net.ipv4.address >> 8) & 255),
311 (unsigned short int)(subnet->net.ipv4.address & 255),
312 subnet->net.ipv4.masklength);
315 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
316 ntohs(subnet->net.ipv6.address.x[0]),
317 ntohs(subnet->net.ipv6.address.x[1]),
318 ntohs(subnet->net.ipv6.address.x[2]),
319 ntohs(subnet->net.ipv6.address.x[3]),
320 ntohs(subnet->net.ipv6.address.x[4]),
321 ntohs(subnet->net.ipv6.address.x[5]),
322 ntohs(subnet->net.ipv6.address.x[6]),
323 ntohs(subnet->net.ipv6.address.x[7]),
324 subnet->net.ipv6.masklength);
327 asprintf(&netstr, _("unknown subnet type"));
333 /* Subnet lookup routines */
335 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
338 return avl_search(owner->subnet_tree, subnet);
341 subnet_t *lookup_subnet_mac(mac_t *address)
345 subnet.type = SUBNET_MAC;
346 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
348 p = (subnet_t *)avl_search(subnet_tree, &subnet);
353 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
357 subnet.type = SUBNET_IPV4;
358 subnet.net.ipv4.address = *address;
359 subnet.net.ipv4.mask = 0xFFFFFFFF;
365 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
367 /* Check if the found subnet REALLY matches */
371 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
375 /* Otherwise, see if there is a bigger enclosing subnet */
377 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
378 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
386 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
391 subnet.type = SUBNET_IPV6;
392 memcpy(subnet.net.ipv6.address.x, address, sizeof(ipv6_t));
393 memset(subnet.net.ipv6.mask.x, 0xFF, 16);
395 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
399 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
405 void dump_subnets(void)
411 syslog(LOG_DEBUG, _("Subnet list:"));
412 for(node = subnet_tree->head; node; node = node->next)
414 subnet = (subnet_t *)node->data;
415 netstr = net2str(subnet);
416 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
419 syslog(LOG_DEBUG, _("End of subnet list."));