📄 connection_or.c
字号:
" 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)) {
/* I was aiming for a particular digest. I didn't get it! */
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)) {
/* We initiated this connection to address:port. Drop all routers
* with the same address:port and a different key.
*/
dirserv_orconn_tls_done(conn->_base.address, conn->_base.port,
digest_rcvd_out, as_advertised);
}
if (!as_advertised)
return -1;
}
return 0;
}
/** The tls handshake is finished.
*
* Make sure we are happy with the person we just handshaked with.
*
* If he initiated the connection, make sure he's not already connected,
* then initialize conn from the information in router.
*
* If all is successful, call circuit_n_conn_done() to handle events
* that have been pending on the <tls handshake completion. Also set the
* directory to be dirty (only matters if I'm an authdirserver).
*/
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);
}
}
/** Allocate a new connection handshake state for the connection
* <b>conn</b>. Return 0 on success, -1 on failure. */
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;
}
/** Free all storage held by <b>state</b>. */
void
or_handshake_state_free(or_handshake_state_t *state)
{
tor_assert(state);
memset(state, 0xBE, sizeof(or_handshake_state_t));
tor_free(state);
}
/** Set <b>conn</b>'s state to OR_CONN_STATE_OPEN, and tell other subsystems
* as appropriate. Called when we are done with all TLS and OR handshaking.
*/
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) {
/* Close any circuits pending on this conn. We leave it in state
* 'open' though, because it didn't actually *fail* -- we just
* chose not to use it. (Otherwise
* connection_about_to_close_connection() will call a big pile of
* functions to indicate we shouldn't try it again.) */
circuit_n_conn_done(conn, 0);
return -1;
}
router_set_status(conn->identity_digest, 1);
} else {
/* only report it to the geoip module if it's not a known router */
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); /* send the pending creates, if any. */
return 0;
}
/** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s outbuf.
* For cells that use or affect a circuit, this should only be called by
* connection_or_flush_from_first_active_circuit().
*/
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);
}
/** Pack a variable-length <b>cell</b> into wire-format, and write it onto
* <b>conn</b>'s outbuf. Right now, this <em>DOES NOT</em> support cells that
* affect a circuit.
*/
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);
}
/** See whether there's a variable-length cell waiting on <b>conn</b>'s
* inbuf. Return values as for fetch_var_cell_from_buf(). */
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);
}
/** Process cells from <b>conn</b>'s inbuf.
*
* Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
* and hand it to command_process_cell().
*
* Always return 0.
*/
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; /* not yet. */
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) /* whole response
available? */
return 0; /* not yet */
connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, TO_CONN(conn));
/* retrieve cell info from buf (create the host-order struct from the
* network-order string) */
cell_unpack(&cell, buf);
command_process_cell(&cell, conn);
}
}
}
/** Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
* onto OR connection <b>conn</b>. Don't perform range-checking on reason:
* we may want to propagate reasons from other cells.
*
* Return 0.
*/
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);
/* XXXX It's possible that under some circumstances, we want the destroy
* to take precedence over other data waiting on the circuit's cell queue.
*/
connection_or_write_cell_to_buf(&cell, conn);
return 0;
}
/** Array of recognized link protocol versions. */
static const uint16_t or_protocol_versions[] = { 1, 2 };
/** Number of versions in <b>or_protocol_versions</b>. */
static const int n_or_protocol_versions =
(int)( sizeof(or_protocol_versions)/sizeof(uint16_t) );
/** Return true iff <b>v</b> is a link protocol version that this Tor
* implementation believes it can support. */
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;
}
/** Send a VERSIONS cell on <b>conn</b>, telling the other host about the
* link protocol versions that this Tor can support. */
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;
}
/** Send a NETINFO cell on <b>conn</b>, telling the other server what we know
* about their address, our address, and the current time. */
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;
/* Their address. */
set_uint32(cell.payload, htonl((uint32_t)now));
cell.payload[4] = RESOLVED_TYPE_IPV4;
cell.payload[5] = 4;
set_uint32(cell.payload+6, htonl(conn->_base.addr));
/* My address. */
if ((me = router_get_my_routerinfo())) {
cell.payload[10] = 1; /* only one address is supported. */
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -