1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090 |
- const char connection_or_c_id[] =
- "$Id$";
- #include "or.h"
- static int connection_tls_finish_handshake(or_connection_t *conn);
- static int connection_or_process_cells_from_inbuf(or_connection_t *conn);
- static int connection_or_send_versions(or_connection_t *conn);
- static int connection_init_or_handshake_state(or_connection_t *conn,
- int started_here);
- static int connection_or_check_valid_tls_handshake(or_connection_t *conn,
- int started_here,
- char *digest_rcvd_out);
- static digestmap_t *orconn_identity_map = NULL;
- void
- connection_or_remove_from_identity_map(or_connection_t *conn)
- {
- or_connection_t *tmp;
- tor_assert(conn);
- if (!orconn_identity_map)
- return;
- tmp = digestmap_get(orconn_identity_map, conn->identity_digest);
- if (!tmp) {
- if (!tor_digest_is_zero(conn->identity_digest)) {
- log_warn(LD_BUG, "Didn't find connection '%s' on identity map when "
- "trying to remove it.",
- conn->nickname ? conn->nickname : "NULL");
- }
- return;
- }
- if (conn == tmp) {
- if (conn->next_with_same_id)
- digestmap_set(orconn_identity_map, conn->identity_digest,
- conn->next_with_same_id);
- else
- digestmap_remove(orconn_identity_map, conn->identity_digest);
- } else {
- while (tmp->next_with_same_id) {
- if (tmp->next_with_same_id == conn) {
- tmp->next_with_same_id = conn->next_with_same_id;
- break;
- }
- tmp = tmp->next_with_same_id;
- }
- }
- memset(conn->identity_digest, 0, DIGEST_LEN);
- conn->next_with_same_id = NULL;
- }
- void
- connection_or_clear_identity_map(void)
- {
- smartlist_t *conns = get_connection_array();
- SMARTLIST_FOREACH(conns, connection_t *, conn,
- {
- if (conn->type == CONN_TYPE_OR) {
- or_connection_t *or_conn = TO_OR_CONN(conn);
- memset(or_conn->identity_digest, 0, DIGEST_LEN);
- or_conn->next_with_same_id = NULL;
- }
- });
- if (orconn_identity_map) {
- digestmap_free(orconn_identity_map, NULL);
- orconn_identity_map = NULL;
- }
- }
- static void
- connection_or_set_identity_digest(or_connection_t *conn, const char *digest)
- {
- or_connection_t *tmp;
- tor_assert(conn);
- tor_assert(digest);
- if (!orconn_identity_map)
- orconn_identity_map = digestmap_new();
- if (!memcmp(conn->identity_digest, digest, DIGEST_LEN))
- return;
-
- if (! tor_digest_is_zero(conn->identity_digest))
- connection_or_remove_from_identity_map(conn);
- memcpy(conn->identity_digest, digest, DIGEST_LEN);
-
- if (tor_digest_is_zero(digest))
- return;
- tmp = digestmap_set(orconn_identity_map, digest, conn);
- conn->next_with_same_id = tmp;
- #if 1
-
- for (; tmp; tmp = tmp->next_with_same_id) {
- tor_assert(!memcmp(tmp->identity_digest, digest, DIGEST_LEN));
- tor_assert(tmp != conn);
- }
- #endif
- }
- void
- cell_pack(packed_cell_t *dst, const cell_t *src)
- {
- char *dest = dst->body;
- *(uint16_t*)dest = htons(src->circ_id);
- *(uint8_t*)(dest+2) = src->command;
- memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
- }
- static void
- cell_unpack(cell_t *dest, const char *src)
- {
- dest->circ_id = ntohs(*(uint16_t*)(src));
- dest->command = *(uint8_t*)(src+2);
- memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
- }
- void
- var_cell_pack_header(const var_cell_t *cell, char *hdr_out)
- {
- *(uint16_t*)(hdr_out) = htons(cell->circ_id);
- *(uint8_t*)(hdr_out+2) = cell->command;
- set_uint16(hdr_out+3, htons(cell->payload_len));
- }
- var_cell_t *
- var_cell_new(uint16_t payload_len)
- {
- var_cell_t *cell = tor_malloc(sizeof(var_cell_t)+payload_len-1);
- cell->payload_len = payload_len;
- cell->command = 0;
- cell->circ_id = 0;
- return cell;
- }
- void
- var_cell_free(var_cell_t *cell)
- {
- tor_free(cell);
- }
- int
- connection_or_reached_eof(or_connection_t *conn)
- {
- log_info(LD_OR,"OR connection reached EOF. Closing.");
- connection_mark_for_close(TO_CONN(conn));
- return 0;
- }
- static int
- connection_or_read_proxy_response(or_connection_t *or_conn)
- {
- char *headers;
- char *reason=NULL;
- int status_code;
- time_t date_header;
- connection_t *conn = TO_CONN(or_conn);
- switch (fetch_from_buf_http(conn->inbuf,
- &headers, MAX_HEADERS_SIZE,
- NULL, NULL, 10000, 0)) {
- case -1:
- log_warn(LD_PROTOCOL,
- "Your https proxy sent back an oversized response. Closing.");
- return -1;
- case 0:
- log_info(LD_OR,"https proxy response not all here yet. Waiting.");
- return 0;
-
- }
- if (parse_http_response(headers, &status_code, &date_header,
- NULL, &reason) < 0) {
- log_warn(LD_OR,
- "Unparseable headers from proxy (connecting to '%s'). Closing.",
- conn->address);
- tor_free(headers);
- return -1;
- }
- if (!reason) reason = tor_strdup("[no reason given]");
- if (status_code == 200) {
- log_info(LD_OR,
- "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
- conn->address, escaped(reason));
- tor_free(reason);
- if (connection_tls_start_handshake(or_conn, 0) < 0) {
-
- connection_mark_for_close(conn);
- return -1;
- }
- return 0;
- }
-
- log_warn(LD_OR,
- "The https proxy sent back an unexpected status code %d (%s). "
- "Closing.",
- status_code, escaped(reason));
- tor_free(reason);
- connection_mark_for_close(conn);
- return -1;
- }
- int
- connection_or_process_inbuf(or_connection_t *conn)
- {
- tor_assert(conn);
- switch (conn->_base.state) {
- case OR_CONN_STATE_PROXY_READING:
- return connection_or_read_proxy_response(conn);
- case OR_CONN_STATE_OPEN:
- case OR_CONN_STATE_OR_HANDSHAKING:
- return connection_or_process_cells_from_inbuf(conn);
- default:
- return 0;
- }
- }
- #define OR_CONN_HIGHWATER (32*1024)
- #define OR_CONN_LOWWATER (16*1024)
- int
- connection_or_flushed_some(or_connection_t *conn)
- {
- size_t datalen = buf_datalen(conn->_base.outbuf);
-
- if (datalen < OR_CONN_LOWWATER) {
- int n = (OR_CONN_HIGHWATER - datalen + CELL_NETWORK_SIZE-1)
- / CELL_NETWORK_SIZE;
- while (conn->active_circuits && n > 0) {
- int flushed = connection_or_flush_from_first_active_circuit(conn, 1);
- n -= flushed;
- }
- }
- return 0;
- }
- int
- connection_or_finished_flushing(or_connection_t *conn)
- {
- tor_assert(conn);
- assert_connection_ok(TO_CONN(conn),0);
- switch (conn->_base.state) {
- case OR_CONN_STATE_PROXY_FLUSHING:
- log_debug(LD_OR,"finished sending CONNECT to proxy.");
- conn->_base.state = OR_CONN_STATE_PROXY_READING;
- connection_stop_writing(TO_CONN(conn));
- break;
- case OR_CONN_STATE_OPEN:
- case OR_CONN_STATE_OR_HANDSHAKING:
- connection_stop_writing(TO_CONN(conn));
- break;
- default:
- log_err(LD_BUG,"Called in unexpected state %d.", conn->_base.state);
- tor_fragile_assert();
- return -1;
- }
- return 0;
- }
- int
- connection_or_finished_connecting(or_connection_t *or_conn)
- {
- connection_t *conn;
- tor_assert(or_conn);
- conn = TO_CONN(or_conn);
- tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
- log_debug(LD_OR,"OR connect() to router at %s:%u finished.",
- conn->address,conn->port);
- if (get_options()->HttpsProxy) {
- char buf[1024];
- char addrbuf[INET_NTOA_BUF_LEN];
- struct in_addr in;
- char *base64_authenticator=NULL;
- const char *authenticator = get_options()->HttpsProxyAuthenticator;
- in.s_addr = htonl(conn->addr);
- tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
- if (authenticator) {
- base64_authenticator = alloc_http_authenticator(authenticator);
- if (!base64_authenticator)
- log_warn(LD_OR, "Encoding https authenticator failed");
- }
- if (base64_authenticator) {
- tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
- "Proxy-Authorization: Basic %s\r\n\r\n", addrbuf,
- conn->port, base64_authenticator);
- tor_free(base64_authenticator);
- } else {
- tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
- addrbuf, conn->port);
- }
- connection_write_to_buf(buf, strlen(buf), conn);
- conn->state = OR_CONN_STATE_PROXY_FLUSHING;
- return 0;
- }
- if (connection_tls_start_handshake(or_conn, 0) < 0) {
-
- connection_mark_for_close(conn);
- return -1;
- }
- return 0;
- }
- static void
- connection_or_init_conn_from_address(or_connection_t *conn,
- uint32_t addr, uint16_t port,
- const char *id_digest,
- int started_here)
- {
- or_options_t *options = get_options();
- routerinfo_t *r = router_get_by_digest(id_digest);
- conn->bandwidthrate = (int)options->BandwidthRate;
- conn->read_bucket = conn->bandwidthburst = (int)options->BandwidthBurst;
- connection_or_set_identity_digest(conn, id_digest);
- conn->_base.addr = addr;
- conn->_base.port = port;
- conn->real_addr = addr;
- if (r) {
- if (conn->_base.addr == r->addr)
- conn->is_canonical = 1;
- if (!started_here) {
-
-
- conn->_base.addr = r->addr;
- conn->_base.port = r->or_port;
- }
- conn->nickname = tor_strdup(r->nickname);
- tor_free(conn->_base.address);
- conn->_base.address = tor_strdup(r->address);
- } else {
- const char *n;
-
- n = dirserv_get_nickname_by_digest(id_digest);
- if (n) {
- conn->nickname = tor_strdup(n);
- } else {
- conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
- conn->nickname[0] = '$';
- base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
- conn->identity_digest, DIGEST_LEN);
- }
- tor_free(conn->_base.address);
- conn->_base.address = tor_dup_addr(addr);
- }
- }
- or_connection_t *
- connection_or_get_by_identity_digest(const char *digest)
- {
- int newer;
- or_connection_t *conn, *best=NULL;
- if (!orconn_identity_map)
- return NULL;
- conn = digestmap_get(orconn_identity_map, digest);
- for (; conn; conn = conn->next_with_same_id) {
- tor_assert(conn->_base.magic == OR_CONNECTION_MAGIC);
- tor_assert(conn->_base.type == CONN_TYPE_OR);
- tor_assert(!memcmp(conn->identity_digest, digest, DIGEST_LEN));
- if (conn->_base.marked_for_close)
- continue;
- if (!best) {
- best = conn;
- continue;
- }
- if (best->_base.state == OR_CONN_STATE_OPEN &&
- conn->_base.state != OR_CONN_STATE_OPEN)
- continue;
- newer = best->_base.timestamp_created < conn->_base.timestamp_created;
- if (best->is_canonical && !conn->is_canonical)
- continue;
- if (!best->_base.or_is_obsolete && conn->_base.or_is_obsolete)
- continue;
- if (
-
- (best->_base.or_is_obsolete && !conn->_base.or_is_obsolete) ||
-
- (best->n_circuits && conn->n_circuits && newer) ||
-
- (!best->n_circuits && !conn->n_circuits && newer) ||
-
- (!best->n_circuits && conn->n_circuits)) {
- best = conn;
- };
- }
- return best;
- }
- or_connection_t *
- connection_or_connect(uint32_t addr, uint16_t port, const char *id_digest)
- {
- or_connection_t *conn;
- or_options_t *options = get_options();
- tor_assert(id_digest);
- if (server_mode(options) && router_digest_is_me(id_digest)) {
- log_info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
- return NULL;
- }
- conn = TO_OR_CONN(connection_new(CONN_TYPE_OR, AF_INET));
-
- connection_or_init_conn_from_address(conn, addr, port, id_digest, 1);
- conn->_base.state = OR_CONN_STATE_CONNECTING;
- control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
- if (options->HttpsProxy) {
-
- addr = options->HttpsProxyAddr;
- port = options->HttpsProxyPort;
- }
- switch (connection_connect(TO_CONN(conn), conn->_base.address, addr, port)) {
- case -1:
-
- if (!options->HttpsProxy) {
- entry_guard_register_connect_status(conn->identity_digest, 0,
- time(NULL));
- router_set_status(conn->identity_digest, 0);
- }
- control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED,
- END_OR_CONN_REASON_TCP_REFUSED);
- connection_free(TO_CONN(conn));
- return NULL;
- case 0:
- connection_watch_events(TO_CONN(conn), EV_READ | EV_WRITE);
-
- return conn;
-
- }
- if (connection_or_finished_connecting(conn) < 0) {
-
- return NULL;
- }
- return conn;
- }
- int
- connection_tls_start_handshake(or_connection_t *conn, int receiving)
- {
- conn->_base.state = OR_CONN_STATE_TLS_HANDSHAKING;
- conn->tls = tor_tls_new(conn->_base.s, receiving);
- if (!conn->tls) {
- log_warn(LD_BUG,"tor_tls_new failed. Closing.");
- return -1;
- }
- connection_start_reading(TO_CONN(conn));
- log_debug(LD_OR,"starting TLS handshake on fd %d", conn->_base.s);
- note_crypto_pk_op(receiving ? TLS_HANDSHAKE_S : TLS_HANDSHAKE_C);
- if (connection_tls_continue_handshake(conn) < 0) {
- return -1;
- }
- return 0;
- }
- static void
- connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn)
- {
- or_connection_t *conn = _conn;
- (void)tls;
-
- tor_tls_set_renegotiate_callback(tls, NULL, NULL);
- if (connection_tls_finish_handshake(conn) < 0) {
-
-
- connection_mark_for_close(TO_CONN(conn));
- }
- }
- int
- connection_tls_continue_handshake(or_connection_t *conn)
- {
- int result;
- check_no_tls_errors();
- again:
- if (conn->_base.state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
-
- result = tor_tls_renegotiate(conn->tls);
-
- } else {
- tor_assert(conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING);
-
- result = tor_tls_handshake(conn->tls);
-
- }
- switch (result) {
- CASE_TOR_TLS_ERROR_ANY:
- log_info(LD_OR,"tls error [%s]. breaking connection.",
- tor_tls_err_to_string(result));
- return -1;
- case TOR_TLS_DONE:
- if (! tor_tls_used_v1_handshake(conn->tls)) {
- if (!tor_tls_is_server(conn->tls)) {
- if (conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING) {
-
- conn->_base.state = OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING;
- goto again;
- }
-
- } else {
-
- tor_tls_set_renegotiate_callback(conn->tls,
- connection_or_tls_renegotiated_cb,
- conn);
- conn->_base.state = OR_CONN_STATE_TLS_SERVER_RENEGOTIATING;
- connection_stop_writing(TO_CONN(conn));
- connection_start_reading(TO_CONN(conn));
- return 0;
- }
- }
- return connection_tls_finish_handshake(conn);
- case TOR_TLS_WANTWRITE:
- connection_start_writing(TO_CONN(conn));
- log_debug(LD_OR,"wanted write");
- return 0;
- case TOR_TLS_WANTREAD:
- log_debug(LD_OR,"wanted read");
- return 0;
- case TOR_TLS_CLOSE:
- log_info(LD_OR,"tls closed. breaking connection.");
- return -1;
- }
- return 0;
- }
- int
- connection_or_nonopen_was_started_here(or_connection_t *conn)
- {
- tor_assert(conn->_base.type == CONN_TYPE_OR);
- if (!conn->tls)
- return 1;
- if (conn->handshake_state)
- return conn->handshake_state->started_here;
- return !tor_tls_is_server(conn->tls);
- }
- static int
- connection_or_check_valid_tls_handshake(or_connection_t *conn,
- int started_here,
- char *digest_rcvd_out)
- {
- crypto_pk_env_t *identity_rcvd=NULL;
- or_options_t *options = get_options();
- int severity = server_mode(options) ? LOG_PROTOCOL_WARN : LOG_WARN;
- const char *safe_address =
- started_here ? conn->_base.address : safe_str(conn->_base.address);
- const char *conn_type = started_here ? "outgoing" : "incoming";
- int has_cert = 0, has_identity=0;
- check_no_tls_errors();
- has_cert = tor_tls_peer_has_cert(conn->tls);
- if (started_here && !has_cert) {
- log_info(LD_PROTOCOL,"Tried connecting to router at %s:%d, but it didn't "
- "send a cert! Closing.",
- safe_address, conn->_base.port);
- return -1;
- } else if (!has_cert) {
- log_debug(LD_PROTOCOL,"Got incoming connection with no certificate. "
- "That's ok.");
- }
- check_no_tls_errors();
- if (has_cert) {
- int v = tor_tls_verify(started_here?severity:LOG_INFO,
- conn->tls, &identity_rcvd);
- if (started_here && v<0) {
- log_fn(severity,LD_OR,"Tried connecting to router at %s:%d: It"
- " has a cert but it's invalid. Closing.",
- safe_address, conn->_base.port);
- return -1;
- } else if (v<0) {
- log_info(LD_PROTOCOL,"Incoming connection gave us an invalid cert "
- "chain; ignoring.");
- } else {
- log_debug(LD_OR,"The certificate seems to be valid on %s connection "
- "with %s:%d", conn_type, safe_address, conn->_base.port);
- }
- check_no_tls_errors();
- }
- if (identity_rcvd) {
- has_identity = 1;
- crypto_pk_get_digest(identity_rcvd, digest_rcvd_out);
- if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
- conn->circ_id_type = CIRC_ID_TYPE_LOWER;
- } else {
- conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
- }
- crypto_free_pk_env(identity_rcvd);
- } else {
- memset(digest_rcvd_out, 0, DIGEST_LEN);
- conn->circ_id_type = CIRC_ID_TYPE_NEITHER;
- }
- if (started_here && tor_digest_is_zero(conn->identity_digest)) {
- connection_or_set_identity_digest(conn, digest_rcvd_out);
- tor_free(conn->nickname);
- conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
- conn->nickname[0] = '$';
- base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
- conn->identity_digest, DIGEST_LEN);
- log_info(LD_OR, "Connected to router %s at %s:%d without knowing "
- "its key. Hoping for the best.",
- conn->nickname, conn->_base.address, conn->_base.port);
- }
- if (started_here) {
- int as_advertised = 1;
- tor_assert(has_cert);
- tor_assert(has_identity);
- if (memcmp(digest_rcvd_out, conn->identity_digest, DIGEST_LEN)) {
-
- char seen[HEX_DIGEST_LEN+1];
- char expected[HEX_DIGEST_LEN+1];
- base16_encode(seen, sizeof(seen), digest_rcvd_out, DIGEST_LEN);
- base16_encode(expected, sizeof(expected), conn->identity_digest,
- DIGEST_LEN);
- log_fn(severity, LD_OR,
- "Tried connecting to router at %s:%d, but identity key was not "
- "as expected: wanted %s but got %s.",
- conn->_base.address, conn->_base.port, expected, seen);
- entry_guard_register_connect_status(conn->identity_digest,0,time(NULL));
- router_set_status(conn->identity_digest, 0);
- control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED,
- END_OR_CONN_REASON_OR_IDENTITY);
- as_advertised = 0;
- }
- if (authdir_mode_tests_reachability(options)) {
-
- dirserv_orconn_tls_done(conn->_base.address, conn->_base.port,
- digest_rcvd_out, as_advertised);
- }
- if (!as_advertised)
- return -1;
- }
- return 0;
- }
- static int
- connection_tls_finish_handshake(or_connection_t *conn)
- {
- char digest_rcvd[DIGEST_LEN];
- int started_here = connection_or_nonopen_was_started_here(conn);
- log_debug(LD_OR,"tls handshake with %s done. verifying.",
- conn->_base.address);
- directory_set_dirty();
- if (connection_or_check_valid_tls_handshake(conn, started_here,
- digest_rcvd) < 0)
- return -1;
- if (tor_tls_used_v1_handshake(conn->tls)) {
- conn->link_proto = 1;
- if (!started_here) {
- connection_or_init_conn_from_address(conn,conn->_base.addr,
- conn->_base.port, digest_rcvd, 0);
- }
- return connection_or_set_state_open(conn);
- } else {
- conn->_base.state = OR_CONN_STATE_OR_HANDSHAKING;
- if (connection_init_or_handshake_state(conn, started_here) < 0)
- return -1;
- if (!started_here) {
- connection_or_init_conn_from_address(conn,conn->_base.addr,
- conn->_base.port, digest_rcvd, 0);
- }
- return connection_or_send_versions(conn);
- }
- }
- static int
- connection_init_or_handshake_state(or_connection_t *conn, int started_here)
- {
- or_handshake_state_t *s;
- s = conn->handshake_state = tor_malloc_zero(sizeof(or_handshake_state_t));
- s->started_here = started_here ? 1 : 0;
- return 0;
- }
- void
- or_handshake_state_free(or_handshake_state_t *state)
- {
- tor_assert(state);
- memset(state, 0xBE, sizeof(or_handshake_state_t));
- tor_free(state);
- }
- int
- connection_or_set_state_open(or_connection_t *conn)
- {
- int started_here = connection_or_nonopen_was_started_here(conn);
- time_t now = time(NULL);
- conn->_base.state = OR_CONN_STATE_OPEN;
- control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED, 0);
- if (started_here) {
- rep_hist_note_connect_succeeded(conn->identity_digest, now);
- if (entry_guard_register_connect_status(conn->identity_digest,
- 1, now) < 0) {
-
- return -1;
- }
- router_set_status(conn->identity_digest, 1);
- } else {
-
- if (!router_get_by_digest(conn->identity_digest))
- geoip_note_client_seen(TO_CONN(conn)->addr, now);
- }
- if (conn->handshake_state) {
- or_handshake_state_free(conn->handshake_state);
- conn->handshake_state = NULL;
- }
- connection_start_reading(TO_CONN(conn));
- circuit_n_conn_done(conn, 1);
- return 0;
- }
- void
- connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
- {
- packed_cell_t networkcell;
- tor_assert(cell);
- tor_assert(conn);
- cell_pack(&networkcell, cell);
- connection_write_to_buf(networkcell.body, CELL_NETWORK_SIZE, TO_CONN(conn));
- if (cell->command != CELL_PADDING)
- conn->timestamp_last_added_nonpadding = time(NULL);
- }
- void
- connection_or_write_var_cell_to_buf(const var_cell_t *cell,
- or_connection_t *conn)
- {
- char hdr[VAR_CELL_HEADER_SIZE];
- tor_assert(cell);
- tor_assert(conn);
- var_cell_pack_header(cell, hdr);
- connection_write_to_buf(hdr, sizeof(hdr), TO_CONN(conn));
- connection_write_to_buf(cell->payload, cell->payload_len, TO_CONN(conn));
- if (cell->command != CELL_PADDING)
- conn->timestamp_last_added_nonpadding = time(NULL);
- }
- static int
- connection_fetch_var_cell_from_buf(or_connection_t *conn, var_cell_t **out)
- {
- return fetch_var_cell_from_buf(conn->_base.inbuf, out, conn->link_proto);
- }
- static int
- connection_or_process_cells_from_inbuf(or_connection_t *conn)
- {
- var_cell_t *var_cell;
- while (1) {
- log_debug(LD_OR,
- "%d: starting, inbuf_datalen %d (%d pending in tls object).",
- conn->_base.s,(int)buf_datalen(conn->_base.inbuf),
- tor_tls_get_pending_bytes(conn->tls));
- if (connection_fetch_var_cell_from_buf(conn, &var_cell)) {
- if (!var_cell)
- return 0;
- command_process_var_cell(var_cell, conn);
- var_cell_free(var_cell);
- } else {
- char buf[CELL_NETWORK_SIZE];
- cell_t cell;
- if (buf_datalen(conn->_base.inbuf) < CELL_NETWORK_SIZE)
- return 0;
- connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, TO_CONN(conn));
-
- cell_unpack(&cell, buf);
- command_process_cell(&cell, conn);
- }
- }
- }
- int
- connection_or_send_destroy(uint16_t circ_id, or_connection_t *conn, int reason)
- {
- cell_t cell;
- tor_assert(conn);
- memset(&cell, 0, sizeof(cell_t));
- cell.circ_id = circ_id;
- cell.command = CELL_DESTROY;
- cell.payload[0] = (uint8_t) reason;
- log_debug(LD_OR,"Sending destroy (circID %d).", circ_id);
-
- connection_or_write_cell_to_buf(&cell, conn);
- return 0;
- }
- static const uint16_t or_protocol_versions[] = { 1, 2 };
- static const int n_or_protocol_versions =
- sizeof(or_protocol_versions)/sizeof(uint16_t);
- int
- is_or_protocol_version_known(uint16_t v)
- {
- int i;
- for (i = 0; i < n_or_protocol_versions; ++i) {
- if (or_protocol_versions[i] == v)
- return 1;
- }
- return 0;
- }
- static int
- connection_or_send_versions(or_connection_t *conn)
- {
- var_cell_t *cell;
- int i;
- tor_assert(conn->handshake_state &&
- !conn->handshake_state->sent_versions_at);
- cell = var_cell_new(n_or_protocol_versions * 2);
- cell->command = CELL_VERSIONS;
- for (i = 0; i < n_or_protocol_versions; ++i) {
- uint16_t v = or_protocol_versions[i];
- set_uint16(cell->payload+(2*i), htons(v));
- }
- connection_or_write_var_cell_to_buf(cell, conn);
- conn->handshake_state->sent_versions_at = time(NULL);
- var_cell_free(cell);
- return 0;
- }
- int
- connection_or_send_netinfo(or_connection_t *conn)
- {
- cell_t cell;
- time_t now = time(NULL);
- routerinfo_t *me;
- memset(&cell, 0, sizeof(cell_t));
- cell.command = CELL_NETINFO;
-
- set_uint32(cell.payload, htonl(now));
- cell.payload[4] = RESOLVED_TYPE_IPV4;
- cell.payload[5] = 4;
- set_uint32(cell.payload+6, htonl(conn->_base.addr));
-
- if ((me = router_get_my_routerinfo())) {
- cell.payload[10] = 1;
- cell.payload[11] = RESOLVED_TYPE_IPV4;
- cell.payload[12] = 4;
- set_uint32(cell.payload+13, htonl(me->addr));
- } else {
- cell.payload[10] = 0;
- }
- connection_or_write_cell_to_buf(&cell, conn);
- return 0;
- }
|