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.4 2000/10/15 00:59:37 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)
49 /* FIXME: do sorting on netmask size if necessary */
51 subnet->next = cl->subnets->next;
53 subnet->next->prev = subnet;
58 void subnet_del(subnet_t *subnet)
63 subnet->prev->next = subnet->next;
67 subnet->owner->subnets = subnet->next;
70 subnet->next->prev = subnet->prev;
75 /* Ascii representation of subnets */
77 subnet_t *str2net(char *subnetstr)
82 if(sscanf(subnetstr, "%d,", &type) != 1)
85 subnet = new_subnet();
90 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
91 &subnet->net.mac.address.x[0],
92 &subnet->net.mac.address.x[1],
93 &subnet->net.mac.address.x[2],
94 &subnet->net.mac.address.x[3],
95 &subnet->net.mac.address.x[4],
96 &subnet->net.mac.address.x[5]) != 7)
103 if(sscanf(subnetstr, "%d,%lx:%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
110 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
111 &subnet->net.ipv6.address.x[0],
112 &subnet->net.ipv6.address.x[1],
113 &subnet->net.ipv6.address.x[2],
114 &subnet->net.ipv6.address.x[3],
115 &subnet->net.ipv6.address.x[4],
116 &subnet->net.ipv6.address.x[5],
117 &subnet->net.ipv6.address.x[6],
118 &subnet->net.ipv6.address.x[7],
119 &subnet->net.ipv6.mask.x[0],
120 &subnet->net.ipv6.mask.x[1],
121 &subnet->net.ipv6.mask.x[2],
122 &subnet->net.ipv6.mask.x[3],
123 &subnet->net.ipv6.mask.x[4],
124 &subnet->net.ipv6.mask.x[5],
125 &subnet->net.ipv6.mask.x[6],
126 &subnet->net.ipv6.mask.x[7]) != 17)
141 char *net2str(subnet_t *subnet)
148 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
149 subnet->net.mac.address.x[0],
150 subnet->net.mac.address.x[1],
151 subnet->net.mac.address.x[2],
152 subnet->net.mac.address.x[3],
153 subnet->net.mac.address.x[4],
154 subnet->net.mac.address.x[5]);
156 asprintf(&netstr, "%d,%lx:%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
158 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
159 subnet->net.ipv6.address.x[0],
160 subnet->net.ipv6.address.x[1],
161 subnet->net.ipv6.address.x[2],
162 subnet->net.ipv6.address.x[3],
163 subnet->net.ipv6.address.x[4],
164 subnet->net.ipv6.address.x[5],
165 subnet->net.ipv6.address.x[6],
166 subnet->net.ipv6.address.x[7],
167 subnet->net.ipv6.mask.x[0],
168 subnet->net.ipv6.mask.x[1],
169 subnet->net.ipv6.mask.x[2],
170 subnet->net.ipv6.mask.x[3],
171 subnet->net.ipv6.mask.x[4],
172 subnet->net.ipv6.mask.x[5],
173 subnet->net.ipv6.mask.x[6],
174 subnet->net.ipv6.mask.x[7]);
182 /* Subnet lookup routines */
184 subnet_t *lookup_subnet_mac(subnet_t *subnets, mac_t address)
188 for(subnet = subnets; subnet != NULL; subnet = subnet->next)
190 if(subnet->type == SUBNET_MAC)
191 if(memcmp(&address, &subnet->net.mac.address, sizeof(address)) == 0)
198 subnet_t *lookup_subnet_ipv4(subnet_t *subnets, ipv4_t address)
202 for(subnet = subnets; subnet != NULL; subnet = subnet->next)
204 if(subnet->type == SUBNET_IPV4)
205 if((address & subnet->net.ipv4.mask) == subnet->net.ipv4.address)
212 subnet_t *lookup_subnet_ipv6(subnet_t *subnets, ipv6_t address)
217 for(subnet = subnets; subnet != NULL; subnet = subnet->next)
219 if(subnet->type == SUBNET_IPV6)
222 if((address.x[i] & subnet->net.ipv6.mask.x[i]) != subnet->net.ipv6.address.x[i])