[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] control: Refactor control_event_hs_descriptor_receive_end



commit beacbbe2106f9af64a64fea3f69e5dcd43c1569e
Author: David Goulet <dgoulet@xxxxxxxxxxxxxx>
Date:   Fri Nov 10 11:16:16 2017 -0500

    control: Refactor control_event_hs_descriptor_receive_end
    
    First, rename and make that function static because it is internal to
    control.c and called by two HS_DESC events.
    
    Second, make it take more basic parameters and thus not a rend_data_t object
    so we can still use the function for v3 HS that doesn't use that object.
    
    Third, move the descriptor ID lookup to the two specific events (yes little
    code duplication there) because they get a rend_data_t object which won't be
    the case for v3.
    
    Finally, through this refactoring, change the pointer check to BUG() and
    change some parameter names to reflect what they really are.
    
    No behavior change at this commit.
    
    Signed-off-by: David Goulet <dgoulet@xxxxxxxxxxxxxx>
---
 src/or/control.c | 93 ++++++++++++++++++++++++++++++++------------------------
 src/or/control.h |  5 ---
 2 files changed, 54 insertions(+), 44 deletions(-)

diff --git a/src/or/control.c b/src/or/control.c
index 9a8d1f302..99acbcdcb 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -7289,33 +7289,20 @@ control_event_hs_descriptor_upload(const char *onion_address,
  *
  * So do not call this function directly.
  */
-void
-control_event_hs_descriptor_receive_end(const char *action,
-                                        const char *onion_address,
-                                        const rend_data_t *rend_data,
-                                        const char *id_digest,
-                                        const char *reason)
+static void
+event_hs_descriptor_receive_end(const char *action,
+                                const char *onion_address,
+                                const char *desc_id,
+                                rend_auth_type_t auth_type,
+                                const char *hsdir_id_digest,
+                                const char *reason)
 {
-  char *desc_id_field = NULL;
   char *reason_field = NULL;
-  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
-  const char *desc_id = NULL;
 
-  if (!action || !rend_data || !onion_address) {
-    log_warn(LD_BUG, "Called with action==%p, rend_data==%p, "
-                     "onion_address==%p", action, rend_data, onion_address);
+  if (BUG(!action || !onion_address)) {
     return;
   }
 
-  desc_id = get_desc_id_from_query(rend_data, id_digest);
-  if (desc_id != NULL) {
-    /* Set the descriptor ID digest to base32 so we can send it. */
-    base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
-                  DIGEST_LEN);
-    /* Extra whitespace is needed before the value. */
-    tor_asprintf(&desc_id_field, " %s", desc_id_base32);
-  }
-
   if (reason) {
     tor_asprintf(&reason_field, " REASON=%s", reason);
   }
@@ -7324,14 +7311,13 @@ control_event_hs_descriptor_receive_end(const char *action,
                      "650 HS_DESC %s %s %s %s%s%s\r\n",
                      action,
                      rend_hsaddress_str_or_unknown(onion_address),
-                     rend_auth_type_to_string(
-                          TO_REND_DATA_V2(rend_data)->auth_type),
-                     id_digest ?
-                        node_describe_longname_by_id(id_digest) : "UNKNOWN",
-                     desc_id_field ? desc_id_field : "",
+                     rend_auth_type_to_string(auth_type),
+                     hsdir_id_digest ?
+                        node_describe_longname_by_id(hsdir_id_digest) :
+                        "UNKNOWN",
+                     desc_id ? desc_id : "",
                      reason_field ? reason_field : "");
 
-  tor_free(desc_id_field);
   tor_free(reason_field);
 }
 
@@ -7376,15 +7362,29 @@ control_event_hs_descriptor_upload_end(const char *action,
 void
 control_event_hs_descriptor_received(const char *onion_address,
                                      const rend_data_t *rend_data,
-                                     const char *id_digest)
+                                     const char *hsdir_id_digest)
 {
-  if (!rend_data || !id_digest || !onion_address) {
-    log_warn(LD_BUG, "Called with rend_data==%p, id_digest==%p, "
-             "onion_address==%p", rend_data, id_digest, onion_address);
+  char *desc_id_field = NULL;
+  const char *desc_id;
+
+  if (BUG(!rend_data || !hsdir_id_digest || !onion_address)) {
     return;
   }
-  control_event_hs_descriptor_receive_end("RECEIVED", onion_address,
-                                          rend_data, id_digest, NULL);
+
+  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
+  if (desc_id != NULL) {
+    char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
+    /* Set the descriptor ID digest to base32 so we can send it. */
+    base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
+                  DIGEST_LEN);
+    /* Extra whitespace is needed before the value. */
+    tor_asprintf(&desc_id_field, " %s", desc_id_base32);
+  }
+
+  event_hs_descriptor_receive_end("RECEIVED", onion_address, desc_id_field,
+                                  TO_REND_DATA_V2(rend_data)->auth_type,
+                                  hsdir_id_digest, NULL);
+  tor_free(desc_id_field);
 }
 
 /** send HS_DESC UPLOADED event
@@ -7410,16 +7410,31 @@ control_event_hs_descriptor_uploaded(const char *id_digest,
  */
 void
 control_event_hs_descriptor_failed(const rend_data_t *rend_data,
-                                   const char *id_digest,
+                                   const char *hsdir_id_digest,
                                    const char *reason)
 {
-  if (!rend_data) {
-    log_warn(LD_BUG, "Called with rend_data==%p", rend_data);
+  char *desc_id_field = NULL;
+  const char *desc_id;
+
+  if (BUG(!rend_data)) {
     return;
   }
-  control_event_hs_descriptor_receive_end("FAILED",
-                                          rend_data_get_address(rend_data),
-                                          rend_data, id_digest, reason);
+
+  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
+  if (desc_id != NULL) {
+    char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
+    /* Set the descriptor ID digest to base32 so we can send it. */
+    base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
+                  DIGEST_LEN);
+    /* Extra whitespace is needed before the value. */
+    tor_asprintf(&desc_id_field, " %s", desc_id_base32);
+  }
+
+  event_hs_descriptor_receive_end("FAILED", rend_data_get_address(rend_data),
+                                  desc_id_field,
+                                  TO_REND_DATA_V2(rend_data)->auth_type,
+                                  hsdir_id_digest, reason);
+  tor_free(desc_id_field);
 }
 
 /** Send HS_DESC_CONTENT event after completion of a successful fetch from hs
diff --git a/src/or/control.h b/src/or/control.h
index 45fc4a9bc..a5f2599d2 100644
--- a/src/or/control.h
+++ b/src/or/control.h
@@ -125,11 +125,6 @@ void control_event_hs_descriptor_created(const char *onion_address,
 void control_event_hs_descriptor_upload(const char *onion_address,
                                         const char *desc_id,
                                         const char *hs_dir);
-void control_event_hs_descriptor_receive_end(const char *action,
-                                             const char *onion_address,
-                                             const rend_data_t *rend_data,
-                                             const char *id_digest,
-                                             const char *reason);
 void control_event_hs_descriptor_upload_end(const char *action,
                                             const char *onion_address,
                                             const char *hs_dir,



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits