Skip to content

Commit

Permalink
Split evutil_found_ifaddr() into helpers (evutil_v{4,6}addr_is_local())
Browse files Browse the repository at this point in the history
  • Loading branch information
azat committed Oct 24, 2018
1 parent d5f8525 commit 6966d39
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
46 changes: 26 additions & 20 deletions evutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,36 +609,42 @@ static inline int evutil_v4addr_is_linklocal(ev_uint32_t addr)
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 == INADDR_ANY ||
evutil_v4addr_is_localhost(addr) ||
evutil_v4addr_is_linklocal(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;
}
Expand Down
11 changes: 11 additions & 0 deletions util-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6966d39

Please sign in to comment.