connection_or.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. tor_fragile_assert();
  132. return -1;
  133. }
  134. return 0;
  135. }
  136. /** Connected handler for OR connections: begin the TLS handshake.
  137. */
  138. int connection_or_finished_connecting(connection_t *conn)
  139. {
  140. tor_assert(conn);
  141. tor_assert(conn->type == CONN_TYPE_OR);
  142. tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
  143. log_fn(LOG_INFO,"OR connect() to router at %s:%u finished.",
  144. conn->address,conn->port);
  145. if (get_options()->HttpsProxy) {
  146. char buf[1024];
  147. char addrbuf[INET_NTOA_BUF_LEN];
  148. struct in_addr in;
  149. char *base64_authenticator=NULL;
  150. const char *authenticator = get_options()->HttpsProxyAuthenticator;
  151. in.s_addr = htonl(conn->addr);
  152. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  153. if (authenticator) {
  154. base64_authenticator = alloc_http_authenticator(authenticator);
  155. if (!base64_authenticator)
  156. log_fn(LOG_WARN, "Encoding https authenticator failed");
  157. }
  158. if (base64_authenticator) {
  159. tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
  160. "Proxy-Authorization: Basic %s\r\n\r\n", addrbuf,
  161. conn->port, base64_authenticator);
  162. tor_free(base64_authenticator);
  163. } else {
  164. tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
  165. addrbuf, conn->port);
  166. }
  167. connection_write_to_buf(buf, strlen(buf), conn);
  168. conn->state = OR_CONN_STATE_PROXY_FLUSHING;
  169. return 0;
  170. }
  171. if (connection_tls_start_handshake(conn, 0) < 0) {
  172. /* TLS handshaking error of some kind. */
  173. connection_mark_for_close(conn);
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. /** Initialize <b>conn</b> to include all the relevant data from <b>router</b>.
  179. * This function is called either from connection_or_connect(), if
  180. * we initiated the connect, or from connection_tls_finish_handshake()
  181. * if the other side initiated it.
  182. */
  183. static void
  184. connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router) {
  185. or_options_t *options = get_options();
  186. conn->addr = router->addr;
  187. conn->port = router->or_port;
  188. conn->receiver_bucket = conn->bandwidth = (int)options->BandwidthBurst;
  189. conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
  190. crypto_pk_get_digest(conn->identity_pkey, conn->identity_digest);
  191. conn->nickname = tor_strdup(router->nickname);
  192. tor_free(conn->address);
  193. conn->address = tor_strdup(router->address);
  194. }
  195. static void
  196. connection_or_init_conn_from_address(connection_t *conn,
  197. uint32_t addr, uint16_t port,
  198. const char *id_digest)
  199. {
  200. struct in_addr in;
  201. const char *n;
  202. or_options_t *options = get_options();
  203. routerinfo_t *r = router_get_by_digest(id_digest);
  204. if (r) {
  205. connection_or_init_conn_from_router(conn,r);
  206. return;
  207. }
  208. conn->addr = addr;
  209. conn->port = port;
  210. /* This next part isn't really right, but it's good enough for now. */
  211. conn->receiver_bucket = conn->bandwidth = (int)options->BandwidthBurst;
  212. memcpy(conn->identity_digest, id_digest, DIGEST_LEN);
  213. /* If we're an authoritative directory server, we may know a
  214. * nickname for this router. */
  215. n = dirserv_get_nickname_by_digest(id_digest);
  216. if (n) {
  217. conn->nickname = tor_strdup(n);
  218. } else {
  219. conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
  220. conn->nickname[0] = '$';
  221. base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
  222. conn->identity_digest, DIGEST_LEN);
  223. }
  224. tor_free(conn->address);
  225. in.s_addr = htonl(addr);
  226. conn->address = tor_malloc(INET_NTOA_BUF_LEN);
  227. tor_inet_ntoa(&in,conn->address,INET_NTOA_BUF_LEN);
  228. }
  229. void
  230. connection_or_update_nickname(connection_t *conn)
  231. {
  232. routerinfo_t *r;
  233. const char *n;
  234. tor_assert(conn);
  235. tor_assert(conn->type == CONN_TYPE_OR);
  236. n = dirserv_get_nickname_by_digest(conn->identity_digest);
  237. if (n) {
  238. if (!conn->nickname || strcmp(conn->nickname, n)) {
  239. tor_free(conn->nickname);
  240. conn->nickname = tor_strdup(n);
  241. }
  242. return;
  243. }
  244. r = router_get_by_digest(conn->identity_digest);
  245. if (r && r->is_verified) {
  246. if (!conn->nickname || strcmp(conn->nickname, r->nickname)) {
  247. tor_free(conn->nickname);
  248. conn->nickname = tor_strdup(r->nickname);
  249. }
  250. return;
  251. }
  252. if (conn->nickname[0] != '$') {
  253. tor_free(conn->nickname);
  254. conn->nickname = tor_malloc(HEX_DIGEST_LEN+1);
  255. base16_encode(conn->nickname, HEX_DIGEST_LEN+1,
  256. conn->identity_digest, DIGEST_LEN);
  257. }
  258. }
  259. /** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
  260. * handshake with an OR with identity digest <b>id_digest</b>.
  261. *
  262. * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
  263. * return that connection. If the connect() is in progress, set the
  264. * new conn's state to 'connecting' and return it. If connect() succeeds,
  265. * call * connection_tls_start_handshake() on it.
  266. *
  267. * This function is called from router_retry_connections(), for
  268. * ORs connecting to ORs, and circuit_establish_circuit(), for
  269. * OPs connecting to ORs.
  270. *
  271. * Return the launched conn, or NULL if it failed.
  272. */
  273. connection_t *connection_or_connect(uint32_t addr, uint16_t port,
  274. const char *id_digest) {
  275. connection_t *conn;
  276. routerinfo_t *me;
  277. or_options_t *options = get_options();
  278. tor_assert(id_digest);
  279. if (server_mode(options) && (me=router_get_my_routerinfo()) &&
  280. !memcmp(me->identity_digest, id_digest,DIGEST_LEN)) {
  281. log_fn(LOG_WARN,"Client asked me to connect to myself! Refusing.");
  282. return NULL;
  283. }
  284. /* this function should never be called if we're already connected to
  285. * id_digest, but check first to be sure */
  286. /*XXX this is getting called, at least by dirservers. */
  287. conn = connection_get_by_identity_digest(id_digest, CONN_TYPE_OR);
  288. if (conn) {
  289. tor_assert(conn->nickname);
  290. log_fn(LOG_WARN,"Asked me to connect to router '%s', but there's already a connection.", conn->nickname);
  291. return conn;
  292. }
  293. conn = connection_new(CONN_TYPE_OR);
  294. /* set up conn so it's got all the data we need to remember */
  295. connection_or_init_conn_from_address(conn, addr, port, id_digest);
  296. conn->state = OR_CONN_STATE_CONNECTING;
  297. control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED);
  298. if (options->HttpsProxy) {
  299. /* we shouldn't connect directly. use the https proxy instead. */
  300. addr = options->HttpsProxyAddr;
  301. port = options->HttpsProxyPort;
  302. }
  303. switch (connection_connect(conn, conn->address, addr, port)) {
  304. case -1:
  305. if (!options->HttpsProxy)
  306. router_mark_as_down(conn->identity_digest);
  307. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  308. connection_free(conn);
  309. return NULL;
  310. case 0:
  311. connection_watch_events(conn, EV_READ | EV_WRITE);
  312. /* writable indicates finish, readable indicates broken link,
  313. error indicates broken link on windows */
  314. return conn;
  315. /* case 1: fall through */
  316. }
  317. if (connection_or_finished_connecting(conn) < 0) {
  318. /* already marked for close */
  319. return NULL;
  320. }
  321. return conn;
  322. }
  323. /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
  324. * we initiated the connection, else it's 1.
  325. *
  326. * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and pass
  327. * <b>conn</b> to connection_tls_continue_handshake().
  328. *
  329. * Return -1 if <b>conn</b> is broken, else return 0.
  330. */
  331. int connection_tls_start_handshake(connection_t *conn, int receiving) {
  332. conn->state = OR_CONN_STATE_HANDSHAKING;
  333. conn->tls = tor_tls_new(conn->s, receiving, 0);
  334. if (!conn->tls) {
  335. log_fn(LOG_WARN,"tor_tls_new failed. Closing.");
  336. return -1;
  337. }
  338. connection_start_reading(conn);
  339. log_fn(LOG_DEBUG,"starting the handshake");
  340. if (connection_tls_continue_handshake(conn) < 0) {
  341. return -1;
  342. }
  343. return 0;
  344. }
  345. /** Move forward with the tls handshake. If it finishes, hand
  346. * <b>conn</b> to connection_tls_finish_handshake().
  347. *
  348. * Return -1 if <b>conn</b> is broken, else return 0.
  349. */
  350. int connection_tls_continue_handshake(connection_t *conn) {
  351. check_no_tls_errors();
  352. switch (tor_tls_handshake(conn->tls)) {
  353. case TOR_TLS_ERROR:
  354. case TOR_TLS_CLOSE:
  355. log_fn(LOG_INFO,"tls error. breaking.");
  356. return -1;
  357. case TOR_TLS_DONE:
  358. return connection_tls_finish_handshake(conn);
  359. case TOR_TLS_WANTWRITE:
  360. connection_start_writing(conn);
  361. log_fn(LOG_DEBUG,"wanted write");
  362. return 0;
  363. case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
  364. log_fn(LOG_DEBUG,"wanted read");
  365. return 0;
  366. }
  367. return 0;
  368. }
  369. static char ZERO_DIGEST[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  370. int connection_or_nonopen_was_started_here(connection_t *conn)
  371. {
  372. tor_assert(sizeof(ZERO_DIGEST) == DIGEST_LEN);
  373. tor_assert(conn->type == CONN_TYPE_OR);
  374. if (!memcmp(ZERO_DIGEST, conn->identity_digest, DIGEST_LEN))
  375. return 0;
  376. else
  377. return 1;
  378. }
  379. /** The tls handshake is finished.
  380. *
  381. * Make sure we are happy with the person we just handshaked with:
  382. * If it's an OP (that is, it has no certificate), make sure I'm an OR.
  383. * If it's an OR (it has a certificate), make sure it has a recognized
  384. * nickname, and its cert is signed by the identity key of that nickname.
  385. * If I initiated the connection, make sure it's the right guy; and if
  386. * he initiated the connection, make sure he's not already connected.
  387. *
  388. * If he initiated the conn, also initialize conn from the information
  389. * in router.
  390. *
  391. * If either of us is an OP, set bandwidth to the default OP bandwidth.
  392. *
  393. * If all is successful and he's an OR, then call circuit_n_conn_done()
  394. * to handle events that have been pending on the tls handshake
  395. * completion, and set the directory to be dirty (only matters if I'm
  396. * an authdirserver).
  397. */
  398. static int
  399. connection_tls_finish_handshake(connection_t *conn) {
  400. routerinfo_t *router;
  401. char nickname[MAX_NICKNAME_LEN+1];
  402. connection_t *c;
  403. crypto_pk_env_t *identity_rcvd=NULL;
  404. char digest_rcvd[DIGEST_LEN];
  405. or_options_t *options = get_options();
  406. int severity = (authdir_mode(options) || !server_mode(options))
  407. ? LOG_WARN : LOG_INFO;
  408. log_fn(LOG_DEBUG,"tls handshake done. verifying.");
  409. check_no_tls_errors();
  410. if (! tor_tls_peer_has_cert(conn->tls)) {
  411. log_fn(LOG_INFO,"Peer didn't send a cert! Closing.");
  412. /* XXX we should handle this case rather than just closing. */
  413. return -1;
  414. }
  415. check_no_tls_errors();
  416. if (tor_tls_get_peer_cert_nickname(conn->tls, nickname, sizeof(nickname))) {
  417. log_fn(LOG_WARN,"Other side (%s:%d) has a cert without a valid nickname. Closing.",
  418. conn->address, conn->port);
  419. return -1;
  420. }
  421. check_no_tls_errors();
  422. log_fn(LOG_DEBUG, "Other side (%s:%d) claims to be router '%s'",
  423. conn->address, conn->port, nickname);
  424. if (tor_tls_verify(conn->tls, &identity_rcvd) < 0) {
  425. log_fn(LOG_WARN,"Other side, which claims to be router '%s' (%s:%d), has a cert but it's invalid. Closing.",
  426. nickname, conn->address, conn->port);
  427. return -1;
  428. }
  429. check_no_tls_errors();
  430. #if 0
  431. if (tor_tls_check_lifetime(conn->tls, LOOSE_CERT_ALLOW_SKEW)<0) {
  432. log_fn(LOG_WARN,"Other side '%s' (%s:%d) has a very highly skewed clock, or an expired certificate. Closing.",
  433. nickname, conn->address, conn->port);
  434. return -1;
  435. }
  436. #endif
  437. log_fn(LOG_DEBUG,"The router's cert is valid.");
  438. crypto_pk_get_digest(identity_rcvd, digest_rcvd);
  439. if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
  440. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  441. } else {
  442. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  443. }
  444. crypto_free_pk_env(identity_rcvd);
  445. router = router_get_by_nickname(nickname);
  446. if (router && /* we know this nickname */
  447. router->is_verified && /* make sure it's the right guy */
  448. memcmp(digest_rcvd, router->identity_digest, DIGEST_LEN) != 0) {
  449. log_fn(severity,
  450. "Identity key not as expected for router claiming to be '%s' (%s:%d)",
  451. nickname, conn->address, conn->port);
  452. return -1;
  453. }
  454. #if 0
  455. if (router_get_by_digest(digest_rcvd)) {
  456. /* This is a known router; don't cut it slack with its clock skew. */
  457. if (tor_tls_check_lifetime(conn->tls, TIGHT_CERT_ALLOW_SKEW)<0) {
  458. log_fn(LOG_WARN,"Router '%s' (%s:%d) has a skewed clock, or an expired certificate; or else our clock is skewed. Closing.",
  459. nickname, conn->address, conn->port);
  460. return -1;
  461. }
  462. }
  463. #endif
  464. if (connection_or_nonopen_was_started_here(conn)) {
  465. if (authdir_mode(options)) {
  466. /* We initiated this connection to address:port. Drop all routers
  467. * with the same address:port and a different key or nickname.
  468. */
  469. dirserv_orconn_tls_done(conn->address, conn->port,
  470. digest_rcvd, nickname);
  471. }
  472. if (conn->nickname[0] == '$') {
  473. /* I was aiming for a particular digest. Did I get it? */
  474. char d[HEX_DIGEST_LEN+1];
  475. base16_encode(d, HEX_DIGEST_LEN+1, digest_rcvd, DIGEST_LEN);
  476. if (strcasecmp(d,conn->nickname+1)) {
  477. log_fn(severity,
  478. "Identity key not as expected for router at %s:%d: wanted %s but got %s",
  479. conn->address, conn->port, conn->nickname, d);
  480. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  481. return -1;
  482. }
  483. } else if (strcasecmp(conn->nickname, nickname)) {
  484. /* I was aiming for a nickname. Did I get it? */
  485. log_fn(severity,
  486. "Other side (%s:%d) is '%s', but we tried to connect to '%s'",
  487. conn->address, conn->port, nickname, conn->nickname);
  488. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  489. return -1;
  490. }
  491. } else {
  492. if ((c=connection_get_by_identity_digest(digest_rcvd, CONN_TYPE_OR))) {
  493. log_fn(LOG_INFO,"Router '%s' is already connected on fd %d. Dropping fd %d.", nickname, c->s, conn->s);
  494. return -1;
  495. }
  496. connection_or_init_conn_from_address(conn,conn->addr,conn->port,digest_rcvd);
  497. }
  498. if (!server_mode(options)) { /* If I'm an OP... */
  499. conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
  500. }
  501. directory_set_dirty();
  502. conn->state = OR_CONN_STATE_OPEN;
  503. connection_watch_events(conn, EV_READ);
  504. circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
  505. rep_hist_note_connect_succeeded(conn->identity_digest, time(NULL));
  506. control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED);
  507. return 0;
  508. }
  509. /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s
  510. * outbuf.
  511. *
  512. * (Commented out) If it's an OR conn, and an entire TLS record is
  513. * ready, then try to flush the record now.
  514. */
  515. void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn) {
  516. char networkcell[CELL_NETWORK_SIZE];
  517. char *n = networkcell;
  518. tor_assert(cell);
  519. tor_assert(conn);
  520. tor_assert(connection_speaks_cells(conn));
  521. cell_pack(n, cell);
  522. connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  523. #if 0 /* commented out -- can we get away with not doing this,
  524. * because we're already round-robining in handle_read?
  525. */
  526. #define MIN_TLS_FLUSHLEN 15872
  527. /* openssl tls record size is 16383, this is close. The goal here is to
  528. * push data out as soon as we know there's enough for a tls record, so
  529. * during periods of high load we won't read the entire megabyte from
  530. * input before pushing any data out. */
  531. if (conn->outbuf_flushlen-CELL_NETWORK_SIZE < MIN_TLS_FLUSHLEN &&
  532. conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
  533. int extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
  534. conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
  535. if (connection_handle_write(conn) < 0) {
  536. log_fn(LOG_WARN,"flushing failed.");
  537. return;
  538. }
  539. if (extra) {
  540. conn->outbuf_flushlen += extra;
  541. connection_start_writing(conn);
  542. }
  543. }
  544. #endif
  545. }
  546. /** Process cells from <b>conn</b>'s inbuf.
  547. *
  548. * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
  549. * and hand it to command_process_cell().
  550. *
  551. * Always return 0.
  552. */
  553. static int connection_or_process_cells_from_inbuf(connection_t *conn) {
  554. char buf[CELL_NETWORK_SIZE];
  555. cell_t cell;
  556. loop:
  557. log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
  558. conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls));
  559. if (buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* entire response available? */
  560. return 0; /* not yet */
  561. connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
  562. /* retrieve cell info from buf (create the host-order struct from the
  563. * network-order string) */
  564. cell_unpack(&cell, buf);
  565. command_process_cell(&cell, conn);
  566. goto loop; /* process the remainder of the buffer */
  567. }