[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [tor/master 1/2] refactor all these tor_inet_ntoa idioms



Author: Roger Dingledine <arma@xxxxxxxxxxxxxx>
Date: Fri, 1 Oct 2010 21:31:09 -0400
Subject: refactor all these tor_inet_ntoa idioms
Commit: 22f723e4a3fc32983480c7403af9d7e77a3200ea

but don't refactor the ones that look messy
---
 src/common/address.c     |   12 ++++++++++++
 src/common/address.h     |    1 +
 src/or/config.c          |   36 ++++++++++++++----------------------
 src/or/connection_edge.c |   15 +++++----------
 src/or/dirserv.c         |   12 ++----------
 src/or/dirvote.c         |   13 +++----------
 src/or/relay.c           |   15 ++++-----------
 src/or/router.c          |    6 +-----
 8 files changed, 42 insertions(+), 68 deletions(-)

diff --git a/src/common/address.c b/src/common/address.c
index c35f04c..dfe8f51 100644
--- a/src/common/address.c
+++ b/src/common/address.c
@@ -927,6 +927,18 @@ fmt_addr(const tor_addr_t *addr)
   return buf;
 }
 
+/** Like fmt_addr(), but for IPv4 addresses. Also not thread-safe, also
+ * clobbers its return buffer on repeated calls. */
+const char *
+fmt_addr32(uint32_t addr)
+{
+  static char buf[INET_NTOA_BUF_LEN];
+  struct in_addr in;
+  in.s_addr = htonl(addr);
+  tor_inet_ntoa(&in, buf, sizeof(buf));
+  return buf;
+}
+
 /** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>.  The string
  * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by
  * square brackets.
diff --git a/src/common/address.h b/src/common/address.h
index 6116bb4..9a3c3ef 100644
--- a/src/common/address.h
+++ b/src/common/address.h
@@ -108,6 +108,7 @@ tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
 int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
 char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
 const char *fmt_addr(const tor_addr_t *addr);
+const char * fmt_addr32(uint32_t addr);
 int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
 
 /** Flag to specify how to do a comparison between addresses.  In an "exact"
diff --git a/src/or/config.c b/src/or/config.c
index 4f70771..d3b4445 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -2336,7 +2336,7 @@ resolve_my_address(int warn_severity, or_options_t *options,
   int explicit_ip=1;
   int explicit_hostname=1;
   int from_interface=0;
-  char tmpbuf[INET_NTOA_BUF_LEN];
+  char *tmpbuf;
   const char *address = options->Address;
   int notice_severity = warn_severity <= LOG_NOTICE ?
                           LOG_NOTICE : warn_severity;
@@ -2378,49 +2378,40 @@ resolve_my_address(int warn_severity, or_options_t *options,
         return -1;
       }
       from_interface = 1;
-      in.s_addr = htonl(interface_ip);
-      tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
+      addr = interface_ip;
       log_fn(notice_severity, LD_CONFIG, "Learned IP address '%s' for "
-             "local interface. Using that.", tmpbuf);
+             "local interface. Using that.", fmt_addr32(addr));
       strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname));
     } else { /* resolved hostname into addr */
-      in.s_addr = htonl(addr);
-
       if (!explicit_hostname &&
-          is_internal_IP(ntohl(in.s_addr), 0)) {
+          is_internal_IP(addr, 0)) {
         uint32_t interface_ip;
 
-        tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
         log_fn(notice_severity, LD_CONFIG, "Guessed local hostname '%s' "
-               "resolves to a private IP address (%s).  Trying something "
-               "else.", hostname, tmpbuf);
+               "resolves to a private IP address (%s). Trying something "
+               "else.", hostname, fmt_addr32(addr));
 
         if (get_interface_address(warn_severity, &interface_ip)) {
           log_fn(warn_severity, LD_CONFIG,
                  "Could not get local interface IP address. Too bad.");
         } else if (is_internal_IP(interface_ip, 0)) {
-          struct in_addr in2;
-          in2.s_addr = htonl(interface_ip);
-          tor_inet_ntoa(&in2,tmpbuf,sizeof(tmpbuf));
           log_fn(notice_severity, LD_CONFIG,
                  "Interface IP address '%s' is a private address too. "
-                 "Ignoring.", tmpbuf);
+                 "Ignoring.", fmt_addr32(interface_ip));
         } else {
           from_interface = 1;
-          in.s_addr = htonl(interface_ip);
-          tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
+          addr = interface_ip;
           log_fn(notice_severity, LD_CONFIG,
                  "Learned IP address '%s' for local interface."
-                 " Using that.", tmpbuf);
+                 " Using that.", fmt_addr32(addr));
           strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname));
         }
       }
     }
   }
 
