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.26 2001/10/27 13:13:35 guus Exp $
39 /* lists type of subnet */
41 avl_tree_t *subnet_tree;
43 void init_subnets(void)
46 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
50 /* Subnet comparison */
52 int subnet_compare_mac(subnet_t *a, subnet_t *b)
56 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
61 return strcmp(a->owner->name, b->owner->name);
64 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
67 /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
69 if(a->net.ipv4.address < b->net.ipv4.address)
71 else if(a->net.ipv4.address > b->net.ipv4.address)
74 if(a->net.ipv4.mask < b->net.ipv4.mask)
76 else if(a->net.ipv4.mask > b->net.ipv4.mask)
79 return strcmp(a->owner->name, b->owner->name);
82 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
86 /* Same as ipv4 case, but with nasty 128 bit addresses */
88 result = memcmp(a->net.ipv6.address.x, b->net.ipv6.address.x, sizeof(ipv6_t));
93 result = memcmp(a->net.ipv6.mask.x, b->net.ipv6.mask.x, sizeof(ipv6_t));
98 return strcmp(a->owner->name, b->owner->name);
101 int subnet_compare(subnet_t *a, subnet_t *b)
105 x = a->type - b->type;
112 return subnet_compare_mac(a, b);
114 return subnet_compare_ipv4(a, b);
116 return subnet_compare_ipv6(a, b);
118 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
124 /* Allocating and freeing space for subnets */
126 subnet_t *new_subnet(void)
129 return (subnet_t *)xmalloc(sizeof(subnet_t));
132 void free_subnet(subnet_t *subnet)
138 /* Linked list management */
140 void subnet_add(node_t *n, subnet_t *subnet)
145 avl_insert(subnet_tree, subnet);
147 avl_insert(n->subnet_tree, subnet);
151 void subnet_del(node_t *n, subnet_t *subnet)
154 avl_delete(n->subnet_tree, subnet);
156 avl_delete(subnet_tree, subnet);
160 /* Ascii representation of subnets */
162 subnet_t *str2net(char *subnetstr)
167 if(sscanf(subnetstr, "%d,", &type) != 1)
170 subnet = new_subnet();
175 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
176 &subnet->net.mac.address.x[0],
177 &subnet->net.mac.address.x[1],
178 &subnet->net.mac.address.x[2],
179 &subnet->net.mac.address.x[3],
180 &subnet->net.mac.address.x[4],
181 &subnet->net.mac.address.x[5]) != 7)
188 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
195 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
196 &subnet->net.ipv6.address.x[0],
197 &subnet->net.ipv6.address.x[1],
198 &subnet->net.ipv6.address.x[2],
199 &subnet->net.ipv6.address.x[3],
200 &subnet->net.ipv6.address.x[4],
201 &subnet->net.ipv6.address.x[5],
202 &subnet->net.ipv6.address.x[6],
203 &subnet->net.ipv6.address.x[7],
204 &subnet->net.ipv6.mask.x[0],
205 &subnet->net.ipv6.mask.x[1],
206 &subnet->net.ipv6.mask.x[2],
207 &subnet->net.ipv6.mask.x[3],
208 &subnet->net.ipv6.mask.x[4],
209 &subnet->net.ipv6.mask.x[5],
210 &subnet->net.ipv6.mask.x[6],
211 &subnet->net.ipv6.mask.x[7]) != 17)
225 char *net2str(subnet_t *subnet)
232 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
233 subnet->net.mac.address.x[0],
234 subnet->net.mac.address.x[1],
235 subnet->net.mac.address.x[2],
236 subnet->net.mac.address.x[3],
237 subnet->net.mac.address.x[4],
238 subnet->net.mac.address.x[5]);
241 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
244 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
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],
253 subnet->net.ipv6.mask.x[0],
254 subnet->net.ipv6.mask.x[1],
255 subnet->net.ipv6.mask.x[2],
256 subnet->net.ipv6.mask.x[3],
257 subnet->net.ipv6.mask.x[4],
258 subnet->net.ipv6.mask.x[5],
259 subnet->net.ipv6.mask.x[6],
260 subnet->net.ipv6.mask.x[7]);
263 asprintf(&netstr, _("unknown subnet type"));
269 /* Subnet lookup routines */
271 subnet_t *lookup_subnet(node_t *owner, subnet_t *subnet)
274 return avl_search(owner->subnet_tree, subnet);
277 subnet_t *lookup_subnet_mac(mac_t *address)
281 subnet.type = SUBNET_MAC;
282 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
284 p = (subnet_t *)avl_search(subnet_tree, &subnet);
289 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
293 subnet.type = SUBNET_IPV4;
294 subnet.net.ipv4.address = *address;
295 subnet.net.ipv4.mask = 0xFFFFFFFF;
301 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
303 /* Check if the found subnet REALLY matches */
307 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
311 /* Otherwise, see if there is a bigger enclosing subnet */
313 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
314 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
322 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
327 subnet.type = SUBNET_IPV6;
328 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
329 memset(&subnet.net.ipv6.mask, 0xFF, 16);
331 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
335 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
341 void dump_subnets(void)
347 syslog(LOG_DEBUG, _("Subnet list:"));
348 for(node = subnet_tree->head; node; node = node->next)
350 subnet = (subnet_t *)node->data;
351 netstr = net2str(subnet);
352 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
355 syslog(LOG_DEBUG, _("End of subnet list."));