From 6888523d73bdb5822b78455fa2a864635bbe1f7f Mon Sep 17 00:00:00 2001 From: rl1987 Date: Mon, 5 Jan 2015 22:12:46 +0200 Subject: Moving the hacky part of get_interface_address6() into separate function. --- src/common/address.c | 83 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/src/common/address.c b/src/common/address.c index 3b4be1d601..e3c2521235 100644 --- a/src/common/address.c +++ b/src/common/address.c @@ -1484,47 +1484,22 @@ tor_addr_is_multicast(const tor_addr_t *a) return 0; } -/** Set *addr to the IP address (if any) of whatever interface - * connects to the Internet. This address should only be used in checking - * whether our address has changed. Return 0 on success, -1 on failure. +/** Attempt to retrieve IP address of current host by utilizing some + * UDP socket trickery. Only look for address of given family. + * Set result to *addr. Return 0 on success, -1 on failure. */ -MOCK_IMPL(int, -get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr)) +STATIC int +get_interface_address6_via_udp_socket_hack(int severity, + sa_family_t family, + tor_addr_t *addr) { - /* XXX really, this function should yield a smartlist of addresses. */ - smartlist_t *addrs; - int sock=-1, r=-1; struct sockaddr_storage my_addr, target_addr; + int sock=-1, r=-1; socklen_t addr_len; - tor_assert(addr); - - /* Try to do this the smart way if possible. */ - if ((addrs = get_interface_addresses_raw(severity))) { - int rv = -1; - SMARTLIST_FOREACH_BEGIN(addrs, tor_addr_t *, a) { - if (family != AF_UNSPEC && family != tor_addr_family(a)) - continue; - if (tor_addr_is_loopback(a) || - tor_addr_is_multicast(a)) - continue; - - tor_addr_copy(addr, a); - rv = 0; - - /* If we found a non-internal address, declare success. Otherwise, - * keep looking. */ - if (!tor_addr_is_internal(a, 0)) - break; - } SMARTLIST_FOREACH_END(a); - SMARTLIST_FOREACH(addrs, tor_addr_t *, a, tor_free(a)); - smartlist_free(addrs); - return rv; - } - - /* Okay, the smart way is out. */ memset(addr, 0, sizeof(tor_addr_t)); memset(&target_addr, 0, sizeof(target_addr)); + /* Don't worry: no packets are sent. We just need to use a real address * on the actual Internet. */ if (family == AF_INET6) { @@ -1546,6 +1521,7 @@ get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr)) } else { return -1; } + if (sock < 0) { int e = tor_socket_errno(-1); log_fn(severity, LD_NET, "unable to create socket: %s", @@ -1574,6 +1550,45 @@ get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr)) return r; } +/** Set *addr to the IP address (if any) of whatever interface + * connects to the Internet. This address should only be used in checking + * whether our address has changed. Return 0 on success, -1 on failure. + */ +MOCK_IMPL(int, +get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr)) +{ + /* XXX really, this function should yield a smartlist of addresses. */ + smartlist_t *addrs; + tor_assert(addr); + + /* Try to do this the smart way if possible. */ + if ((addrs = get_interface_addresses_raw(severity))) { + int rv = -1; + SMARTLIST_FOREACH_BEGIN(addrs, tor_addr_t *, a) { + if (family != AF_UNSPEC && family != tor_addr_family(a)) + continue; + if (tor_addr_is_loopback(a) || + tor_addr_is_multicast(a)) + continue; + + tor_addr_copy(addr, a); + rv = 0; + + /* If we found a non-internal address, declare success. Otherwise, + * keep looking. */ + if (!tor_addr_is_internal(a, 0)) + break; + } SMARTLIST_FOREACH_END(a); + + SMARTLIST_FOREACH(addrs, tor_addr_t *, a, tor_free(a)); + smartlist_free(addrs); + return rv; + } + + /* Okay, the smart way is out. */ + return get_interface_address6_via_udp_socket_hack(severity,family,addr); +} + /* ====== * IPv4 helpers * XXXX024 IPv6 deprecate some of these. -- cgit v1.2.3-54-g00ecf From a13f944314d7f2a71cc4d28546d88ebfe34aaf49 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sun, 25 Jan 2015 13:58:21 +0200 Subject: Black box test for get_interface_address6_via_udp_socket_hack(). --- src/common/address.h | 3 +++ src/test/test_address.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/common/address.h b/src/common/address.h index 8c6ee5abbb..6d9d78893f 100644 --- a/src/common/address.h +++ b/src/common/address.h @@ -274,6 +274,9 @@ tor_addr_port_t *tor_addr_port_new(const tor_addr_t *addr, uint16_t port); #ifdef ADDRESS_PRIVATE STATIC smartlist_t *get_interface_addresses_raw(int severity); +STATIC int get_interface_address6_via_udp_socket_hack(int severity, + sa_family_t family, + tor_addr_t *addr); #ifdef HAVE_IFADDRS_TO_SMARTLIST STATIC smartlist_t *ifaddrs_to_smartlist(const struct ifaddrs *ifa); diff --git a/src/test/test_address.c b/src/test/test_address.c index 7f7347fa88..57bd70cf18 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -452,10 +452,71 @@ test_address_get_if_addrs_ioctl(void *arg) #endif +/** Return 1 iff an address exactly equal to tor_addr is in + * smartlist. Otherwise, return 0. + */ +static int +smartlist_contain_tor_addr(smartlist_t *smartlist, tor_addr_t *tor_addr) +{ + int success = 0; + + SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, a) { + if (tor_addr_compare(tor_addr,a,CMP_EXACT) == 0) { + success = 1; + break; + } + } SMARTLIST_FOREACH_END(a); + + return success; +} + +static void +test_address_udp_socket_trick_blackbox(void *arg) +{ + smartlist_t *all_addrs; + tor_addr_t *addr4; + tor_addr_t *addr6; + int retval; + + (void)arg; + + addr4 = tor_malloc(sizeof(tor_addr_t)); + addr6 = tor_malloc(sizeof(tor_addr_t)); + + all_addrs = get_interface_addresses_raw(LOG_DEBUG); + retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, + AF_INET,addr4); + + tt_assert(all_addrs); + tt_assert(retval == 0); + + tt_assert(smartlist_contain_tor_addr(all_addrs,addr4)); + +#if 0 + retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, + AF_INET6,addr6); + + tt_assert(smartlist_contain_tor_addr(all_addrs,addr6)); +#endif + + retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, + AF_CCITT,addr4); + + tt_assert(retval == -1); + + done: + tor_free(addr4); + tor_free(addr6); + SMARTLIST_FOREACH(all_addrs, tor_addr_t *, t, tor_free(t)); + smartlist_free(all_addrs); + return; +} + #define ADDRESS_TEST(name, flags) \ { #name, test_address_ ## name, flags, NULL, NULL } struct testcase_t address_tests[] = { + ADDRESS_TEST(udp_socket_trick_blackbox, TT_FORK), #ifdef HAVE_IFADDRS_TO_SMARTLIST ADDRESS_TEST(get_if_addrs_ifaddrs, TT_FORK), ADDRESS_TEST(ifaddrs_to_smartlist, 0), -- cgit v1.2.3-54-g00ecf From 6af471f3944a91646db8b7ea0a915ed2f87cd705 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sun, 22 Feb 2015 17:04:22 +0200 Subject: Rework the black box test, like Yawning and nickm suggested. --- src/test/test_address.c | 53 +++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/test/test_address.c b/src/test/test_address.c index 57bd70cf18..a2193e1da7 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -473,42 +473,51 @@ smartlist_contain_tor_addr(smartlist_t *smartlist, tor_addr_t *tor_addr) static void test_address_udp_socket_trick_blackbox(void *arg) { - smartlist_t *all_addrs; - tor_addr_t *addr4; - tor_addr_t *addr6; - int retval; + /* We want get_interface_address6_via_udp_socket_hack() to yield + * the same valid address that get_interface_address6() returns. + * If the latter is unable to find a valid address, we want + * _hack() to fail and return-1. + * + * Furthermore, we want _hack() never to crash, even if + * get_interface_addresses_raw() is returning NULL. + */ + + tor_addr_t addr4; + tor_addr_t addr4_to_check; + tor_addr_t addr6; + tor_addr_t addr6_to_check; + int retval, retval_reference; (void)arg; - addr4 = tor_malloc(sizeof(tor_addr_t)); - addr6 = tor_malloc(sizeof(tor_addr_t)); - - all_addrs = get_interface_addresses_raw(LOG_DEBUG); + retval_reference = get_interface_address6(LOG_DEBUG,AF_INET,&addr4); retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, - AF_INET,addr4); - - tt_assert(all_addrs); - tt_assert(retval == 0); + AF_INET, + &addr4_to_check); - tt_assert(smartlist_contain_tor_addr(all_addrs,addr4)); + tt_int_op(retval,==,retval_reference); + tt_assert( (retval == -1 && retval_reference == -1) || + (tor_addr_compare(&addr4,&addr4_to_check,CMP_EXACT) == 0) ); -#if 0 + retval_reference = get_interface_address6(LOG_DEBUG,AF_INET6,&addr6); retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, - AF_INET6,addr6); + AF_INET6, + &addr6_to_check); - tt_assert(smartlist_contain_tor_addr(all_addrs,addr6)); -#endif + tt_int_op(retval,==,retval_reference); + tt_assert( (retval == -1 && retval_reference == -1) || + (tor_addr_compare(&addr6,&addr6_to_check,CMP_EXACT) == 0) ); + + /* When family is neither AF_INET nor AF_INET6, we want _hack to + * fail and return -1. + */ retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, - AF_CCITT,addr4); + AF_CCITT,&addr4); tt_assert(retval == -1); done: - tor_free(addr4); - tor_free(addr6); - SMARTLIST_FOREACH(all_addrs, tor_addr_t *, t, tor_free(t)); - smartlist_free(all_addrs); return; } -- cgit v1.2.3-54-g00ecf From 92fc728d83b7d02e95aae65b1eb083597bec141d Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sun, 22 Feb 2015 17:34:00 +0200 Subject: Bugfix: we don't want loopback/multicastaddress from _hack(). --- src/common/address.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/common/address.c b/src/common/address.c index e3c2521235..3e26576815 100644 --- a/src/common/address.c +++ b/src/common/address.c @@ -1542,8 +1542,16 @@ get_interface_address6_via_udp_socket_hack(int severity, goto err; } - tor_addr_from_sockaddr(addr, (struct sockaddr*)&my_addr, NULL); - r=0; + if (tor_addr_from_sockaddr(addr, (struct sockaddr*)&my_addr, NULL) == 0) { + if (tor_addr_is_loopback(addr) || tor_addr_is_multicast(addr)) { + log_fn(severity, LD_NET, "Address that we determined via UDP socket" + " magic is unsuitable for public comms."); + } + else { + r=0; + } + } + err: if (sock >= 0) tor_close_socket(sock); -- cgit v1.2.3-54-g00ecf From c03493ad138c024339ff78cb33e22d949c0282a8 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Tue, 24 Feb 2015 21:17:37 +0200 Subject: Skipping the AF_INET6 case because bug #12377 makes it fail. --- src/test/test_address.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/test_address.c b/src/test/test_address.c index a2193e1da7..2f379676b7 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -499,6 +499,10 @@ test_address_udp_socket_trick_blackbox(void *arg) tt_assert( (retval == -1 && retval_reference == -1) || (tor_addr_compare(&addr4,&addr4_to_check,CMP_EXACT) == 0) ); + //[XXX: Skipping the AF_INET6 case because bug #12377 makes it fail.] + (void)addr6_to_check; + (void)addr6; +#if 0 retval_reference = get_interface_address6(LOG_DEBUG,AF_INET6,&addr6); retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, AF_INET6, @@ -508,6 +512,8 @@ test_address_udp_socket_trick_blackbox(void *arg) tt_assert( (retval == -1 && retval_reference == -1) || (tor_addr_compare(&addr6,&addr6_to_check,CMP_EXACT) == 0) ); +#endif + /* When family is neither AF_INET nor AF_INET6, we want _hack to * fail and return -1. */ -- cgit v1.2.3-54-g00ecf From a4f89e21a6df0785f7e7714a7bf64d32f388e380 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sat, 14 Mar 2015 20:20:50 +0200 Subject: Whitebox test for get_interface_address6_via_udp_socket_hack(). Also, fix some whitespace mishaps. --- src/common/address.c | 8 ++--- src/common/compat.c | 20 ++++++++++-- src/common/compat.h | 10 +++++- src/test/test_address.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 113 insertions(+), 11 deletions(-) diff --git a/src/common/address.c b/src/common/address.c index 3e26576815..e4aad436fb 100644 --- a/src/common/address.c +++ b/src/common/address.c @@ -1529,13 +1529,14 @@ get_interface_address6_via_udp_socket_hack(int severity, goto err; } - if (connect(sock,(struct sockaddr *)&target_addr, addr_len) < 0) { + if (tor_connect_socket(sock,(struct sockaddr *)&target_addr, + addr_len) < 0) { int e = tor_socket_errno(sock); log_fn(severity, LD_NET, "connect() failed: %s", tor_socket_strerror(e)); goto err; } - if (getsockname(sock,(struct sockaddr*)&my_addr, &addr_len)) { + if (tor_getsockname(sock,(struct sockaddr*)&my_addr, &addr_len)) { int e = tor_socket_errno(sock); log_fn(severity, LD_NET, "getsockname() to determine interface failed: %s", tor_socket_strerror(e)); @@ -1546,8 +1547,7 @@ get_interface_address6_via_udp_socket_hack(int severity, if (tor_addr_is_loopback(addr) || tor_addr_is_multicast(addr)) { log_fn(severity, LD_NET, "Address that we determined via UDP socket" " magic is unsuitable for public comms."); - } - else { + } else { r=0; } } diff --git a/src/common/compat.c b/src/common/compat.c index fde65d9d15..18ab208aa8 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -1156,12 +1156,20 @@ mark_socket_open(tor_socket_t s) /** @} */ /** As socket(), but counts the number of open sockets. */ -tor_socket_t -tor_open_socket(int domain, int type, int protocol) +MOCK_IMPL(tor_socket_t, +tor_open_socket,(int domain, int type, int protocol)) { return tor_open_socket_with_extensions(domain, type, protocol, 1, 0); } +/** Mockable wrapper for connect(). */ +MOCK_IMPL(tor_socket_t, +tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address, + socklen_t address_len)) +{ + return connect(socket,address,address_len); +} + /** As socket(), but creates a nonblocking socket and * counts the number of open sockets. */ tor_socket_t @@ -1308,6 +1316,14 @@ get_n_open_sockets(void) return n; } +/** Mockable wrapper for getsockname(). */ +MOCK_IMPL(int, +tor_getsockname,(tor_socket_t socket, struct sockaddr *address, + socklen_t *address_len)) +{ + return getsockname(socket, address, address_len); +} + /** Turn socket into a nonblocking socket. Return 0 on success, -1 * on failure. */ diff --git a/src/common/compat.h b/src/common/compat.h index 23f8614196..18902e0928 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -451,7 +451,8 @@ int tor_close_socket(tor_socket_t s); tor_socket_t tor_open_socket_with_extensions( int domain, int type, int protocol, int cloexec, int nonblock); -tor_socket_t tor_open_socket(int domain, int type, int protocol); +MOCK_DECL(tor_socket_t, +tor_open_socket,(int domain, int type, int protocol)); tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol); tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len); @@ -462,8 +463,15 @@ tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len, int cloexec, int nonblock); +MOCK_DECL(tor_socket_t, +tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address, + socklen_t address_len)); int get_n_open_sockets(void); +MOCK_DECL(int, +tor_getsockname,(tor_socket_t socket, struct sockaddr *address, + socklen_t *address_len)); + #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags) #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags) diff --git a/src/test/test_address.c b/src/test/test_address.c index 2f379676b7..bfa2905af5 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -470,15 +470,92 @@ smartlist_contain_tor_addr(smartlist_t *smartlist, tor_addr_t *tor_addr) return success; } +#define FAKE_SOCKET_FD (42) + +tor_socket_t +fake_open_socket(int domain, int type, int protocol) +{ + (void)domain; + (void)type; + (void)protocol; + + return FAKE_SOCKET_FD; +} + +static int last_connected_socket_fd = 0; + +static int connect_retval = 0; + +tor_socket_t +pretend_to_connect(tor_socket_t socket, const struct sockaddr *address, + socklen_t address_len) +{ + (void)address; + (void)address_len; + + last_connected_socket_fd = socket; + + return connect_retval; +} + +static struct sockaddr *mock_addr = NULL; + +int +fake_getsockname(tor_socket_t socket, struct sockaddr *address, + socklen_t *address_len) +{ + if (!mock_addr) + return -1; + + if (*address_len < sizeof(struct sockaddr)) + return -1; + + memcpy(address,mock_addr,sizeof(struct sockaddr)); + *address_len = sizeof(mock_addr); + return 0; +} + +static void +test_address_udp_socket_trick_whitebox(void *arg) +{ + int hack_retval; + tor_addr_t *addr_from_hack = tor_malloc_zero(sizeof(tor_addr_t)); + + (void)arg; + + MOCK(tor_open_socket,fake_open_socket); + MOCK(tor_connect_socket,pretend_to_connect); + MOCK(tor_getsockname,fake_getsockname); + + mock_addr = tor_malloc_zero(sizeof(struct sockaddr)); + sockaddr_in_from_string("23.32.246.118",(struct sockaddr_in *)mock_addr); + + hack_retval = + get_interface_address6_via_udp_socket_hack(LOG_DEBUG, + AF_INET, addr_from_hack); + + tt_int_op(hack_retval,==,0); + tt_assert(tor_addr_eq_ipv4h(addr_from_hack, 0x1720f676)); + + UNMOCK(tor_open_socket); + UNMOCK(tor_connect_socket); + UNMOCK(tor_getsockname); + + done: + tor_free(mock_addr); + tor_free(addr_from_hack); + return; +} + static void test_address_udp_socket_trick_blackbox(void *arg) { - /* We want get_interface_address6_via_udp_socket_hack() to yield - * the same valid address that get_interface_address6() returns. - * If the latter is unable to find a valid address, we want + /* We want get_interface_address6_via_udp_socket_hack() to yield + * the same valid address that get_interface_address6() returns. + * If the latter is unable to find a valid address, we want * _hack() to fail and return-1. * - * Furthermore, we want _hack() never to crash, even if + * Furthermore, we want _hack() never to crash, even if * get_interface_addresses_raw() is returning NULL. */ @@ -531,6 +608,7 @@ test_address_udp_socket_trick_blackbox(void *arg) { #name, test_address_ ## name, flags, NULL, NULL } struct testcase_t address_tests[] = { + ADDRESS_TEST(udp_socket_trick_whitebox, TT_FORK), ADDRESS_TEST(udp_socket_trick_blackbox, TT_FORK), #ifdef HAVE_IFADDRS_TO_SMARTLIST ADDRESS_TEST(get_if_addrs_ifaddrs, TT_FORK), -- cgit v1.2.3-54-g00ecf From b5f84fde494833182a52a56d9f8584919915ec40 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sun, 15 Mar 2015 15:32:16 +0200 Subject: IPv6 case for whitebox test. --- src/test/test_address.c | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/test/test_address.c b/src/test/test_address.c index bfa2905af5..7a9471f48c 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -504,14 +504,26 @@ int fake_getsockname(tor_socket_t socket, struct sockaddr *address, socklen_t *address_len) { + socklen_t bytes_to_copy = 0; + if (!mock_addr) return -1; - if (*address_len < sizeof(struct sockaddr)) + if (mock_addr->sa_family == AF_INET) { + bytes_to_copy = sizeof(struct sockaddr_in); + } else if (mock_addr->sa_family == AF_INET6) { + bytes_to_copy = sizeof(struct sockaddr_in6); + } else { + return -1; + } + + if (*address_len < bytes_to_copy) { return -1; + } + + memcpy(address,mock_addr,bytes_to_copy); + *address_len = bytes_to_copy; - memcpy(address,mock_addr,sizeof(struct sockaddr)); - *address_len = sizeof(mock_addr); return 0; } @@ -520,6 +532,9 @@ test_address_udp_socket_trick_whitebox(void *arg) { int hack_retval; tor_addr_t *addr_from_hack = tor_malloc_zero(sizeof(tor_addr_t)); + struct sockaddr_in6 *mock_addr6; + struct sockaddr_in6 *ipv6_to_check = + tor_malloc_zero(sizeof(struct sockaddr_in6)); (void)arg; @@ -527,7 +542,7 @@ test_address_udp_socket_trick_whitebox(void *arg) MOCK(tor_connect_socket,pretend_to_connect); MOCK(tor_getsockname,fake_getsockname); - mock_addr = tor_malloc_zero(sizeof(struct sockaddr)); + mock_addr = tor_malloc_zero(sizeof(struct sockaddr_storage)); sockaddr_in_from_string("23.32.246.118",(struct sockaddr_in *)mock_addr); hack_retval = @@ -537,11 +552,31 @@ test_address_udp_socket_trick_whitebox(void *arg) tt_int_op(hack_retval,==,0); tt_assert(tor_addr_eq_ipv4h(addr_from_hack, 0x1720f676)); + /* Now, lets do an IPv6 case. */ + memset(mock_addr,0,sizeof(struct sockaddr_storage)); + + mock_addr6 = (struct sockaddr_in6 *)mock_addr; + mock_addr6->sin6_family = AF_INET6; + mock_addr6->sin6_port = 0; + inet_pton(AF_INET6,"2001:cdba::3257:9652",&(mock_addr6->sin6_addr)); + + hack_retval = + get_interface_address6_via_udp_socket_hack(LOG_DEBUG, + AF_INET6, addr_from_hack); + + tt_int_op(hack_retval,==,0); + + tor_addr_to_sockaddr(addr_from_hack,0,(struct sockaddr *)ipv6_to_check, + sizeof(struct sockaddr_in6)); + + tt_assert(sockaddr_in6_are_equal(mock_addr6,ipv6_to_check)); + UNMOCK(tor_open_socket); UNMOCK(tor_connect_socket); UNMOCK(tor_getsockname); done: + tor_free(ipv6_to_check); tor_free(mock_addr); tor_free(addr_from_hack); return; -- cgit v1.2.3-54-g00ecf From 5e759127f8a8e176d40a213d31f340f7ec3d0b0d Mon Sep 17 00:00:00 2001 From: rl1987 Date: Tue, 17 Mar 2015 21:23:12 +0200 Subject: Remove unused function. --- src/test/test_address.c | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/test/test_address.c b/src/test/test_address.c index 7a9471f48c..859c146a2b 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -452,24 +452,6 @@ test_address_get_if_addrs_ioctl(void *arg) #endif -/** Return 1 iff an address exactly equal to tor_addr is in - * smartlist. Otherwise, return 0. - */ -static int -smartlist_contain_tor_addr(smartlist_t *smartlist, tor_addr_t *tor_addr) -{ - int success = 0; - - SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, a) { - if (tor_addr_compare(tor_addr,a,CMP_EXACT) == 0) { - success = 1; - break; - } - } SMARTLIST_FOREACH_END(a); - - return success; -} - #define FAKE_SOCKET_FD (42) tor_socket_t -- cgit v1.2.3-54-g00ecf From 33fbb5011df19d6325e6b1e6e731fdf6b0f22f06 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sun, 22 Mar 2015 15:22:02 +0200 Subject: Use tor_inet_pton() (not inet_pton()) in testcases. --- src/test/test_address.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/test_address.c b/src/test/test_address.c index 859c146a2b..a69a605873 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -130,8 +130,8 @@ test_address_ifaddrs_to_smartlist(void *arg) ipv6_sockaddr = tor_malloc(sizeof(struct sockaddr_in6)); ipv6_sockaddr->sin6_family = AF_INET6; ipv6_sockaddr->sin6_port = 0; - inet_pton(AF_INET6, "2001:db8:8714:3a90::12", - &(ipv6_sockaddr->sin6_addr)); + tor_inet_pton(AF_INET6, "2001:db8:8714:3a90::12", + &(ipv6_sockaddr->sin6_addr)); ifa = tor_malloc(sizeof(struct ifaddrs)); ifa_ipv4 = tor_malloc(sizeof(struct ifaddrs)); @@ -540,7 +540,7 @@ test_address_udp_socket_trick_whitebox(void *arg) mock_addr6 = (struct sockaddr_in6 *)mock_addr; mock_addr6->sin6_family = AF_INET6; mock_addr6->sin6_port = 0; - inet_pton(AF_INET6,"2001:cdba::3257:9652",&(mock_addr6->sin6_addr)); + tor_inet_pton(AF_INET6,"2001:cdba::3257:9652",&(mock_addr6->sin6_addr)); hack_retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG, -- cgit v1.2.3-54-g00ecf From b49ffb2a21ccbcf62c3e38b822043b4302d8c26f Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sun, 22 Mar 2015 16:04:46 +0200 Subject: Changes file for 14710. --- changes/ticket14710 | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changes/ticket14710 diff --git a/changes/ticket14710 b/changes/ticket14710 new file mode 100644 index 0000000000..2b6cb2060f --- /dev/null +++ b/changes/ticket14710 @@ -0,0 +1,10 @@ + o Code simplification and refactoring: + - Move the hacky fallback code out of get_interface_address6() + into separate function and get it covered with unit-tests. Resolves + ticket 14710. + + o Minor bugfixes: + - When attempting to use fallback technique for network interface + lookup, disregard loopback and multicast addresses since they are + unsuitable for public communications. + -- cgit v1.2.3-54-g00ecf