[tor-commits] [tor/master] hs: Move link specifier encoding to a function

nickm at torproject.org nickm at torproject.org
Thu Aug 24 19:13:51 UTC 2017


commit b13ee8e4ae59f85ce75800aa7dd90cfe58c04a5e
Author: David Goulet <dgoulet at torproject.org>
Date:   Tue Jul 18 13:56:19 2017 -0400

    hs: Move link specifier encoding to a function
    
    This commit only moves code into a function. The client code will need a way
    to take a bunch of descriptor link specifier object and encode them into link
    specifiers objects.
    
    Make this a public function so it can be used outside of hs_descriptor.c.
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/or/hs_descriptor.c | 102 ++++++++++++++++++++++++++++---------------------
 src/or/hs_descriptor.h |   5 +++
 2 files changed, 64 insertions(+), 43 deletions(-)

diff --git a/src/or/hs_descriptor.c b/src/or/hs_descriptor.c
index 1e595f3a3..71f16fee1 100644
--- a/src/or/hs_descriptor.c
+++ b/src/or/hs_descriptor.c
@@ -332,50 +332,10 @@ encode_link_specifiers(const smartlist_t *specs)
 
   SMARTLIST_FOREACH_BEGIN(specs, const hs_desc_link_specifier_t *,
                           spec) {
-    link_specifier_t *ls = link_specifier_new();
-    link_specifier_set_ls_type(ls, spec->type);
-
-    switch (spec->type) {
-    case LS_IPV4:
-      link_specifier_set_un_ipv4_addr(ls,
-                                      tor_addr_to_ipv4h(&spec->u.ap.addr));
-      link_specifier_set_un_ipv4_port(ls, spec->u.ap.port);
-      /* Four bytes IPv4 and two bytes port. */
-      link_specifier_set_ls_len(ls, sizeof(spec->u.ap.addr.addr.in_addr) +
-                                    sizeof(spec->u.ap.port));
-      break;
-    case LS_IPV6:
-    {
-      size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
-      const uint8_t *in6_addr = tor_addr_to_in6_addr8(&spec->u.ap.addr);
-      uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
-      memcpy(ipv6_array, in6_addr, addr_len);
-      link_specifier_set_un_ipv6_port(ls, spec->u.ap.port);
-      /* Sixteen bytes IPv6 and two bytes port. */
-      link_specifier_set_ls_len(ls, addr_len + sizeof(spec->u.ap.port));
-      break;
+    link_specifier_t *ls = hs_desc_encode_lspec(spec);
+    if (ls) {
+      link_specifier_list_add_spec(lslist, ls);
     }
-    case LS_LEGACY_ID:
-    {
-      size_t legacy_id_len = link_specifier_getlen_un_legacy_id(ls);
-      uint8_t *legacy_id_array = link_specifier_getarray_un_legacy_id(ls);
-      memcpy(legacy_id_array, spec->u.legacy_id, legacy_id_len);
-      link_specifier_set_ls_len(ls, legacy_id_len);
-      break;
-    }
-    case LS_ED25519_ID:
-    {
-      size_t ed25519_id_len = link_specifier_getlen_un_ed25519_id(ls);
-      uint8_t *ed25519_id_array = link_specifier_getarray_un_ed25519_id(ls);
-      memcpy(ed25519_id_array, spec->u.ed25519_id, ed25519_id_len);
-      link_specifier_set_ls_len(ls, ed25519_id_len);
-      break;
-    }
-    default:
-      tor_assert(0);
-    }
-
-    link_specifier_list_add_spec(lslist, ls);
   } SMARTLIST_FOREACH_END(spec);
 
   {
@@ -2575,3 +2535,59 @@ hs_descriptor_clear_intro_points(hs_descriptor_t *desc)
   }
 }
 
+/* From a descriptor link specifier object spec, returned a newly allocated
+ * link specifier object that is the encoded representation of spec. Return
+ * NULL on error. */
+link_specifier_t *
+hs_desc_encode_lspec(const hs_desc_link_specifier_t *spec)
+{
+  tor_assert(spec);
+
+  link_specifier_t *ls = link_specifier_new();
+  link_specifier_set_ls_type(ls, spec->type);
+
+  switch (spec->type) {
+  case LS_IPV4:
+    link_specifier_set_un_ipv4_addr(ls,
+                                    tor_addr_to_ipv4h(&spec->u.ap.addr));
+    link_specifier_set_un_ipv4_port(ls, spec->u.ap.port);
+    /* Four bytes IPv4 and two bytes port. */
+    link_specifier_set_ls_len(ls, sizeof(spec->u.ap.addr.addr.in_addr) +
+                                  sizeof(spec->u.ap.port));
+    break;
+  case LS_IPV6:
+  {
+    size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
+    const uint8_t *in6_addr = tor_addr_to_in6_addr8(&spec->u.ap.addr);
+    uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
+    memcpy(ipv6_array, in6_addr, addr_len);
+    link_specifier_set_un_ipv6_port(ls, spec->u.ap.port);
+    /* Sixteen bytes IPv6 and two bytes port. */
+    link_specifier_set_ls_len(ls, addr_len + sizeof(spec->u.ap.port));
+    break;
+  }
+  case LS_LEGACY_ID:
+  {
+    size_t legacy_id_len = link_specifier_getlen_un_legacy_id(ls);
+    uint8_t *legacy_id_array = link_specifier_getarray_un_legacy_id(ls);
+    memcpy(legacy_id_array, spec->u.legacy_id, legacy_id_len);
+    link_specifier_set_ls_len(ls, legacy_id_len);
+    break;
+  }
+  case LS_ED25519_ID:
+  {
+    size_t ed25519_id_len = link_specifier_getlen_un_ed25519_id(ls);
+    uint8_t *ed25519_id_array = link_specifier_getarray_un_ed25519_id(ls);
+    memcpy(ed25519_id_array, spec->u.ed25519_id, ed25519_id_len);
+    link_specifier_set_ls_len(ls, ed25519_id_len);
+    break;
+  }
+  default:
+    tor_assert_nonfatal_unreached();
+    link_specifier_free(ls);
+    ls = NULL;
+  }
+
+  return ls;
+}
+
diff --git a/src/or/hs_descriptor.h b/src/or/hs_descriptor.h
index 256a04dd6..b4cda7b84 100644
--- a/src/or/hs_descriptor.h
+++ b/src/or/hs_descriptor.h
@@ -18,6 +18,9 @@
 #include "crypto_ed25519.h"
 #include "torcert.h"
 
+/* Trunnel */
+#include "ed25519_cert.h"
+
 /* The earliest descriptor format version we support. */
 #define HS_DESC_SUPPORTED_FORMAT_VERSION_MIN 3
 /* The latest descriptor format version we support. */
@@ -230,6 +233,8 @@ size_t hs_desc_plaintext_obj_size(const hs_desc_plaintext_data_t *data);
 hs_desc_intro_point_t *hs_desc_intro_point_new(void);
 void hs_desc_intro_point_free(hs_desc_intro_point_t *ip);
 
+link_specifier_t *hs_desc_encode_lspec(const hs_desc_link_specifier_t *spec);
+
 #ifdef HS_DESCRIPTOR_PRIVATE
 
 /* Encoding. */





More information about the tor-commits mailing list