1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
6 const char circuitbuild_c_id
[] = "$Id$";
10 * \brief The actual details of building circuits.
15 /********* START VARIABLES **********/
17 /** A global list of all circuits at this hop. */
18 extern circuit_t
*global_circuitlist
;
20 /********* END VARIABLES ************/
23 circuit_deliver_create_cell(circuit_t
*circ
, char *payload
);
24 static cpath_build_state_t
*
25 onion_new_cpath_build_state(uint8_t purpose
, const char *exit_digest
,
26 int need_uptime
, int need_capacity
, int internal
);
27 static int onion_extend_cpath(crypt_path_t
**head_ptr
,
28 cpath_build_state_t
*state
, routerinfo_t
**router_out
);
29 static int count_acceptable_routers(smartlist_t
*routers
);
31 /** Iterate over values of circ_id, starting from conn-\>next_circ_id,
32 * and with the high bit specified by circ_id_type (see
33 * decide_circ_id_type()), until we get a circ_id that is not in use
34 * by any other circuit on that conn.
36 * Return it, or 0 if can't get a unique circ_id.
38 static uint16_t get_unique_circ_id_by_conn(connection_t
*conn
) {
39 uint16_t test_circ_id
;
44 tor_assert(conn
->type
== CONN_TYPE_OR
);
45 high_bit
= (conn
->circ_id_type
== CIRC_ID_TYPE_HIGHER
) ? 1<<15 : 0;
47 /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
48 * circID such that (high_bit|test_circ_id) is not already used. */
49 test_circ_id
= conn
->next_circ_id
++;
50 if (test_circ_id
== 0 || test_circ_id
>= 1<<15) {
52 conn
->next_circ_id
= 2;
54 if (++attempts
> 1<<15) {
55 /* Make sure we don't loop forever if all circ_id's are used. This
56 * matters because it's an external DoS vulnerability.
58 log_fn(LOG_WARN
,"No unused circ IDs. Failing.");
61 test_circ_id
|= high_bit
;
62 } while (circuit_get_by_circ_id_conn(test_circ_id
, conn
));
66 /** If <b>verbose</b> is false, allocate and return a comma-separated
67 * list of the currently built elements of circuit_t. If
68 * <b>verbose</b> is true, also list information about link status in
69 * a more verbose format using spaces.
72 circuit_list_path(circuit_t
*circ
, int verbose
)
74 struct crypt_path_t
*hop
;
75 smartlist_t
*elements
;
76 const char *states
[] = {"closed", "waiting for keys", "open"};
79 tor_assert(CIRCUIT_IS_ORIGIN(circ
));
80 tor_assert(circ
->cpath
);
82 elements
= smartlist_create();
85 tor_snprintf(buf
, sizeof(buf
)-1, "%s%s circ (length %d, exit %s):",
86 circ
->build_state
->is_internal
? "internal" : "exit",
87 circ
->build_state
->need_uptime
? " (high-uptime)" : "",
88 circ
->build_state
->desired_path_len
,
89 circ
->build_state
->chosen_exit_name
);
90 smartlist_add(elements
, tor_strdup(buf
));
99 if (!verbose
&& hop
->state
!= CPATH_STATE_OPEN
)
101 if ((r
= router_get_by_digest(hop
->identity_digest
))) {
103 } else if (circ
->purpose
== CIRCUIT_PURPOSE_C_REND_JOINED
) {
104 elt
= "<rendezvous splice>";
107 base16_encode(buf
+1,sizeof(buf
)-1,hop
->identity_digest
,DIGEST_LEN
);
111 size_t len
= strlen(elt
)+2+strlen(states
[hop
->state
])+1;
112 char *v
= tor_malloc(len
);
113 tor_assert(hop
->state
<= 2);
114 tor_snprintf(v
,len
,"%s(%s)",elt
,states
[hop
->state
]);
115 smartlist_add(elements
, v
);
117 smartlist_add(elements
, tor_strdup(elt
));
120 } while (hop
!= circ
->cpath
);
122 s
= smartlist_join_strings(elements
, verbose
?" ":",", 0, NULL
);
123 SMARTLIST_FOREACH(elements
, char*, cp
, tor_free(cp
));
124 smartlist_free(elements
);
128 /** Log, at severity <b>severity</b>, the nicknames of each router in
129 * circ's cpath. Also log the length of the cpath, and the intended
132 void circuit_log_path(int severity
, circuit_t
*circ
) {
133 char *s
= circuit_list_path(circ
,1);
134 log_fn(severity
,"%s",s
);
138 /** Tell the rep(utation)hist(ory) module about the status of the links
139 * in circ. Hops that have become OPEN are marked as successfully
140 * extended; the _first_ hop that isn't open (if any) is marked as
143 void circuit_rep_hist_note_result(circuit_t
*circ
) {
144 struct crypt_path_t
*hop
;
145 char *prev_digest
= NULL
;
146 routerinfo_t
*router
;
150 * if !hop, then we're not the beginning of this circuit.
151 * for now, just forget about it. later, we should remember when
152 * extends-through-us failed, too.
156 if (server_mode(get_options())) {
157 routerinfo_t
*me
= router_get_my_routerinfo();
159 prev_digest
= me
->identity_digest
;
162 router
= router_get_by_digest(hop
->identity_digest
);
165 if (hop
->state
== CPATH_STATE_OPEN
)
166 rep_hist_note_extend_succeeded(prev_digest
, router
->identity_digest
);
168 rep_hist_note_extend_failed(prev_digest
, router
->identity_digest
);
172 prev_digest
= router
->identity_digest
;
177 } while (hop
!=circ
->cpath
);
180 /** A helper function for circuit_dump_by_conn() below. Log a bunch
181 * of information about circuit <b>circ</b>.
184 circuit_dump_details(int severity
, circuit_t
*circ
, int poll_index
,
185 const char *type
, int this_circid
, int other_circid
) {
186 log(severity
,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d:",
187 poll_index
, type
, this_circid
, other_circid
, circ
->state
,
188 circuit_state_to_string
[circ
->state
], (int)circ
->timestamp_created
);
189 if (CIRCUIT_IS_ORIGIN(circ
)) { /* circ starts at this node */
190 circuit_log_path(severity
, circ
);
194 /** Log, at severity <b>severity</b>, information about each circuit
195 * that is connected to <b>conn</b>.
197 void circuit_dump_by_conn(connection_t
*conn
, int severity
) {
199 connection_t
*tmpconn
;
201 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
202 if (circ
->marked_for_close
)
204 if (circ
->p_conn
== conn
)
205 circuit_dump_details(severity
, circ
, conn
->poll_index
, "App-ward",
206 circ
->p_circ_id
, circ
->n_circ_id
);
207 for (tmpconn
=circ
->p_streams
; tmpconn
; tmpconn
=tmpconn
->next_stream
) {
208 if (tmpconn
== conn
) {
209 circuit_dump_details(severity
, circ
, conn
->poll_index
, "App-ward",
210 circ
->p_circ_id
, circ
->n_circ_id
);
213 if (circ
->n_conn
== conn
)
214 circuit_dump_details(severity
, circ
, conn
->poll_index
, "Exit-ward",
215 circ
->n_circ_id
, circ
->p_circ_id
);
216 for (tmpconn
=circ
->n_streams
; tmpconn
; tmpconn
=tmpconn
->next_stream
) {
217 if (tmpconn
== conn
) {
218 circuit_dump_details(severity
, circ
, conn
->poll_index
, "Exit-ward",
219 circ
->n_circ_id
, circ
->p_circ_id
);
222 if (!circ
->n_conn
&& circ
->n_addr
&& circ
->n_port
&&
223 circ
->n_addr
== conn
->addr
&&
224 circ
->n_port
== conn
->port
&&
225 !memcmp(conn
->identity_digest
, circ
->n_conn_id_digest
, DIGEST_LEN
)) {
226 circuit_dump_details(severity
, circ
, conn
->poll_index
, "Pending",
227 circ
->n_circ_id
, circ
->p_circ_id
);
232 /** Build a new circuit for <b>purpose</b>. If <b>exit_digest</b>
233 * is defined, then use that as your exit router, else choose a suitable
236 * Also launch a connection to the first OR in the chosen path, if
237 * it's not open already.
240 circuit_establish_circuit(uint8_t purpose
, const char *exit_digest
,
241 int need_uptime
, int need_capacity
, int internal
) {
242 routerinfo_t
*firsthop
;
243 connection_t
*n_conn
;
246 circ
= circuit_new(0, NULL
); /* sets circ->p_circ_id and circ->p_conn */
247 circ
->state
= CIRCUIT_STATE_OR_WAIT
;
248 circ
->build_state
= onion_new_cpath_build_state(purpose
, exit_digest
,
249 need_uptime
, need_capacity
, internal
);
250 circ
->purpose
= purpose
;
252 if (! circ
->build_state
) {
253 log_fn(LOG_INFO
,"Generating cpath failed.");
254 circuit_mark_for_close(circ
);
258 if (onion_extend_cpath(&circ
->cpath
, circ
->build_state
, &firsthop
)<0 ||
259 !CIRCUIT_IS_ORIGIN(circ
)) {
260 log_fn(LOG_INFO
,"Generating first cpath hop failed.");
261 circuit_mark_for_close(circ
);
265 control_event_circuit_status(circ
, CIRC_EVENT_LAUNCHED
);
267 /* now see if we're already connected to the first OR in 'route' */
269 tor_assert(firsthop
);
270 log_fn(LOG_DEBUG
,"Looking for firsthop '%s:%u'",
271 firsthop
->address
,firsthop
->or_port
);
272 /* imprint the circuit with its future n_conn->id */
273 memcpy(circ
->n_conn_id_digest
, firsthop
->identity_digest
, DIGEST_LEN
);
274 n_conn
= connection_get_by_identity_digest(firsthop
->identity_digest
,
276 if (!n_conn
|| n_conn
->state
!= OR_CONN_STATE_OPEN
) { /* not currently connected */
277 circ
->n_addr
= firsthop
->addr
;
278 circ
->n_port
= firsthop
->or_port
;
280 if (!n_conn
) { /* launch the connection */
281 n_conn
= connection_or_connect(firsthop
->addr
, firsthop
->or_port
,
282 firsthop
->identity_digest
);
283 if (!n_conn
) { /* connect failed, forget the whole thing */
284 log_fn(LOG_INFO
,"connect to firsthop failed. Closing.");
285 circuit_mark_for_close(circ
);
290 log_fn(LOG_DEBUG
,"connecting in progress (or finished). Good.");
291 /* return success. The onion/circuit/etc will be taken care of automatically
292 * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
295 } else { /* it's already open. use it. */
296 circ
->n_addr
= n_conn
->addr
;
297 circ
->n_port
= n_conn
->port
;
298 circ
->n_conn
= n_conn
;
299 log_fn(LOG_DEBUG
,"Conn open. Delivering first onion skin.");
300 if (circuit_send_next_onion_skin(circ
) < 0) {
301 log_fn(LOG_INFO
,"circuit_send_next_onion_skin failed.");
302 circuit_mark_for_close(circ
);
309 /** Find circuits that are waiting on <b>or_conn</b> to become open,
310 * if any, and get them to send their create cells forward.
312 * Status is 1 if connect succeeded, or 0 if connect failed.
314 void circuit_n_conn_done(connection_t
*or_conn
, int status
) {
317 log_fn(LOG_DEBUG
,"or_conn to %s, status=%d", or_conn
->nickname
, status
);
319 for (circ
=global_circuitlist
;circ
;circ
= circ
->next
) {
320 if (circ
->marked_for_close
)
323 circ
->n_addr
== or_conn
->addr
&&
324 circ
->n_port
== or_conn
->port
&&
325 !memcmp(or_conn
->identity_digest
, circ
->n_conn_id_digest
, DIGEST_LEN
)) {
326 tor_assert(circ
->state
== CIRCUIT_STATE_OR_WAIT
);
327 if (!status
) { /* or_conn failed; close circ */
328 log_fn(LOG_INFO
,"or_conn failed. Closing circ.");
329 circuit_mark_for_close(circ
);
332 log_fn(LOG_DEBUG
,"Found circ %d, sending create cell.", circ
->n_circ_id
);
333 circ
->n_conn
= or_conn
;
334 memcpy(circ
->n_conn_id_digest
, or_conn
->identity_digest
, DIGEST_LEN
);
335 if (CIRCUIT_IS_ORIGIN(circ
)) {
336 if (circuit_send_next_onion_skin(circ
) < 0) {
337 log_fn(LOG_INFO
,"send_next_onion_skin failed; circuit marked for closing.");
338 circuit_mark_for_close(circ
);
340 /* XXX could this be bad, eg if next_onion_skin failed because conn died? */
343 /* pull the create cell out of circ->onionskin, and send it */
344 if (circuit_deliver_create_cell(circ
, circ
->onionskin
) < 0) {
345 circuit_mark_for_close(circ
);
354 circuit_deliver_create_cell(circuit_t
*circ
, char *payload
) {
358 tor_assert(circ
->n_conn
);
359 tor_assert(circ
->n_conn
->type
== CONN_TYPE_OR
);
362 circ
->n_circ_id
= get_unique_circ_id_by_conn(circ
->n_conn
);
363 if (!circ
->n_circ_id
) {
364 log_fn(LOG_WARN
,"failed to get unique circID.");
367 log_fn(LOG_DEBUG
,"Chosen circID %u.",circ
->n_circ_id
);
369 memset(&cell
, 0, sizeof(cell_t
));
370 cell
.command
= CELL_CREATE
;
371 cell
.circ_id
= circ
->n_circ_id
;
373 memcpy(cell
.payload
, payload
, ONIONSKIN_CHALLENGE_LEN
);
374 connection_or_write_cell_to_buf(&cell
, circ
->n_conn
);
378 extern int has_completed_circuit
;
380 /** This is the backbone function for building circuits.
382 * If circ's first hop is closed, then we need to build a create
383 * cell and send it forward.
385 * Otherwise, we need to build a relay extend cell and send it
388 * Return -1 if we want to tear down circ, else return 0.
390 int circuit_send_next_onion_skin(circuit_t
*circ
) {
392 routerinfo_t
*router
;
394 char payload
[2+4+DIGEST_LEN
+ONIONSKIN_CHALLENGE_LEN
];
399 tor_assert(CIRCUIT_IS_ORIGIN(circ
));
401 if (circ
->cpath
->state
== CPATH_STATE_CLOSED
) {
402 log_fn(LOG_DEBUG
,"First skin; sending create cell.");
404 router
= router_get_by_digest(circ
->n_conn
->identity_digest
);
406 log_fn(LOG_WARN
,"Couldn't find routerinfo for %s",
407 circ
->n_conn
->nickname
);
411 if (onion_skin_create(router
->onion_pkey
,
412 &(circ
->cpath
->handshake_state
),
414 log_fn(LOG_WARN
,"onion_skin_create (first hop) failed.");
418 if (circuit_deliver_create_cell(circ
, payload
) < 0)
421 circ
->cpath
->state
= CPATH_STATE_AWAITING_KEYS
;
422 circ
->state
= CIRCUIT_STATE_BUILDING
;
423 log_fn(LOG_DEBUG
,"first skin; finished sending create cell.");
425 tor_assert(circ
->cpath
->state
== CPATH_STATE_OPEN
);
426 tor_assert(circ
->state
== CIRCUIT_STATE_BUILDING
);
427 log_fn(LOG_DEBUG
,"starting to send subsequent skin.");
428 r
= onion_extend_cpath(&circ
->cpath
, circ
->build_state
, &router
);
430 /* done building the circuit. whew. */
431 circ
->state
= CIRCUIT_STATE_OPEN
;
432 log_fn(LOG_INFO
,"circuit built!");
433 circuit_reset_failure_count(0);
434 if (!has_completed_circuit
) {
435 has_completed_circuit
=1;
436 log_fn(LOG_NOTICE
,"Tor has successfully opened a circuit. Looks like it's working.");
437 /* XXX009 Log a count of known routers here */
439 circuit_rep_hist_note_result(circ
);
440 circuit_has_opened(circ
); /* do other actions as necessary */
443 log_fn(LOG_INFO
,"Unable to extend circuit path.");
446 hop
= circ
->cpath
->prev
;
448 *(uint32_t*)payload
= htonl(hop
->addr
);
449 *(uint16_t*)(payload
+4) = htons(hop
->port
);
451 onionskin
= payload
+2+4;
452 memcpy(payload
+2+4+ONIONSKIN_CHALLENGE_LEN
, hop
->identity_digest
, DIGEST_LEN
);
453 payload_len
= 2+4+ONIONSKIN_CHALLENGE_LEN
+DIGEST_LEN
;
455 if (onion_skin_create(router
->onion_pkey
, &(hop
->handshake_state
), onionskin
) < 0) {
456 log_fn(LOG_WARN
,"onion_skin_create failed.");
460 log_fn(LOG_DEBUG
,"Sending extend relay cell.");
461 /* send it to hop->prev, because it will transfer
462 * it to a create cell and then send to hop */
463 if (connection_edge_send_command(NULL
, circ
, RELAY_COMMAND_EXTEND
,
464 payload
, payload_len
, hop
->prev
) < 0)
465 return 0; /* circuit is closed */
467 hop
->state
= CPATH_STATE_AWAITING_KEYS
;
472 /** Take the 'extend' cell, pull out addr/port plus the onion skin. Make
473 * sure we're connected to the next hop, and pass it the onion skin in
476 int circuit_extend(cell_t
*cell
, circuit_t
*circ
) {
477 connection_t
*n_conn
;
480 char *id_digest
=NULL
;
481 routerinfo_t
*router
;
484 log_fn(LOG_WARN
,"n_conn already set. Bug/attack. Closing.");
488 relay_header_unpack(&rh
, cell
->payload
);
490 if (rh
.length
< 4+2+ONIONSKIN_CHALLENGE_LEN
+DIGEST_LEN
) {
491 log_fn(LOG_WARN
, "Wrong length %d on extend cell. Closing circuit.", rh
.length
);
495 circ
->n_addr
= ntohl(get_uint32(cell
->payload
+RELAY_HEADER_SIZE
));
496 circ
->n_port
= ntohs(get_uint16(cell
->payload
+RELAY_HEADER_SIZE
+4));
498 onionskin
= cell
->payload
+RELAY_HEADER_SIZE
+4+2;
499 id_digest
= cell
->payload
+RELAY_HEADER_SIZE
+4+2+ONIONSKIN_CHALLENGE_LEN
;
500 n_conn
= connection_get_by_identity_digest(id_digest
, CONN_TYPE_OR
);
502 if (!n_conn
|| n_conn
->state
!= OR_CONN_STATE_OPEN
) {
503 /* Note that this will close circuits where the onion has the same
504 * router twice in a row in the path. I think that's ok.
507 in
.s_addr
= htonl(circ
->n_addr
);
508 log_fn(LOG_INFO
,"Next router (%s:%d) not connected. Connecting.",
509 inet_ntoa(in
), circ
->n_port
);
511 router
= router_get_by_digest(id_digest
);
513 memcpy(circ
->onionskin
, onionskin
, ONIONSKIN_CHALLENGE_LEN
);
514 circ
->state
= CIRCUIT_STATE_OR_WAIT
;
516 /* imprint the circuit with its future n_conn->id */
517 memcpy(circ
->n_conn_id_digest
, id_digest
, DIGEST_LEN
);
520 circ
->n_addr
= n_conn
->addr
;
521 circ
->n_port
= n_conn
->port
;
523 /* we should try to open a connection */
524 n_conn
= connection_or_connect(circ
->n_addr
, circ
->n_port
, id_digest
);
526 log_fn(LOG_INFO
,"Launching n_conn failed. Closing.");
529 log_fn(LOG_DEBUG
,"connecting in progress (or finished). Good.");
531 /* return success. The onion/circuit/etc will be taken care of automatically
532 * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
537 /* these may be different if the router connected to us from elsewhere */
538 circ
->n_addr
= n_conn
->addr
;
539 circ
->n_port
= n_conn
->port
;
541 circ
->n_conn
= n_conn
;
542 memcpy(circ
->n_conn_id_digest
, n_conn
->identity_digest
, DIGEST_LEN
);
543 log_fn(LOG_DEBUG
,"n_conn is %s:%u",n_conn
->address
,n_conn
->port
);
545 if (circuit_deliver_create_cell(circ
, onionskin
) < 0)
550 /** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in
551 * key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
553 * - 20 to initialize f_digest
554 * - 20 to initialize b_digest
555 * - 16 to key f_crypto
556 * - 16 to key b_crypto
558 * (If 'reverse' is true, then f_XX and b_XX are swapped.)
560 int circuit_init_cpath_crypto(crypt_path_t
*cpath
, char *key_data
, int reverse
)
562 crypto_digest_env_t
*tmp_digest
;
563 crypto_cipher_env_t
*tmp_crypto
;
566 tor_assert(key_data
);
567 tor_assert(!(cpath
->f_crypto
|| cpath
->b_crypto
||
568 cpath
->f_digest
|| cpath
->b_digest
));
570 // log_fn(LOG_DEBUG,"hop init digest forward 0x%.8x, backward 0x%.8x.",
571 // (unsigned int)*(uint32_t*)key_data, (unsigned int)*(uint32_t*)(key_data+20));
572 cpath
->f_digest
= crypto_new_digest_env();
573 crypto_digest_add_bytes(cpath
->f_digest
, key_data
, DIGEST_LEN
);
574 cpath
->b_digest
= crypto_new_digest_env();
575 crypto_digest_add_bytes(cpath
->b_digest
, key_data
+DIGEST_LEN
, DIGEST_LEN
);
577 // log_fn(LOG_DEBUG,"hop init cipher forward 0x%.8x, backward 0x%.8x.",
578 // (unsigned int)*(uint32_t*)(key_data+40), (unsigned int)*(uint32_t*)(key_data+40+16));
579 if (!(cpath
->f_crypto
=
580 crypto_create_init_cipher(key_data
+(2*DIGEST_LEN
),1))) {
581 log(LOG_WARN
,"Bug: forward cipher initialization failed.");
584 if (!(cpath
->b_crypto
=
585 crypto_create_init_cipher(key_data
+(2*DIGEST_LEN
)+CIPHER_KEY_LEN
,0))) {
586 log(LOG_WARN
,"Bug: backward cipher initialization failed.");
591 tmp_digest
= cpath
->f_digest
;
592 cpath
->f_digest
= cpath
->b_digest
;
593 cpath
->b_digest
= tmp_digest
;
594 tmp_crypto
= cpath
->f_crypto
;
595 cpath
->f_crypto
= cpath
->b_crypto
;
596 cpath
->b_crypto
= tmp_crypto
;
602 /** A created or extended cell came back to us on the circuit,
603 * and it included <b>reply</b> (the second DH key, plus KH).
605 * Calculate the appropriate keys and digests, make sure KH is
606 * correct, and initialize this hop of the cpath.
608 * Return -1 if we want to mark circ for close, else return 0.
610 int circuit_finish_handshake(circuit_t
*circ
, char *reply
) {
611 unsigned char keys
[CPATH_KEY_MATERIAL_LEN
];
614 tor_assert(CIRCUIT_IS_ORIGIN(circ
));
615 if (circ
->cpath
->state
== CPATH_STATE_AWAITING_KEYS
)
618 for (hop
=circ
->cpath
->next
;
619 hop
!= circ
->cpath
&& hop
->state
== CPATH_STATE_OPEN
;
621 if (hop
== circ
->cpath
) { /* got an extended when we're all done? */
622 log_fn(LOG_WARN
,"got extended when circ already built? Closing.");
626 tor_assert(hop
->state
== CPATH_STATE_AWAITING_KEYS
);
628 if (onion_skin_client_handshake(hop
->handshake_state
, reply
, keys
,
629 DIGEST_LEN
*2+CIPHER_KEY_LEN
*2) < 0) {
630 log_fn(LOG_WARN
,"onion_skin_client_handshake failed.");
634 crypto_dh_free(hop
->handshake_state
); /* don't need it anymore */
635 hop
->handshake_state
= NULL
;
636 /* Remember hash of g^xy */
637 memcpy(hop
->handshake_digest
, reply
+DH_KEY_LEN
, DIGEST_LEN
);
639 if (circuit_init_cpath_crypto(hop
, keys
, 0)<0) {
643 hop
->state
= CPATH_STATE_OPEN
;
644 log_fn(LOG_INFO
,"Finished building circuit:");
645 circuit_log_path(LOG_INFO
,circ
);
646 control_event_circuit_status(circ
, CIRC_EVENT_EXTENDED
);
651 /** We received a relay truncated cell on circ.
653 * Since we don't ask for truncates currently, getting a truncated
654 * means that a connection broke or an extend failed. For now,
655 * just give up: for circ to close, and return 0.
657 int circuit_truncated(circuit_t
*circ
, crypt_path_t
*layer
) {
658 // crypt_path_t *victim;
659 // connection_t *stream;
662 tor_assert(CIRCUIT_IS_ORIGIN(circ
));
665 /* XXX Since we don't ask for truncates currently, getting a truncated
666 * means that a connection broke or an extend failed. For now,
669 circuit_mark_for_close(circ
);
673 while (layer
->next
!= circ
->cpath
) {
674 /* we need to clear out layer->next */
675 victim
= layer
->next
;
676 log_fn(LOG_DEBUG
, "Killing a layer of the cpath.");
678 for (stream
= circ
->p_streams
; stream
; stream
=stream
->next_stream
) {
679 if (stream
->cpath_layer
== victim
) {
680 log_fn(LOG_INFO
, "Marking stream %d for close.", stream
->stream_id
);
681 /* no need to send 'end' relay cells,
682 * because the other side's already dead
684 stream
->has_sent_end
= 1;
685 connection_mark_for_close(stream
);
689 layer
->next
= victim
->next
;
690 circuit_free_cpath_node(victim
);
693 log_fn(LOG_INFO
, "finished");
698 /** Given a response payload and keys, initialize, then send a created
701 int onionskin_answer(circuit_t
*circ
, unsigned char *payload
, unsigned char *keys
) {
703 crypt_path_t
*tmp_cpath
;
705 tmp_cpath
= tor_malloc_zero(sizeof(crypt_path_t
));
707 memset(&cell
, 0, sizeof(cell_t
));
708 cell
.command
= CELL_CREATED
;
709 cell
.circ_id
= circ
->p_circ_id
;
711 circ
->state
= CIRCUIT_STATE_OPEN
;
713 log_fn(LOG_DEBUG
,"Entering.");
715 memcpy(cell
.payload
, payload
, ONIONSKIN_REPLY_LEN
);
717 log_fn(LOG_INFO
,"init digest forward 0x%.8x, backward 0x%.8x.",
718 (unsigned int)*(uint32_t*)(keys
), (unsigned int)*(uint32_t*)(keys
+20));
719 if (circuit_init_cpath_crypto(tmp_cpath
, keys
, 0)<0) {
720 log_fn(LOG_WARN
,"Circuit initialization failed");
724 circ
->n_digest
= tmp_cpath
->f_digest
;
725 circ
->n_crypto
= tmp_cpath
->f_crypto
;
726 circ
->p_digest
= tmp_cpath
->b_digest
;
727 circ
->p_crypto
= tmp_cpath
->b_crypto
;
730 memcpy(circ
->handshake_digest
, cell
.payload
+DH_KEY_LEN
, DIGEST_LEN
);
732 connection_or_write_cell_to_buf(&cell
, circ
->p_conn
);
733 log_fn(LOG_DEBUG
,"Finished sending 'created' cell.");
738 /** Choose a length for a circuit of purpose <b>purpose</b>.
739 * Default length is 3 + the number of endpoints that would give something
740 * away. If the routerlist <b>routers</b> doesn't have enough routers
741 * to handle the desired path length, return as large a path length as
742 * is feasible, except if it's less than 2, in which case return -1.
744 static int new_route_len(double cw
, uint8_t purpose
, smartlist_t
*routers
) {
745 int num_acceptable_routers
;
748 tor_assert(cw
>= 0.);
755 if (purpose
== CIRCUIT_PURPOSE_C_GENERAL
)
757 else if (purpose
== CIRCUIT_PURPOSE_C_INTRODUCING
)
759 else if (purpose
== CIRCUIT_PURPOSE_C_ESTABLISH_REND
)
761 else if (purpose
== CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
)
763 else if (purpose
== CIRCUIT_PURPOSE_S_CONNECT_REND
)
766 log_fn(LOG_WARN
,"Bug: unhandled purpose %d", purpose
);
774 for (routelen
= 3; ; routelen
++) { /* 3, increment until coinflip says we're done */
775 if (crypto_pseudo_rand_int(255) >= cw
*255) /* don't extend */
779 log_fn(LOG_DEBUG
,"Chosen route length %d (%d routers available).",routelen
,
780 smartlist_len(routers
));
782 num_acceptable_routers
= count_acceptable_routers(routers
);
784 if (num_acceptable_routers
< 2) {
785 log_fn(LOG_INFO
,"Not enough acceptable routers (%d). Discarding this circuit.",
786 num_acceptable_routers
);
790 if (num_acceptable_routers
< routelen
) {
791 log_fn(LOG_INFO
,"Not enough routers: cutting routelen from %d to %d.",
792 routelen
, num_acceptable_routers
);
793 routelen
= num_acceptable_routers
;
799 /** Fetch the list of predicted ports, dup it into a smartlist of
800 * uint16_t's, remove the ones that are already handled by an
801 * existing circuit, and return it.
804 circuit_get_unhandled_ports(time_t now
) {
805 smartlist_t
*source
= rep_hist_get_predicted_ports(now
);
806 smartlist_t
*dest
= smartlist_create();
810 for (i
= 0; i
< smartlist_len(source
); ++i
) {
811 tmp
= tor_malloc(sizeof(uint16_t));
812 memcpy(tmp
, smartlist_get(source
, i
), sizeof(uint16_t));
813 smartlist_add(dest
, tmp
);
816 circuit_remove_handled_ports(dest
);
820 /** Return 1 if we already have circuits present or on the way for
821 * all anticipated ports. Return 0 if we should make more.
823 * If we're returning 0, set need_uptime and need_capacity to
824 * indicate any requirements that the unhandled ports have.
827 circuit_all_predicted_ports_handled(time_t now
, int *need_uptime
,
828 int *need_capacity
) {
831 smartlist_t
*sl
= circuit_get_unhandled_ports(now
);
832 smartlist_t
*LongLivedServices
= get_options()->LongLivedPorts
;
833 enough
= (smartlist_len(sl
) == 0);
834 for (i
= 0; i
< smartlist_len(sl
); ++i
) {
835 port
= smartlist_get(sl
, i
);
836 if (smartlist_string_num_isin(LongLivedServices
, *port
))
844 /** Return 1 if <b>router</b> can handle one or more of the ports in
845 * <b>needed_ports</b>, else return 0.
848 router_handles_some_port(routerinfo_t
*router
, smartlist_t
*needed_ports
) {
852 for (i
= 0; i
< smartlist_len(needed_ports
); ++i
) {
853 port
= *(uint16_t *)smartlist_get(needed_ports
, i
);
855 if (router_compare_addr_to_addr_policy(0, port
, router
->exit_policy
) !=
856 ADDR_POLICY_REJECTED
)
862 /** How many circuits do we want simultaneously in-progress to handle
865 #define MIN_CIRCUITS_HANDLING_STREAM 2
868 ap_stream_wants_exit_attention(connection_t
*conn
) {
869 if (conn
->type
== CONN_TYPE_AP
&&
870 conn
->state
== AP_CONN_STATE_CIRCUIT_WAIT
&&
871 !conn
->marked_for_close
&&
872 !connection_edge_is_rendezvous_stream(conn
) &&
873 !circuit_stream_is_being_handled(conn
, 0, MIN_CIRCUITS_HANDLING_STREAM
))
878 /** Return a pointer to a suitable router to be the exit node for the
879 * general-purpose circuit we're about to build.
881 * Look through the connection array, and choose a router that maximizes
882 * the number of pending streams that can exit from this router.
884 * Return NULL if we can't find any suitable routers.
886 static routerinfo_t
*
887 choose_good_exit_server_general(routerlist_t
*dir
, int need_uptime
,
892 int n_pending_connections
= 0;
893 connection_t
**carray
;
895 int best_support
= -1;
896 int n_best_support
=0;
897 smartlist_t
*sl
, *preferredexits
, *preferredentries
, *excludedexits
;
898 routerinfo_t
*router
;
899 or_options_t
*options
= get_options();
901 preferredentries
= smartlist_create();
902 add_nickname_list_to_smartlist(preferredentries
,options
->EntryNodes
,1);
904 get_connection_array(&carray
, &n_connections
);
906 /* Count how many connections are waiting for a circuit to be built.
907 * We use this for log messages now, but in the future we may depend on it.
909 for (i
= 0; i
< n_connections
; ++i
) {
910 if (ap_stream_wants_exit_attention(carray
[i
]))
911 ++n_pending_connections
;
913 // log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
914 // n_pending_connections);
915 /* Now we count, for each of the routers in the directory, how many
916 * of the pending connections could possibly exit from that
917 * router (n_supported[i]). (We can't be sure about cases where we
918 * don't know the IP address of the pending connection.)
920 n_supported
= tor_malloc(sizeof(int)*smartlist_len(dir
->routers
));
921 for (i
= 0; i
< smartlist_len(dir
->routers
); ++i
) { /* iterate over routers */
922 router
= smartlist_get(dir
->routers
, i
);
923 if (router_is_me(router
)) {
925 // log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
926 /* XXX there's probably a reverse predecessor attack here, but
927 * it's slow. should we take this out? -RD
931 if (!router
->is_running
) {
933 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- directory says it's not running.",
934 // router->nickname, i);
935 continue; /* skip routers that are known to be down */
937 if (router_is_unreliable(router
, need_uptime
, need_capacity
)) {
939 continue; /* skip routers that are not suitable */
941 if (!router
->is_verified
&&
942 (!(options
->_AllowUnverified
& ALLOW_UNVERIFIED_EXIT
) ||
943 router_is_unreliable(router
, 1, 1))) {
944 /* if it's unverified, and either we don't want it or it's unsuitable */
946 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- unverified router.",
947 // router->nickname, i);
948 continue; /* skip unverified routers */
950 if (router_exit_policy_rejects_all(router
)) {
952 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
953 // router->nickname, i);
954 continue; /* skip routers that reject all */
956 if (smartlist_len(preferredentries
)==1 &&
957 router
== (routerinfo_t
*)smartlist_get(preferredentries
, 0)) {
959 // log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it's our only preferred entry node.", router->nickname, i);
963 for (j
= 0; j
< n_connections
; ++j
) { /* iterate over connections */
964 if (!ap_stream_wants_exit_attention(carray
[j
]))
965 continue; /* Skip everything but APs in CIRCUIT_WAIT */
966 if (connection_ap_can_use_exit(carray
[j
], router
)) {
968 // log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
969 // router->nickname, i, n_supported[i]);
971 // log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
972 // router->nickname, i);
974 } /* End looping over connections. */
975 if (n_supported
[i
] > best_support
) {
976 /* If this router is better than previous ones, remember its index
977 * and goodness, and start counting how many routers are this good. */
978 best_support
= n_supported
[i
]; n_best_support
=1;
979 // log_fn(LOG_DEBUG,"%s is new best supported option so far.",
980 // router->nickname);
981 } else if (n_supported
[i
] == best_support
) {
982 /* If this router is _as good_ as the best one, just increment the
983 * count of equally good routers.*/
987 log_fn(LOG_INFO
, "Found %d servers that might support %d/%d pending connections.",
988 n_best_support
, best_support
, n_pending_connections
);
990 preferredexits
= smartlist_create();
991 add_nickname_list_to_smartlist(preferredexits
,options
->ExitNodes
,1);
993 excludedexits
= smartlist_create();
994 add_nickname_list_to_smartlist(excludedexits
,options
->ExcludeNodes
,0);
996 sl
= smartlist_create();
998 /* If any routers definitely support any pending connections, choose one
1000 if (best_support
> 0) {
1001 for (i
= 0; i
< smartlist_len(dir
->routers
); i
++)
1002 if (n_supported
[i
] == best_support
)
1003 smartlist_add(sl
, smartlist_get(dir
->routers
, i
));
1005 smartlist_subtract(sl
,excludedexits
);
1006 if (options
->StrictExitNodes
|| smartlist_overlap(sl
,preferredexits
))
1007 smartlist_intersect(sl
,preferredexits
);
1008 router
= routerlist_sl_choose_by_bandwidth(sl
);
1010 /* Either there are no pending connections, or no routers even seem to
1011 * possibly support any of them. Choose a router at random that satisfies
1012 * at least one predicted exit port. */
1015 smartlist_t
*needed_ports
= circuit_get_unhandled_ports(time(NULL
));
1017 if (best_support
== -1) {
1018 log(LOG_NOTICE
, "All routers are down or middleman -- choosing a doomed exit at random.");
1020 for (try = 0; try < 2; try++) {
1021 /* try once to pick only from routers that satisfy a needed port,
1022 * then if there are none, pick from any that support exiting. */
1023 for (i
= 0; i
< smartlist_len(dir
->routers
); i
++) {
1024 router
= smartlist_get(dir
->routers
, i
);
1025 if (n_supported
[i
] != -1 &&
1026 (try || router_handles_some_port(router
, needed_ports
))) {
1027 log_fn(LOG_DEBUG
,"Try %d: '%s' is a possibility.", try, router
->nickname
);
1028 smartlist_add(sl
, router
);
1032 smartlist_subtract(sl
,excludedexits
);
1033 if (options
->StrictExitNodes
|| smartlist_overlap(sl
,preferredexits
))
1034 smartlist_intersect(sl
,preferredexits
);
1035 router
= routerlist_sl_choose_by_bandwidth(sl
);
1039 SMARTLIST_FOREACH(needed_ports
, uint16_t *, cp
, tor_free(cp
));
1040 smartlist_free(needed_ports
);
1043 smartlist_free(preferredexits
);
1044 smartlist_free(preferredentries
);
1045 smartlist_free(excludedexits
);
1047 tor_free(n_supported
);
1049 log_fn(LOG_INFO
, "Chose exit server '%s'", router
->nickname
);
1052 if (options
->StrictExitNodes
)
1053 log_fn(LOG_WARN
, "No exit routers seem to be running; can't choose an exit.");
1058 /** Return a pointer to a suitable router to be the exit node for the
1059 * circuit of purpose <b>purpose</b> that we're about to build (or NULL
1060 * if no router is suitable).
1062 * For general-purpose circuits, pass it off to
1063 * choose_good_exit_server_general()
1065 * For client-side rendezvous circuits, choose a random node, weighted
1066 * toward the preferences in 'options'.
1068 static routerinfo_t
*
1069 choose_good_exit_server(uint8_t purpose
, routerlist_t
*dir
,
1070 int need_uptime
, int need_capacity
)
1073 or_options_t
*options
= get_options();
1075 case CIRCUIT_PURPOSE_C_GENERAL
:
1076 return choose_good_exit_server_general(dir
, need_uptime
, need_capacity
);
1077 case CIRCUIT_PURPOSE_C_ESTABLISH_REND
:
1078 r
= router_choose_random_node(options
->RendNodes
, options
->RendExcludeNodes
,
1079 NULL
, need_uptime
, need_capacity
,
1080 options
->_AllowUnverified
& ALLOW_UNVERIFIED_RENDEZVOUS
, 0);
1083 log_fn(LOG_WARN
,"Bug: unhandled purpose %d", purpose
);
1090 /** Allocate a cpath_build_state_t, populate it based on
1091 * <b>purpose</b> and <b>exit_digest</b> (if specified), and
1094 static cpath_build_state_t
*
1095 onion_new_cpath_build_state(uint8_t purpose
, const char *exit_digest
,
1096 int need_uptime
, int need_capacity
, int internal
)
1100 cpath_build_state_t
*info
;
1103 router_get_routerlist(&rl
);
1106 r
= new_route_len(get_options()->PathlenCoinWeight
, purpose
, rl
->routers
);
1107 if (r
< 1) /* must be at least 1 */
1109 info
= tor_malloc_zero(sizeof(cpath_build_state_t
));
1110 info
->desired_path_len
= r
;
1111 info
->need_uptime
= need_uptime
;
1112 info
->need_capacity
= need_capacity
;
1113 info
->is_internal
= internal
;
1114 if (exit_digest
) { /* the circuit-builder pre-requested one */
1115 memcpy(info
->chosen_exit_digest
, exit_digest
, DIGEST_LEN
);
1116 exit
= router_get_by_digest(exit_digest
);
1118 info
->chosen_exit_name
= tor_strdup(exit
->nickname
);
1120 info
->chosen_exit_name
= tor_malloc(HEX_DIGEST_LEN
+1);
1121 base16_encode(info
->chosen_exit_name
, HEX_DIGEST_LEN
+1,
1122 exit_digest
, DIGEST_LEN
);
1124 log_fn(LOG_INFO
,"Using requested exit node '%s'", info
->chosen_exit_name
);
1125 } else { /* we have to decide one */
1126 exit
= choose_good_exit_server(purpose
, rl
, need_uptime
, need_capacity
);
1128 log_fn(LOG_WARN
,"failed to choose an exit server");
1132 memcpy(info
->chosen_exit_digest
, exit
->identity_digest
, DIGEST_LEN
);
1133 info
->chosen_exit_name
= tor_strdup(exit
->nickname
);
1138 /** Take the open circ originating here, give it a new exit destination
1139 * to exit_digest (use nickname directly if it's provided, else strdup
1140 * out of router->nickname), and get it to send the next extend cell.
1143 circuit_append_new_hop(circuit_t
*circ
, char *nickname
, const char *exit_digest
) {
1144 routerinfo_t
*exit
= router_get_by_digest(exit_digest
);
1145 tor_assert(CIRCUIT_IS_ORIGIN(circ
));
1146 circ
->state
= CIRCUIT_STATE_BUILDING
;
1147 tor_free(circ
->build_state
->chosen_exit_name
);
1149 circ
->build_state
->chosen_exit_name
= nickname
;
1151 circ
->build_state
->chosen_exit_name
= tor_strdup(exit
->nickname
);
1153 circ
->build_state
->chosen_exit_name
= tor_malloc(HEX_DIGEST_LEN
+1);
1154 base16_encode(circ
->build_state
->chosen_exit_name
, HEX_DIGEST_LEN
+1,
1155 exit_digest
, DIGEST_LEN
);
1157 memcpy(circ
->build_state
->chosen_exit_digest
, exit_digest
, DIGEST_LEN
);
1158 ++circ
->build_state
->desired_path_len
;
1159 if (circuit_send_next_onion_skin(circ
)<0) {
1160 log_fn(LOG_WARN
, "Couldn't extend circuit to new point '%s'.",
1161 circ
->build_state
->chosen_exit_name
);
1162 circuit_mark_for_close(circ
);
1168 /** Return the number of routers in <b>routers</b> that are currently up
1169 * and available for building circuits through.
1171 static int count_acceptable_routers(smartlist_t
*routers
) {
1176 n
= smartlist_len(routers
);
1178 r
= smartlist_get(routers
, i
);
1179 // log_fn(LOG_DEBUG,"Contemplating whether router %d (%s) is a new option...",
1181 if (r
->is_running
== 0) {
1182 // log_fn(LOG_DEBUG,"Nope, the directory says %d is not running.",i);
1185 if (r
->is_verified
== 0) {
1186 // log_fn(LOG_DEBUG,"Nope, the directory says %d is not verified.",i);
1187 /* XXXX009 But unverified routers *are* sometimes acceptable. */
1191 // log_fn(LOG_DEBUG,"I like %d. num_acceptable_routers now %d.",i, num);
1193 ; /* C requires an explicit statement after the label */
1199 /** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
1201 * This function is used to extend cpath by another hop.
1203 void onion_append_to_cpath(crypt_path_t
**head_ptr
, crypt_path_t
*new_hop
)
1206 new_hop
->next
= (*head_ptr
);
1207 new_hop
->prev
= (*head_ptr
)->prev
;
1208 (*head_ptr
)->prev
->next
= new_hop
;
1209 (*head_ptr
)->prev
= new_hop
;
1211 *head_ptr
= new_hop
;
1212 new_hop
->prev
= new_hop
->next
= new_hop
;
1216 static routerinfo_t
*choose_good_middle_server(cpath_build_state_t
*state
,
1221 routerinfo_t
*r
, *choice
;
1222 crypt_path_t
*cpath
;
1223 smartlist_t
*excluded
;
1225 log_fn(LOG_DEBUG
, "Contemplating intermediate hop: random choice.");
1226 excluded
= smartlist_create();
1227 if ((r
= router_get_by_digest(state
->chosen_exit_digest
))) {
1228 smartlist_add(excluded
, r
);
1229 routerlist_add_family(excluded
, r
);
1231 if ((r
= routerlist_find_my_routerinfo())) {
1232 smartlist_add(excluded
, r
);
1233 routerlist_add_family(excluded
, r
);
1235 for (i
= 0, cpath
= head
; i
< cur_len
; ++i
, cpath
=cpath
->next
) {
1236 if ((r
= router_get_by_digest(cpath
->identity_digest
))) {
1237 smartlist_add(excluded
, r
);
1238 routerlist_add_family(excluded
, r
);
1241 choice
= router_choose_random_node(NULL
, get_options()->ExcludeNodes
, excluded
,
1242 state
->need_uptime
, state
->need_capacity
,
1243 get_options()->_AllowUnverified
& ALLOW_UNVERIFIED_MIDDLE
, 0);
1244 smartlist_free(excluded
);
1248 static routerinfo_t
*choose_good_entry_server(cpath_build_state_t
*state
)
1250 routerinfo_t
*r
, *choice
;
1251 smartlist_t
*excluded
= smartlist_create();
1252 or_options_t
*options
= get_options();
1254 if ((r
= router_get_by_digest(state
->chosen_exit_digest
))) {
1255 smartlist_add(excluded
, r
);
1256 routerlist_add_family(excluded
, r
);
1258 if ((r
= routerlist_find_my_routerinfo())) {
1259 smartlist_add(excluded
, r
);
1260 routerlist_add_family(excluded
, r
);
1262 if (options
->FascistFirewall
) {
1263 /* exclude all ORs that listen on the wrong port */
1267 router_get_routerlist(&rl
);
1271 for (i
=0; i
< smartlist_len(rl
->routers
); i
++) {
1272 r
= smartlist_get(rl
->routers
, i
);
1273 if (!smartlist_string_num_isin(options
->FirewallPorts
, r
->or_port
))
1274 smartlist_add(excluded
, r
);
1277 choice
= router_choose_random_node(options
->EntryNodes
, options
->ExcludeNodes
,
1278 excluded
, state
->need_uptime
, state
->need_capacity
,
1279 options
->_AllowUnverified
& ALLOW_UNVERIFIED_ENTRY
,
1280 options
->StrictEntryNodes
);
1281 smartlist_free(excluded
);
1285 /** Choose a suitable next hop in the cpath <b>head_ptr</b>,
1286 * based on <b>state</b>. Add the hop info to head_ptr, and return a
1287 * pointer to the chosen router in <b>router_out</b>.
1290 onion_extend_cpath(crypt_path_t
**head_ptr
, cpath_build_state_t
1291 *state
, routerinfo_t
**router_out
)
1294 crypt_path_t
*cpath
, *hop
;
1295 routerinfo_t
*choice
;
1296 smartlist_t
*excludednodes
;
1298 tor_assert(head_ptr
);
1299 tor_assert(router_out
);
1305 for (cpath
= *head_ptr
; cpath
->next
!= *head_ptr
; cpath
= cpath
->next
) {
1309 if (cur_len
>= state
->desired_path_len
) {
1310 log_fn(LOG_DEBUG
, "Path is complete: %d steps long",
1311 state
->desired_path_len
);
1314 log_fn(LOG_DEBUG
, "Path is %d long; we want %d", cur_len
,
1315 state
->desired_path_len
);
1317 excludednodes
= smartlist_create();
1318 add_nickname_list_to_smartlist(excludednodes
,get_options()->ExcludeNodes
,0);
1320 if (cur_len
== state
->desired_path_len
- 1) { /* Picking last node */
1321 choice
= router_get_by_digest(state
->chosen_exit_digest
);
1322 } else if (cur_len
== 0) { /* picking first node */
1323 choice
= choose_good_entry_server(state
);
1325 choice
= choose_good_middle_server(state
, *head_ptr
, cur_len
);
1328 smartlist_free(excludednodes
);
1330 log_fn(LOG_WARN
,"Failed to find node for hop %d of our path. Discarding this circuit.", cur_len
);
1334 log_fn(LOG_DEBUG
,"Chose router %s for hop %d (exit is %s)",
1335 choice
->nickname
, cur_len
+1, state
->chosen_exit_name
);
1337 hop
= tor_malloc_zero(sizeof(crypt_path_t
));
1339 /* link hop into the cpath, at the end. */
1340 onion_append_to_cpath(head_ptr
, hop
);
1342 hop
->state
= CPATH_STATE_CLOSED
;
1344 hop
->port
= choice
->or_port
;
1345 hop
->addr
= choice
->addr
;
1346 memcpy(hop
->identity_digest
, choice
->identity_digest
, DIGEST_LEN
);
1348 hop
->package_window
= CIRCWINDOW_START
;
1349 hop
->deliver_window
= CIRCWINDOW_START
;
1351 log_fn(LOG_DEBUG
, "Extended circuit path with %s for hop %d",
1352 choice
->nickname
, cur_len
+1);
1354 *router_out
= choice
;