connection.c 21 KB

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