tor  0.4.2.0-alpha-dev
Macros | Functions | Variables
routerparse.c File Reference
#include "core/or/or.h"
#include "app/config/config.h"
#include "core/or/policies.h"
#include "core/or/versions.h"
#include "feature/dirparse/parsecommon.h"
#include "feature/dirparse/policy_parse.h"
#include "feature/dirparse/routerparse.h"
#include "feature/dirparse/sigcommon.h"
#include "feature/dirparse/unparseable.h"
#include "feature/nodelist/describe.h"
#include "feature/nodelist/nickname.h"
#include "feature/nodelist/routerinfo.h"
#include "feature/nodelist/routerlist.h"
#include "feature/nodelist/torcert.h"
#include "feature/relay/router.h"
#include "lib/crypt_ops/crypto_curve25519.h"
#include "lib/crypt_ops/crypto_ed25519.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/memarea/memarea.h"
#include "lib/sandbox/sandbox.h"
#include "core/or/addr_policy_st.h"
#include "feature/nodelist/extrainfo_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerlist_st.h"

Go to the source code of this file.

Functions

static int router_add_exit_policy (routerinfo_t *router, directory_token_t *tok)
 
static smartlist_tfind_all_exitpolicy (smartlist_t *s)
 
int router_get_router_hash (const char *s, size_t s_len, char *digest)
 
int router_get_extrainfo_hash (const char *s, size_t s_len, char *digest)
 
static int find_start_of_next_router_or_extrainfo (const char **s_ptr, const char *eos, int *is_extrainfo_out)
 
int router_parse_list_from_string (const char **s, const char *eos, smartlist_t *dest, saved_location_t saved_location, int want_extrainfo, int allow_annotations, const char *prepend_annotations, smartlist_t *invalid_digests_out)
 
int find_single_ipv6_orport (const smartlist_t *list, tor_addr_t *addr_out, uint16_t *port_out)
 
routerinfo_trouter_parse_entry_from_string (const char *s, const char *end, int cache_copy, int allow_annotations, const char *prepend_annotations, int *can_dl_again_out)
 
extrainfo_textrainfo_parse_entry_from_string (const char *s, const char *end, int cache_copy, struct digest_ri_map_t *routermap, int *can_dl_again_out)
 
void routerparse_init (void)
 
void routerparse_free_all (void)
 

Variables

const token_rule_t routerdesc_token_table []
 
static token_rule_t extrainfo_token_table []
 

Detailed Description

Code to parse and validate consensus documents and votes.

Code to parse and validate router descriptors, consenus directories, and similar objects.

The objects parsed by this module use a common text-based metaformat, documented in dir-spec.txt in torspec.git. This module is itself divided into two major kinds of function: code to handle the metaformat, and code to convert from particular instances of the metaformat into the objects that Tor uses.

The generic parsing code works by calling a table-based tokenizer on the input string. Each token corresponds to a single line with a token, plus optional arguments on that line, plus an optional base-64 encoded object after that line. Each token has a definition in a table of token_rule_t entries that describes how many arguments it can take, whether it takes an object, how many times it may appear, whether it must appear first, and so on.

The tokenizer function tokenize_string() converts its string input into a smartlist full of instances of directory_token_t, according to a provided table of token_rule_t.

The generic parts of this module additionally include functions for finding the start and end of signed information inside a signed object, and computing the digest that will be signed.

There are also functions for saving objects to disk that have caused parsing to fail.

The specific parts of this module describe conversions between particular lists of directory_token_t and particular objects. The kinds of objects that can be parsed here are:

Definition in file routerparse.c.

Function Documentation

◆ extrainfo_parse_entry_from_string()

extrainfo_t* extrainfo_parse_entry_from_string ( const char *  s,
const char *  end,
int  cache_copy,
struct digest_ri_map_t *  routermap,
int *  can_dl_again_out 
)

Parse a single extrainfo entry from the string s, ending at end. (If end is NULL, parse up to the end of s.) If cache_copy is true, make a copy of the extra-info document in the cache_info fields of the result. If routermap is provided, use it as a map from router identity to routerinfo_t when looking up signing keys.

If can_dl_again_out is provided, set *can_dl_again_out to 1 if it's okay to try to download an extrainfo with this same digest again, and 0 if it isn't. (It might not be okay to download it again if part of the part covered by the digest is invalid.)

Definition at line 961 of file routerparse.c.

◆ find_all_exitpolicy()

static smartlist_t * find_all_exitpolicy ( smartlist_t s)
static

Return a newly allocated smartlist of all accept or reject tokens in s.

Definition at line 1213 of file routerparse.c.

References SMARTLIST_FOREACH.

◆ find_single_ipv6_orport()

int find_single_ipv6_orport ( const smartlist_t list,
tor_addr_t addr_out,
uint16_t *  port_out 
)

Try to find an IPv6 OR port in list of directory_token_t's with at least one argument (use GE(1) in setup). If found, store address and port number to addr_out and port_out. Return number of OR ports found.

Definition at line 336 of file routerparse.c.

