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++) {
47 return (a[i] & (0x100 - (1 << (8 - m)))) -
48 (b[i] & (0x100 - (1 << (8 - m))));
53 void mask(void *va, int masklen, int len) {
61 a[i++] &= (0x100 - (1 << (8 - masklen)));
67 void maskcpy(void *va, const void *vb, int masklen, int len) {
72 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
76 a[i] = b[i] & (0x100 - (1 << (8 - m)));
84 bool maskcheck(const void *va, int masklen, int len) {
91 if(masklen && a[i++] & (0xff >> masklen))
101 /* Subnet comparison */
103 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
106 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof a->net.mac.address);
111 result = a->weight - b->weight;
113 if(result || !a->owner || !b->owner)
116 return strcmp(a->owner->name, b->owner->name);
119 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
122 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
127 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
132 result = a->weight - b->weight;
134 if(result || !a->owner || !b->owner)
137 return strcmp(a->owner->name, b->owner->name);
140 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
143 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
148 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
153 result = a->weight - b->weight;
155 if(result || !a->owner || !b->owner)
158 return strcmp(a->owner->name, b->owner->name);
161 int subnet_compare(const subnet_t *a, const subnet_t *b) {
164 result = a->type - b->type;
171 return subnet_compare_mac(a, b);
173 return subnet_compare_ipv4(a, b);
175 return subnet_compare_ipv6(a, b);
177 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
184 /* Ascii representation of subnets */
186 bool str2net(subnet_t *subnet, const char *subnetstr) {
188 strncpy(str, subnetstr, sizeof(str));
191 int weight = DEFAULT_WEIGHT;
192 char *weight_separator = strchr(str, '#');
193 if (weight_separator) {
194 char *weight_str = weight_separator + 1;
195 if (sscanf(weight_str, "%d%n", &weight, &consumed) < 1)
197 if (weight_str[consumed])
199 *weight_separator = 0;
202 int prefixlength = -1;
203 char *prefixlength_separator = strchr(str, '/');
204 if (prefixlength_separator) {
205 char* prefixlength_str = prefixlength_separator + 1;
206 if (sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1)
208 if (prefixlength_str[consumed])
210 *prefixlength_separator = 0;
212 if (prefixlength < 0)
217 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]) {
219 Normally we should check that each part has two digits to prevent ambiguities.
220 However, in old tinc versions net2str() will agressively return MAC addresses with one-digit parts,
221 so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
223 if (prefixlength >= 0)
226 subnet->type = SUBNET_MAC;
227 subnet->weight = weight;
228 for(int i = 0; i < 6; i++)
229 subnet->net.mac.address.x[i] = x[i];
233 if (sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
234 if (prefixlength == -1)
236 if (prefixlength > 32)
239 subnet->type = SUBNET_IPV4;
240 subnet->net.ipv4.prefixlength = prefixlength;
241 subnet->weight = weight;
242 for(int i = 0; i < 4; i++) {
245 subnet->net.ipv4.address.x[i] = x[i];
252 char* last_colon = strrchr(str, ':');
253 if (last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
254 /* Dotted quad suffix notation, convert to standard IPv6 notation */
255 for (int i = 0; i < 4; i++)
258 sprintf(last_colon, ":%02hx%02hx:%02hx%02hx", x[0], x[1], x[2], x[3]);
261 char* double_colon = strstr(str, "::");
263 /* Figure out how many zero groups we need to expand */
264 int zero_group_count = 8;
265 for (const char* cur = str; *cur; *cur && cur++)
268 for (; *cur && *cur != ':'; cur++);
270 if (zero_group_count < 1)
273 /* Split the double colon in the middle to make room for zero groups */
275 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
277 /* Write zero groups in the resulting gap, overwriting the second colon */
278 for (int i = 0; i < zero_group_count; i++)
279 memcpy(&double_colon[i * 2], "0:", 2);
281 /* Remove any leading or trailing colons */
283 memmove(&str[0], &str[1], strlen(&str[1]) + 1);
284 if (str[strlen(str) - 1] == ':')
285 str[strlen(str) - 1] = 0;
288 if (sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
289 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
290 if (prefixlength == -1)
292 if (prefixlength > 128)
295 subnet->type = SUBNET_IPV6;
296 subnet->net.ipv6.prefixlength = prefixlength;
297 subnet->weight = weight;
298 for(int i = 0; i < 8; i++)
299 subnet->net.ipv6.address.x[i] = htons(x[i]);
306 bool net2str(char *netstr, int len, const subnet_t *subnet) {
307 if(!netstr || !subnet) {
308 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
313 int prefixlength = -1;
314 switch (subnet->type) {
316 result = snprintf(netstr, len, "%02hx:%02hx:%02hx:%02hx:%02hx:%02hx",
317 subnet->net.mac.address.x[0],
318 subnet->net.mac.address.x[1],
319 subnet->net.mac.address.x[2],
320 subnet->net.mac.address.x[3],
321 subnet->net.mac.address.x[4],
322 subnet->net.mac.address.x[5],
329 result = snprintf(netstr, len, "%hu.%hu.%hu.%hu",
330 subnet->net.ipv4.address.x[0],
331 subnet->net.ipv4.address.x[1],
332 subnet->net.ipv4.address.x[2],
333 subnet->net.ipv4.address.x[3]);
336 prefixlength = subnet->net.ipv4.prefixlength;
337 if (prefixlength == 32)
342 /* Find the longest sequence of consecutive zeroes */
343 int max_zero_length = 0;
344 int max_zero_length_index = 0;
345 int current_zero_length = 0;
346 int current_zero_length_index = 0;
347 for (int i = 0; i < 8; i++) {
348 if (subnet->net.ipv6.address.x[i] != 0)
349 current_zero_length = 0;
351 if (current_zero_length == 0)
352 current_zero_length_index = i;
353 current_zero_length++;
354 if (current_zero_length > max_zero_length) {
355 max_zero_length = current_zero_length;
356 max_zero_length_index = current_zero_length_index;
361 /* Print the address */
362 for (int i = 0; i < 8;) {
363 if (max_zero_length > 1 && max_zero_length_index == i) {
364 /* Shorten the representation as per RFC 5952 */
365 const char* const FORMATS[] = { "%.1s", "%.2s", "%.3s" };
366 const char* const* format = &FORMATS[0];
369 if (i + max_zero_length == 8)
371 result = snprintf(netstr, len, *format, ":::");
372 i += max_zero_length;
374 result = snprintf(netstr, len, "%hx:", ntohs(subnet->net.ipv6.address.x[i]));
381 /* Remove the trailing colon */
386 prefixlength = subnet->net.ipv6.prefixlength;
387 if (prefixlength == 128)
393 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
397 if (prefixlength >= 0) {
398 result = snprintf(netstr, len, "/%d", prefixlength);
403 if (subnet->weight != DEFAULT_WEIGHT) {
404 snprintf(netstr, len, "#%d", subnet->weight);