]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
dirvote: Move SR commit parsing into dirauth module
authorDavid Goulet <dgoulet@torproject.org>
Wed, 25 Apr 2018 14:42:56 +0000 (10:42 -0400)
committerDavid Goulet <dgoulet@torproject.org>
Fri, 27 Apr 2018 15:40:44 +0000 (11:40 -0400)
When parsing a vote in routerparse.c, only dirauth extract the commits from
the vote so move all this code into dirvote.c so we can make it specific to
the dirauth module.

If the dirauth module is disabled, the commit parsing does nothing.

Signed-off-by: David Goulet <dgoulet@torproject.org>
src/or/dirauth/dirvote.c
src/or/dirauth/dirvote.h
src/or/routerparse.c

index b28bcf5ce5a44847ef9332aea8ae84d2a6b40d62..3ec4d67416b998ca237758908ab55e315eff72d0 100644 (file)
@@ -13,6 +13,7 @@
 #include "dirvote_common.h"
 #include "microdesc.h"
 #include "networkstatus.h"
+#include "parsecommon.h"
 #include "policies.h"
 #include "protover.h"
 #include "rephist.h"
@@ -3828,3 +3829,73 @@ dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now,
   return result;
 }
 
+/** Parse and extract all SR commits from <b>tokens</b> and place them in
+ *  <b>ns</b>. */
+static void
+extract_shared_random_commits(networkstatus_t *ns, smartlist_t *tokens)
+{
+  smartlist_t *chunks = NULL;
+
+  tor_assert(ns);
+  tor_assert(tokens);
+  /* Commits are only present in a vote. */
+  tor_assert(ns->type == NS_TYPE_VOTE);
+
+  ns->sr_info.commits = smartlist_new();
+
+  smartlist_t *commits = find_all_by_keyword(tokens, K_COMMIT);
+  /* It's normal that a vote might contain no commits even if it participates
+   * in the SR protocol. Don't treat it as an error. */
+  if (commits == NULL) {
+    goto end;
+  }
+
+  /* Parse the commit. We do NO validation of number of arguments or ordering
+   * for forward compatibility, it's the parse commit job to inform us if it's
+   * supported or not. */
+  chunks = smartlist_new();
+  SMARTLIST_FOREACH_BEGIN(commits, directory_token_t *, tok) {
+    /* Extract all arguments and put them in the chunks list. */
+    for (int i = 0; i < tok->n_args; i++) {
+      smartlist_add(chunks, tok->args[i]);
+    }
+    sr_commit_t *commit = sr_parse_commit(chunks);
+    smartlist_clear(chunks);
+    if (commit == NULL) {
+      /* Get voter identity so we can warn that this dirauth vote contains
+       * commit we can't parse. */
+      networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0);
+      tor_assert(voter);
+      log_warn(LD_DIR, "SR: Unable to parse commit %s from vote of voter %s.",
+               escaped(tok->object_body),
+               hex_str(voter->identity_digest,
+                       sizeof(voter->identity_digest)));
+      /* Commitment couldn't be parsed. Continue onto the next commit because
+       * this one could be unsupported for instance. */
+      continue;
+    }
+    /* Add newly created commit object to the vote. */
+    smartlist_add(ns->sr_info.commits, commit);
+  } SMARTLIST_FOREACH_END(tok);
+
+ end:
+  smartlist_free(chunks);
+  smartlist_free(commits);
+}
+
+/* Using the given directory tokens in tokens, parse the shared random commits
+ * and put them in the given vote document ns.
+ *
+ * This also sets the SR participation flag if present in the vote. */
+void
+dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens)
+{
+  /* Does this authority participates in the SR protocol? */
+  directory_token_t *tok = find_opt_by_keyword(tokens, K_SR_FLAG);
+  if (tok) {
+    ns->sr_info.participate = 1;
+    /* Get the SR commitments and reveals from the vote. */
+    extract_shared_random_commits(ns, tokens);
+  }
+}
+
index fcc7cecf26b8b48f79e1c5d027ac19367609e538..4514c6daf01143eb637e23fdec256db833ad4d2d 100644 (file)
@@ -99,6 +99,8 @@
 void dirvote_act(const or_options_t *options, time_t now);
 void dirvote_free_all(void);
 
