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 maskcheck(const void *va, int masklen, int len) {
97 if(masklen && a[i++] & (0xff >> masklen)) {
109 /* Subnet comparison */
111 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
114 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(a->net.mac.address));
120 result = a->weight - b->weight;
122 if(result || !a->owner || !b->owner) {
126 return strcmp(a->owner->name, b->owner->name);
129 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
132 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
138 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
144 result = a->weight - b->weight;
146 if(result || !a->owner || !b->owner) {
150 return strcmp(a->owner->name, b->owner->name);
153 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
156 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
162 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
168 result = a->weight - b->weight;
170 if(result || !a->owner || !b->owner) {
174 return strcmp(a->owner->name, b->owner->name);
177 int subnet_compare(const subnet_t *a, const subnet_t *b) {
180 result = a->type - b->type;
188 return subnet_compare_mac(a, b);
191 return subnet_compare_ipv4(a, b);
194 return subnet_compare_ipv6(a, b);
197 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
204 /* Ascii representation of subnets */
206 bool str2net(subnet_t *subnet, const char *subnetstr) {
208 strncpy(str, subnetstr, sizeof(str));
209 str[sizeof(str) - 1] = 0;
212 int weight = DEFAULT_WEIGHT;
213 char *weight_separator = strchr(str, '#');
215 if(weight_separator) {
216 char *weight_str = weight_separator + 1;
218 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
222 if(weight_str[consumed]) {
226 *weight_separator = 0;
229 int prefixlength = -1;
230 char *prefixlength_separator = strchr(str, '/');
232 if(prefixlength_separator) {
233 char *prefixlength_str = prefixlength_separator + 1;
235 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
239 if(prefixlength_str[consumed]) {
243 *prefixlength_separator = 0;
245 if(prefixlength < 0) {
252 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]) {
254 Normally we should check that each part has two digits to prevent ambiguities.
255 However, in old tinc versions net2str() will agressively return MAC addresses with one-digit parts,
256 so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
258 if(prefixlength >= 0) {
262 subnet->type = SUBNET_MAC;
263 subnet->weight = weight;
265 for(int i = 0; i < 6; i++) {
266 subnet->net.mac.address.x[i] = x[i];
272 if(sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
273 if(prefixlength == -1) {
277 if(prefixlength > 32) {
281 subnet->type = SUBNET_IPV4;
282 subnet->net.ipv4.prefixlength = prefixlength;
283 subnet->weight = weight;
285 for(int i = 0; i < 4; i++) {
290 subnet->net.ipv4.address.x[i] = x[i];
298 char *last_colon = strrchr(str, ':');
300 if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
301 /* Dotted quad suffix notation, convert to standard IPv6 notation */
302 for(int i = 0; i < 4; i++)
307 snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
310 char *double_colon = strstr(str, "::");
313 /* Figure out how many zero groups we need to expand */
314 int zero_group_count = 8;
316 for(const char *cur = str; *cur; cur++)
320 while(cur[1] && cur[1] != ':') {
325 if(zero_group_count < 1) {
329 /* Split the double colon in the middle to make room for zero groups */
331 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
333 /* Write zero groups in the resulting gap, overwriting the second colon */
334 for(int i = 0; i < zero_group_count; i++) {
335 memcpy(&double_colon[i * 2], "0:", 2);
338 /* Remove any leading or trailing colons */
340 memmove(&str[0], &str[1], strlen(&str[1]) + 1);
343 if(str[strlen(str) - 1] == ':') {
344 str[strlen(str) - 1] = 0;
348 if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
349 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
350 if(prefixlength == -1) {
354 if(prefixlength > 128) {
358 subnet->type = SUBNET_IPV6;
359 subnet->net.ipv6.prefixlength = prefixlength;
360 subnet->weight = weight;
362 for(int i = 0; i < 8; i++) {
363 subnet->net.ipv6.address.x[i] = htons(x[i]);
372 bool net2str(char *netstr, int len, const subnet_t *subnet) {
373 if(!netstr || !subnet) {
374 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
379 int prefixlength = -1;
381 switch(subnet->type) {
383 result = snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
384 subnet->net.mac.address.x[0],
385 subnet->net.mac.address.x[1],
386 subnet->net.mac.address.x[2],
387 subnet->net.mac.address.x[3],
388 subnet->net.mac.address.x[4],
389 subnet->net.mac.address.x[5]);
395 result = snprintf(netstr, len, "%u.%u.%u.%u",
396 subnet->net.ipv4.address.x[0],
397 subnet->net.ipv4.address.x[1],
398 subnet->net.ipv4.address.x[2],
399 subnet->net.ipv4.address.x[3]);
402 prefixlength = subnet->net.ipv4.prefixlength;
404 if(prefixlength == 32) {
411 /* Find the longest sequence of consecutive zeroes */
412 int max_zero_length = 0;
413 int max_zero_length_index = 0;
414 int current_zero_length = 0;
415 int current_zero_length_index = 0;
417 for(int i = 0; i < 8; i++) {
418 if(subnet->net.ipv6.address.x[i] != 0) {
419 current_zero_length = 0;
421 if(current_zero_length == 0) {
422 current_zero_length_index = i;
425 current_zero_length++;
427 if(current_zero_length > max_zero_length) {
428 max_zero_length = current_zero_length;
429 max_zero_length_index = current_zero_length_index;
434 /* Print the address */
435 for(int i = 0; i < 8;) {
436 if(max_zero_length > 1 && max_zero_length_index == i) {
437 /* Shorten the representation as per RFC 5952 */
438 const char *const FORMATS[] = { "%.1s", "%.2s", "%.3s" };
439 const char *const *format = &FORMATS[0];
445 if(i + max_zero_length == 8) {
449 result = snprintf(netstr, len, *format, ":::");
450 i += max_zero_length;
452 result = snprintf(netstr, len, "%x:", ntohs(subnet->net.ipv6.address.x[i]));
460 /* Remove the trailing colon */
465 prefixlength = subnet->net.ipv6.prefixlength;
467 if(prefixlength == 128) {
475 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
479 if(prefixlength >= 0) {
480 result = snprintf(netstr, len, "/%d", prefixlength);
485 if(subnet->weight != DEFAULT_WEIGHT) {
486 snprintf(netstr, len, "#%d", subnet->weight);