Tor 0.4.9.0-alpha-dev
Macros | Enumerations | Functions | Variables
dirserv.c File Reference

Directory server core implementation. Manages directory contents and generates directory documents. More...

#include "core/or/or.h"
#include "app/config/config.h"
#include "core/mainloop/connection.h"
#include "feature/dircache/conscache.h"
#include "feature/dircache/consdiffmgr.h"
#include "feature/dircommon/directory.h"
#include "feature/dircache/dirserv.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/routerlist.h"
#include "feature/relay/router.h"
#include "feature/relay/routermode.h"
#include "feature/stats/predict_ports.h"
#include "feature/dircache/cached_dir_st.h"
#include "feature/dircommon/dir_connection_st.h"
#include "feature/nodelist/extrainfo_st.h"
#include "feature/nodelist/microdesc_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "feature/nodelist/routerlist_st.h"
#include "lib/compress/compress.h"

Go to the source code of this file.

Macros

#define DIRSERV_CACHED_DIR_CHUNK_SIZE   8192
 
#define DIRSERV_BUFFER_MIN   16384
 

Enumerations

enum  spooled_resource_flush_status_t { SRFS_ERR = -1 , SRFS_MORE = 0 , SRFS_DONE }
 

Functions

static void clear_cached_dir (cached_dir_t *d)
 
static const signed_descriptor_tget_signed_descriptor_by_fp (const uint8_t *fp, int extrainfo)
 
static int spooled_resource_lookup_body (const spooled_resource_t *spooled, int conn_is_encrypted, const uint8_t **body_out, size_t *size_out, time_t *published_out)
 
static cached_dir_tspooled_resource_lookup_cached_dir (const spooled_resource_t *spooled, time_t *published_out)
 
static cached_dir_tlookup_cached_dir_by_fp (const uint8_t *fp)
 
int directory_caches_unknown_auth_certs (const or_options_t *options)
 
int directory_caches_dir_info (const or_options_t *options)
 
int directory_permits_begindir_requests (const or_options_t *options)
 
void cached_dir_decref (cached_dir_t *d)
 
cached_dir_tnew_cached_dir (char *s, time_t published)
 
static void free_cached_dir_ (void *_d)
 
void dirserv_set_cached_consensus_networkstatus (const char *networkstatus, size_t networkstatus_len, const char *flavor_name, const common_digests_t *digests, const uint8_t *sha3_as_signed, time_t published)
 
cached_dir_tdirserv_get_consensus (const char *flavor_name)
 
int dir_split_resource_into_spoolable (const char *resource, dir_spool_source_t source, smartlist_t *spool_out, int *compressed_out, int flags)
 
int dirserv_get_routerdesc_spool (smartlist_t *spool_out, const char *key, dir_spool_source_t source, int conn_is_encrypted, const char **msg_out)
 
spooled_resource_tspooled_resource_new (dir_spool_source_t source, const uint8_t *digest, size_t digestlen)
 
spooled_resource_tspooled_resource_new_from_cache_entry (consensus_cache_entry_t *entry)
 
void spooled_resource_free_ (spooled_resource_t *spooled)
 
static double estimate_compression_ratio (dir_spool_source_t source)
 
static size_t spooled_resource_estimate_size (const spooled_resource_t *spooled, dir_connection_t *conn, int compressed, time_t *published_out)
 
static spooled_resource_flush_status_t spooled_resource_flush_some (spooled_resource_t *spooled, dir_connection_t *conn)
 
void dirserv_spool_remove_missing_and_guess_size (dir_connection_t *conn, time_t cutoff, int compression, size_t *size_out, int *n_expired_out)
 
static int dirserv_spool_sort_comparison_ (const void **a_, const void **b_)
 
void dirserv_spool_sort (dir_connection_t *conn)
 
int connection_dirserv_flushed_some (dir_connection_t *conn)
 
void dir_conn_clear_spool (dir_connection_t *conn)
 
