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.6 2000/10/28 16:41:40 guus Exp $
30 /* lists type of subnet */
32 subnet_t *subnet_list[SUBNET_TYPES] = { NULL };
34 /* Allocating and freeing space for subnets */
36 subnet_t *new_subnet(void)
39 return (subnet_t *)xmalloc(sizeof(subnet_t));
42 void free_subnet(subnet_t *subnet)
48 /* Linked list management */
50 void subnet_add(conn_list_t *cl, subnet_t *subnet)
57 /* Link it into the owners list of subnets (unsorted) */
59 subnet->next = cl->subnets->next;
62 subnet->next->prev = subnet;
65 /* And now add it to the global subnet list (sorted) */
67 /* Sort on size of subnet mask (IPv4 only at the moment!)
69 Three cases: subnet_list[] = NULL -> just add this subnet
70 insert before first -> add it in front of list
71 rest: insert after another subnet
74 if(subnet_list[subnet->type])
76 p = q = subnet_list[subnet->type];
78 for(; p; p = p->global_next)
80 if(subnet->net.ipv4.mask >= p->net.ipv4.mask)
87 if(!subnet_list[subnet->type] || p == subnet_list[subnet->type]) /* First two cases */
90 subnet->global_next = subnet_list[subnet->type];
91 subnet->global_prev = NULL;
92 subnet_list[subnet->type] = subnet;
97 subnet->global_next = q->global_next;
98 subnet->global_prev = q;
99 q->global_next = subnet;
102 if(subnet->global_next)
103 subnet->global_next->global_prev = subnet;
107 void subnet_del(subnet_t *subnet)
110 /* Remove it from owner's list */
114 subnet->prev->next = subnet->next;
118 subnet->owner->subnets = subnet->next;
121 subnet->next->prev = subnet->prev;
123 /* Remove it from the global list */
125 if(subnet->global_prev)
127 subnet->global_prev->global_next = subnet->global_next;
131 subnet_list[subnet->type] = 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)
206 char *net2str(subnet_t *subnet)
213 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
214 subnet->net.mac.address.x[0],
215 subnet->net.mac.address.x[1],
216 subnet->net.mac.address.x[2],
217 subnet->net.mac.address.x[3],
218 subnet->net.mac.address.x[4],
219 subnet->net.mac.address.x[5]);
221 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
223 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
224 subnet->net.ipv6.address.x[0],
225 subnet->net.ipv6.address.x[1],
226 subnet->net.ipv6.address.x[2],
227 subnet->net.ipv6.address.x[3],
228 subnet->net.ipv6.address.x[4],
229 subnet->net.ipv6.address.x[5],
230 subnet->net.ipv6.address.x[6],
231 subnet->net.ipv6.address.x[7],
232 subnet->net.ipv6.mask.x[0],
233 subnet->net.ipv6.mask.x[1],
234 subnet->net.ipv6.mask.x[2],
235 subnet->net.ipv6.mask.x[3],
236 subnet->net.ipv6.mask.x[4],
237 subnet->net.ipv6.mask.x[5],
238 subnet->net.ipv6.mask.x[6],
239 subnet->net.ipv6.mask.x[7]);
247 /* Subnet lookup routines */
249 subnet_t *lookup_subnet_mac(mac_t address)
253 for(subnet = subnet_list[SUBNET_MAC]; subnet != NULL; subnet = subnet->next)
255 if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
262 subnet_t *lookup_subnet_ipv4(ipv4_t address)
266 for(subnet = subnet_list[SUBNET_IPV4]; subnet != NULL; subnet = subnet->next)
268 if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
275 subnet_t *lookup_subnet_ipv6(ipv6_t address)
280 for(subnet = subnet_list[SUBNET_IPV6]; subnet != NULL; subnet = subnet->next)
283 if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])