circuitlist.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char circuitlist_c_id[] = "$Id$";
  7. /**
  8. * \file circuitlist.c
  9. * \brief Manage the global circuit list.
  10. **/
  11. #include "or.h"
  12. #include "tree.h"
  13. /********* START VARIABLES **********/
  14. /** A global list of all circuits at this hop. */
  15. circuit_t *global_circuitlist=NULL;
  16. static void circuit_free(circuit_t *circ);
  17. static void circuit_free_cpath(crypt_path_t *cpath);
  18. static void circuit_free_cpath_node(crypt_path_t *victim);
  19. /********* END VARIABLES ************/
  20. /** DOCDOC This whole section */
  21. struct orconn_circid_circuit_map_t {
  22. RB_ENTRY(orconn_circid_circuit_map_t) node;
  23. connection_t *or_conn;
  24. uint16_t circ_id;
  25. circuit_t *circuit;
  26. };
  27. /** DOCDOC */
  28. static INLINE int
  29. compare_orconn_circid_entries(struct orconn_circid_circuit_map_t *a,
  30. struct orconn_circid_circuit_map_t *b)
  31. {
  32. if (a->or_conn < b->or_conn)
  33. return -1;
  34. else if (a->or_conn > b->or_conn)
  35. return 1;
  36. else
  37. return ((int)b->circ_id) - ((int)a->circ_id);
  38. };
  39. static RB_HEAD(orconn_circid_tree, orconn_circid_circuit_map_t) orconn_circid_circuit_map = RB_INITIALIZER(orconn_circid_circuit_map);
  40. RB_PROTOTYPE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
  41. RB_GENERATE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
  42. struct orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  43. /** DOCDOC */
  44. void
  45. circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
  46. connection_t *conn,
  47. enum which_conn_changed_t which)
  48. {
  49. uint16_t old_id;
  50. connection_t *old_conn;
  51. struct orconn_circid_circuit_map_t search;
  52. struct orconn_circid_circuit_map_t *found;
  53. tor_assert(!conn || conn->type == CONN_TYPE_OR);
  54. if (which == P_CONN_CHANGED) {
  55. old_id = circ->p_circ_id;
  56. old_conn = circ->p_conn;
  57. circ->p_circ_id = id;
  58. circ->p_conn = conn;
  59. } else {
  60. old_id = circ->n_circ_id;
  61. old_conn = circ->n_conn;
  62. circ->n_circ_id = id;
  63. circ->n_conn = conn;
  64. }
  65. if (_last_circid_orconn_ent &&
  66. ((old_id == _last_circid_orconn_ent->circ_id &&
  67. old_conn == _last_circid_orconn_ent->or_conn) ||
  68. (id == _last_circid_orconn_ent->circ_id &&
  69. conn == _last_circid_orconn_ent->or_conn))) {
  70. _last_circid_orconn_ent = NULL;
  71. }
  72. if (old_conn) {
  73. search.circ_id = old_id;
  74. search.or_conn = old_conn;
  75. found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
  76. if (found) {
  77. RB_REMOVE(orconn_circid_tree, &orconn_circid_circuit_map, found);
  78. }
  79. tor_free(found);
  80. }
  81. if (conn == NULL)
  82. return;
  83. search.circ_id = id;
  84. search.or_conn = conn;
  85. found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
  86. if (found) {
  87. found->circuit = circ;
  88. } else {
  89. found = tor_malloc_zero(sizeof(struct orconn_circid_circuit_map_t));
  90. found->circ_id = id;
  91. found->or_conn = conn;
  92. found->circuit = circ;
  93. RB_INSERT(orconn_circid_tree, &orconn_circid_circuit_map, found);
  94. }
  95. }
  96. /** Add <b>circ</b> to the global list of circuits. This is called only from
  97. * within circuit_new.
  98. */
  99. static void
  100. circuit_add(circuit_t *circ)
  101. {
  102. if (!global_circuitlist) { /* first one */
  103. global_circuitlist = circ;
  104. circ->next = NULL;
  105. } else {
  106. circ->next = global_circuitlist;
  107. global_circuitlist = circ;
  108. }
  109. }
  110. /** Detach from the global circuit list, and deallocate, all
  111. * circuits that have been marked for close.
  112. */
  113. void
  114. circuit_close_all_marked(void)
  115. {
  116. circuit_t *tmp,*m;
  117. while (global_circuitlist && global_circuitlist->marked_for_close) {
  118. tmp = global_circuitlist->next;
  119. circuit_free(global_circuitlist);
  120. global_circuitlist = tmp;
  121. }
  122. tmp = global_circuitlist;
  123. while (tmp && tmp->next) {
  124. if (tmp->next->marked_for_close) {
  125. m = tmp->next->next;
  126. circuit_free(tmp->next);
  127. tmp->next = m;
  128. /* Need to check new tmp->next; don't advance tmp. */
  129. } else {
  130. /* Advance tmp. */
  131. tmp = tmp->next;
  132. }
  133. }
  134. }
  135. /** DOCDOC **/
  136. circuit_t *
  137. _circuit_get_global_list(void)
  138. {
  139. return global_circuitlist;
  140. }
  141. /** Function to make circ-\>state human-readable */
  142. const char *
  143. circuit_state_to_string(int state) {
  144. static char buf[64];
  145. switch (state) {
  146. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  147. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  148. case CIRCUIT_STATE_OR_WAIT: return "connecting to firsthop";
  149. case CIRCUIT_STATE_OPEN: return "open";
  150. default:
  151. log_fn(LOG_WARN, "Bug: unknown circuit state %d", state);
  152. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  153. return buf;
  154. }
  155. }
  156. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  157. * and <b>p_conn</b>. Add it to the global circuit list.
  158. */
  159. circuit_t *
  160. circuit_new(uint16_t p_circ_id, connection_t *p_conn)
  161. {
  162. circuit_t *circ;
  163. static uint32_t n_circuits_allocated = 1;
  164. /* never zero, since a global ID of 0 is treated specially by the controller */
  165. circ = tor_malloc_zero(sizeof(circuit_t));
  166. circ->magic = CIRCUIT_MAGIC;
  167. circ->timestamp_created = time(NULL);
  168. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  169. /* CircIDs */
  170. if (p_conn) {
  171. circuit_set_circid_orconn(circ, p_circ_id, p_conn, P_CONN_CHANGED);
  172. }
  173. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  174. circ->package_window = CIRCWINDOW_START;
  175. circ->deliver_window = CIRCWINDOW_START;
  176. circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
  177. circ->global_identifier = n_circuits_allocated++;
  178. circuit_add(circ);
  179. return circ;
  180. }
  181. /** Deallocate space associated with circ.
  182. */
  183. static void
  184. circuit_free(circuit_t *circ)
  185. {
  186. tor_assert(circ);
  187. tor_assert(circ->magic == CIRCUIT_MAGIC);
  188. if (circ->n_crypto)
  189. crypto_free_cipher_env(circ->n_crypto);
  190. if (circ->p_crypto)
  191. crypto_free_cipher_env(circ->p_crypto);
  192. if (circ->n_digest)
  193. crypto_free_digest_env(circ->n_digest);
  194. if (circ->p_digest)
  195. crypto_free_digest_env(circ->p_digest);
  196. if (circ->build_state) {
  197. tor_free(circ->build_state->chosen_exit_name);
  198. if (circ->build_state->pending_final_cpath)
  199. circuit_free_cpath_node(circ->build_state->pending_final_cpath);
  200. }
  201. tor_free(circ->build_state);
  202. circuit_free_cpath(circ->cpath);
  203. if (circ->rend_splice) {
  204. circ->rend_splice->rend_splice = NULL;
  205. }
  206. /* Remove from map. */
  207. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  208. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  209. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  210. tor_free(circ);
  211. }
  212. /** Deallocate space associated with the linked list <b>cpath</b>. */
  213. static void
  214. circuit_free_cpath(crypt_path_t *cpath)
  215. {
  216. crypt_path_t *victim, *head=cpath;
  217. if (!cpath)
  218. return;
  219. /* it's a doubly linked list, so we have to notice when we've
  220. * gone through it once. */
  221. while (cpath->next && cpath->next != head) {
  222. victim = cpath;
  223. cpath = victim->next;
  224. circuit_free_cpath_node(victim);
  225. }
  226. circuit_free_cpath_node(cpath);
  227. }
  228. /** Release all storage held by circuits. */
  229. void
  230. circuit_free_all(void)
  231. {
  232. circuit_t *next;
  233. while (global_circuitlist) {
  234. next = global_circuitlist->next;
  235. while (global_circuitlist->resolving_streams) {
  236. connection_t *next;
  237. next = global_circuitlist->resolving_streams->next_stream;
  238. connection_free(global_circuitlist->resolving_streams);
  239. global_circuitlist->resolving_streams = next;
  240. }
  241. circuit_free(global_circuitlist);
  242. global_circuitlist = next;
  243. }
  244. }
  245. /** Deallocate space associated with the cpath node <b>victim</b>. */
  246. static void
  247. circuit_free_cpath_node(crypt_path_t *victim)
  248. {
  249. if (victim->f_crypto)
  250. crypto_free_cipher_env(victim->f_crypto);
  251. if (victim->b_crypto)
  252. crypto_free_cipher_env(victim->b_crypto);
  253. if (victim->f_digest)
  254. crypto_free_digest_env(victim->f_digest);
  255. if (victim->b_digest)
  256. crypto_free_digest_env(victim->b_digest);
  257. if (victim->dh_handshake_state)
  258. crypto_dh_free(victim->dh_handshake_state);
  259. victim->magic = 0xDEADBEEFu;
  260. tor_free(victim);
  261. }
  262. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  263. * such circuit exists. */
  264. circuit_t *
  265. circuit_get_by_global_id(uint32_t id)
  266. {
  267. circuit_t *circ;
  268. for (circ=global_circuitlist;circ;circ = circ->next) {
  269. if (circ->global_identifier == id) {
  270. if (circ->marked_for_close)
  271. return NULL;
  272. else
  273. return circ;
  274. }
  275. }
  276. return NULL;
  277. }
  278. /** Return a circ such that:
  279. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  280. * - circ is attached to <b>conn</b>, either as p_conn, n-conn, or
  281. * in p_streams or n_streams.
  282. * Return NULL if no such circuit exists.
  283. */
  284. circuit_t *
  285. circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn)
  286. {
  287. struct orconn_circid_circuit_map_t search;
  288. struct orconn_circid_circuit_map_t *found;
  289. tor_assert(conn->type == CONN_TYPE_OR);
  290. if (_last_circid_orconn_ent &&
  291. circ_id == _last_circid_orconn_ent->circ_id &&
  292. conn == _last_circid_orconn_ent->or_conn) {
  293. found = _last_circid_orconn_ent;
  294. } else {
  295. search.circ_id = circ_id;
  296. search.or_conn = conn;
  297. found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
  298. _last_circid_orconn_ent = found;
  299. }
  300. if (found && found->circuit && !found->circuit->marked_for_close)
  301. return found->circuit;
  302. /* The rest of this can be replaced with
  303. "return NULL;" once we believe the code works. */
  304. {
  305. circuit_t *circ;
  306. for (circ=global_circuitlist;circ;circ = circ->next) {
  307. if (circ->marked_for_close)
  308. continue;
  309. if (circ->p_conn == conn && circ->p_circ_id == circ_id) {
  310. log_fn(LOG_WARN, "circuit matches p_conn, but not in tree (Bug!)");
  311. return circ;
  312. }
  313. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  314. log_fn(LOG_WARN, "circuit matches n_conn, but not in tree (Bug!)");
  315. return circ;
  316. }
  317. }
  318. return NULL;
  319. }
  320. }
  321. /** DOCDOC */
  322. circuit_t *
  323. circuit_get_by_edge_conn(connection_t *conn)
  324. {
  325. circuit_t *circ;
  326. connection_t *tmpconn;
  327. tor_assert(CONN_IS_EDGE(conn));
  328. if (! conn->on_circuit) {
  329. /* return NULL; */
  330. circ = circuit_get_by_conn(conn);
  331. if (circ) {
  332. log_fn(LOG_WARN, "BUG: conn->on_circuit==NULL, but there was in fact a circuit there.");
  333. }
  334. return circ;
  335. }
  336. circ = conn->on_circuit;
  337. /* All this stuff here is sanity-checking. */
  338. tor_assert(circ->magic == CIRCUIT_MAGIC);
  339. for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  340. if (tmpconn == conn)
  341. return circ;
  342. for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  343. if (tmpconn == conn)
  344. return circ;
  345. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
  346. if (tmpconn == conn)
  347. return circ;
  348. tor_assert(0);
  349. }
  350. /** Return a circ such that circ is attached to <b>conn</b>, either as
  351. * p_conn, n-conn, or in p_streams or n_streams or resolving_streams.
  352. *
  353. * Return NULL if no such circuit exists.
  354. */
  355. circuit_t *
  356. circuit_get_by_conn(connection_t *conn)
  357. {
  358. circuit_t *circ;
  359. connection_t *tmpconn;
  360. for (circ=global_circuitlist;circ;circ = circ->next) {
  361. if (circ->marked_for_close)
  362. continue;
  363. if (circ->p_conn == conn)
  364. return circ;
  365. if (circ->n_conn == conn)
  366. return circ;
  367. for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  368. if (tmpconn == conn)
  369. return circ;
  370. for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  371. if (tmpconn == conn)
  372. return circ;
  373. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
  374. if (tmpconn == conn)
  375. return circ;
  376. }
  377. return NULL;
  378. }
  379. /** Return a circ such that:
  380. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  381. * - circ-\>purpose is equal to <b>purpose</b>.
  382. *
  383. * Return NULL if no such circuit exists.
  384. */
  385. circuit_t *
  386. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  387. {
  388. circuit_t *circ;
  389. for (circ = global_circuitlist; circ; circ = circ->next) {
  390. if (!circ->marked_for_close &&
  391. circ->purpose == purpose &&
  392. !rend_cmp_service_ids(rend_query, circ->rend_query))
  393. return circ;
  394. }
  395. return NULL;
  396. }
  397. /** Return the first circuit in global_circuitlist after <b>start</b> whose
  398. * rend_pk_digest field is <b>digest</b> and whose purpose is <b>purpose</b>. Returns
  399. * NULL if no circuit is found. If <b>start</b> is NULL, begin at the start of
  400. * the list.
  401. */
  402. circuit_t *
  403. circuit_get_next_by_pk_and_purpose(circuit_t *start,
  404. const char *digest, uint8_t purpose)
  405. {
  406. circuit_t *circ;
  407. if (start == NULL)
  408. circ = global_circuitlist;
  409. else
  410. circ = start->next;
  411. for ( ; circ; circ = circ->next) {
  412. if (circ->marked_for_close)
  413. continue;
  414. if (circ->purpose != purpose)
  415. continue;
  416. if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
  417. return circ;
  418. }
  419. return NULL;
  420. }
  421. /** Return the circuit waiting for a rendezvous with the provided cookie.
  422. * Return NULL if no such circuit is found.
  423. */
  424. circuit_t *
  425. circuit_get_rendezvous(const char *cookie)
  426. {
  427. circuit_t *circ;
  428. for (circ = global_circuitlist; circ; circ = circ->next) {
  429. if (! circ->marked_for_close &&
  430. circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
  431. ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
  432. return circ;
  433. }
  434. return NULL;
  435. }
  436. /** Return a circuit that is open, has specified <b>purpose</b>,
  437. * has a timestamp_dirty value of 0, and is uptime/capacity/internal
  438. * if required; or NULL if no circuit fits this description.
  439. *
  440. * Avoid returning need_uptime circuits if not necessary.
  441. * FFFF As a more important goal, not yet implemented, avoid returning
  442. * internal circuits if not necessary.
  443. */
  444. circuit_t *
  445. circuit_get_clean_open(uint8_t purpose, int need_uptime,
  446. int need_capacity, int internal)
  447. {
  448. circuit_t *circ;
  449. circuit_t *best=NULL;
  450. log_fn(LOG_DEBUG,"Hunting for a circ to cannibalize: purpose %d, uptime %d, capacity %d, internal %d", purpose, need_uptime, need_capacity, internal);
  451. for (circ=global_circuitlist; circ; circ = circ->next) {
  452. if (CIRCUIT_IS_ORIGIN(circ) &&
  453. circ->state == CIRCUIT_STATE_OPEN &&
  454. !circ->marked_for_close &&
  455. circ->purpose == purpose &&
  456. !circ->timestamp_dirty &&
  457. (!need_uptime || circ->build_state->need_uptime) &&
  458. (!need_capacity || circ->build_state->need_capacity) &&
  459. (!internal || circ->build_state->is_internal)) {
  460. if (!best || (best->build_state->need_uptime && !need_uptime))
  461. best = circ;
  462. }
  463. }
  464. return best;
  465. }
  466. /** Go through the circuitlist; mark-for-close each circuit that starts
  467. * at us but has not yet been used. */
  468. void
  469. circuit_mark_all_unused_circs(void)
  470. {
  471. circuit_t *circ;
  472. for (circ=global_circuitlist; circ; circ = circ->next) {
  473. if (CIRCUIT_IS_ORIGIN(circ) &&
  474. !circ->marked_for_close &&
  475. !circ->timestamp_dirty)
  476. circuit_mark_for_close(circ);
  477. }
  478. }
  479. /** Mark <b>circ</b> to be closed next time we call
  480. * circuit_close_all_marked(). Do any cleanup needed:
  481. * - If state is onionskin_pending, remove circ from the onion_pending
  482. * list.
  483. * - If circ isn't open yet: call circuit_build_failed() if we're
  484. * the origin, and in either case call circuit_rep_hist_note_result()
  485. * to note stats.
  486. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  487. * just tried from our list of intro points for that service
  488. * descriptor.
  489. * - Send appropriate destroys and edge_destroys for conns and
  490. * streams attached to circ.
  491. * - If circ->rend_splice is set (we are the midpoint of a joined
  492. * rendezvous stream), then mark the other circuit to close as well.
  493. */
  494. void
  495. _circuit_mark_for_close(circuit_t *circ, int line, const char *file)
  496. {
  497. connection_t *conn;
  498. assert_circuit_ok(circ);
  499. tor_assert(line);
  500. tor_assert(file);
  501. if (circ->marked_for_close) {
  502. log(LOG_WARN,"Duplicate call to circuit_mark_for_close at %s:%d"
  503. " (first at %s:%d)", file, line,
  504. circ->marked_for_close_file, circ->marked_for_close);
  505. return;
  506. }
  507. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  508. onion_pending_remove(circ);
  509. }
  510. /* If the circuit ever became OPEN, we sent it to the reputation history
  511. * module then. If it isn't OPEN, we send it there now to remember which
  512. * links worked and which didn't.
  513. */
  514. if (circ->state != CIRCUIT_STATE_OPEN) {
  515. if (CIRCUIT_IS_ORIGIN(circ)) {
  516. circuit_build_failed(circ); /* take actions if necessary */
  517. }
  518. circuit_rep_hist_note_result(circ);
  519. }
  520. if (CIRCUIT_IS_ORIGIN(circ)) {
  521. control_event_circuit_status(circ,
  522. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
  523. }
  524. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  525. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  526. /* treat this like getting a nack from it */
  527. log_fn(LOG_INFO,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
  528. safe_str(circ->rend_query), safe_str(circ->build_state->chosen_exit_name));
  529. rend_client_remove_intro_point(circ->build_state->chosen_exit_name, circ->rend_query);
  530. }
  531. if (circ->n_conn)
  532. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  533. for (conn=circ->n_streams; conn; conn=conn->next_stream)
  534. connection_edge_destroy(circ->n_circ_id, conn);
  535. while (circ->resolving_streams) {
  536. conn = circ->resolving_streams;
  537. circ->resolving_streams = conn->next_stream;
  538. if (!conn->marked_for_close) {
  539. /* The other side will see a DESTROY, and infer that the connections
  540. * are closing because the circuit is getting torn down. No need
  541. * to send an end cell*/
  542. conn->has_sent_end = 1; /* we're closing the circuit, nothing to send to */
  543. connection_mark_for_close(conn);
  544. }
  545. conn->on_circuit = NULL;
  546. }
  547. if (circ->p_conn)
  548. connection_send_destroy(circ->p_circ_id, circ->p_conn);
  549. for (conn=circ->p_streams; conn; conn=conn->next_stream)
  550. connection_edge_destroy(circ->p_circ_id, conn);
  551. circ->marked_for_close = line;
  552. circ->marked_for_close_file = file;
  553. if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
  554. /* do this after marking this circuit, to avoid infinite recursion. */
  555. circuit_mark_for_close(circ->rend_splice);
  556. circ->rend_splice = NULL;
  557. }
  558. }
  559. /** Verify that cpath layer <b>cp</b> has all of its invariants
  560. * correct. Trigger an assert if anything is invalid.
  561. */
  562. void
  563. assert_cpath_layer_ok(const crypt_path_t *cp)
  564. {
  565. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  566. // tor_assert(cp->port);
  567. tor_assert(cp);
  568. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  569. switch (cp->state)
  570. {
  571. case CPATH_STATE_OPEN:
  572. tor_assert(cp->f_crypto);
  573. tor_assert(cp->b_crypto);
  574. /* fall through */
  575. case CPATH_STATE_CLOSED:
  576. tor_assert(!cp->dh_handshake_state);
  577. break;
  578. case CPATH_STATE_AWAITING_KEYS:
  579. /* tor_assert(cp->dh_handshake_state); */
  580. break;
  581. default:
  582. log_fn(LOG_ERR,"Unexpected state %d",cp->state);
  583. tor_assert(0);
  584. }
  585. tor_assert(cp->package_window >= 0);
  586. tor_assert(cp->deliver_window >= 0);
  587. }
  588. /** Verify that cpath <b>cp</b> has all of its invariants
  589. * correct. Trigger an assert if anything is invalid.
  590. */
  591. static void
  592. assert_cpath_ok(const crypt_path_t *cp)
  593. {
  594. const crypt_path_t *start = cp;
  595. do {
  596. assert_cpath_layer_ok(cp);
  597. /* layers must be in sequence of: "open* awaiting? closed*" */
  598. if (cp != start) {
  599. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  600. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  601. } else if (cp->state == CPATH_STATE_OPEN) {
  602. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  603. }
  604. }
  605. cp = cp->next;
  606. tor_assert(cp);
  607. } while (cp != start);
  608. }
  609. /** Verify that circuit <b>c</b> has all of its invariants
  610. * correct. Trigger an assert if anything is invalid.
  611. */
  612. void
  613. assert_circuit_ok(const circuit_t *c)
  614. {
  615. connection_t *conn;
  616. tor_assert(c);
  617. tor_assert(c->magic == CIRCUIT_MAGIC);
  618. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  619. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  620. if (c->n_conn) {
  621. tor_assert(c->n_conn->type == CONN_TYPE_OR);
  622. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest, DIGEST_LEN));
  623. if (c->n_circ_id)
  624. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  625. }
  626. if (c->p_conn) {
  627. tor_assert(c->p_conn->type == CONN_TYPE_OR);
  628. if (c->p_circ_id)
  629. tor_assert(c == circuit_get_by_circid_orconn(c->p_circ_id, c->p_conn));
  630. }
  631. for (conn = c->p_streams; conn; conn = conn->next_stream)
  632. tor_assert(conn->type == CONN_TYPE_AP);
  633. for (conn = c->n_streams; conn; conn = conn->next_stream)
  634. tor_assert(conn->type == CONN_TYPE_EXIT);
  635. tor_assert(c->deliver_window >= 0);
  636. tor_assert(c->package_window >= 0);
  637. if (c->state == CIRCUIT_STATE_OPEN) {
  638. if (c->cpath) {
  639. tor_assert(CIRCUIT_IS_ORIGIN(c));
  640. tor_assert(!c->n_crypto);
  641. tor_assert(!c->p_crypto);
  642. tor_assert(!c->n_digest);
  643. tor_assert(!c->p_digest);
  644. } else {
  645. tor_assert(!CIRCUIT_IS_ORIGIN(c));
  646. tor_assert(c->n_crypto);
  647. tor_assert(c->p_crypto);
  648. tor_assert(c->n_digest);
  649. tor_assert(c->p_digest);
  650. }
  651. }
  652. if (c->cpath) {
  653. assert_cpath_ok(c->cpath);
  654. }
  655. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  656. if (!c->marked_for_close) {
  657. tor_assert(c->rend_splice);
  658. tor_assert(c->rend_splice->rend_splice == c);
  659. }
  660. tor_assert(c->rend_splice != c);
  661. } else {
  662. tor_assert(!c->rend_splice);
  663. }
  664. }