[tor-commits] [tor/master] hs: Refactor circuitmap to use circuit_t instead of or_circuit_t.

nickm at torproject.org nickm at torproject.org
Thu Apr 13 18:37:01 UTC 2017


commit 037ce360bd0a7b0db1d6b6e441c90b1773b4cb06
Author: George Kadianakis <desnacked at riseup.net>
Date:   Wed Jan 18 12:01:49 2017 +0200

    hs: Refactor circuitmap to use circuit_t instead of or_circuit_t.
---
 src/or/circuitlist.c        |  9 +++---
 src/or/hs_circuitmap.c      | 72 +++++++++++++++++++++++++++++----------------
 src/or/hs_circuitmap.h      |  4 +--
 src/or/or.h                 | 14 ++++-----
 src/or/rendmid.c            |  2 +-
 src/test/test_circuitlist.c | 10 +++----
 6 files changed, 66 insertions(+), 45 deletions(-)

diff --git a/src/or/circuitlist.c b/src/or/circuitlist.c
index 738119f..80bb7f6 100644
--- a/src/or/circuitlist.c
+++ b/src/or/circuitlist.c
@@ -943,10 +943,6 @@ circuit_free(circuit_t *circ)
     crypto_cipher_free(ocirc->n_crypto);
     crypto_digest_free(ocirc->n_digest);
 
