commit 8362f8854aa3e36b766724226a3baec4d325c1c0 Merge: cd1cdae 4b15606 Author: Nick Mathewson nickm@torproject.org Date: Thu Apr 18 11:13:36 2013 -0400
Merge branch 'less_charbuf_rebased' into maint-0.2.4
Conflicts: src/or/dirserv.c src/or/dirserv.h src/test/test_dir.c
changes/less_charbuf_usage | 5 + src/common/crypto.c | 24 +++++ src/common/crypto.h | 4 + src/or/dirserv.c | 153 +++++++++++---------------- src/or/dirserv.h | 25 +---- src/or/dirvote.c | 200 ++++++++++++----------------------- src/or/networkstatus.c | 4 +- src/or/or.h | 3 +- src/or/router.c | 148 +++++++++++--------------- src/or/router.h | 4 +- src/or/routerparse.c | 72 +++++++++---- src/or/routerparse.h | 5 +- src/test/test_dir.c | 249 +++++++++++++++++++++++++++++++++++++------- 13 files changed, 485 insertions(+), 411 deletions(-)
diff --cc src/or/dirserv.c index e4533b7,173c8cd..3755720 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@@ -2434,13 -2191,12 +2412,13 @@@ routerstatus_format_entry(const routers log_warn(LD_BUG, "Cannot get any descriptor for %s " "(wanted descriptor %s).", id, dd); - return -1; + goto err; }
- /* This assert can fire for the control port, because + /* This assert could fire for the control port, because * it can request NS documents before all descriptors - * have been fetched. */ + * have been fetched. Therefore, we only do this test when + * format != NS_CONTROL_PORT. */ if (tor_memneq(desc->cache_info.signed_descriptor_digest, rs->descriptor_digest, DIGEST_LEN)) { @@@ -2464,29 -2220,19 +2442,19 @@@ }
if (format == NS_CONTROL_PORT && rs->has_bandwidth) { - bw = rs->bandwidth; + bw_kb = rs->bandwidth_kb; } else { tor_assert(desc); - bw = router_get_advertised_bandwidth_capped(desc) / 1000; + bw_kb = router_get_advertised_bandwidth_capped(desc) / 1000; } - r = tor_snprintf(cp, buf_len - (cp-buf), - "w Bandwidth=%d\n", bw_kb); + smartlist_add_asprintf(chunks, - "w Bandwidth=%d", bw); ++ "w Bandwidth=%d", bw_kb);
- if (r<0) { - log_warn(LD_BUG, "Not enough space in buffer."); - return -1; - } - cp += strlen(cp); if (format == NS_V3_VOTE && vrs && vrs->has_measured_bw) { - *--cp = '\0'; /* Kill "\n" */ - r = tor_snprintf(cp, buf_len - (cp-buf), - " Measured=%d\n", vrs->measured_bw_kb); - if (r<0) { - log_warn(LD_BUG, "Not enough space in buffer for weight line."); - return -1; - } - cp += strlen(cp); + smartlist_add_asprintf(chunks, - " Measured=%d", vrs->measured_bw); ++ " Measured=%d", vrs->measured_bw_kb); } + smartlist_add(chunks, tor_strdup("\n"));
if (desc) { summary = policy_summarize(desc->exit_policy, AF_INET); diff --cc src/or/dirserv.h index d6eb4ab,cc4ac2e..f9d36d7 --- a/src/or/dirserv.h +++ b/src/or/dirserv.h @@@ -145,15 -120,7 +123,16 @@@ int measured_bw_line_parse(measured_bw_
int measured_bw_line_apply(measured_bw_line_t *parsed_line, smartlist_t *routerstatuses); + +void dirserv_cache_measured_bw(const measured_bw_line_t *parsed_line, + time_t as_of); +void dirserv_clear_measured_bw_cache(void); +void dirserv_expire_measured_bw_cache(time_t now); +int dirserv_get_measured_bw_cache_size(void); +int dirserv_query_measured_bw_cache_kb(const char *node_id, long *bw_out, + time_t *as_of_out); +int dirserv_has_measured_bw(const char *node_id); + cached_dir_t *generate_v2_networkstatus_opinion(void); #endif
int dirserv_read_measured_bandwidths(const char *from_file, diff --cc src/test/test_dir.c index de34b5e,8e498e4..56ac3b3 --- a/src/test/test_dir.c +++ b/src/test/test_dir.c @@@ -2197,6 -2179,116 +2254,116 @@@ test_dir_clip_unmeasured_bw_kb_alt(void test_routerstatus_for_umbw); }
+ extern time_t time_of_process_start; /* from main.c */ + + static void + test_dir_v2_dir(void *arg) + { + /* Runs in a forked process: acts like a v2 directory just enough to make and + * sign a v2 networkstatus opinion */ + + cached_dir_t *v2 = NULL; + or_options_t *options = get_options_mutable(); + crypto_pk_t *id_key = pk_generate(4); + (void) arg; + + options->ORPort_set = 1; /* So we believe we're a server. */ + options->DirPort_set = 1; + options->Address = tor_strdup("99.99.99.99"); + options->Nickname = tor_strdup("TestV2Auth"); + options->ContactInfo = tor_strdup("TestV2Auth testv2auth@example.com"); + { + /* Give it a DirPort */ + smartlist_t *ports = (smartlist_t *)get_configured_ports(); + port_cfg_t *port = tor_malloc_zero(sizeof(port_cfg_t)); + port->type = CONN_TYPE_DIR_LISTENER; + port->port = 9999; + smartlist_add(ports, port); + } + set_server_identity_key(id_key); + set_client_identity_key(id_key); + + /* Add a router. */ + { + was_router_added_t wra; + const char *msg = NULL; + routerinfo_t *r1 = tor_malloc_zero(sizeof(routerinfo_t)); + r1->address = tor_strdup("18.244.0.1"); + r1->addr = 0xc0a80001u; /* 192.168.0.1 */ + r1->cache_info.published_on = time(NULL)-60; + r1->or_port = 9000; + r1->dir_port = 9003; + tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::"); + r1->ipv6_orport = 9999; + r1->onion_pkey = pk_generate(1); + r1->identity_pkey = pk_generate(2); + r1->bandwidthrate = 1000; + r1->bandwidthburst = 5000; + r1->bandwidthcapacity = 10000; + r1->exit_policy = NULL; + r1->nickname = tor_strdup("Magri"); + r1->platform = tor_strdup("Tor 0.2.7.7-gamma"); + r1->cache_info.routerlist_index = -1; + r1->cache_info.signed_descriptor_body = + router_dump_router_to_string(r1, r1->identity_pkey); + r1->cache_info.signed_descriptor_len = + strlen(r1->cache_info.signed_descriptor_body); + wra = router_add_to_routerlist(r1, &msg, 0, 0); + tt_int_op(wra, ==, ROUTER_ADDED_SUCCESSFULLY); + } + + /* Prevent call of rep_hist_note_router_unreachable(). */ + time_of_process_start = time(NULL); + + /* Make a directory so there's somewhere to store the thing */ + #ifdef _WIN32 + mkdir(get_fname("cached-status")); + #else + mkdir(get_fname("cached-status"), 0700); + #endif + + v2 = generate_v2_networkstatus_opinion(); + tt_assert(v2); + + done: + crypto_pk_free(id_key); + cached_dir_decref(v2); + } + + static void + test_dir_fmt_control_ns(void *arg) + { + char *s = NULL; + routerstatus_t rs; + (void)arg; + + memset(&rs, 0, sizeof(rs)); + rs.published_on = 1364925198; + strlcpy(rs.nickname, "TetsuoMilk", sizeof(rs.nickname)); + memcpy(rs.identity_digest, "Stately, plump Buck ", DIGEST_LEN); + memcpy(rs.descriptor_digest, "Mulligan came up fro", DIGEST_LEN); + rs.addr = 0x20304050; + rs.or_port = 9001; + rs.dir_port = 9002; + rs.is_exit = 1; + rs.is_fast = 1; + rs.is_flagged_running = 1; + rs.has_bandwidth = 1; - rs.bandwidth = 1000; ++ rs.bandwidth_kb = 1000; + + s = networkstatus_getinfo_helper_single(&rs); + tt_assert(s); + tt_str_op(s, ==, + "r TetsuoMilk U3RhdGVseSwgcGx1bXAgQnVjayA " + "TXVsbGlnYW4gY2FtZSB1cCBmcm8 2013-04-02 17:53:18 " + "32.48.64.80 9001 9002\n" + "s Exit Fast Running\n" + "w Bandwidth=1000\n"); + + done: + tor_free(s); + } + #define DIR_LEGACY(name) \ { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_dir_ ## name }
@@@ -2208,15 -2300,16 +2375,17 @@@ struct testcase_t dir_tests[] = DIR_LEGACY(formats), DIR_LEGACY(versions), DIR_LEGACY(fp_pairs), - DIR(split_fps), + DIR(split_fps, 0), - DIR_LEGACY(measured_bw), + DIR_LEGACY(measured_bw_kb), ++ DIR_LEGACY(measured_bw_kb_cache), DIR_LEGACY(param_voting), DIR_LEGACY(v3_networkstatus), - DIR(random_weighted), - DIR(scale_bw), + DIR(random_weighted, 0), + DIR(scale_bw, 0), - DIR_LEGACY(clip_unmeasured_bw), - DIR_LEGACY(clip_unmeasured_bw_alt), + DIR_LEGACY(clip_unmeasured_bw_kb), + DIR_LEGACY(clip_unmeasured_bw_kb_alt), - DIR_LEGACY(measured_bw_kb_cache), + DIR(v2_dir, TT_FORK), + DIR(fmt_control_ns, 0), END_OF_TESTCASES };
tor-commits@lists.torproject.org