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.5 2000/10/24 15:46:18 guus Exp $
30 /* Allocating and freeing space for subnets */
32 subnet_t *new_subnet(void)
35 return (subnet_t *)xmalloc(sizeof(subnet_t));
38 void free_subnet(subnet_t *subnet)
44 /* Linked list management */
46 void subnet_add(conn_list_t *cl, subnet_t *subnet)
53 /* Sort on size of subnet mask (IPv4 only at the moment!)
55 Three cases: cl->subnets = NULL -> just add this subnet
56 insert before first -> add it in front of list
57 rest: insert after another subnet
66 if(subnet->net.ipv4.mask >= p->net.ipv4.mask)
73 if(!cl->subnets || p == cl->subnets) /* First two cases */
76 subnet->next = cl->subnets;
83 subnet->next = q->next;
89 subnet->next->prev = subnet;
93 void subnet_del(subnet_t *subnet)
97 subnet->prev->next = subnet->next;
99 subnet->owner->subnets = subnet->next;
102 subnet->next->prev = subnet->prev;
108 /* Ascii representation of subnets */
110 subnet_t *str2net(char *subnetstr)
115 if(sscanf(subnetstr, "%d,", &type) != 1)
118 subnet = new_subnet();
123 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
124 &subnet->net.mac.address.x[0],
125 &subnet->net.mac.address.x[1],
126 &subnet->net.mac.address.x[2],
127 &subnet->net.mac.address.x[3],
128 &subnet->net.mac.address.x[4],
129 &subnet->net.mac.address.x[5]) != 7)
136 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
143 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
144 &subnet->net.ipv6.address.x[0],
145 &subnet->net.ipv6.address.x[1],
146 &subnet->net.ipv6.address.x[2],
147 &subnet->net.ipv6.address.x[3],
148 &subnet->net.ipv6.address.x[4],
149 &subnet->net.ipv6.address.x[5],
150 &subnet->net.ipv6.address.x[6],
151 &subnet->net.ipv6.address.x[7],
152 &subnet->net.ipv6.mask.x[0],
153 &subnet->net.ipv6.mask.x[1],
154 &subnet->net.ipv6.mask.x[2],
155 &subnet->net.ipv6.mask.x[3],
156 &subnet->net.ipv6.mask.x[4],
157 &subnet->net.ipv6.mask.x[5],
158 &subnet->net.ipv6.mask.x[6],
159 &subnet->net.ipv6.mask.x[7]) != 17)
174 char *net2str(subnet_t *subnet)
181 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
182 subnet->net.mac.address.x[0],
183 subnet->net.mac.address.x[1],
184 subnet->net.mac.address.x[2],
185 subnet->net.mac.address.x[3],
186 subnet->net.mac.address.x[4],
187 subnet->net.mac.address.x[5]);
190 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
193 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
194 subnet->net.ipv6.address.x[0],
195 subnet->net.ipv6.address.x[1],
196 subnet->net.ipv6.address.x[2],
197 subnet->net.ipv6.address.x[3],
198 subnet->net.ipv6.address.x[4],
199 subnet->net.ipv6.address.x[5],
200 subnet->net.ipv6.address.x[6],
201 subnet->net.ipv6.address.x[7],
202 subnet->net.ipv6.mask.x[0],
203 subnet->net.ipv6.mask.x[1],
204 subnet->net.ipv6.mask.x[2],
205 subnet->net.ipv6.mask.x[3],
206 subnet->net.ipv6.mask.x[4],
207 subnet->net.ipv6.mask.x[5],
208 subnet->net.ipv6.mask.x[6],
209 subnet->net.ipv6.mask.x[7]);
218 /* Subnet lookup routines */
220 subnet_t *lookup_subnet_mac(subnet_t *subnets, mac_t address)
224 for(subnet = subnets; subnet != NULL; subnet = subnet->next)
226 if(subnet->type == SUBNET_MAC)
227 if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
234 subnet_t *lookup_subnet_ipv4(subnet_t *subnets, ipv4_t address)
238 for(subnet = subnets; subnet != NULL; subnet = subnet->next)
240 if(subnet->type == SUBNET_IPV4)
241 if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
248 subnet_t *lookup_subnet_ipv6(subnet_t *subnets, ipv6_t address)
253 for(subnet = subnets; subnet != NULL; subnet = subnet->next)
255 if(subnet->type == SUBNET_IPV6)
258 if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])