address.c 50 KB

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