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-2008, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 const char circuituse_c_id
[] =
12 * \brief Launch the right sort of circuits and attach streams to them.
17 /********* START VARIABLES **********/
19 extern circuit_t
*global_circuitlist
; /* from circuitlist.c */
21 /********* END VARIABLES ************/
23 static void circuit_expire_old_circuits(time_t now
);
24 static void circuit_increment_failure_count(void);
26 /** Return 1 if <b>circ</b> could be returned by circuit_get_best().
30 circuit_is_acceptable(circuit_t
*circ
, edge_connection_t
*conn
,
31 int must_be_open
, uint8_t purpose
,
32 int need_uptime
, int need_internal
,
35 routerinfo_t
*exitrouter
;
36 cpath_build_state_t
*build_state
;
39 tor_assert(conn
->socks_request
);
41 if (!CIRCUIT_IS_ORIGIN(circ
))
42 return 0; /* this circ doesn't start at us */
43 if (must_be_open
&& (circ
->state
!= CIRCUIT_STATE_OPEN
|| !circ
->n_conn
))
44 return 0; /* ignore non-open circs */
45 if (circ
->marked_for_close
)
48 /* if this circ isn't our purpose, skip. */
49 if (purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
&& !must_be_open
) {
50 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_ESTABLISH_REND
&&
51 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_READY
&&
52 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
&&
53 circ
->purpose
!= CIRCUIT_PURPOSE_C_REND_JOINED
)
55 } else if (purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
&&
57 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_INTRODUCING
&&
58 circ
->purpose
!= CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
)
61 if (purpose
!= circ
->purpose
)
65 if (purpose
== CIRCUIT_PURPOSE_C_GENERAL
)
66 if (circ
->timestamp_dirty
&&
67 circ
->timestamp_dirty
+get_options()->MaxCircuitDirtiness
<= now
)
70 /* decide if this circ is suitable for this conn */
72 /* for rend circs, circ->cpath->prev is not the last router in the
73 * circuit, it's the magical extra bob hop. so just check the nickname
74 * of the one we meant to finish at.
76 build_state
= TO_ORIGIN_CIRCUIT(circ
)->build_state
;
77 exitrouter
= build_state_get_exit_router(build_state
);
79 if (need_uptime
&& !build_state
->need_uptime
)
81 if (need_internal
!= build_state
->is_internal
)
84 if (purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
85 if (!exitrouter
&& !build_state
->onehop_tunnel
) {
86 log_debug(LD_CIRC
,"Not considering circuit with unknown router.");
87 return 0; /* this circuit is screwed and doesn't know it yet,
88 * or is a rendezvous circuit. */
90 if (build_state
->onehop_tunnel
) {
91 if (!conn
->want_onehop
) {
92 log_debug(LD_CIRC
,"Skipping one-hop circuit.");
95 tor_assert(conn
->chosen_exit_name
);
96 if (build_state
->chosen_exit
) {
97 char digest
[DIGEST_LEN
];
98 if (hexdigest_to_digest(conn
->chosen_exit_name
, digest
) < 0)
99 return 0; /* broken digest, we don't want it */
100 if (memcmp(digest
, build_state
->chosen_exit
->identity_digest
,
102 return 0; /* this is a circuit to somewhere else */
103 if (tor_digest_is_zero(digest
)) {
104 /* we don't know the digest; have to compare addr:port */
106 if (!tor_inet_aton(conn
->socks_request
->address
, &in
) ||
107 build_state
->chosen_exit
->addr
!= ntohl(in
.s_addr
) ||
108 build_state
->chosen_exit
->port
!= conn
->socks_request
->port
)
113 if (conn
->want_onehop
) {
114 /* don't use three-hop circuits -- that could hurt our anonymity. */
118 if (exitrouter
&& !connection_ap_can_use_exit(conn
, exitrouter
)) {
119 /* can't exit from this router */
122 } else { /* not general */
123 if (rend_cmp_service_ids(conn
->rend_query
,
124 TO_ORIGIN_CIRCUIT(circ
)->rend_query
)) {
125 /* this circ is not for this conn */
132 /** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
133 * <b>purpose</b>, and return 0 otherwise. Used by circuit_get_best.
136 circuit_is_better(circuit_t
*a
, circuit_t
*b
, uint8_t purpose
)
139 case CIRCUIT_PURPOSE_C_GENERAL
:
140 /* if it's used but less dirty it's best;
141 * else if it's more recently created it's best
143 if (b
->timestamp_dirty
) {
144 if (a
->timestamp_dirty
&&
145 a
->timestamp_dirty
> b
->timestamp_dirty
)
148 if (a
->timestamp_dirty
||
149 a
->timestamp_created
> b
->timestamp_created
)
151 if (CIRCUIT_IS_ORIGIN(b
) &&
152 TO_ORIGIN_CIRCUIT(b
)->build_state
->is_internal
)
156 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
157 /* the closer it is to ack_wait the better it is */
158 if (a
->purpose
> b
->purpose
)
161 case CIRCUIT_PURPOSE_C_REND_JOINED
:
162 /* the closer it is to rend_joined the better it is */
163 if (a
->purpose
> b
->purpose
)
170 /** Find the best circ that conn can use, preferably one which is
171 * dirty. Circ must not be too old.
173 * Conn must be defined.
175 * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
177 * circ_purpose specifies what sort of circuit we must have.
178 * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
180 * If it's REND_JOINED and must_be_open==0, then return the closest
181 * rendezvous-purposed circuit that you can find.
183 * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
184 * closest introduce-purposed circuit that you can find.
186 static origin_circuit_t
*
187 circuit_get_best(edge_connection_t
*conn
, int must_be_open
, uint8_t purpose
,
188 int need_uptime
, int need_internal
)
190 circuit_t
*circ
, *best
=NULL
;
191 time_t now
= time(NULL
);
195 tor_assert(purpose
== CIRCUIT_PURPOSE_C_GENERAL
||
196 purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
||
197 purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
);
199 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
200 if (!circuit_is_acceptable(circ
,conn
,must_be_open
,purpose
,
201 need_uptime
,need_internal
,now
))
204 /* now this is an acceptable circ to hand back. but that doesn't
205 * mean it's the *best* circ to hand back. try to decide.
207 if (!best
|| circuit_is_better(circ
,best
,purpose
))
211 return best
? TO_ORIGIN_CIRCUIT(best
) : NULL
;
214 /** Close all circuits that start at us, aren't open, and were born
215 * at least CircuitBuildTimeout seconds ago.
218 circuit_expire_building(time_t now
)
220 circuit_t
*victim
, *circ
= global_circuitlist
;
221 time_t cutoff
= now
- get_options()->CircuitBuildTimeout
;
222 time_t begindir_cutoff
= now
- get_options()->CircuitBuildTimeout
/2;
223 cpath_build_state_t
*build_state
;
228 if (!CIRCUIT_IS_ORIGIN(victim
) || /* didn't originate here */
229 victim
->marked_for_close
) /* don't mess with marked circs */
232 build_state
= TO_ORIGIN_CIRCUIT(victim
)->build_state
;
233 if (victim
->timestamp_created
>
234 ((build_state
&& build_state
->onehop_tunnel
) ?
235 begindir_cutoff
: cutoff
))
236 continue; /* it's still young, leave it alone */
239 /* some debug logs, to help track bugs */
240 if (victim
->purpose
>= CIRCUIT_PURPOSE_C_INTRODUCING
&&
241 victim
->purpose
<= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
) {
242 if (!victim
->timestamp_dirty
)
243 log_fn(LOG_DEBUG
,"Considering %sopen purp %d to %s (circid %d)."
245 victim
->state
== CIRCUIT_STATE_OPEN
? "" : "non",
246 victim
->purpose
, victim
->build_state
->chosen_exit_name
,
249 log_fn(LOG_DEBUG
,"Considering %sopen purp %d to %s (circid %d). "
250 "%d secs since dirty.",
251 victim
->state
== CIRCUIT_STATE_OPEN
? "" : "non",
252 victim
->purpose
, victim
->build_state
->chosen_exit_name
,
254 (int)(now
- victim
->timestamp_dirty
));
258 /* if circ is !open, or if it's open but purpose is a non-finished
259 * intro or rend, then mark it for close */
260 if (victim
->state
== CIRCUIT_STATE_OPEN
) {
261 switch (victim
->purpose
) {
262 default: /* most open circuits can be left alone. */
263 continue; /* yes, continue inside a switch refers to the nearest
264 * enclosing loop. C is smart. */
265 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
266 case CIRCUIT_PURPOSE_C_INTRODUCING
:
267 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
268 break; /* too old, need to die */
269 case CIRCUIT_PURPOSE_C_REND_READY
:
270 /* it's a rend_ready circ -- has it already picked a query? */
271 /* c_rend_ready circs measure age since timestamp_dirty,
272 * because that's set when they switch purposes
274 if (TO_ORIGIN_CIRCUIT(victim
)->rend_query
[0] ||
275 victim
->timestamp_dirty
> cutoff
)
278 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
:
279 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
:
280 /* rend and intro circs become dirty each time they
281 * make an introduction attempt. so timestamp_dirty
282 * will reflect the time since the last attempt.
284 if (victim
->timestamp_dirty
> cutoff
)
291 log_info(LD_CIRC
,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
292 victim
->n_conn
->_base
.address
, victim
->n_port
,
294 victim
->state
, circuit_state_to_string(victim
->state
),
297 log_info(LD_CIRC
,"Abandoning circ %d (state %d:%s, purpose %d)",
298 victim
->n_circ_id
, victim
->state
,
299 circuit_state_to_string(victim
->state
), victim
->purpose
);
301 circuit_log_path(LOG_INFO
,LD_CIRC
,TO_ORIGIN_CIRCUIT(victim
));
302 circuit_mark_for_close(victim
, END_CIRC_REASON_TIMEOUT
);
306 /** Remove any elements in <b>needed_ports</b> that are handled by an
307 * open or in-progress circuit.
310 circuit_remove_handled_ports(smartlist_t
*needed_ports
)
315 for (i
= 0; i
< smartlist_len(needed_ports
); ++i
) {
316 port
= smartlist_get(needed_ports
, i
);
318 if (circuit_stream_is_being_handled(NULL
, *port
,
319 MIN_CIRCUITS_HANDLING_STREAM
)) {
320 // log_debug(LD_CIRC,"Port %d is already being handled; removing.", port);
321 smartlist_del(needed_ports
, i
--);
324 log_debug(LD_CIRC
,"Port %d is not handled.", *port
);
329 /** Return 1 if at least <b>min</b> general-purpose non-internal circuits
330 * will have an acceptable exit node for exit stream <b>conn</b> if it
331 * is defined, else for "*:port".
335 circuit_stream_is_being_handled(edge_connection_t
*conn
,
336 uint16_t port
, int min
)
339 routerinfo_t
*exitrouter
;
341 time_t now
= time(NULL
);
342 int need_uptime
= smartlist_string_num_isin(get_options()->LongLivedPorts
,
343 conn
? conn
->socks_request
->port
: port
);
345 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
346 if (CIRCUIT_IS_ORIGIN(circ
) &&
347 !circ
->marked_for_close
&&
348 circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
&&
349 (!circ
->timestamp_dirty
||
350 circ
->timestamp_dirty
+ get_options()->MaxCircuitDirtiness
> now
)) {
351 cpath_build_state_t
*build_state
= TO_ORIGIN_CIRCUIT(circ
)->build_state
;
352 if (build_state
->is_internal
|| build_state
->onehop_tunnel
)
355 exitrouter
= build_state_get_exit_router(build_state
);
356 if (exitrouter
&& (!need_uptime
|| build_state
->need_uptime
)) {
359 ok
= connection_ap_can_use_exit(conn
, exitrouter
);
361 addr_policy_result_t r
= compare_addr_to_addr_policy(
362 0, port
, exitrouter
->exit_policy
);
363 ok
= r
!= ADDR_POLICY_REJECTED
&& r
!= ADDR_POLICY_PROBABLY_REJECTED
;
375 /** Don't keep more than this many unused open circuits around. */
376 #define MAX_UNUSED_OPEN_CIRCUITS 12
378 /** Figure out how many circuits we have open that are clean. Make
379 * sure it's enough for all the upcoming behaviors we predict we'll have.
380 * But if we have too many, close the not-so-useful ones.
383 circuit_predict_and_launch_new(void)
386 int num
=0, num_internal
=0, num_uptime_internal
=0;
387 int hidserv_needs_uptime
=0, hidserv_needs_capacity
=1;
388 int port_needs_uptime
=0, port_needs_capacity
=1;
389 time_t now
= time(NULL
);
392 /* First, count how many of each type of circuit we have already. */
393 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
394 cpath_build_state_t
*build_state
;
395 if (!CIRCUIT_IS_ORIGIN(circ
))
397 if (circ
->marked_for_close
)
398 continue; /* don't mess with marked circs */
399 if (circ
->timestamp_dirty
)
400 continue; /* only count clean circs */
401 if (circ
->purpose
!= CIRCUIT_PURPOSE_C_GENERAL
)
402 continue; /* only pay attention to general-purpose circs */
403 build_state
= TO_ORIGIN_CIRCUIT(circ
)->build_state
;
404 if (build_state
->onehop_tunnel
)
407 if (build_state
->is_internal
)
409 if (build_state
->need_uptime
&& build_state
->is_internal
)
410 num_uptime_internal
++;
413 /* If that's enough, then stop now. */
414 if (num
>= MAX_UNUSED_OPEN_CIRCUITS
)
415 return; /* we already have many, making more probably will hurt */
417 /* Second, see if we need any more exit circuits. */
418 /* check if we know of a port that's been requested recently
419 * and no circuit is currently available that can handle it. */
420 if (!circuit_all_predicted_ports_handled(now
, &port_needs_uptime
,
421 &port_needs_capacity
)) {
422 if (port_needs_uptime
)
423 flags
|= CIRCLAUNCH_NEED_UPTIME
;
424 if (port_needs_capacity
)
425 flags
|= CIRCLAUNCH_NEED_CAPACITY
;
427 "Have %d clean circs (%d internal), need another exit circ.",
429 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, flags
);
433 /* Third, see if we need any more hidden service (server) circuits. */
434 if (num_rend_services() && num_uptime_internal
< 3) {
435 flags
= (CIRCLAUNCH_NEED_CAPACITY
| CIRCLAUNCH_NEED_UPTIME
|
436 CIRCLAUNCH_IS_INTERNAL
);
438 "Have %d clean circs (%d internal), need another internal "
439 "circ for my hidden service.",
441 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, flags
);
445 /* Fourth, see if we need any more hidden service (client) circuits. */
446 if (rep_hist_get_predicted_internal(now
, &hidserv_needs_uptime
,
447 &hidserv_needs_capacity
) &&
448 ((num_uptime_internal
<2 && hidserv_needs_uptime
) ||
450 if (hidserv_needs_uptime
)
451 flags
|= CIRCLAUNCH_NEED_UPTIME
;
452 if (hidserv_needs_capacity
)
453 flags
|= CIRCLAUNCH_NEED_CAPACITY
;
454 flags
|= CIRCLAUNCH_IS_INTERNAL
;
456 "Have %d clean circs (%d uptime-internal, %d internal), need"
457 " another hidserv circ.",
458 num
, num_uptime_internal
, num_internal
);
459 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, flags
);
464 /** Build a new test circuit every 5 minutes */
465 #define TESTING_CIRCUIT_INTERVAL 300
467 /** This function is called once a second, if router_have_min_dir_info() is
468 * true. Its job is to make sure all services we offer have enough circuits
469 * available. Some services just want enough circuits for current tasks,
470 * whereas others want a minimum set of idle circuits hanging around.
473 circuit_build_needed_circs(time_t now
)
475 static long time_to_new_circuit
= 0;
476 or_options_t
*options
= get_options();
478 /* launch a new circ for any pending streams that need one */
479 connection_ap_attach_pending();
481 /* make sure any hidden services have enough intro points */
482 rend_services_introduce();
484 if (time_to_new_circuit
< now
) {
485 circuit_reset_failure_count(1);
486 time_to_new_circuit
= now
+ options
->NewCircuitPeriod
;
487 if (proxy_mode(get_options()))
488 addressmap_clean(now
);
489 circuit_expire_old_circuits(now
);
491 #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
492 circ
= circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL
);
493 if (get_options()->RunTesting
&&
495 circ
->timestamp_created
+ TESTING_CIRCUIT_INTERVAL
< now
) {
496 log_fn(LOG_INFO
,"Creating a new testing circuit.");
497 circuit_launch_by_router(CIRCUIT_PURPOSE_C_GENERAL
, NULL
, 0);
501 if (!options
->DisablePredictedCircuits
)
502 circuit_predict_and_launch_new();
505 /** If the stream <b>conn</b> is a member of any of the linked
506 * lists of <b>circ</b>, then remove it from the list.
509 circuit_detach_stream(circuit_t
*circ
, edge_connection_t
*conn
)
511 edge_connection_t
*prevconn
;
516 conn
->cpath_layer
= NULL
; /* make sure we don't keep a stale pointer */
517 conn
->on_circuit
= NULL
;
519 if (CIRCUIT_IS_ORIGIN(circ
)) {
520 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
521 if (conn
== origin_circ
->p_streams
) {
522 origin_circ
->p_streams
= conn
->next_stream
;
526 for (prevconn
= origin_circ
->p_streams
;
527 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
528 prevconn
= prevconn
->next_stream
)
530 if (prevconn
&& prevconn
->next_stream
) {
531 prevconn
->next_stream
= conn
->next_stream
;
535 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
536 if (conn
== or_circ
->n_streams
) {
537 or_circ
->n_streams
= conn
->next_stream
;
540 if (conn
== or_circ
->resolving_streams
) {
541 or_circ
->resolving_streams
= conn
->next_stream
;
545 for (prevconn
= or_circ
->n_streams
;
546 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
547 prevconn
= prevconn
->next_stream
)
549 if (prevconn
&& prevconn
->next_stream
) {
550 prevconn
->next_stream
= conn
->next_stream
;
554 for (prevconn
= or_circ
->resolving_streams
;
555 prevconn
&& prevconn
->next_stream
&& prevconn
->next_stream
!= conn
;
556 prevconn
= prevconn
->next_stream
)
558 if (prevconn
&& prevconn
->next_stream
) {
559 prevconn
->next_stream
= conn
->next_stream
;
564 log_warn(LD_BUG
,"Edge connection not in circuit's list.");
565 /* Don't give an error here; it's harmless. */
566 tor_fragile_assert();
569 /** Find each circuit that has been unused for too long, or dirty
570 * for too long and has no streams on it: mark it for close.
573 circuit_expire_old_circuits(time_t now
)
576 time_t cutoff
= now
- get_options()->CircuitIdleTimeout
;
578 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
579 if (circ
->marked_for_close
|| ! CIRCUIT_IS_ORIGIN(circ
))
581 /* If the circuit has been dirty for too long, and there are no streams
582 * on it, mark it for close.
584 if (circ
->timestamp_dirty
&&
585 circ
->timestamp_dirty
+ get_options()->MaxCircuitDirtiness
< now
&&
586 !TO_ORIGIN_CIRCUIT(circ
)->p_streams
/* nothing attached */ ) {
587 log_debug(LD_CIRC
, "Closing n_circ_id %d (dirty %d secs ago, purp %d)",
588 circ
->n_circ_id
, (int)(now
- circ
->timestamp_dirty
),
590 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
591 } else if (!circ
->timestamp_dirty
&&
592 circ
->state
== CIRCUIT_STATE_OPEN
&&
593 circ
->purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
594 if (circ
->timestamp_created
< cutoff
) {
596 "Closing circuit that has been unused for %d seconds.",
597 (int)(now
- circ
->timestamp_created
));
598 circuit_mark_for_close(circ
, END_CIRC_REASON_FINISHED
);
604 /** Number of testing circuits we want open before testing our bandwidth. */
605 #define NUM_PARALLEL_TESTING_CIRCS 4
607 /** True iff we've ever had enough testing circuits open to test our
609 static int have_performed_bandwidth_test
= 0;
611 /** Reset have_performed_bandwidth_test, so we'll start building
612 * testing circuits again so we can exercise our bandwidth. */
614 reset_bandwidth_test(void)
616 have_performed_bandwidth_test
= 0;
619 /** Return 1 if we've already exercised our bandwidth, or if we
620 * have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
621 * established or on the way. Else return 0.
624 circuit_enough_testing_circs(void)
629 if (have_performed_bandwidth_test
)
632 for (circ
= global_circuitlist
; circ
; circ
= circ
->next
) {
633 if (!circ
->marked_for_close
&& CIRCUIT_IS_ORIGIN(circ
) &&
634 circ
->purpose
== CIRCUIT_PURPOSE_TESTING
&&
635 circ
->state
== CIRCUIT_STATE_OPEN
)
638 return num
>= NUM_PARALLEL_TESTING_CIRCS
;
641 /** A testing circuit has completed. Take whatever stats we want.
642 * Noticing reachability is taken care of in onionskin_answer(),
643 * so there's no need to record anything here. But if we still want
644 * to do the bandwidth test, and we now have enough testing circuits
648 circuit_testing_opened(origin_circuit_t
*circ
)
650 if (have_performed_bandwidth_test
) {
651 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_AT_ORIGIN
);
652 } else if (circuit_enough_testing_circs()) {
653 router_perform_bandwidth_test(NUM_PARALLEL_TESTING_CIRCS
, time(NULL
));
654 have_performed_bandwidth_test
= 1;
656 consider_testing_reachability(1, 0);
659 /** A testing circuit has failed to build. Take whatever stats we want. */
661 circuit_testing_failed(origin_circuit_t
*circ
, int at_last_hop
)
663 routerinfo_t
*me
= router_get_my_routerinfo();
664 if (server_mode(get_options()) && check_whether_orport_reachable())
670 "Our testing circuit (to see if your ORPort is reachable) "
671 "has failed. I'll try again later.");
672 control_event_server_status(LOG_WARN
, "REACHABILITY_FAILED ORADDRESS=%s:%d",
673 me
->address
, me
->or_port
);
675 /* These aren't used yet. */
680 /** The circuit <b>circ</b> has just become open. Take the next
681 * step: for rendezvous circuits, we pass circ to the appropriate
682 * function in rendclient or rendservice. For general circuits, we
683 * call connection_ap_attach_pending, which looks for pending streams
684 * that could use circ.
687 circuit_has_opened(origin_circuit_t
*circ
)
689 control_event_circuit_status(circ
, CIRC_EVENT_BUILT
, 0);
691 switch (TO_CIRCUIT(circ
)->purpose
) {
692 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
693 rend_client_rendcirc_has_opened(circ
);
694 connection_ap_attach_pending();
696 case CIRCUIT_PURPOSE_C_INTRODUCING
:
697 rend_client_introcirc_has_opened(circ
);
699 case CIRCUIT_PURPOSE_C_GENERAL
:
700 /* Tell any AP connections that have been waiting for a new
701 * circuit that one is ready. */
702 connection_ap_attach_pending();
704 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
705 /* at Bob, waiting for introductions */
706 rend_service_intro_has_opened(circ
);
708 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
709 /* at Bob, connecting to rend point */
710 rend_service_rendezvous_has_opened(circ
);
712 case CIRCUIT_PURPOSE_TESTING
:
713 circuit_testing_opened(circ
);
716 * This won't happen in normal operation, but might happen if the
717 * controller did it. Just let it slide. */
721 /** Called whenever a circuit could not be successfully built.
724 circuit_build_failed(origin_circuit_t
*circ
)
726 /* we should examine circ and see if it failed because of
727 * the last hop or an earlier hop. then use this info below.
729 int failed_at_last_hop
= 0;
730 /* If the last hop isn't open, and the second-to-last is, we failed
731 * at the last hop. */
733 circ
->cpath
->prev
->state
!= CPATH_STATE_OPEN
&&
734 circ
->cpath
->prev
->prev
->state
== CPATH_STATE_OPEN
) {
735 failed_at_last_hop
= 1;
738 circ
->cpath
->state
!= CPATH_STATE_OPEN
) {
739 /* We failed at the first hop. If there's an OR connection
740 to blame, blame it. */
741 or_connection_t
*n_conn
= NULL
;
742 if (circ
->_base
.n_conn
) {
743 n_conn
= circ
->_base
.n_conn
;
744 } else if (circ
->_base
.state
== CIRCUIT_STATE_OR_WAIT
) {
745 /* we have to hunt for it */
746 n_conn
= connection_or_get_by_identity_digest(
747 circ
->_base
.n_conn_id_digest
);
751 "Our circuit failed to get a response from the first hop "
752 "(%s:%d). I'm going to try to rotate to a better connection.",
753 n_conn
->_base
.address
, n_conn
->_base
.port
);
754 n_conn
->_base
.or_is_obsolete
= 1;
755 entry_guard_register_connect_status(n_conn
->identity_digest
, 0,
758 /* if there are any one-hop streams waiting on this circuit, fail
759 * them now so they can retry elsewhere. */
760 connection_ap_fail_onehop(circ
->_base
.n_conn_id_digest
, circ
->build_state
);
763 switch (circ
->_base
.purpose
) {
764 case CIRCUIT_PURPOSE_C_GENERAL
:
765 /* If we never built the circuit, note it as a failure. */
766 circuit_increment_failure_count();
767 if (failed_at_last_hop
) {
768 /* Make sure any streams that demand our last hop as their exit
769 * know that it's unlikely to happen. */
770 circuit_discard_optional_exit_enclaves(circ
->cpath
->prev
->extend_info
);
773 case CIRCUIT_PURPOSE_TESTING
:
774 circuit_testing_failed(circ
, failed_at_last_hop
);
776 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
777 /* at Bob, waiting for introductions */
778 if (circ
->_base
.state
!= CIRCUIT_STATE_OPEN
) {
779 circuit_increment_failure_count();
781 /* no need to care here, because bob will rebuild intro
782 * points periodically. */
784 case CIRCUIT_PURPOSE_C_INTRODUCING
:
785 /* at Alice, connecting to intro point */
786 /* Don't increment failure count, since Bob may have picked
787 * the introduction point maliciously */
788 /* Alice will pick a new intro point when this one dies, if
789 * the stream in question still cares. No need to act here. */
791 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
792 /* at Alice, waiting for Bob */
793 circuit_increment_failure_count();
794 /* Alice will pick a new rend point when this one dies, if
795 * the stream in question still cares. No need to act here. */
797 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
798 /* at Bob, connecting to rend point */
799 /* Don't increment failure count, since Alice may have picked
800 * the rendezvous point maliciously */
802 "Couldn't connect to Alice's chosen rend point %s "
804 escaped(build_state_get_exit_nickname(circ
->build_state
)),
805 failed_at_last_hop
?"last":"non-last");
806 rend_service_relaunch_rendezvous(circ
);
809 * This won't happen in normal operation, but might happen if the
810 * controller did it. Just let it slide. */
814 /** Number of consecutive failures so far; should only be touched by
815 * circuit_launch_new and circuit_*_failure_count.
817 static int n_circuit_failures
= 0;
818 /** Before the last time we called circuit_reset_failure_count(), were
819 * there a lot of failures? */
820 static int did_circs_fail_last_period
= 0;
822 /** Don't retry launching a new circuit if we try this many times with no
824 #define MAX_CIRCUIT_FAILURES 5
826 /** Launch a new circuit; see circuit_launch_by_extend_info() for
827 * details on arguments. */
829 circuit_launch_by_router(uint8_t purpose
,
830 routerinfo_t
*exit
, int flags
)
832 origin_circuit_t
*circ
;
833 extend_info_t
*info
= NULL
;
835 info
= extend_info_from_router(exit
);
836 circ
= circuit_launch_by_extend_info(purpose
, info
, flags
);
838 extend_info_free(info
);
842 /** Launch a new circuit with purpose <b>purpose</b> and exit node
843 * <b>extend_info</b> (or NULL to select a random exit node). If flags
844 * contains CIRCLAUNCH_NEED_UPTIME, choose among routers with high uptime. If
845 * CIRCLAUNCH_NEED_CAPACITY is set, choose among routers with high bandwidth.
846 * If CIRCLAUNCH_IS_INTERNAL is true, the last hop need not be an exit node.
847 * If CIRCLAUNCH_ONEHOP_TUNNEL is set, the circuit will have only one hop.
848 * Return the newly allocated circuit on success, or NULL on failure. */
850 circuit_launch_by_extend_info(uint8_t purpose
,
851 extend_info_t
*extend_info
,
854 origin_circuit_t
*circ
;
855 int onehop_tunnel
= (flags
& CIRCLAUNCH_ONEHOP_TUNNEL
) != 0;
857 if (!onehop_tunnel
&& !router_have_minimum_dir_info()) {
858 log_debug(LD_CIRC
,"Haven't fetched enough directory info yet; canceling "
863 if ((extend_info
|| purpose
!= CIRCUIT_PURPOSE_C_GENERAL
) &&
864 purpose
!= CIRCUIT_PURPOSE_TESTING
&& !onehop_tunnel
) {
865 /* see if there are appropriate circs available to cannibalize. */
866 /* XXX020 if we're planning to add a hop, perhaps we want to look for
867 * internal circs rather than exit circs? -RD */
868 circ
= circuit_find_to_cannibalize(purpose
, extend_info
, flags
);
870 log_info(LD_CIRC
,"Cannibalizing circ '%s' for purpose %d",
871 build_state_get_exit_nickname(circ
->build_state
), purpose
);
872 circ
->_base
.purpose
= purpose
;
873 /* reset the birth date of this circ, else expire_building
874 * will see it and think it's been trying to build since it
876 circ
->_base
.timestamp_created
= time(NULL
);
878 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
879 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
:
880 /* it's ready right now */
882 case CIRCUIT_PURPOSE_C_INTRODUCING
:
883 case CIRCUIT_PURPOSE_S_CONNECT_REND
:
884 case CIRCUIT_PURPOSE_C_GENERAL
:
885 /* need to add a new hop */
886 tor_assert(extend_info
);
887 if (circuit_extend_to_new_exit(circ
, extend_info
) < 0)
892 "unexpected purpose %d when cannibalizing a circ.",
894 tor_fragile_assert();
901 if (did_circs_fail_last_period
&&
902 n_circuit_failures
> MAX_CIRCUIT_FAILURES
) {
903 /* too many failed circs in a row. don't try. */
904 // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
908 /* try a circ. if it fails, circuit_mark_for_close will increment
909 * n_circuit_failures */
910 return circuit_establish_circuit(purpose
, extend_info
, flags
);
913 /** Launch a new circuit; see circuit_launch_by_extend_info() for
914 * details on arguments. */
916 circuit_launch_by_nickname(uint8_t purpose
,
917 const char *exit_nickname
, int flags
)
919 routerinfo_t
*router
= NULL
;
922 router
= router_get_by_nickname(exit_nickname
, 1);
924 log_warn(LD_GENERAL
, "Trying to launch circ by nickname, but "
925 "no such OR as '%s'", exit_nickname
);
929 return circuit_launch_by_router(purpose
, router
, flags
);
932 /** Record another failure at opening a general circuit. When we have
933 * too many, we'll stop trying for the remainder of this minute.
936 circuit_increment_failure_count(void)
938 ++n_circuit_failures
;
939 log_debug(LD_CIRC
,"n_circuit_failures now %d.",n_circuit_failures
);
942 /** Reset the failure count for opening general circuits. This means
943 * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
947 circuit_reset_failure_count(int timeout
)
949 if (timeout
&& n_circuit_failures
> MAX_CIRCUIT_FAILURES
)
950 did_circs_fail_last_period
= 1;
952 did_circs_fail_last_period
= 0;
953 n_circuit_failures
= 0;
956 /** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
957 * there isn't one, and there isn't one on the way, launch one and return
958 * 0. If it will never work, return -1.
960 * Write the found or in-progress or launched circ into *circp.
963 circuit_get_open_circ_or_launch(edge_connection_t
*conn
,
964 uint8_t desired_circuit_purpose
,
965 origin_circuit_t
**circp
)
967 origin_circuit_t
*circ
;
968 int check_exit_policy
;
969 int need_uptime
, need_internal
;
971 or_options_t
*options
= get_options();
975 tor_assert(conn
->_base
.state
== AP_CONN_STATE_CIRCUIT_WAIT
);
977 conn
->socks_request
->command
== SOCKS_COMMAND_CONNECT
&&
978 !conn
->use_begindir
&&
979 !connection_edge_is_rendezvous_stream(conn
);
980 want_onehop
= conn
->want_onehop
;
982 need_uptime
= !conn
->want_onehop
&& !conn
->use_begindir
&&
983 smartlist_string_num_isin(options
->LongLivedPorts
,
984 conn
->socks_request
->port
);
985 need_internal
= desired_circuit_purpose
!= CIRCUIT_PURPOSE_C_GENERAL
;
987 circ
= circuit_get_best(conn
, 1, desired_circuit_purpose
,
988 need_uptime
, need_internal
);
992 return 1; /* we're happy */
995 if (!want_onehop
&& !router_have_minimum_dir_info()) {
996 if (!connection_get_by_type(CONN_TYPE_DIR
)) {
997 if (options
->UseBridges
&& bridges_known_but_down()) {
998 log_notice(LD_APP
|LD_DIR
,
999 "Application request when we're believed to be "
1000 "offline. Optimistically trying known bridges again.");
1001 bridges_retry_all();
1002 } else if (!options
->UseBridges
|| any_bridge_descriptors_known()) {
1003 log_notice(LD_APP
|LD_DIR
,
1004 "Application request when we're believed to be "
1005 "offline. Optimistically trying directory fetches again.");
1006 routerlist_retry_directory_downloads(time(NULL
));
1009 /* the stream will be dealt with when router_have_minimum_dir_info becomes
1010 * 1, or when all directory attempts fail and directory_all_unreachable()
1016 /* Do we need to check exit policy? */
1017 if (check_exit_policy
) {
1020 if (tor_inet_aton(conn
->socks_request
->address
, &in
))
1021 addr
= ntohl(in
.s_addr
);
1022 if (router_exit_policy_all_routers_reject(addr
, conn
->socks_request
->port
,
1025 "No Tor server exists that allows exit to %s:%d. Rejecting.",
1026 safe_str(conn
->socks_request
->address
),
1027 conn
->socks_request
->port
);
1032 /* is one already on the way? */
1033 circ
= circuit_get_best(conn
, 0, desired_circuit_purpose
,
1034 need_uptime
, need_internal
);
1036 log_debug(LD_CIRC
, "one on the way!");
1038 extend_info_t
*extend_info
=NULL
;
1039 uint8_t new_circ_purpose
;
1041 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
) {
1042 /* need to pick an intro point */
1043 extend_info
= rend_client_get_random_intro(conn
->rend_query
);
1046 "No intro points for '%s': refetching service descriptor.",
1047 safe_str(conn
->rend_query
));
1048 rend_client_refetch_renddesc(conn
->rend_query
);
1049 rend_client_refetch_v2_renddesc(conn
->rend_query
);
1050 conn
->_base
.state
= AP_CONN_STATE_RENDDESC_WAIT
;
1053 log_info(LD_REND
,"Chose '%s' as intro point for '%s'.",
1054 extend_info
->nickname
, safe_str(conn
->rend_query
));
1057 /* If we have specified a particular exit node for our
1058 * connection, then be sure to open a circuit to that exit node.
1060 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_GENERAL
) {
1061 if (conn
->chosen_exit_name
) {
1063 int opt
= conn
->_base
.chosen_exit_optional
;
1064 r
= router_get_by_nickname(conn
->chosen_exit_name
, 1);
1066 extend_info
= extend_info_from_router(r
);
1068 log_debug(LD_DIR
, "considering %d, %s",
1069 want_onehop
, conn
->chosen_exit_name
);
1070 if (want_onehop
&& conn
->chosen_exit_name
[0] == '$') {
1071 /* We're asking for a one-hop circuit to a router that
1072 * we don't have a routerinfo about. Make up an extend_info. */
1073 char digest
[DIGEST_LEN
];
1074 char *hexdigest
= conn
->chosen_exit_name
+1;
1076 if (strlen(hexdigest
) < HEX_DIGEST_LEN
||
1077 base16_decode(digest
,DIGEST_LEN
,hexdigest
,HEX_DIGEST_LEN
)<0) {
1078 log_info(LD_DIR
, "Broken exit digest on tunnel conn. Closing.");
1081 if (!tor_inet_aton(conn
->socks_request
->address
, &in
)) {
1082 log_info(LD_DIR
, "Broken address on tunnel conn. Closing.");
1085 extend_info
= extend_info_alloc(conn
->chosen_exit_name
+1,
1086 digest
, NULL
, ntohl(in
.s_addr
),
1087 conn
->socks_request
->port
);
1089 /* We will need an onion key for the router, and we
1090 * don't have one. Refuse or relax requirements. */
1091 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1092 "Requested exit point '%s' is not known. %s.",
1093 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1095 conn
->_base
.chosen_exit_optional
= 0;
1096 tor_free(conn
->chosen_exit_name
);
1105 if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
)
1106 new_circ_purpose
= CIRCUIT_PURPOSE_C_ESTABLISH_REND
;
1107 else if (desired_circuit_purpose
== CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
)
1108 new_circ_purpose
= CIRCUIT_PURPOSE_C_INTRODUCING
;
1110 new_circ_purpose
= desired_circuit_purpose
;
1113 int flags
= CIRCLAUNCH_NEED_CAPACITY
;
1114 if (want_onehop
) flags
|= CIRCLAUNCH_ONEHOP_TUNNEL
;
1115 if (need_uptime
) flags
|= CIRCLAUNCH_NEED_UPTIME
;
1116 if (need_internal
) flags
|= CIRCLAUNCH_IS_INTERNAL
;
1117 circ
= circuit_launch_by_extend_info(new_circ_purpose
, extend_info
,
1122 extend_info_free(extend_info
);
1124 if (desired_circuit_purpose
!= CIRCUIT_PURPOSE_C_GENERAL
) {
1125 /* help predict this next time */
1126 rep_hist_note_used_internal(time(NULL
), need_uptime
, 1);
1128 /* write the service_id into circ */
1129 strlcpy(circ
->rend_query
, conn
->rend_query
, sizeof(circ
->rend_query
));
1130 if (circ
->_base
.purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
&&
1131 circ
->_base
.state
== CIRCUIT_STATE_OPEN
)
1132 rend_client_rendcirc_has_opened(circ
);
1138 "No safe circuit (purpose %d) ready for edge "
1139 "connection; delaying.",
1140 desired_circuit_purpose
);
1145 /** Return true iff <b>crypt_path</b> is one of the crypt_paths for
1148 cpath_is_on_circuit(origin_circuit_t
*circ
, crypt_path_t
*crypt_path
)
1150 crypt_path_t
*cpath
, *cpath_next
= NULL
;
1151 for (cpath
= circ
->cpath
; cpath_next
!= circ
->cpath
; cpath
= cpath_next
) {
1152 cpath_next
= cpath
->next
;
1153 if (crypt_path
== cpath
)
1159 /** Attach the AP stream <b>apconn</b> to circ's linked list of
1160 * p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
1161 * hop in circ's cpath if <b>cpath</b> is NULL.
1164 link_apconn_to_circ(edge_connection_t
*apconn
, origin_circuit_t
*circ
,
1165 crypt_path_t
*cpath
)
1167 /* add it into the linked list of streams on this circuit */
1168 log_debug(LD_APP
|LD_CIRC
, "attaching new conn to circ. n_circ_id %d.",
1169 circ
->_base
.n_circ_id
);
1170 /* reset it, so we can measure circ timeouts */
1171 apconn
->_base
.timestamp_lastread
= time(NULL
);
1172 apconn
->next_stream
= circ
->p_streams
;
1173 apconn
->on_circuit
= TO_CIRCUIT(circ
);
1174 /* assert_connection_ok(conn, time(NULL)); */
1175 circ
->p_streams
= apconn
;
1177 if (cpath
) { /* we were given one; use it */
1178 tor_assert(cpath_is_on_circuit(circ
, cpath
));
1179 apconn
->cpath_layer
= cpath
;
1180 } else { /* use the last hop in the circuit */
1181 tor_assert(circ
->cpath
);
1182 tor_assert(circ
->cpath
->prev
);
1183 tor_assert(circ
->cpath
->prev
->state
== CPATH_STATE_OPEN
);
1184 apconn
->cpath_layer
= circ
->cpath
->prev
;
1188 /** If an exit wasn't specifically chosen, save the history for future
1191 consider_recording_trackhost(edge_connection_t
*conn
, origin_circuit_t
*circ
)
1193 int found_needle
= 0;
1194 or_options_t
*options
= get_options();
1197 char fp
[HEX_DIGEST_LEN
+1];
1199 /* Search the addressmap for this conn's destination. */
1200 /* If he's not in the address map.. */
1201 if (!options
->TrackHostExits
||
1202 addressmap_have_mapping(conn
->socks_request
->address
))
1203 return; /* nothing to track, or already mapped */
1205 SMARTLIST_FOREACH(options
->TrackHostExits
, const char *, cp
, {
1206 if (cp
[0] == '.') { /* match end */
1207 if (cp
[1] == '\0' ||
1208 !strcasecmpend(conn
->socks_request
->address
, cp
) ||
1209 !strcasecmp(conn
->socks_request
->address
, &cp
[1]))
1211 } else if (strcasecmp(cp
, conn
->socks_request
->address
) == 0) {
1216 if (!found_needle
|| !circ
->build_state
->chosen_exit
)
1219 /* write down the fingerprint of the chosen exit, not the nickname,
1220 * because the chosen exit might not be named. */
1221 base16_encode(fp
, sizeof(fp
),
1222 circ
->build_state
->chosen_exit
->identity_digest
, DIGEST_LEN
);
1224 /* Add this exit/hostname pair to the addressmap. */
1225 len
= strlen(conn
->socks_request
->address
) + 1 /* '.' */ +
1226 strlen(fp
) + 1 /* '.' */ +
1227 strlen("exit") + 1 /* '\0' */;
1228 new_address
= tor_malloc(len
);
1230 tor_snprintf(new_address
, len
, "%s.%s.exit",
1231 conn
->socks_request
->address
, fp
);
1233 addressmap_register(conn
->socks_request
->address
, new_address
,
1234 time(NULL
) + options
->TrackHostExitsExpire
,
1235 ADDRMAPSRC_TRACKEXIT
);
1238 /** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
1239 * begin or resolve cell as appropriate. Return values are as for
1240 * connection_ap_handshake_attach_circuit. The stream will exit from the hop
1241 * indicated by <b>cpath</b>, or from the last hop in circ's cpath if
1242 * <b>cpath</b> is NULL. */
1244 connection_ap_handshake_attach_chosen_circuit(edge_connection_t
*conn
,
1245 origin_circuit_t
*circ
,
1246 crypt_path_t
*cpath
)
1249 tor_assert(conn
->_base
.state
== AP_CONN_STATE_CIRCUIT_WAIT
||
1250 conn
->_base
.state
== AP_CONN_STATE_CONTROLLER_WAIT
);
1251 tor_assert(conn
->socks_request
);
1253 tor_assert(circ
->_base
.state
== CIRCUIT_STATE_OPEN
);
1255 conn
->_base
.state
= AP_CONN_STATE_CIRCUIT_WAIT
;
1257 if (!circ
->_base
.timestamp_dirty
)
1258 circ
->_base
.timestamp_dirty
= time(NULL
);
1260 link_apconn_to_circ(conn
, circ
, cpath
);
1261 tor_assert(conn
->socks_request
);
1262 if (conn
->socks_request
->command
== SOCKS_COMMAND_CONNECT
) {
1263 if (!conn
->use_begindir
)
1264 consider_recording_trackhost(conn
, circ
);
1265 if (connection_ap_handshake_send_begin(conn
) < 0)
1268 if (connection_ap_handshake_send_resolve(conn
) < 0)
1275 /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
1276 * we don't find one: if conn cannot be handled by any known nodes,
1277 * warn and return -1 (conn needs to die);
1278 * else launch new circuit (if necessary) and return 0.
1279 * Otherwise, associate conn with a safe live circuit, do the
1280 * right next step, and return 1.
1283 connection_ap_handshake_attach_circuit(edge_connection_t
*conn
)
1290 tor_assert(conn
->_base
.state
== AP_CONN_STATE_CIRCUIT_WAIT
);
1291 tor_assert(conn
->socks_request
);
1292 want_onehop
= conn
->want_onehop
;
1294 conn_age
= (int)(time(NULL
) - conn
->_base
.timestamp_created
);
1296 if (conn_age
>= get_options()->SocksTimeout
) {
1297 int severity
= (!conn
->_base
.addr
&& !conn
->_base
.port
) ?
1298 LOG_INFO
: LOG_NOTICE
;
1299 log_fn(severity
, LD_APP
,
1300 "Tried for %d seconds to get a connection to %s:%d. Giving up.",
1301 conn_age
, safe_str(conn
->socks_request
->address
),
1302 conn
->socks_request
->port
);
1306 if (!connection_edge_is_rendezvous_stream(conn
)) { /* we're a general conn */
1307 origin_circuit_t
*circ
=NULL
;
1309 if (conn
->chosen_exit_name
) {
1310 routerinfo_t
*router
= router_get_by_nickname(conn
->chosen_exit_name
, 1);
1311 int opt
= conn
->_base
.chosen_exit_optional
;
1312 if (!router
&& !want_onehop
) {
1313 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1314 "Requested exit point '%s' is not known. %s.",
1315 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1317 conn
->_base
.chosen_exit_optional
= 0;
1318 tor_free(conn
->chosen_exit_name
);
1323 if (router
&& !connection_ap_can_use_exit(conn
, router
)) {
1324 log_fn(opt
? LOG_INFO
: LOG_WARN
, LD_APP
,
1325 "Requested exit point '%s' would refuse request. %s.",
1326 conn
->chosen_exit_name
, opt
? "Trying others" : "Closing");
1328 conn
->_base
.chosen_exit_optional
= 0;
1329 tor_free(conn
->chosen_exit_name
);
1336 /* find the circuit that we should use, if there is one. */
1337 retval
= circuit_get_open_circ_or_launch(
1338 conn
, CIRCUIT_PURPOSE_C_GENERAL
, &circ
);
1342 log_debug(LD_APP
|LD_CIRC
,
1343 "Attaching apconn to circ %d (stream %d sec old).",
1344 circ
->_base
.n_circ_id
, conn_age
);
1345 /* print the circ's path, so people can figure out which circs are
1347 circuit_log_path(LOG_INFO
,LD_APP
|LD_CIRC
,circ
);
1349 /* We have found a suitable circuit for our conn. Hurray. */
1350 return connection_ap_handshake_attach_chosen_circuit(conn
, circ
, NULL
);
1352 } else { /* we're a rendezvous conn */
1353 origin_circuit_t
*rendcirc
=NULL
, *introcirc
=NULL
;
1355 tor_assert(!conn
->cpath_layer
);
1357 /* start by finding a rendezvous circuit for us */
1359 retval
= circuit_get_open_circ_or_launch(
1360 conn
, CIRCUIT_PURPOSE_C_REND_JOINED
, &rendcirc
);
1361 if (retval
< 0) return -1; /* failed */
1364 tor_assert(rendcirc
);
1365 /* one is already established, attach */
1367 "rend joined circ %d already here. attaching. "
1368 "(stream %d sec old)",
1369 rendcirc
->_base
.n_circ_id
, conn_age
);
1370 /* Mark rendezvous circuits as 'newly dirty' every time you use
1371 * them, since the process of rebuilding a rendezvous circ is so
1372 * expensive. There is a tradeoffs between linkability and
1373 * feasibility, at this point.
1375 rendcirc
->_base
.timestamp_dirty
= time(NULL
);
1376 link_apconn_to_circ(conn
, rendcirc
, NULL
);
1377 if (connection_ap_handshake_send_begin(conn
) < 0)
1378 return 0; /* already marked, let them fade away */
1382 if (rendcirc
&& (rendcirc
->_base
.purpose
==
1383 CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
)) {
1385 "pending-join circ %d already here, with intro ack. "
1386 "Stalling. (stream %d sec old)",
1387 rendcirc
->_base
.n_circ_id
, conn_age
);
1391 /* it's on its way. find an intro circ. */
1392 retval
= circuit_get_open_circ_or_launch(
1393 conn
, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT
, &introcirc
);
1394 if (retval
< 0) return -1; /* failed */
1397 /* one has already sent the intro. keep waiting. */
1398 tor_assert(introcirc
);
1399 log_info(LD_REND
, "Intro circ %d present and awaiting ack (rend %d). "
1400 "Stalling. (stream %d sec old)",
1401 introcirc
->_base
.n_circ_id
,
1402 rendcirc
? rendcirc
->_base
.n_circ_id
: 0,
1407 /* now rendcirc and introcirc are each either undefined or not finished */
1409 if (rendcirc
&& introcirc
&&
1410 rendcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_REND_READY
) {
1412 "ready rend circ %d already here (no intro-ack yet on "
1413 "intro %d). (stream %d sec old)",
1414 rendcirc
->_base
.n_circ_id
,
1415 introcirc
->_base
.n_circ_id
, conn_age
);
1417 tor_assert(introcirc
->_base
.purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
);
1418 if (introcirc
->_base
.state
== CIRCUIT_STATE_OPEN
) {
1419 log_info(LD_REND
,"found open intro circ %d (rend %d); sending "
1420 "introduction. (stream %d sec old)",
1421 introcirc
->_base
.n_circ_id
, rendcirc
->_base
.n_circ_id
,
1423 if (rend_client_send_introduction(introcirc
, rendcirc
) < 0) {
1426 rendcirc
->_base
.timestamp_dirty
= time(NULL
);
1427 introcirc
->_base
.timestamp_dirty
= time(NULL
);
1428 assert_circuit_ok(TO_CIRCUIT(rendcirc
));
1429 assert_circuit_ok(TO_CIRCUIT(introcirc
));
1434 log_info(LD_REND
, "Intro (%d) and rend (%d) circs are not both ready. "
1435 "Stalling conn. (%d sec old)",
1436 introcirc
? introcirc
->_base
.n_circ_id
: 0,
1437 rendcirc
? rendcirc
->_base
.n_circ_id
: 0, conn_age
);