circuitlist.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. cell_queue_clear(&ocirc->p_conn_cells);
  352. tor_free(circ->onionskin);
  353. /* remove from map. */
  354. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  355. /* Clear cell queue _after_ removing it from the map. Otherwise our
  356. * "active" checks will be violated. */
  357. cell_queue_clear(&ocirc->p_conn_cells);
  358. }
  359. cell_queue_clear(&circ->n_conn_cells);
  360. /* Remove from map. */
  361. circuit_set_n_circid_orconn(circ, 0, NULL);
  362. /* Clear cell queue _after_ removing it from the map. Otherwise our
  363. * "active" checks will be violated. */
  364. cell_queue_clear(&circ->n_conn_cells);
  365. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  366. tor_free(mem);
  367. }
  368. /** Deallocate space associated with the linked list <b>cpath</b>. */
  369. static void
  370. circuit_free_cpath(crypt_path_t *cpath)
  371. {
  372. crypt_path_t *victim, *head=cpath;
  373. if (!cpath)
  374. return;
  375. /* it's a doubly linked list, so we have to notice when we've
  376. * gone through it once. */
  377. while (cpath->next && cpath->next != head) {
  378. victim = cpath;
  379. cpath = victim->next;
  380. circuit_free_cpath_node(victim);
  381. }
  382. circuit_free_cpath_node(cpath);
  383. }
  384. /** Release all storage held by circuits. */
  385. void
  386. circuit_free_all(void)
  387. {
  388. circuit_t *next;
  389. while (global_circuitlist) {
  390. next = global_circuitlist->next;
  391. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  392. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  393. while (or_circ->resolving_streams) {
  394. edge_connection_t *next;
  395. next = or_circ->resolving_streams->next_stream;
  396. connection_free(TO_CONN(or_circ->resolving_streams));
  397. or_circ->resolving_streams = next;
  398. }
  399. }
  400. circuit_free(global_circuitlist);
  401. global_circuitlist = next;
  402. }
  403. if (circuits_pending_or_conns) {
  404. smartlist_free(circuits_pending_or_conns);
  405. circuits_pending_or_conns = NULL;
  406. }
  407. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  408. }
  409. /** Deallocate space associated with the cpath node <b>victim</b>. */
  410. static void
  411. circuit_free_cpath_node(crypt_path_t *victim)
  412. {
  413. if (victim->f_crypto)
  414. crypto_free_cipher_env(victim->f_crypto);
  415. if (victim->b_crypto)
  416. crypto_free_cipher_env(victim->b_crypto);
  417. if (victim->f_digest)
  418. crypto_free_digest_env(victim->f_digest);
  419. if (victim->b_digest)
  420. crypto_free_digest_env(victim->b_digest);
  421. if (victim->dh_handshake_state)
  422. crypto_dh_free(victim->dh_handshake_state);
  423. if (victim->extend_info)
  424. extend_info_free(victim->extend_info);
  425. victim->magic = 0xDEADBEEFu;
  426. tor_free(victim);
  427. }
  428. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  429. * of information about circuit <b>circ</b>.
  430. */
  431. static void
  432. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  433. const char *type, int this_circid, int other_circid)
  434. {
  435. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  436. "state %d (%s), born %d:",
  437. conn_array_index, type, this_circid, other_circid, circ->state,
  438. circuit_state_to_string(circ->state), (int)circ->timestamp_created);
  439. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  440. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  441. }
  442. }
  443. /** Log, at severity <b>severity</b>, information about each circuit
  444. * that is connected to <b>conn</b>.
  445. */
  446. void
  447. circuit_dump_by_conn(connection_t *conn, int severity)
  448. {
  449. circuit_t *circ;
  450. edge_connection_t *tmpconn;
  451. for (circ=global_circuitlist;circ;circ = circ->next) {
  452. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  453. if (circ->marked_for_close)
  454. continue;
  455. if (! CIRCUIT_IS_ORIGIN(circ))
  456. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  457. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  458. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  459. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  460. p_circ_id, n_circ_id);
  461. if (CIRCUIT_IS_ORIGIN(circ)) {
  462. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  463. tmpconn=tmpconn->next_stream) {
  464. if (TO_CONN(tmpconn) == conn) {
  465. circuit_dump_details(severity, circ, conn->conn_array_index,
  466. "App-ward", p_circ_id, n_circ_id);
  467. }
  468. }
  469. }
  470. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  471. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  472. n_circ_id, p_circ_id);
  473. if (! CIRCUIT_IS_ORIGIN(circ)) {
  474. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  475. tmpconn=tmpconn->next_stream) {
  476. if (TO_CONN(tmpconn) == conn) {
  477. circuit_dump_details(severity, circ, conn->conn_array_index,
  478. "Exit-ward", n_circ_id, p_circ_id);
  479. }
  480. }
  481. }
  482. if (!circ->n_conn && circ->n_addr && circ->n_port &&
  483. circ->n_addr == conn->addr &&
  484. circ->n_port == conn->port &&
  485. conn->type == CONN_TYPE_OR &&
  486. !memcmp(TO_OR_CONN(conn)->identity_digest, circ->n_conn_id_digest,
  487. DIGEST_LEN)) {
  488. circuit_dump_details(severity, circ, conn->conn_array_index,
  489. (circ->state == CIRCUIT_STATE_OPEN &&
  490. !CIRCUIT_IS_ORIGIN(circ)) ?
  491. "Endpoint" : "Pending",
  492. n_circ_id, p_circ_id);
  493. }
  494. }
  495. }
  496. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  497. * such circuit exists. */
  498. origin_circuit_t *
  499. circuit_get_by_global_id(uint32_t id)
  500. {
  501. circuit_t *circ;
  502. for (circ=global_circuitlist;circ;circ = circ->next) {
  503. if (CIRCUIT_IS_ORIGIN(circ) &&
  504. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  505. if (circ->marked_for_close)
  506. return NULL;
  507. else
  508. return TO_ORIGIN_CIRCUIT(circ);
  509. }
  510. }
  511. return NULL;
  512. }
  513. /** Return a circ such that:
  514. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  515. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  516. * Return NULL if no such circuit exists.
  517. */
  518. static INLINE circuit_t *
  519. circuit_get_by_circid_orconn_impl(uint16_t circ_id, or_connection_t *conn)
  520. {
  521. orconn_circid_circuit_map_t search;
  522. orconn_circid_circuit_map_t *found;
  523. if (_last_circid_orconn_ent &&
  524. circ_id == _last_circid_orconn_ent->circ_id &&
  525. conn == _last_circid_orconn_ent->or_conn) {
  526. found = _last_circid_orconn_ent;
  527. } else {
  528. search.circ_id = circ_id;
  529. search.or_conn = conn;
  530. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  531. _last_circid_orconn_ent = found;
  532. }
  533. if (found && found->circuit)
  534. return found->circuit;
  535. return NULL;
  536. /* The rest of this checks for bugs. Disabled by default. */
  537. {
  538. circuit_t *circ;
  539. for (circ=global_circuitlist;circ;circ = circ->next) {
  540. if (! CIRCUIT_IS_ORIGIN(circ)) {
  541. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  542. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  543. log_warn(LD_BUG,
  544. "circuit matches p_conn, but not in hash table (Bug!)");
  545. return circ;
  546. }
  547. }
  548. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  549. log_warn(LD_BUG,
  550. "circuit matches n_conn, but not in hash table (Bug!)");
  551. return circ;
  552. }
  553. }
  554. return NULL;
  555. }
  556. }
  557. /** Return a circ such that:
  558. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  559. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  560. * - circ is not marked for close.
  561. * Return NULL if no such circuit exists.
  562. */
  563. circuit_t *
  564. circuit_get_by_circid_orconn(uint16_t circ_id, or_connection_t *conn)
  565. {
  566. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  567. if (!circ || circ->marked_for_close)
  568. return NULL;
  569. else
  570. return circ;
  571. }
  572. /** Return the circuit that a given edge connection is using. */
  573. circuit_t *
  574. circuit_get_by_edge_conn(edge_connection_t *conn)
  575. {
  576. circuit_t *circ;
  577. circ = conn->on_circuit;
  578. tor_assert(!circ ||
  579. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  580. : circ->magic == OR_CIRCUIT_MAGIC));
  581. return circ;
  582. }
  583. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  584. * circuit from the orconn,circid map, and mark it for close if it hasn't
  585. * been marked already.
  586. */
  587. void
  588. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  589. {
  590. circuit_t *circ;
  591. connection_or_unlink_all_active_circs(conn);
  592. for (circ = global_circuitlist; circ; circ = circ->next) {
  593. int mark = 0;
  594. if (circ->n_conn == conn) {
  595. circuit_set_n_circid_orconn(circ, 0, NULL);
  596. mark = 1;
  597. }
  598. if (! CIRCUIT_IS_ORIGIN(circ)) {
  599. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  600. if (or_circ->p_conn == conn) {
  601. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  602. mark = 1;
  603. }
  604. }
  605. if (mark && !circ->marked_for_close)
  606. circuit_mark_for_close(circ, reason);
  607. }
  608. }
  609. /** Return a circ such that:
  610. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  611. * - circ-\>purpose is equal to <b>purpose</b>.
  612. *
  613. * Return NULL if no such circuit exists.
  614. */
  615. origin_circuit_t *
  616. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  617. {
  618. circuit_t *circ;
  619. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  620. for (circ = global_circuitlist; circ; circ = circ->next) {
  621. if (!circ->marked_for_close &&
  622. circ->purpose == purpose &&
  623. !rend_cmp_service_ids(rend_query, TO_ORIGIN_CIRCUIT(circ)->rend_query))
  624. return TO_ORIGIN_CIRCUIT(circ);
  625. }
  626. return NULL;
  627. }
  628. /** Return the first circuit originating here in global_circuitlist after
  629. * <b>start</b> whose purpose is <b>purpose</b>, and where
  630. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  631. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  632. */
  633. origin_circuit_t *
  634. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  635. const char *digest, uint8_t purpose)
  636. {
  637. circuit_t *circ;
  638. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  639. if (start == NULL)
  640. circ = global_circuitlist;
  641. else
  642. circ = TO_CIRCUIT(start)->next;
  643. for ( ; circ; circ = circ->next) {
  644. if (circ->marked_for_close)
  645. continue;
  646. if (circ->purpose != purpose)
  647. continue;
  648. if (!digest)
  649. return TO_ORIGIN_CIRCUIT(circ);
  650. else if (!memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_pk_digest,
  651. digest, DIGEST_LEN))
  652. return TO_ORIGIN_CIRCUIT(circ);
  653. }
  654. return NULL;
  655. }
  656. /** Return the first OR circuit in the global list whose purpose is
  657. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  658. * <b>token</b>. */
  659. static or_circuit_t *
  660. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  661. size_t len)
  662. {
  663. circuit_t *circ;
  664. for (circ = global_circuitlist; circ; circ = circ->next) {
  665. if (! circ->marked_for_close &&
  666. circ->purpose == purpose &&
  667. ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  668. return TO_OR_CIRCUIT(circ);
  669. }
  670. return NULL;
  671. }
  672. /** Return the circuit waiting for a rendezvous with the provided cookie.
  673. * Return NULL if no such circuit is found.
  674. */
  675. or_circuit_t *
  676. circuit_get_rendezvous(const char *cookie)
  677. {
  678. return circuit_get_by_rend_token_and_purpose(
  679. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  680. cookie, REND_COOKIE_LEN);
  681. }
  682. /** Return the circuit waiting for intro cells of the given digest.
  683. * Return NULL if no such circuit is found.
  684. */
  685. or_circuit_t *
  686. circuit_get_intro_point(const char *digest)
  687. {
  688. return circuit_get_by_rend_token_and_purpose(
  689. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  690. DIGEST_LEN);
  691. }
  692. /** Return a circuit that is open, has specified <b>purpose</b>,
  693. * has a timestamp_dirty value of 0, is uptime/capacity/internal
  694. * if required, and if info is defined, does not already use info
  695. * as any of its hops; or NULL if no circuit fits this description.
  696. *
  697. * If ! need_uptime, prefer returning non-uptime circuits.
  698. */
  699. origin_circuit_t *
  700. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  701. int need_uptime,
  702. int need_capacity, int internal)
  703. {
  704. circuit_t *_circ;
  705. origin_circuit_t *best=NULL;
  706. log_debug(LD_CIRC,
  707. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  708. "capacity %d, internal %d",
  709. purpose, need_uptime, need_capacity, internal);
  710. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  711. if (CIRCUIT_IS_ORIGIN(_circ) &&
  712. _circ->state == CIRCUIT_STATE_OPEN &&
  713. !_circ->marked_for_close &&
  714. _circ->purpose == purpose &&
  715. !_circ->timestamp_dirty) {
  716. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  717. if ((!need_uptime || circ->build_state->need_uptime) &&
  718. (!need_capacity || circ->build_state->need_capacity) &&
  719. (internal == circ->build_state->is_internal)) {
  720. if (info) {
  721. /* need to make sure we don't duplicate hops */
  722. crypt_path_t *hop = circ->cpath;
  723. do {
  724. if (!memcmp(hop->extend_info->identity_digest,
  725. info->identity_digest, DIGEST_LEN))
  726. goto next;
  727. hop=hop->next;
  728. } while (hop!=circ->cpath);
  729. }
  730. if (!best || (best->build_state->need_uptime && !need_uptime))
  731. best = circ;
  732. next: ;
  733. }
  734. }
  735. }
  736. return best;
  737. }
  738. /** Return the number of hops in circuit's path. */
  739. int
  740. circuit_get_cpath_len(origin_circuit_t *circ)
  741. {
  742. int n = 0;
  743. if (circ && circ->cpath) {
  744. crypt_path_t *cpath, *cpath_next = NULL;
  745. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  746. cpath_next = cpath->next;
  747. ++n;
  748. }
  749. }
  750. return n;
  751. }
  752. /** Go through the circuitlist; mark-for-close each circuit that starts
  753. * at us but has not yet been used. */
  754. void
  755. circuit_mark_all_unused_circs(void)
  756. {
  757. circuit_t *circ;
  758. for (circ=global_circuitlist; circ; circ = circ->next) {
  759. if (CIRCUIT_IS_ORIGIN(circ) &&
  760. !circ->marked_for_close &&
  761. !circ->timestamp_dirty)
  762. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  763. }
  764. }
  765. /** Go through the circuitlist; for each circuit that starts at us
  766. * and is dirty, frob its timestamp_dirty so we won't use it for any
  767. * new streams.
  768. *
  769. * This is useful for letting the user change pseudonyms, so new
  770. * streams will not be linkable to old streams.
  771. */
  772. void
  773. circuit_expire_all_dirty_circs(void)
  774. {
  775. circuit_t *circ;
  776. or_options_t *options = get_options();
  777. for (circ=global_circuitlist; circ; circ = circ->next) {
  778. if (CIRCUIT_IS_ORIGIN(circ) &&
  779. !circ->marked_for_close &&
  780. circ->timestamp_dirty)
  781. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  782. }
  783. }
  784. /** Mark <b>circ</b> to be closed next time we call
  785. * circuit_close_all_marked(). Do any cleanup needed:
  786. * - If state is onionskin_pending, remove circ from the onion_pending
  787. * list.
  788. * - If circ isn't open yet: call circuit_build_failed() if we're
  789. * the origin, and in either case call circuit_rep_hist_note_result()
  790. * to note stats.
  791. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  792. * just tried from our list of intro points for that service
  793. * descriptor.
  794. * - Send appropriate destroys and edge_destroys for conns and
  795. * streams attached to circ.
  796. * - If circ->rend_splice is set (we are the midpoint of a joined
  797. * rendezvous stream), then mark the other circuit to close as well.
  798. */
  799. void
  800. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  801. const char *file)
  802. {
  803. int orig_reason = reason; /* Passed to the controller */
  804. assert_circuit_ok(circ);
  805. tor_assert(line);
  806. tor_assert(file);
  807. if (circ->marked_for_close) {
  808. log(LOG_WARN,LD_BUG,
  809. "Duplicate call to circuit_mark_for_close at %s:%d"
  810. " (first at %s:%d)", file, line,
  811. circ->marked_for_close_file, circ->marked_for_close);
  812. return;
  813. }
  814. if (reason == END_CIRC_AT_ORIGIN) {
  815. if (!CIRCUIT_IS_ORIGIN(circ)) {
  816. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  817. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  818. file, line, circ->purpose);
  819. }
  820. reason = END_CIRC_REASON_NONE;
  821. } else if (CIRCUIT_IS_ORIGIN(circ) && reason < _END_CIRC_REASON_MIN) {
  822. /* We don't send reasons when closing circuits at the origin, but we want
  823. * to track them anyway so we can give them to the controller. */
  824. reason = END_CIRC_REASON_NONE;
  825. }
  826. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  827. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  828. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  829. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  830. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  831. reason = END_CIRC_REASON_NONE;
  832. }
  833. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  834. onion_pending_remove(TO_OR_CIRCUIT(circ));
  835. }
  836. /* If the circuit ever became OPEN, we sent it to the reputation history
  837. * module then. If it isn't OPEN, we send it there now to remember which
  838. * links worked and which didn't.
  839. */
  840. if (circ->state != CIRCUIT_STATE_OPEN) {
  841. if (CIRCUIT_IS_ORIGIN(circ)) {
  842. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  843. circuit_build_failed(ocirc); /* take actions if necessary */
  844. circuit_rep_hist_note_result(ocirc);
  845. }
  846. }
  847. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  848. if (circuits_pending_or_conns)
  849. smartlist_remove(circuits_pending_or_conns, circ);
  850. }
  851. if (CIRCUIT_IS_ORIGIN(circ)) {
  852. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  853. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  854. orig_reason);
  855. }
  856. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  857. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  858. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  859. tor_assert(ocirc->build_state->chosen_exit);
  860. /* treat this like getting a nack from it */
  861. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  862. "Removing from descriptor.",
  863. safe_str(ocirc->rend_query),
  864. safe_str(build_state_get_exit_nickname(ocirc->build_state)));
  865. rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
  866. ocirc->rend_query);
  867. }
  868. if (circ->n_conn)
  869. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  870. if (! CIRCUIT_IS_ORIGIN(circ)) {
  871. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  872. edge_connection_t *conn;
  873. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  874. connection_edge_destroy(or_circ->p_circ_id, conn);
  875. while (or_circ->resolving_streams) {
  876. conn = or_circ->resolving_streams;
  877. or_circ->resolving_streams = conn->next_stream;
  878. if (!conn->_base.marked_for_close) {
  879. /* The client will see a DESTROY, and infer that the connections
  880. * are closing because the circuit is getting torn down. No need
  881. * to send an end cell. */
  882. conn->_base.edge_has_sent_end = 1;
  883. conn->end_reason = END_STREAM_REASON_DESTROY;
  884. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  885. connection_mark_for_close(TO_CONN(conn));
  886. }
  887. conn->on_circuit = NULL;
  888. }
  889. if (or_circ->p_conn)
  890. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  891. } else {
  892. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  893. edge_connection_t *conn;
  894. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  895. connection_edge_destroy(circ->n_circ_id, conn);
  896. }
  897. circ->marked_for_close = line;
  898. circ->marked_for_close_file = file;
  899. if (! CIRCUIT_IS_ORIGIN(circ)) {
  900. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  901. if (or_circ->rend_splice) {
  902. if (!or_circ->rend_splice->_base.marked_for_close) {
  903. /* do this after marking this circuit, to avoid infinite recursion. */
  904. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  905. }
  906. or_circ->rend_splice = NULL;
  907. }
  908. }
  909. }
  910. /** Verify that cpath layer <b>cp</b> has all of its invariants
  911. * correct. Trigger an assert if anything is invalid.
  912. */
  913. void
  914. assert_cpath_layer_ok(const crypt_path_t *cp)
  915. {
  916. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  917. // tor_assert(cp->port);
  918. tor_assert(cp);
  919. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  920. switch (cp->state)
  921. {
  922. case CPATH_STATE_OPEN:
  923. tor_assert(cp->f_crypto);
  924. tor_assert(cp->b_crypto);
  925. /* fall through */
  926. case CPATH_STATE_CLOSED:
  927. tor_assert(!cp->dh_handshake_state);
  928. break;
  929. case CPATH_STATE_AWAITING_KEYS:
  930. /* tor_assert(cp->dh_handshake_state); */
  931. break;
  932. default:
  933. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  934. tor_assert(0);
  935. }
  936. tor_assert(cp->package_window >= 0);
  937. tor_assert(cp->deliver_window >= 0);
  938. }
  939. /** Verify that cpath <b>cp</b> has all of its invariants
  940. * correct. Trigger an assert if anything is invalid.
  941. */
  942. static void
  943. assert_cpath_ok(const crypt_path_t *cp)
  944. {
  945. const crypt_path_t *start = cp;
  946. do {
  947. assert_cpath_layer_ok(cp);
  948. /* layers must be in sequence of: "open* awaiting? closed*" */
  949. if (cp != start) {
  950. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  951. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  952. } else if (cp->state == CPATH_STATE_OPEN) {
  953. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  954. }
  955. }
  956. cp = cp->next;
  957. tor_assert(cp);
  958. } while (cp != start);
  959. }
  960. /** Verify that circuit <b>c</b> has all of its invariants
  961. * correct. Trigger an assert if anything is invalid.
  962. */
  963. void
  964. assert_circuit_ok(const circuit_t *c)
  965. {
  966. edge_connection_t *conn;
  967. const or_circuit_t *or_circ = NULL;
  968. const origin_circuit_t *origin_circ = NULL;
  969. tor_assert(c);
  970. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  971. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  972. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  973. if (CIRCUIT_IS_ORIGIN(c))
  974. origin_circ = TO_ORIGIN_CIRCUIT((circuit_t*)c);
  975. else
  976. or_circ = TO_OR_CIRCUIT((circuit_t*)c);
  977. if (c->n_conn) {
  978. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
  979. DIGEST_LEN));
  980. if (c->n_circ_id)
  981. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  982. }
  983. if (or_circ && or_circ->p_conn) {
  984. if (or_circ->p_circ_id)
  985. tor_assert(c == circuit_get_by_circid_orconn(or_circ->p_circ_id,
  986. or_circ->p_conn));
  987. }
  988. #if 0 /* false now that rendezvous exits are attached to p_streams */
  989. if (origin_circ)
  990. for (conn = origin_circ->p_streams; conn; conn = conn->next_stream)
  991. tor_assert(conn->_base.type == CONN_TYPE_AP);
  992. #endif
  993. if (or_circ)
  994. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  995. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  996. tor_assert(c->deliver_window >= 0);
  997. tor_assert(c->package_window >= 0);
  998. if (c->state == CIRCUIT_STATE_OPEN) {
  999. tor_assert(!c->onionskin);
  1000. if (or_circ) {
  1001. tor_assert(or_circ->n_crypto);
  1002. tor_assert(or_circ->p_crypto);
  1003. tor_assert(or_circ->n_digest);
  1004. tor_assert(or_circ->p_digest);
  1005. }
  1006. }
  1007. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1008. tor_assert(circuits_pending_or_conns &&
  1009. smartlist_isin(circuits_pending_or_conns, c));
  1010. } else {
  1011. tor_assert(!circuits_pending_or_conns ||
  1012. !smartlist_isin(circuits_pending_or_conns, c));
  1013. }
  1014. if (origin_circ && origin_circ->cpath) {
  1015. assert_cpath_ok(origin_circ->cpath);
  1016. }
  1017. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1018. tor_assert(or_circ);
  1019. if (!c->marked_for_close) {
  1020. tor_assert(or_circ->rend_splice);
  1021. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1022. }
  1023. tor_assert(or_circ->rend_splice != or_circ);
  1024. } else {
  1025. tor_assert(!or_circ || !or_circ->rend_splice);
  1026. }
  1027. }