connection.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. "Dir listener",/* 8 */
  17. "Dir", /* 9 */
  18. };
  19. char *conn_state_to_string[][15] = {
  20. { }, /* no type associated with 0 */
  21. { "ready" }, /* op listener, 0 */
  22. { "awaiting keys", /* op, 0 */
  23. "open", /* 1 */
  24. "close", /* 2 */
  25. "close_wait" }, /* 3 */
  26. { "ready" }, /* or listener, 0 */
  27. { "connecting (as OP)", /* or, 0 */
  28. "sending keys (as OP)", /* 1 */
  29. "connecting (as client)", /* 2 */
  30. "sending auth (as client)", /* 3 */
  31. "waiting for auth (as client)", /* 4 */
  32. "sending nonce (as client)", /* 5 */
  33. "waiting for auth (as server)", /* 6 */
  34. "sending auth (as server)", /* 7 */
  35. "waiting for nonce (as server)",/* 8 */
  36. "open" }, /* 9 */
  37. { "waiting for dest info", /* exit, 0 */
  38. "connecting", /* 1 */
  39. "open" }, /* 2 */
  40. { "ready" }, /* app listener, 0 */
  41. { "awaiting dest info", /* app, 0 */
  42. "waiting for OR connection", /* 1 */
  43. "open" }, /* 2 */
  44. { "ready" }, /* dir listener, 0 */
  45. { "connecting", /* 0 */
  46. "sending command", /* 1 */
  47. "reading", /* 2 */
  48. "awaiting command", /* 3 */
  49. "writing" }, /* 4 */
  50. };
  51. /********* END VARIABLES ************/
  52. /**************************************************************/
  53. int tv_cmp(struct timeval *a, struct timeval *b) {
  54. if (a->tv_sec > b->tv_sec)
  55. return 1;
  56. if (a->tv_sec < b->tv_sec)
  57. return -1;
  58. if (a->tv_usec > b->tv_usec)
  59. return 1;
  60. if (a->tv_usec < b->tv_usec)
  61. return -1;
  62. return 0;
  63. }
  64. void tv_add(struct timeval *a, struct timeval *b) {
  65. a->tv_usec += b->tv_usec;
  66. a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
  67. a->tv_usec %= 1000000;
  68. }
  69. void tv_addms(struct timeval *a, long ms) {
  70. a->tv_usec += (ms * 1000) % 1000000;
  71. a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
  72. a->tv_usec %= 1000000;
  73. }
  74. /**************************************************************/
  75. connection_t *connection_new(int type) {
  76. connection_t *conn;
  77. struct timeval now;
  78. if(gettimeofday(&now,NULL) < 0)
  79. return NULL;
  80. conn = (connection_t *)malloc(sizeof(connection_t));
  81. if(!conn)
  82. return NULL;
  83. memset(conn,0,sizeof(connection_t)); /* zero it out to start */
  84. conn->type = type;
  85. if(buf_new(&conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen) < 0 ||
  86. buf_new(&conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen) < 0)
  87. return NULL;
  88. conn->receiver_bucket = 10240; /* should be enough to do the handshake */
  89. conn->bandwidth = conn->receiver_bucket / 10; /* give it a default */
  90. conn->timestamp_created = now.tv_sec;
  91. if (connection_speaks_cells(conn)) {
  92. conn->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  93. if (!conn->f_crypto) {
  94. free((void *)conn);
  95. return NULL;
  96. }
  97. conn->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  98. if (!conn->b_crypto) {
  99. crypto_free_cipher_env(conn->f_crypto);
  100. free((void *)conn);
  101. return NULL;
  102. }
  103. }
  104. return conn;
  105. }
  106. void connection_free(connection_t *conn) {
  107. assert(conn);
  108. buf_free(conn->inbuf);
  109. buf_free(conn->outbuf);
  110. if(conn->address)
  111. free(conn->address);
  112. if(conn->dest_addr)
  113. free(conn->dest_addr);
  114. if(connection_speaks_cells(conn)) {
  115. if (conn->f_crypto)
  116. crypto_free_cipher_env(conn->f_crypto);
  117. if (conn->b_crypto)
  118. crypto_free_cipher_env(conn->b_crypto);
  119. }
  120. if (conn->pkey)
  121. crypto_free_pk_env(conn->pkey);
  122. if(conn->s > 0) {
  123. log(LOG_INFO,"connection_free(): closing fd %d.",conn->s);
  124. close(conn->s);
  125. }
  126. free(conn);
  127. }
  128. int connection_create_listener(struct sockaddr_in *bindaddr, int type) {
  129. connection_t *conn;
  130. int s;
  131. int one=1;
  132. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  133. if (s < 0)
  134. {
  135. log(LOG_ERR,"connection_create_listener(): Socket creation failed.");
  136. return -1;
  137. }
  138. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  139. if(bind(s,(struct sockaddr *)bindaddr,sizeof(*bindaddr)) < 0) {
  140. perror("bind ");
  141. log(LOG_ERR,"Could not bind to port %u.",ntohs(bindaddr->sin_port));
  142. return -1;
  143. }
  144. if(listen(s,SOMAXCONN) < 0) {
  145. log(LOG_ERR,"Could not listen on port %u.",ntohs(bindaddr->sin_port));
  146. return -1;
  147. }
  148. fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
  149. conn = connection_new(type);
  150. if(!conn) {
  151. log(LOG_DEBUG,"connection_create_listener(): connection_new failed. Giving up.");
  152. return -1;
  153. }
  154. conn->s = s;
  155. if(connection_add(conn) < 0) { /* no space, forget it */
  156. log(LOG_DEBUG,"connection_create_listener(): connection_add failed. Giving up.");
  157. connection_free(conn);
  158. return -1;
  159. }
  160. log(LOG_DEBUG,"connection_create_listener(): Listening on port %u.",ntohs(bindaddr->sin_port));
  161. conn->state = LISTENER_STATE_READY;
  162. connection_start_reading(conn);
  163. return 0;
  164. }
  165. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state) {
  166. int news; /* the new socket */
  167. connection_t *newconn;
  168. struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  169. int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */
  170. news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  171. if (news == -1) { /* accept() error */
  172. if(errno==EAGAIN)
  173. return 0; /* he hung up before we could accept(). that's fine. */
  174. /* else there was a real error. */
  175. log(LOG_ERR,"connection_handle_listener_read(): accept() failed. Closing.");
  176. return -1;
  177. }
  178. log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
  179. fcntl(news, F_SETFL, O_NONBLOCK); /* set news to non-blocking */
  180. newconn = connection_new(new_type);
  181. newconn->s = news;
  182. if(!connection_speaks_cells(newconn)) {
  183. newconn->receiver_bucket = -1;
  184. newconn->bandwidth = -1;
  185. }
  186. newconn->address = strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
  187. if(connection_add(newconn) < 0) { /* no space, forget it */
  188. connection_free(newconn);
  189. return 0; /* no need to tear down the parent */
  190. }
  191. log(LOG_DEBUG,"connection_handle_listener_read(): socket %d entered state %d.",newconn->s, new_state);
  192. newconn->state = new_state;
  193. connection_start_reading(newconn);
  194. return 0;
  195. }
  196. int retry_all_connections(int role, uint16_t or_listenport,
  197. uint16_t op_listenport, uint16_t ap_listenport, uint16_t dir_listenport) {
  198. /* start all connections that should be up but aren't */
  199. struct sockaddr_in bindaddr; /* where to bind */
  200. if(role & ROLE_OR_CONNECT_ALL) {
  201. router_retry_connections();
  202. }
  203. memset(&bindaddr,0,sizeof(struct sockaddr_in));
  204. bindaddr.sin_family = AF_INET;
  205. bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* anyone can connect */
  206. if(role & ROLE_OR_LISTEN) {
  207. bindaddr.sin_port = htons(or_listenport);
  208. if(!connection_get_by_type(CONN_TYPE_OR_LISTENER)) {
  209. connection_or_create_listener(&bindaddr);
  210. }
  211. }
  212. if(role & ROLE_OP_LISTEN) {
  213. bindaddr.sin_port = htons(op_listenport);
  214. if(!connection_get_by_type(CONN_TYPE_OP_LISTENER)) {
  215. connection_op_create_listener(&bindaddr);
  216. }
  217. }
  218. if(role & ROLE_DIR_LISTEN) {
  219. bindaddr.sin_port = htons(dir_listenport);
  220. if(!connection_get_by_type(CONN_TYPE_DIR_LISTENER)) {
  221. connection_dir_create_listener(&bindaddr);
  222. }
  223. }
  224. if(role & ROLE_AP_LISTEN) {
  225. bindaddr.sin_port = htons(ap_listenport);
  226. inet_aton("127.0.0.1", &(bindaddr.sin_addr)); /* the AP listens only on localhost! */
  227. if(!connection_get_by_type(CONN_TYPE_AP_LISTENER)) {
  228. connection_ap_create_listener(&bindaddr);
  229. }
  230. }
  231. return 0;
  232. }
  233. int connection_read_to_buf(connection_t *conn) {
  234. int read_result;
  235. struct timeval now;
  236. if(connection_speaks_cells(conn)) {
  237. assert(conn->receiver_bucket >= 0);
  238. }
  239. if(!connection_speaks_cells(conn)) {
  240. assert(conn->receiver_bucket < 0);
  241. }
  242. if(gettimeofday(&now,NULL) < 0)
  243. return -1;
  244. conn->timestamp_lastread = now.tv_sec;
  245. read_result = read_to_buf(conn->s, conn->receiver_bucket, &conn->inbuf, &conn->inbuflen,
  246. &conn->inbuf_datalen, &conn->inbuf_reached_eof);
  247. // log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);
  248. if(read_result >= 0 && connection_speaks_cells(conn)) {
  249. conn->receiver_bucket -= read_result;
  250. if(conn->receiver_bucket <= 0) {
  251. // log(LOG_DEBUG,"connection_read_to_buf() stopping reading, receiver bucket full.");
  252. connection_stop_reading(conn);
  253. /* If we're not in 'open' state here, then we're never going to finish the
  254. * handshake, because we'll never increment the receiver_bucket. But we
  255. * can't check for that here, because the buf we just read might have enough
  256. * on it to finish the handshake. So we check for that in check_conn_read().
  257. */
  258. }
  259. }
  260. return read_result;
  261. }
  262. int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
  263. return fetch_from_buf(string, len, &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
  264. }
  265. int connection_find_on_inbuf(char *string, int len, connection_t *conn) {
  266. return find_on_inbuf(string, len, conn->inbuf, conn->inbuf_datalen);
  267. }
  268. int connection_wants_to_flush(connection_t *conn) {
  269. return conn->outbuf_flushlen;
  270. }
  271. int connection_outbuf_too_full(connection_t *conn) {
  272. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  273. }
  274. int connection_flush_buf(connection_t *conn) {
  275. return flush_buf(conn->s, &conn->outbuf, &conn->outbuflen, &conn->outbuf_flushlen, &conn->outbuf_datalen);
  276. }
  277. int connection_write_to_buf(char *string, int len, connection_t *conn) {
  278. struct timeval now;
  279. if(gettimeofday(&now,NULL) < 0)
  280. return -1;
  281. if(!len)
  282. return 0;
  283. conn->timestamp_lastwritten = now.tv_sec;
  284. if( (!connection_speaks_cells(conn)) ||
  285. (!connection_state_is_open(conn)) ||
  286. (options.LinkPadding == 0) ) {
  287. /* connection types other than or and op, or or/op not in 'open' state, should flush immediately */
  288. /* also flush immediately if we're not doing LinkPadding, since otherwise it will never flush */
  289. connection_start_writing(conn);
  290. conn->outbuf_flushlen += len;
  291. }
  292. return write_to_buf(string, len, &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  293. }
  294. int connection_receiver_bucket_should_increase(connection_t *conn) {
  295. assert(conn);
  296. if(!connection_speaks_cells(conn))
  297. return 0; /* edge connections don't use receiver_buckets */
  298. if(conn->receiver_bucket > 10*conn->bandwidth)
  299. return 0;
  300. return 1;
  301. }
  302. void connection_increment_receiver_bucket (connection_t *conn) {
  303. assert(conn);
  304. if(connection_receiver_bucket_should_increase(conn)) {
  305. /* yes, the receiver_bucket can become overfull here. But not by much. */
  306. conn->receiver_bucket += conn->bandwidth*1.1;
  307. if(connection_state_is_open(conn)) {
  308. /* if we're in state 'open', then start reading again */
  309. connection_start_reading(conn);
  310. }
  311. }
  312. }
  313. int connection_speaks_cells(connection_t *conn) {
  314. assert(conn);
  315. if(conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_OP)
  316. return 1;
  317. return 0;
  318. }
  319. int connection_is_listener(connection_t *conn) {
  320. if(conn->type == CONN_TYPE_OP_LISTENER ||
  321. conn->type == CONN_TYPE_OR_LISTENER ||
  322. conn->type == CONN_TYPE_AP_LISTENER ||
  323. conn->type == CONN_TYPE_DIR_LISTENER)
  324. return 1;
  325. return 0;
  326. }
  327. int connection_state_is_open(connection_t *conn) {
  328. assert(conn);
  329. if((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  330. (conn->type == CONN_TYPE_OP && conn->state == OP_CONN_STATE_OPEN) ||
  331. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  332. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN))
  333. return 1;
  334. return 0;
  335. }
  336. void connection_send_cell(connection_t *conn) {
  337. cell_t cell;
  338. int bytes_in_full_flushlen;
  339. /* this function only gets called if options.LinkPadding is 1 */
  340. assert(options.LinkPadding == 1);
  341. assert(conn);
  342. if(!connection_speaks_cells(conn)) {
  343. /* this conn doesn't speak cells. do nothing. */
  344. return;
  345. }
  346. if(!connection_state_is_open(conn)) {
  347. /* it's not in 'open' state, all data should already be waiting to be flushed */
  348. assert(conn->outbuf_datalen == conn->outbuf_flushlen);
  349. return;
  350. }
  351. #if 0 /* use to send evenly spaced cells, but not padding */
  352. if(conn->outbuf_datalen - conn->outbuf_flushlen >= sizeof(cell_t)) {
  353. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  354. connection_start_writing(conn);
  355. }
  356. #endif
  357. connection_increment_send_timeval(conn); /* update when we'll send the next cell */
  358. bytes_in_full_flushlen = conn->bandwidth / 100; /* 10ms worth */
  359. if(bytes_in_full_flushlen < 10*sizeof(cell_t))
  360. bytes_in_full_flushlen = 10*sizeof(cell_t); /* but at least 10 cells worth */
  361. if(conn->outbuf_flushlen > bytes_in_full_flushlen - sizeof(cell_t)) {
  362. /* if we would exceed bytes_in_full_flushlen by adding a new cell */
  363. return;
  364. }
  365. if(conn->outbuf_datalen - conn->outbuf_flushlen < sizeof(cell_t)) {
  366. /* we need to queue a padding cell first */
  367. memset(&cell,0,sizeof(cell_t));
  368. cell.command = CELL_PADDING;
  369. connection_write_cell_to_buf(&cell, conn);
  370. }
  371. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  372. connection_start_writing(conn);
  373. }
  374. void connection_increment_send_timeval(connection_t *conn) {
  375. /* add "1000000 * sizeof(cell_t) / conn->bandwidth" microseconds to conn->send_timeval */
  376. /* FIXME should perhaps use ceil() of this. For now I simply add 1. */
  377. tv_addms(&conn->send_timeval, 1+1000 * sizeof(cell_t) / conn->bandwidth);
  378. }
  379. void connection_init_timeval(connection_t *conn) {
  380. assert(conn);
  381. if(gettimeofday(&conn->send_timeval,NULL) < 0)
  382. return;
  383. connection_increment_send_timeval(conn);
  384. }
  385. int connection_send_destroy(aci_t aci, connection_t *conn) {
  386. cell_t cell;
  387. assert(conn);
  388. if(!connection_speaks_cells(conn)) {
  389. log(LOG_INFO,"connection_send_destroy(): Aci %d: At an edge. Marking connection for close.", aci);
  390. conn->marked_for_close = 1;
  391. return 0;
  392. }
  393. cell.aci = aci;
  394. cell.command = CELL_DESTROY;
  395. log(LOG_INFO,"connection_send_destroy(): Sending destroy (aci %d).",aci);
  396. return connection_write_cell_to_buf(&cell, conn);
  397. }
  398. int connection_send_connected(aci_t aci, connection_t *conn) {
  399. cell_t cell;
  400. assert(conn);
  401. if(!connection_speaks_cells(conn)) {
  402. log(LOG_INFO,"connection_send_connected(): Aci %d: At entry point. Notifying proxy.", aci);
  403. connection_ap_send_connected(conn);
  404. return 0;
  405. }
  406. cell.aci = aci;
  407. cell.command = CELL_CONNECTED;
  408. log(LOG_INFO,"connection_send_connected(): passing back cell (aci %d).",aci);
  409. return connection_write_cell_to_buf(&cell, conn);
  410. }
  411. int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn) {
  412. char networkcell[CELL_NETWORK_SIZE];
  413. char *n = networkcell;
  414. memset(n,0,CELL_NETWORK_SIZE); /* zero it out to start */
  415. *(aci_t *)n = htons(cellp->aci);
  416. *(n+2) = cellp->command;
  417. *(n+3) = cellp->length;
  418. /* seq is reserved, leave zero */
  419. memcpy(n+8,cellp->payload,CELL_PAYLOAD_SIZE);
  420. if(connection_encrypt_cell(n,conn)<0) {
  421. return -1;
  422. }
  423. return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  424. }
  425. int connection_encrypt_cell(char *cellp, connection_t *conn) {
  426. char cryptcell[CELL_NETWORK_SIZE];
  427. #if 0
  428. int x;
  429. char *px;
  430. printf("Sending: Cell header plaintext: ");
  431. px = (char *)cellp;
  432. for(x=0;x<8;x++) {
  433. printf("%u ",px[x]);
  434. }
  435. printf("\n");
  436. #endif
  437. if(crypto_cipher_encrypt(conn->f_crypto, cellp, CELL_NETWORK_SIZE, cryptcell)) {
  438. log(LOG_ERR,"Could not encrypt cell for connection %s:%u.",conn->address,conn->port);
  439. return -1;
  440. }
  441. #if 0
  442. printf("Sending: Cell header crypttext: ");
  443. px = (char *)&newcell;
  444. for(x=0;x<8;x++) {
  445. printf("%u ",px[x]);
  446. }
  447. printf("\n");
  448. #endif
  449. memcpy(cellp,cryptcell,CELL_NETWORK_SIZE);
  450. return 0;
  451. }
  452. int connection_process_inbuf(connection_t *conn) {
  453. assert(conn);
  454. switch(conn->type) {
  455. case CONN_TYPE_OP:
  456. return connection_op_process_inbuf(conn);
  457. case CONN_TYPE_OR:
  458. return connection_or_process_inbuf(conn);
  459. case CONN_TYPE_EXIT:
  460. return connection_exit_process_inbuf(conn);
  461. case CONN_TYPE_AP:
  462. return connection_ap_process_inbuf(conn);
  463. case CONN_TYPE_DIR:
  464. return connection_dir_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. case CONN_TYPE_DIR:
  573. return connection_dir_finished_flushing(conn);
  574. default:
  575. log(LOG_DEBUG,"connection_finished_flushing() got unexpected conn->type.");
  576. return -1;
  577. }
  578. }
  579. int connection_process_cell_from_inbuf(connection_t *conn) {
  580. /* check if there's a whole cell there.
  581. * if yes, pull it off, decrypt it, and process it.
  582. */
  583. char crypted[CELL_NETWORK_SIZE];
  584. char outbuf[1024];
  585. // int x;
  586. cell_t cell;
  587. if(conn->inbuf_datalen < CELL_NETWORK_SIZE) /* entire response available? */
  588. return 0; /* not yet */
  589. if(connection_fetch_from_buf(crypted,CELL_NETWORK_SIZE,conn) < 0) {
  590. return -1;
  591. }
  592. #if 0
  593. printf("Cell header crypttext: ");
  594. for(x=0;x<8;x++) {
  595. printf("%u ",crypted[x]);
  596. }
  597. printf("\n");
  598. #endif
  599. /* decrypt */
  600. if(crypto_cipher_decrypt(conn->b_crypto,crypted,CELL_NETWORK_SIZE,outbuf)) {
  601. log(LOG_ERR,"connection_process_cell_from_inbuf(): Decryption failed, dropping.");
  602. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  603. }
  604. // log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Cell decrypted (%d bytes).",outlen);
  605. #if 0
  606. printf("Cell header plaintext: ");
  607. for(x=0;x<8;x++) {
  608. printf("%u ",outbuf[x]);
  609. }
  610. printf("\n");
  611. #endif
  612. /* retrieve cell info from outbuf (create the struct from the string) */
  613. memset(&cell,0,sizeof(cell_t)); /* zero it out to start */
  614. cell.aci = ntohs(*(aci_t *)outbuf);
  615. cell.command = *(outbuf+2);
  616. cell.length = *(outbuf+3);
  617. memcpy(cell.payload, outbuf+8, CELL_PAYLOAD_SIZE);
  618. // log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci);
  619. command_process_cell(&cell, conn);
  620. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  621. }