r15106@tombo: nickm | 2007-12-04 00:08:35 -0500

Change tor_addr_t to be a tagged union of in_addr and in6_addr, not of sockaddr_in and sockaddr_in6.  It's hardly used in the main code as it is, but let's get it right before it gets popular.


svn:r12660
This commit is contained in:
Nick Mathewson 2007-12-04 05:19:56 +00:00
parent ce2cf88ebf
commit 593ab7e808
5 changed files with 48 additions and 53 deletions

View File

@ -38,6 +38,9 @@ Changes in version 0.2.0.13-alpha - 2007-12-??
- Authorities and caches fetch the v2 networkstatus documents - Authorities and caches fetch the v2 networkstatus documents
less often, now that v3 is encouraged. less often, now that v3 is encouraged.
o Code simplifications:
-
Changes in version 0.2.0.12-alpha - 2007-11-16 Changes in version 0.2.0.12-alpha - 2007-11-16
This twelfth development snapshot fixes some more build problems as This twelfth development snapshot fixes some more build problems as

View File

@ -1093,12 +1093,12 @@ tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
return -1; return -1;
} else if (tor_inet_pton(AF_INET, name, &iaddr)) { } else if (tor_inet_pton(AF_INET, name, &iaddr)) {
/* It's an IPv4 IP. */ /* It's an IPv4 IP. */
addr->sa.sin_family = AF_INET; addr->family = AF_INET;
memcpy(&addr->sa.sin_addr, &iaddr, sizeof(struct in_addr)); memcpy(&addr->addr.in_addr, &iaddr, sizeof(struct in_addr));
return 0; return 0;
} else if (tor_inet_pton(AF_INET6, name, &iaddr6)) { } else if (tor_inet_pton(AF_INET6, name, &iaddr6)) {
addr->sa6.sin6_family = AF_INET6; addr->family = AF_INET6;
memcpy(&addr->sa6.sin6_addr, &iaddr6, sizeof(struct in6_addr)); memcpy(&addr->addr.in6_addr, &iaddr6, sizeof(struct in6_addr));
return 0; return 0;
} else { } else {
#ifdef HAVE_GETADDRINFO #ifdef HAVE_GETADDRINFO
@ -1129,14 +1129,14 @@ tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
if (!best) if (!best)
best = res; best = res;
if (best->ai_family == AF_INET) { if (best->ai_family == AF_INET) {
addr->sa.sin_family = AF_INET; addr->family = AF_INET;
memcpy(&addr->sa.sin_addr, memcpy(&addr->addr.in_addr,
&((struct sockaddr_in*)best->ai_addr)->sin_addr, &((struct sockaddr_in*)best->ai_addr)->sin_addr,
sizeof(struct in_addr)); sizeof(struct in_addr));
result = 0; result = 0;
} else if (best->ai_family == AF_INET6) { } else if (best->ai_family == AF_INET6) {
addr->sa6.sin6_family = AF_INET6; addr->family = AF_INET6;
memcpy(&addr->sa6.sin6_addr, memcpy(&addr->addr.in6_addr,
&((struct sockaddr_in6*)best->ai_addr)->sin6_addr, &((struct sockaddr_in6*)best->ai_addr)->sin6_addr,
sizeof(struct in6_addr)); sizeof(struct in6_addr));
result = 0; result = 0;
@ -1172,11 +1172,11 @@ tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
#endif #endif
#endif /* endif HAVE_GETHOSTBYNAME_R_6_ARG. */ #endif /* endif HAVE_GETHOSTBYNAME_R_6_ARG. */
if (ent) { if (ent) {
addr->sa.sin_family = ent->h_addrtype; addr->family = ent->h_addrtype;
if (ent->h_addrtype == AF_INET) { if (ent->h_addrtype == AF_INET) {
memcpy(&addr->sa.sin_addr, ent->h_addr, sizeof(struct in_addr)); memcpy(&addr->addr.in_addr, ent->h_addr, sizeof(struct in_addr));
} else if (ent->h_addrtype == AF_INET6) { } else if (ent->h_addrtype == AF_INET6) {
memcpy(&addr->sa6.sin6_addr, ent->h_addr, sizeof(struct in6_addr)); memcpy(&addr->addr.in6_addr, ent->h_addr, sizeof(struct in6_addr));
} else { } else {
tor_assert(0); /* gethostbyname() returned a bizarre addrtype */ tor_assert(0); /* gethostbyname() returned a bizarre addrtype */
} }

View File

@ -325,12 +325,13 @@ struct sockaddr_in6 {
typedef uint8_t maskbits_t; typedef uint8_t maskbits_t;
struct in_addr; struct in_addr;
typedef union tor_addr_t typedef struct tor_addr_t
{ {
/* XXXX020 There are extra fields in sockaddr_in and sockaddr_in6 that sa_family_t family;
* make this union waste space. Do we care? */ union {
struct sockaddr_in sa; struct in_addr in_addr;
struct sockaddr_in6 sa6; struct in6_addr in6_addr;
} addr;
} tor_addr_t; } tor_addr_t;
/* XXXX020 rename these. */ /* XXXX020 rename these. */
@ -340,12 +341,11 @@ static INLINE uint32_t IPV4MAPh(const tor_addr_t *a);
static INLINE uint16_t IN_FAMILY(const tor_addr_t *a); static INLINE uint16_t IN_FAMILY(const tor_addr_t *a);
static INLINE const struct in_addr *IN4_ADDRESS(const tor_addr_t *a); static INLINE const struct in_addr *IN4_ADDRESS(const tor_addr_t *a);
static INLINE const struct in6_addr *IN6_ADDRESS(const tor_addr_t *a); static INLINE const struct in6_addr *IN6_ADDRESS(const tor_addr_t *a);
static INLINE uint16_t IN_PORT(const tor_addr_t *a);
static INLINE const struct in6_addr * static INLINE const struct in6_addr *
IN6_ADDRESS(const tor_addr_t *a) IN6_ADDRESS(const tor_addr_t *a)
{ {
return &a->sa6.sin6_addr; return &a->addr.in6_addr;
} }
#define IN6_ADDRESS16(x) S6_ADDR16(*IN6_ADDRESS(x)) #define IN6_ADDRESS16(x) S6_ADDR16(*IN6_ADDRESS(x))
@ -354,7 +354,7 @@ IN6_ADDRESS(const tor_addr_t *a)
static INLINE uint32_t static INLINE uint32_t
IPV4IP(const tor_addr_t *a) IPV4IP(const tor_addr_t *a)
{ {
return a->sa.sin_addr.s_addr; return a->addr.in_addr.s_addr;
} }
static INLINE uint32_t IPV4IPh(const tor_addr_t *a) static INLINE uint32_t IPV4IPh(const tor_addr_t *a)
{ {
@ -369,20 +369,12 @@ IPV4MAPh(const tor_addr_t *a)
static INLINE uint16_t static INLINE uint16_t
IN_FAMILY(const tor_addr_t *a) IN_FAMILY(const tor_addr_t *a)
{ {
return a->sa.sin_family; return a->family;
} }
static INLINE const struct in_addr * static INLINE const struct in_addr *
IN4_ADDRESS(const tor_addr_t *a) IN4_ADDRESS(const tor_addr_t *a)
{ {
return &a->sa.sin_addr; return &a->addr.in_addr;
}
static INLINE uint16_t
IN_PORT(const tor_addr_t *a)
{
if (IN_FAMILY(a) == AF_INET)
return a->sa.sin_port;
else
return a->sa6.sin6_port;
} }
#define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */ #define INET_NTOA_BUF_LEN 16 /* 255.255.255.255 */

View File

@ -2108,8 +2108,8 @@ int
is_internal_IP(uint32_t ip, int for_listening) is_internal_IP(uint32_t ip, int for_listening)
{ {
tor_addr_t myaddr; tor_addr_t myaddr;
myaddr.sa.sin_family = AF_INET; myaddr.family = AF_INET;
myaddr.sa.sin_addr.s_addr = htonl(ip); myaddr.addr.in_addr.s_addr = htonl(ip);
return tor_addr_is_internal(&myaddr, for_listening); return tor_addr_is_internal(&myaddr, for_listening);
} }
@ -2519,12 +2519,12 @@ tor_addr_parse_mask_ports(const char *s, tor_addr_t *addr_out,
memset(addr_out, 0, sizeof(tor_addr_t)); memset(addr_out, 0, sizeof(tor_addr_t));
if (!strcmp(address, "*")) { if (!strcmp(address, "*")) {
addr_out->sa.sin_family = AF_INET; /* AF_UNSPEC ???? XXXXX020 */ addr_out->family = AF_INET; /* AF_UNSPEC ???? XXXXX020 */
any_flag = 1; any_flag = 1;
} else if (tor_inet_pton(AF_INET6, address, &addr_out->sa6.sin6_addr) > 0) { } else if (tor_inet_pton(AF_INET6, address, &addr_out->addr.in6_addr) > 0) {
addr_out->sa6.sin6_family = AF_INET6; addr_out->family = AF_INET6;
} else if (tor_inet_pton(AF_INET, address, &addr_out->sa.sin_addr) > 0) { } else if (tor_inet_pton(AF_INET, address, &addr_out->addr.in_addr) > 0) {
addr_out->sa.sin_family = AF_INET; addr_out->family = AF_INET;
} else { } else {
log_warn(LD_GENERAL, "Malformed IP %s in address pattern; rejecting.", log_warn(LD_GENERAL, "Malformed IP %s in address pattern; rejecting.",
escaped(address)); escaped(address));
@ -2708,8 +2708,8 @@ tor_addr_from_ipv4(tor_addr_t *dest, uint32_t v4addr)
{ {
tor_assert(dest); tor_assert(dest);
memset(dest, 0, sizeof(dest)); memset(dest, 0, sizeof(dest));
dest->sa.sin_family = AF_INET; dest->family = AF_INET;
dest->sa.sin_addr.s_addr = htonl(v4addr); dest->addr.in_addr.s_addr = htonl(v4addr);
} }
/** Copy a tor_addr_t from <b>src</b> to <b>dest</b>. /** Copy a tor_addr_t from <b>src</b> to <b>dest</b>.

View File

@ -1074,38 +1074,38 @@ _test_eq_ip6(struct in6_addr *a, struct in6_addr *b, const char *e1,
/*XXXX020 make this macro give useful output on failure, and follow the /*XXXX020 make this macro give useful output on failure, and follow the
* conventions of the other test macros. */ * conventions of the other test macros. */
#define test_internal_ip(a,b) STMT_BEGIN \ #define test_internal_ip(a,b) STMT_BEGIN \
r = tor_inet_pton(AF_INET6, a, &t1.sa6.sin6_addr); \ r = tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr); \
test_assert(r==1); \ test_assert(r==1); \
t1.sa6.sin6_family = AF_INET6; \ t1.family = AF_INET6; \
r = tor_addr_is_internal(&t1, b); \ r = tor_addr_is_internal(&t1, b); \
test_assert(r==1); \ test_assert(r==1); \
STMT_END STMT_END
/*XXXX020 make this macro give useful output on failure, and follow the /*XXXX020 make this macro give useful output on failure, and follow the
* conventions of the other test macros. */ * conventions of the other test macros. */
#define test_external_ip(a,b) STMT_BEGIN \ #define test_external_ip(a,b) STMT_BEGIN \
r = tor_inet_pton(AF_INET6, a, &t1.sa6.sin6_addr); \ r = tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr); \
test_assert(r==1); \ test_assert(r==1); \
t1.sa6.sin6_family = AF_INET6; \ t1.family = AF_INET6; \
r = tor_addr_is_internal(&t1, b); \ r = tor_addr_is_internal(&t1, b); \
test_assert(r==0); \ test_assert(r==0); \
STMT_END STMT_END
/*XXXX020 make this macro give useful output on failure, and follow the /*XXXX020 make this macro give useful output on failure, and follow the
* conventions of the other test macros. */ * conventions of the other test macros. */
#define test_addr_convert6(a,b) STMT_BEGIN \ #define test_addr_convert6(a,b) STMT_BEGIN \
tor_inet_pton(AF_INET6, a, &t1.sa6.sin6_addr); \ tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr); \
tor_inet_pton(AF_INET6, b, &t2.sa6.sin6_addr); \ tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr); \
t1.sa6.sin6_family = AF_INET6; \ t1.family = AF_INET6; \
t2.sa6.sin6_family = AF_INET6; \ t2.family = AF_INET6; \
STMT_END STMT_END
/*XXXX020 make this macro give useful output on failure, and follow the /*XXXX020 make this macro give useful output on failure, and follow the
* conventions of the other test macros. */ * conventions of the other test macros. */
#define test_addr_parse(xx) STMT_BEGIN \ #define test_addr_parse(xx) STMT_BEGIN \
r=tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2); \ r=tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2); \
t2.sa6.sin6_family = AF_INET6; \ t2.family = AF_INET6; \
p1=tor_inet_ntop(AF_INET6, &t1.sa6.sin6_addr, bug, sizeof(bug)); \ p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
STMT_END STMT_END
/*XXXX020 make this macro give useful output on failure, and follow the /*XXXX020 make this macro give useful output on failure, and follow the
@ -1309,7 +1309,7 @@ test_util_ip6_helpers(void)
test_assert(TOR_ADDR_BUF_LEN >= test_assert(TOR_ADDR_BUF_LEN >=
sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")); sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
test_assert(sizeof(tor_addr_t) >= sizeof(struct sockaddr_in6)); test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
/* get interface addresses */ /* get interface addresses */
r = get_interface_address6(0, AF_INET, &t1); r = get_interface_address6(0, AF_INET, &t1);