test_address.c 30 KB

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