1 /* Copyright (c) 2014-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define ADDRESS_PRIVATE
10 /* For access to structs needed by GetAdaptersAddresses */
14 #ifdef HAVE_IFADDRS_TO_SMARTLIST
19 #ifdef HAVE_IFCONF_TO_SMARTLIST
20 #ifdef HAVE_SYS_IOCTL_H
21 #include <sys/ioctl.h>
30 /** Return 1 iff <b>sockaddr1</b> and <b>sockaddr2</b> represent
31 * the same IP address and port combination. Otherwise, return 0.
34 sockaddr_in_are_equal(struct sockaddr_in
*sockaddr1
,
35 struct sockaddr_in
*sockaddr2
)
37 return ((sockaddr1
->sin_family
== sockaddr2
->sin_family
) &&
38 (sockaddr1
->sin_port
== sockaddr2
->sin_port
) &&
39 (sockaddr1
->sin_addr
.s_addr
== sockaddr2
->sin_addr
.s_addr
));
42 /** Return 1 iff <b>sockaddr1</b> and <b>sockaddr2</b> represent
43 * the same IP address and port combination. Otherwise, return 0.
46 sockaddr_in6_are_equal(struct sockaddr_in6
*sockaddr1
,
47 struct sockaddr_in6
*sockaddr2
)
49 return ((sockaddr1
->sin6_family
== sockaddr2
->sin6_family
) &&
50 (sockaddr1
->sin6_port
== sockaddr2
->sin6_port
) &&
51 (tor_memeq(sockaddr1
->sin6_addr
.s6_addr
,
52 sockaddr2
->sin6_addr
.s6_addr
,16)));
55 /** Create a sockaddr_in structure from IP address string <b>ip_str</b>.
57 * If <b>out</b> is not NULL, write the result
58 * to the memory address in <b>out</b>. Otherwise, allocate the memory
59 * for result. On success, return pointer to result. Otherwise, return
62 static struct sockaddr_in
*
63 sockaddr_in_from_string(const char *ip_str
, struct sockaddr_in
*out
)
65 // [FIXME: add some error checking?]
67 out
= tor_malloc_zero(sizeof(struct sockaddr_in
));
69 out
->sin_family
= AF_INET
;
71 tor_inet_pton(AF_INET
,ip_str
,&(out
->sin_addr
));
76 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
77 * that is an IPv4 or IPv6 localhost address. Otherwise, return 0.
80 smartlist_contains_localhost_tor_addr(smartlist_t
*smartlist
)
82 SMARTLIST_FOREACH_BEGIN(smartlist
, tor_addr_t
*, tor_addr
) {
83 if (tor_addr_is_loopback(tor_addr
)) {
86 } SMARTLIST_FOREACH_END(tor_addr
);
91 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
92 * that is an IPv4 or IPv6 multicast address. Otherwise, return 0.
95 smartlist_contains_multicast_tor_addr(smartlist_t
*smartlist
)
97 SMARTLIST_FOREACH_BEGIN(smartlist
, tor_addr_t
*, tor_addr
) {
98 if (tor_addr_is_multicast(tor_addr
)) {
101 } SMARTLIST_FOREACH_END(tor_addr
);
106 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
107 * that is an IPv4 or IPv6 internal address. Otherwise, return 0.
110 smartlist_contains_internal_tor_addr(smartlist_t
*smartlist
)
112 SMARTLIST_FOREACH_BEGIN(smartlist
, tor_addr_t
*, tor_addr
) {
113 if (tor_addr_is_internal(tor_addr
, 0)) {
116 } SMARTLIST_FOREACH_END(tor_addr
);
121 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
122 * that is NULL or the null tor_addr_t. Otherwise, return 0.
125 smartlist_contains_null_tor_addr(smartlist_t
*smartlist
)
127 SMARTLIST_FOREACH_BEGIN(smartlist
, tor_addr_t
*, tor_addr
) {
128 if (tor_addr
== NULL
|| tor_addr_is_null(tor_addr
)) {
131 } SMARTLIST_FOREACH_END(tor_addr
);
136 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
137 * that is an IPv4 address. Otherwise, return 0.
140 smartlist_contains_ipv4_tor_addr(smartlist_t
*smartlist
)
142 SMARTLIST_FOREACH_BEGIN(smartlist
, tor_addr_t
*, tor_addr
) {
143 if (tor_addr_is_v4(tor_addr
)) {
146 } SMARTLIST_FOREACH_END(tor_addr
);
151 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
152 * that is an IPv6 address. Otherwise, return 0.
155 smartlist_contains_ipv6_tor_addr(smartlist_t
*smartlist
)
157 SMARTLIST_FOREACH_BEGIN(smartlist
, tor_addr_t
*, tor_addr
) {
158 /* Since there's no tor_addr_is_v6, assume all non-v4s are v6 */
159 if (!tor_addr_is_v4(tor_addr
)) {
162 } SMARTLIST_FOREACH_END(tor_addr
);
167 #ifdef HAVE_IFADDRS_TO_SMARTLIST
169 test_address_ifaddrs_to_smartlist(void *arg
)
171 struct ifaddrs
*ifa
= NULL
;
172 struct ifaddrs
*ifa_ipv4
= NULL
;
173 struct ifaddrs
*ifa_ipv6
= NULL
;
174 struct sockaddr_in
*ipv4_sockaddr_local
= NULL
;
175 struct sockaddr_in
*netmask_slash8
= NULL
;
176 struct sockaddr_in
*ipv4_sockaddr_remote
= NULL
;
177 struct sockaddr_in6
*ipv6_sockaddr
= NULL
;
178 smartlist_t
*smartlist
= NULL
;
179 tor_addr_t
*tor_addr
= NULL
;
180 struct sockaddr
*sockaddr_to_check
= NULL
;
185 netmask_slash8
= sockaddr_in_from_string("255.0.0.0",NULL
);
186 ipv4_sockaddr_local
= sockaddr_in_from_string("127.0.0.1",NULL
);
187 ipv4_sockaddr_remote
= sockaddr_in_from_string("128.52.160.20",NULL
);
189 ipv6_sockaddr
= tor_malloc(sizeof(struct sockaddr_in6
));
190 ipv6_sockaddr
->sin6_family
= AF_INET6
;
191 ipv6_sockaddr
->sin6_port
= 0;
192 tor_inet_pton(AF_INET6
, "2001:db8:8714:3a90::12",
193 &(ipv6_sockaddr
->sin6_addr
));
195 ifa
= tor_malloc(sizeof(struct ifaddrs
));
196 ifa_ipv4
= tor_malloc(sizeof(struct ifaddrs
));
197 ifa_ipv6
= tor_malloc(sizeof(struct ifaddrs
));
199 ifa
->ifa_next
= ifa_ipv4
;
200 ifa
->ifa_name
= tor_strdup("eth0");
201 ifa
->ifa_flags
= IFF_UP
| IFF_RUNNING
;
202 ifa
->ifa_addr
= (struct sockaddr
*)ipv4_sockaddr_local
;
203 ifa
->ifa_netmask
= (struct sockaddr
*)netmask_slash8
;
204 ifa
->ifa_dstaddr
= NULL
;
205 ifa
->ifa_data
= NULL
;
207 ifa_ipv4
->ifa_next
= ifa_ipv6
;
208 ifa_ipv4
->ifa_name
= tor_strdup("eth1");
209 ifa_ipv4
->ifa_flags
= IFF_UP
| IFF_RUNNING
;
210 ifa_ipv4
->ifa_addr
= (struct sockaddr
*)ipv4_sockaddr_remote
;
211 ifa_ipv4
->ifa_netmask
= (struct sockaddr
*)netmask_slash8
;
212 ifa_ipv4
->ifa_dstaddr
= NULL
;
213 ifa_ipv4
->ifa_data
= NULL
;
215 ifa_ipv6
->ifa_next
= NULL
;
216 ifa_ipv6
->ifa_name
= tor_strdup("eth2");
217 ifa_ipv6
->ifa_flags
= IFF_UP
| IFF_RUNNING
;
218 ifa_ipv6
->ifa_addr
= (struct sockaddr
*)ipv6_sockaddr
;
219 ifa_ipv6
->ifa_netmask
= NULL
;
220 ifa_ipv6
->ifa_dstaddr
= NULL
;
221 ifa_ipv6
->ifa_data
= NULL
;
223 smartlist
= ifaddrs_to_smartlist(ifa
);
225 tt_assert(smartlist
);
226 tt_assert(smartlist_len(smartlist
) == 3);
228 sockaddr_to_check
= tor_malloc(sizeof(struct sockaddr_in6
));
230 tor_addr
= smartlist_get(smartlist
,0);
232 tor_addr_to_sockaddr(tor_addr
,0,sockaddr_to_check
,
233 sizeof(struct sockaddr_in
));
235 tt_int_op(addr_len
,==,sizeof(struct sockaddr_in
));
236 tt_assert(sockaddr_in_are_equal((struct sockaddr_in
*)sockaddr_to_check
,
237 ipv4_sockaddr_local
));
239 tor_addr
= smartlist_get(smartlist
,1);
241 tor_addr_to_sockaddr(tor_addr
,0,sockaddr_to_check
,
242 sizeof(struct sockaddr_in
));
244 tt_int_op(addr_len
,==,sizeof(struct sockaddr_in
));
245 tt_assert(sockaddr_in_are_equal((struct sockaddr_in
*)sockaddr_to_check
,
246 ipv4_sockaddr_remote
));
248 tor_addr
= smartlist_get(smartlist
,2);
250 tor_addr_to_sockaddr(tor_addr
,0,sockaddr_to_check
,
251 sizeof(struct sockaddr_in6
));
253 tt_int_op(addr_len
,==,sizeof(struct sockaddr_in6
));
254 tt_assert(sockaddr_in6_are_equal((struct sockaddr_in6
*)sockaddr_to_check
,
258 tor_free(netmask_slash8
);
259 tor_free(ipv4_sockaddr_local
);
260 tor_free(ipv4_sockaddr_remote
);
261 tor_free(ipv6_sockaddr
);
262 tor_free(ifa
->ifa_name
);
263 tor_free(ifa_ipv4
->ifa_name
);
264 tor_free(ifa_ipv6
->ifa_name
);
268 tor_free(sockaddr_to_check
);
270 SMARTLIST_FOREACH(smartlist
, tor_addr_t
*, t
, tor_free(t
));
271 smartlist_free(smartlist
);
277 test_address_get_if_addrs_ifaddrs(void *arg
)
280 smartlist_t
*results
= NULL
;
284 results
= get_interface_addresses_ifaddrs(LOG_ERR
);
287 /* Some FreeBSD jails don't have localhost IP address. Instead, they only
288 * have the address assigned to the jail (whatever that may be).
289 * And a jail without a network connection might not have any addresses at
291 tt_assert(!smartlist_contains_null_tor_addr(results
));
293 /* If there are addresses, they must be IPv4 or IPv6 */
294 if (smartlist_len(results
) > 0) {
295 tt_assert(smartlist_contains_ipv4_tor_addr(results
)
296 || smartlist_contains_ipv6_tor_addr(results
));
300 SMARTLIST_FOREACH(results
, tor_addr_t
*, t
, tor_free(t
));
301 smartlist_free(results
);
307 #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
310 test_address_get_if_addrs_win32(void *arg
)
313 smartlist_t
*results
= NULL
;
317 results
= get_interface_addresses_win32(LOG_ERR
);
319 tt_int_op(smartlist_len(results
),>=,1);
320 tt_assert(smartlist_contains_localhost_tor_addr(results
));
321 tt_assert(!smartlist_contains_null_tor_addr(results
));
323 /* If there are addresses, they must be IPv4 or IPv6 */
324 if (smartlist_len(results
) > 0) {
325 tt_assert(smartlist_contains_ipv4_tor_addr(results
)
326 || smartlist_contains_ipv6_tor_addr(results
));
330 SMARTLIST_FOREACH(results
, tor_addr_t
*, t
, tor_free(t
));
336 test_address_ip_adapter_addresses_to_smartlist(void *arg
)
339 IP_ADAPTER_ADDRESSES
*addrs1
;
340 IP_ADAPTER_ADDRESSES
*addrs2
;
342 IP_ADAPTER_UNICAST_ADDRESS
*unicast11
;
343 IP_ADAPTER_UNICAST_ADDRESS
*unicast12
;
344 IP_ADAPTER_UNICAST_ADDRESS
*unicast21
;
346 smartlist_t
*result
= NULL
;
348 struct sockaddr_in
*sockaddr_test1
;
349 struct sockaddr_in
*sockaddr_test2
;
350 struct sockaddr_in
*sockaddr_localhost
;
351 struct sockaddr_in
*sockaddr_to_check
;
353 tor_addr_t
*tor_addr
;
356 (void)sockaddr_in6_are_equal
;
358 sockaddr_to_check
= tor_malloc_zero(sizeof(struct sockaddr_in
));
361 tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES
));
363 addrs1
->FirstUnicastAddress
=
364 unicast11
= tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS
));
365 sockaddr_test1
= sockaddr_in_from_string("86.59.30.40",NULL
);
366 unicast11
->Address
.lpSockaddr
= (LPSOCKADDR
)sockaddr_test1
;
368 unicast11
->Next
= unicast12
=
369 tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS
));
370 sockaddr_test2
= sockaddr_in_from_string("93.95.227.222", NULL
);
371 unicast12
->Address
.lpSockaddr
= (LPSOCKADDR
)sockaddr_test2
;
373 addrs1
->Next
= addrs2
=
374 tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES
));
376 addrs2
->FirstUnicastAddress
=
377 unicast21
= tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS
));
378 sockaddr_localhost
= sockaddr_in_from_string("127.0.0.1", NULL
);
379 unicast21
->Address
.lpSockaddr
= (LPSOCKADDR
)sockaddr_localhost
;
381 result
= ip_adapter_addresses_to_smartlist(addrs1
);
384 tt_assert(smartlist_len(result
) == 3);
386 tor_addr
= smartlist_get(result
,0);
388 tor_addr_to_sockaddr(tor_addr
,0,(struct sockaddr
*)sockaddr_to_check
,
389 sizeof(struct sockaddr_in
));
391 tt_assert(sockaddr_in_are_equal(sockaddr_test1
,sockaddr_to_check
));
393 tor_addr
= smartlist_get(result
,1);
395 tor_addr_to_sockaddr(tor_addr
,0,(struct sockaddr
*)sockaddr_to_check
,
396 sizeof(struct sockaddr_in
));
398 tt_assert(sockaddr_in_are_equal(sockaddr_test2
,sockaddr_to_check
));
400 tor_addr
= smartlist_get(result
,2);
402 tor_addr_to_sockaddr(tor_addr
,0,(struct sockaddr
*)sockaddr_to_check
,
403 sizeof(struct sockaddr_in
));
405 tt_assert(sockaddr_in_are_equal(sockaddr_localhost
,sockaddr_to_check
));
408 SMARTLIST_FOREACH(result
, tor_addr_t
*, t
, tor_free(t
));
409 smartlist_free(result
);
412 tor_free(unicast11
->Address
.lpSockaddr
);
414 tor_free(unicast12
->Address
.lpSockaddr
);
416 tor_free(unicast21
->Address
.lpSockaddr
);
418 tor_free(sockaddr_to_check
);
423 #ifdef HAVE_IFCONF_TO_SMARTLIST
426 test_address_ifreq_to_smartlist(void *arg
)
428 smartlist_t
*results
= NULL
;
429 const tor_addr_t
*tor_addr
= NULL
;
430 struct sockaddr_in
*sockaddr
= NULL
;
431 struct sockaddr_in
*sockaddr_eth1
= NULL
;
432 struct sockaddr_in
*sockaddr_to_check
= NULL
;
436 struct ifreq
*ifr_next
;
442 sockaddr_to_check
= tor_malloc(sizeof(struct sockaddr_in
));
444 ifr
= tor_malloc(sizeof(struct ifreq
));
445 memset(ifr
,0,sizeof(struct ifreq
));
446 strlcpy(ifr
->ifr_name
,"lo",3);
447 sockaddr
= (struct sockaddr_in
*) &(ifr
->ifr_ifru
.ifru_addr
);
448 sockaddr_in_from_string("127.0.0.1",sockaddr
);
450 ifc
= tor_malloc(sizeof(struct ifconf
));
451 memset(ifc
,0,sizeof(struct ifconf
));
452 ifc
->ifc_len
= sizeof(struct ifreq
);
453 ifc
->ifc_ifcu
.ifcu_req
= ifr
;
455 results
= ifreq_to_smartlist(ifc
->ifc_buf
,ifc
->ifc_len
);
456 tt_int_op(smartlist_len(results
),==,1);
458 tor_addr
= smartlist_get(results
, 0);
460 tor_addr_to_sockaddr(tor_addr
,0,(struct sockaddr
*)sockaddr_to_check
,
461 sizeof(struct sockaddr_in
));
463 tt_int_op(addr_len
,==,sizeof(struct sockaddr_in
));
464 tt_assert(sockaddr_in_are_equal(sockaddr
,sockaddr_to_check
));
466 ifr
= tor_realloc(ifr
,2*sizeof(struct ifreq
));
468 strlcpy(ifr_next
->ifr_name
,"eth1",5);
469 ifc
->ifc_len
= 2*sizeof(struct ifreq
);
470 ifc
->ifc_ifcu
.ifcu_req
= ifr
;
471 sockaddr
= (struct sockaddr_in
*) &(ifr
->ifr_ifru
.ifru_addr
);
473 sockaddr_eth1
= (struct sockaddr_in
*) &(ifr_next
->ifr_ifru
.ifru_addr
);
474 sockaddr_in_from_string("192.168.10.55",sockaddr_eth1
);
475 SMARTLIST_FOREACH(results
, tor_addr_t
*, t
, tor_free(t
));
476 smartlist_free(results
);
478 results
= ifreq_to_smartlist(ifc
->ifc_buf
,ifc
->ifc_len
);
479 tt_int_op(smartlist_len(results
),==,2);
481 tor_addr
= smartlist_get(results
, 0);
483 tor_addr_to_sockaddr(tor_addr
,0,(struct sockaddr
*)sockaddr_to_check
,
484 sizeof(struct sockaddr_in
));
486 tt_int_op(addr_len
,==,sizeof(struct sockaddr_in
));
487 tt_assert(sockaddr_in_are_equal(sockaddr
,sockaddr_to_check
));
489 tor_addr
= smartlist_get(results
, 1);
491 tor_addr_to_sockaddr(tor_addr
,0,(struct sockaddr
*)sockaddr_to_check
,
492 sizeof(struct sockaddr_in
));
494 tt_int_op(addr_len
,==,sizeof(struct sockaddr_in
));
495 tt_assert(sockaddr_in_are_equal(sockaddr_eth1
,sockaddr_to_check
));
498 tor_free(sockaddr_to_check
);
499 SMARTLIST_FOREACH(results
, tor_addr_t
*, t
, tor_free(t
));
500 smartlist_free(results
);
507 test_address_get_if_addrs_ioctl(void *arg
)
510 smartlist_t
*result
= NULL
;
514 result
= get_interface_addresses_ioctl(LOG_ERR
);
516 /* On an IPv6-only system, this will fail and return NULL
520 /* Some FreeBSD jails don't have localhost IP address. Instead, they only
521 * have the address assigned to the jail (whatever that may be).
522 * And a jail without a network connection might not have any addresses at
525 tt_assert(!smartlist_contains_null_tor_addr(result
));
527 /* If there are addresses, they must be IPv4 or IPv6.
528 * (AIX supports IPv6 from SIOCGIFCONF.) */
529 if (smartlist_len(result
) > 0) {
530 tt_assert(smartlist_contains_ipv4_tor_addr(result
)
531 || smartlist_contains_ipv6_tor_addr(result
));
537 SMARTLIST_FOREACH(result
, tor_addr_t
*, t
, tor_free(t
));
538 smartlist_free(result
);
545 #define FAKE_SOCKET_FD (42)
548 fake_open_socket(int domain
, int type
, int protocol
)
554 return FAKE_SOCKET_FD
;
557 static int last_connected_socket_fd
= 0;
559 static int connect_retval
= 0;
562 pretend_to_connect(tor_socket_t socket
, const struct sockaddr
*address
,
563 socklen_t address_len
)
568 last_connected_socket_fd
= socket
;
570 return connect_retval
;
573 static struct sockaddr
*mock_addr
= NULL
;
576 fake_getsockname(tor_socket_t socket
, struct sockaddr
*address
,
577 socklen_t
*address_len
)
579 socklen_t bytes_to_copy
= 0;
585 if (mock_addr
->sa_family
== AF_INET
) {
586 bytes_to_copy
= sizeof(struct sockaddr_in
);
587 } else if (mock_addr
->sa_family
== AF_INET6
) {
588 bytes_to_copy
= sizeof(struct sockaddr_in6
);
593 if (*address_len
< bytes_to_copy
) {
597 memcpy(address
,mock_addr
,bytes_to_copy
);
598 *address_len
= bytes_to_copy
;
604 test_address_udp_socket_trick_whitebox(void *arg
)
607 tor_addr_t
*addr_from_hack
= tor_malloc_zero(sizeof(tor_addr_t
));
608 struct sockaddr_in6
*mock_addr6
;
609 struct sockaddr_in6
*ipv6_to_check
=
610 tor_malloc_zero(sizeof(struct sockaddr_in6
));
614 MOCK(tor_open_socket
,fake_open_socket
);
615 MOCK(tor_connect_socket
,pretend_to_connect
);
616 MOCK(tor_getsockname
,fake_getsockname
);
618 mock_addr
= tor_malloc_zero(sizeof(struct sockaddr_storage
));
619 sockaddr_in_from_string("23.32.246.118",(struct sockaddr_in
*)mock_addr
);
622 get_interface_address6_via_udp_socket_hack(LOG_DEBUG
,
623 AF_INET
, addr_from_hack
);
625 tt_int_op(hack_retval
,==,0);
626 tt_assert(tor_addr_eq_ipv4h(addr_from_hack
, 0x1720f676));
628 /* Now, lets do an IPv6 case. */
629 memset(mock_addr
,0,sizeof(struct sockaddr_storage
));
631 mock_addr6
= (struct sockaddr_in6
*)mock_addr
;
632 mock_addr6
->sin6_family
= AF_INET6
;
633 mock_addr6
->sin6_port
= 0;
634 tor_inet_pton(AF_INET6
,"2001:cdba::3257:9652",&(mock_addr6
->sin6_addr
));
637 get_interface_address6_via_udp_socket_hack(LOG_DEBUG
,
638 AF_INET6
, addr_from_hack
);
640 tt_int_op(hack_retval
,==,0);
642 tor_addr_to_sockaddr(addr_from_hack
,0,(struct sockaddr
*)ipv6_to_check
,
643 sizeof(struct sockaddr_in6
));
645 tt_assert(sockaddr_in6_are_equal(mock_addr6
,ipv6_to_check
));
647 UNMOCK(tor_open_socket
);
648 UNMOCK(tor_connect_socket
);
649 UNMOCK(tor_getsockname
);
652 tor_free(ipv6_to_check
);
654 tor_free(addr_from_hack
);
659 test_address_udp_socket_trick_blackbox(void *arg
)
661 /* We want get_interface_address6_via_udp_socket_hack() to yield
662 * the same valid address that get_interface_address6() returns.
663 * If the latter is unable to find a valid address, we want
664 * _hack() to fail and return-1.
666 * Furthermore, we want _hack() never to crash, even if
667 * get_interface_addresses_raw() is returning NULL.
671 tor_addr_t addr4_to_check
;
673 tor_addr_t addr6_to_check
;
674 int retval
, retval_reference
;
679 retval_reference
= get_interface_address6(LOG_DEBUG
,AF_INET
,&addr4
);
680 retval
= get_interface_address6_via_udp_socket_hack(LOG_DEBUG
,
684 tt_int_op(retval
,==,retval_reference
);
685 tt_assert( (retval
== -1 && retval_reference
== -1) ||
686 (tor_addr_compare(&addr4
,&addr4_to_check
,CMP_EXACT
) == 0) );
688 retval_reference
= get_interface_address6(LOG_DEBUG
,AF_INET6
,&addr6
);
689 retval
= get_interface_address6_via_udp_socket_hack(LOG_DEBUG
,
693 tt_int_op(retval
,==,retval_reference
);
694 tt_assert( (retval
== -1 && retval_reference
== -1) ||
695 (tor_addr_compare(&addr6
,&addr6_to_check
,CMP_EXACT
) == 0) );
698 /* Both of the blackbox test cases fail horribly if:
699 * * The host has no external addreses.
700 * * There are multiple interfaces with either AF_INET or AF_INET6.
701 * * The last address isn't the one associated with the default route.
703 * The tests SHOULD be re-enabled when #12377 is fixed correctly, but till
704 * then this fails a lot, in situations we expect failures due to knowing
705 * about the code being broken.
708 (void)addr4_to_check
;
709 (void)addr6_to_check
;
711 (void) retval_reference
;
714 /* When family is neither AF_INET nor AF_INET6, we want _hack to
715 * fail and return -1.
718 retval
= get_interface_address6_via_udp_socket_hack(LOG_DEBUG
,
719 AF_INET
+AF_INET6
,&addr4
);
721 tt_assert(retval
== -1);
728 test_address_get_if_addrs_list_internal(void *arg
)
730 smartlist_t
*results
= NULL
;
734 results
= get_interface_address_list(LOG_ERR
, 1);
736 tt_assert(results
!= NULL
);
737 /* When the network is down, a system might not have any non-local
738 * non-multicast addresseses, not even internal ones.
739 * Unit tests shouldn't fail because of this. */
740 tt_int_op(smartlist_len(results
),>=,0);
742 tt_assert(!smartlist_contains_localhost_tor_addr(results
));
743 tt_assert(!smartlist_contains_multicast_tor_addr(results
));
744 /* The list may or may not contain internal addresses */
745 tt_assert(!smartlist_contains_null_tor_addr(results
));
747 /* if there are any addresses, they must be IPv4 */
748 if (smartlist_len(results
) > 0) {
749 tt_assert(smartlist_contains_ipv4_tor_addr(results
));
751 tt_assert(!smartlist_contains_ipv6_tor_addr(results
));
754 free_interface_address_list(results
);
759 test_address_get_if_addrs_list_no_internal(void *arg
)
761 smartlist_t
*results
= NULL
;
765 results
= get_interface_address_list(LOG_ERR
, 0);
767 tt_assert(results
!= NULL
);
768 /* Work even on systems with only internal IPv4 addresses */
769 tt_int_op(smartlist_len(results
),>=,0);
771 tt_assert(!smartlist_contains_localhost_tor_addr(results
));
772 tt_assert(!smartlist_contains_multicast_tor_addr(results
));
773 tt_assert(!smartlist_contains_internal_tor_addr(results
));
774 tt_assert(!smartlist_contains_null_tor_addr(results
));
776 /* if there are any addresses, they must be IPv4 */
777 if (smartlist_len(results
) > 0) {
778 tt_assert(smartlist_contains_ipv4_tor_addr(results
));
780 tt_assert(!smartlist_contains_ipv6_tor_addr(results
));
783 free_interface_address_list(results
);
788 test_address_get_if_addrs6_list_internal(void *arg
)
790 smartlist_t
*results
= NULL
;
794 results
= get_interface_address6_list(LOG_ERR
, AF_INET6
, 1);
796 tt_assert(results
!= NULL
);
797 /* Work even on systems without IPv6 interfaces */
798 tt_int_op(smartlist_len(results
),>=,0);
800 tt_assert(!smartlist_contains_localhost_tor_addr(results
));
801 tt_assert(!smartlist_contains_multicast_tor_addr(results
));
802 /* The list may or may not contain internal addresses */
803 tt_assert(!smartlist_contains_null_tor_addr(results
));
805 /* if there are any addresses, they must be IPv6 */
806 tt_assert(!smartlist_contains_ipv4_tor_addr(results
));
807 if (smartlist_len(results
) > 0) {
808 tt_assert(smartlist_contains_ipv6_tor_addr(results
));
812 free_interface_address6_list(results
);
817 test_address_get_if_addrs6_list_no_internal(void *arg
)
819 smartlist_t
*results
= NULL
;
823 results
= get_interface_address6_list(LOG_ERR
, AF_INET6
, 0);
825 tt_assert(results
!= NULL
);
826 /* Work even on systems without IPv6 interfaces */
827 tt_int_op(smartlist_len(results
),>=,0);
829 tt_assert(!smartlist_contains_localhost_tor_addr(results
));
830 tt_assert(!smartlist_contains_multicast_tor_addr(results
));
831 tt_assert(!smartlist_contains_internal_tor_addr(results
));
832 tt_assert(!smartlist_contains_null_tor_addr(results
));
834 /* if there are any addresses, they must be IPv6 */
835 tt_assert(!smartlist_contains_ipv4_tor_addr(results
));
836 if (smartlist_len(results
) > 0) {
837 tt_assert(smartlist_contains_ipv6_tor_addr(results
));
841 free_interface_address6_list(results
);
845 static int called_get_interface_addresses_raw
= 0;
848 mock_get_interface_addresses_raw_fail(int severity
)
852 called_get_interface_addresses_raw
++;
853 return smartlist_new();
856 static int called_get_interface_address6_via_udp_socket_hack
= 0;
859 mock_get_interface_address6_via_udp_socket_hack_fail(int severity
,
867 called_get_interface_address6_via_udp_socket_hack
++;
872 test_address_get_if_addrs_internal_fail(void *arg
)
874 smartlist_t
*results1
= NULL
, *results2
= NULL
;
876 uint32_t ipv4h_addr
= 0;
877 tor_addr_t ipv6_addr
;
879 memset(&ipv6_addr
, 0, sizeof(tor_addr_t
));
883 MOCK(get_interface_addresses_raw
,
884 mock_get_interface_addresses_raw_fail
);
885 MOCK(get_interface_address6_via_udp_socket_hack
,
886 mock_get_interface_address6_via_udp_socket_hack_fail
);
888 results1
= get_interface_address6_list(LOG_ERR
, AF_INET6
, 1);
889 tt_assert(results1
!= NULL
);
890 tt_int_op(smartlist_len(results1
),==,0);
892 results2
= get_interface_address_list(LOG_ERR
, 1);
893 tt_assert(results2
!= NULL
);
894 tt_int_op(smartlist_len(results2
),==,0);
896 rv
= get_interface_address6(LOG_ERR
, AF_INET6
, &ipv6_addr
);
899 rv
= get_interface_address(LOG_ERR
, &ipv4h_addr
);
903 UNMOCK(get_interface_addresses_raw
);
904 UNMOCK(get_interface_address6_via_udp_socket_hack
);
905 free_interface_address6_list(results1
);
906 free_interface_address6_list(results2
);
911 test_address_get_if_addrs_no_internal_fail(void *arg
)
913 smartlist_t
*results1
= NULL
, *results2
= NULL
;
917 MOCK(get_interface_addresses_raw
,
918 mock_get_interface_addresses_raw_fail
);
919 MOCK(get_interface_address6_via_udp_socket_hack
,
920 mock_get_interface_address6_via_udp_socket_hack_fail
);
922 results1
= get_interface_address6_list(LOG_ERR
, AF_INET6
, 0);
923 tt_assert(results1
!= NULL
);
924 tt_int_op(smartlist_len(results1
),==,0);
926 results2
= get_interface_address_list(LOG_ERR
, 0);
927 tt_assert(results2
!= NULL
);
928 tt_int_op(smartlist_len(results2
),==,0);
931 UNMOCK(get_interface_addresses_raw
);
932 UNMOCK(get_interface_address6_via_udp_socket_hack
);
933 free_interface_address6_list(results1
);
934 free_interface_address6_list(results2
);
939 test_address_get_if_addrs(void *arg
)
947 rv
= get_interface_address(LOG_ERR
, &addr_h
);
949 /* When the network is down, a system might not have any non-local
950 * non-multicast IPv4 addresses, not even internal ones.
951 * Unit tests shouldn't fail because of this. */
953 tor_addr_from_ipv4h(&tor_addr
, addr_h
);
955 tt_assert(!tor_addr_is_loopback(&tor_addr
));
956 tt_assert(!tor_addr_is_multicast(&tor_addr
));
957 /* The address may or may not be an internal address */
959 tt_assert(tor_addr_is_v4(&tor_addr
));
967 test_address_get_if_addrs6(void *arg
)
974 rv
= get_interface_address6(LOG_ERR
, AF_INET6
, &tor_addr
);
976 /* Work even on systems without IPv6 interfaces */
978 tt_assert(!tor_addr_is_loopback(&tor_addr
));
979 tt_assert(!tor_addr_is_multicast(&tor_addr
));
980 /* The address may or may not be an internal address */
982 tt_assert(!tor_addr_is_v4(&tor_addr
));
990 test_address_tor_addr_to_in6(void *ignored
)
993 tor_addr_t
*a
= tor_malloc_zero(sizeof(tor_addr_t
));
994 const struct in6_addr
*res
;
995 uint8_t expected
[16] = {42, 1, 2, 3, 4, 5, 6, 7, 8, 9,
996 10, 11, 12, 13, 14, 15};
999 res
= tor_addr_to_in6(a
);
1002 a
->family
= AF_INET6
;
1003 memcpy(a
->addr
.in6_addr
.s6_addr
, expected
, 16);
1004 res
= tor_addr_to_in6(a
);
1006 tt_mem_op(res
->s6_addr
, OP_EQ
, expected
, 16);
1013 test_address_tor_addr_to_in(void *ignored
)
1016 tor_addr_t
*a
= tor_malloc_zero(sizeof(tor_addr_t
));
1017 const struct in_addr
*res
;
1019 a
->family
= AF_INET6
;
1020 res
= tor_addr_to_in(a
);
1023 a
->family
= AF_INET
;
1024 a
->addr
.in_addr
.s_addr
= 44;
1025 res
= tor_addr_to_in(a
);
1027 tt_int_op(res
->s_addr
, OP_EQ
, 44);
1034 test_address_tor_addr_to_ipv4n(void *ignored
)
1037 tor_addr_t
*a
= tor_malloc_zero(sizeof(tor_addr_t
));
1040 a
->family
= AF_INET6
;
1041 res
= tor_addr_to_ipv4n(a
);
1044 a
->family
= AF_INET
;
1045 a
->addr
.in_addr
.s_addr
= 43;
1046 res
= tor_addr_to_ipv4n(a
);
1048 tt_int_op(res
, OP_EQ
, 43);
1055 test_address_tor_addr_to_mapped_ipv4h(void *ignored
)
1058 tor_addr_t
*a
= tor_malloc_zero(sizeof(tor_addr_t
));
1060 uint8_t toset
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 42};
1062 a
->family
= AF_INET
;
1063 res
= tor_addr_to_mapped_ipv4h(a
);
1066 a
->family
= AF_INET6
;
1068 memcpy(a
->addr
.in6_addr
.s6_addr
, toset
, 16);
1069 res
= tor_addr_to_mapped_ipv4h(a
);
1071 tt_int_op(res
, OP_EQ
, 42);
1078 test_address_tor_addr_eq_ipv4h(void *ignored
)
1081 tor_addr_t
*a
= tor_malloc_zero(sizeof(tor_addr_t
));
1084 a
->family
= AF_INET6
;
1085 res
= tor_addr_eq_ipv4h(a
, 42);
1088 a
->family
= AF_INET
;
1089 a
->addr
.in_addr
.s_addr
= 52;
1090 res
= tor_addr_eq_ipv4h(a
, 42);
1093 a
->addr
.in_addr
.s_addr
= 52;
1094 res
= tor_addr_eq_ipv4h(a
, ntohl(52));
1101 #define ADDRESS_TEST(name, flags) \
1102 { #name, test_address_ ## name, flags, NULL, NULL }
1104 struct testcase_t address_tests
[] = {
1105 ADDRESS_TEST(udp_socket_trick_whitebox
, TT_FORK
),
1106 ADDRESS_TEST(udp_socket_trick_blackbox
, TT_FORK
),
1107 ADDRESS_TEST(get_if_addrs_list_internal
, 0),
1108 ADDRESS_TEST(get_if_addrs_list_no_internal
, 0),
1109 ADDRESS_TEST(get_if_addrs6_list_internal
, 0),
1110 ADDRESS_TEST(get_if_addrs6_list_no_internal
, 0),
1111 ADDRESS_TEST(get_if_addrs_internal_fail
, 0),
1112 ADDRESS_TEST(get_if_addrs_no_internal_fail
, 0),
1113 ADDRESS_TEST(get_if_addrs
, 0),
1114 ADDRESS_TEST(get_if_addrs6
, 0),
1115 #ifdef HAVE_IFADDRS_TO_SMARTLIST
1116 ADDRESS_TEST(get_if_addrs_ifaddrs
, TT_FORK
),
1117 ADDRESS_TEST(ifaddrs_to_smartlist
, 0),
1119 #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
1120 ADDRESS_TEST(get_if_addrs_win32
, TT_FORK
),
1121 ADDRESS_TEST(ip_adapter_addresses_to_smartlist
, 0),
1123 #ifdef HAVE_IFCONF_TO_SMARTLIST
1124 ADDRESS_TEST(get_if_addrs_ioctl
, TT_FORK
),
1125 ADDRESS_TEST(ifreq_to_smartlist
, 0),
1127 ADDRESS_TEST(tor_addr_to_in6
, 0),
1128 ADDRESS_TEST(tor_addr_to_in
, 0),
1129 ADDRESS_TEST(tor_addr_to_ipv4n
, 0),
1130 ADDRESS_TEST(tor_addr_to_mapped_ipv4h
, 0),
1131 ADDRESS_TEST(tor_addr_eq_ipv4h
, 0),