2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4 2000,2001 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.28 2001/10/30 12:59:12 guus Exp $
39 /* lists type of subnet */
41 avl_tree_t *subnet_tree;
43 /* Subnet comparison */
45 int subnet_compare_mac(subnet_t *a, subnet_t *b)
48 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
51 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
54 /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
56 if(a->net.ipv4.address < b->net.ipv4.address)
58 else if(a->net.ipv4.address > b->net.ipv4.address)
61 if(a->net.ipv4.mask < b->net.ipv4.mask)
63 else if(a->net.ipv4.mask > b->net.ipv4.mask)
69 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
73 /* Same as ipv4 case, but with nasty 128 bit addresses */
75 result = memcmp(a->net.ipv6.address.x, b->net.ipv6.address.x, sizeof(ipv6_t));
80 result = memcmp(a->net.ipv6.mask.x, b->net.ipv6.mask.x, sizeof(ipv6_t));
88 int subnet_compare(subnet_t *a, subnet_t *b)
92 x = a->type - b->type;
99 return subnet_compare_mac(a, b);
101 return subnet_compare_ipv4(a, b);
103 return subnet_compare_ipv6(a, b);
105 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), 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 subnet_add(node_t *n, subnet_t *subnet)
162 avl_insert(subnet_tree, subnet);
164 avl_insert(n->subnet_tree, subnet);
168 void subnet_del(node_t *n, subnet_t *subnet)
171 avl_delete(n->subnet_tree, subnet);
173 avl_delete(subnet_tree, subnet);
177 /* Ascii representation of subnets */
179 subnet_t *str2net(char *subnetstr)
184 if(sscanf(subnetstr, "%d,", &type) != 1)
187 subnet = new_subnet();
192 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
193 &subnet->net.mac.address.x[0],
194 &subnet->net.mac.address.x[1],
195 &subnet->net.mac.address.x[2],
196 &subnet->net.mac.address.x[3],
197 &subnet->net.mac.address.x[4],
198 &subnet->net.mac.address.x[5]) != 7)
205 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
212 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
213 &subnet->net.ipv6.address.x[0],
214 &subnet->net.ipv6.address.x[1],
215 &subnet->net.ipv6.address.x[2],
216 &subnet->net.ipv6.address.x[3],
217 &subnet->net.ipv6.address.x[4],
218 &subnet->net.ipv6.address.x[5],
219 &subnet->net.ipv6.address.x[6],
220 &subnet->net.ipv6.address.x[7],
221 &subnet->net.ipv6.mask.x[0],
222 &subnet->net.ipv6.mask.x[1],
223 &subnet->net.ipv6.mask.x[2],
224 &subnet->net.ipv6.mask.x[3],
225 &subnet->net.ipv6.mask.x[4],
226 &subnet->net.ipv6.mask.x[5],
227 &subnet->net.ipv6.mask.x[6],
228 &subnet->net.ipv6.mask.x[7]) != 17)
242 char *net2str(subnet_t *subnet)
249 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
250 subnet->net.mac.address.x[0],
251 subnet->net.mac.address.x[1],
252 subnet->net.mac.address.x[2],
253 subnet->net.mac.address.x[3],
254 subnet->net.mac.address.x[4],
255 subnet->net.mac.address.x[5]);
258 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
261 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
262 subnet->net.ipv6.address.x[0],
263 subnet->net.ipv6.address.x[1],
264 subnet->net.ipv6.address.x[2],
265 subnet->net.ipv6.address.x[3],
266 subnet->net.ipv6.address.x[4],
267 subnet->net.ipv6.address.x[5],
268 subnet->net.ipv6.address.x[6],
269 subnet->net.ipv6.address.x[7],
270 subnet->net.ipv6.mask.x[0],
271 subnet->net.ipv6.mask.x[1],
272 subnet->net.ipv6.mask.x[2],
273 subnet->net.ipv6.mask.x[3],
274 subnet->net.ipv6.mask.x[4],
275 subnet->net.ipv6.mask.x[5],
276 subnet->net.ipv6.mask.x[6],
277 subnet->net.ipv6.mask.x[7]);
280 asprintf(&netstr, _("unknown subnet type"));
286 /* Subnet lookup routines */
288 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
291 return avl_search(owner->subnet_tree, subnet);
294 subnet_t *lookup_subnet_mac(mac_t *address)
298 subnet.type = SUBNET_MAC;
299 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
301 p = (subnet_t *)avl_search(subnet_tree, &subnet);
306 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
310 subnet.type = SUBNET_IPV4;
311 subnet.net.ipv4.address = *address;
312 subnet.net.ipv4.mask = 0xFFFFFFFF;
318 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
320 /* Check if the found subnet REALLY matches */
324 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
328 /* Otherwise, see if there is a bigger enclosing subnet */
330 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
331 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
339 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
344 subnet.type = SUBNET_IPV6;
345 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
346 memset(&subnet.net.ipv6.mask, 0xFF, 16);
348 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
352 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
358 void dump_subnets(void)
364 syslog(LOG_DEBUG, _("Subnet list:"));
365 for(node = subnet_tree->head; node; node = node->next)
367 subnet = (subnet_t *)node->data;
368 netstr = net2str(subnet);
369 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
372 syslog(LOG_DEBUG, _("End of subnet list."));