Tor  0.4.7.0-alpha-dev
Macros | Functions | Variables
shared_random_state.c File Reference

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))
 

Functions

 DUMMY_TYPECHECK_INSTANCE (sr_disk_state_t)
 
static const config_mgr_tget_srs_mgr (void)
 
static void state_query_del_ (sr_state_object_t obj_type, void *data)
 
STATIC const char * get_phase_str (sr_phase_t phase)
 
STATIC time_t get_state_valid_until_time (time_t now)
 
STATIC sr_phase_t get_sr_protocol_phase (time_t valid_after)
 
static void commit_add_to_state (sr_commit_t *commit, sr_state_t *state)
 
static void commit_free_ (void *p)
 
static void state_free_ (sr_state_t *state)
 
static sr_state_tstate_new (const char *fname, time_t now)
 
static void state_set (sr_state_t *state)
 
static void disk_state_free_ (sr_disk_state_t *state)
 
static sr_disk_state_tdisk_state_new (time_t now)
 
static void disk_state_set (sr_disk_state_t *state)
 
static int disk_state_validate (const sr_disk_state_t *state)
 
static int disk_state_parse_commits (sr_state_t *state, const sr_disk_state_t *disk_state)
 
static int disk_state_parse_srv (const char *value, sr_srv_t *dst)
 
static int disk_state_parse_sr_values (sr_state_t *state, const sr_disk_state_t *disk_state)
 
static sr_state_tdisk_state_parse (const sr_disk_state_t *new_disk_state)
 
static void disk_state_put_commit_line (const sr_commit_t *commit, config_line_t *line)
 
static void disk_state_put_srv_line (const sr_srv_t *srv, config_line_t *line)
 
static void disk_state_reset (void)
 
static void disk_state_update (void)
 
static int disk_state_load_from_disk (void)
 
STATIC int disk_state_load_from_disk_impl (const char *fname)
 
static int disk_state_save_to_disk (void)
 
STATIC void reset_state_for_new_protocol_run (time_t valid_after)
 
STATIC void new_protocol_run (time_t valid_after)
 
STATIC int is_phase_transition (sr_phase_t next_phase)
 
static sr_commit_tstate_query_get_commit (const char *rsa_fpr)
 
static void * state_query_get_ (sr_state_object_t obj_type, const void *data)
 
static void state_query_put_ (sr_state_object_t obj_type, void *data)
 
static void state_query_del_all_ (sr_state_object_t obj_type)
 
static void state_query (sr_state_action_t action, sr_state_object_t obj_type, void *data, void **out)
 
STATIC void state_del_current_srv (void)
 
STATIC void state_del_previous_srv (void)
 
STATIC void state_rotate_srv (void)
 
void sr_state_set_valid_after (time_t valid_after)
 
sr_phase_t sr_state_get_phase (void)
 
const sr_srv_tsr_state_get_previous_srv (void)
 
void sr_state_set_previous_srv (const sr_srv_t *srv)
 
const sr_srv_tsr_state_get_current_srv (void)
 
void sr_state_set_current_srv (const sr_srv_t *srv)
 
void sr_state_clean_srvs (void)
 
digestmap_t * sr_state_get_commits (void)
 
void sr_state_update (time_t valid_after)
 
sr_commit_tsr_state_get_commit (const char *rsa_identity)
 
void sr_state_add_commit (sr_commit_t *commit)
 
void sr_state_delete_commits (void)
 
void sr_state_copy_reveal_info (sr_commit_t *saved_commit, const sr_commit_t *commit)
 
void sr_state_set_fresh_srv (void)
 
void sr_state_unset_fresh_srv (void)
 
unsigned int sr_state_srv_is_fresh (void)
 
void sr_state_free_all (void)
 
void sr_state_save (void)
 
int sr_state_is_initialized (void)
 
int sr_state_init (int save_to_disk, int read_from_disk)
 

Variables

static const char default_fname [] = "sr-state"
 
static const char * phase_str [] = { "unknown", "commit", "reveal" }
 
static sr_state_tsr_state = NULL
 
static sr_disk_state_tsr_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_tshared_random_state_mgr = NULL
 

Detailed Description

Functions and data structures for the state of the random protocol as defined in proposal #250.

Definition in file shared_random_state.c.

Macro Definition Documentation

◆ SR_DISK_STATE_MAGIC

#define SR_DISK_STATE_MAGIC   0x98AB1254

Our persistent state magic number.

Definition at line 60 of file shared_random_state.c.

Function Documentation

◆ commit_add_to_state()

static void commit_add_to_state ( sr_commit_t commit,
sr_state_t state 
)
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.

◆ commit_free_()

static void commit_free_ ( void *  p)
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.

◆ disk_state_free_()

static void disk_state_free_ ( sr_disk_state_t state)
static

Free an allocated disk state.

Definition at line 269 of file shared_random_state.c.

◆ disk_state_load_from_disk()

static int disk_state_load_from_disk ( void  )
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().

◆ disk_state_load_from_disk_impl()

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.

◆ disk_state_new()

static sr_disk_state_t* disk_state_new ( time_t  now)
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().

◆ disk_state_parse()

static sr_state_t* disk_state_parse ( const sr_disk_state_t new_disk_state)
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.

◆ disk_state_parse_commits()

static int disk_state_parse_commits ( sr_state_t state,
const sr_disk_state_t disk_state 
)
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().

◆ disk_state_parse_sr_values()

static int disk_state_parse_sr_values ( sr_state_t state,
const sr_disk_state_t disk_state 
)
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().

◆ disk_state_parse_srv()

static int disk_state_parse_srv ( const char *  value,
sr_srv_t dst 
)
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.

◆ disk_state_put_commit_line()

