test_address.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /* Copyright (c) 2014-2016, 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 is an IPv4 or IPv6 localhost address. Otherwise, return 0.
  66. */
  67. static int
  68. smartlist_contains_localhost_tor_addr(smartlist_t *smartlist)
  69. {
  70. SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
  71. if (tor_addr_is_loopback(tor_addr)) {
  72. return 1;
  73. }
  74. } SMARTLIST_FOREACH_END(tor_addr);
  75. return 0;
  76. }
  77. /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
  78. * that is an IPv4 or IPv6 multicast address. Otherwise, return 0.
  79. */
  80. static int
  81. smartlist_contains_multicast_tor_addr(smartlist_t *smartlist)
  82. {
  83. SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
  84. if (tor_addr_is_multicast(tor_addr)) {
  85. return 1;
  86. }
  87. } SMARTLIST_FOREACH_END(tor_addr);
  88. return 0;
  89. }
  90. /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
  91. * that is an IPv4 or IPv6 internal address. Otherwise, return 0.
  92. */
  93. static int
  94. smartlist_contains_internal_tor_addr(smartlist_t *smartlist)
  95. {
  96. SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
  97. if (tor_addr_is_internal(tor_addr, 0)) {
  98. return 1;
  99. }
  100. } SMARTLIST_FOREACH_END(tor_addr);
  101. return 0;
  102. }
  103. /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
  104. * that is NULL or the null tor_addr_t. Otherwise, return 0.
  105. */
  106. static int
  107. smartlist_contains_null_tor_addr(smartlist_t *smartlist)
  108. {
  109. SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
  110. if (tor_addr == NULL || tor_addr_is_null(tor_addr)) {
  111. return 1;
  112. }
  113. } SMARTLIST_FOREACH_END(tor_addr);
  114. return 0;
  115. }
  116. /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
  117. * that is an IPv4 address. Otherwise, return 0.
  118. */
  119. static int
  120. smartlist_contains_ipv4_tor_addr(smartlist_t *smartlist)
  121. {
  122. SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
  123. if (tor_addr_is_v4(tor_addr)) {
  124. return 1;
  125. }
  126. } SMARTLIST_FOREACH_END(tor_addr);
  127. return 0;
  128. }
  129. /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
  130. * that is an IPv6 address. Otherwise, return 0.
  131. */
  132. static int
  133. smartlist_contains_ipv6_tor_addr(smartlist_t *smartlist)
  134. {
  135. SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
  136. /* Since there's no tor_addr_is_v6, assume all non-v4s are v6 */
  137. if (!tor_addr_is_v4(tor_addr)) {
  138. return 1;
  139. }
  140. } SMARTLIST_FOREACH_END(tor_addr);
  141. return 0;
  142. }
  143. #ifdef HAVE_IFADDRS_TO_SMARTLIST
  144. static void
  145. test_address_ifaddrs_to_smartlist(void *arg)
  146. {
  147. struct ifaddrs *ifa = NULL;
  148. struct ifaddrs *ifa_ipv4 = NULL;
  149. struct ifaddrs *ifa_ipv6 = NULL;
  150. struct sockaddr_in *ipv4_sockaddr_local = NULL;
  151. struct sockaddr_in *netmask_slash8 = NULL;
  152. struct sockaddr_in *ipv4_sockaddr_remote = NULL;
  153. struct sockaddr_in6 *ipv6_sockaddr = NULL;
  154. smartlist_t *smartlist = NULL;
  155. tor_addr_t *tor_addr = NULL;
  156. struct sockaddr *sockaddr_to_check = NULL;
  157. socklen_t addr_len;
  158. (void)arg;
  159. netmask_slash8 = sockaddr_in_from_string("255.0.0.0",NULL);
  160. ipv4_sockaddr_local = sockaddr_in_from_string("127.0.0.1",NULL);
  161. ipv4_sockaddr_remote = sockaddr_in_from_string("128.52.160.20",NULL);
  162. ipv6_sockaddr = tor_malloc(sizeof(struct sockaddr_in6));
  163. ipv6_sockaddr->sin6_family = AF_INET6;
  164. ipv6_sockaddr->sin6_port = 0;
  165. tor_inet_pton(AF_INET6, "2001:db8:8714:3a90::12",
  166. &(ipv6_sockaddr->sin6_addr));
  167. ifa = tor_malloc(sizeof(struct ifaddrs));
  168. ifa_ipv4 = tor_malloc(sizeof(struct ifaddrs));
  169. ifa_ipv6 = tor_malloc(sizeof(struct ifaddrs));
  170. ifa->ifa_next = ifa_ipv4;
  171. ifa->ifa_name = tor_strdup("eth0");
  172. ifa->ifa_flags = IFF_UP | IFF_RUNNING;
  173. ifa->ifa_addr = (struct sockaddr *)ipv4_sockaddr_local;
  174. ifa->ifa_netmask = (struct sockaddr *)netmask_slash8;
  175. ifa->ifa_dstaddr = NULL;
  176. ifa->ifa_data = NULL;
  177. ifa_ipv4->ifa_next = ifa_ipv6;
  178. ifa_ipv4->ifa_name = tor_strdup("eth1");
  179. ifa_ipv4->ifa_flags = IFF_UP | IFF_RUNNING;
  180. ifa_ipv4->ifa_addr = (struct sockaddr *)ipv4_sockaddr_remote;
  181. ifa_ipv4->ifa_netmask = (struct sockaddr *)netmask_slash8;
  182. ifa_ipv4->ifa_dstaddr = NULL;
  183. ifa_ipv4->ifa_data = NULL;
  184. ifa_ipv6->ifa_next = NULL;
  185. ifa_ipv6->ifa_name = tor_strdup("eth2");
  186. ifa_ipv6->ifa_flags = IFF_UP | IFF_RUNNING;
  187. ifa_ipv6->ifa_addr = (struct sockaddr *)ipv6_sockaddr;
  188. ifa_ipv6->ifa_netmask = NULL;
  189. ifa_ipv6->ifa_dstaddr = NULL;
  190. ifa_ipv6->ifa_data = NULL;
  191. smartlist = ifaddrs_to_smartlist(ifa, AF_UNSPEC);
  192. tt_assert(smartlist);
  193. tt_assert(smartlist_len(smartlist) == 3);
  194. sockaddr_to_check = tor_malloc(sizeof(struct sockaddr_in6));
  195. tor_addr = smartlist_get(smartlist,0);
  196. addr_len =
  197. tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
  198. sizeof(struct sockaddr_in));
  199. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  200. tt_assert(sockaddr_in_are_equal((struct sockaddr_in *)sockaddr_to_check,
  201. ipv4_sockaddr_local));
  202. tor_addr = smartlist_get(smartlist,1);
  203. addr_len =
  204. tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
  205. sizeof(struct sockaddr_in));
  206. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  207. tt_assert(sockaddr_in_are_equal((struct sockaddr_in *)sockaddr_to_check,
  208. ipv4_sockaddr_remote));
  209. tor_addr = smartlist_get(smartlist,2);
  210. addr_len =
  211. tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
  212. sizeof(struct sockaddr_in6));
  213. tt_int_op(addr_len,==,sizeof(struct sockaddr_in6));
  214. tt_assert(sockaddr_in6_are_equal((struct sockaddr_in6*)sockaddr_to_check,
  215. ipv6_sockaddr));
  216. done:
  217. tor_free(netmask_slash8);
  218. tor_free(ipv4_sockaddr_local);
  219. tor_free(ipv4_sockaddr_remote);
  220. tor_free(ipv6_sockaddr);
  221. tor_free(ifa->ifa_name);
  222. tor_free(ifa_ipv4->ifa_name);
  223. tor_free(ifa_ipv6->ifa_name);
  224. tor_free(ifa);
  225. tor_free(ifa_ipv4);
  226. tor_free(ifa_ipv6);
  227. tor_free(sockaddr_to_check);
  228. if (smartlist) {
  229. SMARTLIST_FOREACH(smartlist, tor_addr_t *, t, tor_free(t));
  230. smartlist_free(smartlist);
  231. }
  232. return;
  233. }
  234. static void
  235. test_address_get_if_addrs_ifaddrs(void *arg)
  236. {
  237. smartlist_t *results = NULL;
  238. (void)arg;
  239. results = get_interface_addresses_ifaddrs(LOG_ERR, AF_UNSPEC);
  240. tt_assert(results);
  241. /* Some FreeBSD jails don't have localhost IP address. Instead, they only
  242. * have the address assigned to the jail (whatever that may be).
  243. * And a jail without a network connection might not have any addresses at
  244. * all. */
  245. tt_assert(!smartlist_contains_null_tor_addr(results));
  246. /* If there are addresses, they must be IPv4 or IPv6 */
  247. if (smartlist_len(results) > 0) {
  248. tt_assert(smartlist_contains_ipv4_tor_addr(results)
  249. || smartlist_contains_ipv6_tor_addr(results));
  250. }
  251. done:
  252. if (results) {
  253. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  254. }
  255. smartlist_free(results);
  256. return;
  257. }
  258. #endif
  259. #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
  260. static void
  261. test_address_get_if_addrs_win32(void *arg)
  262. {
  263. smartlist_t *results = NULL;
  264. (void)arg;
  265. results = get_interface_addresses_win32(LOG_ERR, AF_UNSPEC);
  266. tt_int_op(smartlist_len(results),>=,1);
  267. tt_assert(smartlist_contains_localhost_tor_addr(results));
  268. tt_assert(!smartlist_contains_null_tor_addr(results));
  269. /* If there are addresses, they must be IPv4 or IPv6 */
  270. if (smartlist_len(results) > 0) {
  271. tt_assert(smartlist_contains_ipv4_tor_addr(results)
  272. || smartlist_contains_ipv6_tor_addr(results));
  273. }
  274. done:
  275. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  276. tor_free(results);
  277. return;
  278. }
  279. static void
  280. test_address_ip_adapter_addresses_to_smartlist(void *arg)
  281. {
  282. IP_ADAPTER_ADDRESSES *addrs1;
  283. IP_ADAPTER_ADDRESSES *addrs2;
  284. IP_ADAPTER_UNICAST_ADDRESS *unicast11;
  285. IP_ADAPTER_UNICAST_ADDRESS *unicast12;
  286. IP_ADAPTER_UNICAST_ADDRESS *unicast21;
  287. smartlist_t *result = NULL;
  288. struct sockaddr_in *sockaddr_test1;
  289. struct sockaddr_in *sockaddr_test2;
  290. struct sockaddr_in *sockaddr_localhost;
  291. struct sockaddr_in *sockaddr_to_check;
  292. tor_addr_t *tor_addr;
  293. (void)arg;
  294. (void)sockaddr_in6_are_equal;
  295. sockaddr_to_check = tor_malloc_zero(sizeof(struct sockaddr_in));
  296. addrs1 =
  297. tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES));
  298. addrs1->FirstUnicastAddress =
  299. unicast11 = tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
  300. sockaddr_test1 = sockaddr_in_from_string("86.59.30.40",NULL);
  301. unicast11->Address.lpSockaddr = (LPSOCKADDR)sockaddr_test1;
  302. unicast11->Next = unicast12 =
  303. tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
  304. sockaddr_test2 = sockaddr_in_from_string("93.95.227.222", NULL);
  305. unicast12->Address.lpSockaddr = (LPSOCKADDR)sockaddr_test2;
  306. addrs1->Next = addrs2 =
  307. tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES));
  308. addrs2->FirstUnicastAddress =
  309. unicast21 = tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
  310. sockaddr_localhost = sockaddr_in_from_string("127.0.0.1", NULL);
  311. unicast21->Address.lpSockaddr = (LPSOCKADDR)sockaddr_localhost;
  312. result = ip_adapter_addresses_to_smartlist(addrs1);
  313. tt_assert(result);
  314. tt_assert(smartlist_len(result) == 3);
  315. tor_addr = smartlist_get(result,0);
  316. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  317. sizeof(struct sockaddr_in));
  318. tt_assert(sockaddr_in_are_equal(sockaddr_test1,sockaddr_to_check));
  319. tor_addr = smartlist_get(result,1);
  320. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  321. sizeof(struct sockaddr_in));
  322. tt_assert(sockaddr_in_are_equal(sockaddr_test2,sockaddr_to_check));
  323. tor_addr = smartlist_get(result,2);
  324. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  325. sizeof(struct sockaddr_in));
  326. tt_assert(sockaddr_in_are_equal(sockaddr_localhost,sockaddr_to_check));
  327. done:
  328. SMARTLIST_FOREACH(result, tor_addr_t *, t, tor_free(t));
  329. smartlist_free(result);
  330. tor_free(addrs1);
  331. tor_free(addrs2);
  332. tor_free(unicast11->Address.lpSockaddr);
  333. tor_free(unicast11);
  334. tor_free(unicast12->Address.lpSockaddr);
  335. tor_free(unicast12);
  336. tor_free(unicast21->Address.lpSockaddr);
  337. tor_free(unicast21);
  338. tor_free(sockaddr_to_check);
  339. return;
  340. }
  341. #endif
  342. #ifdef HAVE_IFCONF_TO_SMARTLIST
  343. static void
  344. test_address_ifreq_to_smartlist(void *arg)
  345. {
  346. smartlist_t *results = NULL;
  347. const tor_addr_t *tor_addr = NULL;
  348. struct sockaddr_in *sockaddr = NULL;
  349. struct sockaddr_in *sockaddr_eth1 = NULL;
  350. struct sockaddr_in *sockaddr_to_check = NULL;
  351. struct ifconf *ifc;
  352. struct ifreq *ifr;
  353. struct ifreq *ifr_next;
  354. socklen_t addr_len;
  355. (void)arg;
  356. sockaddr_to_check = tor_malloc(sizeof(struct sockaddr_in));
  357. ifr = tor_malloc(sizeof(struct ifreq));
  358. memset(ifr,0,sizeof(struct ifreq));
  359. strlcpy(ifr->ifr_name,"lo",3);
  360. sockaddr = (struct sockaddr_in *) &(ifr->ifr_ifru.ifru_addr);
  361. sockaddr_in_from_string("127.0.0.1",sockaddr);
  362. ifc = tor_malloc(sizeof(struct ifconf));
  363. memset(ifc,0,sizeof(struct ifconf));
  364. ifc->ifc_len = sizeof(struct ifreq);
  365. ifc->ifc_ifcu.ifcu_req = ifr;
  366. results = ifreq_to_smartlist(ifc->ifc_buf,ifc->ifc_len);
  367. tt_int_op(smartlist_len(results),==,1);
  368. tor_addr = smartlist_get(results, 0);
  369. addr_len =
  370. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  371. sizeof(struct sockaddr_in));
  372. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  373. tt_assert(sockaddr_in_are_equal(sockaddr,sockaddr_to_check));
  374. ifr = tor_realloc(ifr,2*sizeof(struct ifreq));
  375. ifr_next = ifr+1;
  376. strlcpy(ifr_next->ifr_name,"eth1",5);
  377. ifc->ifc_len = 2*sizeof(struct ifreq);
  378. ifc->ifc_ifcu.ifcu_req = ifr;
  379. sockaddr = (struct sockaddr_in *) &(ifr->ifr_ifru.ifru_addr);
  380. sockaddr_eth1 = (struct sockaddr_in *) &(ifr_next->ifr_ifru.ifru_addr);
  381. sockaddr_in_from_string("192.168.10.55",sockaddr_eth1);
  382. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  383. smartlist_free(results);
  384. results = ifreq_to_smartlist(ifc->ifc_buf,ifc->ifc_len);
  385. tt_int_op(smartlist_len(results),==,2);
  386. tor_addr = smartlist_get(results, 0);
  387. addr_len =
  388. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  389. sizeof(struct sockaddr_in));
  390. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  391. tt_assert(sockaddr_in_are_equal(sockaddr,sockaddr_to_check));
  392. tor_addr = smartlist_get(results, 1);
  393. addr_len =
  394. tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
  395. sizeof(struct sockaddr_in));
  396. tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
  397. tt_assert(sockaddr_in_are_equal(sockaddr_eth1,sockaddr_to_check));
  398. done:
  399. tor_free(sockaddr_to_check);
  400. SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
  401. smartlist_free(results);
  402. tor_free(ifc);
  403. tor_free(ifr);
  404. return;
  405. }
  406. static void
  407. test_address_get_if_addrs_ioctl(void *arg)
  408. {
  409. smartlist_t *result = NULL;
  410. (void)arg;
  411. result = get_interface_addresses_ioctl(LOG_ERR, AF_INET);
  412. /* On an IPv6-only system, this will fail and return NULL
  413. tt_assert(result);
  414. */
  415. /* Some FreeBSD jails don't have localhost IP address. Instead, they only
  416. * have the address assigned to the jail (whatever that may be).
  417. * And a jail without a network connection might not have any addresses at
  418. * all. */
  419. if (result) {
  420. tt_assert(!smartlist_contains_null_tor_addr(result));
  421. /* If there are addresses, they must be IPv4 or IPv6.
  422. * (AIX supports IPv6 from SIOCGIFCONF.) */
  423. if (smartlist_len(result) > 0) {
  424. tt_assert(smartlist_contains_ipv4_tor_addr(result)
  425. || smartlist_contains_ipv6_tor_addr(result));
  426. }
  427. }
  428. done:
  429. if (result) {
  430. SMARTLIST_FOREACH(result, tor_addr_t *, t, tor_free(t));
  431. smartlist_free(result);
  432. }
  433. return;
  434. }
  435. #endif
  436. #define FAKE_SOCKET_FD (42)
  437. static tor_socket_t
  438. fake_open_socket(int domain, int type, int protocol)
  439. {
  440. (void)domain;
  441. (void)type;
  442. (void)protocol;
  443. return FAKE_SOCKET_FD;
  444. }
  445. static int last_connected_socket_fd = 0;
  446. static int connect_retval = 0;
  447. static tor_socket_t
  448. pretend_to_connect(tor_socket_t socket, const struct sockaddr *address,
  449. socklen_t address_len)
  450. {
  451. (void)address;
  452. (void)address_len;
  453. last_connected_socket_fd = socket;
  454. return connect_retval;
  455. }
  456. static struct sockaddr *mock_addr = NULL;
  457. static int
  458. fake_getsockname(tor_socket_t socket, struct sockaddr *address,
  459. socklen_t *address_len)
  460. {
  461. socklen_t bytes_to_copy = 0;
  462. (void) socket;
  463. if (!mock_addr)
  464. return -1;
  465. if (mock_addr->sa_family == AF_INET) {
  466. bytes_to_copy = sizeof(struct sockaddr_in);
  467. } else if (mock_addr->sa_family == AF_INET6) {
  468. bytes_to_copy = sizeof(struct sockaddr_in6);
  469. } else {
  470. return -1;
  471. }
  472. if (*address_len < bytes_to_copy) {
  473. return -1;
  474. }
  475. memcpy(address,mock_addr,bytes_to_copy);
  476. *address_len = bytes_to_copy;
  477. return 0;
  478. }
  479. static void
  480. test_address_udp_socket_trick_whitebox(void *arg)
  481. {
  482. int hack_retval;
  483. tor_addr_t *addr_from_hack = tor_malloc_zero(sizeof(tor_addr_t));
  484. struct sockaddr_in6 *mock_addr6;
  485. struct sockaddr_in6 *ipv6_to_check =
  486. tor_malloc_zero(sizeof(struct sockaddr_in6));
  487. (void)arg;
  488. MOCK(tor_open_socket,fake_open_socket);
  489. MOCK(tor_connect_socket,pretend_to_connect);
  490. MOCK(tor_getsockname,fake_getsockname);
  491. mock_addr = tor_malloc_zero(sizeof(struct sockaddr_storage));
  492. sockaddr_in_from_string("23.32.246.118",(struct sockaddr_in *)mock_addr);
  493. hack_retval =
  494. get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  495. AF_INET, addr_from_hack);
  496. tt_int_op(hack_retval,==,0);
  497. tt_assert(tor_addr_eq_ipv4h(addr_from_hack, 0x1720f676));
  498. /* Now, lets do an IPv6 case. */
  499. memset(mock_addr,0,sizeof(struct sockaddr_storage));
  500. mock_addr6 = (struct sockaddr_in6 *)mock_addr;
  501. mock_addr6->sin6_family = AF_INET6;
  502. mock_addr6->sin6_port = 0;
  503. tor_inet_pton(AF_INET6,"2001:cdba::3257:9652",&(mock_addr6->sin6_addr));
  504. hack_retval =
  505. get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  506. AF_INET6, addr_from_hack);
  507. tt_int_op(hack_retval,==,0);
  508. tor_addr_to_sockaddr(addr_from_hack,0,(struct sockaddr *)ipv6_to_check,
  509. sizeof(struct sockaddr_in6));
  510. tt_assert(sockaddr_in6_are_equal(mock_addr6,ipv6_to_check));
  511. UNMOCK(tor_open_socket);
  512. UNMOCK(tor_connect_socket);
  513. UNMOCK(tor_getsockname);
  514. done:
  515. tor_free(ipv6_to_check);
  516. tor_free(mock_addr);
  517. tor_free(addr_from_hack);
  518. return;
  519. }
  520. static void
  521. test_address_udp_socket_trick_blackbox(void *arg)
  522. {
  523. /* We want get_interface_address6_via_udp_socket_hack() to yield
  524. * the same valid address that get_interface_address6() returns.
  525. * If the latter is unable to find a valid address, we want
  526. * _hack() to fail and return-1.
  527. *
  528. * Furthermore, we want _hack() never to crash, even if
  529. * get_interface_addresses_raw() is returning NULL.
  530. */
  531. tor_addr_t addr4;
  532. tor_addr_t addr4_to_check;
  533. tor_addr_t addr6;
  534. tor_addr_t addr6_to_check;
  535. int retval, retval_reference;
  536. (void)arg;
  537. #if 0
  538. retval_reference = get_interface_address6(LOG_DEBUG,AF_INET,&addr4);
  539. retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  540. AF_INET,
  541. &addr4_to_check);
  542. tt_int_op(retval,==,retval_reference);
  543. tt_assert( (retval == -1 && retval_reference == -1) ||
  544. (tor_addr_compare(&addr4,&addr4_to_check,CMP_EXACT) == 0) );
  545. retval_reference = get_interface_address6(LOG_DEBUG,AF_INET6,&addr6);
  546. retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  547. AF_INET6,
  548. &addr6_to_check);
  549. tt_int_op(retval,==,retval_reference);
  550. tt_assert( (retval == -1 && retval_reference == -1) ||
  551. (tor_addr_compare(&addr6,&addr6_to_check,CMP_EXACT) == 0) );
  552. #else
  553. /* Both of the blackbox test cases fail horribly if:
  554. * * The host has no external addreses.
  555. * * There are multiple interfaces with either AF_INET or AF_INET6.
  556. * * The last address isn't the one associated with the default route.
  557. *
  558. * The tests SHOULD be re-enabled when #12377 is fixed correctly, but till
  559. * then this fails a lot, in situations we expect failures due to knowing
  560. * about the code being broken.
  561. */
  562. (void)addr4_to_check;
  563. (void)addr6_to_check;
  564. (void)addr6;
  565. (void) retval_reference;
  566. #endif
  567. /* When family is neither AF_INET nor AF_INET6, we want _hack to
  568. * fail and return -1.
  569. */
  570. retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
  571. AF_INET+AF_INET6,&addr4);
  572. tt_assert(retval == -1);
  573. done:
  574. return;
  575. }
  576. static void
  577. test_address_get_if_addrs_list_internal(void *arg)
  578. {
  579. smartlist_t *results = NULL;
  580. (void)arg;
  581. results = get_interface_address_list(LOG_ERR, 1);
  582. tt_assert(results != NULL);
  583. /* When the network is down, a system might not have any non-local
  584. * non-multicast addresseses, not even internal ones.
  585. * Unit tests shouldn't fail because of this. */
  586. tt_int_op(smartlist_len(results),>=,0);
  587. tt_assert(!smartlist_contains_localhost_tor_addr(results));
  588. tt_assert(!smartlist_contains_multicast_tor_addr(results));
  589. /* The list may or may not contain internal addresses */
  590. tt_assert(!smartlist_contains_null_tor_addr(results));
  591. /* if there are any addresses, they must be IPv4 */
  592. if (smartlist_len(results) > 0) {
  593. tt_assert(smartlist_contains_ipv4_tor_addr(results));
  594. }
  595. tt_assert(!smartlist_contains_ipv6_tor_addr(results));
  596. done:
  597. free_interface_address_list(results);
  598. return;
  599. }
  600. static void
  601. test_address_get_if_addrs_list_no_internal(void *arg)
  602. {
  603. smartlist_t *results = NULL;
  604. (void)arg;
  605. results = get_interface_address_list(LOG_ERR, 0);
  606. tt_assert(results != NULL);
  607. /* Work even on systems with only internal IPv4 addresses */
  608. tt_int_op(smartlist_len(results),>=,0);
  609. tt_assert(!smartlist_contains_localhost_tor_addr(results));
  610. tt_assert(!smartlist_contains_multicast_tor_addr(results));
  611. tt_assert(!smartlist_contains_internal_tor_addr(results));
  612. tt_assert(!smartlist_contains_null_tor_addr(results));
  613. /* if there are any addresses, they must be IPv4 */
  614. if (smartlist_len(results) > 0) {
  615. tt_assert(smartlist_contains_ipv4_tor_addr(results));
  616. }
  617. tt_assert(!smartlist_contains_ipv6_tor_addr(results));
  618. done:
  619. free_interface_address_list(results);
  620. return;
  621. }
  622. static void
  623. test_address_get_if_addrs6_list_internal(void *arg)
  624. {
  625. smartlist_t *results = NULL;
  626. (void)arg;
  627. results = get_interface_address6_list(LOG_ERR, AF_INET6, 1);
  628. tt_assert(results != NULL);
  629. /* Work even on systems without IPv6 interfaces */
  630. tt_int_op(smartlist_len(results),>=,0);
  631. tt_assert(!smartlist_contains_localhost_tor_addr(results));
  632. tt_assert(!smartlist_contains_multicast_tor_addr(results));
  633. /* The list may or may not contain internal addresses */
  634. tt_assert(!smartlist_contains_null_tor_addr(results));
  635. /* if there are any addresses, they must be IPv6 */
  636. tt_assert(!smartlist_contains_ipv4_tor_addr(results));
  637. if (smartlist_len(results) > 0) {
  638. tt_assert(smartlist_contains_ipv6_tor_addr(results));
  639. }
  640. done:
  641. free_interface_address6_list(results);
  642. return;
  643. }
  644. static void
  645. test_address_get_if_addrs6_list_no_internal(void *arg)
  646. {
  647. smartlist_t *results = NULL;
  648. (void)arg;
  649. results = get_interface_address6_list(LOG_ERR, AF_INET6, 0);
  650. tt_assert(results != NULL);
  651. /* Work even on systems without IPv6 interfaces */
  652. tt_int_op(smartlist_len(results),>=,0);
  653. tt_assert(!smartlist_contains_localhost_tor_addr(results));
  654. tt_assert(!smartlist_contains_multicast_tor_addr(results));
  655. tt_assert(!smartlist_contains_internal_tor_addr(results));
  656. tt_assert(!smartlist_contains_null_tor_addr(results));
  657. /* if there are any addresses, they must be IPv6 */
  658. tt_assert(!smartlist_contains_ipv4_tor_addr(results));
  659. if (smartlist_len(results) > 0) {
  660. tt_assert(smartlist_contains_ipv6_tor_addr(results));
  661. }
  662. done:
  663. free_interface_address6_list(results);
  664. return;
  665. }
  666. static int called_get_interface_addresses_raw = 0;
  667. static smartlist_t *
  668. mock_get_interface_addresses_raw_fail(int severity, sa_family_t family)
  669. {
  670. (void)severity;
  671. (void)family;
  672. called_get_interface_addresses_raw++;
  673. return smartlist_new();
  674. }
  675. static int called_get_interface_address6_via_udp_socket_hack = 0;
  676. static int
  677. mock_get_interface_address6_via_udp_socket_hack_fail(int severity,
  678. sa_family_t family,
  679. tor_addr_t *addr)
  680. {
  681. (void)severity;
  682. (void)family;
  683. (void)addr;
  684. called_get_interface_address6_via_udp_socket_hack++;
  685. return -1;
  686. }
  687. static void
  688. test_address_get_if_addrs_internal_fail(void *arg)
  689. {
  690. smartlist_t *results1 = NULL, *results2 = NULL;
  691. int rv = 0;
  692. uint32_t ipv4h_addr = 0;
  693. tor_addr_t ipv6_addr;
  694. memset(&ipv6_addr, 0, sizeof(tor_addr_t));
  695. (void)arg;
  696. MOCK(get_interface_addresses_raw,
  697. mock_get_interface_addresses_raw_fail);
  698. MOCK(get_interface_address6_via_udp_socket_hack,
  699. mock_get_interface_address6_via_udp_socket_hack_fail);
  700. results1 = get_interface_address6_list(LOG_ERR, AF_INET6, 1);
  701. tt_assert(results1 != NULL);
  702. tt_int_op(smartlist_len(results1),==,0);
  703. results2 = get_interface_address_list(LOG_ERR, 1);
  704. tt_assert(results2 != NULL);
  705. tt_int_op(smartlist_len(results2),==,0);
  706. rv = get_interface_address6(LOG_ERR, AF_INET6, &ipv6_addr);
  707. tt_assert(rv == -1);
  708. rv = get_interface_address(LOG_ERR, &ipv4h_addr);
  709. tt_assert(rv == -1);
  710. done:
  711. UNMOCK(get_interface_addresses_raw);
  712. UNMOCK(get_interface_address6_via_udp_socket_hack);
  713. free_interface_address6_list(results1);
  714. free_interface_address6_list(results2);
  715. return;
  716. }
  717. static void
  718. test_address_get_if_addrs_no_internal_fail(void *arg)
  719. {
  720. smartlist_t *results1 = NULL, *results2 = NULL;
  721. (void)arg;
  722. MOCK(get_interface_addresses_raw,
  723. mock_get_interface_addresses_raw_fail);
  724. MOCK(get_interface_address6_via_udp_socket_hack,
  725. mock_get_interface_address6_via_udp_socket_hack_fail);
  726. results1 = get_interface_address6_list(LOG_ERR, AF_INET6, 0);
  727. tt_assert(results1 != NULL);
  728. tt_int_op(smartlist_len(results1),==,0);
  729. results2 = get_interface_address_list(LOG_ERR, 0);
  730. tt_assert(results2 != NULL);
  731. tt_int_op(smartlist_len(results2),==,0);
  732. done:
  733. UNMOCK(get_interface_addresses_raw);
  734. UNMOCK(get_interface_address6_via_udp_socket_hack);
  735. free_interface_address6_list(results1);
  736. free_interface_address6_list(results2);
  737. return;
  738. }
  739. static void
  740. test_address_get_if_addrs(void *arg)
  741. {
  742. int rv;
  743. uint32_t addr_h = 0;
  744. tor_addr_t tor_addr;
  745. (void)arg;
  746. rv = get_interface_address(LOG_ERR, &addr_h);
  747. /* When the network is down, a system might not have any non-local
  748. * non-multicast IPv4 addresses, not even internal ones.
  749. * Unit tests shouldn't fail because of this. */
  750. if (rv == 0) {
  751. tor_addr_from_ipv4h(&tor_addr, addr_h);
  752. tt_assert(!tor_addr_is_loopback(&tor_addr));
  753. tt_assert(!tor_addr_is_multicast(&tor_addr));
  754. /* The address may or may not be an internal address */
  755. tt_assert(tor_addr_is_v4(&tor_addr));
  756. }
  757. done:
  758. return;
  759. }
  760. static void
  761. test_address_get_if_addrs6(void *arg)
  762. {
  763. int rv;
  764. tor_addr_t tor_addr;
  765. (void)arg;
  766. rv = get_interface_address6(LOG_ERR, AF_INET6, &tor_addr);
  767. /* Work even on systems without IPv6 interfaces */
  768. if (rv == 0) {
  769. tt_assert(!tor_addr_is_loopback(&tor_addr));
  770. tt_assert(!tor_addr_is_multicast(&tor_addr));
  771. /* The address may or may not be an internal address */
  772. tt_assert(!tor_addr_is_v4(&tor_addr));
  773. }
  774. done:
  775. return;
  776. }
  777. static void
  778. test_address_tor_addr_to_in6(void *ignored)
  779. {
  780. (void)ignored;
  781. tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
  782. const struct in6_addr *res;
  783. uint8_t expected[16] = {42, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  784. 10, 11, 12, 13, 14, 15};
  785. a->family = AF_INET;
  786. res = tor_addr_to_in6(a);
  787. tt_assert(!res);
  788. a->family = AF_INET6;
  789. memcpy(a->addr.in6_addr.s6_addr, expected, 16);
  790. res = tor_addr_to_in6(a);
  791. tt_assert(res);
  792. tt_mem_op(res->s6_addr, OP_EQ, expected, 16);
  793. done:
  794. tor_free(a);
  795. }
  796. static void
  797. test_address_tor_addr_to_in(void *ignored)
  798. {
  799. (void)ignored;
  800. tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
  801. const struct in_addr *res;
  802. a->family = AF_INET6;
  803. res = tor_addr_to_in(a);
  804. tt_assert(!res);
  805. a->family = AF_INET;
  806. a->addr.in_addr.s_addr = 44;
  807. res = tor_addr_to_in(a);
  808. tt_assert(res);
  809. tt_int_op(res->s_addr, OP_EQ, 44);
  810. done:
  811. tor_free(a);
  812. }
  813. static void
  814. test_address_tor_addr_to_ipv4n(void *ignored)
  815. {
  816. (void)ignored;
  817. tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
  818. uint32_t res;
  819. a->family = AF_INET6;
  820. res = tor_addr_to_ipv4n(a);
  821. tt_assert(!res);
  822. a->family = AF_INET;
  823. a->addr.in_addr.s_addr = 43;
  824. res = tor_addr_to_ipv4n(a);
  825. tt_assert(res);
  826. tt_int_op(res, OP_EQ, 43);
  827. done:
  828. tor_free(a);
  829. }
  830. static void
  831. test_address_tor_addr_to_mapped_ipv4h(void *ignored)
  832. {
  833. (void)ignored;
  834. tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
  835. uint32_t res;
  836. uint8_t toset[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 42};
  837. a->family = AF_INET;
  838. res = tor_addr_to_mapped_ipv4h(a);
  839. tt_assert(!res);
  840. a->family = AF_INET6;
  841. memcpy(a->addr.in6_addr.s6_addr, toset, 16);
  842. res = tor_addr_to_mapped_ipv4h(a);
  843. tt_assert(res);
  844. tt_int_op(res, OP_EQ, 42);
  845. done:
  846. tor_free(a);
  847. }
  848. static void
  849. test_address_tor_addr_eq_ipv4h(void *ignored)
  850. {
  851. (void)ignored;
  852. tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
  853. int res;
  854. a->family = AF_INET6;
  855. res = tor_addr_eq_ipv4h(a, 42);
  856. tt_assert(!res);
  857. a->family = AF_INET;
  858. a->addr.in_addr.s_addr = 52;
  859. res = tor_addr_eq_ipv4h(a, 42);
  860. tt_assert(!res);
  861. a->addr.in_addr.s_addr = 52;
  862. res = tor_addr_eq_ipv4h(a, ntohl(52));
  863. tt_assert(res);
  864. done:
  865. tor_free(a);
  866. }
  867. #define ADDRESS_TEST(name, flags) \
  868. { #name, test_address_ ## name, flags, NULL, NULL }
  869. struct testcase_t address_tests[] = {
  870. ADDRESS_TEST(udp_socket_trick_whitebox, TT_FORK),
  871. ADDRESS_TEST(udp_socket_trick_blackbox, TT_FORK),
  872. ADDRESS_TEST(get_if_addrs_list_internal, 0),
  873. ADDRESS_TEST(get_if_addrs_list_no_internal, 0),
  874. ADDRESS_TEST(get_if_addrs6_list_internal, 0),
  875. ADDRESS_TEST(get_if_addrs6_list_no_internal, 0),
  876. ADDRESS_TEST(get_if_addrs_internal_fail, 0),
  877. ADDRESS_TEST(get_if_addrs_no_internal_fail, 0),
  878. ADDRESS_TEST(get_if_addrs, 0),
  879. ADDRESS_TEST(get_if_addrs6, 0),
  880. #ifdef HAVE_IFADDRS_TO_SMARTLIST
  881. ADDRESS_TEST(get_if_addrs_ifaddrs, TT_FORK),
  882. ADDRESS_TEST(ifaddrs_to_smartlist, 0),
  883. #endif
  884. #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
  885. ADDRESS_TEST(get_if_addrs_win32, TT_FORK),
  886. ADDRESS_TEST(ip_adapter_addresses_to_smartlist, 0),
  887. #endif
  888. #ifdef HAVE_IFCONF_TO_SMARTLIST
  889. ADDRESS_TEST(get_if_addrs_ioctl, TT_FORK),
  890. ADDRESS_TEST(ifreq_to_smartlist, 0),
  891. #endif
  892. ADDRESS_TEST(tor_addr_to_in6, 0),
  893. ADDRESS_TEST(tor_addr_to_in, 0),
  894. ADDRESS_TEST(tor_addr_to_ipv4n, 0),
  895. ADDRESS_TEST(tor_addr_to_mapped_ipv4h, 0),
  896. ADDRESS_TEST(tor_addr_eq_ipv4h, 0),
  897. END_OF_TESTCASES
  898. };