connection_or.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. /** DOCDOC */
  209. static void
  210. connection_or_init_conn_from_address(connection_t *conn,
  211. uint32_t addr, uint16_t port,
  212. const char *id_digest)
  213. {
  214. struct in_addr in;
  215. const char *n;
  216. or_options_t *options = get_options();
  217. routerinfo_t *r = router_get_by_digest(id_digest);
  218. if (r) {
  219. connection_or_init_conn_from_router(conn,r);
  220. return;
  221. }
  222. conn->addr = addr;
  223. conn->port = port;
  224. /* This next part isn't really right, but it's good enough for now. */
  225. conn->receiver_bucket = conn->bandwidth = (int)options->BandwidthBurst;
  226. memcpy(conn->identity_digest, id_digest, DIGEST_LEN);
  227. /* If we're an authoritative directory server, we may know a
  228. * nickname for this router. */
  229. n = dirserv_get_nickname_by_digest(id_digest);
  230. if (n) {
  231. conn->nickname = tor_strdup(n);
  232. } else {
  233. conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
  234. conn->nickname[0] = '$';
  235. base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
  236. conn->identity_digest, DIGEST_LEN);
  237. }
  238. tor_free(conn->address);
  239. in.s_addr = htonl(addr);
  240. conn->address = tor_malloc(INET_NTOA_BUF_LEN);
  241. tor_inet_ntoa(&in,conn->address,INET_NTOA_BUF_LEN);
  242. }
  243. /** DOCDOC */
  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. /* this function should never be called if we're already connected to
  301. * id_digest, but check first to be sure */
  302. /*XXX this is getting called, at least by dirservers. */
  303. conn = connection_get_by_identity_digest(id_digest, CONN_TYPE_OR);
  304. if (conn) {
  305. tor_assert(conn->nickname);
  306. log_fn(LOG_WARN,"Asked me to connect to router '%s', but there's already a connection.", conn->nickname);
  307. return conn;
  308. }
  309. conn = connection_new(CONN_TYPE_OR);
  310. /* set up conn so it's got all the data we need to remember */
  311. connection_or_init_conn_from_address(conn, addr, port, id_digest);
  312. conn->state = OR_CONN_STATE_CONNECTING;
  313. control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED);
  314. if (options->HttpsProxy) {
  315. /* we shouldn't connect directly. use the https proxy instead. */
  316. addr = options->HttpsProxyAddr;
  317. port = options->HttpsProxyPort;
  318. }
  319. switch (connection_connect(conn, conn->address, addr, port)) {
  320. case -1:
  321. if (!options->HttpsProxy)
  322. router_mark_as_down(conn->identity_digest);
  323. helper_node_set_status(conn->identity_digest, 0);
  324. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  325. connection_free(conn);
  326. return NULL;
  327. case 0:
  328. connection_watch_events(conn, EV_READ | EV_WRITE);
  329. /* writable indicates finish, readable indicates broken link,
  330. error indicates broken link on windows */
  331. return conn;
  332. /* case 1: fall through */
  333. }
  334. if (connection_or_finished_connecting(conn) < 0) {
  335. /* already marked for close */
  336. return NULL;
  337. }
  338. return conn;
  339. }
  340. /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
  341. * we initiated the connection, else it's 1.
  342. *
  343. * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and pass
  344. * <b>conn</b> to connection_tls_continue_handshake().
  345. *
  346. * Return -1 if <b>conn</b> is broken, else return 0.
  347. */
  348. int
  349. connection_tls_start_handshake(connection_t *conn, int receiving)
  350. {
  351. conn->state = OR_CONN_STATE_HANDSHAKING;
  352. conn->tls = tor_tls_new(conn->s, receiving, 0);
  353. if (!conn->tls) {
  354. log_fn(LOG_WARN,"tor_tls_new failed. Closing.");
  355. return -1;
  356. }
  357. connection_start_reading(conn);
  358. log_fn(LOG_DEBUG,"starting the handshake");
  359. if (connection_tls_continue_handshake(conn) < 0) {
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. /** Move forward with the tls handshake. If it finishes, hand
  365. * <b>conn</b> to connection_tls_finish_handshake().
  366. *
  367. * Return -1 if <b>conn</b> is broken, else return 0.
  368. */
  369. int
  370. connection_tls_continue_handshake(connection_t *conn)
  371. {
  372. check_no_tls_errors();
  373. switch (tor_tls_handshake(conn->tls)) {
  374. case TOR_TLS_ERROR:
  375. case TOR_TLS_CLOSE:
  376. log_fn(LOG_INFO,"tls error. breaking.");
  377. return -1;
  378. case TOR_TLS_DONE:
  379. return connection_tls_finish_handshake(conn);
  380. case TOR_TLS_WANTWRITE:
  381. connection_start_writing(conn);
  382. log_fn(LOG_DEBUG,"wanted write");
  383. return 0;
  384. case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
  385. log_fn(LOG_DEBUG,"wanted read");
  386. return 0;
  387. }
  388. return 0;
  389. }
  390. static char ZERO_DIGEST[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  391. /** Return 1 if we initiated this connection, or 0 if it started
  392. * out as an incoming connection.
  393. *
  394. * This is implemented for now by checking to see if
  395. * conn-\>identity_digest is set or not. Perhaps we should add a flag
  396. * one day so we're clearer.
  397. */
  398. int
  399. connection_or_nonopen_was_started_here(connection_t *conn)
  400. {
  401. tor_assert(sizeof(ZERO_DIGEST) == DIGEST_LEN);
  402. tor_assert(conn->type == CONN_TYPE_OR);
  403. if (!memcmp(ZERO_DIGEST, conn->identity_digest, DIGEST_LEN))
  404. return 0;
  405. else
  406. return 1;
  407. }
  408. /** Conn just completed its handshake. Return 0 if all is well, and
  409. * return -1 if he is lying, broken, or otherwise something is wrong.
  410. *
  411. * Make sure he sent a correctly formed certificate. If it has a
  412. * recognized (approved) nickname, make sure his identity key matches
  413. * to it. If I initiated the connection, make sure it's the right guy.
  414. *
  415. * If we return 0, write a hash of the identity key into digest_rcvd,
  416. * which must have DIGEST_LEN space in it. (If we return -1 this
  417. * buffer is undefined.)
  418. *
  419. * As side effects,
  420. * 1) Set conn->circ_id_type according to tor-spec.txt
  421. * 2) If we're an authdirserver and we initiated the connection: drop all
  422. * descriptors that claim to be on that IP/port but that aren't
  423. * this guy; and note that this guy is reachable.
  424. */
  425. static int
  426. connection_or_check_valid_handshake(connection_t *conn, char *digest_rcvd)
  427. {
  428. routerinfo_t *router;
  429. crypto_pk_env_t *identity_rcvd=NULL;
  430. char nickname[MAX_NICKNAME_LEN+1];
  431. or_options_t *options = get_options();
  432. int severity = (authdir_mode(options) || !server_mode(options))
  433. ? LOG_WARN : LOG_INFO;
  434. check_no_tls_errors();
  435. if (! tor_tls_peer_has_cert(conn->tls)) {
  436. log_fn(LOG_INFO,"Peer didn't send a cert! Closing.");
  437. return -1;
  438. }
  439. check_no_tls_errors();
  440. if (tor_tls_get_peer_cert_nickname(conn->tls, nickname, sizeof(nickname))) {
  441. log_fn(LOG_WARN,"Other side (%s:%d) has a cert without a valid nickname. Closing.",
  442. conn->address, conn->port);
  443. return -1;
  444. }
  445. check_no_tls_errors();
  446. log_fn(LOG_DEBUG, "Other side (%s:%d) claims to be router '%s'",
  447. conn->address, conn->port, nickname);
  448. if (tor_tls_verify(conn->tls, &identity_rcvd) < 0) {
  449. log_fn(LOG_WARN,"Other side, which claims to be router '%s' (%s:%d), has a cert but it's invalid. Closing.",
  450. nickname, conn->address, conn->port);
  451. return -1;
  452. }
  453. check_no_tls_errors();
  454. log_fn(LOG_DEBUG,"The router's cert is valid.");
  455. crypto_pk_get_digest(identity_rcvd, digest_rcvd);
  456. if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
  457. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  458. } else {
  459. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  460. }
  461. crypto_free_pk_env(identity_rcvd);
  462. router = router_get_by_nickname(nickname);
  463. if (router && /* we know this nickname */
  464. router->is_verified && /* make sure it's the right guy */
  465. memcmp(digest_rcvd, router->identity_digest, DIGEST_LEN) != 0) {
  466. log_fn(severity,
  467. "Identity key not as expected for router claiming to be '%s' (%s:%d)",
  468. nickname, conn->address, conn->port);
  469. return -1;
  470. }
  471. if (connection_or_nonopen_was_started_here(conn)) {
  472. int as_advertised = 1;
  473. if (conn->nickname[0] == '$') {
  474. /* I was aiming for a particular digest. Did I get it? */
  475. char d[HEX_DIGEST_LEN+1];
  476. base16_encode(d, HEX_DIGEST_LEN+1, digest_rcvd, DIGEST_LEN);
  477. if (strcasecmp(d,conn->nickname+1)) {
  478. log_fn(severity,
  479. "Identity key not as expected for router at %s:%d: wanted %s but got %s",
  480. conn->address, conn->port, conn->nickname+1, d);
  481. helper_node_set_status(conn->identity_digest, 0);
  482. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  483. as_advertised = 0;
  484. }
  485. } else if (strcasecmp(conn->nickname, nickname)) {
  486. /* I was aiming for a nickname. Did I get it? */
  487. log_fn(severity,
  488. "Other side (%s:%d) is '%s', but we tried to connect to '%s'",
  489. conn->address, conn->port, nickname, conn->nickname);
  490. helper_node_set_status(conn->identity_digest, 0);
  491. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  492. as_advertised = 0;
  493. }
  494. if (authdir_mode(options)) {
  495. /* We initiated this connection to address:port. Drop all routers
  496. * with the same address:port and a different key or nickname.
  497. */
  498. dirserv_orconn_tls_done(conn->address, conn->port,
  499. digest_rcvd, nickname, as_advertised);
  500. }
  501. if (!as_advertised)
  502. return -1;
  503. }
  504. return 0;
  505. }
  506. /** The tls handshake is finished.
  507. *
  508. * Make sure we are happy with the person we just handshaked with.
  509. *
  510. * If he initiated the connection, make sure he's not already connected,
  511. * then initialize conn from the information in router.
  512. *
  513. * If I'm not a server, set bandwidth to the default OP bandwidth.
  514. *
  515. * If all is successful, call circuit_n_conn_done() to handle events
  516. * that have been pending on the tls handshake completion. Also set the
  517. * directory to be dirty (only matters if I'm an authdirserver).
  518. */
  519. static int
  520. connection_tls_finish_handshake(connection_t *conn)
  521. {
  522. char digest_rcvd[DIGEST_LEN];
  523. log_fn(LOG_DEBUG,"tls handshake done. verifying.");
  524. if (connection_or_check_valid_handshake(conn, digest_rcvd) < 0)
  525. return -1;
  526. if (!connection_or_nonopen_was_started_here(conn)) {
  527. connection_t *c;
  528. if ((c=connection_get_by_identity_digest(digest_rcvd, CONN_TYPE_OR))) {
  529. log_fn(LOG_INFO,"Router '%s' is already connected on fd %d. Dropping fd %d.",
  530. c->nickname, c->s, conn->s);
  531. return -1;
  532. }
  533. connection_or_init_conn_from_address(conn,conn->addr,conn->port,digest_rcvd);
  534. }
  535. if (!server_mode(get_options())) { /* If I'm an OP... */
  536. conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
  537. }
  538. directory_set_dirty();
  539. conn->state = OR_CONN_STATE_OPEN;
  540. connection_watch_events(conn, EV_READ);
  541. circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
  542. rep_hist_note_connect_succeeded(conn->identity_digest, time(NULL));
  543. helper_node_set_status(conn->identity_digest, 1);
  544. control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED);
  545. return 0;
  546. }
  547. /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s
  548. * outbuf.
  549. *
  550. * (Commented out) If it's an OR conn, and an entire TLS record is
  551. * ready, then try to flush the record now.
  552. */
  553. void
  554. connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn)
  555. {
  556. char networkcell[CELL_NETWORK_SIZE];
  557. char *n = networkcell;
  558. tor_assert(cell);
  559. tor_assert(conn);
  560. tor_assert(connection_speaks_cells(conn));
  561. cell_pack(n, cell);
  562. connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  563. #define MIN_TLS_FLUSHLEN 15872
  564. /* openssl tls record size is 16383, this is close. The goal here is to
  565. * push data out as soon as we know there's enough for a tls record, so
  566. * during periods of high load we won't read the entire megabyte from
  567. * input before pushing any data out. It also has the feature of not
  568. * growing huge outbufs unless something is slow. */
  569. if (conn->outbuf_flushlen-CELL_NETWORK_SIZE < MIN_TLS_FLUSHLEN &&
  570. conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
  571. int extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
  572. conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
  573. if (connection_handle_write(conn) < 0) {
  574. if (!conn->marked_for_close) {
  575. /* this connection is broken. remove it. */
  576. log_fn(LOG_WARN,"Bug: unhandled error on write for OR conn (fd %d); removing",
  577. conn->s);
  578. tor_fragile_assert();
  579. conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
  580. /* XXX do we need a close-immediate here, so we don't try to flush? */
  581. connection_mark_for_close(conn);
  582. }
  583. return;
  584. }
  585. if (extra) {
  586. conn->outbuf_flushlen += extra;
  587. connection_start_writing(conn);
  588. }
  589. }
  590. }
  591. /** Process cells from <b>conn</b>'s inbuf.
  592. *
  593. * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
  594. * and hand it to command_process_cell().
  595. *
  596. * Always return 0.
  597. */
  598. static int
  599. connection_or_process_cells_from_inbuf(connection_t *conn)
  600. {
  601. char buf[CELL_NETWORK_SIZE];
  602. cell_t cell;
  603. loop:
  604. log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
  605. conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls));
  606. if (buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* entire response available? */
  607. return 0; /* not yet */
  608. connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
  609. /* retrieve cell info from buf (create the host-order struct from the
  610. * network-order string) */
  611. cell_unpack(&cell, buf);
  612. command_process_cell(&cell, conn);
  613. goto loop; /* process the remainder of the buffer */
  614. }