void dirserv_free_all (void)
 

Variables

static strmap_t * cached_consensuses = NULL
 

Detailed Description

Directory server core implementation. Manages directory contents and generates directory documents.

This module implements most of directory cache functionality, and some of the directory authority functionality. The directory.c module delegates here in order to handle incoming requests from clients, via connection_dirserv_flushed_some() and its kin. In order to save RAM, this module is responsible for spooling directory objects (in whole or in part) onto buf_t instances, and then closing the dir_connection_t once the objects are totally flushed.

The directory.c module also delegates here for handling descriptor uploads via dirserv_add_multiple_descriptors().

Additionally, this module handles some aspects of voting, including: deciding how to vote on individual flags (based on decisions reached in rephist.c), of formatting routerstatus lines, and deciding what relays to include in an authority's vote. (TODO: Those functions could profitably be split off. They only live in this file because historically they were shared among the v1, v2, and v3 directory code.)

Definition in file dirserv.c.

Macro Definition Documentation

◆ DIRSERV_BUFFER_MIN

#define DIRSERV_BUFFER_MIN   16384

When we're spooling data onto our outbuf, add more whenever we dip below this threshold.

Definition at line 731 of file dirserv.c.

◆ DIRSERV_CACHED_DIR_CHUNK_SIZE

#define DIRSERV_CACHED_DIR_CHUNK_SIZE   8192

When spooling data from a cached_dir_t object, we always add at least this much.

Definition at line 384 of file dirserv.c.

Enumeration Type Documentation

◆ spooled_resource_flush_status_t

Return code for spooled_resource_flush_some

Definition at line 448 of file dirserv.c.

Function Documentation

◆ cached_dir_decref()

void cached_dir_decref ( cached_dir_t d)

Decrement the reference count on d, and free it if it no longer has any references.

Definition at line 124 of file dirserv.c.

Referenced by free_cached_dir_(), and spooled_resource_free_().

◆ clear_cached_dir()

static void clear_cached_dir ( cached_dir_t d)
static

Remove all storage held in d, but do not free d itself.

Definition at line 151 of file dirserv.c.

Referenced by cached_dir_decref().

◆ connection_dirserv_flushed_some()

int connection_dirserv_flushed_some ( dir_connection_t conn)

Called whenever we have flushed some directory data in state SERVER_WRITING, or whenever we want to fill the buffer with initial directory data (so that subsequent writes will occur, and trigger this function again.)

Return 0 on success, and -1 on failure.

Definition at line 742 of file dirserv.c.

◆ dir_conn_clear_spool()

void dir_conn_clear_spool ( dir_connection_t conn)

Remove every element from conn's outgoing spool, and delete the spool.

Definition at line 787 of file dirserv.c.

◆ dir_split_resource_into_spoolable()

int dir_split_resource_into_spoolable ( const char *  resource,
dir_spool_source_t  source,
smartlist_t spool_out,
int *  compressed_out,
int  flags 
)

As dir_split_resource_into_fingerprints, but instead fills spool_out with a list of spoolable_resource_t for the resource identified through source.

Definition at line 212 of file dirserv.c.

Referenced by handle_get_microdesc().

◆ directory_caches_dir_info()

int directory_caches_dir_info ( const or_options_t options)

Return 1 if we want to fetch and serve descriptors, networkstatuses, etc Else return 0. Check options->DirPort_set and directory_permits_begindir_requests() to see if we are willing to serve these directory documents to others via the DirPort and begindir-over-ORPort, respectively.

To check if we should fetch documents, use we_want_to_fetch_flavor and we_want_to_fetch_unknown_auth_certs instead of this function.

Definition at line 94 of file dirserv.c.

Referenced by dirclient_too_idle_to_fetch_descriptors(), we_fetch_microdescriptors(), and we_fetch_router_descriptors().

◆ directory_caches_unknown_auth_certs()

