Fix incorrect format qualifiers.
[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                 if(result)
43                         return result;
44         }
45
46         if(m)
47                 return (a[i] & (0x100 - (1 << (8 - m)))) -
48                         (b[i] & (0x100 - (1 << (8 - m))));
49
50         return 0;
51 }
52
53 void mask(void *va, int masklen, int len) {
54         int i;
55         char *a = va;
56
57         i = masklen / 8;
58         masklen %= 8;
59
60         if(masklen)
61                 a[i++] &= (0x100 - (1 << (8 - masklen)));
62
63         for(; i < len; i++)
64                 a[i] = 0;
65 }
66
67 void maskcpy(void *va, const void *vb, int masklen, int len) {
68         int i, m;
69         char *a = va;
70         const char *b = vb;
71
72         for(m = masklen, i = 0; m >= 8; m -= 8, i++)
73                 a[i] = b[i];
74
75         if(m) {
76                 a[i] = b[i] & (0x100 - (1 << (8 - m)));
77                 i++;
78         }
79
80         for(; i < len; i++)
81                 a[i] = 0;
82 }
83
84 bool maskcheck(const void *va, int masklen, int len) {
85         int i;
86         const char *a = va;
87
88         i = masklen / 8;
89         masklen %= 8;
90
91         if(masklen && a[i++] & (0xff >> masklen))
92                 return false;
93
94         for(; i < len; i++)
95                 if(a[i] != 0)
96                         return false;
97
98         return true;
99 }
100
101 /* Subnet comparison */
102
103 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
104         int result;
105
106         result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof a->net.mac.address);
107
108         if(result)
109                 return result;
110
111         result = a->weight - b->weight;
112
113         if(result || !a->owner || !b->owner)
114                 return result;
115
116         return strcmp(a->owner->name, b->owner->name);
117 }
118
119 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
120         int result;
121
122         result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
123
124         if(result)
125                 return result;
126
127         result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
128
129         if(result)
130                 return result;
131
132         result = a->weight - b->weight;
133
134         if(result || !a->owner || !b->owner)
135                 return result;
136
137         return strcmp(a->owner->name, b->owner->name);
138 }
139
140 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
141         int result;
142
143         result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
144
145         if(result)
146                 return result;
147
148         result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
149
150         if(result)
151                 return result;
152
153         result = a->weight - b->weight;
154
155         if(result || !a->owner || !b->owner)
156                 return result;
157
158         return strcmp(a->owner->name, b->owner->name);
159 }
160
161 int subnet_compare(const subnet_t *a, const subnet_t *b) {
162         int result;
163
164         result = a->type - b->type;
165
166         if(result)
167                 return result;
168
169         switch (a->type) {
170         case SUBNET_MAC:
171                 return subnet_compare_mac(a, b);
172         case SUBNET_IPV4:
173                 return subnet_compare_ipv4(a, b);
174         case SUBNET_IPV6:
175                 return subnet_compare_ipv6(a, b);
176         default:
177                 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
178                 exit(1);
179         }
180
181         return 0;
182 }
183
184 /* Ascii representation of subnets */
185
186 bool str2net(subnet_t *subnet, const char *subnetstr) {
187         char str[1024];
188         strncpy(str, subnetstr, sizeof(str));
189         int consumed;
190
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)
196                         return false;
197                 if (weight_str[consumed])
198                         return false;
199                 *weight_separator = 0;
200         }
201
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)
207                         return false;
208                 if (prefixlength_str[consumed])
209                         return false;
210                 *prefixlength_separator = 0;
211
212                 if (prefixlength < 0)
213                         return false;
214         }
215
216         uint16_t x[8];
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]) {
218                 /*
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.
222                 */
223                 if (prefixlength >= 0)
224                         return false;
225
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];
230                 return true;
231         }
232
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)
235                         prefixlength = 32;
236                 if (prefixlength > 32)
237                         return false;
238
239                 subnet->type = SUBNET_IPV4;
240                 subnet->net.ipv4.prefixlength = prefixlength;
241                 subnet->weight = weight;
242                 for(int i = 0; i < 4; i++) {
243                         if (x[i] > 255)
244                                 return false;
245                         subnet->net.ipv4.address.x[i] = x[i];
246                 }
247                 return true;
248         }
249
250         /* IPv6 */
251
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++)
256                         if (x[i] > 255)
257                                 return false;
258                 sprintf(last_colon, ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
259         }
260
261         char* double_colon = strstr(str, "::");
262         if (double_colon) {
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++)
266                         if (*cur != ':') {
267                                 zero_group_count--;
268                                 while(cur[1] && cur[1] != ':')
269                                         cur++;
270                         }
271                 if (zero_group_count < 1)
272                         return false;
273
274                 /* Split the double colon in the middle to make room for zero groups */
275                 double_colon++;
276                 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
277
278                 /* Write zero groups in the resulting gap, overwriting the second colon */
279                 for (int i = 0; i < zero_group_count; i++)
280                         memcpy(&double_colon[i * 2], "0:", 2);
281
282                 /* Remove any leading or trailing colons */
283                 if (str[0] == ':')
284                         memmove(&str[0], &str[1], strlen(&str[1]) + 1);
285                 if (str[strlen(str) - 1] == ':')
286                         str[strlen(str) - 1] = 0;
287         }
288
289         if (sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
290                 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
291                 if (prefixlength == -1)
292                         prefixlength = 128;
293                 if (prefixlength > 128)
294                         return false;
295
296                 subnet->type = SUBNET_IPV6;
297                 subnet->net.ipv6.prefixlength = prefixlength;
298                 subnet->weight = weight;
299                 for(int i = 0; i < 8; i++)
300                         subnet->net.ipv6.address.x[i] = htons(x[i]);
301                 return true;
302         }
303
304         return false;
305 }
306
307 bool net2str(char *netstr, int len, const subnet_t *subnet) {
308         if(!netstr || !subnet) {
309                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
310                 return false;
311         }
312
313         int result;
314         int prefixlength = -1;
315         switch (subnet->type) {
316                 case SUBNET_MAC:
317                         result = snprintf(netstr, len, "%02x:%02x:%02x:%02x:%02x:%02x",
318                                          subnet->net.mac.address.x[0],
319                                          subnet->net.mac.address.x[1],
320                                          subnet->net.mac.address.x[2],
321                                          subnet->net.mac.address.x[3],
322                                          subnet->net.mac.address.x[4],
323                                          subnet->net.mac.address.x[5]);
324                         netstr += result;
325                         len -= result;
326                         break;
327
328                 case SUBNET_IPV4:
329                         result = snprintf(netstr, len, "%u.%u.%u.%u",
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]);
334                         netstr += result;
335                         len -= result;
336                         prefixlength = subnet->net.ipv4.prefixlength;
337                         if (prefixlength == 32)
338                                 prefixlength = -1;
339                         break;
340
341                 case SUBNET_IPV6: {
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;
350                                 else {
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;
357                                         }
358                                 }
359                         }
360
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];
367                                         if (i == 0)
368                                                 format++;
369                                         if (i + max_zero_length == 8)
370                                                 format++;
371                                         result = snprintf(netstr, len, *format, ":::");
372                                         i += max_zero_length;
373                                 } else {
374                                         result = snprintf(netstr, len, "%hx:", ntohs(subnet->net.ipv6.address.x[i]));
375                                         i++;
376                                 }
377                                 netstr += result;
378                                 len -= result;
379                         }
380
381                         /* Remove the trailing colon */
382                         netstr--;
383                         len++;
384                         *netstr = 0;
385
386                         prefixlength = subnet->net.ipv6.prefixlength;
387                         if (prefixlength == 128)
388                                 prefixlength = -1;
389                         break;
390                 }
391
392                 default:
393                         logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
394                         exit(1);
395         }
396
397         if (prefixlength >= 0) {
398                 result = snprintf(netstr, len, "/%d", prefixlength);
399                 netstr += result;
400                 len -= result;
401         }
402
403         if (subnet->weight != DEFAULT_WEIGHT) {
404                 snprintf(netstr, len, "#%d", subnet->weight);
405                 netstr += result;
406                 len -= result;
407         }
408
409         return true;
410 }