[tor-commits] [tor/master] control: Add GETINFO command for the shared random values

nickm at torproject.org nickm at torproject.org
Wed Jan 11 19:36:30 UTC 2017


commit 8a33abcd65ebebf268750612d52684659f8df71d
Author: David Goulet <dgoulet at torproject.org>
Date:   Mon Jan 9 11:33:05 2017 -0500

    control: Add GETINFO command for the shared random values
    
    Add the "sr/current" and "sr/previous" keys for the GETINFO command in order
    to get through the control port the shared random values from the consensus.
    
    Closes #19925
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/or/control.c       | 23 +++++++++++++++++++++++
 src/or/shared_random.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 src/or/shared_random.h |  4 ++++
 3 files changed, 73 insertions(+)

diff --git a/src/or/control.c b/src/or/control.c
index 248c780..9f342f1 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -71,6 +71,7 @@
 #include "router.h"
 #include "routerlist.h"
 #include "routerparse.h"
+#include "shared_random.h"
 
 #ifndef _WIN32
 #include <pwd.h>
@@ -2870,6 +2871,26 @@ getinfo_helper_liveness(control_connection_t *control_conn,
   return 0;
 }
 
+/** Implementation helper for GETINFO: answers queries about shared random
+ * value. */
+static int
+getinfo_helper_sr(control_connection_t *control_conn,
+                  const char *question, char **answer,
+                  const char **errmsg)
+{
+  (void) control_conn;
+  (void) errmsg;
+
+  if (!strcmp(question, "sr/current")) {
+    *answer = sr_get_current_for_control();
+  } else if(!strcmp(question, "sr/previous")) {
+    *answer = sr_get_previous_for_control();
+  }
+  /* Else statement here is unrecognized key so do nothing. */
+
+  return 0;
+}
+
 /** Callback function for GETINFO: on a given control connection, try to
  * answer the question <b>q</b> and store the newly-allocated answer in
  * *<b>a</b>. If an internal error occurs, return -1 and optionally set
@@ -3062,6 +3083,8 @@ static const getinfo_item_t getinfo_items[] = {
        "Onion services owned by the current control connection."),
   ITEM("onions/detached", onions,
        "Onion services detached from the control connection."),
+  ITEM("sr/current", sr, "Get current shared random value."),
+  ITEM("sr/previous", sr, "Get previous shared random value."),
   { NULL, NULL, NULL, 0 }
 };
 
diff --git a/src/or/shared_random.c b/src/or/shared_random.c
index 0eb9338..f798a51 100644
--- a/src/or/shared_random.c
+++ b/src/or/shared_random.c
@@ -502,6 +502,20 @@ get_vote_line_from_commit(const sr_commit_t *commit, sr_phase_t phase)
   return vote_line;
 }
 
+/* Convert a given srv object to a string for the control port. This doesn't
+ * fail and the srv object MUST be valid. */
+static char *
+srv_to_control_string(const sr_srv_t *srv)
+{
+  char *srv_str;
+  char srv_hash_encoded[SR_SRV_VALUE_BASE64_LEN + 1];
+  tor_assert(srv);
+
+  sr_srv_encode(srv_hash_encoded, sizeof(srv_hash_encoded), srv);
+  tor_asprintf(&srv_str, "%s", srv_hash_encoded);
+  return srv_str;
+}
+
 /* Return a heap allocated string that contains the given <b>srv</b> string
  * representation formatted for a networkstatus document using the
  * <b>key</b> as the start of the line. This doesn't return NULL. */
@@ -1348,6 +1362,38 @@ sr_save_and_cleanup(void)
   sr_cleanup();
 }
 
+/* Return the current SRV string representation for the control port. Return a
+ * newly allocated string on success containing the value else "" if not found
+ * or if we don't have a valid consensus yet. */
+char *
+sr_get_current_for_control(void)
+{
+  char *srv_str;
+  const networkstatus_t *c = networkstatus_get_latest_consensus();
+  if (c && c->sr_info.current_srv) {
+    srv_str = srv_to_control_string(c->sr_info.current_srv);
+  } else {
+    srv_str = tor_strdup("");
+  }
+  return srv_str;
+}
+
+/* Return the previous SRV string representation for the control port. Return
+ * a newly allocated string on success containing the value else "" if not
+ * found or if we don't have a valid consensus yet. */
+char *
+sr_get_previous_for_control(void)
+{
+  char *srv_str;
+  const networkstatus_t *c = networkstatus_get_latest_consensus();
+  if (c && c->sr_info.previous_srv) {
+    srv_str = srv_to_control_string(c->sr_info.previous_srv);
+  } else {
+    srv_str = tor_strdup("");
+  }
+  return srv_str;
+}
+
 #ifdef TOR_UNIT_TESTS
 
 /* Set the global value of number of SRV agreements so the test can play
diff --git a/src/or/shared_random.h b/src/or/shared_random.h
index 9885934..dbb8eff 100644
--- a/src/or/shared_random.h
+++ b/src/or/shared_random.h
@@ -129,6 +129,10 @@ const char *sr_commit_get_rsa_fpr(const sr_commit_t *commit)
 void sr_compute_srv(void);
 sr_commit_t *sr_generate_our_commit(time_t timestamp,
                                     const authority_cert_t *my_rsa_cert);
+
+char *sr_get_current_for_control(void);
+char *sr_get_previous_for_control(void);
+
 #ifdef SHARED_RANDOM_PRIVATE
 
 /* Encode */





More information about the tor-commits mailing list