tor-commits
Threads by month
- ----- 2025 -----
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
May 2018
- 17 participants
- 1514 discussions

[tor/master] config: Make circuit_build_times_disabled() use authdir_mode()
by nickm@torproject.org 01 May '18
by nickm@torproject.org 01 May '18
01 May '18
commit f0838e7257482e87a90f6844cb832a4ddf2197e5
Author: David Goulet <dgoulet(a)torproject.org>
Date: Thu Apr 5 09:17:12 2018 -0400
config: Make circuit_build_times_disabled() use authdir_mode()
Don't access the AuthoritativeDir options directly. We do this so we can move
authdir_mode() to the dirauth module.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/circuitstats.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/or/circuitstats.c b/src/or/circuitstats.c
index 643831927..6015bcf85 100644
--- a/src/or/circuitstats.c
+++ b/src/or/circuitstats.c
@@ -35,6 +35,7 @@
#include "networkstatus.h"
#include "rendclient.h"
#include "rendservice.h"
+#include "router.h"
#include "statefile.h"
#include "circuitlist.h"
#include "circuituse.h"
@@ -125,7 +126,7 @@ circuit_build_times_disabled_(const or_options_t *options,
ignore_consensus ? 0 : networkstatus_get_param(NULL, "cbtdisabled",
0, 0, 1);
int config_disabled = !options->LearnCircuitBuildTimeout;
- int dirauth_disabled = options->AuthoritativeDir;
+ int dirauth_disabled = authdir_mode(options);
int state_disabled = did_last_state_file_write_fail() ? 1 : 0;
/* LearnCircuitBuildTimeout and Tor2web/Single Onion Services are
* incompatible in two ways:
1
0
commit d177067860c1c23fd3fe109e621710560fc6c43d
Author: David Goulet <dgoulet(a)torproject.org>
Date: Thu Apr 5 11:18:47 2018 -0400
dirvote: Trim down the public API
Many functions become static to the C file or exposed to the tests within the
PRIVATE define of dirvote.h.
This commit moves a function to the top. No code behavior change.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/dirvote.c | 104 +++++++++++++++++++++++-----------------------
src/or/dirvote.h | 42 +++++++++----------
src/test/test_microdesc.c | 1 +
3 files changed, 72 insertions(+), 75 deletions(-)
diff --git a/src/or/dirvote.c b/src/or/dirvote.c
index f3b8a19f0..9f4893c37 100644
--- a/src/or/dirvote.c
+++ b/src/or/dirvote.c
@@ -351,6 +351,53 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
* Consensus generation
* ===== */
+/** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
+ * the digest algorithm <b>alg</b>, decode it and copy it into
+ * <b>digest256_out</b> and return 0. Otherwise return -1. */
+static int
+vote_routerstatus_find_microdesc_hash(char *digest256_out,
+ const vote_routerstatus_t *vrs,
+ int method,
+ digest_algorithm_t alg)
+{
+ /* XXXX only returns the sha256 method. */
+ const vote_microdesc_hash_t *h;
+ char mstr[64];
+ size_t mlen;
+ char dstr[64];
+
+ tor_snprintf(mstr, sizeof(mstr), "%d", method);
+ mlen = strlen(mstr);
+ tor_snprintf(dstr, sizeof(dstr), " %s=",
+ crypto_digest_algorithm_get_name(alg));
+
+ for (h = vrs->microdesc; h; h = h->next) {
+ const char *cp = h->microdesc_hash_line;
+ size_t num_len;
+ /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
+ * the first part. */
+ while (1) {
+ num_len = strspn(cp, "1234567890");
+ if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
+ /* This is the line. */
+ char buf[BASE64_DIGEST256_LEN+1];
+ /* XXXX ignores extraneous stuff if the digest is too long. This
+ * seems harmless enough, right? */
+ cp = strstr(cp, dstr);
+ if (!cp)
+ return -1;
+ cp += strlen(dstr);
+ strlcpy(buf, cp, sizeof(buf));
+ return digest256_from_base64(digest256_out, buf);
+ }
+ if (num_len == 0 || cp[num_len] != ',')
+ break;
+ cp += num_len + 1;
+ }
+ }
+ return -1;
+}
+
/** Given a vote <b>vote</b> (not a consensus!), return its associated
* networkstatus_voter_info_t. */
static networkstatus_voter_info_t *
@@ -1328,7 +1375,7 @@ compute_nth_protocol_set(int n, int n_voters, const smartlist_t *votes)
* behavior, and make the new behavior conditional on a new-enough
* consensus_method.
**/
-char *
+STATIC char *
networkstatus_compute_consensus(smartlist_t *votes,
int total_authorities,
crypto_pk_t *identity_key,
@@ -2372,7 +2419,7 @@ compute_consensus_package_lines(smartlist_t *votes)
* new signature is verifiable.) Return the number of signatures added or
* changed, or -1 if the document signed by <b>sigs</b> isn't the same
* document as <b>target</b>. */
-int
+STATIC int
networkstatus_add_detached_signatures(networkstatus_t *target,
ns_detached_signatures_t *sigs,
const char *source,
@@ -2564,7 +2611,7 @@ networkstatus_format_signatures(networkstatus_t *consensus,
* corresponding to the signatures on <b>consensuses</b>, which must contain
* exactly one FLAV_NS consensus, and no more than one consensus for each
* other flavor. */
-char *
+STATIC char *
networkstatus_get_detached_signatures(smartlist_t *consensuses)
{
smartlist_t *elements;
@@ -3798,7 +3845,7 @@ dirvote_get_vote(const char *fp, int flags)
/** Construct and return a new microdescriptor from a routerinfo <b>ri</b>
* according to <b>consensus_method</b>.
**/
-microdesc_t *
+STATIC microdesc_t *
dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
{
microdesc_t *result = NULL;
@@ -3893,7 +3940,7 @@ dirvote_create_microdescriptor(const routerinfo_t *ri, int consensus_method)
* in a consensus vote document. Write it into the <b>out_len</b>-byte buffer
* in <b>out</b>. Return -1 on failure and the number of characters written
* on success. */
-ssize_t
+static ssize_t
dirvote_format_microdesc_vote_line(char *out_buf, size_t out_buf_len,
const microdesc_t *md,
int consensus_method_low,
@@ -4001,50 +4048,3 @@ dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now,
return result;
}
-/** If <b>vrs</b> has a hash made for the consensus method <b>method</b> with
- * the digest algorithm <b>alg</b>, decode it and copy it into
- * <b>digest256_out</b> and return 0. Otherwise return -1. */
-int
-vote_routerstatus_find_microdesc_hash(char *digest256_out,
- const vote_routerstatus_t *vrs,
- int method,
- digest_algorithm_t alg)
-{
- /* XXXX only returns the sha256 method. */
- const vote_microdesc_hash_t *h;
- char mstr[64];
- size_t mlen;
- char dstr[64];
-
- tor_snprintf(mstr, sizeof(mstr), "%d", method);
- mlen = strlen(mstr);
- tor_snprintf(dstr, sizeof(dstr), " %s=",
- crypto_digest_algorithm_get_name(alg));
-
- for (h = vrs->microdesc; h; h = h->next) {
- const char *cp = h->microdesc_hash_line;
- size_t num_len;
- /* cp looks like \d+(,\d+)* (digesttype=val )+ . Let's hunt for mstr in
- * the first part. */
- while (1) {
- num_len = strspn(cp, "1234567890");
- if (num_len == mlen && fast_memeq(mstr, cp, mlen)) {
- /* This is the line. */
- char buf[BASE64_DIGEST256_LEN+1];
- /* XXXX ignores extraneous stuff if the digest is too long. This
- * seems harmless enough, right? */
- cp = strstr(cp, dstr);
- if (!cp)
- return -1;
- cp += strlen(dstr);
- strlcpy(buf, cp, sizeof(buf));
- return digest256_from_base64(digest256_out, buf);
- }
- if (num_len == 0 || cp[num_len] != ',')
- break;
- cp += num_len + 1;
- }
- }
- return -1;
-}
-
diff --git a/src/or/dirvote.h b/src/or/dirvote.h
index 8a317deb4..dd388fd96 100644
--- a/src/or/dirvote.h
+++ b/src/or/dirvote.h
@@ -89,19 +89,6 @@
void dirvote_free_all(void);
/* vote manipulation */
-char *networkstatus_compute_consensus(smartlist_t *votes,
- int total_authorities,
- crypto_pk_t *identity_key,
- crypto_pk_t *signing_key,
- const char *legacy_identity_key_digest,
- crypto_pk_t *legacy_signing_key,
- consensus_flavor_t flavor);
-int networkstatus_add_detached_signatures(networkstatus_t *target,
- ns_detached_signatures_t *sigs,
- const char *source,
- int severity,
- const char **msg_out);
-char *networkstatus_get_detached_signatures(smartlist_t *consensuses);
void ns_detached_signatures_free_(ns_detached_signatures_t *s);
#define ns_detached_signatures_free(s) \
FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
@@ -178,21 +165,11 @@ networkstatus_t *
dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
authority_cert_t *cert);
-microdesc_t *dirvote_create_microdescriptor(const routerinfo_t *ri,
- int consensus_method);
-ssize_t dirvote_format_microdesc_vote_line(char *out, size_t out_len,
- const microdesc_t *md,
- int consensus_method_low,
- int consensus_method_high);
vote_microdesc_hash_t *dirvote_format_all_microdesc_vote_lines(
const routerinfo_t *ri,
time_t now,
smartlist_t *microdescriptors_out);
-int vote_routerstatus_find_microdesc_hash(char *digest256_out,
- const vote_routerstatus_t *vrs,
- int method,
- digest_algorithm_t alg);
document_signature_t *voter_get_sig_by_algorithm(
const networkstatus_voter_info_t *voter,
digest_algorithm_t alg);
@@ -212,6 +189,25 @@ STATIC int
networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G,
int64_t M, int64_t E, int64_t D,
int64_t T, int64_t weight_scale);
+STATIC
+char *networkstatus_compute_consensus(smartlist_t *votes,
+ int total_authorities,
+ crypto_pk_t *identity_key,
+ crypto_pk_t *signing_key,
+ const char *legacy_identity_key_digest,
+ crypto_pk_t *legacy_signing_key,
+ consensus_flavor_t flavor);
+STATIC
+int networkstatus_add_detached_signatures(networkstatus_t *target,
+ ns_detached_signatures_t *sigs,
+ const char *source,
+ int severity,
+ const char **msg_out);
+STATIC
+char *networkstatus_get_detached_signatures(smartlist_t *consensuses);
+STATIC microdesc_t *dirvote_create_microdescriptor(const routerinfo_t *ri,
+ int consensus_method);
+
#endif /* defined(DIRVOTE_PRIVATE) */
#endif /* !defined(TOR_DIRVOTE_H) */
diff --git a/src/test/test_microdesc.c b/src/test/test_microdesc.c
index 3d02fc1a5..94b4d5e51 100644
--- a/src/test/test_microdesc.c
+++ b/src/test/test_microdesc.c
@@ -5,6 +5,7 @@
#include "or.h"
#include "config.h"
+#define DIRVOTE_PRIVATE
#include "dirvote.h"
#include "microdesc.h"
#include "networkstatus.h"
1
0
commit 35d86b088dd97c65e0af48b4f186682fb011f283
Author: David Goulet <dgoulet(a)torproject.org>
Date: Thu Apr 5 11:38:37 2018 -0400
dirvote: Reorganize the dirvote.h file
Remove useless include.
Clearly identify functions that are used by other part of Tor, functions that
are only used by the dirauth subsystem and functions that are exposed for unit
tests.
This will help us in the dirauth modularization effort.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/dirvote.h | 54 ++++++++++++++++++++++++++++++++----------------------
1 file changed, 32 insertions(+), 22 deletions(-)
diff --git a/src/or/dirvote.h b/src/or/dirvote.h
index 88d0d56ff..4f1f35d85 100644
--- a/src/or/dirvote.h
+++ b/src/or/dirvote.h
@@ -12,8 +12,6 @@
#ifndef TOR_DIRVOTE_H
#define TOR_DIRVOTE_H
-#include "testsupport.h"
-
/*
* Ideally, assuming synced clocks, we should only need 1 second for each of:
* - Vote
@@ -86,41 +84,53 @@
* get confused with the above macros.) */
#define DEFAULT_MAX_UNMEASURED_BW_KB 20
+/* Directory Get Vote (DGV) flags for dirvote_get_vote(). */
+#define DGV_BY_ID 1
+#define DGV_INCLUDE_PENDING 2
+#define DGV_INCLUDE_PREVIOUS 4
+
+/*
+ * Public API. Used outside of the dirauth subsystem.
+ */
+
void dirvote_free_all(void);
-/* vote manipulation */
+/* Vote manipulation */
void ns_detached_signatures_free_(ns_detached_signatures_t *s);
#define ns_detached_signatures_free(s) \
FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
-/* cert manipulation */
-authority_cert_t *authority_cert_dup(authority_cert_t *cert);
-
-void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out);
-time_t dirvote_get_start_of_next_interval(time_t now,
- int interval,
- int offset);
void dirvote_recalculate_timing(const or_options_t *options, time_t now);
void dirvote_act(const or_options_t *options, time_t now);
-time_t dirvote_get_next_valid_after_time(void);
-
-/* invoked on timers and by outside triggers. */
+/* Invoked on timers and by outside triggers. */
struct pending_vote_t * dirvote_add_vote(const char *vote_body,
const char **msg_out,
int *status_out);
int dirvote_add_signatures(const char *detached_signatures_body,
const char *source,
const char **msg_out);
-
/* Item access */
MOCK_DECL(const char*, dirvote_get_pending_consensus,
(consensus_flavor_t flav));
MOCK_DECL(const char*, dirvote_get_pending_detached_signatures, (void));
-
-#define DGV_BY_ID 1
-#define DGV_INCLUDE_PENDING 2
-#define DGV_INCLUDE_PREVIOUS 4
const cached_dir_t *dirvote_get_vote(const char *fp, int flags);
+document_signature_t *voter_get_sig_by_algorithm(
+ const networkstatus_voter_info_t *voter,
+ digest_algorithm_t alg);
+
+/*
+ * API used _only_ by the dirauth subsystem.
+ */
+
+/* Cert manipulation */
+authority_cert_t *authority_cert_dup(authority_cert_t *cert);
+
+void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out);
+time_t dirvote_get_start_of_next_interval(time_t now,
+ int interval,
+ int offset);
+time_t dirvote_get_next_valid_after_time(void);
+
void set_routerstatus_from_routerinfo(routerstatus_t *rs,
node_t *node,
routerinfo_t *ri, time_t now,
@@ -134,11 +144,11 @@ vote_microdesc_hash_t *dirvote_format_all_microdesc_vote_lines(
time_t now,
smartlist_t *microdescriptors_out);
-document_signature_t *voter_get_sig_by_algorithm(
- const networkstatus_voter_info_t *voter,
- digest_algorithm_t alg);
-
+/*
+ * Exposed functions for unit tests.
+ */
#ifdef DIRVOTE_PRIVATE
+
STATIC int32_t dirvote_get_intermediate_param_value(
const smartlist_t *param_list,
const char *keyword,
1
0

[tor/master] ns: Move ns_detached_signatures_free() to networkstatus.c
by nickm@torproject.org 01 May '18
by nickm@torproject.org 01 May '18
01 May '18
commit d7e4706f2293cb199d3246197b2d911188de0e45
Author: David Goulet <dgoulet(a)torproject.org>
Date: Wed Apr 25 10:09:50 2018 -0400
ns: Move ns_detached_signatures_free() to networkstatus.c
From dirvote.c to networkstatus.c where it makes more sense both in terms of
namespace and subsystem responsability.
This removes one less dependency on the dirauth module.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/dirauth/dirvote.c | 19 -------------------
src/or/dirauth/dirvote.h | 5 -----
src/or/networkstatus.c | 19 +++++++++++++++++++
src/or/networkstatus.h | 3 +++
4 files changed, 22 insertions(+), 24 deletions(-)
diff --git a/src/or/dirauth/dirvote.c b/src/or/dirauth/dirvote.c
index 5af6d841f..b28bcf5ce 100644
--- a/src/or/dirauth/dirvote.c
+++ b/src/or/dirauth/dirvote.c
@@ -2703,25 +2703,6 @@ get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
return signatures;
}
-/** Release all storage held in <b>s</b>. */
-void
-ns_detached_signatures_free_(ns_detached_signatures_t *s)
-{
- if (!s)
- return;
- if (s->signatures) {
- STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
- SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
- document_signature_free(sig));
- smartlist_free(sigs);
- } STRMAP_FOREACH_END;
- strmap_free(s->signatures, NULL);
- strmap_free(s->digests, tor_free_);
- }
-
- tor_free(s);
-}
-
/** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
void
dirvote_act(const or_options_t *options, time_t now)
diff --git a/src/or/dirauth/dirvote.h b/src/or/dirauth/dirvote.h
index 6f4c55243..fcc7cecf2 100644
--- a/src/or/dirauth/dirvote.h
+++ b/src/or/dirauth/dirvote.h
@@ -115,11 +115,6 @@ dirvote_free_all(void)
#endif /* HAVE_MODULE_DIRAUTH */
-/* Vote manipulation */
-void ns_detached_signatures_free_(ns_detached_signatures_t *s);
-#define ns_detached_signatures_free(s) \
- FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
-
void dirvote_recalculate_timing(const or_options_t *options, time_t now);
/* Invoked on timers and by outside triggers. */
struct pending_vote_t * dirvote_add_vote(const char *vote_body,
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index f78d9d672..fa8d0bcf1 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -2642,6 +2642,25 @@ networkstatus_check_required_protocols(const networkstatus_t *ns,
return 0;
}
+/** Release all storage held in <b>s</b>. */
+void
+ns_detached_signatures_free_(ns_detached_signatures_t *s)
+{
+ if (!s)
+ return;
+ if (s->signatures) {
+ STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
+ SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
+ document_signature_free(sig));
+ smartlist_free(sigs);
+ } STRMAP_FOREACH_END;
+ strmap_free(s->signatures, NULL);
+ strmap_free(s->digests, tor_free_);
+ }
+
+ tor_free(s);
+}
+
/** Free all storage held locally in this module. */
void
networkstatus_free_all(void)
diff --git a/src/or/networkstatus.h b/src/or/networkstatus.h
index 04cf277d2..bd7829205 100644
--- a/src/or/networkstatus.h
+++ b/src/or/networkstatus.h
@@ -24,6 +24,9 @@ void routerstatus_free_(routerstatus_t *rs);
void networkstatus_vote_free_(networkstatus_t *ns);
#define networkstatus_vote_free(ns) \
FREE_AND_NULL(networkstatus_t, networkstatus_vote_free_, (ns))
+void ns_detached_signatures_free_(ns_detached_signatures_t *s);
+#define ns_detached_signatures_free(s) \
+ FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
networkstatus_voter_info_t *networkstatus_get_voter_by_id(
networkstatus_t *vote,
const char *identity);
1
0

