Move dirserv_get_routerdescs() to control_getinfo.c

This function had some XXX comments indicating (correctly) that it
was not actually used by the dirserver code, and that only the
controller still used it.
This commit is contained in:
Nick Mathewson 2020-01-08 21:32:16 -05:00
parent 4cf15ee015
commit fe8156dbc2
3 changed files with 82 additions and 84 deletions

View File

@ -34,6 +34,7 @@
#include "feature/dircache/dirserv.h"
#include "feature/dirclient/dirclient.h"
#include "feature/dirclient/dlstatus.h"
#include "feature/dircommon/directory.h"
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_cache.h"
#include "feature/hs_common/shared_random_client.h"
@ -361,6 +362,86 @@ getinfo_helper_current_consensus(consensus_flavor_t flavor,
return 0;
}
/** Helper for getinfo_helper_dir.
*
* Add a signed_descriptor_t to <b>descs_out</b> for each router matching
* <b>key</b>. The key should be either
* - "/tor/server/authority" for our own routerinfo;
* - "/tor/server/all" for all the routerinfos we have, concatenated;
* - "/tor/server/fp/FP" where FP is a plus-separated sequence of
* hex identity digests; or
* - "/tor/server/d/D" where D is a plus-separated sequence
* of server descriptor digests, in hex.
*
* Return 0 if we found some matching descriptors, or -1 if we do not
* have any descriptors, no matching descriptors, or if we did not
* recognize the key (URL).
* If -1 is returned *<b>msg</b> will be set to an appropriate error
* message.
*/
static int
controller_get_routerdescs(smartlist_t *descs_out, const char *key,
const char **msg)
{
*msg = NULL;
if (!strcmp(key, "/tor/server/all")) {
routerlist_t *rl = router_get_routerlist();
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
smartlist_add(descs_out, &(r->cache_info)));
} else if (!strcmp(key, "/tor/server/authority")) {
const routerinfo_t *ri = router_get_my_routerinfo();
if (ri)
smartlist_add(descs_out, (void*) &(ri->cache_info));
} else if (!strcmpstart(key, "/tor/server/d/")) {
smartlist_t *digests = smartlist_new();
key += strlen("/tor/server/d/");
dir_split_resource_into_fingerprints(key, digests, NULL,
DSR_HEX|DSR_SORT_UNIQ);
SMARTLIST_FOREACH(digests, const char *, d,
{
signed_descriptor_t *sd = router_get_by_descriptor_digest(d);
if (sd)
smartlist_add(descs_out,sd);
});
SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
smartlist_free(digests);
} else if (!strcmpstart(key, "/tor/server/fp/")) {
smartlist_t *digests = smartlist_new();
time_t cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
key += strlen("/tor/server/fp/");
dir_split_resource_into_fingerprints(key, digests, NULL,
DSR_HEX|DSR_SORT_UNIQ);
SMARTLIST_FOREACH_BEGIN(digests, const char *, d) {
if (router_digest_is_me(d)) {
/* calling router_get_my_routerinfo() to make sure it exists */
const routerinfo_t *ri = router_get_my_routerinfo();
if (ri)
smartlist_add(descs_out, (void*) &(ri->cache_info));
} else {
const routerinfo_t *ri = router_get_by_id_digest(d);
/* Don't actually serve a descriptor that everyone will think is
* expired. This is an (ugly) workaround to keep buggy 0.1.1.10
* Tors from downloading descriptors that they will throw away.
*/
if (ri && ri->cache_info.published_on > cutoff)
smartlist_add(descs_out, (void*) &(ri->cache_info));
}
} SMARTLIST_FOREACH_END(d);
SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
smartlist_free(digests);
} else {
*msg = "Key not recognized";
return -1;
}
if (!smartlist_len(descs_out)) {
*msg = "Servers unavailable";
return -1;
}
return 0;
}
/** Implementation helper for GETINFO: knows the answers for questions about
* directory information. */
STATIC int
@ -590,7 +671,7 @@ getinfo_helper_dir(control_connection_t *control_conn,
int res;
char *cp;
tor_asprintf(&url, "/tor/%s", question+4);
res = dirserv_get_routerdescs(descs, url, &msg);
res = controller_get_routerdescs(descs, url, &msg);
if (res) {
log_warn(LD_CONTROL, "getinfo '%s': %s", question, msg);
smartlist_free(descs);

View File

@ -363,87 +363,6 @@ dirserv_get_routerdesc_spool(smartlist_t *spool_out,
return 0;
}
/** Add a signed_descriptor_t to <b>descs_out</b> for each router matching
* <b>key</b>. The key should be either
* - "/tor/server/authority" for our own routerinfo;
* - "/tor/server/all" for all the routerinfos we have, concatenated;
* - "/tor/server/fp/FP" where FP is a plus-separated sequence of
* hex identity digests; or
* - "/tor/server/d/D" where D is a plus-separated sequence
* of server descriptor digests, in hex.
*
* Return 0 if we found some matching descriptors, or -1 if we do not
* have any descriptors, no matching descriptors, or if we did not
* recognize the key (URL).
* If -1 is returned *<b>msg</b> will be set to an appropriate error
* message.
*
* XXXX rename this function. It's only called from the controller.
* XXXX in fact, refactor this function, merging as much as possible.
*/
int
dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
const char **msg)
{
*msg = NULL;
if (!strcmp(key, "/tor/server/all")) {
routerlist_t *rl = router_get_routerlist();
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
smartlist_add(descs_out, &(r->cache_info)));
} else if (!strcmp(key, "/tor/server/authority")) {
const routerinfo_t *ri = router_get_my_routerinfo();
if (ri)
smartlist_add(descs_out, (void*) &(ri->cache_info));
} else if (!strcmpstart(key, "/tor/server/d/")) {
smartlist_t *digests = smartlist_new();
key += strlen("/tor/server/d/");
dir_split_resource_into_fingerprints(key, digests, NULL,
DSR_HEX|DSR_SORT_UNIQ);
SMARTLIST_FOREACH(digests, const char *, d,
{
signed_descriptor_t *sd = router_get_by_descriptor_digest(d);
if (sd)
smartlist_add(descs_out,sd);
});
SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
smartlist_free(digests);
} else if (!strcmpstart(key, "/tor/server/fp/")) {
smartlist_t *digests = smartlist_new();
time_t cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
key += strlen("/tor/server/fp/");
dir_split_resource_into_fingerprints(key, digests, NULL,
DSR_HEX|DSR_SORT_UNIQ);
SMARTLIST_FOREACH_BEGIN(digests, const char *, d) {
if (router_digest_is_me(d)) {
/* calling router_get_my_routerinfo() to make sure it exists */
const routerinfo_t *ri = router_get_my_routerinfo();
if (ri)
smartlist_add(descs_out, (void*) &(ri->cache_info));
} else {
const routerinfo_t *ri = router_get_by_id_digest(d);
/* Don't actually serve a descriptor that everyone will think is
* expired. This is an (ugly) workaround to keep buggy 0.1.1.10
* Tors from downloading descriptors that they will throw away.
*/
if (ri && ri->cache_info.published_on > cutoff)
smartlist_add(descs_out, (void*) &(ri->cache_info));
}
} SMARTLIST_FOREACH_END(d);
SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
smartlist_free(digests);
} else {
*msg = "Key not recognized";
return -1;
}
if (!smartlist_len(descs_out)) {
*msg = "Servers unavailable";
return -1;
}
return 0;
}
/* ==========
* Spooling code.
* ========== */

View File

@ -101,8 +101,6 @@ int dirserv_get_routerdesc_spool(smartlist_t *spools_out, const char *key,
dir_spool_source_t source,
int conn_is_encrypted,
const char **msg_out);
int dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
const char **msg);
void dirserv_free_all(void);
void cached_dir_decref(cached_dir_t *d);