connection.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. "DNS master", /* 10 */
  19. };
  20. char *conn_state_to_string[][15] = {
  21. { }, /* no type associated with 0 */
  22. { "ready" }, /* op listener, 0 */
  23. { "awaiting keys", /* op, 0 */
  24. "open", /* 1 */
  25. "close", /* 2 */
  26. "close_wait" }, /* 3 */
  27. { "ready" }, /* or listener, 0 */
  28. { "connecting (as OP)", /* or, 0 */
  29. "sending keys (as OP)", /* 1 */
  30. "connecting (as client)", /* 2 */
  31. "sending auth (as client)", /* 3 */
  32. "waiting for auth (as client)", /* 4 */
  33. "sending nonce (as client)", /* 5 */
  34. "waiting for auth (as server)", /* 6 */
  35. "sending auth (as server)", /* 7 */
  36. "waiting for nonce (as server)",/* 8 */
  37. "open" }, /* 9 */
  38. { "waiting for dest info", /* exit, 0 */
  39. "connecting", /* 1 */
  40. "open" }, /* 2 */
  41. { "ready" }, /* app listener, 0 */
  42. { "", /* 0 */
  43. "", /* 1 */
  44. "", /* 2 */
  45. "awaiting dest info", /* app, 3 */
  46. "waiting for OR connection", /* 4 */
  47. "open" }, /* 5 */
  48. { "ready" }, /* dir listener, 0 */
  49. { "connecting", /* 0 */
  50. "sending command", /* 1 */
  51. "reading", /* 2 */
  52. "awaiting command", /* 3 */
  53. "writing" }, /* 4 */
  54. { "open" }, /* dns master, 0 */
  55. };
  56. /********* END VARIABLES ************/
  57. /**************************************************************/
  58. connection_t *connection_new(int type) {
  59. connection_t *conn;
  60. struct timeval now;
  61. my_gettimeofday(&now);
  62. conn = (connection_t *)tor_malloc(sizeof(connection_t));
  63. memset(conn,0,sizeof(connection_t)); /* zero it out to start */
  64. conn->type = type;
  65. if(buf_new(&conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen) < 0 ||
  66. buf_new(&conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen) < 0)
  67. return NULL;
  68. conn->receiver_bucket = 10240; /* should be enough to do the handshake */
  69. conn->bandwidth = conn->receiver_bucket / 10; /* give it a default */
  70. conn->timestamp_created = now.tv_sec;
  71. conn->timestamp_lastread = now.tv_sec;
  72. conn->timestamp_lastwritten = now.tv_sec;
  73. if (connection_speaks_cells(conn)) {
  74. conn->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_3DES);
  75. if (!conn->f_crypto) {
  76. free((void *)conn);
  77. return NULL;
  78. }
  79. conn->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_3DES);
  80. if (!conn->b_crypto) {
  81. crypto_free_cipher_env(conn->f_crypto);
  82. free((void *)conn);
  83. return NULL;
  84. }
  85. }
  86. conn->done_sending = conn->done_receiving = 0;
  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(connection_speaks_cells(conn)) {
  98. if (conn->f_crypto)
  99. crypto_free_cipher_env(conn->f_crypto);
  100. if (conn->b_crypto)
  101. crypto_free_cipher_env(conn->b_crypto);
  102. }
  103. if (conn->pkey)
  104. crypto_free_pk_env(conn->pkey);
  105. if(conn->s > 0) {
  106. log_fn(LOG_INFO,"closing fd %d.",conn->s);
  107. close(conn->s);
  108. }
  109. if(conn->type == CONN_TYPE_OR) {
  110. directory_set_dirty();
  111. }
  112. free(conn);
  113. }
  114. int connection_create_listener(struct sockaddr_in *bindaddr, 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_fn(LOG_ERR,"Socket creation failed.");
  122. return -1;
  123. }
  124. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  125. if(bind(s,(struct sockaddr *)bindaddr,sizeof(*bindaddr)) < 0) {
  126. perror("bind ");
  127. log(LOG_ERR,"Could not bind to port %u.",ntohs(bindaddr->sin_port));
  128. return -1;
  129. }
  130. if(listen(s,SOMAXCONN) < 0) {
  131. log(LOG_ERR,"Could not listen on port %u.",ntohs(bindaddr->sin_port));
  132. return -1;
  133. }
  134. fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
  135. conn = connection_new(type);
  136. if(!conn) {
  137. log_fn(LOG_DEBUG,"connection_new failed. Giving up.");
  138. return -1;
  139. }
  140. conn->s = s;
  141. if(connection_add(conn) < 0) { /* no space, forget it */
  142. log_fn(LOG_DEBUG,"connection_add failed. Giving up.");
  143. connection_free(conn);
  144. return -1;
  145. }
  146. log_fn(LOG_DEBUG,"Listening on port %u.",ntohs(bindaddr->sin_port));
  147. conn->state = LISTENER_STATE_READY;
  148. connection_start_reading(conn);
  149. return 0;
  150. }
  151. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state) {
  152. int news; /* the new socket */
  153. connection_t *newconn;
  154. struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  155. int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */
  156. news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  157. if (news == -1) { /* accept() error */
  158. if(errno==EAGAIN)
  159. return 0; /* he hung up before we could accept(). that's fine. */
  160. /* else there was a real error. */
  161. log_fn(LOG_ERR,"accept() failed. Closing.");
  162. return -1;
  163. }
  164. log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
  165. fcntl(news, F_SETFL, O_NONBLOCK); /* set news to non-blocking */
  166. newconn = connection_new(new_type);
  167. newconn->s = news;
  168. if(!connection_speaks_cells(newconn)) {
  169. newconn->receiver_bucket = -1;
  170. newconn->bandwidth = -1;
  171. }
  172. newconn->address = strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
  173. newconn->addr = ntohl(remote.sin_addr.s_addr);
  174. newconn->port = ntohs(remote.sin_port);
  175. if(connection_add(newconn) < 0) { /* no space, forget it */
  176. connection_free(newconn);
  177. return 0; /* no need to tear down the parent */
  178. }
  179. log(LOG_DEBUG,"connection_handle_listener_read(): socket %d entered state %d.",newconn->s, new_state);
  180. newconn->state = new_state;
  181. connection_start_reading(newconn);
  182. return 0;
  183. }
  184. int retry_all_connections(uint16_t or_listenport, uint16_t ap_listenport, uint16_t dir_listenport) {
  185. /* start all connections that should be up but aren't */
  186. struct sockaddr_in bindaddr; /* where to bind */
  187. if(or_listenport) {
  188. router_retry_connections();
  189. }
  190. memset(&bindaddr,0,sizeof(struct sockaddr_in));
  191. bindaddr.sin_family = AF_INET;
  192. bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* anyone can connect */
  193. if(or_listenport) {
  194. bindaddr.sin_port = htons(or_listenport);
  195. if(!connection_get_by_type(CONN_TYPE_OR_LISTENER)) {
  196. connection_or_create_listener(&bindaddr);
  197. }
  198. }
  199. if(dir_listenport) {
  200. bindaddr.sin_port = htons(dir_listenport);
  201. if(!connection_get_by_type(CONN_TYPE_DIR_LISTENER)) {
  202. connection_dir_create_listener(&bindaddr);
  203. }
  204. }
  205. if(ap_listenport) {
  206. bindaddr.sin_port = htons(ap_listenport);
  207. bindaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* the AP listens only on localhost! */
  208. if(!connection_get_by_type(CONN_TYPE_AP_LISTENER)) {
  209. connection_ap_create_listener(&bindaddr);
  210. }
  211. }
  212. return 0;
  213. }
  214. int connection_read_to_buf(connection_t *conn) {
  215. int read_result;
  216. struct timeval now;
  217. if(connection_speaks_cells(conn)) {
  218. assert(conn->receiver_bucket >= 0);
  219. }
  220. if(!connection_speaks_cells(conn)) {
  221. assert(conn->receiver_bucket < 0);
  222. }
  223. my_gettimeofday(&now);
  224. conn->timestamp_lastread = now.tv_sec;
  225. read_result = read_to_buf(conn->s, conn->receiver_bucket, &conn->inbuf, &conn->inbuflen,
  226. &conn->inbuf_datalen, &conn->inbuf_reached_eof);
  227. // log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);
  228. if(read_result >= 0 && connection_speaks_cells(conn)) {
  229. // log(LOG_DEBUG,"connection_read_to_buf(): Read %d, bucket now %d.",read_result,conn->receiver_bucket);
  230. conn->receiver_bucket -= read_result;
  231. if(conn->receiver_bucket <= 0) {
  232. // log(LOG_DEBUG,"connection_read_to_buf() stopping reading, receiver bucket full.");
  233. connection_stop_reading(conn);
  234. /* If we're not in 'open' state here, then we're never going to finish the
  235. * handshake, because we'll never increment the receiver_bucket. But we
  236. * can't check for that here, because the buf we just read might have enough
  237. * on it to finish the handshake. So we check for that in check_conn_read().
  238. */
  239. }
  240. }
  241. return read_result;
  242. }
  243. int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
  244. return fetch_from_buf(string, len, &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
  245. }
  246. int connection_find_on_inbuf(char *string, int len, connection_t *conn) {
  247. return find_on_inbuf(string, len, conn->inbuf, conn->inbuf_datalen);
  248. }
  249. int connection_wants_to_flush(connection_t *conn) {
  250. return conn->outbuf_flushlen;
  251. }
  252. int connection_outbuf_too_full(connection_t *conn) {
  253. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  254. }
  255. int connection_flush_buf(connection_t *conn) {
  256. return flush_buf(conn->s, &conn->outbuf, &conn->outbuflen, &conn->outbuf_flushlen, &conn->outbuf_datalen);
  257. }
  258. int connection_write_to_buf(char *string, int len, connection_t *conn) {
  259. struct timeval now;
  260. my_gettimeofday(&now);
  261. if(!len)
  262. return 0;
  263. if(conn->marked_for_close)
  264. return 0;
  265. conn->timestamp_lastwritten = now.tv_sec;
  266. if( (!connection_speaks_cells(conn)) ||
  267. (!connection_state_is_open(conn)) ||
  268. (options.LinkPadding == 0) ) {
  269. /* connection types other than or and op, or or/op not in 'open' state, should flush immediately */
  270. /* also flush immediately if we're not doing LinkPadding, since otherwise it will never flush */
  271. connection_start_writing(conn);
  272. conn->outbuf_flushlen += len;
  273. }
  274. return write_to_buf(string, len, &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  275. }
  276. int connection_receiver_bucket_should_increase(connection_t *conn) {
  277. assert(conn);
  278. if(!connection_speaks_cells(conn))
  279. return 0; /* edge connections don't use receiver_buckets */
  280. if(conn->receiver_bucket > 10*conn->bandwidth)
  281. return 0;
  282. return 1;
  283. }
  284. void connection_increment_receiver_bucket(connection_t *conn) {
  285. assert(conn);
  286. if(connection_receiver_bucket_should_increase(conn)) {
  287. /* yes, the receiver_bucket can become overfull here. But not by much. */
  288. conn->receiver_bucket += conn->bandwidth*1.1;
  289. // log(LOG_DEBUG,"connection_increment_receiver_bucket(): Bucket now %d.",conn->receiver_bucket);
  290. if(connection_state_is_open(conn)) {
  291. /* if we're in state 'open', then start reading again */
  292. connection_start_reading(conn);
  293. }
  294. }
  295. }
  296. int connection_is_listener(connection_t *conn) {
  297. if(conn->type == CONN_TYPE_OR_LISTENER ||
  298. conn->type == CONN_TYPE_AP_LISTENER ||
  299. conn->type == CONN_TYPE_DIR_LISTENER)
  300. return 1;
  301. return 0;
  302. }
  303. int connection_state_is_open(connection_t *conn) {
  304. assert(conn);
  305. if((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  306. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  307. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN))
  308. return 1;
  309. return 0;
  310. }
  311. void connection_send_cell(connection_t *conn) {
  312. cell_t cell;
  313. int bytes_in_full_flushlen;
  314. /* this function only gets called if options.LinkPadding is 1 */
  315. assert(options.LinkPadding == 1);
  316. assert(conn);
  317. if(!connection_speaks_cells(conn)) {
  318. /* this conn doesn't speak cells. do nothing. */
  319. return;
  320. }
  321. if(!connection_state_is_open(conn)) {
  322. /* it's not in 'open' state, all data should already be waiting to be flushed */
  323. assert(conn->outbuf_datalen == conn->outbuf_flushlen);
  324. return;
  325. }
  326. #if 0 /* use to send evenly spaced cells, but not padding */
  327. if(conn->outbuf_datalen - conn->outbuf_flushlen >= sizeof(cell_t)) {
  328. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  329. connection_start_writing(conn);
  330. }
  331. #endif
  332. connection_increment_send_timeval(conn); /* update when we'll send the next cell */
  333. bytes_in_full_flushlen = conn->bandwidth / 100; /* 10ms worth */
  334. if(bytes_in_full_flushlen < 10*sizeof(cell_t))
  335. bytes_in_full_flushlen = 10*sizeof(cell_t); /* but at least 10 cells worth */
  336. if(conn->outbuf_flushlen > bytes_in_full_flushlen - sizeof(cell_t)) {
  337. /* if we would exceed bytes_in_full_flushlen by adding a new cell */
  338. return;
  339. }
  340. if(conn->outbuf_datalen - conn->outbuf_flushlen < sizeof(cell_t)) {
  341. /* we need to queue a padding cell first */
  342. memset(&cell,0,sizeof(cell_t));
  343. cell.command = CELL_PADDING;
  344. connection_write_cell_to_buf(&cell, conn);
  345. }
  346. /* The connection_write_cell_to_buf() call doesn't increase the flushlen
  347. * (if link padding is on). So if there isn't a whole cell waiting-but-
  348. * not-yet-flushed, we add a padding cell. Thus in any case the gap between
  349. * outbuf_datalen and outbuf_flushlen is at least sizeof(cell_t).
  350. */
  351. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  352. connection_start_writing(conn);
  353. }
  354. void connection_increment_send_timeval(connection_t *conn) {
  355. /* add "1000000 * sizeof(cell_t) / conn->bandwidth" microseconds to conn->send_timeval */
  356. /* FIXME should perhaps use ceil() of this. For now I simply add 1. */
  357. tv_addms(&conn->send_timeval, 1+1000 * sizeof(cell_t) / conn->bandwidth);
  358. }
  359. void connection_init_timeval(connection_t *conn) {
  360. assert(conn);
  361. my_gettimeofday(&conn->send_timeval);
  362. connection_increment_send_timeval(conn);
  363. }
  364. int connection_send_destroy(aci_t aci, connection_t *conn) {
  365. cell_t cell;
  366. assert(conn);
  367. if(!connection_speaks_cells(conn)) {
  368. log_fn(LOG_INFO,"Aci %d: At an edge. Marking connection for close.", aci);
  369. conn->marked_for_close = 1;
  370. return 0;
  371. }
  372. memset(&cell, 0, sizeof(cell_t));
  373. cell.aci = aci;
  374. cell.command = CELL_DESTROY;
  375. log_fn(LOG_INFO,"Sending destroy (aci %d).",aci);
  376. return connection_write_cell_to_buf(&cell, conn);
  377. }
  378. int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
  379. char networkcell[CELL_NETWORK_SIZE];
  380. char *n = networkcell;
  381. cell_pack(n, cellp);
  382. if(connection_encrypt_cell(n,conn)<0) {
  383. return -1;
  384. }
  385. return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  386. }
  387. int connection_encrypt_cell(char *cellp, connection_t *conn) {
  388. char cryptcell[CELL_NETWORK_SIZE];
  389. #if 0
  390. int x;
  391. char *px;
  392. printf("Sending: Cell header plaintext: ");
  393. px = (char *)cellp;
  394. for(x=0;x<8;x++) {
  395. printf("%u ",px[x]);
  396. }
  397. printf("\n");
  398. #endif
  399. assert(conn);
  400. if(crypto_cipher_encrypt(conn->f_crypto, cellp, CELL_NETWORK_SIZE, cryptcell)) {
  401. log(LOG_ERR,"Could not encrypt cell for connection %s:%u.",conn->address,conn->port);
  402. return -1;
  403. }
  404. #if 0
  405. printf("Sending: Cell header crypttext: ");
  406. px = (char *)&newcell;
  407. for(x=0;x<8;x++) {
  408. printf("%u ",px[x]);
  409. }
  410. printf("\n");
  411. #endif
  412. memcpy(cellp,cryptcell,CELL_NETWORK_SIZE);
  413. return 0;
  414. }
  415. int connection_process_inbuf(connection_t *conn) {
  416. assert(conn);
  417. switch(conn->type) {
  418. case CONN_TYPE_OR:
  419. return connection_or_process_inbuf(conn);
  420. case CONN_TYPE_EXIT:
  421. case CONN_TYPE_AP:
  422. return connection_edge_process_inbuf(conn);
  423. case CONN_TYPE_DIR:
  424. return connection_dir_process_inbuf(conn);
  425. case CONN_TYPE_DNSWORKER:
  426. return connection_dns_process_inbuf(conn);
  427. default:
  428. log_fn(LOG_DEBUG,"got unexpected conn->type.");
  429. return -1;
  430. }
  431. }
  432. int connection_package_raw_inbuf(connection_t *conn) {
  433. int amount_to_process;
  434. cell_t cell;
  435. circuit_t *circ;
  436. assert(conn);
  437. assert(!connection_speaks_cells(conn));
  438. /* this function should never get called if either package_window is 0 */
  439. repeat_connection_package_raw_inbuf:
  440. amount_to_process = conn->inbuf_datalen;
  441. if(!amount_to_process)
  442. return 0;
  443. /* Initialize the cell with 0's */
  444. memset(&cell, 0, sizeof(cell_t));
  445. if(amount_to_process > CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE) {
  446. cell.length = CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE;
  447. } else {
  448. cell.length = amount_to_process;
  449. }
  450. connection_fetch_from_buf(cell.payload+RELAY_HEADER_SIZE, cell.length, conn);
  451. circ = circuit_get_by_conn(conn);
  452. if(!circ) {
  453. log_fn(LOG_DEBUG,"conn has no circuits!");
  454. return -1;
  455. }
  456. log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).",conn->s,cell.length, conn->inbuf_datalen);
  457. cell.command = CELL_RELAY;
  458. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_DATA);
  459. SET_CELL_STREAM_ID(cell, conn->stream_id);
  460. cell.length += RELAY_HEADER_SIZE;
  461. if(conn->type == CONN_TYPE_EXIT) {
  462. cell.aci = circ->p_aci;
  463. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, NULL) < 0) {
  464. log_fn(LOG_DEBUG,"circuit_deliver_relay_cell (backward) failed. Closing.");
  465. circuit_close(circ);
  466. return 0;
  467. }
  468. assert(circ->package_window > 0);
  469. circ->package_window--;
  470. } else { /* send it forward. we're an AP */
  471. assert(conn->type == CONN_TYPE_AP);
  472. cell.aci = circ->n_aci;
  473. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, conn->cpath_layer) < 0) {
  474. log_fn(LOG_DEBUG,"circuit_deliver_relay_cell (forward) failed. Closing.");
  475. circuit_close(circ);
  476. return 0;
  477. }
  478. assert(conn->cpath_layer->package_window > 0);
  479. conn->cpath_layer->package_window--;
  480. }
  481. if(circuit_consider_stop_edge_reading(circ,
  482. conn->type == CONN_TYPE_EXIT ? EDGE_EXIT : EDGE_AP, conn->cpath_layer))
  483. return 0;
  484. assert(conn->package_window > 0);
  485. if(--conn->package_window <= 0) { /* is it 0 after decrement? */
  486. connection_stop_reading(conn);
  487. log_fn(LOG_DEBUG,"conn->package_window reached 0.");
  488. return 0; /* don't process the inbuf any more */
  489. }
  490. log_fn(LOG_DEBUG,"conn->package_window is %d",conn->package_window);
  491. /* handle more if there's more, or return 0 if there isn't */
  492. goto repeat_connection_package_raw_inbuf;
  493. }
  494. int connection_consider_sending_sendme(connection_t *conn, int edge_type) {
  495. circuit_t *circ;
  496. cell_t cell;
  497. if(connection_outbuf_too_full(conn))
  498. return 0;
  499. circ = circuit_get_by_conn(conn);
  500. if(!circ) {
  501. /* this can legitimately happen if the destroy has already arrived and torn down the circuit */
  502. log_fn(LOG_DEBUG,"No circuit associated with conn. Skipping.");
  503. return 0;
  504. }
  505. memset(&cell, 0, sizeof(cell_t));
  506. cell.command = CELL_RELAY;
  507. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  508. SET_CELL_STREAM_ID(cell, conn->stream_id);
  509. cell.length += RELAY_HEADER_SIZE;
  510. if(edge_type == EDGE_EXIT)
  511. cell.aci = circ->p_aci;
  512. else
  513. cell.aci = circ->n_aci;
  514. while(conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  515. log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", conn->outbuf_flushlen);
  516. conn->deliver_window += STREAMWINDOW_INCREMENT;
  517. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION(edge_type), conn->cpath_layer) < 0) {
  518. log_fn(LOG_DEBUG,"circuit_deliver_relay_cell failed. Closing.");
  519. circuit_close(circ);
  520. return 0;
  521. }
  522. }
  523. return 0;
  524. }
  525. int connection_finished_flushing(connection_t *conn) {
  526. assert(conn);
  527. // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
  528. switch(conn->type) {
  529. case CONN_TYPE_OR:
  530. return connection_or_finished_flushing(conn);
  531. case CONN_TYPE_AP:
  532. case CONN_TYPE_EXIT:
  533. return connection_edge_finished_flushing(conn);
  534. case CONN_TYPE_DIR:
  535. return connection_dir_finished_flushing(conn);
  536. case CONN_TYPE_DNSWORKER:
  537. return connection_dns_finished_flushing(conn);
  538. default:
  539. log_fn(LOG_DEBUG,"got unexpected conn->type.");
  540. return -1;
  541. }
  542. }
  543. int connection_process_cell_from_inbuf(connection_t *conn) {
  544. /* check if there's a whole cell there.
  545. * if yes, pull it off, decrypt it, and process it.
  546. */
  547. char crypted[CELL_NETWORK_SIZE];
  548. char outbuf[1024];
  549. // int x;
  550. cell_t cell;
  551. if(conn->inbuf_datalen < CELL_NETWORK_SIZE) /* entire response available? */
  552. return 0; /* not yet */
  553. connection_fetch_from_buf(crypted,CELL_NETWORK_SIZE,conn);
  554. #if 0
  555. printf("Cell header crypttext: ");
  556. for(x=0;x<8;x++) {
  557. printf("%u ",crypted[x]);
  558. }
  559. printf("\n");
  560. #endif
  561. /* decrypt */
  562. if(crypto_cipher_decrypt(conn->b_crypto,crypted,CELL_NETWORK_SIZE,outbuf)) {
  563. log_fn(LOG_ERR,"Decryption failed, dropping.");
  564. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  565. }
  566. // log_fn(LOG_DEBUG,"Cell decrypted (%d bytes).",outlen);
  567. #if 0
  568. printf("Cell header plaintext: ");
  569. for(x=0;x<8;x++) {
  570. printf("%u ",outbuf[x]);
  571. }
  572. printf("\n");
  573. #endif
  574. /* retrieve cell info from outbuf (create the host-order struct from the network-order string) */
  575. cell_unpack(&cell, outbuf);
  576. // log_fn(LOG_DEBUG,"Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci);
  577. command_process_cell(&cell, conn);
  578. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  579. }
  580. void
  581. cell_pack(char *dest, const cell_t *src)
  582. {
  583. *(uint16_t*)dest = htons(src->aci);
  584. *(uint8_t*)(dest+2) = src->command;
  585. *(uint8_t*)(dest+3) = src->length;
  586. *(uint32_t*)(dest+4) = 0; /* Reserved */
  587. memcpy(dest+8, src->payload, CELL_PAYLOAD_SIZE);
  588. }
  589. void
  590. cell_unpack(cell_t *dest, const char *src)
  591. {
  592. dest->aci = ntohs(*(uint16_t*)(src));
  593. dest->command = *(uint8_t*)(src+2);
  594. dest->length = *(uint8_t*)(src+3);
  595. dest->seq = ntohl(*(uint32_t*)(src+4));
  596. memcpy(dest->payload, src+8, CELL_PAYLOAD_SIZE);
  597. }
  598. /*
  599. Local Variables:
  600. mode:c
  601. indent-tabs-mode:nil
  602. c-basic-offset:2
  603. End:
  604. */