Rewrite, fix and improve str2net().
[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 /* Subnet mask handling */
31
32 int maskcmp(const void *va, const void *vb, int masklen) {
33         int i, m, result;
34         const char *a = va;
35         const char *b = vb;
36
37         for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
38                 result = a[i] - b[i];
39                 if(result)
40                         return result;
41         }
42
43         if(m)
44                 return (a[i] & (0x100 - (1 << (8 - m)))) -
45                         (b[i] & (0x100 - (1 << (8 - m))));
46
47         return 0;
48 }
49
50 void mask(void *va, int masklen, int len) {
51         int i;
52         char *a = va;
53
54         i = masklen / 8;
55         masklen %= 8;
56
57         if(masklen)
58                 a[i++] &= (0x100 - (1 << (8 - masklen)));
59
60         for(; i < len; i++)
61                 a[i] = 0;
62 }
63
64 void maskcpy(void *va, const void *vb, int masklen, int len) {
65         int i, m;
66         char *a = va;
67         const char *b = vb;
68
69         for(m = masklen, i = 0; m >= 8; m -= 8, i++)
70                 a[i] = b[i];
71
72         if(m) {
73                 a[i] = b[i] & (0x100 - (1 << (8 - m)));
74                 i++;
75         }
76
77         for(; i < len; i++)
78                 a[i] = 0;
79 }
80
81 bool maskcheck(const void *va, int masklen, int len) {
82         int i;
83         const char *a = va;
84
85         i = masklen / 8;
86         masklen %= 8;
87
88         if(masklen && a[i++] & (0xff >> masklen))
89                 return false;
90
91         for(; i < len; i++)
92                 if(a[i] != 0)
93                         return false;
94
95         return true;
96 }
97
98 /* Subnet comparison */
99
100 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
101         int result;
102
103         result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof a->net.mac.address);
104
105         if(result)
106                 return result;
107
108         result = a->weight - b->weight;
109
110         if(result || !a->owner || !b->owner)
111                 return result;
112
113         return strcmp(a->owner->name, b->owner->name);
114 }
115
116 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
117         int result;
118
119         result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
120
121         if(result)
122                 return result;
123
124         result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
125
126         if(result)
127                 return result;
128
129         result = a->weight - b->weight;
130
131         if(result || !a->owner || !b->owner)
132                 return result;
133
134         return strcmp(a->owner->name, b->owner->name);
135 }
136
137 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
138         int result;
139
140         result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
141
142         if(result)
143                 return result;
144
145         result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
146
147         if(result)
148                 return result;
149
150         result = a->weight - b->weight;
151
152         if(result || !a->owner || !b->owner)
153                 return result;
154
155         return strcmp(a->owner->name, b->owner->name);
156 }
157
158 int subnet_compare(const subnet_t *a, const subnet_t *b) {
159         int result;
160
161         result = a->type - b->type;
162
163         if(result)
164                 return result;
165
166         switch (a->type) {
167         case SUBNET_MAC:
168                 return subnet_compare_mac(a, b);
169         case SUBNET_IPV4:
170                 return subnet_compare_ipv4(a, b);
171         case SUBNET_IPV6:
172                 return subnet_compare_ipv6(a, b);
173         default:
174                 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
175                 exit(1);
176         }
177
178         return 0;
179 }
180
181 /* Ascii representation of subnets */
182
183 bool str2net(subnet_t *subnet, const char *subnetstr) {
184         char str[1024];
185         strncpy(str, subnetstr, sizeof(str));
186         int consumed;
187
188         int weight = 10;
189         char *weight_separator = strchr(str, '#');
190         if (weight_separator) {
191                 char *weight_str = weight_separator + 1;
192                 if (sscanf(weight_str, "%d%n", &weight, &consumed) < 1)
193                         return false;
194                 if (weight_str[consumed])
195                         return false;
196                 *weight_separator = 0;
197         }
198
199         int prefixlength = -1;
200         char *prefixlength_separator = strchr(str, '/');
201         if (prefixlength_separator) {
202                 char* prefixlength_str = prefixlength_separator + 1;
203                 if (sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1)
204                         return false;
205                 if (prefixlength_str[consumed])
206                         return false;
207                 *prefixlength_separator = 0;
208
209                 if (prefixlength < 0)
210                         return false;
211         }
212
213         uint16_t x[8];
214         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]) {
215                 /*
216                    Normally we should check that each part has two digits to prevent ambiguities.
217                    However, in old tinc versions net2str() will agressively return MAC addresses with one-digit parts,
218                    so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
219                 */
220                 if (prefixlength >= 0)
221                         return false;
222
223                 subnet->type = SUBNET_MAC;
224                 subnet->weight = weight;
225                 for(int i = 0; i < 6; i++)
226                         subnet->net.mac.address.x[i] = x[i];
227                 return true;
228         }
229
230         if (sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
231                 if (prefixlength == -1)
232                         prefixlength = 32;
233                 if (prefixlength > 32)
234                         return false;
235
236                 subnet->type = SUBNET_IPV4;
237                 subnet->net.ipv4.prefixlength = prefixlength;
238                 subnet->weight = weight;
239                 for(int i = 0; i < 4; i++) {
240                         if (x[i] > 255)
241                                 return false;
242                         subnet->net.ipv4.address.x[i] = x[i];
243                 }
244                 return true;
245         }
246
247         /* IPv6 */
248
249         char* last_colon = strrchr(str, ':');
250         if (last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
251                 /* Dotted quad suffix notation, convert to standard IPv6 notation */
252                 for (int i = 0; i < 4; i++)
253                         if (x[i] > 255)
254                                 return false;
255                 sprintf(last_colon, ":%02hx%02hx:%02hx%02hx", x[0], x[1], x[2], x[3]);
256         }
257
258         char* double_colon = strstr(str, "::");
259         if (double_colon) {
260                 /* Figure out how many zero groups we need to expand */
261                 int zero_group_count = 8;
262                 for (const char* cur = str; *cur; *cur && cur++)
263                         if (*cur != ':') {
264                                 zero_group_count--;
265                                 for (; *cur && *cur != ':'; cur++);
266                         }
267                 if (zero_group_count < 1)
268                         return false;
269
270                 /* Split the double colon in the middle to make room for zero groups */
271                 double_colon++;
272                 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
273
274                 /* Write zero groups in the resulting gap, overwriting the second colon */
275                 for (int i = 0; i < zero_group_count; i++)
276                         memcpy(&double_colon[i * 2], "0:", 2);
277
278                 /* Remove any leading or trailing colons */
279                 if (str[0] == ':')
280                         memmove(&str[0], &str[1], strlen(&str[1]) + 1);
281                 if (str[strlen(str) - 1] == ':')
282                         str[strlen(str) - 1] = 0;
283         }
284
285         if (sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
286                 &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
287                 if (prefixlength == -1)
288                         prefixlength = 128;
289                 if (prefixlength > 128)
290                         return false;
291
292                 subnet->type = SUBNET_IPV6;
293                 subnet->net.ipv6.prefixlength = prefixlength;
294                 subnet->weight = weight;
295                 for(int i = 0; i < 8; i++)
296                         subnet->net.ipv6.address.x[i] = htons(x[i]);
297                 return true;
298         }
299
300         return false;
301 }
302
303 bool net2str(char *netstr, int len, const subnet_t *subnet) {
304         if(!netstr || !subnet) {
305                 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with netstr=%p, subnet=%p!", netstr, subnet);
306                 return false;
307         }
308
309         switch (subnet->type) {
310                 case SUBNET_MAC:
311                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx#%d",
312                                          subnet->net.mac.address.x[0],
313                                          subnet->net.mac.address.x[1],
314                                          subnet->net.mac.address.x[2],
315                                          subnet->net.mac.address.x[3],
316                                          subnet->net.mac.address.x[4],
317                                          subnet->net.mac.address.x[5],
318                                          subnet->weight);
319                         break;
320
321                 case SUBNET_IPV4:
322                         snprintf(netstr, len, "%hu.%hu.%hu.%hu/%d#%d",
323                                          subnet->net.ipv4.address.x[0],
324                                          subnet->net.ipv4.address.x[1],
325                                          subnet->net.ipv4.address.x[2],
326                                          subnet->net.ipv4.address.x[3],
327                                          subnet->net.ipv4.prefixlength,
328                                          subnet->weight);
329                         break;
330
331                 case SUBNET_IPV6:
332                         snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%d#%d",
333                                          ntohs(subnet->net.ipv6.address.x[0]),
334                                          ntohs(subnet->net.ipv6.address.x[1]),
335                                          ntohs(subnet->net.ipv6.address.x[2]),
336                                          ntohs(subnet->net.ipv6.address.x[3]),
337                                          ntohs(subnet->net.ipv6.address.x[4]),
338                                          ntohs(subnet->net.ipv6.address.x[5]),
339                                          ntohs(subnet->net.ipv6.address.x[6]),
340                                          ntohs(subnet->net.ipv6.address.x[7]),
341                                          subnet->net.ipv6.prefixlength,
342                                          subnet->weight);
343                         break;
344
345                 default:
346                         logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);
347                         exit(1);
348         }
349
350         return true;
351 }