circuitlist.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char circuitlist_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file circuitlist.c
  10. * \brief Manage the global circuit list.
  11. **/
  12. #include "or.h"
  13. #include "ht.h"
  14. /********* START VARIABLES **********/
  15. /** A global list of all circuits at this hop. */
  16. circuit_t *global_circuitlist=NULL;
  17. /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
  18. static smartlist_t *circuits_pending_or_conns=NULL;
  19. static void circuit_free(circuit_t *circ);
  20. static void circuit_free_cpath(crypt_path_t *cpath);
  21. static void circuit_free_cpath_node(crypt_path_t *victim);
  22. /********* END VARIABLES ************/
  23. /** A map from OR connection and circuit ID to circuit. (Lookup performance is
  24. * very important here, since we need to do it every time a cell arrives.) */
  25. typedef struct orconn_circid_circuit_map_t {
  26. HT_ENTRY(orconn_circid_circuit_map_t) node;
  27. or_connection_t *or_conn;
  28. uint16_t circ_id;
  29. circuit_t *circuit;
  30. } orconn_circid_circuit_map_t;
  31. /** Helper for hash tables: compare the OR connection and circuit ID for a and
  32. * b, and return less than, equal to, or greater than zero appropriately.
  33. */
  34. static INLINE int
  35. _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
  36. orconn_circid_circuit_map_t *b)
  37. {
  38. return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
  39. }
  40. /** Helper: return a hash based on circuit ID and the pointer value of
  41. * or_conn in <b>a</b>. */
  42. static INLINE unsigned int
  43. _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
  44. {
  45. return (((unsigned)a->circ_id)<<16) ^ (unsigned)(uintptr_t)(a->or_conn);
  46. }
  47. /** Map from [orconn,circid] to circuit. */
  48. static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
  49. orconn_circid_circuit_map = HT_INITIALIZER();
  50. HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  51. _orconn_circid_entry_hash, _orconn_circid_entries_eq)
  52. HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  53. _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
  54. malloc, realloc, free)
  55. /** The most recently returned entry from circuit_get_by_circid_orconn;
  56. * used to improve performance when many cells arrive in a row from the
  57. * same circuit.
  58. */
  59. orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  60. /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
  61. * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
  62. * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
  63. * the old entry (if any) and adding a new one. If <b>active</b> is true,
  64. * remove the circuit from the list of active circuits on old_conn and add it
  65. * to the list of active circuits on conn.
  66. * XXX "active" isn't an arg anymore */
  67. static void
  68. circuit_set_circid_orconn_helper(circuit_t *circ, int direction,
  69. uint16_t id,
  70. or_connection_t *conn)
  71. {
  72. orconn_circid_circuit_map_t search;
  73. orconn_circid_circuit_map_t *found;
  74. or_connection_t *old_conn, **conn_ptr;
  75. uint16_t old_id, *circid_ptr;
  76. int was_active, make_active;
  77. if (direction == CELL_DIRECTION_OUT) {
  78. conn_ptr = &circ->n_conn;
  79. circid_ptr = &circ->n_circ_id;
  80. was_active = circ->next_active_on_n_conn != NULL;
  81. make_active = circ->n_conn_cells.n > 0;
  82. } else {
  83. or_circuit_t *c = TO_OR_CIRCUIT(circ);
  84. conn_ptr = &c->p_conn;
  85. circid_ptr = &c->p_circ_id;
  86. was_active = c->next_active_on_p_conn != NULL;
  87. make_active = c->p_conn_cells.n > 0;
  88. }
  89. old_conn = *conn_ptr;
  90. old_id = *circid_ptr;
  91. if (id == old_id && conn == old_conn)
  92. return;
  93. if (_last_circid_orconn_ent &&
  94. ((old_id == _last_circid_orconn_ent->circ_id &&
  95. old_conn == _last_circid_orconn_ent->or_conn) ||
  96. (id == _last_circid_orconn_ent->circ_id &&
  97. conn == _last_circid_orconn_ent->or_conn))) {
  98. _last_circid_orconn_ent = NULL;
  99. }
  100. if (old_conn) { /* we may need to remove it from the conn-circid map */
  101. tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
  102. search.circ_id = old_id;
  103. search.or_conn = old_conn;
  104. found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
  105. if (found) {
  106. tor_free(found);
  107. --old_conn->n_circuits;
  108. }
  109. if (was_active && old_conn != conn)
  110. make_circuit_inactive_on_conn(circ,old_conn);
  111. }
  112. /* Change the values only after we have possibly made the circuit inactive
  113. * on the previous conn. */
  114. *conn_ptr = conn;
  115. *circid_ptr = id;
  116. if (conn == NULL)
  117. return;
  118. /* now add the new one to the conn-circid map */
  119. search.circ_id = id;
  120. search.or_conn = conn;
  121. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  122. if (found) {
  123. found->circuit = circ;
  124. } else {
  125. found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
  126. found->circ_id = id;
  127. found->or_conn = conn;
  128. found->circuit = circ;
  129. HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
  130. }
  131. if (make_active && old_conn != conn)
  132. make_circuit_active_on_conn(circ,conn);
  133. ++conn->n_circuits;
  134. }
  135. /** Set the p_conn field of a circuit <b>circ</b>, along
  136. * with the corresponding circuit ID, and add the circuit as appropriate
  137. * to the (orconn,id)-\>circuit map. */
  138. void
  139. circuit_set_p_circid_orconn(or_circuit_t *circ, uint16_t id,
  140. or_connection_t *conn)
  141. {
  142. circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), CELL_DIRECTION_IN,
  143. id, conn);
  144. if (conn)
  145. tor_assert(bool_eq(circ->p_conn_cells.n, circ->next_active_on_p_conn));
  146. }
  147. /** Set the n_conn field of a circuit <b>circ</b>, along
  148. * with the corresponding circuit ID, and add the circuit as appropriate
  149. * to the (orconn,id)-\>circuit map. */
  150. void
  151. circuit_set_n_circid_orconn(circuit_t *circ, uint16_t id,
  152. or_connection_t *conn)
  153. {
  154. circuit_set_circid_orconn_helper(circ, CELL_DIRECTION_OUT, id, conn);
  155. if (conn)
  156. tor_assert(bool_eq(circ->n_conn_cells.n, circ->next_active_on_n_conn));
  157. }
  158. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  159. * it from lists as appropriate. */
  160. void
  161. circuit_set_state(circuit_t *circ, int state)
  162. {
  163. tor_assert(circ);
  164. if (state == circ->state)
  165. return;
  166. if (!circuits_pending_or_conns)
  167. circuits_pending_or_conns = smartlist_create();
  168. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  169. /* remove from waiting-circuit list. */
  170. smartlist_remove(circuits_pending_or_conns, circ);
  171. }
  172. if (state == CIRCUIT_STATE_OR_WAIT) {
  173. /* add to waiting-circuit list. */
  174. smartlist_add(circuits_pending_or_conns, circ);
  175. }
  176. circ->state = state;
  177. }
  178. /** Add <b>circ</b> to the global list of circuits. This is called only from
  179. * within circuit_new.
  180. */
  181. static void
  182. circuit_add(circuit_t *circ)
  183. {
  184. if (!global_circuitlist) { /* first one */
  185. global_circuitlist = circ;
  186. circ->next = NULL;
  187. } else {
  188. circ->next = global_circuitlist;
  189. global_circuitlist = circ;
  190. }
  191. }
  192. /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
  193. * the given connection. */
  194. void
  195. circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
  196. {
  197. tor_assert(out);
  198. tor_assert(or_conn);
  199. if (!circuits_pending_or_conns)
  200. return;
  201. SMARTLIST_FOREACH(circuits_pending_or_conns, circuit_t *, circ,
  202. {
  203. if (circ->marked_for_close)
  204. continue;
  205. tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  206. if (tor_digest_is_zero(circ->n_conn_id_digest)) {
  207. /* Look at addr/port. This is an unkeyed connection. */
  208. if (circ->n_addr != or_conn->_base.addr ||
  209. circ->n_port != or_conn->_base.port)
  210. continue;
  211. } else {
  212. /* We expected a key. See if it's the right one. */
  213. if (memcmp(or_conn->identity_digest,
  214. circ->n_conn_id_digest, DIGEST_LEN))
  215. continue;
  216. }
  217. smartlist_add(out, circ);
  218. });
  219. }
  220. /** Return the number of circuits in state OR_WAIT, waiting for the given
  221. * connection. */
  222. int
  223. circuit_count_pending_on_or_conn(or_connection_t *or_conn)
  224. {
  225. int cnt;
  226. smartlist_t *sl = smartlist_create();
  227. circuit_get_all_pending_on_or_conn(sl, or_conn);
  228. cnt = smartlist_len(sl);
  229. smartlist_free(sl);
  230. log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
  231. or_conn->nickname ? or_conn->nickname : "NULL", cnt);
  232. return cnt;
  233. }
  234. /** Detach from the global circuit list, and deallocate, all
  235. * circuits that have been marked for close.
  236. */
  237. void
  238. circuit_close_all_marked(void)
  239. {
  240. circuit_t *tmp,*m;
  241. while (global_circuitlist && global_circuitlist->marked_for_close) {
  242. tmp = global_circuitlist->next;
  243. circuit_free(global_circuitlist);
  244. global_circuitlist = tmp;
  245. }
  246. tmp = global_circuitlist;
  247. while (tmp && tmp->next) {
  248. if (tmp->next->marked_for_close) {
  249. m = tmp->next->next;
  250. circuit_free(tmp->next);
  251. tmp->next = m;
  252. /* Need to check new tmp->next; don't advance tmp. */
  253. } else {
  254. /* Advance tmp. */
  255. tmp = tmp->next;
  256. }
  257. }
  258. }
  259. /** Return the head of the global linked list of circuits. */
  260. circuit_t *
  261. _circuit_get_global_list(void)
  262. {
  263. return global_circuitlist;
  264. }
  265. /** Function to make circ-\>state human-readable */
  266. const char *
  267. circuit_state_to_string(int state)
  268. {
  269. static char buf[64];
  270. switch (state) {
  271. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  272. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  273. case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
  274. case CIRCUIT_STATE_OPEN: return "open";
  275. default:
  276. log_warn(LD_BUG, "Unknown circuit state %d", state);
  277. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  278. return buf;
  279. }
  280. }
  281. /** Initialize the common elements in a circuit_t, and add it to the global
  282. * list. */
  283. static void
  284. init_circuit_base(circuit_t *circ)
  285. {
  286. circ->timestamp_created = time(NULL);
  287. circ->package_window = CIRCWINDOW_START;
  288. circ->deliver_window = CIRCWINDOW_START;
  289. circuit_add(circ);
  290. }
  291. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  292. * and <b>p_conn</b>. Add it to the global circuit list.
  293. */
  294. origin_circuit_t *
  295. origin_circuit_new(void)
  296. {
  297. origin_circuit_t *circ;
  298. /* never zero, since a global ID of 0 is treated specially by the
  299. * controller */
  300. static uint32_t n_circuits_allocated = 1;
  301. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  302. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  303. circ->next_stream_id = crypto_rand_int(1<<16);
  304. circ->global_identifier = n_circuits_allocated++;
  305. init_circuit_base(TO_CIRCUIT(circ));
  306. return circ;
  307. }
  308. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  309. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  310. or_circuit_t *
  311. or_circuit_new(uint16_t p_circ_id, or_connection_t *p_conn)
  312. {
  313. /* CircIDs */
  314. or_circuit_t *circ;
  315. circ = tor_malloc_zero(sizeof(or_circuit_t));
  316. circ->_base.magic = OR_CIRCUIT_MAGIC;
  317. if (p_conn)
  318. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  319. init_circuit_base(TO_CIRCUIT(circ));
  320. return circ;
  321. }
  322. /** Deallocate space associated with circ.
  323. */
  324. static void
  325. circuit_free(circuit_t *circ)
  326. {
  327. void *mem;
  328. tor_assert(circ);
  329. if (CIRCUIT_IS_ORIGIN(circ)) {
  330. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  331. mem = ocirc;
  332. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  333. if (ocirc->build_state) {
  334. if (ocirc->build_state->chosen_exit)
  335. extend_info_free(ocirc->build_state->chosen_exit);
  336. if (ocirc->build_state->pending_final_cpath)
  337. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  338. }
  339. tor_free(ocirc->build_state);
  340. circuit_free_cpath(ocirc->cpath);
  341. if (ocirc->intro_key)
  342. crypto_free_pk_env(ocirc->intro_key);
  343. } else {
  344. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  345. mem = ocirc;
  346. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  347. if (ocirc->p_crypto)
  348. crypto_free_cipher_env(ocirc->p_crypto);
  349. if (ocirc->p_digest)
  350. crypto_free_digest_env(ocirc->p_digest);
  351. if (ocirc->n_crypto)
  352. crypto_free_cipher_env(ocirc->n_crypto);
  353. if (ocirc->n_digest)
  354. crypto_free_digest_env(ocirc->n_digest);
  355. if (ocirc->rend_splice) {
  356. or_circuit_t *other = ocirc->rend_splice;
  357. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  358. other->rend_splice = NULL;
  359. }
  360. tor_free(circ->onionskin);
  361. /* remove from map. */
  362. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  363. /* Clear cell queue _after_ removing it from the map. Otherwise our
  364. * "active" checks will be violated. */
  365. cell_queue_clear(&ocirc->p_conn_cells);
  366. }
  367. /* Remove from map. */
  368. circuit_set_n_circid_orconn(circ, 0, NULL);
  369. /* Clear cell queue _after_ removing it from the map. Otherwise our
  370. * "active" checks will be violated. */
  371. cell_queue_clear(&circ->n_conn_cells);
  372. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  373. tor_free(mem);
  374. }
  375. /** Deallocate space associated with the linked list <b>cpath</b>. */
  376. static void
  377. circuit_free_cpath(crypt_path_t *cpath)
  378. {
  379. crypt_path_t *victim, *head=cpath;
  380. if (!cpath)
  381. return;
  382. /* it's a doubly linked list, so we have to notice when we've
  383. * gone through it once. */
  384. while (cpath->next && cpath->next != head) {
  385. victim = cpath;
  386. cpath = victim->next;
  387. circuit_free_cpath_node(victim);
  388. }
  389. circuit_free_cpath_node(cpath);
  390. }
  391. /** Release all storage held by circuits. */
  392. void
  393. circuit_free_all(void)
  394. {
  395. circuit_t *next;
  396. while (global_circuitlist) {
  397. next = global_circuitlist->next;
  398. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  399. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  400. while (or_circ->resolving_streams) {
  401. edge_connection_t *next_conn;
  402. next_conn = or_circ->resolving_streams->next_stream;
  403. connection_free(TO_CONN(or_circ->resolving_streams));
  404. or_circ->resolving_streams = next_conn;
  405. }
  406. }
  407. circuit_free(global_circuitlist);
  408. global_circuitlist = next;
  409. }
  410. if (circuits_pending_or_conns) {
  411. smartlist_free(circuits_pending_or_conns);
  412. circuits_pending_or_conns = NULL;
  413. }
  414. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  415. }
  416. /** Deallocate space associated with the cpath node <b>victim</b>. */
  417. static void
  418. circuit_free_cpath_node(crypt_path_t *victim)
  419. {
  420. if (victim->f_crypto)
  421. crypto_free_cipher_env(victim->f_crypto);
  422. if (victim->b_crypto)
  423. crypto_free_cipher_env(victim->b_crypto);
  424. if (victim->f_digest)
  425. crypto_free_digest_env(victim->f_digest);
  426. if (victim->b_digest)
  427. crypto_free_digest_env(victim->b_digest);
  428. if (victim->dh_handshake_state)
  429. crypto_dh_free(victim->dh_handshake_state);
  430. if (victim->extend_info)
  431. extend_info_free(victim->extend_info);
  432. victim->magic = 0xDEADBEEFu;
  433. tor_free(victim);
  434. }
  435. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  436. * of information about circuit <b>circ</b>.
  437. */
  438. static void
  439. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  440. const char *type, int this_circid, int other_circid)
  441. {
  442. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  443. "state %d (%s), born %d:",
  444. conn_array_index, type, this_circid, other_circid, circ->state,
  445. circuit_state_to_string(circ->state), (int)circ->timestamp_created);
  446. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  447. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  448. }
  449. }
  450. /** Log, at severity <b>severity</b>, information about each circuit
  451. * that is connected to <b>conn</b>.
  452. */
  453. void
  454. circuit_dump_by_conn(connection_t *conn, int severity)
  455. {
  456. circuit_t *circ;
  457. edge_connection_t *tmpconn;
  458. for (circ=global_circuitlist;circ;circ = circ->next) {
  459. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  460. if (circ->marked_for_close)
  461. continue;
  462. if (! CIRCUIT_IS_ORIGIN(circ))
  463. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  464. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  465. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  466. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  467. p_circ_id, n_circ_id);
  468. if (CIRCUIT_IS_ORIGIN(circ)) {
  469. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  470. tmpconn=tmpconn->next_stream) {
  471. if (TO_CONN(tmpconn) == conn) {
  472. circuit_dump_details(severity, circ, conn->conn_array_index,
  473. "App-ward", p_circ_id, n_circ_id);
  474. }
  475. }
  476. }
  477. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  478. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  479. n_circ_id, p_circ_id);
  480. if (! CIRCUIT_IS_ORIGIN(circ)) {
  481. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  482. tmpconn=tmpconn->next_stream) {
  483. if (TO_CONN(tmpconn) == conn) {
  484. circuit_dump_details(severity, circ, conn->conn_array_index,
  485. "Exit-ward", n_circ_id, p_circ_id);
  486. }
  487. }
  488. }
  489. if (!circ->n_conn && circ->n_addr && circ->n_port &&
  490. circ->n_addr == conn->addr &&
  491. circ->n_port == conn->port &&
  492. conn->type == CONN_TYPE_OR &&
  493. !memcmp(TO_OR_CONN(conn)->identity_digest, circ->n_conn_id_digest,
  494. DIGEST_LEN)) {
  495. circuit_dump_details(severity, circ, conn->conn_array_index,
  496. (circ->state == CIRCUIT_STATE_OPEN &&
  497. !CIRCUIT_IS_ORIGIN(circ)) ?
  498. "Endpoint" : "Pending",
  499. n_circ_id, p_circ_id);
  500. }
  501. }
  502. }
  503. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  504. * such circuit exists. */
  505. origin_circuit_t *
  506. circuit_get_by_global_id(uint32_t id)
  507. {
  508. circuit_t *circ;
  509. for (circ=global_circuitlist;circ;circ = circ->next) {
  510. if (CIRCUIT_IS_ORIGIN(circ) &&
  511. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  512. if (circ->marked_for_close)
  513. return NULL;
  514. else
  515. return TO_ORIGIN_CIRCUIT(circ);
  516. }
  517. }
  518. return NULL;
  519. }
  520. /** Return a circ such that:
  521. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  522. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  523. * Return NULL if no such circuit exists.
  524. */
  525. static INLINE circuit_t *
  526. circuit_get_by_circid_orconn_impl(uint16_t circ_id, or_connection_t *conn)
  527. {
  528. orconn_circid_circuit_map_t search;
  529. orconn_circid_circuit_map_t *found;
  530. if (_last_circid_orconn_ent &&
  531. circ_id == _last_circid_orconn_ent->circ_id &&
  532. conn == _last_circid_orconn_ent->or_conn) {
  533. found = _last_circid_orconn_ent;
  534. } else {
  535. search.circ_id = circ_id;
  536. search.or_conn = conn;
  537. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  538. _last_circid_orconn_ent = found;
  539. }
  540. if (found && found->circuit)
  541. return found->circuit;
  542. return NULL;
  543. /* The rest of this checks for bugs. Disabled by default. */
  544. {
  545. circuit_t *circ;
  546. for (circ=global_circuitlist;circ;circ = circ->next) {
  547. if (! CIRCUIT_IS_ORIGIN(circ)) {
  548. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  549. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  550. log_warn(LD_BUG,
  551. "circuit matches p_conn, but not in hash table (Bug!)");
  552. return circ;
  553. }
  554. }
  555. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  556. log_warn(LD_BUG,
  557. "circuit matches n_conn, but not in hash table (Bug!)");
  558. return circ;
  559. }
  560. }
  561. return NULL;
  562. }
  563. }
  564. /** Return a circ such that:
  565. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  566. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  567. * - circ is not marked for close.
  568. * Return NULL if no such circuit exists.
  569. */
  570. circuit_t *
  571. circuit_get_by_circid_orconn(uint16_t circ_id, or_connection_t *conn)
  572. {
  573. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  574. if (!circ || circ->marked_for_close)
  575. return NULL;
  576. else
  577. return circ;
  578. }
  579. /** Return the circuit that a given edge connection is using. */
  580. circuit_t *
  581. circuit_get_by_edge_conn(edge_connection_t *conn)
  582. {
  583. circuit_t *circ;
  584. circ = conn->on_circuit;
  585. tor_assert(!circ ||
  586. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  587. : circ->magic == OR_CIRCUIT_MAGIC));
  588. return circ;
  589. }
  590. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  591. * circuit from the orconn,circid map, and mark it for close if it hasn't
  592. * been marked already.
  593. */
  594. void
  595. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  596. {
  597. circuit_t *circ;
  598. connection_or_unlink_all_active_circs(conn);
  599. for (circ = global_circuitlist; circ; circ = circ->next) {
  600. int mark = 0;
  601. if (circ->n_conn == conn) {
  602. circuit_set_n_circid_orconn(circ, 0, NULL);
  603. mark = 1;
  604. }
  605. if (! CIRCUIT_IS_ORIGIN(circ)) {
  606. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  607. if (or_circ->p_conn == conn) {
  608. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  609. mark = 1;
  610. }
  611. }
  612. if (mark && !circ->marked_for_close)
  613. circuit_mark_for_close(circ, reason);
  614. }
  615. }
  616. /** Return a circ such that:
  617. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  618. * - circ-\>purpose is equal to <b>purpose</b>.
  619. *
  620. * Return NULL if no such circuit exists.
  621. */
  622. origin_circuit_t *
  623. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  624. {
  625. circuit_t *circ;
  626. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  627. for (circ = global_circuitlist; circ; circ = circ->next) {
  628. if (!circ->marked_for_close &&
  629. circ->purpose == purpose &&
  630. !rend_cmp_service_ids(rend_query, TO_ORIGIN_CIRCUIT(circ)->rend_query))
  631. return TO_ORIGIN_CIRCUIT(circ);
  632. }
  633. return NULL;
  634. }
  635. /** Return the first circuit originating here in global_circuitlist after
  636. * <b>start</b> whose purpose is <b>purpose</b>, and where
  637. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  638. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  639. */
  640. origin_circuit_t *
  641. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  642. const char *digest, uint8_t purpose)
  643. {
  644. circuit_t *circ;
  645. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  646. if (start == NULL)
  647. circ = global_circuitlist;
  648. else
  649. circ = TO_CIRCUIT(start)->next;
  650. for ( ; circ; circ = circ->next) {
  651. if (circ->marked_for_close)
  652. continue;
  653. if (circ->purpose != purpose)
  654. continue;
  655. if (!digest)
  656. return TO_ORIGIN_CIRCUIT(circ);
  657. else if (!memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_pk_digest,
  658. digest, DIGEST_LEN))
  659. return TO_ORIGIN_CIRCUIT(circ);
  660. }
  661. return NULL;
  662. }
  663. /** Return the first OR circuit in the global list whose purpose is
  664. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  665. * <b>token</b>. */
  666. static or_circuit_t *
  667. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  668. size_t len)
  669. {
  670. circuit_t *circ;
  671. for (circ = global_circuitlist; circ; circ = circ->next) {
  672. if (! circ->marked_for_close &&
  673. circ->purpose == purpose &&
  674. ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  675. return TO_OR_CIRCUIT(circ);
  676. }
  677. return NULL;
  678. }
  679. /** Return the circuit waiting for a rendezvous with the provided cookie.
  680. * Return NULL if no such circuit is found.
  681. */
  682. or_circuit_t *
  683. circuit_get_rendezvous(const char *cookie)
  684. {
  685. return circuit_get_by_rend_token_and_purpose(
  686. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  687. cookie, REND_COOKIE_LEN);
  688. }
  689. /** Return the circuit waiting for intro cells of the given digest.
  690. * Return NULL if no such circuit is found.
  691. */
  692. or_circuit_t *
  693. circuit_get_intro_point(const char *digest)
  694. {
  695. return circuit_get_by_rend_token_and_purpose(
  696. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  697. DIGEST_LEN);
  698. }
  699. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  700. * has a timestamp_dirty value of 0, is uptime/capacity/internal
  701. * if required, and if info is defined, does not already use info
  702. * as any of its hops; or NULL if no circuit fits this description.
  703. *
  704. * If ! need_uptime, prefer returning non-uptime circuits.
  705. */
  706. origin_circuit_t *
  707. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  708. int need_uptime,
  709. int need_capacity, int internal)
  710. {
  711. circuit_t *_circ;
  712. origin_circuit_t *best=NULL;
  713. log_debug(LD_CIRC,
  714. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  715. "capacity %d, internal %d",
  716. purpose, need_uptime, need_capacity, internal);
  717. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  718. if (CIRCUIT_IS_ORIGIN(_circ) &&
  719. _circ->state == CIRCUIT_STATE_OPEN &&
  720. !_circ->marked_for_close &&
  721. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  722. !_circ->timestamp_dirty) {
  723. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  724. #if 0 /* XXX here while roger investigates a reported RendNodes bug */
  725. if (_circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  726. options->RendNodes) {
  727. routerinfo_t *exit = build_state_get_exit_router(circ->build_state);
  728. if (exit && !router_nickname_is_in_list(exit, options->RendNodes))
  729. continue; /* not one of our allowed RendNodes */
  730. }
  731. #endif
  732. if ((!need_uptime || circ->build_state->need_uptime) &&
  733. (!need_capacity || circ->build_state->need_capacity) &&
  734. (internal == circ->build_state->is_internal)) {
  735. if (info) {
  736. /* need to make sure we don't duplicate hops */
  737. crypt_path_t *hop = circ->cpath;
  738. routerinfo_t *ri1 = router_get_by_digest(info->identity_digest);
  739. do {
  740. routerinfo_t *ri2;
  741. if (!memcmp(hop->extend_info->identity_digest,
  742. info->identity_digest, DIGEST_LEN))
  743. goto next;
  744. if (ri1 &&
  745. (ri2 = router_get_by_digest(hop->extend_info->identity_digest))
  746. && routers_in_same_family(ri1, ri2))
  747. goto next;
  748. hop=hop->next;
  749. } while (hop!=circ->cpath);
  750. }
  751. if (!best || (best->build_state->need_uptime && !need_uptime))
  752. best = circ;
  753. next: ;
  754. }
  755. }
  756. }
  757. return best;
  758. }
  759. /** Return the number of hops in circuit's path. */
  760. int
  761. circuit_get_cpath_len(origin_circuit_t *circ)
  762. {
  763. int n = 0;
  764. if (circ && circ->cpath) {
  765. crypt_path_t *cpath, *cpath_next = NULL;
  766. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  767. cpath_next = cpath->next;
  768. ++n;
  769. }
  770. }
  771. return n;
  772. }
  773. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  774. * aren't that many hops in the list. */
  775. crypt_path_t *
  776. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  777. {
  778. if (circ && circ->cpath && hopnum > 0) {
  779. crypt_path_t *cpath, *cpath_next = NULL;
  780. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  781. cpath_next = cpath->next;
  782. if (--hopnum <= 0)
  783. return cpath;
  784. }
  785. }
  786. return NULL;
  787. }
  788. /** Go through the circuitlist; mark-for-close each circuit that starts
  789. * at us but has not yet been used. */
  790. void
  791. circuit_mark_all_unused_circs(void)
  792. {
  793. circuit_t *circ;
  794. for (circ=global_circuitlist; circ; circ = circ->next) {
  795. if (CIRCUIT_IS_ORIGIN(circ) &&
  796. !circ->marked_for_close &&
  797. !circ->timestamp_dirty)
  798. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  799. }
  800. }
  801. /** Go through the circuitlist; for each circuit that starts at us
  802. * and is dirty, frob its timestamp_dirty so we won't use it for any
  803. * new streams.
  804. *
  805. * This is useful for letting the user change pseudonyms, so new
  806. * streams will not be linkable to old streams.
  807. */
  808. void
  809. circuit_expire_all_dirty_circs(void)
  810. {
  811. circuit_t *circ;
  812. or_options_t *options = get_options();
  813. for (circ=global_circuitlist; circ; circ = circ->next) {
  814. if (CIRCUIT_IS_ORIGIN(circ) &&
  815. !circ->marked_for_close &&
  816. circ->timestamp_dirty)
  817. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  818. }
  819. }
  820. /** Mark <b>circ</b> to be closed next time we call
  821. * circuit_close_all_marked(). Do any cleanup needed:
  822. * - If state is onionskin_pending, remove circ from the onion_pending
  823. * list.
  824. * - If circ isn't open yet: call circuit_build_failed() if we're
  825. * the origin, and in either case call circuit_rep_hist_note_result()
  826. * to note stats.
  827. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  828. * just tried from our list of intro points for that service
  829. * descriptor.
  830. * - Send appropriate destroys and edge_destroys for conns and
  831. * streams attached to circ.
  832. * - If circ->rend_splice is set (we are the midpoint of a joined
  833. * rendezvous stream), then mark the other circuit to close as well.
  834. */
  835. void
  836. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  837. const char *file)
  838. {
  839. int orig_reason = reason; /* Passed to the controller */
  840. assert_circuit_ok(circ);
  841. tor_assert(line);
  842. tor_assert(file);
  843. if (circ->marked_for_close) {
  844. log(LOG_WARN,LD_BUG,
  845. "Duplicate call to circuit_mark_for_close at %s:%d"
  846. " (first at %s:%d)", file, line,
  847. circ->marked_for_close_file, circ->marked_for_close);
  848. return;
  849. }
  850. if (reason == END_CIRC_AT_ORIGIN) {
  851. if (!CIRCUIT_IS_ORIGIN(circ)) {
  852. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  853. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  854. file, line, circ->purpose);
  855. }
  856. reason = END_CIRC_REASON_NONE;
  857. }
  858. if (CIRCUIT_IS_ORIGIN(circ)) {
  859. /* We don't send reasons when closing circuits at the origin. */
  860. reason = END_CIRC_REASON_NONE;
  861. }
  862. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  863. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  864. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  865. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  866. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  867. reason = END_CIRC_REASON_NONE;
  868. }
  869. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  870. onion_pending_remove(TO_OR_CIRCUIT(circ));
  871. }
  872. /* If the circuit ever became OPEN, we sent it to the reputation history
  873. * module then. If it isn't OPEN, we send it there now to remember which
  874. * links worked and which didn't.
  875. */
  876. if (circ->state != CIRCUIT_STATE_OPEN) {
  877. if (CIRCUIT_IS_ORIGIN(circ)) {
  878. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  879. circuit_build_failed(ocirc); /* take actions if necessary */
  880. circuit_rep_hist_note_result(ocirc);
  881. }
  882. }
  883. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  884. if (circuits_pending_or_conns)
  885. smartlist_remove(circuits_pending_or_conns, circ);
  886. }
  887. if (CIRCUIT_IS_ORIGIN(circ)) {
  888. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  889. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  890. orig_reason);
  891. }
  892. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  893. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  894. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  895. tor_assert(ocirc->build_state->chosen_exit);
  896. /* treat this like getting a nack from it */
  897. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  898. "Removing from descriptor.",
  899. safe_str(ocirc->rend_query),
  900. safe_str(build_state_get_exit_nickname(ocirc->build_state)));
  901. rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
  902. ocirc->rend_query);
  903. }
  904. if (circ->n_conn)
  905. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  906. if (! CIRCUIT_IS_ORIGIN(circ)) {
  907. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  908. edge_connection_t *conn;
  909. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  910. connection_edge_destroy(or_circ->p_circ_id, conn);
  911. while (or_circ->resolving_streams) {
  912. conn = or_circ->resolving_streams;
  913. or_circ->resolving_streams = conn->next_stream;
  914. if (!conn->_base.marked_for_close) {
  915. /* The client will see a DESTROY, and infer that the connections
  916. * are closing because the circuit is getting torn down. No need
  917. * to send an end cell. */
  918. conn->_base.edge_has_sent_end = 1;
  919. conn->end_reason = END_STREAM_REASON_DESTROY;
  920. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  921. connection_mark_for_close(TO_CONN(conn));
  922. }
  923. conn->on_circuit = NULL;
  924. }
  925. if (or_circ->p_conn)
  926. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  927. } else {
  928. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  929. edge_connection_t *conn;
  930. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  931. connection_edge_destroy(circ->n_circ_id, conn);
  932. }
  933. circ->marked_for_close = line;
  934. circ->marked_for_close_file = file;
  935. if (!CIRCUIT_IS_ORIGIN(circ)) {
  936. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  937. if (or_circ->rend_splice) {
  938. if (!or_circ->rend_splice->_base.marked_for_close) {
  939. /* do this after marking this circuit, to avoid infinite recursion. */
  940. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  941. }
  942. or_circ->rend_splice = NULL;
  943. }
  944. }
  945. }
  946. /** Verify that cpath layer <b>cp</b> has all of its invariants
  947. * correct. Trigger an assert if anything is invalid.
  948. */
  949. void
  950. assert_cpath_layer_ok(const crypt_path_t *cp)
  951. {
  952. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  953. // tor_assert(cp->port);
  954. tor_assert(cp);
  955. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  956. switch (cp->state)
  957. {
  958. case CPATH_STATE_OPEN:
  959. tor_assert(cp->f_crypto);
  960. tor_assert(cp->b_crypto);
  961. /* fall through */
  962. case CPATH_STATE_CLOSED:
  963. tor_assert(!cp->dh_handshake_state);
  964. break;
  965. case CPATH_STATE_AWAITING_KEYS:
  966. /* tor_assert(cp->dh_handshake_state); */
  967. break;
  968. default:
  969. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  970. tor_assert(0);
  971. }
  972. tor_assert(cp->package_window >= 0);
  973. tor_assert(cp->deliver_window >= 0);
  974. }
  975. /** Verify that cpath <b>cp</b> has all of its invariants
  976. * correct. Trigger an assert if anything is invalid.
  977. */
  978. static void
  979. assert_cpath_ok(const crypt_path_t *cp)
  980. {
  981. const crypt_path_t *start = cp;
  982. do {
  983. assert_cpath_layer_ok(cp);
  984. /* layers must be in sequence of: "open* awaiting? closed*" */
  985. if (cp != start) {
  986. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  987. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  988. } else if (cp->state == CPATH_STATE_OPEN) {
  989. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  990. }
  991. }
  992. cp = cp->next;
  993. tor_assert(cp);
  994. } while (cp != start);
  995. }
  996. /** Verify that circuit <b>c</b> has all of its invariants
  997. * correct. Trigger an assert if anything is invalid.
  998. */
  999. void
  1000. assert_circuit_ok(const circuit_t *c)
  1001. {
  1002. edge_connection_t *conn;
  1003. const or_circuit_t *or_circ = NULL;
  1004. const origin_circuit_t *origin_circ = NULL;
  1005. tor_assert(c);
  1006. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1007. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1008. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1009. {
  1010. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1011. * never understand. -NM. */
  1012. circuit_t *nonconst_circ = (circuit_t*) c;
  1013. if (CIRCUIT_IS_ORIGIN(c))
  1014. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1015. else
  1016. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1017. }
  1018. if (c->n_conn) {
  1019. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
  1020. DIGEST_LEN));
  1021. if (c->n_circ_id)
  1022. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  1023. }
  1024. if (or_circ && or_circ->p_conn) {
  1025. if (or_circ->p_circ_id)
  1026. tor_assert(c == circuit_get_by_circid_orconn(or_circ->p_circ_id,
  1027. or_circ->p_conn));
  1028. }
  1029. #if 0 /* false now that rendezvous exits are attached to p_streams */
  1030. if (origin_circ)
  1031. for (conn = origin_circ->p_streams; conn; conn = conn->next_stream)
  1032. tor_assert(conn->_base.type == CONN_TYPE_AP);
  1033. #endif
  1034. if (or_circ)
  1035. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1036. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1037. tor_assert(c->deliver_window >= 0);
  1038. tor_assert(c->package_window >= 0);
  1039. if (c->state == CIRCUIT_STATE_OPEN) {
  1040. tor_assert(!c->onionskin);
  1041. if (or_circ) {
  1042. tor_assert(or_circ->n_crypto);
  1043. tor_assert(or_circ->p_crypto);
  1044. tor_assert(or_circ->n_digest);
  1045. tor_assert(or_circ->p_digest);
  1046. }
  1047. }
  1048. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1049. tor_assert(circuits_pending_or_conns &&
  1050. smartlist_isin(circuits_pending_or_conns, c));
  1051. } else {
  1052. tor_assert(!circuits_pending_or_conns ||
  1053. !smartlist_isin(circuits_pending_or_conns, c));
  1054. }
  1055. if (origin_circ && origin_circ->cpath) {
  1056. assert_cpath_ok(origin_circ->cpath);
  1057. }
  1058. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1059. tor_assert(or_circ);
  1060. if (!c->marked_for_close) {
  1061. tor_assert(or_circ->rend_splice);
  1062. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1063. }
  1064. tor_assert(or_circ->rend_splice != or_circ);
  1065. } else {
  1066. tor_assert(!or_circ || !or_circ->rend_splice);
  1067. }
  1068. }