naked constants are bad
[tor/rransom.git] / src / or / circuitlist.c
blobe18e8926610f63e8ea6478d86c728d1fe8859d37
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 "ht.h"
15 /********* START VARIABLES **********/
17 /** A global list of all circuits at this hop. */
18 circuit_t *global_circuitlist=NULL;
20 /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
21 static smartlist_t *circuits_pending_or_conns=NULL;
23 static void circuit_free(circuit_t *circ);
24 static void circuit_free_cpath(crypt_path_t *cpath);
25 static void circuit_free_cpath_node(crypt_path_t *victim);
27 /********* END VARIABLES ************/
29 /** A map from OR connection and circuit ID to circuit. (Lookup performance is
30 * very important here, since we need to do it every time a cell arrives.) */
31 typedef struct orconn_circid_circuit_map_t {
32 HT_ENTRY(orconn_circid_circuit_map_t) node;
33 or_connection_t *or_conn;
34 circid_t circ_id;
35 circuit_t *circuit;
36 } orconn_circid_circuit_map_t;
38 /** Helper for hash tables: compare the OR connection and circuit ID for a and
39 * b, and return less than, equal to, or greater than zero appropriately.
41 static INLINE int
42 _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
43 orconn_circid_circuit_map_t *b)
45 return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
48 /** Helper: return a hash based on circuit ID and the pointer value of
49 * or_conn in <b>a</b>. */
50 static INLINE unsigned int
51 _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
53 return (((unsigned)a->circ_id)<<8) ^ (unsigned)(uintptr_t)(a->or_conn);
56 /** Map from [orconn,circid] to circuit. */
57 static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
58 orconn_circid_circuit_map = HT_INITIALIZER();
59 HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
60 _orconn_circid_entry_hash, _orconn_circid_entries_eq)
61 HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
62 _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
63 malloc, realloc, free)
65 /** The most recently returned entry from circuit_get_by_circid_orconn;
66 * used to improve performance when many cells arrive in a row from the
67 * same circuit.
69 orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
71 /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
72 * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
73 * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
74 * the old entry (if any) and adding a new one. If <b>active</b> is true,
75 * remove the circuit from the list of active circuits on old_conn and add it
76 * to the list of active circuits on conn.
77 * XXX "active" isn't an arg anymore */
78 static void
79 circuit_set_circid_orconn_helper(circuit_t *circ, int direction,
80 circid_t id,
81 or_connection_t *conn)
83 orconn_circid_circuit_map_t search;
84 orconn_circid_circuit_map_t *found;
85 or_connection_t *old_conn, **conn_ptr;
86 circid_t old_id, *circid_ptr;
87 int was_active, make_active;
89 if (direction == CELL_DIRECTION_OUT) {
90 conn_ptr = &circ->n_conn;
91 circid_ptr = &circ->n_circ_id;
92 was_active = circ->next_active_on_n_conn != NULL;
93 make_active = circ->n_conn_cells.n > 0;
94 } else {
95 or_circuit_t *c = TO_OR_CIRCUIT(circ);
96 conn_ptr = &c->p_conn;
97 circid_ptr = &c->p_circ_id;
98 was_active = c->next_active_on_p_conn != NULL;
99 make_active = c->p_conn_cells.n > 0;
101 old_conn = *conn_ptr;
102 old_id = *circid_ptr;
104 if (id == old_id && conn == old_conn)
105 return;
107 if (_last_circid_orconn_ent &&
108 ((old_id == _last_circid_orconn_ent->circ_id &&
109 old_conn == _last_circid_orconn_ent->or_conn) ||
110 (id == _last_circid_orconn_ent->circ_id &&
111 conn == _last_circid_orconn_ent->or_conn))) {
112 _last_circid_orconn_ent = NULL;
115 if (old_conn) { /* we may need to remove it from the conn-circid map */
116 tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
117 search.circ_id = old_id;
118 search.or_conn = old_conn;
119 found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
120 if (found) {
121 tor_free(found);
122 --old_conn->n_circuits;
124 if (was_active && old_conn != conn)
125 make_circuit_inactive_on_conn(circ,old_conn);
128 /* Change the values only after we have possibly made the circuit inactive
129 * on the previous conn. */
130 *conn_ptr = conn;
131 *circid_ptr = id;
133 if (conn == NULL)
134 return;
136 /* now add the new one to the conn-circid map */
137 search.circ_id = id;
138 search.or_conn = conn;
139 found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
140 if (found) {
141 found->circuit = circ;
142 } else {
143 found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
144 found->circ_id = id;
145 found->or_conn = conn;
146 found->circuit = circ;
147 HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
149 if (make_active && old_conn != conn)
150 make_circuit_active_on_conn(circ,conn);
152 ++conn->n_circuits;
155 /** Set the p_conn field of a circuit <b>circ</b>, along
156 * with the corresponding circuit ID, and add the circuit as appropriate
157 * to the (orconn,id)-\>circuit map. */
158 void
159 circuit_set_p_circid_orconn(or_circuit_t *circ, circid_t id,
160 or_connection_t *conn)
162 circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), CELL_DIRECTION_IN,
163 id, conn);
165 if (conn)
166 tor_assert(bool_eq(circ->p_conn_cells.n, circ->next_active_on_p_conn));
169 /** Set the n_conn field of a circuit <b>circ</b>, along
170 * with the corresponding circuit ID, and add the circuit as appropriate
171 * to the (orconn,id)-\>circuit map. */
172 void
173 circuit_set_n_circid_orconn(circuit_t *circ, circid_t id,
174 or_connection_t *conn)
176 circuit_set_circid_orconn_helper(circ, CELL_DIRECTION_OUT, id, conn);
178 if (conn)
179 tor_assert(bool_eq(circ->n_conn_cells.n, circ->next_active_on_n_conn));
182 /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
183 * it from lists as appropriate. */
184 void
185 circuit_set_state(circuit_t *circ, uint8_t state)
187 tor_assert(circ);
188 if (state == circ->state)
189 return;
190 if (!circuits_pending_or_conns)
191 circuits_pending_or_conns = smartlist_create();
192 if (circ->state == CIRCUIT_STATE_OR_WAIT) {
193 /* remove from waiting-circuit list. */
194 smartlist_remove(circuits_pending_or_conns, circ);
196 if (state == CIRCUIT_STATE_OR_WAIT) {
197 /* add to waiting-circuit list. */
198 smartlist_add(circuits_pending_or_conns, circ);
200 if (state == CIRCUIT_STATE_OPEN)
201 tor_assert(!circ->n_conn_onionskin);
202 circ->state = state;
205 /** Add <b>circ</b> to the global list of circuits. This is called only from
206 * within circuit_new.
208 static void
209 circuit_add(circuit_t *circ)
211 if (!global_circuitlist) { /* first one */
212 global_circuitlist = circ;
213 circ->next = NULL;
214 } else {
215 circ->next = global_circuitlist;
216 global_circuitlist = circ;
220 /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
221 * the given connection. */
222 void
223 circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
225 tor_assert(out);
226 tor_assert(or_conn);
228 if (!circuits_pending_or_conns)
229 return;
231 SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns, circuit_t *, circ) {
232 if (circ->marked_for_close)
233 continue;
234 if (!circ->n_hop)
235 continue;
236 tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
237 if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
238 /* Look at addr/port. This is an unkeyed connection. */
239 if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) ||
240 circ->n_hop->port != or_conn->_base.port)
241 continue;
242 } else {
243 /* We expected a key. See if it's the right one. */
244 if (memcmp(or_conn->identity_digest,
245 circ->n_hop->identity_digest, DIGEST_LEN))
246 continue;
248 smartlist_add(out, circ);
249 } SMARTLIST_FOREACH_END(circ);
252 /** Return the number of circuits in state OR_WAIT, waiting for the given
253 * connection. */
255 circuit_count_pending_on_or_conn(or_connection_t *or_conn)
257 int cnt;
258 smartlist_t *sl = smartlist_create();
259 circuit_get_all_pending_on_or_conn(sl, or_conn);
260 cnt = smartlist_len(sl);
261 smartlist_free(sl);
262 log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
263 or_conn->nickname ? or_conn->nickname : "NULL", cnt);
264 return cnt;
267 /** Detach from the global circuit list, and deallocate, all
268 * circuits that have been marked for close.
270 void
271 circuit_close_all_marked(void)
273 circuit_t *tmp,*m;
275 while (global_circuitlist && global_circuitlist->marked_for_close) {
276 tmp = global_circuitlist->next;
277 circuit_free(global_circuitlist);
278 global_circuitlist = tmp;
281 tmp = global_circuitlist;
282 while (tmp && tmp->next) {
283 if (tmp->next->marked_for_close) {
284 m = tmp->next->next;
285 circuit_free(tmp->next);
286 tmp->next = m;
287 /* Need to check new tmp->next; don't advance tmp. */
288 } else {
289 /* Advance tmp. */
290 tmp = tmp->next;
295 /** Return the head of the global linked list of circuits. */
296 circuit_t *
297 _circuit_get_global_list(void)
299 return global_circuitlist;
302 /** Function to make circ-\>state human-readable */
303 const char *
304 circuit_state_to_string(int state)
306 static char buf[64];
307 switch (state) {
308 case CIRCUIT_STATE_BUILDING: return "doing handshakes";
309 case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
310 case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
311 case CIRCUIT_STATE_OPEN: return "open";
312 default:
313 log_warn(LD_BUG, "Unknown circuit state %d", state);
314 tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
315 return buf;
319 /** Map a circuit purpose to a string suitable to be displayed to a
320 * controller. */
321 const char *
322 circuit_purpose_to_controller_string(uint8_t purpose)
324 static char buf[32];
325 switch (purpose) {
326 case CIRCUIT_PURPOSE_OR:
327 case CIRCUIT_PURPOSE_INTRO_POINT:
328 case CIRCUIT_PURPOSE_REND_POINT_WAITING:
329 case CIRCUIT_PURPOSE_REND_ESTABLISHED:
330 return "SERVER"; /* A controller should never see these, actually. */
332 case CIRCUIT_PURPOSE_C_GENERAL:
333 return "GENERAL";
334 case CIRCUIT_PURPOSE_C_INTRODUCING:
335 case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
336 case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
337 return "HS_CLIENT_INTRO";
339 case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
340 case CIRCUIT_PURPOSE_C_REND_READY:
341 case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
342 case CIRCUIT_PURPOSE_C_REND_JOINED:
343 return "HS_CLIENT_REND";
345 case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
346 case CIRCUIT_PURPOSE_S_INTRO:
347 return "HS_SERVICE_INTRO";
349 case CIRCUIT_PURPOSE_S_CONNECT_REND:
350 case CIRCUIT_PURPOSE_S_REND_JOINED:
351 return "HS_SERVICE_REND";
353 case CIRCUIT_PURPOSE_TESTING:
354 return "TESTING";
355 case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
356 return "EXPIRED";
357 case CIRCUIT_PURPOSE_CONTROLLER:
358 return "CONTROLLER";
360 default:
361 tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
362 return buf;
366 /** Pick a reasonable package_window to start out for our circuits.
367 * Originally this was hard-coded at 1000, but now the consensus votes
368 * on the answer. See proposal 168. */
369 int32_t
370 circuit_initial_package_window(void)
372 int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START);
373 /* If the consensus tells us a negative number, we'd assert. */
374 if (num < 0)
375 num = CIRCWINDOW_START;
376 return num;
379 /** Initialize the common elements in a circuit_t, and add it to the global
380 * list. */
381 static void
382 init_circuit_base(circuit_t *circ)
384 circ->timestamp_created = time(NULL);
385 tor_gettimeofday(&circ->highres_created);
387 circ->package_window = circuit_initial_package_window();
388 circ->deliver_window = CIRCWINDOW_START;
390 /* Initialize the cell_ewma_t structure */
391 circ->n_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
392 circ->n_cell_ewma.cell_count = 0.0;
393 circ->n_cell_ewma.heap_index = -1;
394 circ->n_cell_ewma.is_for_p_conn = 0;
396 circuit_add(circ);
399 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
400 * and <b>p_conn</b>. Add it to the global circuit list.
402 origin_circuit_t *
403 origin_circuit_new(void)
405 origin_circuit_t *circ;
406 /* never zero, since a global ID of 0 is treated specially by the
407 * controller */
408 static uint32_t n_circuits_allocated = 1;
410 circ = tor_malloc_zero(sizeof(origin_circuit_t));
411 circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
413 circ->next_stream_id = crypto_rand_int(1<<16);
414 circ->global_identifier = n_circuits_allocated++;
415 circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
416 circ->remaining_relay_early_cells -= crypto_rand_int(2);
418 init_circuit_base(TO_CIRCUIT(circ));
420 circ_times.last_circ_at = approx_time();
422 return circ;
425 /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
426 * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
427 or_circuit_t *
428 or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
430 /* CircIDs */
431 or_circuit_t *circ;
433 circ = tor_malloc_zero(sizeof(or_circuit_t));
434 circ->_base.magic = OR_CIRCUIT_MAGIC;
436 if (p_conn)
437 circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
439 circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
441 init_circuit_base(TO_CIRCUIT(circ));
443 /* Initialize the cell_ewma_t structure */
445 /* Initialize the cell counts to 0 */
446 circ->p_cell_ewma.cell_count = 0.0;
447 circ->p_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
448 circ->p_cell_ewma.is_for_p_conn = 1;
450 /* It's not in any heap yet. */
451 circ->p_cell_ewma.heap_index = -1;
453 return circ;
456 /** Deallocate space associated with circ.
458 static void
459 circuit_free(circuit_t *circ)
461 void *mem;
462 size_t memlen;
463 if (!circ)
464 return;
466 if (CIRCUIT_IS_ORIGIN(circ)) {
467 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
468 mem = ocirc;
469 memlen = sizeof(origin_circuit_t);
470 tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
471 if (ocirc->build_state) {
472 extend_info_free(ocirc->build_state->chosen_exit);
473 circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
475 tor_free(ocirc->build_state);
477 circuit_free_cpath(ocirc->cpath);
479 crypto_free_pk_env(ocirc->intro_key);
480 rend_data_free(ocirc->rend_data);
481 } else {
482 or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
483 /* Remember cell statistics for this circuit before deallocating. */
484 if (get_options()->CellStatistics)
485 rep_hist_buffer_stats_add_circ(circ, time(NULL));
486 mem = ocirc;
487 memlen = sizeof(or_circuit_t);
488 tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
490 crypto_free_cipher_env(ocirc->p_crypto);
491 crypto_free_digest_env(ocirc->p_digest);
492 crypto_free_cipher_env(ocirc->n_crypto);
493 crypto_free_digest_env(ocirc->n_digest);
495 if (ocirc->rend_splice) {
496 or_circuit_t *other = ocirc->rend_splice;
497 tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
498 other->rend_splice = NULL;
501 /* remove from map. */
502 circuit_set_p_circid_orconn(ocirc, 0, NULL);
504 /* Clear cell queue _after_ removing it from the map. Otherwise our
505 * "active" checks will be violated. */
506 cell_queue_clear(&ocirc->p_conn_cells);
509 extend_info_free(circ->n_hop);
510 tor_free(circ->n_conn_onionskin);
512 /* Remove from map. */
513 circuit_set_n_circid_orconn(circ, 0, NULL);
515 /* Clear cell queue _after_ removing it from the map. Otherwise our
516 * "active" checks will be violated. */
517 cell_queue_clear(&circ->n_conn_cells);
519 memset(mem, 0xAA, memlen); /* poison memory */
520 tor_free(mem);
523 /** Deallocate space associated with the linked list <b>cpath</b>. */
524 static void
525 circuit_free_cpath(crypt_path_t *cpath)
527 crypt_path_t *victim, *head=cpath;
529 if (!cpath)
530 return;
532 /* it's a doubly linked list, so we have to notice when we've
533 * gone through it once. */
534 while (cpath->next && cpath->next != head) {
535 victim = cpath;
536 cpath = victim->next;
537 circuit_free_cpath_node(victim);
540 circuit_free_cpath_node(cpath);
543 /** Release all storage held by circuits. */
544 void
545 circuit_free_all(void)
547 circuit_t *next;
548 while (global_circuitlist) {
549 next = global_circuitlist->next;
550 if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
551 or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
552 while (or_circ->resolving_streams) {
553 edge_connection_t *next_conn;
554 next_conn = or_circ->resolving_streams->next_stream;
555 connection_free(TO_CONN(or_circ->resolving_streams));
556 or_circ->resolving_streams = next_conn;
559 circuit_free(global_circuitlist);
560 global_circuitlist = next;
563 smartlist_free(circuits_pending_or_conns);
564 circuits_pending_or_conns = NULL;
566 HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
569 /** Deallocate space associated with the cpath node <b>victim</b>. */
570 static void
571 circuit_free_cpath_node(crypt_path_t *victim)
573 if (!victim)
574 return;
576 crypto_free_cipher_env(victim->f_crypto);
577 crypto_free_cipher_env(victim->b_crypto);
578 crypto_free_digest_env(victim->f_digest);
579 crypto_free_digest_env(victim->b_digest);
580 crypto_dh_free(victim->dh_handshake_state);
581 extend_info_free(victim->extend_info);
583 memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
584 tor_free(victim);
587 /** A helper function for circuit_dump_by_conn() below. Log a bunch
588 * of information about circuit <b>circ</b>.
590 static void
591 circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
592 const char *type, int this_circid, int other_circid)
594 log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
595 "state %d (%s), born %d:",
596 conn_array_index, type, this_circid, other_circid, circ->state,
597 circuit_state_to_string(circ->state), (int)circ->timestamp_created);
598 if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
599 circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
603 /** Log, at severity <b>severity</b>, information about each circuit
604 * that is connected to <b>conn</b>.
606 void
607 circuit_dump_by_conn(connection_t *conn, int severity)
609 circuit_t *circ;
610 edge_connection_t *tmpconn;
612 for (circ=global_circuitlist;circ;circ = circ->next) {
613 circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
614 if (circ->marked_for_close)
615 continue;
617 if (! CIRCUIT_IS_ORIGIN(circ))
618 p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
620 if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
621 TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
622 circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
623 p_circ_id, n_circ_id);
624 if (CIRCUIT_IS_ORIGIN(circ)) {
625 for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
626 tmpconn=tmpconn->next_stream) {
627 if (TO_CONN(tmpconn) == conn) {
628 circuit_dump_details(severity, circ, conn->conn_array_index,
629 "App-ward", p_circ_id, n_circ_id);
633 if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
634 circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
635 n_circ_id, p_circ_id);
636 if (! CIRCUIT_IS_ORIGIN(circ)) {
637 for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
638 tmpconn=tmpconn->next_stream) {
639 if (TO_CONN(tmpconn) == conn) {
640 circuit_dump_details(severity, circ, conn->conn_array_index,
641 "Exit-ward", n_circ_id, p_circ_id);
645 if (!circ->n_conn && circ->n_hop &&
646 tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
647 circ->n_hop->port == conn->port &&
648 conn->type == CONN_TYPE_OR &&
649 !memcmp(TO_OR_CONN(conn)->identity_digest,
650 circ->n_hop->identity_digest, DIGEST_LEN)) {
651 circuit_dump_details(severity, circ, conn->conn_array_index,
652 (circ->state == CIRCUIT_STATE_OPEN &&
653 !CIRCUIT_IS_ORIGIN(circ)) ?
654 "Endpoint" : "Pending",
655 n_circ_id, p_circ_id);
660 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
661 * such circuit exists. */
662 origin_circuit_t *
663 circuit_get_by_global_id(uint32_t id)
665 circuit_t *circ;
666 for (circ=global_circuitlist;circ;circ = circ->next) {
667 if (CIRCUIT_IS_ORIGIN(circ) &&
668 TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
669 if (circ->marked_for_close)
670 return NULL;
671 else
672 return TO_ORIGIN_CIRCUIT(circ);
675 return NULL;
678 /** Return a circ such that:
679 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
680 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
681 * Return NULL if no such circuit exists.
683 static INLINE circuit_t *
684 circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
686 orconn_circid_circuit_map_t search;
687 orconn_circid_circuit_map_t *found;
689 if (_last_circid_orconn_ent &&
690 circ_id == _last_circid_orconn_ent->circ_id &&
691 conn == _last_circid_orconn_ent->or_conn) {
692 found = _last_circid_orconn_ent;
693 } else {
694 search.circ_id = circ_id;
695 search.or_conn = conn;
696 found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
697 _last_circid_orconn_ent = found;
699 if (found && found->circuit)
700 return found->circuit;
702 return NULL;
704 /* The rest of this checks for bugs. Disabled by default. */
706 circuit_t *circ;
707 for (circ=global_circuitlist;circ;circ = circ->next) {
708 if (! CIRCUIT_IS_ORIGIN(circ)) {
709 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
710 if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
711 log_warn(LD_BUG,
712 "circuit matches p_conn, but not in hash table (Bug!)");
713 return circ;
716 if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
717 log_warn(LD_BUG,
718 "circuit matches n_conn, but not in hash table (Bug!)");
719 return circ;
722 return NULL;
726 /** Return a circ such that:
727 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
728 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
729 * - circ is not marked for close.
730 * Return NULL if no such circuit exists.
732 circuit_t *
733 circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
735 circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
736 if (!circ || circ->marked_for_close)
737 return NULL;
738 else
739 return circ;
742 /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
743 * circuit, marked or not, on <b>conn</b>. */
745 circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
747 return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
750 /** Return the circuit that a given edge connection is using. */
751 circuit_t *
752 circuit_get_by_edge_conn(edge_connection_t *conn)
754 circuit_t *circ;
756 circ = conn->on_circuit;
757 tor_assert(!circ ||
758 (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
759 : circ->magic == OR_CIRCUIT_MAGIC));
761 return circ;
764 /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
765 * circuit from the orconn,circid map, and mark it for close if it hasn't
766 * been marked already.
768 void
769 circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
771 circuit_t *circ;
773 connection_or_unlink_all_active_circs(conn);
775 for (circ = global_circuitlist; circ; circ = circ->next) {
776 int mark = 0;
777 if (circ->n_conn == conn) {
778 circuit_set_n_circid_orconn(circ, 0, NULL);
779 mark = 1;
781 if (! CIRCUIT_IS_ORIGIN(circ)) {
782 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
783 if (or_circ->p_conn == conn) {
784 circuit_set_p_circid_orconn(or_circ, 0, NULL);
785 mark = 1;
788 if (mark && !circ->marked_for_close)
789 circuit_mark_for_close(circ, reason);
793 /** Return a circ such that:
794 * - circ-\>rend_data-\>query is equal to <b>rend_query</b>, and
795 * - circ-\>purpose is equal to <b>purpose</b>.
797 * Return NULL if no such circuit exists.
799 origin_circuit_t *
800 circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
802 circuit_t *circ;
804 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
806 for (circ = global_circuitlist; circ; circ = circ->next) {
807 if (!circ->marked_for_close &&
808 circ->purpose == purpose) {
809 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
810 if (ocirc->rend_data &&
811 !rend_cmp_service_ids(rend_query,
812 ocirc->rend_data->onion_address))
813 return ocirc;
816 return NULL;
819 /** Return the first circuit originating here in global_circuitlist after
820 * <b>start</b> whose purpose is <b>purpose</b>, and where
821 * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
822 * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
824 origin_circuit_t *
825 circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
826 const char *digest, uint8_t purpose)
828 circuit_t *circ;
829 tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
830 if (start == NULL)
831 circ = global_circuitlist;
832 else
833 circ = TO_CIRCUIT(start)->next;
835 for ( ; circ; circ = circ->next) {
836 if (circ->marked_for_close)
837 continue;
838 if (circ->purpose != purpose)
839 continue;
840 if (!digest)
841 return TO_ORIGIN_CIRCUIT(circ);
842 else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
843 !memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
844 digest, DIGEST_LEN))
845 return TO_ORIGIN_CIRCUIT(circ);
847 return NULL;
850 /** Return the first OR circuit in the global list whose purpose is
851 * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
852 * <b>token</b>. */
853 static or_circuit_t *
854 circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
855 size_t len)
857 circuit_t *circ;
858 for (circ = global_circuitlist; circ; circ = circ->next) {
859 if (! circ->marked_for_close &&
860 circ->purpose == purpose &&
861 ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
862 return TO_OR_CIRCUIT(circ);
864 return NULL;
867 /** Return the circuit waiting for a rendezvous with the provided cookie.
868 * Return NULL if no such circuit is found.
870 or_circuit_t *
871 circuit_get_rendezvous(const char *cookie)
873 return circuit_get_by_rend_token_and_purpose(
874 CIRCUIT_PURPOSE_REND_POINT_WAITING,
875 cookie, REND_COOKIE_LEN);
878 /** Return the circuit waiting for intro cells of the given digest.
879 * Return NULL if no such circuit is found.
881 or_circuit_t *
882 circuit_get_intro_point(const char *digest)
884 return circuit_get_by_rend_token_and_purpose(
885 CIRCUIT_PURPOSE_INTRO_POINT, digest,
886 DIGEST_LEN);
889 /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
890 * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
891 * flags in <b>flags</b>, and if info is defined, does not already use info
892 * as any of its hops; or NULL if no circuit fits this description.
894 * The <b>purpose</b> argument (currently ignored) refers to the purpose of
895 * the circuit we want to create, not the purpose of the circuit we want to
896 * cannibalize.
898 * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
900 origin_circuit_t *
901 circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
902 int flags)
904 circuit_t *_circ;
905 origin_circuit_t *best=NULL;
906 int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
907 int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
908 int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
910 /* Make sure we're not trying to create a onehop circ by
911 * cannibalization. */
912 tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
914 log_debug(LD_CIRC,
915 "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
916 "capacity %d, internal %d",
917 purpose, need_uptime, need_capacity, internal);
919 for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
920 if (CIRCUIT_IS_ORIGIN(_circ) &&
921 _circ->state == CIRCUIT_STATE_OPEN &&
922 !_circ->marked_for_close &&
923 _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
924 !_circ->timestamp_dirty) {
925 origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
926 if ((!need_uptime || circ->build_state->need_uptime) &&
927 (!need_capacity || circ->build_state->need_capacity) &&
928 (internal == circ->build_state->is_internal) &&
929 circ->remaining_relay_early_cells &&
930 !circ->build_state->onehop_tunnel) {
931 if (info) {
932 /* need to make sure we don't duplicate hops */
933 crypt_path_t *hop = circ->cpath;
934 routerinfo_t *ri1 = router_get_by_digest(info->identity_digest);
935 do {
936 routerinfo_t *ri2;
937 if (!memcmp(hop->extend_info->identity_digest,
938 info->identity_digest, DIGEST_LEN))
939 goto next;
940 if (ri1 &&
941 (ri2 = router_get_by_digest(hop->extend_info->identity_digest))
942 && routers_in_same_family(ri1, ri2))
943 goto next;
944 hop=hop->next;
945 } while (hop!=circ->cpath);
947 if (!best || (best->build_state->need_uptime && !need_uptime))
948 best = circ;
949 next: ;
953 return best;
956 /** Return the number of hops in circuit's path. */
958 circuit_get_cpath_len(origin_circuit_t *circ)
960 int n = 0;
961 if (circ && circ->cpath) {
962 crypt_path_t *cpath, *cpath_next = NULL;
963 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
964 cpath_next = cpath->next;
965 ++n;
968 return n;
971 /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
972 * aren't that many hops in the list. */
973 crypt_path_t *
974 circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
976 if (circ && circ->cpath && hopnum > 0) {
977 crypt_path_t *cpath, *cpath_next = NULL;
978 for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
979 cpath_next = cpath->next;
980 if (--hopnum <= 0)
981 return cpath;
984 return NULL;
987 /** Go through the circuitlist; mark-for-close each circuit that starts
988 * at us but has not yet been used. */
989 void
990 circuit_mark_all_unused_circs(void)
992 circuit_t *circ;
994 for (circ=global_circuitlist; circ; circ = circ->next) {
995 if (CIRCUIT_IS_ORIGIN(circ) &&
996 !circ->marked_for_close &&
997 !circ->timestamp_dirty)
998 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
1002 /** Go through the circuitlist; for each circuit that starts at us
1003 * and is dirty, frob its timestamp_dirty so we won't use it for any
1004 * new streams.
1006 * This is useful for letting the user change pseudonyms, so new
1007 * streams will not be linkable to old streams.
1009 void
1010 circuit_expire_all_dirty_circs(void)
1012 circuit_t *circ;
1013 or_options_t *options = get_options();
1015 for (circ=global_circuitlist; circ; circ = circ->next) {
1016 if (CIRCUIT_IS_ORIGIN(circ) &&
1017 !circ->marked_for_close &&
1018 circ->timestamp_dirty)
1019 circ->timestamp_dirty -= options->MaxCircuitDirtiness;
1023 /** Mark <b>circ</b> to be closed next time we call
1024 * circuit_close_all_marked(). Do any cleanup needed:
1025 * - If state is onionskin_pending, remove circ from the onion_pending
1026 * list.
1027 * - If circ isn't open yet: call circuit_build_failed() if we're
1028 * the origin, and in either case call circuit_rep_hist_note_result()
1029 * to note stats.
1030 * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
1031 * just tried from our list of intro points for that service
1032 * descriptor.
1033 * - Send appropriate destroys and edge_destroys for conns and
1034 * streams attached to circ.
1035 * - If circ->rend_splice is set (we are the midpoint of a joined
1036 * rendezvous stream), then mark the other circuit to close as well.
1038 void
1039 _circuit_mark_for_close(circuit_t *circ, int reason, int line,
1040 const char *file)
1042 int orig_reason = reason; /* Passed to the controller */
1043 assert_circuit_ok(circ);
1044 tor_assert(line);
1045 tor_assert(file);
1047 if (circ->marked_for_close) {
1048 log(LOG_WARN,LD_BUG,
1049 "Duplicate call to circuit_mark_for_close at %s:%d"
1050 " (first at %s:%d)", file, line,
1051 circ->marked_for_close_file, circ->marked_for_close);
1052 return;
1054 if (reason == END_CIRC_AT_ORIGIN) {
1055 if (!CIRCUIT_IS_ORIGIN(circ)) {
1056 log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
1057 "but circuit was not at origin. (called %s:%d, purpose=%d)",
1058 file, line, circ->purpose);
1060 reason = END_CIRC_REASON_NONE;
1062 if (CIRCUIT_IS_ORIGIN(circ)) {
1063 /* We don't send reasons when closing circuits at the origin. */
1064 reason = END_CIRC_REASON_NONE;
1067 if (reason & END_CIRC_REASON_FLAG_REMOTE)
1068 reason &= ~END_CIRC_REASON_FLAG_REMOTE;
1070 if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
1071 if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
1072 log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
1073 reason = END_CIRC_REASON_NONE;
1076 if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
1077 onion_pending_remove(TO_OR_CIRCUIT(circ));
1079 /* If the circuit ever became OPEN, we sent it to the reputation history
1080 * module then. If it isn't OPEN, we send it there now to remember which
1081 * links worked and which didn't.
1083 if (circ->state != CIRCUIT_STATE_OPEN) {
1084 if (CIRCUIT_IS_ORIGIN(circ)) {
1085 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1086 circuit_build_failed(ocirc); /* take actions if necessary */
1087 circuit_rep_hist_note_result(ocirc);
1090 if (circ->state == CIRCUIT_STATE_OR_WAIT) {
1091 if (circuits_pending_or_conns)
1092 smartlist_remove(circuits_pending_or_conns, circ);
1094 if (CIRCUIT_IS_ORIGIN(circ)) {
1095 control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
1096 (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
1097 orig_reason);
1099 if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
1100 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1101 tor_assert(circ->state == CIRCUIT_STATE_OPEN);
1102 tor_assert(ocirc->build_state->chosen_exit);
1103 tor_assert(ocirc->rend_data);
1104 /* treat this like getting a nack from it */
1105 log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
1106 "Removing from descriptor.",
1107 safe_str_client(ocirc->rend_data->onion_address),
1108 safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
1109 rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
1110 ocirc->rend_data);
1112 if (circ->n_conn)
1113 connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
1115 if (! CIRCUIT_IS_ORIGIN(circ)) {
1116 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1117 edge_connection_t *conn;
1118 for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
1119 connection_edge_destroy(or_circ->p_circ_id, conn);
1120 or_circ->n_streams = NULL;
1122 while (or_circ->resolving_streams) {
1123 conn = or_circ->resolving_streams;
1124 or_circ->resolving_streams = conn->next_stream;
1125 if (!conn->_base.marked_for_close) {
1126 /* The client will see a DESTROY, and infer that the connections
1127 * are closing because the circuit is getting torn down. No need
1128 * to send an end cell. */
1129 conn->edge_has_sent_end = 1;
1130 conn->end_reason = END_STREAM_REASON_DESTROY;
1131 conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
1132 connection_mark_for_close(TO_CONN(conn));
1134 conn->on_circuit = NULL;
1137 if (or_circ->p_conn)
1138 connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
1139 } else {
1140 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1141 edge_connection_t *conn;
1142 for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
1143 connection_edge_destroy(circ->n_circ_id, conn);
1144 ocirc->p_streams = NULL;
1147 circ->marked_for_close = line;
1148 circ->marked_for_close_file = file;
1150 if (!CIRCUIT_IS_ORIGIN(circ)) {
1151 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1152 if (or_circ->rend_splice) {
1153 if (!or_circ->rend_splice->_base.marked_for_close) {
1154 /* do this after marking this circuit, to avoid infinite recursion. */
1155 circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
1157 or_circ->rend_splice = NULL;
1162 /** Verify that cpath layer <b>cp</b> has all of its invariants
1163 * correct. Trigger an assert if anything is invalid.
1165 void
1166 assert_cpath_layer_ok(const crypt_path_t *cp)
1168 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
1169 // tor_assert(cp->port);
1170 tor_assert(cp);
1171 tor_assert(cp->magic == CRYPT_PATH_MAGIC);
1172 switch (cp->state)
1174 case CPATH_STATE_OPEN:
1175 tor_assert(cp->f_crypto);
1176 tor_assert(cp->b_crypto);
1177 /* fall through */
1178 case CPATH_STATE_CLOSED:
1179 tor_assert(!cp->dh_handshake_state);
1180 break;
1181 case CPATH_STATE_AWAITING_KEYS:
1182 /* tor_assert(cp->dh_handshake_state); */
1183 break;
1184 default:
1185 log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
1186 tor_assert(0);
1188 tor_assert(cp->package_window >= 0);
1189 tor_assert(cp->deliver_window >= 0);
1192 /** Verify that cpath <b>cp</b> has all of its invariants
1193 * correct. Trigger an assert if anything is invalid.
1195 static void
1196 assert_cpath_ok(const crypt_path_t *cp)
1198 const crypt_path_t *start = cp;
1200 do {
1201 assert_cpath_layer_ok(cp);
1202 /* layers must be in sequence of: "open* awaiting? closed*" */
1203 if (cp != start) {
1204 if (cp->state == CPATH_STATE_AWAITING_KEYS) {
1205 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
1206 } else if (cp->state == CPATH_STATE_OPEN) {
1207 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
1210 cp = cp->next;
1211 tor_assert(cp);
1212 } while (cp != start);
1215 /** Verify that circuit <b>c</b> has all of its invariants
1216 * correct. Trigger an assert if anything is invalid.
1218 void
1219 assert_circuit_ok(const circuit_t *c)
1221 edge_connection_t *conn;
1222 const or_circuit_t *or_circ = NULL;
1223 const origin_circuit_t *origin_circ = NULL;
1225 tor_assert(c);
1226 tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
1227 tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
1228 c->purpose <= _CIRCUIT_PURPOSE_MAX);
1231 /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
1232 * never understand. -NM. */
1233 circuit_t *nonconst_circ = (circuit_t*) c;
1234 if (CIRCUIT_IS_ORIGIN(c))
1235 origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
1236 else
1237 or_circ = TO_OR_CIRCUIT(nonconst_circ);
1240 if (c->n_conn) {
1241 tor_assert(!c->n_hop);
1243 if (c->n_circ_id) {
1244 /* We use the _impl variant here to make sure we don't fail on marked
1245 * circuits, which would not be returned by the regular function. */
1246 circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
1247 c->n_conn);
1248 tor_assert(c == c2);
1251 if (or_circ && or_circ->p_conn) {
1252 if (or_circ->p_circ_id) {
1253 /* ibid */
1254 circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
1255 or_circ->p_conn);
1256 tor_assert(c == c2);
1259 if (or_circ)
1260 for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
1261 tor_assert(conn->_base.type == CONN_TYPE_EXIT);
1263 tor_assert(c->deliver_window >= 0);
1264 tor_assert(c->package_window >= 0);
1265 if (c->state == CIRCUIT_STATE_OPEN) {
1266 tor_assert(!c->n_conn_onionskin);
1267 if (or_circ) {
1268 tor_assert(or_circ->n_crypto);
1269 tor_assert(or_circ->p_crypto);
1270 tor_assert(or_circ->n_digest);
1271 tor_assert(or_circ->p_digest);
1274 if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
1275 tor_assert(circuits_pending_or_conns &&
1276 smartlist_isin(circuits_pending_or_conns, c));
1277 } else {
1278 tor_assert(!circuits_pending_or_conns ||
1279 !smartlist_isin(circuits_pending_or_conns, c));
1281 if (origin_circ && origin_circ->cpath) {
1282 assert_cpath_ok(origin_circ->cpath);
1284 if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
1285 tor_assert(or_circ);
1286 if (!c->marked_for_close) {
1287 tor_assert(or_circ->rend_splice);
1288 tor_assert(or_circ->rend_splice->rend_splice == or_circ);
1290 tor_assert(or_circ->rend_splice != or_circ);
1291 } else {
1292 tor_assert(!or_circ || !or_circ->rend_splice);