Skip to content

Commit

Permalink
Add: GETINFO support for dumping microdesc consensus
Browse files Browse the repository at this point in the history
test: check if GETINFO commands return expected consensus data..
docs: function docs easier to understand
update: Code simplification and refactoring change
  • Loading branch information
ltbringer committed Oct 6, 2019
1 parent d41c4f1 commit 18100e4
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 61 deletions.
5 changes: 4 additions & 1 deletion changes/ticket31684
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
o Minor features (onion services):
- GETINFO dir/status-vote/current/consensus-microdesc dumps microdesc consensus. Closes ticket 31684.
- Implement a new GETINFO command to fetch microdesc consensus. Closes ticket 31684.

o Code simplification and refactoring (onion services):
- Create a helper function that can fetch network status or microdesc consensuses.
64 changes: 27 additions & 37 deletions src/feature/control/control_getinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,47 +325,32 @@ getinfo_helper_current_time(control_connection_t *control_conn,
return 0;
}

/**
* Switch between microdesc vs networkstatus descriptor dumps
* Assumes that question would be either:
* - dir/status-vote/current/consensus-microdesc
* or
* - dir/status-vote/current/consensus
*/
/** Fetch network-status or micro-descriptor
* returns: 0 on success -1 on error. */
STATIC int
getinfo_helper_current_consensus(const char* question,
char** answer,
const char** errmsg)
getinfo_helper_current_consensus(int flavor,
const char *flavor_name,
char** answer,
const char** errmsg)
{
// Ensures question contains a request for
// ns or microdesc consensus
if (
strcmp(question, "dir/status-vote/current/consensus") &&
strcmp(question, "dir/status-vote/current/consensus-microdesc")) {
return 0;
}

const char* consensus_type = !strcmp(
question, "dir/status-vote/current/consensus-microdesc"
) ? "microdesc" : "ns";

if (we_want_to_fetch_flavor(get_options(), FLAV_NS)) {
const cached_dir_t *consensus = dirserv_get_consensus(consensus_type);
if (we_want_to_fetch_flavor(get_options(), flavor)) {
/** Check from the cache */
const cached_dir_t *consensus = dirserv_get_consensus(flavor_name);
if (consensus) {
*answer = tor_strdup(consensus->dir);
}
}
if (!*answer) { /* try loading it from disk */
tor_mmap_t *mapped = networkstatus_map_cached_consensus(consensus_type);
tor_mmap_t *mapped = networkstatus_map_cached_consensus(flavor_name);
if (mapped) {
*answer = tor_memdup_nulterm(mapped->data, mapped->size);
tor_munmap_file(mapped);
}
if (!*answer) { /* generate an error */
*errmsg = "Could not open cached consensus. "
"Make sure FetchUselessDescriptors is set to 1.";
return -1;
}
return -1;
}
}
return 0;
}
Expand Down Expand Up @@ -622,16 +607,21 @@ getinfo_helper_dir(control_connection_t *control_conn,
smartlist_free(descs);
} else if (!strcmpstart(question, "dir/status/")) {
*answer = tor_strdup("");
} else if (-1 == getinfo_helper_current_consensus(
question,
answer,
errmsg))
{
/**
* answer is set by getinfo_helper_current_consensus
* if the question matches
*/
return -1;
} else if (!strcmp(question, "dir/status-vote/current/consensus")) {
int consensus_result = getinfo_helper_current_consensus(FLAV_NS,
"ns",
answer, errmsg);
if (consensus_result == -1) {
return -1;
}
} else if (!strcmp(question,
"dir/status-vote/current/consensus-microdesc")) {
int consensus_result = getinfo_helper_current_consensus(FLAV_MICRODESC,
"microdesc",
answer, errmsg);
if (consensus_result == -1) {
return -1;
}
} else if (!strcmp(question, "network-status")) { /* v1 */
static int network_status_warned = 0;
if (!network_status_warned) {
Expand Down
4 changes: 3 additions & 1 deletion src/feature/control/control_getinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ STATIC int getinfo_helper_downloads(
const char *question, char **answer,
const char **errmsg);
STATIC int getinfo_helper_current_consensus(
const char *question, char **answer,
int flavor,
const char* flavor_name,
char **answer,
const char **errmsg);
STATIC int getinfo_helper_dir(
control_connection_t *control_conn,
Expand Down
5 changes: 3 additions & 2 deletions src/feature/dircache/dirserv.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ dirserv_set_cached_consensus_networkstatus(const char *networkstatus,

/** Return the latest downloaded consensus networkstatus in encoded, signed,
* optionally compressed format, suitable for sending to clients. */
cached_dir_t *
dirserv_get_consensus(const char *flavor_name)
MOCK_IMPL(cached_dir_t *,
dirserv_get_consensus,
(const char *flavor_name))
{
if (!cached_consensuses)
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/feature/dircache/dirserv.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int directory_permits_begindir_requests(const or_options_t *options);
int directory_too_idle_to_fetch_descriptors(const or_options_t *options,
time_t now);

cached_dir_t *dirserv_get_consensus(const char *flavor_name);
MOCK_DECL(cached_dir_t *, dirserv_get_consensus, (const char *flavor_name));
void dirserv_set_cached_consensus_networkstatus(const char *consensus,
size_t consensus_len,
const char *flavor_name,
Expand Down
14 changes: 8 additions & 6 deletions src/feature/nodelist/networkstatus.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ networkstatus_reset_download_failures(void)
}

/** Return the filename used to cache the consensus of a given flavor */
static char *
networkstatus_get_cache_fname(int flav,
const char *flavorname,
int unverified_consensus)
MOCK_IMPL(char *,
networkstatus_get_cache_fname,
(int flav,
const char *flavorname,
int unverified_consensus))
{
char buf[128];
const char *prefix;
Expand Down Expand Up @@ -856,8 +857,9 @@ router_get_consensus_status_by_id(const char *digest)
*
* For certificate fetches, use we_want_to_fetch_unknown_auth_certs, and
* for serving fetched documents, use directory_caches_dir_info. */
int
we_want_to_fetch_flavor(const or_options_t *options, int flavor)
MOCK_IMPL(int,
we_want_to_fetch_flavor,
(const or_options_t *options, int flavor))
{
if (flavor < 0 || flavor > N_CONSENSUS_FLAVORS) {
/* This flavor is crazy; we don't want it */
Expand Down
11 changes: 9 additions & 2 deletions src/feature/nodelist/networkstatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

void networkstatus_reset_warnings(void);
void networkstatus_reset_download_failures(void);
tor_mmap_t *networkstatus_map_cached_consensus(const char *flavorname);
MOCK_DECL(char *,
networkstatus_get_cache_fname,
(int flav,
const char *flavorname,
int unverified_consensus));
tor_mmap_t * networkstatus_map_cached_consensus(const char *flavorname);
int router_reload_consensus_networkstatus(void);
void routerstatus_free_(routerstatus_t *rs);
#define routerstatus_free(rs) \
Expand Down Expand Up @@ -71,7 +76,9 @@ const routerstatus_t *router_get_consensus_status_by_descriptor_digest(
MOCK_DECL(routerstatus_t *,
router_get_mutable_consensus_status_by_descriptor_digest,
(networkstatus_t *consensus, const char *digest));
int we_want_to_fetch_flavor(const or_options_t *options, int flavor);
MOCK_DECL(int,
we_want_to_fetch_flavor,
(const or_options_t *options, int flavor));
int we_want_to_fetch_unknown_auth_certs(const or_options_t *options);
void networkstatus_consensus_download_failed(int status_code,
const char *flavname);
Expand Down
13 changes: 5 additions & 8 deletions src/lib/fs/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
* failure, return NULL. Sets errno properly, using ERANGE to mean
* "empty file". Must only be called on trusted Tor-owned files, as changing
* the underlying file's size causes unspecified behavior. */
tor_mmap_t *
tor_mmap_file(const char *filename)
MOCK_IMPL(tor_mmap_t *, tor_mmap_file, (const char *filename))
{
int fd; /* router file */
char *string;
Expand Down Expand Up @@ -111,8 +110,7 @@ tor_mmap_file(const char *filename)
}
/** Release storage held for a memory mapping; returns 0 on success,
* or -1 on failure (and logs a warning). */
int
tor_munmap_file(tor_mmap_t *handle)
MOCK_IMPL(int, tor_munmap_file, (tor_mmap_t *handle))
{
int res;

Expand All @@ -132,8 +130,8 @@ tor_munmap_file(tor_mmap_t *handle)
return res;
}
#elif defined(_WIN32)
tor_mmap_t *
tor_mmap_file(const char *filename)

MOCK_IMPL(tor_mmap_t *, tor_mmap_file, (const char *filename))
{
TCHAR tfilename[MAX_PATH]= {0};
tor_mmap_t *res = tor_malloc_zero(sizeof(tor_mmap_t));
Expand Down Expand Up @@ -213,8 +211,7 @@ tor_mmap_file(const char *filename)
}

/* Unmap the file, and return 0 for success or -1 for failure */
int
tor_munmap_file(tor_mmap_t *handle)
MOCK_IMPL(int, tor_munmap_file, (tor_mmap_t *handle))
{
if (handle == NULL)
return 0;
Expand Down
5 changes: 3 additions & 2 deletions src/lib/fs/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define TOR_MMAP_H

#include "lib/cc/compat_compiler.h"
#include "lib/testsupport/testsupport.h"
#include <stddef.h>

#ifdef _WIN32
Expand All @@ -35,7 +36,7 @@ typedef struct tor_mmap_t {

} tor_mmap_t;

tor_mmap_t *tor_mmap_file(const char *filename);
int tor_munmap_file(tor_mmap_t *handle);
MOCK_DECL(tor_mmap_t *, tor_mmap_file, (const char *filename));
MOCK_DECL(int, tor_munmap_file, (tor_mmap_t *handle));

#endif /* !defined(TOR_MMAP_H) */

0 comments on commit 18100e4

Please sign in to comment.