connection_or.c 21 KB

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