static void disk_state_put_commit_line ( const sr_commit_t commit,
config_line_t line 
)
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.

◆ disk_state_put_srv_line()

static void disk_state_put_srv_line ( const sr_srv_t srv,
config_line_t line 
)
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.

◆ disk_state_reset()

static void disk_state_reset ( void  )
static

Reset disk state that is free allocated memory and zeroed the object.

Definition at line 561 of file shared_random_state.c.

◆ disk_state_save_to_disk()

static int disk_state_save_to_disk ( void  )
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().

◆ disk_state_set()

static void disk_state_set ( sr_disk_state_t state)
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().

◆ disk_state_update()

static void disk_state_update ( void  )
static

Update our disk state based on our global SR state.

Definition at line 580 of file shared_random_state.c.

◆ disk_state_validate()

static int disk_state_validate ( const sr_disk_state_t state)
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()

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.

◆ get_phase_str()

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.

◆ get_sr_protocol_phase()

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().

◆ get_srs_mgr()

static const config_mgr_t* get_srs_mgr ( void  )
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().

◆ get_state_valid_until_time()

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.

◆ is_phase_transition()

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.

◆ new_protocol_run()

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.

◆ reset_state_for_new_protocol_run()

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().

◆ sr_state_add_commit()

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().

◆ sr_state_clean_srvs()

void sr_state_clean_srvs ( void  )

Clean all the SRVs in our state.

Definition at line 1094 of file shared_random_state.c.

◆ sr_state_copy_reveal_info()

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().

◆ sr_state_delete_commits()

void sr_state_delete_commits ( void  )

Remove all commits from our state.

Definition at line 1207 of file shared_random_state.c.

◆ sr_state_free_all()

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_state_get_commit()

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().

◆ sr_state_get_commits()

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.

◆ sr_state_get_current_srv()

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_state_get_phase()

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().

◆ sr_state_get_previous_srv()

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.

◆ sr_state_init()

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().

◆ sr_state_is_initialized()

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().

◆ sr_state_save()

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().

◆ sr_state_set_current_srv()

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().

◆ sr_state_set_fresh_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.

◆ sr_state_set_previous_srv()

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().

◆ sr_state_set_valid_after()

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.

◆ sr_state_srv_is_fresh()

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.

◆ sr_state_unset_fresh_srv()

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.

◆ sr_state_update()

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().

◆ state_del_current_srv()

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().

◆ state_del_previous_srv()

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().

◆ state_free_()

static void state_free_ ( sr_state_t state)
static

Free a state that was allocated with state_new().

Definition at line 222 of file shared_random_state.c.

◆ state_new()

static sr_state_t* state_new ( const char *  fname,
time_t  now 
)
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().

◆ state_query()

static void state_query ( sr_state_action_t  action,
sr_state_object_t  obj_type,
void *  data,
void **  out 
)
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().

◆ state_query_del_()

static void state_query_del_ ( sr_state_object_t  obj_type,
void *  data 
)
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.

◆ state_query_del_all_()

static void state_query_del_all_ ( sr_state_object_t  obj_type)
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.

◆ state_query_get_()

static void* state_query_get_ ( sr_state_object_t  obj_type,
const void *  data 
)
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.

◆ state_query_get_commit()

static sr_commit_t* state_query_get_commit ( const char *  rsa_fpr)
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.

◆ state_query_put_()

static void state_query_put_ ( sr_state_object_t  obj_type,
void *  data 
)
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.

◆ state_rotate_srv()

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().

◆ state_set()

static void state_set ( sr_state_t state)
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().

Variable Documentation

◆ default_fname

const char default_fname[] = "sr-state"
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().

◆ phase_str

const char* phase_str[] = { "unknown", "commit", "reveal" }
static

String representation of a protocol phase.

Definition at line 33 of file shared_random_state.c.

Referenced by get_phase_str().

◆ shared_random_state_mgr

config_mgr_t* shared_random_state_mgr = NULL
static

Global configuration manager for the shared-random state file

Definition at line 100 of file shared_random_state.c.

◆ sr_disk_state

sr_disk_state_t* sr_disk_state = NULL
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().

◆ sr_state

sr_state_t* sr_state = NULL
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().

◆ state_extra_var

const struct_member_t state_extra_var
static
Initial value:
= {
.name = "__extra",
.offset = offsetof(sr_disk_state_t, ExtraLines),
}
@ CONFIG_TYPE_LINELIST
Definition: conftypes.h:61

"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.

◆ state_format

const config_format_t state_format
static
Initial value:
= {
.size = sizeof(sr_disk_state_t),
.magic = {
"sr_disk_state_t",
offsetof(sr_disk_state_t, magic_),
},
.vars = state_vars,
.extra = &state_extra_var,
}
#define SR_DISK_STATE_MAGIC
static const config_var_t state_vars[]
static const struct_member_t state_extra_var

Configuration format of sr_disk_state_t.

Definition at line 88 of file shared_random_state.c.

◆ state_vars

const config_var_t state_vars[]
static
Initial value:
= {
V(Version, POSINT, "0"),
V(TorVersion, STRING, NULL),
V(ValidAfter, ISOTIME, NULL),
V(ValidUntil, ISOTIME, NULL),
V(Commit, LINELIST, NULL),
V(SharedRandValues, LINELIST_V, NULL),
VAR("SharedRandPreviousValue",LINELIST_S, SharedRandValues, NULL),
VAR("SharedRandCurrentValue", LINELIST_S, SharedRandValues, NULL),
}
#define VAR(varname, conftype, member, initvalue)
Definition: config.c:259
#define END_OF_CONFIG_VARS
Definition: confmacros.h:22

Array of variables that are saved to disk as a persistent state.

Definition at line 64 of file shared_random_state.c.