Split the circuit timeout and close codepaths.
[tor.git] / src / or / circuitbuild.c
blobf244aeaff0f551b22f14de3ad4e6eb705e82dfaa
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2010, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file circuitbuild.c
9 * \brief The actual details of building circuits.
10 **/
12 #define CIRCUIT_PRIVATE
14 #include "or.h"
15 #include "crypto.h"
16 #undef log
17 #include <math.h>
19 #ifndef MIN
20 #define MIN(a,b) ((a)<(b)?(a):(b))
21 #endif
23 #define CBT_BIN_TO_MS(bin) ((bin)*CBT_BIN_WIDTH + (CBT_BIN_WIDTH/2))
25 /********* START VARIABLES **********/
26 /** Global list of circuit build times */
27 // FIXME: Add this as a member for entry_guard_t instead of global?
28 // Then we could do per-guard statistics, as guards are likely to
29 // vary in their own latency. The downside of this is that guards
30 // can change frequently, so we'd be building a lot more circuits
31 // most likely.
32 circuit_build_times_t circ_times;
34 /** A global list of all circuits at this hop. */
35 extern circuit_t *global_circuitlist;
37 /** An entry_guard_t represents our information about a chosen long-term
38 * first hop, known as a "helper" node in the literature. We can't just
39 * use a routerinfo_t, since we want to remember these even when we
40 * don't have a directory. */
41 typedef struct {
42 char nickname[MAX_NICKNAME_LEN+1];
43 char identity[DIGEST_LEN];
44 time_t chosen_on_date; /**< Approximately when was this guard added?
45 * "0" if we don't know. */
46 char *chosen_by_version; /**< What tor version added this guard? NULL
47 * if we don't know. */
48 unsigned int made_contact : 1; /**< 0 if we have never connected to this
49 * router, 1 if we have. */
50 unsigned int can_retry : 1; /**< Should we retry connecting to this entry,
51 * in spite of having it marked as unreachable?*/
52 time_t bad_since; /**< 0 if this guard is currently usable, or the time at
53 * which it was observed to become (according to the
54 * directory or the user configuration) unusable. */
55 time_t unreachable_since; /**< 0 if we can connect to this guard, or the
56 * time at which we first noticed we couldn't
57 * connect to it. */
58 time_t last_attempted; /**< 0 if we can connect to this guard, or the time
59 * at which we last failed to connect to it. */
60 } entry_guard_t;
62 /** A list of our chosen entry guards. */
63 static smartlist_t *entry_guards = NULL;
64 /** A value of 1 means that the entry_guards list has changed
65 * and those changes need to be flushed to disk. */
66 static int entry_guards_dirty = 0;
68 /** If set, we're running the unit tests: we should avoid clobbering
69 * our state file or accessing get_options() or get_or_state() */
70 static int unit_tests = 0;
72 /********* END VARIABLES ************/
74 static int circuit_deliver_create_cell(circuit_t *circ,
75 uint8_t cell_type, const char *payload);
76 static int onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit);
77 static crypt_path_t *onion_next_hop_in_cpath(crypt_path_t *cpath);
78 static int onion_extend_cpath(origin_circuit_t *circ);
79 static int count_acceptable_routers(smartlist_t *routers);
80 static int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
82 static void entry_guards_changed(void);
84 static int
85 circuit_build_times_disabled(void)
87 if (unit_tests) {
88 return 0;
89 } else {
90 int consensus_disabled = networkstatus_get_param(NULL, "cbtdisabled",
91 0);
92 int config_disabled = !get_options()->LearnCircuitBuildTimeout;
93 int dirauth_disabled = get_options()->AuthoritativeDir;
94 int state_disabled = (get_or_state()->LastWritten == -1);
96 if (consensus_disabled || config_disabled || dirauth_disabled ||
97 state_disabled) {
98 log_info(LD_CIRC,
99 "CircuitBuildTime learning is disabled. "
100 "Consensus=%d, Config=%d, AuthDir=%d, StateFile=%d",
101 consensus_disabled, config_disabled, dirauth_disabled,
102 state_disabled);
103 return 1;
104 } else {
105 return 0;
110 static int32_t
111 circuit_build_times_max_timeouts(void)
113 int32_t num = networkstatus_get_param(NULL, "cbtmaxtimeouts",
114 CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT);
115 return num;
118 static int32_t
119 circuit_build_times_default_num_xm_modes(void)
121 int32_t num = networkstatus_get_param(NULL, "cbtnummodes",
122 CBT_DEFAULT_NUM_XM_MODES);
123 return num;
126 static int32_t
127 circuit_build_times_min_circs_to_observe(void)
129 int32_t num = networkstatus_get_param(NULL, "cbtmincircs",
130 CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE);
131 return num;
134 double
135 circuit_build_times_quantile_cutoff(void)
137 int32_t num = networkstatus_get_param(NULL, "cbtquantile",
138 CBT_DEFAULT_QUANTILE_CUTOFF);
139 return num/100.0;
142 static double
143 circuit_build_times_close_quantile(void)
145 int32_t num = networkstatus_get_param(NULL, "cbtclosequantile",
146 CBT_DEFAULT_CLOSE_QUANTILE);
148 return num/100.0;
151 static int32_t
152 circuit_build_times_test_frequency(void)
154 int32_t num = networkstatus_get_param(NULL, "cbttestfreq",
155 CBT_DEFAULT_TEST_FREQUENCY);
156 return num;
159 static int32_t
160 circuit_build_times_min_timeout(void)
162 int32_t num = networkstatus_get_param(NULL, "cbtmintimeout",
163 CBT_DEFAULT_TIMEOUT_MIN_VALUE);
164 return num;
167 int32_t
168 circuit_build_times_initial_timeout(void)
170 int32_t num = networkstatus_get_param(NULL, "cbtinitialtimeout",
171 CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
172 return num;
175 static int32_t
176 circuit_build_times_recent_circuit_count(void)
178 int32_t num = networkstatus_get_param(NULL, "cbtrecentcount",
179 CBT_DEFAULT_RECENT_CIRCUITS);
180 return num;
184 * This function is called when we get a consensus update.
186 * It checks to see if we have changed any consensus parameters
187 * that require reallocation or discard of previous stats.
189 void
190 circuit_build_times_new_consensus_params(circuit_build_times_t *cbt,
191 networkstatus_t *ns)
193 int32_t num = networkstatus_get_param(ns, "cbtrecentcount",
194 CBT_DEFAULT_RECENT_CIRCUITS);
196 if (num != cbt->liveness.num_recent_circs) {
197 int8_t *recent_circs;
198 log_notice(LD_CIRC, "Changing recent timeout size from %d to %d",
199 cbt->liveness.num_recent_circs, num);
201 tor_assert(num > 0);
202 tor_assert(cbt->liveness.timeouts_after_firsthop);
205 * Technically this is a circular array that we are reallocating
206 * and memcopying. However, since it only consists of either 1s
207 * or 0s, and is only used in a statistical test to determine when
208 * we should discard our history after a sufficient number of 1's
209 * have been reached, it is fine if order is not preserved or
210 * elements are lost.
212 * cbtrecentcount should only be changing in cases of severe network
213 * distress anyway, so memory correctness here is paramount over
214 * doing acrobatics to preserve the array.
216 recent_circs = tor_malloc_zero(sizeof(int8_t)*num);
217 memcpy(recent_circs, cbt->liveness.timeouts_after_firsthop,
218 sizeof(int8_t)*MIN(num, cbt->liveness.num_recent_circs));
220 // Adjust the index if it needs it.
221 if (num < cbt->liveness.num_recent_circs) {
222 cbt->liveness.after_firsthop_idx = MIN(num-1,
223 cbt->liveness.after_firsthop_idx);
226 tor_free(cbt->liveness.timeouts_after_firsthop);
227 cbt->liveness.timeouts_after_firsthop = recent_circs;
228 cbt->liveness.num_recent_circs = num;
232 /** Make a note that we're running unit tests (rather than running Tor
233 * itself), so we avoid clobbering our state file. */
234 void
235 circuitbuild_running_unit_tests(void)
237 unit_tests = 1;
241 * Return the initial default or configured timeout in milliseconds
243 static double
244 circuit_build_times_get_initial_timeout(void)
246 double timeout;
247 if (!unit_tests && get_options()->CircuitBuildTimeout) {
248 timeout = get_options()->CircuitBuildTimeout*1000;
249 if (timeout < circuit_build_times_min_timeout()) {
250 log_warn(LD_CIRC, "Config CircuitBuildTimeout too low. Setting to %ds",
251 circuit_build_times_min_timeout()/1000);
252 timeout = circuit_build_times_min_timeout();
254 } else {
255 timeout = circuit_build_times_initial_timeout();
257 return timeout;
261 * Reset the build time state.
263 * Leave estimated parameters, timeout and network liveness intact
264 * for future use.
266 void
267 circuit_build_times_reset(circuit_build_times_t *cbt)
269 memset(cbt->circuit_build_times, 0, sizeof(cbt->circuit_build_times));
270 cbt->total_build_times = 0;
271 cbt->build_times_idx = 0;
272 cbt->have_computed_timeout = 0;
276 * Initialize the buildtimes structure for first use.
278 * Sets the initial timeout value based to either the
279 * config setting or BUILD_TIMEOUT_INITIAL_VALUE.
281 void
282 circuit_build_times_init(circuit_build_times_t *cbt)
284 memset(cbt, 0, sizeof(*cbt));
285 cbt->liveness.num_recent_circs = circuit_build_times_recent_circuit_count();
286 cbt->liveness.timeouts_after_firsthop = tor_malloc_zero(sizeof(int8_t)*
287 cbt->liveness.num_recent_circs);
288 cbt->close_ms = cbt->timeout_ms = circuit_build_times_get_initial_timeout();
289 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
293 * Rewind our build time history by n positions.
295 static void
296 circuit_build_times_rewind_history(circuit_build_times_t *cbt, int n)
298 int i = 0;
300 cbt->build_times_idx -= n;
301 cbt->build_times_idx %= CBT_NCIRCUITS_TO_OBSERVE;
303 for (i = 0; i < n; i++) {
304 cbt->circuit_build_times[(i+cbt->build_times_idx)
305 %CBT_NCIRCUITS_TO_OBSERVE]=0;
308 if (cbt->total_build_times > n) {
309 cbt->total_build_times -= n;
310 } else {
311 cbt->total_build_times = 0;
314 log_info(LD_CIRC,
315 "Rewound history by %d places. Current index: %d. "
316 "Total: %d", n, cbt->build_times_idx, cbt->total_build_times);
320 * Add a new build time value <b>time</b> to the set of build times. Time
321 * units are milliseconds.
323 * circuit_build_times <b>cbt</a> is a circular array, so loop around when
324 * array is full.
327 circuit_build_times_add_time(circuit_build_times_t *cbt, build_time_t time)
329 if (time <= 0 || time > CBT_BUILD_TIME_MAX) {
330 log_warn(LD_BUG, "Circuit build time is too large (%u)."
331 "This is probably a bug.", time);
332 tor_fragile_assert();
333 return -1;
336 log_debug(LD_CIRC, "Adding circuit build time %u", time);
338 cbt->circuit_build_times[cbt->build_times_idx] = time;
339 cbt->build_times_idx = (cbt->build_times_idx + 1) % CBT_NCIRCUITS_TO_OBSERVE;
340 if (cbt->total_build_times < CBT_NCIRCUITS_TO_OBSERVE)
341 cbt->total_build_times++;
343 if ((cbt->total_build_times % CBT_SAVE_STATE_EVERY) == 0) {
344 /* Save state every n circuit builds */
345 if (!unit_tests && !get_options()->AvoidDiskWrites)
346 or_state_mark_dirty(get_or_state(), 0);
349 return 0;
353 * Return maximum circuit build time
355 static build_time_t
356 circuit_build_times_max(circuit_build_times_t *cbt)
358 int i = 0;
359 build_time_t max_build_time = 0;
360 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
361 if (cbt->circuit_build_times[i] > max_build_time
362 && cbt->circuit_build_times[i] != CBT_BUILD_ABANDONED)
363 max_build_time = cbt->circuit_build_times[i];
365 return max_build_time;
368 #if 0
369 /** Return minimum circuit build time */
370 build_time_t
371 circuit_build_times_min(circuit_build_times_t *cbt)
373 int i = 0;
374 build_time_t min_build_time = CBT_BUILD_TIME_MAX;
375 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
376 if (cbt->circuit_build_times[i] && /* 0 <-> uninitialized */
377 cbt->circuit_build_times[i] < min_build_time)
378 min_build_time = cbt->circuit_build_times[i];
380 if (min_build_time == CBT_BUILD_TIME_MAX) {
381 log_warn(LD_CIRC, "No build times less than CBT_BUILD_TIME_MAX!");
383 return min_build_time;
385 #endif
388 * Calculate and return a histogram for the set of build times.
390 * Returns an allocated array of histrogram bins representing
391 * the frequency of index*CBT_BIN_WIDTH millisecond
392 * build times. Also outputs the number of bins in nbins.
394 * The return value must be freed by the caller.
396 static uint32_t *
397 circuit_build_times_create_histogram(circuit_build_times_t *cbt,
398 build_time_t *nbins)
400 uint32_t *histogram;
401 build_time_t max_build_time = circuit_build_times_max(cbt);
402 int i, c;
404 *nbins = 1 + (max_build_time / CBT_BIN_WIDTH);
405 histogram = tor_malloc_zero(*nbins * sizeof(build_time_t));
407 // calculate histogram
408 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
409 if (cbt->circuit_build_times[i] == 0
410 || cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED)
411 continue; /* 0 <-> uninitialized */
413 c = (cbt->circuit_build_times[i] / CBT_BIN_WIDTH);
414 histogram[c]++;
417 return histogram;
421 * Return the Pareto start-of-curve parameter Xm.
423 * Because we are not a true Pareto curve, we compute this as the
424 * weighted average of the N=3 most frequent build time bins.
426 static build_time_t
427 circuit_build_times_get_xm(circuit_build_times_t *cbt)
429 build_time_t i, nbins;
430 build_time_t *nth_max_bin;
431 int32_t bin_counts=0;
432 build_time_t ret = 0;
433 uint32_t *histogram = circuit_build_times_create_histogram(cbt, &nbins);
434 int n=0;
435 int num_modes = circuit_build_times_default_num_xm_modes();
437 // Only use one mode if < 1000 buildtimes. Not enough data
438 // for multiple.
439 if (cbt->total_build_times < CBT_NCIRCUITS_TO_OBSERVE)
440 num_modes = 1;
442 nth_max_bin = (build_time_t*)tor_malloc_zero(num_modes*sizeof(build_time_t));
444 for (i = 0; i < nbins; i++) {
445 if (histogram[i] >= histogram[nth_max_bin[0]]) {
446 nth_max_bin[0] = i;
449 for (n = 1; n < num_modes; n++) {
450 if (histogram[i] >= histogram[nth_max_bin[n]] &&
451 (!histogram[nth_max_bin[n-1]]
452 || histogram[i] < histogram[nth_max_bin[n-1]])) {
453 nth_max_bin[n] = i;
458 for (n = 0; n < num_modes; n++) {
459 bin_counts += histogram[nth_max_bin[n]];
460 ret += CBT_BIN_TO_MS(nth_max_bin[n])*histogram[nth_max_bin[n]];
461 log_info(LD_CIRC, "Xm mode #%d: %u %u", n, CBT_BIN_TO_MS(nth_max_bin[n]),
462 histogram[nth_max_bin[n]]);
465 ret /= bin_counts;
466 tor_free(histogram);
467 tor_free(nth_max_bin);
469 return ret;
473 * Output a histogram of current circuit build times to
474 * the or_state_t state structure.
476 void
477 circuit_build_times_update_state(circuit_build_times_t *cbt,
478 or_state_t *state)
480 uint32_t *histogram;
481 build_time_t i = 0;
482 build_time_t nbins = 0;
483 config_line_t **next, *line;
485 histogram = circuit_build_times_create_histogram(cbt, &nbins);
486 // write to state
487 config_free_lines(state->BuildtimeHistogram);
488 next = &state->BuildtimeHistogram;
489 *next = NULL;
491 state->TotalBuildTimes = cbt->total_build_times;
492 state->CircuitBuildAbandonedCount = 0;
494 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
495 if (cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED)
496 state->CircuitBuildAbandonedCount++;
499 for (i = 0; i < nbins; i++) {
500 // compress the histogram by skipping the blanks
501 if (histogram[i] == 0) continue;
502 *next = line = tor_malloc_zero(sizeof(config_line_t));
503 line->key = tor_strdup("CircuitBuildTimeBin");
504 line->value = tor_malloc(25);
505 tor_snprintf(line->value, 25, "%d %d",
506 CBT_BIN_TO_MS(i), histogram[i]);
507 next = &(line->next);
510 if (!unit_tests) {
511 if (!get_options()->AvoidDiskWrites)
512 or_state_mark_dirty(get_or_state(), 0);
515 tor_free(histogram);
519 * Shuffle the build times array.
521 * Stolen from http://en.wikipedia.org/wiki/Fisher\u2013Yates_shuffle
523 static void
524 circuit_build_times_shuffle_and_store_array(circuit_build_times_t *cbt,
525 build_time_t *raw_times,
526 int num_times)
528 int n = num_times;
529 if (num_times > CBT_NCIRCUITS_TO_OBSERVE) {
530 log_notice(LD_CIRC, "Decreasing circuit_build_times size from %d to %d",
531 num_times, CBT_NCIRCUITS_TO_OBSERVE);
534 /* This code can only be run on a compact array */
535 while (n-- > 1) {
536 int k = crypto_rand_int(n + 1); /* 0 <= k <= n. */
537 build_time_t tmp = raw_times[k];
538 raw_times[k] = raw_times[n];
539 raw_times[n] = tmp;
542 /* Since the times are now shuffled, take a random CBT_NCIRCUITS_TO_OBSERVE
543 * subset (ie the first CBT_NCIRCUITS_TO_OBSERVE values) */
544 for (n = 0; n < MIN(num_times, CBT_NCIRCUITS_TO_OBSERVE); n++) {
545 circuit_build_times_add_time(cbt, raw_times[n]);
550 * Filter old synthetic timeouts that were created before the
551 * new right-censored Pareto calculation was deployed.
553 * Once all clients before 0.2.1.13-alpha are gone, this code
554 * will be unused.
556 static int
557 circuit_build_times_filter_timeouts(circuit_build_times_t *cbt)
559 int num_filtered=0, i=0;
560 double timeout_rate = 0;
561 build_time_t max_timeout = 0;
563 timeout_rate = circuit_build_times_timeout_rate(cbt);
564 max_timeout = (build_time_t)cbt->close_ms;
566 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
567 if (cbt->circuit_build_times[i] > max_timeout) {
568 build_time_t replaced = cbt->circuit_build_times[i];
569 num_filtered++;
570 cbt->circuit_build_times[i] = CBT_BUILD_ABANDONED;
572 log_debug(LD_CIRC, "Replaced timeout %d with %d", replaced,
573 cbt->circuit_build_times[i]);
577 log_info(LD_CIRC,
578 "We had %d timeouts out of %d build times, "
579 "and filtered %d above the max of %u",
580 (int)(cbt->total_build_times*timeout_rate),
581 cbt->total_build_times, num_filtered, max_timeout);
583 return num_filtered;
587 * Load histogram from <b>state</b>, shuffling the resulting array
588 * after we do so. Use this result to estimate parameters and
589 * calculate the timeout.
591 * Returns -1 and sets msg on error. Msg must be freed by the caller.
594 circuit_build_times_parse_state(circuit_build_times_t *cbt,
595 or_state_t *state, char **msg)
597 int tot_values = 0;
598 uint32_t loaded_cnt = 0, N = 0;
599 config_line_t *line;
600 unsigned int i;
601 build_time_t *loaded_times;
602 circuit_build_times_init(cbt);
603 *msg = NULL;
605 if (circuit_build_times_disabled()) {
606 return 0;
609 /* build_time_t 0 means uninitialized */
610 loaded_times = tor_malloc_zero(sizeof(build_time_t)*state->TotalBuildTimes);
612 for (line = state->BuildtimeHistogram; line; line = line->next) {
613 smartlist_t *args = smartlist_create();
614 smartlist_split_string(args, line->value, " ",
615 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
616 if (smartlist_len(args) < 2) {
617 *msg = tor_strdup("Unable to parse circuit build times: "
618 "Too few arguments to CircuitBuildTime");
619 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
620 smartlist_free(args);
621 break;
622 } else {
623 const char *ms_str = smartlist_get(args,0);
624 const char *count_str = smartlist_get(args,1);
625 uint32_t count, k;
626 build_time_t ms;
627 int ok;
628 ms = (build_time_t)tor_parse_ulong(ms_str, 0, 0,
629 CBT_BUILD_TIME_MAX, &ok, NULL);
630 if (!ok) {
631 *msg = tor_strdup("Unable to parse circuit build times: "
632 "Unparsable bin number");
633 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
634 smartlist_free(args);
635 break;
637 count = (uint32_t)tor_parse_ulong(count_str, 0, 0,
638 UINT32_MAX, &ok, NULL);
639 if (!ok) {
640 *msg = tor_strdup("Unable to parse circuit build times: "
641 "Unparsable bin count");
642 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
643 smartlist_free(args);
644 break;
647 if (loaded_cnt+count+state->CircuitBuildAbandonedCount
648 > state->TotalBuildTimes) {
649 log_warn(LD_CIRC,
650 "Too many build times in state file. "
651 "Stopping short before %d",
652 loaded_cnt+count);
653 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
654 smartlist_free(args);
655 break;
658 for (k = 0; k < count; k++) {
659 loaded_times[loaded_cnt++] = ms;
661 N++;
662 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
663 smartlist_free(args);
667 log_info(LD_CIRC,
668 "Adding %d timeouts.", state->CircuitBuildAbandonedCount);
669 for (i=0; i < state->CircuitBuildAbandonedCount; i++) {
670 loaded_times[loaded_cnt++] = CBT_BUILD_ABANDONED;
673 if (loaded_cnt != state->TotalBuildTimes) {
674 log_warn(LD_CIRC,
675 "Corrupt state file? Build times count mismatch. "
676 "Read %d times, but file says %d", loaded_cnt,
677 state->TotalBuildTimes);
680 circuit_build_times_shuffle_and_store_array(cbt, loaded_times, loaded_cnt);
682 /* Verify that we didn't overwrite any indexes */
683 for (i=0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
684 if (!cbt->circuit_build_times[i])
685 break;
686 tot_values++;
688 log_info(LD_CIRC,
689 "Loaded %d/%d values from %d lines in circuit time histogram",
690 tot_values, cbt->total_build_times, N);
691 tor_assert(cbt->total_build_times == tot_values);
692 tor_assert(cbt->total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
694 circuit_build_times_set_timeout(cbt);
696 if (!state->CircuitBuildAbandonedCount && cbt->total_build_times) {
697 circuit_build_times_filter_timeouts(cbt);
700 tor_free(loaded_times);
701 return *msg ? -1 : 0;
705 * Estimates the Xm and Alpha parameters using
706 * http://en.wikipedia.org/wiki/Pareto_distribution#Parameter_estimation
708 * The notable difference is that we use mode instead of min to estimate Xm.
709 * This is because our distribution is frechet-like. We claim this is
710 * an acceptable approximation because we are only concerned with the
711 * accuracy of the CDF of the tail.
713 void
714 circuit_build_times_update_alpha(circuit_build_times_t *cbt)
716 build_time_t *x=cbt->circuit_build_times;
717 double a = 0;
718 int n=0,i=0,abandoned_count=0;
719 build_time_t max_time=0;
721 /* http://en.wikipedia.org/wiki/Pareto_distribution#Parameter_estimation */
722 /* We sort of cheat here and make our samples slightly more pareto-like
723 * and less frechet-like. */
724 cbt->Xm = circuit_build_times_get_xm(cbt);
726 tor_assert(cbt->Xm > 0);
728 for (i=0; i< CBT_NCIRCUITS_TO_OBSERVE; i++) {
729 if (!x[i]) {
730 continue;
733 if (x[i] < cbt->Xm) {
734 a += tor_mathlog(cbt->Xm);
735 } else if (x[i] == CBT_BUILD_ABANDONED) {
736 abandoned_count++;
737 } else {
738 a += tor_mathlog(x[i]);
739 if (x[i] > max_time)
740 max_time = x[i];
742 n++;
745 if (n!=cbt->total_build_times) {
746 log_err(LD_CIRC, "Discrepancy in build times count: %d vs %d", n,
747 cbt->total_build_times);
749 tor_assert(n==cbt->total_build_times);
751 tor_assert(max_time > 0);
753 a += abandoned_count*tor_mathlog(max_time);
755 a -= n*tor_mathlog(cbt->Xm);
756 // Estimator comes from Eq #4 in:
757 // "Bayesian estimation based on trimmed samples from Pareto populations"
758 // by Arturo J. Fernández. We are right-censored only.
759 a = (n-abandoned_count)/a;
761 cbt->alpha = a;
765 * This is the Pareto Quantile Function. It calculates the point x
766 * in the distribution such that F(x) = quantile (ie quantile*100%
767 * of the mass of the density function is below x on the curve).
769 * We use it to calculate the timeout and also to generate synthetic
770 * values of time for circuits that timeout before completion.
772 * See http://en.wikipedia.org/wiki/Quantile_function,
773 * http://en.wikipedia.org/wiki/Inverse_transform_sampling and
774 * http://en.wikipedia.org/wiki/Pareto_distribution#Generating_a_
775 * random_sample_from_Pareto_distribution
776 * That's right. I'll cite wikipedia all day long.
778 * Return value is in milliseconds.
780 double
781 circuit_build_times_calculate_timeout(circuit_build_times_t *cbt,
782 double quantile)
784 double ret;
785 tor_assert(quantile >= 0);
786 tor_assert(1.0-quantile > 0);
787 tor_assert(cbt->Xm > 0);
789 ret = cbt->Xm/pow(1.0-quantile,1.0/cbt->alpha);
790 if (ret > INT32_MAX) {
791 ret = INT32_MAX;
793 tor_assert(ret > 0);
794 return ret;
797 /** Pareto CDF */
798 double
799 circuit_build_times_cdf(circuit_build_times_t *cbt, double x)
801 double ret;
802 tor_assert(cbt->Xm > 0);
803 ret = 1.0-pow(cbt->Xm/x,cbt->alpha);
804 tor_assert(0 <= ret && ret <= 1.0);
805 return ret;
809 * Generate a synthetic time using our distribution parameters.
811 * The return value will be within the [q_lo, q_hi) quantile points
812 * on the CDF.
814 build_time_t
815 circuit_build_times_generate_sample(circuit_build_times_t *cbt,
816 double q_lo, double q_hi)
818 uint64_t r = crypto_rand_uint64(UINT64_MAX-1);
819 build_time_t ret;
820 double u;
822 /* Generate between [q_lo, q_hi) */
823 q_hi -= 1.0/(INT32_MAX);
825 tor_assert(q_lo >= 0);
826 tor_assert(q_hi < 1);
827 tor_assert(q_lo < q_hi);
829 u = q_lo + ((q_hi-q_lo)*r)/(1.0*UINT64_MAX);
831 tor_assert(0 <= u && u < 1.0);
832 /* circuit_build_times_calculate_timeout returns <= INT32_MAX */
833 ret = (build_time_t)
834 tor_lround(circuit_build_times_calculate_timeout(cbt, u));
835 tor_assert(ret > 0);
836 return ret;
840 * Estimate an initial alpha parameter by solving the quantile
841 * function with a quantile point and a specific timeout value.
843 void
844 circuit_build_times_initial_alpha(circuit_build_times_t *cbt,
845 double quantile, double timeout_ms)
847 // Q(u) = Xm/((1-u)^(1/a))
848 // Q(0.8) = Xm/((1-0.8))^(1/a)) = CircBuildTimeout
849 // CircBuildTimeout = Xm/((1-0.8))^(1/a))
850 // CircBuildTimeout = Xm*((1-0.8))^(-1/a))
851 // ln(CircBuildTimeout) = ln(Xm)+ln(((1-0.8)))*(-1/a)
852 // -ln(1-0.8)/(ln(CircBuildTimeout)-ln(Xm))=a
853 tor_assert(quantile >= 0);
854 tor_assert(cbt->Xm > 0);
855 cbt->alpha = tor_mathlog(1.0-quantile)/
856 (tor_mathlog(cbt->Xm)-tor_mathlog(timeout_ms));
857 tor_assert(cbt->alpha > 0);
861 * Returns true if we need circuits to be built
864 circuit_build_times_needs_circuits(circuit_build_times_t *cbt)
866 /* Return true if < MIN_CIRCUITS_TO_OBSERVE */
867 if (cbt->total_build_times < circuit_build_times_min_circs_to_observe())
868 return 1;
869 return 0;
873 * Returns true if we should build a timeout test circuit
874 * right now.
877 circuit_build_times_needs_circuits_now(circuit_build_times_t *cbt)
879 return circuit_build_times_needs_circuits(cbt) &&
880 approx_time()-cbt->last_circ_at > circuit_build_times_test_frequency();
884 * Called to indicate that the network showed some signs of liveness.
886 * This function is called every time we receive a cell. Avoid
887 * syscalls, events, and other high-intensity work.
889 void
890 circuit_build_times_network_is_live(circuit_build_times_t *cbt)
892 cbt->liveness.network_last_live = approx_time();
893 cbt->liveness.nonlive_discarded = 0;
894 cbt->liveness.nonlive_timeouts = 0;
898 * Called to indicate that we completed a circuit. Because this circuit
899 * succeeded, it doesn't count as a timeout-after-the-first-hop.
901 void
902 circuit_build_times_network_circ_success(circuit_build_times_t *cbt)
904 cbt->liveness.timeouts_after_firsthop[cbt->liveness.after_firsthop_idx] = 0;
905 cbt->liveness.after_firsthop_idx++;
906 cbt->liveness.after_firsthop_idx %= cbt->liveness.num_recent_circs;
910 * A circuit just timed out. If it failed after the first hop, record it
911 * in our history for later deciding if the network speed has changed.
913 static void
914 circuit_build_times_network_timeout(circuit_build_times_t *cbt,
915 int did_onehop)
917 if (did_onehop) {
918 cbt->liveness.timeouts_after_firsthop[cbt->liveness.after_firsthop_idx]=1;
919 cbt->liveness.after_firsthop_idx++;
920 cbt->liveness.after_firsthop_idx %= cbt->liveness.num_recent_circs;
925 * A circuit was just forcibly closed. If there has been no recent network
926 * activity at all, but this circuit was launched back when we thought the
927 * network was live, increment the number of "nonlive" circuit timeouts.
929 static void
930 circuit_build_times_network_close(circuit_build_times_t *cbt,
931 int did_onehop, time_t start_time)
933 time_t now = time(NULL);
935 * Check if this is a timeout that was for a circuit that spent its
936 * entire existence during a time where we have had no network activity.
938 * Also double check that it is a valid timeout after we have possibly
939 * just recently reset cbt->close_ms.
941 * We use close_ms here because timeouts aren't actually counted as timeouts
942 * until close_ms elapses.
944 if (cbt->liveness.network_last_live <= start_time &&
945 start_time <= (now - cbt->close_ms/1000.0)) {
946 if (did_onehop) {
947 log_warn(LD_BUG,
948 "Circuit somehow completed a hop while the network was "
949 "not live. Network was last live at %ld, but circuit launched "
950 "at %ld. It's now %ld.", cbt->liveness.network_last_live,
951 start_time, now);
953 cbt->liveness.nonlive_timeouts++;
958 * Returns false if the network has not received a cell or tls handshake
959 * in the past NETWORK_NOTLIVE_TIMEOUT_COUNT circuits.
961 * Also has the side effect of rewinding the circuit time history
962 * in the case of recent liveness changes.
965 circuit_build_times_network_check_live(circuit_build_times_t *cbt)
967 time_t now = approx_time();
968 if (cbt->liveness.nonlive_timeouts >= CBT_NETWORK_NONLIVE_DISCARD_COUNT) {
969 if (!cbt->liveness.nonlive_discarded) {
970 cbt->liveness.nonlive_discarded = 1;
971 log_notice(LD_CIRC, "Network is no longer live (too many recent "
972 "circuit timeouts). Dead for %ld seconds.",
973 (long int)(now - cbt->liveness.network_last_live));
974 /* Only discard NETWORK_NONLIVE_TIMEOUT_COUNT-1 because we stopped
975 * counting after that */
976 circuit_build_times_rewind_history(cbt,
977 CBT_NETWORK_NONLIVE_TIMEOUT_COUNT-1);
978 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_DISCARD);
980 return 0;
981 } else if (cbt->liveness.nonlive_timeouts >=
982 CBT_NETWORK_NONLIVE_TIMEOUT_COUNT) {
983 if (cbt->timeout_ms < circuit_build_times_get_initial_timeout()) {
984 log_notice(LD_CIRC,
985 "Network is flaky. No activity for %ld seconds. "
986 "Temporarily raising timeout to %lds.",
987 (long int)(now - cbt->liveness.network_last_live),
988 tor_lround(circuit_build_times_get_initial_timeout()/1000));
989 cbt->liveness.suspended_timeout = cbt->timeout_ms;
990 cbt->liveness.suspended_close_timeout = cbt->close_ms;
991 cbt->close_ms = cbt->timeout_ms
992 = circuit_build_times_get_initial_timeout();
993 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_SUSPENDED);
996 return 0;
997 } else if (cbt->liveness.suspended_timeout > 0) {
998 log_notice(LD_CIRC,
999 "Network activity has resumed. "
1000 "Resuming circuit timeout calculations.");
1001 cbt->timeout_ms = cbt->liveness.suspended_timeout;
1002 cbt->close_ms = cbt->liveness.suspended_close_timeout;
1003 cbt->liveness.suspended_timeout = 0;
1004 cbt->liveness.suspended_close_timeout = 0;
1005 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESUME);
1008 return 1;
1012 * Returns true if we have seen more than MAX_RECENT_TIMEOUT_COUNT of
1013 * the past RECENT_CIRCUITS time out after the first hop. Used to detect
1014 * if the network connection has changed significantly.
1016 * Also resets the entire timeout history in this case and causes us
1017 * to restart the process of building test circuits and estimating a
1018 * new timeout.
1021 circuit_build_times_network_check_changed(circuit_build_times_t *cbt)
1023 int total_build_times = cbt->total_build_times;
1024 int timeout_count=0;
1025 int i;
1027 /* how many of our recent circuits made it to the first hop but then
1028 * timed out? */
1029 for (i = 0; i < cbt->liveness.num_recent_circs; i++) {
1030 timeout_count += cbt->liveness.timeouts_after_firsthop[i];
1033 /* If 80% of our recent circuits are timing out after the first hop,
1034 * we need to re-estimate a new initial alpha and timeout. */
1035 if (timeout_count < circuit_build_times_max_timeouts()) {
1036 return 0;
1039 circuit_build_times_reset(cbt);
1040 memset(cbt->liveness.timeouts_after_firsthop, 0,
1041 sizeof(*cbt->liveness.timeouts_after_firsthop)*
1042 cbt->liveness.num_recent_circs);
1043 cbt->liveness.after_firsthop_idx = 0;
1045 /* Check to see if this has happened before. If so, double the timeout
1046 * to give people on abysmally bad network connections a shot at access */
1047 if (cbt->timeout_ms >= circuit_build_times_get_initial_timeout()) {
1048 if (cbt->timeout_ms > INT32_MAX/2 || cbt->close_ms > INT32_MAX/2) {
1049 log_warn(LD_CIRC, "Insanely large circuit build timeout value. "
1050 "(timeout = %lfmsec, close = %lfmsec)",
1051 cbt->timeout_ms, cbt->close_ms);
1052 } else {
1053 cbt->timeout_ms *= 2;
1054 cbt->close_ms *= 2;
1056 } else {
1057 cbt->close_ms = cbt->timeout_ms
1058 = circuit_build_times_get_initial_timeout();
1061 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
1063 log_notice(LD_CIRC,
1064 "Network connection speed appears to have changed. Resetting "
1065 "timeout to %lds after %d timeouts and %d buildtimes.",
1066 tor_lround(cbt->timeout_ms/1000), timeout_count,
1067 total_build_times);
1069 return 1;
1073 * Count the number of timeouts in a set of cbt data.
1075 double
1076 circuit_build_times_timeout_rate(const circuit_build_times_t *cbt)
1078 int i=0,timeouts=0;
1079 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
1080 if (cbt->circuit_build_times[i] >= cbt->timeout_ms) {
1081 timeouts++;
1085 if (!cbt->total_build_times)
1086 return 0;
1088 return ((double)timeouts)/cbt->total_build_times;
1092 * Store a timeout as a synthetic value.
1094 * Returns true if the store was successful and we should possibly
1095 * update our timeout estimate.
1098 circuit_build_times_count_close(circuit_build_times_t *cbt,
1099 int did_onehop,
1100 time_t start_time)
1102 if (circuit_build_times_disabled()) {
1103 cbt->close_ms = cbt->timeout_ms
1104 = circuit_build_times_get_initial_timeout();
1105 return 0;
1108 /* Record this force-close to help determine if the network is dead */
1109 circuit_build_times_network_close(cbt, did_onehop, start_time);
1111 /* Only count timeouts if network is live.. */
1112 if (!circuit_build_times_network_check_live(cbt)) {
1113 return 0;
1116 circuit_build_times_add_time(cbt, CBT_BUILD_ABANDONED);
1117 return 1;
1121 * Update timeout counts to determine if we need to expire
1122 * our build time history due to excessive timeouts.
1124 void
1125 circuit_build_times_count_timeout(circuit_build_times_t *cbt,
1126 int did_onehop)
1128 if (circuit_build_times_disabled()) {
1129 cbt->close_ms = cbt->timeout_ms
1130 = circuit_build_times_get_initial_timeout();
1131 return;
1134 circuit_build_times_network_timeout(cbt, did_onehop);
1136 /* If there are a ton of timeouts, we should reset
1137 * the circuit build timeout.
1139 circuit_build_times_network_check_changed(cbt);
1143 * Estimate a new timeout based on history and set our timeout
1144 * variable accordingly.
1146 static int
1147 circuit_build_times_set_timeout_worker(circuit_build_times_t *cbt)
1149 if (cbt->total_build_times < circuit_build_times_min_circs_to_observe()) {
1150 return 0;
1153 circuit_build_times_update_alpha(cbt);
1155 cbt->timeout_ms = circuit_build_times_calculate_timeout(cbt,
1156 circuit_build_times_quantile_cutoff());
1158 cbt->close_ms = circuit_build_times_calculate_timeout(cbt,
1159 circuit_build_times_close_quantile());
1161 /* Sometimes really fast guard nodes give us such a steep curve
1162 * that this ends up being not that much greater than timeout_ms.
1163 * Make it be at least 1 min to handle this case. */
1164 cbt->close_ms = MAX(cbt->close_ms, circuit_build_times_initial_timeout());
1166 cbt->have_computed_timeout = 1;
1167 return 1;
1171 * Exposed function to compute a new timeout. Dispatches events and
1172 * also filters out extremely high timeout values.
1174 void
1175 circuit_build_times_set_timeout(circuit_build_times_t *cbt)
1177 long prev_timeout = tor_lround(cbt->timeout_ms/1000);
1178 double timeout_rate;
1180 if (!circuit_build_times_set_timeout_worker(cbt))
1181 return;
1183 if (cbt->timeout_ms < circuit_build_times_min_timeout()) {
1184 log_warn(LD_CIRC, "Set buildtimeout to low value %lfms. Setting to %dms",
1185 cbt->timeout_ms, circuit_build_times_min_timeout());
1186 cbt->timeout_ms = circuit_build_times_min_timeout();
1187 if (cbt->close_ms < cbt->timeout_ms) {
1188 /* This shouldn't happen because of MAX() in timeout_worker above,
1189 * but doing it just in case */
1190 cbt->close_ms = circuit_build_times_initial_timeout();
1194 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_COMPUTED);
1196 timeout_rate = circuit_build_times_timeout_rate(cbt);
1198 if (prev_timeout > tor_lround(cbt->timeout_ms/1000)) {
1199 log_notice(LD_CIRC,
1200 "Based on %d circuit times, it looks like we don't need to "
1201 "wait so long for circuits to finish. We will now assume a "
1202 "circuit is too slow to use after waiting %ld seconds.",
1203 cbt->total_build_times,
1204 tor_lround(cbt->timeout_ms/1000));
1205 log_info(LD_CIRC,
1206 "Circuit timeout data: %lfms, %lfms, Xm: %d, a: %lf, r: %lf",
1207 cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha,
1208 timeout_rate);
1209 } else if (prev_timeout < tor_lround(cbt->timeout_ms/1000)) {
1210 log_notice(LD_CIRC,
1211 "Based on %d circuit times, it looks like we need to wait "
1212 "longer for circuits to finish. We will now assume a "
1213 "circuit is too slow to use after waiting %ld seconds.",
1214 cbt->total_build_times,
1215 tor_lround(cbt->timeout_ms/1000));
1216 log_info(LD_CIRC,
1217 "Circuit timeout data: %lfms, %lfms, Xm: %d, a: %lf, r: %lf",
1218 cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha,
1219 timeout_rate);
1220 } else {
1221 log_info(LD_CIRC,
1222 "Set circuit build timeout to %lds (%lfms, %lfms, Xm: %d, a: %lf,"
1223 " r: %lf) based on %d circuit times",
1224 tor_lround(cbt->timeout_ms/1000),
1225 cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha, timeout_rate,
1226 cbt->total_build_times);
1230 /** Iterate over values of circ_id, starting from conn-\>next_circ_id,
1231 * and with the high bit specified by conn-\>circ_id_type, until we get
1232 * a circ_id that is not in use by any other circuit on that conn.
1234 * Return it, or 0 if can't get a unique circ_id.
1236 static circid_t
1237 get_unique_circ_id_by_conn(or_connection_t *conn)
1239 circid_t test_circ_id;
1240 circid_t attempts=0;
1241 circid_t high_bit;
1243 tor_assert(conn);
1244 if (conn->circ_id_type == CIRC_ID_TYPE_NEITHER) {
1245 log_warn(LD_BUG, "Trying to pick a circuit ID for a connection from "
1246 "a client with no identity.");
1247 return 0;
1249 high_bit = (conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
1250 do {
1251 /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
1252 * circID such that (high_bit|test_circ_id) is not already used. */
1253 test_circ_id = conn->next_circ_id++;
1254 if (test_circ_id == 0 || test_circ_id >= 1<<15) {
1255 test_circ_id = 1;
1256 conn->next_circ_id = 2;
1258 if (++attempts > 1<<15) {
1259 /* Make sure we don't loop forever if all circ_id's are used. This
1260 * matters because it's an external DoS opportunity.
1262 log_warn(LD_CIRC,"No unused circ IDs. Failing.");
1263 return 0;
1265 test_circ_id |= high_bit;
1266 } while (circuit_id_in_use_on_orconn(test_circ_id, conn));
1267 return test_circ_id;
1270 /** If <b>verbose</b> is false, allocate and return a comma-separated list of
1271 * the currently built elements of circuit_t. If <b>verbose</b> is true, also
1272 * list information about link status in a more verbose format using spaces.
1273 * If <b>verbose_names</b> is false, give nicknames for Named routers and hex
1274 * digests for others; if <b>verbose_names</b> is true, use $DIGEST=Name style
1275 * names.
1277 static char *
1278 circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
1280 crypt_path_t *hop;
1281 smartlist_t *elements;
1282 const char *states[] = {"closed", "waiting for keys", "open"};
1283 char *s;
1285 elements = smartlist_create();
1287 if (verbose) {
1288 const char *nickname = build_state_get_exit_nickname(circ->build_state);
1289 char *cp;
1290 tor_asprintf(&cp, "%s%s circ (length %d%s%s):",
1291 circ->build_state->is_internal ? "internal" : "exit",
1292 circ->build_state->need_uptime ? " (high-uptime)" : "",
1293 circ->build_state->desired_path_len,
1294 circ->_base.state == CIRCUIT_STATE_OPEN ? "" : ", exit ",
1295 circ->_base.state == CIRCUIT_STATE_OPEN ? "" :
1296 (nickname?nickname:"*unnamed*"));
1297 smartlist_add(elements, cp);
1300 hop = circ->cpath;
1301 do {
1302 routerinfo_t *ri;
1303 routerstatus_t *rs;
1304 char *elt;
1305 const char *id;
1306 if (!hop)
1307 break;
1308 if (!verbose && hop->state != CPATH_STATE_OPEN)
1309 break;
1310 if (!hop->extend_info)
1311 break;
1312 id = hop->extend_info->identity_digest;
1313 if (verbose_names) {
1314 elt = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
1315 if ((ri = router_get_by_digest(id))) {
1316 router_get_verbose_nickname(elt, ri);
1317 } else if ((rs = router_get_consensus_status_by_id(id))) {
1318 routerstatus_get_verbose_nickname(elt, rs);
1319 } else if (is_legal_nickname(hop->extend_info->nickname)) {
1320 elt[0] = '$';
1321 base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
1322 elt[HEX_DIGEST_LEN+1]= '~';
1323 strlcpy(elt+HEX_DIGEST_LEN+2,
1324 hop->extend_info->nickname, MAX_NICKNAME_LEN+1);
1325 } else {
1326 elt[0] = '$';
1327 base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
1329 } else { /* ! verbose_names */
1330 if ((ri = router_get_by_digest(id)) &&
1331 ri->is_named) {
1332 elt = tor_strdup(hop->extend_info->nickname);
1333 } else {
1334 elt = tor_malloc(HEX_DIGEST_LEN+2);
1335 elt[0] = '$';
1336 base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
1339 tor_assert(elt);
1340 if (verbose) {
1341 size_t len = strlen(elt)+2+strlen(states[hop->state])+1;
1342 char *v = tor_malloc(len);
1343 tor_assert(hop->state <= 2);
1344 tor_snprintf(v,len,"%s(%s)",elt,states[hop->state]);
1345 smartlist_add(elements, v);
1346 tor_free(elt);
1347 } else {
1348 smartlist_add(elements, elt);
1350 hop = hop->next;
1351 } while (hop != circ->cpath);
1353 s = smartlist_join_strings(elements, verbose?" ":",", 0, NULL);
1354 SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
1355 smartlist_free(elements);
1356 return s;
1359 /** If <b>verbose</b> is false, allocate and return a comma-separated
1360 * list of the currently built elements of circuit_t. If
1361 * <b>verbose</b> is true, also list information about link status in
1362 * a more verbose format using spaces.
1364 char *
1365 circuit_list_path(origin_circuit_t *circ, int verbose)
1367 return circuit_list_path_impl(circ, verbose, 0);
1370 /** Allocate and return a comma-separated list of the currently built elements
1371 * of circuit_t, giving each as a verbose nickname.
1373 char *
1374 circuit_list_path_for_controller(origin_circuit_t *circ)
1376 return circuit_list_path_impl(circ, 0, 1);
1379 /** Log, at severity <b>severity</b>, the nicknames of each router in
1380 * circ's cpath. Also log the length of the cpath, and the intended
1381 * exit point.
1383 void
1384 circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
1386 char *s = circuit_list_path(circ,1);
1387 tor_log(severity,domain,"%s",s);
1388 tor_free(s);
1391 /** Tell the rep(utation)hist(ory) module about the status of the links
1392 * in circ. Hops that have become OPEN are marked as successfully
1393 * extended; the _first_ hop that isn't open (if any) is marked as
1394 * unable to extend.
1396 /* XXXX Someday we should learn from OR circuits too. */
1397 void
1398 circuit_rep_hist_note_result(origin_circuit_t *circ)
1400 crypt_path_t *hop;
1401 char *prev_digest = NULL;
1402 routerinfo_t *router;
1403 hop = circ->cpath;
1404 if (!hop) /* circuit hasn't started building yet. */
1405 return;
1406 if (server_mode(get_options())) {
1407 routerinfo_t *me = router_get_my_routerinfo();
1408 if (!me)
1409 return;
1410 prev_digest = me->cache_info.identity_digest;
1412 do {
1413 router = router_get_by_digest(hop->extend_info->identity_digest);
1414 if (router) {
1415 if (prev_digest) {
1416 if (hop->state == CPATH_STATE_OPEN)
1417 rep_hist_note_extend_succeeded(prev_digest,
1418 router->cache_info.identity_digest);
1419 else {
1420 rep_hist_note_extend_failed(prev_digest,
1421 router->cache_info.identity_digest);
1422 break;
1425 prev_digest = router->cache_info.identity_digest;
1426 } else {
1427 prev_digest = NULL;
1429 hop=hop->next;
1430 } while (hop!=circ->cpath);
1433 /** Pick all the entries in our cpath. Stop and return 0 when we're
1434 * happy, or return -1 if an error occurs. */
1435 static int
1436 onion_populate_cpath(origin_circuit_t *circ)
1438 int r;
1439 again:
1440 r = onion_extend_cpath(circ);
1441 if (r < 0) {
1442 log_info(LD_CIRC,"Generating cpath hop failed.");
1443 return -1;
1445 if (r == 0)
1446 goto again;
1447 return 0; /* if r == 1 */
1450 /** Create and return a new origin circuit. Initialize its purpose and
1451 * build-state based on our arguments. The <b>flags</b> argument is a
1452 * bitfield of CIRCLAUNCH_* flags. */
1453 origin_circuit_t *
1454 origin_circuit_init(uint8_t purpose, int flags)
1456 /* sets circ->p_circ_id and circ->p_conn */
1457 origin_circuit_t *circ = origin_circuit_new();
1458 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OR_WAIT);
1459 circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
1460 circ->build_state->onehop_tunnel =
1461 ((flags & CIRCLAUNCH_ONEHOP_TUNNEL) ? 1 : 0);
1462 circ->build_state->need_uptime =
1463 ((flags & CIRCLAUNCH_NEED_UPTIME) ? 1 : 0);
1464 circ->build_state->need_capacity =
1465 ((flags & CIRCLAUNCH_NEED_CAPACITY) ? 1 : 0);
1466 circ->build_state->is_internal =
1467 ((flags & CIRCLAUNCH_IS_INTERNAL) ? 1 : 0);
1468 circ->_base.purpose = purpose;
1469 return circ;
1472 /** Build a new circuit for <b>purpose</b>. If <b>exit</b>
1473 * is defined, then use that as your exit router, else choose a suitable
1474 * exit node.
1476 * Also launch a connection to the first OR in the chosen path, if
1477 * it's not open already.
1479 origin_circuit_t *
1480 circuit_establish_circuit(uint8_t purpose, extend_info_t *exit, int flags)
1482 origin_circuit_t *circ;
1483 int err_reason = 0;
1485 circ = origin_circuit_init(purpose, flags);
1487 if (onion_pick_cpath_exit(circ, exit) < 0 ||
1488 onion_populate_cpath(circ) < 0) {
1489 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOPATH);
1490 return NULL;
1493 control_event_circuit_status(circ, CIRC_EVENT_LAUNCHED, 0);
1495 if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
1496 circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
1497 return NULL;
1499 return circ;
1502 /** Start establishing the first hop of our circuit. Figure out what
1503 * OR we should connect to, and if necessary start the connection to
1504 * it. If we're already connected, then send the 'create' cell.
1505 * Return 0 for ok, -reason if circ should be marked-for-close. */
1507 circuit_handle_first_hop(origin_circuit_t *circ)
1509 crypt_path_t *firsthop;
1510 or_connection_t *n_conn;
1511 int err_reason = 0;
1512 const char *msg = NULL;
1513 int should_launch = 0;
1515 firsthop = onion_next_hop_in_cpath(circ->cpath);
1516 tor_assert(firsthop);
1517 tor_assert(firsthop->extend_info);
1519 /* now see if we're already connected to the first OR in 'route' */
1520 log_debug(LD_CIRC,"Looking for firsthop '%s:%u'",
1521 fmt_addr(&firsthop->extend_info->addr),
1522 firsthop->extend_info->port);
1524 n_conn = connection_or_get_for_extend(firsthop->extend_info->identity_digest,
1525 &firsthop->extend_info->addr,
1526 &msg,
1527 &should_launch);
1529 if (!n_conn) {
1530 /* not currently connected in a useful way. */
1531 const char *name = strlen(firsthop->extend_info->nickname) ?
1532 firsthop->extend_info->nickname : fmt_addr(&firsthop->extend_info->addr);
1533 log_info(LD_CIRC, "Next router is %s: %s ",
1534 safe_str_client(name), msg?msg:"???");
1535 circ->_base.n_hop = extend_info_dup(firsthop->extend_info);
1537 if (should_launch) {
1538 if (circ->build_state->onehop_tunnel)
1539 control_event_bootstrap(BOOTSTRAP_STATUS_CONN_DIR, 0);
1540 n_conn = connection_or_connect(&firsthop->extend_info->addr,
1541 firsthop->extend_info->port,
1542 firsthop->extend_info->identity_digest);
1543 if (!n_conn) { /* connect failed, forget the whole thing */
1544 log_info(LD_CIRC,"connect to firsthop failed. Closing.");
1545 return -END_CIRC_REASON_CONNECTFAILED;
1549 log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
1550 /* return success. The onion/circuit/etc will be taken care of
1551 * automatically (may already have been) whenever n_conn reaches
1552 * OR_CONN_STATE_OPEN.
1554 return 0;
1555 } else { /* it's already open. use it. */
1556 tor_assert(!circ->_base.n_hop);
1557 circ->_base.n_conn = n_conn;
1558 log_debug(LD_CIRC,"Conn open. Delivering first onion skin.");
1559 if ((err_reason = circuit_send_next_onion_skin(circ)) < 0) {
1560 log_info(LD_CIRC,"circuit_send_next_onion_skin failed.");
1561 return err_reason;
1564 return 0;
1567 /** Find any circuits that are waiting on <b>or_conn</b> to become
1568 * open and get them to send their create cells forward.
1570 * Status is 1 if connect succeeded, or 0 if connect failed.
1572 void
1573 circuit_n_conn_done(or_connection_t *or_conn, int status)
1575 smartlist_t *pending_circs;
1576 int err_reason = 0;
1578 log_debug(LD_CIRC,"or_conn to %s/%s, status=%d",
1579 or_conn->nickname ? or_conn->nickname : "NULL",
1580 or_conn->_base.address, status);
1582 pending_circs = smartlist_create();
1583 circuit_get_all_pending_on_or_conn(pending_circs, or_conn);
1585 SMARTLIST_FOREACH_BEGIN(pending_circs, circuit_t *, circ)
1587 /* These checks are redundant wrt get_all_pending_on_or_conn, but I'm
1588 * leaving them in in case it's possible for the status of a circuit to
1589 * change as we're going down the list. */
1590 if (circ->marked_for_close || circ->n_conn || !circ->n_hop ||
1591 circ->state != CIRCUIT_STATE_OR_WAIT)
1592 continue;
1594 if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
1595 /* Look at addr/port. This is an unkeyed connection. */
1596 if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) ||
1597 circ->n_hop->port != or_conn->_base.port)
1598 continue;
1599 } else {
1600 /* We expected a key. See if it's the right one. */
1601 if (memcmp(or_conn->identity_digest,
1602 circ->n_hop->identity_digest, DIGEST_LEN))
1603 continue;
1605 if (!status) { /* or_conn failed; close circ */
1606 log_info(LD_CIRC,"or_conn failed. Closing circ.");
1607 circuit_mark_for_close(circ, END_CIRC_REASON_OR_CONN_CLOSED);
1608 continue;
1610 log_debug(LD_CIRC, "Found circ, sending create cell.");
1611 /* circuit_deliver_create_cell will set n_circ_id and add us to
1612 * orconn_circuid_circuit_map, so we don't need to call
1613 * set_circid_orconn here. */
1614 circ->n_conn = or_conn;
1615 extend_info_free(circ->n_hop);
1616 circ->n_hop = NULL;
1618 if (CIRCUIT_IS_ORIGIN(circ)) {
1619 if ((err_reason =
1620 circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ))) < 0) {
1621 log_info(LD_CIRC,
1622 "send_next_onion_skin failed; circuit marked for closing.");
1623 circuit_mark_for_close(circ, -err_reason);
1624 continue;
1625 /* XXX could this be bad, eg if next_onion_skin failed because conn
1626 * died? */
1628 } else {
1629 /* pull the create cell out of circ->onionskin, and send it */
1630 tor_assert(circ->n_conn_onionskin);
1631 if (circuit_deliver_create_cell(circ,CELL_CREATE,
1632 circ->n_conn_onionskin)<0) {
1633 circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
1634 continue;
1636 tor_free(circ->n_conn_onionskin);
1637 circuit_set_state(circ, CIRCUIT_STATE_OPEN);
1640 SMARTLIST_FOREACH_END(circ);
1642 smartlist_free(pending_circs);
1645 /** Find a new circid that isn't currently in use on the circ->n_conn
1646 * for the outgoing
1647 * circuit <b>circ</b>, and deliver a cell of type <b>cell_type</b>
1648 * (either CELL_CREATE or CELL_CREATE_FAST) with payload <b>payload</b>
1649 * to this circuit.
1650 * Return -1 if we failed to find a suitable circid, else return 0.
1652 static int
1653 circuit_deliver_create_cell(circuit_t *circ, uint8_t cell_type,
1654 const char *payload)
1656 cell_t cell;
1657 circid_t id;
1659 tor_assert(circ);
1660 tor_assert(circ->n_conn);
1661 tor_assert(payload);
1662 tor_assert(cell_type == CELL_CREATE || cell_type == CELL_CREATE_FAST);
1664 id = get_unique_circ_id_by_conn(circ->n_conn);
1665 if (!id) {
1666 log_warn(LD_CIRC,"failed to get unique circID.");
1667 return -1;
1669 log_debug(LD_CIRC,"Chosen circID %u.", id);
1670 circuit_set_n_circid_orconn(circ, id, circ->n_conn);
1672 memset(&cell, 0, sizeof(cell_t));
1673 cell.command = cell_type;
1674 cell.circ_id = circ->n_circ_id;
1676 memcpy(cell.payload, payload, ONIONSKIN_CHALLENGE_LEN);
1677 append_cell_to_circuit_queue(circ, circ->n_conn, &cell, CELL_DIRECTION_OUT);
1679 if (CIRCUIT_IS_ORIGIN(circ)) {
1680 /* mark it so it gets better rate limiting treatment. */
1681 circ->n_conn->client_used = time(NULL);
1684 return 0;
1687 /** We've decided to start our reachability testing. If all
1688 * is set, log this to the user. Return 1 if we did, or 0 if
1689 * we chose not to log anything. */
1691 inform_testing_reachability(void)
1693 char dirbuf[128];
1694 routerinfo_t *me = router_get_my_routerinfo();
1695 if (!me)
1696 return 0;
1697 control_event_server_status(LOG_NOTICE,
1698 "CHECKING_REACHABILITY ORADDRESS=%s:%d",
1699 me->address, me->or_port);
1700 if (me->dir_port) {
1701 tor_snprintf(dirbuf, sizeof(dirbuf), " and DirPort %s:%d",
1702 me->address, me->dir_port);
1703 control_event_server_status(LOG_NOTICE,
1704 "CHECKING_REACHABILITY DIRADDRESS=%s:%d",
1705 me->address, me->dir_port);
1707 log_notice(LD_OR, "Now checking whether ORPort %s:%d%s %s reachable... "
1708 "(this may take up to %d minutes -- look for log "
1709 "messages indicating success)",
1710 me->address, me->or_port,
1711 me->dir_port ? dirbuf : "",
1712 me->dir_port ? "are" : "is",
1713 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT/60);
1715 return 1;
1718 /** Return true iff we should send a create_fast cell to start building a given
1719 * circuit */
1720 static INLINE int
1721 should_use_create_fast_for_circuit(origin_circuit_t *circ)
1723 or_options_t *options = get_options();
1724 tor_assert(circ->cpath);
1725 tor_assert(circ->cpath->extend_info);
1727 if (!circ->cpath->extend_info->onion_key)
1728 return 1; /* our hand is forced: only a create_fast will work. */
1729 if (!options->FastFirstHopPK)
1730 return 0; /* we prefer to avoid create_fast */
1731 if (server_mode(options)) {
1732 /* We're a server, and we know an onion key. We can choose.
1733 * Prefer to blend in. */
1734 return 0;
1737 return 1;
1740 /** This is the backbone function for building circuits.
1742 * If circ's first hop is closed, then we need to build a create
1743 * cell and send it forward.
1745 * Otherwise, we need to build a relay extend cell and send it
1746 * forward.
1748 * Return -reason if we want to tear down circ, else return 0.
1751 circuit_send_next_onion_skin(origin_circuit_t *circ)
1753 crypt_path_t *hop;
1754 routerinfo_t *router;
1755 char payload[2+4+DIGEST_LEN+ONIONSKIN_CHALLENGE_LEN];
1756 char *onionskin;
1757 size_t payload_len;
1759 tor_assert(circ);
1761 if (circ->cpath->state == CPATH_STATE_CLOSED) {
1762 int fast;
1763 uint8_t cell_type;
1764 log_debug(LD_CIRC,"First skin; sending create cell.");
1765 if (circ->build_state->onehop_tunnel)
1766 control_event_bootstrap(BOOTSTRAP_STATUS_ONEHOP_CREATE, 0);
1767 else
1768 control_event_bootstrap(BOOTSTRAP_STATUS_CIRCUIT_CREATE, 0);
1770 router = router_get_by_digest(circ->_base.n_conn->identity_digest);
1771 fast = should_use_create_fast_for_circuit(circ);
1772 if (!fast) {
1773 /* We are an OR and we know the right onion key: we should
1774 * send an old slow create cell.
1776 cell_type = CELL_CREATE;
1777 if (onion_skin_create(circ->cpath->extend_info->onion_key,
1778 &(circ->cpath->dh_handshake_state),
1779 payload) < 0) {
1780 log_warn(LD_CIRC,"onion_skin_create (first hop) failed.");
1781 return - END_CIRC_REASON_INTERNAL;
1783 note_request("cell: create", 1);
1784 } else {
1785 /* We are not an OR, and we're building the first hop of a circuit to a
1786 * new OR: we can be speedy and use CREATE_FAST to save an RSA operation
1787 * and a DH operation. */
1788 cell_type = CELL_CREATE_FAST;
1789 memset(payload, 0, sizeof(payload));
1790 crypto_rand(circ->cpath->fast_handshake_state,
1791 sizeof(circ->cpath->fast_handshake_state));
1792 memcpy(payload, circ->cpath->fast_handshake_state,
1793 sizeof(circ->cpath->fast_handshake_state));
1794 note_request("cell: create fast", 1);
1797 if (circuit_deliver_create_cell(TO_CIRCUIT(circ), cell_type, payload) < 0)
1798 return - END_CIRC_REASON_RESOURCELIMIT;
1800 circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
1801 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
1802 log_info(LD_CIRC,"First hop: finished sending %s cell to '%s'",
1803 fast ? "CREATE_FAST" : "CREATE",
1804 router ? router->nickname : "<unnamed>");
1805 } else {
1806 tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
1807 tor_assert(circ->_base.state == CIRCUIT_STATE_BUILDING);
1808 log_debug(LD_CIRC,"starting to send subsequent skin.");
1809 hop = onion_next_hop_in_cpath(circ->cpath);
1810 if (!hop) {
1811 /* done building the circuit. whew. */
1812 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
1813 if (!circ->build_state->onehop_tunnel) {
1814 struct timeval end;
1815 long timediff;
1816 tor_gettimeofday(&end);
1817 timediff = tv_mdiff(&circ->_base.highres_created, &end);
1819 * If the circuit build time is much greater than we would have cut
1820 * it off at, we probably had a suspend event along this codepath,
1821 * and we should discard the value.
1823 if (timediff < 0 || timediff > 2*circ_times.close_ms+1000) {
1824 log_notice(LD_CIRC, "Strange value for circuit build time: %ldmsec. "
1825 "Assuming clock jump.", timediff);
1826 } else if (!circuit_build_times_disabled()) {
1827 /* Don't count circuit times if the network was not live */
1828 if (circuit_build_times_network_check_live(&circ_times)) {
1829 circuit_build_times_add_time(&circ_times, (build_time_t)timediff);
1830 circuit_build_times_set_timeout(&circ_times);
1833 if (circ->_base.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
1834 circuit_build_times_network_circ_success(&circ_times);
1838 log_info(LD_CIRC,"circuit built!");
1839 circuit_reset_failure_count(0);
1840 if (circ->build_state->onehop_tunnel)
1841 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_STATUS, 0);
1842 if (!has_completed_circuit && !circ->build_state->onehop_tunnel) {
1843 or_options_t *options = get_options();
1844 has_completed_circuit=1;
1845 /* FFFF Log a count of known routers here */
1846 log_notice(LD_GENERAL,
1847 "Tor has successfully opened a circuit. "
1848 "Looks like client functionality is working.");
1849 control_event_bootstrap(BOOTSTRAP_STATUS_DONE, 0);
1850 control_event_client_status(LOG_NOTICE, "CIRCUIT_ESTABLISHED");
1851 if (server_mode(options) && !check_whether_orport_reachable()) {
1852 inform_testing_reachability();
1853 consider_testing_reachability(1, 1);
1856 circuit_rep_hist_note_result(circ);
1857 circuit_has_opened(circ); /* do other actions as necessary */
1859 /* We're done with measurement circuits here. Just close them */
1860 if (circ->_base.purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
1861 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
1862 return 0;
1865 if (tor_addr_family(&hop->extend_info->addr) != AF_INET) {
1866 log_warn(LD_BUG, "Trying to extend to a non-IPv4 address.");
1867 return - END_CIRC_REASON_INTERNAL;
1870 set_uint32(payload, tor_addr_to_ipv4n(&hop->extend_info->addr));
1871 set_uint16(payload+4, htons(hop->extend_info->port));
1873 onionskin = payload+2+4;
1874 memcpy(payload+2+4+ONIONSKIN_CHALLENGE_LEN,
1875 hop->extend_info->identity_digest, DIGEST_LEN);
1876 payload_len = 2+4+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN;
1878 if (onion_skin_create(hop->extend_info->onion_key,
1879 &(hop->dh_handshake_state), onionskin) < 0) {
1880 log_warn(LD_CIRC,"onion_skin_create failed.");
1881 return - END_CIRC_REASON_INTERNAL;
1884 log_info(LD_CIRC,"Sending extend relay cell.");
1885 note_request("cell: extend", 1);
1886 /* send it to hop->prev, because it will transfer
1887 * it to a create cell and then send to hop */
1888 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
1889 RELAY_COMMAND_EXTEND,
1890 payload, payload_len, hop->prev) < 0)
1891 return 0; /* circuit is closed */
1893 hop->state = CPATH_STATE_AWAITING_KEYS;
1895 return 0;
1898 /** Our clock just jumped by <b>seconds_elapsed</b>. Assume
1899 * something has also gone wrong with our network: notify the user,
1900 * and abandon all not-yet-used circuits. */
1901 void
1902 circuit_note_clock_jumped(int seconds_elapsed)
1904 int severity = server_mode(get_options()) ? LOG_WARN : LOG_NOTICE;
1905 tor_log(severity, LD_GENERAL, "Your system clock just jumped %d seconds %s; "
1906 "assuming established circuits no longer work.",
1907 seconds_elapsed >=0 ? seconds_elapsed : -seconds_elapsed,
1908 seconds_elapsed >=0 ? "forward" : "backward");
1909 control_event_general_status(LOG_WARN, "CLOCK_JUMPED TIME=%d",
1910 seconds_elapsed);
1911 has_completed_circuit=0; /* so it'll log when it works again */
1912 control_event_client_status(severity, "CIRCUIT_NOT_ESTABLISHED REASON=%s",
1913 "CLOCK_JUMPED");
1914 circuit_mark_all_unused_circs();
1915 circuit_expire_all_dirty_circs();
1918 /** Take the 'extend' <b>cell</b>, pull out addr/port plus the onion
1919 * skin and identity digest for the next hop. If we're already connected,
1920 * pass the onion skin to the next hop using a create cell; otherwise
1921 * launch a new OR connection, and <b>circ</b> will notice when the
1922 * connection succeeds or fails.
1924 * Return -1 if we want to warn and tear down the circuit, else return 0.
1927 circuit_extend(cell_t *cell, circuit_t *circ)
1929 or_connection_t *n_conn;
1930 relay_header_t rh;
1931 char *onionskin;
1932 char *id_digest=NULL;
1933 uint32_t n_addr32;
1934 uint16_t n_port;
1935 tor_addr_t n_addr;
1936 const char *msg = NULL;
1937 int should_launch = 0;
1939 if (circ->n_conn) {
1940 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1941 "n_conn already set. Bug/attack. Closing.");
1942 return -1;
1944 if (circ->n_hop) {
1945 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1946 "conn to next hop already launched. Bug/attack. Closing.");
1947 return -1;
1950 if (!server_mode(get_options())) {
1951 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1952 "Got an extend cell, but running as a client. Closing.");
1953 return -1;
1956 relay_header_unpack(&rh, cell->payload);
1958 if (rh.length < 4+2+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN) {
1959 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1960 "Wrong length %d on extend cell. Closing circuit.",
1961 rh.length);
1962 return -1;
1965 n_addr32 = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
1966 n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
1967 onionskin = cell->payload+RELAY_HEADER_SIZE+4+2;
1968 id_digest = cell->payload+RELAY_HEADER_SIZE+4+2+ONIONSKIN_CHALLENGE_LEN;
1969 tor_addr_from_ipv4h(&n_addr, n_addr32);
1971 if (!n_port || !n_addr32) {
1972 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1973 "Client asked me to extend to zero destination port or addr.");
1974 return -1;
1977 /* Check if they asked us for 0000..0000. We support using
1978 * an empty fingerprint for the first hop (e.g. for a bridge relay),
1979 * but we don't want to let people send us extend cells for empty
1980 * fingerprints -- a) because it opens the user up to a mitm attack,
1981 * and b) because it lets an attacker force the relay to hold open a
1982 * new TLS connection for each extend request. */
1983 if (tor_digest_is_zero(id_digest)) {
1984 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1985 "Client asked me to extend without specifying an id_digest.");
1986 return -1;
1989 /* Next, check if we're being asked to connect to the hop that the
1990 * extend cell came from. There isn't any reason for that, and it can
1991 * assist circular-path attacks. */
1992 if (!memcmp(id_digest, TO_OR_CIRCUIT(circ)->p_conn->identity_digest,
1993 DIGEST_LEN)) {
1994 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1995 "Client asked me to extend back to the previous hop.");
1996 return -1;
1999 n_conn = connection_or_get_for_extend(id_digest,
2000 &n_addr,
2001 &msg,
2002 &should_launch);
2004 if (!n_conn) {
2005 log_debug(LD_CIRC|LD_OR,"Next router (%s:%d): %s",
2006 fmt_addr(&n_addr), (int)n_port, msg?msg:"????");
2008 circ->n_hop = extend_info_alloc(NULL /*nickname*/,
2009 id_digest,
2010 NULL /*onion_key*/,
2011 &n_addr, n_port);
2013 circ->n_conn_onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
2014 memcpy(circ->n_conn_onionskin, onionskin, ONIONSKIN_CHALLENGE_LEN);
2015 circuit_set_state(circ, CIRCUIT_STATE_OR_WAIT);
2017 if (should_launch) {
2018 /* we should try to open a connection */
2019 n_conn = connection_or_connect(&n_addr, n_port, id_digest);
2020 if (!n_conn) {
2021 log_info(LD_CIRC,"Launching n_conn failed. Closing circuit.");
2022 circuit_mark_for_close(circ, END_CIRC_REASON_CONNECTFAILED);
2023 return 0;
2025 log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
2027 /* return success. The onion/circuit/etc will be taken care of
2028 * automatically (may already have been) whenever n_conn reaches
2029 * OR_CONN_STATE_OPEN.
2031 return 0;
2034 tor_assert(!circ->n_hop); /* Connection is already established. */
2035 circ->n_conn = n_conn;
2036 log_debug(LD_CIRC,"n_conn is %s:%u",
2037 n_conn->_base.address,n_conn->_base.port);
2039 if (circuit_deliver_create_cell(circ, CELL_CREATE, onionskin) < 0)
2040 return -1;
2041 return 0;
2044 /** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in
2045 * key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
2046 * used as follows:
2047 * - 20 to initialize f_digest
2048 * - 20 to initialize b_digest
2049 * - 16 to key f_crypto
2050 * - 16 to key b_crypto
2052 * (If 'reverse' is true, then f_XX and b_XX are swapped.)
2055 circuit_init_cpath_crypto(crypt_path_t *cpath, const char *key_data,
2056 int reverse)
2058 crypto_digest_env_t *tmp_digest;
2059 crypto_cipher_env_t *tmp_crypto;
2061 tor_assert(cpath);
2062 tor_assert(key_data);
2063 tor_assert(!(cpath->f_crypto || cpath->b_crypto ||
2064 cpath->f_digest || cpath->b_digest));
2066 cpath->f_digest = crypto_new_digest_env();
2067 crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
2068 cpath->b_digest = crypto_new_digest_env();
2069 crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
2071 if (!(cpath->f_crypto =
2072 crypto_create_init_cipher(key_data+(2*DIGEST_LEN),1))) {
2073 log_warn(LD_BUG,"Forward cipher initialization failed.");
2074 return -1;
2076 if (!(cpath->b_crypto =
2077 crypto_create_init_cipher(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN,0))) {
2078 log_warn(LD_BUG,"Backward cipher initialization failed.");
2079 return -1;
2082 if (reverse) {
2083 tmp_digest = cpath->f_digest;
2084 cpath->f_digest = cpath->b_digest;
2085 cpath->b_digest = tmp_digest;
2086 tmp_crypto = cpath->f_crypto;
2087 cpath->f_crypto = cpath->b_crypto;
2088 cpath->b_crypto = tmp_crypto;
2091 return 0;
2094 /** A created or extended cell came back to us on the circuit, and it included
2095 * <b>reply</b> as its body. (If <b>reply_type</b> is CELL_CREATED, the body
2096 * contains (the second DH key, plus KH). If <b>reply_type</b> is
2097 * CELL_CREATED_FAST, the body contains a secret y and a hash H(x|y).)
2099 * Calculate the appropriate keys and digests, make sure KH is
2100 * correct, and initialize this hop of the cpath.
2102 * Return - reason if we want to mark circ for close, else return 0.
2105 circuit_finish_handshake(origin_circuit_t *circ, uint8_t reply_type,
2106 const char *reply)
2108 char keys[CPATH_KEY_MATERIAL_LEN];
2109 crypt_path_t *hop;
2111 if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
2112 hop = circ->cpath;
2113 else {
2114 hop = onion_next_hop_in_cpath(circ->cpath);
2115 if (!hop) { /* got an extended when we're all done? */
2116 log_warn(LD_PROTOCOL,"got extended when circ already built? Closing.");
2117 return - END_CIRC_REASON_TORPROTOCOL;
2120 tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
2122 if (reply_type == CELL_CREATED && hop->dh_handshake_state) {
2123 if (onion_skin_client_handshake(hop->dh_handshake_state, reply, keys,
2124 DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
2125 log_warn(LD_CIRC,"onion_skin_client_handshake failed.");
2126 return -END_CIRC_REASON_TORPROTOCOL;
2128 /* Remember hash of g^xy */
2129 memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
2130 } else if (reply_type == CELL_CREATED_FAST && !hop->dh_handshake_state) {
2131 if (fast_client_handshake(hop->fast_handshake_state, reply, keys,
2132 DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
2133 log_warn(LD_CIRC,"fast_client_handshake failed.");
2134 return -END_CIRC_REASON_TORPROTOCOL;
2136 memcpy(hop->handshake_digest, reply+DIGEST_LEN, DIGEST_LEN);
2137 } else {
2138 log_warn(LD_PROTOCOL,"CREATED cell type did not match CREATE cell type.");
2139 return -END_CIRC_REASON_TORPROTOCOL;
2142 crypto_dh_free(hop->dh_handshake_state); /* don't need it anymore */
2143 hop->dh_handshake_state = NULL;
2145 memset(hop->fast_handshake_state, 0, sizeof(hop->fast_handshake_state));
2147 if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
2148 return -END_CIRC_REASON_TORPROTOCOL;
2151 hop->state = CPATH_STATE_OPEN;
2152 log_info(LD_CIRC,"Finished building %scircuit hop:",
2153 (reply_type == CELL_CREATED_FAST) ? "fast " : "");
2154 circuit_log_path(LOG_INFO,LD_CIRC,circ);
2155 control_event_circuit_status(circ, CIRC_EVENT_EXTENDED, 0);
2157 return 0;
2160 /** We received a relay truncated cell on circ.
2162 * Since we don't ask for truncates currently, getting a truncated
2163 * means that a connection broke or an extend failed. For now,
2164 * just give up: for circ to close, and return 0.
2167 circuit_truncated(origin_circuit_t *circ, crypt_path_t *layer)
2169 // crypt_path_t *victim;
2170 // connection_t *stream;
2172 tor_assert(circ);
2173 tor_assert(layer);
2175 /* XXX Since we don't ask for truncates currently, getting a truncated
2176 * means that a connection broke or an extend failed. For now,
2177 * just give up.
2179 circuit_mark_for_close(TO_CIRCUIT(circ),
2180 END_CIRC_REASON_FLAG_REMOTE|END_CIRC_REASON_OR_CONN_CLOSED);
2181 return 0;
2183 #if 0
2184 while (layer->next != circ->cpath) {
2185 /* we need to clear out layer->next */
2186 victim = layer->next;
2187 log_debug(LD_CIRC, "Killing a layer of the cpath.");
2189 for (stream = circ->p_streams; stream; stream=stream->next_stream) {
2190 if (stream->cpath_layer == victim) {
2191 log_info(LD_APP, "Marking stream %d for close because of truncate.",
2192 stream->stream_id);
2193 /* no need to send 'end' relay cells,
2194 * because the other side's already dead
2196 connection_mark_unattached_ap(stream, END_STREAM_REASON_DESTROY);
2200 layer->next = victim->next;
2201 circuit_free_cpath_node(victim);
2204 log_info(LD_CIRC, "finished");
2205 return 0;
2206 #endif
2209 /** Given a response payload and keys, initialize, then send a created
2210 * cell back.
2213 onionskin_answer(or_circuit_t *circ, uint8_t cell_type, const char *payload,
2214 const char *keys)
2216 cell_t cell;
2217 crypt_path_t *tmp_cpath;
2219 tmp_cpath = tor_malloc_zero(sizeof(crypt_path_t));
2220 tmp_cpath->magic = CRYPT_PATH_MAGIC;
2222 memset(&cell, 0, sizeof(cell_t));
2223 cell.command = cell_type;
2224 cell.circ_id = circ->p_circ_id;
2226 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
2228 memcpy(cell.payload, payload,
2229 cell_type == CELL_CREATED ? ONIONSKIN_REPLY_LEN : DIGEST_LEN*2);
2231 log_debug(LD_CIRC,"init digest forward 0x%.8x, backward 0x%.8x.",
2232 (unsigned int)*(uint32_t*)(keys),
2233 (unsigned int)*(uint32_t*)(keys+20));
2234 if (circuit_init_cpath_crypto(tmp_cpath, keys, 0)<0) {
2235 log_warn(LD_BUG,"Circuit initialization failed");
2236 tor_free(tmp_cpath);
2237 return -1;
2239 circ->n_digest = tmp_cpath->f_digest;
2240 circ->n_crypto = tmp_cpath->f_crypto;
2241 circ->p_digest = tmp_cpath->b_digest;
2242 circ->p_crypto = tmp_cpath->b_crypto;
2243 tmp_cpath->magic = 0;
2244 tor_free(tmp_cpath);
2246 if (cell_type == CELL_CREATED)
2247 memcpy(circ->handshake_digest, cell.payload+DH_KEY_LEN, DIGEST_LEN);
2248 else
2249 memcpy(circ->handshake_digest, cell.payload+DIGEST_LEN, DIGEST_LEN);
2251 circ->is_first_hop = (cell_type == CELL_CREATED_FAST);
2253 append_cell_to_circuit_queue(TO_CIRCUIT(circ),
2254 circ->p_conn, &cell, CELL_DIRECTION_IN);
2255 log_debug(LD_CIRC,"Finished sending 'created' cell.");
2257 if (!is_local_addr(&circ->p_conn->_base.addr) &&
2258 !connection_or_nonopen_was_started_here(circ->p_conn)) {
2259 /* record that we could process create cells from a non-local conn
2260 * that we didn't initiate; presumably this means that create cells
2261 * can reach us too. */
2262 router_orport_found_reachable();
2265 return 0;
2268 /** Choose a length for a circuit of purpose <b>purpose</b>.
2269 * Default length is 3 + the number of endpoints that would give something
2270 * away. If the routerlist <b>routers</b> doesn't have enough routers
2271 * to handle the desired path length, return as large a path length as
2272 * is feasible, except if it's less than 2, in which case return -1.
2274 static int
2275 new_route_len(uint8_t purpose, extend_info_t *exit,
2276 smartlist_t *routers)
2278 int num_acceptable_routers;
2279 int routelen;
2281 tor_assert(routers);
2283 routelen = 3;
2284 if (exit &&
2285 purpose != CIRCUIT_PURPOSE_TESTING &&
2286 purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
2287 routelen++;
2289 num_acceptable_routers = count_acceptable_routers(routers);
2291 log_debug(LD_CIRC,"Chosen route length %d (%d/%d routers suitable).",
2292 routelen, num_acceptable_routers, smartlist_len(routers));
2294 if (num_acceptable_routers < 2) {
2295 log_info(LD_CIRC,
2296 "Not enough acceptable routers (%d). Discarding this circuit.",
2297 num_acceptable_routers);
2298 return -1;
2301 if (num_acceptable_routers < routelen) {
2302 log_info(LD_CIRC,"Not enough routers: cutting routelen from %d to %d.",
2303 routelen, num_acceptable_routers);
2304 routelen = num_acceptable_routers;
2307 return routelen;
2310 /** Fetch the list of predicted ports, dup it into a smartlist of
2311 * uint16_t's, remove the ones that are already handled by an
2312 * existing circuit, and return it.
2314 static smartlist_t *
2315 circuit_get_unhandled_ports(time_t now)
2317 smartlist_t *source = rep_hist_get_predicted_ports(now);
2318 smartlist_t *dest = smartlist_create();
2319 uint16_t *tmp;
2320 int i;
2322 for (i = 0; i < smartlist_len(source); ++i) {
2323 tmp = tor_malloc(sizeof(uint16_t));
2324 memcpy(tmp, smartlist_get(source, i), sizeof(uint16_t));
2325 smartlist_add(dest, tmp);
2328 circuit_remove_handled_ports(dest);
2329 return dest;
2332 /** Return 1 if we already have circuits present or on the way for
2333 * all anticipated ports. Return 0 if we should make more.
2335 * If we're returning 0, set need_uptime and need_capacity to
2336 * indicate any requirements that the unhandled ports have.
2339 circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
2340 int *need_capacity)
2342 int i, enough;
2343 uint16_t *port;
2344 smartlist_t *sl = circuit_get_unhandled_ports(now);
2345 smartlist_t *LongLivedServices = get_options()->LongLivedPorts;
2346 tor_assert(need_uptime);
2347 tor_assert(need_capacity);
2348 // Always predict need_capacity
2349 *need_capacity = 1;
2350 enough = (smartlist_len(sl) == 0);
2351 for (i = 0; i < smartlist_len(sl); ++i) {
2352 port = smartlist_get(sl, i);
2353 if (smartlist_string_num_isin(LongLivedServices, *port))
2354 *need_uptime = 1;
2355 tor_free(port);
2357 smartlist_free(sl);
2358 return enough;
2361 /** Return 1 if <b>router</b> can handle one or more of the ports in
2362 * <b>needed_ports</b>, else return 0.
2364 static int
2365 router_handles_some_port(routerinfo_t *router, smartlist_t *needed_ports)
2367 int i;
2368 uint16_t port;
2370 for (i = 0; i < smartlist_len(needed_ports); ++i) {
2371 addr_policy_result_t r;
2372 port = *(uint16_t *)smartlist_get(needed_ports, i);
2373 tor_assert(port);
2374 r = compare_addr_to_addr_policy(0, port, router->exit_policy);
2375 if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
2376 return 1;
2378 return 0;
2381 /** Return true iff <b>conn</b> needs another general circuit to be
2382 * built. */
2383 static int
2384 ap_stream_wants_exit_attention(connection_t *conn)
2386 if (conn->type == CONN_TYPE_AP &&
2387 conn->state == AP_CONN_STATE_CIRCUIT_WAIT &&
2388 !conn->marked_for_close &&
2389 !(TO_EDGE_CONN(conn)->want_onehop) && /* ignore one-hop streams */
2390 !(TO_EDGE_CONN(conn)->use_begindir) && /* ignore targeted dir fetches */
2391 !(TO_EDGE_CONN(conn)->chosen_exit_name) && /* ignore defined streams */
2392 !connection_edge_is_rendezvous_stream(TO_EDGE_CONN(conn)) &&
2393 !circuit_stream_is_being_handled(TO_EDGE_CONN(conn), 0,
2394 MIN_CIRCUITS_HANDLING_STREAM))
2395 return 1;
2396 return 0;
2399 /** Return a pointer to a suitable router to be the exit node for the
2400 * general-purpose circuit we're about to build.
2402 * Look through the connection array, and choose a router that maximizes
2403 * the number of pending streams that can exit from this router.
2405 * Return NULL if we can't find any suitable routers.
2407 static routerinfo_t *
2408 choose_good_exit_server_general(routerlist_t *dir, int need_uptime,
2409 int need_capacity)
2411 int *n_supported;
2412 int i;
2413 int n_pending_connections = 0;
2414 smartlist_t *connections;
2415 int best_support = -1;
2416 int n_best_support=0;
2417 routerinfo_t *router;
2418 or_options_t *options = get_options();
2420 connections = get_connection_array();
2422 /* Count how many connections are waiting for a circuit to be built.
2423 * We use this for log messages now, but in the future we may depend on it.
2425 SMARTLIST_FOREACH(connections, connection_t *, conn,
2427 if (ap_stream_wants_exit_attention(conn))
2428 ++n_pending_connections;
2430 // log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
2431 // n_pending_connections);
2432 /* Now we count, for each of the routers in the directory, how many
2433 * of the pending connections could possibly exit from that
2434 * router (n_supported[i]). (We can't be sure about cases where we
2435 * don't know the IP address of the pending connection.)
2437 * -1 means "Don't use this router at all."
2439 n_supported = tor_malloc(sizeof(int)*smartlist_len(dir->routers));
2440 for (i = 0; i < smartlist_len(dir->routers); ++i) {/* iterate over routers */
2441 router = smartlist_get(dir->routers, i);
2442 if (router_is_me(router)) {
2443 n_supported[i] = -1;
2444 // log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
2445 /* XXX there's probably a reverse predecessor attack here, but
2446 * it's slow. should we take this out? -RD
2448 continue;
2450 if (!router->is_running || router->is_bad_exit) {
2451 n_supported[i] = -1;
2452 continue; /* skip routers that are known to be down or bad exits */
2454 if (router_is_unreliable(router, need_uptime, need_capacity, 0) &&
2455 (!options->ExitNodes ||
2456 !routerset_contains_router(options->ExitNodes, router))) {
2457 /* FFFF Someday, differentiate between a routerset that names
2458 * routers, and a routerset that names countries, and only do this
2459 * check if they've asked for specific exit relays. Or if the country
2460 * they ask for is rare. Or something. */
2461 n_supported[i] = -1;
2462 continue; /* skip routers that are not suitable, unless we have
2463 * ExitNodes set, in which case we asked for it */
2465 if (!(router->is_valid || options->_AllowInvalid & ALLOW_INVALID_EXIT)) {
2466 /* if it's invalid and we don't want it */
2467 n_supported[i] = -1;
2468 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- invalid router.",
2469 // router->nickname, i);
2470 continue; /* skip invalid routers */
2472 if (options->ExcludeSingleHopRelays && router->allow_single_hop_exits) {
2473 n_supported[i] = -1;
2474 continue;
2476 if (router_exit_policy_rejects_all(router)) {
2477 n_supported[i] = -1;
2478 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
2479 // router->nickname, i);
2480 continue; /* skip routers that reject all */
2482 n_supported[i] = 0;
2483 /* iterate over connections */
2484 SMARTLIST_FOREACH(connections, connection_t *, conn,
2486 if (!ap_stream_wants_exit_attention(conn))
2487 continue; /* Skip everything but APs in CIRCUIT_WAIT */
2488 if (connection_ap_can_use_exit(TO_EDGE_CONN(conn), router, 1)) {
2489 ++n_supported[i];
2490 // log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
2491 // router->nickname, i, n_supported[i]);
2492 } else {
2493 // log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
2494 // router->nickname, i);
2496 }); /* End looping over connections. */
2497 if (n_pending_connections > 0 && n_supported[i] == 0) {
2498 /* Leave best_support at -1 if that's where it is, so we can
2499 * distinguish it later. */
2500 continue;
2502 if (n_supported[i] > best_support) {
2503 /* If this router is better than previous ones, remember its index
2504 * and goodness, and start counting how many routers are this good. */
2505 best_support = n_supported[i]; n_best_support=1;
2506 // log_fn(LOG_DEBUG,"%s is new best supported option so far.",
2507 // router->nickname);
2508 } else if (n_supported[i] == best_support) {
2509 /* If this router is _as good_ as the best one, just increment the
2510 * count of equally good routers.*/
2511 ++n_best_support;
2514 log_info(LD_CIRC,
2515 "Found %d servers that might support %d/%d pending connections.",
2516 n_best_support, best_support >= 0 ? best_support : 0,
2517 n_pending_connections);
2519 /* If any routers definitely support any pending connections, choose one
2520 * at random. */
2521 if (best_support > 0) {
2522 smartlist_t *supporting = smartlist_create(), *use = smartlist_create();
2524 for (i = 0; i < smartlist_len(dir->routers); i++)
2525 if (n_supported[i] == best_support)
2526 smartlist_add(supporting, smartlist_get(dir->routers, i));
2528 routersets_get_disjunction(use, supporting, options->ExitNodes,
2529 options->_ExcludeExitNodesUnion, 1);
2530 if (smartlist_len(use) == 0 && options->ExitNodes &&
2531 !options->StrictNodes) { /* give up on exitnodes and try again */
2532 routersets_get_disjunction(use, supporting, NULL,
2533 options->_ExcludeExitNodesUnion, 1);
2535 router = routerlist_sl_choose_by_bandwidth(use, WEIGHT_FOR_EXIT);
2536 smartlist_free(use);
2537 smartlist_free(supporting);
2538 } else {
2539 /* Either there are no pending connections, or no routers even seem to
2540 * possibly support any of them. Choose a router at random that satisfies
2541 * at least one predicted exit port. */
2543 int try;
2544 smartlist_t *needed_ports, *supporting, *use;
2546 if (best_support == -1) {
2547 if (need_uptime || need_capacity) {
2548 log_info(LD_CIRC,
2549 "We couldn't find any live%s%s routers; falling back "
2550 "to list of all routers.",
2551 need_capacity?", fast":"",
2552 need_uptime?", stable":"");
2553 tor_free(n_supported);
2554 return choose_good_exit_server_general(dir, 0, 0);
2556 log_notice(LD_CIRC, "All routers are down or won't exit%s -- "
2557 "choosing a doomed exit at random.",
2558 options->_ExcludeExitNodesUnion ? " or are Excluded" : "");
2560 supporting = smartlist_create();
2561 use = smartlist_create();
2562 needed_ports = circuit_get_unhandled_ports(time(NULL));
2563 for (try = 0; try < 2; try++) {
2564 /* try once to pick only from routers that satisfy a needed port,
2565 * then if there are none, pick from any that support exiting. */
2566 for (i = 0; i < smartlist_len(dir->routers); i++) {
2567 router = smartlist_get(dir->routers, i);
2568 if (n_supported[i] != -1 &&
2569 (try || router_handles_some_port(router, needed_ports))) {
2570 // log_fn(LOG_DEBUG,"Try %d: '%s' is a possibility.",
2571 // try, router->nickname);
2572 smartlist_add(supporting, router);
2576 routersets_get_disjunction(use, supporting, options->ExitNodes,
2577 options->_ExcludeExitNodesUnion, 1);
2578 if (smartlist_len(use) == 0 && options->ExitNodes &&
2579 !options->StrictNodes) { /* give up on exitnodes and try again */
2580 routersets_get_disjunction(use, supporting, NULL,
2581 options->_ExcludeExitNodesUnion, 1);
2583 /* FFF sometimes the above results in null, when the requested
2584 * exit node is considered down by the consensus. we should pick
2585 * it anyway, since the user asked for it. */
2586 router = routerlist_sl_choose_by_bandwidth(use, WEIGHT_FOR_EXIT);
2587 if (router)
2588 break;
2589 smartlist_clear(supporting);
2590 smartlist_clear(use);
2592 SMARTLIST_FOREACH(needed_ports, uint16_t *, cp, tor_free(cp));
2593 smartlist_free(needed_ports);
2594 smartlist_free(use);
2595 smartlist_free(supporting);
2598 tor_free(n_supported);
2599 if (router) {
2600 log_info(LD_CIRC, "Chose exit server '%s'", router->nickname);
2601 return router;
2603 if (options->ExitNodes && options->StrictNodes) {
2604 log_warn(LD_CIRC,
2605 "No specified exit routers seem to be running, and "
2606 "StrictNodes is set: can't choose an exit.");
2608 return NULL;
2611 /** Return a pointer to a suitable router to be the exit node for the
2612 * circuit of purpose <b>purpose</b> that we're about to build (or NULL
2613 * if no router is suitable).
2615 * For general-purpose circuits, pass it off to
2616 * choose_good_exit_server_general()
2618 * For client-side rendezvous circuits, choose a random node, weighted
2619 * toward the preferences in 'options'.
2621 static routerinfo_t *
2622 choose_good_exit_server(uint8_t purpose, routerlist_t *dir,
2623 int need_uptime, int need_capacity, int is_internal)
2625 or_options_t *options = get_options();
2626 router_crn_flags_t flags = 0;
2627 if (need_uptime)
2628 flags |= CRN_NEED_UPTIME;
2629 if (need_capacity)
2630 flags |= CRN_NEED_CAPACITY;
2632 switch (purpose) {
2633 case CIRCUIT_PURPOSE_C_GENERAL:
2634 if (options->_AllowInvalid & ALLOW_INVALID_MIDDLE)
2635 flags |= CRN_ALLOW_INVALID;
2636 if (is_internal) /* pick it like a middle hop */
2637 return router_choose_random_node(NULL, options->ExcludeNodes, flags);
2638 else
2639 return choose_good_exit_server_general(dir,need_uptime,need_capacity);
2640 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
2641 if (options->_AllowInvalid & ALLOW_INVALID_RENDEZVOUS)
2642 flags |= CRN_ALLOW_INVALID;
2643 return router_choose_random_node(NULL, options->ExcludeNodes, flags);
2645 log_warn(LD_BUG,"Unhandled purpose %d", purpose);
2646 tor_fragile_assert();
2647 return NULL;
2650 /** Log a warning if the user specified an exit for the circuit that
2651 * has been excluded from use by ExcludeNodes or ExcludeExitNodes. */
2652 static void
2653 warn_if_last_router_excluded(origin_circuit_t *circ, const extend_info_t *exit)
2655 or_options_t *options = get_options();
2656 routerset_t *rs = options->ExcludeNodes;
2657 const char *description;
2658 int domain = LD_CIRC;
2659 uint8_t purpose = circ->_base.purpose;
2661 if (circ->build_state->onehop_tunnel)
2662 return;
2664 switch (purpose)
2666 default:
2667 case CIRCUIT_PURPOSE_OR:
2668 case CIRCUIT_PURPOSE_INTRO_POINT:
2669 case CIRCUIT_PURPOSE_REND_POINT_WAITING:
2670 case CIRCUIT_PURPOSE_REND_ESTABLISHED:
2671 log_warn(LD_BUG, "Called on non-origin circuit (purpose %d)",
2672 (int)purpose);
2673 return;
2674 case CIRCUIT_PURPOSE_C_GENERAL:
2675 if (circ->build_state->is_internal)
2676 return;
2677 description = "Requested exit node";
2678 rs = options->_ExcludeExitNodesUnion;
2679 break;
2680 case CIRCUIT_PURPOSE_C_INTRODUCING:
2681 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
2682 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
2683 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
2684 case CIRCUIT_PURPOSE_S_CONNECT_REND:
2685 case CIRCUIT_PURPOSE_S_REND_JOINED:
2686 case CIRCUIT_PURPOSE_TESTING:
2687 return;
2688 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
2689 case CIRCUIT_PURPOSE_C_REND_READY:
2690 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
2691 case CIRCUIT_PURPOSE_C_REND_JOINED:
2692 description = "Chosen rendezvous point";
2693 domain = LD_BUG;
2694 break;
2695 case CIRCUIT_PURPOSE_CONTROLLER:
2696 rs = options->_ExcludeExitNodesUnion;
2697 description = "Controller-selected circuit target";
2698 break;
2701 if (routerset_contains_extendinfo(rs, exit)) {
2702 log_fn(LOG_WARN, domain, "%s '%s' is in ExcludeNodes%s. Using anyway "
2703 "(circuit purpose %d).",
2704 description,exit->nickname,
2705 rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
2706 (int)purpose);
2707 circuit_log_path(LOG_WARN, domain, circ);
2710 return;
2713 /** Decide a suitable length for circ's cpath, and pick an exit
2714 * router (or use <b>exit</b> if provided). Store these in the
2715 * cpath. Return 0 if ok, -1 if circuit should be closed. */
2716 static int
2717 onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit)
2719 cpath_build_state_t *state = circ->build_state;
2720 routerlist_t *rl = router_get_routerlist();
2722 if (state->onehop_tunnel) {
2723 log_debug(LD_CIRC, "Launching a one-hop circuit for dir tunnel.");
2724 state->desired_path_len = 1;
2725 } else {
2726 int r = new_route_len(circ->_base.purpose, exit, rl->routers);
2727 if (r < 1) /* must be at least 1 */
2728 return -1;
2729 state->desired_path_len = r;
2732 if (exit) { /* the circuit-builder pre-requested one */
2733 warn_if_last_router_excluded(circ, exit);
2734 log_info(LD_CIRC,"Using requested exit node '%s'", exit->nickname);
2735 exit = extend_info_dup(exit);
2736 } else { /* we have to decide one */
2737 routerinfo_t *router =
2738 choose_good_exit_server(circ->_base.purpose, rl, state->need_uptime,
2739 state->need_capacity, state->is_internal);
2740 if (!router) {
2741 log_warn(LD_CIRC,"failed to choose an exit server");
2742 return -1;
2744 exit = extend_info_from_router(router);
2746 state->chosen_exit = exit;
2747 return 0;
2750 /** Give <b>circ</b> a new exit destination to <b>exit</b>, and add a
2751 * hop to the cpath reflecting this. Don't send the next extend cell --
2752 * the caller will do this if it wants to.
2755 circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit)
2757 cpath_build_state_t *state;
2758 tor_assert(exit);
2759 tor_assert(circ);
2761 state = circ->build_state;
2762 tor_assert(state);
2763 extend_info_free(state->chosen_exit);
2764 state->chosen_exit = extend_info_dup(exit);
2766 ++circ->build_state->desired_path_len;
2767 onion_append_hop(&circ->cpath, exit);
2768 return 0;
2771 /** Take an open <b>circ</b>, and add a new hop at the end, based on
2772 * <b>info</b>. Set its state back to CIRCUIT_STATE_BUILDING, and then
2773 * send the next extend cell to begin connecting to that hop.
2776 circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit)
2778 int err_reason = 0;
2779 warn_if_last_router_excluded(circ, exit);
2780 circuit_append_new_exit(circ, exit);
2781 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
2782 if ((err_reason = circuit_send_next_onion_skin(circ))<0) {
2783 log_warn(LD_CIRC, "Couldn't extend circuit to new point '%s'.",
2784 exit->nickname);
2785 circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
2786 return -1;
2788 return 0;
2791 /** Return the number of routers in <b>routers</b> that are currently up
2792 * and available for building circuits through.
2794 static int
2795 count_acceptable_routers(smartlist_t *routers)
2797 int i, n;
2798 int num=0;
2799 routerinfo_t *r;
2801 n = smartlist_len(routers);
2802 for (i=0;i<n;i++) {
2803 r = smartlist_get(routers, i);
2804 // log_debug(LD_CIRC,
2805 // "Contemplating whether router %d (%s) is a new option.",
2806 // i, r->nickname);
2807 if (r->is_running == 0) {
2808 // log_debug(LD_CIRC,"Nope, the directory says %d is not running.",i);
2809 goto next_i_loop;
2811 if (r->is_valid == 0) {
2812 // log_debug(LD_CIRC,"Nope, the directory says %d is not valid.",i);
2813 goto next_i_loop;
2814 /* XXX This clause makes us count incorrectly: if AllowInvalidRouters
2815 * allows this node in some places, then we're getting an inaccurate
2816 * count. For now, be conservative and don't count it. But later we
2817 * should try to be smarter. */
2819 num++;
2820 // log_debug(LD_CIRC,"I like %d. num_acceptable_routers now %d.",i, num);
2821 next_i_loop:
2822 ; /* C requires an explicit statement after the label */
2825 return num;
2828 /** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
2829 * This function is used to extend cpath by another hop.
2831 void
2832 onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop)
2834 if (*head_ptr) {
2835 new_hop->next = (*head_ptr);
2836 new_hop->prev = (*head_ptr)->prev;
2837 (*head_ptr)->prev->next = new_hop;
2838 (*head_ptr)->prev = new_hop;
2839 } else {
2840 *head_ptr = new_hop;
2841 new_hop->prev = new_hop->next = new_hop;
2845 /** A helper function used by onion_extend_cpath(). Use <b>purpose</b>
2846 * and <b>state</b> and the cpath <b>head</b> (currently populated only
2847 * to length <b>cur_len</b> to decide a suitable middle hop for a
2848 * circuit. In particular, make sure we don't pick the exit node or its
2849 * family, and make sure we don't duplicate any previous nodes or their
2850 * families. */
2851 static routerinfo_t *
2852 choose_good_middle_server(uint8_t purpose,
2853 cpath_build_state_t *state,
2854 crypt_path_t *head,
2855 int cur_len)
2857 int i;
2858 routerinfo_t *r, *choice;
2859 crypt_path_t *cpath;
2860 smartlist_t *excluded;
2861 or_options_t *options = get_options();
2862 router_crn_flags_t flags = 0;
2863 tor_assert(_CIRCUIT_PURPOSE_MIN <= purpose &&
2864 purpose <= _CIRCUIT_PURPOSE_MAX);
2866 log_debug(LD_CIRC, "Contemplating intermediate hop: random choice.");
2867 excluded = smartlist_create();
2868 if ((r = build_state_get_exit_router(state))) {
2869 smartlist_add(excluded, r);
2870 routerlist_add_family(excluded, r);
2872 for (i = 0, cpath = head; i < cur_len; ++i, cpath=cpath->next) {
2873 if ((r = router_get_by_digest(cpath->extend_info->identity_digest))) {
2874 smartlist_add(excluded, r);
2875 routerlist_add_family(excluded, r);
2879 if (state->need_uptime)
2880 flags |= CRN_NEED_UPTIME;
2881 if (state->need_capacity)
2882 flags |= CRN_NEED_CAPACITY;
2883 if (options->_AllowInvalid & ALLOW_INVALID_MIDDLE)
2884 flags |= CRN_ALLOW_INVALID;
2885 choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
2886 smartlist_free(excluded);
2887 return choice;
2890 /** Pick a good entry server for the circuit to be built according to
2891 * <b>state</b>. Don't reuse a chosen exit (if any), don't use this
2892 * router (if we're an OR), and respect firewall settings; if we're
2893 * configured to use entry guards, return one.
2895 * If <b>state</b> is NULL, we're choosing a router to serve as an entry
2896 * guard, not for any particular circuit.
2898 static routerinfo_t *
2899 choose_good_entry_server(uint8_t purpose, cpath_build_state_t *state)
2901 routerinfo_t *r, *choice;
2902 smartlist_t *excluded;
2903 or_options_t *options = get_options();
2904 router_crn_flags_t flags = CRN_NEED_GUARD;
2906 if (state && options->UseEntryGuards &&
2907 (purpose != CIRCUIT_PURPOSE_TESTING || options->BridgeRelay)) {
2908 return choose_random_entry(state);
2911 excluded = smartlist_create();
2913 if (state && (r = build_state_get_exit_router(state))) {
2914 smartlist_add(excluded, r);
2915 routerlist_add_family(excluded, r);
2917 if (firewall_is_fascist_or()) {
2918 /*XXXX This could slow things down a lot; use a smarter implementation */
2919 /* exclude all ORs that listen on the wrong port, if anybody notices. */
2920 routerlist_t *rl = router_get_routerlist();
2921 int i;
2923 for (i=0; i < smartlist_len(rl->routers); i++) {
2924 r = smartlist_get(rl->routers, i);
2925 if (!fascist_firewall_allows_or(r))
2926 smartlist_add(excluded, r);
2929 /* and exclude current entry guards, if applicable */
2930 if (options->UseEntryGuards && entry_guards) {
2931 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
2933 if ((r = router_get_by_digest(entry->identity))) {
2934 smartlist_add(excluded, r);
2935 routerlist_add_family(excluded, r);
2940 if (state) {
2941 if (state->need_uptime)
2942 flags |= CRN_NEED_UPTIME;
2943 if (state->need_capacity)
2944 flags |= CRN_NEED_CAPACITY;
2946 if (options->_AllowInvalid & ALLOW_INVALID_ENTRY)
2947 flags |= CRN_ALLOW_INVALID;
2949 choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
2950 smartlist_free(excluded);
2951 return choice;
2954 /** Return the first non-open hop in cpath, or return NULL if all
2955 * hops are open. */
2956 static crypt_path_t *
2957 onion_next_hop_in_cpath(crypt_path_t *cpath)
2959 crypt_path_t *hop = cpath;
2960 do {
2961 if (hop->state != CPATH_STATE_OPEN)
2962 return hop;
2963 hop = hop->next;
2964 } while (hop != cpath);
2965 return NULL;
2968 /** Choose a suitable next hop in the cpath <b>head_ptr</b>,
2969 * based on <b>state</b>. Append the hop info to head_ptr.
2971 static int
2972 onion_extend_cpath(origin_circuit_t *circ)
2974 uint8_t purpose = circ->_base.purpose;
2975 cpath_build_state_t *state = circ->build_state;
2976 int cur_len = circuit_get_cpath_len(circ);
2977 extend_info_t *info = NULL;
2979 if (cur_len >= state->desired_path_len) {
2980 log_debug(LD_CIRC, "Path is complete: %d steps long",
2981 state->desired_path_len);
2982 return 1;
2985 log_debug(LD_CIRC, "Path is %d long; we want %d", cur_len,
2986 state->desired_path_len);
2988 if (cur_len == state->desired_path_len - 1) { /* Picking last node */
2989 info = extend_info_dup(state->chosen_exit);
2990 } else if (cur_len == 0) { /* picking first node */
2991 routerinfo_t *r = choose_good_entry_server(purpose, state);
2992 if (r)
2993 info = extend_info_from_router(r);
2994 } else {
2995 routerinfo_t *r =
2996 choose_good_middle_server(purpose, state, circ->cpath, cur_len);
2997 if (r)
2998 info = extend_info_from_router(r);
3001 if (!info) {
3002 log_warn(LD_CIRC,"Failed to find node for hop %d of our path. Discarding "
3003 "this circuit.", cur_len);
3004 return -1;
3007 log_debug(LD_CIRC,"Chose router %s for hop %d (exit is %s)",
3008 info->nickname, cur_len+1, build_state_get_exit_nickname(state));
3010 onion_append_hop(&circ->cpath, info);
3011 extend_info_free(info);
3012 return 0;
3015 /** Create a new hop, annotate it with information about its
3016 * corresponding router <b>choice</b>, and append it to the
3017 * end of the cpath <b>head_ptr</b>. */
3018 static int
3019 onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
3021 crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
3023 /* link hop into the cpath, at the end. */
3024 onion_append_to_cpath(head_ptr, hop);
3026 hop->magic = CRYPT_PATH_MAGIC;
3027 hop->state = CPATH_STATE_CLOSED;
3029 hop->extend_info = extend_info_dup(choice);
3031 hop->package_window = circuit_initial_package_window();
3032 hop->deliver_window = CIRCWINDOW_START;
3034 return 0;
3037 /** Allocate a new extend_info object based on the various arguments. */
3038 extend_info_t *
3039 extend_info_alloc(const char *nickname, const char *digest,
3040 crypto_pk_env_t *onion_key,
3041 const tor_addr_t *addr, uint16_t port)
3043 extend_info_t *info = tor_malloc_zero(sizeof(extend_info_t));
3044 memcpy(info->identity_digest, digest, DIGEST_LEN);
3045 if (nickname)
3046 strlcpy(info->nickname, nickname, sizeof(info->nickname));
3047 if (onion_key)
3048 info->onion_key = crypto_pk_dup_key(onion_key);
3049 tor_addr_copy(&info->addr, addr);
3050 info->port = port;
3051 return info;
3054 /** Allocate and return a new extend_info_t that can be used to build a
3055 * circuit to or through the router <b>r</b>. */
3056 extend_info_t *
3057 extend_info_from_router(routerinfo_t *r)
3059 tor_addr_t addr;
3060 tor_assert(r);
3061 tor_addr_from_ipv4h(&addr, r->addr);
3062 return extend_info_alloc(r->nickname, r->cache_info.identity_digest,
3063 r->onion_pkey, &addr, r->or_port);
3066 /** Release storage held by an extend_info_t struct. */
3067 void
3068 extend_info_free(extend_info_t *info)
3070 if (!info)
3071 return;
3072 crypto_free_pk_env(info->onion_key);
3073 tor_free(info);
3076 /** Allocate and return a new extend_info_t with the same contents as
3077 * <b>info</b>. */
3078 extend_info_t *
3079 extend_info_dup(extend_info_t *info)
3081 extend_info_t *newinfo;
3082 tor_assert(info);
3083 newinfo = tor_malloc(sizeof(extend_info_t));
3084 memcpy(newinfo, info, sizeof(extend_info_t));
3085 if (info->onion_key)
3086 newinfo->onion_key = crypto_pk_dup_key(info->onion_key);
3087 else
3088 newinfo->onion_key = NULL;
3089 return newinfo;
3092 /** Return the routerinfo_t for the chosen exit router in <b>state</b>.
3093 * If there is no chosen exit, or if we don't know the routerinfo_t for
3094 * the chosen exit, return NULL.
3096 routerinfo_t *
3097 build_state_get_exit_router(cpath_build_state_t *state)
3099 if (!state || !state->chosen_exit)
3100 return NULL;
3101 return router_get_by_digest(state->chosen_exit->identity_digest);
3104 /** Return the nickname for the chosen exit router in <b>state</b>. If
3105 * there is no chosen exit, or if we don't know the routerinfo_t for the
3106 * chosen exit, return NULL.
3108 const char *
3109 build_state_get_exit_nickname(cpath_build_state_t *state)
3111 if (!state || !state->chosen_exit)
3112 return NULL;
3113 return state->chosen_exit->nickname;
3116 /** Check whether the entry guard <b>e</b> is usable, given the directory
3117 * authorities' opinion about the router (stored in <b>ri</b>) and the user's
3118 * configuration (in <b>options</b>). Set <b>e</b>-&gt;bad_since
3119 * accordingly. Return true iff the entry guard's status changes.
3121 * If it's not usable, set *<b>reason</b> to a static string explaining why.
3123 /*XXXX take a routerstatus, not a routerinfo. */
3124 static int
3125 entry_guard_set_status(entry_guard_t *e, routerinfo_t *ri,
3126 time_t now, or_options_t *options, const char **reason)
3128 char buf[HEX_DIGEST_LEN+1];
3129 int changed = 0;
3131 tor_assert(options);
3133 *reason = NULL;
3135 /* Do we want to mark this guard as bad? */
3136 if (!ri)
3137 *reason = "unlisted";
3138 else if (!ri->is_running)
3139 *reason = "down";
3140 else if (options->UseBridges && ri->purpose != ROUTER_PURPOSE_BRIDGE)
3141 *reason = "not a bridge";
3142 else if (!options->UseBridges && !ri->is_possible_guard &&
3143 !routerset_contains_router(options->EntryNodes,ri))
3144 *reason = "not recommended as a guard";
3145 else if (routerset_contains_router(options->ExcludeNodes, ri))
3146 *reason = "excluded";
3148 if (*reason && ! e->bad_since) {
3149 /* Router is newly bad. */
3150 base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
3151 log_info(LD_CIRC, "Entry guard %s (%s) is %s: marking as unusable.",
3152 e->nickname, buf, *reason);
3154 e->bad_since = now;
3155 control_event_guard(e->nickname, e->identity, "BAD");
3156 changed = 1;
3157 } else if (!*reason && e->bad_since) {
3158 /* There's nothing wrong with the router any more. */
3159 base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
3160 log_info(LD_CIRC, "Entry guard %s (%s) is no longer unusable: "
3161 "marking as ok.", e->nickname, buf);
3163 e->bad_since = 0;
3164 control_event_guard(e->nickname, e->identity, "GOOD");
3165 changed = 1;
3167 return changed;
3170 /** Return true iff enough time has passed since we last tried to connect
3171 * to the unreachable guard <b>e</b> that we're willing to try again. */
3172 static int
3173 entry_is_time_to_retry(entry_guard_t *e, time_t now)
3175 long diff;
3176 if (e->last_attempted < e->unreachable_since)
3177 return 1;
3178 diff = now - e->unreachable_since;
3179 if (diff < 6*60*60)
3180 return now > (e->last_attempted + 60*60);
3181 else if (diff < 3*24*60*60)
3182 return now > (e->last_attempted + 4*60*60);
3183 else if (diff < 7*24*60*60)
3184 return now > (e->last_attempted + 18*60*60);
3185 else
3186 return now > (e->last_attempted + 36*60*60);
3189 /** Return the router corresponding to <b>e</b>, if <b>e</b> is
3190 * working well enough that we are willing to use it as an entry
3191 * right now. (Else return NULL.) In particular, it must be
3192 * - Listed as either up or never yet contacted;
3193 * - Present in the routerlist;
3194 * - Listed as 'stable' or 'fast' by the current dirserver consensus,
3195 * if demanded by <b>need_uptime</b> or <b>need_capacity</b>
3196 * (unless it's a configured EntryNode);
3197 * - Allowed by our current ReachableORAddresses config option; and
3198 * - Currently thought to be reachable by us (unless <b>assume_reachable</b>
3199 * is true).
3201 * If the answer is no, set *<b>msg</b> to an explanation of why.
3203 static INLINE routerinfo_t *
3204 entry_is_live(entry_guard_t *e, int need_uptime, int need_capacity,
3205 int assume_reachable, const char **msg)
3207 routerinfo_t *r;
3208 or_options_t *options = get_options();
3209 tor_assert(msg);
3211 if (e->bad_since) {
3212 *msg = "bad";
3213 return NULL;
3215 /* no good if it's unreachable, unless assume_unreachable or can_retry. */
3216 if (!assume_reachable && !e->can_retry &&
3217 e->unreachable_since && !entry_is_time_to_retry(e, time(NULL))) {
3218 *msg = "unreachable";
3219 return NULL;
3221 r = router_get_by_digest(e->identity);
3222 if (!r) {
3223 *msg = "no descriptor";
3224 return NULL;
3226 if (get_options()->UseBridges && r->purpose != ROUTER_PURPOSE_BRIDGE) {
3227 *msg = "not a bridge";
3228 return NULL;
3230 if (!get_options()->UseBridges && r->purpose != ROUTER_PURPOSE_GENERAL) {
3231 *msg = "not general-purpose";
3232 return NULL;
3234 if (options->EntryNodes &&
3235 routerset_contains_router(options->EntryNodes, r)) {
3236 /* they asked for it, they get it */
3237 need_uptime = need_capacity = 0;
3239 if (router_is_unreliable(r, need_uptime, need_capacity, 0)) {
3240 *msg = "not fast/stable";
3241 return NULL;
3243 if (!fascist_firewall_allows_or(r)) {
3244 *msg = "unreachable by config";
3245 return NULL;
3247 return r;
3250 /** Return the number of entry guards that we think are usable. */
3251 static int
3252 num_live_entry_guards(void)
3254 int n = 0;
3255 const char *msg;
3256 if (! entry_guards)
3257 return 0;
3258 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3260 if (entry_is_live(entry, 0, 1, 0, &msg))
3261 ++n;
3263 return n;
3266 /** If <b>digest</b> matches the identity of any node in the
3267 * entry_guards list, return that node. Else return NULL. */
3268 static INLINE entry_guard_t *
3269 is_an_entry_guard(const char *digest)
3271 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3272 if (!memcmp(digest, entry->identity, DIGEST_LEN))
3273 return entry;
3275 return NULL;
3278 /** Dump a description of our list of entry guards to the log at level
3279 * <b>severity</b>. */
3280 static void
3281 log_entry_guards(int severity)
3283 smartlist_t *elements = smartlist_create();
3284 char *s;
3286 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
3288 const char *msg = NULL;
3289 char *cp;
3290 if (entry_is_live(e, 0, 1, 0, &msg))
3291 tor_asprintf(&cp, "%s (up %s)",
3292 e->nickname,
3293 e->made_contact ? "made-contact" : "never-contacted");
3294 else
3295 tor_asprintf(&cp, "%s (%s, %s)",
3296 e->nickname, msg,
3297 e->made_contact ? "made-contact" : "never-contacted");
3298 smartlist_add(elements, cp);
3301 s = smartlist_join_strings(elements, ",", 0, NULL);
3302 SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
3303 smartlist_free(elements);
3304 log_fn(severity,LD_CIRC,"%s",s);
3305 tor_free(s);
3308 /** Called when one or more guards that we would previously have used for some
3309 * purpose are no longer in use because a higher-priority guard has become
3310 * usable again. */
3311 static void
3312 control_event_guard_deferred(void)
3314 /* XXXX We don't actually have a good way to figure out _how many_ entries
3315 * are live for some purpose. We need an entry_is_even_slightly_live()
3316 * function for this to work right. NumEntryGuards isn't reliable: if we
3317 * need guards with weird properties, we can have more than that number
3318 * live.
3320 #if 0
3321 int n = 0;
3322 const char *msg;
3323 or_options_t *options = get_options();
3324 if (!entry_guards)
3325 return;
3326 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3328 if (entry_is_live(entry, 0, 1, 0, &msg)) {
3329 if (n++ == options->NumEntryGuards) {
3330 control_event_guard(entry->nickname, entry->identity, "DEFERRED");
3331 return;
3335 #endif
3338 /** Add a new (preferably stable and fast) router to our
3339 * entry_guards list. Return a pointer to the router if we succeed,
3340 * or NULL if we can't find any more suitable entries.
3342 * If <b>chosen</b> is defined, use that one, and if it's not
3343 * already in our entry_guards list, put it at the *beginning*.
3344 * Else, put the one we pick at the end of the list. */
3345 static routerinfo_t *
3346 add_an_entry_guard(routerinfo_t *chosen, int reset_status)
3348 routerinfo_t *router;
3349 entry_guard_t *entry;
3351 if (chosen) {
3352 router = chosen;
3353 entry = is_an_entry_guard(router->cache_info.identity_digest);
3354 if (entry) {
3355 if (reset_status) {
3356 entry->bad_since = 0;
3357 entry->can_retry = 1;
3359 return NULL;
3361 } else {
3362 router = choose_good_entry_server(CIRCUIT_PURPOSE_C_GENERAL, NULL);
3363 if (!router)
3364 return NULL;
3366 entry = tor_malloc_zero(sizeof(entry_guard_t));
3367 log_info(LD_CIRC, "Chose '%s' as new entry guard.", router->nickname);
3368 strlcpy(entry->nickname, router->nickname, sizeof(entry->nickname));
3369 memcpy(entry->identity, router->cache_info.identity_digest, DIGEST_LEN);
3370 /* Choose expiry time smudged over the past month. The goal here
3371 * is to a) spread out when Tor clients rotate their guards, so they
3372 * don't all select them on the same day, and b) avoid leaving a
3373 * precise timestamp in the state file about when we first picked
3374 * this guard. For details, see the Jan 2010 or-dev thread. */
3375 entry->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
3376 entry->chosen_by_version = tor_strdup(VERSION);
3377 if (chosen) /* prepend */
3378 smartlist_insert(entry_guards, 0, entry);
3379 else /* append */
3380 smartlist_add(entry_guards, entry);
3381 control_event_guard(entry->nickname, entry->identity, "NEW");
3382 control_event_guard_deferred();
3383 log_entry_guards(LOG_INFO);
3384 return router;
3387 /** If the use of entry guards is configured, choose more entry guards
3388 * until we have enough in the list. */
3389 static void
3390 pick_entry_guards(void)
3392 or_options_t *options = get_options();
3393 int changed = 0;
3395 tor_assert(entry_guards);
3397 while (num_live_entry_guards() < options->NumEntryGuards) {
3398 if (!add_an_entry_guard(NULL, 0))
3399 break;
3400 changed = 1;
3402 if (changed)
3403 entry_guards_changed();
3406 /** How long (in seconds) do we allow an entry guard to be nonfunctional,
3407 * unlisted, excluded, or otherwise nonusable before we give up on it? */
3408 #define ENTRY_GUARD_REMOVE_AFTER (30*24*60*60)
3410 /** Release all storage held by <b>e</b>. */
3411 static void
3412 entry_guard_free(entry_guard_t *e)
3414 if (!e)
3415 return;
3416 tor_free(e->chosen_by_version);
3417 tor_free(e);
3420 /** Remove any entry guard which was selected by an unknown version of Tor,
3421 * or which was selected by a version of Tor that's known to select
3422 * entry guards badly. */
3423 static int
3424 remove_obsolete_entry_guards(void)
3426 int changed = 0, i;
3427 time_t now = time(NULL);
3429 for (i = 0; i < smartlist_len(entry_guards); ++i) {
3430 entry_guard_t *entry = smartlist_get(entry_guards, i);
3431 const char *ver = entry->chosen_by_version;
3432 const char *msg = NULL;
3433 tor_version_t v;
3434 int version_is_bad = 0, date_is_bad = 0;
3435 if (!ver) {
3436 msg = "does not say what version of Tor it was selected by";
3437 version_is_bad = 1;
3438 } else if (tor_version_parse(ver, &v)) {
3439 msg = "does not seem to be from any recognized version of Tor";
3440 version_is_bad = 1;
3441 } else {
3442 size_t len = strlen(ver)+5;
3443 char *tor_ver = tor_malloc(len);
3444 tor_snprintf(tor_ver, len, "Tor %s", ver);
3445 if ((tor_version_as_new_as(tor_ver, "0.1.0.10-alpha") &&
3446 !tor_version_as_new_as(tor_ver, "0.1.2.16-dev")) ||
3447 (tor_version_as_new_as(tor_ver, "0.2.0.0-alpha") &&
3448 !tor_version_as_new_as(tor_ver, "0.2.0.6-alpha")) ||
3449 /* above are bug 440; below are bug 1217 */
3450 (tor_version_as_new_as(tor_ver, "0.2.1.3-alpha") &&
3451 !tor_version_as_new_as(tor_ver, "0.2.1.23")) ||
3452 (tor_version_as_new_as(tor_ver, "0.2.2.0-alpha") &&
3453 !tor_version_as_new_as(tor_ver, "0.2.2.7-alpha"))) {
3454 msg = "was selected without regard for guard bandwidth";
3455 version_is_bad = 1;
3457 tor_free(tor_ver);
3459 if (!version_is_bad && entry->chosen_on_date + 3600*24*60 < now) {
3460 /* It's been 2 months since the date listed in our state file. */
3461 msg = "was selected several months ago";
3462 date_is_bad = 1;
3465 if (version_is_bad || date_is_bad) { /* we need to drop it */
3466 char dbuf[HEX_DIGEST_LEN+1];
3467 tor_assert(msg);
3468 base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
3469 log_fn(version_is_bad ? LOG_NOTICE : LOG_INFO, LD_CIRC,
3470 "Entry guard '%s' (%s) %s. (Version=%s.) Replacing it.",
3471 entry->nickname, dbuf, msg, ver?escaped(ver):"none");
3472 control_event_guard(entry->nickname, entry->identity, "DROPPED");
3473 entry_guard_free(entry);
3474 smartlist_del_keeporder(entry_guards, i--);
3475 log_entry_guards(LOG_INFO);
3476 changed = 1;
3480 return changed ? 1 : 0;
3483 /** Remove all entry guards that have been down or unlisted for so
3484 * long that we don't think they'll come up again. Return 1 if we
3485 * removed any, or 0 if we did nothing. */
3486 static int
3487 remove_dead_entry_guards(void)
3489 char dbuf[HEX_DIGEST_LEN+1];
3490 char tbuf[ISO_TIME_LEN+1];
3491 time_t now = time(NULL);
3492 int i;
3493 int changed = 0;
3495 for (i = 0; i < smartlist_len(entry_guards); ) {
3496 entry_guard_t *entry = smartlist_get(entry_guards, i);
3497 if (entry->bad_since &&
3498 entry->bad_since + ENTRY_GUARD_REMOVE_AFTER < now) {
3500 base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
3501 format_local_iso_time(tbuf, entry->bad_since);
3502 log_info(LD_CIRC, "Entry guard '%s' (%s) has been down or unlisted "
3503 "since %s local time; removing.",
3504 entry->nickname, dbuf, tbuf);
3505 control_event_guard(entry->nickname, entry->identity, "DROPPED");
3506 entry_guard_free(entry);
3507 smartlist_del_keeporder(entry_guards, i);
3508 log_entry_guards(LOG_INFO);
3509 changed = 1;
3510 } else
3511 ++i;
3513 return changed ? 1 : 0;
3516 /** A new directory or router-status has arrived; update the down/listed
3517 * status of the entry guards.
3519 * An entry is 'down' if the directory lists it as nonrunning.
3520 * An entry is 'unlisted' if the directory doesn't include it.
3522 * Don't call this on startup; only on a fresh download. Otherwise we'll
3523 * think that things are unlisted.
3525 void
3526 entry_guards_compute_status(void)
3528 time_t now;
3529 int changed = 0;
3530 int severity = LOG_DEBUG;
3531 or_options_t *options;
3532 digestmap_t *reasons;
3534 if (! entry_guards)
3535 return;
3537 options = get_options();
3538 if (options->EntryNodes) /* reshuffle the entry guard list if needed */
3539 entry_nodes_should_be_added();
3541 now = time(NULL);
3543 reasons = digestmap_new();
3544 SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, entry)
3546 routerinfo_t *r = router_get_by_digest(entry->identity);
3547 const char *reason = NULL;
3548 if (entry_guard_set_status(entry, r, now, options, &reason))
3549 changed = 1;
3551 if (entry->bad_since)
3552 tor_assert(reason);
3553 if (reason)
3554 digestmap_set(reasons, entry->identity, (char*)reason);
3556 SMARTLIST_FOREACH_END(entry);
3558 if (remove_dead_entry_guards())
3559 changed = 1;
3561 severity = changed ? LOG_DEBUG : LOG_INFO;
3563 if (changed) {
3564 SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, entry) {
3565 const char *reason = digestmap_get(reasons, entry->identity);
3566 const char *live_msg = "";
3567 routerinfo_t *r = entry_is_live(entry, 0, 1, 0, &live_msg);
3568 log_info(LD_CIRC, "Summary: Entry '%s' is %s, %s%s%s, and %s%s.",
3569 entry->nickname,
3570 entry->unreachable_since ? "unreachable" : "reachable",
3571 entry->bad_since ? "unusable" : "usable",
3572 reason ? ", ": "",
3573 reason ? reason : "",
3574 r ? "live" : "not live / ",
3575 r ? "" : live_msg);
3576 } SMARTLIST_FOREACH_END(entry);
3577 log_info(LD_CIRC, " (%d/%d entry guards are usable/new)",
3578 num_live_entry_guards(), smartlist_len(entry_guards));
3579 log_entry_guards(LOG_INFO);
3580 entry_guards_changed();
3583 digestmap_free(reasons, NULL);
3586 /** Called when a connection to an OR with the identity digest <b>digest</b>
3587 * is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
3588 * If the OR is an entry, change that entry's up/down status.
3589 * Return 0 normally, or -1 if we want to tear down the new connection.
3591 * If <b>mark_relay_status</b>, also call router_set_status() on this
3592 * relay.
3594 * XXX022 change succeeded and mark_relay_status into 'int flags'.
3597 entry_guard_register_connect_status(const char *digest, int succeeded,
3598 int mark_relay_status, time_t now)
3600 int changed = 0;
3601 int refuse_conn = 0;
3602 int first_contact = 0;
3603 entry_guard_t *entry = NULL;
3604 int idx = -1;
3605 char buf[HEX_DIGEST_LEN+1];
3607 if (! entry_guards)
3608 return 0;
3610 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
3612 if (!memcmp(e->identity, digest, DIGEST_LEN)) {
3613 entry = e;
3614 idx = e_sl_idx;
3615 break;
3619 if (!entry)
3620 return 0;
3622 base16_encode(buf, sizeof(buf), entry->identity, DIGEST_LEN);
3624 if (succeeded) {
3625 if (entry->unreachable_since) {
3626 log_info(LD_CIRC, "Entry guard '%s' (%s) is now reachable again. Good.",
3627 entry->nickname, buf);
3628 entry->can_retry = 0;
3629 entry->unreachable_since = 0;
3630 entry->last_attempted = now;
3631 control_event_guard(entry->nickname, entry->identity, "UP");
3632 changed = 1;
3634 if (!entry->made_contact) {
3635 entry->made_contact = 1;
3636 first_contact = changed = 1;
3638 } else { /* ! succeeded */
3639 if (!entry->made_contact) {
3640 /* We've never connected to this one. */
3641 log_info(LD_CIRC,
3642 "Connection to never-contacted entry guard '%s' (%s) failed. "
3643 "Removing from the list. %d/%d entry guards usable/new.",
3644 entry->nickname, buf,
3645 num_live_entry_guards()-1, smartlist_len(entry_guards)-1);
3646 control_event_guard(entry->nickname, entry->identity, "DROPPED");
3647 entry_guard_free(entry);
3648 smartlist_del_keeporder(entry_guards, idx);
3649 log_entry_guards(LOG_INFO);
3650 changed = 1;
3651 } else if (!entry->unreachable_since) {
3652 log_info(LD_CIRC, "Unable to connect to entry guard '%s' (%s). "
3653 "Marking as unreachable.", entry->nickname, buf);
3654 entry->unreachable_since = entry->last_attempted = now;
3655 control_event_guard(entry->nickname, entry->identity, "DOWN");
3656 changed = 1;
3657 entry->can_retry = 0; /* We gave it an early chance; no good. */
3658 } else {
3659 char tbuf[ISO_TIME_LEN+1];
3660 format_iso_time(tbuf, entry->unreachable_since);
3661 log_debug(LD_CIRC, "Failed to connect to unreachable entry guard "
3662 "'%s' (%s). It has been unreachable since %s.",
3663 entry->nickname, buf, tbuf);
3664 entry->last_attempted = now;
3665 entry->can_retry = 0; /* We gave it an early chance; no good. */
3669 /* if the caller asked us to, also update the is_running flags for this
3670 * relay */
3671 if (mark_relay_status)
3672 router_set_status(digest, succeeded);
3674 if (first_contact) {
3675 /* We've just added a new long-term entry guard. Perhaps the network just
3676 * came back? We should give our earlier entries another try too,
3677 * and close this connection so we don't use it before we've given
3678 * the others a shot. */
3679 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e, {
3680 if (e == entry)
3681 break;
3682 if (e->made_contact) {
3683 const char *msg;
3684 routerinfo_t *r = entry_is_live(e, 0, 1, 1, &msg);
3685 if (r && e->unreachable_since) {
3686 refuse_conn = 1;
3687 e->can_retry = 1;
3691 if (refuse_conn) {
3692 log_info(LD_CIRC,
3693 "Connected to new entry guard '%s' (%s). Marking earlier "
3694 "entry guards up. %d/%d entry guards usable/new.",
3695 entry->nickname, buf,
3696 num_live_entry_guards(), smartlist_len(entry_guards));
3697 log_entry_guards(LOG_INFO);
3698 changed = 1;
3702 if (changed)
3703 entry_guards_changed();
3704 return refuse_conn ? -1 : 0;
3707 /** When we try to choose an entry guard, should we parse and add
3708 * config's EntryNodes first? */
3709 static int should_add_entry_nodes = 0;
3711 /** Called when the value of EntryNodes changes in our configuration. */
3712 void
3713 entry_nodes_should_be_added(void)
3715 log_info(LD_CIRC, "EntryNodes config option set. Putting configured "
3716 "relays at the front of the entry guard list.");
3717 should_add_entry_nodes = 1;
3720 /** Add all nodes in EntryNodes that aren't currently guard nodes to the list
3721 * of guard nodes, at the front. */
3722 static void
3723 entry_guards_prepend_from_config(void)
3725 or_options_t *options = get_options();
3726 smartlist_t *entry_routers, *entry_fps;
3727 smartlist_t *old_entry_guards_on_list, *old_entry_guards_not_on_list;
3728 tor_assert(entry_guards);
3730 should_add_entry_nodes = 0;
3732 if (!options->EntryNodes) {
3733 /* It's possible that a controller set EntryNodes, thus making
3734 * should_add_entry_nodes set, then cleared it again, all before the
3735 * call to choose_random_entry() that triggered us. If so, just return.
3737 return;
3741 char *string = routerset_to_string(options->EntryNodes);
3742 log_info(LD_CIRC,"Adding configured EntryNodes '%s'.", string);
3743 tor_free(string);
3746 entry_routers = smartlist_create();
3747 entry_fps = smartlist_create();
3748 old_entry_guards_on_list = smartlist_create();
3749 old_entry_guards_not_on_list = smartlist_create();
3751 /* Split entry guards into those on the list and those not. */
3753 /* XXXX022 Now that we allow countries and IP ranges in EntryNodes, this is
3754 * potentially an enormous list. For now, we disable such values for
3755 * EntryNodes in options_validate(); really, this wants a better solution.
3756 * Perhaps we should do this calculation once whenever the list of routers
3757 * changes or the entrynodes setting changes.
3759 routerset_get_all_routers(entry_routers, options->EntryNodes, 0);
3760 SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri,
3761 smartlist_add(entry_fps,ri->cache_info.identity_digest));
3762 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e, {
3763 if (smartlist_digest_isin(entry_fps, e->identity))
3764 smartlist_add(old_entry_guards_on_list, e);
3765 else
3766 smartlist_add(old_entry_guards_not_on_list, e);
3769 /* Remove all currently configured entry guards from entry_routers. */
3770 SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri, {
3771 if (is_an_entry_guard(ri->cache_info.identity_digest)) {
3772 SMARTLIST_DEL_CURRENT(entry_routers, ri);
3776 /* Now build the new entry_guards list. */
3777 smartlist_clear(entry_guards);
3778 /* First, the previously configured guards that are in EntryNodes. */
3779 smartlist_add_all(entry_guards, old_entry_guards_on_list);
3780 /* Next, the rest of EntryNodes */
3781 SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri, {
3782 add_an_entry_guard(ri, 0);
3784 /* Finally, the remaining previously configured guards that are not in
3785 * EntryNodes, unless we're strict in which case we drop them */
3786 if (options->StrictNodes) {
3787 SMARTLIST_FOREACH(old_entry_guards_not_on_list, entry_guard_t *, e,
3788 entry_guard_free(e));
3789 } else {
3790 smartlist_add_all(entry_guards, old_entry_guards_not_on_list);
3793 smartlist_free(entry_routers);
3794 smartlist_free(entry_fps);
3795 smartlist_free(old_entry_guards_on_list);
3796 smartlist_free(old_entry_guards_not_on_list);
3797 entry_guards_changed();
3800 /** Return 0 if we're fine adding arbitrary routers out of the
3801 * directory to our entry guard list, or return 1 if we have a
3802 * list already and we'd prefer to stick to it.
3805 entry_list_is_constrained(or_options_t *options)
3807 if (options->EntryNodes)
3808 return 1;
3809 if (options->UseBridges)
3810 return 1;
3811 return 0;
3814 /* Are we dead set against changing our entry guard list, or would we
3815 * change it if it means keeping Tor usable? */
3816 static int
3817 entry_list_is_totally_static(or_options_t *options)
3819 if (options->EntryNodes && options->StrictNodes)
3820 return 1;
3821 if (options->UseBridges)
3822 return 1;
3823 return 0;
3826 /** Pick a live (up and listed) entry guard from entry_guards. If
3827 * <b>state</b> is non-NULL, this is for a specific circuit --
3828 * make sure not to pick this circuit's exit or any node in the
3829 * exit's family. If <b>state</b> is NULL, we're looking for a random
3830 * guard (likely a bridge). */
3831 routerinfo_t *
3832 choose_random_entry(cpath_build_state_t *state)
3834 or_options_t *options = get_options();
3835 smartlist_t *live_entry_guards = smartlist_create();
3836 smartlist_t *exit_family = smartlist_create();
3837 routerinfo_t *chosen_exit = state?build_state_get_exit_router(state) : NULL;
3838 routerinfo_t *r = NULL;
3839 int need_uptime = state ? state->need_uptime : 0;
3840 int need_capacity = state ? state->need_capacity : 0;
3841 int preferred_min, consider_exit_family = 0;
3843 if (chosen_exit) {
3844 smartlist_add(exit_family, chosen_exit);
3845 routerlist_add_family(exit_family, chosen_exit);
3846 consider_exit_family = 1;
3849 if (!entry_guards)
3850 entry_guards = smartlist_create();
3852 if (should_add_entry_nodes)
3853 entry_guards_prepend_from_config();
3855 if (!entry_list_is_constrained(options) &&
3856 smartlist_len(entry_guards) < options->NumEntryGuards)
3857 pick_entry_guards();
3859 retry:
3860 smartlist_clear(live_entry_guards);
3861 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3863 const char *msg;
3864 r = entry_is_live(entry, need_uptime, need_capacity, 0, &msg);
3865 if (!r)
3866 continue; /* down, no point */
3867 if (consider_exit_family && smartlist_isin(exit_family, r))
3868 continue; /* avoid relays that are family members of our exit */
3869 if (options->EntryNodes &&
3870 !routerset_contains_router(options->EntryNodes, r)) {
3871 /* We've come to the end of our preferred entry nodes. */
3872 if (smartlist_len(live_entry_guards))
3873 goto choose_and_finish; /* only choose from the ones we like */
3874 if (options->StrictNodes) {
3875 /* in theory this case should never happen, since
3876 * entry_guards_prepend_from_config() drops unwanted relays */
3877 tor_fragile_assert();
3878 } else {
3879 log_info(LD_CIRC,
3880 "No relays from EntryNodes available. Using others.");
3883 smartlist_add(live_entry_guards, r);
3884 if (!entry->made_contact) {
3885 /* Always start with the first not-yet-contacted entry
3886 * guard. Otherwise we might add several new ones, pick
3887 * the second new one, and now we've expanded our entry
3888 * guard list without needing to. */
3889 goto choose_and_finish;
3891 if (smartlist_len(live_entry_guards) >= options->NumEntryGuards)
3892 break; /* we have enough */
3895 if (entry_list_is_constrained(options)) {
3896 /* If we prefer the entry nodes we've got, and we have at least
3897 * one choice, that's great. Use it. */
3898 preferred_min = 1;
3899 } else {
3900 /* Try to have at least 2 choices available. This way we don't
3901 * get stuck with a single live-but-crummy entry and just keep
3902 * using him.
3903 * (We might get 2 live-but-crummy entry guards, but so be it.) */
3904 preferred_min = 2;
3907 if (smartlist_len(live_entry_guards) < preferred_min) {
3908 if (!entry_list_is_totally_static(options)) {
3909 /* still no? try adding a new entry then */
3910 /* XXX if guard doesn't imply fast and stable, then we need
3911 * to tell add_an_entry_guard below what we want, or it might
3912 * be a long time til we get it. -RD */
3913 r = add_an_entry_guard(NULL, 0);
3914 if (r) {
3915 entry_guards_changed();
3916 /* XXX we start over here in case the new node we added shares
3917 * a family with our exit node. There's a chance that we'll just
3918 * load up on entry guards here, if the network we're using is
3919 * one big family. Perhaps we should teach add_an_entry_guard()
3920 * to understand nodes-to-avoid-if-possible? -RD */
3921 goto retry;
3924 if (!r && need_uptime) {
3925 need_uptime = 0; /* try without that requirement */
3926 goto retry;
3928 if (!r && need_capacity) {
3929 /* still no? last attempt, try without requiring capacity */
3930 need_capacity = 0;
3931 goto retry;
3933 if (!r && entry_list_is_constrained(options) && consider_exit_family) {
3934 /* still no? if we're using bridges or have strictentrynodes
3935 * set, and our chosen exit is in the same family as all our
3936 * bridges/entry guards, then be flexible about families. */
3937 consider_exit_family = 0;
3938 goto retry;
3940 /* live_entry_guards may be empty below. Oh well, we tried. */
3943 choose_and_finish:
3944 if (entry_list_is_constrained(options)) {
3945 /* We need to weight by bandwidth, because our bridges or entryguards
3946 * were not already selected proportional to their bandwidth. */
3947 r = routerlist_sl_choose_by_bandwidth(live_entry_guards, WEIGHT_FOR_GUARD);
3948 } else {
3949 /* We choose uniformly at random here, because choose_good_entry_server()
3950 * already weights its choices by bandwidth, so we don't want to
3951 * *double*-weight our guard selection. */
3952 r = smartlist_choose(live_entry_guards);
3954 smartlist_free(live_entry_guards);
3955 smartlist_free(exit_family);
3956 return r;
3959 /** Parse <b>state</b> and learn about the entry guards it describes.
3960 * If <b>set</b> is true, and there are no errors, replace the global
3961 * entry_list with what we find.
3962 * On success, return 0. On failure, alloc into *<b>msg</b> a string
3963 * describing the error, and return -1.
3966 entry_guards_parse_state(or_state_t *state, int set, char **msg)
3968 entry_guard_t *node = NULL;
3969 smartlist_t *new_entry_guards = smartlist_create();
3970 config_line_t *line;
3971 time_t now = time(NULL);
3972 const char *state_version = state->TorVersion;
3973 digestmap_t *added_by = digestmap_new();
3975 *msg = NULL;
3976 for (line = state->EntryGuards; line; line = line->next) {
3977 if (!strcasecmp(line->key, "EntryGuard")) {
3978 smartlist_t *args = smartlist_create();
3979 node = tor_malloc_zero(sizeof(entry_guard_t));
3980 /* all entry guards on disk have been contacted */
3981 node->made_contact = 1;
3982 smartlist_add(new_entry_guards, node);
3983 smartlist_split_string(args, line->value, " ",
3984 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
3985 if (smartlist_len(args)<2) {
3986 *msg = tor_strdup("Unable to parse entry nodes: "
3987 "Too few arguments to EntryGuard");
3988 } else if (!is_legal_nickname(smartlist_get(args,0))) {
3989 *msg = tor_strdup("Unable to parse entry nodes: "
3990 "Bad nickname for EntryGuard");
3991 } else {
3992 strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1);
3993 if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1),
3994 strlen(smartlist_get(args,1)))<0) {
3995 *msg = tor_strdup("Unable to parse entry nodes: "
3996 "Bad hex digest for EntryGuard");
3999 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
4000 smartlist_free(args);
4001 if (*msg)
4002 break;
4003 } else if (!strcasecmp(line->key, "EntryGuardDownSince") ||
4004 !strcasecmp(line->key, "EntryGuardUnlistedSince")) {
4005 time_t when;
4006 time_t last_try = 0;
4007 if (!node) {
4008 *msg = tor_strdup("Unable to parse entry nodes: "
4009 "EntryGuardDownSince/UnlistedSince without EntryGuard");
4010 break;
4012 if (parse_iso_time(line->value, &when)<0) {
4013 *msg = tor_strdup("Unable to parse entry nodes: "
4014 "Bad time in EntryGuardDownSince/UnlistedSince");
4015 break;
4017 if (when > now) {
4018 /* It's a bad idea to believe info in the future: you can wind
4019 * up with timeouts that aren't allowed to happen for years. */
4020 continue;
4022 if (strlen(line->value) >= ISO_TIME_LEN+ISO_TIME_LEN+1) {
4023 /* ignore failure */
4024 (void) parse_iso_time(line->value+ISO_TIME_LEN+1, &last_try);
4026 if (!strcasecmp(line->key, "EntryGuardDownSince")) {
4027 node->unreachable_since = when;
4028 node->last_attempted = last_try;
4029 } else {
4030 node->bad_since = when;
4032 } else if (!strcasecmp(line->key, "EntryGuardAddedBy")) {
4033 char d[DIGEST_LEN];
4034 /* format is digest version date */
4035 if (strlen(line->value) < HEX_DIGEST_LEN+1+1+1+ISO_TIME_LEN) {
4036 log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough.");
4037 continue;
4039 if (base16_decode(d, sizeof(d), line->value, HEX_DIGEST_LEN)<0 ||
4040 line->value[HEX_DIGEST_LEN] != ' ') {
4041 log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with "
4042 "hex digest", escaped(line->value));
4043 continue;
4045 digestmap_set(added_by, d, tor_strdup(line->value+HEX_DIGEST_LEN+1));
4046 } else {
4047 log_warn(LD_BUG, "Unexpected key %s", line->key);
4051 SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
4053 char *sp;
4054 char *val = digestmap_get(added_by, e->identity);
4055 if (val && (sp = strchr(val, ' '))) {
4056 time_t when;
4057 *sp++ = '\0';
4058 if (parse_iso_time(sp, &when)<0) {
4059 log_warn(LD_BUG, "Can't read time %s in EntryGuardAddedBy", sp);
4060 } else {
4061 e->chosen_by_version = tor_strdup(val);
4062 e->chosen_on_date = when;
4064 } else {
4065 if (state_version) {
4066 e->chosen_by_version = tor_strdup(state_version);
4067 e->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
4072 if (*msg || !set) {
4073 SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
4074 entry_guard_free(e));
4075 smartlist_free(new_entry_guards);
4076 } else { /* !err && set */
4077 if (entry_guards) {
4078 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4079 entry_guard_free(e));
4080 smartlist_free(entry_guards);
4082 entry_guards = new_entry_guards;
4083 entry_guards_dirty = 0;
4084 /* XXX022 hand new_entry_guards to this func, and move it up a
4085 * few lines, so we don't have to re-dirty it */
4086 if (remove_obsolete_entry_guards())
4087 entry_guards_dirty = 1;
4089 digestmap_free(added_by, _tor_free);
4090 return *msg ? -1 : 0;
4093 /** Our list of entry guards has changed, or some element of one
4094 * of our entry guards has changed. Write the changes to disk within
4095 * the next few minutes.
4097 static void
4098 entry_guards_changed(void)
4100 time_t when;
4101 entry_guards_dirty = 1;
4103 /* or_state_save() will call entry_guards_update_state(). */
4104 when = get_options()->AvoidDiskWrites ? time(NULL) + 3600 : time(NULL)+600;
4105 or_state_mark_dirty(get_or_state(), when);
4108 /** If the entry guard info has not changed, do nothing and return.
4109 * Otherwise, free the EntryGuards piece of <b>state</b> and create
4110 * a new one out of the global entry_guards list, and then mark
4111 * <b>state</b> dirty so it will get saved to disk.
4113 void
4114 entry_guards_update_state(or_state_t *state)
4116 config_line_t **next, *line;
4117 if (! entry_guards_dirty)
4118 return;
4120 config_free_lines(state->EntryGuards);
4121 next = &state->EntryGuards;
4122 *next = NULL;
4123 if (!entry_guards)
4124 entry_guards = smartlist_create();
4125 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4127 char dbuf[HEX_DIGEST_LEN+1];
4128 if (!e->made_contact)
4129 continue; /* don't write this one to disk */
4130 *next = line = tor_malloc_zero(sizeof(config_line_t));
4131 line->key = tor_strdup("EntryGuard");
4132 line->value = tor_malloc(HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2);
4133 base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
4134 tor_snprintf(line->value,HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2,
4135 "%s %s", e->nickname, dbuf);
4136 next = &(line->next);
4137 if (e->unreachable_since) {
4138 *next = line = tor_malloc_zero(sizeof(config_line_t));
4139 line->key = tor_strdup("EntryGuardDownSince");
4140 line->value = tor_malloc(ISO_TIME_LEN+1+ISO_TIME_LEN+1);
4141 format_iso_time(line->value, e->unreachable_since);
4142 if (e->last_attempted) {
4143 line->value[ISO_TIME_LEN] = ' ';
4144 format_iso_time(line->value+ISO_TIME_LEN+1, e->last_attempted);
4146 next = &(line->next);
4148 if (e->bad_since) {
4149 *next = line = tor_malloc_zero(sizeof(config_line_t));
4150 line->key = tor_strdup("EntryGuardUnlistedSince");
4151 line->value = tor_malloc(ISO_TIME_LEN+1);
4152 format_iso_time(line->value, e->bad_since);
4153 next = &(line->next);
4155 if (e->chosen_on_date && e->chosen_by_version &&
4156 !strchr(e->chosen_by_version, ' ')) {
4157 char d[HEX_DIGEST_LEN+1];
4158 char t[ISO_TIME_LEN+1];
4159 size_t val_len;
4160 *next = line = tor_malloc_zero(sizeof(config_line_t));
4161 line->key = tor_strdup("EntryGuardAddedBy");
4162 val_len = (HEX_DIGEST_LEN+1+strlen(e->chosen_by_version)
4163 +1+ISO_TIME_LEN+1);
4164 line->value = tor_malloc(val_len);
4165 base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
4166 format_iso_time(t, e->chosen_on_date);
4167 tor_snprintf(line->value, val_len, "%s %s %s",
4168 d, e->chosen_by_version, t);
4169 next = &(line->next);
4172 if (!get_options()->AvoidDiskWrites)
4173 or_state_mark_dirty(get_or_state(), 0);
4174 entry_guards_dirty = 0;
4177 /** If <b>question</b> is the string "entry-guards", then dump
4178 * to *<b>answer</b> a newly allocated string describing all of
4179 * the nodes in the global entry_guards list. See control-spec.txt
4180 * for details.
4181 * For backward compatibility, we also handle the string "helper-nodes".
4182 * */
4184 getinfo_helper_entry_guards(control_connection_t *conn,
4185 const char *question, char **answer)
4187 (void) conn;
4188 if (!strcmp(question,"entry-guards") ||
4189 !strcmp(question,"helper-nodes")) {
4190 smartlist_t *sl = smartlist_create();
4191 char tbuf[ISO_TIME_LEN+1];
4192 char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
4193 if (!entry_guards)
4194 entry_guards = smartlist_create();
4195 SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
4196 size_t len = MAX_VERBOSE_NICKNAME_LEN+ISO_TIME_LEN+32;
4197 char *c = tor_malloc(len);
4198 const char *status = NULL;
4199 time_t when = 0;
4200 routerinfo_t *ri;
4202 if (!e->made_contact) {
4203 status = "never-connected";
4204 } else if (e->bad_since) {
4205 when = e->bad_since;
4206 status = "unusable";
4207 } else {
4208 status = "up";
4211 ri = router_get_by_digest(e->identity);
4212 if (ri) {
4213 router_get_verbose_nickname(nbuf, ri);
4214 } else {
4215 nbuf[0] = '$';
4216 base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
4217 /* e->nickname field is not very reliable if we don't know about
4218 * this router any longer; don't include it. */
4221 if (when) {
4222 format_iso_time(tbuf, when);
4223 tor_snprintf(c, len, "%s %s %s\n", nbuf, status, tbuf);
4224 } else {
4225 tor_snprintf(c, len, "%s %s\n", nbuf, status);
4227 smartlist_add(sl, c);
4228 } SMARTLIST_FOREACH_END(e);
4229 *answer = smartlist_join_strings(sl, "", 0, NULL);
4230 SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
4231 smartlist_free(sl);
4233 return 0;
4236 /** Information about a configured bridge. Currently this just matches the
4237 * ones in the torrc file, but one day we may be able to learn about new
4238 * bridges on our own, and remember them in the state file. */
4239 typedef struct {
4240 /** Address of the bridge. */
4241 tor_addr_t addr;
4242 /** TLS port for the bridge. */
4243 uint16_t port;
4244 /** Expected identity digest, or all zero bytes if we don't know what the
4245 * digest should be. */
4246 char identity[DIGEST_LEN];
4247 /** When should we next try to fetch a descriptor for this bridge? */
4248 download_status_t fetch_status;
4249 } bridge_info_t;
4251 /** A list of configured bridges. Whenever we actually get a descriptor
4252 * for one, we add it as an entry guard. */
4253 static smartlist_t *bridge_list = NULL;
4255 /** Initialize the bridge list to empty, creating it if needed. */
4256 void
4257 clear_bridge_list(void)
4259 if (!bridge_list)
4260 bridge_list = smartlist_create();
4261 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, tor_free(b));
4262 smartlist_clear(bridge_list);
4265 /** Return a bridge pointer if <b>ri</b> is one of our known bridges
4266 * (either by comparing keys if possible, else by comparing addr/port).
4267 * Else return NULL. */
4268 static bridge_info_t *
4269 routerinfo_get_configured_bridge(routerinfo_t *ri)
4271 if (!bridge_list)
4272 return NULL;
4273 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
4275 if (tor_digest_is_zero(bridge->identity) &&
4276 tor_addr_eq_ipv4h(&bridge->addr, ri->addr) &&
4277 bridge->port == ri->or_port)
4278 return bridge;
4279 if (!memcmp(bridge->identity, ri->cache_info.identity_digest,
4280 DIGEST_LEN))
4281 return bridge;
4283 SMARTLIST_FOREACH_END(bridge);
4284 return NULL;
4287 /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
4289 routerinfo_is_a_configured_bridge(routerinfo_t *ri)
4291 return routerinfo_get_configured_bridge(ri) ? 1 : 0;
4294 /** Remember a new bridge at <b>addr</b>:<b>port</b>. If <b>digest</b>
4295 * is set, it tells us the identity key too. */
4296 void
4297 bridge_add_from_config(const tor_addr_t *addr, uint16_t port, char *digest)
4299 bridge_info_t *b = tor_malloc_zero(sizeof(bridge_info_t));
4300 tor_addr_copy(&b->addr, addr);
4301 b->port = port;
4302 if (digest)
4303 memcpy(b->identity, digest, DIGEST_LEN);
4304 b->fetch_status.schedule = DL_SCHED_BRIDGE;
4305 if (!bridge_list)
4306 bridge_list = smartlist_create();
4307 smartlist_add(bridge_list, b);
4310 /** If <b>digest</b> is one of our known bridges, return it. */
4311 static bridge_info_t *
4312 find_bridge_by_digest(const char *digest)
4314 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
4316 if (!memcmp(bridge->identity, digest, DIGEST_LEN))
4317 return bridge;
4319 return NULL;
4322 /** We need to ask <b>bridge</b> for its server descriptor. <b>address</b>
4323 * is a helpful string describing this bridge. */
4324 static void
4325 launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
4327 char *address;
4329 if (connection_get_by_type_addr_port_purpose(
4330 CONN_TYPE_DIR, &bridge->addr, bridge->port,
4331 DIR_PURPOSE_FETCH_SERVERDESC))
4332 return; /* it's already on the way */
4334 address = tor_dup_addr(&bridge->addr);
4335 directory_initiate_command(address, &bridge->addr,
4336 bridge->port, 0,
4337 0, /* does not matter */
4338 1, bridge->identity,
4339 DIR_PURPOSE_FETCH_SERVERDESC,
4340 ROUTER_PURPOSE_BRIDGE,
4341 0, "authority.z", NULL, 0, 0);
4342 tor_free(address);
4345 /** Fetching the bridge descriptor from the bridge authority returned a
4346 * "not found". Fall back to trying a direct fetch. */
4347 void
4348 retry_bridge_descriptor_fetch_directly(const char *digest)
4350 bridge_info_t *bridge = find_bridge_by_digest(digest);
4351 if (!bridge)
4352 return; /* not found? oh well. */
4354 launch_direct_bridge_descriptor_fetch(bridge);
4357 /** For each bridge in our list for which we don't currently have a
4358 * descriptor, fetch a new copy of its descriptor -- either directly
4359 * from the bridge or via a bridge authority. */
4360 void
4361 fetch_bridge_descriptors(time_t now)
4363 or_options_t *options = get_options();
4364 int num_bridge_auths = get_n_authorities(BRIDGE_AUTHORITY);
4365 int ask_bridge_directly;
4366 int can_use_bridge_authority;
4368 if (!bridge_list)
4369 return;
4371 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
4373 if (!download_status_is_ready(&bridge->fetch_status, now,
4374 IMPOSSIBLE_TO_DOWNLOAD))
4375 continue; /* don't bother, no need to retry yet */
4377 /* schedule another fetch as if this one will fail, in case it does */
4378 download_status_failed(&bridge->fetch_status, 0);
4380 can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
4381 num_bridge_auths;
4382 ask_bridge_directly = !can_use_bridge_authority ||
4383 !options->UpdateBridgesFromAuthority;
4384 log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
4385 ask_bridge_directly, tor_digest_is_zero(bridge->identity),
4386 !options->UpdateBridgesFromAuthority, !num_bridge_auths);
4388 if (ask_bridge_directly &&
4389 !fascist_firewall_allows_address_or(&bridge->addr, bridge->port)) {
4390 log_notice(LD_DIR, "Bridge at '%s:%d' isn't reachable by our "
4391 "firewall policy. %s.", fmt_addr(&bridge->addr),
4392 bridge->port,
4393 can_use_bridge_authority ?
4394 "Asking bridge authority instead" : "Skipping");
4395 if (can_use_bridge_authority)
4396 ask_bridge_directly = 0;
4397 else
4398 continue;
4401 if (ask_bridge_directly) {
4402 /* we need to ask the bridge itself for its descriptor. */
4403 launch_direct_bridge_descriptor_fetch(bridge);
4404 } else {
4405 /* We have a digest and we want to ask an authority. We could
4406 * combine all the requests into one, but that may give more
4407 * hints to the bridge authority than we want to give. */
4408 char resource[10 + HEX_DIGEST_LEN];
4409 memcpy(resource, "fp/", 3);
4410 base16_encode(resource+3, HEX_DIGEST_LEN+1,
4411 bridge->identity, DIGEST_LEN);
4412 memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
4413 log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
4414 resource);
4415 directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
4416 ROUTER_PURPOSE_BRIDGE, resource, 0);
4419 SMARTLIST_FOREACH_END(bridge);
4422 /** We just learned a descriptor for a bridge. See if that
4423 * digest is in our entry guard list, and add it if not. */
4424 void
4425 learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
4427 tor_assert(ri);
4428 tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
4429 if (get_options()->UseBridges) {
4430 int first = !any_bridge_descriptors_known();
4431 bridge_info_t *bridge = routerinfo_get_configured_bridge(ri);
4432 time_t now = time(NULL);
4433 ri->is_running = 1;
4435 if (bridge) { /* if we actually want to use this one */
4436 /* it's here; schedule its re-fetch for a long time from now. */
4437 if (!from_cache)
4438 download_status_reset(&bridge->fetch_status);
4440 add_an_entry_guard(ri, 1);
4441 log_notice(LD_DIR, "new bridge descriptor '%s' (%s)", ri->nickname,
4442 from_cache ? "cached" : "fresh");
4443 if (first)
4444 routerlist_retry_directory_downloads(now);
4449 /** Return 1 if any of our entry guards have descriptors that
4450 * are marked with purpose 'bridge' and are running. Else return 0.
4452 * We use this function to decide if we're ready to start building
4453 * circuits through our bridges, or if we need to wait until the
4454 * directory "server/authority" requests finish. */
4456 any_bridge_descriptors_known(void)
4458 tor_assert(get_options()->UseBridges);
4459 return choose_random_entry(NULL)!=NULL ? 1 : 0;
4462 /** Return 1 if there are any directory conns fetching bridge descriptors
4463 * that aren't marked for close. We use this to guess if we should tell
4464 * the controller that we have a problem. */
4466 any_pending_bridge_descriptor_fetches(void)
4468 smartlist_t *conns = get_connection_array();
4469 SMARTLIST_FOREACH(conns, connection_t *, conn,
4471 if (conn->type == CONN_TYPE_DIR &&
4472 conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
4473 TO_DIR_CONN(conn)->router_purpose == ROUTER_PURPOSE_BRIDGE &&
4474 !conn->marked_for_close &&
4475 conn->linked && !conn->linked_conn->marked_for_close) {
4476 log_debug(LD_DIR, "found one: %s", conn->address);
4477 return 1;
4480 return 0;
4483 /** Return 1 if we have at least one descriptor for a bridge and
4484 * all descriptors we know are down. Else return 0. If <b>act</b> is
4485 * 1, then mark the down bridges up; else just observe and report. */
4486 static int
4487 bridges_retry_helper(int act)
4489 routerinfo_t *ri;
4490 int any_known = 0;
4491 int any_running = 0;
4492 if (!entry_guards)
4493 entry_guards = smartlist_create();
4494 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4496 ri = router_get_by_digest(e->identity);
4497 if (ri && ri->purpose == ROUTER_PURPOSE_BRIDGE) {
4498 any_known = 1;
4499 if (ri->is_running)
4500 any_running = 1; /* some bridge is both known and running */
4501 else if (act) { /* mark it for retry */
4502 ri->is_running = 1;
4503 e->can_retry = 1;
4504 e->bad_since = 0;
4508 log_debug(LD_DIR, "any_known %d, any_running %d", any_known, any_running);
4509 return any_known && !any_running;
4512 /** Do we know any descriptors for our bridges, and are they all
4513 * down? */
4515 bridges_known_but_down(void)
4517 return bridges_retry_helper(0);
4520 /** Mark all down known bridges up. */
4521 void
4522 bridges_retry_all(void)
4524 bridges_retry_helper(1);
4527 /** Release all storage held by the list of entry guards and related
4528 * memory structs. */
4529 void
4530 entry_guards_free_all(void)
4532 if (entry_guards) {
4533 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4534 entry_guard_free(e));
4535 smartlist_free(entry_guards);
4536 entry_guards = NULL;
4538 clear_bridge_list();
4539 smartlist_free(bridge_list);
4540 bridge_list = NULL;