circuit.c 25 KB

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