connection_or.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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_WARN,"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. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  324. connection_free(conn);
  325. return NULL;
  326. case 0:
  327. connection_watch_events(conn, EV_READ | EV_WRITE);
  328. /* writable indicates finish, readable indicates broken link,
  329. error indicates broken link on windows */
  330. return conn;
  331. /* case 1: fall through */
  332. }
  333. if (connection_or_finished_connecting(conn) < 0) {
  334. /* already marked for close */
  335. return NULL;
  336. }
  337. return conn;
  338. }
  339. /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
  340. * we initiated the connection, else it's 1.
  341. *
  342. * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and pass
  343. * <b>conn</b> to connection_tls_continue_handshake().
  344. *
  345. * Return -1 if <b>conn</b> is broken, else return 0.
  346. */
  347. int
  348. connection_tls_start_handshake(connection_t *conn, int receiving)
  349. {
  350. conn->state = OR_CONN_STATE_HANDSHAKING;
  351. conn->tls = tor_tls_new(conn->s, receiving, 0);
  352. if (!conn->tls) {
  353. log_fn(LOG_WARN,"tor_tls_new failed. Closing.");
  354. return -1;
  355. }
  356. connection_start_reading(conn);
  357. log_fn(LOG_DEBUG,"starting the handshake");
  358. if (connection_tls_continue_handshake(conn) < 0) {
  359. return -1;
  360. }
  361. return 0;
  362. }
  363. /** Move forward with the tls handshake. If it finishes, hand
  364. * <b>conn</b> to connection_tls_finish_handshake().
  365. *
  366. * Return -1 if <b>conn</b> is broken, else return 0.
  367. */
  368. int
  369. connection_tls_continue_handshake(connection_t *conn)
  370. {
  371. check_no_tls_errors();
  372. switch (tor_tls_handshake(conn->tls)) {
  373. case TOR_TLS_ERROR:
  374. case TOR_TLS_CLOSE:
  375. log_fn(LOG_INFO,"tls error. breaking.");
  376. return -1;
  377. case TOR_TLS_DONE:
  378. return connection_tls_finish_handshake(conn);
  379. case TOR_TLS_WANTWRITE:
  380. connection_start_writing(conn);
  381. log_fn(LOG_DEBUG,"wanted write");
  382. return 0;
  383. case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
  384. log_fn(LOG_DEBUG,"wanted read");
  385. return 0;
  386. }
  387. return 0;
  388. }
  389. static char ZERO_DIGEST[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  390. /** DOCDOC */
  391. int
  392. connection_or_nonopen_was_started_here(connection_t *conn)
  393. {
  394. tor_assert(sizeof(ZERO_DIGEST) == DIGEST_LEN);
  395. tor_assert(conn->type == CONN_TYPE_OR);
  396. if (!memcmp(ZERO_DIGEST, conn->identity_digest, DIGEST_LEN))
  397. return 0;
  398. else
  399. return 1;
  400. }
  401. /** The tls handshake is finished.
  402. *
  403. * Make sure we are happy with the person we just handshaked with:
  404. * If it's an OP (that is, it has no certificate), make sure I'm an OR.
  405. * If it's an OR (it has a certificate), make sure it has a recognized
  406. * nickname, and its cert is signed by the identity key of that nickname.
  407. * If I initiated the connection, make sure it's the right guy; and if
  408. * he initiated the connection, make sure he's not already connected.
  409. *
  410. * If he initiated the conn, also initialize conn from the information
  411. * in router.
  412. *
  413. * If either of us is an OP, set bandwidth to the default OP bandwidth.
  414. *
  415. * If all is successful and he's an OR, then call circuit_n_conn_done()
  416. * to handle events that have been pending on the tls handshake
  417. * completion, and set the directory to be dirty (only matters if I'm
  418. * an authdirserver).
  419. */
  420. static int
  421. connection_tls_finish_handshake(connection_t *conn)
  422. {
  423. routerinfo_t *router;
  424. char nickname[MAX_NICKNAME_LEN+1];
  425. connection_t *c;
  426. crypto_pk_env_t *identity_rcvd=NULL;
  427. char digest_rcvd[DIGEST_LEN];
  428. or_options_t *options = get_options();
  429. int severity = (authdir_mode(options) || !server_mode(options))
  430. ? LOG_WARN : LOG_INFO;
  431. log_fn(LOG_DEBUG,"tls handshake done. verifying.");
  432. check_no_tls_errors();
  433. if (! tor_tls_peer_has_cert(conn->tls)) {
  434. log_fn(LOG_INFO,"Peer didn't send a cert! Closing.");
  435. /* XXX we should handle this case rather than just closing. */
  436. return -1;
  437. }
  438. check_no_tls_errors();
  439. if (tor_tls_get_peer_cert_nickname(conn->tls, nickname, sizeof(nickname))) {
  440. log_fn(LOG_WARN,"Other side (%s:%d) has a cert without a valid nickname. Closing.",
  441. conn->address, conn->port);
  442. return -1;
  443. }
  444. check_no_tls_errors();
  445. log_fn(LOG_DEBUG, "Other side (%s:%d) claims to be router '%s'",
  446. conn->address, conn->port, nickname);
  447. if (tor_tls_verify(conn->tls, &identity_rcvd) < 0) {
  448. log_fn(LOG_WARN,"Other side, which claims to be router '%s' (%s:%d), has a cert but it's invalid. Closing.",
  449. nickname, conn->address, conn->port);
  450. return -1;
  451. }
  452. check_no_tls_errors();
  453. #if 0
  454. if (tor_tls_check_lifetime(conn->tls, LOOSE_CERT_ALLOW_SKEW)<0) {
  455. log_fn(LOG_WARN,"Other side '%s' (%s:%d) has a very highly skewed clock, or an expired certificate. Closing.",
  456. nickname, conn->address, conn->port);
  457. return -1;
  458. }
  459. #endif
  460. log_fn(LOG_DEBUG,"The router's cert is valid.");
  461. crypto_pk_get_digest(identity_rcvd, digest_rcvd);
  462. if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
  463. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  464. } else {
  465. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  466. }
  467. crypto_free_pk_env(identity_rcvd);
  468. router = router_get_by_nickname(nickname);
  469. if (router && /* we know this nickname */
  470. router->is_verified && /* make sure it's the right guy */
  471. memcmp(digest_rcvd, router->identity_digest, DIGEST_LEN) != 0) {
  472. log_fn(severity,
  473. "Identity key not as expected for router claiming to be '%s' (%s:%d)",
  474. nickname, conn->address, conn->port);
  475. return -1;
  476. }
  477. #if 0
  478. if (router_get_by_digest(digest_rcvd)) {
  479. /* This is a known router; don't cut it slack with its clock skew. */
  480. if (tor_tls_check_lifetime(conn->tls, TIGHT_CERT_ALLOW_SKEW)<0) {
  481. log_fn(LOG_WARN,"Router '%s' (%s:%d) has a skewed clock, or an expired certificate; or else our clock is skewed. Closing.",
  482. nickname, conn->address, conn->port);
  483. return -1;
  484. }
  485. }
  486. #endif
  487. if (connection_or_nonopen_was_started_here(conn)) {
  488. if (authdir_mode(options)) {
  489. /* We initiated this connection to address:port. Drop all routers
  490. * with the same address:port and a different key or nickname.
  491. */
  492. dirserv_orconn_tls_done(conn->address, conn->port,
  493. digest_rcvd, nickname);
  494. }
  495. if (conn->nickname[0] == '$') {
  496. /* I was aiming for a particular digest. Did I get it? */
  497. char d[HEX_DIGEST_LEN+1];
  498. base16_encode(d, HEX_DIGEST_LEN+1, digest_rcvd, DIGEST_LEN);
  499. if (strcasecmp(d,conn->nickname+1)) {
  500. log_fn(severity,
  501. "Identity key not as expected for router at %s:%d: wanted %s but got %s",
  502. conn->address, conn->port, conn->nickname, d);
  503. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  504. return -1;
  505. }
  506. } else if (strcasecmp(conn->nickname, nickname)) {
  507. /* I was aiming for a nickname. Did I get it? */
  508. log_fn(severity,
  509. "Other side (%s:%d) is '%s', but we tried to connect to '%s'",
  510. conn->address, conn->port, nickname, conn->nickname);
  511. control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
  512. return -1;
  513. }
  514. } else {
  515. if ((c=connection_get_by_identity_digest(digest_rcvd, CONN_TYPE_OR))) {
  516. log_fn(LOG_INFO,"Router '%s' is already connected on fd %d. Dropping fd %d.", nickname, c->s, conn->s);
  517. return -1;
  518. }
  519. connection_or_init_conn_from_address(conn,conn->addr,conn->port,digest_rcvd);
  520. }
  521. if (!server_mode(options)) { /* If I'm an OP... */
  522. conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
  523. }
  524. directory_set_dirty();
  525. conn->state = OR_CONN_STATE_OPEN;
  526. connection_watch_events(conn, EV_READ);
  527. circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
  528. rep_hist_note_connect_succeeded(conn->identity_digest, time(NULL));
  529. control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED);
  530. return 0;
  531. }
  532. /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s
  533. * outbuf.
  534. *
  535. * (Commented out) If it's an OR conn, and an entire TLS record is
  536. * ready, then try to flush the record now.
  537. */
  538. void
  539. connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn)
  540. {
  541. char networkcell[CELL_NETWORK_SIZE];
  542. char *n = networkcell;
  543. tor_assert(cell);
  544. tor_assert(conn);
  545. tor_assert(connection_speaks_cells(conn));
  546. cell_pack(n, cell);
  547. connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  548. #define MIN_TLS_FLUSHLEN 15872
  549. /* openssl tls record size is 16383, this is close. The goal here is to
  550. * push data out as soon as we know there's enough for a tls record, so
  551. * during periods of high load we won't read the entire megabyte from
  552. * input before pushing any data out. It also has the feature of not
  553. * growing huge outbufs unless something is slow. */
  554. if (conn->outbuf_flushlen-CELL_NETWORK_SIZE < MIN_TLS_FLUSHLEN &&
  555. conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
  556. int extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
  557. conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
  558. if (connection_handle_write(conn) < 0) {
  559. if (!conn->marked_for_close) {
  560. /* this connection is broken. remove it. */
  561. log_fn(LOG_WARN,"Bug: unhandled error on write for OR conn (fd %d); removing",
  562. conn->s);
  563. tor_fragile_assert();
  564. conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
  565. /* XXX do we need a close-immediate here, so we don't try to flush? */
  566. connection_mark_for_close(conn);
  567. }
  568. return;
  569. }
  570. if (extra) {
  571. conn->outbuf_flushlen += extra;
  572. connection_start_writing(conn);
  573. }
  574. }
  575. }
  576. /** Process cells from <b>conn</b>'s inbuf.
  577. *
  578. * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
  579. * and hand it to command_process_cell().
  580. *
  581. * Always return 0.
  582. */
  583. static int
  584. connection_or_process_cells_from_inbuf(connection_t *conn)
  585. {
  586. char buf[CELL_NETWORK_SIZE];
  587. cell_t cell;
  588. loop:
  589. log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
  590. conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls));
  591. if (buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* entire response available? */
  592. return 0; /* not yet */
  593. connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
  594. /* retrieve cell info from buf (create the host-order struct from the
  595. * network-order string) */
  596. cell_unpack(&cell, buf);
  597. command_process_cell(&cell, conn);
  598. goto loop; /* process the remainder of the buffer */
  599. }