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 /* Subnet mask handling */
32 int maskcmp(const void *va, const void *vb, int masklen) {
37 for(m = masklen, i = 0; m >= 8; m -= 8, i++) {
44 return (a[i] & (0x100 - (1 << (8 - m)))) -
45 (b[i] & (0x100 - (1 << (8 - m))));
50 void mask(void *va, int masklen, int len) {
58 a[i++] &= (0x100 - (1 << (8 - masklen)));
64 void maskcpy(void *va, const void *vb, int masklen, int len) {
69 for(m = masklen, i = 0; m >= 8; m -= 8, i++)
73 a[i] = b[i] & (0x100 - (1 << (8 - m)));
81 bool maskcheck(const void *va, int masklen, int len) {
88 if(masklen && a[i++] & (0xff >> masklen))
98 /* Subnet comparison */
100 static int subnet_compare_mac(const subnet_t *a, const subnet_t *b) {
103 result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof a->net.mac.address);
108 result = a->weight - b->weight;
110 if(result || !a->owner || !b->owner)
113 return strcmp(a->owner->name, b->owner->name);
116 static int subnet_compare_ipv4(const subnet_t *a, const subnet_t *b) {
119 result = b->net.ipv4.prefixlength - a->net.ipv4.prefixlength;
124 result = memcmp(&a->net.ipv4.address, &b->net.ipv4.address, sizeof(ipv4_t));
129 result = a->weight - b->weight;
131 if(result || !a->owner || !b->owner)
134 return strcmp(a->owner->name, b->owner->name);
137 static int subnet_compare_ipv6(const subnet_t *a, const subnet_t *b) {
140 result = b->net.ipv6.prefixlength - a->net.ipv6.prefixlength;
145 result = memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
150 result = a->weight - b->weight;
152 if(result || !a->owner || !b->owner)
155 return strcmp(a->owner->name, b->owner->name);
158 int subnet_compare(const subnet_t *a, const subnet_t *b) {
161 result = a->type - b->type;
168 return subnet_compare_mac(a, b);
170 return subnet_compare_ipv4(a, b);
172 return subnet_compare_ipv6(a, b);
174 logger(DEBUG_ALWAYS, LOG_ERR, "subnet_compare() was called with unknown subnet type %d, exitting!", a->type);
181 /* Ascii representation of subnets */
183 bool str2net(subnet_t *subnet, const char *subnetstr) {
185 strncpy(str, subnetstr, sizeof(str));
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)
194 if (weight_str[consumed])
196 *weight_separator = 0;
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)
205 if (prefixlength_str[consumed])
207 *prefixlength_separator = 0;
209 if (prefixlength < 0)
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]) {
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.
220 if (prefixlength >= 0)
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];
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)
233 if (prefixlength > 32)
236 subnet->type = SUBNET_IPV4;
237 subnet->net.ipv4.prefixlength = prefixlength;
238 subnet->weight = weight;
239 for(int i = 0; i < 4; i++) {
242 subnet->net.ipv4.address.x[i] = x[i];
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++)
255 sprintf(last_colon, ":%02hx%02hx:%02hx%02hx", x[0], x[1], x[2], x[3]);
258 char* double_colon = strstr(str, "::");
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++)
265 for (; *cur && *cur != ':'; cur++);
267 if (zero_group_count < 1)
270 /* Split the double colon in the middle to make room for zero groups */
272 memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
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);
278 /* Remove any leading or trailing colons */
280 memmove(&str[0], &str[1], strlen(&str[1]) + 1);
281 if (str[strlen(str) - 1] == ':')
282 str[strlen(str) - 1] = 0;
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)
289 if (prefixlength > 128)
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]);
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);
309 switch (subnet->type) {
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],
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,
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,
346 logger(DEBUG_ALWAYS, LOG_ERR, "net2str() was called with unknown subnet type %d, exiting!", subnet->type);