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.10 2000/11/04 11:49:58 guus Exp $
36 /* lists type of subnet */
38 subnet_t *subnet_list[SUBNET_TYPES] = { NULL };
40 /* Allocating and freeing space for subnets */
42 subnet_t *new_subnet(void)
45 return (subnet_t *)xmalloc(sizeof(subnet_t));
48 void free_subnet(subnet_t *subnet)
54 /* Linked list management */
56 void subnet_add(conn_list_t *cl, subnet_t *subnet)
63 /* Link it into the owners list of subnets (unsorted) */
65 subnet->next = cl->subnets;
68 subnet->next->prev = subnet;
71 /* And now add it to the global subnet list (sorted) */
73 /* Sort on size of subnet mask (IPv4 only at the moment!)
75 Three cases: subnet_list[] = NULL -> just add this subnet
76 insert before first -> add it in front of list
77 rest: insert after another subnet
80 if(subnet_list[subnet->type])
82 p = q = subnet_list[subnet->type];
84 for(; p; p = p->global_next)
86 if(subnet->net.ipv4.mask >= p->net.ipv4.mask)
93 if(p == subnet_list[subnet->type]) /* First two cases */
96 subnet->global_next = subnet_list[subnet->type];
97 subnet->global_prev = NULL;
98 subnet_list[subnet->type] = subnet;
100 else /* Third case */
103 subnet->global_next = q->global_next;
104 subnet->global_prev = q;
105 q->global_next = subnet;
108 if(subnet->global_next)
109 subnet->global_next->global_prev = subnet;
113 void subnet_del(subnet_t *subnet)
116 /* Remove it from owner's list */
119 subnet->prev->next = subnet->next;
121 subnet->owner->subnets = subnet->next;
124 subnet->next->prev = subnet->prev;
126 /* Remove it from the global list */
128 if(subnet->global_prev)
129 subnet->global_prev->global_next = subnet->global_next;
131 subnet_list[subnet->type] = subnet->global_next;
133 if(subnet->global_next)
134 subnet->global_next->global_prev = subnet->global_prev;
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 for(subnet = subnet_list[SUBNET_MAC]; subnet != NULL; subnet = subnet->global_next)
257 if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
264 subnet_t *lookup_subnet_ipv4(ipv4_t address)
268 for(subnet = subnet_list[SUBNET_IPV4]; subnet != NULL; subnet = subnet->global_next)
270 if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
277 subnet_t *lookup_subnet_ipv6(ipv6_t address)
282 for(subnet = subnet_list[SUBNET_IPV6]; subnet != NULL; subnet = subnet->global_next)
285 if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])
294 void dump_subnet_list(void)
299 syslog(LOG_DEBUG, _("Subnet list:"));
301 for(subnet = subnet_list[SUBNET_IPV4]; subnet != NULL; subnet = subnet->global_next)
303 netstr = net2str(subnet);
304 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
308 syslog(LOG_DEBUG, _("End of subnet list."));