Tor  0.4.4.0-alpha-dev
shared_random_state.c
Go to the documentation of this file.
1 /* Copyright (c) 2016-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file shared_random_state.c
6  *
7  * \brief Functions and data structures for the state of the random protocol
8  * as defined in proposal #250.
9  **/
10 
11 #define SHARED_RANDOM_STATE_PRIVATE
12 
13 #include "core/or/or.h"
14 #include "app/config/config.h"
15 #include "lib/confmgt/confmgt.h"
19 #include "feature/relay/router.h"
24 #include "lib/encoding/confline.h"
25 #include "lib/version/torversion.h"
26 
27 #include "app/config/or_state_st.h"
28 
29 /** Default filename of the shared random state on disk. */
30 static const char default_fname[] = "sr-state";
31 
32 /** String representation of a protocol phase. */
33 static const char *phase_str[] = { "unknown", "commit", "reveal" };
34 
35 /** Our shared random protocol state. There is only one possible state per
36  * protocol run so this is the global state which is reset at every run once
37  * the shared random value has been computed. */
38 static sr_state_t *sr_state = NULL;
39 
40 /** Representation of our persistent state on disk. The sr_state above
41  * contains the data parsed from this state. When we save to disk, we
42  * translate the sr_state to this sr_disk_state. */
44 
45 /* Disk state file keys. */
46 static const char dstate_commit_key[] = "Commit";
47 static const char dstate_prev_srv_key[] = "SharedRandPreviousValue";
48 static const char dstate_cur_srv_key[] = "SharedRandCurrentValue";
49 
50 /** dummy instance of sr_disk_state_t, used for type-checking its
51  * members with CONF_CHECK_VAR_TYPE. */
53 
54 #define VAR(varname,conftype,member,initvalue) \
55  CONFIG_VAR_ETYPE(sr_disk_state_t, varname, conftype, member, 0, initvalue)
56 #define V(member,conftype,initvalue) \
57  VAR(#member, conftype, member, initvalue)
58 
59 /** Our persistent state magic number. */
60 #define SR_DISK_STATE_MAGIC 0x98AB1254
61 
62 /** Array of variables that are saved to disk as a persistent state. */
63 static const config_var_t state_vars[] = {
64  V(Version, POSINT, "0"),
65  V(TorVersion, STRING, NULL),
66  V(ValidAfter, ISOTIME, NULL),
67  V(ValidUntil, ISOTIME, NULL),
68 
69  V(Commit, LINELIST, NULL),
70 
71  V(SharedRandValues, LINELIST_V, NULL),
72  VAR("SharedRandPreviousValue",LINELIST_S, SharedRandValues, NULL),
73  VAR("SharedRandCurrentValue", LINELIST_S, SharedRandValues, NULL),
75 };
76 
77 /** "Extra" variable in the state that receives lines we can't parse. This
78  * lets us preserve options from versions of Tor newer than us. */
80  .name = "__extra",
81  .type = CONFIG_TYPE_LINELIST,
82  .offset = offsetof(sr_disk_state_t, ExtraLines),
83 };
84 
85 /** Configuration format of sr_disk_state_t. */
86 static const config_format_t state_format = {
87  .size = sizeof(sr_disk_state_t),
88  .magic = {
89  "sr_disk_state_t",
91  offsetof(sr_disk_state_t, magic_),
92  },
93  .vars = state_vars,
94  .extra = &state_extra_var,
95 };
96 
97 /** Global configuration manager for the shared-random state file */
99 
100 /** Return the configuration manager for the shared-random state file. */
101 static const config_mgr_t *
103 {
104  if (PREDICT_UNLIKELY(shared_random_state_mgr == NULL)) {
107  }
109 }
110 
111 static void state_query_del_(sr_state_object_t obj_type, void *data);
112 
113 /** Return a string representation of a protocol phase. */
114 STATIC const char *
116 {
117  const char *the_string = NULL;
118 
119  switch (phase) {
120  case SR_PHASE_COMMIT:
121  case SR_PHASE_REVEAL:
122  the_string = phase_str[phase];
123  break;
124  default:
125  /* Unknown phase shouldn't be possible. */
126  tor_assert(0);
127  }
128 
129  return the_string;
130 }
131 /** Return the time we should expire the state file created at <b>now</b>.
132  * We expire the state file in the beginning of the next protocol run. */
133 STATIC time_t
135 {
136  int total_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
137  int current_round, voting_interval, rounds_left;
138  time_t valid_until, beginning_of_current_round;
139 
140  voting_interval = get_voting_interval();
141  /* Find the time the current round started. */
142  beginning_of_current_round = get_start_time_of_current_round();
143 
144  /* Find how many rounds are left till the end of the protocol run */
145  current_round = (now / voting_interval) % total_rounds;
146  rounds_left = total_rounds - current_round;
147 
148  /* To find the valid-until time now, take the start time of the current
149  * round and add to it the time it takes for the leftover rounds to
150  * complete. */
151  valid_until = beginning_of_current_round + (rounds_left * voting_interval);
152 
153  { /* Logging */
154  char tbuf[ISO_TIME_LEN + 1];
155  format_iso_time(tbuf, valid_until);
156  log_debug(LD_DIR, "SR: Valid until time for state set to %s.", tbuf);
157  }
158 
159  return valid_until;
160 }
161 
162 /** Given the consensus 'valid-after' time, return the protocol phase we should
163  * be in. */
165 get_sr_protocol_phase(time_t valid_after)
166 {
167  /* Shared random protocol has two phases, commit and reveal. */
168  int total_periods = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
169  int current_slot;
170 
171  /* Split time into slots of size 'voting_interval'. See which slot we are
172  * currently into, and find which phase it corresponds to. */
173  current_slot = (valid_after / get_voting_interval()) % total_periods;
174 
175  if (current_slot < SHARED_RANDOM_N_ROUNDS) {
176  return SR_PHASE_COMMIT;
177  } else {
178  return SR_PHASE_REVEAL;
179  }
180 }
181 
182 /** Add the given <b>commit</b> to <b>state</b>. It MUST be a valid commit
183  * and there shouldn't be a commit from the same authority in the state
184  * already else verification hasn't been done prior. This takes ownership of
185  * the commit once in our state. */
186 static void
188 {
189  sr_commit_t *saved_commit;
190 
191  tor_assert(commit);
192  tor_assert(state);
193 
194  saved_commit = digestmap_set(state->commits, commit->rsa_identity,
195  commit);
196  if (saved_commit != NULL) {
197  /* This means we already have that commit in our state so adding twice
198  * the same commit is either a code flow error, a corrupted disk state
199  * or some new unknown issue. */
200  log_warn(LD_DIR, "SR: Commit from %s exists in our state while "
201  "adding it: '%s'", sr_commit_get_rsa_fpr(commit),
202  commit->encoded_commit);
203  sr_commit_free(saved_commit);
204  }
205 }
206 
207 /** Helper: deallocate a commit object. (Used with digestmap_free(), which
208  * requires a function pointer whose argument is void *). */
209 static void
210 commit_free_(void *p)
211 {
212  sr_commit_free_(p);
213 }
214 
215 #define state_free(val) \
216  FREE_AND_NULL(sr_state_t, state_free_, (val))
217 
218 /** Free a state that was allocated with state_new(). */
219 static void
221 {
222  if (state == NULL) {
223  return;
224  }
225  tor_free(state->fname);
226  digestmap_free(state->commits, commit_free_);
227  tor_free(state->current_srv);
228  tor_free(state->previous_srv);
229  tor_free(state);
230 }
231 
232 /** Allocate an sr_state_t object and returns it. If no <b>fname</b>, the
233  * default file name is used. This function does NOT initialize the state
234  * timestamp, phase or shared random value. NULL is never returned. */
235 static sr_state_t *
236 state_new(const char *fname, time_t now)
237 {
238  sr_state_t *new_state = tor_malloc_zero(sizeof(*new_state));
239  /* If file name is not provided, use default. */
240  if (fname == NULL) {
241  fname = default_fname;
242  }
243  new_state->fname = tor_strdup(fname);
244  new_state->version = SR_PROTO_VERSION;
245  new_state->commits = digestmap_new();
246  new_state->phase = get_sr_protocol_phase(now);
247  new_state->valid_until = get_state_valid_until_time(now);
248  return new_state;
249 }
250 
251 /** Set our global state pointer with the one given. */
252 static void
254 {
255  tor_assert(state);
256  if (sr_state != NULL) {
257  state_free(sr_state);
258  }
259  sr_state = state;
260 }
261 
262 #define disk_state_free(val) \
263  FREE_AND_NULL(sr_disk_state_t, disk_state_free_, (val))
264 
265 /** Free an allocated disk state. */
266 static void
268 {
269  if (state == NULL) {
270  return;
271  }
272  config_free(get_srs_mgr(), state);
273 }
274 
275 /** Allocate a new disk state, initialize it and return it. */
276 static sr_disk_state_t *
277 disk_state_new(time_t now)
278 {
279  sr_disk_state_t *new_state = config_new(get_srs_mgr());
280 
281  new_state->Version = SR_PROTO_VERSION;
282  new_state->TorVersion = tor_strdup(get_version());
283  new_state->ValidUntil = get_state_valid_until_time(now);
284  new_state->ValidAfter = now;
285 
286  /* Init config format. */
287  config_init(get_srs_mgr(), new_state);
288  return new_state;
289 }
290 
291 /** Set our global disk state with the given state. */
292 static void
294 {
295  tor_assert(state);
296  if (sr_disk_state != NULL) {
297  disk_state_free(sr_disk_state);
298  }
299  sr_disk_state = state;
300 }
301 
302 /** Return -1 if the disk state is invalid (something in there that we can't or
303  * shouldn't use). Return 0 if everything checks out. */
304 static int
306 {
307  time_t now;
308 
309  tor_assert(state);
310 
311  /* Do we support the protocol version in the state or is it 0 meaning
312  * Version wasn't found in the state file or bad anyway ? */
313  if (state->Version == 0 || state->Version > SR_PROTO_VERSION) {
314  goto invalid;
315  }
316 
317  /* If the valid until time is before now, we shouldn't use that state. */
318  now = time(NULL);
319  if (state->ValidUntil < now) {
320  log_info(LD_DIR, "SR: Disk state has expired. Ignoring it.");
321  goto invalid;
322  }
323 
324  /* Make sure we don't have a valid after time that is earlier than a valid
325  * until time which would make things not work well. */
326  if (state->ValidAfter >= state->ValidUntil) {
327  log_info(LD_DIR, "SR: Disk state valid after/until times are invalid.");
328  goto invalid;
329  }
330 
331  return 0;
332 
333  invalid:
334  return -1;
335 }
336 
337 /** Parse the Commit line(s) in the disk state and translate them to the
338  * the memory state. Return 0 on success else -1 on error. */
339 static int
341  const sr_disk_state_t *disk_state)
342 {
343  config_line_t *line;
344  smartlist_t *args = NULL;
345 
346  tor_assert(state);
347  tor_assert(disk_state);
348 
349  for (line = disk_state->Commit; line; line = line->next) {
350  sr_commit_t *commit = NULL;
351 
352  /* Extra safety. */
353  if (strcasecmp(line->key, dstate_commit_key) ||
354  line->value == NULL) {
355  /* Ignore any lines that are not commits. */
357  continue;
358  }
359  args = smartlist_new();
360  smartlist_split_string(args, line->value, " ",
361  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
362  if (smartlist_len(args) < 3) {
363  log_warn(LD_BUG, "SR: Too few arguments in Commit Line: %s",
364  escaped(line->value));
365  goto error;
366  }
367  commit = sr_parse_commit(args);
368  if (commit == NULL) {
369  /* Ignore badly formed commit. It could also be a authority
370  * fingerprint that we don't know about so it shouldn't be used. */
371  smartlist_free(args);
372  continue;
373  }
374  /* We consider parseable commit from our disk state to be valid because
375  * they need to be in the first place to get in there. */
376  commit->valid = 1;
377  /* Add commit to our state pointer. */
378  commit_add_to_state(commit, state);
379 
380  SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
381  smartlist_free(args);
382  }
383 
384  return 0;
385 
386  error:
387  SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
388  smartlist_free(args);
389  return -1;
390 }
391 
392 /** Parse a share random value line from the disk state and save it to dst
393  * which is an allocated srv object. Return 0 on success else -1. */
394 static int
395 disk_state_parse_srv(const char *value, sr_srv_t *dst)
396 {
397  int ret = -1;
398  smartlist_t *args;
399  sr_srv_t *srv;
400 
401  tor_assert(value);
402  tor_assert(dst);
403 
404  args = smartlist_new();
405  smartlist_split_string(args, value, " ",
406  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
407  if (smartlist_len(args) < 2) {
408  log_warn(LD_BUG, "SR: Too few arguments in shared random value. "
409  "Line: %s", escaped(value));
410  goto error;
411  }
412  srv = sr_parse_srv(args);
413  if (srv == NULL) {
414  goto error;
415  }
416  dst->num_reveals = srv->num_reveals;
417  memcpy(dst->value, srv->value, sizeof(dst->value));
418  tor_free(srv);
419  ret = 0;
420 
421  error:
422  SMARTLIST_FOREACH(args, char *, s, tor_free(s));
423  smartlist_free(args);
424  return ret;
425 }
426 
427 /** Parse both SharedRandCurrentValue and SharedRandPreviousValue line from
428  * the state. Return 0 on success else -1. */
429 static int
431  const sr_disk_state_t *disk_state)
432 {
433  /* Only one value per type (current or previous) is allowed so we keep
434  * track of it with these flag. */
435  unsigned int seen_previous = 0, seen_current = 0;
436  config_line_t *line;
437  sr_srv_t *srv = NULL;
438 
439  tor_assert(state);
440  tor_assert(disk_state);
441 
442  for (line = disk_state->SharedRandValues; line; line = line->next) {
443  if (line->value == NULL) {
444  continue;
445  }
446  srv = tor_malloc_zero(sizeof(*srv));
447  if (disk_state_parse_srv(line->value, srv) < 0) {
448  log_warn(LD_BUG, "SR: Broken current SRV line in state %s",
449  escaped(line->value));
450  goto bad;
451  }
452  if (!strcasecmp(line->key, dstate_prev_srv_key)) {
453  if (seen_previous) {
454  log_warn(LD_DIR, "SR: Second previous SRV value seen. Bad state");
455  goto bad;
456  }
457  state->previous_srv = srv;
458  seen_previous = 1;
459  } else if (!strcasecmp(line->key, dstate_cur_srv_key)) {
460  if (seen_current) {
461  log_warn(LD_DIR, "SR: Second current SRV value seen. Bad state");
462  goto bad;
463  }
464  state->current_srv = srv;
465  seen_current = 1;
466  } else {
467  /* Unknown key. Ignoring. */
468  tor_free(srv);
469  }
470  }
471 
472  return 0;
473  bad:
474  tor_free(srv);
475  return -1;
476 }
477 
478 /** Parse the given disk state and set a newly allocated state. On success,
479  * return that state else NULL. */
480 static sr_state_t *
481 disk_state_parse(const sr_disk_state_t *new_disk_state)
482 {
483  sr_state_t *new_state = state_new(default_fname, time(NULL));
484 
485  tor_assert(new_disk_state);
486 
487  new_state->version = new_disk_state->Version;
488  new_state->valid_until = new_disk_state->ValidUntil;
489  new_state->valid_after = new_disk_state->ValidAfter;
490 
491  /* Set our current phase according to the valid-after time in our disk
492  * state. The disk state we are parsing contains everything for the phase
493  * starting at valid_after so make sure our phase reflects that. */
494  new_state->phase = get_sr_protocol_phase(new_state->valid_after);
495 
496  /* Parse the shared random values. */
497  if (disk_state_parse_sr_values(new_state, new_disk_state) < 0) {
498  goto error;
499  }
500  /* Parse the commits. */
501  if (disk_state_parse_commits(new_state, new_disk_state) < 0) {
502  goto error;
503  }
504  /* Great! This new state contains everything we had on disk. */
505  return new_state;
506 
507  error:
508  state_free(new_state);
509  return NULL;
510 }
511 
512 /** From a valid commit object and an allocated config line, set the line's
513  * value to the state string representation of a commit. */
514 static void
516 {
517  char *reveal_str = NULL;
518 
519  tor_assert(commit);
520  tor_assert(line);
521 
522  if (!fast_mem_is_zero(commit->encoded_reveal,
523  sizeof(commit->encoded_reveal))) {
524  /* Add extra whitespace so we can format the line correctly. */
525  tor_asprintf(&reveal_str, " %s", commit->encoded_reveal);
526  }
527  tor_asprintf(&line->value, "%u %s %s %s%s",
530  sr_commit_get_rsa_fpr(commit),
531  commit->encoded_commit,
532  reveal_str != NULL ? reveal_str : "");
533  if (reveal_str != NULL) {
534  memwipe(reveal_str, 0, strlen(reveal_str));
535  tor_free(reveal_str);
536  }
537 }
538 
539 /** From a valid srv object and an allocated config line, set the line's
540  * value to the state string representation of a shared random value. */
541 static void
543 {
544  char encoded[SR_SRV_VALUE_BASE64_LEN + 1];
545 
546  tor_assert(line);
547 
548  /* No SRV value thus don't add the line. This is possible since we might
549  * not have a current or previous SRV value in our state. */
550  if (srv == NULL) {
551  return;
552  }
553  sr_srv_encode(encoded, sizeof(encoded), srv);
554  tor_asprintf(&line->value, "%" PRIu64 " %s", srv->num_reveals, encoded);
555 }
556 
557 /** Reset disk state that is free allocated memory and zeroed the object. */
558 static void
560 {
561  /* Free allocated memory */
562  config_free_lines(sr_disk_state->Commit);
563  config_free_lines(sr_disk_state->SharedRandValues);
564  config_free_lines(sr_disk_state->ExtraLines);
566 
567  /* Clear other fields. */
570  sr_disk_state->Version = 0;
571 
572  /* Reset it with useful data */
573  sr_disk_state->TorVersion = tor_strdup(get_version());
574 }
575 
576 /** Update our disk state based on our global SR state. */
577 static void
579 {
580  config_line_t **next, *line;
581 
582  if (BUG(!sr_disk_state))
583  return;
584  if (BUG(!sr_state))
585  return;
586 
587  /* Reset current disk state. */
589 
590  /* First, update elements that we don't need to do a construction. */
594 
595  /* Shared random values. */
597  if (sr_state->previous_srv != NULL) {
598  *next = line = tor_malloc_zero(sizeof(config_line_t));
599  line->key = tor_strdup(dstate_prev_srv_key);
601  /* Go to the next shared random value. */
602  next = &(line->next);
603  }
604  if (sr_state->current_srv != NULL) {
605  *next = line = tor_malloc_zero(sizeof(*line));
606  line->key = tor_strdup(dstate_cur_srv_key);
608  }
609 
610  /* Parse the commits and construct config line(s). */
611  next = &sr_disk_state->Commit;
612  DIGESTMAP_FOREACH(sr_state->commits, key, sr_commit_t *, commit) {
613  *next = line = tor_malloc_zero(sizeof(*line));
614  line->key = tor_strdup(dstate_commit_key);
615  disk_state_put_commit_line(commit, line);
616  next = &(line->next);
618 }
619 
620 /** Load state from disk and put it into our disk state. If the state passes
621  * validation, our global state will be updated with it. Return 0 on
622  * success. On error, -EINVAL is returned if the state on disk did contained
623  * something malformed or is unreadable. -ENOENT is returned indicating that
624  * the state file is either empty of non existing. */
625 static int
627 {
628  int ret;
629  char *fname;
630 
631  fname = get_datadir_fname(default_fname);
632  ret = disk_state_load_from_disk_impl(fname);
633  tor_free(fname);
634 
635  return ret;
636 }
637 
638 /** Helper for disk_state_load_from_disk(). */
639 STATIC int
641 {
642  int ret;
643  char *content = NULL;
644  sr_state_t *parsed_state = NULL;
645  sr_disk_state_t *disk_state = NULL;
646 
647  /* Read content of file so we can parse it. */
648  if ((content = read_file_to_str(fname, 0, NULL)) == NULL) {
649  log_warn(LD_FS, "SR: Unable to read SR state file %s",
650  escaped(fname));
651  ret = -errno;
652  goto error;
653  }
654 
655  {
656  config_line_t *lines = NULL;
657  char *errmsg = NULL;
658 
659  /* Every error in this code path will return EINVAL. */
660  ret = -EINVAL;
661  if (config_get_lines(content, &lines, 0) < 0) {
662  config_free_lines(lines);
663  goto error;
664  }
665 
666  disk_state = disk_state_new(time(NULL));
667  config_assign(get_srs_mgr(), disk_state, lines, 0, &errmsg);
668  config_free_lines(lines);
669  if (errmsg) {
670  log_warn(LD_DIR, "SR: Reading state error: %s", errmsg);
671  tor_free(errmsg);
672  goto error;
673  }
674  }
675 
676  /* So far so good, we've loaded our state file into our disk state. Let's
677  * validate it and then parse it. */
678  if (disk_state_validate(disk_state) < 0) {
679  ret = -EINVAL;
680  goto error;
681  }
682 
683  parsed_state = disk_state_parse(disk_state);
684  if (parsed_state == NULL) {
685  ret = -EINVAL;
686  goto error;
687  }
688  state_set(parsed_state);
689  disk_state_set(disk_state);
690  tor_free(content);
691  log_info(LD_DIR, "SR: State loaded successfully from file %s", fname);
692  return 0;
693 
694  error:
695  disk_state_free(disk_state);
696  tor_free(content);
697  return ret;
698 }
699 
700 /** Save the disk state to disk but before that update it from the current
701  * state so we always have the latest. Return 0 on success else -1. */
702 static int
704 {
705  int ret;
706  char *state, *content = NULL, *fname = NULL;
707  char tbuf[ISO_TIME_LEN + 1];
708  time_t now = time(NULL);
709 
710  /* If we didn't have the opportunity to setup an internal disk state,
711  * don't bother saving something to disk. */
712  if (sr_disk_state == NULL) {
713  ret = 0;
714  goto done;
715  }
716 
717  /* Make sure that our disk state is up to date with our memory state
718  * before saving it to disk. */
720  state = config_dump(get_srs_mgr(), NULL, sr_disk_state, 0, 0);
721  format_local_iso_time(tbuf, now);
722  tor_asprintf(&content,
723  "# Tor shared random state file last generated on %s "
724  "local time\n"
725  "# Other times below are in UTC\n"
726  "# Please *do not* edit this file.\n\n%s",
727  tbuf, state);
728  tor_free(state);
729  fname = get_datadir_fname(default_fname);
730  if (write_str_to_file(fname, content, 0) < 0) {
731  log_warn(LD_FS, "SR: Unable to write SR state to file %s", fname);
732  ret = -1;
733  goto done;
734  }
735  ret = 0;
736  log_debug(LD_DIR, "SR: Saved state to file %s", fname);
737 
738  done:
739  tor_free(fname);
740  tor_free(content);
741  return ret;
742 }
743 
744 /** Reset our state to prepare for a new protocol run. Once this returns, all
745  * commits in the state will be removed and freed. */
746 STATIC void
748 {
749  if (BUG(!sr_state))
750  return;
751 
752  /* Keep counters in track */
756 
757  /* Reset valid-until */
759  sr_state->valid_after = valid_after;
760 
761  /* We are in a new protocol run so cleanup commits. */
763 }
764 
765 /** This is the first round of the new protocol run starting at
766  * <b>valid_after</b>. Do the necessary housekeeping. */
767 STATIC void
768 new_protocol_run(time_t valid_after)
769 {
770  sr_commit_t *our_commitment = NULL;
771 
772  /* Only compute the srv at the end of the reveal phase. */
773  if (sr_state->phase == SR_PHASE_REVEAL) {
774  /* We are about to compute a new shared random value that will be set in
775  * our state as the current value so rotate values. */
777  /* Compute the shared randomness value of the day. */
778  sr_compute_srv();
779  }
780 
781  /* Prepare for the new protocol run by reseting the state */
783 
784  /* Do some logging */
785  log_info(LD_DIR, "SR: Protocol run #%" PRIu64 " starting!",
787 
788  /* Generate fresh commitments for this protocol run */
789  our_commitment = sr_generate_our_commit(valid_after,
791  if (our_commitment) {
792  /* Add our commitment to our state. In case we are unable to create one
793  * (highly unlikely), we won't vote for this protocol run since our
794  * commitment won't be in our state. */
795  sr_state_add_commit(our_commitment);
796  }
797 }
798 
799 /** Return 1 iff the <b>next_phase</b> is a phase transition from the current
800  * phase that is it's different. */
801 STATIC int
803 {
804  return sr_state->phase != next_phase;
805 }
806 
807 /** Helper function: return a commit using the RSA fingerprint of the
808  * authority or NULL if no such commit is known. */
809 static sr_commit_t *
810 state_query_get_commit(const char *rsa_fpr)
811 {
812  tor_assert(rsa_fpr);
813  return digestmap_get(sr_state->commits, rsa_fpr);
814 }
815 
816 /** Helper function: This handles the GET state action using an
817  * <b>obj_type</b> and <b>data</b> needed for the action. */
818 static void *
819 state_query_get_(sr_state_object_t obj_type, const void *data)
820 {
821  if (BUG(!sr_state))
822  return NULL;
823 
824  void *obj = NULL;
825 
826  switch (obj_type) {
827  case SR_STATE_OBJ_COMMIT:
828  {
829  obj = state_query_get_commit(data);
830  break;
831  }
833  obj = sr_state->commits;
834  break;
835  case SR_STATE_OBJ_CURSRV:
836  obj = sr_state->current_srv;
837  break;
839  obj = sr_state->previous_srv;
840  break;
841  case SR_STATE_OBJ_PHASE:
842  obj = &sr_state->phase;
843  break;
845  default:
846  tor_assert(0);
847  }
848  return obj;
849 }
850 
851 /** Helper function: This handles the PUT state action using an
852  * <b>obj_type</b> and <b>data</b> needed for the action.
853  * PUT frees the previous data before replacing it, if needed. */
854 static void
855 state_query_put_(sr_state_object_t obj_type, void *data)
856 {
857  if (BUG(!sr_state))
858  return;
859 
860  switch (obj_type) {
861  case SR_STATE_OBJ_COMMIT:
862  {
863  sr_commit_t *commit = data;
864  tor_assert(commit);
865  /* commit_add_to_state() frees the old commit, if there is one */
866  commit_add_to_state(commit, sr_state);
867  break;
868  }
869  case SR_STATE_OBJ_CURSRV:
870  /* Check if the new pointer is the same as the old one: if it is, it's
871  * probably a bug. The caller may have confused current and previous,
872  * or they may have forgotten to sr_srv_dup().
873  * Putting NULL multiple times is allowed. */
874  if (!BUG(data && sr_state->current_srv == (sr_srv_t *) data)) {
875  /* We own the old SRV, so we need to free it. */
877  sr_state->current_srv = (sr_srv_t *) data;
878  }
879  break;
881  /* Check if the new pointer is the same as the old one: if it is, it's
882  * probably a bug. The caller may have confused current and previous,
883  * or they may have forgotten to sr_srv_dup().
884  * Putting NULL multiple times is allowed. */
885  if (!BUG(data && sr_state->previous_srv == (sr_srv_t *) data)) {
886  /* We own the old SRV, so we need to free it. */
888  sr_state->previous_srv = (sr_srv_t *) data;
889  }
890  break;
892  sr_state->valid_after = *((time_t *) data);
893  break;
894  /* It's not allowed to change the phase nor the full commitments map from
895  * the state. The phase is decided during a strict process post voting and
896  * the commits should be put individually. */
897  case SR_STATE_OBJ_PHASE:
899  default:
900  tor_assert(0);
901  }
902 }
903 
904 /** Helper function: This handles the DEL_ALL state action using an
905  * <b>obj_type</b> and <b>data</b> needed for the action. */
906 static void
908 {
909  if (BUG(!sr_state))
910  return;
911 
912  switch (obj_type) {
913  case SR_STATE_OBJ_COMMIT:
914  {
915  /* We are in a new protocol run so cleanup commitments. */
917  sr_commit_free(c);
918  MAP_DEL_CURRENT(key);
920  break;
921  }
922  /* The following objects are _NOT_ supposed to be removed. */
923  case SR_STATE_OBJ_CURSRV:
925  case SR_STATE_OBJ_PHASE:
928  default:
929  tor_assert(0);
930  }
931 }
932 
933 /** Helper function: This handles the DEL state action using an
934  * <b>obj_type</b> and <b>data</b> needed for the action. */
935 static void
936 state_query_del_(sr_state_object_t obj_type, void *data)
937 {
938  (void) data;
939 
940  if (BUG(!sr_state))
941  return;
942 
943  switch (obj_type) {
946  break;
947  case SR_STATE_OBJ_CURSRV:
949  break;
950  case SR_STATE_OBJ_COMMIT:
952  case SR_STATE_OBJ_PHASE:
954  default:
955  tor_assert(0);
956  }
957 }
958 
959 /** Query state using an <b>action</b> for an object type <b>obj_type</b>.
960  * The <b>data</b> pointer needs to point to an object that the action needs
961  * to use and if anything is required to be returned, it is stored in
962  * <b>out</b>.
963  *
964  * This mechanism exists so we have one single point where we synchronized
965  * our memory state with our disk state for every actions that changes it.
966  * We then trigger a write on disk immediately.
967  *
968  * This should be the only entry point to our memory state. It's used by all
969  * our state accessors and should be in the future. */
970 static void
972  void *data, void **out)
973 {
974  switch (action) {
975  case SR_STATE_ACTION_GET:
976  *out = state_query_get_(obj_type, data);
977  break;
978  case SR_STATE_ACTION_PUT:
979  state_query_put_(obj_type, data);
980  break;
981  case SR_STATE_ACTION_DEL:
982  state_query_del_(obj_type, data);
983  break;
984  case SR_STATE_ACTION_DEL_ALL:
985  state_query_del_all_(obj_type);
986  break;
987  case SR_STATE_ACTION_SAVE:
988  /* Only trigger a disk state save. */
989  break;
990  default:
991  tor_assert(0);
992  }
993 
994  /* If the action actually changes the state, immediately save it to disk.
995  * The following will sync the state -> disk state and then save it. */
996  if (action != SR_STATE_ACTION_GET) {
998  }
999 }
1000 
1001 /** Delete the current SRV value from the state freeing it and the value is set
1002  * to NULL meaning empty. */
1003 STATIC void
1005 {
1006  state_query(SR_STATE_ACTION_DEL, SR_STATE_OBJ_CURSRV, NULL, NULL);
1007 }
1008 
1009 /** Delete the previous SRV value from the state freeing it and the value is
1010  * set to NULL meaning empty. */
1011 STATIC void
1013 {
1014  state_query(SR_STATE_ACTION_DEL, SR_STATE_OBJ_PREVSRV, NULL, NULL);
1015 }
1016 
1017 /** Rotate SRV value by setting the previous SRV to the current SRV, and
1018  * clearing the current SRV. */
1019 STATIC void
1021 {
1022  /* First delete previous SRV from the state. Object will be freed. */
1024  /* Set previous SRV to a copy of the current one. */
1026  /* Free and NULL the current srv. */
1028 }
1029 
1030 /** Set valid after time in the our state. */
1031 void
1032 sr_state_set_valid_after(time_t valid_after)
1033 {
1034  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_VALID_AFTER,
1035  (void *) &valid_after, NULL);
1036 }
1037 
1038 /** Return the phase we are currently in according to our state. */
1039 sr_phase_t
1041 {
1042  void *ptr;
1043  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_PHASE, NULL, &ptr);
1044  return *(sr_phase_t *) ptr;
1045 }
1046 
1047 /** Return the previous SRV value from our state. Value CAN be NULL.
1048  * The state object owns the SRV, so the calling code should not free the SRV.
1049  * Use sr_srv_dup() if you want to keep a copy of the SRV. */
1050 const sr_srv_t *
1052 {
1053  const sr_srv_t *srv;
1054  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_PREVSRV, NULL,
1055  (void *) &srv);
1056  return srv;
1057 }
1058 
1059 /** Set the current SRV value from our state. Value CAN be NULL. The srv
1060  * object ownership is transferred to the state object. */
1061 void
1063 {
1064  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_PREVSRV, (void *) srv,
1065  NULL);
1066 }
1067 
1068 /** Return the current SRV value from our state. Value CAN be NULL.
1069  * The state object owns the SRV, so the calling code should not free the SRV.
1070  * Use sr_srv_dup() if you want to keep a copy of the SRV. */
1071 const sr_srv_t *
1073 {
1074  const sr_srv_t *srv;
1075  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_CURSRV, NULL,
1076  (void *) &srv);
1077  return srv;
1078 }
1079 
1080 /** Set the current SRV value from our state. Value CAN be NULL. The srv
1081  * object ownership is transferred to the state object. */
1082 void
1084 {
1085  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_CURSRV, (void *) srv,
1086  NULL);
1087 }
1088 
1089 /** Clean all the SRVs in our state. */
1090 void
1092 {
1093  /* Remove SRVs from state. They will be set to NULL as "empty". */
1096 }
1097 
1098 /** Return a pointer to the commits map from our state. CANNOT be NULL. */
1099 digestmap_t *
1101 {
1102  digestmap_t *commits;
1103  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_COMMITS,
1104  NULL, (void *) &commits);
1105  tor_assert(commits);
1106  return commits;
1107 }
1108 
1109 /** Update the current SR state as needed for the upcoming voting round at
1110  * <b>valid_after</b>. */
1111 void
1112 sr_state_update(time_t valid_after)
1113 {
1114  sr_phase_t next_phase;
1115 
1116  if (BUG(!sr_state))
1117  return;
1118 
1119  /* Don't call this function twice in the same voting period. */
1120  if (valid_after <= sr_state->valid_after) {
1121  log_info(LD_DIR, "SR: Asked to update state twice. Ignoring.");
1122  return;
1123  }
1124 
1125  /* Get phase of upcoming round. */
1126  next_phase = get_sr_protocol_phase(valid_after);
1127 
1128  /* If we are transitioning to a new protocol phase, prepare the stage. */
1129  if (is_phase_transition(next_phase)) {
1130  if (next_phase == SR_PHASE_COMMIT) {
1131  /* Going into commit phase means we are starting a new protocol run. */
1132  new_protocol_run(valid_after);
1133  }
1134  /* Set the new phase for this round */
1135  sr_state->phase = next_phase;
1136  } else if (sr_state->phase == SR_PHASE_COMMIT &&
1137  digestmap_size(sr_state->commits) == 0) {
1138  /* We are _NOT_ in a transition phase so if we are in the commit phase
1139  * and have no commit, generate one. Chances are that we are booting up
1140  * so let's have a commit in our state for the next voting period. */
1141  sr_commit_t *our_commit =
1143  if (our_commit) {
1144  /* Add our commitment to our state. In case we are unable to create one
1145  * (highly unlikely), we won't vote for this protocol run since our
1146  * commitment won't be in our state. */
1147  sr_state_add_commit(our_commit);
1148  }
1149  }
1150 
1151  sr_state_set_valid_after(valid_after);
1152 
1153  /* Count the current round */
1154  if (sr_state->phase == SR_PHASE_COMMIT) {
1155  /* invariant check: we've not entered reveal phase yet */
1156  if (BUG(sr_state->n_reveal_rounds != 0))
1157  return;
1159  } else {
1161  }
1162 
1163  { /* Debugging. */
1164  char tbuf[ISO_TIME_LEN + 1];
1165  format_iso_time(tbuf, valid_after);
1166  log_info(LD_DIR, "SR: State prepared for upcoming voting period (%s). "
1167  "Upcoming phase is %s (counters: %d commit & %d reveal rounds).",
1168  tbuf, get_phase_str(sr_state->phase),
1170  }
1171 }
1172 
1173 /** Return commit object from the given authority digest <b>rsa_identity</b>.
1174  * Return NULL if not found. */
1175 sr_commit_t *
1176 sr_state_get_commit(const char *rsa_identity)
1177 {
1178  sr_commit_t *commit;
1179 
1180  tor_assert(rsa_identity);
1181 
1182  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_COMMIT,
1183  (void *) rsa_identity, (void *) &commit);
1184  return commit;
1185 }
1186 
1187 /** Add <b>commit</b> to the permanent state. The commit object ownership is
1188  * transferred to the state so the caller MUST not free it. */
1189 void
1191 {
1192  tor_assert(commit);
1193 
1194  /* Put the commit to the global state. */
1195  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_COMMIT,
1196  (void *) commit, NULL);
1197 
1198  log_debug(LD_DIR, "SR: Commit from %s has been added to our state.",
1199  sr_commit_get_rsa_fpr(commit));
1200 }
1201 
1202 /** Remove all commits from our state. */
1203 void
1205 {
1206  state_query(SR_STATE_ACTION_DEL_ALL, SR_STATE_OBJ_COMMIT, NULL, NULL);
1207 }
1208 
1209 /** Copy the reveal information from <b>commit</b> into <b>saved_commit</b>.
1210  * This <b>saved_commit</b> MUST come from our current SR state. Once modified,
1211  * the disk state is updated. */
1212 void
1214 {
1215  tor_assert(saved_commit);
1216  tor_assert(commit);
1217 
1218  saved_commit->reveal_ts = commit->reveal_ts;
1219  memcpy(saved_commit->random_number, commit->random_number,
1220  sizeof(saved_commit->random_number));
1221 
1222  strlcpy(saved_commit->encoded_reveal, commit->encoded_reveal,
1223  sizeof(saved_commit->encoded_reveal));
1224  state_query(SR_STATE_ACTION_SAVE, 0, NULL, NULL);
1225  log_debug(LD_DIR, "SR: Reveal value learned %s (for commit %s) from %s",
1226  saved_commit->encoded_reveal, saved_commit->encoded_commit,
1227  sr_commit_get_rsa_fpr(saved_commit));
1228 }
1229 
1230 /** Set the fresh SRV flag from our state. This doesn't need to trigger a
1231  * disk state synchronization so we directly change the state. */
1232 void
1234 {
1235  sr_state->is_srv_fresh = 1;
1236 }
1237 
1238 /** Unset the fresh SRV flag from our state. This doesn't need to trigger a
1239  * disk state synchronization so we directly change the state. */
1240 void
1242 {
1243  sr_state->is_srv_fresh = 0;
1244 }
1245 
1246 /** Return the value of the fresh SRV flag. */
1247 unsigned int
1249 {
1250  return sr_state->is_srv_fresh;
1251 }
1252 
1253 /** Cleanup and free our disk and memory state. */
1254 void
1256 {
1257  state_free(sr_state);
1258  disk_state_free(sr_disk_state);
1259  /* Nullify our global state. */
1260  sr_state = NULL;
1261  sr_disk_state = NULL;
1262  config_mgr_free(shared_random_state_mgr);
1263 }
1264 
1265 /** Save our current state in memory to disk. */
1266 void
1268 {
1269  /* Query a SAVE action on our current state so it's synced and saved. */
1270  state_query(SR_STATE_ACTION_SAVE, 0, NULL, NULL);
1271 }
1272 
1273 /** Return 1 iff the state has been initialized that is it exists in memory.
1274  * Return 0 otherwise. */
1275 int
1277 {
1278  return sr_state == NULL ? 0 : 1;
1279 }
1280 
1281 /** Initialize the disk and memory state.
1282  *
1283  * If save_to_disk is set to 1, the state is immediately saved to disk after
1284  * creation else it's not thus only kept in memory.
1285  * If read_from_disk is set to 1, we try to load the state from the disk and
1286  * if not found, a new state is created.
1287  *
1288  * Return 0 on success else a negative value on error. */
1289 int
1290 sr_state_init(int save_to_disk, int read_from_disk)
1291 {
1292  int ret = -ENOENT;
1293  time_t now = time(NULL);
1294 
1295  /* We shouldn't have those assigned. */
1296  tor_assert(sr_disk_state == NULL);
1297  tor_assert(sr_state == NULL);
1298 
1299  /* First, try to load the state from disk. */
1300  if (read_from_disk) {
1301  ret = disk_state_load_from_disk();
1302  }
1303 
1304  if (ret < 0) {
1305  switch (-ret) {
1306  case EINVAL:
1307  /* We have a state on disk but it contains something we couldn't parse
1308  * or an invalid entry in the state file. Let's remove it since it's
1309  * obviously unusable and replace it by an new fresh state below. */
1310  case ENOENT:
1311  {
1312  /* No state on disk so allocate our states for the first time. */
1313  sr_state_t *new_state = state_new(default_fname, now);
1314  sr_disk_state_t *new_disk_state = disk_state_new(now);
1315  state_set(new_state);
1316  /* It's important to set our disk state pointer since the save call
1317  * below uses it to synchronized it with our memory state. */
1318  disk_state_set(new_disk_state);
1319  /* No entry, let's save our new state to disk. */
1320  if (save_to_disk && disk_state_save_to_disk() < 0) {
1321  goto error;
1322  }
1323  break;
1324  }
1325  default:
1326  /* Big problem. Not possible. */
1327  tor_assert(0);
1328  }
1329  }
1330  /* We have a state in memory, let's make sure it's updated for the current
1331  * and next voting round. */
1332  {
1333  time_t valid_after = voting_schedule_get_next_valid_after_time();
1334  sr_state_update(valid_after);
1335  }
1336  return 0;
1337 
1338  error:
1339  return -1;
1340 }
1341 
1342 #ifdef TOR_UNIT_TESTS
1343 
1344 /** Set the current phase of the protocol. Used only by unit tests. */
1345 void
1346 set_sr_phase(sr_phase_t phase)
1347 {
1348  if (BUG(!sr_state))
1349  return;
1350  sr_state->phase = phase;
1351 }
1352 
1353 /** Get the SR state. Used only by unit tests */
1354 sr_state_t *
1355 get_sr_state(void)
1356 {
1357  return sr_state;
1358 }
1359 
1360 #endif /* defined(TOR_UNIT_TESTS) */
static config_mgr_t * shared_random_state_mgr
static int disk_state_parse_sr_values(sr_state_t *state, const sr_disk_state_t *disk_state)
void sr_state_delete_commits(void)
static int disk_state_load_from_disk(void)
STATIC int disk_state_load_from_disk_impl(const char *fname)
void * config_new(const config_mgr_t *mgr)
Definition: confmgt.c:387
STATIC void state_del_previous_srv(void)
static void disk_state_free_(sr_disk_state_t *state)
int get_voting_interval(void)
const sr_srv_t * sr_state_get_current_srv(void)
Header for confline.c.
sr_phase_t phase
void sr_state_set_valid_after(time_t valid_after)
STATIC void state_rotate_srv(void)
char encoded_reveal[SR_REVEAL_BASE64_LEN+1]
Header for shared_random_state.c.
static void state_query_del_all_(sr_state_object_t obj_type)
static void commit_free_(void *p)
static sr_disk_state_t * sr_disk_state
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
uint64_t num_reveals
Definition: shared_random.h:64
char rsa_identity[DIGEST_LEN]
Definition: shared_random.h:80
time_t get_start_time_of_current_round(void)
void sr_state_update(time_t valid_after)
static sr_state_t * state_new(const char *fname, time_t now)
static sr_state_t * disk_state_parse(const sr_disk_state_t *new_disk_state)
#define SR_PROTO_VERSION
Definition: shared_random.h:18
static void state_set(sr_state_t *state)
unsigned int valid
Definition: shared_random.h:74
sr_commit_t * sr_state_get_commit(const char *rsa_identity)
static sr_state_t * sr_state
digestmap_t * commits
sr_phase_t sr_state_get_phase(void)
void sr_compute_srv(void)
static const config_format_t state_format
int sr_state_init(int save_to_disk, int read_from_disk)
Header file for config.c.
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
#define tor_assert(expr)
Definition: util_bug.h:102
sr_state_object_t
STATIC void reset_state_for_new_protocol_run(time_t valid_after)
#define END_OF_CONFIG_VARS
Definition: confmacros.h:22
static sr_commit_t * state_query_get_commit(const char *rsa_fpr)
#define SR_DISK_STATE_MAGIC
STATIC void state_del_current_srv(void)
unsigned int sr_state_srv_is_fresh(void)
#define MAP_DEL_CURRENT(keyvar)
Definition: map.h:140
#define tor_free(p)
Definition: malloc.h:52
#define tor_fragile_assert()
Definition: util_bug.h:246
struct config_line_t * SharedRandValues
#define SR_SRV_VALUE_BASE64_LEN
Definition: shared_random.h:48
void sr_state_add_commit(sr_commit_t *commit)
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:55
smartlist_t * smartlist_new(void)
const sr_srv_t * sr_state_get_previous_srv(void)
DUMMY_TYPECHECK_INSTANCE(sr_disk_state_t)
sr_srv_t * sr_parse_srv(const smartlist_t *args)
int config_get_lines(const char *string, config_line_t **result, int extended)
Definition: confline.c:201
#define STATIC
Definition: testsupport.h:32
int write_str_to_file(const char *fname, const char *str, int bin)
Definition: files.c:258
void sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv)
static int disk_state_parse_commits(sr_state_t *state, const sr_disk_state_t *disk_state)
uint64_t n_protocol_runs
sr_phase_t
Definition: shared_random.h:54
config_mgr_t * config_mgr_new(const config_format_t *toplevel_fmt)
Definition: confmgt.c:145
static int disk_state_save_to_disk(void)
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar)
Definition: map.h:154
STATIC time_t get_state_valid_until_time(time_t now)
Common functions for cryptographic routines.
sr_state_action_t
uint64_t reveal_ts
Definition: shared_random.h:86
void sr_commit_free_(sr_commit_t *commit)
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
int sr_state_is_initialized(void)
void sr_state_free_all(void)
struct config_line_t * Commit
uint8_t value[DIGEST256_LEN]
Definition: shared_random.h:66
uint8_t random_number[SR_RANDOM_NUMBER_LEN]
Definition: shared_random.h:97
Header for version.c.
static void disk_state_update(void)
char * config_dump(const config_mgr_t *mgr, const void *default_options, const void *options, int minimal, int comment_defaults)
Definition: confmgt.c:1314
const char * name
Definition: conftypes.h:98
Master header file for Tor-specific functionality.
STATIC void new_protocol_run(time_t valid_after)
static void state_free_(sr_state_t *state)
STATIC sr_phase_t get_sr_protocol_phase(time_t valid_after)
Header file for shared_random_client.c.
int config_assign(const config_mgr_t *mgr, void *options, config_line_t *list, unsigned config_assign_flags, char **msg)
Definition: confmgt.c:937
static const char default_fname[]
static void state_query_del_(sr_state_object_t obj_type, void *data)
static const config_mgr_t * get_srs_mgr(void)
void sr_state_save(void)
Header file for voting_schedule.c.
sr_srv_t * current_srv
static sr_disk_state_t * disk_state_new(time_t now)
static void disk_state_put_srv_line(const sr_srv_t *srv, config_line_t *line)
static void * state_query_get_(sr_state_object_t obj_type, const void *data)
#define DIGESTMAP_FOREACH_END
Definition: map.h:168
#define LD_FS
Definition: log.h:70
static void commit_add_to_state(sr_commit_t *commit, sr_state_t *state)
#define LD_DIR
Definition: log.h:88
unsigned int n_commit_rounds
digest_algorithm_t alg
Definition: shared_random.h:72
const char * crypto_digest_algorithm_get_name(digest_algorithm_t alg)
Definition: crypto_digest.c:44
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
sr_srv_t * previous_srv
authority_cert_t * get_my_v3_authority_cert(void)
Definition: router.c:430
static void state_query_put_(sr_state_object_t obj_type, void *data)
Header file for dirvote.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
The or_state_t structure, which represents Tor's state file.
Header file for router.c.
const char * escaped(const char *s)
Definition: escape.c:126
STATIC int is_phase_transition(sr_phase_t next_phase)
STATIC const char * get_phase_str(sr_phase_t phase)
static void disk_state_reset(void)
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar)
Definition: map.h:165
sr_srv_t * sr_srv_dup(const sr_srv_t *orig)
unsigned int n_reveal_rounds
sr_commit_t * sr_parse_commit(const smartlist_t *args)
static int disk_state_parse_srv(const char *value, sr_srv_t *dst)
Header for confmgt.c.
void sr_state_copy_reveal_info(sr_commit_t *saved_commit, const sr_commit_t *commit)
static const struct_member_t state_extra_var
void sr_state_set_current_srv(const sr_srv_t *srv)
void sr_state_set_fresh_srv(void)
void sr_state_clean_srvs(void)
void config_mgr_freeze(config_mgr_t *mgr)
Definition: confmgt.c:285
#define VAR(varname, conftype, member, initvalue)
Definition: config.c:259
void sr_state_set_previous_srv(const sr_srv_t *srv)
static const char * phase_str[]
const char * get_version(void)
Definition: version.c:38
static void disk_state_set(sr_disk_state_t *state)
static int disk_state_validate(const sr_disk_state_t *state)
static void state_query(sr_state_action_t action, sr_state_object_t obj_type, void *data, void **out)
This file contains ABI/API of the shared random protocol defined in proposal #250....
sr_commit_t * sr_generate_our_commit(time_t timestamp, const authority_cert_t *my_rsa_cert)
void config_init(const config_mgr_t *mgr, void *options)
Definition: confmgt.c:1158
void sr_state_unset_fresh_srv(void)
struct config_line_t * ExtraLines
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:86
static const config_var_t state_vars[]
digestmap_t * sr_state_get_commits(void)
static void disk_state_put_commit_line(const sr_commit_t *commit, config_line_t *line)
unsigned int is_srv_fresh
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)