circuitlist.c 21 KB

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