Specify descriptor cache type in rend_cache_lookup_entry()

Adds an Enum which represents the different types of rendezvous
descriptor caches. This argument is passed in each call to
rend_cache_lookup_entry() to specify lookup in the client-side or
service-side descriptor caches.
This commit is contained in:
Donncha O'Cearbhaill 2015-08-03 17:33:28 +02:00
parent 580673cf94
commit 5dc2cbafef
5 changed files with 21 additions and 12 deletions

View File

@ -1527,7 +1527,8 @@ connection_ap_handshake_rewrite_and_attach(entry_connection_t *conn,
unsigned int refetch_desc = 0; unsigned int refetch_desc = 0;
rend_cache_entry_t *entry = NULL; rend_cache_entry_t *entry = NULL;
const int rend_cache_lookup_result = const int rend_cache_lookup_result =
rend_cache_lookup_entry(rend_data->onion_address, -1, &entry, 0); rend_cache_lookup_entry(rend_data->onion_address, -1, &entry,
REND_CACHE_TYPE_CLIENT);
if (rend_cache_lookup_result < 0) { if (rend_cache_lookup_result < 0) {
switch (-rend_cache_lookup_result) { switch (-rend_cache_lookup_result) {
case EINVAL: case EINVAL:

View File

@ -1920,7 +1920,7 @@ getinfo_helper_dir(control_connection_t *control_conn,
return -1; return -1;
} }
if (!rend_cache_lookup_entry(question, -1, &e, 0)) { if (!rend_cache_lookup_entry(question, -1, &e, REND_CACHE_TYPE_CLIENT)) {
/* Descriptor found in cache */ /* Descriptor found in cache */
*answer = tor_strdup(e->desc); *answer = tor_strdup(e->desc);
} else { } else {
@ -1936,7 +1936,7 @@ getinfo_helper_dir(control_connection_t *control_conn,
return -1; return -1;
} }
if (!rend_cache_lookup_entry(question, -1, &e, 1)) { if (!rend_cache_lookup_entry(question, -1, &e, REND_CACHE_TYPE_SERVICE)) {
/* Descriptor found in cache */ /* Descriptor found in cache */
*answer = tor_strdup(e->desc); *answer = tor_strdup(e->desc);
} else { } else {

View File

@ -479,7 +479,7 @@ rend_cache_clean_v2_descs_as_dir(time_t now, size_t force_remove)
* -ENOENT means that no entry in the cache was found. */ * -ENOENT means that no entry in the cache was found. */
int int
rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e, rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e,
int service) rend_cache_type_t cache)
{ {
int ret = 0; int ret = 0;
char key[REND_SERVICE_ID_LEN_BASE32 + 2]; /* <version><query>\0 */ char key[REND_SERVICE_ID_LEN_BASE32 + 2]; /* <version><query>\0 */
@ -502,7 +502,7 @@ rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e,
case 2: case 2:
/* Default is version 2. */ /* Default is version 2. */
default: default:
if(service){ if(cache == REND_CACHE_TYPE_SERVICE){
entry = strmap_get_lc(rend_cache_service, query); entry = strmap_get_lc(rend_cache_service, query);
} else { } else {
tor_snprintf(key, sizeof(key), "%d%s", default_version, query); tor_snprintf(key, sizeof(key), "%d%s", default_version, query);
@ -515,7 +515,7 @@ rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e,
goto end; goto end;
} }
/* Check descriptor is parsed only if lookup is from client cache */ /* Check descriptor is parsed only if lookup is from client cache */
if(!service){ if(cache == REND_CACHE_TYPE_CLIENT){
tor_assert(entry->parsed && entry->parsed->intro_nodes); tor_assert(entry->parsed && entry->parsed->intro_nodes);
} }

View File

@ -48,6 +48,11 @@ typedef struct rend_cache_failure_t {
digestmap_t *intro_failures; digestmap_t *intro_failures;
} rend_cache_failure_t; } rend_cache_failure_t;
typedef enum {
REND_CACHE_TYPE_CLIENT = 1,
REND_CACHE_TYPE_SERVICE = 2,
} rend_cache_type_t;
void rend_cache_init(void); void rend_cache_init(void);
void rend_cache_clean(time_t now); void rend_cache_clean(time_t now);
void rend_cache_failure_clean(time_t now); void rend_cache_failure_clean(time_t now);
@ -56,7 +61,7 @@ void rend_cache_purge(void);
void rend_cache_free_all(void); void rend_cache_free_all(void);
int rend_cache_lookup_entry(const char *query, int version, int rend_cache_lookup_entry(const char *query, int version,
rend_cache_entry_t **entry_out, rend_cache_entry_t **entry_out,
int service); rend_cache_type_t cache);
int rend_cache_lookup_v2_desc_as_dir(const char *query, const char **desc); int rend_cache_lookup_v2_desc_as_dir(const char *query, const char **desc);
/** Return value from rend_cache_store_v2_desc_as_{dir,client}. */ /** Return value from rend_cache_store_v2_desc_as_{dir,client}. */
typedef enum { typedef enum {

View File

@ -160,7 +160,7 @@ rend_client_send_introduction(origin_circuit_t *introcirc,
#endif #endif
r = rend_cache_lookup_entry(introcirc->rend_data->onion_address, -1, r = rend_cache_lookup_entry(introcirc->rend_data->onion_address, -1,
&entry, 0); &entry, REND_CACHE_TYPE_CLIENT);
/* An invalid onion address is not possible else we have a big issue. */ /* An invalid onion address is not possible else we have a big issue. */
tor_assert(r != -EINVAL); tor_assert(r != -EINVAL);
if (r < 0 || !rend_client_any_intro_points_usable(entry)) { if (r < 0 || !rend_client_any_intro_points_usable(entry)) {
@ -906,7 +906,8 @@ rend_client_refetch_v2_renddesc(rend_data_t *rend_query)
return; return;
} }
/* Before fetching, check if we already have a usable descriptor here. */ /* Before fetching, check if we already have a usable descriptor here. */
if (rend_cache_lookup_entry(rend_query->onion_address, -1, &e, 0) == 0 && if (rend_cache_lookup_entry(rend_query->onion_address, -1, &e,
REND_CACHE_TYPE_CLIENT) == 0 &&
rend_client_any_intro_points_usable(e)) { rend_client_any_intro_points_usable(e)) {
log_info(LD_REND, "We would fetch a v2 rendezvous descriptor, but we " log_info(LD_REND, "We would fetch a v2 rendezvous descriptor, but we "
"already have a usable descriptor here. Not fetching."); "already have a usable descriptor here. Not fetching.");
@ -987,7 +988,8 @@ rend_client_report_intro_point_failure(extend_info_t *failed_intro,
rend_cache_entry_t *ent; rend_cache_entry_t *ent;
connection_t *conn; connection_t *conn;
r = rend_cache_lookup_entry(rend_query->onion_address, -1, &ent, 0); r = rend_cache_lookup_entry(rend_query->onion_address, -1, &ent,
REND_CACHE_TYPE_CLIENT);
if (r < 0) { if (r < 0) {
/* Either invalid onion address or cache entry not found. */ /* Either invalid onion address or cache entry not found. */
switch (-r) { switch (-r) {
@ -1213,7 +1215,7 @@ rend_client_desc_trynow(const char *query)
continue; continue;
assert_connection_ok(base_conn, now); assert_connection_ok(base_conn, now);
if (rend_cache_lookup_entry(rend_data->onion_address, -1, if (rend_cache_lookup_entry(rend_data->onion_address, -1,
&entry, 0) == 0 && &entry, REND_CACHE_TYPE_CLIENT) == 0 &&
rend_client_any_intro_points_usable(entry)) { rend_client_any_intro_points_usable(entry)) {
/* either this fetch worked, or it failed but there was a /* either this fetch worked, or it failed but there was a
* valid entry from before which we should reuse */ * valid entry from before which we should reuse */
@ -1295,7 +1297,8 @@ rend_client_get_random_intro(const rend_data_t *rend_query)
extend_info_t *result; extend_info_t *result;
rend_cache_entry_t *entry; rend_cache_entry_t *entry;
ret = rend_cache_lookup_entry(rend_query->onion_address, -1, &entry, 0); ret = rend_cache_lookup_entry(rend_query->onion_address, -1, &entry,
REND_CACHE_TYPE_CLIENT);
if (ret < 0 || !rend_client_any_intro_points_usable(entry)) { if (ret < 0 || !rend_client_any_intro_points_usable(entry)) {
log_warn(LD_REND, log_warn(LD_REND,
"Query '%s' didn't have valid rend desc in cache. Failing.", "Query '%s' didn't have valid rend desc in cache. Failing.",