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.7 2000/10/28 21:05:20 guus Exp $
34 /* lists type of subnet */
36 subnet_t *subnet_list[SUBNET_TYPES] = { NULL };
38 /* Allocating and freeing space for subnets */
40 subnet_t *new_subnet(void)
43 return (subnet_t *)xmalloc(sizeof(subnet_t));
46 void free_subnet(subnet_t *subnet)
52 /* Linked list management */
54 void subnet_add(conn_list_t *cl, subnet_t *subnet)
61 /* Link it into the owners list of subnets (unsorted) */
63 subnet->next = cl->subnets;
66 subnet->next->prev = subnet;
69 /* And now add it to the global subnet list (sorted) */
71 /* Sort on size of subnet mask (IPv4 only at the moment!)
73 Three cases: subnet_list[] = NULL -> just add this subnet
74 insert before first -> add it in front of list
75 rest: insert after another subnet
78 if(subnet_list[subnet->type])
80 p = q = subnet_list[subnet->type];
82 for(; p; p = p->global_next)
84 if(subnet->net.ipv4.mask >= p->net.ipv4.mask)
91 if(p == subnet_list[subnet->type]) /* First two cases */
94 subnet->global_next = subnet_list[subnet->type];
95 subnet->global_prev = NULL;
96 subnet_list[subnet->type] = subnet;
101 subnet->global_next = q->global_next;
102 subnet->global_prev = q;
103 q->global_next = subnet;
106 if(subnet->global_next)
107 subnet->global_next->global_prev = subnet;
111 void subnet_del(subnet_t *subnet)
114 /* Remove it from owner's list */
118 subnet->prev->next = subnet->next;
122 subnet->owner->subnets = subnet->next;
125 subnet->next->prev = subnet->prev;
127 /* Remove it from the global list */
129 if(subnet->global_prev)
131 subnet->global_prev->global_next = subnet->global_next;
135 subnet_list[subnet->type] = subnet->global_next;
138 subnet->global_next->global_prev = subnet->global_prev;
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",
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"));
253 /* Subnet lookup routines */
255 subnet_t *lookup_subnet_mac(mac_t address)
259 for(subnet = subnet_list[SUBNET_MAC]; subnet != NULL; subnet = subnet->global_next)
261 if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
268 subnet_t *lookup_subnet_ipv4(ipv4_t address)
272 for(subnet = subnet_list[SUBNET_IPV4]; subnet != NULL; subnet = subnet->global_next)
274 if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
281 subnet_t *lookup_subnet_ipv6(ipv6_t address)
286 for(subnet = subnet_list[SUBNET_IPV6]; subnet != NULL; subnet = subnet->global_next)
289 if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])
298 void dump_subnet_list(void)
303 syslog(LOG_DEBUG, _("Subnet list:"));
305 for(subnet = subnet_list[SUBNET_IPV4]; subnet != NULL; subnet = subnet->global_next)
307 netstr = net2str(subnet);
308 syslog(LOG_DEBUG, "%s owner %s", netstr, subnet->owner->name);
312 syslog(LOG_DEBUG, _("End of subnet list."));