Tor
0.4.7.0-alpha-dev
|
Functions and data structures for the state of the random protocol as defined in proposal #250. More...
#include "core/or/or.h"
#include "app/config/config.h"
#include "lib/confmgt/confmgt.h"
#include "lib/crypt_ops/crypto_util.h"
#include "feature/dirauth/dirvote.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/relay/router.h"
#include "feature/dirauth/shared_random.h"
#include "feature/hs_common/shared_random_client.h"
#include "feature/dirauth/shared_random_state.h"
#include "feature/dirauth/voting_schedule.h"
#include "lib/encoding/confline.h"
#include "lib/version/torversion.h"
#include "app/config/or_state_st.h"
Go to the source code of this file.
Macros | |
#define | SHARED_RANDOM_STATE_PRIVATE |
#define | VAR(varname, conftype, member, initvalue) CONFIG_VAR_ETYPE(sr_disk_state_t, varname, conftype, member, 0, initvalue) |
#define | V(member, conftype, initvalue) VAR(#member, conftype, member, initvalue) |
#define | SR_DISK_STATE_MAGIC 0x98AB1254 |
#define | state_free(val) FREE_AND_NULL(sr_state_t, state_free_, (val)) |
#define | disk_state_free(val) FREE_AND_NULL(sr_disk_state_t, disk_state_free_, (val)) |
Variables | |
static const char | default_fname [] = "sr-state" |
static const char * | phase_str [] = { "unknown", "commit", "reveal" } |
static sr_state_t * | sr_state = NULL |
static sr_disk_state_t * | sr_disk_state = NULL |
static const char | dstate_commit_key [] = "Commit" |
static const char | dstate_prev_srv_key [] = "SharedRandPreviousValue" |
static const char | dstate_cur_srv_key [] = "SharedRandCurrentValue" |
static const config_var_t | state_vars [] |
static const struct_member_t | state_extra_var |
static const config_format_t | state_format |
static config_mgr_t * | shared_random_state_mgr = NULL |
Functions and data structures for the state of the random protocol as defined in proposal #250.
Definition in file shared_random_state.c.
#define SR_DISK_STATE_MAGIC 0x98AB1254 |
Our persistent state magic number.
Definition at line 60 of file shared_random_state.c.
|
static |
Add the given commit to state. It MUST be a valid commit and there shouldn't be a commit from the same authority in the state already else verification hasn't been done prior. This takes ownership of the commit once in our state.
Definition at line 189 of file shared_random_state.c.
|
static |
Helper: deallocate a commit object. (Used with digestmap_free(), which requires a function pointer whose argument is void *).
Definition at line 212 of file shared_random_state.c.
|
static |
Free an allocated disk state.
Definition at line 269 of file shared_random_state.c.
|
static |
Load state from disk and put it into our disk state. If the state passes validation, our global state will be updated with it. Return 0 on success. On error, -EINVAL is returned if the state on disk did contained something malformed or is unreadable. -ENOENT is returned indicating that the state file is either empty of non existing.
Definition at line 628 of file shared_random_state.c.
Referenced by sr_state_init().
STATIC int disk_state_load_from_disk_impl | ( | const char * | fname | ) |
Helper for disk_state_load_from_disk().
Definition at line 642 of file shared_random_state.c.
|
static |
Allocate a new disk state, initialize it and return it.
Definition at line 279 of file shared_random_state.c.
Referenced by sr_state_init().
|
static |
Parse the given disk state and set a newly allocated state. On success, return that state else NULL.
Definition at line 483 of file shared_random_state.c.
|
static |
Parse the Commit line(s) in the disk state and translate them to the the memory state. Return 0 on success else -1 on error.
Definition at line 342 of file shared_random_state.c.
Referenced by disk_state_parse().
|
static |
Parse both SharedRandCurrentValue and SharedRandPreviousValue line from the state. Return 0 on success else -1.
Definition at line 432 of file shared_random_state.c.
Referenced by disk_state_parse().
|
static |
Parse a share random value line from the disk state and save it to dst which is an allocated srv object. Return 0 on success else -1.
Definition at line 397 of file shared_random_state.c.
|
static |
From a valid commit object and an allocated config line, set the line's value to the state string representation of a commit.
Definition at line 517 of file shared_random_state.c.
|
static |
From a valid srv object and an allocated config line, set the line's value to the state string representation of a shared random value.
Definition at line 544 of file shared_random_state.c.
|
static |
Reset disk state that is free allocated memory and zeroed the object.
Definition at line 561 of file shared_random_state.c.
|
static |
Save the disk state to disk but before that update it from the current state so we always have the latest. Return 0 on success else -1.
Definition at line 705 of file shared_random_state.c.
Referenced by sr_state_init().
|
static |
Set our global disk state with the given state.
Definition at line 295 of file shared_random_state.c.
Referenced by sr_state_init().
|
static |
Update our disk state based on our global SR state.
Definition at line 580 of file shared_random_state.c.
|
static |
Return -1 if the disk state is invalid (something in there that we can't or shouldn't use). Return 0 if everything checks out.
Definition at line 307 of file shared_random_state.c.
DUMMY_TYPECHECK_INSTANCE | ( | sr_disk_state_t | ) |
dummy instance of sr_disk_state_t, used for type-checking its members with CONF_CHECK_VAR_TYPE.
STATIC const char* get_phase_str | ( | sr_phase_t | phase | ) |
Return a string representation of a protocol phase.
Definition at line 117 of file shared_random_state.c.
STATIC sr_phase_t get_sr_protocol_phase | ( | time_t | valid_after | ) |
Given the consensus 'valid-after' time, return the protocol phase we should be in.
Definition at line 167 of file shared_random_state.c.
Referenced by disk_state_parse().
|
static |
Return the configuration manager for the shared-random state file.
Definition at line 104 of file shared_random_state.c.
Referenced by disk_state_new().
STATIC time_t get_state_valid_until_time | ( | time_t | now | ) |
Return the time we should expire the state file created at now. We expire the state file in the beginning of the next protocol run.
Definition at line 136 of file shared_random_state.c.
STATIC int is_phase_transition | ( | sr_phase_t | next_phase | ) |
Return 1 iff the next_phase is a phase transition from the current phase that is it's different.
Definition at line 804 of file shared_random_state.c.
STATIC void new_protocol_run | ( | time_t | valid_after | ) |
This is the first round of the new protocol run starting at valid_after. Do the necessary housekeeping.
Definition at line 770 of file shared_random_state.c.
STATIC void reset_state_for_new_protocol_run | ( | time_t | valid_after | ) |
Reset our state to prepare for a new protocol run. Once this returns, all commits in the state will be removed and freed.
Definition at line 749 of file shared_random_state.c.
Referenced by new_protocol_run().
void sr_state_add_commit | ( | sr_commit_t * | commit | ) |
Add commit to the permanent state. The commit object ownership is transferred to the state so the caller MUST not free it.
Definition at line 1193 of file shared_random_state.c.
Referenced by save_commit_to_state().
void sr_state_clean_srvs | ( | void | ) |
Clean all the SRVs in our state.
Definition at line 1094 of file shared_random_state.c.
void sr_state_copy_reveal_info | ( | sr_commit_t * | saved_commit, |
const sr_commit_t * | commit | ||
) |
Copy the reveal information from commit into saved_commit. This saved_commit MUST come from our current SR state. Once modified, the disk state is updated.
Definition at line 1216 of file shared_random_state.c.
Referenced by save_commit_during_reveal_phase().
void sr_state_delete_commits | ( | void | ) |
Remove all commits from our state.
Definition at line 1207 of file shared_random_state.c.
void sr_state_free_all | ( | void | ) |
Cleanup and free our disk and memory state.
Definition at line 1258 of file shared_random_state.c.
Referenced by sr_cleanup().
sr_commit_t* sr_state_get_commit | ( | const char * | rsa_identity | ) |
Return commit object from the given authority digest rsa_identity. Return NULL if not found.
Definition at line 1179 of file shared_random_state.c.
Referenced by save_commit_during_reveal_phase().
digestmap_t* sr_state_get_commits | ( | void | ) |
Return a pointer to the commits map from our state. CANNOT be NULL.
Definition at line 1103 of file shared_random_state.c.
const sr_srv_t* sr_state_get_current_srv | ( | void | ) |
Return the current SRV value from our state. Value CAN be NULL. The state object owns the SRV, so the calling code should not free the SRV. Use sr_srv_dup() if you want to keep a copy of the SRV.
Definition at line 1075 of file shared_random_state.c.
Referenced by state_rotate_srv().
sr_phase_t sr_state_get_phase | ( | void | ) |
Return the phase we are currently in according to our state.
Definition at line 1042 of file shared_random_state.c.
Referenced by save_commit_to_state(), and sr_handle_received_commits().
const sr_srv_t* sr_state_get_previous_srv | ( | void | ) |
Return the previous SRV value from our state. Value CAN be NULL. The state object owns the SRV, so the calling code should not free the SRV. Use sr_srv_dup() if you want to keep a copy of the SRV.
Definition at line 1054 of file shared_random_state.c.
int sr_state_init | ( | int | save_to_disk, |
int | read_from_disk | ||
) |
Initialize the disk and memory state.
If save_to_disk is set to 1, the state is immediately saved to disk after creation else it's not thus only kept in memory. If read_from_disk is set to 1, we try to load the state from the disk and if not found, a new state is created.
Return 0 on success else a negative value on error.
Definition at line 1293 of file shared_random_state.c.
Referenced by sr_init().
int sr_state_is_initialized | ( | void | ) |
Return 1 iff the state has been initialized that is it exists in memory. Return 0 otherwise.
Definition at line 1279 of file shared_random_state.c.
Referenced by sr_act_post_consensus().
void sr_state_save | ( | void | ) |
Save our current state in memory to disk.
Definition at line 1270 of file shared_random_state.c.
Referenced by sr_save_and_cleanup().
void sr_state_set_current_srv | ( | const sr_srv_t * | srv | ) |
Set the current SRV value from our state. Value CAN be NULL. The srv object ownership is transferred to the state object.
Definition at line 1086 of file shared_random_state.c.
Referenced by state_rotate_srv().
void sr_state_set_fresh_srv | ( | void | ) |
Set the fresh SRV flag from our state. This doesn't need to trigger a disk state synchronization so we directly change the state.
Definition at line 1236 of file shared_random_state.c.
void sr_state_set_previous_srv | ( | const sr_srv_t * | srv | ) |
Set the current SRV value from our state. Value CAN be NULL. The srv object ownership is transferred to the state object.
Definition at line 1065 of file shared_random_state.c.
Referenced by state_rotate_srv().
void sr_state_set_valid_after | ( | time_t | valid_after | ) |
Set valid after time in the our state.
Definition at line 1034 of file shared_random_state.c.
unsigned int sr_state_srv_is_fresh | ( | void | ) |
Return the value of the fresh SRV flag.
Definition at line 1251 of file shared_random_state.c.
void sr_state_unset_fresh_srv | ( | void | ) |
Unset the fresh SRV flag from our state. This doesn't need to trigger a disk state synchronization so we directly change the state.
Definition at line 1244 of file shared_random_state.c.
void sr_state_update | ( | time_t | valid_after | ) |
Update the current SR state as needed for the upcoming voting round at valid_after.
Definition at line 1115 of file shared_random_state.c.
Referenced by sr_state_init().
STATIC void state_del_current_srv | ( | void | ) |
Delete the current SRV value from the state freeing it and the value is set to NULL meaning empty.
Definition at line 1006 of file shared_random_state.c.
Referenced by sr_state_clean_srvs().
STATIC void state_del_previous_srv | ( | void | ) |
Delete the previous SRV value from the state freeing it and the value is set to NULL meaning empty.
Definition at line 1014 of file shared_random_state.c.
Referenced by sr_state_clean_srvs(), and state_rotate_srv().
|
static |
Free a state that was allocated with state_new().
Definition at line 222 of file shared_random_state.c.
|
static |
Allocate an sr_state_t object and returns it. If no fname, the default file name is used. This function does NOT initialize the state timestamp, phase or shared random value. NULL is never returned.
Definition at line 238 of file shared_random_state.c.
Referenced by disk_state_parse(), and sr_state_init().
|
static |
Query state using an action for an object type obj_type. The data pointer needs to point to an object that the action needs to use and if anything is required to be returned, it is stored in out.
This mechanism exists so we have one single point where we synchronized our memory state with our disk state for every actions that changes it. We then trigger a write on disk immediately.
This should be the only entry point to our memory state. It's used by all our state accessors and should be in the future.
Definition at line 973 of file shared_random_state.c.
Referenced by sr_state_add_commit(), sr_state_delete_commits(), sr_state_get_commit(), sr_state_get_commits(), sr_state_get_current_srv(), sr_state_get_phase(), sr_state_get_previous_srv(), sr_state_save(), sr_state_set_current_srv(), sr_state_set_previous_srv(), sr_state_set_valid_after(), state_del_current_srv(), and state_del_previous_srv().
|
static |
Helper function: This handles the DEL state action using an obj_type and data needed for the action.
Definition at line 938 of file shared_random_state.c.
|
static |
Helper function: This handles the DEL_ALL state action using an obj_type and data needed for the action.
Definition at line 909 of file shared_random_state.c.
|
static |
Helper function: This handles the GET state action using an obj_type and data needed for the action.
Definition at line 821 of file shared_random_state.c.
|
static |
Helper function: return a commit using the RSA fingerprint of the authority or NULL if no such commit is known.
Definition at line 812 of file shared_random_state.c.
|
static |
Helper function: This handles the PUT state action using an obj_type and data needed for the action. PUT frees the previous data before replacing it, if needed.
Definition at line 857 of file shared_random_state.c.
STATIC void state_rotate_srv | ( | void | ) |
Rotate SRV value by setting the previous SRV to the current SRV, and clearing the current SRV.
Definition at line 1022 of file shared_random_state.c.
Referenced by new_protocol_run().
|
static |
Set our global state pointer with the one given.
Definition at line 255 of file shared_random_state.c.
Referenced by sr_state_init().
|
static |
Default filename of the shared random state on disk.
Definition at line 30 of file shared_random_state.c.
Referenced by config_load_geoip_file_(), disk_state_parse(), and sr_state_init().
|
static |
String representation of a protocol phase.
Definition at line 33 of file shared_random_state.c.
Referenced by get_phase_str().
|
static |
Global configuration manager for the shared-random state file
Definition at line 100 of file shared_random_state.c.
|
static |
Representation of our persistent state on disk. The sr_state above contains the data parsed from this state. When we save to disk, we translate the sr_state to this sr_disk_state.
Definition at line 43 of file shared_random_state.c.
Referenced by disk_state_set(), and sr_state_init().
|
static |
Our shared random protocol state. There is only one possible state per protocol run so this is the global state which is reset at every run once the shared random value has been computed.
Definition at line 38 of file shared_random_state.c.
Referenced by is_phase_transition(), new_protocol_run(), sr_state_init(), sr_state_is_initialized(), sr_state_set_fresh_srv(), sr_state_srv_is_fresh(), sr_state_unset_fresh_srv(), state_query_get_commit(), and state_set().
|
static |
"Extra" variable in the state that receives lines we can't parse. This lets us preserve options from versions of Tor newer than us.
Definition at line 81 of file shared_random_state.c.
|
static |
Configuration format of sr_disk_state_t.
Definition at line 88 of file shared_random_state.c.
|
static |
Array of variables that are saved to disk as a persistent state.
Definition at line 64 of file shared_random_state.c.