-subnet_t *str2net(char *subnetstr)
-{
- int type;
- subnet_t *subnet;
-cp
- if(sscanf(subnetstr, "%d,", &type) != 1)
- return NULL;
-cp
- subnet = new_subnet();
-cp
- switch(type)
- {
- case SUBNET_MAC:
- if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
- &subnet->net.mac.address.x[0],
- &subnet->net.mac.address.x[1],
- &subnet->net.mac.address.x[2],
- &subnet->net.mac.address.x[3],
- &subnet->net.mac.address.x[4],
- &subnet->net.mac.address.x[5]) != 7)
- {
- free_subnet(subnet);
- return NULL;
- }
- break;
- case SUBNET_IPV4:
- if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
- {
- free_subnet(subnet);
- return NULL;
- }
- break;
- case SUBNET_IPV6:
- if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
- &subnet->net.ipv6.address.x[0],
- &subnet->net.ipv6.address.x[1],
- &subnet->net.ipv6.address.x[2],
- &subnet->net.ipv6.address.x[3],
- &subnet->net.ipv6.address.x[4],
- &subnet->net.ipv6.address.x[5],
- &subnet->net.ipv6.address.x[6],
- &subnet->net.ipv6.address.x[7],
- &subnet->net.ipv6.mask.x[0],
- &subnet->net.ipv6.mask.x[1],
- &subnet->net.ipv6.mask.x[2],
- &subnet->net.ipv6.mask.x[3],
- &subnet->net.ipv6.mask.x[4],
- &subnet->net.ipv6.mask.x[5],
- &subnet->net.ipv6.mask.x[6],
- &subnet->net.ipv6.mask.x[7]) != 17)
- {
- free_subnet(subnet);
- return NULL;
- }
- break;
- default:
- free_subnet(subnet);
- return NULL;
- }
-cp
- return subnet;
+bool str2net(subnet_t *subnet, const char *subnetstr) {
+ char str[1024];
+ strncpy(str, subnetstr, sizeof(str));
+ str[sizeof(str) - 1] = 0;
+ int consumed;
+
+ int weight = 10;
+ char *weight_separator = strchr(str, '#');
+
+ if(weight_separator) {
+ char *weight_str = weight_separator + 1;
+
+ if(sscanf(weight_str, "%d%n", &weight, &consumed) < 1) {
+ return false;
+ }
+
+ if(weight_str[consumed]) {
+ return false;
+ }
+
+ *weight_separator = 0;
+ }
+
+ int prefixlength = -1;
+ char *prefixlength_separator = strchr(str, '/');
+
+ if(prefixlength_separator) {
+ char *prefixlength_str = prefixlength_separator + 1;
+
+ if(sscanf(prefixlength_str, "%d%n", &prefixlength, &consumed) < 1) {
+ return false;
+ }
+
+ if(prefixlength_str[consumed]) {
+ return false;
+ }
+
+ *prefixlength_separator = 0;
+
+ if(prefixlength < 0) {
+ return false;
+ }
+ }
+
+ uint16_t x[8];
+
+ 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]) {
+ /*
+ Normally we should check that each part has two digits to prevent ambiguities.
+ However, in old tinc versions net2str() will aggressively return MAC addresses with one-digit parts,
+ so we have to accept them otherwise we would be unable to parse ADD_SUBNET messages.
+ */
+ if(prefixlength >= 0) {
+ return false;
+ }
+
+ subnet->type = SUBNET_MAC;
+ subnet->weight = weight;
+
+ for(int i = 0; i < 6; i++) {
+ subnet->net.mac.address.x[i] = x[i];
+ }
+
+ return true;
+ }
+
+ if(sscanf(str, "%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !str[consumed]) {
+ if(prefixlength == -1) {
+ prefixlength = 32;
+ }
+
+ if(prefixlength > 32) {
+ return false;
+ }
+
+ subnet->type = SUBNET_IPV4;
+ subnet->net.ipv4.prefixlength = prefixlength;
+ subnet->weight = weight;
+
+ for(int i = 0; i < 4; i++) {
+ if(x[i] > 255) {
+ return false;
+ }
+
+ subnet->net.ipv4.address.x[i] = x[i];
+ }
+
+ return true;
+ }
+
+ /* IPv6 */
+
+ char *last_colon = strrchr(str, ':');
+
+ if(last_colon && sscanf(last_colon, ":%hu.%hu.%hu.%hu%n", &x[0], &x[1], &x[2], &x[3], &consumed) >= 4 && !last_colon[consumed]) {
+ /* Dotted quad suffix notation, convert to standard IPv6 notation */
+ for(int i = 0; i < 4; i++)
+ if(x[i] > 255) {
+ return false;
+ }
+
+ snprintf(last_colon, sizeof(str) - (last_colon - str), ":%02x%02x:%02x%02x", x[0], x[1], x[2], x[3]);
+ }
+
+ char *double_colon = strstr(str, "::");
+
+ if(double_colon) {
+ /* Figure out how many zero groups we need to expand */
+ int zero_group_count = 8;
+
+ for(const char *cur = str; *cur; cur++)
+ if(*cur != ':') {
+ zero_group_count--;
+
+ while(cur[1] && cur[1] != ':') {
+ cur++;
+ }
+ }
+
+ if(zero_group_count < 1) {
+ return false;
+ }
+
+ /* Split the double colon in the middle to make room for zero groups */
+ double_colon++;
+ memmove(double_colon + (zero_group_count * 2 - 1), double_colon, strlen(double_colon) + 1);
+
+ /* Write zero groups in the resulting gap, overwriting the second colon */
+ for(int i = 0; i < zero_group_count; i++) {
+ memcpy(&double_colon[i * 2], "0:", 2);
+ }
+
+ /* Remove any leading or trailing colons */
+ if(str[0] == ':') {
+ memmove(&str[0], &str[1], strlen(&str[1]) + 1);
+ }
+
+ if(str[strlen(str) - 1] == ':') {
+ str[strlen(str) - 1] = 0;
+ }
+ }
+
+ if(sscanf(str, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx%n",
+ &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], &consumed) >= 8 && !str[consumed]) {
+ if(prefixlength == -1) {
+ prefixlength = 128;
+ }
+
+ if(prefixlength > 128) {
+ return false;
+ }
+
+ subnet->type = SUBNET_IPV6;
+ subnet->net.ipv6.prefixlength = prefixlength;
+ subnet->weight = weight;
+
+ for(int i = 0; i < 8; i++) {
+ subnet->net.ipv6.address.x[i] = htons(x[i]);
+ }
+
+ return true;
+ }
+
+ return false;