From: Alan T. DeKok Date: Wed, 17 Nov 2021 17:24:58 +0000 (-0500) Subject: inet_pton() on OSX doesn't accept "127.0" as 127.0.0.0 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=227a0077a93bbfc6c173953ba6ce99a5ee375024;p=thirdparty%2Ffreeradius-server.git inet_pton() on OSX doesn't accept "127.0" as 127.0.0.0 despite what the documentation says. But getaddrinfo() does accept it. So when we turn off hostname lookups, IP address parsing fails. Work around it by just doing the work ourselves. --- diff --git a/src/lib/util/inet.c b/src/lib/util/inet.c index 012c3100f9..8291b7d005 100644 --- a/src/lib/util/inet.c +++ b/src/lib/util/inet.c @@ -517,10 +517,21 @@ int fr_inet_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resol out->addr.v4.s_addr = htonl(strtoul(value, NULL, 0)); } else if (!resolve) { - if (inet_pton(AF_INET, value, &out->addr.v4.s_addr) <= 0) { + unsigned int a, b, c, d; + int num; + char rest; + + a = b = c = d = 0; + + num = sscanf(value, "%u.%u.%u.%u%c", &a, &b, &c, &d, &rest); + if ((num == 0) || (num == 5) || + (a > 255) || (b > 255) || (c > 255) || (d > 255)) { fr_strerror_printf("Failed to parse IPv4 address string \"%s\"", value); return -1; } + + out->addr.v4.s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d); + } else if (fr_inet_hton(out, AF_INET, value, fallback) < 0) return -1; return 0; diff --git a/src/tests/unit/data_types.txt b/src/tests/unit/data_types.txt index 1d8876831e..fc14c5210d 100644 --- a/src/tests/unit/data_types.txt +++ b/src/tests/unit/data_types.txt @@ -9,6 +9,12 @@ match 0 value ipaddr 127.0.0.1 match 127.0.0.1 +# +# The rest of the octets should get filled out as 0. +# +value ipaddr 127.0 +match 127.0.0.0 + # And canonicalized value ipaddr 127.0.0.001 match 127.0.0.1 @@ -238,4 +244,4 @@ encode-dns-label www_foo.com match Invalid character 0x5f in label count -match 118 +match 120