[or-cvs] r16458: {tor} Make tor_addr_from_sockaddr also give away the port in a use (in tor/trunk: . src/common src/or)

nickm at seul.org nickm at seul.org
Thu Aug 7 19:13:35 UTC 2008


Author: nickm
Date: 2008-08-07 15:13:35 -0400 (Thu, 07 Aug 2008)
New Revision: 16458

Modified:
   tor/trunk/
   tor/trunk/src/common/address.c
   tor/trunk/src/common/address.h
   tor/trunk/src/or/connection.c
   tor/trunk/src/or/dnsserv.c
   tor/trunk/src/or/test.c
Log:
 r17666 at tombo:  nickm | 2008-08-07 15:12:30 -0400
 Make tor_addr_from_sockaddr also give away the port in a useful format



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r17666] on 49666b30-7950-49c5-bedf-9dc8f3168102

Modified: tor/trunk/src/common/address.c
===================================================================
--- tor/trunk/src/common/address.c	2008-08-07 14:23:12 UTC (rev 16457)
+++ tor/trunk/src/common/address.c	2008-08-07 19:13:35 UTC (rev 16458)
@@ -94,7 +94,8 @@
 /** 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 @@
     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;

Modified: tor/trunk/src/common/address.h
===================================================================
--- tor/trunk/src/common/address.h	2008-08-07 14:23:12 UTC (rev 16457)
+++ tor/trunk/src/common/address.h	2008-08-07 19:13:35 UTC (rev 16458)
@@ -40,7 +40,8 @@
 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 *

Modified: tor/trunk/src/or/connection.c
===================================================================
--- tor/trunk/src/or/connection.c	2008-08-07 14:23:12 UTC (rev 16457)
+++ tor/trunk/src/or/connection.c	2008-08-07 19:13:35 UTC (rev 16458)
@@ -1032,12 +1032,7 @@
       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) {

Modified: tor/trunk/src/or/dnsserv.c
===================================================================
--- tor/trunk/src/or/dnsserv.c	2008-08-07 14:23:12 UTC (rev 16457)
+++ tor/trunk/src/or/dnsserv.c	2008-08-07 19:13:35 UTC (rev 16458)
@@ -46,7 +46,7 @@
   }
   (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 @@
     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

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2008-08-07 14:23:12 UTC (rev 16457)
+++ tor/trunk/src/or/test.c	2008-08-07 19:13:35 UTC (rev 16458)
@@ -1328,7 +1328,7 @@
   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 @@
   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::");



More information about the tor-commits mailing list