circuitlist.c 36 KB

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