circuit.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /* Copyright 2001,2002 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. /********* START VARIABLES **********/
  7. static circuit_t *global_circuitlist=NULL;
  8. char *circuit_state_to_string[] = {
  9. "receiving the onion", /* 0 */
  10. "waiting to process create", /* 1 */
  11. "connecting to firsthop", /* 2 */
  12. "open" /* 3 */
  13. };
  14. /********* END VARIABLES ************/
  15. void circuit_add(circuit_t *circ) {
  16. if(!global_circuitlist) { /* first one */
  17. global_circuitlist = circ;
  18. circ->next = NULL;
  19. } else {
  20. circ->next = global_circuitlist;
  21. global_circuitlist = circ;
  22. }
  23. }
  24. void circuit_remove(circuit_t *circ) {
  25. circuit_t *tmpcirc;
  26. assert(circ && global_circuitlist);
  27. if(global_circuitlist == circ) {
  28. global_circuitlist = global_circuitlist->next;
  29. return;
  30. }
  31. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  32. if(tmpcirc->next == circ) {
  33. tmpcirc->next = circ->next;
  34. return;
  35. }
  36. }
  37. }
  38. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  39. circuit_t *circ;
  40. struct timeval now;
  41. my_gettimeofday(&now);
  42. circ = (circuit_t *)malloc(sizeof(circuit_t));
  43. if(!circ)
  44. return NULL;
  45. memset(circ,0,sizeof(circuit_t)); /* zero it out */
  46. circ->timestamp_created = now.tv_sec;
  47. circ->p_aci = p_aci;
  48. circ->p_conn = p_conn;
  49. circ->state = CIRCUIT_STATE_ONION_WAIT;
  50. /* ACIs */
  51. circ->p_aci = p_aci;
  52. /* circ->n_aci remains 0 because we haven't identified the next hop yet */
  53. circ->n_receive_circwindow = CIRCWINDOW_START;
  54. circ->p_receive_circwindow = CIRCWINDOW_START;
  55. circuit_add(circ);
  56. return circ;
  57. }
  58. void circuit_free(circuit_t *circ) {
  59. struct data_queue_t *tmpd;
  60. if (circ->n_crypto)
  61. crypto_free_cipher_env(circ->n_crypto);
  62. if (circ->p_crypto)
  63. crypto_free_cipher_env(circ->p_crypto);
  64. if(circ->onion)
  65. free(circ->onion);
  66. if(circ->cpath)
  67. circuit_free_cpath(circ->cpath, circ->cpathlen);
  68. while(circ->data_queue) {
  69. tmpd = circ->data_queue;
  70. circ->data_queue = tmpd->next;
  71. free(tmpd->cell);
  72. free(tmpd);
  73. }
  74. free(circ);
  75. }
  76. void circuit_free_cpath(crypt_path_t **cpath, int cpathlen) {
  77. int i;
  78. for(i=0;i<cpathlen;i++)
  79. free(cpath[i]);
  80. free(cpath);
  81. }
  82. /* return 0 if can't get a unique aci. */
  83. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  84. aci_t test_aci;
  85. connection_t *conn;
  86. try_again:
  87. log(LOG_DEBUG,"get_unique_aci_by_addr_port() trying to get a unique aci");
  88. if (CRYPTO_PSEUDO_RAND_INT(test_aci))
  89. return -1;
  90. if(aci_type == ACI_TYPE_LOWER && test_aci >= (1<<15))
  91. test_aci -= (1<<15);
  92. if(aci_type == ACI_TYPE_HIGHER && test_aci < (1<<15))
  93. test_aci += (1<<15);
  94. /* if aci_type == ACI_BOTH, don't filter any of it */
  95. if(test_aci == 0)
  96. goto try_again;
  97. conn = connection_exact_get_by_addr_port(addr,port);
  98. if(!conn) /* there can't be a conflict -- no connection of that sort yet */
  99. return test_aci;
  100. if(circuit_get_by_aci_conn(test_aci, conn))
  101. goto try_again;
  102. return test_aci;
  103. }
  104. int circuit_init(circuit_t *circ, int aci_type, onion_layer_t *layer) {
  105. unsigned char iv[16];
  106. unsigned char digest1[20];
  107. unsigned char digest2[20];
  108. struct timeval start, end;
  109. long time_passed;
  110. assert(circ && circ->onion);
  111. log(LOG_DEBUG,"circuit_init(): starting");
  112. circ->n_port = layer->port;
  113. log(LOG_DEBUG,"circuit_init(): Set port to %u.",circ->n_port);
  114. circ->n_addr = layer->addr;
  115. circ->state = CIRCUIT_STATE_OPEN;
  116. log(LOG_DEBUG,"circuit_init(): aci_type = %u.",aci_type);
  117. my_gettimeofday(&start);
  118. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
  119. if(!circ->n_aci) {
  120. log(LOG_ERR,"circuit_init(): failed to get unique aci.");
  121. return -1;
  122. }
  123. my_gettimeofday(&end);
  124. time_passed = tv_udiff(&start, &end);
  125. if (time_passed > 1000) {/* more than 1ms */
  126. log(LOG_NOTICE,"circuit_init(): get_unique_aci just took %d us!",time_passed);
  127. }
  128. log(LOG_DEBUG,"circuit_init(): Chosen ACI %u.",circ->n_aci);
  129. /* keys */
  130. memset(iv, 0, 16);
  131. crypto_SHA_digest(layer->keyseed,16,digest1);
  132. crypto_SHA_digest(digest1,20,digest2);
  133. crypto_SHA_digest(digest2,20,digest1);
  134. log(LOG_DEBUG,"circuit_init(): Computed keys.");
  135. if (!(circ->p_crypto =
  136. crypto_create_init_cipher(DEFAULT_CIPHER,digest2,iv,1))) {
  137. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  138. return -1;
  139. }
  140. if (!(circ->n_crypto =
  141. crypto_create_init_cipher(DEFAULT_CIPHER,digest1,iv,0))) {
  142. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  143. return -1;
  144. }
  145. log(LOG_DEBUG,"circuit_init(): Cipher initialization complete.");
  146. circ->expire = layer->expire;
  147. return 0;
  148. }
  149. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
  150. if(!circ) /* use circ if it's defined, else start from the beginning */
  151. circ = global_circuitlist;
  152. else
  153. circ = circ->next;
  154. for( ; circ; circ = circ->next) {
  155. if(circ->n_addr == naddr && circ->n_port == nport)
  156. return circ;
  157. }
  158. return NULL;
  159. }
  160. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  161. circuit_t *circ;
  162. connection_t *tmpconn;
  163. for(circ=global_circuitlist;circ;circ = circ->next) {
  164. if(circ->p_aci == aci) {
  165. for(tmpconn = circ->p_conn; tmpconn; tmpconn = tmpconn->next_topic) {
  166. if(tmpconn == conn)
  167. return circ;
  168. }
  169. }
  170. if(circ->n_aci == aci) {
  171. for(tmpconn = circ->n_conn; tmpconn; tmpconn = tmpconn->next_topic) {
  172. if(tmpconn == conn)
  173. return circ;
  174. }
  175. }
  176. }
  177. return NULL;
  178. }
  179. circuit_t *circuit_get_by_conn(connection_t *conn) {
  180. circuit_t *circ;
  181. connection_t *tmpconn;
  182. for(circ=global_circuitlist;circ;circ = circ->next) {
  183. for(tmpconn = circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic)
  184. if(tmpconn == conn)
  185. return circ;
  186. for(tmpconn = circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic)
  187. if(tmpconn == conn)
  188. return circ;
  189. }
  190. return NULL;
  191. }
  192. circuit_t *circuit_get_newest_by_edge_type(char edge_type) {
  193. circuit_t *circ, *bestcirc=NULL;
  194. for(circ=global_circuitlist;circ;circ = circ->next) {
  195. if(edge_type == EDGE_AP && (!circ->p_conn || circ->p_conn->type == CONN_TYPE_AP)) {
  196. if(!bestcirc ||
  197. (circ->state == CIRCUIT_STATE_OPEN && bestcirc->timestamp_created < circ->timestamp_created)) {
  198. log(LOG_DEBUG,"circuit_get_newest_by_edge_type(): Choosing n_aci %d.", circ->n_aci);
  199. bestcirc = circ;
  200. }
  201. }
  202. if(edge_type == EDGE_EXIT && (!circ->n_conn || circ->n_conn->type == CONN_TYPE_EXIT)) {
  203. if(!bestcirc ||
  204. (circ->state == CIRCUIT_STATE_OPEN && bestcirc->timestamp_created < circ->timestamp_created))
  205. bestcirc = circ;
  206. }
  207. }
  208. return bestcirc;
  209. }
  210. int circuit_deliver_data_cell_from_edge(cell_t *cell, circuit_t *circ, char edge_type) {
  211. int cell_direction;
  212. static int numsent_ap=0, numsent_exit=0;
  213. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): called, edge_type %d.", edge_type);
  214. if(edge_type == EDGE_AP) { /* i'm the AP */
  215. cell_direction = CELL_DIRECTION_OUT;
  216. numsent_ap++;
  217. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): now sent %d data cells from ap", numsent_ap);
  218. if(circ->p_receive_circwindow <= 0) {
  219. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): pwindow 0, queueing for later.");
  220. circ->data_queue = data_queue_add(circ->data_queue, cell);
  221. return 0;
  222. }
  223. circ->p_receive_circwindow--;
  224. // log(LOG_INFO,"circuit_deliver_data_cell_from_edge(): p_receive_circwindow now %d.",circ->p_receive_circwindow);
  225. } else { /* i'm the exit */
  226. cell_direction = CELL_DIRECTION_IN;
  227. numsent_exit++;
  228. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): now sent %d data cells from exit", numsent_exit);
  229. if(circ->n_receive_circwindow <= 0) {
  230. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): nwindow 0, queueing for later.");
  231. circ->data_queue = data_queue_add(circ->data_queue, cell);
  232. return 0;
  233. }
  234. circ->n_receive_circwindow--;
  235. }
  236. if(circuit_deliver_data_cell(cell, circ, cell_direction) < 0) {
  237. return -1;
  238. }
  239. circuit_consider_stop_edge_reading(circ, edge_type); /* has window reached 0? */
  240. return 0;
  241. }
  242. int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, int cell_direction) {
  243. connection_t *conn;
  244. assert(cell && circ);
  245. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  246. if(cell_direction == CELL_DIRECTION_OUT)
  247. conn = circ->n_conn;
  248. else
  249. conn = circ->p_conn;
  250. /* first crypt cell->length */
  251. if(circuit_crypt(circ, &(cell->length), 1, cell_direction) < 0) {
  252. log(LOG_DEBUG,"circuit_deliver_data_cell(): length crypt failed. Dropping connection.");
  253. return -1;
  254. }
  255. /* then crypt the payload */
  256. if(circuit_crypt(circ, (char *)&(cell->payload), CELL_PAYLOAD_SIZE, cell_direction) < 0) {
  257. log(LOG_DEBUG,"circuit_deliver_data_cell(): payload crypt failed. Dropping connection.");
  258. return -1;
  259. }
  260. if(cell_direction == CELL_DIRECTION_OUT && (!conn || conn->type == CONN_TYPE_EXIT)) {
  261. log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to exit.");
  262. return connection_edge_process_data_cell(cell, circ, EDGE_EXIT);
  263. }
  264. if(cell_direction == CELL_DIRECTION_IN && (!conn || conn->type == CONN_TYPE_AP)) {
  265. log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to AP.");
  266. return connection_edge_process_data_cell(cell, circ, EDGE_AP);
  267. }
  268. /* else send it as a cell */
  269. assert(conn);
  270. //log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to connection.");
  271. return connection_write_cell_to_buf(cell, conn);
  272. }
  273. int circuit_crypt(circuit_t *circ, char *in, int inlen, char cell_direction) {
  274. char *out;
  275. int i;
  276. crypt_path_t *thishop;
  277. assert(circ && in);
  278. out = (char *)malloc(inlen);
  279. if(!out)
  280. return -1;
  281. if(cell_direction == CELL_DIRECTION_IN) {
  282. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  283. for (i=circ->cpathlen-1; i >= 0; i--) /* moving from first to last hop
  284. * Remember : cpath is in reverse order, i.e. last hop first
  285. */
  286. {
  287. thishop = circ->cpath[i];
  288. /* decrypt */
  289. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  290. log(LOG_ERR,"Error performing decryption:%s",crypto_perror());
  291. free(out);
  292. return -1;
  293. }
  294. /* copy ciphertext back to buf */
  295. memcpy(in,out,inlen);
  296. }
  297. } else { /* we're in the middle. Just one crypt. */
  298. if(crypto_cipher_encrypt(circ->p_crypto,in, inlen, out)) {
  299. log(LOG_ERR,"circuit_encrypt(): Encryption failed for ACI : %u (%s).",
  300. circ->p_aci, crypto_perror());
  301. free(out);
  302. return -1;
  303. }
  304. memcpy(in,out,inlen);
  305. }
  306. } else if(cell_direction == CELL_DIRECTION_OUT) {
  307. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  308. for (i=0; i < circ->cpathlen; i++) /* moving from last to first hop
  309. * Remember : cpath is in reverse order, i.e. last hop first
  310. */
  311. {
  312. thishop = circ->cpath[i];
  313. /* encrypt */
  314. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, (unsigned char *)out)) {
  315. log(LOG_ERR,"Error performing encryption:%s",crypto_perror());
  316. free(out);
  317. return -1;
  318. }
  319. /* copy ciphertext back to buf */
  320. memcpy(in,out,inlen);
  321. }
  322. } else { /* we're in the middle. Just one crypt. */
  323. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  324. log(LOG_ERR,"circuit_crypt(): Decryption failed for ACI : %u (%s).",
  325. circ->n_aci, crypto_perror());
  326. free(out);
  327. return -1;
  328. }
  329. memcpy(in,out,inlen);
  330. }
  331. } else {
  332. log(LOG_ERR,"circuit_crypt(): unknown cell direction %d.", cell_direction);
  333. assert(0);
  334. }
  335. free(out);
  336. return 0;
  337. }
  338. void circuit_resume_edge_reading(circuit_t *circ, int edge_type) {
  339. connection_t *conn;
  340. struct data_queue_t *tmpd;
  341. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  342. /* first, send the queue waiting at circ onto the circuit */
  343. while(circ->data_queue) {
  344. assert(circ->data_queue->cell);
  345. if(edge_type == EDGE_EXIT) {
  346. circ->n_receive_circwindow--;
  347. assert(circ->n_receive_circwindow >= 0);
  348. if(circuit_deliver_data_cell(circ->data_queue->cell, circ, CELL_DIRECTION_IN) < 0) {
  349. circuit_close(circ);
  350. return;
  351. }
  352. } else { /* ap */
  353. circ->p_receive_circwindow--;
  354. assert(circ->p_receive_circwindow >= 0);
  355. if(circuit_deliver_data_cell(circ->data_queue->cell, circ, CELL_DIRECTION_OUT) < 0) {
  356. circuit_close(circ);
  357. return;
  358. }
  359. }
  360. tmpd = circ->data_queue;
  361. circ->data_queue = tmpd->next;
  362. free(tmpd->cell);
  363. free(tmpd);
  364. if(circuit_consider_stop_edge_reading(circ, edge_type))
  365. return;
  366. }
  367. if(edge_type == EDGE_EXIT)
  368. conn = circ->n_conn;
  369. else
  370. conn = circ->p_conn;
  371. for( ; conn; conn=conn->next_topic) {
  372. if((edge_type == EDGE_EXIT && conn->n_receive_topicwindow > 0) ||
  373. (edge_type == EDGE_AP && conn->p_receive_topicwindow > 0)) {
  374. connection_start_reading(conn);
  375. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  376. }
  377. }
  378. circuit_consider_stop_edge_reading(circ, edge_type);
  379. }
  380. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  381. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type) {
  382. connection_t *conn = NULL;
  383. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  384. if(edge_type == EDGE_EXIT && circ->n_receive_circwindow <= 0)
  385. conn = circ->n_conn;
  386. else if(edge_type == EDGE_AP && circ->p_receive_circwindow <= 0)
  387. conn = circ->p_conn;
  388. else
  389. return 0;
  390. for( ; conn; conn=conn->next_topic)
  391. connection_stop_reading(conn);
  392. return 1;
  393. }
  394. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type) {
  395. cell_t sendme;
  396. assert(circ);
  397. memset(&sendme, 0, sizeof(cell_t));
  398. sendme.command = CELL_SENDME;
  399. sendme.length = CIRCWINDOW_INCREMENT;
  400. if(edge_type == EDGE_AP) { /* i'm the AP */
  401. while(circ->n_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  402. log(LOG_DEBUG,"circuit_consider_sending_sendme(): n_receive_circwindow %d, Queueing sendme forward.", circ->n_receive_circwindow);
  403. circ->n_receive_circwindow += CIRCWINDOW_INCREMENT;
  404. sendme.aci = circ->n_aci;
  405. if(connection_write_cell_to_buf(&sendme, circ->n_conn) < 0) {
  406. return -1;
  407. }
  408. }
  409. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  410. while(circ->p_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  411. log(LOG_DEBUG,"circuit_consider_sending_sendme(): p_receive_circwindow %d, Queueing sendme back.", circ->p_receive_circwindow);
  412. circ->p_receive_circwindow += CIRCWINDOW_INCREMENT;
  413. sendme.aci = circ->p_aci;
  414. if(connection_write_cell_to_buf(&sendme, circ->p_conn) < 0) {
  415. return -1;
  416. }
  417. }
  418. }
  419. return 0;
  420. }
  421. void circuit_close(circuit_t *circ) {
  422. connection_t *conn;
  423. circuit_t *youngest=NULL;
  424. assert(circ);
  425. if(options.APPort)
  426. youngest = circuit_get_newest_by_edge_type(EDGE_AP);
  427. circuit_remove(circ);
  428. for(conn=circ->n_conn; conn; conn=conn->next_topic) {
  429. connection_send_destroy(circ->n_aci, circ->n_conn);
  430. }
  431. for(conn=circ->p_conn; conn; conn=conn->next_topic) {
  432. connection_send_destroy(circ->p_aci, circ->p_conn);
  433. }
  434. if(options.APPort && youngest == circ) { /* check this after we've sent the destroys, to reduce races */
  435. /* our current circuit just died. Launch another one pronto. */
  436. log(LOG_INFO,"circuit_close(): Youngest circuit dying. Launching a replacement.");
  437. circuit_launch_new(1);
  438. }
  439. circuit_free(circ);
  440. }
  441. void circuit_about_to_close_connection(connection_t *conn) {
  442. /* send destroys for all circuits using conn */
  443. /* currently, we assume it's too late to flush conn's buf here.
  444. * down the road, maybe we'll consider that eof doesn't mean can't-write
  445. */
  446. circuit_t *circ;
  447. connection_t *prevconn, *tmpconn;
  448. if(!connection_speaks_cells(conn)) {
  449. /* it's an edge conn. need to remove it from the linked list of
  450. * conn's for this circuit. Send an 'end' data topic.
  451. * But don't kill the circuit.
  452. */
  453. circ = circuit_get_by_conn(conn);
  454. if(!circ)
  455. return;
  456. if(conn == circ->p_conn) {
  457. circ->p_conn = conn->next_topic;
  458. goto send_end;
  459. }
  460. if(conn == circ->n_conn) {
  461. circ->n_conn = conn->next_topic;
  462. goto send_end;
  463. }
  464. for(prevconn = circ->p_conn; prevconn->next_topic && prevconn->next_topic != conn; prevconn = prevconn->next_topic) ;
  465. if(prevconn->next_topic) {
  466. prevconn->next_topic = conn->next_topic;
  467. goto send_end;
  468. }
  469. for(prevconn = circ->n_conn; prevconn->next_topic && prevconn->next_topic != conn; prevconn = prevconn->next_topic) ;
  470. if(prevconn->next_topic) {
  471. prevconn->next_topic = conn->next_topic;
  472. goto send_end;
  473. }
  474. log(LOG_ERR,"circuit_about_to_close_connection(): edge conn not in circuit's list?");
  475. assert(0); /* should never get here */
  476. send_end:
  477. if(connection_edge_send_command(conn, circ, TOPIC_COMMAND_END) < 0) {
  478. log(LOG_DEBUG,"circuit_about_to_close_connection(): sending end failed. Closing.");
  479. circuit_close(circ);
  480. }
  481. return;
  482. }
  483. while((circ = circuit_get_by_conn(conn))) {
  484. circuit_remove(circ);
  485. if(circ->n_conn == conn) /* it's closing in front of us */
  486. for(tmpconn=circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  487. connection_send_destroy(circ->p_aci, tmpconn);
  488. }
  489. if(circ->p_conn == conn) /* it's closing behind us */
  490. for(tmpconn=circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  491. connection_send_destroy(circ->n_aci, tmpconn);
  492. }
  493. circuit_free(circ);
  494. }
  495. }
  496. /* FIXME this now leaves some out */
  497. void circuit_dump_by_conn(connection_t *conn) {
  498. circuit_t *circ;
  499. connection_t *tmpconn;
  500. for(circ=global_circuitlist;circ;circ = circ->next) {
  501. for(tmpconn=circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  502. if(tmpconn == conn) {
  503. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  504. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  505. }
  506. }
  507. for(tmpconn=circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  508. if(tmpconn == conn) {
  509. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  510. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  511. }
  512. }
  513. }
  514. }
  515. void circuit_expire_unused_circuits(void) {
  516. circuit_t *circ, *tmpcirc;
  517. circuit_t *youngest;
  518. youngest = circuit_get_newest_by_edge_type(EDGE_AP);
  519. circ = global_circuitlist;
  520. while(circ) {
  521. tmpcirc = circ;
  522. circ = circ->next;
  523. if(tmpcirc != youngest && (!tmpcirc->p_conn || tmpcirc->p_conn->type == CONN_TYPE_AP)) {
  524. log(LOG_DEBUG,"circuit_expire_unused_circuits(): Closing n_aci %d",tmpcirc->n_aci);
  525. circuit_close(tmpcirc);
  526. }
  527. }
  528. }
  529. /* failure_status code: negative means reset failures to 0. Other values mean
  530. * add that value to the current number of failures, then if we don't have too
  531. * many failures on record, try to make a new circuit.
  532. */
  533. void circuit_launch_new(int failure_status) {
  534. static int failures=0;
  535. if(failure_status == -1) { /* I was called because a circuit succeeded */
  536. failures = 0;
  537. return;
  538. }
  539. failures += failure_status;
  540. retry_circuit:
  541. if(failures > 5) {
  542. log(LOG_INFO,"circuit_launch_new(): Giving up, %d failures.", failures);
  543. return;
  544. }
  545. if(circuit_create_onion() < 0) {
  546. failures++;
  547. goto retry_circuit;
  548. }
  549. failures = 0;
  550. return;
  551. }
  552. int circuit_create_onion(void) {
  553. int routelen; /* length of the route */
  554. unsigned int *route; /* hops in the route as an array of indexes into rarray */
  555. unsigned char *onion; /* holds the onion */
  556. int onionlen; /* onion length in host order */
  557. crypt_path_t **cpath; /* defines the crypt operations that need to be performed on incoming/outgoing data */
  558. /* choose a route */
  559. route = (unsigned int *)router_new_route(&routelen);
  560. if (!route) {
  561. log(LOG_ERR,"circuit_create_onion(): Error choosing a route through the OR network.");
  562. return -1;
  563. }
  564. log(LOG_DEBUG,"circuit_create_onion(): Chosen a route of length %u : ",routelen);
  565. /* allocate memory for the crypt path */
  566. cpath = malloc(routelen * sizeof(crypt_path_t *));
  567. if (!cpath) {
  568. log(LOG_ERR,"circuit_create_onion(): Error allocating memory for cpath.");
  569. free(route);
  570. return -1;
  571. }
  572. /* create an onion and calculate crypto keys */
  573. onion = router_create_onion(route,routelen,&onionlen,cpath);
  574. if (!onion) {
  575. log(LOG_ERR,"circuit_create_onion(): Error creating an onion.");
  576. free(route);
  577. free(cpath); /* it's got nothing in it, since !onion */
  578. return -1;
  579. }
  580. log(LOG_DEBUG,"circuit_create_onion(): Created an onion of size %u bytes.",onionlen);
  581. log(LOG_DEBUG,"circuit_create_onion(): Crypt path :");
  582. return circuit_establish_circuit(route, routelen, onion, onionlen, cpath);
  583. }
  584. int circuit_establish_circuit(unsigned int *route, int routelen, char *onion,
  585. int onionlen, crypt_path_t **cpath) {
  586. routerinfo_t *firsthop;
  587. connection_t *n_conn;
  588. circuit_t *circ;
  589. /* now see if we're already connected to the first OR in 'route' */
  590. firsthop = router_get_first_in_route(route, routelen);
  591. assert(firsthop); /* should always be defined */
  592. free(route); /* we don't need it anymore */
  593. circ = circuit_new(0, NULL); /* sets circ->p_aci and circ->p_conn */
  594. circ->state = CIRCUIT_STATE_OR_WAIT;
  595. circ->onion = onion;
  596. circ->onionlen = onionlen;
  597. circ->cpath = cpath;
  598. circ->cpathlen = routelen;
  599. log(LOG_DEBUG,"circuit_establish_circuit(): Looking for firsthop '%s:%u'",
  600. firsthop->address,firsthop->or_port);
  601. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  602. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  603. circ->n_addr = firsthop->addr;
  604. circ->n_port = firsthop->or_port;
  605. if(options.ORPort) { /* we would be connected if he were up. but he's not. */
  606. log(LOG_DEBUG,"circuit_establish_circuit(): Route's firsthop isn't connected.");
  607. circuit_close(circ);
  608. return -1;
  609. }
  610. if(!n_conn) { /* launch the connection */
  611. n_conn = connection_or_connect_as_op(firsthop);
  612. if(!n_conn) { /* connect failed, forget the whole thing */
  613. log(LOG_DEBUG,"circuit_establish_circuit(): connect to firsthop failed. Closing.");
  614. circuit_close(circ);
  615. return -1;
  616. }
  617. }
  618. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  619. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  620. */
  621. } else { /* it (or a twin) is already open. use it. */
  622. circ->n_addr = n_conn->addr;
  623. circ->n_port = n_conn->port;
  624. return circuit_send_onion(n_conn, circ);
  625. }
  626. }
  627. /* find circuits that are waiting on me, if any, and get them to send the onion */
  628. void circuit_n_conn_open(connection_t *or_conn) {
  629. circuit_t *circ;
  630. log(LOG_DEBUG,"circuit_n_conn_open(): Starting.");
  631. circ = circuit_enumerate_by_naddr_nport(NULL, or_conn->addr, or_conn->port);
  632. for(;;) {
  633. if(!circ)
  634. return;
  635. log(LOG_DEBUG,"circuit_n_conn_open(): Found circ, sending onion.");
  636. if(circuit_send_onion(or_conn, circ) < 0) {
  637. log(LOG_DEBUG,"circuit_n_conn_open(): circuit marked for closing.");
  638. circuit_close(circ);
  639. return; /* FIXME will want to try the other circuits too? */
  640. }
  641. circ = circuit_enumerate_by_naddr_nport(circ, or_conn->addr, or_conn->port);
  642. }
  643. }
  644. int circuit_send_onion(connection_t *n_conn, circuit_t *circ) {
  645. cell_t cell;
  646. int tmpbuflen, dataleft;
  647. char *tmpbuf;
  648. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  649. circ->n_conn = n_conn;
  650. log(LOG_DEBUG,"circuit_send_onion(): n_conn is %s:%u",n_conn->address,n_conn->port);
  651. /* deliver the onion as one or more create cells */
  652. cell.command = CELL_CREATE;
  653. cell.aci = circ->n_aci;
  654. tmpbuflen = circ->onionlen+4;
  655. tmpbuf = malloc(tmpbuflen);
  656. if(!tmpbuf)
  657. return -1;
  658. *(uint32_t*)tmpbuf = htonl(circ->onionlen);
  659. memcpy(tmpbuf+4, circ->onion, circ->onionlen);
  660. dataleft = tmpbuflen;
  661. while(dataleft) {
  662. cell.command = CELL_CREATE;
  663. cell.aci = circ->n_aci;
  664. log(LOG_DEBUG,"circuit_send_onion(): Sending a create cell for the onion...");
  665. if(dataleft >= CELL_PAYLOAD_SIZE) {
  666. cell.length = CELL_PAYLOAD_SIZE;
  667. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, CELL_PAYLOAD_SIZE);
  668. connection_write_cell_to_buf(&cell, n_conn);
  669. dataleft -= CELL_PAYLOAD_SIZE;
  670. } else { /* last cell */
  671. cell.length = dataleft;
  672. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, dataleft);
  673. /* fill extra space with 0 bytes */
  674. memset(cell.payload + dataleft, 0, CELL_PAYLOAD_SIZE - dataleft);
  675. connection_write_cell_to_buf(&cell, n_conn);
  676. dataleft = 0;
  677. }
  678. }
  679. free(tmpbuf);
  680. circ->state = CIRCUIT_STATE_OPEN;
  681. /* FIXME should set circ->expire to something here */
  682. return 0;
  683. }
  684. /*
  685. Local Variables:
  686. mode:c
  687. indent-tabs-mode:nil
  688. c-basic-offset:2
  689. End:
  690. */