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 /* Changing this default will affect ADD_SUBNET messages - beware of inconsistencies between versions */
31 static const int DEFAULT_WEIGHT = 10;
33 /* Subnet mask handling */
35 int maskcmp(const void *va, const void *vb, int masklen) {
40 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
49 return (a[i] & (0x100 - (1 << (8 - m)))) -
50 (b[i] & (0x100 - (1 << (8 - m))));
55 void mask(void *va, int masklen, int len) {
63 a[i++] &= (0x100 - (1 << (8 - masklen)));
71 void maskcpy(void *va, const void *vb, int masklen, int len) {
76 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
81 a[i] = b[i] & (0x100 - (1 << (8 - m)));
90 bool subnetcheck(const subnet_t subnet) {
91 if(((subnet.type == SUBNET_IPV4)
92 && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(subnet.net.ipv4.address)))
93 || ((subnet.type == SUBNET_IPV6)
94 && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(subnet.net.ipv6.address)))) {
101 bool maskcheck(const void *va, int masklen, int len) {
108 if(masklen && a[i++] & (0xff >> masklen)) {
120 /* Subnet comparison */
122 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
125 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(a->net.mac.address));
131 result = a->weight - b->weight;
133 if(result || !a->owner || !b->owner) {
137 return strcmp(a->owner->name, b->owner->name);
140 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
143 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
149 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
155 result = a->weight - b->weight;
157 if(result || !a->owner || !b->owner) {
161 return strcmp(a->owner->name, b->owner->name);
164 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
167 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
173 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
179 result = a->weight - b->weight;
181 if(result || !a->owner || !b->owner) {
185 return strcmp(a->owner->name, b->owner->name);
188 int subnet_compare(const subnet_t *a, const subnet_t *b) {
191 result = a->type - b->type;
199 return subnet_compare_mac(a, b);
202 return subnet_compare_ipv4(a, b);
205 return subnet_compare_ipv6(a, b);
208 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
215 /* Ascii representation of subnets */
217 bool str2net(subnet_t *subnet, const char *subnetstr) {
219 strncpy(str, subnetstr, sizeof(str));
220 str[sizeof(str) - 1] = 0;
223 int weight = DEFAULT_WEIGHT;
224 char *weight_separator = strchr(str, '#');
226 if(weight_separator) {
227 char *weight_str = weight_separator + 1;
229 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
233 if(weight_str[consumed]) {
237 *weight_separator = 0;
240 int prefixlength = -1;
241 char *prefixlength_separator = strchr(str, '/');
243 if(prefixlength_separator) {
244 char *prefixlength_str = prefixlength_separator + 1;
246 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
250 if(prefixlength_str[consumed]) {
254 *prefixlength_separator = 0;
256 if(prefixlength < 0) {
263 if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx%n", &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &consumed) >= 6 && !str[consumed]) {
265 Normally we should check that each part has two digits to prevent ambiguities.
266 However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
267 so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
269 if(prefixlength >= 0) {
273 subnet->type = SUBNET_MAC;
274 subnet->weight = weight;
276 for(int i = 0; i < 6; i++) {
277 subnet->net.mac.address.x[i] = x[i];
283 if(sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
284 if(prefixlength == -1) {
288 if(prefixlength > 32) {
292 subnet->type = SUBNET_IPV4;
293 subnet->net.ipv4.prefixlength = prefixlength;
294 subnet->weight = weight;
296 for(int i = 0; i < 4; i++) {
301 subnet->net.ipv4.address.x[i] = x[i];
309 char *last_colon = strrchr(str, ':');
311 if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
312 /* Dotted quad suffix notation, convert to standard IPv6 notation */
313 for(int i = 0; i < 4; i++)
318 snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
321 char *double_colon = strstr(str, "::");
324 /* Figure out how many zero groups we need to expand */
325 int zero_group_count = 8;
327 for(const char *cur = str; *cur; cur++)
331 while(cur[1] && cur[1] != ':') {
336 if(zero_group_count < 1) {
340 /* Split the double colon in the middle to make room for zero groups */
342 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
344 /* Write zero groups in the resulting gap, overwriting the second colon */
345 for(int i = 0; i < zero_group_count; i++) {
346 memcpy(&double_colon[i * 2], "0:", 2);
349 /* Remove any leading or trailing colons */
351 memmove(&str[0], &str[1], strlen(&str[1]) + 1);
354 if(str[strlen(str) - 1] == ':') {
355 str[strlen(str) - 1] = 0;
359 if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
360 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
361 if(prefixlength == -1) {
365 if(prefixlength > 128) {
369 subnet->type = SUBNET_IPV6;
370 subnet->net.ipv6.prefixlength = prefixlength;
371 subnet->weight = weight;
373 for(int i = 0; i < 8; i++) {
374 subnet->net.ipv6.address.x[i] = htons(x[i]);
383 bool net2str(char *netstr, int len, const subnet_t *subnet) {
384 if(!netstr || !subnet) {
385 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", (void *)netstr, (void *)subnet);
390 int prefixlength = -1;
392 switch(subnet->type) {
394 result = snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
395 subnet->net.mac.address.x[0],
396 subnet->net.mac.address.x[1],
397 subnet->net.mac.address.x[2],
398 subnet->net.mac.address.x[3],
399 subnet->net.mac.address.x[4],
400 subnet->net.mac.address.x[5]);
406 result = snprintf(netstr, len, "%u.%u.%u.%u",
407 subnet->net.ipv4.address.x[0],
408 subnet->net.ipv4.address.x[1],
409 subnet->net.ipv4.address.x[2],
410 subnet->net.ipv4.address.x[3]);
413 prefixlength = subnet->net.ipv4.prefixlength;
415 if(prefixlength == 32) {
422 /* Find the longest sequence of consecutive zeroes */
423 int max_zero_length = 0;
424 int max_zero_length_index = 0;
425 int current_zero_length = 0;
426 int current_zero_length_index = 0;
428 for(int i = 0; i < 8; i++) {
429 if(subnet->net.ipv6.address.x[i] != 0) {
430 current_zero_length = 0;
432 if(current_zero_length == 0) {
433 current_zero_length_index = i;
436 current_zero_length++;
438 if(current_zero_length > max_zero_length) {
439 max_zero_length = current_zero_length;
440 max_zero_length_index = current_zero_length_index;
445 /* Print the address */
446 for(int i = 0; i < 8;) {
447 if(max_zero_length > 1 && max_zero_length_index == i) {
448 /* Shorten the representation as per RFC 5952 */
449 const char *const FORMATS[] = { "%.1s", "%.2s", "%.3s" };
450 const char *const *format = &FORMATS[0];
456 if(i + max_zero_length == 8) {
460 result = snprintf(netstr, len, *format, ":::");
461 i += max_zero_length;
463 result = snprintf(netstr, len, "%x:", ntohs(subnet->net.ipv6.address.x[i]));
471 /* Remove the trailing colon */
476 prefixlength = subnet->net.ipv6.prefixlength;
478 if(prefixlength == 128) {
486 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
490 if(prefixlength >= 0) {
491 result = snprintf(netstr, len, "/%d", prefixlength);
496 if(subnet->weight != DEFAULT_WEIGHT) {
497 snprintf(netstr, len, "#%d", subnet->weight);