naked constants are bad
[tor/rransom.git] / src / or / circuitbuild.c
blobef6751858af9e46f55530b251d0f52637ca2ee62
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 > 0 && 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(cbt->liveness.timeouts_after_firsthop);
204 * Technically this is a circular array that we are reallocating
205 * and memcopying. However, since it only consists of either 1s
206 * or 0s, and is only used in a statistical test to determine when
207 * we should discard our history after a sufficient number of 1's
208 * have been reached, it is fine if order is not preserved or
209 * elements are lost.
211 * cbtrecentcount should only be changing in cases of severe network
212 * distress anyway, so memory correctness here is paramount over
213 * doing acrobatics to preserve the array.
215 recent_circs = tor_malloc_zero(sizeof(int8_t)*num);
216 memcpy(recent_circs, cbt->liveness.timeouts_after_firsthop,
217 sizeof(int8_t)*MIN(num, cbt->liveness.num_recent_circs));
219 // Adjust the index if it needs it.
220 if (num < cbt->liveness.num_recent_circs) {
221 cbt->liveness.after_firsthop_idx = MIN(num-1,
222 cbt->liveness.after_firsthop_idx);
225 tor_free(cbt->liveness.timeouts_after_firsthop);
226 cbt->liveness.timeouts_after_firsthop = recent_circs;
227 cbt->liveness.num_recent_circs = num;
231 /** Make a note that we're running unit tests (rather than running Tor
232 * itself), so we avoid clobbering our state file. */
233 void
234 circuitbuild_running_unit_tests(void)
236 unit_tests = 1;
240 * Return the initial default or configured timeout in milliseconds
242 static double
243 circuit_build_times_get_initial_timeout(void)
245 double timeout;
246 if (!unit_tests && get_options()->CircuitBuildTimeout) {
247 timeout = get_options()->CircuitBuildTimeout*1000;
248 if (timeout < circuit_build_times_min_timeout()) {
249 log_warn(LD_CIRC, "Config CircuitBuildTimeout too low. Setting to %ds",
250 circuit_build_times_min_timeout()/1000);
251 timeout = circuit_build_times_min_timeout();
253 } else {
254 timeout = circuit_build_times_initial_timeout();
256 return timeout;
260 * Reset the build time state.
262 * Leave estimated parameters, timeout and network liveness intact
263 * for future use.
265 void
266 circuit_build_times_reset(circuit_build_times_t *cbt)
268 memset(cbt->circuit_build_times, 0, sizeof(cbt->circuit_build_times));
269 cbt->total_build_times = 0;
270 cbt->build_times_idx = 0;
271 cbt->have_computed_timeout = 0;
275 * Initialize the buildtimes structure for first use.
277 * Sets the initial timeout value based to either the
278 * config setting or BUILD_TIMEOUT_INITIAL_VALUE.
280 void
281 circuit_build_times_init(circuit_build_times_t *cbt)
283 memset(cbt, 0, sizeof(*cbt));
284 cbt->liveness.num_recent_circs = circuit_build_times_recent_circuit_count();
285 cbt->liveness.timeouts_after_firsthop = tor_malloc_zero(sizeof(int8_t)*
286 cbt->liveness.num_recent_circs);
287 cbt->close_ms = cbt->timeout_ms = circuit_build_times_get_initial_timeout();
288 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
292 * Rewind our build time history by n positions.
294 static void
295 circuit_build_times_rewind_history(circuit_build_times_t *cbt, int n)
297 int i = 0;
299 cbt->build_times_idx -= n;
300 cbt->build_times_idx %= CBT_NCIRCUITS_TO_OBSERVE;
302 for (i = 0; i < n; i++) {
303 cbt->circuit_build_times[(i+cbt->build_times_idx)
304 %CBT_NCIRCUITS_TO_OBSERVE]=0;
307 if (cbt->total_build_times > n) {
308 cbt->total_build_times -= n;
309 } else {
310 cbt->total_build_times = 0;
313 log_info(LD_CIRC,
314 "Rewound history by %d places. Current index: %d. "
315 "Total: %d", n, cbt->build_times_idx, cbt->total_build_times);
319 * Add a new build time value <b>time</b> to the set of build times. Time
320 * units are milliseconds.
322 * circuit_build_times <b>cbt</a> is a circular array, so loop around when
323 * array is full.
326 circuit_build_times_add_time(circuit_build_times_t *cbt, build_time_t time)
328 if (time <= 0 || time > CBT_BUILD_TIME_MAX) {
329 log_warn(LD_BUG, "Circuit build time is too large (%u)."
330 "This is probably a bug.", time);
331 tor_fragile_assert();
332 return -1;
335 log_debug(LD_CIRC, "Adding circuit build time %u", time);
337 cbt->circuit_build_times[cbt->build_times_idx] = time;
338 cbt->build_times_idx = (cbt->build_times_idx + 1) % CBT_NCIRCUITS_TO_OBSERVE;
339 if (cbt->total_build_times < CBT_NCIRCUITS_TO_OBSERVE)
340 cbt->total_build_times++;
342 if ((cbt->total_build_times % CBT_SAVE_STATE_EVERY) == 0) {
343 /* Save state every n circuit builds */
344 if (!unit_tests && !get_options()->AvoidDiskWrites)
345 or_state_mark_dirty(get_or_state(), 0);
348 return 0;
352 * Return maximum circuit build time
354 static build_time_t
355 circuit_build_times_max(circuit_build_times_t *cbt)
357 int i = 0;
358 build_time_t max_build_time = 0;
359 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
360 if (cbt->circuit_build_times[i] > max_build_time
361 && cbt->circuit_build_times[i] != CBT_BUILD_ABANDONED)
362 max_build_time = cbt->circuit_build_times[i];
364 return max_build_time;
367 #if 0
368 /** Return minimum circuit build time */
369 build_time_t
370 circuit_build_times_min(circuit_build_times_t *cbt)
372 int i = 0;
373 build_time_t min_build_time = CBT_BUILD_TIME_MAX;
374 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
375 if (cbt->circuit_build_times[i] && /* 0 <-> uninitialized */
376 cbt->circuit_build_times[i] < min_build_time)
377 min_build_time = cbt->circuit_build_times[i];
379 if (min_build_time == CBT_BUILD_TIME_MAX) {
380 log_warn(LD_CIRC, "No build times less than CBT_BUILD_TIME_MAX!");
382 return min_build_time;
384 #endif
387 * Calculate and return a histogram for the set of build times.
389 * Returns an allocated array of histrogram bins representing
390 * the frequency of index*CBT_BIN_WIDTH millisecond
391 * build times. Also outputs the number of bins in nbins.
393 * The return value must be freed by the caller.
395 static uint32_t *
396 circuit_build_times_create_histogram(circuit_build_times_t *cbt,
397 build_time_t *nbins)
399 uint32_t *histogram;
400 build_time_t max_build_time = circuit_build_times_max(cbt);
401 int i, c;
403 *nbins = 1 + (max_build_time / CBT_BIN_WIDTH);
404 histogram = tor_malloc_zero(*nbins * sizeof(build_time_t));
406 // calculate histogram
407 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
408 if (cbt->circuit_build_times[i] == 0
409 || cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED)
410 continue; /* 0 <-> uninitialized */
412 c = (cbt->circuit_build_times[i] / CBT_BIN_WIDTH);
413 histogram[c]++;
416 return histogram;
420 * Return the Pareto start-of-curve parameter Xm.
422 * Because we are not a true Pareto curve, we compute this as the
423 * weighted average of the N=3 most frequent build time bins.
425 static build_time_t
426 circuit_build_times_get_xm(circuit_build_times_t *cbt)
428 build_time_t i, nbins;
429 build_time_t *nth_max_bin;
430 int32_t bin_counts=0;
431 build_time_t ret = 0;
432 uint32_t *histogram = circuit_build_times_create_histogram(cbt, &nbins);
433 int n=0;
434 int num_modes = circuit_build_times_default_num_xm_modes();
436 // Only use one mode if < 1000 buildtimes. Not enough data
437 // for multiple.
438 if (cbt->total_build_times < CBT_NCIRCUITS_TO_OBSERVE)
439 num_modes = 1;
441 nth_max_bin = (build_time_t*)tor_malloc_zero(num_modes*sizeof(build_time_t));
443 for (i = 0; i < nbins; i++) {
444 if (histogram[i] >= histogram[nth_max_bin[0]]) {
445 nth_max_bin[0] = i;
448 for (n = 1; n < num_modes; n++) {
449 if (histogram[i] >= histogram[nth_max_bin[n]] &&
450 (!histogram[nth_max_bin[n-1]]
451 || histogram[i] < histogram[nth_max_bin[n-1]])) {
452 nth_max_bin[n] = i;
457 for (n = 0; n < num_modes; n++) {
458 bin_counts += histogram[nth_max_bin[n]];
459 ret += CBT_BIN_TO_MS(nth_max_bin[n])*histogram[nth_max_bin[n]];
460 log_info(LD_CIRC, "Xm mode #%d: %u %u", n, CBT_BIN_TO_MS(nth_max_bin[n]),
461 histogram[nth_max_bin[n]]);
464 ret /= bin_counts;
465 tor_free(histogram);
466 tor_free(nth_max_bin);
468 return ret;
472 * Output a histogram of current circuit build times to
473 * the or_state_t state structure.
475 void
476 circuit_build_times_update_state(circuit_build_times_t *cbt,
477 or_state_t *state)
479 uint32_t *histogram;
480 build_time_t i = 0;
481 build_time_t nbins = 0;
482 config_line_t **next, *line;
484 histogram = circuit_build_times_create_histogram(cbt, &nbins);
485 // write to state
486 config_free_lines(state->BuildtimeHistogram);
487 next = &state->BuildtimeHistogram;
488 *next = NULL;
490 state->TotalBuildTimes = cbt->total_build_times;
491 state->CircuitBuildAbandonedCount = 0;
493 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
494 if (cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED)
495 state->CircuitBuildAbandonedCount++;
498 for (i = 0; i < nbins; i++) {
499 // compress the histogram by skipping the blanks
500 if (histogram[i] == 0) continue;
501 *next = line = tor_malloc_zero(sizeof(config_line_t));
502 line->key = tor_strdup("CircuitBuildTimeBin");
503 line->value = tor_malloc(25);
504 tor_snprintf(line->value, 25, "%d %d",
505 CBT_BIN_TO_MS(i), histogram[i]);
506 next = &(line->next);
509 if (!unit_tests) {
510 if (!get_options()->AvoidDiskWrites)
511 or_state_mark_dirty(get_or_state(), 0);
514 tor_free(histogram);
518 * Shuffle the build times array.
520 * Stolen from http://en.wikipedia.org/wiki/Fisher\u2013Yates_shuffle
522 static void
523 circuit_build_times_shuffle_and_store_array(circuit_build_times_t *cbt,
524 build_time_t *raw_times,
525 int num_times)
527 int n = num_times;
528 if (num_times > CBT_NCIRCUITS_TO_OBSERVE) {
529 log_notice(LD_CIRC, "Decreasing circuit_build_times size from %d to %d",
530 num_times, CBT_NCIRCUITS_TO_OBSERVE);
533 /* This code can only be run on a compact array */
534 while (n-- > 1) {
535 int k = crypto_rand_int(n + 1); /* 0 <= k <= n. */
536 build_time_t tmp = raw_times[k];
537 raw_times[k] = raw_times[n];
538 raw_times[n] = tmp;
541 /* Since the times are now shuffled, take a random CBT_NCIRCUITS_TO_OBSERVE
542 * subset (ie the first CBT_NCIRCUITS_TO_OBSERVE values) */
543 for (n = 0; n < MIN(num_times, CBT_NCIRCUITS_TO_OBSERVE); n++) {
544 circuit_build_times_add_time(cbt, raw_times[n]);
549 * Filter old synthetic timeouts that were created before the
550 * new right-censored Pareto calculation was deployed.
552 * Once all clients before 0.2.1.13-alpha are gone, this code
553 * will be unused.
555 static int
556 circuit_build_times_filter_timeouts(circuit_build_times_t *cbt)
558 int num_filtered=0, i=0;
559 double timeout_rate = 0;
560 build_time_t max_timeout = 0;
562 timeout_rate = circuit_build_times_timeout_rate(cbt);
563 max_timeout = (build_time_t)cbt->close_ms;
565 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
566 if (cbt->circuit_build_times[i] > max_timeout) {
567 build_time_t replaced = cbt->circuit_build_times[i];
568 num_filtered++;
569 cbt->circuit_build_times[i] = CBT_BUILD_ABANDONED;
571 log_debug(LD_CIRC, "Replaced timeout %d with %d", replaced,
572 cbt->circuit_build_times[i]);
576 log_info(LD_CIRC,
577 "We had %d timeouts out of %d build times, "
578 "and filtered %d above the max of %u",
579 (int)(cbt->total_build_times*timeout_rate),
580 cbt->total_build_times, num_filtered, max_timeout);
582 return num_filtered;
586 * Load histogram from <b>state</b>, shuffling the resulting array
587 * after we do so. Use this result to estimate parameters and
588 * calculate the timeout.
590 * Returns -1 and sets msg on error. Msg must be freed by the caller.
593 circuit_build_times_parse_state(circuit_build_times_t *cbt,
594 or_state_t *state, char **msg)
596 int tot_values = 0;
597 uint32_t loaded_cnt = 0, N = 0;
598 config_line_t *line;
599 unsigned int i;
600 build_time_t *loaded_times;
601 circuit_build_times_init(cbt);
602 *msg = NULL;
604 if (circuit_build_times_disabled()) {
605 return 0;
608 /* build_time_t 0 means uninitialized */
609 loaded_times = tor_malloc_zero(sizeof(build_time_t)*state->TotalBuildTimes);
611 for (line = state->BuildtimeHistogram; line; line = line->next) {
612 smartlist_t *args = smartlist_create();
613 smartlist_split_string(args, line->value, " ",
614 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
615 if (smartlist_len(args) < 2) {
616 *msg = tor_strdup("Unable to parse circuit build times: "
617 "Too few arguments to CircuitBuildTime");
618 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
619 smartlist_free(args);
620 break;
621 } else {
622 const char *ms_str = smartlist_get(args,0);
623 const char *count_str = smartlist_get(args,1);
624 uint32_t count, k;
625 build_time_t ms;
626 int ok;
627 ms = (build_time_t)tor_parse_ulong(ms_str, 0, 0,
628 CBT_BUILD_TIME_MAX, &ok, NULL);
629 if (!ok) {
630 *msg = tor_strdup("Unable to parse circuit build times: "
631 "Unparsable bin number");
632 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
633 smartlist_free(args);
634 break;
636 count = (uint32_t)tor_parse_ulong(count_str, 0, 0,
637 UINT32_MAX, &ok, NULL);
638 if (!ok) {
639 *msg = tor_strdup("Unable to parse circuit build times: "
640 "Unparsable bin count");
641 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
642 smartlist_free(args);
643 break;
646 if (loaded_cnt+count+state->CircuitBuildAbandonedCount
647 > state->TotalBuildTimes) {
648 log_warn(LD_CIRC,
649 "Too many build times in state file. "
650 "Stopping short before %d",
651 loaded_cnt+count);
652 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
653 smartlist_free(args);
654 break;
657 for (k = 0; k < count; k++) {
658 loaded_times[loaded_cnt++] = ms;
660 N++;
661 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
662 smartlist_free(args);
666 log_info(LD_CIRC,
667 "Adding %d timeouts.", state->CircuitBuildAbandonedCount);
668 for (i=0; i < state->CircuitBuildAbandonedCount; i++) {
669 loaded_times[loaded_cnt++] = CBT_BUILD_ABANDONED;
672 if (loaded_cnt != state->TotalBuildTimes) {
673 log_warn(LD_CIRC,
674 "Corrupt state file? Build times count mismatch. "
675 "Read %d times, but file says %d", loaded_cnt,
676 state->TotalBuildTimes);
677 *msg = tor_strdup("Build times count mismatch.");
678 circuit_build_times_reset(cbt);
679 tor_free(loaded_times);
680 return -1;
683 circuit_build_times_shuffle_and_store_array(cbt, loaded_times, loaded_cnt);
685 /* Verify that we didn't overwrite any indexes */
686 for (i=0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
687 if (!cbt->circuit_build_times[i])
688 break;
689 tot_values++;
691 log_info(LD_CIRC,
692 "Loaded %d/%d values from %d lines in circuit time histogram",
693 tot_values, cbt->total_build_times, N);
695 if (cbt->total_build_times != tot_values
696 || cbt->total_build_times > CBT_NCIRCUITS_TO_OBSERVE) {
697 log_warn(LD_CIRC,
698 "Corrupt state file? Shuffled build times mismatch. "
699 "Read %d times, but file says %d", tot_values,
700 state->TotalBuildTimes);
701 *msg = tor_strdup("Build times count mismatch.");
702 circuit_build_times_reset(cbt);
703 tor_free(loaded_times);
704 return -1;
707 circuit_build_times_set_timeout(cbt);
709 if (!state->CircuitBuildAbandonedCount && cbt->total_build_times) {
710 circuit_build_times_filter_timeouts(cbt);
713 tor_free(loaded_times);
714 return *msg ? -1 : 0;
718 * Estimates the Xm and Alpha parameters using
719 * http://en.wikipedia.org/wiki/Pareto_distribution#Parameter_estimation
721 * The notable difference is that we use mode instead of min to estimate Xm.
722 * This is because our distribution is frechet-like. We claim this is
723 * an acceptable approximation because we are only concerned with the
724 * accuracy of the CDF of the tail.
727 circuit_build_times_update_alpha(circuit_build_times_t *cbt)
729 build_time_t *x=cbt->circuit_build_times;
730 double a = 0;
731 int n=0,i=0,abandoned_count=0;
732 build_time_t max_time=0;
734 /* http://en.wikipedia.org/wiki/Pareto_distribution#Parameter_estimation */
735 /* We sort of cheat here and make our samples slightly more pareto-like
736 * and less frechet-like. */
737 cbt->Xm = circuit_build_times_get_xm(cbt);
739 tor_assert(cbt->Xm > 0);
741 for (i=0; i< CBT_NCIRCUITS_TO_OBSERVE; i++) {
742 if (!x[i]) {
743 continue;
746 if (x[i] < cbt->Xm) {
747 a += tor_mathlog(cbt->Xm);
748 } else if (x[i] == CBT_BUILD_ABANDONED) {
749 abandoned_count++;
750 } else {
751 a += tor_mathlog(x[i]);
752 if (x[i] > max_time)
753 max_time = x[i];
755 n++;
759 * We are erring and asserting here because this can only happen
760 * in codepaths other than startup. The startup state parsing code
761 * performs this same check, and resets state if it hits it. If we
762 * hit it at runtime, something serious has gone wrong.
764 if (n!=cbt->total_build_times) {
765 log_err(LD_CIRC, "Discrepancy in build times count: %d vs %d", n,
766 cbt->total_build_times);
768 tor_assert(n==cbt->total_build_times);
770 if (max_time <= 0) {
771 /* This can happen if Xm is actually the *maximum* value in the set.
772 * It can also happen if we've abandoned every single circuit somehow.
773 * In either case, tell the caller not to compute a new build timeout. */
774 log_warn(LD_BUG,
775 "Could not determine largest build time (%d). "
776 "Xm is %dms and we've abandoned %d out of %d circuits.", max_time,
777 cbt->Xm, abandoned_count, n);
778 return 0;
781 a += abandoned_count*tor_mathlog(max_time);
783 a -= n*tor_mathlog(cbt->Xm);
784 // Estimator comes from Eq #4 in:
785 // "Bayesian estimation based on trimmed samples from Pareto populations"
786 // by Arturo J. Fernández. We are right-censored only.
787 a = (n-abandoned_count)/a;
789 cbt->alpha = a;
791 return 1;
795 * This is the Pareto Quantile Function. It calculates the point x
796 * in the distribution such that F(x) = quantile (ie quantile*100%
797 * of the mass of the density function is below x on the curve).
799 * We use it to calculate the timeout and also to generate synthetic
800 * values of time for circuits that timeout before completion.
802 * See http://en.wikipedia.org/wiki/Quantile_function,
803 * http://en.wikipedia.org/wiki/Inverse_transform_sampling and
804 * http://en.wikipedia.org/wiki/Pareto_distribution#Generating_a_
805 * random_sample_from_Pareto_distribution
806 * That's right. I'll cite wikipedia all day long.
808 * Return value is in milliseconds.
810 double
811 circuit_build_times_calculate_timeout(circuit_build_times_t *cbt,
812 double quantile)
814 double ret;
815 tor_assert(quantile >= 0);
816 tor_assert(1.0-quantile > 0);
817 tor_assert(cbt->Xm > 0);
819 ret = cbt->Xm/pow(1.0-quantile,1.0/cbt->alpha);
820 if (ret > INT32_MAX) {
821 ret = INT32_MAX;
823 tor_assert(ret > 0);
824 return ret;
827 /** Pareto CDF */
828 double
829 circuit_build_times_cdf(circuit_build_times_t *cbt, double x)
831 double ret;
832 tor_assert(cbt->Xm > 0);
833 ret = 1.0-pow(cbt->Xm/x,cbt->alpha);
834 tor_assert(0 <= ret && ret <= 1.0);
835 return ret;
839 * Generate a synthetic time using our distribution parameters.
841 * The return value will be within the [q_lo, q_hi) quantile points
842 * on the CDF.
844 build_time_t
845 circuit_build_times_generate_sample(circuit_build_times_t *cbt,
846 double q_lo, double q_hi)
848 double randval = crypto_rand_double();
849 build_time_t ret;
850 double u;
852 /* Generate between [q_lo, q_hi) */
853 /*XXXX This is what nextafter is supposed to be for; we should use it on the
854 * platforms that support it. */
855 q_hi -= 1.0/(INT32_MAX);
857 tor_assert(q_lo >= 0);
858 tor_assert(q_hi < 1);
859 tor_assert(q_lo < q_hi);
861 u = q_lo + (q_hi-q_lo)*randval;
863 tor_assert(0 <= u && u < 1.0);
864 /* circuit_build_times_calculate_timeout returns <= INT32_MAX */
865 ret = (build_time_t)
866 tor_lround(circuit_build_times_calculate_timeout(cbt, u));
867 tor_assert(ret > 0);
868 return ret;
872 * Estimate an initial alpha parameter by solving the quantile
873 * function with a quantile point and a specific timeout value.
875 void
876 circuit_build_times_initial_alpha(circuit_build_times_t *cbt,
877 double quantile, double timeout_ms)
879 // Q(u) = Xm/((1-u)^(1/a))
880 // Q(0.8) = Xm/((1-0.8))^(1/a)) = CircBuildTimeout
881 // CircBuildTimeout = Xm/((1-0.8))^(1/a))
882 // CircBuildTimeout = Xm*((1-0.8))^(-1/a))
883 // ln(CircBuildTimeout) = ln(Xm)+ln(((1-0.8)))*(-1/a)
884 // -ln(1-0.8)/(ln(CircBuildTimeout)-ln(Xm))=a
885 tor_assert(quantile >= 0);
886 tor_assert(cbt->Xm > 0);
887 cbt->alpha = tor_mathlog(1.0-quantile)/
888 (tor_mathlog(cbt->Xm)-tor_mathlog(timeout_ms));
889 tor_assert(cbt->alpha > 0);
893 * Returns true if we need circuits to be built
896 circuit_build_times_needs_circuits(circuit_build_times_t *cbt)
898 /* Return true if < MIN_CIRCUITS_TO_OBSERVE */
899 if (cbt->total_build_times < circuit_build_times_min_circs_to_observe())
900 return 1;
901 return 0;
905 * Returns true if we should build a timeout test circuit
906 * right now.
909 circuit_build_times_needs_circuits_now(circuit_build_times_t *cbt)
911 return circuit_build_times_needs_circuits(cbt) &&
912 approx_time()-cbt->last_circ_at > circuit_build_times_test_frequency();
916 * Called to indicate that the network showed some signs of liveness.
918 * This function is called every time we receive a cell. Avoid
919 * syscalls, events, and other high-intensity work.
921 void
922 circuit_build_times_network_is_live(circuit_build_times_t *cbt)
924 cbt->liveness.network_last_live = approx_time();
925 cbt->liveness.nonlive_discarded = 0;
926 cbt->liveness.nonlive_timeouts = 0;
930 * Called to indicate that we completed a circuit. Because this circuit
931 * succeeded, it doesn't count as a timeout-after-the-first-hop.
933 void
934 circuit_build_times_network_circ_success(circuit_build_times_t *cbt)
936 cbt->liveness.timeouts_after_firsthop[cbt->liveness.after_firsthop_idx] = 0;
937 cbt->liveness.after_firsthop_idx++;
938 cbt->liveness.after_firsthop_idx %= cbt->liveness.num_recent_circs;
942 * A circuit just timed out. If it failed after the first hop, record it
943 * in our history for later deciding if the network speed has changed.
945 static void
946 circuit_build_times_network_timeout(circuit_build_times_t *cbt,
947 int did_onehop)
949 if (did_onehop) {
950 cbt->liveness.timeouts_after_firsthop[cbt->liveness.after_firsthop_idx]=1;
951 cbt->liveness.after_firsthop_idx++;
952 cbt->liveness.after_firsthop_idx %= cbt->liveness.num_recent_circs;
957 * A circuit was just forcibly closed. If there has been no recent network
958 * activity at all, but this circuit was launched back when we thought the
959 * network was live, increment the number of "nonlive" circuit timeouts.
961 static void
962 circuit_build_times_network_close(circuit_build_times_t *cbt,
963 int did_onehop, time_t start_time)
965 time_t now = time(NULL);
967 * Check if this is a timeout that was for a circuit that spent its
968 * entire existence during a time where we have had no network activity.
970 * Also double check that it is a valid timeout after we have possibly
971 * just recently reset cbt->close_ms.
973 * We use close_ms here because timeouts aren't actually counted as timeouts
974 * until close_ms elapses.
976 if (cbt->liveness.network_last_live <= start_time &&
977 start_time <= (now - cbt->close_ms/1000.0)) {
978 if (did_onehop) {
979 char last_live_buf[ISO_TIME_LEN+1];
980 char start_time_buf[ISO_TIME_LEN+1];
981 char now_buf[ISO_TIME_LEN+1];
982 format_local_iso_time(last_live_buf, cbt->liveness.network_last_live);
983 format_local_iso_time(start_time_buf, start_time);
984 format_local_iso_time(now_buf, now);
985 log_warn(LD_BUG,
986 "Circuit somehow completed a hop while the network was "
987 "not live. Network was last live at %s, but circuit launched "
988 "at %s. It's now %s.", last_live_buf, start_time_buf,
989 now_buf);
991 cbt->liveness.nonlive_timeouts++;
996 * Returns false if the network has not received a cell or tls handshake
997 * in the past NETWORK_NOTLIVE_TIMEOUT_COUNT circuits.
999 * Also has the side effect of rewinding the circuit time history
1000 * in the case of recent liveness changes.
1003 circuit_build_times_network_check_live(circuit_build_times_t *cbt)
1005 time_t now = approx_time();
1006 if (cbt->liveness.nonlive_timeouts >= CBT_NETWORK_NONLIVE_DISCARD_COUNT) {
1007 if (!cbt->liveness.nonlive_discarded) {
1008 cbt->liveness.nonlive_discarded = 1;
1009 log_notice(LD_CIRC, "Network is no longer live (too many recent "
1010 "circuit timeouts). Dead for %ld seconds.",
1011 (long int)(now - cbt->liveness.network_last_live));
1012 /* Only discard NETWORK_NONLIVE_TIMEOUT_COUNT-1 because we stopped
1013 * counting after that */
1014 circuit_build_times_rewind_history(cbt,
1015 CBT_NETWORK_NONLIVE_TIMEOUT_COUNT-1);
1016 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_DISCARD);
1018 return 0;
1019 } else if (cbt->liveness.nonlive_timeouts >=
1020 CBT_NETWORK_NONLIVE_TIMEOUT_COUNT) {
1021 if (cbt->timeout_ms < circuit_build_times_get_initial_timeout()) {
1022 log_notice(LD_CIRC,
1023 "Network is flaky. No activity for %ld seconds. "
1024 "Temporarily raising timeout to %lds.",
1025 (long int)(now - cbt->liveness.network_last_live),
1026 tor_lround(circuit_build_times_get_initial_timeout()/1000));
1027 cbt->liveness.suspended_timeout = cbt->timeout_ms;
1028 cbt->liveness.suspended_close_timeout = cbt->close_ms;
1029 cbt->close_ms = cbt->timeout_ms
1030 = circuit_build_times_get_initial_timeout();
1031 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_SUSPENDED);
1034 return 0;
1035 } else if (cbt->liveness.suspended_timeout > 0) {
1036 log_notice(LD_CIRC,
1037 "Network activity has resumed. "
1038 "Resuming circuit timeout calculations.");
1039 cbt->timeout_ms = cbt->liveness.suspended_timeout;
1040 cbt->close_ms = cbt->liveness.suspended_close_timeout;
1041 cbt->liveness.suspended_timeout = 0;
1042 cbt->liveness.suspended_close_timeout = 0;
1043 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESUME);
1046 return 1;
1050 * Returns true if we have seen more than MAX_RECENT_TIMEOUT_COUNT of
1051 * the past RECENT_CIRCUITS time out after the first hop. Used to detect
1052 * if the network connection has changed significantly.
1054 * Also resets the entire timeout history in this case and causes us
1055 * to restart the process of building test circuits and estimating a
1056 * new timeout.
1059 circuit_build_times_network_check_changed(circuit_build_times_t *cbt)
1061 int total_build_times = cbt->total_build_times;
1062 int timeout_count=0;
1063 int i;
1065 /* how many of our recent circuits made it to the first hop but then
1066 * timed out? */
1067 for (i = 0; i < cbt->liveness.num_recent_circs; i++) {
1068 timeout_count += cbt->liveness.timeouts_after_firsthop[i];
1071 /* If 80% of our recent circuits are timing out after the first hop,
1072 * we need to re-estimate a new initial alpha and timeout. */
1073 if (timeout_count < circuit_build_times_max_timeouts()) {
1074 return 0;
1077 circuit_build_times_reset(cbt);
1078 memset(cbt->liveness.timeouts_after_firsthop, 0,
1079 sizeof(*cbt->liveness.timeouts_after_firsthop)*
1080 cbt->liveness.num_recent_circs);
1081 cbt->liveness.after_firsthop_idx = 0;
1083 /* Check to see if this has happened before. If so, double the timeout
1084 * to give people on abysmally bad network connections a shot at access */
1085 if (cbt->timeout_ms >= circuit_build_times_get_initial_timeout()) {
1086 if (cbt->timeout_ms > INT32_MAX/2 || cbt->close_ms > INT32_MAX/2) {
1087 log_warn(LD_CIRC, "Insanely large circuit build timeout value. "
1088 "(timeout = %lfmsec, close = %lfmsec)",
1089 cbt->timeout_ms, cbt->close_ms);
1090 } else {
1091 cbt->timeout_ms *= 2;
1092 cbt->close_ms *= 2;
1094 } else {
1095 cbt->close_ms = cbt->timeout_ms
1096 = circuit_build_times_get_initial_timeout();
1099 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_RESET);
1101 log_notice(LD_CIRC,
1102 "Network connection speed appears to have changed. Resetting "
1103 "timeout to %lds after %d timeouts and %d buildtimes.",
1104 tor_lround(cbt->timeout_ms/1000), timeout_count,
1105 total_build_times);
1107 return 1;
1111 * Count the number of timeouts in a set of cbt data.
1113 double
1114 circuit_build_times_timeout_rate(const circuit_build_times_t *cbt)
1116 int i=0,timeouts=0;
1117 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
1118 if (cbt->circuit_build_times[i] >= cbt->timeout_ms) {
1119 timeouts++;
1123 if (!cbt->total_build_times)
1124 return 0;
1126 return ((double)timeouts)/cbt->total_build_times;
1130 * Count the number of closed circuits in a set of cbt data.
1132 double
1133 circuit_build_times_close_rate(const circuit_build_times_t *cbt)
1135 int i=0,closed=0;
1136 for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
1137 if (cbt->circuit_build_times[i] == CBT_BUILD_ABANDONED) {
1138 closed++;
1142 if (!cbt->total_build_times)
1143 return 0;
1145 return ((double)closed)/cbt->total_build_times;
1149 * Store a timeout as a synthetic value.
1151 * Returns true if the store was successful and we should possibly
1152 * update our timeout estimate.
1155 circuit_build_times_count_close(circuit_build_times_t *cbt,
1156 int did_onehop,
1157 time_t start_time)
1159 if (circuit_build_times_disabled()) {
1160 cbt->close_ms = cbt->timeout_ms
1161 = circuit_build_times_get_initial_timeout();
1162 return 0;
1165 /* Record this force-close to help determine if the network is dead */
1166 circuit_build_times_network_close(cbt, did_onehop, start_time);
1168 /* Only count timeouts if network is live.. */
1169 if (!circuit_build_times_network_check_live(cbt)) {
1170 return 0;
1173 circuit_build_times_add_time(cbt, CBT_BUILD_ABANDONED);
1174 return 1;
1178 * Update timeout counts to determine if we need to expire
1179 * our build time history due to excessive timeouts.
1181 void
1182 circuit_build_times_count_timeout(circuit_build_times_t *cbt,
1183 int did_onehop)
1185 if (circuit_build_times_disabled()) {
1186 cbt->close_ms = cbt->timeout_ms
1187 = circuit_build_times_get_initial_timeout();
1188 return;
1191 circuit_build_times_network_timeout(cbt, did_onehop);
1193 /* If there are a ton of timeouts, we should reset
1194 * the circuit build timeout.
1196 circuit_build_times_network_check_changed(cbt);
1200 * Estimate a new timeout based on history and set our timeout
1201 * variable accordingly.
1203 static int
1204 circuit_build_times_set_timeout_worker(circuit_build_times_t *cbt)
1206 if (cbt->total_build_times < circuit_build_times_min_circs_to_observe()) {
1207 return 0;
1210 if (!circuit_build_times_update_alpha(cbt))
1211 return 0;
1213 cbt->timeout_ms = circuit_build_times_calculate_timeout(cbt,
1214 circuit_build_times_quantile_cutoff());
1216 cbt->close_ms = circuit_build_times_calculate_timeout(cbt,
1217 circuit_build_times_close_quantile());
1219 /* Sometimes really fast guard nodes give us such a steep curve
1220 * that this ends up being not that much greater than timeout_ms.
1221 * Make it be at least 1 min to handle this case. */
1222 cbt->close_ms = MAX(cbt->close_ms, circuit_build_times_initial_timeout());
1224 cbt->have_computed_timeout = 1;
1225 return 1;
1229 * Exposed function to compute a new timeout. Dispatches events and
1230 * also filters out extremely high timeout values.
1232 void
1233 circuit_build_times_set_timeout(circuit_build_times_t *cbt)
1235 long prev_timeout = tor_lround(cbt->timeout_ms/1000);
1236 double timeout_rate;
1238 if (!circuit_build_times_set_timeout_worker(cbt))
1239 return;
1241 if (cbt->timeout_ms < circuit_build_times_min_timeout()) {
1242 log_warn(LD_CIRC, "Set buildtimeout to low value %lfms. Setting to %dms",
1243 cbt->timeout_ms, circuit_build_times_min_timeout());
1244 cbt->timeout_ms = circuit_build_times_min_timeout();
1245 if (cbt->close_ms < cbt->timeout_ms) {
1246 /* This shouldn't happen because of MAX() in timeout_worker above,
1247 * but doing it just in case */
1248 cbt->close_ms = circuit_build_times_initial_timeout();
1252 control_event_buildtimeout_set(cbt, BUILDTIMEOUT_SET_EVENT_COMPUTED);
1254 timeout_rate = circuit_build_times_timeout_rate(cbt);
1256 if (prev_timeout > tor_lround(cbt->timeout_ms/1000)) {
1257 log_notice(LD_CIRC,
1258 "Based on %d circuit times, it looks like we don't need to "
1259 "wait so long for circuits to finish. We will now assume a "
1260 "circuit is too slow to use after waiting %ld seconds.",
1261 cbt->total_build_times,
1262 tor_lround(cbt->timeout_ms/1000));
1263 log_info(LD_CIRC,
1264 "Circuit timeout data: %lfms, %lfms, Xm: %d, a: %lf, r: %lf",
1265 cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha,
1266 timeout_rate);
1267 } else if (prev_timeout < tor_lround(cbt->timeout_ms/1000)) {
1268 log_notice(LD_CIRC,
1269 "Based on %d circuit times, it looks like we need to wait "
1270 "longer for circuits to finish. We will now assume a "
1271 "circuit is too slow to use after waiting %ld seconds.",
1272 cbt->total_build_times,
1273 tor_lround(cbt->timeout_ms/1000));
1274 log_info(LD_CIRC,
1275 "Circuit timeout data: %lfms, %lfms, Xm: %d, a: %lf, r: %lf",
1276 cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha,
1277 timeout_rate);
1278 } else {
1279 log_info(LD_CIRC,
1280 "Set circuit build timeout to %lds (%lfms, %lfms, Xm: %d, a: %lf,"
1281 " r: %lf) based on %d circuit times",
1282 tor_lround(cbt->timeout_ms/1000),
1283 cbt->timeout_ms, cbt->close_ms, cbt->Xm, cbt->alpha, timeout_rate,
1284 cbt->total_build_times);
1288 /** Iterate over values of circ_id, starting from conn-\>next_circ_id,
1289 * and with the high bit specified by conn-\>circ_id_type, until we get
1290 * a circ_id that is not in use by any other circuit on that conn.
1292 * Return it, or 0 if can't get a unique circ_id.
1294 static circid_t
1295 get_unique_circ_id_by_conn(or_connection_t *conn)
1297 circid_t test_circ_id;
1298 circid_t attempts=0;
1299 circid_t high_bit;
1301 tor_assert(conn);
1302 if (conn->circ_id_type == CIRC_ID_TYPE_NEITHER) {
1303 log_warn(LD_BUG, "Trying to pick a circuit ID for a connection from "
1304 "a client with no identity.");
1305 return 0;
1307 high_bit = (conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
1308 do {
1309 /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
1310 * circID such that (high_bit|test_circ_id) is not already used. */
1311 test_circ_id = conn->next_circ_id++;
1312 if (test_circ_id == 0 || test_circ_id >= 1<<15) {
1313 test_circ_id = 1;
1314 conn->next_circ_id = 2;
1316 if (++attempts > 1<<15) {
1317 /* Make sure we don't loop forever if all circ_id's are used. This
1318 * matters because it's an external DoS opportunity.
1320 log_warn(LD_CIRC,"No unused circ IDs. Failing.");
1321 return 0;
1323 test_circ_id |= high_bit;
1324 } while (circuit_id_in_use_on_orconn(test_circ_id, conn));
1325 return test_circ_id;
1328 /** If <b>verbose</b> is false, allocate and return a comma-separated list of
1329 * the currently built elements of circuit_t. If <b>verbose</b> is true, also
1330 * list information about link status in a more verbose format using spaces.
1331 * If <b>verbose_names</b> is false, give nicknames for Named routers and hex
1332 * digests for others; if <b>verbose_names</b> is true, use $DIGEST=Name style
1333 * names.
1335 static char *
1336 circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
1338 crypt_path_t *hop;
1339 smartlist_t *elements;
1340 const char *states[] = {"closed", "waiting for keys", "open"};
1341 char *s;
1343 elements = smartlist_create();
1345 if (verbose) {
1346 const char *nickname = build_state_get_exit_nickname(circ->build_state);
1347 char *cp;
1348 tor_asprintf(&cp, "%s%s circ (length %d%s%s):",
1349 circ->build_state->is_internal ? "internal" : "exit",
1350 circ->build_state->need_uptime ? " (high-uptime)" : "",
1351 circ->build_state->desired_path_len,
1352 circ->_base.state == CIRCUIT_STATE_OPEN ? "" : ", exit ",
1353 circ->_base.state == CIRCUIT_STATE_OPEN ? "" :
1354 (nickname?nickname:"*unnamed*"));
1355 smartlist_add(elements, cp);
1358 hop = circ->cpath;
1359 do {
1360 routerinfo_t *ri;
1361 routerstatus_t *rs;
1362 char *elt;
1363 const char *id;
1364 if (!hop)
1365 break;
1366 if (!verbose && hop->state != CPATH_STATE_OPEN)
1367 break;
1368 if (!hop->extend_info)
1369 break;
1370 id = hop->extend_info->identity_digest;
1371 if (verbose_names) {
1372 elt = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
1373 if ((ri = router_get_by_digest(id))) {
1374 router_get_verbose_nickname(elt, ri);
1375 } else if ((rs = router_get_consensus_status_by_id(id))) {
1376 routerstatus_get_verbose_nickname(elt, rs);
1377 } else if (is_legal_nickname(hop->extend_info->nickname)) {
1378 elt[0] = '$';
1379 base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
1380 elt[HEX_DIGEST_LEN+1]= '~';
1381 strlcpy(elt+HEX_DIGEST_LEN+2,
1382 hop->extend_info->nickname, MAX_NICKNAME_LEN+1);
1383 } else {
1384 elt[0] = '$';
1385 base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
1387 } else { /* ! verbose_names */
1388 if ((ri = router_get_by_digest(id)) &&
1389 ri->is_named) {
1390 elt = tor_strdup(hop->extend_info->nickname);
1391 } else {
1392 elt = tor_malloc(HEX_DIGEST_LEN+2);
1393 elt[0] = '$';
1394 base16_encode(elt+1, HEX_DIGEST_LEN+1, id, DIGEST_LEN);
1397 tor_assert(elt);
1398 if (verbose) {
1399 size_t len = strlen(elt)+2+strlen(states[hop->state])+1;
1400 char *v = tor_malloc(len);
1401 tor_assert(hop->state <= 2);
1402 tor_snprintf(v,len,"%s(%s)",elt,states[hop->state]);
1403 smartlist_add(elements, v);
1404 tor_free(elt);
1405 } else {
1406 smartlist_add(elements, elt);
1408 hop = hop->next;
1409 } while (hop != circ->cpath);
1411 s = smartlist_join_strings(elements, verbose?" ":",", 0, NULL);
1412 SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
1413 smartlist_free(elements);
1414 return s;
1417 /** If <b>verbose</b> is false, allocate and return a comma-separated
1418 * list of the currently built elements of circuit_t. If
1419 * <b>verbose</b> is true, also list information about link status in
1420 * a more verbose format using spaces.
1422 char *
1423 circuit_list_path(origin_circuit_t *circ, int verbose)
1425 return circuit_list_path_impl(circ, verbose, 0);
1428 /** Allocate and return a comma-separated list of the currently built elements
1429 * of circuit_t, giving each as a verbose nickname.
1431 char *
1432 circuit_list_path_for_controller(origin_circuit_t *circ)
1434 return circuit_list_path_impl(circ, 0, 1);
1437 /** Log, at severity <b>severity</b>, the nicknames of each router in
1438 * circ's cpath. Also log the length of the cpath, and the intended
1439 * exit point.
1441 void
1442 circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
1444 char *s = circuit_list_path(circ,1);
1445 tor_log(severity,domain,"%s",s);
1446 tor_free(s);
1449 /** Tell the rep(utation)hist(ory) module about the status of the links
1450 * in circ. Hops that have become OPEN are marked as successfully
1451 * extended; the _first_ hop that isn't open (if any) is marked as
1452 * unable to extend.
1454 /* XXXX Someday we should learn from OR circuits too. */
1455 void
1456 circuit_rep_hist_note_result(origin_circuit_t *circ)
1458 crypt_path_t *hop;
1459 char *prev_digest = NULL;
1460 routerinfo_t *router;
1461 hop = circ->cpath;
1462 if (!hop) /* circuit hasn't started building yet. */
1463 return;
1464 if (server_mode(get_options())) {
1465 routerinfo_t *me = router_get_my_routerinfo();
1466 if (!me)
1467 return;
1468 prev_digest = me->cache_info.identity_digest;
1470 do {
1471 router = router_get_by_digest(hop->extend_info->identity_digest);
1472 if (router) {
1473 if (prev_digest) {
1474 if (hop->state == CPATH_STATE_OPEN)
1475 rep_hist_note_extend_succeeded(prev_digest,
1476 router->cache_info.identity_digest);
1477 else {
1478 rep_hist_note_extend_failed(prev_digest,
1479 router->cache_info.identity_digest);
1480 break;
1483 prev_digest = router->cache_info.identity_digest;
1484 } else {
1485 prev_digest = NULL;
1487 hop=hop->next;
1488 } while (hop!=circ->cpath);
1491 /** Pick all the entries in our cpath. Stop and return 0 when we're
1492 * happy, or return -1 if an error occurs. */
1493 static int
1494 onion_populate_cpath(origin_circuit_t *circ)
1496 int r;
1497 again:
1498 r = onion_extend_cpath(circ);
1499 if (r < 0) {
1500 log_info(LD_CIRC,"Generating cpath hop failed.");
1501 return -1;
1503 if (r == 0)
1504 goto again;
1505 return 0; /* if r == 1 */
1508 /** Create and return a new origin circuit. Initialize its purpose and
1509 * build-state based on our arguments. The <b>flags</b> argument is a
1510 * bitfield of CIRCLAUNCH_* flags. */
1511 origin_circuit_t *
1512 origin_circuit_init(uint8_t purpose, int flags)
1514 /* sets circ->p_circ_id and circ->p_conn */
1515 origin_circuit_t *circ = origin_circuit_new();
1516 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OR_WAIT);
1517 circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
1518 circ->build_state->onehop_tunnel =
1519 ((flags & CIRCLAUNCH_ONEHOP_TUNNEL) ? 1 : 0);
1520 circ->build_state->need_uptime =
1521 ((flags & CIRCLAUNCH_NEED_UPTIME) ? 1 : 0);
1522 circ->build_state->need_capacity =
1523 ((flags & CIRCLAUNCH_NEED_CAPACITY) ? 1 : 0);
1524 circ->build_state->is_internal =
1525 ((flags & CIRCLAUNCH_IS_INTERNAL) ? 1 : 0);
1526 circ->_base.purpose = purpose;
1527 return circ;
1530 /** Build a new circuit for <b>purpose</b>. If <b>exit</b>
1531 * is defined, then use that as your exit router, else choose a suitable
1532 * exit node.
1534 * Also launch a connection to the first OR in the chosen path, if
1535 * it's not open already.
1537 origin_circuit_t *
1538 circuit_establish_circuit(uint8_t purpose, extend_info_t *exit, int flags)
1540 origin_circuit_t *circ;
1541 int err_reason = 0;
1543 circ = origin_circuit_init(purpose, flags);
1545 if (onion_pick_cpath_exit(circ, exit) < 0 ||
1546 onion_populate_cpath(circ) < 0) {
1547 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOPATH);
1548 return NULL;
1551 control_event_circuit_status(circ, CIRC_EVENT_LAUNCHED, 0);
1553 if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
1554 circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
1555 return NULL;
1557 return circ;
1560 /** Start establishing the first hop of our circuit. Figure out what
1561 * OR we should connect to, and if necessary start the connection to
1562 * it. If we're already connected, then send the 'create' cell.
1563 * Return 0 for ok, -reason if circ should be marked-for-close. */
1565 circuit_handle_first_hop(origin_circuit_t *circ)
1567 crypt_path_t *firsthop;
1568 or_connection_t *n_conn;
1569 int err_reason = 0;
1570 const char *msg = NULL;
1571 int should_launch = 0;
1573 firsthop = onion_next_hop_in_cpath(circ->cpath);
1574 tor_assert(firsthop);
1575 tor_assert(firsthop->extend_info);
1577 /* now see if we're already connected to the first OR in 'route' */
1578 log_debug(LD_CIRC,"Looking for firsthop '%s:%u'",
1579 fmt_addr(&firsthop->extend_info->addr),
1580 firsthop->extend_info->port);
1582 n_conn = connection_or_get_for_extend(firsthop->extend_info->identity_digest,
1583 &firsthop->extend_info->addr,
1584 &msg,
1585 &should_launch);
1587 if (!n_conn) {
1588 /* not currently connected in a useful way. */
1589 const char *name = strlen(firsthop->extend_info->nickname) ?
1590 firsthop->extend_info->nickname : fmt_addr(&firsthop->extend_info->addr);
1591 log_info(LD_CIRC, "Next router is %s: %s ",
1592 safe_str_client(name), msg?msg:"???");
1593 circ->_base.n_hop = extend_info_dup(firsthop->extend_info);
1595 if (should_launch) {
1596 if (circ->build_state->onehop_tunnel)
1597 control_event_bootstrap(BOOTSTRAP_STATUS_CONN_DIR, 0);
1598 n_conn = connection_or_connect(&firsthop->extend_info->addr,
1599 firsthop->extend_info->port,
1600 firsthop->extend_info->identity_digest);
1601 if (!n_conn) { /* connect failed, forget the whole thing */
1602 log_info(LD_CIRC,"connect to firsthop failed. Closing.");
1603 return -END_CIRC_REASON_CONNECTFAILED;
1607 log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
1608 /* return success. The onion/circuit/etc will be taken care of
1609 * automatically (may already have been) whenever n_conn reaches
1610 * OR_CONN_STATE_OPEN.
1612 return 0;
1613 } else { /* it's already open. use it. */
1614 tor_assert(!circ->_base.n_hop);
1615 circ->_base.n_conn = n_conn;
1616 log_debug(LD_CIRC,"Conn open. Delivering first onion skin.");
1617 if ((err_reason = circuit_send_next_onion_skin(circ)) < 0) {
1618 log_info(LD_CIRC,"circuit_send_next_onion_skin failed.");
1619 return err_reason;
1622 return 0;
1625 /** Find any circuits that are waiting on <b>or_conn</b> to become
1626 * open and get them to send their create cells forward.
1628 * Status is 1 if connect succeeded, or 0 if connect failed.
1630 void
1631 circuit_n_conn_done(or_connection_t *or_conn, int status)
1633 smartlist_t *pending_circs;
1634 int err_reason = 0;
1636 log_debug(LD_CIRC,"or_conn to %s/%s, status=%d",
1637 or_conn->nickname ? or_conn->nickname : "NULL",
1638 or_conn->_base.address, status);
1640 pending_circs = smartlist_create();
1641 circuit_get_all_pending_on_or_conn(pending_circs, or_conn);
1643 SMARTLIST_FOREACH_BEGIN(pending_circs, circuit_t *, circ)
1645 /* These checks are redundant wrt get_all_pending_on_or_conn, but I'm
1646 * leaving them in in case it's possible for the status of a circuit to
1647 * change as we're going down the list. */
1648 if (circ->marked_for_close || circ->n_conn || !circ->n_hop ||
1649 circ->state != CIRCUIT_STATE_OR_WAIT)
1650 continue;
1652 if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
1653 /* Look at addr/port. This is an unkeyed connection. */
1654 if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) ||
1655 circ->n_hop->port != or_conn->_base.port)
1656 continue;
1657 } else {
1658 /* We expected a key. See if it's the right one. */
1659 if (memcmp(or_conn->identity_digest,
1660 circ->n_hop->identity_digest, DIGEST_LEN))
1661 continue;
1663 if (!status) { /* or_conn failed; close circ */
1664 log_info(LD_CIRC,"or_conn failed. Closing circ.");
1665 circuit_mark_for_close(circ, END_CIRC_REASON_OR_CONN_CLOSED);
1666 continue;
1668 log_debug(LD_CIRC, "Found circ, sending create cell.");
1669 /* circuit_deliver_create_cell will set n_circ_id and add us to
1670 * orconn_circuid_circuit_map, so we don't need to call
1671 * set_circid_orconn here. */
1672 circ->n_conn = or_conn;
1673 extend_info_free(circ->n_hop);
1674 circ->n_hop = NULL;
1676 if (CIRCUIT_IS_ORIGIN(circ)) {
1677 if ((err_reason =
1678 circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ))) < 0) {
1679 log_info(LD_CIRC,
1680 "send_next_onion_skin failed; circuit marked for closing.");
1681 circuit_mark_for_close(circ, -err_reason);
1682 continue;
1683 /* XXX could this be bad, eg if next_onion_skin failed because conn
1684 * died? */
1686 } else {
1687 /* pull the create cell out of circ->onionskin, and send it */
1688 tor_assert(circ->n_conn_onionskin);
1689 if (circuit_deliver_create_cell(circ,CELL_CREATE,
1690 circ->n_conn_onionskin)<0) {
1691 circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
1692 continue;
1694 tor_free(circ->n_conn_onionskin);
1695 circuit_set_state(circ, CIRCUIT_STATE_OPEN);
1698 SMARTLIST_FOREACH_END(circ);
1700 smartlist_free(pending_circs);
1703 /** Find a new circid that isn't currently in use on the circ->n_conn
1704 * for the outgoing
1705 * circuit <b>circ</b>, and deliver a cell of type <b>cell_type</b>
1706 * (either CELL_CREATE or CELL_CREATE_FAST) with payload <b>payload</b>
1707 * to this circuit.
1708 * Return -1 if we failed to find a suitable circid, else return 0.
1710 static int
1711 circuit_deliver_create_cell(circuit_t *circ, uint8_t cell_type,
1712 const char *payload)
1714 cell_t cell;
1715 circid_t id;
1717 tor_assert(circ);
1718 tor_assert(circ->n_conn);
1719 tor_assert(payload);
1720 tor_assert(cell_type == CELL_CREATE || cell_type == CELL_CREATE_FAST);
1722 id = get_unique_circ_id_by_conn(circ->n_conn);
1723 if (!id) {
1724 log_warn(LD_CIRC,"failed to get unique circID.");
1725 return -1;
1727 log_debug(LD_CIRC,"Chosen circID %u.", id);
1728 circuit_set_n_circid_orconn(circ, id, circ->n_conn);
1730 memset(&cell, 0, sizeof(cell_t));
1731 cell.command = cell_type;
1732 cell.circ_id = circ->n_circ_id;
1734 memcpy(cell.payload, payload, ONIONSKIN_CHALLENGE_LEN);
1735 append_cell_to_circuit_queue(circ, circ->n_conn, &cell, CELL_DIRECTION_OUT);
1737 if (CIRCUIT_IS_ORIGIN(circ)) {
1738 /* mark it so it gets better rate limiting treatment. */
1739 circ->n_conn->client_used = time(NULL);
1742 return 0;
1745 /** We've decided to start our reachability testing. If all
1746 * is set, log this to the user. Return 1 if we did, or 0 if
1747 * we chose not to log anything. */
1749 inform_testing_reachability(void)
1751 char dirbuf[128];
1752 routerinfo_t *me = router_get_my_routerinfo();
1753 if (!me)
1754 return 0;
1755 control_event_server_status(LOG_NOTICE,
1756 "CHECKING_REACHABILITY ORADDRESS=%s:%d",
1757 me->address, me->or_port);
1758 if (me->dir_port) {
1759 tor_snprintf(dirbuf, sizeof(dirbuf), " and DirPort %s:%d",
1760 me->address, me->dir_port);
1761 control_event_server_status(LOG_NOTICE,
1762 "CHECKING_REACHABILITY DIRADDRESS=%s:%d",
1763 me->address, me->dir_port);
1765 log_notice(LD_OR, "Now checking whether ORPort %s:%d%s %s reachable... "
1766 "(this may take up to %d minutes -- look for log "
1767 "messages indicating success)",
1768 me->address, me->or_port,
1769 me->dir_port ? dirbuf : "",
1770 me->dir_port ? "are" : "is",
1771 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT/60);
1773 return 1;
1776 /** Return true iff we should send a create_fast cell to start building a given
1777 * circuit */
1778 static INLINE int
1779 should_use_create_fast_for_circuit(origin_circuit_t *circ)
1781 or_options_t *options = get_options();
1782 tor_assert(circ->cpath);
1783 tor_assert(circ->cpath->extend_info);
1785 if (!circ->cpath->extend_info->onion_key)
1786 return 1; /* our hand is forced: only a create_fast will work. */
1787 if (!options->FastFirstHopPK)
1788 return 0; /* we prefer to avoid create_fast */
1789 if (server_mode(options)) {
1790 /* We're a server, and we know an onion key. We can choose.
1791 * Prefer to blend in. */
1792 return 0;
1795 return 1;
1798 /** This is the backbone function for building circuits.
1800 * If circ's first hop is closed, then we need to build a create
1801 * cell and send it forward.
1803 * Otherwise, we need to build a relay extend cell and send it
1804 * forward.
1806 * Return -reason if we want to tear down circ, else return 0.
1809 circuit_send_next_onion_skin(origin_circuit_t *circ)
1811 crypt_path_t *hop;
1812 routerinfo_t *router;
1813 char payload[2+4+DIGEST_LEN+ONIONSKIN_CHALLENGE_LEN];
1814 char *onionskin;
1815 size_t payload_len;
1817 tor_assert(circ);
1819 if (circ->cpath->state == CPATH_STATE_CLOSED) {
1820 int fast;
1821 uint8_t cell_type;
1822 log_debug(LD_CIRC,"First skin; sending create cell.");
1823 if (circ->build_state->onehop_tunnel)
1824 control_event_bootstrap(BOOTSTRAP_STATUS_ONEHOP_CREATE, 0);
1825 else
1826 control_event_bootstrap(BOOTSTRAP_STATUS_CIRCUIT_CREATE, 0);
1828 router = router_get_by_digest(circ->_base.n_conn->identity_digest);
1829 fast = should_use_create_fast_for_circuit(circ);
1830 if (!fast) {
1831 /* We are an OR and we know the right onion key: we should
1832 * send an old slow create cell.
1834 cell_type = CELL_CREATE;
1835 if (onion_skin_create(circ->cpath->extend_info->onion_key,
1836 &(circ->cpath->dh_handshake_state),
1837 payload) < 0) {
1838 log_warn(LD_CIRC,"onion_skin_create (first hop) failed.");
1839 return - END_CIRC_REASON_INTERNAL;
1841 note_request("cell: create", 1);
1842 } else {
1843 /* We are not an OR, and we're building the first hop of a circuit to a
1844 * new OR: we can be speedy and use CREATE_FAST to save an RSA operation
1845 * and a DH operation. */
1846 cell_type = CELL_CREATE_FAST;
1847 memset(payload, 0, sizeof(payload));
1848 crypto_rand(circ->cpath->fast_handshake_state,
1849 sizeof(circ->cpath->fast_handshake_state));
1850 memcpy(payload, circ->cpath->fast_handshake_state,
1851 sizeof(circ->cpath->fast_handshake_state));
1852 note_request("cell: create fast", 1);
1855 if (circuit_deliver_create_cell(TO_CIRCUIT(circ), cell_type, payload) < 0)
1856 return - END_CIRC_REASON_RESOURCELIMIT;
1858 circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
1859 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
1860 log_info(LD_CIRC,"First hop: finished sending %s cell to '%s'",
1861 fast ? "CREATE_FAST" : "CREATE",
1862 router ? router->nickname : "<unnamed>");
1863 } else {
1864 tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
1865 tor_assert(circ->_base.state == CIRCUIT_STATE_BUILDING);
1866 log_debug(LD_CIRC,"starting to send subsequent skin.");
1867 hop = onion_next_hop_in_cpath(circ->cpath);
1868 if (!hop) {
1869 /* done building the circuit. whew. */
1870 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
1871 if (!circ->build_state->onehop_tunnel) {
1872 struct timeval end;
1873 long timediff;
1874 tor_gettimeofday(&end);
1875 timediff = tv_mdiff(&circ->_base.highres_created, &end);
1877 * If the circuit build time is much greater than we would have cut
1878 * it off at, we probably had a suspend event along this codepath,
1879 * and we should discard the value.
1881 if (timediff < 0 || timediff > 2*circ_times.close_ms+1000) {
1882 log_notice(LD_CIRC, "Strange value for circuit build time: %ldmsec. "
1883 "Assuming clock jump.", timediff);
1884 } else if (!circuit_build_times_disabled()) {
1885 /* Don't count circuit times if the network was not live */
1886 if (circuit_build_times_network_check_live(&circ_times)) {
1887 circuit_build_times_add_time(&circ_times, (build_time_t)timediff);
1888 circuit_build_times_set_timeout(&circ_times);
1891 if (circ->_base.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
1892 circuit_build_times_network_circ_success(&circ_times);
1896 log_info(LD_CIRC,"circuit built!");
1897 circuit_reset_failure_count(0);
1898 if (circ->build_state->onehop_tunnel)
1899 control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_STATUS, 0);
1900 if (!has_completed_circuit && !circ->build_state->onehop_tunnel) {
1901 or_options_t *options = get_options();
1902 has_completed_circuit=1;
1903 /* FFFF Log a count of known routers here */
1904 log_notice(LD_GENERAL,
1905 "Tor has successfully opened a circuit. "
1906 "Looks like client functionality is working.");
1907 control_event_bootstrap(BOOTSTRAP_STATUS_DONE, 0);
1908 control_event_client_status(LOG_NOTICE, "CIRCUIT_ESTABLISHED");
1909 if (server_mode(options) && !check_whether_orport_reachable()) {
1910 inform_testing_reachability();
1911 consider_testing_reachability(1, 1);
1914 circuit_rep_hist_note_result(circ);
1915 circuit_has_opened(circ); /* do other actions as necessary */
1917 /* We're done with measurement circuits here. Just close them */
1918 if (circ->_base.purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
1919 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
1920 return 0;
1923 if (tor_addr_family(&hop->extend_info->addr) != AF_INET) {
1924 log_warn(LD_BUG, "Trying to extend to a non-IPv4 address.");
1925 return - END_CIRC_REASON_INTERNAL;
1928 set_uint32(payload, tor_addr_to_ipv4n(&hop->extend_info->addr));
1929 set_uint16(payload+4, htons(hop->extend_info->port));
1931 onionskin = payload+2+4;
1932 memcpy(payload+2+4+ONIONSKIN_CHALLENGE_LEN,
1933 hop->extend_info->identity_digest, DIGEST_LEN);
1934 payload_len = 2+4+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN;
1936 if (onion_skin_create(hop->extend_info->onion_key,
1937 &(hop->dh_handshake_state), onionskin) < 0) {
1938 log_warn(LD_CIRC,"onion_skin_create failed.");
1939 return - END_CIRC_REASON_INTERNAL;
1942 log_info(LD_CIRC,"Sending extend relay cell.");
1943 note_request("cell: extend", 1);
1944 /* send it to hop->prev, because it will transfer
1945 * it to a create cell and then send to hop */
1946 if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
1947 RELAY_COMMAND_EXTEND,
1948 payload, payload_len, hop->prev) < 0)
1949 return 0; /* circuit is closed */
1951 hop->state = CPATH_STATE_AWAITING_KEYS;
1953 return 0;
1956 /** Our clock just jumped by <b>seconds_elapsed</b>. Assume
1957 * something has also gone wrong with our network: notify the user,
1958 * and abandon all not-yet-used circuits. */
1959 void
1960 circuit_note_clock_jumped(int seconds_elapsed)
1962 int severity = server_mode(get_options()) ? LOG_WARN : LOG_NOTICE;
1963 tor_log(severity, LD_GENERAL, "Your system clock just jumped %d seconds %s; "
1964 "assuming established circuits no longer work.",
1965 seconds_elapsed >=0 ? seconds_elapsed : -seconds_elapsed,
1966 seconds_elapsed >=0 ? "forward" : "backward");
1967 control_event_general_status(LOG_WARN, "CLOCK_JUMPED TIME=%d",
1968 seconds_elapsed);
1969 has_completed_circuit=0; /* so it'll log when it works again */
1970 control_event_client_status(severity, "CIRCUIT_NOT_ESTABLISHED REASON=%s",
1971 "CLOCK_JUMPED");
1972 circuit_mark_all_unused_circs();
1973 circuit_expire_all_dirty_circs();
1976 /** Take the 'extend' <b>cell</b>, pull out addr/port plus the onion
1977 * skin and identity digest for the next hop. If we're already connected,
1978 * pass the onion skin to the next hop using a create cell; otherwise
1979 * launch a new OR connection, and <b>circ</b> will notice when the
1980 * connection succeeds or fails.
1982 * Return -1 if we want to warn and tear down the circuit, else return 0.
1985 circuit_extend(cell_t *cell, circuit_t *circ)
1987 or_connection_t *n_conn;
1988 relay_header_t rh;
1989 char *onionskin;
1990 char *id_digest=NULL;
1991 uint32_t n_addr32;
1992 uint16_t n_port;
1993 tor_addr_t n_addr;
1994 const char *msg = NULL;
1995 int should_launch = 0;
1997 if (circ->n_conn) {
1998 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1999 "n_conn already set. Bug/attack. Closing.");
2000 return -1;
2002 if (circ->n_hop) {
2003 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2004 "conn to next hop already launched. Bug/attack. Closing.");
2005 return -1;
2008 if (!server_mode(get_options())) {
2009 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2010 "Got an extend cell, but running as a client. Closing.");
2011 return -1;
2014 relay_header_unpack(&rh, cell->payload);
2016 if (rh.length < 4+2+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN) {
2017 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2018 "Wrong length %d on extend cell. Closing circuit.",
2019 rh.length);
2020 return -1;
2023 n_addr32 = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
2024 n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
2025 onionskin = cell->payload+RELAY_HEADER_SIZE+4+2;
2026 id_digest = cell->payload+RELAY_HEADER_SIZE+4+2+ONIONSKIN_CHALLENGE_LEN;
2027 tor_addr_from_ipv4h(&n_addr, n_addr32);
2029 if (!n_port || !n_addr32) {
2030 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2031 "Client asked me to extend to zero destination port or addr.");
2032 return -1;
2035 /* Check if they asked us for 0000..0000. We support using
2036 * an empty fingerprint for the first hop (e.g. for a bridge relay),
2037 * but we don't want to let people send us extend cells for empty
2038 * fingerprints -- a) because it opens the user up to a mitm attack,
2039 * and b) because it lets an attacker force the relay to hold open a
2040 * new TLS connection for each extend request. */
2041 if (tor_digest_is_zero(id_digest)) {
2042 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2043 "Client asked me to extend without specifying an id_digest.");
2044 return -1;
2047 /* Next, check if we're being asked to connect to the hop that the
2048 * extend cell came from. There isn't any reason for that, and it can
2049 * assist circular-path attacks. */
2050 if (!memcmp(id_digest, TO_OR_CIRCUIT(circ)->p_conn->identity_digest,
2051 DIGEST_LEN)) {
2052 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2053 "Client asked me to extend back to the previous hop.");
2054 return -1;
2057 n_conn = connection_or_get_for_extend(id_digest,
2058 &n_addr,
2059 &msg,
2060 &should_launch);
2062 if (!n_conn) {
2063 log_debug(LD_CIRC|LD_OR,"Next router (%s:%d): %s",
2064 fmt_addr(&n_addr), (int)n_port, msg?msg:"????");
2066 circ->n_hop = extend_info_alloc(NULL /*nickname*/,
2067 id_digest,
2068 NULL /*onion_key*/,
2069 &n_addr, n_port);
2071 circ->n_conn_onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
2072 memcpy(circ->n_conn_onionskin, onionskin, ONIONSKIN_CHALLENGE_LEN);
2073 circuit_set_state(circ, CIRCUIT_STATE_OR_WAIT);
2075 if (should_launch) {
2076 /* we should try to open a connection */
2077 n_conn = connection_or_connect(&n_addr, n_port, id_digest);
2078 if (!n_conn) {
2079 log_info(LD_CIRC,"Launching n_conn failed. Closing circuit.");
2080 circuit_mark_for_close(circ, END_CIRC_REASON_CONNECTFAILED);
2081 return 0;
2083 log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
2085 /* return success. The onion/circuit/etc will be taken care of
2086 * automatically (may already have been) whenever n_conn reaches
2087 * OR_CONN_STATE_OPEN.
2089 return 0;
2092 tor_assert(!circ->n_hop); /* Connection is already established. */
2093 circ->n_conn = n_conn;
2094 log_debug(LD_CIRC,"n_conn is %s:%u",
2095 n_conn->_base.address,n_conn->_base.port);
2097 if (circuit_deliver_create_cell(circ, CELL_CREATE, onionskin) < 0)
2098 return -1;
2099 return 0;
2102 /** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in
2103 * key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
2104 * used as follows:
2105 * - 20 to initialize f_digest
2106 * - 20 to initialize b_digest
2107 * - 16 to key f_crypto
2108 * - 16 to key b_crypto
2110 * (If 'reverse' is true, then f_XX and b_XX are swapped.)
2113 circuit_init_cpath_crypto(crypt_path_t *cpath, const char *key_data,
2114 int reverse)
2116 crypto_digest_env_t *tmp_digest;
2117 crypto_cipher_env_t *tmp_crypto;
2119 tor_assert(cpath);
2120 tor_assert(key_data);
2121 tor_assert(!(cpath->f_crypto || cpath->b_crypto ||
2122 cpath->f_digest || cpath->b_digest));
2124 cpath->f_digest = crypto_new_digest_env();
2125 crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
2126 cpath->b_digest = crypto_new_digest_env();
2127 crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
2129 if (!(cpath->f_crypto =
2130 crypto_create_init_cipher(key_data+(2*DIGEST_LEN),1))) {
2131 log_warn(LD_BUG,"Forward cipher initialization failed.");
2132 return -1;
2134 if (!(cpath->b_crypto =
2135 crypto_create_init_cipher(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN,0))) {
2136 log_warn(LD_BUG,"Backward cipher initialization failed.");
2137 return -1;
2140 if (reverse) {
2141 tmp_digest = cpath->f_digest;
2142 cpath->f_digest = cpath->b_digest;
2143 cpath->b_digest = tmp_digest;
2144 tmp_crypto = cpath->f_crypto;
2145 cpath->f_crypto = cpath->b_crypto;
2146 cpath->b_crypto = tmp_crypto;
2149 return 0;
2152 /** A created or extended cell came back to us on the circuit, and it included
2153 * <b>reply</b> as its body. (If <b>reply_type</b> is CELL_CREATED, the body
2154 * contains (the second DH key, plus KH). If <b>reply_type</b> is
2155 * CELL_CREATED_FAST, the body contains a secret y and a hash H(x|y).)
2157 * Calculate the appropriate keys and digests, make sure KH is
2158 * correct, and initialize this hop of the cpath.
2160 * Return - reason if we want to mark circ for close, else return 0.
2163 circuit_finish_handshake(origin_circuit_t *circ, uint8_t reply_type,
2164 const char *reply)
2166 char keys[CPATH_KEY_MATERIAL_LEN];
2167 crypt_path_t *hop;
2169 if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
2170 hop = circ->cpath;
2171 else {
2172 hop = onion_next_hop_in_cpath(circ->cpath);
2173 if (!hop) { /* got an extended when we're all done? */
2174 log_warn(LD_PROTOCOL,"got extended when circ already built? Closing.");
2175 return - END_CIRC_REASON_TORPROTOCOL;
2178 tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
2180 if (reply_type == CELL_CREATED && hop->dh_handshake_state) {
2181 if (onion_skin_client_handshake(hop->dh_handshake_state, reply, keys,
2182 DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
2183 log_warn(LD_CIRC,"onion_skin_client_handshake failed.");
2184 return -END_CIRC_REASON_TORPROTOCOL;
2186 /* Remember hash of g^xy */
2187 memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
2188 } else if (reply_type == CELL_CREATED_FAST && !hop->dh_handshake_state) {
2189 if (fast_client_handshake(hop->fast_handshake_state, reply, keys,
2190 DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
2191 log_warn(LD_CIRC,"fast_client_handshake failed.");
2192 return -END_CIRC_REASON_TORPROTOCOL;
2194 memcpy(hop->handshake_digest, reply+DIGEST_LEN, DIGEST_LEN);
2195 } else {
2196 log_warn(LD_PROTOCOL,"CREATED cell type did not match CREATE cell type.");
2197 return -END_CIRC_REASON_TORPROTOCOL;
2200 crypto_dh_free(hop->dh_handshake_state); /* don't need it anymore */
2201 hop->dh_handshake_state = NULL;
2203 memset(hop->fast_handshake_state, 0, sizeof(hop->fast_handshake_state));
2205 if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
2206 return -END_CIRC_REASON_TORPROTOCOL;
2209 hop->state = CPATH_STATE_OPEN;
2210 log_info(LD_CIRC,"Finished building %scircuit hop:",
2211 (reply_type == CELL_CREATED_FAST) ? "fast " : "");
2212 circuit_log_path(LOG_INFO,LD_CIRC,circ);
2213 control_event_circuit_status(circ, CIRC_EVENT_EXTENDED, 0);
2215 return 0;
2218 /** We received a relay truncated cell on circ.
2220 * Since we don't ask for truncates currently, getting a truncated
2221 * means that a connection broke or an extend failed. For now,
2222 * just give up: for circ to close, and return 0.
2225 circuit_truncated(origin_circuit_t *circ, crypt_path_t *layer)
2227 // crypt_path_t *victim;
2228 // connection_t *stream;
2230 tor_assert(circ);
2231 tor_assert(layer);
2233 /* XXX Since we don't ask for truncates currently, getting a truncated
2234 * means that a connection broke or an extend failed. For now,
2235 * just give up.
2237 circuit_mark_for_close(TO_CIRCUIT(circ),
2238 END_CIRC_REASON_FLAG_REMOTE|END_CIRC_REASON_OR_CONN_CLOSED);
2239 return 0;
2241 #if 0
2242 while (layer->next != circ->cpath) {
2243 /* we need to clear out layer->next */
2244 victim = layer->next;
2245 log_debug(LD_CIRC, "Killing a layer of the cpath.");
2247 for (stream = circ->p_streams; stream; stream=stream->next_stream) {
2248 if (stream->cpath_layer == victim) {
2249 log_info(LD_APP, "Marking stream %d for close because of truncate.",
2250 stream->stream_id);
2251 /* no need to send 'end' relay cells,
2252 * because the other side's already dead
2254 connection_mark_unattached_ap(stream, END_STREAM_REASON_DESTROY);
2258 layer->next = victim->next;
2259 circuit_free_cpath_node(victim);
2262 log_info(LD_CIRC, "finished");
2263 return 0;
2264 #endif
2267 /** Given a response payload and keys, initialize, then send a created
2268 * cell back.
2271 onionskin_answer(or_circuit_t *circ, uint8_t cell_type, const char *payload,
2272 const char *keys)
2274 cell_t cell;
2275 crypt_path_t *tmp_cpath;
2277 tmp_cpath = tor_malloc_zero(sizeof(crypt_path_t));
2278 tmp_cpath->magic = CRYPT_PATH_MAGIC;
2280 memset(&cell, 0, sizeof(cell_t));
2281 cell.command = cell_type;
2282 cell.circ_id = circ->p_circ_id;
2284 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
2286 memcpy(cell.payload, payload,
2287 cell_type == CELL_CREATED ? ONIONSKIN_REPLY_LEN : DIGEST_LEN*2);
2289 log_debug(LD_CIRC,"init digest forward 0x%.8x, backward 0x%.8x.",
2290 (unsigned int)*(uint32_t*)(keys),
2291 (unsigned int)*(uint32_t*)(keys+20));
2292 if (circuit_init_cpath_crypto(tmp_cpath, keys, 0)<0) {
2293 log_warn(LD_BUG,"Circuit initialization failed");
2294 tor_free(tmp_cpath);
2295 return -1;
2297 circ->n_digest = tmp_cpath->f_digest;
2298 circ->n_crypto = tmp_cpath->f_crypto;
2299 circ->p_digest = tmp_cpath->b_digest;
2300 circ->p_crypto = tmp_cpath->b_crypto;
2301 tmp_cpath->magic = 0;
2302 tor_free(tmp_cpath);
2304 if (cell_type == CELL_CREATED)
2305 memcpy(circ->handshake_digest, cell.payload+DH_KEY_LEN, DIGEST_LEN);
2306 else
2307 memcpy(circ->handshake_digest, cell.payload+DIGEST_LEN, DIGEST_LEN);
2309 circ->is_first_hop = (cell_type == CELL_CREATED_FAST);
2311 append_cell_to_circuit_queue(TO_CIRCUIT(circ),
2312 circ->p_conn, &cell, CELL_DIRECTION_IN);
2313 log_debug(LD_CIRC,"Finished sending 'created' cell.");
2315 if (!is_local_addr(&circ->p_conn->_base.addr) &&
2316 !connection_or_nonopen_was_started_here(circ->p_conn)) {
2317 /* record that we could process create cells from a non-local conn
2318 * that we didn't initiate; presumably this means that create cells
2319 * can reach us too. */
2320 router_orport_found_reachable();
2323 return 0;
2326 /** How many hops does a general-purpose circuit have by default? */
2327 #define DEFAULT_ROUTE_LEN 3
2329 /** Choose a length for a circuit of purpose <b>purpose</b>.
2330 * Default length is 3 + the number of endpoints that would give something
2331 * away. If the routerlist <b>routers</b> doesn't have enough routers
2332 * to handle the desired path length, return as large a path length as
2333 * is feasible, except if it's less than 2, in which case return -1.
2335 static int
2336 new_route_len(uint8_t purpose, extend_info_t *exit,
2337 smartlist_t *routers)
2339 int num_acceptable_routers;
2340 int routelen;
2342 tor_assert(routers);
2344 routelen = DEFAULT_ROUTE_LEN;
2345 if (exit &&
2346 purpose != CIRCUIT_PURPOSE_TESTING &&
2347 purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
2348 routelen++;
2350 num_acceptable_routers = count_acceptable_routers(routers);
2352 log_debug(LD_CIRC,"Chosen route length %d (%d/%d routers suitable).",
2353 routelen, num_acceptable_routers, smartlist_len(routers));
2355 if (num_acceptable_routers < 2) {
2356 log_info(LD_CIRC,
2357 "Not enough acceptable routers (%d). Discarding this circuit.",
2358 num_acceptable_routers);
2359 return -1;
2362 if (num_acceptable_routers < routelen) {
2363 log_info(LD_CIRC,"Not enough routers: cutting routelen from %d to %d.",
2364 routelen, num_acceptable_routers);
2365 routelen = num_acceptable_routers;
2368 return routelen;
2371 /** Fetch the list of predicted ports, dup it into a smartlist of
2372 * uint16_t's, remove the ones that are already handled by an
2373 * existing circuit, and return it.
2375 static smartlist_t *
2376 circuit_get_unhandled_ports(time_t now)
2378 smartlist_t *source = rep_hist_get_predicted_ports(now);
2379 smartlist_t *dest = smartlist_create();
2380 uint16_t *tmp;
2381 int i;
2383 for (i = 0; i < smartlist_len(source); ++i) {
2384 tmp = tor_malloc(sizeof(uint16_t));
2385 memcpy(tmp, smartlist_get(source, i), sizeof(uint16_t));
2386 smartlist_add(dest, tmp);
2389 circuit_remove_handled_ports(dest);
2390 return dest;
2393 /** Return 1 if we already have circuits present or on the way for
2394 * all anticipated ports. Return 0 if we should make more.
2396 * If we're returning 0, set need_uptime and need_capacity to
2397 * indicate any requirements that the unhandled ports have.
2400 circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
2401 int *need_capacity)
2403 int i, enough;
2404 uint16_t *port;
2405 smartlist_t *sl = circuit_get_unhandled_ports(now);
2406 smartlist_t *LongLivedServices = get_options()->LongLivedPorts;
2407 tor_assert(need_uptime);
2408 tor_assert(need_capacity);
2409 // Always predict need_capacity
2410 *need_capacity = 1;
2411 enough = (smartlist_len(sl) == 0);
2412 for (i = 0; i < smartlist_len(sl); ++i) {
2413 port = smartlist_get(sl, i);
2414 if (smartlist_string_num_isin(LongLivedServices, *port))
2415 *need_uptime = 1;
2416 tor_free(port);
2418 smartlist_free(sl);
2419 return enough;
2422 /** Return 1 if <b>router</b> can handle one or more of the ports in
2423 * <b>needed_ports</b>, else return 0.
2425 static int
2426 router_handles_some_port(routerinfo_t *router, smartlist_t *needed_ports)
2428 int i;
2429 uint16_t port;
2431 for (i = 0; i < smartlist_len(needed_ports); ++i) {
2432 addr_policy_result_t r;
2433 port = *(uint16_t *)smartlist_get(needed_ports, i);
2434 tor_assert(port);
2435 r = compare_addr_to_addr_policy(0, port, router->exit_policy);
2436 if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
2437 return 1;
2439 return 0;
2442 /** Return true iff <b>conn</b> needs another general circuit to be
2443 * built. */
2444 static int
2445 ap_stream_wants_exit_attention(connection_t *conn)
2447 if (conn->type == CONN_TYPE_AP &&
2448 conn->state == AP_CONN_STATE_CIRCUIT_WAIT &&
2449 !conn->marked_for_close &&
2450 !(TO_EDGE_CONN(conn)->want_onehop) && /* ignore one-hop streams */
2451 !(TO_EDGE_CONN(conn)->use_begindir) && /* ignore targeted dir fetches */
2452 !(TO_EDGE_CONN(conn)->chosen_exit_name) && /* ignore defined streams */
2453 !connection_edge_is_rendezvous_stream(TO_EDGE_CONN(conn)) &&
2454 !circuit_stream_is_being_handled(TO_EDGE_CONN(conn), 0,
2455 MIN_CIRCUITS_HANDLING_STREAM))
2456 return 1;
2457 return 0;
2460 /** Return a pointer to a suitable router to be the exit node for the
2461 * general-purpose circuit we're about to build.
2463 * Look through the connection array, and choose a router that maximizes
2464 * the number of pending streams that can exit from this router.
2466 * Return NULL if we can't find any suitable routers.
2468 static routerinfo_t *
2469 choose_good_exit_server_general(routerlist_t *dir, int need_uptime,
2470 int need_capacity)
2472 int *n_supported;
2473 int i;
2474 int n_pending_connections = 0;
2475 smartlist_t *connections;
2476 int best_support = -1;
2477 int n_best_support=0;
2478 routerinfo_t *router;
2479 or_options_t *options = get_options();
2481 connections = get_connection_array();
2483 /* Count how many connections are waiting for a circuit to be built.
2484 * We use this for log messages now, but in the future we may depend on it.
2486 SMARTLIST_FOREACH(connections, connection_t *, conn,
2488 if (ap_stream_wants_exit_attention(conn))
2489 ++n_pending_connections;
2491 // log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
2492 // n_pending_connections);
2493 /* Now we count, for each of the routers in the directory, how many
2494 * of the pending connections could possibly exit from that
2495 * router (n_supported[i]). (We can't be sure about cases where we
2496 * don't know the IP address of the pending connection.)
2498 * -1 means "Don't use this router at all."
2500 n_supported = tor_malloc(sizeof(int)*smartlist_len(dir->routers));
2501 for (i = 0; i < smartlist_len(dir->routers); ++i) {/* iterate over routers */
2502 router = smartlist_get(dir->routers, i);
2503 if (router_is_me(router)) {
2504 n_supported[i] = -1;
2505 // log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
2506 /* XXX there's probably a reverse predecessor attack here, but
2507 * it's slow. should we take this out? -RD
2509 continue;
2511 if (!router->is_running || router->is_bad_exit) {
2512 n_supported[i] = -1;
2513 continue; /* skip routers that are known to be down or bad exits */
2515 if (router_is_unreliable(router, need_uptime, need_capacity, 0) &&
2516 (!options->ExitNodes ||
2517 !routerset_contains_router(options->ExitNodes, router))) {
2518 /* FFFF Someday, differentiate between a routerset that names
2519 * routers, and a routerset that names countries, and only do this
2520 * check if they've asked for specific exit relays. Or if the country
2521 * they ask for is rare. Or something. */
2522 n_supported[i] = -1;
2523 continue; /* skip routers that are not suitable, unless we have
2524 * ExitNodes set, in which case we asked for it */
2526 if (!(router->is_valid || options->_AllowInvalid & ALLOW_INVALID_EXIT)) {
2527 /* if it's invalid and we don't want it */
2528 n_supported[i] = -1;
2529 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- invalid router.",
2530 // router->nickname, i);
2531 continue; /* skip invalid routers */
2533 if (options->ExcludeSingleHopRelays && router->allow_single_hop_exits) {
2534 n_supported[i] = -1;
2535 continue;
2537 if (router_exit_policy_rejects_all(router)) {
2538 n_supported[i] = -1;
2539 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
2540 // router->nickname, i);
2541 continue; /* skip routers that reject all */
2543 n_supported[i] = 0;
2544 /* iterate over connections */
2545 SMARTLIST_FOREACH(connections, connection_t *, conn,
2547 if (!ap_stream_wants_exit_attention(conn))
2548 continue; /* Skip everything but APs in CIRCUIT_WAIT */
2549 if (connection_ap_can_use_exit(TO_EDGE_CONN(conn), router, 1)) {
2550 ++n_supported[i];
2551 // log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
2552 // router->nickname, i, n_supported[i]);
2553 } else {
2554 // log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
2555 // router->nickname, i);
2557 }); /* End looping over connections. */
2558 if (n_pending_connections > 0 && n_supported[i] == 0) {
2559 /* Leave best_support at -1 if that's where it is, so we can
2560 * distinguish it later. */
2561 continue;
2563 if (n_supported[i] > best_support) {
2564 /* If this router is better than previous ones, remember its index
2565 * and goodness, and start counting how many routers are this good. */
2566 best_support = n_supported[i]; n_best_support=1;
2567 // log_fn(LOG_DEBUG,"%s is new best supported option so far.",
2568 // router->nickname);
2569 } else if (n_supported[i] == best_support) {
2570 /* If this router is _as good_ as the best one, just increment the
2571 * count of equally good routers.*/
2572 ++n_best_support;
2575 log_info(LD_CIRC,
2576 "Found %d servers that might support %d/%d pending connections.",
2577 n_best_support, best_support >= 0 ? best_support : 0,
2578 n_pending_connections);
2580 /* If any routers definitely support any pending connections, choose one
2581 * at random. */
2582 if (best_support > 0) {
2583 smartlist_t *supporting = smartlist_create(), *use = smartlist_create();
2585 for (i = 0; i < smartlist_len(dir->routers); i++)
2586 if (n_supported[i] == best_support)
2587 smartlist_add(supporting, smartlist_get(dir->routers, i));
2589 routersets_get_disjunction(use, supporting, options->ExitNodes,
2590 options->_ExcludeExitNodesUnion, 1);
2591 if (smartlist_len(use) == 0 && options->ExitNodes &&
2592 !options->StrictNodes) { /* give up on exitnodes and try again */
2593 routersets_get_disjunction(use, supporting, NULL,
2594 options->_ExcludeExitNodesUnion, 1);
2596 router = routerlist_sl_choose_by_bandwidth(use, WEIGHT_FOR_EXIT);
2597 smartlist_free(use);
2598 smartlist_free(supporting);
2599 } else {
2600 /* Either there are no pending connections, or no routers even seem to
2601 * possibly support any of them. Choose a router at random that satisfies
2602 * at least one predicted exit port. */
2604 int attempt;
2605 smartlist_t *needed_ports, *supporting, *use;
2607 if (best_support == -1) {
2608 if (need_uptime || need_capacity) {
2609 log_info(LD_CIRC,
2610 "We couldn't find any live%s%s routers; falling back "
2611 "to list of all routers.",
2612 need_capacity?", fast":"",
2613 need_uptime?", stable":"");
2614 tor_free(n_supported);
2615 return choose_good_exit_server_general(dir, 0, 0);
2617 log_notice(LD_CIRC, "All routers are down or won't exit%s -- "
2618 "choosing a doomed exit at random.",
2619 options->_ExcludeExitNodesUnion ? " or are Excluded" : "");
2621 supporting = smartlist_create();
2622 use = smartlist_create();
2623 needed_ports = circuit_get_unhandled_ports(time(NULL));
2624 for (attempt = 0; attempt < 2; attempt++) {
2625 /* try once to pick only from routers that satisfy a needed port,
2626 * then if there are none, pick from any that support exiting. */
2627 for (i = 0; i < smartlist_len(dir->routers); i++) {
2628 router = smartlist_get(dir->routers, i);
2629 if (n_supported[i] != -1 &&
2630 (attempt || router_handles_some_port(router, needed_ports))) {
2631 // log_fn(LOG_DEBUG,"Try %d: '%s' is a possibility.",
2632 // try, router->nickname);
2633 smartlist_add(supporting, router);
2637 routersets_get_disjunction(use, supporting, options->ExitNodes,
2638 options->_ExcludeExitNodesUnion, 1);
2639 if (smartlist_len(use) == 0 && options->ExitNodes &&
2640 !options->StrictNodes) { /* give up on exitnodes and try again */
2641 routersets_get_disjunction(use, supporting, NULL,
2642 options->_ExcludeExitNodesUnion, 1);
2644 /* FFF sometimes the above results in null, when the requested
2645 * exit node is considered down by the consensus. we should pick
2646 * it anyway, since the user asked for it. */
2647 router = routerlist_sl_choose_by_bandwidth(use, WEIGHT_FOR_EXIT);
2648 if (router)
2649 break;
2650 smartlist_clear(supporting);
2651 smartlist_clear(use);
2653 SMARTLIST_FOREACH(needed_ports, uint16_t *, cp, tor_free(cp));
2654 smartlist_free(needed_ports);
2655 smartlist_free(use);
2656 smartlist_free(supporting);
2659 tor_free(n_supported);
2660 if (router) {
2661 log_info(LD_CIRC, "Chose exit server '%s'", router->nickname);
2662 return router;
2664 if (options->ExitNodes && options->StrictNodes) {
2665 log_warn(LD_CIRC,
2666 "No specified exit routers seem to be running, and "
2667 "StrictNodes is set: can't choose an exit.");
2669 return NULL;
2672 /** Return a pointer to a suitable router to be the exit node for the
2673 * circuit of purpose <b>purpose</b> that we're about to build (or NULL
2674 * if no router is suitable).
2676 * For general-purpose circuits, pass it off to
2677 * choose_good_exit_server_general()
2679 * For client-side rendezvous circuits, choose a random node, weighted
2680 * toward the preferences in 'options'.
2682 static routerinfo_t *
2683 choose_good_exit_server(uint8_t purpose, routerlist_t *dir,
2684 int need_uptime, int need_capacity, int is_internal)
2686 or_options_t *options = get_options();
2687 router_crn_flags_t flags = 0;
2688 if (need_uptime)
2689 flags |= CRN_NEED_UPTIME;
2690 if (need_capacity)
2691 flags |= CRN_NEED_CAPACITY;
2693 switch (purpose) {
2694 case CIRCUIT_PURPOSE_C_GENERAL:
2695 if (options->_AllowInvalid & ALLOW_INVALID_MIDDLE)
2696 flags |= CRN_ALLOW_INVALID;
2697 if (is_internal) /* pick it like a middle hop */
2698 return router_choose_random_node(NULL, options->ExcludeNodes, flags);
2699 else
2700 return choose_good_exit_server_general(dir,need_uptime,need_capacity);
2701 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
2702 if (options->_AllowInvalid & ALLOW_INVALID_RENDEZVOUS)
2703 flags |= CRN_ALLOW_INVALID;
2704 return router_choose_random_node(NULL, options->ExcludeNodes, flags);
2706 log_warn(LD_BUG,"Unhandled purpose %d", purpose);
2707 tor_fragile_assert();
2708 return NULL;
2711 /** Log a warning if the user specified an exit for the circuit that
2712 * has been excluded from use by ExcludeNodes or ExcludeExitNodes. */
2713 static void
2714 warn_if_last_router_excluded(origin_circuit_t *circ, const extend_info_t *exit)
2716 or_options_t *options = get_options();
2717 routerset_t *rs = options->ExcludeNodes;
2718 const char *description;
2719 int domain = LD_CIRC;
2720 uint8_t purpose = circ->_base.purpose;
2722 if (circ->build_state->onehop_tunnel)
2723 return;
2725 switch (purpose)
2727 default:
2728 case CIRCUIT_PURPOSE_OR:
2729 case CIRCUIT_PURPOSE_INTRO_POINT:
2730 case CIRCUIT_PURPOSE_REND_POINT_WAITING:
2731 case CIRCUIT_PURPOSE_REND_ESTABLISHED:
2732 log_warn(LD_BUG, "Called on non-origin circuit (purpose %d)",
2733 (int)purpose);
2734 return;
2735 case CIRCUIT_PURPOSE_C_GENERAL:
2736 if (circ->build_state->is_internal)
2737 return;
2738 description = "Requested exit node";
2739 rs = options->_ExcludeExitNodesUnion;
2740 break;
2741 case CIRCUIT_PURPOSE_C_INTRODUCING:
2742 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
2743 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
2744 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
2745 case CIRCUIT_PURPOSE_S_CONNECT_REND:
2746 case CIRCUIT_PURPOSE_S_REND_JOINED:
2747 case CIRCUIT_PURPOSE_TESTING:
2748 return;
2749 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
2750 case CIRCUIT_PURPOSE_C_REND_READY:
2751 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
2752 case CIRCUIT_PURPOSE_C_REND_JOINED:
2753 description = "Chosen rendezvous point";
2754 domain = LD_BUG;
2755 break;
2756 case CIRCUIT_PURPOSE_CONTROLLER:
2757 rs = options->_ExcludeExitNodesUnion;
2758 description = "Controller-selected circuit target";
2759 break;
2762 if (routerset_contains_extendinfo(rs, exit)) {
2763 log_fn(LOG_WARN, domain, "%s '%s' is in ExcludeNodes%s. Using anyway "
2764 "(circuit purpose %d).",
2765 description,exit->nickname,
2766 rs==options->ExcludeNodes?"":" or ExcludeExitNodes",
2767 (int)purpose);
2768 circuit_log_path(LOG_WARN, domain, circ);
2771 return;
2774 /** Decide a suitable length for circ's cpath, and pick an exit
2775 * router (or use <b>exit</b> if provided). Store these in the
2776 * cpath. Return 0 if ok, -1 if circuit should be closed. */
2777 static int
2778 onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit)
2780 cpath_build_state_t *state = circ->build_state;
2781 routerlist_t *rl = router_get_routerlist();
2783 if (state->onehop_tunnel) {
2784 log_debug(LD_CIRC, "Launching a one-hop circuit for dir tunnel.");
2785 state->desired_path_len = 1;
2786 } else {
2787 int r = new_route_len(circ->_base.purpose, exit, rl->routers);
2788 if (r < 1) /* must be at least 1 */
2789 return -1;
2790 state->desired_path_len = r;
2793 if (exit) { /* the circuit-builder pre-requested one */
2794 warn_if_last_router_excluded(circ, exit);
2795 log_info(LD_CIRC,"Using requested exit node '%s'", exit->nickname);
2796 exit = extend_info_dup(exit);
2797 } else { /* we have to decide one */
2798 routerinfo_t *router =
2799 choose_good_exit_server(circ->_base.purpose, rl, state->need_uptime,
2800 state->need_capacity, state->is_internal);
2801 if (!router) {
2802 log_warn(LD_CIRC,"failed to choose an exit server");
2803 return -1;
2805 exit = extend_info_from_router(router);
2807 state->chosen_exit = exit;
2808 return 0;
2811 /** Give <b>circ</b> a new exit destination to <b>exit</b>, and add a
2812 * hop to the cpath reflecting this. Don't send the next extend cell --
2813 * the caller will do this if it wants to.
2816 circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit)
2818 cpath_build_state_t *state;
2819 tor_assert(exit);
2820 tor_assert(circ);
2822 state = circ->build_state;
2823 tor_assert(state);
2824 extend_info_free(state->chosen_exit);
2825 state->chosen_exit = extend_info_dup(exit);
2827 ++circ->build_state->desired_path_len;
2828 onion_append_hop(&circ->cpath, exit);
2829 return 0;
2832 /** Take an open <b>circ</b>, and add a new hop at the end, based on
2833 * <b>info</b>. Set its state back to CIRCUIT_STATE_BUILDING, and then
2834 * send the next extend cell to begin connecting to that hop.
2837 circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit)
2839 int err_reason = 0;
2840 warn_if_last_router_excluded(circ, exit);
2841 circuit_append_new_exit(circ, exit);
2842 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
2843 if ((err_reason = circuit_send_next_onion_skin(circ))<0) {
2844 log_warn(LD_CIRC, "Couldn't extend circuit to new point '%s'.",
2845 exit->nickname);
2846 circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
2847 return -1;
2849 return 0;
2852 /** Return the number of routers in <b>routers</b> that are currently up
2853 * and available for building circuits through.
2855 static int
2856 count_acceptable_routers(smartlist_t *routers)
2858 int i, n;
2859 int num=0;
2860 routerinfo_t *r;
2862 n = smartlist_len(routers);
2863 for (i=0;i<n;i++) {
2864 r = smartlist_get(routers, i);
2865 // log_debug(LD_CIRC,
2866 // "Contemplating whether router %d (%s) is a new option.",
2867 // i, r->nickname);
2868 if (r->is_running == 0) {
2869 // log_debug(LD_CIRC,"Nope, the directory says %d is not running.",i);
2870 goto next_i_loop;
2872 if (r->is_valid == 0) {
2873 // log_debug(LD_CIRC,"Nope, the directory says %d is not valid.",i);
2874 goto next_i_loop;
2875 /* XXX This clause makes us count incorrectly: if AllowInvalidRouters
2876 * allows this node in some places, then we're getting an inaccurate
2877 * count. For now, be conservative and don't count it. But later we
2878 * should try to be smarter. */
2880 num++;
2881 // log_debug(LD_CIRC,"I like %d. num_acceptable_routers now %d.",i, num);
2882 next_i_loop:
2883 ; /* C requires an explicit statement after the label */
2886 return num;
2889 /** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
2890 * This function is used to extend cpath by another hop.
2892 void
2893 onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop)
2895 if (*head_ptr) {
2896 new_hop->next = (*head_ptr);
2897 new_hop->prev = (*head_ptr)->prev;
2898 (*head_ptr)->prev->next = new_hop;
2899 (*head_ptr)->prev = new_hop;
2900 } else {
2901 *head_ptr = new_hop;
2902 new_hop->prev = new_hop->next = new_hop;
2906 /** A helper function used by onion_extend_cpath(). Use <b>purpose</b>
2907 * and <b>state</b> and the cpath <b>head</b> (currently populated only
2908 * to length <b>cur_len</b> to decide a suitable middle hop for a
2909 * circuit. In particular, make sure we don't pick the exit node or its
2910 * family, and make sure we don't duplicate any previous nodes or their
2911 * families. */
2912 static routerinfo_t *
2913 choose_good_middle_server(uint8_t purpose,
2914 cpath_build_state_t *state,
2915 crypt_path_t *head,
2916 int cur_len)
2918 int i;
2919 routerinfo_t *r, *choice;
2920 crypt_path_t *cpath;
2921 smartlist_t *excluded;
2922 or_options_t *options = get_options();
2923 router_crn_flags_t flags = 0;
2924 tor_assert(_CIRCUIT_PURPOSE_MIN <= purpose &&
2925 purpose <= _CIRCUIT_PURPOSE_MAX);
2927 log_debug(LD_CIRC, "Contemplating intermediate hop: random choice.");
2928 excluded = smartlist_create();
2929 if ((r = build_state_get_exit_router(state))) {
2930 smartlist_add(excluded, r);
2931 routerlist_add_family(excluded, r);
2933 for (i = 0, cpath = head; i < cur_len; ++i, cpath=cpath->next) {
2934 if ((r = router_get_by_digest(cpath->extend_info->identity_digest))) {
2935 smartlist_add(excluded, r);
2936 routerlist_add_family(excluded, r);
2940 if (state->need_uptime)
2941 flags |= CRN_NEED_UPTIME;
2942 if (state->need_capacity)
2943 flags |= CRN_NEED_CAPACITY;
2944 if (options->_AllowInvalid & ALLOW_INVALID_MIDDLE)
2945 flags |= CRN_ALLOW_INVALID;
2946 choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
2947 smartlist_free(excluded);
2948 return choice;
2951 /** Pick a good entry server for the circuit to be built according to
2952 * <b>state</b>. Don't reuse a chosen exit (if any), don't use this
2953 * router (if we're an OR), and respect firewall settings; if we're
2954 * configured to use entry guards, return one.
2956 * If <b>state</b> is NULL, we're choosing a router to serve as an entry
2957 * guard, not for any particular circuit.
2959 static routerinfo_t *
2960 choose_good_entry_server(uint8_t purpose, cpath_build_state_t *state)
2962 routerinfo_t *r, *choice;
2963 smartlist_t *excluded;
2964 or_options_t *options = get_options();
2965 router_crn_flags_t flags = CRN_NEED_GUARD;
2967 if (state && options->UseEntryGuards &&
2968 (purpose != CIRCUIT_PURPOSE_TESTING || options->BridgeRelay)) {
2969 return choose_random_entry(state);
2972 excluded = smartlist_create();
2974 if (state && (r = build_state_get_exit_router(state))) {
2975 smartlist_add(excluded, r);
2976 routerlist_add_family(excluded, r);
2978 if (firewall_is_fascist_or()) {
2979 /*XXXX This could slow things down a lot; use a smarter implementation */
2980 /* exclude all ORs that listen on the wrong port, if anybody notices. */
2981 routerlist_t *rl = router_get_routerlist();
2982 int i;
2984 for (i=0; i < smartlist_len(rl->routers); i++) {
2985 r = smartlist_get(rl->routers, i);
2986 if (!fascist_firewall_allows_or(r))
2987 smartlist_add(excluded, r);
2990 /* and exclude current entry guards, if applicable */
2991 if (options->UseEntryGuards && entry_guards) {
2992 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
2994 if ((r = router_get_by_digest(entry->identity))) {
2995 smartlist_add(excluded, r);
2996 routerlist_add_family(excluded, r);
3001 if (state) {
3002 if (state->need_uptime)
3003 flags |= CRN_NEED_UPTIME;
3004 if (state->need_capacity)
3005 flags |= CRN_NEED_CAPACITY;
3007 if (options->_AllowInvalid & ALLOW_INVALID_ENTRY)
3008 flags |= CRN_ALLOW_INVALID;
3010 choice = router_choose_random_node(excluded, options->ExcludeNodes, flags);
3011 smartlist_free(excluded);
3012 return choice;
3015 /** Return the first non-open hop in cpath, or return NULL if all
3016 * hops are open. */
3017 static crypt_path_t *
3018 onion_next_hop_in_cpath(crypt_path_t *cpath)
3020 crypt_path_t *hop = cpath;
3021 do {
3022 if (hop->state != CPATH_STATE_OPEN)
3023 return hop;
3024 hop = hop->next;
3025 } while (hop != cpath);
3026 return NULL;
3029 /** Choose a suitable next hop in the cpath <b>head_ptr</b>,
3030 * based on <b>state</b>. Append the hop info to head_ptr.
3032 static int
3033 onion_extend_cpath(origin_circuit_t *circ)
3035 uint8_t purpose = circ->_base.purpose;
3036 cpath_build_state_t *state = circ->build_state;
3037 int cur_len = circuit_get_cpath_len(circ);
3038 extend_info_t *info = NULL;
3040 if (cur_len >= state->desired_path_len) {
3041 log_debug(LD_CIRC, "Path is complete: %d steps long",
3042 state->desired_path_len);
3043 return 1;
3046 log_debug(LD_CIRC, "Path is %d long; we want %d", cur_len,
3047 state->desired_path_len);
3049 if (cur_len == state->desired_path_len - 1) { /* Picking last node */
3050 info = extend_info_dup(state->chosen_exit);
3051 } else if (cur_len == 0) { /* picking first node */
3052 routerinfo_t *r = choose_good_entry_server(purpose, state);
3053 if (r)
3054 info = extend_info_from_router(r);
3055 } else {
3056 routerinfo_t *r =
3057 choose_good_middle_server(purpose, state, circ->cpath, cur_len);
3058 if (r)
3059 info = extend_info_from_router(r);
3062 if (!info) {
3063 log_warn(LD_CIRC,"Failed to find node for hop %d of our path. Discarding "
3064 "this circuit.", cur_len);
3065 return -1;
3068 log_debug(LD_CIRC,"Chose router %s for hop %d (exit is %s)",
3069 info->nickname, cur_len+1, build_state_get_exit_nickname(state));
3071 onion_append_hop(&circ->cpath, info);
3072 extend_info_free(info);
3073 return 0;
3076 /** Create a new hop, annotate it with information about its
3077 * corresponding router <b>choice</b>, and append it to the
3078 * end of the cpath <b>head_ptr</b>. */
3079 static int
3080 onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
3082 crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
3084 /* link hop into the cpath, at the end. */
3085 onion_append_to_cpath(head_ptr, hop);
3087 hop->magic = CRYPT_PATH_MAGIC;
3088 hop->state = CPATH_STATE_CLOSED;
3090 hop->extend_info = extend_info_dup(choice);
3092 hop->package_window = circuit_initial_package_window();
3093 hop->deliver_window = CIRCWINDOW_START;
3095 return 0;
3098 /** Allocate a new extend_info object based on the various arguments. */
3099 extend_info_t *
3100 extend_info_alloc(const char *nickname, const char *digest,
3101 crypto_pk_env_t *onion_key,
3102 const tor_addr_t *addr, uint16_t port)
3104 extend_info_t *info = tor_malloc_zero(sizeof(extend_info_t));
3105 memcpy(info->identity_digest, digest, DIGEST_LEN);
3106 if (nickname)
3107 strlcpy(info->nickname, nickname, sizeof(info->nickname));
3108 if (onion_key)
3109 info->onion_key = crypto_pk_dup_key(onion_key);
3110 tor_addr_copy(&info->addr, addr);
3111 info->port = port;
3112 return info;
3115 /** Allocate and return a new extend_info_t that can be used to build a
3116 * circuit to or through the router <b>r</b>. */
3117 extend_info_t *
3118 extend_info_from_router(routerinfo_t *r)
3120 tor_addr_t addr;
3121 tor_assert(r);
3122 tor_addr_from_ipv4h(&addr, r->addr);
3123 return extend_info_alloc(r->nickname, r->cache_info.identity_digest,
3124 r->onion_pkey, &addr, r->or_port);
3127 /** Release storage held by an extend_info_t struct. */
3128 void
3129 extend_info_free(extend_info_t *info)
3131 if (!info)
3132 return;
3133 crypto_free_pk_env(info->onion_key);
3134 tor_free(info);
3137 /** Allocate and return a new extend_info_t with the same contents as
3138 * <b>info</b>. */
3139 extend_info_t *
3140 extend_info_dup(extend_info_t *info)
3142 extend_info_t *newinfo;
3143 tor_assert(info);
3144 newinfo = tor_malloc(sizeof(extend_info_t));
3145 memcpy(newinfo, info, sizeof(extend_info_t));
3146 if (info->onion_key)
3147 newinfo->onion_key = crypto_pk_dup_key(info->onion_key);
3148 else
3149 newinfo->onion_key = NULL;
3150 return newinfo;
3153 /** Return the routerinfo_t for the chosen exit router in <b>state</b>.
3154 * If there is no chosen exit, or if we don't know the routerinfo_t for
3155 * the chosen exit, return NULL.
3157 routerinfo_t *
3158 build_state_get_exit_router(cpath_build_state_t *state)
3160 if (!state || !state->chosen_exit)
3161 return NULL;
3162 return router_get_by_digest(state->chosen_exit->identity_digest);
3165 /** Return the nickname for the chosen exit router in <b>state</b>. If
3166 * there is no chosen exit, or if we don't know the routerinfo_t for the
3167 * chosen exit, return NULL.
3169 const char *
3170 build_state_get_exit_nickname(cpath_build_state_t *state)
3172 if (!state || !state->chosen_exit)
3173 return NULL;
3174 return state->chosen_exit->nickname;
3177 /** Check whether the entry guard <b>e</b> is usable, given the directory
3178 * authorities' opinion about the router (stored in <b>ri</b>) and the user's
3179 * configuration (in <b>options</b>). Set <b>e</b>-&gt;bad_since
3180 * accordingly. Return true iff the entry guard's status changes.
3182 * If it's not usable, set *<b>reason</b> to a static string explaining why.
3184 /*XXXX take a routerstatus, not a routerinfo. */
3185 static int
3186 entry_guard_set_status(entry_guard_t *e, routerinfo_t *ri,
3187 time_t now, or_options_t *options, const char **reason)
3189 char buf[HEX_DIGEST_LEN+1];
3190 int changed = 0;
3192 tor_assert(options);
3194 *reason = NULL;
3196 /* Do we want to mark this guard as bad? */
3197 if (!ri)
3198 *reason = "unlisted";
3199 else if (!ri->is_running)
3200 *reason = "down";
3201 else if (options->UseBridges && ri->purpose != ROUTER_PURPOSE_BRIDGE)
3202 *reason = "not a bridge";
3203 else if (!options->UseBridges && !ri->is_possible_guard &&
3204 !routerset_contains_router(options->EntryNodes,ri))
3205 *reason = "not recommended as a guard";
3206 else if (routerset_contains_router(options->ExcludeNodes, ri))
3207 *reason = "excluded";
3209 if (*reason && ! e->bad_since) {
3210 /* Router is newly bad. */
3211 base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
3212 log_info(LD_CIRC, "Entry guard %s (%s) is %s: marking as unusable.",
3213 e->nickname, buf, *reason);
3215 e->bad_since = now;
3216 control_event_guard(e->nickname, e->identity, "BAD");
3217 changed = 1;
3218 } else if (!*reason && e->bad_since) {
3219 /* There's nothing wrong with the router any more. */
3220 base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
3221 log_info(LD_CIRC, "Entry guard %s (%s) is no longer unusable: "
3222 "marking as ok.", e->nickname, buf);
3224 e->bad_since = 0;
3225 control_event_guard(e->nickname, e->identity, "GOOD");
3226 changed = 1;
3228 return changed;
3231 /** Return true iff enough time has passed since we last tried to connect
3232 * to the unreachable guard <b>e</b> that we're willing to try again. */
3233 static int
3234 entry_is_time_to_retry(entry_guard_t *e, time_t now)
3236 long diff;
3237 if (e->last_attempted < e->unreachable_since)
3238 return 1;
3239 diff = now - e->unreachable_since;
3240 if (diff < 6*60*60)
3241 return now > (e->last_attempted + 60*60);
3242 else if (diff < 3*24*60*60)
3243 return now > (e->last_attempted + 4*60*60);
3244 else if (diff < 7*24*60*60)
3245 return now > (e->last_attempted + 18*60*60);
3246 else
3247 return now > (e->last_attempted + 36*60*60);
3250 /** Return the router corresponding to <b>e</b>, if <b>e</b> is
3251 * working well enough that we are willing to use it as an entry
3252 * right now. (Else return NULL.) In particular, it must be
3253 * - Listed as either up or never yet contacted;
3254 * - Present in the routerlist;
3255 * - Listed as 'stable' or 'fast' by the current dirserver consensus,
3256 * if demanded by <b>need_uptime</b> or <b>need_capacity</b>
3257 * (unless it's a configured EntryNode);
3258 * - Allowed by our current ReachableORAddresses config option; and
3259 * - Currently thought to be reachable by us (unless <b>assume_reachable</b>
3260 * is true).
3262 * If the answer is no, set *<b>msg</b> to an explanation of why.
3264 static INLINE routerinfo_t *
3265 entry_is_live(entry_guard_t *e, int need_uptime, int need_capacity,
3266 int assume_reachable, const char **msg)
3268 routerinfo_t *r;
3269 or_options_t *options = get_options();
3270 tor_assert(msg);
3272 if (e->bad_since) {
3273 *msg = "bad";
3274 return NULL;
3276 /* no good if it's unreachable, unless assume_unreachable or can_retry. */
3277 if (!assume_reachable && !e->can_retry &&
3278 e->unreachable_since && !entry_is_time_to_retry(e, time(NULL))) {
3279 *msg = "unreachable";
3280 return NULL;
3282 r = router_get_by_digest(e->identity);
3283 if (!r) {
3284 *msg = "no descriptor";
3285 return NULL;
3287 if (get_options()->UseBridges && r->purpose != ROUTER_PURPOSE_BRIDGE) {
3288 *msg = "not a bridge";
3289 return NULL;
3291 if (!get_options()->UseBridges && r->purpose != ROUTER_PURPOSE_GENERAL) {
3292 *msg = "not general-purpose";
3293 return NULL;
3295 if (options->EntryNodes &&
3296 routerset_contains_router(options->EntryNodes, r)) {
3297 /* they asked for it, they get it */
3298 need_uptime = need_capacity = 0;
3300 if (router_is_unreliable(r, need_uptime, need_capacity, 0)) {
3301 *msg = "not fast/stable";
3302 return NULL;
3304 if (!fascist_firewall_allows_or(r)) {
3305 *msg = "unreachable by config";
3306 return NULL;
3308 return r;
3311 /** Return the number of entry guards that we think are usable. */
3312 static int
3313 num_live_entry_guards(void)
3315 int n = 0;
3316 const char *msg;
3317 if (! entry_guards)
3318 return 0;
3319 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3321 if (entry_is_live(entry, 0, 1, 0, &msg))
3322 ++n;
3324 return n;
3327 /** If <b>digest</b> matches the identity of any node in the
3328 * entry_guards list, return that node. Else return NULL. */
3329 static INLINE entry_guard_t *
3330 is_an_entry_guard(const char *digest)
3332 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3333 if (!memcmp(digest, entry->identity, DIGEST_LEN))
3334 return entry;
3336 return NULL;
3339 /** Dump a description of our list of entry guards to the log at level
3340 * <b>severity</b>. */
3341 static void
3342 log_entry_guards(int severity)
3344 smartlist_t *elements = smartlist_create();
3345 char *s;
3347 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
3349 const char *msg = NULL;
3350 char *cp;
3351 if (entry_is_live(e, 0, 1, 0, &msg))
3352 tor_asprintf(&cp, "%s (up %s)",
3353 e->nickname,
3354 e->made_contact ? "made-contact" : "never-contacted");
3355 else
3356 tor_asprintf(&cp, "%s (%s, %s)",
3357 e->nickname, msg,
3358 e->made_contact ? "made-contact" : "never-contacted");
3359 smartlist_add(elements, cp);
3362 s = smartlist_join_strings(elements, ",", 0, NULL);
3363 SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
3364 smartlist_free(elements);
3365 log_fn(severity,LD_CIRC,"%s",s);
3366 tor_free(s);
3369 /** Called when one or more guards that we would previously have used for some
3370 * purpose are no longer in use because a higher-priority guard has become
3371 * usable again. */
3372 static void
3373 control_event_guard_deferred(void)
3375 /* XXXX We don't actually have a good way to figure out _how many_ entries
3376 * are live for some purpose. We need an entry_is_even_slightly_live()
3377 * function for this to work right. NumEntryGuards isn't reliable: if we
3378 * need guards with weird properties, we can have more than that number
3379 * live.
3381 #if 0
3382 int n = 0;
3383 const char *msg;
3384 or_options_t *options = get_options();
3385 if (!entry_guards)
3386 return;
3387 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3389 if (entry_is_live(entry, 0, 1, 0, &msg)) {
3390 if (n++ == options->NumEntryGuards) {
3391 control_event_guard(entry->nickname, entry->identity, "DEFERRED");
3392 return;
3396 #endif
3399 /** Add a new (preferably stable and fast) router to our
3400 * entry_guards list. Return a pointer to the router if we succeed,
3401 * or NULL if we can't find any more suitable entries.
3403 * If <b>chosen</b> is defined, use that one, and if it's not
3404 * already in our entry_guards list, put it at the *beginning*.
3405 * Else, put the one we pick at the end of the list. */
3406 static routerinfo_t *
3407 add_an_entry_guard(routerinfo_t *chosen, int reset_status)
3409 routerinfo_t *router;
3410 entry_guard_t *entry;
3412 if (chosen) {
3413 router = chosen;
3414 entry = is_an_entry_guard(router->cache_info.identity_digest);
3415 if (entry) {
3416 if (reset_status) {
3417 entry->bad_since = 0;
3418 entry->can_retry = 1;
3420 return NULL;
3422 } else {
3423 router = choose_good_entry_server(CIRCUIT_PURPOSE_C_GENERAL, NULL);
3424 if (!router)
3425 return NULL;
3427 entry = tor_malloc_zero(sizeof(entry_guard_t));
3428 log_info(LD_CIRC, "Chose '%s' as new entry guard.", router->nickname);
3429 strlcpy(entry->nickname, router->nickname, sizeof(entry->nickname));
3430 memcpy(entry->identity, router->cache_info.identity_digest, DIGEST_LEN);
3431 /* Choose expiry time smudged over the past month. The goal here
3432 * is to a) spread out when Tor clients rotate their guards, so they
3433 * don't all select them on the same day, and b) avoid leaving a
3434 * precise timestamp in the state file about when we first picked
3435 * this guard. For details, see the Jan 2010 or-dev thread. */
3436 entry->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
3437 entry->chosen_by_version = tor_strdup(VERSION);
3438 if (chosen) /* prepend */
3439 smartlist_insert(entry_guards, 0, entry);
3440 else /* append */
3441 smartlist_add(entry_guards, entry);
3442 control_event_guard(entry->nickname, entry->identity, "NEW");
3443 control_event_guard_deferred();
3444 log_entry_guards(LOG_INFO);
3445 return router;
3448 /** If the use of entry guards is configured, choose more entry guards
3449 * until we have enough in the list. */
3450 static void
3451 pick_entry_guards(void)
3453 or_options_t *options = get_options();
3454 int changed = 0;
3456 tor_assert(entry_guards);
3458 while (num_live_entry_guards() < options->NumEntryGuards) {
3459 if (!add_an_entry_guard(NULL, 0))
3460 break;
3461 changed = 1;
3463 if (changed)
3464 entry_guards_changed();
3467 /** How long (in seconds) do we allow an entry guard to be nonfunctional,
3468 * unlisted, excluded, or otherwise nonusable before we give up on it? */
3469 #define ENTRY_GUARD_REMOVE_AFTER (30*24*60*60)
3471 /** Release all storage held by <b>e</b>. */
3472 static void
3473 entry_guard_free(entry_guard_t *e)
3475 if (!e)
3476 return;
3477 tor_free(e->chosen_by_version);
3478 tor_free(e);
3481 /** Remove any entry guard which was selected by an unknown version of Tor,
3482 * or which was selected by a version of Tor that's known to select
3483 * entry guards badly. */
3484 static int
3485 remove_obsolete_entry_guards(void)
3487 int changed = 0, i;
3488 time_t now = time(NULL);
3490 for (i = 0; i < smartlist_len(entry_guards); ++i) {
3491 entry_guard_t *entry = smartlist_get(entry_guards, i);
3492 const char *ver = entry->chosen_by_version;
3493 const char *msg = NULL;
3494 tor_version_t v;
3495 int version_is_bad = 0, date_is_bad = 0;
3496 if (!ver) {
3497 msg = "does not say what version of Tor it was selected by";
3498 version_is_bad = 1;
3499 } else if (tor_version_parse(ver, &v)) {
3500 msg = "does not seem to be from any recognized version of Tor";
3501 version_is_bad = 1;
3502 } else {
3503 size_t len = strlen(ver)+5;
3504 char *tor_ver = tor_malloc(len);
3505 tor_snprintf(tor_ver, len, "Tor %s", ver);
3506 if ((tor_version_as_new_as(tor_ver, "0.1.0.10-alpha") &&
3507 !tor_version_as_new_as(tor_ver, "0.1.2.16-dev")) ||
3508 (tor_version_as_new_as(tor_ver, "0.2.0.0-alpha") &&
3509 !tor_version_as_new_as(tor_ver, "0.2.0.6-alpha")) ||
3510 /* above are bug 440; below are bug 1217 */
3511 (tor_version_as_new_as(tor_ver, "0.2.1.3-alpha") &&
3512 !tor_version_as_new_as(tor_ver, "0.2.1.23")) ||
3513 (tor_version_as_new_as(tor_ver, "0.2.2.0-alpha") &&
3514 !tor_version_as_new_as(tor_ver, "0.2.2.7-alpha"))) {
3515 msg = "was selected without regard for guard bandwidth";
3516 version_is_bad = 1;
3518 tor_free(tor_ver);
3520 if (!version_is_bad && entry->chosen_on_date + 3600*24*60 < now) {
3521 /* It's been 2 months since the date listed in our state file. */
3522 msg = "was selected several months ago";
3523 date_is_bad = 1;
3526 if (version_is_bad || date_is_bad) { /* we need to drop it */
3527 char dbuf[HEX_DIGEST_LEN+1];
3528 tor_assert(msg);
3529 base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
3530 log_fn(version_is_bad ? LOG_NOTICE : LOG_INFO, LD_CIRC,
3531 "Entry guard '%s' (%s) %s. (Version=%s.) Replacing it.",
3532 entry->nickname, dbuf, msg, ver?escaped(ver):"none");
3533 control_event_guard(entry->nickname, entry->identity, "DROPPED");
3534 entry_guard_free(entry);
3535 smartlist_del_keeporder(entry_guards, i--);
3536 log_entry_guards(LOG_INFO);
3537 changed = 1;
3541 return changed ? 1 : 0;
3544 /** Remove all entry guards that have been down or unlisted for so
3545 * long that we don't think they'll come up again. Return 1 if we
3546 * removed any, or 0 if we did nothing. */
3547 static int
3548 remove_dead_entry_guards(void)
3550 char dbuf[HEX_DIGEST_LEN+1];
3551 char tbuf[ISO_TIME_LEN+1];
3552 time_t now = time(NULL);
3553 int i;
3554 int changed = 0;
3556 for (i = 0; i < smartlist_len(entry_guards); ) {
3557 entry_guard_t *entry = smartlist_get(entry_guards, i);
3558 if (entry->bad_since &&
3559 entry->bad_since + ENTRY_GUARD_REMOVE_AFTER < now) {
3561 base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
3562 format_local_iso_time(tbuf, entry->bad_since);
3563 log_info(LD_CIRC, "Entry guard '%s' (%s) has been down or unlisted "
3564 "since %s local time; removing.",
3565 entry->nickname, dbuf, tbuf);
3566 control_event_guard(entry->nickname, entry->identity, "DROPPED");
3567 entry_guard_free(entry);
3568 smartlist_del_keeporder(entry_guards, i);
3569 log_entry_guards(LOG_INFO);
3570 changed = 1;
3571 } else
3572 ++i;
3574 return changed ? 1 : 0;
3577 /** A new directory or router-status has arrived; update the down/listed
3578 * status of the entry guards.
3580 * An entry is 'down' if the directory lists it as nonrunning.
3581 * An entry is 'unlisted' if the directory doesn't include it.
3583 * Don't call this on startup; only on a fresh download. Otherwise we'll
3584 * think that things are unlisted.
3586 void
3587 entry_guards_compute_status(void)
3589 time_t now;
3590 int changed = 0;
3591 int severity = LOG_DEBUG;
3592 or_options_t *options;
3593 digestmap_t *reasons;
3595 if (! entry_guards)
3596 return;
3598 options = get_options();
3599 if (options->EntryNodes) /* reshuffle the entry guard list if needed */
3600 entry_nodes_should_be_added();
3602 now = time(NULL);
3604 reasons = digestmap_new();
3605 SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, entry)
3607 routerinfo_t *r = router_get_by_digest(entry->identity);
3608 const char *reason = NULL;
3609 if (entry_guard_set_status(entry, r, now, options, &reason))
3610 changed = 1;
3612 if (entry->bad_since)
3613 tor_assert(reason);
3614 if (reason)
3615 digestmap_set(reasons, entry->identity, (char*)reason);
3617 SMARTLIST_FOREACH_END(entry);
3619 if (remove_dead_entry_guards())
3620 changed = 1;
3622 severity = changed ? LOG_DEBUG : LOG_INFO;
3624 if (changed) {
3625 SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, entry) {
3626 const char *reason = digestmap_get(reasons, entry->identity);
3627 const char *live_msg = "";
3628 routerinfo_t *r = entry_is_live(entry, 0, 1, 0, &live_msg);
3629 log_info(LD_CIRC, "Summary: Entry '%s' is %s, %s%s%s, and %s%s.",
3630 entry->nickname,
3631 entry->unreachable_since ? "unreachable" : "reachable",
3632 entry->bad_since ? "unusable" : "usable",
3633 reason ? ", ": "",
3634 reason ? reason : "",
3635 r ? "live" : "not live / ",
3636 r ? "" : live_msg);
3637 } SMARTLIST_FOREACH_END(entry);
3638 log_info(LD_CIRC, " (%d/%d entry guards are usable/new)",
3639 num_live_entry_guards(), smartlist_len(entry_guards));
3640 log_entry_guards(LOG_INFO);
3641 entry_guards_changed();
3644 digestmap_free(reasons, NULL);
3647 /** Called when a connection to an OR with the identity digest <b>digest</b>
3648 * is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
3649 * If the OR is an entry, change that entry's up/down status.
3650 * Return 0 normally, or -1 if we want to tear down the new connection.
3652 * If <b>mark_relay_status</b>, also call router_set_status() on this
3653 * relay.
3655 * XXX022 change succeeded and mark_relay_status into 'int flags'.
3658 entry_guard_register_connect_status(const char *digest, int succeeded,
3659 int mark_relay_status, time_t now)
3661 int changed = 0;
3662 int refuse_conn = 0;
3663 int first_contact = 0;
3664 entry_guard_t *entry = NULL;
3665 int idx = -1;
3666 char buf[HEX_DIGEST_LEN+1];
3668 if (! entry_guards)
3669 return 0;
3671 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
3673 if (!memcmp(e->identity, digest, DIGEST_LEN)) {
3674 entry = e;
3675 idx = e_sl_idx;
3676 break;
3680 if (!entry)
3681 return 0;
3683 base16_encode(buf, sizeof(buf), entry->identity, DIGEST_LEN);
3685 if (succeeded) {
3686 if (entry->unreachable_since) {
3687 log_info(LD_CIRC, "Entry guard '%s' (%s) is now reachable again. Good.",
3688 entry->nickname, buf);
3689 entry->can_retry = 0;
3690 entry->unreachable_since = 0;
3691 entry->last_attempted = now;
3692 control_event_guard(entry->nickname, entry->identity, "UP");
3693 changed = 1;
3695 if (!entry->made_contact) {
3696 entry->made_contact = 1;
3697 first_contact = changed = 1;
3699 } else { /* ! succeeded */
3700 if (!entry->made_contact) {
3701 /* We've never connected to this one. */
3702 log_info(LD_CIRC,
3703 "Connection to never-contacted entry guard '%s' (%s) failed. "
3704 "Removing from the list. %d/%d entry guards usable/new.",
3705 entry->nickname, buf,
3706 num_live_entry_guards()-1, smartlist_len(entry_guards)-1);
3707 control_event_guard(entry->nickname, entry->identity, "DROPPED");
3708 entry_guard_free(entry);
3709 smartlist_del_keeporder(entry_guards, idx);
3710 log_entry_guards(LOG_INFO);
3711 changed = 1;
3712 } else if (!entry->unreachable_since) {
3713 log_info(LD_CIRC, "Unable to connect to entry guard '%s' (%s). "
3714 "Marking as unreachable.", entry->nickname, buf);
3715 entry->unreachable_since = entry->last_attempted = now;
3716 control_event_guard(entry->nickname, entry->identity, "DOWN");
3717 changed = 1;
3718 entry->can_retry = 0; /* We gave it an early chance; no good. */
3719 } else {
3720 char tbuf[ISO_TIME_LEN+1];
3721 format_iso_time(tbuf, entry->unreachable_since);
3722 log_debug(LD_CIRC, "Failed to connect to unreachable entry guard "
3723 "'%s' (%s). It has been unreachable since %s.",
3724 entry->nickname, buf, tbuf);
3725 entry->last_attempted = now;
3726 entry->can_retry = 0; /* We gave it an early chance; no good. */
3730 /* if the caller asked us to, also update the is_running flags for this
3731 * relay */
3732 if (mark_relay_status)
3733 router_set_status(digest, succeeded);
3735 if (first_contact) {
3736 /* We've just added a new long-term entry guard. Perhaps the network just
3737 * came back? We should give our earlier entries another try too,
3738 * and close this connection so we don't use it before we've given
3739 * the others a shot. */
3740 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e, {
3741 if (e == entry)
3742 break;
3743 if (e->made_contact) {
3744 const char *msg;
3745 routerinfo_t *r = entry_is_live(e, 0, 1, 1, &msg);
3746 if (r && e->unreachable_since) {
3747 refuse_conn = 1;
3748 e->can_retry = 1;
3752 if (refuse_conn) {
3753 log_info(LD_CIRC,
3754 "Connected to new entry guard '%s' (%s). Marking earlier "
3755 "entry guards up. %d/%d entry guards usable/new.",
3756 entry->nickname, buf,
3757 num_live_entry_guards(), smartlist_len(entry_guards));
3758 log_entry_guards(LOG_INFO);
3759 changed = 1;
3763 if (changed)
3764 entry_guards_changed();
3765 return refuse_conn ? -1 : 0;
3768 /** When we try to choose an entry guard, should we parse and add
3769 * config's EntryNodes first? */
3770 static int should_add_entry_nodes = 0;
3772 /** Called when the value of EntryNodes changes in our configuration. */
3773 void
3774 entry_nodes_should_be_added(void)
3776 log_info(LD_CIRC, "EntryNodes config option set. Putting configured "
3777 "relays at the front of the entry guard list.");
3778 should_add_entry_nodes = 1;
3781 /** Add all nodes in EntryNodes that aren't currently guard nodes to the list
3782 * of guard nodes, at the front. */
3783 static void
3784 entry_guards_prepend_from_config(void)
3786 or_options_t *options = get_options();
3787 smartlist_t *entry_routers, *entry_fps;
3788 smartlist_t *old_entry_guards_on_list, *old_entry_guards_not_on_list;
3789 tor_assert(entry_guards);
3791 should_add_entry_nodes = 0;
3793 if (!options->EntryNodes) {
3794 /* It's possible that a controller set EntryNodes, thus making
3795 * should_add_entry_nodes set, then cleared it again, all before the
3796 * call to choose_random_entry() that triggered us. If so, just return.
3798 return;
3802 char *string = routerset_to_string(options->EntryNodes);
3803 log_info(LD_CIRC,"Adding configured EntryNodes '%s'.", string);
3804 tor_free(string);
3807 entry_routers = smartlist_create();
3808 entry_fps = smartlist_create();
3809 old_entry_guards_on_list = smartlist_create();
3810 old_entry_guards_not_on_list = smartlist_create();
3812 /* Split entry guards into those on the list and those not. */
3814 /* XXXX022 Now that we allow countries and IP ranges in EntryNodes, this is
3815 * potentially an enormous list. For now, we disable such values for
3816 * EntryNodes in options_validate(); really, this wants a better solution.
3817 * Perhaps we should do this calculation once whenever the list of routers
3818 * changes or the entrynodes setting changes.
3820 routerset_get_all_routers(entry_routers, options->EntryNodes, 0);
3821 SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri,
3822 smartlist_add(entry_fps,ri->cache_info.identity_digest));
3823 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e, {
3824 if (smartlist_digest_isin(entry_fps, e->identity))
3825 smartlist_add(old_entry_guards_on_list, e);
3826 else
3827 smartlist_add(old_entry_guards_not_on_list, e);
3830 /* Remove all currently configured entry guards from entry_routers. */
3831 SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri, {
3832 if (is_an_entry_guard(ri->cache_info.identity_digest)) {
3833 SMARTLIST_DEL_CURRENT(entry_routers, ri);
3837 /* Now build the new entry_guards list. */
3838 smartlist_clear(entry_guards);
3839 /* First, the previously configured guards that are in EntryNodes. */
3840 smartlist_add_all(entry_guards, old_entry_guards_on_list);
3841 /* Next, the rest of EntryNodes */
3842 SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri, {
3843 add_an_entry_guard(ri, 0);
3845 /* Finally, the remaining previously configured guards that are not in
3846 * EntryNodes, unless we're strict in which case we drop them */
3847 if (options->StrictNodes) {
3848 SMARTLIST_FOREACH(old_entry_guards_not_on_list, entry_guard_t *, e,
3849 entry_guard_free(e));
3850 } else {
3851 smartlist_add_all(entry_guards, old_entry_guards_not_on_list);
3854 smartlist_free(entry_routers);
3855 smartlist_free(entry_fps);
3856 smartlist_free(old_entry_guards_on_list);
3857 smartlist_free(old_entry_guards_not_on_list);
3858 entry_guards_changed();
3861 /** Return 0 if we're fine adding arbitrary routers out of the
3862 * directory to our entry guard list, or return 1 if we have a
3863 * list already and we'd prefer to stick to it.
3866 entry_list_is_constrained(or_options_t *options)
3868 if (options->EntryNodes)
3869 return 1;
3870 if (options->UseBridges)
3871 return 1;
3872 return 0;
3875 /* Are we dead set against changing our entry guard list, or would we
3876 * change it if it means keeping Tor usable? */
3877 static int
3878 entry_list_is_totally_static(or_options_t *options)
3880 if (options->EntryNodes && options->StrictNodes)
3881 return 1;
3882 if (options->UseBridges)
3883 return 1;
3884 return 0;
3887 /** Pick a live (up and listed) entry guard from entry_guards. If
3888 * <b>state</b> is non-NULL, this is for a specific circuit --
3889 * make sure not to pick this circuit's exit or any node in the
3890 * exit's family. If <b>state</b> is NULL, we're looking for a random
3891 * guard (likely a bridge). */
3892 routerinfo_t *
3893 choose_random_entry(cpath_build_state_t *state)
3895 or_options_t *options = get_options();
3896 smartlist_t *live_entry_guards = smartlist_create();
3897 smartlist_t *exit_family = smartlist_create();
3898 routerinfo_t *chosen_exit = state?build_state_get_exit_router(state) : NULL;
3899 routerinfo_t *r = NULL;
3900 int need_uptime = state ? state->need_uptime : 0;
3901 int need_capacity = state ? state->need_capacity : 0;
3902 int preferred_min, consider_exit_family = 0;
3904 if (chosen_exit) {
3905 smartlist_add(exit_family, chosen_exit);
3906 routerlist_add_family(exit_family, chosen_exit);
3907 consider_exit_family = 1;
3910 if (!entry_guards)
3911 entry_guards = smartlist_create();
3913 if (should_add_entry_nodes)
3914 entry_guards_prepend_from_config();
3916 if (!entry_list_is_constrained(options) &&
3917 smartlist_len(entry_guards) < options->NumEntryGuards)
3918 pick_entry_guards();
3920 retry:
3921 smartlist_clear(live_entry_guards);
3922 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
3924 const char *msg;
3925 r = entry_is_live(entry, need_uptime, need_capacity, 0, &msg);
3926 if (!r)
3927 continue; /* down, no point */
3928 if (consider_exit_family && smartlist_isin(exit_family, r))
3929 continue; /* avoid relays that are family members of our exit */
3930 if (options->EntryNodes &&
3931 !routerset_contains_router(options->EntryNodes, r)) {
3932 /* We've come to the end of our preferred entry nodes. */
3933 if (smartlist_len(live_entry_guards))
3934 goto choose_and_finish; /* only choose from the ones we like */
3935 if (options->StrictNodes) {
3936 /* in theory this case should never happen, since
3937 * entry_guards_prepend_from_config() drops unwanted relays */
3938 tor_fragile_assert();
3939 } else {
3940 log_info(LD_CIRC,
3941 "No relays from EntryNodes available. Using others.");
3944 smartlist_add(live_entry_guards, r);
3945 if (!entry->made_contact) {
3946 /* Always start with the first not-yet-contacted entry
3947 * guard. Otherwise we might add several new ones, pick
3948 * the second new one, and now we've expanded our entry
3949 * guard list without needing to. */
3950 goto choose_and_finish;
3952 if (smartlist_len(live_entry_guards) >= options->NumEntryGuards)
3953 break; /* we have enough */
3956 if (entry_list_is_constrained(options)) {
3957 /* If we prefer the entry nodes we've got, and we have at least
3958 * one choice, that's great. Use it. */
3959 preferred_min = 1;
3960 } else {
3961 /* Try to have at least 2 choices available. This way we don't
3962 * get stuck with a single live-but-crummy entry and just keep
3963 * using him.
3964 * (We might get 2 live-but-crummy entry guards, but so be it.) */
3965 preferred_min = 2;
3968 if (smartlist_len(live_entry_guards) < preferred_min) {
3969 if (!entry_list_is_totally_static(options)) {
3970 /* still no? try adding a new entry then */
3971 /* XXX if guard doesn't imply fast and stable, then we need
3972 * to tell add_an_entry_guard below what we want, or it might
3973 * be a long time til we get it. -RD */
3974 r = add_an_entry_guard(NULL, 0);
3975 if (r) {
3976 entry_guards_changed();
3977 /* XXX we start over here in case the new node we added shares
3978 * a family with our exit node. There's a chance that we'll just
3979 * load up on entry guards here, if the network we're using is
3980 * one big family. Perhaps we should teach add_an_entry_guard()
3981 * to understand nodes-to-avoid-if-possible? -RD */
3982 goto retry;
3985 if (!r && need_uptime) {
3986 need_uptime = 0; /* try without that requirement */
3987 goto retry;
3989 if (!r && need_capacity) {
3990 /* still no? last attempt, try without requiring capacity */
3991 need_capacity = 0;
3992 goto retry;
3994 if (!r && entry_list_is_constrained(options) && consider_exit_family) {
3995 /* still no? if we're using bridges or have strictentrynodes
3996 * set, and our chosen exit is in the same family as all our
3997 * bridges/entry guards, then be flexible about families. */
3998 consider_exit_family = 0;
3999 goto retry;
4001 /* live_entry_guards may be empty below. Oh well, we tried. */
4004 choose_and_finish:
4005 if (entry_list_is_constrained(options)) {
4006 /* We need to weight by bandwidth, because our bridges or entryguards
4007 * were not already selected proportional to their bandwidth. */
4008 r = routerlist_sl_choose_by_bandwidth(live_entry_guards, WEIGHT_FOR_GUARD);
4009 } else {
4010 /* We choose uniformly at random here, because choose_good_entry_server()
4011 * already weights its choices by bandwidth, so we don't want to
4012 * *double*-weight our guard selection. */
4013 r = smartlist_choose(live_entry_guards);
4015 smartlist_free(live_entry_guards);
4016 smartlist_free(exit_family);
4017 return r;
4020 /** Parse <b>state</b> and learn about the entry guards it describes.
4021 * If <b>set</b> is true, and there are no errors, replace the global
4022 * entry_list with what we find.
4023 * On success, return 0. On failure, alloc into *<b>msg</b> a string
4024 * describing the error, and return -1.
4027 entry_guards_parse_state(or_state_t *state, int set, char **msg)
4029 entry_guard_t *node = NULL;
4030 smartlist_t *new_entry_guards = smartlist_create();
4031 config_line_t *line;
4032 time_t now = time(NULL);
4033 const char *state_version = state->TorVersion;
4034 digestmap_t *added_by = digestmap_new();
4036 *msg = NULL;
4037 for (line = state->EntryGuards; line; line = line->next) {
4038 if (!strcasecmp(line->key, "EntryGuard")) {
4039 smartlist_t *args = smartlist_create();
4040 node = tor_malloc_zero(sizeof(entry_guard_t));
4041 /* all entry guards on disk have been contacted */
4042 node->made_contact = 1;
4043 smartlist_add(new_entry_guards, node);
4044 smartlist_split_string(args, line->value, " ",
4045 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
4046 if (smartlist_len(args)<2) {
4047 *msg = tor_strdup("Unable to parse entry nodes: "
4048 "Too few arguments to EntryGuard");
4049 } else if (!is_legal_nickname(smartlist_get(args,0))) {
4050 *msg = tor_strdup("Unable to parse entry nodes: "
4051 "Bad nickname for EntryGuard");
4052 } else {
4053 strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1);
4054 if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1),
4055 strlen(smartlist_get(args,1)))<0) {
4056 *msg = tor_strdup("Unable to parse entry nodes: "
4057 "Bad hex digest for EntryGuard");
4060 SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
4061 smartlist_free(args);
4062 if (*msg)
4063 break;
4064 } else if (!strcasecmp(line->key, "EntryGuardDownSince") ||
4065 !strcasecmp(line->key, "EntryGuardUnlistedSince")) {
4066 time_t when;
4067 time_t last_try = 0;
4068 if (!node) {
4069 *msg = tor_strdup("Unable to parse entry nodes: "
4070 "EntryGuardDownSince/UnlistedSince without EntryGuard");
4071 break;
4073 if (parse_iso_time(line->value, &when)<0) {
4074 *msg = tor_strdup("Unable to parse entry nodes: "
4075 "Bad time in EntryGuardDownSince/UnlistedSince");
4076 break;
4078 if (when > now) {
4079 /* It's a bad idea to believe info in the future: you can wind
4080 * up with timeouts that aren't allowed to happen for years. */
4081 continue;
4083 if (strlen(line->value) >= ISO_TIME_LEN+ISO_TIME_LEN+1) {
4084 /* ignore failure */
4085 (void) parse_iso_time(line->value+ISO_TIME_LEN+1, &last_try);
4087 if (!strcasecmp(line->key, "EntryGuardDownSince")) {
4088 node->unreachable_since = when;
4089 node->last_attempted = last_try;
4090 } else {
4091 node->bad_since = when;
4093 } else if (!strcasecmp(line->key, "EntryGuardAddedBy")) {
4094 char d[DIGEST_LEN];
4095 /* format is digest version date */
4096 if (strlen(line->value) < HEX_DIGEST_LEN+1+1+1+ISO_TIME_LEN) {
4097 log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough.");
4098 continue;
4100 if (base16_decode(d, sizeof(d), line->value, HEX_DIGEST_LEN)<0 ||
4101 line->value[HEX_DIGEST_LEN] != ' ') {
4102 log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with "
4103 "hex digest", escaped(line->value));
4104 continue;
4106 digestmap_set(added_by, d, tor_strdup(line->value+HEX_DIGEST_LEN+1));
4107 } else {
4108 log_warn(LD_BUG, "Unexpected key %s", line->key);
4112 SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
4114 char *sp;
4115 char *val = digestmap_get(added_by, e->identity);
4116 if (val && (sp = strchr(val, ' '))) {
4117 time_t when;
4118 *sp++ = '\0';
4119 if (parse_iso_time(sp, &when)<0) {
4120 log_warn(LD_BUG, "Can't read time %s in EntryGuardAddedBy", sp);
4121 } else {
4122 e->chosen_by_version = tor_strdup(val);
4123 e->chosen_on_date = when;
4125 } else {
4126 if (state_version) {
4127 e->chosen_by_version = tor_strdup(state_version);
4128 e->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
4133 if (*msg || !set) {
4134 SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
4135 entry_guard_free(e));
4136 smartlist_free(new_entry_guards);
4137 } else { /* !err && set */
4138 if (entry_guards) {
4139 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4140 entry_guard_free(e));
4141 smartlist_free(entry_guards);
4143 entry_guards = new_entry_guards;
4144 entry_guards_dirty = 0;
4145 /* XXX022 hand new_entry_guards to this func, and move it up a
4146 * few lines, so we don't have to re-dirty it */
4147 if (remove_obsolete_entry_guards())
4148 entry_guards_dirty = 1;
4150 digestmap_free(added_by, _tor_free);
4151 return *msg ? -1 : 0;
4154 /** Our list of entry guards has changed, or some element of one
4155 * of our entry guards has changed. Write the changes to disk within
4156 * the next few minutes.
4158 static void
4159 entry_guards_changed(void)
4161 time_t when;
4162 entry_guards_dirty = 1;
4164 /* or_state_save() will call entry_guards_update_state(). */
4165 when = get_options()->AvoidDiskWrites ? time(NULL) + 3600 : time(NULL)+600;
4166 or_state_mark_dirty(get_or_state(), when);
4169 /** If the entry guard info has not changed, do nothing and return.
4170 * Otherwise, free the EntryGuards piece of <b>state</b> and create
4171 * a new one out of the global entry_guards list, and then mark
4172 * <b>state</b> dirty so it will get saved to disk.
4174 void
4175 entry_guards_update_state(or_state_t *state)
4177 config_line_t **next, *line;
4178 if (! entry_guards_dirty)
4179 return;
4181 config_free_lines(state->EntryGuards);
4182 next = &state->EntryGuards;
4183 *next = NULL;
4184 if (!entry_guards)
4185 entry_guards = smartlist_create();
4186 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4188 char dbuf[HEX_DIGEST_LEN+1];
4189 if (!e->made_contact)
4190 continue; /* don't write this one to disk */
4191 *next = line = tor_malloc_zero(sizeof(config_line_t));
4192 line->key = tor_strdup("EntryGuard");
4193 line->value = tor_malloc(HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2);
4194 base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
4195 tor_snprintf(line->value,HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2,
4196 "%s %s", e->nickname, dbuf);
4197 next = &(line->next);
4198 if (e->unreachable_since) {
4199 *next = line = tor_malloc_zero(sizeof(config_line_t));
4200 line->key = tor_strdup("EntryGuardDownSince");
4201 line->value = tor_malloc(ISO_TIME_LEN+1+ISO_TIME_LEN+1);
4202 format_iso_time(line->value, e->unreachable_since);
4203 if (e->last_attempted) {
4204 line->value[ISO_TIME_LEN] = ' ';
4205 format_iso_time(line->value+ISO_TIME_LEN+1, e->last_attempted);
4207 next = &(line->next);
4209 if (e->bad_since) {
4210 *next = line = tor_malloc_zero(sizeof(config_line_t));
4211 line->key = tor_strdup("EntryGuardUnlistedSince");
4212 line->value = tor_malloc(ISO_TIME_LEN+1);
4213 format_iso_time(line->value, e->bad_since);
4214 next = &(line->next);
4216 if (e->chosen_on_date && e->chosen_by_version &&
4217 !strchr(e->chosen_by_version, ' ')) {
4218 char d[HEX_DIGEST_LEN+1];
4219 char t[ISO_TIME_LEN+1];
4220 size_t val_len;
4221 *next = line = tor_malloc_zero(sizeof(config_line_t));
4222 line->key = tor_strdup("EntryGuardAddedBy");
4223 val_len = (HEX_DIGEST_LEN+1+strlen(e->chosen_by_version)
4224 +1+ISO_TIME_LEN+1);
4225 line->value = tor_malloc(val_len);
4226 base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
4227 format_iso_time(t, e->chosen_on_date);
4228 tor_snprintf(line->value, val_len, "%s %s %s",
4229 d, e->chosen_by_version, t);
4230 next = &(line->next);
4233 if (!get_options()->AvoidDiskWrites)
4234 or_state_mark_dirty(get_or_state(), 0);
4235 entry_guards_dirty = 0;
4238 /** If <b>question</b> is the string "entry-guards", then dump
4239 * to *<b>answer</b> a newly allocated string describing all of
4240 * the nodes in the global entry_guards list. See control-spec.txt
4241 * for details.
4242 * For backward compatibility, we also handle the string "helper-nodes".
4243 * */
4245 getinfo_helper_entry_guards(control_connection_t *conn,
4246 const char *question, char **answer,
4247 const char **errmsg)
4249 (void) conn;
4250 (void) errmsg;
4252 if (!strcmp(question,"entry-guards") ||
4253 !strcmp(question,"helper-nodes")) {
4254 smartlist_t *sl = smartlist_create();
4255 char tbuf[ISO_TIME_LEN+1];
4256 char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
4257 if (!entry_guards)
4258 entry_guards = smartlist_create();
4259 SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
4260 size_t len = MAX_VERBOSE_NICKNAME_LEN+ISO_TIME_LEN+32;
4261 char *c = tor_malloc(len);
4262 const char *status = NULL;
4263 time_t when = 0;
4264 routerinfo_t *ri;
4266 if (!e->made_contact) {
4267 status = "never-connected";
4268 } else if (e->bad_since) {
4269 when = e->bad_since;
4270 status = "unusable";
4271 } else {
4272 status = "up";
4275 ri = router_get_by_digest(e->identity);
4276 if (ri) {
4277 router_get_verbose_nickname(nbuf, ri);
4278 } else {
4279 nbuf[0] = '$';
4280 base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
4281 /* e->nickname field is not very reliable if we don't know about
4282 * this router any longer; don't include it. */
4285 if (when) {
4286 format_iso_time(tbuf, when);
4287 tor_snprintf(c, len, "%s %s %s\n", nbuf, status, tbuf);
4288 } else {
4289 tor_snprintf(c, len, "%s %s\n", nbuf, status);
4291 smartlist_add(sl, c);
4292 } SMARTLIST_FOREACH_END(e);
4293 *answer = smartlist_join_strings(sl, "", 0, NULL);
4294 SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
4295 smartlist_free(sl);
4297 return 0;
4300 /** Information about a configured bridge. Currently this just matches the
4301 * ones in the torrc file, but one day we may be able to learn about new
4302 * bridges on our own, and remember them in the state file. */
4303 typedef struct {
4304 /** Address of the bridge. */
4305 tor_addr_t addr;
4306 /** TLS port for the bridge. */
4307 uint16_t port;
4308 /** Expected identity digest, or all zero bytes if we don't know what the
4309 * digest should be. */
4310 char identity[DIGEST_LEN];
4311 /** When should we next try to fetch a descriptor for this bridge? */
4312 download_status_t fetch_status;
4313 } bridge_info_t;
4315 /** A list of configured bridges. Whenever we actually get a descriptor
4316 * for one, we add it as an entry guard. */
4317 static smartlist_t *bridge_list = NULL;
4319 /** Initialize the bridge list to empty, creating it if needed. */
4320 void
4321 clear_bridge_list(void)
4323 if (!bridge_list)
4324 bridge_list = smartlist_create();
4325 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, tor_free(b));
4326 smartlist_clear(bridge_list);
4329 /** Return a bridge pointer if <b>ri</b> is one of our known bridges
4330 * (either by comparing keys if possible, else by comparing addr/port).
4331 * Else return NULL. */
4332 static bridge_info_t *
4333 get_configured_bridge_by_addr_port_digest(tor_addr_t *addr, uint16_t port,
4334 const char *digest)
4336 if (!bridge_list)
4337 return NULL;
4338 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
4340 if (tor_digest_is_zero(bridge->identity) &&
4341 !tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
4342 bridge->port == port)
4343 return bridge;
4344 if (!memcmp(bridge->identity, digest, DIGEST_LEN))
4345 return bridge;
4347 SMARTLIST_FOREACH_END(bridge);
4348 return NULL;
4351 /** Wrapper around get_configured_bridge_by_addr_port_digest() to look
4352 * it up via router descriptor <b>ri</b>. */
4353 static bridge_info_t *
4354 get_configured_bridge_by_routerinfo(routerinfo_t *ri)
4356 tor_addr_t addr;
4357 tor_addr_from_ipv4h(&addr, ri->addr);
4358 return get_configured_bridge_by_addr_port_digest(&addr,
4359 ri->or_port, ri->cache_info.identity_digest);
4362 /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
4364 routerinfo_is_a_configured_bridge(routerinfo_t *ri)
4366 return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
4369 /** We made a connection to a router at <b>addr</b>:<b>port</b>
4370 * without knowing its digest. Its digest turned out to be <b>digest</b>.
4371 * If it was a bridge, and we still don't know its digest, record it.
4373 void
4374 learned_router_identity(tor_addr_t *addr, uint16_t port, const char *digest)
4376 bridge_info_t *bridge =
4377 get_configured_bridge_by_addr_port_digest(addr, port, digest);
4378 if (bridge && tor_digest_is_zero(bridge->identity)) {
4379 memcpy(bridge->identity, digest, DIGEST_LEN);
4380 log_notice(LD_DIR, "Learned fingerprint %s for bridge %s:%d",
4381 hex_str(digest, DIGEST_LEN), fmt_addr(addr), port);
4385 /** Remember a new bridge at <b>addr</b>:<b>port</b>. If <b>digest</b>
4386 * is set, it tells us the identity key too. */
4387 void
4388 bridge_add_from_config(const tor_addr_t *addr, uint16_t port, char *digest)
4390 bridge_info_t *b = tor_malloc_zero(sizeof(bridge_info_t));
4391 tor_addr_copy(&b->addr, addr);
4392 b->port = port;
4393 if (digest)
4394 memcpy(b->identity, digest, DIGEST_LEN);
4395 b->fetch_status.schedule = DL_SCHED_BRIDGE;
4396 if (!bridge_list)
4397 bridge_list = smartlist_create();
4398 smartlist_add(bridge_list, b);
4401 /** If <b>digest</b> is one of our known bridges, return it. */
4402 static bridge_info_t *
4403 find_bridge_by_digest(const char *digest)
4405 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
4407 if (!memcmp(bridge->identity, digest, DIGEST_LEN))
4408 return bridge;
4410 return NULL;
4413 /** We need to ask <b>bridge</b> for its server descriptor. <b>address</b>
4414 * is a helpful string describing this bridge. */
4415 static void
4416 launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
4418 char *address;
4420 if (connection_get_by_type_addr_port_purpose(
4421 CONN_TYPE_DIR, &bridge->addr, bridge->port,
4422 DIR_PURPOSE_FETCH_SERVERDESC))
4423 return; /* it's already on the way */
4425 address = tor_dup_addr(&bridge->addr);
4426 directory_initiate_command(address, &bridge->addr,
4427 bridge->port, 0,
4428 0, /* does not matter */
4429 1, bridge->identity,
4430 DIR_PURPOSE_FETCH_SERVERDESC,
4431 ROUTER_PURPOSE_BRIDGE,
4432 0, "authority.z", NULL, 0, 0);
4433 tor_free(address);
4436 /** Fetching the bridge descriptor from the bridge authority returned a
4437 * "not found". Fall back to trying a direct fetch. */
4438 void
4439 retry_bridge_descriptor_fetch_directly(const char *digest)
4441 bridge_info_t *bridge = find_bridge_by_digest(digest);
4442 if (!bridge)
4443 return; /* not found? oh well. */
4445 launch_direct_bridge_descriptor_fetch(bridge);
4448 /** For each bridge in our list for which we don't currently have a
4449 * descriptor, fetch a new copy of its descriptor -- either directly
4450 * from the bridge or via a bridge authority. */
4451 void
4452 fetch_bridge_descriptors(time_t now)
4454 or_options_t *options = get_options();
4455 int num_bridge_auths = get_n_authorities(BRIDGE_AUTHORITY);
4456 int ask_bridge_directly;
4457 int can_use_bridge_authority;
4459 if (!bridge_list)
4460 return;
4462 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
4464 if (!download_status_is_ready(&bridge->fetch_status, now,
4465 IMPOSSIBLE_TO_DOWNLOAD))
4466 continue; /* don't bother, no need to retry yet */
4468 /* schedule another fetch as if this one will fail, in case it does */
4469 download_status_failed(&bridge->fetch_status, 0);
4471 can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
4472 num_bridge_auths;
4473 ask_bridge_directly = !can_use_bridge_authority ||
4474 !options->UpdateBridgesFromAuthority;
4475 log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
4476 ask_bridge_directly, tor_digest_is_zero(bridge->identity),
4477 !options->UpdateBridgesFromAuthority, !num_bridge_auths);
4479 if (ask_bridge_directly &&
4480 !fascist_firewall_allows_address_or(&bridge->addr, bridge->port)) {
4481 log_notice(LD_DIR, "Bridge at '%s:%d' isn't reachable by our "
4482 "firewall policy. %s.", fmt_addr(&bridge->addr),
4483 bridge->port,
4484 can_use_bridge_authority ?
4485 "Asking bridge authority instead" : "Skipping");
4486 if (can_use_bridge_authority)
4487 ask_bridge_directly = 0;
4488 else
4489 continue;
4492 if (ask_bridge_directly) {
4493 /* we need to ask the bridge itself for its descriptor. */
4494 launch_direct_bridge_descriptor_fetch(bridge);
4495 } else {
4496 /* We have a digest and we want to ask an authority. We could
4497 * combine all the requests into one, but that may give more
4498 * hints to the bridge authority than we want to give. */
4499 char resource[10 + HEX_DIGEST_LEN];
4500 memcpy(resource, "fp/", 3);
4501 base16_encode(resource+3, HEX_DIGEST_LEN+1,
4502 bridge->identity, DIGEST_LEN);
4503 memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
4504 log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
4505 resource);
4506 directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
4507 ROUTER_PURPOSE_BRIDGE, resource, 0);
4510 SMARTLIST_FOREACH_END(bridge);
4513 /** We just learned a descriptor for a bridge. See if that
4514 * digest is in our entry guard list, and add it if not. */
4515 void
4516 learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
4518 tor_assert(ri);
4519 tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
4520 if (get_options()->UseBridges) {
4521 int first = !any_bridge_descriptors_known();
4522 bridge_info_t *bridge = get_configured_bridge_by_routerinfo(ri);
4523 time_t now = time(NULL);
4524 ri->is_running = 1;
4526 if (bridge) { /* if we actually want to use this one */
4527 /* it's here; schedule its re-fetch for a long time from now. */
4528 if (!from_cache)
4529 download_status_reset(&bridge->fetch_status);
4531 add_an_entry_guard(ri, 1);
4532 log_notice(LD_DIR, "new bridge descriptor '%s' (%s)", ri->nickname,
4533 from_cache ? "cached" : "fresh");
4534 if (first)
4535 routerlist_retry_directory_downloads(now);
4540 /** Return 1 if any of our entry guards have descriptors that
4541 * are marked with purpose 'bridge' and are running. Else return 0.
4543 * We use this function to decide if we're ready to start building
4544 * circuits through our bridges, or if we need to wait until the
4545 * directory "server/authority" requests finish. */
4547 any_bridge_descriptors_known(void)
4549 tor_assert(get_options()->UseBridges);
4550 return choose_random_entry(NULL)!=NULL ? 1 : 0;
4553 /** Return 1 if there are any directory conns fetching bridge descriptors
4554 * that aren't marked for close. We use this to guess if we should tell
4555 * the controller that we have a problem. */
4557 any_pending_bridge_descriptor_fetches(void)
4559 smartlist_t *conns = get_connection_array();
4560 SMARTLIST_FOREACH(conns, connection_t *, conn,
4562 if (conn->type == CONN_TYPE_DIR &&
4563 conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
4564 TO_DIR_CONN(conn)->router_purpose == ROUTER_PURPOSE_BRIDGE &&
4565 !conn->marked_for_close &&
4566 conn->linked && !conn->linked_conn->marked_for_close) {
4567 log_debug(LD_DIR, "found one: %s", conn->address);
4568 return 1;
4571 return 0;
4574 /** Return 1 if we have at least one descriptor for a bridge and
4575 * all descriptors we know are down. Else return 0. If <b>act</b> is
4576 * 1, then mark the down bridges up; else just observe and report. */
4577 static int
4578 bridges_retry_helper(int act)
4580 routerinfo_t *ri;
4581 int any_known = 0;
4582 int any_running = 0;
4583 if (!entry_guards)
4584 entry_guards = smartlist_create();
4585 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4587 ri = router_get_by_digest(e->identity);
4588 if (ri && ri->purpose == ROUTER_PURPOSE_BRIDGE) {
4589 any_known = 1;
4590 if (ri->is_running)
4591 any_running = 1; /* some bridge is both known and running */
4592 else if (act) { /* mark it for retry */
4593 ri->is_running = 1;
4594 e->can_retry = 1;
4595 e->bad_since = 0;
4599 log_debug(LD_DIR, "any_known %d, any_running %d", any_known, any_running);
4600 return any_known && !any_running;
4603 /** Do we know any descriptors for our bridges, and are they all
4604 * down? */
4606 bridges_known_but_down(void)
4608 return bridges_retry_helper(0);
4611 /** Mark all down known bridges up. */
4612 void
4613 bridges_retry_all(void)
4615 bridges_retry_helper(1);
4618 /** Release all storage held by the list of entry guards and related
4619 * memory structs. */
4620 void
4621 entry_guards_free_all(void)
4623 if (entry_guards) {
4624 SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
4625 entry_guard_free(e));
4626 smartlist_free(entry_guards);
4627 entry_guards = NULL;
4629 clear_bridge_list();
4630 smartlist_free(bridge_list);
4631 bridge_list = NULL;