address.c 50 KB

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