test_address.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* Copyright (c) 2014-2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define ADDRESS_PRIVATE
  4. #include "orconfig.h"
  5. #ifdef _WIN32
  6. #include <winsock2.h>
  7. /* For access to structs needed by GetAdaptersAddresses */
  8. #include <iphlpapi.h>
  9. #endif
  10. #ifdef HAVE_IFADDRS_TO_SMARTLIST
  11. #include <net/if.h>
  12. #include <ifaddrs.h>
  13. #endif
  14. #ifdef HAVE_IFCONF_TO_SMARTLIST
  15. #ifdef HAVE_SYS_IOCTL_H
  16. #include <sys/ioctl.h>
  17. #endif
  18. #include <net/if.h>
  19. #endif
  20. #include "or.h"
  21. #include "address.h"
  22. #include "test.h"
  23. /** Return 1 iff <b>sockaddr1</b> and <b>sockaddr2</b> represent
  24. * the same IP address and port combination. Otherwise, return 0.
  25. */
  26. static uint8_t
  27. sockaddr_in_are_equal(struct sockaddr_in *sockaddr1,
  28. struct sockaddr_in *sockaddr2)
  29. {
  30. return ((sockaddr1->sin_family == sockaddr2->sin_family) &&
  31. (sockaddr1->sin_port == sockaddr2->sin_port) &&
  32. (sockaddr1->sin_addr.s_addr == sockaddr2->sin_addr.s_addr));
  33. }
  34. /** Return 1 iff <b>sockaddr1</b> and <b>sockaddr2</b> represent
  35. * the same IP address and port combination. Otherwise, return 0.
  36. */
  37. static uint8_t
  38. sockaddr_in6_are_equal(struct sockaddr_in6 *sockaddr1,
  39. struct sockaddr_in6 *sockaddr2)
  40. {
  41. return ((sockaddr1->sin6_family == sockaddr2->sin6_family) &&
  42. (sockaddr1->sin6_port == sockaddr2->sin6_port) &&
  43. (tor_memeq(sockaddr1->sin6_addr.s6_addr,
  44. sockaddr2->sin6_addr.s6_addr,16)));
  45. }
  46. /** Create a sockaddr_in structure from IP address string <b>ip_str</b>.
  47. *
  48. * If <b>out</b> is not NULL, write the result
  49. * to the memory address in <b>out</b>. Otherwise, allocate the memory
  50. * for result. On success, return pointer to result. Otherwise, return
  51. * NULL.
  52. */
  53. static struct sockaddr_in *
  54. sockaddr_in_from_string(const char *ip_str, struct sockaddr_in *out)
  55. {
  56. // [FIXME: add some error checking?]
  57. if (!out)
  58. out = tor_malloc_zero(sizeof(struct sockaddr_in));
  59. out->sin_family = AF_INET;
  60. out->sin_port = 0;
  61. tor_inet_pton(AF_INET,ip_str,&(out->sin_addr));
  62. return out;
  63. }
  64. /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
  65. * that points to 127.0.0.1. Otherwise, return 0.
  66. */
  67. static int
  68. smartlist_contains_localhost_tor_addr(smartlist_t *smartlist)
  69. {
  70. int found_localhost = 0;
  71. struct sockaddr_in *sockaddr_localhost;
  72. struct sockaddr_storage *sockaddr_to_check;
  73. sockaddr_localhost = sockaddr_in_from_string("127.0.0.1",NULL);
  74. sockaddr_to_check = tor_malloc(sizeof(struct sockaddr_in));
  75. SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
  76. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  77. sizeof(struct sockaddr_in));
  78. if (sockaddr_in_are_equal((struct sockaddr_in *)sockaddr_to_check,
  79. sockaddr_localhost)) {
  80. found_localhost = 1;
  81. break;
  82. }
  83. } SMARTLIST_FOREACH_END(tor_addr);
  84. tor_free(sockaddr_localhost);
  85. tor_free(sockaddr_to_check);
  86. return found_localhost;
  87. }
  88. #ifdef HAVE_IFADDRS_TO_SMARTLIST
  89. static void
  90. test_address_ifaddrs_to_smartlist(void *arg)
  91. {
  92. struct ifaddrs *ifa = NULL;
  93. struct ifaddrs *ifa_ipv4 = NULL;
  94. struct ifaddrs *ifa_ipv6 = NULL;
  95. struct sockaddr_in *ipv4_sockaddr_local = NULL;
  96. struct sockaddr_in *netmask_slash8 = NULL;
  97. struct sockaddr_in *ipv4_sockaddr_remote = NULL;
  98. struct sockaddr_in6 *ipv6_sockaddr = NULL;
  99. smartlist_t *smartlist = NULL;
  100. tor_addr_t *tor_addr = NULL;
  101. struct sockaddr *sockaddr_to_check = NULL;
  102. socklen_t addr_len;
  103. (void)arg;
  104. netmask_slash8 = sockaddr_in_from_string("255.0.0.0",NULL);
  105. ipv4_sockaddr_local = sockaddr_in_from_string("127.0.0.1",NULL);
  106. ipv4_sockaddr_remote = sockaddr_in_from_string("128.52.160.20",NULL);
  107. ipv6_sockaddr = tor_malloc(sizeof(struct sockaddr_in6));
  108. ipv6_sockaddr->sin6_family = AF_INET6;
  109. ipv6_sockaddr->sin6_port = 0;
  110. tor_inet_pton(AF_INET6, "2001:db8:8714:3a90::12",
  111. &(ipv6_sockaddr->sin6_addr));
  112. ifa = tor_malloc(sizeof(struct ifaddrs));
  113. ifa_ipv4 = tor_malloc(sizeof(struct ifaddrs));
  114. ifa_ipv6 = tor_malloc(sizeof(struct ifaddrs));
  115. ifa->ifa_next = ifa_ipv4;
  116. ifa->ifa_name = tor_strdup("eth0");
  117. ifa->ifa_flags = IFF_UP | IFF_RUNNING;
  118. ifa->ifa_addr = (struct sockaddr *)ipv4_sockaddr_local;
  119. ifa->ifa_netmask = (struct sockaddr *)netmask_slash8;
  120. ifa->ifa_dstaddr = NULL;
  121. ifa->ifa_data = NULL;
  122. ifa_ipv4->ifa_next = ifa_ipv6;
  123. ifa_ipv4->ifa_name = tor_strdup("eth1");
  124. ifa_ipv4->ifa_flags = IFF_UP | IFF_RUNNING;
  125. ifa_ipv4->ifa_addr = (struct sockaddr *)ipv4_sockaddr_remote;
  126. ifa_ipv4->ifa_netmask = (struct sockaddr *)netmask_slash8;
  127. ifa_ipv4->ifa_dstaddr = NULL;
  128. ifa_ipv4->ifa_data = NULL;
  129. ifa_ipv6->ifa_next = NULL;
  130. ifa_ipv6->ifa_name = tor_strdup("eth2");
  131. ifa_ipv6->ifa_flags = IFF_UP | IFF_RUNNING;
  132. ifa_ipv6->ifa_addr = (struct sockaddr *)ipv6_sockaddr;
  133. ifa_ipv6->ifa_netmask = NULL;
  134. ifa_ipv6->ifa_dstaddr = NULL;
  135. ifa_ipv6->ifa_data = NULL;
  136. smartlist = ifaddrs_to_smartlist(ifa);
  137. tt_assert(smartlist);
  138. tt_assert(smartlist_len(smartlist) == 3);
  139. sockaddr_to_check = tor_malloc(sizeof(struct sockaddr_in6));
  140. tor_addr = smartlist_get(smartlist,0);
  141. addr_len =
  142. tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
  143. sizeof(struct sockaddr_in));
  144. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  145. tt_assert(sockaddr_in_are_equal((struct sockaddr_in *)sockaddr_to_check,
  146. ipv4_sockaddr_local));
  147. tor_addr = smartlist_get(smartlist,1);
  148. addr_len =
  149. tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
  150. sizeof(struct sockaddr_in));
  151. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  152. tt_assert(sockaddr_in_are_equal((struct sockaddr_in *)sockaddr_to_check,
  153. ipv4_sockaddr_remote));
  154. tor_addr = smartlist_get(smartlist,2);
  155. addr_len =
  156. tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
  157. sizeof(struct sockaddr_in6));
  158. tt_int_op(addr_len,==,sizeof(struct sockaddr_in6));
  159. tt_assert(sockaddr_in6_are_equal((struct sockaddr_in6*)sockaddr_to_check,
  160. ipv6_sockaddr));
  161. done:
  162. tor_free(netmask_slash8);
  163. tor_free(ipv4_sockaddr_local);
  164. tor_free(ipv4_sockaddr_remote);
  165. tor_free(ipv6_sockaddr);
  166. tor_free(ifa->ifa_name);
  167. tor_free(ifa_ipv4->ifa_name);
  168. tor_free(ifa_ipv6->ifa_name);
  169. tor_free(ifa);
  170. tor_free(ifa_ipv4);
  171. tor_free(ifa_ipv6);
  172. tor_free(sockaddr_to_check);
  173. if (smartlist) {
  174. SMARTLIST_FOREACH(smartlist, tor_addr_t *, t, tor_free(t));
  175. smartlist_free(smartlist);
  176. }
  177. return;
  178. }
  179. static void
  180. test_address_get_if_addrs_ifaddrs(void *arg)
  181. {
  182. smartlist_t *results = NULL;
  183. (void)arg;
  184. results = get_interface_addresses_ifaddrs(LOG_ERR);
  185. tt_int_op(smartlist_len(results),>=,1);
  186. tt_assert(smartlist_contains_localhost_tor_addr(results));
  187. done:
  188. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  189. smartlist_free(results);
  190. return;
  191. }
  192. #endif
  193. #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
  194. static void
  195. test_address_get_if_addrs_win32(void *arg)
  196. {
  197. smartlist_t *results = NULL;
  198. (void)arg;
  199. results = get_interface_addresses_win32(LOG_ERR);
  200. tt_int_op(smartlist_len(results),>=,1);
  201. tt_assert(smartlist_contains_localhost_tor_addr(results));
  202. done:
  203. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  204. tor_free(results);
  205. return;
  206. }
  207. static void
  208. test_address_ip_adapter_addresses_to_smartlist(void *arg)
  209. {
  210. IP_ADAPTER_ADDRESSES *addrs1;
  211. IP_ADAPTER_ADDRESSES *addrs2;
  212. IP_ADAPTER_UNICAST_ADDRESS *unicast11;
  213. IP_ADAPTER_UNICAST_ADDRESS *unicast12;
  214. IP_ADAPTER_UNICAST_ADDRESS *unicast21;
  215. smartlist_t *result = NULL;
  216. struct sockaddr_in *sockaddr_test1;
  217. struct sockaddr_in *sockaddr_test2;
  218. struct sockaddr_in *sockaddr_localhost;
  219. struct sockaddr_in *sockaddr_to_check;
  220. tor_addr_t *tor_addr;
  221. (void)arg;
  222. (void)sockaddr_in6_are_equal;
  223. sockaddr_to_check = tor_malloc_zero(sizeof(struct sockaddr_in));
  224. addrs1 =
  225. tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES));
  226. addrs1->FirstUnicastAddress =
  227. unicast11 = tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
  228. sockaddr_test1 = sockaddr_in_from_string("86.59.30.40",NULL);
  229. unicast11->Address.lpSockaddr = (LPSOCKADDR)sockaddr_test1;
  230. unicast11->Next = unicast12 =
  231. tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
  232. sockaddr_test2 = sockaddr_in_from_string("93.95.227.222", NULL);
  233. unicast12->Address.lpSockaddr = (LPSOCKADDR)sockaddr_test2;
  234. addrs1->Next = addrs2 =
  235. tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES));
  236. addrs2->FirstUnicastAddress =
  237. unicast21 = tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
  238. sockaddr_localhost = sockaddr_in_from_string("127.0.0.1", NULL);
  239. unicast21->Address.lpSockaddr = (LPSOCKADDR)sockaddr_localhost;
  240. result = ip_adapter_addresses_to_smartlist(addrs1);
  241. tt_assert(result);
  242. tt_assert(smartlist_len(result) == 3);
  243. tor_addr = smartlist_get(result,0);
  244. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  245. sizeof(struct sockaddr_in));
  246. tt_assert(sockaddr_in_are_equal(sockaddr_test1,sockaddr_to_check));
  247. tor_addr = smartlist_get(result,1);
  248. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  249. sizeof(struct sockaddr_in));
  250. tt_assert(sockaddr_in_are_equal(sockaddr_test2,sockaddr_to_check));
  251. tor_addr = smartlist_get(result,2);
  252. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  253. sizeof(struct sockaddr_in));
  254. tt_assert(sockaddr_in_are_equal(sockaddr_localhost,sockaddr_to_check));
  255. done:
  256. SMARTLIST_FOREACH(result, tor_addr_t *, t, tor_free(t));
  257. smartlist_free(result);
  258. tor_free(addrs1);
  259. tor_free(addrs2);
  260. tor_free(unicast11->Address.lpSockaddr);
  261. tor_free(unicast11);
  262. tor_free(unicast12->Address.lpSockaddr);
  263. tor_free(unicast12);
  264. tor_free(unicast21->Address.lpSockaddr);
  265. tor_free(unicast21);
  266. tor_free(sockaddr_to_check);
  267. return;
  268. }
  269. #endif
  270. #ifdef HAVE_IFCONF_TO_SMARTLIST
  271. static void
  272. test_address_ifreq_to_smartlist(void *arg)
  273. {
  274. smartlist_t *results = NULL;
  275. const tor_addr_t *tor_addr = NULL;
  276. struct sockaddr_in *sockaddr = NULL;
  277. struct sockaddr_in *sockaddr_eth1 = NULL;
  278. struct sockaddr_in *sockaddr_to_check = NULL;
  279. struct ifconf *ifc;
  280. struct ifreq *ifr;
  281. struct ifreq *ifr_next;
  282. socklen_t addr_len;
  283. (void)arg;
  284. sockaddr_to_check = tor_malloc(sizeof(struct sockaddr_in));
  285. ifr = tor_malloc(sizeof(struct ifreq));
  286. memset(ifr,0,sizeof(struct ifreq));
  287. strlcpy(ifr->ifr_name,"lo",3);
  288. sockaddr = (struct sockaddr_in *) &(ifr->ifr_ifru.ifru_addr);
  289. sockaddr_in_from_string("127.0.0.1",sockaddr);
  290. ifc = tor_malloc(sizeof(struct ifconf));
  291. memset(ifc,0,sizeof(struct ifconf));
  292. ifc->ifc_len = sizeof(struct ifreq);
  293. ifc->ifc_ifcu.ifcu_req = ifr;
  294. results = ifreq_to_smartlist(ifc->ifc_buf,ifc->ifc_len);
  295. tt_int_op(smartlist_len(results),==,1);
  296. tor_addr = smartlist_get(results, 0);
  297. addr_len =
  298. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  299. sizeof(struct sockaddr_in));
  300. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  301. tt_assert(sockaddr_in_are_equal(sockaddr,sockaddr_to_check));
  302. ifr = tor_realloc(ifr,2*sizeof(struct ifreq));
  303. ifr_next = ifr+1;
  304. strlcpy(ifr_next->ifr_name,"eth1",5);
  305. ifc->ifc_len = 2*sizeof(struct ifreq);
  306. ifc->ifc_ifcu.ifcu_req = ifr;
  307. sockaddr = (struct sockaddr_in *) &(ifr->ifr_ifru.ifru_addr);
  308. sockaddr_eth1 = (struct sockaddr_in *) &(ifr_next->ifr_ifru.ifru_addr);
  309. sockaddr_in_from_string("192.168.10.55",sockaddr_eth1);
  310. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  311. smartlist_free(results);
  312. results = ifreq_to_smartlist(ifc->ifc_buf,ifc->ifc_len);
  313. tt_int_op(smartlist_len(results),==,2);
  314. tor_addr = smartlist_get(results, 0);
  315. addr_len =
  316. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  317. sizeof(struct sockaddr_in));
  318. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  319. tt_assert(sockaddr_in_are_equal(sockaddr,sockaddr_to_check));
  320. tor_addr = smartlist_get(results, 1);
  321. addr_len =
  322. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  323. sizeof(struct sockaddr_in));
  324. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  325. tt_assert(sockaddr_in_are_equal(sockaddr_eth1,sockaddr_to_check));
  326. done:
  327. tor_free(sockaddr_to_check);
  328. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  329. smartlist_free(results);
  330. tor_free(ifc);
  331. tor_free(ifr);
  332. return;
  333. }
  334. static void
  335. test_address_get_if_addrs_ioctl(void *arg)
  336. {
  337. smartlist_t *result = NULL;
  338. (void)arg;
  339. result = get_interface_addresses_ioctl(LOG_ERR);
  340. tt_assert(result);
  341. tt_int_op(smartlist_len(result),>=,1);
  342. tt_assert(smartlist_contains_localhost_tor_addr(result));
  343. done:
  344. if (result) {
  345. SMARTLIST_FOREACH(result, tor_addr_t *, t, tor_free(t));
  346. smartlist_free(result);
  347. }
  348. return;
  349. }
  350. #endif
  351. #define FAKE_SOCKET_FD (42)
  352. static tor_socket_t
  353. fake_open_socket(int domain, int type, int protocol)
  354. {
  355. (void)domain;
  356. (void)type;
  357. (void)protocol;
  358. return FAKE_SOCKET_FD;
  359. }
  360. static int last_connected_socket_fd = 0;
  361. static int connect_retval = 0;
  362. static tor_socket_t
  363. pretend_to_connect(tor_socket_t socket, const struct sockaddr *address,
  364. socklen_t address_len)
  365. {
  366. (void)address;
  367. (void)address_len;
  368. last_connected_socket_fd = socket;
  369. return connect_retval;
  370. }
  371. static struct sockaddr *mock_addr = NULL;
  372. static int
  373. fake_getsockname(tor_socket_t socket, struct sockaddr *address,
  374. socklen_t *address_len)
  375. {
  376. socklen_t bytes_to_copy = 0;
  377. (void) socket;
  378. if (!mock_addr)
  379. return -1;
  380. if (mock_addr->sa_family == AF_INET) {
  381. bytes_to_copy = sizeof(struct sockaddr_in);
  382. } else if (mock_addr->sa_family == AF_INET6) {
  383. bytes_to_copy = sizeof(struct sockaddr_in6);
  384. } else {
  385. return -1;
  386. }
  387. if (*address_len < bytes_to_copy) {
  388. return -1;
  389. }
  390. memcpy(address,mock_addr,bytes_to_copy);
  391. *address_len = bytes_to_copy;
  392. return 0;
  393. }
  394. static void
  395. test_address_udp_socket_trick_whitebox(void *arg)
  396. {
  397. int hack_retval;
  398. tor_addr_t *addr_from_hack = tor_malloc_zero(sizeof(tor_addr_t));
  399. struct sockaddr_in6 *mock_addr6;
  400. struct sockaddr_in6 *ipv6_to_check =
  401. tor_malloc_zero(sizeof(struct sockaddr_in6));
  402. (void)arg;
  403. MOCK(tor_open_socket,fake_open_socket);
  404. MOCK(tor_connect_socket,pretend_to_connect);
  405. MOCK(tor_getsockname,fake_getsockname);
  406. mock_addr = tor_malloc_zero(sizeof(struct sockaddr_storage));
  407. sockaddr_in_from_string("23.32.246.118",(struct sockaddr_in *)mock_addr);
  408. hack_retval =
  409. get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  410. AF_INET, addr_from_hack);
  411. tt_int_op(hack_retval,==,0);
  412. tt_assert(tor_addr_eq_ipv4h(addr_from_hack, 0x1720f676));
  413. /* Now, lets do an IPv6 case. */
  414. memset(mock_addr,0,sizeof(struct sockaddr_storage));
  415. mock_addr6 = (struct sockaddr_in6 *)mock_addr;
  416. mock_addr6->sin6_family = AF_INET6;
  417. mock_addr6->sin6_port = 0;
  418. tor_inet_pton(AF_INET6,"2001:cdba::3257:9652",&(mock_addr6->sin6_addr));
  419. hack_retval =
  420. get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  421. AF_INET6, addr_from_hack);
  422. tt_int_op(hack_retval,==,0);
  423. tor_addr_to_sockaddr(addr_from_hack,0,(struct sockaddr *)ipv6_to_check,
  424. sizeof(struct sockaddr_in6));
  425. tt_assert(sockaddr_in6_are_equal(mock_addr6,ipv6_to_check));
  426. UNMOCK(tor_open_socket);
  427. UNMOCK(tor_connect_socket);
  428. UNMOCK(tor_getsockname);
  429. done:
  430. tor_free(ipv6_to_check);
  431. tor_free(mock_addr);
  432. tor_free(addr_from_hack);
  433. return;
  434. }
  435. static void
  436. test_address_udp_socket_trick_blackbox(void *arg)
  437. {
  438. /* We want get_interface_address6_via_udp_socket_hack() to yield
  439. * the same valid address that get_interface_address6() returns.
  440. * If the latter is unable to find a valid address, we want
  441. * _hack() to fail and return-1.
  442. *
  443. * Furthermore, we want _hack() never to crash, even if
  444. * get_interface_addresses_raw() is returning NULL.
  445. */
  446. tor_addr_t addr4;
  447. tor_addr_t addr4_to_check;
  448. tor_addr_t addr6;
  449. tor_addr_t addr6_to_check;
  450. int retval, retval_reference;
  451. (void)arg;
  452. #if 0
  453. retval_reference = get_interface_address6(LOG_DEBUG,AF_INET,&addr4);
  454. retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  455. AF_INET,
  456. &addr4_to_check);
  457. tt_int_op(retval,==,retval_reference);
  458. tt_assert( (retval == -1 && retval_reference == -1) ||
  459. (tor_addr_compare(&addr4,&addr4_to_check,CMP_EXACT) == 0) );
  460. retval_reference = get_interface_address6(LOG_DEBUG,AF_INET6,&addr6);
  461. retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  462. AF_INET6,
  463. &addr6_to_check);
  464. tt_int_op(retval,==,retval_reference);
  465. tt_assert( (retval == -1 && retval_reference == -1) ||
  466. (tor_addr_compare(&addr6,&addr6_to_check,CMP_EXACT) == 0) );
  467. #else
  468. /* Both of the blackbox test cases fail horribly if:
  469. * * The host has no external addreses.
  470. * * There are multiple interfaces with either AF_INET or AF_INET6.
  471. * * The last address isn't the one associated with the default route.
  472. *
  473. * The tests SHOULD be re-enabled when #12377 is fixed correctly, but till
  474. * then this fails a lot, in situations we expect failures due to knowing
  475. * about the code being broken.
  476. */
  477. (void)addr4_to_check;
  478. (void)addr6_to_check;
  479. (void)addr6;
  480. (void) retval_reference;
  481. #endif
  482. /* When family is neither AF_INET nor AF_INET6, we want _hack to
  483. * fail and return -1.
  484. */
  485. retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  486. AF_INET+AF_INET6,&addr4);
  487. tt_assert(retval == -1);
  488. done:
  489. return;
  490. }
  491. #define ADDRESS_TEST(name, flags) \
  492. { #name, test_address_ ## name, flags, NULL, NULL }
  493. struct testcase_t address_tests[] = {
  494. ADDRESS_TEST(udp_socket_trick_whitebox, TT_FORK),
  495. ADDRESS_TEST(udp_socket_trick_blackbox, TT_FORK),
  496. #ifdef HAVE_IFADDRS_TO_SMARTLIST
  497. ADDRESS_TEST(get_if_addrs_ifaddrs, TT_FORK),
  498. ADDRESS_TEST(ifaddrs_to_smartlist, 0),
  499. #endif
  500. #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
  501. ADDRESS_TEST(get_if_addrs_win32, TT_FORK),
  502. ADDRESS_TEST(ip_adapter_addresses_to_smartlist, 0),
  503. #endif
  504. #ifdef HAVE_IFCONF_TO_SMARTLIST
  505. ADDRESS_TEST(get_if_addrs_ioctl, TT_FORK),
  506. ADDRESS_TEST(ifreq_to_smartlist, 0),
  507. #endif
  508. END_OF_TESTCASES
  509. };