2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2004 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>
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.
34 /* lists type of subnet */
36 avl_tree_t *subnet_tree;
38 /* Subnet comparison */
40 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b)
44 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
46 if(result || !a->owner || !b->owner)
49 return strcmp(a->owner->name, b->owner->name);
52 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b)
56 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
61 result = a->net.ipv4.prefixlength - b->net.ipv4.prefixlength;
63 if(result || !a->owner || !b->owner)
66 return strcmp(a->owner->name, b->owner->name);
69 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b)
73 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
78 result = a->net.ipv6.prefixlength - b->net.ipv6.prefixlength;
80 if(result || !a->owner || !b->owner)
83 return strcmp(a->owner->name, b->owner->name);
86 int subnet_compare(const subnet_t *a, const subnet_t *b)
90 result = a->type - b->type;
97 return subnet_compare_mac(a, b);
99 return subnet_compare_ipv4(a, b);
101 return subnet_compare_ipv6(a, b);
103 logger(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, exitting!"),
112 /* Initialising trees */
114 void init_subnets(void)
118 subnet_tree = avl_alloc_tree((avl_compare_t) subnet_compare, (avl_action_t) free_subnet);
121 void exit_subnets(void)
125 avl_delete_tree(subnet_tree);
128 avl_tree_t *new_subnet_tree(void)
132 return avl_alloc_tree((avl_compare_t) subnet_compare, NULL);
135 void free_subnet_tree(avl_tree_t *subnet_tree)
139 avl_delete_tree(subnet_tree);
142 /* Allocating and freeing space for subnets */
144 subnet_t *new_subnet(void)
148 return xmalloc_and_zero(sizeof(subnet_t));
151 void free_subnet(subnet_t *subnet)
158 /* Adding and removing subnets */
160 void subnet_add(node_t *n, subnet_t *subnet)
166 avl_insert(subnet_tree, subnet);
167 avl_insert(n->subnet_tree, subnet);
170 void subnet_del(node_t *n, subnet_t *subnet)
174 avl_delete(n->subnet_tree, subnet);
175 avl_delete(subnet_tree, subnet);
178 /* Ascii representation of subnets */
180 bool str2net(subnet_t *subnet, const char *subnetstr)
187 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d",
188 &x[0], &x[1], &x[2], &x[3], &l) == 5) {
189 subnet->type = SUBNET_IPV4;
190 subnet->net.ipv4.prefixlength = l;
192 for(i = 0; i < 4; i++)
193 subnet->net.ipv4.address.x[i] = x[i];
198 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
199 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
201 subnet->type = SUBNET_IPV6;
202 subnet->net.ipv6.prefixlength = l;
204 for(i = 0; i < 8; i++)
205 subnet->net.ipv6.address.x[i] = htons(x[i]);
210 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu", &x[0], &x[1], &x[2], &x[3]) == 4) {
211 subnet->type = SUBNET_IPV4;
212 subnet->net.ipv4.prefixlength = 32;
214 for(i = 0; i < 4; i++)
215 subnet->net.ipv4.address.x[i] = x[i];
220 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
221 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7]) == 8) {
222 subnet->type = SUBNET_IPV6;
223 subnet->net.ipv6.prefixlength = 128;
225 for(i = 0; i < 8; i++)
226 subnet->net.ipv6.address.x[i] = htons(x[i]);
231 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx",
232 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]) == 6) {
233 subnet->type = SUBNET_MAC;
235 for(i = 0; i < 6; i++)
236 subnet->net.mac.address.x[i] = x[i];
244 bool net2str(char *netstr, int len, const subnet_t *subnet)
248 switch (subnet->type) {
250 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx",
251 subnet->net.mac.address.x[0],
252 subnet->net.mac.address.x[1],
253 subnet->net.mac.address.x[2],
254 subnet->net.mac.address.x[3],
255 subnet->net.mac.address.x[4], subnet->net.mac.address.x[5]);
259 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d",
260 subnet->net.ipv4.address.x[0],
261 subnet->net.ipv4.address.x[1],
262 subnet->net.ipv4.address.x[2],
263 subnet->net.ipv4.address.x[3], subnet->net.ipv4.prefixlength);
267 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d",
268 ntohs(subnet->net.ipv6.address.x[0]),
269 ntohs(subnet->net.ipv6.address.x[1]),
270 ntohs(subnet->net.ipv6.address.x[2]),
271 ntohs(subnet->net.ipv6.address.x[3]),
272 ntohs(subnet->net.ipv6.address.x[4]),
273 ntohs(subnet->net.ipv6.address.x[5]),
274 ntohs(subnet->net.ipv6.address.x[6]),
275 ntohs(subnet->net.ipv6.address.x[7]),
276 subnet->net.ipv6.prefixlength);
281 _("net2str() was called with unknown subnet type %d, exiting!"),
290 /* Subnet lookup routines */
292 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet)
296 return avl_search(owner->subnet_tree, subnet);
299 subnet_t *lookup_subnet_mac(const mac_t *address)
301 subnet_t *p, subnet = {0};
305 subnet.type = SUBNET_MAC;
306 subnet.net.mac.address = *address;
309 p = avl_search(subnet_tree, &subnet);
314 subnet_t *lookup_subnet_ipv4(const ipv4_t *address)
316 subnet_t *p, subnet = {0};
320 subnet.type = SUBNET_IPV4;
321 subnet.net.ipv4.address = *address;
322 subnet.net.ipv4.prefixlength = 32;
328 p = avl_search_closest_smaller(subnet_tree, &subnet);
330 /* Check if the found subnet REALLY matches */
333 if(p->type != SUBNET_IPV4) {
338 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t)))
341 /* Otherwise, see if there is a bigger enclosing subnet */
343 subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1;
344 maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t));
352 subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
354 subnet_t *p, subnet = {0};
358 subnet.type = SUBNET_IPV6;
359 subnet.net.ipv6.address = *address;
360 subnet.net.ipv6.prefixlength = 128;
366 p = avl_search_closest_smaller(subnet_tree, &subnet);
368 /* Check if the found subnet REALLY matches */
371 if(p->type != SUBNET_IPV6)
374 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t)))
377 /* Otherwise, see if there is a bigger enclosing subnet */
379 subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
380 maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t));
388 void dump_subnets(void)
390 char netstr[MAXNETSTR];
396 logger(LOG_DEBUG, _("Subnet list:"));
398 for(node = subnet_tree->head; node; node = node->next) {
400 if(!net2str(netstr, sizeof netstr, subnet))
402 logger(LOG_DEBUG, _(" %s owner %s"), netstr, subnet->owner->name);
405 logger(LOG_DEBUG, _("End of subnet list."));