Fix a compile warning when using clang
[tor/rransom.git] / src / common / address.c
blob82a709601f8bbdd20bf3b2e0f1791a6540e1429a
1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file address.c
8 * \brief Functions to use and manipulate the tor_addr_t structure.
9 **/
11 #include "orconfig.h"
12 #include "compat.h"
13 #include "util.h"
14 #include "address.h"
15 #include "log.h"
17 #ifdef MS_WINDOWS
18 #include <process.h>
19 #include <windows.h>
20 #endif
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
33 #endif
34 #ifdef HAVE_ARPA_INET_H
35 #include <arpa/inet.h>
36 #endif
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
39 #endif
40 #ifdef HAVE_NETDB_H
41 #include <netdb.h>
42 #endif
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h> /* FreeBSD needs this to know what version it is */
45 #endif
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <assert.h>
52 /** Convert the tor_addr_t in <b>a</b>, with port in <b>port</b>, into a
53 * socklen object in *<b>sa_out</b> of object size <b>len</b>. If not enough
54 * room is free, or on error, return -1. Else return the length of the
55 * sockaddr. */
56 /* XXXX021 This returns socklen_t. socklen_t is sometimes unsigned. This
57 * function claims to return -1 sometimes. Problematic! */
58 socklen_t
59 tor_addr_to_sockaddr(const tor_addr_t *a,
60 uint16_t port,
61 struct sockaddr *sa_out,
62 socklen_t len)
64 sa_family_t family = tor_addr_family(a);
65 if (family == AF_INET) {
66 struct sockaddr_in *sin;
67 if (len < (int)sizeof(struct sockaddr_in))
68 return -1;
69 sin = (struct sockaddr_in *)sa_out;
70 memset(sin, 0, sizeof(struct sockaddr_in));
71 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
72 sin->sin_len = sizeof(struct sockaddr_in);
73 #endif
74 sin->sin_family = AF_INET;
75 sin->sin_port = htons(port);
76 sin->sin_addr.s_addr = tor_addr_to_ipv4n(a);
77 return sizeof(struct sockaddr_in);
78 } else if (family == AF_INET6) {
79 struct sockaddr_in6 *sin6;
80 if (len < (int)sizeof(struct sockaddr_in6))
81 return -1;
82 sin6 = (struct sockaddr_in6 *)sa_out;
83 memset(sin6, 0, sizeof(struct sockaddr_in6));
84 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
85 sin6->sin6_len = sizeof(struct sockaddr_in6);
86 #endif
87 sin6->sin6_family = AF_INET6;
88 sin6->sin6_port = htons(port);
89 memcpy(&sin6->sin6_addr, tor_addr_to_in6(a), sizeof(struct in6_addr));
90 return sizeof(struct sockaddr_in6);
91 } else {
92 return -1;
96 /** Set the tor_addr_t in <b>a</b> to contain the socket address contained in
97 * <b>sa</b>. */
98 int
99 tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
100 uint16_t *port_out)
102 tor_assert(a);
103 tor_assert(sa);
104 if (sa->sa_family == AF_INET) {
105 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
106 tor_addr_from_ipv4n(a, sin->sin_addr.s_addr);
107 if (port_out)
108 *port_out = ntohs(sin->sin_port);
109 } else if (sa->sa_family == AF_INET6) {
110 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
111 tor_addr_from_in6(a, &sin6->sin6_addr);
112 if (port_out)
113 *port_out = ntohs(sin6->sin6_port);
114 } else {
115 tor_addr_make_unspec(a);
116 return -1;
118 return 0;
121 /** Set address <b>a</b> to the unspecified address. This address belongs to
122 * no family. */
123 void
124 tor_addr_make_unspec(tor_addr_t *a)
126 memset(a, 0, sizeof(*a));
127 a->family = AF_UNSPEC;
130 /** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
131 * *<b>addr</b> to the proper IP address and family. The <b>family</b>
132 * argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a
133 * <i>preferred</i> family, though another one may be returned if only one
134 * family is implemented for this address.
136 * Return 0 on success, -1 on failure; 1 on transient failure.
139 tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
141 /* Perhaps eventually this should be replaced by a tor_getaddrinfo or
142 * something.
144 struct in_addr iaddr;
145 struct in6_addr iaddr6;
146 tor_assert(name);
147 tor_assert(addr);
148 tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);
149 if (!*name) {
150 /* Empty address is an error. */
151 return -1;
152 } else if (tor_inet_pton(AF_INET, name, &iaddr)) {
153 /* It's an IPv4 IP. */
154 if (family == AF_INET6)
155 return -1;
156 tor_addr_from_in(addr, &iaddr);
157 return 0;
158 } else if (tor_inet_pton(AF_INET6, name, &iaddr6)) {
159 if (family == AF_INET)
160 return -1;
161 tor_addr_from_in6(addr, &iaddr6);
162 return 0;
163 } else {
164 #ifdef HAVE_GETADDRINFO
165 int err;
166 struct addrinfo *res=NULL, *res_p;
167 struct addrinfo *best=NULL;
168 struct addrinfo hints;
169 int result = -1;
170 memset(&hints, 0, sizeof(hints));
171 hints.ai_family = family;
172 hints.ai_socktype = SOCK_STREAM;
173 err = getaddrinfo(name, NULL, &hints, &res);
174 if (!err) {
175 best = NULL;
176 for (res_p = res; res_p; res_p = res_p->ai_next) {
177 if (family == AF_UNSPEC) {
178 if (res_p->ai_family == AF_INET) {
179 best = res_p;
180 break;
181 } else if (res_p->ai_family == AF_INET6 && !best) {
182 best = res_p;
184 } else if (family == res_p->ai_family) {
185 best = res_p;
186 break;
189 if (!best)
190 best = res;
191 if (best->ai_family == AF_INET) {
192 tor_addr_from_in(addr,
193 &((struct sockaddr_in*)best->ai_addr)->sin_addr);
194 result = 0;
195 } else if (best->ai_family == AF_INET6) {
196 tor_addr_from_in6(addr,
197 &((struct sockaddr_in6*)best->ai_addr)->sin6_addr);
198 result = 0;
200 freeaddrinfo(res);
201 return result;
203 return (err == EAI_AGAIN) ? 1 : -1;
204 #else
205 struct hostent *ent;
206 int err;
207 #ifdef HAVE_GETHOSTBYNAME_R_6_ARG
208 char buf[2048];
209 struct hostent hostent;
210 int r;
211 r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err);
212 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
213 char buf[2048];
214 struct hostent hostent;
215 ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err);
216 #elif defined(HAVE_GETHOSTBYNAME_R_3_ARG)
217 struct hostent_data data;
218 struct hostent hent;
219 memset(&data, 0, sizeof(data));
220 err = gethostbyname_r(name, &hent, &data);
221 ent = err ? NULL : &hent;
222 #else
223 ent = gethostbyname(name);
224 #ifdef MS_WINDOWS
225 err = WSAGetLastError();
226 #else
227 err = h_errno;
228 #endif
229 #endif /* endif HAVE_GETHOSTBYNAME_R_6_ARG. */
230 if (ent) {
231 if (ent->h_addrtype == AF_INET) {
232 tor_addr_from_in(addr, (struct in_addr*) ent->h_addr);
233 } else if (ent->h_addrtype == AF_INET6) {
234 tor_addr_from_in6(addr, (struct in6_addr*) ent->h_addr);
235 } else {
236 tor_assert(0); /* gethostbyname() returned a bizarre addrtype */
238 return 0;
240 #ifdef MS_WINDOWS
241 return (err == WSATRY_AGAIN) ? 1 : -1;
242 #else
243 return (err == TRY_AGAIN) ? 1 : -1;
244 #endif
245 #endif
249 /** Return true iff <b>ip</b> is an IP reserved to localhost or local networks
250 * in RFC1918 or RFC4193 or RFC4291. (fec0::/10, deprecated by RFC3879, is
251 * also treated as internal for now.)
254 tor_addr_is_internal(const tor_addr_t *addr, int for_listening)
256 uint32_t iph4 = 0;
257 uint32_t iph6[4];
258 sa_family_t v_family;
259 v_family = tor_addr_family(addr);
261 if (v_family == AF_INET) {
262 iph4 = tor_addr_to_ipv4h(addr);
263 } else if (v_family == AF_INET6) {
264 if (tor_addr_is_v4(addr)) { /* v4-mapped */
265 v_family = AF_INET;
266 iph4 = ntohl(tor_addr_to_in6_addr32(addr)[3]);
270 if (v_family == AF_INET6) {
271 const uint32_t *a32 = tor_addr_to_in6_addr32(addr);
272 iph6[0] = ntohl(a32[0]);
273 iph6[1] = ntohl(a32[1]);
274 iph6[2] = ntohl(a32[2]);
275 iph6[3] = ntohl(a32[3]);
276 if (for_listening && !iph6[0] && !iph6[1] && !iph6[2] && !iph6[3]) /* :: */
277 return 0;
279 if (((iph6[0] & 0xfe000000) == 0xfc000000) || /* fc00/7 - RFC4193 */
280 ((iph6[0] & 0xffc00000) == 0xfe800000) || /* fe80/10 - RFC4291 */
281 ((iph6[0] & 0xffc00000) == 0xfec00000)) /* fec0/10 D- RFC3879 */
282 return 1;
284 if (!iph6[0] && !iph6[1] && !iph6[2] &&
285 ((iph6[3] & 0xfffffffe) == 0x00000000)) /* ::/127 */
286 return 1;
288 return 0;
289 } else if (v_family == AF_INET) {
290 if (for_listening && !iph4) /* special case for binding to 0.0.0.0 */
291 return 0;
292 if (((iph4 & 0xff000000) == 0x0a000000) || /* 10/8 */
293 ((iph4 & 0xff000000) == 0x00000000) || /* 0/8 */
294 ((iph4 & 0xff000000) == 0x7f000000) || /* 127/8 */
295 ((iph4 & 0xffff0000) == 0xa9fe0000) || /* 169.254/16 */
296 ((iph4 & 0xfff00000) == 0xac100000) || /* 172.16/12 */
297 ((iph4 & 0xffff0000) == 0xc0a80000)) /* 192.168/16 */
298 return 1;
299 return 0;
302 /* unknown address family... assume it's not safe for external use */
303 /* rather than tor_assert(0) */
304 log_warn(LD_BUG, "tor_addr_is_internal() called with a non-IP address.");
305 return 1;
308 /** Convert a tor_addr_t <b>addr</b> into a string, and store it in
309 * <b>dest</b> of size <b>len</b>. Returns a pointer to dest on success,
310 * or NULL on failure. If <b>decorate</b>, surround IPv6 addresses with
311 * brackets.
313 const char *
314 tor_addr_to_str(char *dest, const tor_addr_t *addr, int len, int decorate)
316 const char *ptr;
317 tor_assert(addr && dest);
319 switch (tor_addr_family(addr)) {
320 case AF_INET:
321 if (len<3)
322 return NULL;
323 ptr = tor_inet_ntop(AF_INET, &addr->addr.in_addr, dest, len);
324 break;
325 case AF_INET6:
326 if (decorate)
327 ptr = tor_inet_ntop(AF_INET6, &addr->addr.in6_addr, dest+1, len-2);
328 else
329 ptr = tor_inet_ntop(AF_INET6, &addr->addr.in6_addr, dest, len);
330 if (ptr && decorate) {
331 *dest = '[';
332 memcpy(dest+strlen(dest), "]", 2);
333 tor_assert(ptr == dest+1);
334 ptr = dest;
336 break;
337 default:
338 return NULL;
340 return ptr;
343 /** Parse an .in-addr.arpa or .ip6.arpa address from <b>address</b>. Return 0
344 * if this is not an .in-addr.arpa address or an .ip6.arpa address. Return -1
345 * if this is an ill-formed .in-addr.arpa address or an .ip6.arpa address.
346 * Also return -1 if <b>family</b> is not AF_UNSPEC, and the parsed address
347 * family does not match <b>family</b>. On success, return 1, and store the
348 * result, if any, into <b>result</b>, if provided.
350 * If <b>accept_regular</b> is set and the address is in neither recognized
351 * reverse lookup hostname format, try parsing the address as a regular
352 * IPv4 or IPv6 address too.
355 tor_addr_parse_reverse_lookup_name(tor_addr_t *result, const char *address,
356 int family, int accept_regular)
358 if (!strcasecmpend(address, ".in-addr.arpa")) {
359 /* We have an in-addr.arpa address. */
360 char buf[INET_NTOA_BUF_LEN];
361 size_t len;
362 struct in_addr inaddr;
363 if (family == AF_INET6)
364 return -1;
366 len = strlen(address) - strlen(".in-addr.arpa");
367 if (len >= INET_NTOA_BUF_LEN)
368 return -1; /* Too long. */
370 memcpy(buf, address, len);
371 buf[len] = '\0';
372 if (tor_inet_aton(buf, &inaddr) == 0)
373 return -1; /* malformed. */
375 /* reverse the bytes */
376 inaddr.s_addr = (uint32_t)
377 (((inaddr.s_addr & 0x000000ff) << 24)
378 |((inaddr.s_addr & 0x0000ff00) << 8)
379 |((inaddr.s_addr & 0x00ff0000) >> 8)
380 |((inaddr.s_addr & 0xff000000) >> 24));
382 if (result) {
383 tor_addr_from_in(result, &inaddr);
385 return 1;
388 if (!strcasecmpend(address, ".ip6.arpa")) {
389 const char *cp;
390 int i;
391 int n0, n1;
392 struct in6_addr in6;
394 if (family == AF_INET)
395 return -1;
397 cp = address;
398 for (i = 0; i < 16; ++i) {
399 n0 = hex_decode_digit(*cp++); /* The low-order nybble appears first. */
400 if (*cp++ != '.') return -1; /* Then a dot. */
401 n1 = hex_decode_digit(*cp++); /* The high-order nybble appears first. */
402 if (*cp++ != '.') return -1; /* Then another dot. */
403 if (n0<0 || n1 < 0) /* Both nybbles must be hex. */
404 return -1;
406 /* We don't check the length of the string in here. But that's okay,
407 * since we already know that the string ends with ".ip6.arpa", and
408 * there is no way to frameshift .ip6.arpa so it fits into the pattern
409 * of hexdigit, period, hexdigit, period that we enforce above.
412 /* Assign from low-byte to high-byte. */
413 in6.s6_addr[15-i] = n0 | (n1 << 4);
415 if (strcasecmp(cp, "ip6.arpa"))
416 return -1;
418 if (result) {
419 tor_addr_from_in6(result, &in6);
421 return 1;
424 if (accept_regular) {
425 tor_addr_t tmp;
426 int r = tor_addr_from_str(&tmp, address);
427 if (r < 0)
428 return 0;
429 if (r != family && family != AF_UNSPEC)
430 return -1;
432 if (result)
433 memcpy(result, &tmp, sizeof(tor_addr_t));
435 return 1;
438 return 0;
441 /** Convert <b>addr</b> to an in-addr.arpa name or a .ip6.arpa name, and store
442 * the result in the <b>outlen</b>-byte buffer at <b>out</b>. Return 0 on
443 * success, -1 on failure. */
445 tor_addr_to_reverse_lookup_name(char *out, size_t outlen,
446 const tor_addr_t *addr)
448 if (addr->family == AF_INET) {
449 uint32_t a = tor_addr_to_ipv4h(addr);
451 return tor_snprintf(out, outlen, "%d.%d.%d.%d.in-addr.arpa",
452 (int)(uint8_t)((a )&0xff),
453 (int)(uint8_t)((a>>8 )&0xff),
454 (int)(uint8_t)((a>>16)&0xff),
455 (int)(uint8_t)((a>>24)&0xff));
456 } else if (addr->family == AF_INET6) {
457 int i;
458 char *cp = out;
459 const uint8_t *bytes = tor_addr_to_in6_addr8(addr);
460 if (outlen < REVERSE_LOOKUP_NAME_BUF_LEN)
461 return -1;
462 for (i = 15; i >= 0; --i) {
463 uint8_t byte = bytes[i];
464 *cp++ = "0123456789abcdef"[byte & 0x0f];
465 *cp++ = '.';
466 *cp++ = "0123456789abcdef"[byte >> 4];
467 *cp++ = '.';
469 memcpy(cp, "ip6.arpa", 9); /* 8 characters plus NUL */
470 return 0;
472 return -1;
475 /** Parse a string <b>s</b> containing an IPv4/IPv6 address, and possibly
476 * a mask and port or port range. Store the parsed address in
477 * <b>addr_out</b>, a mask (if any) in <b>mask_out</b>, and port(s) (if any)
478 * in <b>port_min_out</b> and <b>port_max_out</b>.
480 * The syntax is:
481 * Address OptMask OptPortRange
482 * Address ::= IPv4Address / "[" IPv6Address "]" / "*"
483 * OptMask ::= "/" Integer /
484 * OptPortRange ::= ":*" / ":" Integer / ":" Integer "-" Integer /
486 * - If mask, minport, or maxport are NULL, we do not want these
487 * options to be set; treat them as an error if present.
488 * - If the string has no mask, the mask is set to /32 (IPv4) or /128 (IPv6).
489 * - If the string has one port, it is placed in both min and max port
490 * variables.
491 * - If the string has no port(s), port_(min|max)_out are set to 1 and 65535.
493 * Return an address family on success, or -1 if an invalid address string is
494 * provided.
497 tor_addr_parse_mask_ports(const char *s, tor_addr_t *addr_out,
498 maskbits_t *maskbits_out,
499 uint16_t *port_min_out, uint16_t *port_max_out)
501 char *base = NULL, *address, *mask = NULL, *port = NULL, *rbracket = NULL;
502 char *endptr;
503 int any_flag=0, v4map=0;
504 sa_family_t family;
505 struct in6_addr in6_tmp;
506 struct in_addr in_tmp;
508 tor_assert(s);
509 tor_assert(addr_out);
511 /* IP, [], /mask, ports */
512 #define MAX_ADDRESS_LENGTH (TOR_ADDR_BUF_LEN+2+(1+INET_NTOA_BUF_LEN)+12+1)
514 if (strlen(s) > MAX_ADDRESS_LENGTH) {
515 log_warn(LD_GENERAL, "Impossibly long IP %s; rejecting", escaped(s));
516 goto err;
518 base = tor_strdup(s);
520 /* Break 'base' into separate strings. */
521 address = base;
522 if (*address == '[') { /* Probably IPv6 */
523 address++;
524 rbracket = strchr(address, ']');
525 if (!rbracket) {
526 log_warn(LD_GENERAL,
527 "No closing IPv6 bracket in address pattern; rejecting.");
528 goto err;
531 mask = strchr((rbracket?rbracket:address),'/');
532 port = strchr((mask?mask:(rbracket?rbracket:address)), ':');
533 if (port)
534 *port++ = '\0';
535 if (mask)
536 *mask++ = '\0';
537 if (rbracket)
538 *rbracket = '\0';
539 if (port && mask)
540 tor_assert(port > mask);
541 if (mask && rbracket)
542 tor_assert(mask > rbracket);
544 /* Now "address" is the a.b.c.d|'*'|abcd::1 part...
545 * "mask" is the Mask|Maskbits part...
546 * and "port" is the *|port|min-max part.
549 /* Process the address portion */
550 memset(addr_out, 0, sizeof(tor_addr_t));
552 if (!strcmp(address, "*")) {
553 family = AF_INET; /* AF_UNSPEC ???? XXXX_IP6 */
554 tor_addr_from_ipv4h(addr_out, 0);
555 any_flag = 1;
556 } else if (tor_inet_pton(AF_INET6, address, &in6_tmp) > 0) {
557 family = AF_INET6;
558 tor_addr_from_in6(addr_out, &in6_tmp);
559 } else if (tor_inet_pton(AF_INET, address, &in_tmp) > 0) {
560 family = AF_INET;
561 tor_addr_from_in(addr_out, &in_tmp);
562 } else {
563 log_warn(LD_GENERAL, "Malformed IP %s in address pattern; rejecting.",
564 escaped(address));
565 goto err;
568 v4map = tor_addr_is_v4(addr_out);
570 /* Parse mask */
571 if (maskbits_out) {
572 int bits = 0;
573 struct in_addr v4mask;
575 if (mask) { /* the caller (tried to) specify a mask */
576 bits = (int) strtol(mask, &endptr, 10);
577 if (!*endptr) { /* strtol converted everything, so it was an integer */
578 if ((bits<0 || bits>128) ||
579 (family == AF_INET && bits > 32)) {
580 log_warn(LD_GENERAL,
581 "Bad number of mask bits (%d) on address range; rejecting.",
582 bits);
583 goto err;
585 } else { /* mask might still be an address-style mask */
586 if (tor_inet_pton(AF_INET, mask, &v4mask) > 0) {
587 bits = addr_mask_get_bits(ntohl(v4mask.s_addr));
588 if (bits < 0) {
589 log_warn(LD_GENERAL,
590 "IPv4-style mask %s is not a prefix address; rejecting.",
591 escaped(mask));
592 goto err;
594 } else { /* Not IPv4; we don't do address-style IPv6 masks. */
595 log_warn(LD_GENERAL,
596 "Malformed mask on address range %s; rejecting.",
597 escaped(s));
598 goto err;
601 if (family == AF_INET6 && v4map) {
602 if (bits > 32 && bits < 96) { /* Crazy */
603 log_warn(LD_GENERAL,
604 "Bad mask bits %i for V4-mapped V6 address; rejecting.",
605 bits);
606 goto err;
608 /* XXXX_IP6 is this really what we want? */
609 bits = 96 + bits%32; /* map v4-mapped masks onto 96-128 bits */
611 } else { /* pick an appropriate mask, as none was given */
612 if (any_flag)
613 bits = 0; /* This is okay whether it's V6 or V4 (FIX V4-mapped V6!) */
614 else if (tor_addr_family(addr_out) == AF_INET)
615 bits = 32;
616 else if (tor_addr_family(addr_out) == AF_INET6)
617 bits = 128;
619 *maskbits_out = (maskbits_t) bits;
620 } else {
621 if (mask) {
622 log_warn(LD_GENERAL,
623 "Unexpected mask in address %s; rejecting", escaped(s));
624 goto err;
628 /* Parse port(s) */
629 if (port_min_out) {
630 uint16_t port2;
631 if (!port_max_out) /* caller specified one port; fake the second one */
632 port_max_out = &port2;
634 if (parse_port_range(port, port_min_out, port_max_out) < 0) {
635 goto err;
636 } else if ((*port_min_out != *port_max_out) && port_max_out == &port2) {
637 log_warn(LD_GENERAL,
638 "Wanted one port from address range, but there are two.");
640 port_max_out = NULL; /* caller specified one port, so set this back */
641 goto err;
643 } else {
644 if (port) {
645 log_warn(LD_GENERAL,
646 "Unexpected ports in address %s; rejecting", escaped(s));
647 goto err;
651 tor_free(base);
652 return tor_addr_family(addr_out);
653 err:
654 tor_free(base);
655 return -1;
658 /** Determine whether an address is IPv4, either native or IPv4-mapped IPv6.
659 * Note that this is about representation only, as any decent stack will
660 * reject IPv4-mapped addresses received on the wire (and won't use them
661 * on the wire either).
664 tor_addr_is_v4(const tor_addr_t *addr)
666 tor_assert(addr);
668 if (tor_addr_family(addr) == AF_INET)
669 return 1;
671 if (tor_addr_family(addr) == AF_INET6) {
672 /* First two don't need to be ordered */
673 uint32_t *a32 = tor_addr_to_in6_addr32(addr);
674 if (a32[0] == 0 && a32[1] == 0 && ntohl(a32[2]) == 0x0000ffffu)
675 return 1;
678 return 0; /* Not IPv4 - unknown family or a full-blood IPv6 address */
681 /** Determine whether an address <b>addr</b> is null, either all zeroes or
682 * belonging to family AF_UNSPEC.
685 tor_addr_is_null(const tor_addr_t *addr)
687 tor_assert(addr);
689 switch (tor_addr_family(addr)) {
690 case AF_INET6: {
691 uint32_t *a32 = tor_addr_to_in6_addr32(addr);
692 return (a32[0] == 0) && (a32[1] == 0) && (a32[2] == 0) && (a32[3] == 0);
694 case AF_INET:
695 return (tor_addr_to_ipv4n(addr) == 0);
696 case AF_UNSPEC:
697 return 1;
698 default:
699 log_warn(LD_BUG, "Called with unknown address family %d",
700 (int)tor_addr_family(addr));
701 return 0;
703 //return 1;
706 /** Return true iff <b>addr</b> is a loopback address */
708 tor_addr_is_loopback(const tor_addr_t *addr)
710 tor_assert(addr);
711 switch (tor_addr_family(addr)) {
712 case AF_INET6: {
713 /* ::1 */
714 uint32_t *a32 = tor_addr_to_in6_addr32(addr);
715 return (a32[0] == 0) && (a32[1] == 0) && (a32[2] == 0) && (a32[3] == 1);
717 case AF_INET:
718 /* 127.0.0.1 */
719 return (tor_addr_to_ipv4h(addr) & 0xff000000) == 0x7f000000;
720 case AF_UNSPEC:
721 return 0;
722 default:
723 tor_fragile_assert();
724 return 0;
728 /** Set <b>dest</b> to equal the IPv4 address in <b>v4addr</b> (given in
729 * network order. */
730 void
731 tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr)
733 tor_assert(dest);
734 memset(dest, 0, sizeof(tor_addr_t));
735 dest->family = AF_INET;
736 dest->addr.in_addr.s_addr = v4addr;
739 /** Set <b>dest</b> to equal the IPv6 address in the 16 bytes at
740 * <b>ipv6_bytes</b>. */
741 void
742 tor_addr_from_ipv6_bytes(tor_addr_t *dest, const char *ipv6_bytes)
744 tor_assert(dest);
745 tor_assert(ipv6_bytes);
746 memset(dest, 0, sizeof(tor_addr_t));
747 dest->family = AF_INET6;
748 memcpy(dest->addr.in6_addr.s6_addr, ipv6_bytes, 16);
751 /** Set <b>dest</b> equal to the IPv6 address in the in6_addr <b>in6</b>. */
752 void
753 tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
755 tor_addr_from_ipv6_bytes(dest, (const char*)in6->s6_addr);
758 /** Copy a tor_addr_t from <b>src</b> to <b>dest</b>.
760 void
761 tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
763 tor_assert(src);
764 tor_assert(dest);
765 memcpy(dest, src, sizeof(tor_addr_t));
768 /** Given two addresses <b>addr1</b> and <b>addr2</b>, return 0 if the two
769 * addresses are equivalent under the mask mbits, less than 0 if addr1
770 * precedes addr2, and greater than 0 otherwise.
772 * Different address families (IPv4 vs IPv6) are always considered unequal if
773 * <b>how</b> is CMP_EXACT; otherwise, IPv6-mapped IPv4 addresses are
774 * considered equivalent to their IPv4 equivalents.
777 tor_addr_compare(const tor_addr_t *addr1, const tor_addr_t *addr2,
778 tor_addr_comparison_t how)
780 return tor_addr_compare_masked(addr1, addr2, 128, how);
783 /** As tor_addr_compare(), but only looks at the first <b>mask</b> bits of
784 * the address.
786 * Reduce over-specific masks (>128 for ipv6, >32 for ipv4) to 128 or 32.
788 * The mask is interpreted relative to <b>addr1</b>, so that if a is
789 * ::ffff:1.2.3.4, and b is 3.4.5.6,
790 * tor_addr_compare_masked(a,b,100,CMP_SEMANTIC) is the same as
791 * -tor_addr_compare_masked(b,a,4,CMP_SEMANTIC).
793 * We guarantee that the ordering from tor_addr_compare_masked is a total
794 * order on addresses, but not that it is any particular order, or that it
795 * will be the same from one version to the next.
798 tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
799 maskbits_t mbits, tor_addr_comparison_t how)
801 #define TRISTATE(a,b) (((a)<(b))?-1: (((a)==(b))?0:1))
802 sa_family_t family1, family2, v_family1, v_family2;
804 tor_assert(addr1 && addr2);
806 v_family1 = family1 = tor_addr_family(addr1);
807 v_family2 = family2 = tor_addr_family(addr2);
809 if (family1==family2) {
810 /* When the families are the same, there's only one way to do the
811 * comparison: exactly. */
812 int r;
813 switch (family1) {
814 case AF_UNSPEC:
815 return 0; /* All unspecified addresses are equal */
816 case AF_INET: {
817 uint32_t a1 = tor_addr_to_ipv4h(addr1);
818 uint32_t a2 = tor_addr_to_ipv4h(addr2);
819 if (mbits <= 0)
820 return 0;
821 if (mbits > 32)
822 mbits = 32;
823 a1 >>= (32-mbits);
824 a2 >>= (32-mbits);
825 r = TRISTATE(a1, a2);
826 return r;
828 case AF_INET6: {
829 const uint8_t *a1 = tor_addr_to_in6_addr8(addr1);
830 const uint8_t *a2 = tor_addr_to_in6_addr8(addr2);
831 const int bytes = mbits >> 3;
832 const int leftover_bits = mbits & 7;
833 if (bytes && (r = memcmp(a1, a2, bytes))) {
834 return r;
835 } else if (leftover_bits) {
836 uint8_t b1 = a1[bytes] >> (8-leftover_bits);
837 uint8_t b2 = a2[bytes] >> (8-leftover_bits);
838 return TRISTATE(b1, b2);
839 } else {
840 return 0;
843 default:
844 tor_fragile_assert();
845 return 0;
847 } else if (how == CMP_EXACT) {
848 /* Unequal families and an exact comparison? Stop now! */
849 return TRISTATE(family1, family2);
852 if (mbits == 0)
853 return 0;
855 if (family1 == AF_INET6 && tor_addr_is_v4(addr1))
856 v_family1 = AF_INET;
857 if (family2 == AF_INET6 && tor_addr_is_v4(addr2))
858 v_family2 = AF_INET;
859 if (v_family1 == v_family2) {
860 /* One or both addresses are a mapped ipv4 address. */
861 uint32_t a1, a2;
862 if (family1 == AF_INET6) {
863 a1 = tor_addr_to_mapped_ipv4h(addr1);
864 if (mbits <= 96)
865 return 0;
866 mbits -= 96; /* We just decided that the first 96 bits of a1 "match". */
867 } else {
868 a1 = tor_addr_to_ipv4h(addr1);
870 if (family2 == AF_INET6) {
871 a2 = tor_addr_to_mapped_ipv4h(addr2);
872 } else {
873 a2 = tor_addr_to_ipv4h(addr2);
875 if (mbits <= 0) return 0;
876 if (mbits > 32) mbits = 32;
877 a1 >>= (32-mbits);
878 a2 >>= (32-mbits);
879 return TRISTATE(a1, a2);
880 } else {
881 /* Unequal families, and semantic comparison, and no semantic family
882 * matches. */
883 return TRISTATE(family1, family2);
887 /** Return a hash code based on the address addr */
888 unsigned int
889 tor_addr_hash(const tor_addr_t *addr)
891 switch (tor_addr_family(addr)) {
892 case AF_INET:
893 return tor_addr_to_ipv4h(addr);
894 case AF_UNSPEC:
895 return 0x4e4d5342;
896 case AF_INET6: {
897 const uint32_t *u = tor_addr_to_in6_addr32(addr);
898 return u[0] + u[1] + u[2] + u[3];
900 default:
901 tor_fragile_assert();
902 return 0;
906 /** Return a newly allocated string with a representation of <b>addr</b>. */
907 char *
908 tor_dup_addr(const tor_addr_t *addr)
910 char buf[TOR_ADDR_BUF_LEN];
911 tor_addr_to_str(buf, addr, sizeof(buf), 0);
912 return tor_strdup(buf);
915 /** Copy the address in <b>src</b> to <b>dest</b> */
916 void
917 tor_addr_assign(tor_addr_t *dest, const tor_addr_t *src)
919 memcpy(dest, src, sizeof(tor_addr_t));
922 /** Return a string representing the address <b>addr</b>. This string is
923 * statically allocated, and must not be freed. Each call to
924 * <b>fmt_addr</b> invalidates the last result of the function. This
925 * function is not thread-safe. */
926 const char *
927 fmt_addr(const tor_addr_t *addr)
929 static char buf[TOR_ADDR_BUF_LEN];
930 if (!addr) return "<null>";
931 tor_addr_to_str(buf, addr, sizeof(buf), 0);
932 return buf;
935 /** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>. The string
936 * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by
937 * square brackets.
939 * Return an address family on success, or -1 if an invalid address string is
940 * provided. */
942 tor_addr_from_str(tor_addr_t *addr, const char *src)
944 char *tmp = NULL; /* Holds substring if we got a dotted quad. */
945 int result;
946 struct in_addr in_tmp;
947 struct in6_addr in6_tmp;
948 tor_assert(addr && src);
949 if (src[0] == '[' && src[1])
950 src = tmp = tor_strndup(src+1, strlen(src)-2);
952 if (tor_inet_pton(AF_INET6, src, &in6_tmp) > 0) {
953 result = AF_INET6;
954 tor_addr_from_in6(addr, &in6_tmp);
955 } else if (tor_inet_pton(AF_INET, src, &in_tmp) > 0) {
956 result = AF_INET;
957 tor_addr_from_in(addr, &in_tmp);
958 } else {
959 result = -1;
962 tor_free(tmp);
963 return result;
966 /** Parse an address or address-port combination from <b>s</b>, resolve the
967 * address as needed, and put the result in <b>addr_out</b> and (optionally)
968 * <b>port_out</b>. Return 0 on success, negative on failure. */
970 tor_addr_port_parse(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
972 const char *port;
973 tor_addr_t addr;
974 uint16_t portval;
975 char *tmp = NULL;
977 tor_assert(s);
978 tor_assert(addr_out);
980 s = eat_whitespace(s);
982 if (*s == '[') {
983 port = strstr(s, "]");
984 if (!port)
985 goto err;
986 tmp = tor_strndup(s+1, port-(s+1));
987 port = port+1;
988 if (*port == ':')
989 port++;
990 else
991 port = NULL;
992 } else {
993 port = strchr(s, ':');
994 if (port)
995 tmp = tor_strndup(s, port-s);
996 else
997 tmp = tor_strdup(s);
998 if (port)
999 ++port;
1002 if (tor_addr_lookup(tmp, AF_UNSPEC, &addr) < 0)
1003 goto err;
1004 tor_free(tmp);
1006 if (port) {
1007 portval = (int) tor_parse_long(port, 10, 1, 65535, NULL, NULL);
1008 if (!portval)
1009 goto err;
1010 } else {
1011 portval = 0;
1014 if (port_out)
1015 *port_out = portval;
1016 tor_addr_copy(addr_out, &addr);
1018 return 0;
1019 err:
1020 tor_free(tmp);
1021 return -1;
1024 /** Set *<b>addr</b> to the IP address (if any) of whatever interface
1025 * connects to the Internet. This address should only be used in checking
1026 * whether our address has changed. Return 0 on success, -1 on failure.
1029 get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr)
1031 int sock=-1, r=-1;
1032 struct sockaddr_storage my_addr, target_addr;
1033 socklen_t my_addr_len;
1035 tor_assert(addr);
1037 memset(addr, 0, sizeof(tor_addr_t));
1038 memset(&target_addr, 0, sizeof(target_addr));
1039 my_addr_len = (socklen_t)sizeof(my_addr);
1040 /* Use the "discard" service port */
1041 ((struct sockaddr_in*)&target_addr)->sin_port = 9;
1042 /* Don't worry: no packets are sent. We just need to use a real address
1043 * on the actual Internet. */
1044 if (family == AF_INET6) {
1045 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&target_addr;
1046 sock = tor_open_socket(PF_INET6,SOCK_DGRAM,IPPROTO_UDP);
1047 my_addr_len = (socklen_t)sizeof(struct sockaddr_in6);
1048 sin6->sin6_family = AF_INET6;
1049 S6_ADDR16(sin6->sin6_addr)[0] = htons(0x2002); /* 2002:: */
1050 } else if (family == AF_INET) {
1051 struct sockaddr_in *sin = (struct sockaddr_in*)&target_addr;
1052 sock = tor_open_socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
1053 my_addr_len = (socklen_t)sizeof(struct sockaddr_in);
1054 sin->sin_family = AF_INET;
1055 sin->sin_addr.s_addr = htonl(0x12000001); /* 18.0.0.1 */
1056 } else {
1057 return -1;
1059 if (sock < 0) {
1060 int e = tor_socket_errno(-1);
1061 log_fn(severity, LD_NET, "unable to create socket: %s",
1062 tor_socket_strerror(e));
1063 goto err;
1066 if (connect(sock,(struct sockaddr *)&target_addr,
1067 (socklen_t)sizeof(target_addr))<0) {
1068 int e = tor_socket_errno(sock);
1069 log_fn(severity, LD_NET, "connect() failed: %s", tor_socket_strerror(e));
1070 goto err;
1073 if (getsockname(sock,(struct sockaddr*)&my_addr, &my_addr_len)) {
1074 int e = tor_socket_errno(sock);
1075 log_fn(severity, LD_NET, "getsockname() to determine interface failed: %s",
1076 tor_socket_strerror(e));
1077 goto err;
1080 tor_addr_from_sockaddr(addr, (struct sockaddr*)&my_addr, NULL);
1081 r=0;
1082 err:
1083 if (sock >= 0)
1084 tor_close_socket(sock);
1085 return r;
1088 /* ======
1089 * IPv4 helpers
1090 * XXXX022 IPv6 deprecate some of these.
1093 /** Return true iff <b>ip</b> (in host order) is an IP reserved to localhost,
1094 * or reserved for local networks by RFC 1918.
1097 is_internal_IP(uint32_t ip, int for_listening)
1099 tor_addr_t myaddr;
1100 myaddr.family = AF_INET;
1101 myaddr.addr.in_addr.s_addr = htonl(ip);
1103 return tor_addr_is_internal(&myaddr, for_listening);
1106 /** Parse a string of the form "host[:port]" from <b>addrport</b>. If
1107 * <b>address</b> is provided, set *<b>address</b> to a copy of the
1108 * host portion of the string. If <b>addr</b> is provided, try to
1109 * resolve the host portion of the string and store it into
1110 * *<b>addr</b> (in host byte order). If <b>port_out</b> is provided,
1111 * store the port number into *<b>port_out</b>, or 0 if no port is given.
1112 * If <b>port_out</b> is NULL, then there must be no port number in
1113 * <b>addrport</b>.
1114 * Return 0 on success, -1 on failure.
1117 parse_addr_port(int severity, const char *addrport, char **address,
1118 uint32_t *addr, uint16_t *port_out)
1120 const char *colon;
1121 char *_address = NULL;
1122 int _port;
1123 int ok = 1;
1125 tor_assert(addrport);
1127 colon = strchr(addrport, ':');
1128 if (colon) {
1129 _address = tor_strndup(addrport, colon-addrport);
1130 _port = (int) tor_parse_long(colon+1,10,1,65535,NULL,NULL);
1131 if (!_port) {
1132 log_fn(severity, LD_GENERAL, "Port %s out of range", escaped(colon+1));
1133 ok = 0;
1135 if (!port_out) {
1136 char *esc_addrport = esc_for_log(addrport);
1137 log_fn(severity, LD_GENERAL,
1138 "Port %s given on %s when not required",
1139 escaped(colon+1), esc_addrport);
1140 tor_free(esc_addrport);
1141 ok = 0;
1143 } else {
1144 _address = tor_strdup(addrport);
1145 _port = 0;
1148 if (addr) {
1149 /* There's an addr pointer, so we need to resolve the hostname. */
1150 if (tor_lookup_hostname(_address,addr)) {
1151 log_fn(severity, LD_NET, "Couldn't look up %s", escaped(_address));
1152 ok = 0;
1153 *addr = 0;
1157 if (address && ok) {
1158 *address = _address;
1159 } else {
1160 if (address)
1161 *address = NULL;
1162 tor_free(_address);
1164 if (port_out)
1165 *port_out = ok ? ((uint16_t) _port) : 0;
1167 return ok ? 0 : -1;
1170 /** If <b>mask</b> is an address mask for a bit-prefix, return the number of
1171 * bits. Otherwise, return -1. */
1173 addr_mask_get_bits(uint32_t mask)
1175 int i;
1176 if (mask == 0)
1177 return 0;
1178 if (mask == 0xFFFFFFFFu)
1179 return 32;
1180 for (i=0; i<=32; ++i) {
1181 if (mask == (uint32_t) ~((1u<<(32-i))-1)) {
1182 return i;
1185 return -1;
1188 /** Compare two addresses <b>a1</b> and <b>a2</b> for equality under a
1189 * netmask of <b>mbits</b> bits. Return -1, 0, or 1.
1191 * XXXX_IP6 Temporary function to allow masks as bitcounts everywhere. This
1192 * will be replaced with an IPv6-aware version as soon as 32-bit addresses are
1193 * no longer passed around.
1196 addr_mask_cmp_bits(uint32_t a1, uint32_t a2, maskbits_t bits)
1198 if (bits > 32)
1199 bits = 32;
1200 else if (bits == 0)
1201 return 0;
1203 a1 >>= (32-bits);
1204 a2 >>= (32-bits);
1206 if (a1 < a2)
1207 return -1;
1208 else if (a1 > a2)
1209 return 1;
1210 else
1211 return 0;
1214 /** Parse a string <b>s</b> in the format of (*|port(-maxport)?)?, setting the
1215 * various *out pointers as appropriate. Return 0 on success, -1 on failure.
1218 parse_port_range(const char *port, uint16_t *port_min_out,
1219 uint16_t *port_max_out)
1221 int port_min, port_max, ok;
1222 tor_assert(port_min_out);
1223 tor_assert(port_max_out);
1225 if (!port || *port == '\0' || strcmp(port, "*") == 0) {
1226 port_min = 1;
1227 port_max = 65535;
1228 } else {
1229 char *endptr = NULL;
1230 port_min = (int)tor_parse_long(port, 10, 0, 65535, &ok, &endptr);
1231 if (!ok) {
1232 log_warn(LD_GENERAL,
1233 "Malformed port %s on address range; rejecting.",
1234 escaped(port));
1235 return -1;
1236 } else if (endptr && *endptr == '-') {
1237 port = endptr+1;
1238 endptr = NULL;
1239 port_max = (int)tor_parse_long(port, 10, 1, 65536, &ok, &endptr);
1240 if (!ok) {
1241 log_warn(LD_GENERAL,
1242 "Malformed port %s on address range; rejecting.",
1243 escaped(port));
1244 return -1;
1246 } else {
1247 port_max = port_min;
1249 if (port_min > port_max) {
1250 log_warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
1251 return -1;
1255 if (port_min < 1)
1256 port_min = 1;
1257 if (port_max > 65535)
1258 port_max = 65535;
1260 *port_min_out = (uint16_t) port_min;
1261 *port_max_out = (uint16_t) port_max;
1263 return 0;
1266 /** Parse a string <b>s</b> in the format of
1267 * (IP(/mask|/mask-bits)?|*)(:(*|port(-maxport))?)?, setting the various
1268 * *out pointers as appropriate. Return 0 on success, -1 on failure.
1271 parse_addr_and_port_range(const char *s, uint32_t *addr_out,
1272 maskbits_t *maskbits_out, uint16_t *port_min_out,
1273 uint16_t *port_max_out)
1275 char *address;
1276 char *mask, *port, *endptr;
1277 struct in_addr in;
1278 int bits;
1280 tor_assert(s);
1281 tor_assert(addr_out);
1282 tor_assert(maskbits_out);
1283 tor_assert(port_min_out);
1284 tor_assert(port_max_out);
1286 address = tor_strdup(s);
1287 /* Break 'address' into separate strings.
1289 mask = strchr(address,'/');
1290 port = strchr(mask?mask:address,':');
1291 if (mask)
1292 *mask++ = '\0';
1293 if (port)
1294 *port++ = '\0';
1295 /* Now "address" is the IP|'*' part...
1296 * "mask" is the Mask|Maskbits part...
1297 * and "port" is the *|port|min-max part.
1300 if (strcmp(address,"*")==0) {
1301 *addr_out = 0;
1302 } else if (tor_inet_aton(address, &in) != 0) {
1303 *addr_out = ntohl(in.s_addr);
1304 } else {
1305 log_warn(LD_GENERAL, "Malformed IP %s in address pattern; rejecting.",
1306 escaped(address));
1307 goto err;
1310 if (!mask) {
1311 if (strcmp(address,"*")==0)
1312 *maskbits_out = 0;
1313 else
1314 *maskbits_out = 32;
1315 } else {
1316 endptr = NULL;
1317 bits = (int) strtol(mask, &endptr, 10);
1318 if (!*endptr) {
1319 /* strtol handled the whole mask. */
1320 if (bits < 0 || bits > 32) {
1321 log_warn(LD_GENERAL,
1322 "Bad number of mask bits on address range; rejecting.");
1323 goto err;
1325 *maskbits_out = bits;
1326 } else if (tor_inet_aton(mask, &in) != 0) {
1327 bits = addr_mask_get_bits(ntohl(in.s_addr));
1328 if (bits < 0) {
1329 log_warn(LD_GENERAL,
1330 "Mask %s on address range isn't a prefix; dropping",
1331 escaped(mask));
1332 goto err;
1334 *maskbits_out = bits;
1335 } else {
1336 log_warn(LD_GENERAL,
1337 "Malformed mask %s on address range; rejecting.",
1338 escaped(mask));
1339 goto err;
1343 if (parse_port_range(port, port_min_out, port_max_out)<0)
1344 goto err;
1346 tor_free(address);
1347 return 0;
1348 err:
1349 tor_free(address);
1350 return -1;
1353 /** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual),
1354 * write it as a string into the <b>buf_len</b>-byte buffer in
1355 * <b>buf</b>.
1358 tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len)
1360 uint32_t a = ntohl(in->s_addr);
1361 return tor_snprintf(buf, buf_len, "%d.%d.%d.%d",
1362 (int)(uint8_t)((a>>24)&0xff),
1363 (int)(uint8_t)((a>>16)&0xff),
1364 (int)(uint8_t)((a>>8 )&0xff),
1365 (int)(uint8_t)((a )&0xff));
1368 /** Given a host-order <b>addr</b>, call tor_inet_ntop() on it
1369 * and return a strdup of the resulting address.
1371 char *
1372 tor_dup_ip(uint32_t addr)
1374 char buf[TOR_ADDR_BUF_LEN];
1375 struct in_addr in;
1377 in.s_addr = htonl(addr);
1378 tor_inet_ntop(AF_INET, &in, buf, sizeof(buf));
1379 return tor_strdup(buf);
1383 * Set *<b>addr</b> to the host-order IPv4 address (if any) of whatever
1384 * interface connects to the Internet. This address should only be used in
1385 * checking whether our address has changed. Return 0 on success, -1 on
1386 * failure.
1389 get_interface_address(int severity, uint32_t *addr)
1391 tor_addr_t local_addr;
1392 int r;
1394 r = get_interface_address6(severity, AF_INET, &local_addr);
1395 if (r>=0)
1396 *addr = tor_addr_to_ipv4h(&local_addr);
1397 return r;