diff options
author | Roger Dingledine <arma@torproject.org> | 2016-03-07 19:29:05 -0500 |
---|---|---|
committer | Roger Dingledine <arma@torproject.org> | 2016-03-11 10:49:57 -0500 |
commit | edeba3d472e303a9a3263582d1e4c0fbcb5a28b8 (patch) | |
tree | 7fc0743c3ca4f8da4c1c07a6a32f432ec235f5bf | |
parent | dc500c8cb4dc7643230d160d1b458a0acdadd242 (diff) | |
download | tor-edeba3d472e303a9a3263582d1e4c0fbcb5a28b8.tar.gz tor-edeba3d472e303a9a3263582d1e4c0fbcb5a28b8.zip |
simplify rend_cache_store_status_t back to a boolean
it used to be a tri-state, but now it's just a bi-state, so we can
take out all the machinery like the enum.
-rw-r--r-- | src/or/directory.c | 20 | ||||
-rw-r--r-- | src/or/rendcache.c | 30 | ||||
-rw-r--r-- | src/or/rendcache.h | 20 | ||||
-rw-r--r-- | src/test/test_dir_handle_get.c | 2 | ||||
-rw-r--r-- | src/test/test_rendcache.c | 58 |
5 files changed, 58 insertions, 72 deletions
diff --git a/src/or/directory.c b/src/or/directory.c index 961e33424b..6f271b2b1f 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -2297,10 +2297,8 @@ connection_dir_client_reached_eof(dir_connection_t *conn) { rend_cache_entry_t *entry = NULL; - switch (rend_cache_store_v2_desc_as_client(body, - conn->requested_resource, conn->rend_data, - &entry)) { - case RCS_BADDESC: + if (rend_cache_store_v2_desc_as_client(body, + conn->requested_resource, conn->rend_data, &entry) < 0) { log_warn(LD_REND,"Fetching v2 rendezvous descriptor failed. " "Retrying at another directory."); /* We'll retry when connection_about_to_close_connection() @@ -2308,11 +2306,9 @@ connection_dir_client_reached_eof(dir_connection_t *conn) SEND_HS_DESC_FAILED_EVENT("BAD_DESC"); SEND_HS_DESC_FAILED_CONTENT(); break; - case RCS_OKAY: - default: - { + } else { char service_id[REND_SERVICE_ID_LEN_BASE32 + 1]; - /* Should never be NULL here for an OKAY returned code. */ + /* Should never be NULL here if we found the descriptor. */ tor_assert(entry); rend_get_service_id(entry->parsed->pk, service_id); @@ -2330,7 +2326,6 @@ connection_dir_client_reached_eof(dir_connection_t *conn) rend_client_desc_trynow(service_id); memwipe(service_id, 0, sizeof(service_id)); break; - } } break; } @@ -3426,15 +3421,12 @@ directory_handle_command_post(dir_connection_t *conn, const char *headers, /* Handle v2 rendezvous service publish request. */ if (connection_dir_is_encrypted(conn) && !strcmpstart(url,"/tor/rendezvous2/publish")) { - switch (rend_cache_store_v2_desc_as_dir(body)) { - case RCS_BADDESC: + if (rend_cache_store_v2_desc_as_dir(body) < 0) { log_warn(LD_REND, "Rejected v2 rend descriptor (length %d) from %s.", (int)body_len, conn->base_.address); write_http_status_line(conn, 400, "Invalid v2 service descriptor rejected"); - break; - case RCS_OKAY: - default: + } else { write_http_status_line(conn, 200, "Service descriptor (v2) stored"); log_info(LD_REND, "Handled v2 rendezvous descriptor post: accepted"); } diff --git a/src/or/rendcache.c b/src/or/rendcache.c index dad4b2b5ba..7c5844262b 100644 --- a/src/or/rendcache.c +++ b/src/or/rendcache.c @@ -622,9 +622,9 @@ rend_cache_lookup_v2_desc_as_dir(const char *desc_id, const char **desc) * If we have a newer descriptor with the same ID, ignore this one. * If we have an older descriptor with the same ID, replace it. * - * Return an appropriate rend_cache_store_status_t. + * Return 0 on success, or -1 if we couldn't parse any of them. */ -rend_cache_store_status_t +int rend_cache_store_v2_desc_as_dir(const char *desc) { const or_options_t *options = get_options(); @@ -717,11 +717,11 @@ rend_cache_store_v2_desc_as_dir(const char *desc) } if (!number_parsed) { log_info(LD_REND, "Could not parse any descriptor."); - return RCS_BADDESC; + return -1; } log_info(LD_REND, "Parsed %d and added %d descriptor%s.", number_parsed, number_stored, number_stored != 1 ? "s" : ""); - return RCS_OKAY; + return 0; } /** Parse the v2 service descriptor in <b>desc</b> and store it to the @@ -731,9 +731,9 @@ rend_cache_store_v2_desc_as_dir(const char *desc) * If we have a newer descriptor with the same ID, ignore this one. * If we have an older descriptor with the same ID, replace it. * -* Return an appropriate rend_cache_store_status_t. +* Return 0 on success, or -1 if we couldn't understand the descriptor. */ -rend_cache_store_status_t +int rend_cache_store_v2_desc_as_service(const char *desc) { rend_service_descriptor_t *parsed = NULL; @@ -744,7 +744,7 @@ rend_cache_store_v2_desc_as_service(const char *desc) const char *next_desc; char service_id[REND_SERVICE_ID_LEN_BASE32+1]; rend_cache_entry_t *e; - rend_cache_store_status_t retval = RCS_BADDESC; + int retval = -1; tor_assert(rend_cache_local_service); tor_assert(desc); @@ -787,10 +787,10 @@ rend_cache_store_v2_desc_as_service(const char *desc) rend_cache_increment_allocation(rend_cache_entry_allocation(e)); log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.", safe_str_client(service_id), (int)encoded_size); - return RCS_OKAY; + return 0; okay: - retval = RCS_OKAY; + retval = 0; err: rend_service_descriptor_free(parsed); @@ -811,10 +811,10 @@ rend_cache_store_v2_desc_as_service(const char *desc) * If the descriptor's descriptor ID doesn't match <b>desc_id_base32</b>, * reject it. * - * Return an appropriate rend_cache_store_status_t. If entry is not NULL, - * set it with the cache entry pointer of the descriptor. + * Return 0 on success, or -1 if we rejected the descriptor. + * If entry is not NULL, set it with the cache entry pointer of the descriptor. */ -rend_cache_store_status_t +int rend_cache_store_v2_desc_as_client(const char *desc, const char *desc_id_base32, const rend_data_t *rend_query, @@ -846,7 +846,7 @@ rend_cache_store_v2_desc_as_client(const char *desc, char service_id[REND_SERVICE_ID_LEN_BASE32+1]; char want_desc_id[DIGEST_LEN]; rend_cache_entry_t *e; - rend_cache_store_status_t retval = RCS_BADDESC; + int retval = -1; tor_assert(rend_cache); tor_assert(desc); tor_assert(desc_id_base32); @@ -993,13 +993,13 @@ rend_cache_store_v2_desc_as_client(const char *desc, if (entry) { *entry = e; } - return RCS_OKAY; + return 0; okay: if (entry) { *entry = e; } - retval = RCS_OKAY; + retval = 0; err: rend_service_descriptor_free(parsed); diff --git a/src/or/rendcache.h b/src/or/rendcache.h index 3eecd3b2a0..0e8b918753 100644 --- a/src/or/rendcache.h +++ b/src/or/rendcache.h @@ -64,19 +64,13 @@ int rend_cache_lookup_entry(const char *query, int version, int rend_cache_lookup_v2_desc_as_service(const char *query, rend_cache_entry_t **entry_out); 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}. */ -typedef enum { - RCS_BADDESC = -1, /**< This descriptor is no good. */ - RCS_OKAY = 0 /**< All worked as expected */ -} rend_cache_store_status_t; - -rend_cache_store_status_t rend_cache_store_v2_desc_as_dir(const char *desc); -rend_cache_store_status_t rend_cache_store_v2_desc_as_service( - const char *desc); -rend_cache_store_status_t rend_cache_store_v2_desc_as_client(const char *desc, - const char *desc_id_base32, - const rend_data_t *rend_query, - rend_cache_entry_t **entry); + +int rend_cache_store_v2_desc_as_dir(const char *desc); +int rend_cache_store_v2_desc_as_service(const char *desc); +int rend_cache_store_v2_desc_as_client(const char *desc, + const char *desc_id_base32, + const rend_data_t *rend_query, + rend_cache_entry_t **entry); size_t rend_cache_get_total_allocation(void); void rend_cache_intro_failure_note(rend_intro_point_failure_t failure, diff --git a/src/test/test_dir_handle_get.c b/src/test/test_dir_handle_get.c index c75b2b6632..d74aeb6110 100644 --- a/src/test/test_dir_handle_get.c +++ b/src/test/test_dir_handle_get.c @@ -427,7 +427,7 @@ test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data) generate_desc(RECENT_TIME, &desc_holder, &service_id, 3); tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder->desc_str), - OP_EQ, RCS_OKAY); + OP_EQ, 0); base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id, DIGEST_LEN); diff --git a/src/test/test_rendcache.c b/src/test/test_rendcache.c index 3ccfe5f9bc..d1b52649b2 100644 --- a/src/test/test_rendcache.c +++ b/src/test/test_rendcache.c @@ -91,7 +91,7 @@ test_rend_cache_lookup_entry(void *data) static void test_rend_cache_store_v2_desc_as_client(void *data) { - rend_cache_store_status_t ret; + int ret; rend_data_t *mock_rend_query; char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1]; rend_cache_entry_t *entry = NULL; @@ -112,7 +112,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) desc_id_base32, mock_rend_query, &entry); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); tt_assert(entry); tt_int_op(entry->len, OP_EQ, strlen(desc_holder->desc_str)); tt_str_op(entry->desc, OP_EQ, desc_holder->desc_str); @@ -124,7 +124,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) /* desc_holder->desc_str, */ /* "3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG", */ /* &mock_rend_query, NULL); */ - /* tt_int_op(ret, OP_EQ, RCS_BADDESC); */ + /* tt_int_op(ret, OP_EQ, -1); */ // Test bad base32 failure // This causes an assertion failure if we're running with assertions. @@ -132,13 +132,13 @@ test_rend_cache_store_v2_desc_as_client(void *data) #ifdef DISABLE_ASSERTS_IN_UNIT_TESTS ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, "!xqunszqnaolrrfmtzgaki7mxelgvkj", mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); #endif // Test invalid descriptor ret = rend_cache_store_v2_desc_as_client("invalid descriptor", "3xqunszqnaolrrfmtzgaki7mxelgvkje", mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); // TODO: it doesn't seem to be possible to test invalid service ID condition. // that means it is likely not possible to have that condition without @@ -152,7 +152,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); rend_cache_free_all(); rend_data_free(mock_rend_query); @@ -163,7 +163,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); desc_id_base32[0]--; rend_cache_free_all(); @@ -181,7 +181,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); rend_cache_free_all(); // Test too new descriptor (in the future) @@ -198,7 +198,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); rend_cache_free_all(); // Test when a descriptor is already in the cache @@ -217,12 +217,12 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, &entry); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); tt_assert(entry); rend_cache_free_all(); @@ -243,7 +243,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); rend_cache_free_all(); // Test successful run when we have REND_BASIC_AUTH but not cookie @@ -260,7 +260,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); rend_cache_free_all(); @@ -277,7 +277,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); rend_cache_free_all(); // Test when we have too many intro points @@ -293,7 +293,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); done: rend_encoded_v2_service_descriptor_free(desc_holder); @@ -305,7 +305,7 @@ test_rend_cache_store_v2_desc_as_client(void *data) static void test_rend_cache_store_v2_desc_as_client_with_different_time(void *data) { - rend_cache_store_status_t ret; + int ret; rend_data_t *mock_rend_query; char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1]; rend_service_descriptor_t *generated = NULL; @@ -350,7 +350,7 @@ test_rend_cache_store_v2_desc_as_client_with_different_time(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder_older->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); rend_cache_free_all(); @@ -361,7 +361,7 @@ test_rend_cache_store_v2_desc_as_client_with_different_time(void *data) ret = rend_cache_store_v2_desc_as_client(desc_holder_newer->desc_str, desc_id_base32, mock_rend_query, NULL); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); done: rend_encoded_v2_service_descriptor_free(desc_holder_newer); @@ -446,7 +446,7 @@ static void test_rend_cache_store_v2_desc_as_dir(void *data) { (void)data; - rend_cache_store_status_t ret; + int ret; rend_encoded_v2_service_descriptor_t *desc_holder = NULL; char *service_id = NULL; @@ -457,12 +457,12 @@ test_rend_cache_store_v2_desc_as_dir(void *data) // Test when we can't parse the descriptor mock_routerinfo = tor_malloc(sizeof(routerinfo_t)); ret = rend_cache_store_v2_desc_as_dir("unparseable"); - tt_int_op(ret, OP_EQ, RCS_BADDESC); + tt_int_op(ret, OP_EQ, -1); // Test when we have an old descriptor generate_desc(TIME_IN_THE_PAST, &desc_holder, &service_id, 3); ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); rend_encoded_v2_service_descriptor_free(desc_holder); tor_free(service_id); @@ -470,7 +470,7 @@ test_rend_cache_store_v2_desc_as_dir(void *data) // Test when we have a descriptor in the future generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3); ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); rend_encoded_v2_service_descriptor_free(desc_holder); tor_free(service_id); @@ -478,7 +478,7 @@ test_rend_cache_store_v2_desc_as_dir(void *data) // Test when two descriptors generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3); ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); rend_encoded_v2_service_descriptor_free(desc_holder); tor_free(service_id); @@ -488,7 +488,7 @@ test_rend_cache_store_v2_desc_as_dir(void *data) generate_desc(RECENT_TIME, &desc_holder, &service_id, 3); get_options_mutable()->HiddenServiceStatistics = 1; ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); done: NS_UNMOCK(router_get_my_routerinfo); @@ -503,7 +503,7 @@ test_rend_cache_store_v2_desc_as_dir_with_different_time(void *data) { (void)data; - rend_cache_store_status_t ret; + int ret; rend_service_descriptor_t *generated = NULL; smartlist_t *descs = smartlist_new(); time_t t; @@ -540,13 +540,13 @@ test_rend_cache_store_v2_desc_as_dir_with_different_time(void *data) mock_routerinfo = tor_malloc(sizeof(routerinfo_t)); rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str); ret = rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); // Test when we have an old descriptor stored rend_cache_purge(); rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str); ret = rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); done: NS_UNMOCK(router_get_my_routerinfo); @@ -566,7 +566,7 @@ test_rend_cache_store_v2_desc_as_dir_with_different_content(void *data) { (void)data; - rend_cache_store_status_t ret; + int ret; rend_service_descriptor_t *generated = NULL; smartlist_t *descs = smartlist_new(); time_t t; @@ -605,7 +605,7 @@ test_rend_cache_store_v2_desc_as_dir_with_different_content(void *data) mock_routerinfo = tor_malloc(sizeof(routerinfo_t)); rend_cache_store_v2_desc_as_dir(desc_holder_one->desc_str); ret = rend_cache_store_v2_desc_as_dir(desc_holder_two->desc_str); - tt_int_op(ret, OP_EQ, RCS_OKAY); + tt_int_op(ret, OP_EQ, 0); done: NS_UNMOCK(router_get_my_routerinfo); |