Bump copyright date to 2019
[tor.git] / src / feature / dirauth / voteflags.c
blob4f7593a3e1b6d13feedc288e75c33ca5deb56900
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file voteflags.c
8 * \brief Authority code for deciding the performance thresholds for flags,
9 * and assigning flags to routers.
10 **/
12 #define VOTEFLAGS_PRIVATE
13 #include "core/or/or.h"
14 #include "feature/dirauth/voteflags.h"
16 #include "app/config/config.h"
17 #include "core/mainloop/mainloop.h"
18 #include "core/or/policies.h"
19 #include "feature/dirauth/bwauth.h"
20 #include "feature/dirauth/reachability.h"
21 #include "feature/hibernate/hibernate.h"
22 #include "feature/nodelist/dirlist.h"
23 #include "feature/nodelist/networkstatus.h"
24 #include "feature/nodelist/nodelist.h"
25 #include "feature/nodelist/routerlist.h"
26 #include "feature/nodelist/routerset.h"
27 #include "feature/relay/router.h"
28 #include "feature/stats/rephist.h"
30 #include "feature/nodelist/node_st.h"
31 #include "feature/nodelist/routerinfo_st.h"
32 #include "feature/nodelist/vote_routerstatus_st.h"
34 #include "lib/container/order.h"
36 /** If a router's uptime is at least this value, then it is always
37 * considered stable, regardless of the rest of the network. This
38 * way we resist attacks where an attacker doubles the size of the
39 * network using allegedly high-uptime nodes, displacing all the
40 * current guards. */
41 #define UPTIME_TO_GUARANTEE_STABLE (3600*24*30)
42 /** If a router's MTBF is at least this value, then it is always stable.
43 * See above. (Corresponds to about 7 days for current decay rates.) */
44 #define MTBF_TO_GUARANTEE_STABLE (60*60*24*5)
45 /** Similarly, every node with at least this much weighted time known can be
46 * considered familiar enough to be a guard. Corresponds to about 20 days for
47 * current decay rates.
49 #define TIME_KNOWN_TO_GUARANTEE_FAMILIAR (8*24*60*60)
50 /** Similarly, every node with sufficient WFU is around enough to be a guard.
52 #define WFU_TO_GUARANTEE_GUARD (0.98)
54 /* Thresholds for server performance: set by
55 * dirserv_compute_performance_thresholds, and used by
56 * generate_v2_networkstatus */
58 /** Any router with an uptime of at least this value is stable. */
59 static uint32_t stable_uptime = 0; /* start at a safe value */
60 /** Any router with an mtbf of at least this value is stable. */
61 static double stable_mtbf = 0.0;
62 /** If true, we have measured enough mtbf info to look at stable_mtbf rather
63 * than stable_uptime. */
64 static int enough_mtbf_info = 0;
65 /** Any router with a weighted fractional uptime of at least this much might
66 * be good as a guard. */
67 static double guard_wfu = 0.0;
68 /** Don't call a router a guard unless we've known about it for at least this
69 * many seconds. */
70 static long guard_tk = 0;
71 /** Any router with a bandwidth at least this high is "Fast" */
72 static uint32_t fast_bandwidth_kb = 0;
73 /** If exits can be guards, then all guards must have a bandwidth this
74 * high. */
75 static uint32_t guard_bandwidth_including_exits_kb = 0;
76 /** If exits can't be guards, then all guards must have a bandwidth this
77 * high. */
78 static uint32_t guard_bandwidth_excluding_exits_kb = 0;
80 /** Helper: estimate the uptime of a router given its stated uptime and the
81 * amount of time since it last stated its stated uptime. */
82 static inline long
83 real_uptime(const routerinfo_t *router, time_t now)
85 if (now < router->cache_info.published_on)
86 return router->uptime;
87 else
88 return router->uptime + (now - router->cache_info.published_on);
91 /** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
92 * If <b>need_uptime</b> is non-zero, we require a minimum uptime.
93 * If <b>need_capacity</b> is non-zero, we require a minimum advertised
94 * bandwidth.
96 static int
97 dirserv_thinks_router_is_unreliable(time_t now,
98 const routerinfo_t *router,
99 int need_uptime, int need_capacity)
101 if (need_uptime) {
102 if (!enough_mtbf_info) {
103 /* XXXX We should change the rule from
104 * "use uptime if we don't have mtbf data" to "don't advertise Stable on
105 * v3 if we don't have enough mtbf data." Or maybe not, since if we ever
106 * hit a point where we need to reset a lot of authorities at once,
107 * none of them would be in a position to declare Stable.
109 long uptime = real_uptime(router, now);
110 if ((unsigned)uptime < stable_uptime &&
111 (unsigned)uptime < UPTIME_TO_GUARANTEE_STABLE)
112 return 1;
113 } else {
114 double mtbf =
115 rep_hist_get_stability(router->cache_info.identity_digest, now);
116 if (mtbf < stable_mtbf &&
117 mtbf < MTBF_TO_GUARANTEE_STABLE)
118 return 1;
121 if (need_capacity) {
122 uint32_t bw_kb = dirserv_get_credible_bandwidth_kb(router);
123 if (bw_kb < fast_bandwidth_kb)
124 return 1;
126 return 0;
129 /** Return 1 if <b>ri</b>'s descriptor is "active" -- running, valid,
130 * not hibernating, having observed bw greater 0, and not too old. Else
131 * return 0.
133 static int
134 router_is_active(const routerinfo_t *ri, const node_t *node, time_t now)
136 time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
137 if (ri->cache_info.published_on < cutoff) {
138 return 0;
140 if (!node->is_running || !node->is_valid || ri->is_hibernating) {
141 return 0;
143 /* Only require bandwidth capacity in non-test networks, or
144 * if TestingTorNetwork, and TestingMinExitFlagThreshold is non-zero */
145 if (!ri->bandwidthcapacity) {
146 if (get_options()->TestingTorNetwork) {
147 if (get_options()->TestingMinExitFlagThreshold > 0) {
148 /* If we're in a TestingTorNetwork, and TestingMinExitFlagThreshold is,
149 * then require bandwidthcapacity */
150 return 0;
152 } else {
153 /* If we're not in a TestingTorNetwork, then require bandwidthcapacity */
154 return 0;
157 return 1;
160 /** Return true iff <b>router</b> should be assigned the "HSDir" flag.
162 * Right now this means it advertises support for it, it has a high uptime,
163 * it's a directory cache, it has the Stable and Fast flags, and it's currently
164 * considered Running.
166 * This function needs to be called after router-\>is_running has
167 * been set.
169 static int
170 dirserv_thinks_router_is_hs_dir(const routerinfo_t *router,
171 const node_t *node, time_t now)
174 long uptime;
176 /* If we haven't been running for at least
177 * get_options()->MinUptimeHidServDirectoryV2 seconds, we can't
178 * have accurate data telling us a relay has been up for at least
179 * that long. We also want to allow a bit of slack: Reachability
180 * tests aren't instant. If we haven't been running long enough,
181 * trust the relay. */
183 if (get_uptime() >
184 get_options()->MinUptimeHidServDirectoryV2 * 1.1)
185 uptime = MIN(rep_hist_get_uptime(router->cache_info.identity_digest, now),
186 real_uptime(router, now));
187 else
188 uptime = real_uptime(router, now);
190 return (router->wants_to_be_hs_dir &&
191 router->supports_tunnelled_dir_requests &&
192 node->is_stable && node->is_fast &&
193 uptime >= get_options()->MinUptimeHidServDirectoryV2 &&
194 router_is_active(router, node, now));
197 /** Don't consider routers with less bandwidth than this when computing
198 * thresholds. */
199 #define ABSOLUTE_MIN_BW_VALUE_TO_CONSIDER_KB 4
201 /** Helper for dirserv_compute_performance_thresholds(): Decide whether to
202 * include a router in our calculations, and return true iff we should; the
203 * require_mbw parameter is passed in by
204 * dirserv_compute_performance_thresholds() and controls whether we ever
205 * count routers with only advertised bandwidths */
206 static int
207 router_counts_toward_thresholds(const node_t *node, time_t now,
208 const digestmap_t *omit_as_sybil,
209 int require_mbw)
211 /* Have measured bw? */
212 int have_mbw =
213 dirserv_has_measured_bw(node->identity);
214 uint64_t min_bw_kb = ABSOLUTE_MIN_BW_VALUE_TO_CONSIDER_KB;
215 const or_options_t *options = get_options();
217 if (options->TestingTorNetwork) {
218 min_bw_kb = (int64_t)options->TestingMinExitFlagThreshold / 1000;
221 return node->ri && router_is_active(node->ri, node, now) &&
222 !digestmap_get(omit_as_sybil, node->identity) &&
223 (dirserv_get_credible_bandwidth_kb(node->ri) >= min_bw_kb) &&
224 (have_mbw || !require_mbw);
227 /** Look through the routerlist, the Mean Time Between Failure history, and
228 * the Weighted Fractional Uptime history, and use them to set thresholds for
229 * the Stable, Fast, and Guard flags. Update the fields stable_uptime,
230 * stable_mtbf, enough_mtbf_info, guard_wfu, guard_tk, fast_bandwidth,
231 * guard_bandwidth_including_exits, and guard_bandwidth_excluding_exits.
233 * Also, set the is_exit flag of each router appropriately. */
234 void
235 dirserv_compute_performance_thresholds(digestmap_t *omit_as_sybil)
237 int n_active, n_active_nonexit, n_familiar;
238 uint32_t *uptimes, *bandwidths_kb, *bandwidths_excluding_exits_kb;
239 long *tks;
240 double *mtbfs, *wfus;
241 smartlist_t *nodelist;
242 time_t now = time(NULL);
243 const or_options_t *options = get_options();
245 /* Require mbw? */
246 int require_mbw =
247 (dirserv_get_last_n_measured_bws() >
248 options->MinMeasuredBWsForAuthToIgnoreAdvertised) ? 1 : 0;
250 /* initialize these all here, in case there are no routers */
251 stable_uptime = 0;
252 stable_mtbf = 0;
253 fast_bandwidth_kb = 0;
254 guard_bandwidth_including_exits_kb = 0;
255 guard_bandwidth_excluding_exits_kb = 0;
256 guard_tk = 0;
257 guard_wfu = 0;
259 nodelist_assert_ok();
260 nodelist = nodelist_get_list();
262 /* Initialize arrays that will hold values for each router. We'll
263 * sort them and use that to compute thresholds. */
264 n_active = n_active_nonexit = 0;
265 /* Uptime for every active router. */
266 uptimes = tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
267 /* Bandwidth for every active router. */
268 bandwidths_kb = tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
269 /* Bandwidth for every active non-exit router. */
270 bandwidths_excluding_exits_kb =
271 tor_calloc(smartlist_len(nodelist), sizeof(uint32_t));
272 /* Weighted mean time between failure for each active router. */
273 mtbfs = tor_calloc(smartlist_len(nodelist), sizeof(double));
274 /* Time-known for each active router. */
275 tks = tor_calloc(smartlist_len(nodelist), sizeof(long));
276 /* Weighted fractional uptime for each active router. */
277 wfus = tor_calloc(smartlist_len(nodelist), sizeof(double));
279 /* Now, fill in the arrays. */
280 SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
281 if (options->BridgeAuthoritativeDir &&
282 node->ri &&
283 node->ri->purpose != ROUTER_PURPOSE_BRIDGE)
284 continue;
286 routerinfo_t *ri = node->ri;
287 if (ri) {
288 node->is_exit = (!router_exit_policy_rejects_all(ri) &&
289 exit_policy_is_general_exit(ri->exit_policy));
292 if (router_counts_toward_thresholds(node, now, omit_as_sybil,
293 require_mbw)) {
294 const char *id = node->identity;
295 uint32_t bw_kb;
297 /* resolve spurious clang shallow analysis null pointer errors */
298 tor_assert(ri);
300 uptimes[n_active] = (uint32_t)real_uptime(ri, now);
301 mtbfs[n_active] = rep_hist_get_stability(id, now);
302 tks [n_active] = rep_hist_get_weighted_time_known(id, now);
303 bandwidths_kb[n_active] = bw_kb = dirserv_get_credible_bandwidth_kb(ri);
304 if (!node->is_exit || node->is_bad_exit) {
305 bandwidths_excluding_exits_kb[n_active_nonexit] = bw_kb;
306 ++n_active_nonexit;
308 ++n_active;
310 } SMARTLIST_FOREACH_END(node);
312 /* Now, compute thresholds. */
313 if (n_active) {
314 /* The median uptime is stable. */
315 stable_uptime = median_uint32(uptimes, n_active);
316 /* The median mtbf is stable, if we have enough mtbf info */
317 stable_mtbf = median_double(mtbfs, n_active);
318 /* The 12.5th percentile bandwidth is fast. */
319 fast_bandwidth_kb = find_nth_uint32(bandwidths_kb, n_active, n_active/8);
320 /* (Now bandwidths is sorted.) */
321 if (fast_bandwidth_kb < RELAY_REQUIRED_MIN_BANDWIDTH/(2 * 1000))
322 fast_bandwidth_kb = bandwidths_kb[n_active/4];
323 guard_bandwidth_including_exits_kb =
324 third_quartile_uint32(bandwidths_kb, n_active);
325 guard_tk = find_nth_long(tks, n_active, n_active/8);
328 if (guard_tk > TIME_KNOWN_TO_GUARANTEE_FAMILIAR)
329 guard_tk = TIME_KNOWN_TO_GUARANTEE_FAMILIAR;
332 /* We can vote on a parameter for the minimum and maximum. */
333 #define ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG 4
334 int32_t min_fast_kb, max_fast_kb, min_fast, max_fast;
335 min_fast = networkstatus_get_param(NULL, "FastFlagMinThreshold",
336 ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG,
337 ABSOLUTE_MIN_VALUE_FOR_FAST_FLAG,
338 INT32_MAX);
339 if (options->TestingTorNetwork) {
340 min_fast = (int32_t)options->TestingMinFastFlagThreshold;
342 max_fast = networkstatus_get_param(NULL, "FastFlagMaxThreshold",
343 INT32_MAX, min_fast, INT32_MAX);
344 min_fast_kb = min_fast / 1000;
345 max_fast_kb = max_fast / 1000;
347 if (fast_bandwidth_kb < (uint32_t)min_fast_kb)
348 fast_bandwidth_kb = min_fast_kb;
349 if (fast_bandwidth_kb > (uint32_t)max_fast_kb)
350 fast_bandwidth_kb = max_fast_kb;
352 /* Protect sufficiently fast nodes from being pushed out of the set
353 * of Fast nodes. */
354 if (options->AuthDirFastGuarantee &&
355 fast_bandwidth_kb > options->AuthDirFastGuarantee/1000)
356 fast_bandwidth_kb = (uint32_t)options->AuthDirFastGuarantee/1000;
358 /* Now that we have a time-known that 7/8 routers are known longer than,
359 * fill wfus with the wfu of every such "familiar" router. */
360 n_familiar = 0;
362 SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
363 if (router_counts_toward_thresholds(node, now,
364 omit_as_sybil, require_mbw)) {
365 routerinfo_t *ri = node->ri;
366 const char *id = ri->cache_info.identity_digest;
367 long tk = rep_hist_get_weighted_time_known(id, now);
368 if (tk < guard_tk)
369 continue;
370 wfus[n_familiar++] = rep_hist_get_weighted_fractional_uptime(id, now);
372 } SMARTLIST_FOREACH_END(node);
373 if (n_familiar)
374 guard_wfu = median_double(wfus, n_familiar);
375 if (guard_wfu > WFU_TO_GUARANTEE_GUARD)
376 guard_wfu = WFU_TO_GUARANTEE_GUARD;
378 enough_mtbf_info = rep_hist_have_measured_enough_stability();
380 if (n_active_nonexit) {
381 guard_bandwidth_excluding_exits_kb =
382 find_nth_uint32(bandwidths_excluding_exits_kb,
383 n_active_nonexit, n_active_nonexit*3/4);
386 log_info(LD_DIRSERV,
387 "Cutoffs: For Stable, %lu sec uptime, %lu sec MTBF. "
388 "For Fast: %lu kilobytes/sec. "
389 "For Guard: WFU %.03f%%, time-known %lu sec, "
390 "and bandwidth %lu or %lu kilobytes/sec. "
391 "We%s have enough stability data.",
392 (unsigned long)stable_uptime,
393 (unsigned long)stable_mtbf,
394 (unsigned long)fast_bandwidth_kb,
395 guard_wfu*100,
396 (unsigned long)guard_tk,
397 (unsigned long)guard_bandwidth_including_exits_kb,
398 (unsigned long)guard_bandwidth_excluding_exits_kb,
399 enough_mtbf_info ? "" : " don't");
401 tor_free(uptimes);
402 tor_free(mtbfs);
403 tor_free(bandwidths_kb);
404 tor_free(bandwidths_excluding_exits_kb);
405 tor_free(tks);
406 tor_free(wfus);
409 /* Use dirserv_compute_performance_thresholds() to compute the thresholds
410 * for the status flags, specifically for bridges.
412 * This is only called by a Bridge Authority from
413 * networkstatus_getinfo_by_purpose().
415 void
416 dirserv_compute_bridge_flag_thresholds(void)
418 digestmap_t *omit_as_sybil = digestmap_new();
419 dirserv_compute_performance_thresholds(omit_as_sybil);
420 digestmap_free(omit_as_sybil, NULL);
423 /** Give a statement of our current performance thresholds for inclusion
424 * in a vote document. */
425 char *
426 dirserv_get_flag_thresholds_line(void)
428 char *result=NULL;
429 const int measured_threshold =
430 get_options()->MinMeasuredBWsForAuthToIgnoreAdvertised;
431 const int enough_measured_bw =
432 dirserv_get_last_n_measured_bws() > measured_threshold;
434 tor_asprintf(&result,
435 "stable-uptime=%lu stable-mtbf=%lu "
436 "fast-speed=%lu "
437 "guard-wfu=%.03f%% guard-tk=%lu "
438 "guard-bw-inc-exits=%lu guard-bw-exc-exits=%lu "
439 "enough-mtbf=%d ignoring-advertised-bws=%d",
440 (unsigned long)stable_uptime,
441 (unsigned long)stable_mtbf,
442 (unsigned long)fast_bandwidth_kb*1000,
443 guard_wfu*100,
444 (unsigned long)guard_tk,
445 (unsigned long)guard_bandwidth_including_exits_kb*1000,
446 (unsigned long)guard_bandwidth_excluding_exits_kb*1000,
447 enough_mtbf_info ? 1 : 0,
448 enough_measured_bw ? 1 : 0);
450 return result;
453 /* DOCDOC running_long_enough_to_decide_unreachable */
455 running_long_enough_to_decide_unreachable(void)
457 return time_of_process_start
458 + get_options()->TestingAuthDirTimeToLearnReachability < approx_time();
461 /** Each server needs to have passed a reachability test no more
462 * than this number of seconds ago, or it is listed as down in
463 * the directory. */
464 #define REACHABLE_TIMEOUT (45*60)
466 /** If we tested a router and found it reachable _at least this long_ after it
467 * declared itself hibernating, it is probably done hibernating and we just
468 * missed a descriptor from it. */
469 #define HIBERNATION_PUBLICATION_SKEW (60*60)
471 /** Treat a router as alive if
472 * - It's me, and I'm not hibernating.
473 * or - We've found it reachable recently. */
474 void
475 dirserv_set_router_is_running(routerinfo_t *router, time_t now)
477 /*XXXX This function is a mess. Separate out the part that calculates
478 whether it's reachable and the part that tells rephist that the router was
479 unreachable.
481 int answer;
482 const or_options_t *options = get_options();
483 node_t *node = node_get_mutable_by_id(router->cache_info.identity_digest);
484 tor_assert(node);
486 if (router_is_me(router)) {
487 /* We always know if we are shutting down or hibernating ourselves. */
488 answer = ! we_are_hibernating();
489 } else if (router->is_hibernating &&
490 (router->cache_info.published_on +
491 HIBERNATION_PUBLICATION_SKEW) > node->last_reachable) {
492 /* A hibernating router is down unless we (somehow) had contact with it
493 * since it declared itself to be hibernating. */
494 answer = 0;
495 } else if (options->AssumeReachable) {
496 /* If AssumeReachable, everybody is up unless they say they are down! */
497 answer = 1;
498 } else {
499 /* Otherwise, a router counts as up if we found all announced OR
500 ports reachable in the last REACHABLE_TIMEOUT seconds.
502 XXX prop186 For now there's always one IPv4 and at most one
503 IPv6 OR port.
505 If we're not on IPv6, don't consider reachability of potential
506 IPv6 OR port since that'd kill all dual stack relays until a
507 majority of the dir auths have IPv6 connectivity. */
508 answer = (now < node->last_reachable + REACHABLE_TIMEOUT &&
509 (options->AuthDirHasIPv6Connectivity != 1 ||
510 tor_addr_is_null(&router->ipv6_addr) ||
511 now < node->last_reachable6 + REACHABLE_TIMEOUT));
514 if (!answer && running_long_enough_to_decide_unreachable()) {
515 /* Not considered reachable. tell rephist about that.
517 Because we launch a reachability test for each router every
518 REACHABILITY_TEST_CYCLE_PERIOD seconds, then the router has probably
519 been down since at least that time after we last successfully reached
522 XXX ipv6
524 time_t when = now;
525 if (node->last_reachable &&
526 node->last_reachable + REACHABILITY_TEST_CYCLE_PERIOD < now)
527 when = node->last_reachable + REACHABILITY_TEST_CYCLE_PERIOD;
528 rep_hist_note_router_unreachable(router->cache_info.identity_digest, when);
531 node->is_running = answer;
534 /** Extract status information from <b>ri</b> and from other authority
535 * functions and store it in <b>rs</b>. <b>rs</b> is zeroed out before it is
536 * set.
538 * We assume that ri-\>is_running has already been set, e.g. by
539 * dirserv_set_router_is_running(ri, now);
541 void
542 set_routerstatus_from_routerinfo(routerstatus_t *rs,
543 node_t *node,
544 const routerinfo_t *ri,
545 time_t now,
546 int listbadexits)
548 const or_options_t *options = get_options();
549 uint32_t routerbw_kb = dirserv_get_credible_bandwidth_kb(ri);
551 memset(rs, 0, sizeof(routerstatus_t));
553 rs->is_authority =
554 router_digest_is_trusted_dir(ri->cache_info.identity_digest);
556 /* Already set by compute_performance_thresholds. */
557 rs->is_exit = node->is_exit;
558 rs->is_stable = node->is_stable =
559 !dirserv_thinks_router_is_unreliable(now, ri, 1, 0);
560 rs->is_fast = node->is_fast =
561 !dirserv_thinks_router_is_unreliable(now, ri, 0, 1);
562 rs->is_flagged_running = node->is_running; /* computed above */
564 rs->is_valid = node->is_valid;
566 if (node->is_fast && node->is_stable &&
567 ri->supports_tunnelled_dir_requests &&
568 ((options->AuthDirGuardBWGuarantee &&
569 routerbw_kb >= options->AuthDirGuardBWGuarantee/1000) ||
570 routerbw_kb >= MIN(guard_bandwidth_including_exits_kb,
571 guard_bandwidth_excluding_exits_kb))) {
572 long tk = rep_hist_get_weighted_time_known(
573 node->identity, now);
574 double wfu = rep_hist_get_weighted_fractional_uptime(
575 node->identity, now);
576 rs->is_possible_guard = (wfu >= guard_wfu && tk >= guard_tk) ? 1 : 0;
577 } else {
578 rs->is_possible_guard = 0;
581 rs->is_bad_exit = listbadexits && node->is_bad_exit;
582 rs->is_hs_dir = node->is_hs_dir =
583 dirserv_thinks_router_is_hs_dir(ri, node, now);
585 rs->is_named = rs->is_unnamed = 0;
587 rs->published_on = ri->cache_info.published_on;
588 memcpy(rs->identity_digest, node->identity, DIGEST_LEN);
589 memcpy(rs->descriptor_digest, ri->cache_info.signed_descriptor_digest,
590 DIGEST_LEN);
591 rs->addr = ri->addr;
592 strlcpy(rs->nickname, ri->nickname, sizeof(rs->nickname));
593 rs->or_port = ri->or_port;
594 rs->dir_port = ri->dir_port;
595 rs->is_v2_dir = ri->supports_tunnelled_dir_requests;
597 rs->is_staledesc =
598 (ri->cache_info.published_on + DESC_IS_STALE_INTERVAL) < now;
600 if (options->AuthDirHasIPv6Connectivity == 1 &&
601 !tor_addr_is_null(&ri->ipv6_addr) &&
602 node->last_reachable6 >= now - REACHABLE_TIMEOUT) {
603 /* We're configured as having IPv6 connectivity. There's an IPv6
604 OR port and it's reachable so copy it to the routerstatus. */
605 tor_addr_copy(&rs->ipv6_addr, &ri->ipv6_addr);
606 rs->ipv6_orport = ri->ipv6_orport;
607 } else {
608 tor_addr_make_null(&rs->ipv6_addr, AF_INET6);
609 rs->ipv6_orport = 0;
612 if (options->TestingTorNetwork) {
613 dirserv_set_routerstatus_testing(rs);
617 /** Use TestingDirAuthVoteExit, TestingDirAuthVoteGuard, and
618 * TestingDirAuthVoteHSDir to give out the Exit, Guard, and HSDir flags,
619 * respectively. But don't set the corresponding node flags.
620 * Should only be called if TestingTorNetwork is set. */
621 STATIC void
622 dirserv_set_routerstatus_testing(routerstatus_t *rs)
624 const or_options_t *options = get_options();
626 tor_assert(options->TestingTorNetwork);
628 if (routerset_contains_routerstatus(options->TestingDirAuthVoteExit,
629 rs, 0)) {
630 rs->is_exit = 1;
631 } else if (options->TestingDirAuthVoteExitIsStrict) {
632 rs->is_exit = 0;
635 if (routerset_contains_routerstatus(options->TestingDirAuthVoteGuard,
636 rs, 0)) {
637 rs->is_possible_guard = 1;
638 } else if (options->TestingDirAuthVoteGuardIsStrict) {
639 rs->is_possible_guard = 0;
642 if (routerset_contains_routerstatus(options->TestingDirAuthVoteHSDir,
643 rs, 0)) {
644 rs->is_hs_dir = 1;
645 } else if (options->TestingDirAuthVoteHSDirIsStrict) {
646 rs->is_hs_dir = 0;