int directory_caches_unknown_auth_certs ( const or_options_t options)

Return true iff we want to serve certificates for authorities that we don't acknowledge as authorities ourself. Use we_want_to_fetch_unknown_auth_certs to check if we want to fetch and keep these certificates.

Definition at line 79 of file dirserv.c.

◆ directory_permits_begindir_requests()

int directory_permits_begindir_requests ( const or_options_t options)

Return 1 if we want to allow remote clients to ask us directory requests via the "begin_dir" interface, which doesn't require having any separate port open.

Definition at line 110 of file dirserv.c.

◆ dirserv_free_all()

void dirserv_free_all ( void  )

Release all storage used by the directory server.

Definition at line 799 of file dirserv.c.

◆ dirserv_get_consensus()

cached_dir_t * dirserv_get_consensus ( const char *  flavor_name)

Return the latest downloaded consensus networkstatus in encoded, signed, optionally compressed format, suitable for sending to clients.

Definition at line 201 of file dirserv.c.

Referenced by handle_response_fetch_consensus().

◆ dirserv_get_routerdesc_spool()

int dirserv_get_routerdesc_spool ( smartlist_t spool_out,
const char *  key,
dir_spool_source_t  source,
int  conn_is_encrypted,
const char **  msg_out 
)

As dirserv_get_routerdescs(), but instead of getting signed_descriptor_t pointers, adds copies of digests to fps_out, and doesn't use the /tor/server/ prefix. For a /d/ request, adds descriptor digests; for other requests, adds identity digests.

Definition at line 245 of file dirserv.c.

◆ dirserv_set_cached_consensus_networkstatus()

void dirserv_set_cached_consensus_networkstatus ( const char *  networkstatus,
size_t  networkstatus_len,
const char *  flavor_name,
const common_digests_t digests,
const uint8_t *  sha3_as_signed,
time_t  published 
)

Replace the v3 consensus networkstatus of type flavor_name that we're serving with networkstatus, published at published. No validation is performed.

Definition at line 174 of file dirserv.c.

◆ dirserv_spool_remove_missing_and_guess_size()

void dirserv_spool_remove_missing_and_guess_size ( dir_connection_t conn,
time_t  cutoff,
int  compression,
size_t *  size_out,
int *  n_expired_out 
)

Try to guess the number of bytes that will be needed to send the spooled objects for conn's outgoing spool. In the process, remove every element of the spool that refers to an absent object, or which was published earlier than cutoff. Set *size_out to the number of bytes, and *n_expired_out to the number of objects removed for being too old.

Definition at line 644 of file dirserv.c.

◆ dirserv_spool_sort()

void dirserv_spool_sort ( dir_connection_t conn)

Sort all the entries in conn by digest.

Definition at line 697 of file dirserv.c.

◆ dirserv_spool_sort_comparison_()

static int dirserv_spool_sort_comparison_ ( const void **  a_,
const void **  b_ 
)
static

Helper: used to sort a connection's spool.

Definition at line 688 of file dirserv.c.

Referenced by dirserv_spool_sort().

◆ estimate_compression_ratio()

static double estimate_compression_ratio ( dir_spool_source_t  source)
static

Return an compression ratio for compressing objects from source.

Definition at line 389 of file dirserv.c.

Referenced by spooled_resource_estimate_size().

◆ free_cached_dir_()

static void free_cached_dir_ ( void *  _d)
static

Free all storage held by the cached_dir_t in d.

Definition at line 160 of file dirserv.c.

◆ get_signed_descriptor_by_fp()

static const signed_descriptor_t * get_signed_descriptor_by_fp ( const uint8_t *  fp,
int  extrainfo 
)
static

Return the cache-info for identity fingerprint fp, or its extra-info document if extrainfo is true. Return NULL if not found or if the descriptor is older than publish_cutoff.

Definition at line 709 of file dirserv.c.

◆ lookup_cached_dir_by_fp()

