test_address.c 32 KB

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