summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-08-07 19:13:35 +0000
committerNick Mathewson <nickm@torproject.org>2008-08-07 19:13:35 +0000
commitd9601c65e0529c1f79e52ca3576cbc8f0a82f8f6 (patch)
tree7025b74af0ad9c3bfb8a0ce6953864787d150e6a
parent635f3c8aeef717d03a86117dfa81944fb6788bca (diff)
downloadtor-d9601c65e0529c1f79e52ca3576cbc8f0a82f8f6.tar.gz
tor-d9601c65e0529c1f79e52ca3576cbc8f0a82f8f6.zip
r17666@tombo: nickm | 2008-08-07 15:12:30 -0400
Make tor_addr_from_sockaddr also give away the port in a useful format svn:r16458
-rw-r--r--src/common/address.c7
-rw-r--r--src/common/address.h3
-rw-r--r--src/or/connection.c7
-rw-r--r--src/or/dnsserv.c8
-rw-r--r--src/or/test.c4
5 files changed, 12 insertions, 17 deletions
diff --git a/src/common/address.c b/src/common/address.c
index e5b11a4ffd..ac4ae041d8 100644
--- a/src/common/address.c
+++ b/src/common/address.c
@@ -94,7 +94,8 @@ tor_addr_to_sockaddr(const tor_addr_t *a,
/** Set the tor_addr_t in <b>a</b> to contain the socket address contained in
* <b>sa</b>. */
int
-tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa)
+tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
+ uint16_t *port_out)
{
tor_assert(a);
tor_assert(sa);
@@ -103,10 +104,14 @@ tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa)
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
a->family = AF_INET;
a->addr.in_addr.s_addr = sin->sin_addr.s_addr;
+ if (port_out)
+ *port_out = ntohs(sin->sin_port);
} else if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
a->family = AF_INET6;
memcpy(&a->addr.in6_addr, &sin6->sin6_addr, sizeof(struct in6_addr));
+ if (port_out)
+ *port_out = ntohs(sin6->sin6_port);
} else {
a->family = AF_UNSPEC;
return -1;
diff --git a/src/common/address.h b/src/common/address.h
index 133a9b192d..c206bf1445 100644
--- a/src/common/address.h
+++ b/src/common/address.h
@@ -40,7 +40,8 @@ static INLINE const struct in6_addr *tor_addr_to_in6(const tor_addr_t *a);
static INLINE int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u);
socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
struct sockaddr *sa_out, socklen_t len);
-int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa);
+int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
+ uint16_t *port_out);
void tor_addr_make_unspec(tor_addr_t *a);
static INLINE const struct in6_addr *
diff --git a/src/or/connection.c b/src/or/connection.c
index cf00f90eed..8cb04b35c3 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -1032,12 +1032,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
return 0;
}
- tor_addr_from_sockaddr(&addr, remote);
- if (remote->sa_family == AF_INET)
- port = ((struct sockaddr_in *)remote)->sin_port;
- else
- port = ((struct sockaddr_in6 *)remote)->sin6_port;
- port = ntohs(port);
+ tor_addr_from_sockaddr(&addr, remote, &port);
/* process entrance policies here, before we even create the connection */
if (new_type == CONN_TYPE_AP) {
diff --git a/src/or/dnsserv.c b/src/or/dnsserv.c
index 5a1c023834..c65dfe08ca 100644
--- a/src/or/dnsserv.c
+++ b/src/or/dnsserv.c
@@ -46,7 +46,7 @@ evdns_server_callback(struct evdns_server_request *req, void *_data)
}
(void) addrlen;
sa = (struct sockaddr*) &addr;
- if (tor_addr_from_sockaddr(&tor_addr, sa)<0) {
+ if (tor_addr_from_sockaddr(&tor_addr, sa, &port)<0) {
log_warn(LD_APP, "Requesting address wasn't recognized.");
evdns_server_request_respond(req, DNS_ERR_SERVERFAILED);
return;
@@ -58,12 +58,6 @@ evdns_server_callback(struct evdns_server_request *req, void *_data)
return;
}
- if (sa->sa_family == AF_INET)
- port = ((struct sockaddr_in *)sa)->sin_port;
- else
- port = ((struct sockaddr_in6 *)sa)->sin6_port;
- port = ntohs(port);
-
/* Now, let's find the first actual question of a type we can answer in this
* DNS request. It makes us a little noncompliant to act like this; we
* should fix that eventually if it turns out to make a difference for
diff --git a/src/or/test.c b/src/or/test.c
index 0f19e5b015..fde4f5b1d7 100644
--- a/src/or/test.c
+++ b/src/or/test.c
@@ -1328,7 +1328,7 @@ test_util_ip6_helpers(void)
sin->sin_family = AF_INET;
sin->sin_port = 9090;
sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
- tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin);
+ tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
test_eq(tor_addr_family(&t1), AF_INET);
test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
@@ -1344,7 +1344,7 @@ test_util_ip6_helpers(void)
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(7070);
sin6->sin6_addr.s6_addr[0] = 128;
- tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6);
+ tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
test_eq(tor_addr_family(&t1), AF_INET6);
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
test_streq(p1, "8000::");