Ticket #23094: b23094-r001.patch

File b23094-r001.patch, 10.6 KB (added by neel, 21 months ago)

Patch (Revision 1)

  • new file changes/bug23094

    From 768a55b6d69ad9f6b6b200ce24a2637668a891ba Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Sat, 28 Apr 2018 19:56:12 -0400
    Subject: [PATCH] Make hsdir_index in node_t a hsdir_index_t rather than a
     pointer.
    
    ---
     changes/bug23094           |  4 ++++
     src/or/hs_common.c         | 27 ++++++++++++---------------
     src/or/hs_common.h         | 13 -------------
     src/or/hs_control.c        |  3 +--
     src/or/hs_service.c        |  4 ++--
     src/or/nodelist.c          | 16 +++++++---------
     src/or/or.h                | 17 ++++++++++++++---
     src/test/test_hs_control.c |  5 ++---
     8 files changed, 42 insertions(+), 47 deletions(-)
     create mode 100644 changes/bug23094
    
    diff --git a/changes/bug23094 b/changes/bug23094
    new file mode 100644
    index 000000000..5040ab4e7
    - +  
     1  o Code simplification and refactoring:
     2    - Make hsdir_index in node_t a hsdir_index_t rather than a pointer
     3      as hsdir_index is always present. Also, we move hsdir_index_t into
     4      or.h. Closes ticket 23094. Patch by Neel Chauhan.
  • src/or/hs_common.c

    diff --git a/src/or/hs_common.c b/src/or/hs_common.c
    index 24eb7a104..c01428099 100644
    a b compare_digest_to_fetch_hsdir_index(const void *_key, const void **_member) 
    103103{
    104104  const char *key = _key;
    105105  const node_t *node = *_member;
    106   return tor_memcmp(key, node->hsdir_index->fetch, DIGEST256_LEN);
     106  return tor_memcmp(key, node->hsdir_index.fetch, DIGEST256_LEN);
    107107}
    108108
    109109/* Helper function: The key is a digest that we compare to a node_t object
    compare_digest_to_store_first_hsdir_index(const void *_key, 
    114114{
    115115  const char *key = _key;
    116116  const node_t *node = *_member;
    117   return tor_memcmp(key, node->hsdir_index->store_first, DIGEST256_LEN);
     117  return tor_memcmp(key, node->hsdir_index.store_first, DIGEST256_LEN);
    118118}
    119119
    120120/* Helper function: The key is a digest that we compare to a node_t object
    compare_digest_to_store_second_hsdir_index(const void *_key, 
    125125{
    126126  const char *key = _key;
    127127  const node_t *node = *_member;
    128   return tor_memcmp(key, node->hsdir_index->store_second, DIGEST256_LEN);
     128  return tor_memcmp(key, node->hsdir_index.store_second, DIGEST256_LEN);
    129129}
    130130
    131131/* Helper function: Compare two node_t objects current hsdir_index. */
    compare_node_fetch_hsdir_index(const void **a, const void **b) 
    134134{
    135135  const node_t *node1= *a;
    136136  const node_t *node2 = *b;
    137   return tor_memcmp(node1->hsdir_index->fetch,
    138                     node2->hsdir_index->fetch,
     137  return tor_memcmp(node1->hsdir_index.fetch,
     138                    node2->hsdir_index.fetch,
    139139                    DIGEST256_LEN);
    140140}
    141141
    compare_node_store_first_hsdir_index(const void **a, const void **b) 
    145145{
    146146  const node_t *node1= *a;
    147147  const node_t *node2 = *b;
    148   return tor_memcmp(node1->hsdir_index->store_first,
    149                     node2->hsdir_index->store_first,
     148  return tor_memcmp(node1->hsdir_index.store_first,
     149                    node2->hsdir_index.store_first,
    150150                    DIGEST256_LEN);
    151151}
    152152
    compare_node_store_second_hsdir_index(const void **a, const void **b) 
    156156{
    157157  const node_t *node1= *a;
    158158  const node_t *node2 = *b;
    159   return tor_memcmp(node1->hsdir_index->store_second,
    160                     node2->hsdir_index->store_second,
     159  return tor_memcmp(node1->hsdir_index.store_second,
     160                    node2->hsdir_index.store_second,
    161161                    DIGEST256_LEN);
    162162}
    163163
    node_has_hsdir_index(const node_t *node) 
    12881288
    12891289  /* At this point, since the node has a desc, this node must also have an
    12901290   * hsdir index. If not, something went wrong, so BUG out. */
    1291   if (BUG(node->hsdir_index == NULL)) {
    1292     return 0;
    1293   }
    1294   if (BUG(tor_mem_is_zero((const char*)node->hsdir_index->fetch,
     1291  if (BUG(tor_mem_is_zero((const char*)node->hsdir_index.fetch,
    12951292                          DIGEST256_LEN))) {
    12961293    return 0;
    12971294  }
    1298   if (BUG(tor_mem_is_zero((const char*)node->hsdir_index->store_first,
     1295  if (BUG(tor_mem_is_zero((const char*)node->hsdir_index.store_first,
    12991296                          DIGEST256_LEN))) {
    13001297    return 0;
    13011298  }
    1302   if (BUG(tor_mem_is_zero((const char*)node->hsdir_index->store_second,
     1299  if (BUG(tor_mem_is_zero((const char*)node->hsdir_index.store_second,
    13031300                          DIGEST256_LEN))) {
    13041301    return 0;
    13051302  }
  • src/or/hs_common.h

    diff --git a/src/or/hs_common.h b/src/or/hs_common.h
    index 83ba1b859..ef7d5dca2 100644
    a b typedef struct rend_service_port_config_t { 
    156156  char unix_addr[FLEXIBLE_ARRAY_MEMBER];
    157157} rend_service_port_config_t;
    158158
    159 /* Hidden service directory index used in a node_t which is set once we set
    160  * the consensus. */
    161 typedef struct hsdir_index_t {
    162   /* HSDir index to use when fetching a descriptor. */
    163   uint8_t fetch[DIGEST256_LEN];
    164 
    165   /* HSDir index used by services to store their first and second
    166    * descriptor. The first descriptor is chronologically older than the second
    167    * one and uses older TP and SRV values. */
    168   uint8_t store_first[DIGEST256_LEN];
    169   uint8_t store_second[DIGEST256_LEN];
    170 } hsdir_index_t;
    171 
    172159void hs_init(void);
    173160void hs_free_all(void);
    174161
  • src/or/hs_control.c

    diff --git a/src/or/hs_control.c b/src/or/hs_control.c
    index 87b4e3fca..eca9ed1dd 100644
    a b hs_control_desc_event_requested(const ed25519_public_key_t *onion_pk, 
    3939   * can't pick a node without an hsdir_index. */
    4040  hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
    4141  tor_assert(hsdir_node);
    42   tor_assert(hsdir_node->hsdir_index);
    4342  /* This is a fetch event. */
    44   hsdir_index = hsdir_node->hsdir_index->fetch;
     43  hsdir_index = hsdir_node->hsdir_index.fetch;
    4544
    4645  /* Trigger the event. */
    4746  control_event_hs_descriptor_requested(onion_address, REND_NO_AUTH,
  • src/or/hs_service.c

    diff --git a/src/or/hs_service.c b/src/or/hs_service.c
    index 7af14373d..4686eda0b 100644
    a b upload_descriptor_to_hsdir(const hs_service_t *service, 
    23042304  /* Logging so we know where it was sent. */
    23052305  {
    23062306    int is_next_desc = (service->desc_next == desc);
    2307     const uint8_t *idx = (is_next_desc) ? hsdir->hsdir_index->store_second:
    2308                                           hsdir->hsdir_index->store_first;
     2307    const uint8_t *idx = (is_next_desc) ? hsdir->hsdir_index.store_second:
     2308                                          hsdir->hsdir_index.store_first;
    23092309    log_info(LD_REND, "Service %s %s descriptor of revision %" PRIu64
    23102310                      " initiated upload request to %s with index %s",
    23112311             safe_str_client(service->onion_address),
  • src/or/nodelist.c

    diff --git a/src/or/nodelist.c b/src/or/nodelist.c
    index e7342f979..675cbb005 100644
    a b node_get_or_create(const char *identity_digest) 
    225225
    226226  smartlist_add(the_nodelist->nodes, node);
    227227  node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
    228   node->hsdir_index = tor_malloc_zero(sizeof(hsdir_index_t));
    229228
    230229  node->country = -1;
    231230
    node_set_hsdir_index(node_t *node, const networkstatus_t *ns) 
    350349
    351350  /* Build the fetch index. */
    352351  hs_build_hsdir_index(node_identity_pk, fetch_srv, fetch_tp,
    353                        node->hsdir_index->fetch);
     352                       node->hsdir_index.fetch);
    354353
    355354  /* If we are in the time segment between SRV#N and TP#N, the fetch index is
    356355     the same as the first store index */
    357356  if (!hs_in_period_between_tp_and_srv(ns, now)) {
    358     memcpy(node->hsdir_index->store_first, node->hsdir_index->fetch,
    359            sizeof(node->hsdir_index->store_first));
     357    memcpy(node->hsdir_index.store_first, node->hsdir_index.fetch,
     358           sizeof(node->hsdir_index.store_first));
    360359  } else {
    361360    hs_build_hsdir_index(node_identity_pk, store_first_srv, store_first_tp,
    362                          node->hsdir_index->store_first);
     361                         node->hsdir_index.store_first);
    363362  }
    364363
    365364  /* If we are in the time segment between TP#N and SRV#N+1, the fetch index is
    366365     the same as the second store index */
    367366  if (hs_in_period_between_tp_and_srv(ns, now)) {
    368     memcpy(node->hsdir_index->store_second, node->hsdir_index->fetch,
    369            sizeof(node->hsdir_index->store_second));
     367    memcpy(node->hsdir_index.store_second, node->hsdir_index.fetch,
     368           sizeof(node->hsdir_index.store_second));
    370369  } else {
    371370    hs_build_hsdir_index(node_identity_pk, store_second_srv, store_second_tp,
    372                          node->hsdir_index->store_second);
     371                         node->hsdir_index.store_second);
    373372  }
    374373
    375374 done:
    node_free_(node_t *node) 
    720719  if (node->md)
    721720    node->md->held_by_nodes--;
    722721  tor_assert(node->nodelist_idx == -1);
    723   tor_free(node->hsdir_index);
    724722  tor_free(node);
    725723}
    726724
  • src/or/or.h

    diff --git a/src/or/or.h b/src/or/or.h
    index 475274340..e8edb0067 100644
    a b rend_data_v2_t *TO_REND_DATA_V2(const rend_data_t *d) 
    894894struct hs_ident_edge_conn_t;
    895895struct hs_ident_dir_conn_t;
    896896struct hs_ident_circuit_t;
    897 /* Stub because we can't include hs_common.h. */
    898 struct hsdir_index_t;
     897
     898/* Hidden service directory index used in a node_t which is set once we set
     899 * the consensus. */
     900typedef struct hsdir_index_t {
     901  /* HSDir index to use when fetching a descriptor. */
     902  uint8_t fetch[DIGEST256_LEN];
     903
     904  /* HSDir index used by services to store their first and second
     905   * descriptor. The first descriptor is chronologically older than the second
     906   * one and uses older TP and SRV values. */
     907  uint8_t store_first[DIGEST256_LEN];
     908  uint8_t store_second[DIGEST256_LEN];
     909} hsdir_index_t;
    899910
    900911/** Time interval for tracking replays of DH public keys received in
    901912 * INTRODUCE2 cells.  Used only to avoid launching multiple
    typedef struct node_t { 
    25612572  /* Hidden service directory index data. This is used by a service or client
    25622573   * in order to know what's the hs directory index for this node at the time
    25632574   * the consensus is set. */
    2564   struct hsdir_index_t *hsdir_index;
     2575  struct hsdir_index_t hsdir_index;
    25652576} node_t;
    25662577
    25672578/** Linked list of microdesc hash lines for a single router in a directory
  • src/test/test_hs_control.c

    diff --git a/src/test/test_hs_control.c b/src/test/test_hs_control.c
    index 207a55de6..308843e9b 100644
    a b mock_node_get_by_id(const char *digest) 
    7676{
    7777  static node_t node;
    7878  memcpy(node.identity, digest, DIGEST_LEN);
    79   node.hsdir_index = tor_malloc_zero(sizeof(hsdir_index_t));
    80   memset(node.hsdir_index->fetch, 'C', DIGEST256_LEN);
    81   memset(node.hsdir_index->store_first, 'D', DIGEST256_LEN);
     79  memset(node.hsdir_index.fetch, 'C', DIGEST256_LEN);
     80  memset(node.hsdir_index.store_first, 'D', DIGEST256_LEN);
    8281  return &node;
    8382}
    8483