connection.c 22 KB

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