2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4 2000 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.12 2000/11/20 19:12:17 guus Exp $
30 #include "connection.h"
38 /* lists type of subnet */
40 rbltree_t _subnet_tree = { NULL };
41 rbltree_t *subnet_tree = &_subnet_tree;
43 /* Subnet comparison */
45 int subnet_compare_mac(subnet_t *a, subnet_t *b)
48 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
51 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
54 /* If the subnet of a falls within the range of subnet b,
55 then we consider a smaller then b.
56 Otherwise, the addresses alone (and not the subnet masks) will be compared.
59 if(a->net.ipv4.mask > b->net.ipv4.mask)
60 if((a->net.ipv4.address & b->net.ipv4.mask) == b->net.ipv4.address)
63 return a->net.ipv4.address - b->net.ipv4.address;
66 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
69 /* Same as ipv4 case, but with nasty 128 bit addresses */
71 if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
72 if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
73 (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
74 (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
75 (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
76 (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
77 (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
78 (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
79 (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
82 return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
85 int subnet_compare(subnet_t *a, subnet_t *b)
89 x = a->type - b->type;
96 return subnet_compare_mac(a, b);
98 return subnet_compare_ipv4(a, b);
100 return subnet_compare_ipv6(a, b);
102 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
108 /* Allocating and freeing space for subnets */
110 subnet_t *new_subnet(void)
113 return (subnet_t *)xmalloc(sizeof(subnet_t));
116 void free_subnet(subnet_t *subnet)
122 /* Linked list management */
124 void subnet_add(connection_t *cl, subnet_t *subnet)
127 rbl_insert(subnet_tree, subnet);
128 rbl_insert(cl->subnet_tree, subnet);
132 void subnet_del(subnet_t *subnet)
135 free_rbl(rbl_unlink(subnet->owner->subnet_tree, subnet));
136 rbl_delete(subnet_tree, subnet);
140 /* Ascii representation of subnets */
142 subnet_t *str2net(char *subnetstr)
147 if(sscanf(subnetstr, "%d,", &type) != 1)
150 subnet = new_subnet();
155 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
156 &subnet->net.mac.address.x[0],
157 &subnet->net.mac.address.x[1],
158 &subnet->net.mac.address.x[2],
159 &subnet->net.mac.address.x[3],
160 &subnet->net.mac.address.x[4],
161 &subnet->net.mac.address.x[5]) != 7)
168 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
175 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
176 &subnet->net.ipv6.address.x[0],
177 &subnet->net.ipv6.address.x[1],
178 &subnet->net.ipv6.address.x[2],
179 &subnet->net.ipv6.address.x[3],
180 &subnet->net.ipv6.address.x[4],
181 &subnet->net.ipv6.address.x[5],
182 &subnet->net.ipv6.address.x[6],
183 &subnet->net.ipv6.address.x[7],
184 &subnet->net.ipv6.mask.x[0],
185 &subnet->net.ipv6.mask.x[1],
186 &subnet->net.ipv6.mask.x[2],
187 &subnet->net.ipv6.mask.x[3],
188 &subnet->net.ipv6.mask.x[4],
189 &subnet->net.ipv6.mask.x[5],
190 &subnet->net.ipv6.mask.x[6],
191 &subnet->net.ipv6.mask.x[7]) != 17)
205 char *net2str(subnet_t *subnet)
212 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
213 subnet->net.mac.address.x[0],
214 subnet->net.mac.address.x[1],
215 subnet->net.mac.address.x[2],
216 subnet->net.mac.address.x[3],
217 subnet->net.mac.address.x[4],
218 subnet->net.mac.address.x[5]);
221 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
224 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
225 subnet->net.ipv6.address.x[0],
226 subnet->net.ipv6.address.x[1],
227 subnet->net.ipv6.address.x[2],
228 subnet->net.ipv6.address.x[3],
229 subnet->net.ipv6.address.x[4],
230 subnet->net.ipv6.address.x[5],
231 subnet->net.ipv6.address.x[6],
232 subnet->net.ipv6.address.x[7],
233 subnet->net.ipv6.mask.x[0],
234 subnet->net.ipv6.mask.x[1],
235 subnet->net.ipv6.mask.x[2],
236 subnet->net.ipv6.mask.x[3],
237 subnet->net.ipv6.mask.x[4],
238 subnet->net.ipv6.mask.x[5],
239 subnet->net.ipv6.mask.x[6],
240 subnet->net.ipv6.mask.x[7]);
243 asprintf(&netstr, _("unknown"));
249 /* Subnet lookup routines */
251 subnet_t *lookup_subnet_mac(mac_t address)
255 subnet.type = SUBNET_MAC;
256 subnet.net.mac.address = address;
257 return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
260 subnet_t *lookup_subnet_ipv4(ipv4_t address)
264 subnet.type = SUBNET_IPV4;
265 subnet.net.ipv4.address = address;
266 subnet.net.ipv4.mask = 0xFFFFFFFF;
267 return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
270 subnet_t *lookup_subnet_ipv6(ipv6_t address)
274 subnet.type = SUBNET_IPV6;
275 subnet.net.ipv6.address = address;
276 memset(&subnet.net.ipv6.mask, 0xFF, 16);
277 return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
280 void dump_subnet_list(void)
286 syslog(LOG_DEBUG, _("Subnet list:"));
287 RBL_FOREACH(subnet_tree, rbl)
289 subnet = (subnet_t *)rbl->data;
290 netstr = net2str(subnet);
291 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
294 syslog(LOG_DEBUG, _("End of subnet list."));