similarly, don't throw around an int for the uint8_t circ->state
[tor.git] / src / or / circuitlist.c
blobf43579efbbfda35355a83c74a3b071b95ca3794f
1 /* Copyright 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 */
6 /* $Id$ */
7 const char circuitlist_c_id[] =
8 "$Id$";
10 /**
11 * \file circuitlist.c
12 * \brief Manage the global circuit list.
13 **/
15 #include "or.h"
16 #include "ht.h"
18 /********* START VARIABLES **********/
20 /** A global list of all circuits at this hop. */
21 circuit_t *global_circuitlist=NULL;
23 /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
24 static smartlist_t *circuits_pending_or_conns=NULL;
26 static void circuit_free(circuit_t *circ);
27 static void circuit_free_cpath(crypt_path_t *cpath);
28 static void circuit_free_cpath_node(crypt_path_t *victim);
30 /********* END VARIABLES ************/
32 /** A map from OR connection and circuit ID to circuit. (Lookup performance is
33 * very important here, since we need to do it every time a cell arrives.) */
34 typedef struct orconn_circid_circuit_map_t {
35 HT_ENTRY(orconn_circid_circuit_map_t) node;
36 or_connection_t *or_conn;
37 uint16_t circ_id;
38 circuit_t *circuit;
39 } orconn_circid_circuit_map_t;
41 /** Helper for hash tables: compare the OR connection and circuit ID for a and
42 * b, and return less than, equal to, or greater than zero appropriately.
44 static INLINE int
45 _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
46 orconn_circid_circuit_map_t *b)
48 return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
51 /** Helper: return a hash based on circuit ID and the pointer value of
52 * or_conn in <b>a</b>. */
53 static INLINE unsigned int
54 _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
56 return (((unsigned)a->circ_id)<<16) ^ (unsigned)(uintptr_t)(a->or_conn);
59 /** Map from [orconn,circid] to circuit. */
60 static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
61 orconn_circid_circuit_map = HT_INITIALIZER();
62 HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
63 _orconn_circid_entry_hash, _orconn_circid_entries_eq)
64 HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
65 _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
66 malloc, realloc, free)
68 /** The most recently returned entry from circuit_get_by_circid_orconn;
69 * used to improve performance when many cells arrive in a row from the
70 * same circuit.
72 orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
74 /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
75 * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
76 * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
77 * the old entry (if any) and adding a new one. If <b>active</b> is true,
78 * remove the circuit from the list of active circuits on old_conn and add it
79 * to the list of active circuits on conn.
80 * XXX "active" isn't an arg anymore */
81 static void
82 circuit_set_circid_orconn_helper(circuit_t *circ, int direction,
83 uint16_t id,
84 or_connection_t *conn)
86 orconn_circid_circuit_map_t search;
87 orconn_circid_circuit_map_t *found;
88 or_connection_t *old_conn, **conn_ptr;
89 uint16_t old_id, *circid_ptr;
90 int was_active, make_active;
92 if (direction == CELL_DIRECTION_OUT) {
93 conn_ptr = &circ->n_conn;
94 circid_ptr = &circ->n_circ_id;
95 was_active = circ->next_active_on_n_conn != NULL;
96 make_active = circ->n_conn_cells.n > 0;
97 } else {
98 or_circuit_t *c = TO_OR_CIRCUIT(circ);
99 conn_ptr = &c->p_conn;
100 circid_ptr = &c->p_circ_id;
101 was_active = c->next_active_on_p_conn != NULL;
102 make_active = c->p_conn_cells.n > 0;
104 old_conn = *conn_ptr;
105 old_id = *circid_ptr;
107 if (id == old_id && conn == old_conn)
108 return;
110 if (_last_circid_orconn_ent &&
111 ((old_id == _last_circid_orconn_ent->circ_id &&
112 old_conn == _last_circid_orconn_ent->or_conn) ||
113 (id == _last_circid_orconn_ent->circ_id &&
114 conn == _last_circid_orconn_ent->or_conn))) {
115 _last_circid_orconn_ent = NULL;
118 if (old_conn) { /* we may need to remove it from the conn-circid map */
119 tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
120 search.circ_id = old_id;
121 search.or_conn = old_conn;
122 found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
123 if (found) {
124 tor_free(found);
125 --old_conn->n_circuits;
127 if (was_active && old_conn != conn)
128 make_circuit_inactive_on_conn(circ,old_conn);
131 /* Change the values only after we have possibly made the circuit inactive
132 * on the previous conn. */
133 *conn_ptr = conn;
134 *circid_ptr = id;
136 if (conn == NULL)
137 return;
139 /* now add the new one to the conn-circid map */
140 search.circ_id = id;
141 search.or_conn = conn;
142 found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
143 if (found) {
144 found->circuit = circ;
145 } else {
146 found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
147 found->circ_id = id;
148 found->or_conn = conn;
149 found->circuit = circ;
150 HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
152 if (make_active && old_conn != conn)
153 make_circuit_active_on_conn(circ,conn);
155 ++conn->n_circuits;
158 /** Set the p_conn field of a circuit <b>circ</b>, along
159 * with the corresponding circuit ID, and add the circuit as appropriate
160 * to the (orconn,id)-\>circuit map. */
161 void
162 circuit_set_p_circid_orconn(or_circuit_t *circ, uint16_t id,
163 or_connection_t *conn)
165 circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), CELL_DIRECTION_IN,
166 id, conn);
168 if (conn)
169 tor_assert(bool_eq(circ->p_conn_cells.n, circ->next_active_on_p_conn));
172 /** Set the n_conn field of a circuit <b>circ</b>, along
173 * with the corresponding circuit ID, and add the circuit as appropriate
174 * to the (orconn,id)-\>circuit map. */
175 void
176 circuit_set_n_circid_orconn(circuit_t *circ, uint16_t id,
177 or_connection_t *conn)
179 circuit_set_circid_orconn_helper(circ, CELL_DIRECTION_OUT, id, conn);
181 if (conn)
182 tor_assert(bool_eq(circ->n_conn_cells.n, circ->next_active_on_n_conn));
185 /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
186 * it from lists as appropriate. */
187 void
188 circuit_set_state(circuit_t *circ, uint8_t state)
190 tor_assert(circ);
191 if (state == circ->state)
192 return;
193 if (!circuits_pending_or_conns)
194 circuits_pending_or_conns = smartlist_create();
195 if (circ->state == CIRCUIT_STATE_OR_WAIT) {
196 /* remove from waiting-circuit list. */
197 smartlist_remove(circuits_pending_or_conns, circ);
199 if (state == CIRCUIT_STATE_OR_WAIT) {
200 /* add to waiting-circuit list. */
201 smartlist_add(circuits_pending_or_conns, circ);
203 if (state == CIRCUIT_STATE_OPEN)
204 tor_assert(!circ->n_conn_onionskin);
205 circ->state = state;
208 /** Add <b>circ</b> to the global list of circuits. This is called only from
209 * within circuit_new.
211 static void
212 circuit_add(circuit_t *circ)
214 if (!global_circuitlist) { /* first one */
215 global_circuitlist = circ;
216 circ->next = NULL;
217 } else {
218 circ->next = global_circuitlist;
219 global_circuitlist = circ;
223 /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
224 * the given connection. */
225 void
226 circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
228 tor_assert(out);
229 tor_assert(or_conn);
231 if (!circuits_pending_or_conns)
232 return;
234 SMARTLIST_FOREACH(circuits_pending_or_conns, circuit_t *, circ,
236 if (circ->marked_for_close)
237 continue;
238 tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
239 if (tor_digest_is_zero(circ->n_conn_id_digest)) {
240 /* Look at addr/port. This is an unkeyed connection. */
241 if (circ->n_addr != or_conn->_base.addr ||
242 circ->n_port != or_conn->_base.port)
243 continue;
244 } else {
245 /* We expected a key. See if it's the right one. */
246 if (memcmp(or_conn->identity_digest,
247 circ->n_conn_id_digest, DIGEST_LEN))
248 continue;
250 smartlist_add(out, circ);
254 /** Return the number of circuits in state OR_WAIT, waiting for the given
255 * connection. */
257 circuit_count_pending_on_or_conn(or_connection_t *or_conn)
259 int cnt;
260 smartlist_t *sl = smartlist_create();
261 circuit_get_all_pending_on_or_conn(sl, or_conn);
262 cnt = smartlist_len(sl);
263 smartlist_free(sl);
264 log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
265 or_conn->nickname ? or_conn->nickname : "NULL", cnt);
266 return cnt;
269 /** Detach from the global circuit list, and deallocate, all
270 * circuits that have been marked for close.
272 void
273 circuit_close_all_marked(void)
275 circuit_t *tmp,*m;
277 while (global_circuitlist && global_circuitlist->marked_for_close) {
278 tmp = global_circuitlist->next;
279 circuit_free(global_circuitlist);
280 global_circuitlist = tmp;
283 tmp = global_circuitlist;
284 while (tmp && tmp->next) {
285 if (tmp->next->marked_for_close) {
286 m = tmp->next->next;
287 circuit_free(tmp->next);
288 tmp->next = m;
289 /* Need to check new tmp->next; don't advance tmp. */
290 } else {
291 /* Advance tmp. */
292 tmp = tmp->next;
297 /** Return the head of the global linked list of circuits. */
298 circuit_t *
299 _circuit_get_global_list(void)
301 return global_circuitlist;
304 /** Function to make circ-\>state human-readable */
305 const char *
306 circuit_state_to_string(int state)
308 static char buf[64];
309 switch (state) {
310 case CIRCUIT_STATE_BUILDING: return "doing handshakes";
311 case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
312 case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
313 case CIRCUIT_STATE_OPEN: return "open";
314 default:
315 log_warn(LD_BUG, "Unknown circuit state %d", state);
316 tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
317 return buf;
321 /** Initialize the common elements in a circuit_t, and add it to the global
322 * list. */
323 static void
324 init_circuit_base(circuit_t *circ)
326 circ->timestamp_created = time(NULL);
328 circ->package_window = CIRCWINDOW_START;
329 circ->deliver_window = CIRCWINDOW_START;
331 circuit_add(circ);
334 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
335 * and <b>p_conn</b>. Add it to the global circuit list.
337 origin_circuit_t *
338 origin_circuit_new(void)
340 origin_circuit_t *circ;
341 /* never zero, since a global ID of 0 is treated specially by the
342 * controller */
343 static uint32_t n_circuits_allocated = 1;
345 circ = tor_malloc_zero(sizeof(origin_circuit_t));
346 circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
348 circ->next_stream_id = crypto_rand_int(1<<16);
349 circ->global_identifier = n_circuits_allocated++;
351 init_circuit_base(TO_CIRCUIT(circ));
353 return circ;
356 /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
357 * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
358 or_circuit_t *
359 or_circuit_new(uint16_t p_circ_id, or_connection_t *p_conn)
361 /* CircIDs */
362 or_circuit_t *circ;
364 circ = tor_malloc_zero(sizeof(or_circuit_t));
365 circ->_base.magic = OR_CIRCUIT_MAGIC;
367 if (p_conn)
368 circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
370 init_circuit_base(TO_CIRCUIT(circ));
372 return circ;
375 /** Deallocate space associated with circ.
377 static void
378 circuit_free(circuit_t *circ)
380 void *mem;
381 size_t memlen;
382 tor_assert(circ);
383 if (CIRCUIT_IS_ORIGIN(circ)) {
384 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
385 mem = ocirc;
386 memlen = sizeof(origin_circuit_t);
387 tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
388 if (ocirc->build_state) {
389 if (ocirc->build_state->chosen_exit)
390 extend_info_free(ocirc->build_state->chosen_exit);
391 if (ocirc->build_state->pending_final_cpath)
392 circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
394 tor_free(ocirc->build_state);
396 circuit_free_cpath(ocirc->cpath);
397 if (ocirc->intro_key)
398 crypto_free_pk_env(ocirc->intro_key);
400 } else {
401 or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
402 mem = ocirc;
403 memlen = sizeof(or_circuit_t);
404 tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
406 if (ocirc->p_crypto)
407 crypto_free_cipher_env(ocirc->p_crypto);
408 if (ocirc->p_digest)
409 crypto_free_digest_env(ocirc->p_digest);
410 if (ocirc->n_crypto)
411 crypto_free_cipher_env(ocirc->n_crypto);
412 if (ocirc->n_digest)
413 crypto_free_digest_env(ocirc->n_digest);
415 if (ocirc->rend_splice) {
416 or_circuit_t *other = ocirc->rend_splice;
417 tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
418 other->rend_splice = NULL;
421 /* remove from map. */
422 circuit_set_p_circid_orconn(ocirc, 0, NULL);
424 /* Clear cell queue _after_ removing it from the map. Otherwise our
425 * "active" checks will be violated. */
426 cell_queue_clear(&ocirc->p_conn_cells);
429 tor_free(circ->n_conn_onionskin);
431 /* Remove from map. */
432 circuit_set_n_circid_orconn(circ, 0, NULL);
434 /* Clear cell queue _after_ removing it from the map. Otherwise our
435 * "active" checks will be violated. */
436 cell_queue_clear(&circ->n_conn_cells);
438 memset(circ, 0xAA, memlen); /* poison memory */
439 tor_free(mem);
442 /** Deallocate space associated with the linked list <b>cpath</b>. */
443 static void
444 circuit_free_cpath(crypt_path_t *cpath)
446 crypt_path_t *victim, *head=cpath;
448 if (!cpath)
449 return;
451 /* it's a doubly linked list, so we have to notice when we've
452 * gone through it once. */
453 while (cpath->next && cpath->next != head) {
454 victim = cpath;
455 cpath = victim->next;
456 circuit_free_cpath_node(victim);
459 circuit_free_cpath_node(cpath);
462 /** Release all storage held by circuits. */
463 void
464 circuit_free_all(void)
466 circuit_t *next;
467 while (global_circuitlist) {
468 next = global_circuitlist->next;
469 if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
470 or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
471 while (or_circ->resolving_streams) {
472 edge_connection_t *next_conn;
473 next_conn = or_circ->resolving_streams->next_stream;
474 connection_free(TO_CONN(or_circ->resolving_streams));
475 or_circ->resolving_streams = next_conn;
478 circuit_free(global_circuitlist);
479 global_circuitlist = next;
481 if (circuits_pending_or_conns) {
482 smartlist_free(circuits_pending_or_conns);
483 circuits_pending_or_conns = NULL;
485 HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
488 /** Deallocate space associated with the cpath node <b>victim</b>. */
489 static void
490 circuit_free_cpath_node(crypt_path_t *victim)
492 if (victim->f_crypto)
493 crypto_free_cipher_env(victim->f_crypto);
494 if (victim->b_crypto)
495 crypto_free_cipher_env(victim->b_crypto);
496 if (victim->f_digest)
497 crypto_free_digest_env(victim->f_digest);
498 if (victim->b_digest)
499 crypto_free_digest_env(victim->b_digest);
500 if (victim->dh_handshake_state)
501 crypto_dh_free(victim->dh_handshake_state);
502 if (victim->extend_info)
503 extend_info_free(victim->extend_info);
505 memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
506 tor_free(victim);
509 /** A helper function for circuit_dump_by_conn() below. Log a bunch
510 * of information about circuit <b>circ</b>.
512 static void
513 circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
514 const char *type, int this_circid, int other_circid)
516 log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
517 "state %d (%s), born %d:",
518 conn_array_index, type, this_circid, other_circid, circ->state,
519 circuit_state_to_string(circ->state), (int)circ->timestamp_created);
520 if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
521 circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
525 /** Log, at severity <b>severity</b>, information about each circuit
526 * that is connected to <b>conn</b>.
528 void
529 circuit_dump_by_conn(connection_t *conn, int severity)
531 circuit_t *circ;
532 edge_connection_t *tmpconn;
534 for (circ=global_circuitlist;circ;circ = circ->next) {
535 circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
536 if (circ->marked_for_close)
537 continue;
539 if (! CIRCUIT_IS_ORIGIN(circ))
540 p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
542 if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
543 TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
544 circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
545 p_circ_id, n_circ_id);
546 if (CIRCUIT_IS_ORIGIN(circ)) {
547 for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
548 tmpconn=tmpconn->next_stream) {
549 if (TO_CONN(tmpconn) == conn) {
550 circuit_dump_details(severity, circ, conn->conn_array_index,
551 "App-ward", p_circ_id, n_circ_id);
555 if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
556 circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
557 n_circ_id, p_circ_id);
558 if (! CIRCUIT_IS_ORIGIN(circ)) {
559 for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
560 tmpconn=tmpconn->next_stream) {
561 if (TO_CONN(tmpconn) == conn) {
562 circuit_dump_details(severity, circ, conn->conn_array_index,
563 "Exit-ward", n_circ_id, p_circ_id);
567 if (!circ->n_conn && circ->n_addr && circ->n_port &&
568 circ->n_addr == conn->addr &&
569 circ->n_port == conn->port &&
570 conn->type == CONN_TYPE_OR &&
571 !memcmp(TO_OR_CONN(conn)->identity_digest, circ->n_conn_id_digest,
572 DIGEST_LEN)) {
573 circuit_dump_details(severity, circ, conn->conn_array_index,
574 (circ->state == CIRCUIT_STATE_OPEN &&
575 !CIRCUIT_IS_ORIGIN(circ)) ?
576 "Endpoint" : "Pending",
577 n_circ_id, p_circ_id);
582 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
583 * such circuit exists. */
584 origin_circuit_t *
585 circuit_get_by_global_id(uint32_t id)
587 circuit_t *circ;
588 for (circ=global_circuitlist;circ;circ = circ->next) {
589 if (CIRCUIT_IS_ORIGIN(circ) &&
590 TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
591 if (circ->marked_for_close)
592 return NULL;
593 else
594 return TO_ORIGIN_CIRCUIT(circ);
597 return NULL;
600 /** Return a circ such that:
601 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
602 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
603 * Return NULL if no such circuit exists.
605 static INLINE circuit_t *
606 circuit_get_by_circid_orconn_impl(uint16_t circ_id, or_connection_t *conn)
608 orconn_circid_circuit_map_t search;
609 orconn_circid_circuit_map_t *found;
611 if (_last_circid_orconn_ent &&
612 circ_id == _last_circid_orconn_ent->circ_id &&
613 conn == _last_circid_orconn_ent->or_conn) {
614 found = _last_circid_orconn_ent;
615 } else {
616 search.circ_id = circ_id;
617 search.or_conn = conn;
618 found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
619 _last_circid_orconn_ent = found;
621 if (found && found->circuit)
622 return found->circuit;
624 return NULL;
626 /* The rest of this checks for bugs. Disabled by default. */
628 circuit_t *circ;
629 for (circ=global_circuitlist;circ;circ = circ->next) {
630 if (! CIRCUIT_IS_ORIGIN(circ)) {
631 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
632 if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
633 log_warn(LD_BUG,
634 "circuit matches p_conn, but not in hash table (Bug!)");
635 return circ;
638 if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
639 log_warn(LD_BUG,
640 "circuit matches n_conn, but not in hash table (Bug!)");
641 return circ;
644 return NULL;
648 /** Return a circ such that:
649 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
650 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
651 * - circ is not marked for close.
652 * Return NULL if no such circuit exists.
654 circuit_t *
655 circuit_get_by_circid_orconn(uint16_t circ_id, or_connection_t *conn)
657 circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
658 if (!circ || circ->marked_for_close)
659 return NULL;
660 else
661 return circ;
664 /** Return the circuit that a given edge connection is using. */
665 circuit_t *
666 circuit_get_by_edge_conn(edge_connection_t *conn)
668 circuit_t *circ;
670 circ = conn->on_circuit;
671 tor_assert(!circ ||
672 (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
673 : circ->magic == OR_CIRCUIT_MAGIC));
675 return circ;
678 /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
679 * circuit from the orconn,circid map, and mark it for close if it hasn't
680 * been marked already.
682 void
683 circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
685 circuit_t *circ;
687 connection_or_unlink_all_active_circs(conn);
689 for (circ = global_circuitlist; circ; circ = circ->next) {
690 int mark = 0;
691 if (circ->n_conn == conn) {
692 circuit_set_n_circid_orconn(circ, 0, NULL);
693 mark = 1;
695 if (! CIRCUIT_IS_ORIGIN(circ)) {
696 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
697 if (or_circ->p_conn == conn) {
698 circuit_set_p_circid_orconn(or_circ, 0, NULL);
699 mark = 1;
702 if (mark && !circ->marked_for_close)
703 circuit_mark_for_close(circ, reason);
707 /** Return a circ such that:
708 * - circ-\>rend_query is equal to <b>rend_query</b>, and
709 * - circ-\>purpose is equal to <b>purpose</b>.
711 * Return NULL if no such circuit exists.
713 origin_circuit_t *
714 circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
716 circuit_t *circ;
718 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
720 for (circ = global_circuitlist; circ; circ = circ->next) {
721 if (!circ->marked_for_close &&
722 circ->purpose == purpose &&
723 !rend_cmp_service_ids(rend_query, TO_ORIGIN_CIRCUIT(circ)->rend_query))
724 return TO_ORIGIN_CIRCUIT(circ);
726 return NULL;
729 /** Return the first circuit originating here in global_circuitlist after
730 * <b>start</b> whose purpose is <b>purpose</b>, and where
731 * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
732 * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
734 origin_circuit_t *
735 circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
736 const char *digest, uint8_t purpose)
738 circuit_t *circ;
739 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
740 if (start == NULL)
741 circ = global_circuitlist;
742 else
743 circ = TO_CIRCUIT(start)->next;
745 for ( ; circ; circ = circ->next) {
746 if (circ->marked_for_close)
747 continue;
748 if (circ->purpose != purpose)
749 continue;
750 if (!digest)
751 return TO_ORIGIN_CIRCUIT(circ);
752 else if (!memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_pk_digest,
753 digest, DIGEST_LEN))
754 return TO_ORIGIN_CIRCUIT(circ);
756 return NULL;
759 /** Return the first OR circuit in the global list whose purpose is
760 * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
761 * <b>token</b>. */
762 static or_circuit_t *
763 circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
764 size_t len)
766 circuit_t *circ;
767 for (circ = global_circuitlist; circ; circ = circ->next) {
768 if (! circ->marked_for_close &&
769 circ->purpose == purpose &&
770 ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
771 return TO_OR_CIRCUIT(circ);
773 return NULL;
776 /** Return the circuit waiting for a rendezvous with the provided cookie.
777 * Return NULL if no such circuit is found.
779 or_circuit_t *
780 circuit_get_rendezvous(const char *cookie)
782 return circuit_get_by_rend_token_and_purpose(
783 CIRCUIT_PURPOSE_REND_POINT_WAITING,
784 cookie, REND_COOKIE_LEN);
787 /** Return the circuit waiting for intro cells of the given digest.
788 * Return NULL if no such circuit is found.
790 or_circuit_t *
791 circuit_get_intro_point(const char *digest)
793 return circuit_get_by_rend_token_and_purpose(
794 CIRCUIT_PURPOSE_INTRO_POINT, digest,
795 DIGEST_LEN);
798 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
799 * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
800 * flags in <b>flags</b>, and if info is defined, does not already use info
801 * as any of its hops; or NULL if no circuit fits this description.
803 * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
805 origin_circuit_t *
806 circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
807 int flags)
809 /*XXXX021 arma: The purpose argument is ignored. Can that possibly be
810 * right? */
811 /* XXXX <arma> i don't know of any actual bugs that this causes. since i
812 * think we only call the function for purposes where we want it to do what
813 * the function does. somebody should check this though. */
815 circuit_t *_circ;
816 origin_circuit_t *best=NULL;
817 int need_uptime = flags & CIRCLAUNCH_NEED_UPTIME;
818 int need_capacity = flags & CIRCLAUNCH_NEED_CAPACITY;
819 int internal = flags & CIRCLAUNCH_IS_INTERNAL;
821 log_debug(LD_CIRC,
822 "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
823 "capacity %d, internal %d",
824 purpose, need_uptime, need_capacity, internal);
826 for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
827 if (CIRCUIT_IS_ORIGIN(_circ) &&
828 _circ->state == CIRCUIT_STATE_OPEN &&
829 !_circ->marked_for_close &&
830 _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
831 !_circ->timestamp_dirty) {
832 origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
833 #if 0 /* XXX here while roger investigates a reported RendNodes bug */
834 if (_circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
835 options->RendNodes) {
836 routerinfo_t *exit = build_state_get_exit_router(circ->build_state);
837 if (exit && !router_nickname_is_in_list(exit, options->RendNodes))
838 continue; /* not one of our allowed RendNodes */
840 #endif
841 if ((!need_uptime || circ->build_state->need_uptime) &&
842 (!need_capacity || circ->build_state->need_capacity) &&
843 (internal == circ->build_state->is_internal)) {
844 if (info) {
845 /* need to make sure we don't duplicate hops */
846 crypt_path_t *hop = circ->cpath;
847 routerinfo_t *ri1 = router_get_by_digest(info->identity_digest);
848 do {
849 routerinfo_t *ri2;
850 if (!memcmp(hop->extend_info->identity_digest,
851 info->identity_digest, DIGEST_LEN))
852 goto next;
853 if (ri1 &&
854 (ri2 = router_get_by_digest(hop->extend_info->identity_digest))
855 && routers_in_same_family(ri1, ri2))
856 goto next;
857 hop=hop->next;
858 } while (hop!=circ->cpath);
860 if (!best || (best->build_state->need_uptime && !need_uptime))
861 best = circ;
862 next: ;
866 return best;
869 /** Return the number of hops in circuit's path. */
871 circuit_get_cpath_len(origin_circuit_t *circ)
873 int n = 0;
874 if (circ && circ->cpath) {
875 crypt_path_t *cpath, *cpath_next = NULL;
876 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
877 cpath_next = cpath->next;
878 ++n;
881 return n;
884 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
885 * aren't that many hops in the list. */
886 crypt_path_t *
887 circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
889 if (circ && circ->cpath && hopnum > 0) {
890 crypt_path_t *cpath, *cpath_next = NULL;
891 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
892 cpath_next = cpath->next;
893 if (--hopnum <= 0)
894 return cpath;
897 return NULL;
900 /** Go through the circuitlist; mark-for-close each circuit that starts
901 * at us but has not yet been used. */
902 void
903 circuit_mark_all_unused_circs(void)
905 circuit_t *circ;
907 for (circ=global_circuitlist; circ; circ = circ->next) {
908 if (CIRCUIT_IS_ORIGIN(circ) &&
909 !circ->marked_for_close &&
910 !circ->timestamp_dirty)
911 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
915 /** Go through the circuitlist; for each circuit that starts at us
916 * and is dirty, frob its timestamp_dirty so we won't use it for any
917 * new streams.
919 * This is useful for letting the user change pseudonyms, so new
920 * streams will not be linkable to old streams.
922 void
923 circuit_expire_all_dirty_circs(void)
925 circuit_t *circ;
926 or_options_t *options = get_options();
928 for (circ=global_circuitlist; circ; circ = circ->next) {
929 if (CIRCUIT_IS_ORIGIN(circ) &&
930 !circ->marked_for_close &&
931 circ->timestamp_dirty)
932 circ->timestamp_dirty -= options->MaxCircuitDirtiness;
936 /** Mark <b>circ</b> to be closed next time we call
937 * circuit_close_all_marked(). Do any cleanup needed:
938 * - If state is onionskin_pending, remove circ from the onion_pending
939 * list.
940 * - If circ isn't open yet: call circuit_build_failed() if we're
941 * the origin, and in either case call circuit_rep_hist_note_result()
942 * to note stats.
943 * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
944 * just tried from our list of intro points for that service
945 * descriptor.
946 * - Send appropriate destroys and edge_destroys for conns and
947 * streams attached to circ.
948 * - If circ->rend_splice is set (we are the midpoint of a joined
949 * rendezvous stream), then mark the other circuit to close as well.
951 void
952 _circuit_mark_for_close(circuit_t *circ, int reason, int line,
953 const char *file)
955 int orig_reason = reason; /* Passed to the controller */
956 assert_circuit_ok(circ);
957 tor_assert(line);
958 tor_assert(file);
960 if (circ->marked_for_close) {
961 log(LOG_WARN,LD_BUG,
962 "Duplicate call to circuit_mark_for_close at %s:%d"
963 " (first at %s:%d)", file, line,
964 circ->marked_for_close_file, circ->marked_for_close);
965 return;
967 if (reason == END_CIRC_AT_ORIGIN) {
968 if (!CIRCUIT_IS_ORIGIN(circ)) {
969 log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
970 "but circuit was not at origin. (called %s:%d, purpose=%d)",
971 file, line, circ->purpose);
973 reason = END_CIRC_REASON_NONE;
975 if (CIRCUIT_IS_ORIGIN(circ)) {
976 /* We don't send reasons when closing circuits at the origin. */
977 reason = END_CIRC_REASON_NONE;
980 if (reason & END_CIRC_REASON_FLAG_REMOTE)
981 reason &= ~END_CIRC_REASON_FLAG_REMOTE;
983 if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
984 if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
985 log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
986 reason = END_CIRC_REASON_NONE;
989 if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
990 onion_pending_remove(TO_OR_CIRCUIT(circ));
992 /* If the circuit ever became OPEN, we sent it to the reputation history
993 * module then. If it isn't OPEN, we send it there now to remember which
994 * links worked and which didn't.
996 if (circ->state != CIRCUIT_STATE_OPEN) {
997 if (CIRCUIT_IS_ORIGIN(circ)) {
998 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
999 circuit_build_failed(ocirc); /* take actions if necessary */
1000 circuit_rep_hist_note_result(ocirc);
1003 if (circ->state == CIRCUIT_STATE_OR_WAIT) {
1004 if (circuits_pending_or_conns)
1005 smartlist_remove(circuits_pending_or_conns, circ);
1007 if (CIRCUIT_IS_ORIGIN(circ)) {
1008 control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
1009 (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
1010 orig_reason);
1012 if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
1013 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1014 tor_assert(circ->state == CIRCUIT_STATE_OPEN);
1015 tor_assert(ocirc->build_state->chosen_exit);
1016 /* treat this like getting a nack from it */
1017 log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
1018 "Removing from descriptor.",
1019 safe_str(ocirc->rend_query),
1020 safe_str(build_state_get_exit_nickname(ocirc->build_state)));
1021 rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
1022 ocirc->rend_query);
1024 if (circ->n_conn)
1025 connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
1027 if (! CIRCUIT_IS_ORIGIN(circ)) {
1028 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1029 edge_connection_t *conn;
1030 for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
1031 connection_edge_destroy(or_circ->p_circ_id, conn);
1033 while (or_circ->resolving_streams) {
1034 conn = or_circ->resolving_streams;
1035 or_circ->resolving_streams = conn->next_stream;
1036 if (!conn->_base.marked_for_close) {
1037 /* The client will see a DESTROY, and infer that the connections
1038 * are closing because the circuit is getting torn down. No need
1039 * to send an end cell. */
1040 conn->_base.edge_has_sent_end = 1;
1041 conn->end_reason = END_STREAM_REASON_DESTROY;
1042 conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
1043 connection_mark_for_close(TO_CONN(conn));
1045 conn->on_circuit = NULL;
1048 if (or_circ->p_conn)
1049 connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
1050 } else {
1051 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1052 edge_connection_t *conn;
1053 for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
1054 connection_edge_destroy(circ->n_circ_id, conn);
1057 circ->marked_for_close = line;
1058 circ->marked_for_close_file = file;
1060 if (!CIRCUIT_IS_ORIGIN(circ)) {
1061 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1062 if (or_circ->rend_splice) {
1063 if (!or_circ->rend_splice->_base.marked_for_close) {
1064 /* do this after marking this circuit, to avoid infinite recursion. */
1065 circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
1067 or_circ->rend_splice = NULL;
1072 /** Verify that cpath layer <b>cp</b> has all of its invariants
1073 * correct. Trigger an assert if anything is invalid.
1075 void
1076 assert_cpath_layer_ok(const crypt_path_t *cp)
1078 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
1079 // tor_assert(cp->port);
1080 tor_assert(cp);
1081 tor_assert(cp->magic == CRYPT_PATH_MAGIC);
1082 switch (cp->state)
1084 case CPATH_STATE_OPEN:
1085 tor_assert(cp->f_crypto);
1086 tor_assert(cp->b_crypto);
1087 /* fall through */
1088 case CPATH_STATE_CLOSED:
1089 tor_assert(!cp->dh_handshake_state);
1090 break;
1091 case CPATH_STATE_AWAITING_KEYS:
1092 /* tor_assert(cp->dh_handshake_state); */
1093 break;
1094 default:
1095 log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
1096 tor_assert(0);
1098 tor_assert(cp->package_window >= 0);
1099 tor_assert(cp->deliver_window >= 0);
1102 /** Verify that cpath <b>cp</b> has all of its invariants
1103 * correct. Trigger an assert if anything is invalid.
1105 static void
1106 assert_cpath_ok(const crypt_path_t *cp)
1108 const crypt_path_t *start = cp;
1110 do {
1111 assert_cpath_layer_ok(cp);
1112 /* layers must be in sequence of: "open* awaiting? closed*" */
1113 if (cp != start) {
1114 if (cp->state == CPATH_STATE_AWAITING_KEYS) {
1115 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
1116 } else if (cp->state == CPATH_STATE_OPEN) {
1117 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
1120 cp = cp->next;
1121 tor_assert(cp);
1122 } while (cp != start);
1125 /** Verify that circuit <b>c</b> has all of its invariants
1126 * correct. Trigger an assert if anything is invalid.
1128 void
1129 assert_circuit_ok(const circuit_t *c)
1131 edge_connection_t *conn;
1132 const or_circuit_t *or_circ = NULL;
1133 const origin_circuit_t *origin_circ = NULL;
1135 tor_assert(c);
1136 tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
1137 tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
1138 c->purpose <= _CIRCUIT_PURPOSE_MAX);
1141 /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
1142 * never understand. -NM. */
1143 circuit_t *nonconst_circ = (circuit_t*) c;
1144 if (CIRCUIT_IS_ORIGIN(c))
1145 origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
1146 else
1147 or_circ = TO_OR_CIRCUIT(nonconst_circ);
1150 if (c->n_conn) {
1151 tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
1152 DIGEST_LEN));
1153 if (c->n_circ_id)
1154 tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
1156 if (or_circ && or_circ->p_conn) {
1157 if (or_circ->p_circ_id)
1158 tor_assert(c == circuit_get_by_circid_orconn(or_circ->p_circ_id,
1159 or_circ->p_conn));
1161 #if 0 /* false now that rendezvous exits are attached to p_streams */
1162 if (origin_circ)
1163 for (conn = origin_circ->p_streams; conn; conn = conn->next_stream)
1164 tor_assert(conn->_base.type == CONN_TYPE_AP);
1165 #endif
1166 if (or_circ)
1167 for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
1168 tor_assert(conn->_base.type == CONN_TYPE_EXIT);
1170 tor_assert(c->deliver_window >= 0);
1171 tor_assert(c->package_window >= 0);
1172 if (c->state == CIRCUIT_STATE_OPEN) {
1173 tor_assert(!c->n_conn_onionskin);
1174 if (or_circ) {
1175 tor_assert(or_circ->n_crypto);
1176 tor_assert(or_circ->p_crypto);
1177 tor_assert(or_circ->n_digest);
1178 tor_assert(or_circ->p_digest);
1181 if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
1182 tor_assert(circuits_pending_or_conns &&
1183 smartlist_isin(circuits_pending_or_conns, c));
1184 } else {
1185 tor_assert(!circuits_pending_or_conns ||
1186 !smartlist_isin(circuits_pending_or_conns, c));
1188 if (origin_circ && origin_circ->cpath) {
1189 assert_cpath_ok(origin_circ->cpath);
1191 if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
1192 tor_assert(or_circ);
1193 if (!c->marked_for_close) {
1194 tor_assert(or_circ->rend_splice);
1195 tor_assert(or_circ->rend_splice->rend_splice == or_circ);
1197 tor_assert(or_circ->rend_splice != or_circ);
1198 } else {
1199 tor_assert(!or_circ || !or_circ->rend_splice);