circuitlist.c 38 KB

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