Browse Source

hs: Refactor circuitmap to use circuit_t instead of or_circuit_t.

George Kadianakis 7 years ago
parent
commit
037ce360bd
6 changed files with 66 additions and 45 deletions
  1. 5 4
      src/or/circuitlist.c
  2. 46 26
      src/or/hs_circuitmap.c
  3. 2 2
      src/or/hs_circuitmap.h
  4. 7 7
      src/or/or.h
  5. 1 1
      src/or/rendmid.c
  6. 5 5
      src/test/test_circuitlist.c

+ 5 - 4
src/or/circuitlist.c

@@ -943,10 +943,6 @@ circuit_free(circuit_t *circ)
     crypto_cipher_free(ocirc->n_crypto);
     crypto_cipher_free(ocirc->n_crypto);
     crypto_digest_free(ocirc->n_digest);
     crypto_digest_free(ocirc->n_digest);
 
 
-    if (ocirc->hs_token) {
-      hs_circuitmap_remove_circuit(ocirc);
-    }
-
     if (ocirc->rend_splice) {
     if (ocirc->rend_splice) {
       or_circuit_t *other = ocirc->rend_splice;
       or_circuit_t *other = ocirc->rend_splice;
       tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
       tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
@@ -978,6 +974,11 @@ circuit_free(circuit_t *circ)
   /* Remove from map. */
   /* Remove from map. */
   circuit_set_n_circid_chan(circ, 0, NULL);
   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
   /* Clear cell queue _after_ removing it from the map.  Otherwise our
    * "active" checks will be violated. */
    * "active" checks will be violated. */
   cell_queue_clear(&circ->n_chan_cells);
   cell_queue_clear(&circ->n_chan_cells);

+ 46 - 26
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
 /* This is a helper function used by the hash table code (HT_). It returns 1 if
  * two circuits have the same HS token. */
  * two circuits have the same HS token. */
 static int
 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 *first_token;
   const hs_token_t *second_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
 /* 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.*/
  * HS token into an unsigned int for use as a key by the hash table routines.*/
 static inline unsigned int
 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);
   tor_assert(circuit->hs_token);
 
 
@@ -67,11 +67,11 @@ hs_circuit_hash_token(const or_circuit_t *circuit)
 
 
 /* Register the circuitmap hash table */
 /* Register the circuitmap hash table */
 HT_PROTOTYPE(hs_circuitmap_ht, // The name of the hashtable struct
 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_circuitmap_node,        // The name of HT_ENTRY member
              hs_circuit_hash_token, hs_circuits_have_same_token)
              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,
              hs_circuit_hash_token, hs_circuits_have_same_token,
              0.6, tor_reallocarray, tor_free_)
              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>. */
 /** 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)
 get_circuit_with_token(hs_token_t *search_token)
 {
 {
   tor_assert(the_hs_circuitmap);
   tor_assert(the_hs_circuitmap);
 
 
   /* We use a dummy circuit object for the hash table search routine. */
   /* 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;
   search_circ.hs_token = search_token;
   return HT_FIND(hs_circuitmap_ht, the_hs_circuitmap, &search_circ);
   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
 /* Helper function that registers <b>circ</b> with <b>token</b> on the HS
    circuitmap. This function steals reference of <b>token</b>. */
    circuitmap. This function steals reference of <b>token</b>. */
 static void
 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(circ);
   tor_assert(token);
   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
      take precedence over old ones, so that HSes and clients and reestablish
      killed circuits without changing the HS token. */
      killed circuits without changing the HS token. */
   {
   {
-    or_circuit_t *found_circ;
+    circuit_t *found_circ;
     found_circ = get_circuit_with_token(token);
     found_circ = get_circuit_with_token(token);
     if (found_circ) {
     if (found_circ) {
       hs_circuitmap_remove_circuit(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
  *  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. */
  *  <b>token</b> is not set, clear the circuit of any HS tokens. */
 static void
 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,
                                hs_token_type_t type, size_t token_len,
                                const uint8_t *token)
                                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>
  * 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
  * parameter and if it is NOT marked for close. Return NULL if no such circuit
  * is found. */
  * is found. */
-static or_circuit_t *
+static circuit_t *
 hs_circuitmap_get_circuit(hs_token_type_t type,
 hs_circuitmap_get_circuit(hs_token_type_t type,
                           size_t token_len,
                           size_t token_len,
                           const uint8_t *token,
                           const uint8_t *token,
                           uint8_t wanted_circ_purpose)
                           uint8_t wanted_circ_purpose)
 {
 {
-  or_circuit_t *found_circ = NULL;
+  circuit_t *found_circ = NULL;
 
 
   tor_assert(the_hs_circuitmap);
   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 */
   /* Check that the circuit is useful to us */
   if (!found_circ ||
   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;
     return NULL;
   }
   }
 
 
@@ -217,11 +216,18 @@ hs_circuitmap_get_circuit(hs_token_type_t type,
 or_circuit_t *
 or_circuit_t *
 hs_circuitmap_get_intro_circ_v3(const ed25519_public_key_t *auth_key)
 hs_circuitmap_get_intro_circ_v3(const ed25519_public_key_t *auth_key)
 {
 {
+  circuit_t *circ;
   tor_assert(auth_key);
   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,
                                    ED25519_PUBKEY_LEN, auth_key->pubkey,
                                    CIRCUIT_PURPOSE_INTRO_POINT);
                                    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
 /* 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 *
 or_circuit_t *
 hs_circuitmap_get_intro_circ_v2(const uint8_t *digest)
 hs_circuitmap_get_intro_circ_v2(const uint8_t *digest)
 {
 {
+  circuit_t *circ;
   tor_assert(digest);
   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,
                                    REND_TOKEN_LEN, digest,
                                    CIRCUIT_PURPOSE_INTRO_POINT);
                                    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
 /* Public function: Return rendezvous circuit with rendezvous
@@ -241,11 +254,18 @@ hs_circuitmap_get_intro_circ_v2(const uint8_t *digest)
 or_circuit_t *
 or_circuit_t *
 hs_circuitmap_get_rend_circ(const uint8_t *cookie)
 hs_circuitmap_get_rend_circ(const uint8_t *cookie)
 {
 {
+  circuit_t *circ;
   tor_assert(cookie);
   tor_assert(cookie);
 
 
-  return hs_circuitmap_get_circuit(HS_TOKEN_REND,
+  circ = hs_circuitmap_get_circuit(HS_TOKEN_REND,
                                    REND_TOKEN_LEN, cookie,
                                    REND_TOKEN_LEN, cookie,
                                    CIRCUIT_PURPOSE_REND_POINT_WAITING);
                                    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
 /* 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
 void
 hs_circuitmap_register_rend_circ(or_circuit_t *circ, const uint8_t *cookie)
 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,
                                  HS_TOKEN_REND,
                                  REND_TOKEN_LEN, cookie);
                                  REND_TOKEN_LEN, cookie);
 }
 }
@@ -263,7 +283,7 @@ hs_circuitmap_register_rend_circ(or_circuit_t *circ, const uint8_t *cookie)
 void
 void
 hs_circuitmap_register_intro_circ_v2(or_circuit_t *circ, const uint8_t *digest)
 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,
                                  HS_TOKEN_INTRO_V2,
                                  REND_TOKEN_LEN, digest);
                                  REND_TOKEN_LEN, digest);
 }
 }
@@ -274,7 +294,7 @@ void
 hs_circuitmap_register_intro_circ_v3(or_circuit_t *circ,
 hs_circuitmap_register_intro_circ_v3(or_circuit_t *circ,
                                      const ed25519_public_key_t *auth_key)
                                      const ed25519_public_key_t *auth_key)
 {
 {
-  hs_circuitmap_register_circuit(circ,
+  hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
                                  HS_TOKEN_INTRO_V3,
                                  HS_TOKEN_INTRO_V3,
                                  ED25519_PUBKEY_LEN, auth_key->pubkey);
                                  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
 /** Remove this circuit from the HS circuitmap. Clear its HS token, and remove
  *  it from the hashtable. */
  *  it from the hashtable. */
 void
 void
-hs_circuitmap_remove_circuit(or_circuit_t *circ)
+hs_circuitmap_remove_circuit(circuit_t *circ)
 {
 {
   tor_assert(the_hs_circuitmap);
   tor_assert(the_hs_circuitmap);
 
 
@@ -291,14 +311,14 @@ hs_circuitmap_remove_circuit(or_circuit_t *circ)
   }
   }
 
 
   /* Remove circ from circuitmap */
   /* Remove circ from circuitmap */
-  or_circuit_t *tmp;
+  circuit_t *tmp;
   tmp = HT_REMOVE(hs_circuitmap_ht, the_hs_circuitmap, circ);
   tmp = HT_REMOVE(hs_circuitmap_ht, the_hs_circuitmap, circ);
   /* ... and ensure the removal was successful. */
   /* ... and ensure the removal was successful. */
   if (tmp) {
   if (tmp) {
     tor_assert(tmp == circ);
     tor_assert(tmp == circ);
   } else {
   } else {
     log_warn(LD_BUG, "Could not find circuit (%u) in circuitmap.",
     log_warn(LD_BUG, "Could not find circuit (%u) in circuitmap.",
-             circ->p_circ_id);
+             circ->n_circ_id);
   }
   }
 
 
   /* Clear token from circ */
   /* Clear token from circ */

+ 2 - 2
src/or/hs_circuitmap.h

@@ -9,7 +9,7 @@
 #ifndef TOR_HS_CIRCUITMAP_H
 #ifndef TOR_HS_CIRCUITMAP_H
 #define 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;
 typedef struct hs_token_s hs_token_t;
 struct or_circuit_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,
 void hs_circuitmap_register_intro_circ_v3(struct or_circuit_t *circ,
                                          const ed25519_public_key_t *auth_key);
                                          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_init(void);
 void hs_circuitmap_free_all(void);
 void hs_circuitmap_free_all(void);

+ 7 - 7
src/or/or.h

@@ -3064,6 +3064,13 @@ typedef struct circuit_t {
    * circuit's queues; used only if CELL_STATS events are enabled and
    * circuit's queues; used only if CELL_STATS events are enabled and
    * cleared after being sent to control port. */
    * cleared after being sent to control port. */
   smartlist_t *testing_cell_stats;
   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;
 } circuit_t;
 
 
 /** Largest number of relay_early cells that we can send on a given
 /** 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. */
    * is not marked for close. */
   struct or_circuit_t *rend_splice;
   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. */
   /** Stores KH for the handshake. */
   char rend_circ_nonce[DIGEST_LEN];/* KH in tor-spec.txt */
   char rend_circ_nonce[DIGEST_LEN];/* KH in tor-spec.txt */
 
 

+ 1 - 1
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(circ), CIRCUIT_PURPOSE_REND_ESTABLISHED);
   circuit_change_purpose(TO_CIRCUIT(rend_circ),
   circuit_change_purpose(TO_CIRCUIT(rend_circ),
                          CIRCUIT_PURPOSE_REND_ESTABLISHED);
                          CIRCUIT_PURPOSE_REND_ESTABLISHED);
-  hs_circuitmap_remove_circuit(circ);
+  hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
 
 
   rend_circ->rend_splice = circ;
   rend_circ->rend_splice = circ;
   circ->rend_splice = rend_circ;
   circ->rend_splice = rend_circ;

+ 5 - 5
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(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. */
   /* 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));
   tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2(tok3));
 
 
  done:
  done: