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.23 2001/06/29 13:09:55 guus Exp $
31 #include "connection.h"
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)
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)
61 /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
63 if(a->net.ipv4.address == b->net.ipv4.address)
64 return a->net.ipv4.mask - b->net.ipv4.mask;
66 return a->net.ipv4.address - b->net.ipv4.address;
69 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
72 /* Same as ipv4 case, but with nasty 128 bit addresses */
74 if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
75 if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
76 (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
77 (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
78 (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
79 (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
80 (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
81 (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
82 (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
85 return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, 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 /* Allocating and freeing space for subnets */
113 subnet_t *new_subnet(void)
116 return (subnet_t *)xmalloc(sizeof(subnet_t));
119 void free_subnet(subnet_t *subnet)
125 /* Linked list management */
127 void subnet_add(connection_t *cl, subnet_t *subnet)
132 while(!avl_insert(subnet_tree, subnet))
136 old = (subnet_t *)avl_search(subnet_tree, subnet);
138 if(debug_lvl >= DEBUG_PROTOCOL)
141 subnetstr = net2str(subnet);
142 syslog(LOG_WARNING, _("Duplicate subnet %s for %s (%s), previous owner %s (%s)!"),
143 subnetstr, cl->name, cl->hostname, old->owner->name, old->owner->hostname);
150 avl_insert(cl->subnet_tree, subnet);
154 void subnet_del(subnet_t *subnet)
157 avl_delete(subnet->owner->subnet_tree, subnet);
159 avl_delete(subnet_tree, subnet);
163 /* Ascii representation of subnets */
165 subnet_t *str2net(char *subnetstr)
170 if(sscanf(subnetstr, "%d,", &type) != 1)
173 subnet = new_subnet();
178 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
179 &subnet->net.mac.address.x[0],
180 &subnet->net.mac.address.x[1],
181 &subnet->net.mac.address.x[2],
182 &subnet->net.mac.address.x[3],
183 &subnet->net.mac.address.x[4],
184 &subnet->net.mac.address.x[5]) != 7)
191 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
198 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
199 &subnet->net.ipv6.address.x[0],
200 &subnet->net.ipv6.address.x[1],
201 &subnet->net.ipv6.address.x[2],
202 &subnet->net.ipv6.address.x[3],
203 &subnet->net.ipv6.address.x[4],
204 &subnet->net.ipv6.address.x[5],
205 &subnet->net.ipv6.address.x[6],
206 &subnet->net.ipv6.address.x[7],
207 &subnet->net.ipv6.mask.x[0],
208 &subnet->net.ipv6.mask.x[1],
209 &subnet->net.ipv6.mask.x[2],
210 &subnet->net.ipv6.mask.x[3],
211 &subnet->net.ipv6.mask.x[4],
212 &subnet->net.ipv6.mask.x[5],
213 &subnet->net.ipv6.mask.x[6],
214 &subnet->net.ipv6.mask.x[7]) != 17)
228 char *net2str(subnet_t *subnet)
235 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
236 subnet->net.mac.address.x[0],
237 subnet->net.mac.address.x[1],
238 subnet->net.mac.address.x[2],
239 subnet->net.mac.address.x[3],
240 subnet->net.mac.address.x[4],
241 subnet->net.mac.address.x[5]);
244 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
247 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
248 subnet->net.ipv6.address.x[0],
249 subnet->net.ipv6.address.x[1],
250 subnet->net.ipv6.address.x[2],
251 subnet->net.ipv6.address.x[3],
252 subnet->net.ipv6.address.x[4],
253 subnet->net.ipv6.address.x[5],
254 subnet->net.ipv6.address.x[6],
255 subnet->net.ipv6.address.x[7],
256 subnet->net.ipv6.mask.x[0],
257 subnet->net.ipv6.mask.x[1],
258 subnet->net.ipv6.mask.x[2],
259 subnet->net.ipv6.mask.x[3],
260 subnet->net.ipv6.mask.x[4],
261 subnet->net.ipv6.mask.x[5],
262 subnet->net.ipv6.mask.x[6],
263 subnet->net.ipv6.mask.x[7]);
266 asprintf(&netstr, _("unknown subnet type"));
272 /* Subnet lookup routines */
274 subnet_t *lookup_subnet_mac(mac_t *address)
278 subnet.type = SUBNET_MAC;
279 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
281 p = (subnet_t *)avl_search(subnet_tree, &subnet);
286 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
290 subnet.type = SUBNET_IPV4;
291 subnet.net.ipv4.address = *address;
292 subnet.net.ipv4.mask = 0xFFFFFFFF;
298 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
300 /* Check if the found subnet REALLY matches */
304 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
308 /* Otherwise, see if there is a bigger enclosing subnet */
310 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
311 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
319 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
324 subnet.type = SUBNET_IPV6;
325 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
326 memset(&subnet.net.ipv6.mask, 0xFF, 16);
328 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
332 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
338 void dump_subnet_list(void)
344 syslog(LOG_DEBUG, _("Subnet list:"));
345 for(node = subnet_tree->head; node; node = node->next)
347 subnet = (subnet_t *)node->data;
348 netstr = net2str(subnet);
349 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
352 syslog(LOG_DEBUG, _("End of subnet list."));