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));
189 str[sizeof str - 1] = 0;
192 int weight = DEFAULT_WEIGHT;
193 char *weight_separator = strchr(str, '#');
194 if (weight_separator) {
195 char *weight_str = weight_separator + 1;
196 if (sscanf(weight_str, "%d%n", &weight, &consumed) < 1)
198 if (weight_str[consumed])
200 *weight_separator = 0;
203 int prefixlength = -1;
204 char *prefixlength_separator = strchr(str, '/');
205 if (prefixlength_separator) {
206 char* prefixlength_str = prefixlength_separator + 1;
207 if (sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1)
209 if (prefixlength_str[consumed])
211 *prefixlength_separator = 0;
213 if (prefixlength < 0)
218 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]) {
220 Normally we should check that each part has two digits to prevent ambiguities.
221 However, in old tinc versions net2str() will agressively return MAC addresses with one-digit parts,
222 so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
224 if (prefixlength >= 0)
227 subnet->type = SUBNET_MAC;
228 subnet->weight = weight;
229 for(int i = 0; i < 6; i++)
230 subnet->net.mac.address.x[i] = x[i];
234 if (sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
235 if (prefixlength == -1)
237 if (prefixlength > 32)
240 subnet->type = SUBNET_IPV4;
241 subnet->net.ipv4.prefixlength = prefixlength;
242 subnet->weight = weight;
243 for(int i = 0; i < 4; i++) {
246 subnet->net.ipv4.address.x[i] = x[i];
253 char* last_colon = strrchr(str, ':');
254 if (last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
255 /* Dotted quad suffix notation, convert to standard IPv6 notation */
256 for (int i = 0; i < 4; i++)
259 snprintf(last_colon, sizeof str - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
262 char* double_colon = strstr(str, "::");
264 /* Figure out how many zero groups we need to expand */
265 int zero_group_count = 8;
266 for (const char* cur = str; *cur; cur++)
269 while(cur[1] && cur[1] != ':')
272 if (zero_group_count < 1)
275 /* Split the double colon in the middle to make room for zero groups */
277 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
279 /* Write zero groups in the resulting gap, overwriting the second colon */
280 for (int i = 0; i < zero_group_count; i++)
281 memcpy(&double_colon[i * 2], "0:", 2);
283 /* Remove any leading or trailing colons */
285 memmove(&str[0], &str[1], strlen(&str[1]) + 1);
286 if (str[strlen(str) - 1] == ':')
287 str[strlen(str) - 1] = 0;
290 if (sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
291 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
292 if (prefixlength == -1)
294 if (prefixlength > 128)
297 subnet->type = SUBNET_IPV6;
298 subnet->net.ipv6.prefixlength = prefixlength;
299 subnet->weight = weight;
300 for(int i = 0; i < 8; i++)
301 subnet->net.ipv6.address.x[i] = htons(x[i]);
308 bool net2str(char *netstr, int len, const subnet_t *subnet) {
309 if(!netstr || !subnet) {
310 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
315 int prefixlength = -1;
316 switch (subnet->type) {
318 result = snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
319 subnet->net.mac.address.x[0],
320 subnet->net.mac.address.x[1],
321 subnet->net.mac.address.x[2],
322 subnet->net.mac.address.x[3],
323 subnet->net.mac.address.x[4],
324 subnet->net.mac.address.x[5]);
330 result = snprintf(netstr, len, "%u.%u.%u.%u",
331 subnet->net.ipv4.address.x[0],
332 subnet->net.ipv4.address.x[1],
333 subnet->net.ipv4.address.x[2],
334 subnet->net.ipv4.address.x[3]);
337 prefixlength = subnet->net.ipv4.prefixlength;
338 if (prefixlength == 32)
343 /* Find the longest sequence of consecutive zeroes */
344 int max_zero_length = 0;
345 int max_zero_length_index = 0;
346 int current_zero_length = 0;
347 int current_zero_length_index = 0;
348 for (int i = 0; i < 8; i++) {
349 if (subnet->net.ipv6.address.x[i] != 0)
350 current_zero_length = 0;
352 if (current_zero_length == 0)
353 current_zero_length_index = i;
354 current_zero_length++;
355 if (current_zero_length > max_zero_length) {
356 max_zero_length = current_zero_length;
357 max_zero_length_index = current_zero_length_index;
362 /* Print the address */
363 for (int i = 0; i < 8;) {
364 if (max_zero_length > 1 && max_zero_length_index == i) {
365 /* Shorten the representation as per RFC 5952 */
366 const char* const FORMATS[] = { "%.1s", "%.2s", "%.3s" };
367 const char* const* format = &FORMATS[0];
370 if (i + max_zero_length == 8)
372 result = snprintf(netstr, len, *format, ":::");
373 i += max_zero_length;
375 result = snprintf(netstr, len, "%hx:", ntohs(subnet->net.ipv6.address.x[i]));
382 /* Remove the trailing colon */
387 prefixlength = subnet->net.ipv6.prefixlength;
388 if (prefixlength == 128)
394 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
398 if (prefixlength >= 0) {
399 result = snprintf(netstr, len, "/%d", prefixlength);
404 if (subnet->weight != DEFAULT_WEIGHT) {
405 snprintf(netstr, len, "#%d", subnet->weight);