connection.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. int tv_cmp(struct timeval *a, struct timeval *b) {
  59. if (a->tv_sec > b->tv_sec)
  60. return 1;
  61. if (a->tv_sec < b->tv_sec)
  62. return -1;
  63. if (a->tv_usec > b->tv_usec)
  64. return 1;
  65. if (a->tv_usec < b->tv_usec)
  66. return -1;
  67. return 0;
  68. }
  69. void tv_add(struct timeval *a, struct timeval *b) {
  70. a->tv_usec += b->tv_usec;
  71. a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
  72. a->tv_usec %= 1000000;
  73. }
  74. void tv_addms(struct timeval *a, long ms) {
  75. a->tv_usec += (ms * 1000) % 1000000;
  76. a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
  77. a->tv_usec %= 1000000;
  78. }
  79. /**************************************************************/
  80. connection_t *connection_new(int type) {
  81. connection_t *conn;
  82. struct timeval now;
  83. if(gettimeofday(&now,NULL) < 0)
  84. return NULL;
  85. conn = (connection_t *)malloc(sizeof(connection_t));
  86. if(!conn)
  87. return NULL;
  88. memset(conn,0,sizeof(connection_t)); /* zero it out to start */
  89. conn->type = type;
  90. if(buf_new(&conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen) < 0 ||
  91. buf_new(&conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen) < 0)
  92. return NULL;
  93. conn->receiver_bucket = 10240; /* should be enough to do the handshake */
  94. conn->bandwidth = conn->receiver_bucket / 10; /* give it a default */
  95. conn->timestamp_created = now.tv_sec;
  96. conn->timestamp_lastread = now.tv_sec;
  97. conn->timestamp_lastwritten = now.tv_sec;
  98. if (connection_speaks_cells(conn)) {
  99. conn->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_3DES);
  100. if (!conn->f_crypto) {
  101. free((void *)conn);
  102. return NULL;
  103. }
  104. conn->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_3DES);
  105. if (!conn->b_crypto) {
  106. crypto_free_cipher_env(conn->f_crypto);
  107. free((void *)conn);
  108. return NULL;
  109. }
  110. }
  111. #ifdef USE_ZLIB
  112. if (type == CONN_TYPE_AP || type == CONN_TYPE_EXIT) {
  113. if (buf_new(&conn->z_outbuf, &conn->z_outbuflen, &conn->z_outbuf_datalen) < 0)
  114. return NULL;
  115. if (! (conn->compression = compression_new()))
  116. return NULL;
  117. if (! (conn->decompression = decompression_new()))
  118. return NULL;
  119. } else {
  120. conn->compression = conn->decompression = NULL;
  121. }
  122. #endif
  123. conn->done_sending = conn->done_receiving = 0;
  124. return conn;
  125. }
  126. void connection_free(connection_t *conn) {
  127. assert(conn);
  128. buf_free(conn->inbuf);
  129. buf_free(conn->outbuf);
  130. if(conn->address)
  131. free(conn->address);
  132. if(conn->dest_addr)
  133. free(conn->dest_addr);
  134. if(connection_speaks_cells(conn)) {
  135. if (conn->f_crypto)
  136. crypto_free_cipher_env(conn->f_crypto);
  137. if (conn->b_crypto)
  138. crypto_free_cipher_env(conn->b_crypto);
  139. }
  140. if (conn->pkey)
  141. crypto_free_pk_env(conn->pkey);
  142. if(conn->s > 0) {
  143. log(LOG_INFO,"connection_free(): closing fd %d.",conn->s);
  144. close(conn->s);
  145. }
  146. if(conn->type == CONN_TYPE_OR) {
  147. directory_set_dirty();
  148. }
  149. #ifdef USE_ZLIB
  150. if (conn->compression) {
  151. decompression_free(conn->decompression);
  152. compression_free(conn->compression);
  153. buf_free(conn->z_outbuf);
  154. }
  155. #endif
  156. free(conn);
  157. }
  158. int connection_create_listener(struct sockaddr_in *bindaddr, int type) {
  159. connection_t *conn;
  160. int s;
  161. int one=1;
  162. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  163. if (s < 0)
  164. {
  165. log(LOG_ERR,"connection_create_listener(): Socket creation failed.");
  166. return -1;
  167. }
  168. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  169. if(bind(s,(struct sockaddr *)bindaddr,sizeof(*bindaddr)) < 0) {
  170. perror("bind ");
  171. log(LOG_ERR,"Could not bind to port %u.",ntohs(bindaddr->sin_port));
  172. return -1;
  173. }
  174. if(listen(s,SOMAXCONN) < 0) {
  175. log(LOG_ERR,"Could not listen on port %u.",ntohs(bindaddr->sin_port));
  176. return -1;
  177. }
  178. fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
  179. conn = connection_new(type);
  180. if(!conn) {
  181. log(LOG_DEBUG,"connection_create_listener(): connection_new failed. Giving up.");
  182. return -1;
  183. }
  184. conn->s = s;
  185. if(connection_add(conn) < 0) { /* no space, forget it */
  186. log(LOG_DEBUG,"connection_create_listener(): connection_add failed. Giving up.");
  187. connection_free(conn);
  188. return -1;
  189. }
  190. log(LOG_DEBUG,"connection_create_listener(): Listening on port %u.",ntohs(bindaddr->sin_port));
  191. conn->state = LISTENER_STATE_READY;
  192. connection_start_reading(conn);
  193. return 0;
  194. }
  195. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state) {
  196. int news; /* the new socket */
  197. connection_t *newconn;
  198. struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  199. int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */
  200. news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  201. if (news == -1) { /* accept() error */
  202. if(errno==EAGAIN)
  203. return 0; /* he hung up before we could accept(). that's fine. */
  204. /* else there was a real error. */
  205. log(LOG_ERR,"connection_handle_listener_read(): accept() failed. Closing.");
  206. return -1;
  207. }
  208. log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
  209. fcntl(news, F_SETFL, O_NONBLOCK); /* set news to non-blocking */
  210. newconn = connection_new(new_type);
  211. newconn->s = news;
  212. if(!connection_speaks_cells(newconn)) {
  213. newconn->receiver_bucket = -1;
  214. newconn->bandwidth = -1;
  215. }
  216. newconn->address = strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
  217. newconn->addr = ntohl(remote.sin_addr.s_addr);
  218. newconn->port = ntohs(remote.sin_port);
  219. if(connection_add(newconn) < 0) { /* no space, forget it */
  220. connection_free(newconn);
  221. return 0; /* no need to tear down the parent */
  222. }
  223. log(LOG_DEBUG,"connection_handle_listener_read(): socket %d entered state %d.",newconn->s, new_state);
  224. newconn->state = new_state;
  225. connection_start_reading(newconn);
  226. return 0;
  227. }
  228. int retry_all_connections(uint16_t or_listenport,
  229. uint16_t op_listenport, uint16_t ap_listenport, uint16_t dir_listenport) {
  230. /* start all connections that should be up but aren't */
  231. struct sockaddr_in bindaddr; /* where to bind */
  232. if(or_listenport) {
  233. router_retry_connections();
  234. }
  235. memset(&bindaddr,0,sizeof(struct sockaddr_in));
  236. bindaddr.sin_family = AF_INET;
  237. bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* anyone can connect */
  238. if(or_listenport) {
  239. bindaddr.sin_port = htons(or_listenport);
  240. if(!connection_get_by_type(CONN_TYPE_OR_LISTENER)) {
  241. connection_or_create_listener(&bindaddr);
  242. }
  243. }
  244. if(op_listenport) {
  245. bindaddr.sin_port = htons(op_listenport);
  246. if(!connection_get_by_type(CONN_TYPE_OP_LISTENER)) {
  247. connection_op_create_listener(&bindaddr);
  248. }
  249. }
  250. if(dir_listenport) {
  251. bindaddr.sin_port = htons(dir_listenport);
  252. if(!connection_get_by_type(CONN_TYPE_DIR_LISTENER)) {
  253. connection_dir_create_listener(&bindaddr);
  254. }
  255. }
  256. if(ap_listenport) {
  257. bindaddr.sin_port = htons(ap_listenport);
  258. inet_aton("127.0.0.1", &(bindaddr.sin_addr)); /* the AP listens only on localhost! */
  259. if(!connection_get_by_type(CONN_TYPE_AP_LISTENER)) {
  260. connection_ap_create_listener(&bindaddr);
  261. }
  262. }
  263. return 0;
  264. }
  265. int connection_read_to_buf(connection_t *conn) {
  266. int read_result;
  267. struct timeval now;
  268. if(connection_speaks_cells(conn)) {
  269. assert(conn->receiver_bucket >= 0);
  270. }
  271. if(!connection_speaks_cells(conn)) {
  272. assert(conn->receiver_bucket < 0);
  273. }
  274. if(gettimeofday(&now,NULL) < 0)
  275. return -1;
  276. conn->timestamp_lastread = now.tv_sec;
  277. read_result = read_to_buf(conn->s, conn->receiver_bucket, &conn->inbuf, &conn->inbuflen,
  278. &conn->inbuf_datalen, &conn->inbuf_reached_eof);
  279. // log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);
  280. if(read_result >= 0 && connection_speaks_cells(conn)) {
  281. // log(LOG_DEBUG,"connection_read_to_buf(): Read %d, bucket now %d.",read_result,conn->receiver_bucket);
  282. conn->receiver_bucket -= read_result;
  283. if(conn->receiver_bucket <= 0) {
  284. // log(LOG_DEBUG,"connection_read_to_buf() stopping reading, receiver bucket full.");
  285. connection_stop_reading(conn);
  286. /* If we're not in 'open' state here, then we're never going to finish the
  287. * handshake, because we'll never increment the receiver_bucket. But we
  288. * can't check for that here, because the buf we just read might have enough
  289. * on it to finish the handshake. So we check for that in check_conn_read().
  290. */
  291. }
  292. }
  293. return read_result;
  294. }
  295. int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
  296. return fetch_from_buf(string, len, &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
  297. }
  298. #ifdef USE_ZLIB
  299. int connection_compress_from_buf(char *string, int len, connection_t *conn,
  300. int flush) {
  301. return compress_from_buf(string, len,
  302. &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen,
  303. conn->compression, flush);
  304. }
  305. int connection_decompress_to_buf(char *string, int len, connection_t *conn,
  306. int flush) {
  307. int n;
  308. struct timeval now;
  309. assert(conn);
  310. if (len) {
  311. if (write_to_buf(string, len,
  312. &conn->z_outbuf, &conn->z_outbuflen, &conn->z_outbuf_datalen) < 0)
  313. return -1;
  314. }
  315. /* If we have more that 10 payloads worth of data waiting in outbuf,
  316. * don't uncompress any more; queue this data in z_outbuf.
  317. *
  318. * This check should may be different.
  319. */
  320. if (connection_outbuf_too_full(conn))
  321. return 0;
  322. n = decompress_buf_to_buf(
  323. &conn->z_outbuf, &conn->z_outbuflen, &conn->z_outbuf_datalen,
  324. &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen,
  325. conn->decompression, flush);
  326. if (n < 0)
  327. return -1;
  328. if(gettimeofday(&now,NULL) < 0)
  329. return -1;
  330. if(!n)
  331. return 0;
  332. if(conn->marked_for_close)
  333. return 0;
  334. conn->timestamp_lastwritten = now.tv_sec;
  335. conn->outbuf_flushlen += n;
  336. return n;
  337. }
  338. #endif
  339. int connection_find_on_inbuf(char *string, int len, connection_t *conn) {
  340. return find_on_inbuf(string, len, conn->inbuf, conn->inbuf_datalen);
  341. }
  342. int connection_wants_to_flush(connection_t *conn) {
  343. return conn->outbuf_flushlen;
  344. }
  345. int connection_outbuf_too_full(connection_t *conn) {
  346. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  347. }
  348. int connection_flush_buf(connection_t *conn) {
  349. return flush_buf(conn->s, &conn->outbuf, &conn->outbuflen, &conn->outbuf_flushlen, &conn->outbuf_datalen);
  350. }
  351. int connection_write_to_buf(char *string, int len, connection_t *conn) {
  352. struct timeval now;
  353. if(gettimeofday(&now,NULL) < 0)
  354. return -1;
  355. if(!len)
  356. return 0;
  357. if(conn->marked_for_close)
  358. return 0;
  359. conn->timestamp_lastwritten = now.tv_sec;
  360. if( (!connection_speaks_cells(conn)) ||
  361. (!connection_state_is_open(conn)) ||
  362. (options.LinkPadding == 0) ) {
  363. /* connection types other than or and op, or or/op not in 'open' state, should flush immediately */
  364. /* also flush immediately if we're not doing LinkPadding, since otherwise it will never flush */
  365. connection_start_writing(conn);
  366. conn->outbuf_flushlen += len;
  367. }
  368. return write_to_buf(string, len, &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  369. }
  370. int connection_receiver_bucket_should_increase(connection_t *conn) {
  371. assert(conn);
  372. if(!connection_speaks_cells(conn))
  373. return 0; /* edge connections don't use receiver_buckets */
  374. if(conn->receiver_bucket > 10*conn->bandwidth)
  375. return 0;
  376. return 1;
  377. }
  378. void connection_increment_receiver_bucket(connection_t *conn) {
  379. assert(conn);
  380. if(connection_receiver_bucket_should_increase(conn)) {
  381. /* yes, the receiver_bucket can become overfull here. But not by much. */
  382. conn->receiver_bucket += conn->bandwidth*1.1;
  383. // log(LOG_DEBUG,"connection_increment_receiver_bucket(): Bucket now %d.",conn->receiver_bucket);
  384. if(connection_state_is_open(conn)) {
  385. /* if we're in state 'open', then start reading again */
  386. connection_start_reading(conn);
  387. }
  388. }
  389. }
  390. int connection_speaks_cells(connection_t *conn) {
  391. assert(conn);
  392. if(conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_OP)
  393. return 1;
  394. return 0;
  395. }
  396. int connection_is_listener(connection_t *conn) {
  397. if(conn->type == CONN_TYPE_OP_LISTENER ||
  398. conn->type == CONN_TYPE_OR_LISTENER ||
  399. conn->type == CONN_TYPE_AP_LISTENER ||
  400. conn->type == CONN_TYPE_DIR_LISTENER)
  401. return 1;
  402. return 0;
  403. }
  404. int connection_state_is_open(connection_t *conn) {
  405. assert(conn);
  406. if((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  407. (conn->type == CONN_TYPE_OP && conn->state == OP_CONN_STATE_OPEN) ||
  408. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  409. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN))
  410. return 1;
  411. return 0;
  412. }
  413. void connection_send_cell(connection_t *conn) {
  414. cell_t cell;
  415. int bytes_in_full_flushlen;
  416. /* this function only gets called if options.LinkPadding is 1 */
  417. assert(options.LinkPadding == 1);
  418. assert(conn);
  419. if(!connection_speaks_cells(conn)) {
  420. /* this conn doesn't speak cells. do nothing. */
  421. return;
  422. }
  423. if(!connection_state_is_open(conn)) {
  424. /* it's not in 'open' state, all data should already be waiting to be flushed */
  425. assert(conn->outbuf_datalen == conn->outbuf_flushlen);
  426. return;
  427. }
  428. #if 0 /* use to send evenly spaced cells, but not padding */
  429. if(conn->outbuf_datalen - conn->outbuf_flushlen >= sizeof(cell_t)) {
  430. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  431. connection_start_writing(conn);
  432. }
  433. #endif
  434. connection_increment_send_timeval(conn); /* update when we'll send the next cell */
  435. bytes_in_full_flushlen = conn->bandwidth / 100; /* 10ms worth */
  436. if(bytes_in_full_flushlen < 10*sizeof(cell_t))
  437. bytes_in_full_flushlen = 10*sizeof(cell_t); /* but at least 10 cells worth */
  438. if(conn->outbuf_flushlen > bytes_in_full_flushlen - sizeof(cell_t)) {
  439. /* if we would exceed bytes_in_full_flushlen by adding a new cell */
  440. return;
  441. }
  442. if(conn->outbuf_datalen - conn->outbuf_flushlen < sizeof(cell_t)) {
  443. /* we need to queue a padding cell first */
  444. memset(&cell,0,sizeof(cell_t));
  445. cell.command = CELL_PADDING;
  446. connection_write_cell_to_buf(&cell, conn);
  447. }
  448. /* ???? If we might not have added a cell above, why are we
  449. * ???? increasing outbuf_flushlen? -NM */
  450. /* The connection_write_cell_to_buf() call doesn't increase the flushlen
  451. * (if link padding is on). So if there isn't a whole cell waiting-but-
  452. * not-yet-flushed, we add a padding cell. Thus in any case the gap between
  453. * outbuf_datalen and outbuf_flushlen is at least sizeof(cell_t). -RD
  454. */
  455. /* XXXX actually, there are some subtle bugs lurking in here. They
  456. * have to do with the fact that we don't handle connection failure
  457. * cleanly. Sometimes we mark things to be closed later. Inside
  458. * connection_write_cell_to_buf, it returns successfully without
  459. * writing if the connection has been marked for close. We need to
  460. * look at all our failure cases more carefully and make sure they do
  461. * the right thing.
  462. */
  463. conn->outbuf_flushlen += sizeof(cell_t); /* instruct it to send a cell */
  464. connection_start_writing(conn);
  465. }
  466. void connection_increment_send_timeval(connection_t *conn) {
  467. /* add "1000000 * sizeof(cell_t) / conn->bandwidth" microseconds to conn->send_timeval */
  468. /* FIXME should perhaps use ceil() of this. For now I simply add 1. */
  469. tv_addms(&conn->send_timeval, 1+1000 * sizeof(cell_t) / conn->bandwidth);
  470. }
  471. void connection_init_timeval(connection_t *conn) {
  472. assert(conn);
  473. if(gettimeofday(&conn->send_timeval,NULL) < 0)
  474. return;
  475. connection_increment_send_timeval(conn);
  476. }
  477. int connection_send_destroy(aci_t aci, connection_t *conn) {
  478. cell_t cell;
  479. assert(conn);
  480. if(!connection_speaks_cells(conn)) {
  481. log(LOG_INFO,"connection_send_destroy(): Aci %d: At an edge. Marking connection for close.", aci);
  482. conn->marked_for_close = 1;
  483. return 0;
  484. }
  485. memset(&cell, 0, sizeof(cell_t));
  486. cell.aci = aci;
  487. cell.command = CELL_DESTROY;
  488. log(LOG_INFO,"connection_send_destroy(): Sending destroy (aci %d).",aci);
  489. return connection_write_cell_to_buf(&cell, conn);
  490. }
  491. int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
  492. char networkcell[CELL_NETWORK_SIZE];
  493. char *n = networkcell;
  494. memset(n,0,CELL_NETWORK_SIZE); /* zero it out to start */
  495. *(aci_t *)n = htons(cellp->aci);
  496. *(n+2) = cellp->command;
  497. *(n+3) = cellp->length;
  498. /* seq is reserved, leave zero */
  499. memcpy(n+8,cellp->payload,CELL_PAYLOAD_SIZE);
  500. if(connection_encrypt_cell(n,conn)<0) {
  501. return -1;
  502. }
  503. return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  504. }
  505. int connection_encrypt_cell(char *cellp, connection_t *conn) {
  506. char cryptcell[CELL_NETWORK_SIZE];
  507. #if 0
  508. int x;
  509. char *px;
  510. printf("Sending: Cell header plaintext: ");
  511. px = (char *)cellp;
  512. for(x=0;x<8;x++) {
  513. printf("%u ",px[x]);
  514. }
  515. printf("\n");
  516. #endif
  517. if(crypto_cipher_encrypt(conn->f_crypto, cellp, CELL_NETWORK_SIZE, cryptcell)) {
  518. log(LOG_ERR,"Could not encrypt cell for connection %s:%u.",conn->address,conn->port);
  519. return -1;
  520. }
  521. #if 0
  522. printf("Sending: Cell header crypttext: ");
  523. px = (char *)&newcell;
  524. for(x=0;x<8;x++) {
  525. printf("%u ",px[x]);
  526. }
  527. printf("\n");
  528. #endif
  529. memcpy(cellp,cryptcell,CELL_NETWORK_SIZE);
  530. return 0;
  531. }
  532. int connection_process_inbuf(connection_t *conn) {
  533. assert(conn);
  534. switch(conn->type) {
  535. case CONN_TYPE_OP:
  536. return connection_op_process_inbuf(conn);
  537. case CONN_TYPE_OR:
  538. return connection_or_process_inbuf(conn);
  539. case CONN_TYPE_EXIT:
  540. case CONN_TYPE_AP:
  541. return connection_edge_process_inbuf(conn);
  542. case CONN_TYPE_DIR:
  543. return connection_dir_process_inbuf(conn);
  544. case CONN_TYPE_DNSMASTER:
  545. return connection_dns_process_inbuf(conn);
  546. default:
  547. log(LOG_DEBUG,"connection_process_inbuf() got unexpected conn->type.");
  548. return -1;
  549. }
  550. }
  551. int connection_package_raw_inbuf(connection_t *conn) {
  552. int amount_to_process, len;
  553. cell_t cell;
  554. circuit_t *circ;
  555. assert(conn);
  556. assert(!connection_speaks_cells(conn));
  557. /* this function should never get called if the receive_topicwindow is 0 */
  558. repeat_connection_package_raw_inbuf:
  559. amount_to_process = conn->inbuf_datalen;
  560. if(!amount_to_process)
  561. return 0;
  562. /* Initialize the cell with 0's */
  563. memset(&cell, 0, sizeof(cell_t));
  564. #ifdef USE_ZLIB
  565. /* This compression logic is not necessarily optimal:
  566. * 1) Maybe we should try to read as much as we can onto the inbuf before
  567. * compressing.
  568. * 2)
  569. */
  570. len = connection_compress_from_buf(cell.payload + TOPIC_HEADER_SIZE,
  571. CELL_PAYLOAD_SIZE - TOPIC_HEADER_SIZE,
  572. conn, Z_SYNC_FLUSH);
  573. if (len < 0)
  574. return -1;
  575. cell.length = len;
  576. #else
  577. if(amount_to_process > CELL_PAYLOAD_SIZE - TOPIC_HEADER_SIZE) {
  578. cell.length = CELL_PAYLOAD_SIZE - TOPIC_HEADER_SIZE;
  579. } else {
  580. cell.length = amount_to_process;
  581. }
  582. if(connection_fetch_from_buf(cell.payload+TOPIC_HEADER_SIZE, cell.length, conn) < 0)
  583. return -1;
  584. #endif
  585. circ = circuit_get_by_conn(conn);
  586. if(!circ) {
  587. log(LOG_DEBUG,"connection_package_raw_inbuf(): conn has no circuits!");
  588. return -1;
  589. }
  590. log(LOG_DEBUG,"connection_package_raw_inbuf(): (%d) Packaging %d bytes (%d waiting).",conn->s,cell.length, conn->inbuf_datalen);
  591. *(uint16_t *)(cell.payload+2) = htons(conn->topic_id);
  592. *cell.payload = TOPIC_COMMAND_DATA;
  593. cell.length += TOPIC_HEADER_SIZE;
  594. cell.command = CELL_DATA;
  595. if(conn->type == CONN_TYPE_EXIT) {
  596. cell.aci = circ->p_aci;
  597. if(circuit_deliver_data_cell_from_edge(&cell, circ, EDGE_EXIT) < 0) {
  598. log(LOG_DEBUG,"connection_package_raw_inbuf(): circuit_deliver_data_cell_from_edge (backward) failed. Closing.");
  599. circuit_close(circ);
  600. return 0;
  601. }
  602. assert(conn->n_receive_topicwindow > 0);
  603. if(--conn->n_receive_topicwindow <= 0) { /* is it 0 after decrement? */
  604. connection_stop_reading(conn);
  605. log(LOG_DEBUG,"connection_package_raw_inbuf(): receive_topicwindow at exit reached 0.");
  606. return 0; /* don't process the inbuf any more */
  607. }
  608. log(LOG_DEBUG,"connection_package_raw_inbuf(): receive_topicwindow at exit is %d",conn->n_receive_topicwindow);
  609. } else { /* send it forward. we're an AP */
  610. assert(conn->type == CONN_TYPE_AP);
  611. cell.aci = circ->n_aci;
  612. if(circuit_deliver_data_cell_from_edge(&cell, circ, EDGE_AP) < 0) {
  613. log(LOG_DEBUG,"connection_package_raw_inbuf(): circuit_deliver_data_cell_from_edge (forward) failed. Closing.");
  614. circuit_close(circ);
  615. return 0;
  616. }
  617. assert(conn->p_receive_topicwindow > 0);
  618. if(--conn->p_receive_topicwindow <= 0) { /* is it 0 after decrement? */
  619. connection_stop_reading(conn);
  620. log(LOG_DEBUG,"connection_package_raw_inbuf(): receive_topicwindow at AP reached 0.");
  621. return 0; /* don't process the inbuf any more */
  622. }
  623. log(LOG_DEBUG,"connection_package_raw_inbuf(): receive_topicwindow at AP is %d",conn->p_receive_topicwindow);
  624. }
  625. /* handle more if there's more, or return 0 if there isn't */
  626. goto repeat_connection_package_raw_inbuf;
  627. }
  628. int connection_consider_sending_sendme(connection_t *conn, int edge_type) {
  629. circuit_t *circ;
  630. cell_t cell;
  631. if(connection_outbuf_too_full(conn))
  632. return 0;
  633. circ = circuit_get_by_conn(conn);
  634. if(!circ) {
  635. /* this can legitimately happen if the destroy has already arrived and torn down the circuit */
  636. log(LOG_DEBUG,"connection_consider_sending_sendme(): No circuit associated with conn. Skipping.");
  637. return 0;
  638. }
  639. memset(&cell, 0, sizeof(cell_t));
  640. *(uint16_t *)(cell.payload+2) = htons(conn->topic_id);
  641. *cell.payload = TOPIC_COMMAND_SENDME;
  642. cell.length += TOPIC_HEADER_SIZE;
  643. cell.command = CELL_DATA;
  644. if(edge_type == EDGE_EXIT) { /* we're at an exit */
  645. if(conn->p_receive_topicwindow < TOPICWINDOW_START - TOPICWINDOW_INCREMENT) {
  646. log(LOG_DEBUG,"connection_consider_sending_sendme(): Outbuf %d, Queueing topic sendme back.", conn->outbuf_flushlen);
  647. conn->p_receive_topicwindow += TOPICWINDOW_INCREMENT;
  648. cell.aci = circ->p_aci;
  649. if(circuit_deliver_data_cell_from_edge(&cell, circ, edge_type) < 0) {
  650. log(LOG_DEBUG,"connection_consider_sending_sendme(): circuit_deliver_data_cell_from_edge (backward) failed. Closing.");
  651. circuit_close(circ);
  652. return 0;
  653. }
  654. }
  655. } else { /* we're at an AP */
  656. assert(edge_type == EDGE_AP);
  657. if(conn->n_receive_topicwindow < TOPICWINDOW_START-TOPICWINDOW_INCREMENT) {
  658. log(LOG_DEBUG,"connection_consider_sending_sendme(): Outbuf %d, Queueing topic sendme forward.", conn->outbuf_flushlen);
  659. conn->n_receive_topicwindow += TOPICWINDOW_INCREMENT;
  660. cell.aci = circ->n_aci;
  661. if(circuit_deliver_data_cell_from_edge(&cell, circ, edge_type) < 0) {
  662. log(LOG_DEBUG,"connection_consider_sending_sendme(): circuit_deliver_data_cell_from_edge (forward) failed. Closing.");
  663. circuit_close(circ);
  664. return 0;
  665. }
  666. }
  667. }
  668. return 0;
  669. }
  670. int connection_finished_flushing(connection_t *conn) {
  671. assert(conn);
  672. // log(LOG_DEBUG,"connection_finished_flushing() entered. Socket %u.", conn->s);
  673. switch(conn->type) {
  674. case CONN_TYPE_OP:
  675. return connection_op_finished_flushing(conn);
  676. case CONN_TYPE_OR:
  677. return connection_or_finished_flushing(conn);
  678. case CONN_TYPE_AP:
  679. case CONN_TYPE_EXIT:
  680. return connection_edge_finished_flushing(conn);
  681. case CONN_TYPE_DIR:
  682. return connection_dir_finished_flushing(conn);
  683. case CONN_TYPE_DNSMASTER:
  684. return connection_dns_finished_flushing(conn);
  685. default:
  686. log(LOG_DEBUG,"connection_finished_flushing() got unexpected conn->type.");
  687. return -1;
  688. }
  689. }
  690. int connection_process_cell_from_inbuf(connection_t *conn) {
  691. /* check if there's a whole cell there.
  692. * if yes, pull it off, decrypt it, and process it.
  693. */
  694. char crypted[CELL_NETWORK_SIZE];
  695. char outbuf[1024];
  696. // int x;
  697. cell_t cell;
  698. if(conn->inbuf_datalen < CELL_NETWORK_SIZE) /* entire response available? */
  699. return 0; /* not yet */
  700. if(connection_fetch_from_buf(crypted,CELL_NETWORK_SIZE,conn) < 0) {
  701. return -1;
  702. }
  703. #if 0
  704. printf("Cell header crypttext: ");
  705. for(x=0;x<8;x++) {
  706. printf("%u ",crypted[x]);
  707. }
  708. printf("\n");
  709. #endif
  710. /* decrypt */
  711. if(crypto_cipher_decrypt(conn->b_crypto,crypted,CELL_NETWORK_SIZE,outbuf)) {
  712. log(LOG_ERR,"connection_process_cell_from_inbuf(): Decryption failed, dropping.");
  713. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  714. }
  715. // log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Cell decrypted (%d bytes).",outlen);
  716. #if 0
  717. printf("Cell header plaintext: ");
  718. for(x=0;x<8;x++) {
  719. printf("%u ",outbuf[x]);
  720. }
  721. printf("\n");
  722. #endif
  723. /* retrieve cell info from outbuf (create the host-order struct from the network-order string) */
  724. memset(&cell,0,sizeof(cell_t)); /* zero it out to start */
  725. cell.aci = ntohs(*(aci_t *)outbuf);
  726. cell.command = *(outbuf+2);
  727. cell.length = *(outbuf+3);
  728. memcpy(cell.payload, outbuf+8, CELL_PAYLOAD_SIZE);
  729. // log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci);
  730. command_process_cell(&cell, conn);
  731. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  732. }
  733. /*
  734. Local Variables:
  735. mode:c
  736. indent-tabs-mode:nil
  737. c-basic-offset:2
  738. End:
  739. */