connection.c 23 KB

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