01 May '18
commit 43bee06dd090b5e221a0622389e18a21c0d1e153
Author: David Goulet <dgoulet(a)torproject.org>
Date: Wed Apr 25 10:00:17 2018 -0400
dirvote: Rename voter_get_sig_by_algorithm()
In order to follow the public namespace of dirvote.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/dirauth/dirvote.c | 2 +-
src/or/dirvote_common.c | 4 ++--
src/or/dirvote_common.h | 2 +-
src/or/routerparse.c | 2 +-
4 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/or/dirauth/dirvote.c b/src/or/dirauth/dirvote.c
index dc35c892e..5af6d841f 100644
--- a/src/or/dirauth/dirvote.c
+++ b/src/or/dirauth/dirvote.c
@@ -2490,7 +2490,7 @@ networkstatus_add_detached_signatures(networkstatus_t *target,
continue;
}
- old_sig = voter_get_sig_by_algorithm(target_voter, sig->alg);
+ old_sig = dirvote_get_voter_sig_by_alg(target_voter, sig->alg);
/* If the target already has a good signature from this voter, then skip
* this one. */
diff --git a/src/or/dirvote_common.c b/src/or/dirvote_common.c
index caa9138f0..b34f4fe20 100644
--- a/src/or/dirvote_common.c
+++ b/src/or/dirvote_common.c
@@ -182,8 +182,8 @@ dirvote_recalculate_timing(const or_options_t *options, time_t now)
/** Return the signature made by <b>voter</b> using the algorithm
* <b>alg</b>, or NULL if none is found. */
document_signature_t *
-voter_get_sig_by_algorithm(const networkstatus_voter_info_t *voter,
- digest_algorithm_t alg)
+dirvote_get_voter_sig_by_alg(const networkstatus_voter_info_t *voter,
+ digest_algorithm_t alg)
{
if (!voter->sigs)
return NULL;
diff --git a/src/or/dirvote_common.h b/src/or/dirvote_common.h
index 91d32aaa8..16621244b 100644
--- a/src/or/dirvote_common.h
+++ b/src/or/dirvote_common.h
@@ -58,7 +58,7 @@ time_t dirvote_get_start_of_next_interval(time_t now,
int offset);
time_t dirvote_get_next_valid_after_time(void);
-document_signature_t *voter_get_sig_by_algorithm(
+document_signature_t *dirvote_get_voter_sig_by_alg(
const networkstatus_voter_info_t *voter,
digest_algorithm_t alg);
diff --git a/src/or/routerparse.c b/src/or/routerparse.c
index b27833c3e..9769d5ab5 100644
--- a/src/or/routerparse.c
+++ b/src/or/routerparse.c
@@ -3972,7 +3972,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
}
}
- if (voter_get_sig_by_algorithm(v, sig->alg)) {
+ if (dirvote_get_voter_sig_by_alg(v, sig->alg)) {
/* We already parsed a vote with this algorithm from this voter. Use the
first one. */
log_fn(LOG_PROTOCOL_WARN, LD_DIR, "We received a networkstatus "
1
0

01 May '18
commit 26817d9d2231d2580361ae9082db17250b454628
Author: David Goulet <dgoulet(a)torproject.org>
Date: Wed Apr 25 09:06:29 2018 -0400
dirvote: Extract shared functions to common file
No code behavior change.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/dirauth/dirvote.c | 237 +-----------------------------------------
src/or/dirauth/dirvote.h | 33 +++---
src/or/dirserv.c | 1 +
src/or/dirvote_common.c | 219 ++++++++++++++++++++++++++++++++++++++
src/or/dirvote_common.h | 69 ++++++++++++
src/or/include.am | 2 +
src/or/routerparse.c | 1 +
src/or/shared_random_common.c | 1 +
src/test/test_dir_common.c | 1 +
src/test/test_shared_random.c | 1 +
10 files changed, 316 insertions(+), 249 deletions(-)
diff --git a/src/or/dirauth/dirvote.c b/src/or/dirauth/dirvote.c
index 67ab1d4ff..dc35c892e 100644
--- a/src/or/dirauth/dirvote.c
+++ b/src/or/dirauth/dirvote.c
@@ -10,6 +10,7 @@
#include "directory.h"
#include "dirserv.h"
#include "dirvote.h"
+#include "dirvote_common.h"
#include "microdesc.h"
#include "networkstatus.h"
#include "policies.h"
@@ -72,40 +73,6 @@ typedef struct pending_consensus_t {
networkstatus_t *consensus;
} pending_consensus_t;
-/** Scheduling information for a voting interval. */
-typedef struct {
- /** When do we generate and distribute our vote for this interval? */
- time_t voting_starts;
- /** When do we send an HTTP request for any votes that we haven't
- * been posted yet?*/
- time_t fetch_missing_votes;
- /** When do we give up on getting more votes and generate a consensus? */
- time_t voting_ends;
- /** When do we send an HTTP request for any signatures we're expecting to
- * see on the consensus? */
- time_t fetch_missing_signatures;
- /** When do we publish the consensus? */
- time_t interval_starts;
-
- /* True iff we have generated and distributed our vote. */
- int have_voted;
- /* True iff we've requested missing votes. */
- int have_fetched_missing_votes;
- /* True iff we have built a consensus and sent the signatures around. */
- int have_built_consensus;
- /* True iff we've fetched missing signatures. */
- int have_fetched_missing_signatures;
- /* True iff we have published our consensus. */
- int have_published_consensus;
-
- /* True iff this voting schedule was set on demand meaning not through the
- * normal vote operation of a dirauth or when a consensus is set. This only
- * applies to a directory authority that needs to recalculate the voting
- * timings only for the first vote even though this object was initilized
- * prior to voting. */
- int created_on_demand;
-} voting_schedule_t;
-
/* DOCDOC dirvote_add_signatures_to_all_pending_consensuses */
static int dirvote_add_signatures_to_all_pending_consensuses(
const char *detached_signatures_body,
@@ -444,20 +411,6 @@ get_voter(const networkstatus_t *vote)
return smartlist_get(vote->voters, 0);
}
-/** Return the signature made by <b>voter</b> using the algorithm
- * <b>alg</b>, or NULL if none is found. */
-document_signature_t *
-voter_get_sig_by_algorithm(const networkstatus_voter_info_t *voter,
- digest_algorithm_t alg)
-{
- if (!voter->sigs)
- return NULL;
- SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
- if (sig->alg == alg)
- return sig);
- return NULL;
-}
-
/** Temporary structure used in constructing a list of dir-source entries
* for a consensus. One of these is generated for every vote, and one more
* for every legacy key in each vote. */
@@ -2769,194 +2722,6 @@ ns_detached_signatures_free_(ns_detached_signatures_t *s)
tor_free(s);
}
-/* =====
- * Certificate functions
- * ===== */
-
-/** Allocate and return a new authority_cert_t with the same contents as
- * <b>cert</b>. */
-authority_cert_t *
-authority_cert_dup(authority_cert_t *cert)
-{
- authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
- tor_assert(cert);
-
- memcpy(out, cert, sizeof(authority_cert_t));
- /* Now copy pointed-to things. */
- out->cache_info.signed_descriptor_body =
- tor_strndup(cert->cache_info.signed_descriptor_body,
- cert->cache_info.signed_descriptor_len);
- out->cache_info.saved_location = SAVED_NOWHERE;
- out->identity_key = crypto_pk_dup_key(cert->identity_key);
- out->signing_key = crypto_pk_dup_key(cert->signing_key);
-
- return out;
-}
-
-/* =====
- * Vote scheduling
- * ===== */
-
-/** Set *<b>timing_out</b> to the intervals at which we would like to vote.
- * Note that these aren't the intervals we'll use to vote; they're the ones
- * that we'll vote to use. */
-void
-dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
-{
- const or_options_t *options = get_options();
-
- tor_assert(timing_out);
-
- timing_out->vote_interval = options->V3AuthVotingInterval;
- timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
- timing_out->vote_delay = options->V3AuthVoteDelay;
- timing_out->dist_delay = options->V3AuthDistDelay;
-}
-
-/** Return the start of the next interval of size <b>interval</b> (in
- * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
- * starts a fresh interval, and if the last interval of a day would be
- * truncated to less than half its size, it is rolled into the
- * previous interval. */
-time_t
-dirvote_get_start_of_next_interval(time_t now, int interval, int offset)
-{
- struct tm tm;
- time_t midnight_today=0;
- time_t midnight_tomorrow;
- time_t next;
-
- tor_gmtime_r(&now, &tm);
- tm.tm_hour = 0;
- tm.tm_min = 0;
- tm.tm_sec = 0;
-
- if (tor_timegm(&tm, &midnight_today) < 0) {
- log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
- }
- midnight_tomorrow = midnight_today + (24*60*60);
-
- next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
-
- /* Intervals never cross midnight. */
- if (next > midnight_tomorrow)
- next = midnight_tomorrow;
-
- /* If the interval would only last half as long as it's supposed to, then
- * skip over to the next day. */
- if (next + interval/2 > midnight_tomorrow)
- next = midnight_tomorrow;
-
- next += offset;
- if (next - interval > now)
- next -= interval;
-
- return next;
-}
-
-/* Populate and return a new voting_schedule_t that can be used to schedule
- * voting. The object is allocated on the heap and it's the responsibility of
- * the caller to free it. Can't fail. */
-static voting_schedule_t *
-get_voting_schedule(const or_options_t *options, time_t now, int severity)
-{
- int interval, vote_delay, dist_delay;
- time_t start;
- time_t end;
- networkstatus_t *consensus;
- voting_schedule_t *new_voting_schedule;
-
- new_voting_schedule = tor_malloc_zero(sizeof(voting_schedule_t));
-
- consensus = networkstatus_get_live_consensus(now);
-
- if (consensus) {
- interval = (int)( consensus->fresh_until - consensus->valid_after );
- vote_delay = consensus->vote_seconds;
- dist_delay = consensus->dist_seconds;
- } else {
- interval = options->TestingV3AuthInitialVotingInterval;
- vote_delay = options->TestingV3AuthInitialVoteDelay;
- dist_delay = options->TestingV3AuthInitialDistDelay;
- }
-
- tor_assert(interval > 0);
-
- if (vote_delay + dist_delay > interval/2)
- vote_delay = dist_delay = interval / 4;
-
- start = new_voting_schedule->interval_starts =
- dirvote_get_start_of_next_interval(now,interval,
- options->TestingV3AuthVotingStartOffset);
- end = dirvote_get_start_of_next_interval(start+1, interval,
- options->TestingV3AuthVotingStartOffset);
-
- tor_assert(end > start);
-
- new_voting_schedule->fetch_missing_signatures = start - (dist_delay/2);
- new_voting_schedule->voting_ends = start - dist_delay;
- new_voting_schedule->fetch_missing_votes =
- start - dist_delay - (vote_delay/2);
- new_voting_schedule->voting_starts = start - dist_delay - vote_delay;
-
- {
- char tbuf[ISO_TIME_LEN+1];
- format_iso_time(tbuf, new_voting_schedule->interval_starts);
- tor_log(severity, LD_DIR,"Choosing expected valid-after time as %s: "
- "consensus_set=%d, interval=%d",
- tbuf, consensus?1:0, interval);
- }
-
- return new_voting_schedule;
-}
-
-#define voting_schedule_free(s) \
- FREE_AND_NULL(voting_schedule_t, voting_schedule_free_, (s))
-
-/** Frees a voting_schedule_t. This should be used instead of the generic
- * tor_free. */
-static void
-voting_schedule_free_(voting_schedule_t *voting_schedule_to_free)
-{
- if (!voting_schedule_to_free)
- return;
- tor_free(voting_schedule_to_free);
-}
-
-static voting_schedule_t voting_schedule;
-
-/* Using the time <b>now</b>, return the next voting valid-after time. */
-time_t
-dirvote_get_next_valid_after_time(void)
-{
- /* This is a safe guard in order to make sure that the voting schedule
- * static object is at least initialized. Using this function with a zeroed
- * voting schedule can lead to bugs. */
- if (tor_mem_is_zero((const char *) &voting_schedule,
- sizeof(voting_schedule))) {
- dirvote_recalculate_timing(get_options(), time(NULL));
- voting_schedule.created_on_demand = 1;
- }
- return voting_schedule.interval_starts;
-}
-
-/** Set voting_schedule to hold the timing for the next vote we should be
- * doing. All type of tor do that because HS subsystem needs the timing as
- * well to function properly. */
-void
-dirvote_recalculate_timing(const or_options_t *options, time_t now)
-{
- voting_schedule_t *new_voting_schedule;
-
- /* get the new voting schedule */
- new_voting_schedule = get_voting_schedule(options, now, LOG_INFO);
- tor_assert(new_voting_schedule);
-
- /* Fill in the global static struct now */
- memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
- voting_schedule_free(new_voting_schedule);
-}
-
/** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
void
dirvote_act(const or_options_t *options, time_t now)
diff --git a/src/or/dirauth/dirvote.h b/src/or/dirauth/dirvote.h
index 4f1f35d85..6f4c55243 100644
--- a/src/or/dirauth/dirvote.h
+++ b/src/or/dirauth/dirvote.h
@@ -91,17 +91,36 @@
/*
* Public API. Used outside of the dirauth subsystem.
+ *
+ * We need to nullify them if the module is disabled.
*/
+#ifdef HAVE_MODULE_DIRAUTH
+void dirvote_act(const or_options_t *options, time_t now);
void dirvote_free_all(void);
+#else /* HAVE_MODULE_DIRAUTH */
+
+static inline void
+dirvote_act(const or_options_t *options, time_t now)
+{
+ (void) options;
+ (void) now;
+}
+
+static inline void
+dirvote_free_all(void)
+{
+}
+
+#endif /* HAVE_MODULE_DIRAUTH */
+
/* Vote manipulation */
void ns_detached_signatures_free_(ns_detached_signatures_t *s);
#define ns_detached_signatures_free(s) \
FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s))
void dirvote_recalculate_timing(const or_options_t *options, time_t now);
-void dirvote_act(const or_options_t *options, time_t now);
/* Invoked on timers and by outside triggers. */
struct pending_vote_t * dirvote_add_vote(const char *vote_body,
const char **msg_out,
@@ -114,23 +133,11 @@ MOCK_DECL(const char*, dirvote_get_pending_consensus,
(consensus_flavor_t flav));
MOCK_DECL(const char*, dirvote_get_pending_detached_signatures, (void));
const cached_dir_t *dirvote_get_vote(const char *fp, int flags);
-document_signature_t *voter_get_sig_by_algorithm(
- const networkstatus_voter_info_t *voter,
- digest_algorithm_t alg);
/*
* API used _only_ by the dirauth subsystem.
*/
-/* Cert manipulation */
-authority_cert_t *authority_cert_dup(authority_cert_t *cert);
-
-void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out);
-time_t dirvote_get_start_of_next_interval(time_t now,
- int interval,
- int offset);
-time_t dirvote_get_next_valid_after_time(void);
-
void set_routerstatus_from_routerinfo(routerstatus_t *rs,
node_t *node,
routerinfo_t *ri, time_t now,
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index c20e2966c..58294f6cf 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -18,6 +18,7 @@
#include "control.h"
#include "directory.h"
#include "dirserv.h"
+#include "dirvote_common.h"
#include "hibernate.h"
#include "keypin.h"
#include "main.h"
diff --git a/src/or/dirvote_common.c b/src/or/dirvote_common.c
new file mode 100644
index 000000000..caa9138f0
--- /dev/null
+++ b/src/or/dirvote_common.c
@@ -0,0 +1,219 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file dirvote_common.c
+ * \brief This file contains functions that are from the directory authority
+ * subsystem related to voting specifically but used by many part of
+ * tor. The full feature is built as part of the dirauth module.
+ **/
+
+#define DIRVOTE_COMMON_PRIVATE
+#include "dirvote_common.h"
+
+#include "or.h"
+#include "config.h"
+#include "networkstatus.h"
+
+/* =====
+ * Vote scheduling
+ * ===== */
+
+/** Set *<b>timing_out</b> to the intervals at which we would like to vote.
+ * Note that these aren't the intervals we'll use to vote; they're the ones
+ * that we'll vote to use. */
+void
+dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
+{
+ const or_options_t *options = get_options();
+
+ tor_assert(timing_out);
+
+ timing_out->vote_interval = options->V3AuthVotingInterval;
+ timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
+ timing_out->vote_delay = options->V3AuthVoteDelay;
+ timing_out->dist_delay = options->V3AuthDistDelay;
+}
+
+/** Return the start of the next interval of size <b>interval</b> (in
+ * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
+ * starts a fresh interval, and if the last interval of a day would be
+ * truncated to less than half its size, it is rolled into the
+ * previous interval. */
+time_t
+dirvote_get_start_of_next_interval(time_t now, int interval, int offset)
+{
+ struct tm tm;
+ time_t midnight_today=0;
+ time_t midnight_tomorrow;
+ time_t next;
+
+ tor_gmtime_r(&now, &tm);
+ tm.tm_hour = 0;
+ tm.tm_min = 0;
+ tm.tm_sec = 0;
+
+ if (tor_timegm(&tm, &midnight_today) < 0) {
+ log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
+ }
+ midnight_tomorrow = midnight_today + (24*60*60);
+
+ next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
+
+ /* Intervals never cross midnight. */
+ if (next > midnight_tomorrow)
+ next = midnight_tomorrow;
+
+ /* If the interval would only last half as long as it's supposed to, then
+ * skip over to the next day. */
+ if (next + interval/2 > midnight_tomorrow)
+ next = midnight_tomorrow;
+
+ next += offset;
+ if (next - interval > now)
+ next -= interval;
+
+ return next;
+}
+
+/* Populate and return a new voting_schedule_t that can be used to schedule
+ * voting. The object is allocated on the heap and it's the responsibility of
+ * the caller to free it. Can't fail. */
+static voting_schedule_t *
+get_voting_schedule(const or_options_t *options, time_t now, int severity)
+{
+ int interval, vote_delay, dist_delay;
+ time_t start;
+ time_t end;
+ networkstatus_t *consensus;
+ voting_schedule_t *new_voting_schedule;
+
+ new_voting_schedule = tor_malloc_zero(sizeof(voting_schedule_t));
+
+ consensus = networkstatus_get_live_consensus(now);
+
+ if (consensus) {
+ interval = (int)( consensus->fresh_until - consensus->valid_after );
+ vote_delay = consensus->vote_seconds;
+ dist_delay = consensus->dist_seconds;
+ } else {
+ interval = options->TestingV3AuthInitialVotingInterval;
+ vote_delay = options->TestingV3AuthInitialVoteDelay;
+ dist_delay = options->TestingV3AuthInitialDistDelay;
+ }
+
+ tor_assert(interval > 0);
+
+ if (vote_delay + dist_delay > interval/2)
+ vote_delay = dist_delay = interval / 4;
+
+ start = new_voting_schedule->interval_starts =
+ dirvote_get_start_of_next_interval(now,interval,
+ options->TestingV3AuthVotingStartOffset);
+ end = dirvote_get_start_of_next_interval(start+1, interval,
+ options->TestingV3AuthVotingStartOffset);
+
+ tor_assert(end > start);
+
+ new_voting_schedule->fetch_missing_signatures = start - (dist_delay/2);
+ new_voting_schedule->voting_ends = start - dist_delay;
+ new_voting_schedule->fetch_missing_votes =
+ start - dist_delay - (vote_delay/2);
+ new_voting_schedule->voting_starts = start - dist_delay - vote_delay;
+
+ {
+ char tbuf[ISO_TIME_LEN+1];
+ format_iso_time(tbuf, new_voting_schedule->interval_starts);
+ tor_log(severity, LD_DIR,"Choosing expected valid-after time as %s: "
+ "consensus_set=%d, interval=%d",
+ tbuf, consensus?1:0, interval);
+ }
+
+ return new_voting_schedule;
+}
+
+#define voting_schedule_free(s) \
+ FREE_AND_NULL(voting_schedule_t, voting_schedule_free_, (s))
+
+/** Frees a voting_schedule_t. This should be used instead of the generic
+ * tor_free. */
+static void
+voting_schedule_free_(voting_schedule_t *voting_schedule_to_free)
+{
+ if (!voting_schedule_to_free)
+ return;
+ tor_free(voting_schedule_to_free);
+}
+
+voting_schedule_t voting_schedule;
+
+/* Using the time <b>now</b>, return the next voting valid-after time. */
+time_t
+dirvote_get_next_valid_after_time(void)
+{
+ /* This is a safe guard in order to make sure that the voting schedule
+ * static object is at least initialized. Using this function with a zeroed
+ * voting schedule can lead to bugs. */
+ if (tor_mem_is_zero((const char *) &voting_schedule,
+ sizeof(voting_schedule))) {
+ dirvote_recalculate_timing(get_options(), time(NULL));
+ voting_schedule.created_on_demand = 1;
+ }
+ return voting_schedule.interval_starts;
+}
+
+/** Set voting_schedule to hold the timing for the next vote we should be
+ * doing. All type of tor do that because HS subsystem needs the timing as
+ * well to function properly. */
+void
+dirvote_recalculate_timing(const or_options_t *options, time_t now)
+{
+ voting_schedule_t *new_voting_schedule;
+
+ /* get the new voting schedule */
+ new_voting_schedule = get_voting_schedule(options, now, LOG_INFO);
+ tor_assert(new_voting_schedule);
+
+ /* Fill in the global static struct now */
+ memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
+ voting_schedule_free(new_voting_schedule);
+}
+
+/** Return the signature made by <b>voter</b> using the algorithm
+ * <b>alg</b>, or NULL if none is found. */
+document_signature_t *
+voter_get_sig_by_algorithm(const networkstatus_voter_info_t *voter,
+ digest_algorithm_t alg)
+{
+ if (!voter->sigs)
+ return NULL;
+ SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
+ if (sig->alg == alg)
+ return sig);
+ return NULL;
+}
+
+/* =====
+ * Certificate functions
+ * ===== */
+
+/** Allocate and return a new authority_cert_t with the same contents as
+ * <b>cert</b>. */
+authority_cert_t *
+authority_cert_dup(authority_cert_t *cert)
+{
+ authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
+ tor_assert(cert);
+
+ memcpy(out, cert, sizeof(authority_cert_t));
+ /* Now copy pointed-to things. */
+ out->cache_info.signed_descriptor_body =
+ tor_strndup(cert->cache_info.signed_descriptor_body,
+ cert->cache_info.signed_descriptor_len);
+ out->cache_info.saved_location = SAVED_NOWHERE;
+ out->identity_key = crypto_pk_dup_key(cert->identity_key);
+ out->signing_key = crypto_pk_dup_key(cert->signing_key);
+
+ return out;
+}
+
diff --git a/src/or/dirvote_common.h b/src/or/dirvote_common.h
new file mode 100644
index 000000000..91d32aaa8
--- /dev/null
+++ b/src/or/dirvote_common.h
@@ -0,0 +1,69 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file dirvote_common.h
+ * \brief Header file for dirvote_common.c.
+ **/
+
+#ifndef TOR_DIRVOTE_COMMON_H
+#define TOR_DIRVOTE_COMMON_H
+
+#include "or.h"
+
+/* Dirauth module. */
+#include "dirauth/dirvote.h"
+
+/** Scheduling information for a voting interval. */
+typedef struct {
+ /** When do we generate and distribute our vote for this interval? */
+ time_t voting_starts;
+ /** When do we send an HTTP request for any votes that we haven't
+ * been posted yet?*/
+ time_t fetch_missing_votes;
+ /** When do we give up on getting more votes and generate a consensus? */
+ time_t voting_ends;
+ /** When do we send an HTTP request for any signatures we're expecting to
+ * see on the consensus? */
+ time_t fetch_missing_signatures;
+ /** When do we publish the consensus? */
+ time_t interval_starts;
+
+ /* True iff we have generated and distributed our vote. */
+ int have_voted;
+ /* True iff we've requested missing votes. */
+ int have_fetched_missing_votes;
+ /* True iff we have built a consensus and sent the signatures around. */
+ int have_built_consensus;
+ /* True iff we've fetched missing signatures. */
+ int have_fetched_missing_signatures;
+ /* True iff we have published our consensus. */
+ int have_published_consensus;
+
+ /* True iff this voting schedule was set on demand meaning not through the
+ * normal vote operation of a dirauth or when a consensus is set. This only
+ * applies to a directory authority that needs to recalculate the voting
+ * timings only for the first vote even though this object was initilized
+ * prior to voting. */
+ int created_on_demand;
+} voting_schedule_t;
+
+/* Public API. */
+
+extern voting_schedule_t voting_schedule;
+
+void dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out);
+time_t dirvote_get_start_of_next_interval(time_t now,
+ int interval,
+ int offset);
+time_t dirvote_get_next_valid_after_time(void);
+
+document_signature_t *voter_get_sig_by_algorithm(
+ const networkstatus_voter_info_t *voter,
+ digest_algorithm_t alg);
+
+/* Cert manipulation */
+authority_cert_t *authority_cert_dup(authority_cert_t *cert);
+
+#endif /* TOR_DIRVOTE_COMMON_H */
+
diff --git a/src/or/include.am b/src/or/include.am
index e90b1fb2a..93f405d61 100644
--- a/src/or/include.am
+++ b/src/or/include.am
@@ -43,6 +43,7 @@ LIBTOR_A_SOURCES = \
src/or/cpuworker.c \
src/or/directory.c \
src/or/dirserv.c \
+ src/or/dirvote_common.c \
src/or/dns.c \
src/or/dnsserv.c \
src/or/dos.c \
@@ -198,6 +199,7 @@ ORHEADERS = \
src/or/cpuworker.h \
src/or/directory.h \
src/or/dirserv.h \
+ src/or/dirvote_common.h \
src/or/dns.h \
src/or/dns_structs.h \
src/or/dnsserv.h \
diff --git a/src/or/routerparse.c b/src/or/routerparse.c
index 2d64a82b6..b27833c3e 100644
--- a/src/or/routerparse.c
+++ b/src/or/routerparse.c
@@ -75,6 +75,7 @@
#include "torcert.h"
#include "sandbox.h"
#include "shared_random_common.h"
+#include "dirvote_common.h"
#include "dirauth/shared_random.h"
#undef log
diff --git a/src/or/shared_random_common.c b/src/or/shared_random_common.c
index c782e709f..7d897d953 100644
--- a/src/or/shared_random_common.c
+++ b/src/or/shared_random_common.c
@@ -12,6 +12,7 @@
#include "shared_random_common.h"
#include "config.h"
+#include "dirvote_common.h"
#include "networkstatus.h"
#include "util.h"
#include "util_format.h"
diff --git a/src/test/test_dir_common.c b/src/test/test_dir_common.c
index c3e3662fd..02d3295ca 100644
--- a/src/test/test_dir_common.c
+++ b/src/test/test_dir_common.c
@@ -10,6 +10,7 @@
#include "container.h"
#include "or.h"
#include "dirauth/dirvote.h"
+#include "dirvote_common.h"
#include "nodelist.h"
#include "routerlist.h"
#include "test_dir_common.h"
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
index 30d0d44ca..95a7ff28f 100644
--- a/src/test/test_shared_random.c
+++ b/src/test/test_shared_random.c
@@ -16,6 +16,7 @@
#include "routerlist.h"
#include "router.h"
#include "routerparse.h"
+#include "shared_random_common.h"
#include "networkstatus.h"
#include "log_test_helpers.h"
1
0
commit 35ff2a3b864d921d45aec50f551b59f24e31a512
Author: David Goulet <dgoulet(a)torproject.org>
Date: Wed Apr 25 10:05:30 2018 -0400
dirvote: Rename authority_cert_dup()
Renamed to follow the file namespace.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/dirserv.c | 2 +-
src/or/dirvote_common.c | 2 +-
src/or/dirvote_common.h | 2 +-
src/test/test_dir_common.c | 6 +++---
4 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 58294f6cf..1ade52e81 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -3131,7 +3131,7 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
v3_out->voters = smartlist_new();
smartlist_add(v3_out->voters, voter);
- v3_out->cert = authority_cert_dup(cert);
+ v3_out->cert = dirvote_authority_cert_dup(cert);
v3_out->routerstatus_list = routerstatuses;
/* Note: networkstatus_digest is unset; it won't get set until we actually
* format the vote. */
diff --git a/src/or/dirvote_common.c b/src/or/dirvote_common.c
index b34f4fe20..9d2c867c8 100644
--- a/src/or/dirvote_common.c
+++ b/src/or/dirvote_common.c
@@ -200,7 +200,7 @@ dirvote_get_voter_sig_by_alg(const networkstatus_voter_info_t *voter,
/** Allocate and return a new authority_cert_t with the same contents as
* <b>cert</b>. */
authority_cert_t *
-authority_cert_dup(authority_cert_t *cert)
+dirvote_authority_cert_dup(authority_cert_t *cert)
{
authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
tor_assert(cert);
diff --git a/src/or/dirvote_common.h b/src/or/dirvote_common.h
index 16621244b..07e625937 100644
--- a/src/or/dirvote_common.h
+++ b/src/or/dirvote_common.h
@@ -63,7 +63,7 @@ document_signature_t *dirvote_get_voter_sig_by_alg(
digest_algorithm_t alg);
/* Cert manipulation */
-authority_cert_t *authority_cert_dup(authority_cert_t *cert);
+authority_cert_t *dirvote_authority_cert_dup(authority_cert_t *cert);
#endif /* TOR_DIRVOTE_COMMON_H */
diff --git a/src/test/test_dir_common.c b/src/test/test_dir_common.c
index 02d3295ca..8c6d99cce 100644
--- a/src/test/test_dir_common.c
+++ b/src/test/test_dir_common.c
@@ -307,7 +307,7 @@ dir_common_construct_vote_1(networkstatus_t **vote, authority_cert_t *cert,
* Set up a vote; generate it; try to parse it.
*/
smartlist_add((*vote)->voters, voter);
- (*vote)->cert = authority_cert_dup(cert);
+ (*vote)->cert = dirvote_authority_cert_dup(cert);
smartlist_split_string((*vote)->net_params, "circuitwindow=101 foo=990",
NULL, 0, 0);
*n_vrs = 0;
@@ -356,7 +356,7 @@ dir_common_construct_vote_2(networkstatus_t **vote, authority_cert_t *cert,
* Set up a vote; generate it; try to parse it.
*/
smartlist_add((*vote)->voters, voter);
- (*vote)->cert = authority_cert_dup(cert);
+ (*vote)->cert = dirvote_authority_cert_dup(cert);
if (! (*vote)->net_params)
(*vote)->net_params = smartlist_new();
smartlist_split_string((*vote)->net_params,
@@ -407,7 +407,7 @@ dir_common_construct_vote_3(networkstatus_t **vote, authority_cert_t *cert,
* Set up a vote; generate it; try to parse it.
*/
smartlist_add((*vote)->voters, voter);
- (*vote)->cert = authority_cert_dup(cert);
+ (*vote)->cert = dirvote_authority_cert_dup(cert);
smartlist_split_string((*vote)->net_params, "circuitwindow=80 foo=660",
NULL, 0, 0);
/* add routerstatuses */
1
0

01 May '18
commit 6ee6533fd89018036f11c5ee62897f8a2b6c39c0
Author: David Goulet <dgoulet(a)torproject.org>
Date: Wed Apr 25 10:49:32 2018 -0400
dirvote: Free vote commits in the dirauth module
In order to make sr_commit_free() only used by the dirauth module, this
commits moves the commits free from a vote object into the dirvote.c file
which is now only for the module.
The function does nothing if the module is disabled.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/dirauth/dirvote.c | 13 +++++++++++++
src/or/dirauth/dirvote.h | 7 +++++++
src/or/networkstatus.c | 4 +---
3 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/src/or/dirauth/dirvote.c b/src/or/dirauth/dirvote.c
index 3ec4d6741..53a896eb4 100644
--- a/src/or/dirauth/dirvote.c
+++ b/src/or/dirauth/dirvote.c
@@ -3899,3 +3899,16 @@ dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens)
}
}
+/* For the given vote, free the shared random commits if any. */
+void
+dirvote_clear_commits(networkstatus_t *ns)
+{
+ tor_assert(ns->type == NS_TYPE_VOTE);
+
+ if (ns->sr_info.commits) {
+ SMARTLIST_FOREACH(ns->sr_info.commits, sr_commit_t *, c,
+ sr_commit_free(c));
+ smartlist_free(ns->sr_info.commits);
+ }
+}
+
diff --git a/src/or/dirauth/dirvote.h b/src/or/dirauth/dirvote.h
index 4514c6daf..9682c6036 100644
--- a/src/or/dirauth/dirvote.h
+++ b/src/or/dirauth/dirvote.h
@@ -100,6 +100,7 @@ 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);
+void dirvote_clear_commits(networkstatus_t *ns);
#else /* HAVE_MODULE_DIRAUTH */
@@ -122,6 +123,12 @@ dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens)
(void) tokens;
}
+static inline void
+dirvote_clear_commits(networkstatus_t *ns)
+{
+ (void) ns;
+}
+
#endif /* HAVE_MODULE_DIRAUTH */
void dirvote_recalculate_timing(const or_options_t *options, time_t now);
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index fa8d0bcf1..5ca320d28 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -366,9 +366,7 @@ networkstatus_vote_free_(networkstatus_t *ns)
digestmap_free(ns->desc_digest_map, NULL);
if (ns->sr_info.commits) {
- SMARTLIST_FOREACH(ns->sr_info.commits, sr_commit_t *, c,
- sr_commit_free(c));
- smartlist_free(ns->sr_info.commits);
+ dirvote_clear_commits(ns);
}
tor_free(ns->sr_info.previous_srv);
tor_free(ns->sr_info.current_srv);
1
0

