connection.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /********* START VARIABLES **********/
  6. extern or_options_t options; /* command-line and config-file options */
  7. char *conn_type_to_string[] = {
  8. "", /* 0 */
  9. "OP listener", /* 1 */
  10. "OP", /* 2 */
  11. "OR listener", /* 3 */
  12. "OR", /* 4 */
  13. "Exit", /* 5 */
  14. "App listener",/* 6 */
  15. "App" /* 7 */
  16. };
  17. char *conn_state_to_string[][10] = {
  18. { }, /* no type associated with 0 */
  19. { "ready" }, /* op listener, 0 */
  20. { "awaiting keys", /* op, 0 */
  21. "open", /* 1 */
  22. "close", /* 2 */
  23. "close_wait" }, /* 3 */
  24. { "ready" }, /* or listener, 0 */
  25. { "connecting (as OP)", /* or, 0 */
  26. "sending keys (as OP)", /* 1 */
  27. "connecting (as client)", /* 2 */
  28. "sending auth (as client)", /* 3 */
  29. "waiting for auth (as client)", /* 4 */
  30. "sending nonce (as client)", /* 5 */
  31. "waiting for auth (as server)", /* 6 */
  32. "sending auth (as server)", /* 7 */
  33. "waiting for nonce (as server)",/* 8 */
  34. "open" }, /* 9 */
  35. { "waiting for dest info", /* exit, 0 */
  36. "connecting", /* 1 */
  37. "open" }, /* 2 */
  38. { "ready" }, /* app listener, 0 */
  39. { "awaiting dest info", /* app, 0 */
  40. "waiting for OR connection", /* 1 */
  41. "open" } /* 2 */
  42. };
  43. /********* END VARIABLES ************/
  44. /**************************************************************/
  45. int tv_cmp(struct timeval *a, struct timeval *b) {
  46. if (a->tv_sec > b->tv_sec)
  47. return 1;
  48. if (a->tv_sec < b->tv_sec)
  49. return -1;
  50. if (a->tv_usec > b->tv_usec)
  51. return 1;
  52. if (a->tv_usec < b->tv_usec)
  53. return -1;
  54. return 0;
  55. }
  56. void tv_add(struct timeval *a, struct timeval *b) {
  57. a->tv_usec += b->tv_usec;
  58. a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
  59. a->tv_usec %= 1000000;
  60. }
  61. void tv_addms(struct timeval *a, long ms) {
  62. a->tv_usec += (ms * 1000) % 1000000;
  63. a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
  64. a->tv_usec %= 1000000;
  65. }
  66. /**************************************************************/
  67. connection_t *connection_new(int type) {
  68. connection_t *conn;
  69. conn = (connection_t *)malloc(sizeof(connection_t));
  70. if(!conn)
  71. return NULL;
  72. memset(conn,0,sizeof(connection_t)); /* zero it out to start */
  73. conn->type = type;
  74. if(buf_new(&conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen) < 0 ||
  75. buf_new(&conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen) < 0)
  76. return NULL;
  77. conn->receiver_bucket = 10240; /* should be enough to do the handshake */
  78. conn->bandwidth = conn->receiver_bucket / 10; /* give it a default */
  79. if (connection_speaks_cells(conn)) {
  80. conn->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  81. if (!conn->f_crypto) {
  82. free((void *)conn);
  83. return NULL;
  84. }
  85. conn->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  86. if (!conn->b_crypto) {
  87. crypto_free_cipher_env(conn->f_crypto);
  88. free((void *)conn);
  89. return NULL;
  90. }
  91. }
  92. return conn;
  93. }
  94. void connection_free(connection_t *conn) {
  95. assert(conn);
  96. buf_free(conn->inbuf);
  97. buf_free(conn->outbuf);
  98. if(conn->address)
  99. free(conn->address);
  100. if(conn->dest_addr)
  101. free(conn->dest_addr);
  102. if(connection_speaks_cells(conn)) {
  103. if (conn->f_crypto)
  104. crypto_free_cipher_env(conn->f_crypto);
  105. if (conn->b_crypto)
  106. crypto_free_cipher_env(conn->b_crypto);
  107. }
  108. if(conn->s > 0) {
  109. log(LOG_INFO,"connection_free(): closing fd %d.",conn->s);
  110. close(conn->s);
  111. }
  112. free(conn);
  113. }
  114. int connection_create_listener(crypto_pk_env_t *prkey, struct sockaddr_in *local, int type) {
  115. connection_t *conn;
  116. int s;
  117. int one=1;
  118. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  119. if (s < 0)
  120. {
  121. log(LOG_ERR,"connection_create_listener(): Socket creation failed.");
  122. return -1;
  123. }
  124. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  125. if(bind(s,(struct sockaddr *)local,sizeof(*local)) < 0) {
  126. perror("bind ");
  127. log(LOG_ERR,"Could not bind to local port %u.",ntohs(local->sin_port));
  128. return -1;
  129. }
  130. /* start local server */
  131. if(listen(s,SOMAXCONN) < 0) {
  132. log(LOG_ERR,"Could not listen on local port %u.",ntohs(local->sin_port));
  133. return -1;
  134. }
  135. fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
  136. conn = connection_new(type);
  137. if(!conn) {
  138. log(LOG_DEBUG,"connection_create_listener(): connection_new failed. Giving up.");
  139. return -1;
  140. }
  141. conn->s = s;
  142. if(connection_add(conn) < 0) { /* no space, forget it */
  143. log(LOG_DEBUG,"connection_create_listener(): connection_add failed. Giving up.");
  144. connection_free(conn);
  145. return -1;
  146. }
  147. /* remember things so you can tell the baby sockets */
  148. memcpy(&conn->local,local,sizeof(struct sockaddr_in));
  149. conn->prkey = prkey;
  150. log(LOG_DEBUG,"connection_create_listener(): Listening on local port %u.",ntohs(local->sin_port));
  151. conn->state = LISTENER_STATE_READY;
  152. connection_start_reading(conn);
  153. return 0;
  154. }
  155. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state) {
  156. int news; /* the new socket */
  157. connection_t *newconn;
  158. struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  159. int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */
  160. news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  161. if (news == -1) { /* accept() error */
  162. if(errno==EAGAIN)
  163. return 0; /* he hung up before we could accept(). that's fine. */
  164. /* else there was a real error. */
  165. log(LOG_ERR,"connection_handle_listener_read(): accept() failed. Closing.");
  166. return -1;
  167. }
  168. log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
  169. fcntl(news, F_SETFL, O_NONBLOCK); /* set news to non-blocking */
  170. newconn = connection_new(new_type);
  171. newconn->s = news;
  172. if(!connection_speaks_cells(newconn)) {
  173. newconn->receiver_bucket = -1;
  174. newconn->bandwidth = -1;
  175. }
  176. /* learn things from parent, so we can perform auth */
  177. memcpy(&newconn->local,&conn->local,sizeof(struct sockaddr_in));
  178. newconn->prkey = conn->prkey;
  179. newconn->address = strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
  180. if(connection_add(newconn) < 0) { /* no space, forget it */
  181. connection_free(newconn);
  182. return 0; /* no need to tear down the parent */
  183. }
  184. log(LOG_DEBUG,"connection_handle_listener_read(): socket %d entered state %d.",newconn->s, new_state);
  185. newconn->state = new_state;
  186. connection_start_reading(newconn);
  187. return 0;
  188. }
  189. /* private function, to create the 'local' variable used below */
  190. static int learn_local(struct sockaddr_in *local) {
  191. /* local host information */
  192. char localhostname[512];
  193. struct hostent *localhost;
  194. /* obtain local host information */
  195. if(gethostname(localhostname,512) < 0) {
  196. log(LOG_ERR,"Error obtaining local hostname.");
  197. return -1;
  198. }
  199. log(LOG_DEBUG,"learn_local: localhostname is '%s'.",localhostname);
  200. localhost = gethostbyname(localhostname);
  201. if (!localhost) {
  202. log(LOG_ERR,"Error obtaining local host info.");
  203. return -1;
  204. }
  205. memset((void *)local,0,sizeof(struct sockaddr_in));
  206. local->sin_family = AF_INET;
  207. memcpy((void *)&local->sin_addr,(void *)localhost->h_addr,sizeof(struct in_addr));
  208. log(LOG_DEBUG,"learn_local: chose address as '%s'.",inet_ntoa(local->sin_addr));
  209. return 0;
  210. }
  211. int retry_all_connections(int role, routerinfo_t **router_array, int rarray_len,
  212. crypto_pk_env_t *prkey, uint16_t or_listenport, uint16_t op_listenport, uint16_t ap_listenport) {
  213. /* start all connections that should be up but aren't */
  214. routerinfo_t *router;
  215. int i;
  216. struct sockaddr_in local; /* local address */
  217. if(learn_local(&local) < 0)
  218. return -1;
  219. local.sin_port = htons(or_listenport);
  220. if(role & ROLE_OR_CONNECT_ALL) {
  221. for (i=0;i<rarray_len;i++) {
  222. router = router_array[i];
  223. if(!connection_exact_get_by_addr_port(router->addr,router->or_port)) { /* not in the list */
  224. log(LOG_DEBUG,"retry_all_connections(): connecting to OR %s:%u.",router->address,router->or_port);
  225. connection_or_connect_as_or(router, prkey, &local);
  226. }
  227. }
  228. }
  229. if(role & ROLE_OR_LISTEN) {
  230. if(!connection_get_by_type(CONN_TYPE_OR_LISTENER)) {
  231. connection_or_create_listener(prkey, &local);
  232. }
  233. }
  234. if(role & ROLE_OP_LISTEN) {
  235. local.sin_port = htons(op_listenport);
  236. if(!connection_get_by_type(CONN_TYPE_OP_LISTENER)) {
  237. connection_op_create_listener(prkey, &local);
  238. }
  239. }
  240. if(role & ROLE_AP_LISTEN) {
  241. local.sin_port = htons(ap_listenport);
  242. if(!connection_get_by_type(CONN_TYPE_AP_LISTENER)) {
  243. connection_ap_create_listener(NULL, &local); /* no need to tell it the private key. */
  244. }
  245. }
  246. return 0;
  247. }
  248. connection_t *connection_connect_to_router_as_op(routerinfo_t *router, uint16_t local_or_port) {
  249. struct sockaddr_in local; /* local address */
  250. if(learn_local(&local) < 0)
  251. return NULL;
  252. local.sin_port = htons(local_or_port);
  253. return connection_or_connect_as_op(router, &local);
  254. }
  255. int connection_read_to_buf(connection_t *conn) {
  256. int read_result;
  257. if(connection_speaks_cells(conn)) {
  258. assert(conn->receiver_bucket >= 0);
  259. }
  260. if(!connection_speaks_cells(conn)) {
  261. assert(conn->receiver_bucket < 0);
  262. }
  263. read_result = read_to_buf(conn->s, conn->receiver_bucket, &conn->inbuf, &conn->inbuflen,
  264. &conn->inbuf_datalen, &conn->inbuf_reached_eof);
  265. // log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);
  266. if(read_result >= 0 && connection_speaks_cells(conn)) {
  267. conn->receiver_bucket -= read_result;
  268. if(conn->receiver_bucket <= 0) {
  269. // log(LOG_DEBUG,"connection_read_to_buf() stopping reading, receiver bucket full.");
  270. connection_stop_reading(conn);
  271. /* If we're not in 'open' state here, then we're never going to finish the
  272. * handshake, because we'll never increment the receiver_bucket. But we
  273. * can't check for that here, because the buf we just read might have enough
  274. * on it to finish the handshake. So we check for that in check_conn_read().
  275. */
  276. }
  277. }
  278. return read_result;
  279. }
  280. int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
  281. return fetch_from_buf(string, len, &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
  282. }
  283. int connection_wants_to_flush(connection_t *conn) {
  284. return conn->outbuf_flushlen;
  285. }
  286. int connection_outbuf_too_full(connection_t *conn) {
  287. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  288. }
  289. int connection_flush_buf(connection_t *conn) {
  290. return flush_buf(conn->s, &conn->outbuf, &conn->outbuflen, &conn->outbuf_flushlen, &conn->outbuf_datalen);
  291. }
  292. int connection_write_to_buf(char *string, int len, connection_t *conn) {
  293. if(!len)
  294. return 0;
  295. if( (!connection_speaks_cells(conn)) ||
  296. (!connection_state_is_open(conn)) ||
  297. (options.LinkPadding == 0) ) {
  298. /* connection types other than or and op, or or/op not in 'open' state, should flush immediately */
  299. /* also flush immediately if we're not doing LinkPadding, since otherwise it will never flush */
  300. connection_start_writing(conn);
  301. conn->outbuf_flushlen += len;
  302. }
  303. return write_to_buf(string, len, &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  304. }
  305. int connection_receiver_bucket_should_increase(connection_t *conn) {
  306. assert(conn);
  307. if(!connection_speaks_cells(conn))
  308. return 0; /* edge connections don't use receiver_buckets */
  309. if(conn->receiver_bucket > 10*conn->bandwidth)
  310. return 0;
  311. return 1;
  312. }
  313. void connection_increment_receiver_bucket (connection_t *conn) {
  314. assert(conn);
  315. if(connection_receiver_bucket_should_increase(conn)) {
  316. /* yes, the receiver_bucket can become overfull here. But not by much. */
  317. conn->receiver_bucket += conn->bandwidth*1.1;
  318. if(connection_state_is_open(conn)) {
  319. /* if we're in state 'open', then start reading again */
  320. connection_start_reading(conn);
  321. }
  322. }
  323. }
  324. int connection_speaks_cells(connection_t *conn) {
  325. assert(conn);
  326. if(conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_OP)
  327. return 1;
  328. return 0;
  329. }
  330. int connection_is_listener(connection_t *conn) {
  331. if(conn->type == CONN_TYPE_OP_LISTENER ||
  332. conn->type == CONN_TYPE_OR_LISTENER ||
  333. conn->type == CONN_TYPE_AP_LISTENER)
  334. return 1;
  335. return 0;
  336. }
  337. int connection_state_is_open(connection_t *conn) {
  338. assert(conn);
  339. if((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  340. (conn->type == CONN_TYPE_OP && conn->state == OP_CONN_STATE_OPEN) ||
  341. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  342. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN))
  343. return 1;
  344. return 0;
  345. }
  346. void connection_send_cell(connection_t *conn) {
  347. cell_t cell;
  348. int bytes_in_full_flushlen;
  349. /* this function only gets called if options.LinkPadding is 1 */
  350. assert(options.LinkPadding == 1);
  351. assert(conn);
  352. if(!connection_speaks_cells(conn)) {
  353. /* this conn doesn't speak cells. do nothing. */
  354. return;
  355. }
  356. if(!connection_state_is_open(conn)) {
  357. /* it's not in 'open' state, all data should already be waiting to be flushed */
  358. assert(conn->outbuf_datalen == conn->outbuf_flushlen);
  359. return;
  360. }
  361. #if 0 /* use to send evenly spaced cells, but not padding */
  362. if(conn->outbuf_datalen - conn->outbuf_flushlen >= sizeof(cell_t)) {
  363. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  364. connection_start_writing(conn);
  365. }
  366. #endif
  367. connection_increment_send_timeval(conn); /* update when we'll send the next cell */
  368. bytes_in_full_flushlen = conn->bandwidth / 100; /* 10ms worth */
  369. if(bytes_in_full_flushlen < 10*sizeof(cell_t))
  370. bytes_in_full_flushlen = 10*sizeof(cell_t); /* but at least 10 cells worth */
  371. if(conn->outbuf_flushlen > bytes_in_full_flushlen - sizeof(cell_t)) {
  372. /* if we would exceed bytes_in_full_flushlen by adding a new cell */
  373. return;
  374. }
  375. if(conn->outbuf_datalen - conn->outbuf_flushlen < sizeof(cell_t)) {
  376. /* we need to queue a padding cell first */
  377. memset(&cell,0,sizeof(cell_t));
  378. cell.command = CELL_PADDING;
  379. connection_write_cell_to_buf(&cell, conn);
  380. }
  381. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  382. connection_start_writing(conn);
  383. }
  384. void connection_increment_send_timeval(connection_t *conn) {
  385. /* add "1000000 * sizeof(cell_t) / conn->bandwidth" microseconds to conn->send_timeval */
  386. /* FIXME should perhaps use ceil() of this. For now I simply add 1. */
  387. tv_addms(&conn->send_timeval, 1+1000 * sizeof(cell_t) / conn->bandwidth);
  388. }
  389. void connection_init_timeval(connection_t *conn) {
  390. assert(conn);
  391. if(gettimeofday(&conn->send_timeval,NULL) < 0)
  392. return;
  393. connection_increment_send_timeval(conn);
  394. }
  395. int connection_send_destroy(aci_t aci, connection_t *conn) {
  396. cell_t cell;
  397. assert(conn);
  398. if(!connection_speaks_cells(conn)) {
  399. log(LOG_INFO,"connection_send_destroy(): Aci %d: At an edge. Marking connection for close.", aci);
  400. conn->marked_for_close = 1;
  401. return 0;
  402. }
  403. cell.aci = aci;
  404. cell.command = CELL_DESTROY;
  405. log(LOG_INFO,"connection_send_destroy(): Sending destroy (aci %d).",aci);
  406. return connection_write_cell_to_buf(&cell, conn);
  407. }
  408. int connection_send_connected(aci_t aci, connection_t *conn) {
  409. cell_t cell;
  410. assert(conn);
  411. if(!connection_speaks_cells(conn)) {
  412. log(LOG_INFO,"connection_send_connected(): Aci %d: At entry point. Notifying proxy.", aci);
  413. connection_ap_send_connected(conn);
  414. return 0;
  415. }
  416. cell.aci = aci;
  417. cell.command = CELL_CONNECTED;
  418. log(LOG_INFO,"connection_send_connected(): passing back cell (aci %d).",aci);
  419. return connection_write_cell_to_buf(&cell, conn);
  420. }
  421. int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn) {
  422. if(connection_encrypt_cell(cellp,conn)<0) {
  423. return -1;
  424. }
  425. return connection_write_to_buf((char *)cellp, sizeof(cell_t), conn);
  426. }
  427. int connection_encrypt_cell(cell_t *cellp, connection_t *conn) {
  428. cell_t newcell;
  429. #if 0
  430. int x;
  431. char *px;
  432. printf("Sending: Cell header plaintext: ");
  433. px = (char *)cellp;
  434. for(x=0;x<8;x++) {
  435. printf("%u ",px[x]);
  436. }
  437. printf("\n");
  438. #endif
  439. if(crypto_cipher_encrypt(conn->f_crypto, (char *)cellp, sizeof(cell_t), (char *)&newcell)) {
  440. log(LOG_ERR,"Could not encrypt cell for connection %s:%u.",conn->address,conn->port);
  441. return -1;
  442. }
  443. #if 0
  444. printf("Sending: Cell header crypttext: ");
  445. px = (char *)&newcell;
  446. for(x=0;x<8;x++) {
  447. printf("%u ",px[x]);
  448. }
  449. printf("\n");
  450. #endif
  451. memcpy(cellp,&newcell,sizeof(cell_t));
  452. return 0;
  453. }
  454. int connection_process_inbuf(connection_t *conn) {
  455. assert(conn);
  456. switch(conn->type) {
  457. case CONN_TYPE_OP:
  458. return connection_op_process_inbuf(conn);
  459. case CONN_TYPE_OR:
  460. return connection_or_process_inbuf(conn);
  461. case CONN_TYPE_EXIT:
  462. return connection_exit_process_inbuf(conn);
  463. case CONN_TYPE_AP:
  464. return connection_ap_process_inbuf(conn);
  465. default:
  466. log(LOG_DEBUG,"connection_process_inbuf() got unexpected conn->type.");
  467. return -1;
  468. }
  469. }
  470. int connection_package_raw_inbuf(connection_t *conn) {
  471. int amount_to_process;
  472. cell_t cell;
  473. circuit_t *circ;
  474. assert(conn);
  475. assert(!connection_speaks_cells(conn));
  476. /* this function should never get called if the receiver_window is 0 */
  477. amount_to_process = conn->inbuf_datalen;
  478. if(!amount_to_process)
  479. return 0;
  480. if(amount_to_process > CELL_PAYLOAD_SIZE) {
  481. cell.length = CELL_PAYLOAD_SIZE;
  482. } else {
  483. cell.length = amount_to_process;
  484. }
  485. if(connection_fetch_from_buf(cell.payload, cell.length, conn) < 0)
  486. return -1;
  487. circ = circuit_get_by_conn(conn);
  488. if(!circ) {
  489. log(LOG_DEBUG,"connection_raw_package_inbuf(): conn has no circuits!");
  490. return -1;
  491. }
  492. log(LOG_DEBUG,"connection_raw_package_inbuf(): Packaging %d bytes.",cell.length);
  493. if(circ->n_conn == conn) { /* send it backward. we're an exit. */
  494. cell.aci = circ->p_aci;
  495. cell.command = CELL_DATA;
  496. if(circuit_deliver_data_cell(&cell, circ, circ->p_conn, 'e') < 0) {
  497. log(LOG_DEBUG,"connection_raw_package_inbuf(): circuit_deliver_data_cell (backward) failed. Closing.");
  498. circuit_close(circ);
  499. return 0;
  500. }
  501. assert(circ->n_receive_window > 0);
  502. if(--circ->n_receive_window <= 0) { /* is it 0 after decrement? */
  503. connection_stop_reading(circ->n_conn);
  504. log(LOG_DEBUG,"connection_raw_package_inbuf(): receive_window at exit reached 0.");
  505. return 0; /* don't process the inbuf any more */
  506. }
  507. log(LOG_DEBUG,"connection_raw_package_inbuf(): receive_window at exit is %d",circ->n_receive_window);
  508. } else { /* send it forward. we're an AP */
  509. cell.aci = circ->n_aci;
  510. cell.command = CELL_DATA;
  511. if(circuit_deliver_data_cell(&cell, circ, circ->n_conn, 'e') < 0) {
  512. /* yes, we use 'e' here, because the AP connection must *encrypt* its input. */
  513. log(LOG_DEBUG,"connection_raw_package_inbuf(): circuit_deliver_data_cell (forward) failed. Closing.");
  514. circuit_close(circ);
  515. return 0;
  516. }
  517. assert(circ->p_receive_window > 0);
  518. if(--circ->p_receive_window <= 0) { /* is it 0 after decrement? */
  519. connection_stop_reading(circ->p_conn);
  520. log(LOG_DEBUG,"connection_raw_package_inbuf(): receive_window at AP reached 0.");
  521. return 0; /* don't process the inbuf any more */
  522. }
  523. log(LOG_DEBUG,"connection_raw_package_inbuf(): receive_window at AP is %d",circ->p_receive_window);
  524. }
  525. if(amount_to_process > CELL_PAYLOAD_SIZE)
  526. log(LOG_DEBUG,"connection_raw_package_inbuf(): recursing.");
  527. return connection_package_raw_inbuf(conn);
  528. return 0;
  529. }
  530. int connection_consider_sending_sendme(connection_t *conn) {
  531. circuit_t *circ;
  532. cell_t sendme;
  533. if(connection_outbuf_too_full(conn))
  534. return 0;
  535. circ = circuit_get_by_conn(conn);
  536. if(!circ) {
  537. /* this can legitimately happen if the destroy has already arrived and torn down the circuit */
  538. log(LOG_DEBUG,"connection_consider_sending_sendme(): No circuit associated with conn. Skipping.");
  539. return 0;
  540. }
  541. sendme.command = CELL_SENDME;
  542. sendme.length = RECEIVE_WINDOW_INCREMENT;
  543. if(circ->n_conn == conn) { /* we're at an exit */
  544. if(circ->p_receive_window < RECEIVE_WINDOW_START-RECEIVE_WINDOW_INCREMENT) {
  545. log(LOG_DEBUG,"connection_consider_sending_sendme(): Outbuf %d, Queueing sendme back.", conn->outbuf_flushlen);
  546. circ->p_receive_window += RECEIVE_WINDOW_INCREMENT;
  547. sendme.aci = circ->p_aci;
  548. return connection_write_cell_to_buf(&sendme, circ->p_conn); /* (clobbers sendme) */
  549. }
  550. } else { /* we're at an AP */
  551. if(circ->n_receive_window < RECEIVE_WINDOW_START-RECEIVE_WINDOW_INCREMENT) {
  552. log(LOG_DEBUG,"connection_consider_sending_sendme(): Outbuf %d, Queueing sendme forward.", conn->outbuf_flushlen);
  553. circ->n_receive_window += RECEIVE_WINDOW_INCREMENT;
  554. sendme.aci = circ->n_aci;
  555. return connection_write_cell_to_buf(&sendme, circ->n_conn); /* (clobbers sendme) */
  556. }
  557. }
  558. return 0;
  559. }
  560. int connection_finished_flushing(connection_t *conn) {
  561. assert(conn);
  562. // log(LOG_DEBUG,"connection_finished_flushing() entered. Socket %u.", conn->s);
  563. switch(conn->type) {
  564. case CONN_TYPE_AP:
  565. return connection_ap_finished_flushing(conn);
  566. case CONN_TYPE_OP:
  567. return connection_op_finished_flushing(conn);
  568. case CONN_TYPE_OR:
  569. return connection_or_finished_flushing(conn);
  570. case CONN_TYPE_EXIT:
  571. return connection_exit_finished_flushing(conn);
  572. default:
  573. log(LOG_DEBUG,"connection_finished_flushing() got unexpected conn->type.");
  574. return -1;
  575. }
  576. }
  577. int connection_process_cell_from_inbuf(connection_t *conn) {
  578. /* check if there's a whole cell there.
  579. * if yes, pull it off, decrypt it, and process it.
  580. */
  581. char crypted[128];
  582. char outbuf[1024];
  583. // int x;
  584. cell_t *cellp;
  585. if(conn->inbuf_datalen < 128) /* entire response available? */
  586. return 0; /* not yet */
  587. if(connection_fetch_from_buf(crypted,128,conn) < 0) {
  588. return -1;
  589. }
  590. #if 0
  591. printf("Cell header crypttext: ");
  592. for(x=0;x<8;x++) {
  593. printf("%u ",crypted[x]);
  594. }
  595. printf("\n");
  596. #endif
  597. /* decrypt */
  598. if(crypto_cipher_decrypt(conn->b_crypto,crypted,sizeof(cell_t),(unsigned char *)outbuf)) {
  599. log(LOG_ERR,"connection_process_cell_from_inbuf(): Decryption failed, dropping.");
  600. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  601. }
  602. // log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Cell decrypted (%d bytes).",outlen);
  603. #if 0
  604. printf("Cell header plaintext: ");
  605. for(x=0;x<8;x++) {
  606. printf("%u ",outbuf[x]);
  607. }
  608. printf("\n");
  609. #endif
  610. /* copy the rest of the cell */
  611. // memcpy((char *)outbuf+8, (char *)crypted+8, sizeof(cell_t)-8);
  612. cellp = (cell_t *)outbuf;
  613. // log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci);
  614. command_process_cell(cellp, conn);
  615. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  616. }