diff --git a/evutil.c b/evutil.c index 49c9014f..d93eff5f 100644 --- a/evutil.c +++ b/evutil.c @@ -595,44 +595,56 @@ evutil_socket_finished_connecting_(evutil_socket_t fd) set by evutil_check_interfaces. */ static int have_checked_interfaces, had_ipv4_address, had_ipv6_address; -/* Macro: True iff the IPv4 address 'addr', in host order, is in 127.0.0.0/8 - */ -#define EVUTIL_V4ADDR_IS_LOCALHOST(addr) (((addr)>>24) == 127) +/* True iff the IPv4 address 'addr', in host order, is in 127.0.0.0/8 */ +static inline int evutil_v4addr_is_localhost(ev_uint32_t addr) +{ return addr>>24 == 127; } -/* Macro: True iff the IPv4 address 'addr', in host order, is a class D - * (multiclass) address. - */ -#define EVUTIL_V4ADDR_IS_CLASSD(addr) ((((addr)>>24) & 0xf0) == 0xe0) +/* True iff the IPv4 address 'addr', in host order, is link-local + * 169.254.0.0/16 (RFC3927) */ +static inline int evutil_v4addr_is_linklocal(ev_uint32_t addr) +{ return ((addr & 0xffff0000U) == 0xa9fe0000U); } + +/* True iff the IPv4 address 'addr', in host order, is a class D + * (multiclass) address. */ +static inline int evutil_v4addr_is_classd(ev_uint32_t addr) +{ return ((addr>>24) & 0xf0) == 0xe0; } + +int +evutil_v4addr_is_local_(const struct in_addr *in) +{ + const ev_uint32_t addr = ntohl(in->s_addr); + return addr == INADDR_ANY || + evutil_v4addr_is_localhost(addr) || + evutil_v4addr_is_linklocal(addr) || + evutil_v4addr_is_classd(addr); +} +int +evutil_v6addr_is_local_(const struct in6_addr *in) +{ + static const char ZEROES[] = + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00"; + + const unsigned char *addr = (const unsigned char *)in->s6_addr; + return !memcmp(addr, ZEROES, 8) || + ((addr[0] & 0xfe) == 0xfc) || + (addr[0] == 0xfe && (addr[1] & 0xc0) == 0x80) || + (addr[0] == 0xfe && (addr[1] & 0xc0) == 0xc0) || + (addr[0] == 0xff); +} static void evutil_found_ifaddr(const struct sockaddr *sa) { - const char ZEROES[] = "\x00\x00\x00\x00\x00\x00\x00\x00" - "\x00\x00\x00\x00\x00\x00\x00\x00"; - if (sa->sa_family == AF_INET) { const struct sockaddr_in *sin = (struct sockaddr_in *)sa; - ev_uint32_t addr = ntohl(sin->sin_addr.s_addr); - if (addr == 0 || - EVUTIL_V4ADDR_IS_LOCALHOST(addr) || - EVUTIL_V4ADDR_IS_CLASSD(addr)) { - /* Not actually a usable external address. */ - } else { + if (!evutil_v4addr_is_local_(&sin->sin_addr)) { event_debug(("Detected an IPv4 interface")); had_ipv4_address = 1; } } else if (sa->sa_family == AF_INET6) { const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; - const unsigned char *addr = - (unsigned char*)sin6->sin6_addr.s6_addr; - if (!memcmp(addr, ZEROES, 8) || - ((addr[0] & 0xfe) == 0xfc) || - (addr[0] == 0xfe && (addr[1] & 0xc0) == 0x80) || - (addr[0] == 0xfe && (addr[1] & 0xc0) == 0xc0) || - (addr[0] == 0xff)) { - /* This is a reserved, ipv4compat, ipv4map, loopback, - * link-local, multicast, or unspecified address. */ - } else { + if (!evutil_v6addr_is_local_(&sin6->sin6_addr)) { event_debug(("Detected an IPv6 interface")); had_ipv6_address = 1; } diff --git a/test/regress_util.c b/test/regress_util.c index 68281e61..320047fa 100644 --- a/test/regress_util.c +++ b/test/regress_util.c @@ -1455,6 +1455,83 @@ end: ; } +static void +test_evutil_v4addr_is_local(void *arg) +{ + struct sockaddr_in sin; + sin.sin_family = AF_INET; + + /* we use evutil_inet_pton() here to fill in network-byte order */ +#define LOCAL(str, yes) do { \ + tt_int_op(evutil_inet_pton(AF_INET, str, &sin.sin_addr), ==, 1); \ + tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, yes); \ +} while (0) + + /** any */ + sin.sin_addr.s_addr = INADDR_ANY; + tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1); + + /** loopback */ + sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1); + LOCAL("127.0.0.1", 1); + LOCAL("127.255.255.255", 1); + LOCAL("121.0.0.1", 0); + + /** link-local */ + LOCAL("169.254.0.1", 1); + LOCAL("169.254.255.255", 1); + LOCAL("170.0.0.0", 0); + + /** Multicast */ + LOCAL("224.0.0.0", 1); + LOCAL("239.255.255.255", 1); + LOCAL("240.0.0.0", 0); +end: + ; +} + +static void +test_evutil_v6addr_is_local(void *arg) +{ + struct sockaddr_in6 sin6; + struct in6_addr anyaddr = IN6ADDR_ANY_INIT; + struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT; + + sin6.sin6_family = AF_INET6; +#define LOCAL6(str, yes) do { \ + tt_int_op(evutil_inet_pton(AF_INET6, str, &sin6.sin6_addr), ==, 1);\ + tt_int_op(evutil_v6addr_is_local_(&sin6.sin6_addr), ==, yes); \ +} while (0) + + /** any */ + tt_int_op(evutil_v6addr_is_local_(&anyaddr), ==, 1); + LOCAL6("::0", 1); + + /** loopback */ + tt_int_op(evutil_v6addr_is_local_(&loopback), ==, 1); + LOCAL6("::1", 1); + + /** IPV4 mapped */ + LOCAL6("::ffff:0:0", 1); + /** IPv4 translated */ + LOCAL6("::ffff:0:0:0", 1); + /** IPv4/IPv6 translation */ + LOCAL6("64:ff9b::", 0); + /** Link-local */ + LOCAL6("fe80::", 1); + /** Multicast */ + LOCAL6("ff00::", 1); + /** Unspecified */ + LOCAL6("::", 1); + + /** Global Internet */ + LOCAL6("2001::", 0); + LOCAL6("2001:4860:4802:32::1b", 0); +end: + ; +} + struct testcase_t util_testcases[] = { { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL }, { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL }, @@ -1486,6 +1563,8 @@ struct testcase_t util_testcases[] = { { "monotonic_prc_precise", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"precise" }, { "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" }, { "date_rfc1123", test_evutil_date_rfc1123, 0, NULL, NULL }, + { "evutil_v4addr_is_local", test_evutil_v4addr_is_local, 0, NULL, NULL }, + { "evutil_v6addr_is_local", test_evutil_v6addr_is_local, 0, NULL, NULL }, END_OF_TESTCASES, }; diff --git a/util-internal.h b/util-internal.h index fe416409..b727bf1f 100644 --- a/util-internal.h +++ b/util-internal.h @@ -527,6 +527,17 @@ evutil_socket_t evutil_eventfd_(unsigned initval, int flags); void evutil_memclear_(void *mem, size_t len); +struct in_addr; +struct in6_addr; + +/* This is a any, loopback, link-local, multicast */ +EVENT2_EXPORT_SYMBOL +int evutil_v4addr_is_local_(const struct in_addr *in); +/* This is a reserved, ipv4compat, ipv4map, loopback, + * link-local, multicast, or unspecified address. */ +EVENT2_EXPORT_SYMBOL +int evutil_v6addr_is_local_(const struct in6_addr *in); + #ifdef __cplusplus } #endif