circuitlist.c 37 KB

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