address.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file address.c
  7. * \brief Functions to use and manipulate the tor_addr_t structure.
  8. **/
  9. #include "orconfig.h"
  10. #include "compat.h"
  11. #include "util.h"
  12. #include "address.h"
  13. #include "torlog.h"
  14. #include "container.h"
  15. #include "sandbox.h"
  16. #ifdef _WIN32
  17. #include <process.h>
  18. #include <windows.h>
  19. #include <winsock2.h>
  20. /* For access to structs needed by GetAdaptersAddresses */
  21. #undef _WIN32_WINNT
  22. #define _WIN32_WINNT 0x0501
  23. #include <iphlpapi.h>
  24. #endif
  25. #ifdef HAVE_SYS_TIME_H
  26. #include <sys/time.h>
  27. #endif
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #ifdef HAVE_ERRNO_H
  32. #include <errno.h>
  33. #endif
  34. #ifdef HAVE_NETINET_IN_H
  35. #include <netinet/in.h>
  36. #endif
  37. #ifdef HAVE_ARPA_INET_H
  38. #include <arpa/inet.h>
  39. #endif
  40. #ifdef HAVE_SYS_SOCKET_H
  41. #include <sys/socket.h>
  42. #endif
  43. #ifdef HAVE_NETDB_H
  44. #include <netdb.h>
  45. #endif
  46. #ifdef HAVE_SYS_PARAM_H
  47. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  48. #endif
  49. #ifdef HAVE_SYS_UN_H
  50. #include <sys/un.h>
  51. #endif
  52. #ifdef HAVE_IFADDRS_H
  53. #include <ifaddrs.h>
  54. #endif
  55. #ifdef HAVE_SYS_IOCTL_H
  56. #include <sys/ioctl.h>
  57. #endif
  58. #ifdef HAVE_NET_IF_H
  59. #include <net/if.h>
  60. #endif
  61. #include <stdarg.h>
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <assert.h>
  66. /* tor_addr_is_null() and maybe other functions rely on AF_UNSPEC being 0 to
  67. * work correctly. Bail out here if we've found a platform where AF_UNSPEC
  68. * isn't 0. */
  69. #if AF_UNSPEC != 0
  70. #error We rely on AF_UNSPEC being 0. Let us know about your platform, please!
  71. #endif
  72. /** Convert the tor_addr_t in <b>a</b>, with port in <b>port</b>, into a
  73. * sockaddr object in *<b>sa_out</b> of object size <b>len</b>. If not enough
  74. * room is available in sa_out, or on error, return 0. On success, return
  75. * the length of the sockaddr.
  76. *
  77. * Interface note: ordinarily, we return -1 for error. We can't do that here,
  78. * since socklen_t is unsigned on some platforms.
  79. **/
  80. socklen_t
  81. tor_addr_to_sockaddr(const tor_addr_t *a,
  82. uint16_t port,
  83. struct sockaddr *sa_out,
  84. socklen_t len)
  85. {
  86. memset(sa_out, 0, len);
  87. sa_family_t family = tor_addr_family(a);
  88. if (family == AF_INET) {
  89. struct sockaddr_in *sin;
  90. if (len < (int)sizeof(struct sockaddr_in))
  91. return 0;
  92. sin = (struct sockaddr_in *)sa_out;
  93. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  94. sin->sin_len = sizeof(struct sockaddr_in);
  95. #endif
  96. sin->sin_family = AF_INET;
  97. sin->sin_port = htons(port);
  98. sin->sin_addr.s_addr = tor_addr_to_ipv4n(a);
  99. return sizeof(struct sockaddr_in);
  100. } else if (family == AF_INET6) {
  101. struct sockaddr_in6 *sin6;
  102. if (len < (int)sizeof(struct sockaddr_in6))
  103. return 0;
  104. sin6 = (struct sockaddr_in6 *)sa_out;
  105. #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
  106. sin6->sin6_len = sizeof(struct sockaddr_in6);
  107. #endif
  108. sin6->sin6_family = AF_INET6;
  109. sin6->sin6_port = htons(port);
  110. memcpy(&sin6->sin6_addr, tor_addr_to_in6(a), sizeof(struct in6_addr));
  111. return sizeof(struct sockaddr_in6);
  112. } else {
  113. return 0;
  114. }
  115. }
  116. /** Set address <b>a</b> to zero. This address belongs to
  117. * the AF_UNIX family. */
  118. static void
  119. tor_addr_make_af_unix(tor_addr_t *a)
  120. {
  121. memset(a, 0, sizeof(*a));
  122. a->family = AF_UNIX;
  123. }
  124. /** Set the tor_addr_t in <b>a</b> to contain the socket address contained in
  125. * <b>sa</b>. */
  126. int
  127. tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
  128. uint16_t *port_out)
  129. {
  130. tor_assert(a);
  131. tor_assert(sa);
  132. memset(a, 0, sizeof(*a));
  133. if (sa->sa_family == AF_INET) {
  134. struct sockaddr_in *sin = (struct sockaddr_in *) sa;
  135. tor_addr_from_ipv4n(a, sin->sin_addr.s_addr);
  136. if (port_out)
  137. *port_out = ntohs(sin->sin_port);
  138. } else if (sa->sa_family == AF_INET6) {
  139. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
  140. tor_addr_from_in6(a, &sin6->sin6_addr);
  141. if (port_out)
  142. *port_out = ntohs(sin6->sin6_port);
  143. } else if (sa->sa_family == AF_UNIX) {
  144. tor_addr_make_af_unix(a);
  145. return 0;
  146. } else {
  147. tor_addr_make_unspec(a);
  148. return -1;
  149. }
  150. return 0;
  151. }
  152. /** Return a newly allocated string holding the address described in
  153. * <b>sa</b>. AF_UNIX, AF_UNSPEC, AF_INET, and AF_INET6 are supported. */
  154. char *
  155. tor_sockaddr_to_str(const struct sockaddr *sa)
  156. {
  157. char address[TOR_ADDR_BUF_LEN];
  158. char *result;
  159. tor_addr_t addr;
  160. uint16_t port;
  161. #ifdef HAVE_SYS_UN_H
  162. if (sa->sa_family == AF_UNIX) {
  163. struct sockaddr_un *s_un = (struct sockaddr_un *)sa;
  164. tor_asprintf(&result, "unix:%s", s_un->sun_path);
  165. return result;
  166. }
  167. #endif
  168. if (sa->sa_family == AF_UNSPEC)
  169. return tor_strdup("unspec");
  170. if (tor_addr_from_sockaddr(&addr, sa, &port) < 0)
  171. return NULL;
  172. if (! tor_addr_to_str(address, &addr, sizeof(address), 1))
  173. return NULL;
  174. tor_asprintf(&result, "%s:%d", address, (int)port);
  175. return result;
  176. }
  177. /** Set address <b>a</b> to the unspecified address. This address belongs to
  178. * no family. */
  179. void
  180. tor_addr_make_unspec(tor_addr_t *a)
  181. {
  182. memset(a, 0, sizeof(*a));
  183. a->family = AF_UNSPEC;
  184. }
  185. /** Set address <b>a</b> to the null address in address family <b>family</b>.
  186. * The null address for AF_INET is 0.0.0.0. The null address for AF_INET6 is
  187. * [::]. AF_UNSPEC is all null. */
  188. void
  189. tor_addr_make_null(tor_addr_t *a, sa_family_t family)
  190. {
  191. memset(a, 0, sizeof(*a));
  192. a->family = family;
  193. }
  194. /** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
  195. * *<b>addr</b> to the proper IP address and family. The <b>family</b>
  196. * argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a
  197. * <i>preferred</i> family, though another one may be returned if only one
  198. * family is implemented for this address.
  199. *
  200. * Return 0 on success, -1 on failure; 1 on transient failure.
  201. */
  202. int
  203. tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
  204. {
  205. /* Perhaps eventually this should be replaced by a tor_getaddrinfo or
  206. * something.
  207. */
  208. struct in_addr iaddr;
  209. struct in6_addr iaddr6;
  210. tor_assert(name);
  211. tor_assert(addr);
  212. tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);
  213. if (!*name) {
  214. /* Empty address is an error. */
  215. return -1;
  216. } else if (tor_inet_pton(AF_INET, name, &iaddr)) {
  217. /* It's an IPv4 IP. */
  218. if (family == AF_INET6)
  219. return -1;
  220. tor_addr_from_in(addr, &iaddr);
  221. return 0;
  222. } else if (tor_inet_pton(AF_INET6, name, &iaddr6)) {
  223. if (family == AF_INET)
  224. return -1;
  225. tor_addr_from_in6(addr, &iaddr6);
  226. return 0;
  227. } else {
  228. #ifdef HAVE_GETADDRINFO
  229. int err;
  230. struct addrinfo *res=NULL, *res_p;
  231. struct addrinfo *best=NULL;
  232. struct addrinfo hints;
  233. int result = -1;
  234. memset(&hints, 0, sizeof(hints));
  235. hints.ai_family = family;
  236. hints.ai_socktype = SOCK_STREAM;
  237. err = sandbox_getaddrinfo(name, NULL, &hints, &res);
  238. /* The check for 'res' here shouldn't be necessary, but it makes static
  239. * analysis tools happy. */
  240. if (!err && res) {
  241. best = NULL;
  242. for (res_p = res; res_p; res_p = res_p->ai_next) {
  243. if (family == AF_UNSPEC) {
  244. if (res_p->ai_family == AF_INET) {
  245. best = res_p;
  246. break;
  247. } else if (res_p->ai_family == AF_INET6 && !best) {
  248. best = res_p;
  249. }
  250. } else if (family == res_p->ai_family) {
  251. best = res_p;
  252. break;
  253. }
  254. }
  255. if (!best)
  256. best = res;
  257. if (best->ai_family == AF_INET) {
  258. tor_addr_from_in(addr,
  259. &((struct sockaddr_in*)best->ai_addr)->sin_addr);
  260. result = 0;
  261. } else if (best->ai_family == AF_INET6) {
  262. tor_addr_from_in6(addr,
  263. &((struct sockaddr_in6*)best->ai_addr)->sin6_addr);
  264. result = 0;
  265. }
  266. sandbox_freeaddrinfo(res);
  267. return result;
  268. }
  269. return (err == EAI_AGAIN) ? 1 : -1;
  270. #else
  271. struct hostent *ent;
  272. int err;
  273. #ifdef HAVE_GETHOSTBYNAME_R_6_ARG
  274. char buf[2048];
  275. struct hostent hostent;
  276. int r;
  277. r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err);
  278. #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
  279. char buf[2048];
  280. struct hostent hostent;
  281. ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err);
  282. #elif defined(HAVE_GETHOSTBYNAME_R_3_ARG)
  283. struct hostent_data data;
  284. struct hostent hent;
  285. memset(&data, 0, sizeof(data));
  286. err = gethostbyname_r(name, &hent, &data);
  287. ent = err ? NULL : &hent;
  288. #else
  289. ent = gethostbyname(name);
  290. #ifdef _WIN32
  291. err = WSAGetLastError();
  292. #else
  293. err = h_errno;
  294. #endif
  295. #endif /* endif HAVE_GETHOSTBYNAME_R_6_ARG. */
  296. if (ent) {
  297. if (ent->h_addrtype == AF_INET) {
  298. tor_addr_from_in(addr, (struct in_addr*) ent->h_addr);
  299. } else if (ent->h_addrtype == AF_INET6) {
  300. tor_addr_from_in6(addr, (struct in6_addr*) ent->h_addr);
  301. } else {
  302. tor_assert(0); /* gethostbyname() returned a bizarre addrtype */
  303. }
  304. return 0;
  305. }
  306. #ifdef _WIN32
  307. return (err == WSATRY_AGAIN) ? 1 : -1;
  308. #else
  309. return (err == TRY_AGAIN) ? 1 : -1;
  310. #endif
  311. #endif
  312. }
  313. }
  314. /** Return true iff <b>ip</b> is an IP reserved to localhost or local networks
  315. * in RFC1918 or RFC4193 or RFC4291. (fec0::/10, deprecated by RFC3879, is
  316. * also treated as internal for now.)
  317. */
  318. int
  319. tor_addr_is_internal_(const tor_addr_t *addr, int for_listening,
  320. const char *filename, int lineno)
  321. {
  322. uint32_t iph4 = 0;
  323. uint32_t iph6[4];
  324. tor_assert(addr);
  325. sa_family_t v_family = tor_addr_family(addr);
  326. if (v_family == AF_INET) {
  327. iph4 = tor_addr_to_ipv4h(addr);
  328. } else if (v_family == AF_INET6) {
  329. if (tor_addr_is_v4(addr)) { /* v4-mapped */
  330. uint32_t *addr32 = NULL;
  331. v_family = AF_INET;
  332. // Work around an incorrect NULL pointer dereference warning in
  333. // "clang --analyze" due to limited analysis depth
  334. addr32 = tor_addr_to_in6_addr32(addr);
  335. // To improve performance, wrap this assertion in:
  336. // #if !defined(__clang_analyzer__) || PARANOIA
  337. tor_assert(addr32);
  338. iph4 = ntohl(addr32[3]);
  339. }
  340. }
  341. if (v_family == AF_INET6) {
  342. const uint32_t *a32 = tor_addr_to_in6_addr32(addr);
  343. iph6[0] = ntohl(a32[0]);
  344. iph6[1] = ntohl(a32[1]);
  345. iph6[2] = ntohl(a32[2]);
  346. iph6[3] = ntohl(a32[3]);
  347. if (for_listening && !iph6[0] && !iph6[1] && !iph6[2] && !iph6[3]) /* :: */
  348. return 0;
  349. if (((iph6[0] & 0xfe000000) == 0xfc000000) || /* fc00/7 - RFC4193 */
  350. ((iph6[0] & 0xffc00000) == 0xfe800000) || /* fe80/10 - RFC4291 */
  351. ((iph6[0] & 0xffc00000) == 0xfec00000)) /* fec0/10 D- RFC3879 */
  352. return 1;
  353. if (!iph6[0] && !iph6[1] && !iph6[2] &&
  354. ((iph6[3] & 0xfffffffe) == 0x00000000)) /* ::/127 */
  355. return 1;
  356. return 0;
  357. } else if (v_family == AF_INET) {
  358. if (for_listening && !iph4) /* special case for binding to 0.0.0.0 */
  359. return 0;
  360. if (((iph4 & 0xff000000) == 0x0a000000) || /* 10/8 */
  361. ((iph4 & 0xff000000) == 0x00000000) || /* 0/8 */
  362. ((iph4 & 0xff000000) == 0x7f000000) || /* 127/8 */
  363. ((iph4 & 0xffff0000) == 0xa9fe0000) || /* 169.254/16 */
  364. ((iph4 & 0xfff00000) == 0xac100000) || /* 172.16/12 */
  365. ((iph4 & 0xffff0000) == 0xc0a80000)) /* 192.168/16 */
  366. return 1;
  367. return 0;
  368. }
  369. /* unknown address family... assume it's not safe for external use */
  370. /* rather than tor_assert(0) */
  371. log_warn(LD_BUG, "tor_addr_is_internal() called from %s:%d with a "
  372. "non-IP address of type %d", filename, lineno, (int)v_family);
  373. tor_fragile_assert();
  374. return 1;
  375. }
  376. /** Convert a tor_addr_t <b>addr</b> into a string, and store it in
  377. * <b>dest</b> of size <b>len</b>. Returns a pointer to dest on success,
  378. * or NULL on failure. If <b>decorate</b>, surround IPv6 addresses with
  379. * brackets.
  380. */
  381. const char *
  382. tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
  383. {
  384. const char *ptr;
  385. tor_assert(addr && dest);
  386. switch (tor_addr_family(addr)) {
  387. case AF_INET:
  388. /* Shortest addr x.x.x.x + \0 */
  389. if (len < 8)
  390. return NULL;
  391. ptr = tor_inet_ntop(AF_INET, &addr->addr.in_addr, dest, len);
  392. break;
  393. case AF_INET6:
  394. /* Shortest addr [ :: ] + \0 */
  395. if (len < (3 + (decorate ? 2 : 0)))
  396. return NULL;
  397. if (decorate)
  398. ptr = tor_inet_ntop(AF_INET6, &addr->addr.in6_addr, dest+1, len-2);
  399. else
  400. ptr = tor_inet_ntop(AF_INET6, &addr->addr.in6_addr, dest, len);
  401. if (ptr && decorate) {
  402. *dest = '[';
  403. memcpy(dest+strlen(dest), "]", 2);
  404. tor_assert(ptr == dest+1);
  405. ptr = dest;
  406. }
  407. break;
  408. case AF_UNIX:
  409. tor_snprintf(dest, len, "AF_UNIX");
  410. ptr = dest;
  411. break;
  412. default:
  413. return NULL;
  414. }
  415. return ptr;
  416. }
  417. /** Parse an .in-addr.arpa or .ip6.arpa address from <b>address</b>. Return 0
  418. * if this is not an .in-addr.arpa address or an .ip6.arpa address. Return -1
  419. * if this is an ill-formed .in-addr.arpa address or an .ip6.arpa address.
  420. * Also return -1 if <b>family</b> is not AF_UNSPEC, and the parsed address
  421. * family does not match <b>family</b>. On success, return 1, and store the
  422. * result, if any, into <b>result</b>, if provided.
  423. *
  424. * If <b>accept_regular</b> is set and the address is in neither recognized
  425. * reverse lookup hostname format, try parsing the address as a regular
  426. * IPv4 or IPv6 address too.
  427. */
  428. int
  429. tor_addr_parse_PTR_name(tor_addr_t *result, const char *address,
  430. int family, int accept_regular)
  431. {
  432. if (!strcasecmpend(address, ".in-addr.arpa")) {
  433. /* We have an in-addr.arpa address. */
  434. char buf[INET_NTOA_BUF_LEN];
  435. size_t len;
  436. struct in_addr inaddr;
  437. if (family == AF_INET6)
  438. return -1;
  439. len = strlen(address) - strlen(".in-addr.arpa");
  440. if (len >= INET_NTOA_BUF_LEN)
  441. return -1; /* Too long. */
  442. memcpy(buf, address, len);
  443. buf[len] = '\0';
  444. if (tor_inet_aton(buf, &inaddr) == 0)
  445. return -1; /* malformed. */
  446. /* reverse the bytes */
  447. inaddr.s_addr = (uint32_t)
  448. (((inaddr.s_addr & 0x000000ff) << 24)
  449. |((inaddr.s_addr & 0x0000ff00) << 8)
  450. |((inaddr.s_addr & 0x00ff0000) >> 8)
  451. |((inaddr.s_addr & 0xff000000) >> 24));
  452. if (result) {
  453. tor_addr_from_in(result, &inaddr);
  454. }
  455. return 1;
  456. }
  457. if (!strcasecmpend(address, ".ip6.arpa")) {
  458. const char *cp;
  459. int n0, n1;
  460. struct in6_addr in6;
  461. if (family == AF_INET)
  462. return -1;
  463. cp = address;
  464. for (int i = 0; i < 16; ++i) {
  465. n0 = hex_decode_digit(*cp++); /* The low-order nybble appears first. */
  466. if (*cp++ != '.') return -1; /* Then a dot. */
  467. n1 = hex_decode_digit(*cp++); /* The high-order nybble appears first. */
  468. if (*cp++ != '.') return -1; /* Then another dot. */
  469. if (n0<0 || n1 < 0) /* Both nybbles must be hex. */
  470. return -1;
  471. /* We don't check the length of the string in here. But that's okay,
  472. * since we already know that the string ends with ".ip6.arpa", and
  473. * there is no way to frameshift .ip6.arpa so it fits into the pattern
  474. * of hexdigit, period, hexdigit, period that we enforce above.
  475. */
  476. /* Assign from low-byte to high-byte. */
  477. in6.s6_addr[15-i] = n0 | (n1 << 4);
  478. }
  479. if (strcasecmp(cp, "ip6.arpa"))
  480. return -1;
  481. if (result) {
  482. tor_addr_from_in6(result, &in6);
  483. }
  484. return 1;
  485. }
  486. if (accept_regular) {
  487. tor_addr_t tmp;
  488. int r = tor_addr_parse(&tmp, address);
  489. if (r < 0)
  490. return 0;
  491. if (r != family && family != AF_UNSPEC)
  492. return -1;
  493. if (result)
  494. memcpy(result, &tmp, sizeof(tor_addr_t));
  495. return 1;
  496. }
  497. return 0;
  498. }
  499. /** Convert <b>addr</b> to an in-addr.arpa name or a .ip6.arpa name,
  500. * and store the result in the <b>outlen</b>-byte buffer at
  501. * <b>out</b>. Return the number of chars written to <b>out</b>, not
  502. * including the trailing \0, on success. Returns -1 on failure. */
  503. int
  504. tor_addr_to_PTR_name(char *out, size_t outlen,
  505. const tor_addr_t *addr)
  506. {
  507. tor_assert(out);
  508. tor_assert(addr);
  509. if (addr->family == AF_INET) {
  510. uint32_t a = tor_addr_to_ipv4h(addr);
  511. return tor_snprintf(out, outlen, "%d.%d.%d.%d.in-addr.arpa",
  512. (int)(uint8_t)((a )&0xff),
  513. (int)(uint8_t)((a>>8 )&0xff),
  514. (int)(uint8_t)((a>>16)&0xff),
  515. (int)(uint8_t)((a>>24)&0xff));
  516. } else if (addr->family == AF_INET6) {
  517. int i;
  518. char *cp = out;
  519. const uint8_t *bytes = tor_addr_to_in6_addr8(addr);
  520. if (outlen < REVERSE_LOOKUP_NAME_BUF_LEN)
  521. return -1;
  522. for (i = 15; i >= 0; --i) {
  523. uint8_t byte = bytes[i];
  524. *cp++ = "0123456789abcdef"[byte & 0x0f];
  525. *cp++ = '.';
  526. *cp++ = "0123456789abcdef"[byte >> 4];
  527. *cp++ = '.';
  528. }
  529. memcpy(cp, "ip6.arpa", 9); /* 8 characters plus NUL */
  530. return 32 * 2 + 8;
  531. }
  532. return -1;
  533. }
  534. /** Parse a string <b>s</b> containing an IPv4/IPv6 address, and possibly
  535. * a mask and port or port range. Store the parsed address in
  536. * <b>addr_out</b>, a mask (if any) in <b>mask_out</b>, and port(s) (if any)
  537. * in <b>port_min_out</b> and <b>port_max_out</b>.
  538. *
  539. * The syntax is:
  540. * Address OptMask OptPortRange
  541. * Address ::= IPv4Address / "[" IPv6Address "]" / "*"
  542. * OptMask ::= "/" Integer /
  543. * OptPortRange ::= ":*" / ":" Integer / ":" Integer "-" Integer /
  544. *
  545. * - If mask, minport, or maxport are NULL, we do not want these
  546. * options to be set; treat them as an error if present.
  547. * - If the string has no mask, the mask is set to /32 (IPv4) or /128 (IPv6).
  548. * - If the string has one port, it is placed in both min and max port
  549. * variables.
  550. * - If the string has no port(s), port_(min|max)_out are set to 1 and 65535.
  551. *
  552. * Return an address family on success, or -1 if an invalid address string is
  553. * provided.
  554. *
  555. * If 'flags & TAPMP_EXTENDED_STAR' is false, then the wildcard address '*'
  556. * yield an IPv4 wildcard.
  557. *
  558. * If 'flags & TAPMP_EXTENDED_STAR' is true, then the wildcard address '*'
  559. * yields an AF_UNSPEC wildcard address, and the following change is made
  560. * in the grammar above:
  561. * Address ::= IPv4Address / "[" IPv6Address "]" / "*" / "*4" / "*6"
  562. * with the new "*4" and "*6" productions creating a wildcard to match
  563. * IPv4 or IPv6 addresses.
  564. *
  565. */
  566. int
  567. tor_addr_parse_mask_ports(const char *s,
  568. unsigned flags,
  569. tor_addr_t *addr_out,
  570. maskbits_t *maskbits_out,
  571. uint16_t *port_min_out, uint16_t *port_max_out)
  572. {
  573. char *base = NULL, *address, *mask = NULL, *port = NULL, *rbracket = NULL;
  574. char *endptr;
  575. int any_flag=0, v4map=0;
  576. sa_family_t family;
  577. struct in6_addr in6_tmp;
  578. struct in_addr in_tmp = { .s_addr = 0 };
  579. tor_assert(s);
  580. tor_assert(addr_out);
  581. /** Longest possible length for an address, mask, and port-range combination.
  582. * Includes IP, [], /mask, :, ports */
  583. #define MAX_ADDRESS_LENGTH (TOR_ADDR_BUF_LEN+2+(1+INET_NTOA_BUF_LEN)+12+1)
  584. if (strlen(s) > MAX_ADDRESS_LENGTH) {
  585. log_warn(LD_GENERAL, "Impossibly long IP %s; rejecting", escaped(s));
  586. goto err;
  587. }
  588. base = tor_strdup(s);
  589. /* Break 'base' into separate strings. */
  590. address = base;
  591. if (*address == '[') { /* Probably IPv6 */
  592. address++;
  593. rbracket = strchr(address, ']');
  594. if (!rbracket) {
  595. log_warn(LD_GENERAL,
  596. "No closing IPv6 bracket in address pattern; rejecting.");
  597. goto err;
  598. }
  599. }
  600. mask = strchr((rbracket?rbracket:address),'/');
  601. port = strchr((mask?mask:(rbracket?rbracket:address)), ':');
  602. if (port)
  603. *port++ = '\0';
  604. if (mask)
  605. *mask++ = '\0';
  606. if (rbracket)
  607. *rbracket = '\0';
  608. if (port && mask)
  609. tor_assert(port > mask);
  610. if (mask && rbracket)
  611. tor_assert(mask > rbracket);
  612. /* Now "address" is the a.b.c.d|'*'|abcd::1 part...
  613. * "mask" is the Mask|Maskbits part...
  614. * and "port" is the *|port|min-max part.
  615. */
  616. /* Process the address portion */
  617. memset(addr_out, 0, sizeof(tor_addr_t));
  618. if (!strcmp(address, "*")) {
  619. if (flags & TAPMP_EXTENDED_STAR) {
  620. family = AF_UNSPEC;
  621. tor_addr_make_unspec(addr_out);
  622. } else {
  623. family = AF_INET;
  624. tor_addr_from_ipv4h(addr_out, 0);
  625. }
  626. any_flag = 1;
  627. } else if (!strcmp(address, "*4") && (flags & TAPMP_EXTENDED_STAR)) {
  628. family = AF_INET;
  629. tor_addr_from_ipv4h(addr_out, 0);
  630. any_flag = 1;
  631. } else if (!strcmp(address, "*6") && (flags & TAPMP_EXTENDED_STAR)) {
  632. static char nil_bytes[16] = { [0]=0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
  633. family = AF_INET6;
  634. tor_addr_from_ipv6_bytes(addr_out, nil_bytes);
  635. any_flag = 1;
  636. } else if (tor_inet_pton(AF_INET6, address, &in6_tmp) > 0) {
  637. family = AF_INET6;
  638. tor_addr_from_in6(addr_out, &in6_tmp);
  639. } else if (tor_inet_pton(AF_INET, address, &in_tmp) > 0) {
  640. family = AF_INET;
  641. tor_addr_from_in(addr_out, &in_tmp);
  642. } else {
  643. log_warn(LD_GENERAL, "Malformed IP %s in address pattern; rejecting.",
  644. escaped(address));
  645. goto err;
  646. }
  647. v4map = tor_addr_is_v4(addr_out);
  648. /* Parse mask */
  649. if (maskbits_out) {
  650. int bits = 0;
  651. struct in_addr v4mask;
  652. if (mask) { /* the caller (tried to) specify a mask */
  653. bits = (int) strtol(mask, &endptr, 10);
  654. if (!*endptr) { /* strtol converted everything, so it was an integer */
  655. if ((bits<0 || bits>128) ||
  656. (family == AF_INET && bits > 32)) {
  657. log_warn(LD_GENERAL,
  658. "Bad number of mask bits (%d) on address range; rejecting.",
  659. bits);
  660. goto err;
  661. }
  662. } else { /* mask might still be an address-style mask */
  663. if (tor_inet_pton(AF_INET, mask, &v4mask) > 0) {
  664. bits = addr_mask_get_bits(ntohl(v4mask.s_addr));
  665. if (bits < 0) {
  666. log_warn(LD_GENERAL,
  667. "IPv4-style mask %s is not a prefix address; rejecting.",
  668. escaped(mask));
  669. goto err;
  670. }
  671. } else { /* Not IPv4; we don't do address-style IPv6 masks. */
  672. log_warn(LD_GENERAL,
  673. "Malformed mask on address range %s; rejecting.",
  674. escaped(s));
  675. goto err;
  676. }
  677. }
  678. if (family == AF_INET6 && v4map) {
  679. if (bits > 32 && bits < 96) { /* Crazy */
  680. log_warn(LD_GENERAL,
  681. "Bad mask bits %d for V4-mapped V6 address; rejecting.",
  682. bits);
  683. goto err;
  684. }
  685. /* XXXX_IP6 is this really what we want? */
  686. bits = 96 + bits%32; /* map v4-mapped masks onto 96-128 bits */
  687. }
  688. if (any_flag) {
  689. log_warn(LD_GENERAL,
  690. "Found bit prefix with wildcard address; rejecting");
  691. goto err;
  692. }
  693. } else { /* pick an appropriate mask, as none was given */
  694. if (any_flag)
  695. bits = 0; /* This is okay whether it's V6 or V4 (FIX V4-mapped V6!) */
  696. else if (tor_addr_family(addr_out) == AF_INET)
  697. bits = 32;
  698. else if (tor_addr_family(addr_out) == AF_INET6)
  699. bits = 128;
  700. }
  701. *maskbits_out = (maskbits_t) bits;
  702. } else {
  703. if (mask) {
  704. log_warn(LD_GENERAL,
  705. "Unexpected mask in address %s; rejecting", escaped(s));
  706. goto err;
  707. }
  708. }
  709. /* Parse port(s) */
  710. if (port_min_out) {
  711. uint16_t port2;
  712. if (!port_max_out) /* caller specified one port; fake the second one */
  713. port_max_out = &port2;
  714. if (parse_port_range(port, port_min_out, port_max_out) < 0) {
  715. goto err;
  716. } else if ((*port_min_out != *port_max_out) && port_max_out == &port2) {
  717. log_warn(LD_GENERAL,
  718. "Wanted one port from address range, but there are two.");
  719. port_max_out = NULL; /* caller specified one port, so set this back */
  720. goto err;
  721. }
  722. } else {
  723. if (port) {
  724. log_warn(LD_GENERAL,
  725. "Unexpected ports in address %s; rejecting", escaped(s));
  726. goto err;
  727. }
  728. }
  729. tor_free(base);
  730. return tor_addr_family(addr_out);
  731. err:
  732. tor_free(base);
  733. return -1;
  734. }
  735. /** Determine whether an address is IPv4, either native or IPv4-mapped IPv6.
  736. * Note that this is about representation only, as any decent stack will
  737. * reject IPv4-mapped addresses received on the wire (and won't use them
  738. * on the wire either).
  739. */
  740. int
  741. tor_addr_is_v4(const tor_addr_t *addr)
  742. {
  743. tor_assert(addr);
  744. if (tor_addr_family(addr) == AF_INET)
  745. return 1;
  746. if (tor_addr_family(addr) == AF_INET6) {
  747. /* First two don't need to be ordered */
  748. uint32_t *a32 = tor_addr_to_in6_addr32(addr);
  749. if (a32[0] == 0 && a32[1] == 0 && ntohl(a32[2]) == 0x0000ffffu)
  750. return 1;
  751. }
  752. return 0; /* Not IPv4 - unknown family or a full-blood IPv6 address */
  753. }
  754. /** Determine whether an address <b>addr</b> is null, either all zeroes or
  755. * belonging to family AF_UNSPEC.
  756. */
  757. int
  758. tor_addr_is_null(const tor_addr_t *addr)
  759. {
  760. tor_assert(addr);
  761. switch (tor_addr_family(addr)) {
  762. case AF_INET6: {
  763. uint32_t *a32 = tor_addr_to_in6_addr32(addr);
  764. return (a32[0] == 0) && (a32[1] == 0) && (a32[2] == 0) && (a32[3] == 0);
  765. }
  766. case AF_INET:
  767. return (tor_addr_to_ipv4n(addr) == 0);
  768. case AF_UNIX:
  769. return 1;
  770. case AF_UNSPEC:
  771. return 1;
  772. default:
  773. log_warn(LD_BUG, "Called with unknown address family %d",
  774. (int)tor_addr_family(addr));
  775. return 0;
  776. }
  777. //return 1;
  778. }
  779. /** Return true iff <b>addr</b> is a loopback address */
  780. int
  781. tor_addr_is_loopback(const tor_addr_t *addr)
  782. {
  783. tor_assert(addr);
  784. switch (tor_addr_family(addr)) {
  785. case AF_INET6: {
  786. /* ::1 */
  787. uint32_t *a32 = tor_addr_to_in6_addr32(addr);
  788. return (a32[0] == 0) && (a32[1] == 0) && (a32[2] == 0) &&
  789. (ntohl(a32[3]) == 1);
  790. }
  791. case AF_INET:
  792. /* 127.0.0.1 */
  793. return (tor_addr_to_ipv4h(addr) & 0xff000000) == 0x7f000000;
  794. case AF_UNSPEC:
  795. return 0;
  796. default:
  797. tor_fragile_assert();
  798. return 0;
  799. }
  800. }
  801. /** Set <b>dest</b> to equal the IPv4 address in <b>v4addr</b> (given in
  802. * network order). */
  803. void
  804. tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr)
  805. {
  806. tor_assert(dest);
  807. memset(dest, 0, sizeof(tor_addr_t));
  808. dest->family = AF_INET;
  809. dest->addr.in_addr.s_addr = v4addr;
  810. }
  811. /** Set <b>dest</b> to equal the IPv6 address in the 16 bytes at
  812. * <b>ipv6_bytes</b>. */
  813. void
  814. tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *ipv6_bytes)
  815. {
  816. tor_assert(dest);
  817. tor_assert(ipv6_bytes);
  818. memset(dest, 0, sizeof(tor_addr_t));
  819. dest->family = AF_INET6;
  820. memcpy(dest->addr.in6_addr.s6_addr, ipv6_bytes, 16);
  821. }
  822. /** Set <b>dest</b> equal to the IPv6 address in the in6_addr <b>in6</b>. */
  823. void
  824. tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
  825. {
  826. tor_addr_from_ipv6_bytes(dest, (const char*)in6->s6_addr);
  827. }
  828. /** Copy a tor_addr_t from <b>src</b> to <b>dest</b>.
  829. */
  830. void
  831. tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
  832. {
  833. if (src == dest)
  834. return;
  835. tor_assert(src);
  836. tor_assert(dest);
  837. memcpy(dest, src, sizeof(tor_addr_t));
  838. }
  839. /** Copy a tor_addr_t from <b>src</b> to <b>dest</b>, taking extra care to
  840. * copy only the well-defined portions. Used for computing hashes of
  841. * addresses.
  842. */
  843. void
  844. tor_addr_copy_tight(tor_addr_t *dest, const tor_addr_t *src)
  845. {
  846. tor_assert(src != dest);
  847. tor_assert(src);
  848. tor_assert(dest);
  849. memset(dest, 0, sizeof(tor_addr_t));
  850. dest->family = src->family;
  851. switch (tor_addr_family(src))
  852. {
  853. case AF_INET:
  854. dest->addr.in_addr.s_addr = src->addr.in_addr.s_addr;
  855. break;
  856. case AF_INET6:
  857. memcpy(dest->addr.in6_addr.s6_addr, src->addr.in6_addr.s6_addr, 16);
  858. case AF_UNSPEC:
  859. break;
  860. default:
  861. tor_fragile_assert();
  862. }
  863. }
  864. /** Given two addresses <b>addr1</b> and <b>addr2</b>, return 0 if the two
  865. * addresses are equivalent under the mask mbits, less than 0 if addr1
  866. * precedes addr2, and greater than 0 otherwise.
  867. *
  868. * Different address families (IPv4 vs IPv6) are always considered unequal if
  869. * <b>how</b> is CMP_EXACT; otherwise, IPv6-mapped IPv4 addresses are
  870. * considered equivalent to their IPv4 equivalents.
  871. */
  872. int
  873. tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
  874. tor_addr_comparison_t how)
  875. {
  876. return tor_addr_compare_masked(addr1, addr2, 128, how);
  877. }
  878. /** As tor_addr_compare(), but only looks at the first <b>mask</b> bits of
  879. * the address.
  880. *
  881. * Reduce over-specific masks (>128 for ipv6, >32 for ipv4) to 128 or 32.
  882. *
  883. * The mask is interpreted relative to <b>addr1</b>, so that if a is
  884. * \::ffff:1.2.3.4, and b is 3.4.5.6,
  885. * tor_addr_compare_masked(a,b,100,CMP_SEMANTIC) is the same as
  886. * -tor_addr_compare_masked(b,a,4,CMP_SEMANTIC).
  887. *
  888. * We guarantee that the ordering from tor_addr_compare_masked is a total
  889. * order on addresses, but not that it is any particular order, or that it
  890. * will be the same from one version to the next.
  891. */
  892. int
  893. tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
  894. maskbits_t mbits, tor_addr_comparison_t how)
  895. {
  896. /** Helper: Evaluates to -1 if a is less than b, 0 if a equals b, or 1 if a
  897. * is greater than b. May evaluate a and b more than once. */
  898. #define TRISTATE(a,b) (((a)<(b))?-1: (((a)==(b))?0:1))
  899. sa_family_t family1, family2, v_family1, v_family2;
  900. tor_assert(addr1 && addr2);
  901. v_family1 = family1 = tor_addr_family(addr1);
  902. v_family2 = family2 = tor_addr_family(addr2);
  903. if (family1==family2) {
  904. /* When the families are the same, there's only one way to do the
  905. * comparison: exactly. */
  906. int r;
  907. switch (family1) {
  908. case AF_UNSPEC:
  909. return 0; /* All unspecified addresses are equal */
  910. case AF_INET: {
  911. uint32_t a1 = tor_addr_to_ipv4h(addr1);
  912. uint32_t a2 = tor_addr_to_ipv4h(addr2);
  913. if (mbits <= 0)
  914. return 0;
  915. if (mbits > 32)
  916. mbits = 32;
  917. a1 >>= (32-mbits);
  918. a2 >>= (32-mbits);
  919. r = TRISTATE(a1, a2);
  920. return r;
  921. }
  922. case AF_INET6: {
  923. const uint8_t *a1 = tor_addr_to_in6_addr8(addr1);
  924. const uint8_t *a2 = tor_addr_to_in6_addr8(addr2);
  925. const int bytes = mbits >> 3;
  926. const int leftover_bits = mbits & 7;
  927. if (bytes && (r = tor_memcmp(a1, a2, bytes))) {
  928. return r;
  929. } else if (leftover_bits) {
  930. uint8_t b1 = a1[bytes] >> (8-leftover_bits);
  931. uint8_t b2 = a2[bytes] >> (8-leftover_bits);
  932. return TRISTATE(b1, b2);
  933. } else {
  934. return 0;
  935. }
  936. }
  937. default:
  938. tor_fragile_assert();
  939. return 0;
  940. }
  941. } else if (how == CMP_EXACT) {
  942. /* Unequal families and an exact comparison? Stop now! */
  943. return TRISTATE(family1, family2);
  944. }
  945. if (mbits == 0)
  946. return 0;
  947. if (family1 == AF_INET6 && tor_addr_is_v4(addr1))
  948. v_family1 = AF_INET;
  949. if (family2 == AF_INET6 && tor_addr_is_v4(addr2))
  950. v_family2 = AF_INET;
  951. if (v_family1 == v_family2) {
  952. /* One or both addresses are a mapped ipv4 address. */
  953. uint32_t a1, a2;
  954. if (family1 == AF_INET6) {
  955. a1 = tor_addr_to_mapped_ipv4h(addr1);
  956. if (mbits <= 96)
  957. return 0;
  958. mbits -= 96; /* We just decided that the first 96 bits of a1 "match". */
  959. } else {
  960. a1 = tor_addr_to_ipv4h(addr1);
  961. }
  962. if (family2 == AF_INET6) {
  963. a2 = tor_addr_to_mapped_ipv4h(addr2);
  964. } else {
  965. a2 = tor_addr_to_ipv4h(addr2);
  966. }
  967. if (mbits > 32) mbits = 32;
  968. a1 >>= (32-mbits);
  969. a2 >>= (32-mbits);
  970. return TRISTATE(a1, a2);
  971. } else {
  972. /* Unequal families, and semantic comparison, and no semantic family
  973. * matches. */
  974. return TRISTATE(family1, family2);
  975. }
  976. }
  977. /** Return a hash code based on the address addr. DOCDOC extra */
  978. uint64_t
  979. tor_addr_hash(const tor_addr_t *addr)
  980. {
  981. switch (tor_addr_family(addr)) {
  982. case AF_INET:
  983. return siphash24g(&addr->addr.in_addr.s_addr, 4);
  984. case AF_UNSPEC:
  985. return 0x4e4d5342;
  986. case AF_INET6:
  987. return siphash24g(&addr->addr.in6_addr.s6_addr, 16);
  988. default:
  989. tor_fragile_assert();
  990. return 0;
  991. }
  992. }
  993. /** Return a newly allocated string with a representation of <b>addr</b>. */
  994. char *
  995. tor_dup_addr(const tor_addr_t *addr)
  996. {
  997. char buf[TOR_ADDR_BUF_LEN];
  998. if (tor_addr_to_str(buf, addr, sizeof(buf), 0)) {
  999. return tor_strdup(buf);
  1000. } else {
  1001. return tor_strdup("<unknown address type>");
  1002. }
  1003. }
  1004. /** Return a string representing the address <b>addr</b>. This string
  1005. * is statically allocated, and must not be freed. Each call to
  1006. * <b>fmt_addr_impl</b> invalidates the last result of the function.
  1007. * This function is not thread-safe. If <b>decorate</b> is set, add
  1008. * brackets to IPv6 addresses.
  1009. *
  1010. * It's better to use the wrapper macros of this function:
  1011. * <b>fmt_addr()</b> and <b>fmt_and_decorate_addr()</b>.
  1012. */
  1013. const char *
  1014. fmt_addr_impl(const tor_addr_t *addr, int decorate)
  1015. {
  1016. static char buf[TOR_ADDR_BUF_LEN];
  1017. if (!addr) return "<null>";
  1018. if (tor_addr_to_str(buf, addr, sizeof(buf), decorate))
  1019. return buf;
  1020. else
  1021. return "???";
  1022. }
  1023. /** Return a string representing the pair <b>addr</b> and <b>port</b>.
  1024. * This calls fmt_and_decorate_addr internally, so IPv6 addresses will
  1025. * have brackets, and the caveats of fmt_addr_impl apply.
  1026. */
  1027. const char *
  1028. fmt_addrport(const tor_addr_t *addr, uint16_t port)
  1029. {
  1030. /* Add space for a colon and up to 5 digits. */
  1031. static char buf[TOR_ADDR_BUF_LEN + 6];
  1032. tor_snprintf(buf, sizeof(buf), "%s:%u", fmt_and_decorate_addr(addr), port);
  1033. return buf;
  1034. }
  1035. /** Like fmt_addr(), but takes <b>addr</b> as a host-order IPv4
  1036. * addresses. Also not thread-safe, also clobbers its return buffer on
  1037. * repeated calls. */
  1038. const char *
  1039. fmt_addr32(uint32_t addr)
  1040. {
  1041. static char buf[INET_NTOA_BUF_LEN];
  1042. struct in_addr in;
  1043. in.s_addr = htonl(addr);
  1044. tor_inet_ntoa(&in, buf, sizeof(buf));
  1045. return buf;
  1046. }
  1047. /** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>. The string
  1048. * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by
  1049. * square brackets.
  1050. *
  1051. * Return an address family on success, or -1 if an invalid address string is
  1052. * provided. */
  1053. int
  1054. tor_addr_parse(tor_addr_t *addr, const char *src)
  1055. {
  1056. /* Holds substring of IPv6 address after removing square brackets */
  1057. char *tmp = NULL;
  1058. int result;
  1059. struct in_addr in_tmp;
  1060. struct in6_addr in6_tmp;
  1061. tor_assert(addr && src);
  1062. if (src[0] == '[' && src[1])
  1063. src = tmp = tor_strndup(src+1, strlen(src)-2);
  1064. if (tor_inet_pton(AF_INET6, src, &in6_tmp) > 0) {
  1065. result = AF_INET6;
  1066. tor_addr_from_in6(addr, &in6_tmp);
  1067. } else if (tor_inet_pton(AF_INET, src, &in_tmp) > 0) {
  1068. result = AF_INET;
  1069. tor_addr_from_in(addr, &in_tmp);
  1070. } else {
  1071. result = -1;
  1072. }
  1073. tor_free(tmp);
  1074. return result;
  1075. }
  1076. /** Parse an address or address-port combination from <b>s</b>, resolve the
  1077. * address as needed, and put the result in <b>addr_out</b> and (optionally)
  1078. * <b>port_out</b>. Return 0 on success, negative on failure. */
  1079. int
  1080. tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
  1081. {
  1082. const char *port;
  1083. tor_addr_t addr;
  1084. uint16_t portval;
  1085. char *tmp = NULL;
  1086. tor_assert(s);
  1087. tor_assert(addr_out);
  1088. s = eat_whitespace(s);
  1089. if (*s == '[') {
  1090. port = strstr(s, "]");
  1091. if (!port)
  1092. goto err;
  1093. tmp = tor_strndup(s+1, port-(s+1));
  1094. port = port+1;
  1095. if (*port == ':')
  1096. port++;
  1097. else
  1098. port = NULL;
  1099. } else {
  1100. port = strchr(s, ':');
  1101. if (port)
  1102. tmp = tor_strndup(s, port-s);
  1103. else
  1104. tmp = tor_strdup(s);
  1105. if (port)
  1106. ++port;
  1107. }
  1108. if (tor_addr_lookup(tmp, AF_UNSPEC, &addr) != 0)
  1109. goto err;
  1110. tor_free(tmp);
  1111. if (port) {
  1112. portval = (int) tor_parse_long(port, 10, 1, 65535, NULL, NULL);
  1113. if (!portval)
  1114. goto err;
  1115. } else {
  1116. portval = 0;
  1117. }
  1118. if (port_out)
  1119. *port_out = portval;
  1120. tor_addr_copy(addr_out, &addr);
  1121. return 0;
  1122. err:
  1123. tor_free(tmp);
  1124. return -1;
  1125. }
  1126. #ifdef _WIN32
  1127. typedef ULONG (WINAPI *GetAdaptersAddresses_fn_t)(
  1128. ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG);
  1129. #endif
  1130. /** Try to ask our network interfaces what addresses they are bound to.
  1131. * Return a new smartlist of tor_addr_t on success, and NULL on failure.
  1132. * (An empty smartlist indicates that we successfully learned that we have no
  1133. * addresses.) Log failure messages at <b>severity</b>. */
  1134. static smartlist_t *
  1135. get_interface_addresses_raw(int severity)
  1136. {
  1137. #if defined(HAVE_GETIFADDRS)
  1138. /* Most free Unixy systems provide getifaddrs, which gives us a linked list
  1139. * of struct ifaddrs. */
  1140. struct ifaddrs *ifa = NULL;
  1141. const struct ifaddrs *i;
  1142. smartlist_t *result;
  1143. if (getifaddrs(&ifa) < 0) {
  1144. log_fn(severity, LD_NET, "Unable to call getifaddrs(): %s",
  1145. strerror(errno));
  1146. return NULL;
  1147. }
  1148. result = smartlist_new();
  1149. for (i = ifa; i; i = i->ifa_next) {
  1150. tor_addr_t tmp;
  1151. if ((i->ifa_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
  1152. continue;
  1153. if (!i->ifa_addr)
  1154. continue;
  1155. if (i->ifa_addr->sa_family != AF_INET &&
  1156. i->ifa_addr->sa_family != AF_INET6)
  1157. continue;
  1158. if (tor_addr_from_sockaddr(&tmp, i->ifa_addr, NULL) < 0)
  1159. continue;
  1160. smartlist_add(result, tor_memdup(&tmp, sizeof(tmp)));
  1161. }
  1162. freeifaddrs(ifa);
  1163. return result;
  1164. #elif defined(_WIN32)
  1165. /* Windows XP began to provide GetAdaptersAddresses. Windows 2000 had a
  1166. "GetAdaptersInfo", but that's deprecated; let's just try
  1167. GetAdaptersAddresses and fall back to connect+getsockname.
  1168. */
  1169. HANDLE lib = load_windows_system_library(TEXT("iphlpapi.dll"));
  1170. smartlist_t *result = NULL;
  1171. GetAdaptersAddresses_fn_t fn;
  1172. ULONG size, res;
  1173. IP_ADAPTER_ADDRESSES *addresses = NULL, *address;
  1174. (void) severity;
  1175. #define FLAGS (GAA_FLAG_SKIP_ANYCAST | \
  1176. GAA_FLAG_SKIP_MULTICAST | \
  1177. GAA_FLAG_SKIP_DNS_SERVER)
  1178. if (!lib) {
  1179. log_fn(severity, LD_NET, "Unable to load iphlpapi.dll");
  1180. goto done;
  1181. }
  1182. if (!(fn = (GetAdaptersAddresses_fn_t)
  1183. GetProcAddress(lib, "GetAdaptersAddresses"))) {
  1184. log_fn(severity, LD_NET, "Unable to obtain pointer to "
  1185. "GetAdaptersAddresses");
  1186. goto done;
  1187. }
  1188. /* Guess how much space we need. */
  1189. size = 15*1024;
  1190. addresses = tor_malloc(size);
  1191. res = fn(AF_UNSPEC, FLAGS, NULL, addresses, &size);
  1192. if (res == ERROR_BUFFER_OVERFLOW) {
  1193. /* we didn't guess that we needed enough space; try again */
  1194. tor_free(addresses);
  1195. addresses = tor_malloc(size);
  1196. res = fn(AF_UNSPEC, FLAGS, NULL, addresses, &size);
  1197. }
  1198. if (res != NO_ERROR) {
  1199. log_fn(severity, LD_NET, "GetAdaptersAddresses failed (result: %lu)", res);
  1200. goto done;
  1201. }
  1202. result = smartlist_new();
  1203. for (address = addresses; address; address = address->Next) {
  1204. IP_ADAPTER_UNICAST_ADDRESS *a;
  1205. for (a = address->FirstUnicastAddress; a; a = a->Next) {
  1206. /* Yes, it's a linked list inside a linked list */
  1207. struct sockaddr *sa = a->Address.lpSockaddr;
  1208. tor_addr_t tmp;
  1209. if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
  1210. continue;
  1211. if (tor_addr_from_sockaddr(&tmp, sa, NULL) < 0)
  1212. continue;
  1213. smartlist_add(result, tor_memdup(&tmp, sizeof(tmp)));
  1214. }
  1215. }
  1216. done:
  1217. if (lib)
  1218. FreeLibrary(lib);
  1219. tor_free(addresses);
  1220. return result;
  1221. #elif defined(SIOCGIFCONF) && defined(HAVE_IOCTL)
  1222. /* Some older unixy systems make us use ioctl(SIOCGIFCONF) */
  1223. struct ifconf ifc;
  1224. int fd, i, sz, n;
  1225. smartlist_t *result = NULL;
  1226. /* This interface, AFAICT, only supports AF_INET addresses */
  1227. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1228. if (fd < 0) {
  1229. tor_log(severity, LD_NET, "socket failed: %s", strerror(errno));
  1230. goto done;
  1231. }
  1232. /* Guess how much space we need. */
  1233. ifc.ifc_len = sz = 15*1024;
  1234. ifc.ifc_ifcu.ifcu_req = tor_malloc(sz);
  1235. if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
  1236. tor_log(severity, LD_NET, "ioctl failed: %s", strerror(errno));
  1237. close(fd);
  1238. goto done;
  1239. }
  1240. close(fd);
  1241. result = smartlist_new();
  1242. if (ifc.ifc_len < sz)
  1243. sz = ifc.ifc_len;
  1244. n = sz / sizeof(struct ifreq);
  1245. for (i = 0; i < n ; ++i) {
  1246. struct ifreq *r = &ifc.ifc_ifcu.ifcu_req[i];
  1247. struct sockaddr *sa = &r->ifr_addr;
  1248. tor_addr_t tmp;
  1249. if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
  1250. continue; /* should be impossible */
  1251. if (tor_addr_from_sockaddr(&tmp, sa, NULL) < 0)
  1252. continue;
  1253. smartlist_add(result, tor_memdup(&tmp, sizeof(tmp)));
  1254. }
  1255. done:
  1256. tor_free(ifc.ifc_ifcu.ifcu_req);
  1257. return result;
  1258. #else
  1259. (void) severity;
  1260. return NULL;
  1261. #endif
  1262. }
  1263. /** Return true iff <b>a</b> is a multicast address. */
  1264. static int
  1265. tor_addr_is_multicast(const tor_addr_t *a)
  1266. {
  1267. sa_family_t family = tor_addr_family(a);
  1268. if (family == AF_INET) {
  1269. uint32_t ipv4h = tor_addr_to_ipv4h(a);
  1270. if ((ipv4h >> 24) == 0xe0)
  1271. return 1; /* Multicast */
  1272. } else if (family == AF_INET6) {
  1273. const uint8_t *a32 = tor_addr_to_in6_addr8(a);
  1274. if (a32[0] == 0xff)
  1275. return 1;
  1276. }
  1277. return 0;
  1278. }
  1279. /** Set *<b>addr</b> to the IP address (if any) of whatever interface
  1280. * connects to the Internet. This address should only be used in checking
  1281. * whether our address has changed. Return 0 on success, -1 on failure.
  1282. */
  1283. MOCK_IMPL(int,
  1284. get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr))
  1285. {
  1286. /* XXX really, this function should yield a smartlist of addresses. */
  1287. smartlist_t *addrs;
  1288. int sock=-1, r=-1;
  1289. struct sockaddr_storage my_addr, target_addr;
  1290. socklen_t addr_len;
  1291. tor_assert(addr);
  1292. /* Try to do this the smart way if possible. */
  1293. if ((addrs = get_interface_addresses_raw(severity))) {
  1294. int rv = -1;
  1295. SMARTLIST_FOREACH_BEGIN(addrs, tor_addr_t *, a) {
  1296. if (family != AF_UNSPEC && family != tor_addr_family(a))
  1297. continue;
  1298. if (tor_addr_is_loopback(a) ||
  1299. tor_addr_is_multicast(a))
  1300. continue;
  1301. tor_addr_copy(addr, a);
  1302. rv = 0;
  1303. /* If we found a non-internal address, declare success. Otherwise,
  1304. * keep looking. */
  1305. if (!tor_addr_is_internal(a, 0))
  1306. break;
  1307. } SMARTLIST_FOREACH_END(a);
  1308. SMARTLIST_FOREACH(addrs, tor_addr_t *, a, tor_free(a));
  1309. smartlist_free(addrs);
  1310. return rv;
  1311. }
  1312. /* Okay, the smart way is out. */
  1313. memset(addr, 0, sizeof(tor_addr_t));
  1314. memset(&target_addr, 0, sizeof(target_addr));
  1315. /* Don't worry: no packets are sent. We just need to use a real address
  1316. * on the actual Internet. */
  1317. if (family == AF_INET6) {
  1318. struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&target_addr;
  1319. /* Use the "discard" service port */
  1320. sin6->sin6_port = htons(9);
  1321. sock = tor_open_socket(PF_INET6,SOCK_DGRAM,IPPROTO_UDP);
  1322. addr_len = (socklen_t)sizeof(struct sockaddr_in6);
  1323. sin6->sin6_family = AF_INET6;
  1324. S6_ADDR16(sin6->sin6_addr)[0] = htons(0x2002); /* 2002:: */
  1325. } else if (family == AF_INET) {
  1326. struct sockaddr_in *sin = (struct sockaddr_in*)&target_addr;
  1327. /* Use the "discard" service port */
  1328. sin->sin_port = htons(9);
  1329. sock = tor_open_socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
  1330. addr_len = (socklen_t)sizeof(struct sockaddr_in);
  1331. sin->sin_family = AF_INET;
  1332. sin->sin_addr.s_addr = htonl(0x12000001); /* 18.0.0.1 */
  1333. } else {
  1334. return -1;
  1335. }
  1336. if (sock < 0) {
  1337. int e = tor_socket_errno(-1);
  1338. log_fn(severity, LD_NET, "unable to create socket: %s",
  1339. tor_socket_strerror(e));
  1340. goto err;
  1341. }
  1342. if (connect(sock,(struct sockaddr *)&target_addr, addr_len) < 0) {
  1343. int e = tor_socket_errno(sock);
  1344. log_fn(severity, LD_NET, "connect() failed: %s", tor_socket_strerror(e));
  1345. goto err;
  1346. }
  1347. if (getsockname(sock,(struct sockaddr*)&my_addr, &addr_len)) {
  1348. int e = tor_socket_errno(sock);
  1349. log_fn(severity, LD_NET, "getsockname() to determine interface failed: %s",
  1350. tor_socket_strerror(e));
  1351. goto err;
  1352. }
  1353. tor_addr_from_sockaddr(addr, (struct sockaddr*)&my_addr, NULL);
  1354. r=0;
  1355. err:
  1356. if (sock >= 0)
  1357. tor_close_socket(sock);
  1358. return r;
  1359. }
  1360. /* ======
  1361. * IPv4 helpers
  1362. * XXXX024 IPv6 deprecate some of these.
  1363. */
  1364. /** Given an address of the form "ip:port", try to divide it into its
  1365. * ip and port portions, setting *<b>address_out</b> to a newly
  1366. * allocated string holding the address portion and *<b>port_out</b>
  1367. * to the port.
  1368. *
  1369. * Don't do DNS lookups and don't allow domain names in the "ip" field.
  1370. *
  1371. * If <b>default_port</b> is less than 0, don't accept <b>addrport</b> of the
  1372. * form "ip" or "ip:0". Otherwise, accept those forms, and set
  1373. * *<b>port_out</b> to <b>default_port</b>.
  1374. *
  1375. * Return 0 on success, -1 on failure. */
  1376. int
  1377. tor_addr_port_parse(int severity, const char *addrport,
  1378. tor_addr_t *address_out, uint16_t *port_out,
  1379. int default_port)
  1380. {
  1381. int retval = -1;
  1382. int r;
  1383. char *addr_tmp = NULL;
  1384. tor_assert(addrport);
  1385. tor_assert(address_out);
  1386. tor_assert(port_out);
  1387. r = tor_addr_port_split(severity, addrport, &addr_tmp, port_out);
  1388. if (r < 0)
  1389. goto done;
  1390. if (!*port_out) {
  1391. if (default_port >= 0)
  1392. *port_out = default_port;
  1393. else
  1394. goto done;
  1395. }
  1396. /* make sure that address_out is an IP address */
  1397. if (tor_addr_parse(address_out, addr_tmp) < 0)
  1398. goto done;
  1399. retval = 0;
  1400. done:
  1401. tor_free(addr_tmp);
  1402. return retval;
  1403. }
  1404. /** Given an address of the form "host[:port]", try to divide it into its host
  1405. * ane port portions, setting *<b>address_out</b> to a newly allocated string
  1406. * holding the address portion and *<b>port_out</b> to the port (or 0 if no
  1407. * port is given). Return 0 on success, -1 on failure. */
  1408. int
  1409. tor_addr_port_split(int severity, const char *addrport,
  1410. char **address_out, uint16_t *port_out)
  1411. {
  1412. tor_addr_t a_tmp;
  1413. tor_assert(addrport);
  1414. tor_assert(address_out);
  1415. tor_assert(port_out);
  1416. /* We need to check for IPv6 manually because addr_port_lookup() doesn't
  1417. * do a good job on IPv6 addresses that lack a port. */
  1418. if (tor_addr_parse(&a_tmp, addrport) == AF_INET6) {
  1419. *port_out = 0;
  1420. *address_out = tor_strdup(addrport);
  1421. return 0;
  1422. }
  1423. return addr_port_lookup(severity, addrport, address_out, NULL, port_out);
  1424. }
  1425. /** Parse a string of the form "host[:port]" from <b>addrport</b>. If
  1426. * <b>address</b> is provided, set *<b>address</b> to a copy of the
  1427. * host portion of the string. If <b>addr</b> is provided, try to
  1428. * resolve the host portion of the string and store it into
  1429. * *<b>addr</b> (in host byte order). If <b>port_out</b> is provided,
  1430. * store the port number into *<b>port_out</b>, or 0 if no port is given.
  1431. * If <b>port_out</b> is NULL, then there must be no port number in
  1432. * <b>addrport</b>.
  1433. * Return 0 on success, -1 on failure.
  1434. */
  1435. int
  1436. addr_port_lookup(int severity, const char *addrport, char **address,
  1437. uint32_t *addr, uint16_t *port_out)
  1438. {
  1439. const char *colon;
  1440. char *address_ = NULL;
  1441. int port_;
  1442. int ok = 1;
  1443. tor_assert(addrport);
  1444. colon = strrchr(addrport, ':');
  1445. if (colon) {
  1446. address_ = tor_strndup(addrport, colon-addrport);
  1447. port_ = (int) tor_parse_long(colon+1,10,1,65535,NULL,NULL);
  1448. if (!port_) {
  1449. log_fn(severity, LD_GENERAL, "Port %s out of range", escaped(colon+1));
  1450. ok = 0;
  1451. }
  1452. if (!port_out) {
  1453. char *esc_addrport = esc_for_log(addrport);
  1454. log_fn(severity, LD_GENERAL,
  1455. "Port %s given on %s when not required",
  1456. escaped(colon+1), esc_addrport);
  1457. tor_free(esc_addrport);
  1458. ok = 0;
  1459. }
  1460. } else {
  1461. address_ = tor_strdup(addrport);
  1462. port_ = 0;
  1463. }
  1464. if (addr) {
  1465. /* There's an addr pointer, so we need to resolve the hostname. */
  1466. if (tor_lookup_hostname(address_,addr)) {
  1467. log_fn(severity, LD_NET, "Couldn't look up %s", escaped(address_));
  1468. ok = 0;
  1469. *addr = 0;
  1470. }
  1471. }
  1472. if (address && ok) {
  1473. *address = address_;
  1474. } else {
  1475. if (address)
  1476. *address = NULL;
  1477. tor_free(address_);
  1478. }
  1479. if (port_out)
  1480. *port_out = ok ? ((uint16_t) port_) : 0;
  1481. return ok ? 0 : -1;
  1482. }
  1483. /** If <b>mask</b> is an address mask for a bit-prefix, return the number of
  1484. * bits. Otherwise, return -1. */
  1485. int
  1486. addr_mask_get_bits(uint32_t mask)
  1487. {
  1488. int i;
  1489. if (mask == 0)
  1490. return 0;
  1491. if (mask == 0xFFFFFFFFu)
  1492. return 32;
  1493. for (i=1; i<=32; ++i) {
  1494. if (mask == (uint32_t) ~((1u<<(32-i))-1)) {
  1495. return i;
  1496. }
  1497. }
  1498. return -1;
  1499. }
  1500. /** Parse a string <b>s</b> in the format of (*|port(-maxport)?)?, setting the
  1501. * various *out pointers as appropriate. Return 0 on success, -1 on failure.
  1502. */
  1503. int
  1504. parse_port_range(const char *port, uint16_t *port_min_out,
  1505. uint16_t *port_max_out)
  1506. {
  1507. int port_min, port_max, ok;
  1508. tor_assert(port_min_out);
  1509. tor_assert(port_max_out);
  1510. if (!port || *port == '\0' || strcmp(port, "*") == 0) {
  1511. port_min = 1;
  1512. port_max = 65535;
  1513. } else {
  1514. char *endptr = NULL;
  1515. port_min = (int)tor_parse_long(port, 10, 0, 65535, &ok, &endptr);
  1516. if (!ok) {
  1517. log_warn(LD_GENERAL,
  1518. "Malformed port %s on address range; rejecting.",
  1519. escaped(port));
  1520. return -1;
  1521. } else if (endptr && *endptr == '-') {
  1522. port = endptr+1;
  1523. endptr = NULL;
  1524. port_max = (int)tor_parse_long(port, 10, 1, 65535, &ok, &endptr);
  1525. if (!ok) {
  1526. log_warn(LD_GENERAL,
  1527. "Malformed port %s on address range; rejecting.",
  1528. escaped(port));
  1529. return -1;
  1530. }
  1531. } else {
  1532. port_max = port_min;
  1533. }
  1534. if (port_min > port_max) {
  1535. log_warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
  1536. return -1;
  1537. }
  1538. }
  1539. if (port_min < 1)
  1540. port_min = 1;
  1541. if (port_max > 65535)
  1542. port_max = 65535;
  1543. *port_min_out = (uint16_t) port_min;
  1544. *port_max_out = (uint16_t) port_max;
  1545. return 0;
  1546. }
  1547. /** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual),
  1548. * write it as a string into the <b>buf_len</b>-byte buffer in
  1549. * <b>buf</b>.
  1550. */
  1551. int
  1552. tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len)
  1553. {
  1554. uint32_t a = ntohl(in->s_addr);
  1555. return tor_snprintf(buf, buf_len, "%d.%d.%d.%d",
  1556. (int)(uint8_t)((a>>24)&0xff),
  1557. (int)(uint8_t)((a>>16)&0xff),
  1558. (int)(uint8_t)((a>>8 )&0xff),
  1559. (int)(uint8_t)((a )&0xff));
  1560. }
  1561. /** Given a host-order <b>addr</b>, call tor_inet_ntop() on it
  1562. * and return a strdup of the resulting address.
  1563. */
  1564. char *
  1565. tor_dup_ip(uint32_t addr)
  1566. {
  1567. char buf[TOR_ADDR_BUF_LEN];
  1568. struct in_addr in;
  1569. in.s_addr = htonl(addr);
  1570. tor_inet_ntop(AF_INET, &in, buf, sizeof(buf));
  1571. return tor_strdup(buf);
  1572. }
  1573. /**
  1574. * Set *<b>addr</b> to the host-order IPv4 address (if any) of whatever
  1575. * interface connects to the Internet. This address should only be used in
  1576. * checking whether our address has changed. Return 0 on success, -1 on
  1577. * failure.
  1578. */
  1579. MOCK_IMPL(int,
  1580. get_interface_address,(int severity, uint32_t *addr))
  1581. {
  1582. tor_addr_t local_addr;
  1583. int r;
  1584. r = get_interface_address6(severity, AF_INET, &local_addr);
  1585. if (r>=0)
  1586. *addr = tor_addr_to_ipv4h(&local_addr);
  1587. return r;
  1588. }
  1589. /** Return true if we can tell that <b>name</b> is a canonical name for the
  1590. * loopback address. */
  1591. int
  1592. tor_addr_hostname_is_local(const char *name)
  1593. {
  1594. return !strcasecmp(name, "localhost") ||
  1595. !strcasecmp(name, "local") ||
  1596. !strcasecmpend(name, ".local");
  1597. }
  1598. /** Return a newly allocated tor_addr_port_t with <b>addr</b> and
  1599. <b>port</b> filled in. */
  1600. tor_addr_port_t *
  1601. tor_addr_port_new(const tor_addr_t *addr, uint16_t port)
  1602. {
  1603. tor_addr_port_t *ap = tor_malloc_zero(sizeof(tor_addr_port_t));
  1604. if (addr)
  1605. tor_addr_copy(&ap->addr, addr);
  1606. ap->port = port;
  1607. return ap;
  1608. }