[tor-commits] [tor/master] decorated ipv6 address for directory send command

nickm at torproject.org nickm at torproject.org
Mon Feb 8 13:33:40 UTC 2016


commit 061586e36cb8d34e0db761e77fa8407b993632ce
Author: Malek <malek at mit.edu>
Date:   Sat Feb 6 16:30:49 2016 -0500

    decorated ipv6 address for directory send command
---
 src/or/directory.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/src/or/directory.c b/src/or/directory.c
index c1fa37f..13392d6 100644
--- a/src/or/directory.c
+++ b/src/or/directory.c
@@ -1159,6 +1159,23 @@ directory_get_consensus_url(const char *resource)
   return url;
 }
 
+/**
+ * Copies the ipv6 from source to destination, subject to buffer size limit
+ * size. If decorate is true, makes sure the copied address is decorated.
+ */
+static void
+copy_ipv6_address(char* destination, const char* source, size_t len,
+                  int decorate) {
+  tor_assert(destination);
+  tor_assert(source);
+
+  if (decorate && source[0] != '[') {
+    tor_snprintf(destination, len, "[%s]", source);
+  } else {
+    strlcpy(destination, source, len);
+  }
+}
+
 /** Queue an appropriate HTTP command on conn-\>outbuf.  The other args
  * are as in directory_initiate_command().
  */
@@ -1170,6 +1187,9 @@ directory_send_command(dir_connection_t *conn,
 {
   char proxystring[256];
   char hoststring[128];
+  /* NEEDS to be the same size hoststring.
+   Will be decorated with brackets around it if it is ipv6. */
+  char decorated_address[128];
   smartlist_t *headers = smartlist_new();
   char *url;
   char request[8192];
@@ -1182,12 +1202,20 @@ directory_send_command(dir_connection_t *conn,
   if (resource)
     conn->requested_resource = tor_strdup(resource);
 
+  /* decorate the ip address if it is ipv6 */
+  if(strchr(conn->base_.address, ':')) {
+    copy_ipv6_address(decorated_address, conn->base_.address,
+                      sizeof(decorated_address), 1);
+  } else {
+    strlcpy(decorated_address, conn->base_.address, sizeof(decorated_address));
+  }
+
   /* come up with a string for which Host: we want */
   if (conn->base_.port == 80) {
-    strlcpy(hoststring, conn->base_.address, sizeof(hoststring));
+    strlcpy(hoststring, decorated_address, sizeof(hoststring));
   } else {
-    tor_snprintf(hoststring, sizeof(hoststring),"%s:%d",
-                 conn->base_.address, conn->base_.port);
+    tor_snprintf(hoststring, sizeof(hoststring), "%s:%d",
+                 decorated_address, conn->base_.port);
   }
 
   /* Format if-modified-since */
@@ -4157,4 +4185,3 @@ dir_split_resource_into_fingerprints(const char *resource,
   smartlist_free(fp_tmp);
   return 0;
 }
-





More information about the tor-commits mailing list