References SMARTLIST_FOREACH_BEGIN, tor_addr_copy(), tor_addr_parse_mask_ports(), and tor_assert().

◆ find_start_of_next_router_or_extrainfo()

static int find_start_of_next_router_or_extrainfo ( const char **  s_ptr,
const char *  eos,
int *  is_extrainfo_out 
)
static

Helper: move *s_ptr ahead to the next router, the next extra-info, or to the first of the annotations proceeding the next router or extra-info—whichever comes first. Set is_extrainfo_out to true if we found an extrainfo, or false if found a router. Do not scan beyond eos. Return -1 if we found nothing; 0 if we found something.

Definition at line 198 of file routerparse.c.

References eat_whitespace_eos(), strcmpstart(), and tor_assert().

Referenced by router_parse_list_from_string().

◆ router_add_exit_policy()

static int router_add_exit_policy ( routerinfo_t router,
directory_token_t tok 
)
static

Add an exit policy stored in the token tok to the router info in router. Return 0 on success, -1 on failure.

Definition at line 1176 of file routerparse.c.

References routerinfo_t::exit_policy, router_parse_addr_policy(), and directory_token_t::tp.

◆ router_get_extrainfo_hash()

int router_get_extrainfo_hash ( const char *  s,
size_t  s_len,
char *  digest 
)

Set digest to the SHA-1 digest of the hash of the s_len-byte extrainfo string at s. Return 0 on success, -1 on failure.

Definition at line 186 of file routerparse.c.

References router_get_hash_impl().

◆ router_get_router_hash()

int router_get_router_hash ( const char *  s,
size_t  s_len,
char *  digest 
)

Set digest to the SHA-1 digest of the hash of the first router in s. Return 0 on success, -1 on failure.

Definition at line 176 of file routerparse.c.

References router_get_hash_impl().

◆ router_parse_entry_from_string()

routerinfo_t* router_parse_entry_from_string ( const char *  s,
const char *  end,
int  cache_copy,
int  allow_annotations,
const char *  prepend_annotations,
int *  can_dl_again_out 
)

Helper function: reads a single router entry from *s ... *end. Mallocs a new router and returns it if all goes well, else returns NULL. If cache_copy is true, duplicate the contents of s through end into the signed_descriptor_body of the resulting routerinfo_t.

If end is NULL, s must be properly NUL-terminated.

If allow_annotations, it's okay to encounter annotations in s before the router; if it's false, reject the router if it's annotated. If prepend_annotations is set, it should contain some annotations: append them to the front of the router before parsing it, and keep them around when caching the router.

Only one of allow_annotations and prepend_annotations may be set.

If can_dl_again_out is provided, set *can_dl_again_out to 1 if it's okay to try to download a descriptor with this same digest again, and 0 if it isn't. (It might not be okay to download it again if part of the part covered by the digest is invalid.)

Definition at line 390 of file routerparse.c.

References memarea_new(), routerdesc_token_table, tokenize_string(), and tor_assert().

Referenced by router_load_single_router(), and routerlist_reparse_old().

◆ router_parse_list_from_string()

int router_parse_list_from_string ( const char **  s,
const char *  eos,
smartlist_t dest,
saved_location_t  saved_location,
int  want_extrainfo,
int  allow_annotations,
const char *  prepend_annotations,
smartlist_t invalid_digests_out 
)

Given a string *s containing a concatenated sequence of router descriptors (or extra-info documents if want_extrainfo is set), parses them and stores the result in dest. All routers are marked running and valid. Advances *s to a point immediately following the last router entry. Ignore any trailing router entries that are not complete.

If saved_location isn't SAVED_IN_CACHE, make a local copy of each descriptor in the signed_descriptor_body field of each routerinfo_t. If it isn't SAVED_NOWHERE, remember the offset of each descriptor.

Returns 0 on success and -1 on failure. Adds a digest to invalid_digests_out for every entry that was unparseable or invalid. (This may cause duplicate entries.)

Definition at line 245 of file routerparse.c.

References DIGEST_LEN, find_start_of_next_router_or_extrainfo(), and tor_assert().

Referenced by router_load_extrainfo_from_string(), and router_load_routers_from_string().

◆ routerparse_free_all()

void routerparse_free_all ( void  )

Clean up all data structures used by routerparse.c at exit

Definition at line 1242 of file routerparse.c.

References dump_desc_fifo_cleanup().

◆ routerparse_init()

void routerparse_init ( void  )

Called on startup; right now we just handle scanning the unparseable descriptor dumps, but hang anything else we might need to do in the future here as well.

Definition at line 1228 of file routerparse.c.

References dump_desc_init(), and sandbox_is_active().

Variable Documentation

◆ extrainfo_token_table

token_rule_t extrainfo_token_table[]
static

List of tokens recognized in extra-info documents.

Definition at line 128 of file routerparse.c.

◆ routerdesc_token_table

const token_rule_t routerdesc_token_table[]

List of tokens recognized in router descriptors

Definition at line 84 of file routerparse.c.

Referenced by router_parse_entry_from_string().