From 29148632a56939d90fb909b11a8d50a673ddc496 Mon Sep 17 00:00:00 2001 From: Thomas Gschwantner Date: Wed, 11 Dec 2019 05:48:49 +0100 Subject: Allow /32 and /128 to be omitted in ip= keys --- common.c | 27 +++++++++++++++------------ 1 file 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; } -- cgit v1.2.3-59-g8ed1b