Convert circuituse, command, config, connection, relay, router, test to new logging...
[tor.git] / src / or / circuitlist.c
blob68b174404e0f79cf76c719bdaf3745b90253d150
1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char circuitlist_c_id[] = "$Id$";
8 /**
9 * \file circuitlist.c
10 * \brief Manage the global circuit list.
11 **/
13 #define NEW_LOG_INTERFACE
14 #include "or.h"
16 /* Define RB_AUGMENT to avoid warnings about if statements with emtpy bodies.
18 #define RB_AUGMENT(x) do{}while(0)
19 #include "tree.h"
21 /********* START VARIABLES **********/
23 /** A global list of all circuits at this hop. */
24 circuit_t *global_circuitlist=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 RB_ENTRY(orconn_circid_circuit_map_t) node;
36 connection_t *or_conn;
37 uint16_t circ_id;
38 circuit_t *circuit;
39 } orconn_circid_circuit_map_t;
41 /** Helper for RB tree: compare the OR connection and circuit ID for a and b,
42 * and return less than, equal to, or greater than zero appropriately.
44 static INLINE int
45 compare_orconn_circid_entries(orconn_circid_circuit_map_t *a,
46 orconn_circid_circuit_map_t *b)
48 if (a->or_conn < b->or_conn)
49 return -1;
50 else if (a->or_conn > b->or_conn)
51 return 1;
52 else
53 return ((int)b->circ_id) - ((int)a->circ_id);
56 static RB_HEAD(orconn_circid_tree, orconn_circid_circuit_map_t) orconn_circid_circuit_map = RB_INITIALIZER(orconn_circid_circuit_map);
57 RB_PROTOTYPE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
58 RB_GENERATE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
60 /** The most recently returned entry from circuit_get_by_circid_orconn;
61 * used to improve performance when many cells arrive in a row from the
62 * same circuit.
64 /* (We tried using splay trees, but round-robin turned out to make them
65 * suck.) */
66 orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
68 /** Set the p_conn or n_conn field of a circuit <b>circ</b>, along
69 * with the corresponding circuit ID, and add the circuit as appropriate
70 * to the (orconn,id)-\>circuit map. */
71 void
72 circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
73 connection_t *conn,
74 enum which_conn_changed_t which)
76 uint16_t old_id;
77 connection_t *old_conn;
78 orconn_circid_circuit_map_t search;
79 orconn_circid_circuit_map_t *found;
81 tor_assert(!conn || conn->type == CONN_TYPE_OR);
83 if (which == P_CONN_CHANGED) {
84 old_id = circ->p_circ_id;
85 old_conn = circ->p_conn;
86 circ->p_circ_id = id;
87 circ->p_conn = conn;
88 } else {
89 old_id = circ->n_circ_id;
90 old_conn = circ->n_conn;
91 circ->n_circ_id = id;
92 circ->n_conn = conn;
95 if (_last_circid_orconn_ent &&
96 ((old_id == _last_circid_orconn_ent->circ_id &&
97 old_conn == _last_circid_orconn_ent->or_conn) ||
98 (id == _last_circid_orconn_ent->circ_id &&
99 conn == _last_circid_orconn_ent->or_conn))) {
100 _last_circid_orconn_ent = NULL;
103 if (old_conn) {
104 search.circ_id = old_id;
105 search.or_conn = old_conn;
106 found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
107 if (found) {
108 RB_REMOVE(orconn_circid_tree, &orconn_circid_circuit_map, found);
110 tor_free(found);
113 if (conn == NULL)
114 return;
116 search.circ_id = id;
117 search.or_conn = conn;
118 found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
119 if (found) {
120 found->circuit = circ;
121 } else {
122 found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
123 found->circ_id = id;
124 found->or_conn = conn;
125 found->circuit = circ;
126 RB_INSERT(orconn_circid_tree, &orconn_circid_circuit_map, found);
130 /** Add <b>circ</b> to the global list of circuits. This is called only from
131 * within circuit_new.
133 static void
134 circuit_add(circuit_t *circ)
136 if (!global_circuitlist) { /* first one */
137 global_circuitlist = circ;
138 circ->next = NULL;
139 } else {
140 circ->next = global_circuitlist;
141 global_circuitlist = circ;
145 /** Detach from the global circuit list, and deallocate, all
146 * circuits that have been marked for close.
148 void
149 circuit_close_all_marked(void)
151 circuit_t *tmp,*m;
153 while (global_circuitlist && global_circuitlist->marked_for_close) {
154 tmp = global_circuitlist->next;
155 circuit_free(global_circuitlist);
156 global_circuitlist = tmp;
159 tmp = global_circuitlist;
160 while (tmp && tmp->next) {
161 if (tmp->next->marked_for_close) {
162 m = tmp->next->next;
163 circuit_free(tmp->next);
164 tmp->next = m;
165 /* Need to check new tmp->next; don't advance tmp. */
166 } else {
167 /* Advance tmp. */
168 tmp = tmp->next;
173 /** Return the head of the global linked list of circuits. **/
174 circuit_t *
175 _circuit_get_global_list(void)
177 return global_circuitlist;
180 /** Function to make circ-\>state human-readable */
181 const char *
182 circuit_state_to_string(int state)
184 static char buf[64];
185 switch (state) {
186 case CIRCUIT_STATE_BUILDING: return "doing handshakes";
187 case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
188 case CIRCUIT_STATE_OR_WAIT: return "connecting to firsthop";
189 case CIRCUIT_STATE_OPEN: return "open";
190 default:
191 warn(LD_BUG, "Bug: unknown circuit state %d", state);
192 tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
193 return buf;
197 /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
198 * and <b>p_conn</b>. Add it to the global circuit list.
200 circuit_t *
201 circuit_new(uint16_t p_circ_id, connection_t *p_conn)
203 circuit_t *circ;
204 static uint32_t n_circuits_allocated = 1;
205 /* never zero, since a global ID of 0 is treated specially by the
206 * controller */
208 circ = tor_malloc_zero(sizeof(circuit_t));
209 circ->magic = CIRCUIT_MAGIC;
211 circ->timestamp_created = time(NULL);
213 circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
215 /* CircIDs */
216 if (p_conn) {
217 circuit_set_circid_orconn(circ, p_circ_id, p_conn, P_CONN_CHANGED);
219 /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
221 circ->package_window = CIRCWINDOW_START;
222 circ->deliver_window = CIRCWINDOW_START;
224 circ->next_stream_id = crypto_rand_int(1<<16);
225 circ->global_identifier = n_circuits_allocated++;
227 circuit_add(circ);
229 return circ;
232 /** Deallocate space associated with circ.
234 static void
235 circuit_free(circuit_t *circ)
237 tor_assert(circ);
238 tor_assert(circ->magic == CIRCUIT_MAGIC);
239 if (circ->n_crypto)
240 crypto_free_cipher_env(circ->n_crypto);
241 if (circ->p_crypto)
242 crypto_free_cipher_env(circ->p_crypto);
243 if (circ->n_digest)
244 crypto_free_digest_env(circ->n_digest);
245 if (circ->p_digest)
246 crypto_free_digest_env(circ->p_digest);
247 if (circ->build_state) {
248 if (circ->build_state->chosen_exit)
249 extend_info_free(circ->build_state->chosen_exit);
250 if (circ->build_state->pending_final_cpath)
251 circuit_free_cpath_node(circ->build_state->pending_final_cpath);
253 tor_free(circ->build_state);
254 circuit_free_cpath(circ->cpath);
255 if (circ->rend_splice) {
256 circ->rend_splice->rend_splice = NULL;
258 /* Remove from map. */
259 circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
260 circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
262 memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
263 tor_free(circ);
266 /** Deallocate space associated with the linked list <b>cpath</b>. */
267 static void
268 circuit_free_cpath(crypt_path_t *cpath)
270 crypt_path_t *victim, *head=cpath;
272 if (!cpath)
273 return;
275 /* it's a doubly linked list, so we have to notice when we've
276 * gone through it once. */
277 while (cpath->next && cpath->next != head) {
278 victim = cpath;
279 cpath = victim->next;
280 circuit_free_cpath_node(victim);
283 circuit_free_cpath_node(cpath);
286 /** Release all storage held by circuits. */
287 void
288 circuit_free_all(void)
290 circuit_t *next;
291 while (global_circuitlist) {
292 next = global_circuitlist->next;
293 while (global_circuitlist->resolving_streams) {
294 connection_t *next;
295 next = global_circuitlist->resolving_streams->next_stream;
296 connection_free(global_circuitlist->resolving_streams);
297 global_circuitlist->resolving_streams = next;
299 circuit_free(global_circuitlist);
300 global_circuitlist = next;
304 /** Deallocate space associated with the cpath node <b>victim</b>. */
305 static void
306 circuit_free_cpath_node(crypt_path_t *victim)
308 if (victim->f_crypto)
309 crypto_free_cipher_env(victim->f_crypto);
310 if (victim->b_crypto)
311 crypto_free_cipher_env(victim->b_crypto);
312 if (victim->f_digest)
313 crypto_free_digest_env(victim->f_digest);
314 if (victim->b_digest)
315 crypto_free_digest_env(victim->b_digest);
316 if (victim->dh_handshake_state)
317 crypto_dh_free(victim->dh_handshake_state);
318 if (victim->extend_info)
319 extend_info_free(victim->extend_info);
321 victim->magic = 0xDEADBEEFu;
322 tor_free(victim);
325 /** Return the circuit whose global ID is <b>id</b>, or NULL if no
326 * such circuit exists. */
327 circuit_t *
328 circuit_get_by_global_id(uint32_t id)
330 circuit_t *circ;
331 for (circ=global_circuitlist;circ;circ = circ->next) {
332 if (circ->global_identifier == id) {
333 if (circ->marked_for_close)
334 return NULL;
335 else
336 return circ;
339 return NULL;
342 /** Return a circ such that:
343 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
344 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
345 * Return NULL if no such circuit exists.
347 static INLINE circuit_t *
348 circuit_get_by_circid_orconn_impl(uint16_t circ_id, connection_t *conn)
350 orconn_circid_circuit_map_t search;
351 orconn_circid_circuit_map_t *found;
353 tor_assert(conn->type == CONN_TYPE_OR);
355 if (_last_circid_orconn_ent &&
356 circ_id == _last_circid_orconn_ent->circ_id &&
357 conn == _last_circid_orconn_ent->or_conn) {
358 found = _last_circid_orconn_ent;
359 } else {
360 search.circ_id = circ_id;
361 search.or_conn = conn;
362 found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
363 _last_circid_orconn_ent = found;
365 if (found && found->circuit)
366 return found->circuit;
368 /* The rest of this can be replaced with
369 "return NULL;" once we believe the code works. */
372 circuit_t *circ;
373 for (circ=global_circuitlist;circ;circ = circ->next) {
374 if (circ->p_conn == conn && circ->p_circ_id == circ_id) {
375 warn(LD_BUG, "circuit matches p_conn, but not in tree (Bug!)");
376 return circ;
378 if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
379 warn(LD_BUG, "circuit matches n_conn, but not in tree (Bug!)");
380 return circ;
383 return NULL;
387 /** Return a circ such that:
388 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
389 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
390 * - circ is not marked for close.
391 * Return NULL if no such circuit exists.
393 circuit_t *
394 circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn)
396 circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
397 if (circ->marked_for_close)
398 return NULL;
399 else
400 return circ;
403 /** Return true iff there is a circ such that
404 * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
405 * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
406 * Return NULL if no such circuit exists.
409 circuit_id_used_on_conn(uint16_t circ_id, connection_t *conn)
411 circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
412 if (circ && circ->marked_for_close)
413 log_fn(LOG_NOTICE, LD_CIRC,
414 "I was about to re-use a circuit ID that had been marked."
415 " Good thing we fixed that bug!");
416 return circ != NULL;
419 /** Return the circuit that a given edge connection is using. */
420 circuit_t *
421 circuit_get_by_edge_conn(connection_t *conn)
423 circuit_t *circ;
424 #if 0
425 connection_t *tmpconn;
426 #endif
427 tor_assert(CONN_IS_EDGE(conn));
429 if (! conn->on_circuit) {
430 /* return NULL; */
431 circ = circuit_get_by_conn(conn);
432 if (circ) {
433 warn(LD_BUG, "BUG: conn->on_circuit==NULL, but there was in fact a circuit there.");
435 return circ;
438 circ = conn->on_circuit;
439 tor_assert(circ->magic == CIRCUIT_MAGIC);
440 #if 0
441 /* All this stuff here is sanity-checking. */
442 for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
443 if (tmpconn == conn)
444 return circ;
445 for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
446 if (tmpconn == conn)
447 return circ;
448 for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
449 if (tmpconn == conn)
450 return circ;
452 tor_assert(0);
453 #endif
454 return circ;
457 /** Return a circ such that circ is attached to <b>conn</b>, either as
458 * p_conn, n_conn, or in p_streams or n_streams or resolving_streams.
460 * Return NULL if no such circuit exists.
462 circuit_t *
463 circuit_get_by_conn(connection_t *conn)
465 circuit_t *circ;
466 connection_t *tmpconn;
468 for (circ=global_circuitlist;circ;circ = circ->next) {
469 if (circ->marked_for_close)
470 continue;
472 if (circ->p_conn == conn)
473 return circ;
474 if (circ->n_conn == conn)
475 return circ;
476 for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
477 if (tmpconn == conn)
478 return circ;
479 for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
480 if (tmpconn == conn)
481 return circ;
482 for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
483 if (tmpconn == conn)
484 return circ;
486 return NULL;
489 /** Return a circ such that:
490 * - circ-\>rend_query is equal to <b>rend_query</b>, and
491 * - circ-\>purpose is equal to <b>purpose</b>.
493 * Return NULL if no such circuit exists.
495 circuit_t *
496 circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
498 circuit_t *circ;
500 for (circ = global_circuitlist; circ; circ = circ->next) {
501 if (!circ->marked_for_close &&
502 circ->purpose == purpose &&
503 !rend_cmp_service_ids(rend_query, circ->rend_query))
504 return circ;
506 return NULL;
509 /** Return the first circuit in global_circuitlist after <b>start</b>
510 * whose rend_pk_digest field is <b>digest</b> and whose purpose is
511 * <b>purpose</b>. Returns NULL if no circuit is found.
512 * If <b>start</b> is NULL, begin at the start of the list.
514 circuit_t *
515 circuit_get_next_by_pk_and_purpose(circuit_t *start,
516 const char *digest, uint8_t purpose)
518 circuit_t *circ;
519 if (start == NULL)
520 circ = global_circuitlist;
521 else
522 circ = start->next;
524 for ( ; circ; circ = circ->next) {
525 if (circ->marked_for_close)
526 continue;
527 if (circ->purpose != purpose)
528 continue;
529 if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
530 return circ;
532 return NULL;
535 /** Return the circuit waiting for a rendezvous with the provided cookie.
536 * Return NULL if no such circuit is found.
538 circuit_t *
539 circuit_get_rendezvous(const char *cookie)
541 circuit_t *circ;
542 for (circ = global_circuitlist; circ; circ = circ->next) {
543 if (! circ->marked_for_close &&
544 circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
545 ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
546 return circ;
548 return NULL;
551 /** Return a circuit that is open, has specified <b>purpose</b>,
552 * has a timestamp_dirty value of 0, and is uptime/capacity/internal
553 * if required; or NULL if no circuit fits this description.
555 * Avoid returning need_uptime circuits if not necessary.
556 * FFFF As a more important goal, not yet implemented, avoid returning
557 * internal circuits if not necessary.
559 circuit_t *
560 circuit_get_clean_open(uint8_t purpose, int need_uptime,
561 int need_capacity, int internal)
563 circuit_t *circ;
564 circuit_t *best=NULL;
566 debug(LD_CIRC,"Hunting for a circ to cannibalize: purpose %d, uptime %d, capacity %d, internal %d", purpose, need_uptime, need_capacity, internal);
568 for (circ=global_circuitlist; circ; circ = circ->next) {
569 if (CIRCUIT_IS_ORIGIN(circ) &&
570 circ->state == CIRCUIT_STATE_OPEN &&
571 !circ->marked_for_close &&
572 circ->purpose == purpose &&
573 !circ->timestamp_dirty &&
574 (!need_uptime || circ->build_state->need_uptime) &&
575 (!need_capacity || circ->build_state->need_capacity) &&
576 (!internal || circ->build_state->is_internal)) {
577 if (!best || (best->build_state->need_uptime && !need_uptime))
578 best = circ;
581 return best;
584 /** Go through the circuitlist; mark-for-close each circuit that starts
585 * at us but has not yet been used. */
586 void
587 circuit_mark_all_unused_circs(void)
589 circuit_t *circ;
591 for (circ=global_circuitlist; circ; circ = circ->next) {
592 if (CIRCUIT_IS_ORIGIN(circ) &&
593 !circ->marked_for_close &&
594 !circ->timestamp_dirty)
595 circuit_mark_for_close(circ);
599 /** Go through the circuitlist; for each circuit that starts at us
600 * and is dirty, frob its timestamp_dirty so we won't use it for any
601 * new streams.
603 * This is useful for letting the user change pseudonyms, so new
604 * streams will not be linkable to old streams.
606 void
607 circuit_expire_all_dirty_circs(void)
609 circuit_t *circ;
610 or_options_t *options = get_options();
612 for (circ=global_circuitlist; circ; circ = circ->next) {
613 if (CIRCUIT_IS_ORIGIN(circ) &&
614 !circ->marked_for_close &&
615 circ->timestamp_dirty)
616 circ->timestamp_dirty -= options->MaxCircuitDirtiness;
620 /** Mark <b>circ</b> to be closed next time we call
621 * circuit_close_all_marked(). Do any cleanup needed:
622 * - If state is onionskin_pending, remove circ from the onion_pending
623 * list.
624 * - If circ isn't open yet: call circuit_build_failed() if we're
625 * the origin, and in either case call circuit_rep_hist_note_result()
626 * to note stats.
627 * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
628 * just tried from our list of intro points for that service
629 * descriptor.
630 * - Send appropriate destroys and edge_destroys for conns and
631 * streams attached to circ.
632 * - If circ->rend_splice is set (we are the midpoint of a joined
633 * rendezvous stream), then mark the other circuit to close as well.
635 void
636 _circuit_mark_for_close(circuit_t *circ, int line, const char *file)
638 connection_t *conn;
640 assert_circuit_ok(circ);
641 tor_assert(line);
642 tor_assert(file);
644 if (circ->marked_for_close) {
645 log(LOG_WARN,LD_BUG,
646 "Duplicate call to circuit_mark_for_close at %s:%d"
647 " (first at %s:%d)", file, line,
648 circ->marked_for_close_file, circ->marked_for_close);
649 return;
652 if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
653 onion_pending_remove(circ);
655 /* If the circuit ever became OPEN, we sent it to the reputation history
656 * module then. If it isn't OPEN, we send it there now to remember which
657 * links worked and which didn't.
659 if (circ->state != CIRCUIT_STATE_OPEN) {
660 if (CIRCUIT_IS_ORIGIN(circ)) {
661 circuit_build_failed(circ); /* take actions if necessary */
663 circuit_rep_hist_note_result(circ);
665 if (CIRCUIT_IS_ORIGIN(circ)) {
666 control_event_circuit_status(circ,
667 (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
669 if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
670 tor_assert(circ->state == CIRCUIT_STATE_OPEN);
671 tor_assert(circ->build_state->chosen_exit);
672 /* treat this like getting a nack from it */
673 info(LD_REND,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
674 safe_str(circ->rend_query),
675 safe_str(build_state_get_exit_nickname(circ->build_state)));
676 rend_client_remove_intro_point(circ->build_state->chosen_exit,
677 circ->rend_query);
680 if (circ->n_conn)
681 connection_send_destroy(circ->n_circ_id, circ->n_conn);
682 for (conn=circ->n_streams; conn; conn=conn->next_stream)
683 connection_edge_destroy(circ->n_circ_id, conn);
684 while (circ->resolving_streams) {
685 conn = circ->resolving_streams;
686 circ->resolving_streams = conn->next_stream;
687 if (!conn->marked_for_close) {
688 /* The other side will see a DESTROY, and infer that the connections
689 * are closing because the circuit is getting torn down. No need
690 * to send an end cell*/
691 conn->has_sent_end = 1; /* we're closing the circuit, nothing to send to */
692 connection_mark_for_close(conn);
694 conn->on_circuit = NULL;
696 if (circ->p_conn)
697 connection_send_destroy(circ->p_circ_id, circ->p_conn);
698 for (conn=circ->p_streams; conn; conn=conn->next_stream)
699 connection_edge_destroy(circ->p_circ_id, conn);
701 circ->marked_for_close = line;
702 circ->marked_for_close_file = file;
704 if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
705 /* do this after marking this circuit, to avoid infinite recursion. */
706 circuit_mark_for_close(circ->rend_splice);
707 circ->rend_splice = NULL;
711 /** Verify that cpath layer <b>cp</b> has all of its invariants
712 * correct. Trigger an assert if anything is invalid.
714 void
715 assert_cpath_layer_ok(const crypt_path_t *cp)
717 // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
718 // tor_assert(cp->port);
719 tor_assert(cp);
720 tor_assert(cp->magic == CRYPT_PATH_MAGIC);
721 switch (cp->state)
723 case CPATH_STATE_OPEN:
724 tor_assert(cp->f_crypto);
725 tor_assert(cp->b_crypto);
726 /* fall through */
727 case CPATH_STATE_CLOSED:
728 tor_assert(!cp->dh_handshake_state);
729 break;
730 case CPATH_STATE_AWAITING_KEYS:
731 /* tor_assert(cp->dh_handshake_state); */
732 break;
733 default:
734 err("Unexpected state %d",cp->state);
735 tor_assert(0);
737 tor_assert(cp->package_window >= 0);
738 tor_assert(cp->deliver_window >= 0);
741 /** Verify that cpath <b>cp</b> has all of its invariants
742 * correct. Trigger an assert if anything is invalid.
744 static void
745 assert_cpath_ok(const crypt_path_t *cp)
747 const crypt_path_t *start = cp;
749 do {
750 assert_cpath_layer_ok(cp);
751 /* layers must be in sequence of: "open* awaiting? closed*" */
752 if (cp != start) {
753 if (cp->state == CPATH_STATE_AWAITING_KEYS) {
754 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
755 } else if (cp->state == CPATH_STATE_OPEN) {
756 tor_assert(cp->prev->state == CPATH_STATE_OPEN);
759 cp = cp->next;
760 tor_assert(cp);
761 } while (cp != start);
764 /** Verify that circuit <b>c</b> has all of its invariants
765 * correct. Trigger an assert if anything is invalid.
767 void
768 assert_circuit_ok(const circuit_t *c)
770 connection_t *conn;
772 tor_assert(c);
773 tor_assert(c->magic == CIRCUIT_MAGIC);
774 tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
775 c->purpose <= _CIRCUIT_PURPOSE_MAX);
777 if (c->n_conn) {
778 tor_assert(c->n_conn->type == CONN_TYPE_OR);
779 tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest, DIGEST_LEN));
780 if (c->n_circ_id)
781 tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
783 if (c->p_conn) {
784 tor_assert(c->p_conn->type == CONN_TYPE_OR);
785 if (c->p_circ_id)
786 tor_assert(c == circuit_get_by_circid_orconn(c->p_circ_id, c->p_conn));
788 for (conn = c->p_streams; conn; conn = conn->next_stream)
789 tor_assert(conn->type == CONN_TYPE_AP);
790 for (conn = c->n_streams; conn; conn = conn->next_stream)
791 tor_assert(conn->type == CONN_TYPE_EXIT);
793 tor_assert(c->deliver_window >= 0);
794 tor_assert(c->package_window >= 0);
795 if (c->state == CIRCUIT_STATE_OPEN) {
796 if (c->cpath) {
797 tor_assert(CIRCUIT_IS_ORIGIN(c));
798 tor_assert(!c->n_crypto);
799 tor_assert(!c->p_crypto);
800 tor_assert(!c->n_digest);
801 tor_assert(!c->p_digest);
802 } else {
803 tor_assert(!CIRCUIT_IS_ORIGIN(c));
804 tor_assert(c->n_crypto);
805 tor_assert(c->p_crypto);
806 tor_assert(c->n_digest);
807 tor_assert(c->p_digest);
810 if (c->cpath) {
811 assert_cpath_ok(c->cpath);
813 if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
814 if (!c->marked_for_close) {
815 tor_assert(c->rend_splice);
816 tor_assert(c->rend_splice->rend_splice == c);
818 tor_assert(c->rend_splice != c);
819 } else {
820 tor_assert(!c->rend_splice);