2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.eu.org>,
4 2000-2002 Ivo Timmermans <ivo@o2w.nl>
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.41 2002/09/09 21:25: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)
55 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
57 if(result || !a->owner || !b->owner)
60 return strcmp(a->owner->name, b->owner->name);
63 int subnet_compare_ipv4(subnet_t * a, subnet_t * b)
67 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
72 result = a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
74 if(result || !a->owner || !b->owner)
77 return strcmp(a->owner->name, b->owner->name);
80 int subnet_compare_ipv6(subnet_t * a, subnet_t * b)
84 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
89 result = a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
91 if(result || !a->owner || !b->owner)
94 return strcmp(a->owner->name, b->owner->name);
97 int subnet_compare(subnet_t * a, subnet_t * b)
101 result = a->type - b->type;
108 return subnet_compare_mac(a, b);
110 return subnet_compare_ipv4(a, b);
112 return subnet_compare_ipv6(a, b);
116 ("subnet_compare() was called with unknown subnet type %d, exitting!"),
125 /* Initialising trees */
127 void init_subnets(void)
131 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
134 void exit_subnets(void)
138 avl_delete_tree(subnet_tree);
141 avl_tree_t *new_subnet_tree(void)
145 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
148 void free_subnet_tree(avl_tree_t * subnet_tree)
152 avl_delete_tree(subnet_tree);
155 /* Allocating and freeing space for subnets */
157 subnet_t *new_subnet(void)
161 return (subnet_t *) xmalloc_and_zero(sizeof(subnet_t));
164 void free_subnet(subnet_t * subnet)
171 /* Adding and removing subnets */
173 void subnet_add(node_t * n, subnet_t * subnet)
179 avl_insert(subnet_tree, subnet);
180 avl_insert(n->subnet_tree, subnet);
183 void subnet_del(node_t * n, subnet_t * subnet)
187 avl_delete(n->subnet_tree, subnet);
188 avl_delete(subnet_tree, subnet);
191 /* Ascii representation of subnets */
193 subnet_t *str2net(char *subnetstr)
201 subnet = new_subnet();
203 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
204 &x[0], &x[1], &x[2], &x[3], &l) == 5) {
205 subnet->type = SUBNET_IPV4;
206 subnet->net.ipv4.prefixlength = l;
208 for(i = 0; i < 4; i++)
209 subnet->net.ipv4.address.x[i] = x[i];
214 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
215 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
217 subnet->type = SUBNET_IPV6;
218 subnet->net.ipv6.prefixlength = l;
220 for(i = 0; i < 8; i++)
221 subnet->net.ipv6.address.x[i] = htons(x[i]);
226 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu", &x[0], &x[1], &x[2], &x[3]) == 4) {
227 subnet->type = SUBNET_IPV4;
228 subnet->net.ipv4.prefixlength = 32;
230 for(i = 0; i < 4; i++)
231 subnet->net.ipv4.address.x[i] = x[i];
236 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
237 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
238 subnet->type = SUBNET_IPV6;
239 subnet->net.ipv6.prefixlength = 128;
241 for(i = 0; i < 8; i++)
242 subnet->net.ipv6.address.x[i] = htons(x[i]);
247 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
248 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
249 subnet->type = SUBNET_MAC;
251 for(i = 0; i < 6; i++)
252 subnet->net.mac.address.x[i] = x[i];
262 char *net2str(subnet_t * subnet)
268 switch (subnet->type) {
270 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx",
271 subnet->net.mac.address.x[0],
272 subnet->net.mac.address.x[1],
273 subnet->net.mac.address.x[2],
274 subnet->net.mac.address.x[3],
275 subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
279 asprintf(&netstr, "%hu.%hu.%hu.%hu/%d",
280 subnet->net.ipv4.address.x[0],
281 subnet->net.ipv4.address.x[1],
282 subnet->net.ipv4.address.x[2],
283 subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
287 asprintf(&netstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
288 ntohs(subnet->net.ipv6.address.x[0]),
289 ntohs(subnet->net.ipv6.address.x[1]),
290 ntohs(subnet->net.ipv6.address.x[2]),
291 ntohs(subnet->net.ipv6.address.x[3]),
292 ntohs(subnet->net.ipv6.address.x[4]),
293 ntohs(subnet->net.ipv6.address.x[5]),
294 ntohs(subnet->net.ipv6.address.x[6]),
295 ntohs(subnet->net.ipv6.address.x[7]),
296 subnet->net.ipv6.prefixlength);
301 _("net2str() was called with unknown subnet type %d, exiting!"),
310 /* Subnet lookup routines */
312 subnet_t *lookup_subnet(node_t * owner, subnet_t * subnet)
316 return avl_search(owner->subnet_tree, subnet);
319 subnet_t *lookup_subnet_mac(mac_t * address)
325 subnet.type = SUBNET_MAC;
326 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
329 p = (subnet_t *) avl_search(subnet_tree, &subnet);
334 subnet_t *lookup_subnet_ipv4(ipv4_t * address)
340 subnet.type = SUBNET_IPV4;
341 memcpy(&subnet.net.ipv4.address, address, sizeof(ipv4_t));
342 subnet.net.ipv4.prefixlength = 32;
348 p = (subnet_t *) avl_search_closest_smaller(subnet_tree, &subnet);
350 /* Check if the found subnet REALLY matches */
353 if(p->type != SUBNET_IPV4) {
358 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
361 /* Otherwise, see if there is a bigger enclosing subnet */
363 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
364 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
372 subnet_t *lookup_subnet_ipv6(ipv6_t * address)
378 subnet.type = SUBNET_IPV6;
379 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
380 subnet.net.ipv6.prefixlength = 128;
386 p = (subnet_t *) avl_search_closest_smaller(subnet_tree, &subnet);
388 /* Check if the found subnet REALLY matches */
391 if(p->type != SUBNET_IPV6)
394 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
397 /* Otherwise, see if there is a bigger enclosing subnet */
399 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
400 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
408 void dump_subnets(void)
416 syslog(LOG_DEBUG, _("Subnet list:"));
418 for(node = subnet_tree->head; node; node = node->next) {
419 subnet = (subnet_t *) node->data;
420 netstr = net2str(subnet);
421 syslog(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
425 syslog(LOG_DEBUG, _("End of subnet list."));