Commit b384c5a1 authored by Roger Dingledine's avatar Roger Dingledine
Browse files

bugfix: do directory_send_command only after we've established a socket

also many comments, cleaning, etc


svn:r1823
parent d18528a2
/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
/* Copyright 2001 Matej Pfajfar, 2001-2004 Roger Dingledine. */
/* See LICENSE for licensing information */
/* $Id$ */
......@@ -37,7 +37,10 @@ char *circuit_state_to_string[] = {
/********* END VARIABLES ************/
void circuit_add(circuit_t *circ) {
/* add 'circ' to the global list of circuits. This is called only from
* within circuit_new.
*/
static void circuit_add(circuit_t *circ) {
if(!global_circuitlist) { /* first one */
global_circuitlist = circ;
circ->next = NULL;
......
......@@ -8,6 +8,7 @@
extern or_options_t options; /* command-line and config-file options */
/* Array of strings to make conn->type human-readable */
char *conn_type_to_string[] = {
"", /* 0 */
"OP listener", /* 1 */
......@@ -23,6 +24,7 @@ char *conn_type_to_string[] = {
"CPU worker", /* 11 */
};
/* Array of string arrays to make {conn->type,conn->state} human-readable */
char *conn_state_to_string[][_CONN_TYPE_MAX+1] = {
{ NULL }, /* no type associated with 0 */
{ NULL }, /* op listener, obsolete */
......@@ -72,6 +74,19 @@ static int connection_receiver_bucket_should_increase(connection_t *conn);
/**************************************************************/
/* Allocate space for a new connection_t. This function just initializes
* conn; you must call connection_add() to link it into the main array.
*
* Set conn->type to 'type'. Set conn->s and conn->poll_index to
* -1 to signify they are not yet assigned.
*
* If conn is not a listener type, allocate buffers for it. If it's
* an AP type, allocate space to store the socks_request.
*
* Assign a pseudorandom next_circ_id between 0 and 2**15.
*
* Initialize conn's timestamps to now.
*/
connection_t *connection_new(int type) {
connection_t *conn;
time_t now = time(NULL);
......@@ -99,6 +114,10 @@ connection_t *connection_new(int type) {
return conn;
}
/* Deallocate memory used by 'conn'. Deallocate its buffers if necessary,
* close its socket if necessary, and mark the directory as dirty if conn
* is an OR or OP connection.
*/
void connection_free(connection_t *conn) {
tor_assert(conn);
tor_assert(conn->magic == CONNECTION_MAGIC);
......@@ -110,7 +129,7 @@ void connection_free(connection_t *conn) {
tor_free(conn->address);
if(connection_speaks_cells(conn)) {
directory_set_dirty();
directory_set_dirty(); /* XXX should only do this for an open OR conn */
if (conn->tls)
tor_tls_free(conn->tls);
}
......
......@@ -122,7 +122,7 @@ connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router) {
* state to 'connecting' and return. If connect to router succeeds, call
* connection_tls_start_handshake() on it.
*
* This function is called from router_retry_connections() , for
* This function is called from router_retry_connections(), for
* ORs connecting to ORs, and circuit_establish_circuit(), for
* OPs connecting to ORs.
*
......@@ -193,7 +193,7 @@ int connection_tls_start_handshake(connection_t *conn, int receiving) {
return 0;
}
/* Move forward with ths tls handshake. If it finishes, hand
/* Move forward with the tls handshake. If it finishes, hand
* conn to connection_tls_finish_handshake().
*
* Return -1 if conn is broken, else return 0.
......@@ -221,9 +221,9 @@ int connection_tls_continue_handshake(connection_t *conn) {
*
* 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.
* If it's an OR (is has a certificate), make sure it has a recognized
* nickname, 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
* 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
* he initiated the connection, make sure he's not already connected.
*
* If he initiated the conn, also initialize conn from the information
......
......@@ -204,7 +204,7 @@ int cpuworker_main(void *data) {
if(recv(fd, &question_type, 1, 0) != 1) {
// log_fn(LOG_ERR,"read type failed. Exiting.");
log_fn(LOG_INFO,"cpuworker exiting because tor process died.");
log_fn(LOG_INFO,"cpuworker exiting because tor process closed connection (either rotated keys or died).");
goto end;
}
tor_assert(question_type == CPUWORKER_TASK_ONION);
......
......@@ -75,9 +75,6 @@ void directory_initiate_command(routerinfo_t *router, int purpose,
conn->purpose = purpose;
/* queue the command on the outbuf */
directory_send_command(conn, purpose, payload, payload_len);
/* give it an initial state */
conn->state = DIR_CONN_STATE_CONNECTING;
......@@ -93,6 +90,9 @@ void directory_initiate_command(routerinfo_t *router, int purpose,
conn->state = DIR_CONN_STATE_CLIENT_SENDING; /* start flushing conn */
/* fall through */
case 0:
/* queue the command on the outbuf */
directory_send_command(conn, purpose, payload, payload_len);
connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
/* writable indicates finish, readable indicates broken link,
error indicates broken link in windowsland. */
......@@ -111,7 +111,9 @@ void directory_initiate_command(routerinfo_t *router, int purpose,
conn->state = DIR_CONN_STATE_CLIENT_SENDING;
connection_add(conn);
connection_start_reading(conn);
/* queue the command on the outbuf */
directory_send_command(conn, purpose, payload, payload_len);
connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
}
}
......@@ -286,7 +288,7 @@ int connection_dir_process_inbuf(connection_t *conn) {
log_fn(LOG_WARN,"http status 400 (bad request) response from dirserver. Malformed server descriptor?");
break;
case 403:
log_fn(LOG_WARN,"http status 403 (unapproved server) response from dirserver. Is your clock skewed? Have you mailed arma your identity fingerprint? Are you using the right key? See README.");
log_fn(LOG_WARN,"http status 403 (unapproved server) response from dirserver. Is your clock skewed? Have you mailed us your identity fingerprint? Are you using the right key? See README.");
break;
default:
......
......@@ -172,9 +172,7 @@ int connection_is_writing(connection_t *conn) {
/* Tell the main loop to stop notifying 'conn' of any write events. */
void connection_stop_writing(connection_t *conn) {
tor_assert(conn && conn->poll_index >= 0 && conn->poll_index < nfds);
if(poll_array[conn->poll_index].events & POLLOUT)
poll_array[conn->poll_index].events -= POLLOUT;
}
......
......@@ -674,7 +674,6 @@ void assert_buf_ok(buf_t *buf);
/********************************* circuit.c ***************************/
extern char *circuit_state_to_string[];
void circuit_add(circuit_t *circ);
void circuit_remove(circuit_t *circ);
circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn);
void circuit_close_all_marked(void);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment