connection.c 22 KB

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