2 subnet_parse.c -- handle subnet parsing
3 Copyright (C) 2000-2012 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 /* Subnet mask handling */
32 int maskcmp(const void *va, const void *vb, int masklen) {
37 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
44 return (a[i] & (0x100 - (1 << (8 - m)))) -
45 (b[i] & (0x100 - (1 << (8 - m))));
50 void mask(void *va, int masklen, int len) {
58 a[i++] &= (0x100 - (1 << (8 - masklen)));
64 void maskcpy(void *va, const void *vb, int masklen, int len) {
69 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
73 a[i] = b[i] & (0x100 - (1 << (8 - m)));
81 bool maskcheck(const void *va, int masklen, int len) {
88 if(masklen && a[i++] & (0xff >> masklen))
98 /* Subnet comparison */
100 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
103 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof a->net.mac.address);
108 result = a->weight - b->weight;
110 if(result || !a->owner || !b->owner)
113 return strcmp(a->owner->name, b->owner->name);
116 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
119 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
124 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
129 result = a->weight - b->weight;
131 if(result || !a->owner || !b->owner)
134 return strcmp(a->owner->name, b->owner->name);
137 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
140 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
145 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
150 result = a->weight - b->weight;
152 if(result || !a->owner || !b->owner)
155 return strcmp(a->owner->name, b->owner->name);
158 int subnet_compare(const subnet_t *a, const subnet_t *b) {
161 result = a->type - b->type;
168 return subnet_compare_mac(a, b);
170 return subnet_compare_ipv4(a, b);
172 return subnet_compare_ipv6(a, b);
174 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
181 /* Ascii representation of subnets */
183 bool str2net(subnet_t *subnet, const char *subnetstr) {
188 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu/%d#%d",
189 &x[0], &x[1], &x[2], &x[3], &l, &weight) >= 5) {
193 subnet->type = SUBNET_IPV4;
194 subnet->net.ipv4.prefixlength = l;
195 subnet->weight = weight;
197 for(int i = 0; i < 4; i++) {
200 subnet->net.ipv4.address.x[i] = x[i];
206 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
207 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
212 subnet->type = SUBNET_IPV6;
213 subnet->net.ipv6.prefixlength = l;
214 subnet->weight = weight;
216 for(i = 0; i < 8; i++)
217 subnet->net.ipv6.address.x[i] = htons(x[i]);
222 if(sscanf(subnetstr, "%hu.%hu.%hu.%hu#%d", &x[0], &x[1], &x[2], &x[3], &weight) >= 4) {
223 subnet->type = SUBNET_IPV4;
224 subnet->net.ipv4.prefixlength = 32;
225 subnet->weight = weight;
227 for(i = 0; i < 4; i++) {
230 subnet->net.ipv4.address.x[i] = x[i];
236 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx#%d",
237 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &weight) >= 8) {
238 subnet->type = SUBNET_IPV6;
239 subnet->net.ipv6.prefixlength = 128;
240 subnet->weight = weight;
242 for(i = 0; i < 8; i++)
243 subnet->net.ipv6.address.x[i] = htons(x[i]);
248 if(sscanf(subnetstr, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
249 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &weight) >= 6) {
250 subnet->type = SUBNET_MAC;
251 subnet->weight = weight;
253 for(i = 0; i < 6; i++)
254 subnet->net.mac.address.x[i] = x[i];
260 if(strstr(subnetstr, "::")) {
265 // Count number of colons
266 for(p = subnetstr; *p; p++)
273 // Scan numbers before the double colon
275 for(i = 0; i < colons; i++) {
278 x[i] = strtoul(p, &q, 0x10);
279 if(!q || p == q || *q != ':')
291 if(!*p || *p == '/' || *p == '#')
294 // Fill in the blanks
295 for(; i < 8 - colons; i++)
298 // Scan the remaining numbers
300 x[i] = strtoul(p, &q, 0x10);
314 sscanf(p, "/%d#%d", &l, &weight);
316 sscanf(p, "#%d", &weight);
321 subnet->type = SUBNET_IPV6;
322 subnet->net.ipv6.prefixlength = l;
323 subnet->weight = weight;
325 for(i = 0; i < 8; i++)
326 subnet->net.ipv6.address.x[i] = htons(x[i]);
334 bool net2str(char *netstr, int len, const subnet_t *subnet) {
335 if(!netstr || !subnet) {
336 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
340 switch (subnet->type) {
342 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
343 subnet->net.mac.address.x[0],
344 subnet->net.mac.address.x[1],
345 subnet->net.mac.address.x[2],
346 subnet->net.mac.address.x[3],
347 subnet->net.mac.address.x[4],
348 subnet->net.mac.address.x[5],
353 snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
354 subnet->net.ipv4.address.x[0],
355 subnet->net.ipv4.address.x[1],
356 subnet->net.ipv4.address.x[2],
357 subnet->net.ipv4.address.x[3],
358 subnet->net.ipv4.prefixlength,
363 snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
364 ntohs(subnet->net.ipv6.address.x[0]),
365 ntohs(subnet->net.ipv6.address.x[1]),
366 ntohs(subnet->net.ipv6.address.x[2]),
367 ntohs(subnet->net.ipv6.address.x[3]),
368 ntohs(subnet->net.ipv6.address.x[4]),
369 ntohs(subnet->net.ipv6.address.x[5]),
370 ntohs(subnet->net.ipv6.address.x[6]),
371 ntohs(subnet->net.ipv6.address.x[7]),
372 subnet->net.ipv6.prefixlength,
377 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);