summaryrefslogtreecommitdiff
path: root/src/or/dirauth
diff options
context:
space:
mode:
authorDavid Goulet <dgoulet@torproject.org>2018-04-25 10:42:56 -0400
committerDavid Goulet <dgoulet@torproject.org>2018-04-27 11:40:44 -0400
commit2963e65c30d1c1463e71385439246e0df1e363c5 (patch)
tree3c54c6f9b0f3914ef58b607270450ff6c84ad62a /src/or/dirauth
parentd7e4706f2293cb199d3246197b2d911188de0e45 (diff)
downloadtor-2963e65c30d1c1463e71385439246e0df1e363c5.tar.gz
tor-2963e65c30d1c1463e71385439246e0df1e363c5.zip
dirvote: Move SR commit parsing into dirauth module
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>
Diffstat (limited to 'src/or/dirauth')
-rw-r--r--src/or/dirauth/dirvote.c71
-rw-r--r--src/or/dirauth/dirvote.h9
2 files changed, 80 insertions, 0 deletions
diff --git a/src/or/dirauth/dirvote.c b/src/or/dirauth/dirvote.c
index b28bcf5ce5..3ec4d67416 100644
--- a/src/or/dirauth/dirvote.c
+++ b/src/or/dirauth/dirvote.c
@@ -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);
+ }
+}
+
diff --git a/src/or/dirauth/dirvote.h b/src/or/dirauth/dirvote.h
index fcc7cecf26..4514c6daf0 100644
--- a/src/or/dirauth/dirvote.h
+++ b/src/or/dirauth/dirvote.h
@@ -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);