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.13 2000/11/20 19:41:13 guus Exp $
30 #include "connection.h"
38 /* lists type of subnet */
40 rbltree_t *subnet_tree;
42 void init_subnets(void)
45 subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, (rbl_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 /* If the subnet of a falls within the range of subnet b,
61 then we consider a smaller then b.
62 Otherwise, the addresses alone (and not the subnet masks) will be compared.
65 if(a->net.ipv4.mask > b->net.ipv4.mask)
66 if((a->net.ipv4.address & b->net.ipv4.mask) == b->net.ipv4.address)
69 return a->net.ipv4.address - b->net.ipv4.address;
72 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
75 /* Same as ipv4 case, but with nasty 128 bit addresses */
77 if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
78 if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
79 (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
80 (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
81 (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
82 (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
83 (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
84 (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
85 (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
88 return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
91 int subnet_compare(subnet_t *a, subnet_t *b)
95 x = a->type - b->type;
102 return subnet_compare_mac(a, b);
104 return subnet_compare_ipv4(a, b);
106 return subnet_compare_ipv6(a, b);
108 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
114 /* Allocating and freeing space for subnets */
116 subnet_t *new_subnet(void)
119 return (subnet_t *)xmalloc(sizeof(subnet_t));
122 void free_subnet(subnet_t *subnet)
128 /* Linked list management */
130 void subnet_add(connection_t *cl, subnet_t *subnet)
133 rbl_insert(subnet_tree, subnet);
134 rbl_insert(cl->subnet_tree, subnet);
138 void subnet_del(subnet_t *subnet)
141 rbl_delete(subnet->owner->subnet_tree, subnet);
142 rbl_delete(subnet_tree, subnet);
146 /* Ascii representation of subnets */
148 subnet_t *str2net(char *subnetstr)
153 if(sscanf(subnetstr, "%d,", &type) != 1)
156 subnet = new_subnet();
161 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
162 &subnet->net.mac.address.x[0],
163 &subnet->net.mac.address.x[1],
164 &subnet->net.mac.address.x[2],
165 &subnet->net.mac.address.x[3],
166 &subnet->net.mac.address.x[4],
167 &subnet->net.mac.address.x[5]) != 7)
174 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
181 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
182 &subnet->net.ipv6.address.x[0],
183 &subnet->net.ipv6.address.x[1],
184 &subnet->net.ipv6.address.x[2],
185 &subnet->net.ipv6.address.x[3],
186 &subnet->net.ipv6.address.x[4],
187 &subnet->net.ipv6.address.x[5],
188 &subnet->net.ipv6.address.x[6],
189 &subnet->net.ipv6.address.x[7],
190 &subnet->net.ipv6.mask.x[0],
191 &subnet->net.ipv6.mask.x[1],
192 &subnet->net.ipv6.mask.x[2],
193 &subnet->net.ipv6.mask.x[3],
194 &subnet->net.ipv6.mask.x[4],
195 &subnet->net.ipv6.mask.x[5],
196 &subnet->net.ipv6.mask.x[6],
197 &subnet->net.ipv6.mask.x[7]) != 17)
211 char *net2str(subnet_t *subnet)
218 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
219 subnet->net.mac.address.x[0],
220 subnet->net.mac.address.x[1],
221 subnet->net.mac.address.x[2],
222 subnet->net.mac.address.x[3],
223 subnet->net.mac.address.x[4],
224 subnet->net.mac.address.x[5]);
227 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
230 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
231 subnet->net.ipv6.address.x[0],
232 subnet->net.ipv6.address.x[1],
233 subnet->net.ipv6.address.x[2],
234 subnet->net.ipv6.address.x[3],
235 subnet->net.ipv6.address.x[4],
236 subnet->net.ipv6.address.x[5],
237 subnet->net.ipv6.address.x[6],
238 subnet->net.ipv6.address.x[7],
239 subnet->net.ipv6.mask.x[0],
240 subnet->net.ipv6.mask.x[1],
241 subnet->net.ipv6.mask.x[2],
242 subnet->net.ipv6.mask.x[3],
243 subnet->net.ipv6.mask.x[4],
244 subnet->net.ipv6.mask.x[5],
245 subnet->net.ipv6.mask.x[6],
246 subnet->net.ipv6.mask.x[7]);
249 asprintf(&netstr, _("unknown"));
255 /* Subnet lookup routines */
257 subnet_t *lookup_subnet_mac(mac_t address)
261 subnet.type = SUBNET_MAC;
262 subnet.net.mac.address = address;
263 return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
266 subnet_t *lookup_subnet_ipv4(ipv4_t address)
270 subnet.type = SUBNET_IPV4;
271 subnet.net.ipv4.address = address;
272 subnet.net.ipv4.mask = 0xFFFFFFFF;
273 return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
276 subnet_t *lookup_subnet_ipv6(ipv6_t address)
280 subnet.type = SUBNET_IPV6;
281 subnet.net.ipv6.address = address;
282 memset(&subnet.net.ipv6.mask, 0xFF, 16);
283 return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
286 void dump_subnet_list(void)
292 syslog(LOG_DEBUG, _("Subnet list:"));
293 RBL_FOREACH(subnet_tree, rbl)
295 subnet = (subnet_t *)rbl->data;
296 netstr = net2str(subnet);
297 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
300 syslog(LOG_DEBUG, _("End of subnet list."));