connection_or.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char connection_or_c_id[] = "$Id$";
  7. /**
  8. * \file connection_or.c
  9. * \brief Functions to handle OR connections, TLS handshaking, and
  10. * cells on the network.
  11. **/
  12. #include "or.h"
  13. /** How much clock skew do we tolerate when checking certificates for
  14. * known routers? (sec) */
  15. #define TIGHT_CERT_ALLOW_SKEW (90*60)
  16. static int connection_tls_finish_handshake(connection_t *conn);
  17. static int connection_or_process_cells_from_inbuf(connection_t *conn);
  18. /**************************************************************/
  19. /** Pack the cell_t host-order structure <b>src</b> into network-order
  20. * in the buffer <b>dest</b>. See tor-spec.txt for details about the
  21. * wire format.
  22. */
  23. static void cell_pack(char *dest, const cell_t *src) {
  24. *(uint16_t*)dest = htons(src->circ_id);
  25. *(uint8_t*)(dest+2) = src->command;
  26. memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
  27. }
  28. /** Unpack the network-order buffer <b>src</b> into a host-order
  29. * cell_t structure <b>dest</b>.
  30. */
  31. static void cell_unpack(cell_t *dest, const char *src) {
  32. dest->circ_id = ntohs(*(uint16_t*)(src));
  33. dest->command = *(uint8_t*)(src+2);
  34. memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
  35. }
  36. int connection_or_reached_eof(connection_t *conn) {
  37. log_fn(LOG_INFO,"OR connection reached EOF. Closing.");
  38. connection_mark_for_close(conn);
  39. return 0;
  40. }
  41. /** Read conn's inbuf. If the http response from the proxy is all
  42. * here, make sure it's good news, and begin the tls handshake. If
  43. * it's bad news, close the connection and return -1. Else return 0
  44. * and hope for better luck next time.
  45. */
  46. static int
  47. connection_or_read_proxy_response(connection_t *conn) {
  48. char *headers;
  49. char *reason=NULL;
  50. int status_code;
  51. time_t date_header;
  52. int compression;
  53. switch (fetch_from_buf_http(conn->inbuf,
  54. &headers, MAX_HEADERS_SIZE,
  55. NULL, NULL, 10000)) {
  56. case -1: /* overflow */
  57. log_fn(LOG_WARN,"Your https proxy sent back an oversized response. Closing.");
  58. return -1;
  59. case 0:
  60. log_fn(LOG_INFO,"https proxy response not all here yet. Waiting.");
  61. return 0;
  62. /* case 1, fall through */
  63. }
  64. if (parse_http_response(headers, &status_code, &date_header,
  65. &compression, &reason) < 0) {
  66. log_fn(LOG_WARN,"Unparseable headers (connecting to '%s'). Closing.",
  67. conn->address);
  68. tor_free(headers);
  69. return -1;
  70. }
  71. if (!reason) reason = tor_strdup("[no reason given]");
  72. if (status_code == 200) {
  73. log_fn(LOG_INFO,
  74. "HTTPS connect to '%s' successful! (200 \"%s\") Starting TLS.",
  75. conn->address, reason);
  76. tor_free(reason);
  77. if (connection_tls_start_handshake(conn, 0) < 0) {
  78. /* TLS handshaking error of some kind. */
  79. connection_mark_for_close(conn);
  80. return -1;
  81. }
  82. return 0;
  83. }
  84. /* else, bad news on the status code */
  85. log_fn(LOG_WARN,"The https proxy sent back an unexpected status code %d (\"%s\"). Closing.",
  86. status_code, reason);
  87. tor_free(reason);
  88. connection_mark_for_close(conn);
  89. return -1;
  90. }
  91. /** Handle any new bytes that have come in on connection <b>conn</b>.
  92. * If conn is in 'open' state, hand it to
  93. * connection_or_process_cells_from_inbuf()
  94. * (else do nothing).
  95. */
  96. int connection_or_process_inbuf(connection_t *conn) {
  97. tor_assert(conn);
  98. tor_assert(conn->type == CONN_TYPE_OR);
  99. switch (conn->state) {
  100. case OR_CONN_STATE_PROXY_READING:
  101. return connection_or_read_proxy_response(conn);
  102. case OR_CONN_STATE_OPEN:
  103. return connection_or_process_cells_from_inbuf(conn);
  104. default:
  105. return 0; /* don't do anything */
  106. }
  107. }
  108. /** Connection <b>conn</b> has finished writing and has no bytes left on
  109. * its outbuf.
  110. *
  111. * Otherwise it's in state "open": stop writing and return.
  112. *
  113. * If <b>conn</b> is broken, mark it for close and return -1, else
  114. * return 0.
  115. */
  116. int connection_or_finished_flushing(connection_t *conn) {
  117. tor_assert(conn);
  118. tor_assert(conn->type == CONN_TYPE_OR);
  119. assert_connection_ok(conn,0);
  120. switch (conn->state) {
  121. case OR_CONN_STATE_PROXY_FLUSHING:
  122. log_fn(LOG_DEBUG,"finished sending CONNECT to proxy.");
  123. conn->state = OR_CONN_STATE_PROXY_READING;
  124. connection_stop_writing(conn);
  125. break;
  126. case OR_CONN_STATE_OPEN:
  127. connection_stop_writing(conn);
  128. break;
  129. default:
  130. log_fn(LOG_WARN,"BUG: called in unexpected state %d.", conn->state);
  131. #ifdef TOR_FRAGILE
  132. tor_assert(0);
  133. #endif
  134. return -1;
  135. }
  136. return 0;
  137. }
  138. /** Connected handler for OR connections: begin the TLS handshake.
  139. */
  140. int connection_or_finished_connecting(connection_t *conn)
  141. {
  142. tor_assert(conn);
  143. tor_assert(conn->type == CONN_TYPE_OR);
  144. tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
  145. log_fn(LOG_INFO,"OR connect() to router at %s:%u finished.",
  146. conn->address,conn->port);
  147. if (get_options()->HttpsProxy) {
  148. char buf[1024];
  149. char addrbuf[INET_NTOA_BUF_LEN];
  150. struct in_addr in;
  151. in.s_addr = htonl(conn->addr);
  152. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  153. tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
  154. addrbuf, conn->port);
  155. connection_write_to_buf(buf, strlen(buf), conn);
  156. conn->state = OR_CONN_STATE_PROXY_FLUSHING;
  157. return 0;
  158. }
  159. if (connection_tls_start_handshake(conn, 0) < 0) {
  160. /* TLS handshaking error of some kind. */
  161. connection_mark_for_close(conn);
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. /** Initialize <b>conn</b> to include all the relevant data from <b>router</b>.
  167. * This function is called either from connection_or_connect(), if
  168. * we initiated the connect, or from connection_tls_finish_handshake()
  169. * if the other side initiated it.
  170. */
  171. static void
  172. connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router) {
  173. or_options_t *options = get_options();
  174. conn->addr = router->addr;
  175. conn->port = router->or_port;
  176. conn->receiver_bucket = conn->bandwidth = (int)options->BandwidthBurst;
  177. conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
  178. crypto_pk_get_digest(conn->identity_pkey, conn->identity_digest);
  179. conn->nickname = tor_strdup(router->nickname);
  180. tor_free(conn->address);
  181. conn->address = tor_strdup(router->address);
  182. }
  183. static void
  184. connection_or_init_conn_from_address(connection_t *conn,
  185. uint32_t addr, uint16_t port,
  186. const char *id_digest)
  187. {
  188. struct in_addr in;
  189. const char *n;
  190. or_options_t *options = get_options();
  191. routerinfo_t *r = router_get_by_digest(id_digest);
  192. if (r) {
  193. connection_or_init_conn_from_router(conn,r);
  194. return;
  195. }
  196. conn->addr = addr;
  197. conn->port = port;
  198. /* This next part isn't really right, but it's good enough for now. */
  199. conn->receiver_bucket = conn->bandwidth = (int)options->BandwidthBurst;
  200. memcpy(conn->identity_digest, id_digest, DIGEST_LEN);
  201. /* If we're an authoritative directory server, we may know a
  202. * nickname for this router. */
  203. n = dirserv_get_nickname_by_digest(id_digest);
  204. if (n) {
  205. conn->nickname = tor_strdup(n);
  206. } else {
  207. conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
  208. conn->nickname[0] = '$';
  209. base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
  210. conn->identity_digest, DIGEST_LEN);
  211. }
  212. tor_free(conn->address);
  213. in.s_addr = htonl(addr);
  214. conn->address = tor_malloc(INET_NTOA_BUF_LEN);
  215. tor_inet_ntoa(&in,conn->address,INET_NTOA_BUF_LEN);
  216. }
  217. void
  218. connection_or_update_nickname(connection_t *conn)
  219. {
  220. routerinfo_t *r;
  221. const char *n;
  222. tor_assert(conn);
  223. tor_assert(conn->type == CONN_TYPE_OR);
  224. n = dirserv_get_nickname_by_digest(conn->identity_digest);
  225. if (n) {
  226. if (!conn->nickname || strcmp(conn->nickname, n)) {
  227. tor_free(conn->nickname);
  228. conn->nickname = tor_strdup(n);
  229. }
  230. return;
  231. }
  232. r = router_get_by_digest(conn->identity_digest);
  233. if (r && r->is_verified) {
  234. if (!conn->nickname || strcmp(conn->nickname, r->nickname)) {
  235. tor_free(conn->nickname);
  236. conn->nickname = tor_strdup(r->nickname);
  237. }
  238. return;
  239. }
  240. if (conn->nickname[0] != '$') {
  241. tor_free(conn->nickname);
  242. conn->nickname = tor_malloc(HEX_DIGEST_LEN+1);
  243. base16_encode(conn->nickname, HEX_DIGEST_LEN+1,
  244. conn->identity_digest, DIGEST_LEN);
  245. }
  246. }
  247. /** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
  248. * handshake with an OR with identity digest <b>id_digest</b>.
  249. *
  250. * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
  251. * return that connection. If the connect() is in progress, set the
  252. * new conn's state to 'connecting' and return it. If connect() succeeds,
  253. * call * connection_tls_start_handshake() on it.
  254. *
  255. * This function is called from router_retry_connections(), for
  256. * ORs connecting to ORs, and circuit_establish_circuit(), for
  257. * OPs connecting to ORs.
  258. *
  259. * Return the launched conn, or NULL if it failed.
  260. */
  261. connection_t *connection_or_connect(uint32_t addr, uint16_t port,
  262. const char *id_digest) {
  263. connection_t *conn;
  264. routerinfo_t *me;
  265. or_options_t *options = get_options();
  266. tor_assert(id_digest);
  267. if (server_mode(options) && (me=router_get_my_routerinfo()) &&
  268. !memcmp(me->identity_digest, id_digest,DIGEST_LEN)) {
  269. log_fn(LOG_WARN,"Bug: Client asked me to connect to myself! Refusing.");
  270. return NULL;
  271. }
  272. /* this function should never be called if we're already connected to
  273. * id_digest, but check first to be sure */
  274. /*XXX this is getting called, at least by dirservers. */
  275. conn = connection_get_by_identity_digest(id_digest, CONN_TYPE_OR);
  276. if (conn) {
  277. tor_assert(conn->nickname);
  278. log_fn(LOG_WARN,"Asked me to connect to router '%s', but there's already a connection.", conn->nickname);
  279. return conn;
  280. }
  281. conn = connection_new(CONN_TYPE_OR);
  282. /* set up conn so it's got all the data we need to remember */
  283. connection_or_init_conn_from_address(conn, addr, port, id_digest);
  284. conn->state = OR_CONN_STATE_CONNECTING;
  285. control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED);
  286. if (options->HttpsProxy) {
  287. /* we shouldn't connect directly. use the https proxy instead. */
  288. addr = options->HttpsProxyAddr;
  289. port = options->HttpsProxyPort;
  290. }
  291. switch (connection_connect(conn, conn->address, addr, port)) {
  292. case -1:
  293. if (!options->HttpsProxy)
  294. router_mark_as_down(conn->identity_digest);
  295. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  296. connection_free(conn);
  297. return NULL;
  298. case 0:
  299. connection_watch_events(conn, EV_READ | EV_WRITE);
  300. /* writable indicates finish, readable indicates broken link,
  301. error indicates broken link on windows */
  302. return conn;
  303. /* case 1: fall through */
  304. }
  305. if (connection_or_finished_connecting(conn) < 0) {
  306. /* already marked for close */
  307. return NULL;
  308. }
  309. return conn;
  310. }
  311. /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
  312. * we initiated the connection, else it's 1.
  313. *
  314. * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and pass
  315. * <b>conn</b> to connection_tls_continue_handshake().
  316. *
  317. * Return -1 if <b>conn</b> is broken, else return 0.
  318. */
  319. int connection_tls_start_handshake(connection_t *conn, int receiving) {
  320. conn->state = OR_CONN_STATE_HANDSHAKING;
  321. conn->tls = tor_tls_new(conn->s, receiving, 0);
  322. if (!conn->tls) {
  323. log_fn(LOG_WARN,"tor_tls_new failed. Closing.");
  324. return -1;
  325. }
  326. connection_start_reading(conn);
  327. log_fn(LOG_DEBUG,"starting the handshake");
  328. if (connection_tls_continue_handshake(conn) < 0) {
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. /** Move forward with the tls handshake. If it finishes, hand
  334. * <b>conn</b> to connection_tls_finish_handshake().
  335. *
  336. * Return -1 if <b>conn</b> is broken, else return 0.
  337. */
  338. int connection_tls_continue_handshake(connection_t *conn) {
  339. switch (tor_tls_handshake(conn->tls)) {
  340. case TOR_TLS_ERROR:
  341. case TOR_TLS_CLOSE:
  342. log_fn(LOG_INFO,"tls error. breaking.");
  343. return -1;
  344. case TOR_TLS_DONE:
  345. return connection_tls_finish_handshake(conn);
  346. case TOR_TLS_WANTWRITE:
  347. connection_start_writing(conn);
  348. log_fn(LOG_DEBUG,"wanted write");
  349. return 0;
  350. case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
  351. log_fn(LOG_DEBUG,"wanted read");
  352. return 0;
  353. }
  354. return 0;
  355. }
  356. static char ZERO_DIGEST[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  357. int connection_or_nonopen_was_started_here(connection_t *conn)
  358. {
  359. tor_assert(sizeof(ZERO_DIGEST) == DIGEST_LEN);
  360. tor_assert(conn->type == CONN_TYPE_OR);
  361. if (!memcmp(ZERO_DIGEST, conn->identity_digest, DIGEST_LEN))
  362. return 0;
  363. else
  364. return 1;
  365. }
  366. /** The tls handshake is finished.
  367. *
  368. * Make sure we are happy with the person we just handshaked with:
  369. * If it's an OP (that is, it has no certificate), make sure I'm an OR.
  370. * If it's an OR (it has a certificate), make sure it has a recognized
  371. * nickname, and its cert is signed by the identity key of that nickname.
  372. * If I initiated the connection, make sure it's the right guy; and if
  373. * he initiated the connection, make sure he's not already connected.
  374. *
  375. * If he initiated the conn, also initialize conn from the information
  376. * in router.
  377. *
  378. * If either of us is an OP, set bandwidth to the default OP bandwidth.
  379. *
  380. * If all is successful and he's an OR, then call circuit_n_conn_done()
  381. * to handle events that have been pending on the tls handshake
  382. * completion, and set the directory to be dirty (only matters if I'm
  383. * an authdirserver).
  384. */
  385. static int
  386. connection_tls_finish_handshake(connection_t *conn) {
  387. routerinfo_t *router;
  388. char nickname[MAX_NICKNAME_LEN+1];
  389. connection_t *c;
  390. crypto_pk_env_t *identity_rcvd=NULL;
  391. char digest_rcvd[DIGEST_LEN];
  392. or_options_t *options = get_options();
  393. conn->state = OR_CONN_STATE_OPEN;
  394. connection_watch_events(conn, EV_READ);
  395. log_fn(LOG_DEBUG,"tls handshake done. verifying.");
  396. if (! tor_tls_peer_has_cert(conn->tls)) {
  397. log_fn(LOG_INFO,"Peer didn't send a cert! Closing.");
  398. /* XXX we should handle this case rather than just closing. */
  399. return -1;
  400. }
  401. if (tor_tls_get_peer_cert_nickname(conn->tls, nickname, sizeof(nickname))) {
  402. log_fn(LOG_WARN,"Other side (%s:%d) has a cert without a valid nickname. Closing.",
  403. conn->address, conn->port);
  404. return -1;
  405. }
  406. log_fn(LOG_DEBUG, "Other side (%s:%d) claims to be router '%s'",
  407. conn->address, conn->port, nickname);
  408. if (tor_tls_verify(conn->tls, &identity_rcvd) < 0) {
  409. log_fn(LOG_WARN,"Other side, which claims to be router '%s' (%s:%d), has a cert but it's invalid. Closing.",
  410. nickname, conn->address, conn->port);
  411. return -1;
  412. }
  413. #if 0
  414. if (tor_tls_check_lifetime(conn->tls, LOOSE_CERT_ALLOW_SKEW)<0) {
  415. log_fn(LOG_WARN,"Other side '%s' (%s:%d) has a very highly skewed clock, or an expired certificate. Closing.",
  416. nickname, conn->address, conn->port);
  417. return -1;
  418. }
  419. #endif
  420. log_fn(LOG_DEBUG,"The router's cert is valid.");
  421. crypto_pk_get_digest(identity_rcvd, digest_rcvd);
  422. if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
  423. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  424. } else {
  425. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  426. }
  427. crypto_free_pk_env(identity_rcvd);
  428. router = router_get_by_nickname(nickname);
  429. if (router && /* we know this nickname */
  430. router->is_verified && /* make sure it's the right guy */
  431. memcmp(digest_rcvd, router->identity_digest, DIGEST_LEN) != 0) {
  432. log_fn(LOG_WARN, "Identity key not as expected for router claiming to be '%s' (%s:%d) ", nickname, conn->address, conn->port);
  433. return -1;
  434. }
  435. #if 0
  436. if (router_get_by_digest(digest_rcvd)) {
  437. /* This is a known router; don't cut it slack with its clock skew. */
  438. if (tor_tls_check_lifetime(conn->tls, TIGHT_CERT_ALLOW_SKEW)<0) {
  439. log_fn(LOG_WARN,"Router '%s' (%s:%d) has a skewed clock, or an expired certificate; or else our clock is skewed. Closing.",
  440. nickname, conn->address, conn->port);
  441. return -1;
  442. }
  443. }
  444. #endif
  445. if (connection_or_nonopen_was_started_here(conn)) {
  446. /* I initiated this connection. */
  447. if (conn->nickname[0] == '$') {
  448. /* I was aiming for a particular digest. Did I get it? */
  449. char d[HEX_DIGEST_LEN+1];
  450. base16_encode(d, HEX_DIGEST_LEN+1, digest_rcvd, DIGEST_LEN);
  451. if (strcasecmp(d,conn->nickname+1)) {
  452. log_fn(LOG_WARN, "Identity key not as expected for router at %s:%d: wanted %s but got %s",
  453. conn->address, conn->port, conn->nickname, d);
  454. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  455. return -1;
  456. }
  457. } else if (strcasecmp(conn->nickname, nickname)) {
  458. /* I was aiming for a nickname. Did I get it? */
  459. log_fn(authdir_mode(options) ? LOG_WARN : LOG_INFO,
  460. "Other side (%s:%d) is '%s', but we tried to connect to '%s'",
  461. conn->address, conn->port, nickname, conn->nickname);
  462. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  463. return -1;
  464. }
  465. } else {
  466. if ((c=connection_get_by_identity_digest(digest_rcvd, CONN_TYPE_OR))) {
  467. log_fn(LOG_INFO,"Router '%s' is already connected on fd %d. Dropping fd %d.", nickname, c->s, conn->s);
  468. return -1;
  469. }
  470. connection_or_init_conn_from_address(conn,conn->addr,conn->port,digest_rcvd);
  471. }
  472. if (!server_mode(options)) { /* If I'm an OP... */
  473. conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
  474. }
  475. directory_set_dirty();
  476. circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
  477. /* Note the success */
  478. rep_hist_note_connect_succeeded(conn->identity_digest, time(NULL));
  479. control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED);
  480. return 0;
  481. }
  482. /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s
  483. * outbuf.
  484. *
  485. * (Commented out) If it's an OR conn, and an entire TLS record is
  486. * ready, then try to flush the record now.
  487. */
  488. void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn) {
  489. char networkcell[CELL_NETWORK_SIZE];
  490. char *n = networkcell;
  491. tor_assert(cell);
  492. tor_assert(conn);
  493. tor_assert(connection_speaks_cells(conn));
  494. cell_pack(n, cell);
  495. connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  496. #if 0 /* commented out -- can we get away with not doing this,
  497. * because we're already round-robining in handle_read?
  498. */
  499. #define MIN_TLS_FLUSHLEN 15872
  500. /* openssl tls record size is 16383, this is close. The goal here is to
  501. * push data out as soon as we know there's enough for a tls record, so
  502. * during periods of high load we won't read the entire megabyte from
  503. * input before pushing any data out. */
  504. if (conn->outbuf_flushlen-CELL_NETWORK_SIZE < MIN_TLS_FLUSHLEN &&
  505. conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
  506. int extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
  507. conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
  508. if (connection_handle_write(conn) < 0) {
  509. log_fn(LOG_WARN,"flushing failed.");
  510. return;
  511. }
  512. if (extra) {
  513. conn->outbuf_flushlen += extra;
  514. connection_start_writing(conn);
  515. }
  516. }
  517. #endif
  518. }
  519. /** Process cells from <b>conn</b>'s inbuf.
  520. *
  521. * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
  522. * and hand it to command_process_cell().
  523. *
  524. * Always return 0.
  525. */
  526. static int connection_or_process_cells_from_inbuf(connection_t *conn) {
  527. char buf[CELL_NETWORK_SIZE];
  528. cell_t cell;
  529. loop:
  530. log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
  531. conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls));
  532. if (buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* entire response available? */
  533. return 0; /* not yet */
  534. connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
  535. /* retrieve cell info from buf (create the host-order struct from the
  536. * network-order string) */
  537. cell_unpack(&cell, buf);
  538. command_process_cell(&cell, conn);
  539. goto loop; /* process the remainder of the buffer */
  540. }