-  tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
-  if (is_internal_IP(ntohl(in.s_addr), 0) &&
-      options->_PublishServerDescriptor) {
+  tmpbuf = tor_dup_ip(addr);
+  if (is_internal_IP(addr, 0) && options->_PublishServerDescriptor) {
     /* make sure we're ok with publishing an internal IP */
     if (!options->DirServers && !options->AlternateDirAuthority) {
       /* if they are using the default dirservers, disallow internal IPs
@@ -2441,8 +2432,8 @@ resolve_my_address(int warn_severity, or_options_t *options,
     }
   }
 
-  log_debug(LD_CONFIG, "Resolved Address to '%s'.", tmpbuf);
-  *addr_out = ntohl(in.s_addr);
+  log_debug(LD_CONFIG, "Resolved Address to '%s'.", fmt_addr32(addr));
+  *addr_out = addr;
   if (last_resolved_addr && last_resolved_addr != *addr_out) {
     /* Leave this as a notice, regardless of the requested severity,
      * at least until dynamic IP address support becomes bulletproof. */
@@ -2472,6 +2463,7 @@ resolve_my_address(int warn_severity, or_options_t *options,
   last_resolved_addr = *addr_out;
   if (hostname_out)
     *hostname_out = tor_strdup(hostname);
+  tor_free(tmpbuf);
   return 0;
 }
 
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index 9627631..e89bc66 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -1185,7 +1185,6 @@ static char *
 addressmap_get_virtual_address(int type)
 {
   char buf[64];
-  struct in_addr in;
   tor_assert(addressmap);
 
   if (type == RESOLVED_TYPE_HOSTNAME) {
@@ -1206,9 +1205,7 @@ addressmap_get_virtual_address(int type)
              (next_virtual_addr & 0xff) == 0xff) {
         ++next_virtual_addr;
       }
-      in.s_addr = htonl(next_virtual_addr);
-      tor_inet_ntoa(&in, buf, sizeof(buf));
-      if (!strmap_get(addressmap, buf)) {
+      if (!strmap_get(addressmap, fmt_addr32(next_virtual_addr))) {
         ++next_virtual_addr;
         break;
       }
@@ -2298,13 +2295,11 @@ tell_controller_about_resolved_result(edge_connection_t *conn,
                    answer_type == RESOLVED_TYPE_HOSTNAME)) {
     return; /* we already told the controller. */
   } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) {
-    struct in_addr in;
-    char buf[INET_NTOA_BUF_LEN];
-    in.s_addr = get_uint32(answer);
-    tor_inet_ntoa(&in, buf, sizeof(buf));
+    char *cp = tor_dup_ip(get_uint32(answer));
     control_event_address_mapped(conn->socks_request->address,
-                                 buf, expires, NULL);
-  } else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len <256) {
+                                 cp, expires, NULL);
+    tor_free(cp);
+  } else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) {
     char *cp = tor_strndup(answer, answer_len);
     control_event_address_mapped(conn->socks_request->address,
                                  cp, expires, NULL);
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 75e3e86..5f94ceb 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -1954,20 +1954,16 @@ routerstatus_format_entry(char *buf, size_t buf_len,
                           routerstatus_format_type_t format)
 {
   int r;
-  struct in_addr in;
   char *cp;
   char *summary;
 
   char published[ISO_TIME_LEN+1];
-  char ipaddr[INET_NTOA_BUF_LEN];
   char identity64[BASE64_DIGEST_LEN+1];
   char digest64[BASE64_DIGEST_LEN+1];
 
   format_iso_time(published, rs->published_on);
   digest_to_base64(identity64, rs->identity_digest);
   digest_to_base64(digest64, rs->descriptor_digest);
-  in.s_addr = htonl(rs->addr);
-  tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
 
   r = tor_snprintf(buf, buf_len,
                    "r %s %s %s%s%s %s %d %d\n",
@@ -1976,7 +1972,7 @@ routerstatus_format_entry(char *buf, size_t buf_len,
                    (format==NS_V3_CONSENSUS_MICRODESC)?"":digest64,
                    (format==NS_V3_CONSENSUS_MICRODESC)?"":" ",
                    published,
-                   ipaddr,
+                   fmt_addr32(rs->addr),
                    (int)rs->or_port,
                    (int)rs->dir_port);
   if (r<0) {
@@ -2693,10 +2689,8 @@ generate_v2_networkstatus_opinion(void)
   char *outp, *endp;
   or_options_t *options = get_options();
   char fingerprint[FINGERPRINT_LEN+1];
-  char ipaddr[INET_NTOA_BUF_LEN];
   char published[ISO_TIME_LEN+1];
   char digest[DIGEST_LEN];
-  struct in_addr in;
   uint32_t addr;
   crypto_pk_env_t *private_key;
   routerlist_t *rl = router_get_routerlist();
@@ -2717,8 +2711,6 @@ generate_v2_networkstatus_opinion(void)
     log_warn(LD_NET, "Couldn't resolve my hostname");
     goto done;
   }
-  in.s_addr = htonl(addr);
-  tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
 
   format_iso_time(published, now);
 
@@ -2764,7 +2756,7 @@ generate_v2_networkstatus_opinion(void)
                "dir-options%s%s%s%s\n"
                "%s" /* client version line, server version line. */
                "dir-signing-key\n%s",
-               hostname, ipaddr, (int)options->DirPort,
+               hostname, fmt_addr32(addr), (int)options->DirPort,
                fingerprint,
                contact,
                published,
diff --git a/src/or/dirvote.c b/src/or/dirvote.c
index dd36a0f..1cb3563 100644
--- a/src/or/dirvote.c
+++ b/src/or/dirvote.c
@@ -83,9 +83,7 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
   const char *client_versions = NULL, *server_versions = NULL;
   char *outp, *endp;
   char fingerprint[FINGERPRINT_LEN+1];
-  char ipaddr[INET_NTOA_BUF_LEN];
   char digest[DIGEST_LEN];
-  struct in_addr in;
   uint32_t addr;
   routerlist_t *rl = router_get_routerlist();
   char *version_lines = NULL;
@@ -98,8 +96,6 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
   voter = smartlist_get(v3_ns->voters, 0);
 
   addr = voter->addr;
-  in.s_addr = htonl(addr);
-  tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
 
   base16_encode(fingerprint, sizeof(fingerprint),
                 v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
@@ -186,7 +182,8 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
                  flags,
                  params,
                  voter->nickname, fingerprint, voter->address,
-                 ipaddr, voter->dir_port, voter->or_port, voter->contact);
+                 fmt_addr32(addr), voter->dir_port, voter->or_port,
+                 voter->contact);
 
     if (r < 0) {
       log_err(LD_BUG, "Insufficient memory for network status line");
@@ -1529,8 +1526,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
     smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id);
 
     SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
-      struct in_addr in;
-      char ip[INET_NTOA_BUF_LEN];
       char fingerprint[HEX_DIGEST_LEN+1];
       char votedigest[HEX_DIGEST_LEN+1];
       networkstatus_t *v = e->v;
@@ -1540,8 +1535,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
       if (e->is_legacy)
         tor_assert(consensus_method >= 2);
 
-      in.s_addr = htonl(voter->addr);
-      tor_inet_ntoa(&in, ip, sizeof(ip));
       base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
       base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
                     DIGEST_LEN);
@@ -1549,7 +1542,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
       tor_asprintf(&buf,
                    "dir-source %s%s %s %s %s %d %d\n",
                    voter->nickname, e->is_legacy ? "-legacy" : "",
-                   fingerprint, voter->address, ip,
+                   fingerprint, voter->address, fmt_addr32(voter->addr),
                    voter->dir_port,
                    voter->or_port);
       smartlist_add(chunks, buf);
diff --git a/src/or/relay.c b/src/or/relay.c
index f9a44cf..05af38e 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -906,12 +906,8 @@ connection_edge_process_relay_cell_not_open(
       int ttl;
       if (!addr || (get_options()->ClientDNSRejectInternalAddresses &&
                     is_internal_IP(addr, 0))) {
-        char buf[INET_NTOA_BUF_LEN];
-        struct in_addr a;
-        a.s_addr = htonl(addr);
-        tor_inet_ntoa(&a, buf, sizeof(buf));
-        log_info(LD_APP,
-                 "...but it claims the IP address was %s. Closing.", buf);
+        log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
+                 fmt_addr32(addr));
         connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
         connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
         return 0;
@@ -988,11 +984,8 @@ connection_edge_process_relay_cell_not_open(
       uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
       if (get_options()->ClientDNSRejectInternalAddresses &&
           is_internal_IP(addr, 0)) {
-        char buf[INET_NTOA_BUF_LEN];
-        struct in_addr a;
-        a.s_addr = htonl(addr);
-        tor_inet_ntoa(&a, buf, sizeof(buf));
-        log_info(LD_APP,"Got a resolve with answer %s.  Rejecting.", buf);
+        log_info(LD_APP,"Got a resolve with answer %s. Rejecting.",
+                 fmt_addr32(addr));
         connection_ap_handshake_socks_resolved(conn,
                                                RESOLVED_TYPE_ERROR_TRANSIENT,
                                                0, NULL, 0, TIME_MAX);
diff --git a/src/or/router.c b/src/or/router.c
index 8b3a184..3fc1690 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1257,8 +1257,6 @@ static int router_guess_address_from_dir_headers(uint32_t *guess);
 int
 router_pick_published_address(or_options_t *options, uint32_t *addr)
 {
-  char buf[INET_NTOA_BUF_LEN];
-  struct in_addr a;
   if (resolve_my_address(LOG_INFO, options, addr, NULL) < 0) {
     log_info(LD_CONFIG, "Could not determine our address locally. "
              "Checking if directory headers provide any hints.");
@@ -1268,9 +1266,7 @@ router_pick_published_address(or_options_t *options, uint32_t *addr)
       return -1;
     }
   }
-  a.s_addr = htonl(*addr);
-  tor_inet_ntoa(&a, buf, sizeof(buf));
-  log_info(LD_CONFIG,"Success: chose address '%s'.", buf);
+  log_info(LD_CONFIG,"Success: chose address '%s'.", fmt_addr32(*addr));
   return 0;
 }
 
-- 
1.7.1