+void dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens);
+
 #else /* HAVE_MODULE_DIRAUTH */
 
 static inline void
@@ -113,6 +115,13 @@ dirvote_free_all(void)
 {
 }
 
+static inline void
+dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens)
+{
+  (void) ns;
+  (void) tokens;
+}
+
 #endif /* HAVE_MODULE_DIRAUTH */
 
 void dirvote_recalculate_timing(const or_options_t *options, time_t now);
index 9769d5ab501ebc6668f2106a45ee94ba7883f477..3a55c00cc1e00c881c91ca4e46cf512c49d10e62 100644 (file)
@@ -3285,60 +3285,6 @@ networkstatus_verify_bw_weights(networkstatus_t *ns, int consensus_method)
   return valid;
 }
 
-/** Parse and extract all SR commits from <b>tokens</b> and place them in
- *  <b>ns</b>. */
-static void
-extract_shared_random_commits(networkstatus_t *ns, smartlist_t *tokens)
-{
-  smartlist_t *chunks = NULL;
-
-  tor_assert(ns);
-  tor_assert(tokens);
-  /* Commits are only present in a vote. */
-  tor_assert(ns->type == NS_TYPE_VOTE);
-
-  ns->sr_info.commits = smartlist_new();
-
-  smartlist_t *commits = find_all_by_keyword(tokens, K_COMMIT);
-  /* It's normal that a vote might contain no commits even if it participates
-   * in the SR protocol. Don't treat it as an error. */
-  if (commits == NULL) {
-    goto end;
-  }
-
-  /* Parse the commit. We do NO validation of number of arguments or ordering
-   * for forward compatibility, it's the parse commit job to inform us if it's
-   * supported or not. */
-  chunks = smartlist_new();
-  SMARTLIST_FOREACH_BEGIN(commits, directory_token_t *, tok) {
-    /* Extract all arguments and put them in the chunks list. */
-    for (int i = 0; i < tok->n_args; i++) {
-      smartlist_add(chunks, tok->args[i]);
-    }
-    sr_commit_t *commit = sr_parse_commit(chunks);
-    smartlist_clear(chunks);
-    if (commit == NULL) {
-      /* Get voter identity so we can warn that this dirauth vote contains
-       * commit we can't parse. */
-      networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0);
-      tor_assert(voter);
-      log_warn(LD_DIR, "SR: Unable to parse commit %s from vote of voter %s.",
-               escaped(tok->object_body),
-               hex_str(voter->identity_digest,
-                       sizeof(voter->identity_digest)));
-      /* Commitment couldn't be parsed. Continue onto the next commit because
-       * this one could be unsupported for instance. */
-      continue;
-    }
-    /* Add newly created commit object to the vote. */
-    smartlist_add(ns->sr_info.commits, commit);
-  } SMARTLIST_FOREACH_END(tok);
-
- end:
-  smartlist_free(chunks);
-  smartlist_free(commits);
-}
-
 /** Check if a shared random value of type <b>srv_type</b> is in
  *  <b>tokens</b>. If there is, parse it and set it to <b>srv_out</b>. Return
  *  -1 on failure, 0 on success. The resulting srv is allocated on the heap and
@@ -3776,13 +3722,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
   /* If this is a vote document, check if information about the shared
      randomness protocol is included, and extract it. */
   if (ns->type == NS_TYPE_VOTE) {
-    /* Does this authority participates in the SR protocol? */
-    tok = find_opt_by_keyword(tokens, K_SR_FLAG);
-    if (tok) {
-      ns->sr_info.participate = 1;
-      /* Get the SR commitments and reveals from the vote. */
-      extract_shared_random_commits(ns, tokens);
-    }
+    dirvote_parse_sr_commits(ns, tokens);
   }
   /* For both a vote and consensus, extract the shared random values. */
   if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_CONSENSUS) {