-    if (ocirc->hs_token) {
-      hs_circuitmap_remove_circuit(ocirc);
-    }
-
     if (ocirc->rend_splice) {
       or_circuit_t *other = ocirc->rend_splice;
       tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
@@ -978,6 +974,11 @@ circuit_free(circuit_t *circ)
   /* Remove from map. */
   circuit_set_n_circid_chan(circ, 0, NULL);
 
+  /* Clear HS circuitmap token from this circ (if any) */
+  if (circ->hs_token) {
+    hs_circuitmap_remove_circuit(circ);
+  }
+
   /* Clear cell queue _after_ removing it from the map.  Otherwise our
    * "active" checks will be violated. */
   cell_queue_clear(&circ->n_chan_cells);
diff --git a/src/or/hs_circuitmap.c b/src/or/hs_circuitmap.c
index 32017f1..4d5c893 100644
--- a/src/or/hs_circuitmap.c
+++ b/src/or/hs_circuitmap.c
@@ -25,8 +25,8 @@ static struct hs_circuitmap_ht *the_hs_circuitmap = NULL;
 /* This is a helper function used by the hash table code (HT_). It returns 1 if
  * two circuits have the same HS token. */
 static int
-hs_circuits_have_same_token(const or_circuit_t *first_circuit,
-                            const or_circuit_t *second_circuit)
+hs_circuits_have_same_token(const circuit_t *first_circuit,
+                            const circuit_t *second_circuit)
 {
   const hs_token_t *first_token;
   const hs_token_t *second_token;
@@ -57,7 +57,7 @@ hs_circuits_have_same_token(const or_circuit_t *first_circuit,
 /* This is a helper function for the hash table code (HT_). It hashes a circuit
  * HS token into an unsigned int for use as a key by the hash table routines.*/
 static inline unsigned int
-hs_circuit_hash_token(const or_circuit_t *circuit)
+hs_circuit_hash_token(const circuit_t *circuit)
 {
   tor_assert(circuit->hs_token);
 
@@ -67,11 +67,11 @@ hs_circuit_hash_token(const or_circuit_t *circuit)
 
 /* Register the circuitmap hash table */
 HT_PROTOTYPE(hs_circuitmap_ht, // The name of the hashtable struct
-             or_circuit_t,    // The name of the element struct,
+             circuit_t,    // The name of the element struct,
              hs_circuitmap_node,        // The name of HT_ENTRY member
              hs_circuit_hash_token, hs_circuits_have_same_token)
 
-HT_GENERATE2(hs_circuitmap_ht, or_circuit_t, hs_circuitmap_node,
+HT_GENERATE2(hs_circuitmap_ht, circuit_t, hs_circuitmap_node,
              hs_circuit_hash_token, hs_circuits_have_same_token,
              0.6, tor_reallocarray, tor_free_)
 
@@ -116,13 +116,13 @@ hs_token_free(hs_token_t *hs_token)
 }
 
 /** Return the circuit from the circuitmap with token <b>search_token</b>. */
-static or_circuit_t *
+static circuit_t *
 get_circuit_with_token(hs_token_t *search_token)
 {
   tor_assert(the_hs_circuitmap);
 
   /* We use a dummy circuit object for the hash table search routine. */
-  or_circuit_t search_circ;
+  circuit_t search_circ;
   search_circ.hs_token = search_token;
   return HT_FIND(hs_circuitmap_ht, the_hs_circuitmap, &search_circ);
 }
@@ -130,7 +130,7 @@ get_circuit_with_token(hs_token_t *search_token)
 /* Helper function that registers <b>circ</b> with <b>token</b> on the HS
    circuitmap. This function steals reference of <b>token</b>. */
 static void
-hs_circuitmap_register_impl(or_circuit_t *circ, hs_token_t *token)
+hs_circuitmap_register_impl(circuit_t *circ, hs_token_t *token)
 {
   tor_assert(circ);
   tor_assert(token);
@@ -145,13 +145,12 @@ hs_circuitmap_register_impl(or_circuit_t *circ, hs_token_t *token)
      take precedence over old ones, so that HSes and clients and reestablish
      killed circuits without changing the HS token. */
   {
-    or_circuit_t *found_circ;
+    circuit_t *found_circ;
     found_circ = get_circuit_with_token(token);
     if (found_circ) {
       hs_circuitmap_remove_circuit(found_circ);
-      if (!found_circ->base_.marked_for_close) {
-        circuit_mark_for_close(TO_CIRCUIT(found_circ),
-                               END_CIRC_REASON_FINISHED);
+      if (!found_circ->marked_for_close) {
+        circuit_mark_for_close(found_circ, END_CIRC_REASON_FINISHED);
       }
     }
   }
@@ -165,7 +164,7 @@ hs_circuitmap_register_impl(or_circuit_t *circ, hs_token_t *token)
  *  circuitmap. Use the HS <b>token</b> as the key to the hash table.  If
  *  <b>token</b> is not set, clear the circuit of any HS tokens. */
 static void
-hs_circuitmap_register_circuit(or_circuit_t *circ,
+hs_circuitmap_register_circuit(circuit_t *circ,
                                hs_token_type_t type, size_t token_len,
                                const uint8_t *token)
 {
@@ -182,13 +181,13 @@ hs_circuitmap_register_circuit(or_circuit_t *circ,
  * Only returns a circuit with purpose equal to the <b>wanted_circ_purpose</b>
  * parameter and if it is NOT marked for close. Return NULL if no such circuit
  * is found. */
-static or_circuit_t *
+static circuit_t *
 hs_circuitmap_get_circuit(hs_token_type_t type,
                           size_t token_len,
                           const uint8_t *token,
                           uint8_t wanted_circ_purpose)
 {
-  or_circuit_t *found_circ = NULL;
+  circuit_t *found_circ = NULL;
 
   tor_assert(the_hs_circuitmap);
 
@@ -202,8 +201,8 @@ hs_circuitmap_get_circuit(hs_token_type_t type,
 
   /* Check that the circuit is useful to us */
   if (!found_circ ||
-      found_circ->base_.purpose != wanted_circ_purpose ||
-      found_circ->base_.marked_for_close) {
+      found_circ->purpose != wanted_circ_purpose ||
+      found_circ->marked_for_close) {
     return NULL;
   }
 
@@ -217,11 +216,18 @@ hs_circuitmap_get_circuit(hs_token_type_t type,
 or_circuit_t *
 hs_circuitmap_get_intro_circ_v3(const ed25519_public_key_t *auth_key)
 {
+  circuit_t *circ;
   tor_assert(auth_key);
 
-  return hs_circuitmap_get_circuit(HS_TOKEN_INTRO_V3,
+  circ = hs_circuitmap_get_circuit(HS_TOKEN_INTRO_V3,
                                    ED25519_PUBKEY_LEN, auth_key->pubkey,
                                    CIRCUIT_PURPOSE_INTRO_POINT);
+  if (!circ) {
+    return NULL;
+  }
+
+  tor_assert(CIRCUIT_IS_ORCIRC(circ));
+  return TO_OR_CIRCUIT(circ);
 }
 
 /* Public function: Return v2 introduction circuit with <b>digest</b>. Return
@@ -229,11 +235,18 @@ hs_circuitmap_get_intro_circ_v3(const ed25519_public_key_t *auth_key)
 or_circuit_t *
 hs_circuitmap_get_intro_circ_v2(const uint8_t *digest)
 {
+  circuit_t *circ;
   tor_assert(digest);
 
-  return hs_circuitmap_get_circuit(HS_TOKEN_INTRO_V2,
+  circ = hs_circuitmap_get_circuit(HS_TOKEN_INTRO_V2,
                                    REND_TOKEN_LEN, digest,
                                    CIRCUIT_PURPOSE_INTRO_POINT);
+  if (!circ) {
+    return NULL;
+  }
+
+  tor_assert(CIRCUIT_IS_ORCIRC(circ));
+  return TO_OR_CIRCUIT(circ);
 }
 
 /* Public function: Return rendezvous circuit with rendezvous
@@ -241,11 +254,18 @@ hs_circuitmap_get_intro_circ_v2(const uint8_t *digest)
 or_circuit_t *
 hs_circuitmap_get_rend_circ(const uint8_t *cookie)
 {
+  circuit_t *circ;
   tor_assert(cookie);
 
-  return hs_circuitmap_get_circuit(HS_TOKEN_REND,
+  circ = hs_circuitmap_get_circuit(HS_TOKEN_REND,
                                    REND_TOKEN_LEN, cookie,
                                    CIRCUIT_PURPOSE_REND_POINT_WAITING);
+  if (!circ) {
+    return NULL;
+  }
+
+  tor_assert(CIRCUIT_IS_ORCIRC(circ));
+  return TO_OR_CIRCUIT(circ);
 }
 
 /* Public function: Register rendezvous circuit with key <b>cookie</b> to the
@@ -253,7 +273,7 @@ hs_circuitmap_get_rend_circ(const uint8_t *cookie)
 void
 hs_circuitmap_register_rend_circ(or_circuit_t *circ, const uint8_t *cookie)
 {
-  hs_circuitmap_register_circuit(circ,
+  hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
                                  HS_TOKEN_REND,
                                  REND_TOKEN_LEN, cookie);
 }
@@ -263,7 +283,7 @@ hs_circuitmap_register_rend_circ(or_circuit_t *circ, const uint8_t *cookie)
 void
 hs_circuitmap_register_intro_circ_v2(or_circuit_t *circ, const uint8_t *digest)
 {
-  hs_circuitmap_register_circuit(circ,
+  hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
                                  HS_TOKEN_INTRO_V2,
                                  REND_TOKEN_LEN, digest);
 }
@@ -274,7 +294,7 @@ void
 hs_circuitmap_register_intro_circ_v3(or_circuit_t *circ,
                                      const ed25519_public_key_t *auth_key)
 {
-  hs_circuitmap_register_circuit(circ,
+  hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
                                  HS_TOKEN_INTRO_V3,
                                  ED25519_PUBKEY_LEN, auth_key->pubkey);
 }
@@ -282,7 +302,7 @@ hs_circuitmap_register_intro_circ_v3(or_circuit_t *circ,
 /** Remove this circuit from the HS circuitmap. Clear its HS token, and remove
  *  it from the hashtable. */
 void
-hs_circuitmap_remove_circuit(or_circuit_t *circ)
+hs_circuitmap_remove_circuit(circuit_t *circ)
 {
   tor_assert(the_hs_circuitmap);
 
@@ -291,14 +311,14 @@ hs_circuitmap_remove_circuit(or_circuit_t *circ)
   }
 
   /* Remove circ from circuitmap */
-  or_circuit_t *tmp;
+  circuit_t *tmp;
   tmp = HT_REMOVE(hs_circuitmap_ht, the_hs_circuitmap, circ);
   /* ... and ensure the removal was successful. */
   if (tmp) {
     tor_assert(tmp == circ);
   } else {
     log_warn(LD_BUG, "Could not find circuit (%u) in circuitmap.",
-             circ->p_circ_id);
+             circ->n_circ_id);
   }
 
   /* Clear token from circ */
diff --git a/src/or/hs_circuitmap.h b/src/or/hs_circuitmap.h
index 6e0c2d4..79dea1a 100644
--- a/src/or/hs_circuitmap.h
+++ b/src/or/hs_circuitmap.h
@@ -9,7 +9,7 @@
 #ifndef TOR_HS_CIRCUITMAP_H
 #define TOR_HS_CIRCUITMAP_H
 
-typedef HT_HEAD(hs_circuitmap_ht, or_circuit_t) hs_circuitmap_ht;
+typedef HT_HEAD(hs_circuitmap_ht, circuit_t) hs_circuitmap_ht;
 
 typedef struct hs_token_s hs_token_t;
 struct or_circuit_t;
@@ -28,7 +28,7 @@ void hs_circuitmap_register_intro_circ_v2(struct or_circuit_t *circ,
 void hs_circuitmap_register_intro_circ_v3(struct or_circuit_t *circ,
                                          const ed25519_public_key_t *auth_key);
 
-void hs_circuitmap_remove_circuit(struct or_circuit_t *circ);
+void hs_circuitmap_remove_circuit(struct circuit_t *circ);
 
 void hs_circuitmap_init(void);
 void hs_circuitmap_free_all(void);
diff --git a/src/or/or.h b/src/or/or.h
index 8a73cc2..034cd5e 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -3064,6 +3064,13 @@ typedef struct circuit_t {
    * circuit's queues; used only if CELL_STATS events are enabled and
    * cleared after being sent to control port. */
   smartlist_t *testing_cell_stats;
+
+  /** If set, points to an HS token that this circuit might be carrying.
+   *  Used by the HS circuitmap.  */
+  hs_token_t *hs_token;
+  /** Hashtable node: used to look up the circuit by its HS token using the HS
+      circuitmap. */
+  HT_ENTRY(circuit_t) hs_circuitmap_node;
 } circuit_t;
 
 /** Largest number of relay_early cells that we can send on a given
@@ -3373,13 +3380,6 @@ typedef struct or_circuit_t {
    * is not marked for close. */
   struct or_circuit_t *rend_splice;
 
-  /** If set, points to an HS token that this circuit might be carrying.
-   *  Used by the HS circuitmap.  */
-  hs_token_t *hs_token;
-  /** Hashtable node: used to look up the circuit by its HS token using the HS
-      circuitmap. */
-  HT_ENTRY(or_circuit_t) hs_circuitmap_node;
-
   /** Stores KH for the handshake. */
   char rend_circ_nonce[DIGEST_LEN];/* KH in tor-spec.txt */
 
diff --git a/src/or/rendmid.c b/src/or/rendmid.c
index a7b37c7..b581f0e 100644
--- a/src/or/rendmid.c
+++ b/src/or/rendmid.c
@@ -342,7 +342,7 @@ rend_mid_rendezvous(or_circuit_t *circ, const uint8_t *request,
   circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
   circuit_change_purpose(TO_CIRCUIT(rend_circ),
                          CIRCUIT_PURPOSE_REND_ESTABLISHED);
-  hs_circuitmap_remove_circuit(circ);
+  hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
 
   rend_circ->rend_splice = circ;
   circ->rend_splice = rend_circ;
diff --git a/src/test/test_circuitlist.c b/src/test/test_circuitlist.c
index 61d3b43..ed18cbf 100644
--- a/src/test/test_circuitlist.c
+++ b/src/test/test_circuitlist.c
@@ -255,13 +255,13 @@ test_rend_token_maps(void *arg)
 
   tt_ptr_op(c4, OP_EQ, hs_circuitmap_get_intro_circ_v2(tok3));
 
-  tt_ptr_op(c3->hs_token, OP_EQ, NULL);
-  tt_ptr_op(c4->hs_token, OP_NE, NULL);
-  tt_mem_op(c4->hs_token->token, OP_EQ, tok3, REND_TOKEN_LEN);
+  tt_ptr_op(TO_CIRCUIT(c3)->hs_token, OP_EQ, NULL);
+  tt_ptr_op(TO_CIRCUIT(c4)->hs_token, OP_NE, NULL);
+  tt_mem_op(TO_CIRCUIT(c4)->hs_token->token, OP_EQ, tok3, REND_TOKEN_LEN);
 
   /* Now clear c4's cookie. */
-  hs_circuitmap_remove_circuit(c4);
-  tt_ptr_op(c4->hs_token, OP_EQ, NULL);
+  hs_circuitmap_remove_circuit(TO_CIRCUIT(c4));
+  tt_ptr_op(TO_CIRCUIT(c4)->hs_token, OP_EQ, NULL);
   tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2(tok3));
 
  done:





More information about the tor-commits mailing list