circuit.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. static void circuit_free_cpath_node(crypt_path_t *victim);
  7. static circ_id_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type);
  8. unsigned long stats_n_relay_cells_relayed = 0;
  9. unsigned long stats_n_relay_cells_delivered = 0;
  10. /********* START VARIABLES **********/
  11. static circuit_t *global_circuitlist=NULL;
  12. char *circuit_state_to_string[] = {
  13. "doing handshakes", /* 0 */
  14. "processing the onion", /* 1 */
  15. "connecting to firsthop", /* 2 */
  16. "open" /* 3 */
  17. };
  18. /********* END VARIABLES ************/
  19. void circuit_add(circuit_t *circ) {
  20. if(!global_circuitlist) { /* first one */
  21. global_circuitlist = circ;
  22. circ->next = NULL;
  23. } else {
  24. circ->next = global_circuitlist;
  25. global_circuitlist = circ;
  26. }
  27. }
  28. void circuit_remove(circuit_t *circ) {
  29. circuit_t *tmpcirc;
  30. assert(circ && global_circuitlist);
  31. if(global_circuitlist == circ) {
  32. global_circuitlist = global_circuitlist->next;
  33. return;
  34. }
  35. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  36. if(tmpcirc->next == circ) {
  37. tmpcirc->next = circ->next;
  38. return;
  39. }
  40. }
  41. }
  42. circuit_t *circuit_new(circ_id_t p_circ_id, connection_t *p_conn) {
  43. circuit_t *circ;
  44. circ = tor_malloc_zero(sizeof(circuit_t));
  45. circ->timestamp_created = time(NULL);
  46. circ->p_circ_id = p_circ_id;
  47. circ->p_conn = p_conn;
  48. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  49. /* CircIDs */
  50. circ->p_circ_id = p_circ_id;
  51. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  52. circ->package_window = CIRCWINDOW_START;
  53. circ->deliver_window = CIRCWINDOW_START;
  54. circuit_add(circ);
  55. return circ;
  56. }
  57. void circuit_free(circuit_t *circ) {
  58. assert(circ);
  59. if (circ->n_crypto)
  60. crypto_free_cipher_env(circ->n_crypto);
  61. if (circ->p_crypto)
  62. crypto_free_cipher_env(circ->p_crypto);
  63. if (circ->n_digest)
  64. crypto_free_digest_env(circ->n_digest);
  65. if (circ->p_digest)
  66. crypto_free_digest_env(circ->p_digest);
  67. if(circ->build_state)
  68. tor_free(circ->build_state->chosen_exit);
  69. tor_free(circ->build_state);
  70. circuit_free_cpath(circ->cpath);
  71. free(circ);
  72. }
  73. void circuit_free_cpath(crypt_path_t *cpath) {
  74. crypt_path_t *victim, *head=cpath;
  75. if(!cpath)
  76. return;
  77. /* it's a doubly linked list, so we have to notice when we've
  78. * gone through it once. */
  79. while(cpath->next && cpath->next != head) {
  80. victim = cpath;
  81. cpath = victim->next;
  82. circuit_free_cpath_node(victim);
  83. }
  84. circuit_free_cpath_node(cpath);
  85. }
  86. static void circuit_free_cpath_node(crypt_path_t *victim) {
  87. if(victim->f_crypto)
  88. crypto_free_cipher_env(victim->f_crypto);
  89. if(victim->b_crypto)
  90. crypto_free_cipher_env(victim->b_crypto);
  91. if(victim->f_digest)
  92. crypto_free_digest_env(victim->f_digest);
  93. if(victim->b_digest)
  94. crypto_free_digest_env(victim->b_digest);
  95. if(victim->handshake_state)
  96. crypto_dh_free(victim->handshake_state);
  97. free(victim);
  98. }
  99. /* return 0 if can't get a unique circ_id. */
  100. static circ_id_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
  101. circ_id_t test_circ_id;
  102. int attempts=0;
  103. uint16_t high_bit;
  104. assert(conn && conn->type == CONN_TYPE_OR);
  105. high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
  106. do {
  107. /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
  108. * circID such that (high_bit|test_circ_id) is not already used. */
  109. test_circ_id = conn->next_circ_id++;
  110. if (test_circ_id == 0 || test_circ_id >= 1<<15) {
  111. test_circ_id = 1;
  112. conn->next_circ_id = 2;
  113. }
  114. if(++attempts > 1<<15) {
  115. /* Make sure we don't loop forever if all circ_id's are used. This
  116. * matters because it's an external DoS vulnerability.
  117. */
  118. log_fn(LOG_WARN,"No unused circ IDs. Failing.");
  119. return 0;
  120. }
  121. test_circ_id |= high_bit;
  122. } while(circuit_get_by_circ_id_conn(test_circ_id, conn));
  123. return test_circ_id;
  124. }
  125. circuit_t *circuit_get_by_circ_id_conn(circ_id_t circ_id, connection_t *conn) {
  126. circuit_t *circ;
  127. connection_t *tmpconn;
  128. for(circ=global_circuitlist;circ;circ = circ->next) {
  129. if(circ->p_circ_id == circ_id) {
  130. if(circ->p_conn == conn)
  131. return circ;
  132. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  133. if(tmpconn == conn)
  134. return circ;
  135. }
  136. }
  137. if(circ->n_circ_id == circ_id) {
  138. if(circ->n_conn == conn)
  139. return circ;
  140. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  141. if(tmpconn == conn)
  142. return circ;
  143. }
  144. }
  145. }
  146. return NULL;
  147. }
  148. circuit_t *circuit_get_by_conn(connection_t *conn) {
  149. circuit_t *circ;
  150. connection_t *tmpconn;
  151. for(circ=global_circuitlist;circ;circ = circ->next) {
  152. if(circ->p_conn == conn)
  153. return circ;
  154. if(circ->n_conn == conn)
  155. return circ;
  156. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  157. if(tmpconn == conn)
  158. return circ;
  159. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  160. if(tmpconn == conn)
  161. return circ;
  162. }
  163. return NULL;
  164. }
  165. /* Find the newest circ that conn can use, preferably one which is
  166. * dirty. Circ must not be too old.
  167. * If !conn, return newest.
  168. *
  169. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  170. */
  171. circuit_t *circuit_get_newest(connection_t *conn, int must_be_open) {
  172. circuit_t *circ, *newest=NULL, *leastdirty=NULL;
  173. routerinfo_t *exitrouter;
  174. for(circ=global_circuitlist;circ;circ = circ->next) {
  175. if(!circ->cpath)
  176. continue; /* this circ doesn't start at us */
  177. if(must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
  178. continue; /* ignore non-open circs */
  179. if(conn) {
  180. if(circ->state == CIRCUIT_STATE_OPEN && circ->n_conn) /* open */
  181. exitrouter = router_get_by_addr_port(circ->cpath->prev->addr, circ->cpath->prev->port);
  182. else /* not open */
  183. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  184. if(!exitrouter || connection_ap_can_use_exit(conn, exitrouter) < 0) {
  185. /* can't exit from this router */
  186. continue;
  187. }
  188. }
  189. if(!newest || newest->timestamp_created < circ->timestamp_created) {
  190. newest = circ;
  191. }
  192. if(conn && circ->timestamp_dirty &&
  193. (!leastdirty || leastdirty->timestamp_dirty < circ->timestamp_dirty)) {
  194. leastdirty = circ;
  195. }
  196. }
  197. if(leastdirty &&
  198. leastdirty->timestamp_dirty+options.NewCircuitPeriod > time(NULL)) {
  199. /* log_fn(LOG_DEBUG,"Choosing in-use circuit %s:%d:%d.",
  200. leastdirty->n_conn->address, leastdirty->n_port, leastdirty->n_circ_id); */
  201. return leastdirty;
  202. }
  203. if(newest) {
  204. /* log_fn(LOG_DEBUG,"Choosing circuit %s:%d:%d.",
  205. newest->n_conn->address, newest->n_port, newest->n_circ_id); */
  206. return newest;
  207. }
  208. return NULL;
  209. }
  210. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 3
  211. /* circuits that were born at the end of their second might be expired
  212. * after 3.1 seconds; circuits born at the beginning might be expired
  213. * after closer to 4 seconds.
  214. */
  215. /* close all circuits that start at us, aren't open, and were born
  216. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago */
  217. void circuit_expire_building(void) {
  218. int now = time(NULL);
  219. circuit_t *victim, *circ = global_circuitlist;
  220. while(circ) {
  221. victim = circ;
  222. circ = circ->next;
  223. if(victim->cpath &&
  224. victim->state != CIRCUIT_STATE_OPEN &&
  225. victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC+1 < now) {
  226. if(victim->n_conn)
  227. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s)",
  228. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  229. victim->state, circuit_state_to_string[victim->state]);
  230. else
  231. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s)", victim->n_circ_id,
  232. victim->state, circuit_state_to_string[victim->state]);
  233. circuit_close(victim);
  234. }
  235. }
  236. }
  237. /* count the number of circs starting at us that aren't open */
  238. int circuit_count_building(void) {
  239. circuit_t *circ;
  240. int num=0;
  241. for(circ=global_circuitlist;circ;circ = circ->next) {
  242. if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN)
  243. num++;
  244. }
  245. return num;
  246. }
  247. #define MIN_CIRCUITS_HANDLING_STREAM 2
  248. /* return 1 if at least MIN_CIRCUITS_HANDLING_STREAM non-open circuits
  249. * will have an acceptable exit node for conn. Else return 0.
  250. */
  251. int circuit_stream_is_being_handled(connection_t *conn) {
  252. circuit_t *circ;
  253. routerinfo_t *exitrouter;
  254. int num=0;
  255. for(circ=global_circuitlist;circ;circ = circ->next) {
  256. if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN) {
  257. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  258. if(exitrouter && connection_ap_can_use_exit(conn, exitrouter) >= 0)
  259. if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
  260. return 1;
  261. }
  262. }
  263. return 0;
  264. }
  265. /* update digest from the payload of cell. assign integrity part to cell. */
  266. void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell) {
  267. uint32_t integrity;
  268. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  269. crypto_digest_get_digest(digest, (char *)&integrity, 4);
  270. log_fn(LOG_DEBUG,"Putting digest of %u into relay cell.",integrity);
  271. SET_CELL_RELAY_INTEGRITY(*cell, integrity);
  272. }
  273. /* update digest from the payload of cell (with the integrity part set
  274. * to 0). If the integrity part is valid return 0, else return -1.
  275. */
  276. int relay_check_digest(crypto_digest_env_t *digest, cell_t *cell) {
  277. uint32_t received_integrity, calculated_integrity;
  278. received_integrity = CELL_RELAY_INTEGRITY(*cell);
  279. log_fn(LOG_DEBUG,"Reading digest of %u from relay cell.",received_integrity);
  280. SET_CELL_RELAY_INTEGRITY(*cell, 0);
  281. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  282. crypto_digest_get_digest(digest, (char *)&calculated_integrity, 4);
  283. if(received_integrity != calculated_integrity) {
  284. log_fn(LOG_WARN,"Integrity check on cell payload failed. Bug or attack. (%d vs %d).",
  285. received_integrity, calculated_integrity);
  286. return -1;
  287. }
  288. return 0;
  289. }
  290. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  291. int cell_direction, crypt_path_t *layer_hint) {
  292. connection_t *conn=NULL;
  293. char recognized=0;
  294. assert(cell && circ);
  295. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  296. log_fn(LOG_DEBUG,"direction %d, streamid %d before crypt.", cell_direction, *(int*)(cell->payload+1));
  297. if(relay_crypt(circ, cell->payload, CELL_PAYLOAD_SIZE, cell_direction,
  298. &layer_hint, &recognized, &conn) < 0) {
  299. log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
  300. return -1;
  301. }
  302. if(recognized) {
  303. if(cell_direction == CELL_DIRECTION_OUT) {
  304. if(relay_check_digest(circ->n_digest, cell) < 0) {
  305. log_fn(LOG_WARN,"outgoing cell failed integrity check. Closing circ.");
  306. return -1;
  307. }
  308. ++stats_n_relay_cells_delivered;
  309. log_fn(LOG_DEBUG,"Sending to exit.");
  310. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL) < 0) {
  311. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at exit) failed.");
  312. return -1;
  313. }
  314. }
  315. if(cell_direction == CELL_DIRECTION_IN) {
  316. if(relay_check_digest(layer_hint->b_digest, cell) < 0) {
  317. log_fn(LOG_WARN,"incoming cell failed integrity check. Closing circ.");
  318. return -1;
  319. }
  320. ++stats_n_relay_cells_delivered;
  321. log_fn(LOG_DEBUG,"Sending to AP.");
  322. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint) < 0) {
  323. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at AP) failed.");
  324. return -1;
  325. }
  326. }
  327. return 0;
  328. }
  329. /* not recognized. pass it on. */
  330. if(cell_direction == CELL_DIRECTION_OUT)
  331. conn = circ->n_conn;
  332. else
  333. conn = circ->p_conn;
  334. if(!conn) {
  335. log_fn(LOG_INFO,"Didn't recognize cell (%d), but circ stops here! Dropping.",
  336. *(int *)(cell->payload+1));
  337. return 0;
  338. }
  339. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  340. ++stats_n_relay_cells_relayed;
  341. connection_or_write_cell_to_buf(cell, conn);
  342. return 0;
  343. }
  344. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  345. crypt_path_t **layer_hint, char *recognized, connection_t **conn) {
  346. crypt_path_t *thishop;
  347. char out[CELL_NETWORK_SIZE];
  348. assert(circ && in && recognized && conn);
  349. assert(inlen < CELL_NETWORK_SIZE);
  350. if(cell_direction == CELL_DIRECTION_IN) {
  351. if(circ->cpath) { /* we're at the beginning of the circuit.
  352. We'll want to do layered crypts. */
  353. thishop = circ->cpath;
  354. if(thishop->state != CPATH_STATE_OPEN) {
  355. log_fn(LOG_WARN,"Relay cell before first created cell?");
  356. return -1;
  357. }
  358. do { /* Remember: cpath is in forward order, that is, first hop first. */
  359. assert(thishop);
  360. log_fn(LOG_DEBUG,"before decrypt: %d",*(int*)(in+1));
  361. /* decrypt */
  362. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  363. log_fn(LOG_WARN,"Error performing onion decryption: %s", crypto_perror());
  364. return -1;
  365. }
  366. memcpy(in,out,inlen);
  367. log_fn(LOG_DEBUG,"after decrypt: %d",*(int*)(in+1));
  368. if( (*recognized = relay_check_recognized(circ, cell_direction, in+1, conn))) {
  369. *layer_hint = thishop;
  370. return 0;
  371. }
  372. thishop = thishop->next;
  373. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  374. log_fn(LOG_INFO,"in-cell at OP not recognized. Dropping.");
  375. return 0;
  376. } else { /* we're in the middle. Just one crypt. */
  377. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+1));
  378. if(crypto_cipher_encrypt(circ->p_crypto, in, inlen, out)) {
  379. log_fn(LOG_WARN,"Onion encryption failed for circID %u: %s",
  380. circ->p_circ_id, crypto_perror());
  381. return -1;
  382. }
  383. memcpy(in,out,inlen);
  384. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+1));
  385. log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  386. /* don't check for recognized. only the OP can recognize a stream on the way back. */
  387. }
  388. } else if(cell_direction == CELL_DIRECTION_OUT) {
  389. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  390. thishop = *layer_hint; /* we already know which layer, from when we package_raw_inbuf'ed */
  391. /* moving from last to first hop */
  392. do {
  393. assert(thishop);
  394. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+1));
  395. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, out)) {
  396. log_fn(LOG_WARN,"Error performing encryption: %s", crypto_perror());
  397. return -1;
  398. }
  399. memcpy(in,out,inlen);
  400. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+1));
  401. thishop = thishop->prev;
  402. } while(thishop != circ->cpath->prev);
  403. } else { /* we're in the middle. Just one crypt. */
  404. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  405. log_fn(LOG_WARN,"Decryption failed for circID %u: %s",
  406. circ->n_circ_id, crypto_perror());
  407. return -1;
  408. }
  409. memcpy(in,out,inlen);
  410. if( (*recognized = relay_check_recognized(circ, cell_direction, in+1, conn)))
  411. return 0;
  412. }
  413. } else {
  414. log_fn(LOG_ERR,"unknown cell direction %d.", cell_direction);
  415. assert(0);
  416. }
  417. return 0;
  418. }
  419. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn) {
  420. /* FIXME can optimize by passing thishop in */
  421. connection_t *tmpconn;
  422. if(!memcmp(stream,ZERO_STREAM,STREAM_ID_SIZE)) {
  423. log_fn(LOG_DEBUG,"It's the zero stream. Recognized.");
  424. return 1; /* the zero stream is always recognized */
  425. }
  426. log_fn(LOG_DEBUG,"not the zero stream.");
  427. if(cell_direction == CELL_DIRECTION_OUT)
  428. tmpconn = circ->n_streams;
  429. else
  430. tmpconn = circ->p_streams;
  431. if(!tmpconn) {
  432. log_fn(LOG_DEBUG,"No conns. Not recognized.");
  433. return 0;
  434. }
  435. for( ; tmpconn; tmpconn=tmpconn->next_stream) {
  436. if(!memcmp(stream,tmpconn->stream_id, STREAM_ID_SIZE)) {
  437. log_fn(LOG_DEBUG,"recognized stream %d.", *(int*)stream);
  438. *conn = tmpconn;
  439. return 1;
  440. }
  441. log_fn(LOG_DEBUG,"considered stream %d, not it.",*(int*)tmpconn->stream_id);
  442. }
  443. log_fn(LOG_DEBUG,"Didn't recognize on this iteration of decryption.");
  444. return 0;
  445. }
  446. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  447. connection_t *conn;
  448. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  449. log_fn(LOG_DEBUG,"resuming");
  450. if(edge_type == EDGE_EXIT)
  451. conn = circ->n_streams;
  452. else
  453. conn = circ->p_streams;
  454. for( ; conn; conn=conn->next_stream) {
  455. if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
  456. (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  457. connection_start_reading(conn);
  458. connection_edge_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  459. /* If the circuit won't accept any more data, return without looking
  460. * at any more of the streams. Any connections that should be stopped
  461. * have already been stopped by connection_edge_package_raw_inbuf. */
  462. if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
  463. return;
  464. }
  465. }
  466. }
  467. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  468. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  469. connection_t *conn = NULL;
  470. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  471. assert(edge_type == EDGE_EXIT || layer_hint);
  472. log_fn(LOG_DEBUG,"considering");
  473. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  474. conn = circ->n_streams;
  475. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  476. conn = circ->p_streams;
  477. else
  478. return 0;
  479. for( ; conn; conn=conn->next_stream)
  480. if(!layer_hint || conn->cpath_layer == layer_hint)
  481. connection_stop_reading(conn);
  482. log_fn(LOG_DEBUG,"yes. stopped.");
  483. return 1;
  484. }
  485. void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  486. while((edge_type == EDGE_AP ? layer_hint->deliver_window : circ->deliver_window) <
  487. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  488. log_fn(LOG_DEBUG,"Queueing circuit sendme.");
  489. if(edge_type == EDGE_AP)
  490. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  491. else
  492. circ->deliver_window += CIRCWINDOW_INCREMENT;
  493. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
  494. NULL, 0, layer_hint) < 0) {
  495. log_fn(LOG_WARN,"connection_edge_send_command failed. Returning.");
  496. return; /* the circuit's closed, don't continue */
  497. }
  498. }
  499. }
  500. void circuit_close(circuit_t *circ) {
  501. connection_t *conn;
  502. assert(circ);
  503. circuit_remove(circ);
  504. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  505. onion_pending_remove(circ);
  506. }
  507. if(circ->n_conn)
  508. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  509. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  510. connection_send_destroy(circ->n_circ_id, conn);
  511. }
  512. if(circ->p_conn)
  513. connection_send_destroy(circ->n_circ_id, circ->p_conn);
  514. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  515. connection_send_destroy(circ->p_circ_id, conn);
  516. }
  517. if (circ->state == CIRCUIT_STATE_BUILDING ||
  518. circ->state == CIRCUIT_STATE_OR_WAIT) {
  519. /* If we never built the circuit, note it as a failure. */
  520. /* Note that we can't just check circ->cpath here, because if
  521. * circuit-building failed immediately, it won't be set yet. */
  522. circuit_increment_failure_count();
  523. }
  524. circuit_free(circ);
  525. }
  526. void circuit_about_to_close_connection(connection_t *conn) {
  527. /* send destroys for all circuits using conn */
  528. /* currently, we assume it's too late to flush conn's buf here.
  529. * down the road, maybe we'll consider that eof doesn't mean can't-write
  530. */
  531. circuit_t *circ;
  532. connection_t *prevconn;
  533. switch(conn->type) {
  534. case CONN_TYPE_OR:
  535. /* We must close all the circuits on it. */
  536. while((circ = circuit_get_by_conn(conn))) {
  537. if(circ->n_conn == conn) /* it's closing in front of us */
  538. circ->n_conn = NULL;
  539. if(circ->p_conn == conn) /* it's closing behind us */
  540. circ->p_conn = NULL;
  541. circuit_close(circ);
  542. }
  543. return;
  544. case CONN_TYPE_AP:
  545. case CONN_TYPE_EXIT:
  546. /* It's an edge conn. Need to remove it from the linked list of
  547. * conn's for this circuit. Confirm that 'end' relay command has
  548. * been sent. But don't kill the circuit.
  549. */
  550. circ = circuit_get_by_conn(conn);
  551. if(!circ)
  552. return;
  553. if(!conn->has_sent_end) {
  554. log_fn(LOG_INFO,"Edge connection hasn't sent end yet? Bug.");
  555. if(connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer) < 0)
  556. log_fn(LOG_WARN,"1: I called connection_edge_end redundantly.");
  557. }
  558. if(conn == circ->p_streams) {
  559. circ->p_streams = conn->next_stream;
  560. return;
  561. }
  562. if(conn == circ->n_streams) {
  563. circ->n_streams = conn->next_stream;
  564. return;
  565. }
  566. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  567. if(prevconn && prevconn->next_stream) {
  568. prevconn->next_stream = conn->next_stream;
  569. return;
  570. }
  571. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  572. if(prevconn && prevconn->next_stream) {
  573. prevconn->next_stream = conn->next_stream;
  574. return;
  575. }
  576. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  577. assert(0); /* should never get here */
  578. } /* end switch */
  579. }
  580. void circuit_dump_details(int severity, circuit_t *circ, int poll_index,
  581. char *type, int this_circid, int other_circid) {
  582. struct crypt_path_t *hop;
  583. log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
  584. poll_index, type, this_circid, other_circid, circ->state,
  585. circuit_state_to_string[circ->state], (int)circ->timestamp_created);
  586. if(circ->cpath) { /* circ starts at this node */
  587. if(circ->state == CIRCUIT_STATE_BUILDING)
  588. log(severity,"Building: desired len %d, planned exit node %s.",
  589. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  590. for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next)
  591. log(severity,"hop: state %d, addr %x, port %d", hop->state,
  592. (unsigned int)hop->addr,
  593. (int)hop->port);
  594. }
  595. }
  596. void circuit_dump_by_conn(connection_t *conn, int severity) {
  597. circuit_t *circ;
  598. connection_t *tmpconn;
  599. for(circ=global_circuitlist;circ;circ = circ->next) {
  600. if(circ->p_conn == conn)
  601. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  602. circ->p_circ_id, circ->n_circ_id);
  603. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  604. if(tmpconn == conn) {
  605. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  606. circ->p_circ_id, circ->n_circ_id);
  607. }
  608. }
  609. if(circ->n_conn == conn)
  610. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  611. circ->n_circ_id, circ->p_circ_id);
  612. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  613. if(tmpconn == conn) {
  614. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  615. circ->n_circ_id, circ->p_circ_id);
  616. }
  617. }
  618. }
  619. }
  620. void circuit_expire_unused_circuits(void) {
  621. circuit_t *circ, *tmpcirc;
  622. time_t now = time(NULL);
  623. circ = global_circuitlist;
  624. while(circ) {
  625. tmpcirc = circ;
  626. circ = circ->next;
  627. if(tmpcirc->timestamp_dirty &&
  628. tmpcirc->timestamp_dirty + options.NewCircuitPeriod < now &&
  629. !tmpcirc->p_conn && !tmpcirc->p_streams) {
  630. log_fn(LOG_DEBUG,"Closing n_circ_id %d",tmpcirc->n_circ_id);
  631. circuit_close(tmpcirc);
  632. }
  633. }
  634. }
  635. /* Number of consecutive failures so far; should only be touched by
  636. * circuit_launch_new and circuit_*_failure_count.
  637. */
  638. static int n_circuit_failures = 0;
  639. /* Return -1 if you aren't going to try to make a circuit, 0 if you did try. */
  640. int circuit_launch_new(void) {
  641. if(!options.SocksPort) /* we're not an application proxy. no need for circuits. */
  642. return -1;
  643. if(n_circuit_failures > 5) { /* too many failed circs in a row. don't try. */
  644. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  645. return -1;
  646. }
  647. /* try a circ. if it fails, circuit_close will increment n_circuit_failures */
  648. circuit_establish_circuit();
  649. return 0;
  650. }
  651. void circuit_increment_failure_count(void) {
  652. ++n_circuit_failures;
  653. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  654. }
  655. void circuit_reset_failure_count(void) {
  656. n_circuit_failures = 0;
  657. }
  658. int circuit_establish_circuit(void) {
  659. routerinfo_t *firsthop;
  660. connection_t *n_conn;
  661. circuit_t *circ;
  662. circ = circuit_new(0, NULL); /* sets circ->p_circ_id and circ->p_conn */
  663. circ->state = CIRCUIT_STATE_OR_WAIT;
  664. circ->build_state = onion_new_cpath_build_state();
  665. if (! circ->build_state) {
  666. log_fn(LOG_INFO,"Generating cpath length failed.");
  667. circuit_close(circ);
  668. return -1;
  669. }
  670. onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop);
  671. if(!circ->cpath) {
  672. log_fn(LOG_INFO,"Generating first cpath hop failed.");
  673. circuit_close(circ);
  674. return -1;
  675. }
  676. /* now see if we're already connected to the first OR in 'route' */
  677. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  678. firsthop->address,firsthop->or_port);
  679. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  680. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  681. circ->n_addr = firsthop->addr;
  682. circ->n_port = firsthop->or_port;
  683. if(options.ORPort) { /* we would be connected if he were up. and he's not. */
  684. log_fn(LOG_INFO,"Route's firsthop isn't connected.");
  685. circuit_close(circ);
  686. return -1;
  687. }
  688. if(!n_conn) { /* launch the connection */
  689. n_conn = connection_or_connect(firsthop);
  690. if(!n_conn) { /* connect failed, forget the whole thing */
  691. log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
  692. circuit_close(circ);
  693. return -1;
  694. }
  695. }
  696. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  697. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  698. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  699. */
  700. } else { /* it (or a twin) is already open. use it. */
  701. circ->n_addr = n_conn->addr;
  702. circ->n_port = n_conn->port;
  703. circ->n_conn = n_conn;
  704. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  705. if(circuit_send_next_onion_skin(circ) < 0) {
  706. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  707. circuit_close(circ);
  708. return -1;
  709. }
  710. }
  711. return 0;
  712. }
  713. /* find circuits that are waiting on me, if any, and get them to send the onion */
  714. void circuit_n_conn_open(connection_t *or_conn) {
  715. circuit_t *circ;
  716. for(circ=global_circuitlist;circ;circ = circ->next) {
  717. if(circ->cpath && circ->n_addr == or_conn->addr && circ->n_port == or_conn->port) {
  718. assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  719. log_fn(LOG_DEBUG,"Found circ %d, sending onion skin.", circ->n_circ_id);
  720. circ->n_conn = or_conn;
  721. if(circuit_send_next_onion_skin(circ) < 0) {
  722. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  723. circuit_close(circ);
  724. continue;
  725. /* XXX could this be bad, eg if next_onion_skin failed because conn died? */
  726. }
  727. }
  728. }
  729. }
  730. int circuit_send_next_onion_skin(circuit_t *circ) {
  731. cell_t cell;
  732. crypt_path_t *hop;
  733. routerinfo_t *router;
  734. int r;
  735. int circ_id_type;
  736. char payload[6+ONIONSKIN_CHALLENGE_LEN];
  737. assert(circ && circ->cpath);
  738. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  739. assert(circ->n_conn && circ->n_conn->type == CONN_TYPE_OR);
  740. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  741. circ_id_type = decide_circ_id_type(options.Nickname,
  742. circ->n_conn->nickname);
  743. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  744. memset(&cell, 0, sizeof(cell_t));
  745. cell.command = CELL_CREATE;
  746. cell.circ_id = circ->n_circ_id;
  747. if(onion_skin_create(circ->n_conn->onion_pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
  748. log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
  749. return -1;
  750. }
  751. connection_or_write_cell_to_buf(&cell, circ->n_conn);
  752. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  753. circ->state = CIRCUIT_STATE_BUILDING;
  754. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  755. } else {
  756. assert(circ->cpath->state == CPATH_STATE_OPEN);
  757. assert(circ->state == CIRCUIT_STATE_BUILDING);
  758. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  759. r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
  760. if (r==1) {
  761. /* done building the circuit. whew. */
  762. circ->state = CIRCUIT_STATE_OPEN;
  763. log_fn(LOG_INFO,"circuit built!");
  764. circuit_reset_failure_count();
  765. /* Tell any AP connections that have been waiting for a new
  766. * circuit that one is ready. */
  767. connection_ap_attach_pending();
  768. return 0;
  769. } else if (r<0) {
  770. log_fn(LOG_INFO,"Unable to extend circuit path.");
  771. return -1;
  772. }
  773. hop = circ->cpath->prev;
  774. *(uint32_t*)payload = htonl(hop->addr);
  775. *(uint16_t*)(payload+4) = htons(hop->port);
  776. if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), payload+6) < 0) {
  777. log_fn(LOG_WARN,"onion_skin_create failed.");
  778. return -1;
  779. }
  780. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  781. /* send it to hop->prev, because it will transfer
  782. * it to a create cell and then send to hop */
  783. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTEND,
  784. payload, sizeof(payload), hop->prev) < 0)
  785. return -1; /* circuit is closed */
  786. hop->state = CPATH_STATE_AWAITING_KEYS;
  787. }
  788. return 0;
  789. }
  790. /* take the 'extend' cell, pull out addr/port plus the onion skin. Make
  791. * sure we're connected to the next hop, and pass it the onion skin in
  792. * a create cell.
  793. */
  794. int circuit_extend(cell_t *cell, circuit_t *circ) {
  795. connection_t *n_conn;
  796. int circ_id_type;
  797. cell_t newcell;
  798. if(circ->n_conn) {
  799. log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
  800. return -1;
  801. }
  802. circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
  803. circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
  804. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  805. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  806. /* i've disabled making connections through OPs, but it's definitely
  807. * possible here. I'm not sure if it would be a bug or a feature. -RD
  808. */
  809. /* note also that this will close circuits where the onion has the same
  810. * router twice in a row in the path. i think that's ok. -RD
  811. */
  812. struct in_addr in;
  813. in.s_addr = htonl(circ->n_addr);
  814. log_fn(LOG_INFO,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  815. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  816. NULL, 0, NULL);
  817. return 0;
  818. }
  819. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  820. circ->n_port = n_conn->port;
  821. circ->n_conn = n_conn;
  822. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  823. circ_id_type = decide_circ_id_type(options.Nickname, n_conn->nickname);
  824. log_fn(LOG_DEBUG,"circ_id_type = %u.",circ_id_type);
  825. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  826. if(!circ->n_circ_id) {
  827. log_fn(LOG_WARN,"failed to get unique circID.");
  828. return -1;
  829. }
  830. log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
  831. memset(&newcell, 0, sizeof(cell_t));
  832. newcell.command = CELL_CREATE;
  833. newcell.circ_id = circ->n_circ_id;
  834. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6,
  835. ONIONSKIN_CHALLENGE_LEN);
  836. connection_or_write_cell_to_buf(&newcell, circ->n_conn);
  837. return 0;
  838. }
  839. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  840. unsigned char iv[16];
  841. unsigned char keys[40+32];
  842. crypt_path_t *hop;
  843. memset(iv, 0, 16);
  844. assert(circ->cpath);
  845. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  846. hop = circ->cpath;
  847. else {
  848. for(hop=circ->cpath->next;
  849. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  850. hop=hop->next) ;
  851. if(hop == circ->cpath) { /* got an extended when we're all done? */
  852. log_fn(LOG_WARN,"got extended when circ already built? Closing.");
  853. return -1;
  854. }
  855. }
  856. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  857. if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 40+32) < 0) {
  858. log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
  859. return -1;
  860. }
  861. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  862. hop->handshake_state = NULL;
  863. log_fn(LOG_INFO,"hop %d init digest forward %u, backward %u.",
  864. (int)hop, (unsigned)*(uint32_t*)keys, (unsigned)*(uint32_t*)(keys+20));
  865. hop->f_digest = crypto_new_digest_env(CRYPTO_SHA1_DIGEST);
  866. crypto_digest_add_bytes(hop->f_digest, keys, 20);
  867. hop->b_digest = crypto_new_digest_env(CRYPTO_SHA1_DIGEST);
  868. crypto_digest_add_bytes(hop->b_digest, keys+20, 20);
  869. log_fn(LOG_DEBUG,"hop %d init cipher forward %u, backward %u.",
  870. (int)hop, (unsigned)*(uint32_t*)(keys+40), (unsigned) *(uint32_t*)(keys+40+16));
  871. if (!(hop->f_crypto =
  872. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+40,iv,1))) {
  873. log(LOG_WARN,"forward cipher initialization failed.");
  874. return -1;
  875. }
  876. if (!(hop->b_crypto =
  877. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+40+16,iv,0))) {
  878. log(LOG_WARN,"backward cipher initialization failed.");
  879. return -1;
  880. }
  881. hop->state = CPATH_STATE_OPEN;
  882. log_fn(LOG_INFO,"finished");
  883. return 0;
  884. }
  885. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  886. crypt_path_t *victim;
  887. connection_t *stream;
  888. assert(circ);
  889. assert(layer);
  890. /* XXX Since we don't ask for truncates currently, getting a truncated
  891. * means that a connection broke or an extend failed. For now,
  892. * just give up.
  893. */
  894. circuit_close(circ);
  895. return 0;
  896. while(layer->next != circ->cpath) {
  897. /* we need to clear out layer->next */
  898. victim = layer->next;
  899. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  900. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  901. if(stream->cpath_layer == victim) {
  902. log_fn(LOG_INFO, "Marking stream %d for close.", *(int*)stream->stream_id);
  903. /* no need to send 'end' relay cells,
  904. * because the other side's already dead
  905. */
  906. stream->marked_for_close = 1;
  907. stream->has_sent_end = 1;
  908. }
  909. }
  910. layer->next = victim->next;
  911. circuit_free_cpath_node(victim);
  912. }
  913. log_fn(LOG_INFO, "finished");
  914. return 0;
  915. }
  916. void assert_cpath_layer_ok(const crypt_path_t *cp)
  917. {
  918. assert(cp->f_crypto);
  919. assert(cp->b_crypto);
  920. assert(cp->addr);
  921. assert(cp->port);
  922. switch(cp->state)
  923. {
  924. case CPATH_STATE_CLOSED:
  925. case CPATH_STATE_OPEN:
  926. assert(!cp->handshake_state);
  927. break;
  928. case CPATH_STATE_AWAITING_KEYS:
  929. assert(cp->handshake_state);
  930. break;
  931. default:
  932. assert(0);
  933. }
  934. assert(cp->package_window >= 0);
  935. assert(cp->deliver_window >= 0);
  936. }
  937. void assert_cpath_ok(const crypt_path_t *cp)
  938. {
  939. while(cp->prev)
  940. cp = cp->prev;
  941. while(cp->next) {
  942. assert_cpath_layer_ok(cp);
  943. /* layers must be in sequence of: "open* awaiting? closed*" */
  944. if (cp->prev) {
  945. if (cp->prev->state == CPATH_STATE_OPEN) {
  946. assert(cp->state == CPATH_STATE_CLOSED ||
  947. cp->state == CPATH_STATE_AWAITING_KEYS);
  948. } else {
  949. assert(cp->state == CPATH_STATE_CLOSED);
  950. }
  951. }
  952. cp = cp->next;
  953. }
  954. }
  955. void assert_circuit_ok(const circuit_t *c)
  956. {
  957. connection_t *conn;
  958. assert(c->n_addr);
  959. assert(c->n_port);
  960. assert(c->n_conn);
  961. assert(c->n_conn->type == CONN_TYPE_OR);
  962. if (c->p_conn)
  963. assert(c->p_conn->type == CONN_TYPE_OR);
  964. for (conn = c->p_streams; conn; conn = conn->next_stream)
  965. assert(c->p_conn->type == CONN_TYPE_EXIT);
  966. for (conn = c->n_streams; conn; conn = conn->next_stream)
  967. assert(conn->type == CONN_TYPE_EXIT);
  968. assert(c->deliver_window >= 0);
  969. assert(c->package_window >= 0);
  970. if (c->state == CIRCUIT_STATE_OPEN) {
  971. if (c->cpath) {
  972. assert(!c->n_crypto);
  973. assert(!c->p_crypto);
  974. assert(!c->n_digest);
  975. assert(!c->p_digest);
  976. } else {
  977. assert(c->n_crypto);
  978. assert(c->p_crypto);
  979. assert(c->n_digest);
  980. assert(c->p_digest);
  981. }
  982. }
  983. if (c->cpath) {
  984. assert_cpath_ok(c->cpath);
  985. }
  986. }
  987. /*
  988. Local Variables:
  989. mode:c
  990. indent-tabs-mode:nil
  991. c-basic-offset:2
  992. End:
  993. */