Attempt to make the test suite work with Windows executables.
[tinc] / src / subnet_parse.c
1 /*
2     subnet_parse.c -- handle subnet parsing
3     Copyright (C) 2000-2012 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "logger.h"
24 #include "net.h"
25 #include "netutl.h"
26 #include "subnet.h"
27 #include "utils.h"
28 #include "xalloc.h"
29
30 /* Changing this default will affect ADD_SUBNET messages - beware of inconsistencies between versions */
31 static const int DEFAULT_WEIGHT = 10;
32
33 /* Subnet mask handling */
34
35 int maskcmp(const void *va, const void *vb, int masklen) {
36         int i, m, result;
37         const char *a = va;
38         const char *b = vb;
39
40         for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
41                 result = a[i] - b[i];
42
43                 if(result) {
44                         return result;
45                 }
46         }
47
48         if(m)
49                 return (a[i] & (0x100 - (1 << (8 - m)))) -
50                        (b[i] & (0x100 - (1 << (8 - m))));
51
52         return 0;
53 }
54
55 void mask(void *va, int masklen, int len) {
56         int i;
57         char *a = va;
58
59         i = masklen / 8;
60         masklen %= 8;
61
62         if(masklen) {
63                 a[i++] &= (0x100 - (1 << (8 - masklen)));
64         }
65
66         for(; i < len; i++) {
67                 a[i] = 0;
68         }
69 }
70
71 void maskcpy(void *va, const void *vb, int masklen, int len) {
72         int i, m;
73         char *a = va;
74         const char *b = vb;
75
76         for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
77                 a[i] = b[i];
78         }
79
80         if(m) {
81                 a[i] = b[i] & (0x100 - (1 << (8 - m)));
82                 i++;
83         }
84
85         for(; i < len; i++) {
86                 a[i] = 0;
87         }
88 }
89
90 bool maskcheck(const void *va, int masklen, int len) {
91         int i;
92         const char *a = va;
93
94         i = masklen / 8;
95         masklen %= 8;
96
97         if(masklen && a[i++] & (0xff >> masklen)) {
98                 return false;
99         }
100
101         for(; i < len; i++)
102                 if(a[i] != 0) {
103                         return false;
104                 }
105
106         return true;
107 }
108
109 /* Subnet comparison */
110
111 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
112         int result;
113
114         result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(a->net.mac.address));
115
116         if(result) {
117                 return result;
118         }
119
120         result = a->weight - b->weight;
121
122         if(result || !a->owner || !b->owner) {
123                 return result;
124         }
125
126         return strcmp(a->owner->name, b->owner->name);
127 }
128
129 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
130         int result;
131
132         result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
133
134         if(result) {
135                 return result;
136         }
137
138         result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
139
140         if(result) {
141                 return result;
142         }
143
144         result = a->weight - b->weight;
145
146         if(result || !a->owner || !b->owner) {
147                 return result;
148         }
149
150         return strcmp(a->owner->name, b->owner->name);
151 }
152
153 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
154         int result;
155
156         result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
157
158         if(result) {
159                 return result;
160         }
161
162         result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
163
164         if(result) {
165                 return result;
166         }
167
168         result = a->weight - b->weight;
169
170         if(result || !a->owner || !b->owner) {
171                 return result;
172         }
173
174         return strcmp(a->owner->name, b->owner->name);
175 }
176
177 int subnet_compare(const subnet_t *a, const subnet_t *b) {
178         int result;
179
180         result = a->type - b->type;
181
182         if(result) {
183                 return result;
184         }
185
186         switch(a->type) {
187         case SUBNET_MAC:
188                 return subnet_compare_mac(a, b);
189
190         case SUBNET_IPV4:
191                 return subnet_compare_ipv4(a, b);
192
193         case SUBNET_IPV6:
194                 return subnet_compare_ipv6(a, b);
195
196         default:
197                 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
198                 exit(1);
199         }
200
201         return 0;
202 }
203
204 /* Ascii representation of subnets */
205
206 bool str2net(subnet_t *subnet, const char *subnetstr) {
207         char str[1024];
208         strncpy(str, subnetstr, sizeof(str));
209         str[sizeof(str) - 1] = 0;
210         int consumed;
211
212         int weight = DEFAULT_WEIGHT;
213         char *weight_separator = strchr(str, '#');
214
215         if(weight_separator) {
216                 char *weight_str = weight_separator + 1;
217
218                 if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
219                         return false;
220                 }
221
222                 if(weight_str[consumed]) {
223                         return false;
224                 }
225
226                 *weight_separator = 0;
227         }
228
229         int prefixlength = -1;
230         char *prefixlength_separator = strchr(str, '/');
231
232         if(prefixlength_separator) {
233                 char *prefixlength_str = prefixlength_separator + 1;
234
235                 if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
236                         return false;
237                 }
238
239                 if(prefixlength_str[consumed]) {
240                         return false;
241                 }
242
243                 *prefixlength_separator = 0;
244
245                 if(prefixlength < 0) {
246                         return false;
247                 }
248         }
249
250         uint16_t x[8];
251
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]) {
253                 /*
254                    Normally we should check that each part has two digits to prevent ambiguities.
255                    However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
256                    so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
257                 */
258                 if(prefixlength >= 0) {
259                         return false;
260                 }
261
262                 subnet->type = SUBNET_MAC;
263                 subnet->weight = weight;
264
265                 for(int i = 0; i < 6; i++) {
266                         subnet->net.mac.address.x[i] = x[i];
267                 }
268
269                 return true;
270         }
271
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) {
274                         prefixlength = 32;
275                 }
276
277                 if(prefixlength > 32) {
278                         return false;
279                 }
280
281                 subnet->type = SUBNET_IPV4;
282                 subnet->net.ipv4.prefixlength = prefixlength;
283                 subnet->weight = weight;
284
285                 for(int i = 0; i < 4; i++) {
286                         if(x[i] > 255) {
287                                 return false;
288                         }
289
290                         subnet->net.ipv4.address.x[i] = x[i];
291                 }
292
293                 return true;
294         }
295
296         /* IPv6 */
297
298         char *last_colon = strrchr(str, ':');
299
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++)
303                         if(x[i] > 255) {
304                                 return false;
305                         }
306
307                 snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
308         }
309
310         char *double_colon = strstr(str, "::");
311
312         if(double_colon) {
313                 /* Figure out how many zero groups we need to expand */
314                 int zero_group_count = 8;
315
316                 for(const char *cur = str; *cur; cur++)
317                         if(*cur != ':') {
318                                 zero_group_count--;
319
320                                 while(cur[1] && cur[1] != ':') {
321                                         cur++;
322                                 }
323                         }
324
325                 if(zero_group_count < 1) {
326                         return false;
327                 }
328
329                 /* Split the double colon in the middle to make room for zero groups */
330                 double_colon++;
331                 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
332
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);
336                 }
337
338                 /* Remove any leading or trailing colons */
339                 if(str[0] == ':') {
340                         memmove(&str[0], &str[1], strlen(&str[1]) + 1);
341                 }
342
343                 if(str[strlen(str) - 1] == ':') {
344                         str[strlen(str) - 1] = 0;
345                 }
346         }
347
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) {
351                         prefixlength = 128;
352                 }
353
354                 if(prefixlength > 128) {
355                         return false;
356                 }
357
358                 subnet->type = SUBNET_IPV6;
359                 subnet->net.ipv6.prefixlength = prefixlength;
360                 subnet->weight = weight;
361
362                 for(int i = 0; i < 8; i++) {
363                         subnet->net.ipv6.address.x[i] = htons(x[i]);
364                 }
365
366                 return true;
367         }
368
369         return false;
370 }
371
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!", (void *)netstr, (void *)subnet);
375                 return false;
376         }
377
378         int result;
379         int prefixlength = -1;
380
381         switch(subnet->type) {
382         case SUBNET_MAC:
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]);
390                 netstr += result;
391                 len -= result;
392                 break;
393
394         case SUBNET_IPV4:
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]);
400                 netstr += result;
401                 len -= result;
402                 prefixlength = subnet->net.ipv4.prefixlength;
403
404                 if(prefixlength == 32) {
405                         prefixlength = -1;
406                 }
407
408                 break;
409
410         case SUBNET_IPV6: {
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;
416
417                 for(int i = 0; i < 8; i++) {
418                         if(subnet->net.ipv6.address.x[i] != 0) {
419                                 current_zero_length = 0;
420                         } else {
421                                 if(current_zero_length == 0) {
422                                         current_zero_length_index = i;
423                                 }
424
425                                 current_zero_length++;
426
427                                 if(current_zero_length > max_zero_length) {
428                                         max_zero_length = current_zero_length;
429                                         max_zero_length_index = current_zero_length_index;
430                                 }
431                         }
432                 }
433
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];
440
441                                 if(i == 0) {
442                                         format++;
443                                 }
444
445                                 if(i + max_zero_length == 8) {
446                                         format++;
447                                 }
448
449                                 result = snprintf(netstr, len, *format, ":::");
450                                 i += max_zero_length;
451                         } else {
452                                 result = snprintf(netstr, len, "%x:", ntohs(subnet->net.ipv6.address.x[i]));
453                                 i++;
454                         }
455
456                         netstr += result;
457                         len -= result;
458                 }
459
460                 /* Remove the trailing colon */
461                 netstr--;
462                 len++;
463                 *netstr = 0;
464
465                 prefixlength = subnet->net.ipv6.prefixlength;
466
467                 if(prefixlength == 128) {
468                         prefixlength = -1;
469                 }
470
471                 break;
472         }
473
474         default:
475                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
476                 exit(1);
477         }
478
479         if(prefixlength >= 0) {
480                 result = snprintf(netstr, len, "/%d", prefixlength);
481                 netstr += result;
482                 len -= result;
483         }
484
485         if(subnet->weight != DEFAULT_WEIGHT) {
486                 snprintf(netstr, len, "#%d", subnet->weight);
487         }
488
489         return true;
490 }