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.21 2001/06/05 18:07:14 guus Exp $
30 #include "connection.h"
38 /* lists type of subnet */
40 avl_tree_t *subnet_tree;
42 void init_subnets(void)
45 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
49 /* Subnet comparison */
51 int subnet_compare_mac(subnet_t *a, subnet_t *b)
54 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
57 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
60 /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
62 if(a->net.ipv4.address == b->net.ipv4.address)
63 return a->net.ipv4.mask - b->net.ipv4.mask;
65 return a->net.ipv4.address - b->net.ipv4.address;
68 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
71 /* Same as ipv4 case, but with nasty 128 bit addresses */
73 if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
74 if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
75 (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
76 (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
77 (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
78 (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
79 (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
80 (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
81 (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
84 return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
87 int subnet_compare(subnet_t *a, subnet_t *b)
91 x = a->type - b->type;
98 return subnet_compare_mac(a, b);
100 return subnet_compare_ipv4(a, b);
102 return subnet_compare_ipv6(a, b);
104 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
110 /* Allocating and freeing space for subnets */
112 subnet_t *new_subnet(void)
115 return (subnet_t *)xmalloc(sizeof(subnet_t));
118 void free_subnet(subnet_t *subnet)
124 /* Linked list management */
126 void subnet_add(connection_t *cl, subnet_t *subnet)
130 avl_insert(subnet_tree, subnet);
131 avl_insert(cl->subnet_tree, subnet);
135 void subnet_del(subnet_t *subnet)
138 avl_delete(subnet->owner->subnet_tree, subnet);
140 avl_delete(subnet_tree, subnet);
144 /* Ascii representation of subnets */
146 subnet_t *str2net(char *subnetstr)
151 if(sscanf(subnetstr, "%d,", &type) != 1)
154 subnet = new_subnet();
159 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
160 &subnet->net.mac.address.x[0],
161 &subnet->net.mac.address.x[1],
162 &subnet->net.mac.address.x[2],
163 &subnet->net.mac.address.x[3],
164 &subnet->net.mac.address.x[4],
165 &subnet->net.mac.address.x[5]) != 7)
172 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
179 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
180 &subnet->net.ipv6.address.x[0],
181 &subnet->net.ipv6.address.x[1],
182 &subnet->net.ipv6.address.x[2],
183 &subnet->net.ipv6.address.x[3],
184 &subnet->net.ipv6.address.x[4],
185 &subnet->net.ipv6.address.x[5],
186 &subnet->net.ipv6.address.x[6],
187 &subnet->net.ipv6.address.x[7],
188 &subnet->net.ipv6.mask.x[0],
189 &subnet->net.ipv6.mask.x[1],
190 &subnet->net.ipv6.mask.x[2],
191 &subnet->net.ipv6.mask.x[3],
192 &subnet->net.ipv6.mask.x[4],
193 &subnet->net.ipv6.mask.x[5],
194 &subnet->net.ipv6.mask.x[6],
195 &subnet->net.ipv6.mask.x[7]) != 17)
209 char *net2str(subnet_t *subnet)
216 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
217 subnet->net.mac.address.x[0],
218 subnet->net.mac.address.x[1],
219 subnet->net.mac.address.x[2],
220 subnet->net.mac.address.x[3],
221 subnet->net.mac.address.x[4],
222 subnet->net.mac.address.x[5]);
225 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
228 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
229 subnet->net.ipv6.address.x[0],
230 subnet->net.ipv6.address.x[1],
231 subnet->net.ipv6.address.x[2],
232 subnet->net.ipv6.address.x[3],
233 subnet->net.ipv6.address.x[4],
234 subnet->net.ipv6.address.x[5],
235 subnet->net.ipv6.address.x[6],
236 subnet->net.ipv6.address.x[7],
237 subnet->net.ipv6.mask.x[0],
238 subnet->net.ipv6.mask.x[1],
239 subnet->net.ipv6.mask.x[2],
240 subnet->net.ipv6.mask.x[3],
241 subnet->net.ipv6.mask.x[4],
242 subnet->net.ipv6.mask.x[5],
243 subnet->net.ipv6.mask.x[6],
244 subnet->net.ipv6.mask.x[7]);
247 asprintf(&netstr, _("unknown subnet type"));
253 /* Subnet lookup routines */
255 subnet_t *lookup_subnet_mac(mac_t *address)
259 subnet.type = SUBNET_MAC;
260 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
262 p = (subnet_t *)avl_search(subnet_tree, &subnet);
267 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
271 subnet.type = SUBNET_IPV4;
272 subnet.net.ipv4.address = *address;
273 subnet.net.ipv4.mask = 0xFFFFFFFF;
279 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
281 /* Check if the found subnet REALLY matches */
285 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
289 /* Otherwise, see if there is a bigger enclosing subnet */
291 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
292 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
300 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
305 subnet.type = SUBNET_IPV6;
306 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
307 memset(&subnet.net.ipv6.mask, 0xFF, 16);
309 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
313 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
319 void dump_subnet_list(void)
325 syslog(LOG_DEBUG, _("Subnet list:"));
326 for(node = subnet_tree->head; node; node = node->next)
328 subnet = (subnet_t *)node->data;
329 netstr = net2str(subnet);
330 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
333 syslog(LOG_DEBUG, _("End of subnet list."));