aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/common.c b/common.c
index 8c3baf6..2ad5717 100644
--- a/common.c
+++ b/common.c
@@ -77,24 +77,27 @@ static void (*const deserialize_fptr[])(enum wg_dynamic_key key,
static bool parse_ip_cidr(struct wg_combined_ip *ip, char *value)
{
- uintmax_t res;
- char *endptr;
char *sep;
sep = strchr(value, '/');
- if (!sep)
- return false;
+ if (sep) {
+ char *endptr;
+ uintmax_t res = strtoumax(sep + 1, &endptr, 10);
+ if (res > UINT8_MAX || *endptr != '\0' || sep + 1 == endptr)
+ return false;
- *sep = '\0';
- if (inet_pton(ip->family, value, ip) != 1)
- return false;
+ if ((ip->family == AF_INET && res > 32) ||
+ (ip->family == AF_INET6 && res > 128))
+ return false;
- res = strtoumax(sep + 1, &endptr, 10);
- if (res > UINT8_MAX || *endptr != '\0' || sep + 1 == endptr)
- return false;
+ *sep = '\0';
+ ip->cidr = (uint8_t)res;
+ } else {
+ ip->cidr = ip->family == AF_INET ? 32 : 128;
+ }
- // TODO: validate cidr range depending on ip->family
- ip->cidr = (uint8_t)res;
+ if (inet_pton(ip->family, value, ip) != 1)
+ return false;
return true;
}