circuit.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /********* START VARIABLES **********/
  6. static circuit_t *global_circuitlist=NULL;
  7. char *circuit_state_to_string[] = {
  8. "receiving the onion", /* 0 */
  9. "waiting to process create", /* 1 */
  10. "connecting to firsthop", /* 2 */
  11. "open" /* 3 */
  12. };
  13. /********* END VARIABLES ************/
  14. void circuit_add(circuit_t *circ) {
  15. if(!global_circuitlist) { /* first one */
  16. global_circuitlist = circ;
  17. circ->next = NULL;
  18. } else {
  19. circ->next = global_circuitlist;
  20. global_circuitlist = circ;
  21. }
  22. }
  23. void circuit_remove(circuit_t *circ) {
  24. circuit_t *tmpcirc;
  25. assert(circ && global_circuitlist);
  26. if(global_circuitlist == circ) {
  27. global_circuitlist = global_circuitlist->next;
  28. return;
  29. }
  30. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  31. if(tmpcirc->next == circ) {
  32. tmpcirc->next = circ->next;
  33. return;
  34. }
  35. }
  36. }
  37. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  38. circuit_t *circ;
  39. circ = (circuit_t *)malloc(sizeof(circuit_t));
  40. if(!circ)
  41. return NULL;
  42. memset(circ,0,sizeof(circuit_t)); /* zero it out */
  43. circ->p_aci = p_aci;
  44. circ->p_conn = p_conn;
  45. circ->state = CIRCUIT_STATE_ONION_WAIT;
  46. /* ACIs */
  47. circ->p_aci = p_aci;
  48. /* circ->n_aci remains 0 because we haven't identified the next hop yet */
  49. circ->n_receive_circwindow = CIRCWINDOW_START;
  50. circ->p_receive_circwindow = CIRCWINDOW_START;
  51. circuit_add(circ);
  52. return circ;
  53. }
  54. void circuit_free(circuit_t *circ) {
  55. struct data_queue_t *tmpd;
  56. if (circ->n_crypto)
  57. crypto_free_cipher_env(circ->n_crypto);
  58. if (circ->p_crypto)
  59. crypto_free_cipher_env(circ->p_crypto);
  60. if(circ->onion)
  61. free(circ->onion);
  62. if(circ->cpath)
  63. circuit_free_cpath(circ->cpath, circ->cpathlen);
  64. while(circ->data_queue) {
  65. tmpd = circ->data_queue;
  66. circ->data_queue = tmpd->next;
  67. free(tmpd->cell);
  68. free(tmpd);
  69. }
  70. free(circ);
  71. }
  72. void circuit_free_cpath(crypt_path_t **cpath, int cpathlen) {
  73. int i;
  74. for(i=0;i<cpathlen;i++)
  75. free(cpath[i]);
  76. free(cpath);
  77. }
  78. /* return 0 if can't get a unique aci. */
  79. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  80. aci_t test_aci;
  81. connection_t *conn;
  82. try_again:
  83. log(LOG_DEBUG,"get_unique_aci_by_addr_port() trying to get a unique aci");
  84. crypto_pseudo_rand(2, (unsigned char *)&test_aci);
  85. if(aci_type == ACI_TYPE_LOWER && test_aci >= (2<<15))
  86. test_aci -= (2<<15);
  87. if(aci_type == ACI_TYPE_HIGHER && test_aci < (2<<15))
  88. test_aci += (2<<15);
  89. /* if aci_type == ACI_BOTH, don't filter any of it */
  90. if(test_aci == 0)
  91. goto try_again;
  92. conn = connection_exact_get_by_addr_port(addr,port);
  93. if(!conn) /* there can't be a conflict -- no connection of that sort yet */
  94. return test_aci;
  95. if(circuit_get_by_aci_conn(test_aci, conn))
  96. goto try_again;
  97. return test_aci;
  98. }
  99. int circuit_init(circuit_t *circ, int aci_type) {
  100. unsigned char iv[16];
  101. unsigned char digest1[20];
  102. unsigned char digest2[20];
  103. struct timeval start, end;
  104. int time_passed;
  105. assert(circ && circ->onion);
  106. log(LOG_DEBUG,"circuit_init(): starting");
  107. circ->n_port = ntohs(*(uint16_t *)(circ->onion+2));
  108. log(LOG_DEBUG,"circuit_init(): Set port to %u.",circ->n_port);
  109. circ->n_addr = ntohl(*(uint32_t *)(circ->onion+4));
  110. circ->p_f = *(circ->onion+1) >> 4; /* backf */
  111. log(LOG_DEBUG,"circuit_init(): Set BACKF to %u.",circ->p_f);
  112. circ->n_f = *(circ->onion+1) & 0x0f; /* forwf */
  113. log(LOG_DEBUG,"circuit_init(): Set FORWF to %u.",circ->n_f);
  114. circ->state = CIRCUIT_STATE_OPEN;
  115. log(LOG_DEBUG,"circuit_init(): aci_type = %u.",aci_type);
  116. gettimeofday(&start,NULL);
  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. gettimeofday(&end,NULL);
  123. if(end.tv_usec < start.tv_usec) {
  124. end.tv_sec--;
  125. end.tv_usec += 1000000;
  126. }
  127. time_passed = ((end.tv_sec - start.tv_sec)*1000000) + (end.tv_usec - start.tv_usec);
  128. if(time_passed > 1000) { /* more than 1ms */
  129. log(LOG_NOTICE,"circuit_init(): get_unique_aci just took %d us!",time_passed);
  130. }
  131. log(LOG_DEBUG,"circuit_init(): Chosen ACI %u.",circ->n_aci);
  132. /* keys */
  133. memset(iv, 0, 16);
  134. crypto_SHA_digest(circ->onion+12,16,digest1);
  135. crypto_SHA_digest(digest1,20,digest2);
  136. crypto_SHA_digest(digest2,20,digest1);
  137. log(LOG_DEBUG,"circuit_init(): Computed keys.");
  138. if (!(circ->p_crypto = create_onion_cipher(circ->p_f,digest2,iv,1))) {
  139. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  140. return -1;
  141. }
  142. if (!(circ->n_crypto = create_onion_cipher(circ->n_f, digest1, iv, 0))) {
  143. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  144. return -1;
  145. }
  146. log(LOG_DEBUG,"circuit_init(): Cipher initialization complete.");
  147. circ->expire = ntohl(*(uint32_t *)(circ->onion+8));
  148. return 0;
  149. }
  150. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
  151. if(!circ) /* use circ if it's defined, else start from the beginning */
  152. circ = global_circuitlist;
  153. else
  154. circ = circ->next;
  155. for( ; circ; circ = circ->next) {
  156. if(circ->n_addr == naddr && circ->n_port == nport)
  157. return circ;
  158. }
  159. return NULL;
  160. }
  161. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  162. circuit_t *circ;
  163. connection_t *tmpconn;
  164. for(circ=global_circuitlist;circ;circ = circ->next) {
  165. if(circ->p_aci == aci) {
  166. for(tmpconn = circ->p_conn; tmpconn; tmpconn = tmpconn->next_topic) {
  167. if(tmpconn == conn)
  168. return circ;
  169. }
  170. }
  171. if(circ->n_aci == aci) {
  172. for(tmpconn = circ->n_conn; tmpconn; tmpconn = tmpconn->next_topic) {
  173. if(tmpconn == conn)
  174. return circ;
  175. }
  176. }
  177. }
  178. return NULL;
  179. }
  180. circuit_t *circuit_get_by_conn(connection_t *conn) {
  181. circuit_t *circ;
  182. connection_t *tmpconn;
  183. for(circ=global_circuitlist;circ;circ = circ->next) {
  184. for(tmpconn = circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic)
  185. if(tmpconn == conn)
  186. return circ;
  187. for(tmpconn = circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic)
  188. if(tmpconn == conn)
  189. return circ;
  190. }
  191. return NULL;
  192. }
  193. circuit_t *circuit_get_by_edge_type(char edge_type) {
  194. circuit_t *circ;
  195. for(circ=global_circuitlist;circ;circ = circ->next) {
  196. if(edge_type == EDGE_AP && circ->n_conn && circ->n_conn->type == CONN_TYPE_OR) {
  197. log(LOG_DEBUG,"circuit_get_by_edge_type(): Choosing n_aci %d.", circ->n_aci);
  198. return circ;
  199. }
  200. if(edge_type == EDGE_EXIT && circ->p_conn && circ->p_conn->type == CONN_TYPE_OR) {
  201. return circ;
  202. }
  203. log(LOG_DEBUG,"circuit_get_by_edge_type(): Skipping p_aci %d / n_aci %d.", circ->p_aci, circ->n_aci);
  204. }
  205. return NULL;
  206. }
  207. int circuit_deliver_data_cell_from_edge(cell_t *cell, circuit_t *circ, char edge_type) {
  208. int cell_direction;
  209. static int numsent_ap=0, numsent_exit=0;
  210. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): called, edge_type %d.", edge_type);
  211. if(edge_type == EDGE_AP) { /* i'm the AP */
  212. cell_direction = CELL_DIRECTION_OUT;
  213. numsent_ap++;
  214. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): now sent %d data cells from ap", numsent_ap);
  215. if(circ->p_receive_circwindow <= 0) {
  216. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): window 0, queueing for later.");
  217. circ->data_queue = data_queue_add(circ->data_queue, cell);
  218. return 0;
  219. }
  220. circ->p_receive_circwindow--;
  221. } else { /* i'm the exit */
  222. cell_direction = CELL_DIRECTION_IN;
  223. numsent_exit++;
  224. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): now sent %d data cells from exit", numsent_exit);
  225. if(circ->n_receive_circwindow <= 0) {
  226. log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): window 0, queueing for later.");
  227. circ->data_queue = data_queue_add(circ->data_queue, cell);
  228. return 0;
  229. }
  230. circ->n_receive_circwindow--;
  231. }
  232. if(circuit_deliver_data_cell(cell, circ, cell_direction) < 0) {
  233. return -1;
  234. }
  235. circuit_consider_stop_edge_reading(circ, edge_type); /* has window reached 0? */
  236. return 0;
  237. }
  238. int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, int cell_direction) {
  239. connection_t *conn;
  240. assert(cell && circ);
  241. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  242. if(cell_direction == CELL_DIRECTION_OUT)
  243. conn = circ->n_conn;
  244. else
  245. conn = circ->p_conn;
  246. /* first crypt cell->length */
  247. if(circuit_crypt(circ, &(cell->length), 1, cell_direction) < 0) {
  248. log(LOG_DEBUG,"circuit_deliver_data_cell(): length crypt failed. Dropping connection.");
  249. return -1;
  250. }
  251. /* then crypt the payload */
  252. if(circuit_crypt(circ, (char *)&(cell->payload), CELL_PAYLOAD_SIZE, cell_direction) < 0) {
  253. log(LOG_DEBUG,"circuit_deliver_data_cell(): payload crypt failed. Dropping connection.");
  254. return -1;
  255. }
  256. if((!conn && cell_direction == CELL_DIRECTION_OUT) || (conn && conn->type == CONN_TYPE_EXIT)) {
  257. log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to exit.");
  258. return connection_exit_process_data_cell(cell, circ);
  259. }
  260. if((!conn && cell_direction == CELL_DIRECTION_IN) || (conn && conn->type == CONN_TYPE_AP)) {
  261. log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to AP.");
  262. return connection_ap_process_data_cell(cell, circ);
  263. }
  264. /* else send it as a cell */
  265. assert(conn);
  266. //log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to connection.");
  267. return connection_write_cell_to_buf(cell, conn);
  268. }
  269. int circuit_crypt(circuit_t *circ, char *in, int inlen, char cell_direction) {
  270. char *out;
  271. int i;
  272. crypt_path_t *thishop;
  273. assert(circ && in);
  274. out = (char *)malloc(inlen);
  275. if(!out)
  276. return -1;
  277. if(cell_direction == CELL_DIRECTION_IN) { //crypt_type == 'e') {
  278. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  279. for (i=circ->cpathlen-1; i >= 0; i--) /* moving from first to last hop
  280. * Remember : cpath is in reverse order, i.e. last hop first
  281. */
  282. {
  283. thishop = circ->cpath[i];
  284. /* decrypt */
  285. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  286. log(LOG_ERR,"Error performing decryption:%s",crypto_perror());
  287. free(out);
  288. return -1;
  289. }
  290. /* copy ciphertext back to buf */
  291. memcpy(in,out,inlen);
  292. }
  293. } else { /* we're in the middle. Just one crypt. */
  294. if(crypto_cipher_encrypt(circ->p_crypto,in, inlen, out)) {
  295. log(LOG_ERR,"circuit_encrypt(): Encryption failed for ACI : %u (%s).",
  296. circ->p_aci, crypto_perror());
  297. free(out);
  298. return -1;
  299. }
  300. memcpy(in,out,inlen);
  301. }
  302. } else if(cell_direction == CELL_DIRECTION_OUT) { //crypt_type == 'd') {
  303. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  304. for (i=0; i < circ->cpathlen; i++) /* moving from last to first hop
  305. * Remember : cpath is in reverse order, i.e. last hop first
  306. */
  307. {
  308. thishop = circ->cpath[i];
  309. /* encrypt */
  310. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, (unsigned char *)out)) {
  311. log(LOG_ERR,"Error performing encryption:%s",crypto_perror());
  312. free(out);
  313. return -1;
  314. }
  315. /* copy ciphertext back to buf */
  316. memcpy(in,out,inlen);
  317. }
  318. } else { /* we're in the middle. Just one crypt. */
  319. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  320. log(LOG_ERR,"circuit_crypt(): Decryption failed for ACI : %u (%s).",
  321. circ->n_aci, crypto_perror());
  322. free(out);
  323. return -1;
  324. }
  325. memcpy(in,out,inlen);
  326. }
  327. } else {
  328. log(LOG_ERR,"circuit_crypt(): unknown cell direction %d.", cell_direction);
  329. assert(0);
  330. }
  331. free(out);
  332. return 0;
  333. }
  334. void circuit_resume_edge_reading(circuit_t *circ, int edge_type) {
  335. connection_t *conn;
  336. struct data_queue_t *tmpd;
  337. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  338. if(edge_type == EDGE_EXIT)
  339. conn = circ->n_conn;
  340. else
  341. conn = circ->p_conn;
  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->p_receive_circwindow--;
  347. assert(circ->p_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_IN) < 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. for( ; conn; conn=conn->next_topic) {
  368. if((edge_type == EDGE_EXIT && conn->n_receive_topicwindow > 0) ||
  369. (edge_type == EDGE_AP && conn->p_receive_topicwindow > 0)) {
  370. connection_start_reading(conn);
  371. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  372. }
  373. }
  374. circuit_consider_stop_edge_reading(circ, edge_type);
  375. }
  376. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  377. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type) {
  378. connection_t *conn = NULL;
  379. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  380. if(edge_type == EDGE_EXIT && circ->p_receive_circwindow <= 0)
  381. conn = circ->n_conn;
  382. else if(edge_type == EDGE_AP && circ->n_receive_circwindow <= 0)
  383. conn = circ->p_conn;
  384. else
  385. return 0;
  386. for( ; conn; conn=conn->next_topic)
  387. connection_stop_reading(conn);
  388. return 1;
  389. }
  390. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type) {
  391. cell_t sendme;
  392. assert(circ);
  393. sendme.command = CELL_SENDME;
  394. sendme.length = CIRCWINDOW_INCREMENT;
  395. if(edge_type == EDGE_AP) { /* i'm the AP */
  396. if(circ->n_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  397. log(LOG_DEBUG,"circuit_consider_sending_sendme(): Queueing sendme forward.");
  398. circ->n_receive_circwindow += CIRCWINDOW_INCREMENT;
  399. sendme.aci = circ->n_aci;
  400. return connection_write_cell_to_buf(&sendme, circ->n_conn); /* (clobbers sendme) */
  401. }
  402. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  403. if(circ->p_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  404. log(LOG_DEBUG,"circuit_consider_sending_sendme(): Queueing sendme back.");
  405. circ->p_receive_circwindow += CIRCWINDOW_INCREMENT;
  406. sendme.aci = circ->p_aci;
  407. return connection_write_cell_to_buf(&sendme, circ->p_conn); /* (clobbers sendme) */
  408. }
  409. }
  410. return 0;
  411. }
  412. void circuit_close(circuit_t *circ) {
  413. connection_t *conn;
  414. assert(circ);
  415. circuit_remove(circ);
  416. for(conn=circ->n_conn; conn; conn=conn->next_topic) {
  417. connection_send_destroy(circ->n_aci, circ->n_conn);
  418. }
  419. for(conn=circ->p_conn; conn; conn=conn->next_topic) {
  420. connection_send_destroy(circ->p_aci, circ->p_conn);
  421. }
  422. circuit_free(circ);
  423. }
  424. void circuit_about_to_close_connection(connection_t *conn) {
  425. /* send destroys for all circuits using conn */
  426. /* currently, we assume it's too late to flush conn's buf here.
  427. * down the road, maybe we'll consider that eof doesn't mean can't-write
  428. */
  429. circuit_t *circ;
  430. connection_t *prevconn, *tmpconn;
  431. cell_t cell;
  432. int edge_type;
  433. if(!connection_speaks_cells(conn)) {
  434. /* it's an edge conn. need to remove it from the linked list of
  435. * conn's for this circuit. Send an 'end' data topic.
  436. * But don't kill the circuit.
  437. */
  438. circ = circuit_get_by_conn(conn);
  439. if(!circ)
  440. return;
  441. memset(&cell, 0, sizeof(cell_t));
  442. cell.command = CELL_DATA;
  443. cell.length = TOPIC_HEADER_SIZE;
  444. *(uint16_t *)(cell.payload+2) = htons(conn->topic_id);
  445. *cell.payload = TOPIC_COMMAND_END;
  446. if(conn == circ->p_conn) {
  447. circ->p_conn = conn->next_topic;
  448. edge_type = EDGE_AP;
  449. goto send_end;
  450. }
  451. if(conn == circ->n_conn) {
  452. circ->n_conn = conn->next_topic;
  453. edge_type = EDGE_EXIT;
  454. goto send_end;
  455. }
  456. for(prevconn = circ->p_conn; prevconn->next_topic && prevconn->next_topic != conn; prevconn = prevconn->next_topic) ;
  457. if(prevconn->next_topic) {
  458. prevconn->next_topic = conn->next_topic;
  459. edge_type = EDGE_AP;
  460. goto send_end;
  461. }
  462. for(prevconn = circ->n_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. edge_type = EDGE_EXIT;
  466. goto send_end;
  467. }
  468. log(LOG_ERR,"circuit_about_to_close_connection(): edge conn not in circuit's list?");
  469. assert(0); /* should never get here */
  470. send_end:
  471. if(edge_type == EDGE_AP) { /* send to circ->n_conn */
  472. log(LOG_INFO,"circuit_about_to_close_connection(): send data end forward (aci %d).",circ->n_aci);
  473. cell.aci = circ->n_aci;
  474. } else { /* send to circ->p_conn */
  475. assert(edge_type == EDGE_EXIT);
  476. log(LOG_INFO,"circuit_about_to_close_connection(): send data end backward (aci %d).",circ->p_aci);
  477. cell.aci = circ->p_aci;
  478. }
  479. if(circuit_deliver_data_cell_from_edge(&cell, circ, edge_type) < 0) {
  480. log(LOG_DEBUG,"circuit_about_to_close_connection(): circuit_deliver_data_cell_from_edge (%d) failed. Closing.", edge_type);
  481. circuit_close(circ);
  482. }
  483. return;
  484. }
  485. while((circ = circuit_get_by_conn(conn))) {
  486. circuit_remove(circ);
  487. if(circ->n_conn == conn) /* it's closing in front of us */
  488. for(tmpconn=circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  489. connection_send_destroy(circ->p_aci, tmpconn);
  490. }
  491. if(circ->p_conn == conn) /* it's closing behind us */
  492. for(tmpconn=circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  493. connection_send_destroy(circ->n_aci, tmpconn);
  494. }
  495. circuit_free(circ);
  496. }
  497. }
  498. /* FIXME this now leaves some out */
  499. void circuit_dump_by_conn(connection_t *conn) {
  500. circuit_t *circ;
  501. connection_t *tmpconn;
  502. for(circ=global_circuitlist;circ;circ = circ->next) {
  503. for(tmpconn=circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  504. if(tmpconn == conn) {
  505. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  506. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  507. }
  508. }
  509. for(tmpconn=circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  510. if(tmpconn == conn) {
  511. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  512. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  513. }
  514. }
  515. }
  516. }