01 May '18
commit 2115a54b4ab91d6d573691fc7368fcb8a2db32af
Author: David Goulet <dgoulet(a)torproject.org>
Date: Thu Apr 5 14:27:30 2018 -0400
mod: Move dirauth specific files to its own module
This is a pretty big commit but it only moves these files to src/or/dirauth:
dircollate.c dirvote.c shared_random.c shared_random_state.c
dircollate.h dirvote.h shared_random.h shared_random_state.h
Then many files are modified to change the include line for those header files
that have moved into a new directory.
Without using --disable-module-dirauth, everything builds fine. When using the
flag to disable the module, tor doesn't build due to linking errors. This will
be addressed in the next commit(s).
No code behavior change.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/config.c | 3 ++-
src/or/control.c | 2 +-
src/or/{ => dirauth}/dircollate.c | 0
src/or/{ => dirauth}/dircollate.h | 0
src/or/{ => dirauth}/dirvote.c | 0
src/or/{ => dirauth}/dirvote.h | 0
src/or/{ => dirauth}/shared_random.c | 3 ++-
src/or/{ => dirauth}/shared_random.h | 0
src/or/{ => dirauth}/shared_random_state.c | 3 ++-
src/or/{ => dirauth}/shared_random_state.h | 0
src/or/directory.c | 5 +++--
src/or/dirserv.c | 3 ++-
src/or/hs_common.c | 4 ++--
src/or/hs_service.c | 2 +-
src/or/include.am | 20 ++++++++++----------
src/or/main.c | 5 +++--
src/or/networkstatus.c | 5 +++--
src/or/routerlist.c | 3 ++-
src/or/routerparse.c | 5 +++--
src/test/test_config.c | 2 +-
src/test/test_dir.c | 4 ++--
src/test/test_dir_common.c | 2 +-
src/test/test_dir_handle_get.c | 2 +-
src/test/test_hs_common.c | 4 ++--
src/test/test_hs_service.c | 4 ++--
src/test/test_microdesc.c | 2 +-
src/test/test_routerlist.c | 4 ++--
src/test/test_shared_random.c | 6 +++---
28 files changed, 51 insertions(+), 42 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index 87a3588db..8f5839e2d 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -79,7 +79,6 @@
#include "confparse.h"
#include "cpuworker.h"
#include "dirserv.h"
-#include "dirvote.h"
#include "dns.h"
#include "dos.h"
#include "entrynodes.h"
@@ -110,6 +109,8 @@
#include "procmon.h"
+#include "dirauth/dirvote.h"
+
#ifdef HAVE_SYSTEMD
# if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
/* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
diff --git a/src/or/control.c b/src/or/control.c
index dda887218..3e643c2d7 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -76,7 +76,7 @@
#include "router.h"
#include "routerlist.h"
#include "routerparse.h"
-#include "shared_random.h"
+#include "dirauth/shared_random.h"
#ifndef _WIN32
#include <pwd.h>
diff --git a/src/or/dircollate.c b/src/or/dirauth/dircollate.c
similarity index 100%
rename from src/or/dircollate.c
rename to src/or/dirauth/dircollate.c
diff --git a/src/or/dircollate.h b/src/or/dirauth/dircollate.h
similarity index 100%
rename from src/or/dircollate.h
rename to src/or/dirauth/dircollate.h
diff --git a/src/or/dirvote.c b/src/or/dirauth/dirvote.c
similarity index 100%
rename from src/or/dirvote.c
rename to src/or/dirauth/dirvote.c
diff --git a/src/or/dirvote.h b/src/or/dirauth/dirvote.h
similarity index 100%
rename from src/or/dirvote.h
rename to src/or/dirauth/dirvote.h
diff --git a/src/or/shared_random.c b/src/or/dirauth/shared_random.c
similarity index 99%
rename from src/or/shared_random.c
rename to src/or/dirauth/shared_random.c
index 13416d6bc..e9644adf9 100644
--- a/src/or/shared_random.c
+++ b/src/or/dirauth/shared_random.c
@@ -91,7 +91,6 @@
#include "shared_random.h"
#include "config.h"
#include "confparse.h"
-#include "dirvote.h"
#include "networkstatus.h"
#include "routerkeys.h"
#include "router.h"
@@ -99,6 +98,8 @@
#include "shared_random_state.h"
#include "util.h"
+#include "dirauth/dirvote.h"
+
/* String prefix of shared random values in votes/consensuses. */
static const char previous_srv_str[] = "shared-rand-previous-value";
static const char current_srv_str[] = "shared-rand-current-value";
diff --git a/src/or/shared_random.h b/src/or/dirauth/shared_random.h
similarity index 100%
rename from src/or/shared_random.h
rename to src/or/dirauth/shared_random.h
diff --git a/src/or/shared_random_state.c b/src/or/dirauth/shared_random_state.c
similarity index 99%
rename from src/or/shared_random_state.c
rename to src/or/dirauth/shared_random_state.c
index 53782af59..11d7a0637 100644
--- a/src/or/shared_random_state.c
+++ b/src/or/dirauth/shared_random_state.c
@@ -14,11 +14,12 @@
#include "shared_random.h"
#include "config.h"
#include "confparse.h"
-#include "dirvote.h"
#include "networkstatus.h"
#include "router.h"
#include "shared_random_state.h"
+#include "dirauth/dirvote.h"
+
/* Default filename of the shared random state on disk. */
static const char default_fname[] = "sr-state";
diff --git a/src/or/shared_random_state.h b/src/or/dirauth/shared_random_state.h
similarity index 100%
rename from src/or/shared_random_state.h
rename to src/or/dirauth/shared_random_state.h
diff --git a/src/or/directory.c b/src/or/directory.c
index c419b61d0..8a343ac02 100644
--- a/src/or/directory.c
+++ b/src/or/directory.c
@@ -20,7 +20,6 @@
#include "compat.h"
#include "directory.h"
#include "dirserv.h"
-#include "dirvote.h"
#include "entrynodes.h"
#include "geoip.h"
#include "hs_cache.h"
@@ -41,7 +40,7 @@
#include "routerlist.h"
#include "routerparse.h"
#include "routerset.h"
-#include "shared_random.h"
+#include "dirauth/shared_random.h"
#if defined(EXPORTMALLINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
#if !defined(OpenBSD)
@@ -49,6 +48,8 @@
#endif
#endif
+#include "dirauth/dirvote.h"
+
/**
* \file directory.c
* \brief Code to send and fetch information from directory authorities and
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 68727f071..c20e2966c 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -18,7 +18,6 @@
#include "control.h"
#include "directory.h"
#include "dirserv.h"
-#include "dirvote.h"
#include "hibernate.h"
#include "keypin.h"
#include "main.h"
@@ -34,6 +33,8 @@
#include "routerset.h"
#include "torcert.h"
+#include "dirauth/dirvote.h"
+
/**
* \file dirserv.c
* \brief Directory server core implementation. Manages directory
diff --git a/src/or/hs_common.c b/src/or/hs_common.c
index 24eb7a104..fc7bc5729 100644
--- a/src/or/hs_common.c
+++ b/src/or/hs_common.c
@@ -28,8 +28,8 @@
#include "rendservice.h"
#include "routerset.h"
#include "router.h"
-#include "shared_random.h"
-#include "shared_random_state.h"
+#include "dirauth/shared_random.h"
+#include "dirauth/shared_random_state.h"
/* Trunnel */
#include "ed25519_cert.h"
diff --git a/src/or/hs_service.c b/src/or/hs_service.c
index f6c7e3cd8..ebb58f7f6 100644
--- a/src/or/hs_service.c
+++ b/src/or/hs_service.c
@@ -24,7 +24,7 @@
#include "router.h"
#include "routerkeys.h"
#include "routerlist.h"
-#include "shared_random_state.h"
+#include "dirauth/shared_random_state.h"
#include "statefile.h"
#include "hs_circuit.h"
diff --git a/src/or/include.am b/src/or/include.am
index 56d41e47d..9265ecf48 100644
--- a/src/or/include.am
+++ b/src/or/include.am
@@ -41,10 +41,8 @@ LIBTOR_A_SOURCES = \
src/or/consdiffmgr.c \
src/or/control.c \
src/or/cpuworker.c \
- src/or/dircollate.c \
src/or/directory.c \
src/or/dirserv.c \
- src/or/dirvote.c \
src/or/dns.c \
src/or/dnsserv.c \
src/or/dos.c \
@@ -76,8 +74,6 @@ LIBTOR_A_SOURCES = \
src/or/onion.c \
src/or/onion_fast.c \
src/or/onion_tap.c \
- src/or/shared_random.c \
- src/or/shared_random_state.c \
src/or/transports.c \
src/or/parsecommon.c \
src/or/periodic.c \
@@ -121,7 +117,11 @@ LIBTOR_A_SOURCES = \
# The Directory Authority module.
if BUILD_MODULE_DIRAUTH
-#LIBTOR_A_SOURCES +=
+LIBTOR_A_SOURCES += \
+ src/or/dirauth/dircollate.c \
+ src/or/dirauth/dirvote.c \
+ src/or/dirauth/shared_random.c \
+ src/or/dirauth/shared_random_state.c
endif
src_or_libtor_a_SOURCES = $(LIBTOR_A_SOURCES)
@@ -195,10 +195,8 @@ ORHEADERS = \
src/or/consdiffmgr.h \
src/or/control.h \
src/or/cpuworker.h \
- src/or/dircollate.h \
src/or/directory.h \
src/or/dirserv.h \
- src/or/dirvote.h \
src/or/dns.h \
src/or/dns_structs.h \
src/or/dnsserv.h \
@@ -235,8 +233,6 @@ ORHEADERS = \
src/or/onion_ntor.h \
src/or/onion_tap.h \
src/or/or.h \
- src/or/shared_random.h \
- src/or/shared_random_state.h \
src/or/transports.h \
src/or/parsecommon.h \
src/or/periodic.h \
@@ -273,7 +269,11 @@ ORHEADERS = \
# properly compiled the entry points stub.
# The Directory Authority module headers.
-#ORHEADERS +=
+ORHEADERS += \
+ src/or/dirauth/dircollate.h \
+ src/or/dirauth/dirvote.h \
+ src/or/dirauth/shared_random.h \
+ src/or/dirauth/shared_random_state.h
# This may someday want to be an installed file?
noinst_HEADERS += src/or/tor_api.h
diff --git a/src/or/main.c b/src/or/main.c
index c1103edb3..b7b7b7162 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -72,7 +72,6 @@
#include "crypto_s2k.h"
#include "directory.h"
#include "dirserv.h"
-#include "dirvote.h"
#include "dns.h"
#include "dnsserv.h"
#include "dos.h"
@@ -103,7 +102,7 @@
#include "routerlist.h"
#include "routerparse.h"
#include "scheduler.h"
-#include "shared_random.h"
+#include "dirauth/shared_random.h"
#include "statefile.h"
#include "status.h"
#include "tor_api.h"
@@ -118,6 +117,8 @@
#include <event2/event.h>
+#include "dirauth/dirvote.h"
+
#ifdef HAVE_SYSTEMD
# if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
/* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index b0db0cecb..f78d9d672 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -50,7 +50,6 @@
#include "control.h"
#include "directory.h"
#include "dirserv.h"
-#include "dirvote.h"
#include "dos.h"
#include "entrynodes.h"
#include "hibernate.h"
@@ -64,11 +63,13 @@
#include "routerlist.h"
#include "routerparse.h"
#include "scheduler.h"
-#include "shared_random.h"
+#include "dirauth/shared_random.h"
#include "transports.h"
#include "torcert.h"
#include "channelpadding.h"
+#include "dirauth/dirvote.h"
+
/** Most recently received and validated v3 "ns"-flavored consensus network
* status. */
STATIC networkstatus_t *current_ns_consensus = NULL;
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 1bfbd9f67..7eb9ec799 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -101,7 +101,6 @@
#include "control.h"
#include "directory.h"
#include "dirserv.h"
-#include "dirvote.h"
#include "entrynodes.h"
#include "fp_pair.h"
#include "geoip.h"
@@ -122,6 +121,8 @@
#include "sandbox.h"
#include "torcert.h"
+#include "dirauth/dirvote.h"
+
// #define DEBUG_ROUTERLIST
/****************************************************************************/
diff --git a/src/or/routerparse.c b/src/or/routerparse.c
index 1834cfad2..84db23915 100644
--- a/src/or/routerparse.c
+++ b/src/or/routerparse.c
@@ -59,7 +59,6 @@
#include "config.h"
#include "circuitstats.h"
#include "dirserv.h"
-#include "dirvote.h"
#include "parsecommon.h"
#include "policies.h"
#include "protover.h"
@@ -75,11 +74,13 @@
#include "entrynodes.h"
#include "torcert.h"
#include "sandbox.h"
-#include "shared_random.h"
+#include "dirauth/shared_random.h"
#undef log
#include <math.h>
+#include "dirauth/dirvote.h"
+
/****************************************************************************/
/** List of tokens recognized in router descriptors */
diff --git a/src/test/test_config.c b/src/test/test_config.c
index 7983106a2..461aa646d 100644
--- a/src/test/test_config.c
+++ b/src/test/test_config.c
@@ -24,7 +24,7 @@
#include "control.h"
#include "cpuworker.h"
#include "dirserv.h"
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "dns.h"
#include "entrynodes.h"
#include "transports.h"
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index df5ae2d59..74153c671 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -25,7 +25,7 @@
#include "crypto_ed25519.h"
#include "directory.h"
#include "dirserv.h"
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "entrynodes.h"
#include "hibernate.h"
#include "memarea.h"
@@ -35,7 +35,7 @@
#include "routerlist.h"
#include "routerparse.h"
#include "routerset.h"
-#include "shared_random_state.h"
+#include "dirauth/shared_random_state.h"
#include "test.h"
#include "test_dir_common.h"
#include "torcert.h"
diff --git a/src/test/test_dir_common.c b/src/test/test_dir_common.c
index fdf43533a..c3e3662fd 100644
--- a/src/test/test_dir_common.c
+++ b/src/test/test_dir_common.c
@@ -9,7 +9,7 @@
#include "test.h"
#include "container.h"
#include "or.h"
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "nodelist.h"
#include "routerlist.h"
#include "test_dir_common.h"
diff --git a/src/test/test_dir_handle_get.c b/src/test/test_dir_handle_get.c
index 71faf70af..1bff249df 100644
--- a/src/test/test_dir_handle_get.c
+++ b/src/test/test_dir_handle_get.c
@@ -30,7 +30,7 @@
#include "proto_http.h"
#include "geoip.h"
#include "dirserv.h"
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "log_test_helpers.h"
#ifdef _WIN32
diff --git a/src/test/test_hs_common.c b/src/test/test_hs_common.c
index 17ba11ca7..43a2c5e80 100644
--- a/src/test/test_hs_common.c
+++ b/src/test/test_hs_common.c
@@ -23,12 +23,12 @@
#include "config.h"
#include "networkstatus.h"
#include "directory.h"
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "nodelist.h"
#include "routerlist.h"
#include "statefile.h"
#include "circuitlist.h"
-#include "shared_random.h"
+#include "dirauth/shared_random.h"
#include "util.h"
/** Test the validation of HS v3 addresses */
diff --git a/src/test/test_hs_service.c b/src/test/test_hs_service.c
index 481521520..fc8e016b5 100644
--- a/src/test/test_hs_service.c
+++ b/src/test/test_hs_service.c
@@ -34,7 +34,7 @@
#include "circuitlist.h"
#include "circuituse.h"
#include "crypto.h"
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "networkstatus.h"
#include "nodelist.h"
#include "relay.h"
@@ -51,7 +51,7 @@
#include "main.h"
#include "rendservice.h"
#include "statefile.h"
-#include "shared_random_state.h"
+#include "dirauth/shared_random_state.h"
/* Trunnel */
#include "hs/cell_establish_intro.h"
diff --git a/src/test/test_microdesc.c b/src/test/test_microdesc.c
index 94b4d5e51..4b168f49e 100644
--- a/src/test/test_microdesc.c
+++ b/src/test/test_microdesc.c
@@ -6,7 +6,7 @@
#include "config.h"
#define DIRVOTE_PRIVATE
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "microdesc.h"
#include "networkstatus.h"
#include "routerlist.h"
diff --git a/src/test/test_routerlist.c b/src/test/test_routerlist.c
index c19d66ef9..71b487f35 100644
--- a/src/test/test_routerlist.c
+++ b/src/test/test_routerlist.c
@@ -19,7 +19,7 @@
#include "container.h"
#include "control.h"
#include "directory.h"
-#include "dirvote.h"
+#include "dirauth/dirvote.h"
#include "entrynodes.h"
#include "hibernate.h"
#include "microdesc.h"
@@ -30,7 +30,7 @@
#include "routerlist.h"
#include "routerset.h"
#include "routerparse.h"
-#include "shared_random.h"
+#include "dirauth/shared_random.h"
#include "statefile.h"
#include "test.h"
#include "test_dir_common.h"
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
index 4fe9ee45f..30d0d44ca 100644
--- a/src/test/test_shared_random.c
+++ b/src/test/test_shared_random.c
@@ -9,9 +9,9 @@
#include "or.h"
#include "test.h"
#include "config.h"
-#include "dirvote.h"
-#include "shared_random.h"
-#include "shared_random_state.h"
+#include "dirauth/dirvote.h"
+#include "dirauth/shared_random.h"
+#include "dirauth/shared_random_state.h"
#include "routerkeys.h"
#include "routerlist.h"
#include "router.h"
1
0
commit bdcf3a383932cc48a360c9a75b93b44a6968171e
Author: David Goulet <dgoulet(a)torproject.org>
Date: Tue Apr 24 11:34:31 2018 -0400
sr: Extract shared SR functions
Move most of the shared random functions that are needed outside of the
dirauth module.
At this commit, because dirvote.c hasn't been refactor, it doesn't compile
because some SR functions need a dirvote function.
Furthermore, 5 functions haven't been touched yet because they are dirauth
only but are in used in other C files than the dirauth module ones.
No code behavior change. Only moving code around.
Signed-off-by: David Goulet <dgoulet(a)torproject.org>
---
src/or/control.c | 2 +-
src/or/dirauth/shared_random.c | 163 +---------------------
src/or/dirauth/shared_random.h | 8 --
src/or/dirauth/shared_random_state.c | 80 +----------
src/or/dirauth/shared_random_state.h | 5 -
src/or/hs_common.c | 2 +-
src/or/hs_service.c | 2 +-
src/or/include.am | 2 +
src/or/routerparse.c | 1 +
src/or/shared_random_common.c | 258 +++++++++++++++++++++++++++++++++++
src/or/shared_random_common.h | 47 +++++++
11 files changed, 313 insertions(+), 257 deletions(-)
diff --git a/src/or/control.c b/src/or/control.c
index 3e643c2d7..aab854ecd 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -76,7 +76,7 @@
#include "router.h"
#include "routerlist.h"
#include "routerparse.h"
-#include "dirauth/shared_random.h"
+#include "shared_random_common.h"
#ifndef _WIN32
#include <pwd.h>
diff --git a/src/or/dirauth/shared_random.c b/src/or/dirauth/shared_random.c
index e9644adf9..5dee77dce 100644
--- a/src/or/dirauth/shared_random.c
+++ b/src/or/dirauth/shared_random.c
@@ -96,6 +96,7 @@
#include "router.h"
#include "routerlist.h"
#include "shared_random_state.h"
+#include "shared_random_common.h"
#include "util.h"
#include "dirauth/dirvote.h"
@@ -499,20 +500,6 @@ 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. */
@@ -875,27 +862,6 @@ get_majority_srv_from_votes(const smartlist_t *votes, int current)
return the_srv;
}
-/* Encode the given shared random value and put it in dst. Destination
- * buffer must be at least SR_SRV_VALUE_BASE64_LEN plus the NULL byte. */
-void
-sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv)
-{
- int ret;
- /* Extra byte for the NULL terminated char. */
- char buf[SR_SRV_VALUE_BASE64_LEN + 1];
-
- tor_assert(dst);
- tor_assert(srv);
- tor_assert(dst_len >= sizeof(buf));
-
- ret = base64_encode(buf, sizeof(buf), (const char *) srv->value,
- sizeof(srv->value), 0);
- /* Always expect the full length without the NULL byte. */
- tor_assert(ret == (sizeof(buf) - 1));
- tor_assert(ret <= (int) dst_len);
- strlcpy(dst, buf, dst_len);
-}
-
/* Free a commit object. */
void
sr_commit_free_(sr_commit_t *commit)
@@ -1037,55 +1003,6 @@ sr_compute_srv(void)
tor_free(reveals);
}
-/* Parse a list of arguments from a SRV value either from a vote, consensus
- * or from our disk state and return a newly allocated srv object. NULL is
- * returned on error.
- *
- * The arguments' order:
- * num_reveals, value
- */
-sr_srv_t *
-sr_parse_srv(const smartlist_t *args)
-{
- char *value;
- int ok, ret;
- uint64_t num_reveals;
- sr_srv_t *srv = NULL;
-
- tor_assert(args);
-
- if (smartlist_len(args) < 2) {
- goto end;
- }
-
- /* First argument is the number of reveal values */
- num_reveals = tor_parse_uint64(smartlist_get(args, 0),
- 10, 0, UINT64_MAX, &ok, NULL);
- if (!ok) {
- goto end;
- }
- /* Second and last argument is the shared random value it self. */
- value = smartlist_get(args, 1);
- if (strlen(value) != SR_SRV_VALUE_BASE64_LEN) {
- goto end;
- }
-
- srv = tor_malloc_zero(sizeof(*srv));
- srv->num_reveals = num_reveals;
- /* We subtract one byte from the srclen because the function ignores the
- * '=' character in the given buffer. This is broken but it's a documented
- * behavior of the implementation. */
- ret = base64_decode((char *) srv->value, sizeof(srv->value), value,
- SR_SRV_VALUE_BASE64_LEN - 1);
- if (ret != sizeof(srv->value)) {
- tor_free(srv);
- srv = NULL;
- goto end;
- }
- end:
- return srv;
-}
-
/* Parse a commit from a vote or from our disk state and return a newly
* allocated commit object. NULL is returned on error.
*
@@ -1353,84 +1270,6 @@ 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;
-}
-
-/* Return current shared random value from the latest consensus. Caller can
- * NOT keep a reference to the returned pointer. Return NULL if none. */
-const sr_srv_t *
-sr_get_current(const networkstatus_t *ns)
-{
- const networkstatus_t *consensus;
-
- /* Use provided ns else get a live one */
- if (ns) {
- consensus = ns;
- } else {
- consensus = networkstatus_get_live_consensus(approx_time());
- }
- /* Ideally we would never be asked for an SRV without a live consensus. Make
- * sure this assumption is correct. */
- tor_assert_nonfatal(consensus);
-
- if (consensus) {
- return consensus->sr_info.current_srv;
- }
- return NULL;
-}
-
-/* Return previous shared random value from the latest consensus. Caller can
- * NOT keep a reference to the returned pointer. Return NULL if none. */
-const sr_srv_t *
-sr_get_previous(const networkstatus_t *ns)
-{
- const networkstatus_t *consensus;
-
- /* Use provided ns else get a live one */
- if (ns) {
- consensus = ns;
- } else {
- consensus = networkstatus_get_live_consensus(approx_time());
- }
- /* Ideally we would never be asked for an SRV without a live consensus. Make
- * sure this assumption is correct. */
- tor_assert_nonfatal(consensus);
-
- if (consensus) {
- return consensus->sr_info.previous_srv;
- }
- return NULL;
-}
-
#ifdef TOR_UNIT_TESTS
/* Set the global value of number of SRV agreements so the test can play
diff --git a/src/or/dirauth/shared_random.h b/src/or/dirauth/shared_random.h
index 675a8d8b0..d1b2ac2e2 100644
--- a/src/or/dirauth/shared_random.h
+++ b/src/or/dirauth/shared_random.h
@@ -109,13 +109,11 @@ void sr_act_post_consensus(const networkstatus_t *consensus);
void sr_handle_received_commits(smartlist_t *commits,
crypto_pk_t *voter_key);
sr_commit_t *sr_parse_commit(const smartlist_t *args);
-sr_srv_t *sr_parse_srv(const smartlist_t *args);
char *sr_get_string_for_vote(void);
char *sr_get_string_for_consensus(const smartlist_t *votes,
int32_t num_srv_agreements);
void sr_commit_free_(sr_commit_t *commit);
#define sr_commit_free(sr) FREE_AND_NULL(sr_commit_t, sr_commit_free_, (sr))
-void sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv);
/* Private methods (only used by shared_random_state.c): */
static inline
@@ -128,12 +126,6 @@ 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);
-
-const sr_srv_t *sr_get_current(const networkstatus_t *ns);
-const sr_srv_t *sr_get_previous(const networkstatus_t *ns);
-
#ifdef SHARED_RANDOM_PRIVATE
/* Encode */
diff --git a/src/or/dirauth/shared_random_state.c b/src/or/dirauth/shared_random_state.c
index 11d7a0637..846c3c7aa 100644
--- a/src/or/dirauth/shared_random_state.c
+++ b/src/or/dirauth/shared_random_state.c
@@ -17,6 +17,7 @@
#include "networkstatus.h"
#include "router.h"
#include "shared_random_state.h"
+#include "shared_random_common.h"
#include "dirauth/dirvote.h"
@@ -54,10 +55,6 @@ DUMMY_TYPECHECK_INSTANCE(sr_disk_state_t);
VAR(#member, conftype, member, initvalue)
/* Our persistent state magic number. */
#define SR_DISK_STATE_MAGIC 0x98AB1254
-/* Each protocol phase has 12 rounds */
-#define SHARED_RANDOM_N_ROUNDS 12
-/* Number of phase we have in a protocol. */
-#define SHARED_RANDOM_N_PHASES 2
static int
disk_state_validate_cb(void *old_state, void *state, void *default_state,
@@ -116,81 +113,6 @@ get_phase_str(sr_phase_t phase)
return the_string;
}
-
-/* Return the voting interval of the tor vote subsystem. */
-static int
-get_voting_interval(void)
-{
- int interval;
- networkstatus_t *consensus = networkstatus_get_live_consensus(time(NULL));
-
- if (consensus) {
- interval = (int)(consensus->fresh_until - consensus->valid_after);
- } else {
- /* Same for both a testing and real network. We voluntarily ignore the
- * InitialVotingInterval since it complexifies things and it doesn't
- * affect the SR protocol. */
- interval = get_options()->V3AuthVotingInterval;
- }
- tor_assert(interval > 0);
- return interval;
-}
-
-/* Given the time <b>now</b>, return the start time of the current round of
- * the SR protocol. For example, if it's 23:47:08, the current round thus
- * started at 23:47:00 for a voting interval of 10 seconds. */
-STATIC time_t
-get_start_time_of_current_round(void)
-{
- const or_options_t *options = get_options();
- int voting_interval = get_voting_interval();
- /* First, get the start time of the next round */
- time_t next_start = dirvote_get_next_valid_after_time();
- /* Now roll back next_start by a voting interval to find the start time of
- the current round. */
- time_t curr_start = dirvote_get_start_of_next_interval(
- next_start - voting_interval - 1,
- voting_interval,
- options->TestingV3AuthVotingStartOffset);
- return curr_start;
-}
-
-/** Return the start time of the current SR protocol run. For example, if the
- * time is 23/06/2017 23:47:08 and a full SR protocol run is 24 hours, this
- * function should return 23/06/2017 00:00:00. */
-time_t
-sr_state_get_start_time_of_current_protocol_run(time_t now)
-{
- int total_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
- int voting_interval = get_voting_interval();
- /* Find the time the current round started. */
- time_t beginning_of_current_round = get_start_time_of_current_round();
-
- /* Get current SR protocol round */
- int current_round = (now / voting_interval) % total_rounds;
-
- /* Get start time by subtracting the time elapsed from the beginning of the
- protocol run */
- time_t time_elapsed_since_start_of_run = current_round * voting_interval;
- return beginning_of_current_round - time_elapsed_since_start_of_run;
-}
-
-/** Return the time (in seconds) it takes to complete a full SR protocol phase
- * (e.g. the commit phase). */
-unsigned int
-sr_state_get_phase_duration(void)
-{
- return SHARED_RANDOM_N_ROUNDS * get_voting_interval();
-}
-
-/** Return the time (in seconds) it takes to complete a full SR protocol run */
-unsigned int
-sr_state_get_protocol_run_duration(void)
-{
- int total_protocol_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
- return total_protocol_rounds * get_voting_interval();
-}
-
/* Return the time we should expire the state file created at <b>now</b>.
* We expire the state file in the beginning of the next protocol run. */
STATIC time_t
diff --git a/src/or/dirauth/shared_random_state.h b/src/or/dirauth/shared_random_state.h
index fdbbf4919..60a326f86 100644
--- a/src/or/dirauth/shared_random_state.h
+++ b/src/or/dirauth/shared_random_state.h
@@ -121,16 +121,11 @@ int sr_state_is_initialized(void);
void sr_state_save(void);
void sr_state_free_all(void);
-time_t sr_state_get_start_time_of_current_protocol_run(time_t now);
-unsigned int sr_state_get_phase_duration(void);
-unsigned int sr_state_get_protocol_run_duration(void);
-
#ifdef SHARED_RANDOM_STATE_PRIVATE
STATIC int disk_state_load_from_disk_impl(const char *fname);
STATIC sr_phase_t get_sr_protocol_phase(time_t valid_after);
-STATIC time_t get_start_time_of_current_round(void);
STATIC time_t get_state_valid_until_time(time_t now);
STATIC const char *get_phase_str(sr_phase_t phase);
diff --git a/src/or/hs_common.c b/src/or/hs_common.c
index fc7bc5729..8cc7d0891 100644
--- a/src/or/hs_common.c
+++ b/src/or/hs_common.c
@@ -28,7 +28,7 @@
#include "rendservice.h"
#include "routerset.h"
#include "router.h"
-#include "dirauth/shared_random.h"
+#include "shared_random_common.h"
#include "dirauth/shared_random_state.h"
/* Trunnel */
diff --git a/src/or/hs_service.c b/src/or/hs_service.c
index ebb58f7f6..4ac0df2ad 100644
--- a/src/or/hs_service.c
+++ b/src/or/hs_service.c
@@ -24,7 +24,7 @@
#include "router.h"
#include "routerkeys.h"
#include "routerlist.h"
-#include "dirauth/shared_random_state.h"
+#include "shared_random_common.h"
#include "statefile.h"
#include "hs_circuit.h"
diff --git a/src/or/include.am b/src/or/include.am
index 9265ecf48..e90b1fb2a 100644
--- a/src/or/include.am
+++ b/src/or/include.am
@@ -103,6 +103,7 @@ LIBTOR_A_SOURCES = \
src/or/scheduler.c \
src/or/scheduler_kist.c \
src/or/scheduler_vanilla.c \
+ src/or/shared_random_common.c \
src/or/statefile.c \
src/or/status.c \
src/or/torcert.c \
@@ -260,6 +261,7 @@ ORHEADERS = \
src/or/routerset.h \
src/or/routerparse.h \
src/or/scheduler.h \
+ src/or/shared_random_common.h \
src/or/statefile.h \
src/or/status.h \
src/or/torcert.h \
diff --git a/src/or/routerparse.c b/src/or/routerparse.c
index 84db23915..2d64a82b6 100644
--- a/src/or/routerparse.c
+++ b/src/or/routerparse.c
@@ -74,6 +74,7 @@
#include "entrynodes.h"
#include "torcert.h"
#include "sandbox.h"
+#include "shared_random_common.h"
#include "dirauth/shared_random.h"
#undef log
diff --git a/src/or/shared_random_common.c b/src/or/shared_random_common.c
new file mode 100644
index 000000000..c782e709f
--- /dev/null
+++ b/src/or/shared_random_common.c
@@ -0,0 +1,258 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file shared_random_common.c
+ * \brief This file contains functions that are from the shared random
+ * subsystem but used by many part of tor. The full feature is built
+ * as part of the dirauth module.
+ **/
+
+#define SHARED_RANDOM_COMMON_PRIVATE
+#include "shared_random_common.h"
+
+#include "config.h"
+#include "networkstatus.h"
+#include "util.h"
+#include "util_format.h"
+
+/* 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 the voting interval of the tor vote subsystem. */
+int
+get_voting_interval(void)
+{
+ int interval;
+ networkstatus_t *consensus = networkstatus_get_live_consensus(time(NULL));
+
+ if (consensus) {
+ interval = (int)(consensus->fresh_until - consensus->valid_after);
+ } else {
+ /* Same for both a testing and real network. We voluntarily ignore the
+ * InitialVotingInterval since it complexifies things and it doesn't
+ * affect the SR protocol. */
+ interval = get_options()->V3AuthVotingInterval;
+ }
+ tor_assert(interval > 0);
+ return interval;
+}
+
+/* Given the time <b>now</b>, return the start time of the current round of
+ * the SR protocol. For example, if it's 23:47:08, the current round thus
+ * started at 23:47:00 for a voting interval of 10 seconds. */
+time_t
+get_start_time_of_current_round(void)
+{
+ const or_options_t *options = get_options();
+ int voting_interval = get_voting_interval();
+ /* First, get the start time of the next round */
+ time_t next_start = dirvote_get_next_valid_after_time();
+ /* Now roll back next_start by a voting interval to find the start time of
+ the current round. */
+ time_t curr_start = dirvote_get_start_of_next_interval(
+ next_start - voting_interval - 1,
+ voting_interval,
+ options->TestingV3AuthVotingStartOffset);
+ return curr_start;
+}
+
+/*
+ * Public API
+ */
+
+/* Encode the given shared random value and put it in dst. Destination
+ * buffer must be at least SR_SRV_VALUE_BASE64_LEN plus the NULL byte. */
+void
+sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv)
+{
+ int ret;
+ /* Extra byte for the NULL terminated char. */
+ char buf[SR_SRV_VALUE_BASE64_LEN + 1];
+
+ tor_assert(dst);
+ tor_assert(srv);
+ tor_assert(dst_len >= sizeof(buf));
+
+ ret = base64_encode(buf, sizeof(buf), (const char *) srv->value,
+ sizeof(srv->value), 0);
+ /* Always expect the full length without the NULL byte. */
+ tor_assert(ret == (sizeof(buf) - 1));
+ tor_assert(ret <= (int) dst_len);
+ strlcpy(dst, buf, dst_len);
+}
+
+/* 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;
+}
+
+/* Return current shared random value from the latest consensus. Caller can
+ * NOT keep a reference to the returned pointer. Return NULL if none. */
+const sr_srv_t *
+sr_get_current(const networkstatus_t *ns)
+{
+ const networkstatus_t *consensus;
+
+ /* Use provided ns else get a live one */
+ if (ns) {
+ consensus = ns;
+ } else {
+ consensus = networkstatus_get_live_consensus(approx_time());
+ }
+ /* Ideally we would never be asked for an SRV without a live consensus. Make
+ * sure this assumption is correct. */
+ tor_assert_nonfatal(consensus);
+
+ if (consensus) {
+ return consensus->sr_info.current_srv;
+ }
+ return NULL;
+}
+
+/* Return previous shared random value from the latest consensus. Caller can
+ * NOT keep a reference to the returned pointer. Return NULL if none. */
+const sr_srv_t *
+sr_get_previous(const networkstatus_t *ns)
+{
+ const networkstatus_t *consensus;
+
+ /* Use provided ns else get a live one */
+ if (ns) {
+ consensus = ns;
+ } else {
+ consensus = networkstatus_get_live_consensus(approx_time());
+ }
+ /* Ideally we would never be asked for an SRV without a live consensus. Make
+ * sure this assumption is correct. */
+ tor_assert_nonfatal(consensus);
+
+ if (consensus) {
+ return consensus->sr_info.previous_srv;
+ }
+ return NULL;
+}
+
+/* Parse a list of arguments from a SRV value either from a vote, consensus
+ * or from our disk state and return a newly allocated srv object. NULL is
+ * returned on error.
+ *
+ * The arguments' order:
+ * num_reveals, value
+ */
+sr_srv_t *
+sr_parse_srv(const smartlist_t *args)
+{
+ char *value;
+ int ok, ret;
+ uint64_t num_reveals;
+ sr_srv_t *srv = NULL;
+
+ tor_assert(args);
+
+ if (smartlist_len(args) < 2) {
+ goto end;
+ }
+
+ /* First argument is the number of reveal values */
+ num_reveals = tor_parse_uint64(smartlist_get(args, 0),
+ 10, 0, UINT64_MAX, &ok, NULL);
+ if (!ok) {
+ goto end;
+ }
+ /* Second and last argument is the shared random value it self. */
+ value = smartlist_get(args, 1);
+ if (strlen(value) != SR_SRV_VALUE_BASE64_LEN) {
+ goto end;
+ }
+
+ srv = tor_malloc_zero(sizeof(*srv));
+ srv->num_reveals = num_reveals;
+ /* We subtract one byte from the srclen because the function ignores the
+ * '=' character in the given buffer. This is broken but it's a documented
+ * behavior of the implementation. */
+ ret = base64_decode((char *) srv->value, sizeof(srv->value), value,
+ SR_SRV_VALUE_BASE64_LEN - 1);
+ if (ret != sizeof(srv->value)) {
+ tor_free(srv);
+ srv = NULL;
+ goto end;
+ }
+ end:
+ return srv;
+}
+
+/** Return the start time of the current SR protocol run. For example, if the
+ * time is 23/06/2017 23:47:08 and a full SR protocol run is 24 hours, this
+ * function should return 23/06/2017 00:00:00. */
+time_t
+sr_state_get_start_time_of_current_protocol_run(time_t now)
+{
+ int total_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
+ int voting_interval = get_voting_interval();
+ /* Find the time the current round started. */
+ time_t beginning_of_current_round = get_start_time_of_current_round();
+
+ /* Get current SR protocol round */
+ int current_round = (now / voting_interval) % total_rounds;
+
+ /* Get start time by subtracting the time elapsed from the beginning of the
+ protocol run */
+ time_t time_elapsed_since_start_of_run = current_round * voting_interval;
+ return beginning_of_current_round - time_elapsed_since_start_of_run;
+}
+
+/** Return the time (in seconds) it takes to complete a full SR protocol phase
+ * (e.g. the commit phase). */
+unsigned int
+sr_state_get_phase_duration(void)
+{
+ return SHARED_RANDOM_N_ROUNDS * get_voting_interval();
+}
+
+/** Return the time (in seconds) it takes to complete a full SR protocol run */
+unsigned int
+sr_state_get_protocol_run_duration(void)
+{
+ int total_protocol_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
+ return total_protocol_rounds * get_voting_interval();
+}
+
diff --git a/src/or/shared_random_common.h b/src/or/shared_random_common.h
new file mode 100644
index 000000000..35e08ddf3
--- /dev/null
+++ b/src/or/shared_random_common.h
@@ -0,0 +1,47 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file shared_random_common.h
+ * \brief Header file for shared_random_common.c.
+ **/
+
+#ifndef TOR_SHARED_RANDOM_COMMON_H
+#define TOR_SHARED_RANDOM_COMMON_H
+
+/* Dirauth module. */
+#include "dirauth/shared_random.h"
+
+/* Helper functions. */
+void sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv);
+int get_voting_interval(void);
+
+/* Control port functions. */
+char *sr_get_current_for_control(void);
+char *sr_get_previous_for_control(void);
+
+/* SRV functions. */
+const sr_srv_t *sr_get_current(const networkstatus_t *ns);
+const sr_srv_t *sr_get_previous(const networkstatus_t *ns);
+sr_srv_t *sr_parse_srv(const smartlist_t *args);
+
+/*
+ * Shared Random State API
+ */
+
+/* Each protocol phase has 12 rounds */
+#define SHARED_RANDOM_N_ROUNDS 12
+/* Number of phase we have in a protocol. */
+#define SHARED_RANDOM_N_PHASES 2
+
+time_t sr_state_get_start_time_of_current_protocol_run(time_t now);
+unsigned int sr_state_get_phase_duration(void);
+unsigned int sr_state_get_protocol_run_duration(void);
+time_t get_start_time_of_current_round(void);
+
+#ifdef TOR_UNIT_TESTS
+
+#endif /* TOR_UNIT_TESTS */
+
+#endif /* TOR_SHARED_RANDOM_COMMON_H */
+
1
0