Create circuitbuild.h
[tor/rransom.git] / src / or / circuitlist.c
blob52b33197a2b951b6bcee34b9a5e9347486d0f9dc
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-2010, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file circuitlist.c
9 * \brief Manage the global circuit list.
10 **/
12 #include "or.h"
13 #include "circuitbuild.h"
14 #include "rendclient.h"
15 #include "rendcommon.h"
16 #include "routerlist.h"
17 #include "ht.h"
19 /********* START VARIABLES **********/
21 /** A global list of all circuits at this hop. */
22 circuit_t *global_circuitlist=NULL;
24 /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
25 static smartlist_t *circuits_pending_or_conns=NULL;
27 static void circuit_free(circuit_t *circ);
28 static void circuit_free_cpath(crypt_path_t *cpath);
29 static void circuit_free_cpath_node(crypt_path_t *victim);
31 /********* END VARIABLES ************/
33 /** A map from OR connection and circuit ID to circuit. (Lookup performance is
34 * very important here, since we need to do it every time a cell arrives.) */
35 typedef struct orconn_circid_circuit_map_t {
36 HT_ENTRY(orconn_circid_circuit_map_t) node;
37 or_connection_t *or_conn;
38 circid_t circ_id;
39 circuit_t *circuit;
40 } orconn_circid_circuit_map_t;
42 /** Helper for hash tables: compare the OR connection and circuit ID for a and
43 * b, and return less than, equal to, or greater than zero appropriately.
45 static INLINE int
46 _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
47 orconn_circid_circuit_map_t *b)
49 return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
52 /** Helper: return a hash based on circuit ID and the pointer value of
53 * or_conn in <b>a</b>. */
54 static INLINE unsigned int
55 _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
57 return (((unsigned)a->circ_id)<<8) ^ (unsigned)(uintptr_t)(a->or_conn);
60 /** Map from [orconn,circid] to circuit. */
61 static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
62 orconn_circid_circuit_map = HT_INITIALIZER();
63 HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
64 _orconn_circid_entry_hash, _orconn_circid_entries_eq)
65 HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
66 _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
67 malloc, realloc, free)
69 /** The most recently returned entry from circuit_get_by_circid_orconn;
70 * used to improve performance when many cells arrive in a row from the
71 * same circuit.
73 orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
75 /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
76 * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
77 * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
78 * the old entry (if any) and adding a new one. If <b>active</b> is true,
79 * remove the circuit from the list of active circuits on old_conn and add it
80 * to the list of active circuits on conn.
81 * XXX "active" isn't an arg anymore */
82 static void
83 circuit_set_circid_orconn_helper(circuit_t *circ, int direction,
84 circid_t id,
85 or_connection_t *conn)
87 orconn_circid_circuit_map_t search;
88 orconn_circid_circuit_map_t *found;
89 or_connection_t *old_conn, **conn_ptr;
90 circid_t old_id, *circid_ptr;
91 int was_active, make_active;
93 if (direction == CELL_DIRECTION_OUT) {
94 conn_ptr = &circ->n_conn;
95 circid_ptr = &circ->n_circ_id;
96 was_active = circ->next_active_on_n_conn != NULL;
97 make_active = circ->n_conn_cells.n > 0;
98 } else {
99 or_circuit_t *c = TO_OR_CIRCUIT(circ);
100 conn_ptr = &c->p_conn;
101 circid_ptr = &c->p_circ_id;
102 was_active = c->next_active_on_p_conn != NULL;
103 make_active = c->p_conn_cells.n > 0;
105 old_conn = *conn_ptr;
106 old_id = *circid_ptr;
108 if (id == old_id && conn == old_conn)
109 return;
111 if (_last_circid_orconn_ent &&
112 ((old_id == _last_circid_orconn_ent->circ_id &&
113 old_conn == _last_circid_orconn_ent->or_conn) ||
114 (id == _last_circid_orconn_ent->circ_id &&
115 conn == _last_circid_orconn_ent->or_conn))) {
116 _last_circid_orconn_ent = NULL;
119 if (old_conn) { /* we may need to remove it from the conn-circid map */
120 tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
121 search.circ_id = old_id;
122 search.or_conn = old_conn;
123 found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
124 if (found) {
125 tor_free(found);
126 --old_conn->n_circuits;
128 if (was_active && old_conn != conn)
129 make_circuit_inactive_on_conn(circ,old_conn);
132 /* Change the values only after we have possibly made the circuit inactive
133 * on the previous conn. */
134 *conn_ptr = conn;
135 *circid_ptr = id;
137 if (conn == NULL)
138 return;
140 /* now add the new one to the conn-circid map */
141 search.circ_id = id;
142 search.or_conn = conn;
143 found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
144 if (found) {
145 found->circuit = circ;
146 } else {
147 found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
148 found->circ_id = id;
149 found->or_conn = conn;
150 found->circuit = circ;
151 HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
153 if (make_active && old_conn != conn)
154 make_circuit_active_on_conn(circ,conn);
156 ++conn->n_circuits;
159 /** Set the p_conn field of a circuit <b>circ</b>, along
160 * with the corresponding circuit ID, and add the circuit as appropriate
161 * to the (orconn,id)-\>circuit map. */
162 void
163 circuit_set_p_circid_orconn(or_circuit_t *circ, circid_t id,
164 or_connection_t *conn)
166 circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), CELL_DIRECTION_IN,
167 id, conn);
169 if (conn)
170 tor_assert(bool_eq(circ->p_conn_cells.n, circ->next_active_on_p_conn));
173 /** Set the n_conn field of a circuit <b>circ</b>, along
174 * with the corresponding circuit ID, and add the circuit as appropriate
175 * to the (orconn,id)-\>circuit map. */
176 void
177 circuit_set_n_circid_orconn(circuit_t *circ, circid_t id,
178 or_connection_t *conn)
180 circuit_set_circid_orconn_helper(circ, CELL_DIRECTION_OUT, id, conn);
182 if (conn)
183 tor_assert(bool_eq(circ->n_conn_cells.n, circ->next_active_on_n_conn));
186 /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
187 * it from lists as appropriate. */
188 void
189 circuit_set_state(circuit_t *circ, uint8_t state)
191 tor_assert(circ);
192 if (state == circ->state)
193 return;
194 if (!circuits_pending_or_conns)
195 circuits_pending_or_conns = smartlist_create();
196 if (circ->state == CIRCUIT_STATE_OR_WAIT) {
197 /* remove from waiting-circuit list. */
198 smartlist_remove(circuits_pending_or_conns, circ);
200 if (state == CIRCUIT_STATE_OR_WAIT) {
201 /* add to waiting-circuit list. */
202 smartlist_add(circuits_pending_or_conns, circ);
204 if (state == CIRCUIT_STATE_OPEN)
205 tor_assert(!circ->n_conn_onionskin);
206 circ->state = state;
209 /** Add <b>circ</b> to the global list of circuits. This is called only from
210 * within circuit_new.
212 static void
213 circuit_add(circuit_t *circ)
215 if (!global_circuitlist) { /* first one */
216 global_circuitlist = circ;
217 circ->next = NULL;
218 } else {
219 circ->next = global_circuitlist;
220 global_circuitlist = circ;
224 /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
225 * the given connection. */
226 void
227 circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
229 tor_assert(out);
230 tor_assert(or_conn);
232 if (!circuits_pending_or_conns)
233 return;
235 SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns, circuit_t *, circ) {
236 if (circ->marked_for_close)
237 continue;
238 if (!circ->n_hop)
239 continue;
240 tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
241 if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
242 /* Look at addr/port. This is an unkeyed connection. */
243 if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) ||
244 circ->n_hop->port != or_conn->_base.port)
245 continue;
246 } else {
247 /* We expected a key. See if it's the right one. */
248 if (memcmp(or_conn->identity_digest,
249 circ->n_hop->identity_digest, DIGEST_LEN))
250 continue;
252 smartlist_add(out, circ);
253 } SMARTLIST_FOREACH_END(circ);
256 /** Return the number of circuits in state OR_WAIT, waiting for the given
257 * connection. */
259 circuit_count_pending_on_or_conn(or_connection_t *or_conn)
261 int cnt;
262 smartlist_t *sl = smartlist_create();
263 circuit_get_all_pending_on_or_conn(sl, or_conn);
264 cnt = smartlist_len(sl);
265 smartlist_free(sl);
266 log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
267 or_conn->nickname ? or_conn->nickname : "NULL", cnt);
268 return cnt;
271 /** Detach from the global circuit list, and deallocate, all
272 * circuits that have been marked for close.
274 void
275 circuit_close_all_marked(void)
277 circuit_t *tmp,*m;
279 while (global_circuitlist && global_circuitlist->marked_for_close) {
280 tmp = global_circuitlist->next;
281 circuit_free(global_circuitlist);
282 global_circuitlist = tmp;
285 tmp = global_circuitlist;
286 while (tmp && tmp->next) {
287 if (tmp->next->marked_for_close) {
288 m = tmp->next->next;
289 circuit_free(tmp->next);
290 tmp->next = m;
291 /* Need to check new tmp->next; don't advance tmp. */
292 } else {
293 /* Advance tmp. */
294 tmp = tmp->next;
299 /** Return the head of the global linked list of circuits. */
300 circuit_t *
301 _circuit_get_global_list(void)
303 return global_circuitlist;
306 /** Function to make circ-\>state human-readable */
307 const char *
308 circuit_state_to_string(int state)
310 static char buf[64];
311 switch (state) {
312 case CIRCUIT_STATE_BUILDING: return "doing handshakes";
313 case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
314 case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
315 case CIRCUIT_STATE_OPEN: return "open";
316 default:
317 log_warn(LD_BUG, "Unknown circuit state %d", state);
318 tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
319 return buf;
323 /** Map a circuit purpose to a string suitable to be displayed to a
324 * controller. */
325 const char *
326 circuit_purpose_to_controller_string(uint8_t purpose)
328 static char buf[32];
329 switch (purpose) {
330 case CIRCUIT_PURPOSE_OR:
331 case CIRCUIT_PURPOSE_INTRO_POINT:
332 case CIRCUIT_PURPOSE_REND_POINT_WAITING:
333 case CIRCUIT_PURPOSE_REND_ESTABLISHED:
334 return "SERVER"; /* A controller should never see these, actually. */
336 case CIRCUIT_PURPOSE_C_GENERAL:
337 return "GENERAL";
338 case CIRCUIT_PURPOSE_C_INTRODUCING:
339 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
340 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
341 return "HS_CLIENT_INTRO";
343 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
344 case CIRCUIT_PURPOSE_C_REND_READY:
345 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
346 case CIRCUIT_PURPOSE_C_REND_JOINED:
347 return "HS_CLIENT_REND";
349 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
350 case CIRCUIT_PURPOSE_S_INTRO:
351 return "HS_SERVICE_INTRO";
353 case CIRCUIT_PURPOSE_S_CONNECT_REND:
354 case CIRCUIT_PURPOSE_S_REND_JOINED:
355 return "HS_SERVICE_REND";
357 case CIRCUIT_PURPOSE_TESTING:
358 return "TESTING";
359 case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
360 return "EXPIRED";
361 case CIRCUIT_PURPOSE_CONTROLLER:
362 return "CONTROLLER";
364 default:
365 tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
366 return buf;
370 /** Pick a reasonable package_window to start out for our circuits.
371 * Originally this was hard-coded at 1000, but now the consensus votes
372 * on the answer. See proposal 168. */
373 int32_t
374 circuit_initial_package_window(void)
376 int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START);
377 /* If the consensus tells us a negative number, we'd assert. */
378 if (num < 0)
379 num = CIRCWINDOW_START;
380 return num;
383 /** Initialize the common elements in a circuit_t, and add it to the global
384 * list. */
385 static void
386 init_circuit_base(circuit_t *circ)
388 circ->timestamp_created = time(NULL);
389 tor_gettimeofday(&circ->highres_created);
391 circ->package_window = circuit_initial_package_window();
392 circ->deliver_window = CIRCWINDOW_START;
394 /* Initialize the cell_ewma_t structure */
395 circ->n_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
396 circ->n_cell_ewma.cell_count = 0.0;
397 circ->n_cell_ewma.heap_index = -1;
398 circ->n_cell_ewma.is_for_p_conn = 0;
400 circuit_add(circ);
403 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
404 * and <b>p_conn</b>. Add it to the global circuit list.
406 origin_circuit_t *
407 origin_circuit_new(void)
409 origin_circuit_t *circ;
410 /* never zero, since a global ID of 0 is treated specially by the
411 * controller */
412 static uint32_t n_circuits_allocated = 1;
414 circ = tor_malloc_zero(sizeof(origin_circuit_t));
415 circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
417 circ->next_stream_id = crypto_rand_int(1<<16);
418 circ->global_identifier = n_circuits_allocated++;
419 circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
420 circ->remaining_relay_early_cells -= crypto_rand_int(2);
422 init_circuit_base(TO_CIRCUIT(circ));
424 circ_times.last_circ_at = approx_time();
426 return circ;
429 /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
430 * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
431 or_circuit_t *
432 or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
434 /* CircIDs */
435 or_circuit_t *circ;
437 circ = tor_malloc_zero(sizeof(or_circuit_t));
438 circ->_base.magic = OR_CIRCUIT_MAGIC;
440 if (p_conn)
441 circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
443 circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
445 init_circuit_base(TO_CIRCUIT(circ));
447 /* Initialize the cell_ewma_t structure */
449 /* Initialize the cell counts to 0 */
450 circ->p_cell_ewma.cell_count = 0.0;
451 circ->p_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
452 circ->p_cell_ewma.is_for_p_conn = 1;
454 /* It's not in any heap yet. */
455 circ->p_cell_ewma.heap_index = -1;
457 return circ;
460 /** Deallocate space associated with circ.
462 static void
463 circuit_free(circuit_t *circ)
465 void *mem;
466 size_t memlen;
467 if (!circ)
468 return;
470 if (CIRCUIT_IS_ORIGIN(circ)) {
471 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
472 mem = ocirc;
473 memlen = sizeof(origin_circuit_t);
474 tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
475 if (ocirc->build_state) {
476 extend_info_free(ocirc->build_state->chosen_exit);
477 circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
479 tor_free(ocirc->build_state);
481 circuit_free_cpath(ocirc->cpath);
483 crypto_free_pk_env(ocirc->intro_key);
484 rend_data_free(ocirc->rend_data);
485 } else {
486 or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
487 /* Remember cell statistics for this circuit before deallocating. */
488 if (get_options()->CellStatistics)
489 rep_hist_buffer_stats_add_circ(circ, time(NULL));
490 mem = ocirc;
491 memlen = sizeof(or_circuit_t);
492 tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
494 crypto_free_cipher_env(ocirc->p_crypto);
495 crypto_free_digest_env(ocirc->p_digest);
496 crypto_free_cipher_env(ocirc->n_crypto);
497 crypto_free_digest_env(ocirc->n_digest);
499 if (ocirc->rend_splice) {
500 or_circuit_t *other = ocirc->rend_splice;
501 tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
502 other->rend_splice = NULL;
505 /* remove from map. */
506 circuit_set_p_circid_orconn(ocirc, 0, NULL);
508 /* Clear cell queue _after_ removing it from the map. Otherwise our
509 * "active" checks will be violated. */
510 cell_queue_clear(&ocirc->p_conn_cells);
513 extend_info_free(circ->n_hop);
514 tor_free(circ->n_conn_onionskin);
516 /* Remove from map. */
517 circuit_set_n_circid_orconn(circ, 0, NULL);
519 /* Clear cell queue _after_ removing it from the map. Otherwise our
520 * "active" checks will be violated. */
521 cell_queue_clear(&circ->n_conn_cells);
523 memset(mem, 0xAA, memlen); /* poison memory */
524 tor_free(mem);
527 /** Deallocate space associated with the linked list <b>cpath</b>. */
528 static void
529 circuit_free_cpath(crypt_path_t *cpath)
531 crypt_path_t *victim, *head=cpath;
533 if (!cpath)
534 return;
536 /* it's a doubly linked list, so we have to notice when we've
537 * gone through it once. */
538 while (cpath->next && cpath->next != head) {
539 victim = cpath;
540 cpath = victim->next;
541 circuit_free_cpath_node(victim);
544 circuit_free_cpath_node(cpath);
547 /** Release all storage held by circuits. */
548 void
549 circuit_free_all(void)
551 circuit_t *next;
552 while (global_circuitlist) {
553 next = global_circuitlist->next;
554 if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
555 or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
556 while (or_circ->resolving_streams) {
557 edge_connection_t *next_conn;
558 next_conn = or_circ->resolving_streams->next_stream;
559 connection_free(TO_CONN(or_circ->resolving_streams));
560 or_circ->resolving_streams = next_conn;
563 circuit_free(global_circuitlist);
564 global_circuitlist = next;
567 smartlist_free(circuits_pending_or_conns);
568 circuits_pending_or_conns = NULL;
570 HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
573 /** Deallocate space associated with the cpath node <b>victim</b>. */
574 static void
575 circuit_free_cpath_node(crypt_path_t *victim)
577 if (!victim)
578 return;
580 crypto_free_cipher_env(victim->f_crypto);
581 crypto_free_cipher_env(victim->b_crypto);
582 crypto_free_digest_env(victim->f_digest);
583 crypto_free_digest_env(victim->b_digest);
584 crypto_dh_free(victim->dh_handshake_state);
585 extend_info_free(victim->extend_info);
587 memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
588 tor_free(victim);
591 /** A helper function for circuit_dump_by_conn() below. Log a bunch
592 * of information about circuit <b>circ</b>.
594 static void
595 circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
596 const char *type, int this_circid, int other_circid)
598 log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
599 "state %d (%s), born %d:",
600 conn_array_index, type, this_circid, other_circid, circ->state,
601 circuit_state_to_string(circ->state), (int)circ->timestamp_created);
602 if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
603 circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
607 /** Log, at severity <b>severity</b>, information about each circuit
608 * that is connected to <b>conn</b>.
610 void
611 circuit_dump_by_conn(connection_t *conn, int severity)
613 circuit_t *circ;
614 edge_connection_t *tmpconn;
616 for (circ=global_circuitlist;circ;circ = circ->next) {
617 circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
618 if (circ->marked_for_close)
619 continue;
621 if (! CIRCUIT_IS_ORIGIN(circ))
622 p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
624 if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
625 TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
626 circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
627 p_circ_id, n_circ_id);
628 if (CIRCUIT_IS_ORIGIN(circ)) {
629 for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
630 tmpconn=tmpconn->next_stream) {
631 if (TO_CONN(tmpconn) == conn) {
632 circuit_dump_details(severity, circ, conn->conn_array_index,
633 "App-ward", p_circ_id, n_circ_id);
637 if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
638 circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
639 n_circ_id, p_circ_id);
640 if (! CIRCUIT_IS_ORIGIN(circ)) {
641 for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
642 tmpconn=tmpconn->next_stream) {
643 if (TO_CONN(tmpconn) == conn) {
644 circuit_dump_details(severity, circ, conn->conn_array_index,
645 "Exit-ward", n_circ_id, p_circ_id);
649 if (!circ->n_conn && circ->n_hop &&
650 tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
651 circ->n_hop->port == conn->port &&
652 conn->type == CONN_TYPE_OR &&
653 !memcmp(TO_OR_CONN(conn)->identity_digest,
654 circ->n_hop->identity_digest, DIGEST_LEN)) {
655 circuit_dump_details(severity, circ, conn->conn_array_index,
656 (circ->state == CIRCUIT_STATE_OPEN &&
657 !CIRCUIT_IS_ORIGIN(circ)) ?
658 "Endpoint" : "Pending",
659 n_circ_id, p_circ_id);
664 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
665 * such circuit exists. */
666 origin_circuit_t *
667 circuit_get_by_global_id(uint32_t id)
669 circuit_t *circ;
670 for (circ=global_circuitlist;circ;circ = circ->next) {
671 if (CIRCUIT_IS_ORIGIN(circ) &&
672 TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
673 if (circ->marked_for_close)
674 return NULL;
675 else
676 return TO_ORIGIN_CIRCUIT(circ);
679 return NULL;
682 /** Return a circ such that:
683 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
684 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
685 * Return NULL if no such circuit exists.
687 static INLINE circuit_t *
688 circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
690 orconn_circid_circuit_map_t search;
691 orconn_circid_circuit_map_t *found;
693 if (_last_circid_orconn_ent &&
694 circ_id == _last_circid_orconn_ent->circ_id &&
695 conn == _last_circid_orconn_ent->or_conn) {
696 found = _last_circid_orconn_ent;
697 } else {
698 search.circ_id = circ_id;
699 search.or_conn = conn;
700 found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
701 _last_circid_orconn_ent = found;
703 if (found && found->circuit)
704 return found->circuit;
706 return NULL;
708 /* The rest of this checks for bugs. Disabled by default. */
710 circuit_t *circ;
711 for (circ=global_circuitlist;circ;circ = circ->next) {
712 if (! CIRCUIT_IS_ORIGIN(circ)) {
713 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
714 if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
715 log_warn(LD_BUG,
716 "circuit matches p_conn, but not in hash table (Bug!)");
717 return circ;
720 if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
721 log_warn(LD_BUG,
722 "circuit matches n_conn, but not in hash table (Bug!)");
723 return circ;
726 return NULL;
730 /** Return a circ such that:
731 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
732 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
733 * - circ is not marked for close.
734 * Return NULL if no such circuit exists.
736 circuit_t *
737 circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
739 circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
740 if (!circ || circ->marked_for_close)
741 return NULL;
742 else
743 return circ;
746 /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
747 * circuit, marked or not, on <b>conn</b>. */
749 circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
751 return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
754 /** Return the circuit that a given edge connection is using. */
755 circuit_t *
756 circuit_get_by_edge_conn(edge_connection_t *conn)
758 circuit_t *circ;
760 circ = conn->on_circuit;
761 tor_assert(!circ ||
762 (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
763 : circ->magic == OR_CIRCUIT_MAGIC));
765 return circ;
768 /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
769 * circuit from the orconn,circid map, and mark it for close if it hasn't
770 * been marked already.
772 void
773 circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
775 circuit_t *circ;
777 connection_or_unlink_all_active_circs(conn);
779 for (circ = global_circuitlist; circ; circ = circ->next) {
780 int mark = 0;
781 if (circ->n_conn == conn) {
782 circuit_set_n_circid_orconn(circ, 0, NULL);
783 mark = 1;
785 if (! CIRCUIT_IS_ORIGIN(circ)) {
786 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
787 if (or_circ->p_conn == conn) {
788 circuit_set_p_circid_orconn(or_circ, 0, NULL);
789 mark = 1;
792 if (mark && !circ->marked_for_close)
793 circuit_mark_for_close(circ, reason);
797 /** Return a circ such that:
798 * - circ-\>rend_data-\>query is equal to <b>rend_query</b>, and
799 * - circ-\>purpose is equal to <b>purpose</b>.
801 * Return NULL if no such circuit exists.
803 origin_circuit_t *
804 circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
806 circuit_t *circ;
808 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
810 for (circ = global_circuitlist; circ; circ = circ->next) {
811 if (!circ->marked_for_close &&
812 circ->purpose == purpose) {
813 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
814 if (ocirc->rend_data &&
815 !rend_cmp_service_ids(rend_query,
816 ocirc->rend_data->onion_address))
817 return ocirc;
820 return NULL;
823 /** Return the first circuit originating here in global_circuitlist after
824 * <b>start</b> whose purpose is <b>purpose</b>, and where
825 * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
826 * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
828 origin_circuit_t *
829 circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
830 const char *digest, uint8_t purpose)
832 circuit_t *circ;
833 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
834 if (start == NULL)
835 circ = global_circuitlist;
836 else
837 circ = TO_CIRCUIT(start)->next;
839 for ( ; circ; circ = circ->next) {
840 if (circ->marked_for_close)
841 continue;
842 if (circ->purpose != purpose)
843 continue;
844 if (!digest)
845 return TO_ORIGIN_CIRCUIT(circ);
846 else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
847 !memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
848 digest, DIGEST_LEN))
849 return TO_ORIGIN_CIRCUIT(circ);
851 return NULL;
854 /** Return the first OR circuit in the global list whose purpose is
855 * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
856 * <b>token</b>. */
857 static or_circuit_t *
858 circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
859 size_t len)
861 circuit_t *circ;
862 for (circ = global_circuitlist; circ; circ = circ->next) {
863 if (! circ->marked_for_close &&
864 circ->purpose == purpose &&
865 ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
866 return TO_OR_CIRCUIT(circ);
868 return NULL;
871 /** Return the circuit waiting for a rendezvous with the provided cookie.
872 * Return NULL if no such circuit is found.
874 or_circuit_t *
875 circuit_get_rendezvous(const char *cookie)
877 return circuit_get_by_rend_token_and_purpose(
878 CIRCUIT_PURPOSE_REND_POINT_WAITING,
879 cookie, REND_COOKIE_LEN);
882 /** Return the circuit waiting for intro cells of the given digest.
883 * Return NULL if no such circuit is found.
885 or_circuit_t *
886 circuit_get_intro_point(const char *digest)
888 return circuit_get_by_rend_token_and_purpose(
889 CIRCUIT_PURPOSE_INTRO_POINT, digest,
890 DIGEST_LEN);
893 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
894 * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
895 * flags in <b>flags</b>, and if info is defined, does not already use info
896 * as any of its hops; or NULL if no circuit fits this description.
898 * The <b>purpose</b> argument (currently ignored) refers to the purpose of
899 * the circuit we want to create, not the purpose of the circuit we want to
900 * cannibalize.
902 * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
904 origin_circuit_t *
905 circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
906 int flags)
908 circuit_t *_circ;
909 origin_circuit_t *best=NULL;
910 int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
911 int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
912 int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
914 /* Make sure we're not trying to create a onehop circ by
915 * cannibalization. */
916 tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
918 log_debug(LD_CIRC,
919 "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
920 "capacity %d, internal %d",
921 purpose, need_uptime, need_capacity, internal);
923 for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
924 if (CIRCUIT_IS_ORIGIN(_circ) &&
925 _circ->state == CIRCUIT_STATE_OPEN &&
926 !_circ->marked_for_close &&
927 _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
928 !_circ->timestamp_dirty) {
929 origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
930 if ((!need_uptime || circ->build_state->need_uptime) &&
931 (!need_capacity || circ->build_state->need_capacity) &&
932 (internal == circ->build_state->is_internal) &&
933 circ->remaining_relay_early_cells &&
934 !circ->build_state->onehop_tunnel) {
935 if (info) {
936 /* need to make sure we don't duplicate hops */
937 crypt_path_t *hop = circ->cpath;
938 routerinfo_t *ri1 = router_get_by_digest(info->identity_digest);
939 do {
940 routerinfo_t *ri2;
941 if (!memcmp(hop->extend_info->identity_digest,
942 info->identity_digest, DIGEST_LEN))
943 goto next;
944 if (ri1 &&
945 (ri2 = router_get_by_digest(hop->extend_info->identity_digest))
946 && routers_in_same_family(ri1, ri2))
947 goto next;
948 hop=hop->next;
949 } while (hop!=circ->cpath);
951 if (!best || (best->build_state->need_uptime && !need_uptime))
952 best = circ;
953 next: ;
957 return best;
960 /** Return the number of hops in circuit's path. */
962 circuit_get_cpath_len(origin_circuit_t *circ)
964 int n = 0;
965 if (circ && circ->cpath) {
966 crypt_path_t *cpath, *cpath_next = NULL;
967 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
968 cpath_next = cpath->next;
969 ++n;
972 return n;
975 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
976 * aren't that many hops in the list. */
977 crypt_path_t *
978 circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
980 if (circ && circ->cpath && hopnum > 0) {
981 crypt_path_t *cpath, *cpath_next = NULL;
982 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
983 cpath_next = cpath->next;
984 if (--hopnum <= 0)
985 return cpath;
988 return NULL;
991 /** Go through the circuitlist; mark-for-close each circuit that starts
992 * at us but has not yet been used. */
993 void
994 circuit_mark_all_unused_circs(void)
996 circuit_t *circ;
998 for (circ=global_circuitlist; circ; circ = circ->next) {
999 if (CIRCUIT_IS_ORIGIN(circ) &&
1000 !circ->marked_for_close &&
1001 !circ->timestamp_dirty)
1002 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1006 /** Go through the circuitlist; for each circuit that starts at us
1007 * and is dirty, frob its timestamp_dirty so we won't use it for any
1008 * new streams.
1010 * This is useful for letting the user change pseudonyms, so new
1011 * streams will not be linkable to old streams.
1013 void
1014 circuit_expire_all_dirty_circs(void)
1016 circuit_t *circ;
1017 or_options_t *options = get_options();
1019 for (circ=global_circuitlist; circ; circ = circ->next) {
1020 if (CIRCUIT_IS_ORIGIN(circ) &&
1021 !circ->marked_for_close &&
1022 circ->timestamp_dirty)
1023 circ->timestamp_dirty -= options->MaxCircuitDirtiness;
1027 /** Mark <b>circ</b> to be closed next time we call
1028 * circuit_close_all_marked(). Do any cleanup needed:
1029 * - If state is onionskin_pending, remove circ from the onion_pending
1030 * list.
1031 * - If circ isn't open yet: call circuit_build_failed() if we're
1032 * the origin, and in either case call circuit_rep_hist_note_result()
1033 * to note stats.
1034 * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
1035 * just tried from our list of intro points for that service
1036 * descriptor.
1037 * - Send appropriate destroys and edge_destroys for conns and
1038 * streams attached to circ.
1039 * - If circ->rend_splice is set (we are the midpoint of a joined
1040 * rendezvous stream), then mark the other circuit to close as well.
1042 void
1043 _circuit_mark_for_close(circuit_t *circ, int reason, int line,
1044 const char *file)
1046 int orig_reason = reason; /* Passed to the controller */
1047 assert_circuit_ok(circ);
1048 tor_assert(line);
1049 tor_assert(file);
1051 if (circ->marked_for_close) {
1052 log(LOG_WARN,LD_BUG,
1053 "Duplicate call to circuit_mark_for_close at %s:%d"
1054 " (first at %s:%d)", file, line,
1055 circ->marked_for_close_file, circ->marked_for_close);
1056 return;
1058 if (reason == END_CIRC_AT_ORIGIN) {
1059 if (!CIRCUIT_IS_ORIGIN(circ)) {
1060 log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
1061 "but circuit was not at origin. (called %s:%d, purpose=%d)",
1062 file, line, circ->purpose);
1064 reason = END_CIRC_REASON_NONE;
1066 if (CIRCUIT_IS_ORIGIN(circ)) {
1067 /* We don't send reasons when closing circuits at the origin. */
1068 reason = END_CIRC_REASON_NONE;
1071 if (reason & END_CIRC_REASON_FLAG_REMOTE)
1072 reason &= ~END_CIRC_REASON_FLAG_REMOTE;
1074 if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
1075 if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
1076 log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
1077 reason = END_CIRC_REASON_NONE;
1080 if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
1081 onion_pending_remove(TO_OR_CIRCUIT(circ));
1083 /* If the circuit ever became OPEN, we sent it to the reputation history
1084 * module then. If it isn't OPEN, we send it there now to remember which
1085 * links worked and which didn't.
1087 if (circ->state != CIRCUIT_STATE_OPEN) {
1088 if (CIRCUIT_IS_ORIGIN(circ)) {
1089 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1090 circuit_build_failed(ocirc); /* take actions if necessary */
1091 circuit_rep_hist_note_result(ocirc);
1094 if (circ->state == CIRCUIT_STATE_OR_WAIT) {
1095 if (circuits_pending_or_conns)
1096 smartlist_remove(circuits_pending_or_conns, circ);
1098 if (CIRCUIT_IS_ORIGIN(circ)) {
1099 control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
1100 (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
1101 orig_reason);
1103 if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
1104 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1105 tor_assert(circ->state == CIRCUIT_STATE_OPEN);
1106 tor_assert(ocirc->build_state->chosen_exit);
1107 tor_assert(ocirc->rend_data);
1108 /* treat this like getting a nack from it */
1109 log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
1110 "Removing from descriptor.",
1111 safe_str_client(ocirc->rend_data->onion_address),
1112 safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
1113 rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
1114 ocirc->rend_data);
1116 if (circ->n_conn)
1117 connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
1119 if (! CIRCUIT_IS_ORIGIN(circ)) {
1120 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1121 edge_connection_t *conn;
1122 for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
1123 connection_edge_destroy(or_circ->p_circ_id, conn);
1124 or_circ->n_streams = NULL;
1126 while (or_circ->resolving_streams) {
1127 conn = or_circ->resolving_streams;
1128 or_circ->resolving_streams = conn->next_stream;
1129 if (!conn->_base.marked_for_close) {
1130 /* The client will see a DESTROY, and infer that the connections
1131 * are closing because the circuit is getting torn down. No need
1132 * to send an end cell. */
1133 conn->edge_has_sent_end = 1;
1134 conn->end_reason = END_STREAM_REASON_DESTROY;
1135 conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
1136 connection_mark_for_close(TO_CONN(conn));
1138 conn->on_circuit = NULL;
1141 if (or_circ->p_conn)
1142 connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
1143 } else {
1144 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1145 edge_connection_t *conn;
1146 for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
1147 connection_edge_destroy(circ->n_circ_id, conn);
1148 ocirc->p_streams = NULL;
1151 circ->marked_for_close = line;
1152 circ->marked_for_close_file = file;
1154 if (!CIRCUIT_IS_ORIGIN(circ)) {
1155 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1156 if (or_circ->rend_splice) {
1157 if (!or_circ->rend_splice->_base.marked_for_close) {
1158 /* do this after marking this circuit, to avoid infinite recursion. */
1159 circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
1161 or_circ->rend_splice = NULL;
1166 /** Verify that cpath layer <b>cp</b> has all of its invariants
1167 * correct. Trigger an assert if anything is invalid.
1169 void
1170 assert_cpath_layer_ok(const crypt_path_t *cp)
1172 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
1173 // tor_assert(cp->port);
1174 tor_assert(cp);
1175 tor_assert(cp->magic == CRYPT_PATH_MAGIC);
1176 switch (cp->state)
1178 case CPATH_STATE_OPEN:
1179 tor_assert(cp->f_crypto);
1180 tor_assert(cp->b_crypto);
1181 /* fall through */
1182 case CPATH_STATE_CLOSED:
1183 tor_assert(!cp->dh_handshake_state);
1184 break;
1185 case CPATH_STATE_AWAITING_KEYS:
1186 /* tor_assert(cp->dh_handshake_state); */
1187 break;
1188 default:
1189 log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
1190 tor_assert(0);
1192 tor_assert(cp->package_window >= 0);
1193 tor_assert(cp->deliver_window >= 0);
1196 /** Verify that cpath <b>cp</b> has all of its invariants
1197 * correct. Trigger an assert if anything is invalid.
1199 static void
1200 assert_cpath_ok(const crypt_path_t *cp)
1202 const crypt_path_t *start = cp;
1204 do {
1205 assert_cpath_layer_ok(cp);
1206 /* layers must be in sequence of: "open* awaiting? closed*" */
1207 if (cp != start) {
1208 if (cp->state == CPATH_STATE_AWAITING_KEYS) {
1209 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
1210 } else if (cp->state == CPATH_STATE_OPEN) {
1211 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
1214 cp = cp->next;
1215 tor_assert(cp);
1216 } while (cp != start);
1219 /** Verify that circuit <b>c</b> has all of its invariants
1220 * correct. Trigger an assert if anything is invalid.
1222 void
1223 assert_circuit_ok(const circuit_t *c)
1225 edge_connection_t *conn;
1226 const or_circuit_t *or_circ = NULL;
1227 const origin_circuit_t *origin_circ = NULL;
1229 tor_assert(c);
1230 tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
1231 tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
1232 c->purpose <= _CIRCUIT_PURPOSE_MAX);
1235 /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
1236 * never understand. -NM. */
1237 circuit_t *nonconst_circ = (circuit_t*) c;
1238 if (CIRCUIT_IS_ORIGIN(c))
1239 origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
1240 else
1241 or_circ = TO_OR_CIRCUIT(nonconst_circ);
1244 if (c->n_conn) {
1245 tor_assert(!c->n_hop);
1247 if (c->n_circ_id) {
1248 /* We use the _impl variant here to make sure we don't fail on marked
1249 * circuits, which would not be returned by the regular function. */
1250 circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
1251 c->n_conn);
1252 tor_assert(c == c2);
1255 if (or_circ && or_circ->p_conn) {
1256 if (or_circ->p_circ_id) {
1257 /* ibid */
1258 circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
1259 or_circ->p_conn);
1260 tor_assert(c == c2);
1263 if (or_circ)
1264 for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
1265 tor_assert(conn->_base.type == CONN_TYPE_EXIT);
1267 tor_assert(c->deliver_window >= 0);
1268 tor_assert(c->package_window >= 0);
1269 if (c->state == CIRCUIT_STATE_OPEN) {
1270 tor_assert(!c->n_conn_onionskin);
1271 if (or_circ) {
1272 tor_assert(or_circ->n_crypto);
1273 tor_assert(or_circ->p_crypto);
1274 tor_assert(or_circ->n_digest);
1275 tor_assert(or_circ->p_digest);
1278 if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
1279 tor_assert(circuits_pending_or_conns &&
1280 smartlist_isin(circuits_pending_or_conns, c));
1281 } else {
1282 tor_assert(!circuits_pending_or_conns ||
1283 !smartlist_isin(circuits_pending_or_conns, c));
1285 if (origin_circ && origin_circ->cpath) {
1286 assert_cpath_ok(origin_circ->cpath);
1288 if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
1289 tor_assert(or_circ);
1290 if (!c->marked_for_close) {
1291 tor_assert(or_circ->rend_splice);
1292 tor_assert(or_circ->rend_splice->rend_splice == or_circ);
1294 tor_assert(or_circ->rend_splice != or_circ);
1295 } else {
1296 tor_assert(!or_circ || !or_circ->rend_splice);