static cached_dir_t * lookup_cached_dir_by_fp ( const uint8_t *  fp)
static

Given a fingerprint fp which is either set if we're looking for a v2 status, or zeroes if we're looking for a v3 status, or a NUL-padded flavor name if we want a flavored v3 status, return a pointer to the appropriate cached dir object, or NULL if there isn't one available.

Definition at line 624 of file dirserv.c.

Referenced by spooled_resource_lookup_cached_dir().

◆ new_cached_dir()

cached_dir_t * new_cached_dir ( char *  s,
time_t  published 
)

Allocate and return a new cached_dir_t containing the string s, published at published.

Definition at line 135 of file dirserv.c.

Referenced by dirserv_set_cached_consensus_networkstatus().

◆ spooled_resource_estimate_size()

static size_t spooled_resource_estimate_size ( const spooled_resource_t spooled,
dir_connection_t conn,
int  compressed,
time_t *  published_out 
)
static

Return an estimated number of bytes needed for transmitting the resource in spooled on conn

As a convenient side-effect, set *published_out to the resource's publication time.

Definition at line 404 of file dirserv.c.

◆ spooled_resource_flush_some()

static spooled_resource_flush_status_t spooled_resource_flush_some ( spooled_resource_t spooled,
dir_connection_t conn 
)
static

Flush some or all of the bytes from spooled onto conn. Return SRFS_ERR on error, SRFS_MORE if there are more bytes to flush from this spooled resource, or SRFS_DONE if we are done flushing this spooled resource.

Definition at line 460 of file dirserv.c.

◆ spooled_resource_free_()

void spooled_resource_free_ ( spooled_resource_t spooled)

Release all storage held by spooled.

Definition at line 366 of file dirserv.c.

◆ spooled_resource_lookup_body()

static int spooled_resource_lookup_body ( const spooled_resource_t spooled,
int  conn_is_encrypted,
const uint8_t **  body_out,
size_t *  size_out,
time_t *  published_out 
)
static

Helper: Look up the body for an eagerly-served spooled_resource. If conn_is_encrypted is false, don't look up any resource that shouldn't be sent over an unencrypted connection. On success, set body_out, size_out, and published_out to refer to the resource's body, size, and publication date, and return 0. On failure return -1.

Definition at line 551 of file dirserv.c.

Referenced by spooled_resource_estimate_size(), and spooled_resource_flush_some().

◆ spooled_resource_lookup_cached_dir()

static cached_dir_t * spooled_resource_lookup_cached_dir ( const spooled_resource_t spooled,
time_t *  published_out 
)
static

Helper: find the cached_dir_t for a spooled_resource_t, for sending it to conn. Set *published_out, if provided, to the published time of the cached_dir_t.

DOES NOT increase the reference count on the result. Callers must do that themselves if they mean to hang on to it.

Definition at line 532 of file dirserv.c.

Referenced by spooled_resource_estimate_size().

◆ spooled_resource_new()

spooled_resource_t * spooled_resource_new ( dir_spool_source_t  source,
const uint8_t *  digest,
size_t  digestlen 
)

Definition at line 310 of file dirserv.c.

◆ spooled_resource_new_from_cache_entry()

spooled_resource_t * spooled_resource_new_from_cache_entry ( consensus_cache_entry_t entry)

Create a new spooled_resource_t to spool the contents of entry to the user. Return the spooled object on success, or NULL on failure (which is probably caused by a failure to map the body of the item from disk).

Adds a reference to entry's reference counter.

Definition at line 345 of file dirserv.c.

Variable Documentation

◆ cached_consensuses

strmap_t* cached_consensuses = NULL
static

Map from flavor name to the cached_dir_t for the v3 consensuses that we're currently serving.

Definition at line 119 of file dirserv.c.

Referenced by dirserv_get_consensus(), dirserv_set_cached_consensus_networkstatus(), and lookup_cached_dir_by_fp().