2003-10-08 04:04:08 +02:00
|
|
|
/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file connection_or.c
|
|
|
|
* \brief Functions to handle OR connections, TLS handshaking, and
|
|
|
|
* cells on the network.
|
|
|
|
**/
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
#include "or.h"
|
2003-06-21 21:29:32 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
extern or_options_t options; /**< command-line and config-file options */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-09-30 20:45:55 +02:00
|
|
|
static int connection_tls_finish_handshake(connection_t *conn);
|
2004-02-28 05:11:53 +01:00
|
|
|
static int connection_or_process_cells_from_inbuf(connection_t *conn);
|
2003-09-30 20:45:55 +02:00
|
|
|
|
2003-09-16 07:41:49 +02:00
|
|
|
/**************************************************************/
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Pack the cell_t host-order structure <b>src</b> into network-order
|
|
|
|
* in the buffer <b>dest</b>. See tor-spec.txt for details about the
|
2004-05-07 10:53:40 +02:00
|
|
|
* wire format.
|
|
|
|
*/
|
2003-09-16 07:41:49 +02:00
|
|
|
static void cell_pack(char *dest, const cell_t *src) {
|
2003-11-11 04:01:48 +01:00
|
|
|
*(uint16_t*)dest = htons(src->circ_id);
|
2003-09-16 07:41:49 +02:00
|
|
|
*(uint8_t*)(dest+2) = src->command;
|
2003-12-16 10:48:17 +01:00
|
|
|
memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
|
2003-09-16 07:41:49 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Unpack the network-order buffer <b>src</b> into a host-order
|
|
|
|
* cell_t structure <b>dest</b>.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2003-09-16 07:41:49 +02:00
|
|
|
static void cell_unpack(cell_t *dest, const char *src) {
|
2003-11-11 04:01:48 +01:00
|
|
|
dest->circ_id = ntohs(*(uint16_t*)(src));
|
2003-09-16 07:41:49 +02:00
|
|
|
dest->command = *(uint8_t*)(src+2);
|
2003-12-16 10:48:17 +01:00
|
|
|
memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
|
2003-09-16 07:41:49 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Handle any new bytes that have come in on connection <b>conn</b>.
|
2004-05-07 10:53:40 +02:00
|
|
|
* If conn is in 'open' state, hand it to
|
|
|
|
* connection_or_process_cells_from_inbuf()
|
|
|
|
* (else do nothing).
|
|
|
|
*/
|
2002-06-27 00:45:49 +02:00
|
|
|
int connection_or_process_inbuf(connection_t *conn) {
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn && conn->type == CONN_TYPE_OR);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
if(conn->inbuf_reached_eof) {
|
2004-02-28 05:11:53 +01:00
|
|
|
log_fn(LOG_INFO,"OR connection reached EOF. Closing.");
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(conn);
|
2004-03-03 07:26:34 +01:00
|
|
|
return 0;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2003-09-12 08:20:36 +02:00
|
|
|
if(conn->state != OR_CONN_STATE_OPEN)
|
|
|
|
return 0; /* don't do anything */
|
2004-02-28 05:11:53 +01:00
|
|
|
return connection_or_process_cells_from_inbuf(conn);
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Connection <b>conn</b> has finished writing and has no bytes left on
|
2004-05-07 10:53:40 +02:00
|
|
|
* its outbuf.
|
|
|
|
*
|
2004-05-10 06:34:48 +02:00
|
|
|
* Otherwise it's in state "open": stop writing and return.
|
2004-05-10 05:54:33 +02:00
|
|
|
*
|
|
|
|
* If <b>conn</b> is broken, mark it for close and return -1, else
|
|
|
|
* return 0.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2002-06-27 00:45:49 +02:00
|
|
|
int connection_or_finished_flushing(connection_t *conn) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn && conn->type == CONN_TYPE_OR);
|
2004-05-12 21:17:09 +02:00
|
|
|
|
2004-02-28 05:11:53 +01:00
|
|
|
assert_connection_ok(conn,0);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-12 21:17:09 +02:00
|
|
|
if (conn->state != OR_CONN_STATE_OPEN) {
|
|
|
|
log_fn(LOG_WARN,"BUG: called in unexpected state %d",conn->state);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_stop_writing(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Connected handler for OR connections: begin the TLS handshake.
|
|
|
|
*/
|
|
|
|
int connection_or_finished_connecting(connection_t *conn)
|
|
|
|
{
|
|
|
|
tor_assert(conn && conn->type == CONN_TYPE_OR);
|
|
|
|
tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
|
|
|
|
|
|
|
|
log_fn(LOG_INFO,"OR connect() to router %s:%u finished.",
|
|
|
|
conn->address,conn->port);
|
|
|
|
|
|
|
|
if(connection_tls_start_handshake(conn, 0) < 0) {
|
2004-05-12 23:12:33 +02:00
|
|
|
/* TLS handshaking error of some kind. */
|
|
|
|
connection_mark_for_close(conn);
|
2004-05-12 21:17:09 +02:00
|
|
|
return -1;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2004-05-12 21:17:09 +02:00
|
|
|
return 0;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Initialize <b>conn</b> to include all the relevant data from <b>router</b>.
|
2004-05-07 10:53:40 +02:00
|
|
|
* This function is called either from connection_or_connect(), if
|
|
|
|
* we initiated the connect, or from connection_tls_finish_handshake()
|
|
|
|
* if the other side initiated it.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router) {
|
2003-09-25 12:42:07 +02:00
|
|
|
conn->addr = router->addr;
|
|
|
|
conn->port = router->or_port;
|
2004-01-11 08:41:01 +01:00
|
|
|
conn->receiver_bucket = conn->bandwidth = router->bandwidthburst;
|
2003-09-25 12:42:07 +02:00
|
|
|
conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
|
2004-07-01 03:16:59 +02:00
|
|
|
crypto_pk_get_digest(conn->identity_pkey, conn->identity_digest);
|
2003-10-04 05:29:09 +02:00
|
|
|
conn->nickname = tor_strdup(router->nickname);
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(conn->address);
|
2003-10-04 05:29:09 +02:00
|
|
|
conn->address = tor_strdup(router->address);
|
2003-09-25 12:42:07 +02:00
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
static void
|
|
|
|
connection_or_init_conn_from_address(connection_t *conn,
|
|
|
|
uint32_t addr, uint16_t port,
|
|
|
|
const char *id_digest)
|
|
|
|
{
|
|
|
|
routerinfo_t *r;
|
2004-07-02 11:29:01 +02:00
|
|
|
struct in_addr in;
|
2004-07-01 03:16:59 +02:00
|
|
|
r = router_get_by_digest(id_digest);
|
|
|
|
if (r) {
|
|
|
|
connection_or_init_conn_from_router(conn,r);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
conn->addr = addr;
|
|
|
|
conn->port = port;
|
|
|
|
/* This next part isn't really right, but it's good enough for now. */
|
|
|
|
conn->receiver_bucket = conn->bandwidth = options.BandwidthBurst;
|
|
|
|
memcpy(conn->identity_digest, id_digest, DIGEST_LEN);
|
|
|
|
conn->nickname = tor_malloc(HEX_DIGEST_LEN+1);
|
|
|
|
base16_encode(conn->nickname, HEX_DIGEST_LEN+1,
|
|
|
|
conn->identity_digest, DIGEST_LEN);
|
2004-07-02 11:29:01 +02:00
|
|
|
tor_free(conn->address);
|
|
|
|
in.s_addr = htonl(addr);
|
|
|
|
conn->address = tor_strdup(inet_ntoa(in));
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
2004-07-02 11:29:01 +02:00
|
|
|
/** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
|
|
|
|
* handshake with an OR with identity digest <b>id_digest</b>.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-07-02 11:29:01 +02:00
|
|
|
* If <b>id_digest</b> is me, do nothing. If we're already connected to it,
|
2004-07-21 02:12:42 +02:00
|
|
|
* return that connection. If the connect() is in progress, set the
|
|
|
|
* new conn's state to 'connecting' and return it. If connect() succeeds,
|
|
|
|
* call * connection_tls_start_handshake() on it.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-05-09 18:33:04 +02:00
|
|
|
* This function is called from router_retry_connections(), for
|
2004-05-07 10:53:40 +02:00
|
|
|
* ORs connecting to ORs, and circuit_establish_circuit(), for
|
|
|
|
* OPs connecting to ORs.
|
|
|
|
*
|
|
|
|
* Return the launched conn, or NULL if it failed.
|
|
|
|
*/
|
2004-07-02 11:29:01 +02:00
|
|
|
connection_t *connection_or_connect(uint32_t addr, uint16_t port,
|
|
|
|
const char *id_digest) {
|
2002-06-27 00:45:49 +02:00
|
|
|
connection_t *conn;
|
2004-07-21 02:44:04 +02:00
|
|
|
routerinfo_t *me;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-07-02 11:29:01 +02:00
|
|
|
tor_assert(id_digest);
|
2003-05-28 04:03:25 +02:00
|
|
|
|
2004-07-21 02:44:04 +02:00
|
|
|
if(server_mode() && (me=router_get_my_routerinfo()) &&
|
|
|
|
!memcmp(me->identity_digest, id_digest,DIGEST_LEN)) {
|
2004-07-02 11:29:01 +02:00
|
|
|
log_fn(LOG_WARN,"Request to connect to myself! Failing.");
|
2003-05-28 04:03:25 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-07-02 11:29:01 +02:00
|
|
|
/* this function should never be called if we're already connected to
|
|
|
|
* id_digest, but check first to be sure */
|
|
|
|
conn = connection_get_by_identity_digest(id_digest, CONN_TYPE_OR);
|
2003-05-28 04:03:25 +02:00
|
|
|
if(conn)
|
|
|
|
return conn;
|
|
|
|
|
2002-06-30 09:37:49 +02:00
|
|
|
conn = connection_new(CONN_TYPE_OR);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
/* set up conn so it's got all the data we need to remember */
|
2004-07-02 11:29:01 +02:00
|
|
|
connection_or_init_conn_from_address(conn, addr, port, id_digest);
|
2004-03-26 23:07:45 +01:00
|
|
|
conn->state = OR_CONN_STATE_CONNECTING;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-07-02 11:29:01 +02:00
|
|
|
switch(connection_connect(conn, conn->address, addr, port)) {
|
2003-09-16 03:58:46 +02:00
|
|
|
case -1:
|
2004-07-12 20:19:55 +02:00
|
|
|
router_mark_as_down(conn->identity_digest);
|
2004-05-05 03:26:57 +02:00
|
|
|
connection_free(conn);
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
return NULL;
|
2003-09-16 03:58:46 +02:00
|
|
|
case 0:
|
2003-12-17 22:09:31 +01:00
|
|
|
connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
|
2003-08-14 19:13:52 +02:00
|
|
|
/* writable indicates finish, readable indicates broken link,
|
|
|
|
error indicates broken link on windows */
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
return conn;
|
2003-09-16 03:58:46 +02:00
|
|
|
/* case 1: fall through */
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
}
|
|
|
|
|
2003-09-12 08:50:21 +02:00
|
|
|
if(connection_tls_start_handshake(conn, 0) >= 0)
|
2003-09-08 12:59:00 +02:00
|
|
|
return conn;
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
|
2003-05-28 04:03:25 +02:00
|
|
|
/* failure */
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(conn);
|
2003-05-28 04:03:25 +02:00
|
|
|
return NULL;
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
|
|
|
|
* we initiated the connection, else it's 1.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-05-09 18:47:25 +02:00
|
|
|
* Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and pass
|
|
|
|
* <b>conn</b> to connection_tls_continue_handshake().
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-05-09 18:47:25 +02:00
|
|
|
* Return -1 if <b>conn</b> is broken, else return 0.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2003-09-30 20:45:55 +02:00
|
|
|
int connection_tls_start_handshake(connection_t *conn, int receiving) {
|
|
|
|
conn->state = OR_CONN_STATE_HANDSHAKING;
|
|
|
|
conn->tls = tor_tls_new(conn->s, receiving);
|
|
|
|
if(!conn->tls) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"tor_tls_new failed. Closing.");
|
2003-09-30 20:45:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
connection_start_reading(conn);
|
|
|
|
log_fn(LOG_DEBUG,"starting the handshake");
|
2004-03-06 02:43:37 +01:00
|
|
|
if(connection_tls_continue_handshake(conn) < 0) {
|
2003-09-30 20:45:55 +02:00
|
|
|
return -1;
|
2004-03-06 02:43:37 +01:00
|
|
|
}
|
2003-09-30 20:45:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Move forward with the tls handshake. If it finishes, hand
|
|
|
|
* <b>conn</b> to connection_tls_finish_handshake().
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
2004-05-09 18:47:25 +02:00
|
|
|
* Return -1 if <b>conn</b> is broken, else return 0.
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
2003-09-30 20:45:55 +02:00
|
|
|
int connection_tls_continue_handshake(connection_t *conn) {
|
|
|
|
switch(tor_tls_handshake(conn->tls)) {
|
|
|
|
case TOR_TLS_ERROR:
|
|
|
|
case TOR_TLS_CLOSE:
|
|
|
|
log_fn(LOG_INFO,"tls error. breaking.");
|
|
|
|
return -1;
|
|
|
|
case TOR_TLS_DONE:
|
|
|
|
return connection_tls_finish_handshake(conn);
|
|
|
|
case TOR_TLS_WANTWRITE:
|
|
|
|
connection_start_writing(conn);
|
|
|
|
log_fn(LOG_DEBUG,"wanted write");
|
|
|
|
return 0;
|
|
|
|
case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
|
|
|
|
log_fn(LOG_DEBUG,"wanted read");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-07-21 04:25:14 +02:00
|
|
|
static int digest_is_nonzero(const char *id) {
|
|
|
|
char ZERO_DIGEST[DIGEST_LEN];
|
|
|
|
memset(ZERO_DIGEST, 0, DIGEST_LEN);
|
|
|
|
return !memcmp(ZERO_DIGEST, id, DIGEST_LEN);
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** The tls handshake is finished.
|
2004-05-07 10:53:40 +02:00
|
|
|
*
|
|
|
|
* Make sure we are happy with the person we just handshaked with:
|
|
|
|
* If it's an OP (that is, it has no certificate), make sure I'm an OR.
|
2004-05-09 18:33:04 +02:00
|
|
|
* If it's an OR (it has a certificate), make sure it has a recognized
|
|
|
|
* nickname, and its cert is signed by the identity key of that nickname.
|
|
|
|
* If I initiated the connection, make sure it's the right guy; and if
|
2004-05-07 10:53:40 +02:00
|
|
|
* he initiated the connection, make sure he's not already connected.
|
|
|
|
*
|
|
|
|
* If he initiated the conn, also initialize conn from the information
|
|
|
|
* in router.
|
|
|
|
*
|
|
|
|
* If either of us is an OP, set bandwidth to the default OP bandwidth.
|
|
|
|
*
|
2004-07-02 11:29:01 +02:00
|
|
|
* If all is successful and he's an OR, then call circuit_n_conn_done()
|
2004-05-07 10:53:40 +02:00
|
|
|
* to handle events that have been pending on the tls handshake
|
|
|
|
* completion, and set the directory to be dirty (only matters if I'm
|
2004-07-21 02:12:42 +02:00
|
|
|
* an authdirserver).
|
2004-05-07 10:53:40 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
connection_tls_finish_handshake(connection_t *conn) {
|
2003-09-30 20:45:55 +02:00
|
|
|
routerinfo_t *router;
|
2003-11-18 11:17:52 +01:00
|
|
|
char nickname[MAX_NICKNAME_LEN+1];
|
2004-04-08 21:49:55 +02:00
|
|
|
connection_t *c;
|
2004-07-21 02:44:04 +02:00
|
|
|
crypto_pk_env_t *identity_rcvd=NULL;
|
2004-07-21 04:25:14 +02:00
|
|
|
char digest_rcvd[DIGEST_LEN];
|
2003-09-30 20:45:55 +02:00
|
|
|
|
|
|
|
conn->state = OR_CONN_STATE_OPEN;
|
|
|
|
connection_watch_events(conn, POLLIN);
|
|
|
|
log_fn(LOG_DEBUG,"tls handshake done. verifying.");
|
2003-11-18 08:25:04 +01:00
|
|
|
if (! tor_tls_peer_has_cert(conn->tls)) { /* It's an OP. */
|
2004-07-18 23:47:04 +02:00
|
|
|
if (server_mode()) { /* I'm an OR; good. */
|
2003-09-30 20:45:55 +02:00
|
|
|
conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
|
2003-11-18 08:25:04 +01:00
|
|
|
return 0;
|
|
|
|
} else { /* Neither side sent a certificate: ouch. */
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"Neither peer sent a cert! Closing.");
|
2003-09-30 20:45:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-11-18 08:25:04 +01:00
|
|
|
}
|
|
|
|
/* Okay; the other side is an OR. */
|
2003-11-18 11:17:52 +01:00
|
|
|
if (tor_tls_get_peer_cert_nickname(conn->tls, nickname, MAX_NICKNAME_LEN)) {
|
2003-11-18 08:25:04 +01:00
|
|
|
log_fn(LOG_WARN,"Other side (%s:%d) has a cert without a valid nickname. Closing.",
|
|
|
|
conn->address, conn->port);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-25 00:40:32 +02:00
|
|
|
log_fn(LOG_DEBUG, "Other side (%s:%d) claims to be '%s'", conn->address,
|
|
|
|
conn->port, nickname);
|
2004-07-21 04:25:14 +02:00
|
|
|
|
|
|
|
if(tor_tls_verify(conn->tls, &identity_rcvd) < 0) {
|
2004-04-25 00:17:50 +02:00
|
|
|
log_fn(LOG_WARN,"Other side '%s' (%s:%d) has a cert but it's invalid. Closing.",
|
2003-11-18 08:25:04 +01:00
|
|
|
nickname, conn->address, conn->port);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-25 00:17:50 +02:00
|
|
|
log_fn(LOG_DEBUG,"The router's cert is valid.");
|
2004-07-21 04:25:14 +02:00
|
|
|
crypto_pk_get_digest(identity_rcvd, digest_rcvd);
|
|
|
|
crypto_free_pk_env(identity_rcvd);
|
|
|
|
|
|
|
|
router = router_get_by_nickname(nickname);
|
|
|
|
if(router && /* we know this nickname; make sure it's the right guy */
|
|
|
|
memcmp(digest_rcvd, router->identity_digest, DIGEST_LEN) != 0) {
|
2004-07-21 02:44:04 +02:00
|
|
|
log_fn(LOG_WARN, "Identity key not as expected for %s", nickname);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-25 00:17:50 +02:00
|
|
|
|
2004-07-21 04:25:14 +02:00
|
|
|
if (digest_is_nonzero(conn->identity_digest)) {
|
2004-04-25 00:17:50 +02:00
|
|
|
/* I initiated this connection. */
|
2004-04-25 00:34:31 +02:00
|
|
|
if (strcasecmp(conn->nickname, nickname)) {
|
2004-04-25 00:17:50 +02:00
|
|
|
log_fn(options.DirPort ? LOG_WARN : LOG_INFO,
|
2004-04-25 00:40:32 +02:00
|
|
|
"Other side (%s:%d) is '%s', but we tried to connect to '%s'",
|
|
|
|
conn->address, conn->port, nickname, conn->nickname);
|
2003-09-30 20:45:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-11-18 08:25:04 +01:00
|
|
|
} else {
|
2004-07-21 04:25:14 +02:00
|
|
|
if((c=connection_get_by_identity_digest(digest_rcvd, CONN_TYPE_OR))) {
|
|
|
|
log_fn(LOG_INFO,"Router %s is already connected on fd %d. Dropping fd %d.", nickname, c->s, conn->s);
|
2003-11-18 11:38:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-07-21 04:25:14 +02:00
|
|
|
connection_or_init_conn_from_address(conn,conn->addr,conn->port,digest_rcvd);
|
2003-11-18 08:25:04 +01:00
|
|
|
}
|
2004-04-25 00:17:50 +02:00
|
|
|
|
2004-07-18 23:47:04 +02:00
|
|
|
if (!server_mode()) { /* If I'm an OP... */
|
2003-09-30 20:45:55 +02:00
|
|
|
conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
|
|
|
|
}
|
2004-05-07 10:53:40 +02:00
|
|
|
directory_set_dirty();
|
2004-07-02 11:29:01 +02:00
|
|
|
circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
|
2004-03-20 05:59:29 +01:00
|
|
|
/* Note the success */
|
2004-07-13 20:23:40 +02:00
|
|
|
rep_hist_note_connect_succeeded(conn->identity_digest, time(NULL));
|
2003-09-30 20:45:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-13 00:56:26 +02:00
|
|
|
/** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s
|
|
|
|
* outbuf.
|
|
|
|
*
|
|
|
|
* (Commented out) If it's an OR conn, and an entire TLS record is
|
|
|
|
* ready, then try to flush the record now.
|
|
|
|
*/
|
2003-12-23 08:45:31 +01:00
|
|
|
void connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn) {
|
2003-09-13 00:45:31 +02:00
|
|
|
char networkcell[CELL_NETWORK_SIZE];
|
|
|
|
char *n = networkcell;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cell && conn);
|
|
|
|
tor_assert(connection_speaks_cells(conn));
|
2003-10-09 20:45:14 +02:00
|
|
|
|
2003-12-23 08:45:31 +01:00
|
|
|
cell_pack(n, cell);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-10-04 04:38:18 +02:00
|
|
|
connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
|
2004-05-13 00:56:26 +02:00
|
|
|
|
|
|
|
#if 0 /* commented out -- can we get away with not doing this,
|
|
|
|
* because we're already round-robining in handle_read?
|
|
|
|
*/
|
|
|
|
#define MIN_TLS_FLUSHLEN 15872
|
|
|
|
/* openssl tls record size is 16383, this is close. The goal here is to
|
|
|
|
* push data out as soon as we know there's enough for a tls record, so
|
|
|
|
* during periods of high load we won't read the entire megabyte from
|
|
|
|
* input before pushing any data out. */
|
|
|
|
if(conn->outbuf_flushlen-CELL_NETWORK_SIZE < MIN_TLS_FLUSHLEN &&
|
|
|
|
conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
|
|
|
|
int extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
|
|
|
|
conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
|
|
|
|
if(connection_handle_write(conn) < 0) {
|
|
|
|
log_fn(LOG_WARN,"flushing failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(extra) {
|
|
|
|
conn->outbuf_flushlen += extra;
|
|
|
|
connection_start_writing(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-09-13 00:45:31 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** 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().
|
|
|
|
*
|
2004-05-07 10:53:40 +02:00
|
|
|
* Always return 0.
|
|
|
|
*/
|
2004-02-28 05:11:53 +01:00
|
|
|
static int connection_or_process_cells_from_inbuf(connection_t *conn) {
|
2003-09-13 00:45:31 +02:00
|
|
|
char buf[CELL_NETWORK_SIZE];
|
|
|
|
cell_t cell;
|
2003-09-25 07:17:11 +02:00
|
|
|
|
2003-11-12 06:12:51 +01:00
|
|
|
loop:
|
2003-09-30 22:36:20 +02:00
|
|
|
log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
|
2003-10-15 20:48:48 +02:00
|
|
|
conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls));
|
2003-09-25 07:17:11 +02:00
|
|
|
if(buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* entire response available? */
|
2003-09-13 00:45:31 +02:00
|
|
|
return 0; /* not yet */
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-09-13 00:45:31 +02:00
|
|
|
connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-11-12 06:12:51 +01:00
|
|
|
/* retrieve cell info from buf (create the host-order struct from the
|
|
|
|
* network-order string) */
|
2003-09-13 00:45:31 +02:00
|
|
|
cell_unpack(&cell, buf);
|
2003-12-17 22:09:31 +01:00
|
|
|
|
2003-09-13 00:45:31 +02:00
|
|
|
command_process_cell(&cell, conn);
|
2003-10-09 20:45:14 +02:00
|
|
|
|
2003-11-12 06:12:51 +01:00
|
|
|
goto loop; /* process the remainder of the buffer */
|
2003-09-13 00:45:31 +02:00
|
|
|
}
|
|
|
|
|
2003-04-07 04:12:02 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|