diff options
Diffstat (limited to 'src/or/control.c')
-rw-r--r-- | src/or/control.c | 322 |
1 files changed, 165 insertions, 157 deletions
diff --git a/src/or/control.c b/src/or/control.c index 7737e9a21f..d8e2fb5a7b 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -131,57 +131,57 @@ static int disable_log_messages = 0; static int authentication_cookie_is_set = 0; static char authentication_cookie[AUTHENTICATION_COOKIE_LEN]; -static void connection_printf_to_buf(connection_t *conn, +static void connection_printf_to_buf(control_connection_t *conn, const char *format, ...) CHECK_PRINTF(2,3); /*static*/ size_t write_escaped_data(const char *data, size_t len, int translate_newlines, char **out); /*static*/ size_t read_escaped_data(const char *data, size_t len, int translate_newlines, char **out); -static void send_control0_message(connection_t *conn, uint16_t type, +static void send_control0_message(control_connection_t *conn, uint16_t type, uint32_t len, const char *body); -static void send_control_done(connection_t *conn); -static void send_control_done2(connection_t *conn, const char *msg, +static void send_control_done(control_connection_t *conn); +static void send_control_done2(control_connection_t *conn, const char *msg, size_t len); -static void send_control0_error(connection_t *conn, uint16_t error, +static void send_control0_error(control_connection_t *conn, uint16_t error, const char *message); static void send_control0_event(uint16_t event, uint32_t len, const char *body); static void send_control1_event(uint16_t event, const char *format, ...) CHECK_PRINTF(2,3); -static int handle_control_setconf(connection_t *conn, uint32_t len, +static int handle_control_setconf(control_connection_t *conn, uint32_t len, char *body); -static int handle_control_resetconf(connection_t *conn, uint32_t len, +static int handle_control_resetconf(control_connection_t *conn, uint32_t len, char *body); -static int handle_control_getconf(connection_t *conn, uint32_t len, +static int handle_control_getconf(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_setevents(connection_t *conn, uint32_t len, +static int handle_control_setevents(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_authenticate(connection_t *conn, uint32_t len, +static int handle_control_authenticate(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_saveconf(connection_t *conn, uint32_t len, +static int handle_control_saveconf(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_signal(connection_t *conn, uint32_t len, +static int handle_control_signal(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_mapaddress(connection_t *conn, uint32_t len, +static int handle_control_mapaddress(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_getinfo(connection_t *conn, uint32_t len, +static int handle_control_getinfo(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_extendcircuit(connection_t *conn, uint32_t len, +static int handle_control_extendcircuit(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_setpurpose(connection_t *conn, int for_circuits, +static int handle_control_setpurpose(control_connection_t *conn, int for_circuits, uint32_t len, const char *body); -static int handle_control_attachstream(connection_t *conn, uint32_t len, +static int handle_control_attachstream(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_postdescriptor(connection_t *conn, uint32_t len, +static int handle_control_postdescriptor(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_redirectstream(connection_t *conn, uint32_t len, +static int handle_control_redirectstream(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_closestream(connection_t *conn, uint32_t len, +static int handle_control_closestream(control_connection_t *conn, uint32_t len, const char *body); -static int handle_control_closecircuit(connection_t *conn, uint32_t len, +static int handle_control_closecircuit(control_connection_t *conn, uint32_t len, const char *body); -static int write_stream_target_to_buf(connection_t *conn, char *buf, +static int write_stream_target_to_buf(edge_connection_t *conn, char *buf, size_t len); /** Given a possibly invalid message type code <b>cmd</b>, return a @@ -235,10 +235,11 @@ control_update_global_event_mask(void) for (i = 0; i < n_conns; ++i) { if (conns[i]->type == CONN_TYPE_CONTROL && STATE_IS_OPEN(conns[i]->state)) { - if (STATE_IS_V0(conns[i]->state)) - global_event_mask0 |= conns[i]->event_mask; + control_connection_t *conn = TO_CONTROL_CONN(conns[i]); + if (STATE_IS_V0(conn->_base.state)) + global_event_mask0 |= conn->event_mask; else - global_event_mask1 |= conns[i]->event_mask; + global_event_mask1 |= conn->event_mask; } } @@ -281,10 +282,10 @@ control_adjust_event_log_severity(void) * <b>conn</b>-\>outbuf */ static INLINE void -connection_write_str_to_buf(const char *s, connection_t *conn) +connection_write_str_to_buf(const char *s, control_connection_t *conn) { size_t len = strlen(s); - connection_write_to_buf(s, len, conn); + connection_write_to_buf(s, len, TO_CONN(conn)); } /** Given a <b>len</b>-character string in <b>data</b>, made of lines @@ -446,7 +447,7 @@ get_escaped_string(const char *start, size_t in_len_max, * Currently the length of the message is limited to 1024 (including the * ending \n\r\0. */ static void -connection_printf_to_buf(connection_t *conn, const char *format, ...) +connection_printf_to_buf(control_connection_t *conn, const char *format, ...) { #define CONNECTION_PRINTF_TO_BUF_BUFFERSIZE 1024 va_list ap; @@ -462,41 +463,41 @@ connection_printf_to_buf(connection_t *conn, const char *format, ...) buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-2] = '\n'; buf[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE-3] = '\r'; } - connection_write_to_buf(buf, len, conn); + connection_write_to_buf(buf, len, TO_CONN(conn)); } /** Send a message of type <b>type</b> containing <b>len</b> bytes * from <b>body</b> along the control connection <b>conn</b> */ static void -send_control0_message(connection_t *conn, uint16_t type, uint32_t len, - const char *body) +send_control0_message(control_connection_t *conn, uint16_t type, uint32_t len, + const char *body) { char buf[10]; tor_assert(conn); - tor_assert(STATE_IS_V0(conn->state)); + tor_assert(STATE_IS_V0(conn->_base.state)); tor_assert(len || !body); tor_assert(type <= _CONTROL0_CMD_MAX_RECOGNIZED); if (len < 65536) { set_uint16(buf, htons(len)); set_uint16(buf+2, htons(type)); - connection_write_to_buf(buf, 4, conn); + connection_write_to_buf(buf, 4, TO_CONN(conn)); if (len) - connection_write_to_buf(body, len, conn); + connection_write_to_buf(body, len, TO_CONN(conn)); } else { set_uint16(buf, htons(65535)); set_uint16(buf+2, htons(CONTROL0_CMD_FRAGMENTHEADER)); set_uint16(buf+4, htons(type)); set_uint32(buf+6, htonl(len)); - connection_write_to_buf(buf, 10, conn); - connection_write_to_buf(body, 65535-6, conn); + connection_write_to_buf(buf, 10, TO_CONN(conn)); + connection_write_to_buf(body, 65535-6, TO_CONN(conn)); len -= (65535-6); body += (65535-6); while (len) { size_t chunklen = (len<65535)?len:65535; set_uint16(buf, htons((uint16_t)chunklen)); set_uint16(buf+2, htons(CONTROL0_CMD_FRAGMENT)); - connection_write_to_buf(buf, 4, conn); - connection_write_to_buf(body, chunklen, conn); + connection_write_to_buf(buf, 4, TO_CONN(conn)); + connection_write_to_buf(body, chunklen, TO_CONN(conn)); len -= chunklen; body += chunklen; } @@ -505,9 +506,9 @@ send_control0_message(connection_t *conn, uint16_t type, uint32_t len, /** Send a "DONE" message down the control connection <b>conn</b> */ static void -send_control_done(connection_t *conn) +send_control_done(control_connection_t *conn) { - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { send_control0_message(conn, CONTROL0_CMD_DONE, 0, NULL); } else { connection_write_str_to_buf("250 OK\r\n", conn); @@ -518,7 +519,7 @@ send_control_done(connection_t *conn) * as provided in the <b>len</b> bytes at <b>msg</b>. */ static void -send_control_done2(connection_t *conn, const char *msg, size_t len) +send_control_done2(control_connection_t *conn, const char *msg, size_t len) { if (len==0) len = strlen(msg); @@ -528,7 +529,7 @@ send_control_done2(connection_t *conn, const char *msg, size_t len) /** Send an error message with error code <b>error</b> and body * <b>message</b> down the connection <b>conn</b> */ static void -send_control0_error(connection_t *conn, uint16_t error, const char *message) +send_control0_error(control_connection_t *conn, uint16_t error, const char *message) { char buf[256]; size_t len; @@ -561,11 +562,13 @@ send_control0_event(uint16_t event, uint32_t len, const char *body) for (i = 0; i < n_conns; ++i) { if (conns[i]->type == CONN_TYPE_CONTROL && !conns[i]->marked_for_close && - conns[i]->state == CONTROL_CONN_STATE_OPEN_V0 && - conns[i]->event_mask & (1<<event)) { - send_control0_message(conns[i], CONTROL0_CMD_EVENT, buflen, buf); - if (event == EVENT_ERR_MSG) - _connection_controller_force_write(conns[i]); + conns[i]->state == CONTROL_CONN_STATE_OPEN_V0) { + control_connection_t *control_conn = TO_CONTROL_CONN(conns[i]); + if (control_conn->event_mask & (1<<event)) { + send_control0_message(control_conn, CONTROL0_CMD_EVENT, buflen, buf); + if (event == EVENT_ERR_MSG) + _connection_controller_force_write(control_conn); + } } } @@ -586,11 +589,13 @@ send_control1_event_string(uint16_t event, const char *msg) for (i = 0; i < n_conns; ++i) { if (conns[i]->type == CONN_TYPE_CONTROL && !conns[i]->marked_for_close && - conns[i]->state == CONTROL_CONN_STATE_OPEN_V1 && - conns[i]->event_mask & (1<<event)) { - connection_write_to_buf(msg, strlen(msg), conns[i]); - if (event == EVENT_ERR_MSG) - _connection_controller_force_write(conns[i]); + conns[i]->state == CONTROL_CONN_STATE_OPEN_V1) { + control_connection_t *control_conn = TO_CONTROL_CONN(conns[i]); + if (control_conn->event_mask & (1<<event)) { + connection_write_to_buf(msg, strlen(msg), TO_CONN(control_conn)); + if (event == EVENT_ERR_MSG) + _connection_controller_force_write(control_conn); + } } } } @@ -657,14 +662,14 @@ get_stream(const char *id) * it passes <b>use_defaults</b> on to options_trial_assign(). */ static int -control_setconf_helper(connection_t *conn, uint32_t len, char *body, +control_setconf_helper(control_connection_t *conn, uint32_t len, char *body, int use_defaults, int clear_first) { int r; config_line_t *lines=NULL; char *start = body; char *errstring = NULL; - int v0 = STATE_IS_V0(conn->state); + int v0 = STATE_IS_V0(conn->_base.state); if (!v0) { char *config = tor_malloc(len+1); @@ -761,7 +766,7 @@ control_setconf_helper(connection_t *conn, uint32_t len, char *body, /** Called when we receive a SETCONF message: parse the body and try * to update our configuration. Reply with a DONE or ERROR message. */ static int -handle_control_setconf(connection_t *conn, uint32_t len, char *body) +handle_control_setconf(control_connection_t *conn, uint32_t len, char *body) { return control_setconf_helper(conn, len, body, 0, 1); } @@ -769,9 +774,9 @@ handle_control_setconf(connection_t *conn, uint32_t len, char *body) /** Called when we receive a RESETCONF message: parse the body and try * to update our configuration. Reply with a DONE or ERROR message. */ static int -handle_control_resetconf(connection_t *conn, uint32_t len, char *body) +handle_control_resetconf(control_connection_t *conn, uint32_t len, char *body) { - int v0 = STATE_IS_V0(conn->state); + int v0 = STATE_IS_V0(conn->_base.state); tor_assert(!v0); return control_setconf_helper(conn, len, body, 1, 1); } @@ -779,7 +784,7 @@ handle_control_resetconf(connection_t *conn, uint32_t len, char *body) /** Called when we receive a GETCONF message. Parse the request, and * reply with a CONFVALUE or an ERROR message */ static int -handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body) +handle_control_getconf(control_connection_t *conn, uint32_t body_len, const char *body) { smartlist_t *questions = NULL; smartlist_t *answers = NULL; @@ -787,7 +792,7 @@ handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body) char *msg = NULL; size_t msg_len; or_options_t *options = get_options(); - int v0 = STATE_IS_V0(conn->state); + int v0 = STATE_IS_V0(conn->_base.state); questions = smartlist_create(); (void) body_len; /* body is nul-terminated; so we can ignore len. */ @@ -858,7 +863,7 @@ handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body) tor_assert(strlen(tmp)>4); tmp[3] = ' '; msg = smartlist_join_strings(answers, "", 0, &msg_len); - connection_write_to_buf(msg, msg_len, conn); + connection_write_to_buf(msg, msg_len, TO_CONN(conn)); } else { connection_write_str_to_buf("250 OK\r\n", conn); } @@ -882,13 +887,13 @@ handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body) /** Called when we get a SETEVENTS message: update conn->event_mask, * and reply with DONE or ERROR. */ static int -handle_control_setevents(connection_t *conn, uint32_t len, const char *body) +handle_control_setevents(control_connection_t *conn, uint32_t len, const char *body) { uint16_t event_code; uint32_t event_mask = 0; unsigned int extended = 0; - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { if (len % 2) { send_control0_error(conn, ERR_SYNTAX, "Odd number of bytes in setevents message"); @@ -950,7 +955,7 @@ handle_control_setevents(connection_t *conn, uint32_t len, const char *body) smartlist_free(events); } conn->event_mask = event_mask; - conn->control_events_are_extended = extended; + conn->_base.control_events_are_extended = extended; control_update_global_event_mask(); send_control_done(conn); @@ -987,13 +992,13 @@ decode_hashed_password(char *buf, const char *hashed) * OPEN. Reply with DONE or ERROR. */ static int -handle_control_authenticate(connection_t *conn, uint32_t len, const char *body) +handle_control_authenticate(control_connection_t *conn, uint32_t len, const char *body) { int used_quoted_string = 0; or_options_t *options = get_options(); char *password; size_t password_len; - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { password = (char*)body; password_len = len; } else { @@ -1047,7 +1052,7 @@ handle_control_authenticate(connection_t *conn, uint32_t len, const char *body) } err: - if (STATE_IS_V0(conn->state)) + if (STATE_IS_V0(conn->_base.state)) send_control0_error(conn,ERR_REJECTED_AUTHENTICATION, "Authentication failed"); else { @@ -1061,12 +1066,12 @@ handle_control_authenticate(connection_t *conn, uint32_t len, const char *body) } return 0; ok: - log_info(LD_CONTROL, "Authenticated control connection (%d)", conn->s); + log_info(LD_CONTROL, "Authenticated control connection (%d)", conn->_base.s); send_control_done(conn); - if (STATE_IS_V0(conn->state)) - conn->state = CONTROL_CONN_STATE_OPEN_V0; + if (STATE_IS_V0(conn->_base.state)) + conn->_base.state = CONTROL_CONN_STATE_OPEN_V0; else { - conn->state = CONTROL_CONN_STATE_OPEN_V1; + conn->_base.state = CONTROL_CONN_STATE_OPEN_V1; tor_free(password); } return 0; @@ -1075,13 +1080,13 @@ handle_control_authenticate(connection_t *conn, uint32_t len, const char *body) /** Called when we get a SAVECONF command. Try to flush the current options to * disk, and report success or failure. */ static int -handle_control_saveconf(connection_t *conn, uint32_t len, +handle_control_saveconf(control_connection_t *conn, uint32_t len, const char *body) { (void) len; (void) body; if (options_save_current()<0) { - if (STATE_IS_V0(conn->state)) + if (STATE_IS_V0(conn->_base.state)) send_control0_error(conn, ERR_INTERNAL, "Unable to write configuration to disk."); else @@ -1097,11 +1102,11 @@ handle_control_saveconf(connection_t *conn, uint32_t len, * report success or failure. (If the signal results in a shutdown, success * may not be reported.) */ static int -handle_control_signal(connection_t *conn, uint32_t len, +handle_control_signal(control_connection_t *conn, uint32_t len, const char *body) { int sig; - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { if (len != 1) { send_control0_error(conn, ERR_SYNTAX, "Body of SIGNAL command too long or too short."); @@ -1138,7 +1143,7 @@ handle_control_signal(connection_t *conn, uint32_t len, } if (control_signal_act(sig) < 0) { - if (STATE_IS_V0(conn->state)) + if (STATE_IS_V0(conn->_base.state)) send_control0_error(conn, ERR_SYNTAX, "Unrecognized signal number."); else connection_write_str_to_buf("551 Internal error acting on signal\r\n", @@ -1152,14 +1157,14 @@ handle_control_signal(connection_t *conn, uint32_t len, /** Called when we get a MAPADDRESS command; try to bind all listed addresses, * and report success or failrue. */ static int -handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body) +handle_control_mapaddress(control_connection_t *conn, uint32_t len, const char *body) { smartlist_t *elts; smartlist_t *lines; smartlist_t *reply; char *r; size_t sz; - int v0 = STATE_IS_V0(conn->state); + int v0 = STATE_IS_V0(conn->_base.state); (void) len; /* body is nul-terminated, so it's safe to ignore the length. */ lines = smartlist_create(); @@ -1257,12 +1262,12 @@ handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body) if (smartlist_len(reply)) { ((char*)smartlist_get(reply,smartlist_len(reply)-1))[3] = ' '; r = smartlist_join_strings(reply, "\r\n", 1, &sz); - connection_write_to_buf(r, sz, conn); + connection_write_to_buf(r, sz, TO_CONN(conn)); tor_free(r); } else { const char *response = "512 syntax error: not enough arguments to mapaddress.\r\n"; - connection_write_to_buf(response, strlen(response), conn); + connection_write_to_buf(response, strlen(response), TO_CONN(conn)); } } @@ -1399,6 +1404,7 @@ handle_getinfo_helper(const char *question, char **answer) get_connection_array(&conns, &n_conns); for (i=0; i < n_conns; ++i) { const char *state; + edge_connection_t *conn; char *s; size_t slen; circuit_t *circ; @@ -1406,12 +1412,13 @@ handle_getinfo_helper(const char *question, char **answer) conns[i]->marked_for_close || conns[i]->state == AP_CONN_STATE_SOCKS_WAIT) continue; - switch (conns[i]->state) + conn = TO_EDGE_CONN(conns[i]); + switch (conn->_base.state) { case AP_CONN_STATE_CONTROLLER_WAIT: case AP_CONN_STATE_CIRCUIT_WAIT: - if (conns[i]->socks_request && - conns[i]->socks_request->command == SOCKS_COMMAND_RESOLVE) + if (conn->socks_request && + conn->socks_request->command == SOCKS_COMMAND_RESOLVE) state = "NEWRESOLVE"; else state = "NEW"; @@ -1425,15 +1432,15 @@ handle_getinfo_helper(const char *question, char **answer) state = "SUCCEEDED"; break; default: log_warn(LD_BUG, "Asked for stream in unknown state %d", - conns[i]->state); + conn->_base.state); continue; } - circ = circuit_get_by_edge_conn(conns[i]); - write_stream_target_to_buf(conns[i], buf, sizeof(buf)); + circ = circuit_get_by_edge_conn(conn); + write_stream_target_to_buf(conn, buf, sizeof(buf)); slen = strlen(buf)+strlen(state)+32; s = tor_malloc(slen+1); tor_snprintf(s, slen, "%lu %s %lu %s", - (unsigned long) conns[i]->global_identifier,state, + (unsigned long) conn->_base.global_identifier,state, circ?(unsigned long)circ->global_identifier : 0ul, buf); smartlist_add(status, s); @@ -1451,10 +1458,11 @@ handle_getinfo_helper(const char *question, char **answer) char *s; char name[128]; size_t slen; - connection_t *conn = conns[i]; - if (conn->type != CONN_TYPE_OR || conn->marked_for_close) + or_connection_t *conn; + if (conns[i]->type != CONN_TYPE_OR || conns[i]->marked_for_close) continue; - if (conn->state == OR_CONN_STATE_OPEN) + conn = TO_OR_CONN(conns[i]); + if (conn->_base.state == OR_CONN_STATE_OPEN) state = "CONNECTED"; else if (conn->nickname) state = "LAUNCHED"; @@ -1464,7 +1472,7 @@ handle_getinfo_helper(const char *question, char **answer) strlcpy(name, conn->nickname, sizeof(name)); else tor_snprintf(name, sizeof(name), "%s:%d", - conn->address, conn->port); + conn->_base.address, conn->_base.port); slen = strlen(name)+strlen(state)+2; s = tor_malloc(slen+1); @@ -1544,14 +1552,14 @@ handle_getinfo_helper(const char *question, char **answer) /** Called when we receive a GETINFO command. Try to fetch all requested * information, and reply with information or error message. */ static int -handle_control_getinfo(connection_t *conn, uint32_t len, const char *body) +handle_control_getinfo(control_connection_t *conn, uint32_t len, const char *body) { smartlist_t *questions = NULL; smartlist_t *answers = NULL; smartlist_t *unrecognized = NULL; char *msg = NULL, *ans = NULL; size_t msg_len; - int v0 = STATE_IS_V0(conn->state); + int v0 = STATE_IS_V0(conn->_base.state); (void) len; /* body is nul-terminated, so it's safe to ignore the length. */ questions = smartlist_create(); @@ -1615,7 +1623,7 @@ handle_control_getinfo(connection_t *conn, uint32_t len, const char *body) size_t len; len = write_escaped_data(v, strlen(v), 1, &esc); connection_printf_to_buf(conn, "250+%s=\r\n", k); - connection_write_to_buf(esc, len, conn); + connection_write_to_buf(esc, len, TO_CONN(conn)); tor_free(esc); } } @@ -1662,7 +1670,7 @@ get_purpose(char *string, int for_circuits, uint8_t *purpose) /** Called when we get an EXTENDCIRCUIT message. Try to extend the listed * circuit, and report success or failure. */ static int -handle_control_extendcircuit(connection_t *conn, uint32_t len, +handle_control_extendcircuit(control_connection_t *conn, uint32_t len, const char *body) { smartlist_t *router_nicknames=NULL, *routers=NULL; @@ -1672,7 +1680,7 @@ handle_control_extendcircuit(connection_t *conn, uint32_t len, char reply[4]; uint8_t intended_purpose = CIRCUIT_PURPOSE_C_GENERAL; - v0 = STATE_IS_V0(conn->state); + v0 = STATE_IS_V0(conn->_base.state); router_nicknames = smartlist_create(); if (v0) { @@ -1816,7 +1824,7 @@ handle_control_extendcircuit(connection_t *conn, uint32_t len, * is 1) or SETROUTERPURPOSE message. If we can find * the circuit/router and it's a valid purpose, change it. */ static int -handle_control_setpurpose(connection_t *conn, int for_circuits, +handle_control_setpurpose(control_connection_t *conn, int for_circuits, uint32_t len, const char *body) { circuit_t *circ = NULL; @@ -1868,14 +1876,15 @@ done: /** Called when we get an ATTACHSTREAM message. Try to attach the requested * stream, and report success or failure. */ static int -handle_control_attachstream(connection_t *conn, uint32_t len, +handle_control_attachstream(control_connection_t *conn, uint32_t len, const char *body) { connection_t *ap_conn = NULL; circuit_t *circ = NULL; int zero_circ; + edge_connection_t *edge_conn; - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { uint32_t conn_id; uint32_t circ_id; if (len < 8) { @@ -1927,7 +1936,7 @@ handle_control_attachstream(connection_t *conn, uint32_t len, if (ap_conn->state != AP_CONN_STATE_CONTROLLER_WAIT && ap_conn->state != AP_CONN_STATE_CONNECT_WAIT && ap_conn->state != AP_CONN_STATE_RESOLVE_WAIT) { - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { send_control0_error(conn, ERR_NO_STREAM, "Connection is not managed by controller."); } else { @@ -1938,20 +1947,23 @@ handle_control_attachstream(connection_t *conn, uint32_t len, return 0; } + edge_conn = TO_EDGE_CONN(ap_conn); + /* Do we need to detach it first? */ if (ap_conn->state != AP_CONN_STATE_CONTROLLER_WAIT) { - circuit_t *tmpcirc = circuit_get_by_edge_conn(ap_conn); - connection_edge_end(ap_conn, END_STREAM_REASON_TIMEOUT, conn->cpath_layer); + circuit_t *tmpcirc = circuit_get_by_edge_conn(edge_conn); + connection_edge_end(edge_conn, END_STREAM_REASON_TIMEOUT, + edge_conn->cpath_layer); /* Un-mark it as ending, since we're going to reuse it. */ - ap_conn->has_sent_end = 0; + ap_conn->edge_has_sent_end = 0; if (tmpcirc) - circuit_detach_stream(tmpcirc,ap_conn); + circuit_detach_stream(tmpcirc,edge_conn); ap_conn->state = AP_CONN_STATE_CONTROLLER_WAIT; } if (circ && (circ->state != CIRCUIT_STATE_OPEN || ! CIRCUIT_IS_ORIGIN(circ))) { - if (STATE_IS_V0(conn->state)) + if (STATE_IS_V0(conn->_base.state)) send_control0_error(conn, ERR_INTERNAL, "Refuse to attach stream to non-open, origin circ."); else @@ -1960,9 +1972,9 @@ handle_control_attachstream(connection_t *conn, uint32_t len, conn); return 0; } - if (connection_ap_handshake_rewrite_and_attach(ap_conn, + if (connection_ap_handshake_rewrite_and_attach(edge_conn, circ ? TO_ORIGIN_CIRCUIT(circ) : NULL) < 0) { - if (STATE_IS_V0(conn->state)) + if (STATE_IS_V0(conn->_base.state)) send_control0_error(conn, ERR_INTERNAL, "Unable to attach stream."); else connection_write_str_to_buf("551 Unable to attach stream\r\n", conn); @@ -1975,11 +1987,11 @@ handle_control_attachstream(connection_t *conn, uint32_t len, /** Called when we get a POSTDESCRIPTOR message. Try to learn the provided * descriptor, and report success or failure. */ static int -handle_control_postdescriptor(connection_t *conn, uint32_t len, +handle_control_postdescriptor(control_connection_t *conn, uint32_t len, const char *body) { char *desc; - int v0 = STATE_IS_V0(conn->state); + int v0 = STATE_IS_V0(conn->_base.state); const char *msg=NULL; uint8_t purpose = ROUTER_PURPOSE_GENERAL; @@ -2034,14 +2046,14 @@ handle_control_postdescriptor(connection_t *conn, uint32_t len, /** Called when we receive a REDIRECTSTERAM command. Try to change the target * address of the named AP stream, and report success or failure. */ static int -handle_control_redirectstream(connection_t *conn, uint32_t len, +handle_control_redirectstream(control_connection_t *conn, uint32_t len, const char *body) { connection_t *ap_conn = NULL; uint32_t conn_id; char *new_addr = NULL; uint16_t new_port = 0; - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { if (len < 6) { send_control0_error(conn, ERR_SYNTAX, "redirectstream message too short"); @@ -2051,7 +2063,7 @@ handle_control_redirectstream(connection_t *conn, uint32_t len, if (!(ap_conn = connection_get_by_global_id(conn_id)) || ap_conn->state != CONN_TYPE_AP - || !ap_conn->socks_request) { + || !TO_EDGE_CONN(ap_conn)->socks_request) { send_control0_error(conn, ERR_NO_STREAM, "No AP connection found with given ID"); return 0; @@ -2066,7 +2078,7 @@ handle_control_redirectstream(connection_t *conn, uint32_t len, connection_printf_to_buf(conn, "512 Missing argument to REDIRECTSTREAM\r\n"); else if (!(ap_conn = get_stream(smartlist_get(args, 0))) - || !ap_conn->socks_request) { + || !TO_EDGE_CONN(ap_conn)->socks_request) { connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n", (char*)smartlist_get(args, 0)); } else { @@ -2089,25 +2101,28 @@ handle_control_redirectstream(connection_t *conn, uint32_t len, return 0; } - strlcpy(ap_conn->socks_request->address, new_addr, - sizeof(ap_conn->socks_request->address)); - if (new_port) - ap_conn->socks_request->port = new_port; - tor_free(new_addr); - send_control_done(conn); - return 0; + { + edge_connection_t *ap = TO_EDGE_CONN(ap_conn); + strlcpy(ap->socks_request->address, new_addr, + sizeof(ap->socks_request->address)); + if (new_port) + ap->socks_request->port = new_port; + tor_free(new_addr); + send_control_done(conn); + return 0; + } } /** Called when we get a CLOSESTREAM command; try to close the named stream * and report success or failure. */ static int -handle_control_closestream(connection_t *conn, uint32_t len, +handle_control_closestream(control_connection_t *conn, uint32_t len, const char *body) { connection_t *ap_conn=NULL; uint8_t reason=0; - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { uint32_t conn_id; if (len < 6) { send_control0_error(conn, ERR_SYNTAX, "closestream message too short"); @@ -2119,7 +2134,7 @@ handle_control_closestream(connection_t *conn, uint32_t len, if (!(ap_conn = connection_get_by_global_id(conn_id)) || ap_conn->state != CONN_TYPE_AP - || !ap_conn->socks_request) { + || !TO_EDGE_CONN(ap_conn)->socks_request) { send_control0_error(conn, ERR_NO_STREAM, "No AP connection found with given ID"); return 0; @@ -2151,7 +2166,7 @@ handle_control_closestream(connection_t *conn, uint32_t len, return 0; } - connection_mark_unattached_ap(ap_conn, reason); + connection_mark_unattached_ap(TO_EDGE_CONN(ap_conn), reason); send_control_done(conn); return 0; } @@ -2159,13 +2174,13 @@ handle_control_closestream(connection_t *conn, uint32_t len, /** Called when we get a CLOSECIRCUIT command; try to close the named circuit * and report success or failure. */ static int -handle_control_closecircuit(connection_t *conn, uint32_t len, +handle_control_closecircuit(control_connection_t *conn, uint32_t len, const char *body) { circuit_t *circ = NULL; int safe = 0; - if (STATE_IS_V0(conn->state)) { + if (STATE_IS_V0(conn->_base.state)) { uint32_t circ_id; if (len < 5) { send_control0_error(conn, ERR_SYNTAX, "closecircuit message too short"); @@ -2221,7 +2236,7 @@ handle_control_closecircuit(connection_t *conn, uint32_t len, * fragments and report failure. */ static int -handle_control_fragments(connection_t *conn, uint16_t command_type, +handle_control_fragments(control_connection_t *conn, uint16_t command_type, uint32_t body_len, char *body) { if (command_type == CONTROL0_CMD_FRAGMENTHEADER) { @@ -2262,24 +2277,22 @@ handle_control_fragments(connection_t *conn, uint16_t command_type, /** Called when <b>conn</b> has no more bytes left on its outbuf. */ int -connection_control_finished_flushing(connection_t *conn) +connection_control_finished_flushing(control_connection_t *conn) { tor_assert(conn); - tor_assert(conn->type == CONN_TYPE_CONTROL); - connection_stop_writing(conn); + connection_stop_writing(TO_CONN(conn)); return 0; } /** Called when <b>conn</b> has gotten its socket closed. */ int -connection_control_reached_eof(connection_t *conn) +connection_control_reached_eof(control_connection_t *conn) { tor_assert(conn); - tor_assert(conn->type == CONN_TYPE_CONTROL); log_info(LD_CONTROL,"Control connection reached EOF. Closing."); - connection_mark_for_close(conn); + connection_mark_for_close(TO_CONN(conn)); return 0; } @@ -2287,16 +2300,15 @@ connection_control_reached_eof(connection_t *conn) * commands from conn->inbuf, and execute them. */ static int -connection_control_process_inbuf_v1(connection_t *conn) +connection_control_process_inbuf_v1(control_connection_t *conn) { size_t data_len; int cmd_len; char *args; tor_assert(conn); - tor_assert(conn->type == CONN_TYPE_CONTROL); - tor_assert(conn->state == CONTROL_CONN_STATE_OPEN_V1 || - conn->state == CONTROL_CONN_STATE_NEEDAUTH_V1); + tor_assert(conn->_base.state == CONTROL_CONN_STATE_OPEN_V1 || + conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V1); if (!conn->incoming_cmd) { conn->incoming_cmd = tor_malloc(1024); @@ -2311,7 +2323,7 @@ connection_control_process_inbuf_v1(connection_t *conn) /* First, fetch a line. */ do { data_len = conn->incoming_cmd_len - conn->incoming_cmd_cur_len; - r = fetch_from_buf_line(conn->inbuf, + r = fetch_from_buf_line(conn->_base.inbuf, conn->incoming_cmd+conn->incoming_cmd_cur_len, &data_len); if (r == 0) @@ -2361,11 +2373,11 @@ connection_control_process_inbuf_v1(connection_t *conn) if (!strcasecmp(conn->incoming_cmd, "QUIT")) { connection_write_str_to_buf("250 closing connection\r\n", conn); - connection_mark_for_close(conn); + connection_mark_for_close(TO_CONN(conn)); return 0; } - if (conn->state == CONTROL_CONN_STATE_NEEDAUTH_V1 && + if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V1 && strcasecmp(conn->incoming_cmd, "AUTHENTICATE")) { connection_write_str_to_buf("514 Authentication required.\r\n", conn); conn->incoming_cmd_cur_len = 0; @@ -2436,7 +2448,7 @@ connection_control_process_inbuf_v1(connection_t *conn) * commands from conn->inbuf, and execute them. */ static int -connection_control_process_inbuf_v0(connection_t *conn) +connection_control_process_inbuf_v0(control_connection_t *conn) { uint32_t body_len; uint16_t command_type; @@ -2444,15 +2456,15 @@ connection_control_process_inbuf_v0(connection_t *conn) again: /* Try to suck a control message from the buffer. */ - switch (fetch_from_buf_control0(conn->inbuf, &body_len, &command_type, &body, - conn->state == CONTROL_CONN_STATE_NEEDAUTH_V0)) + switch (fetch_from_buf_control0(conn->_base.inbuf, &body_len, &command_type, &body, + conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V0)) { case -2: tor_free(body); log_info(LD_CONTROL, "Detected v1 control protocol on connection (fd %d)", - conn->s); - conn->state = CONTROL_CONN_STATE_NEEDAUTH_V1; + conn->_base.s); + conn->_base.state = CONTROL_CONN_STATE_NEEDAUTH_V1; return connection_control_process_inbuf_v1(conn); case -1: tor_free(body); @@ -2470,7 +2482,7 @@ connection_control_process_inbuf_v0(connection_t *conn) /* We got a command. If we need authentication, only authentication * commands will be considered. */ - if (conn->state == CONTROL_CONN_STATE_NEEDAUTH_V0 && + if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH_V0 && command_type != CONTROL0_CMD_AUTHENTICATE) { log_info(LD_CONTROL, "Rejecting '%s' command; authentication needed.", control_cmd_to_string(command_type)); @@ -2584,12 +2596,11 @@ connection_control_process_inbuf_v0(connection_t *conn) /** Called when <b>conn</b> has received more bytes on its inbuf. */ int -connection_control_process_inbuf(connection_t *conn) +connection_control_process_inbuf(control_connection_t *conn) { tor_assert(conn); - tor_assert(conn->type == CONN_TYPE_CONTROL); - if (STATE_IS_V0(conn->state)) + if (STATE_IS_V0(conn->_base.state)) return connection_control_process_inbuf_v0(conn); else return connection_control_process_inbuf_v1(conn); @@ -2644,7 +2655,7 @@ control_event_circuit_status(origin_circuit_t *circ, circuit_status_event_t tp) * <b>conn</b>, and write it to <b>buf</b>. Return 0 on success, -1 on * failure. */ static int -write_stream_target_to_buf(connection_t *conn, char *buf, size_t len) +write_stream_target_to_buf(edge_connection_t *conn, char *buf, size_t len) { char buf2[256]; if (conn->chosen_exit_name) @@ -2663,12 +2674,11 @@ write_stream_target_to_buf(connection_t *conn, char *buf, size_t len) /** Something has happened to the stream associated with AP connection * <b>conn</b>: tell any interested control connections. */ int -control_event_stream_status(connection_t *conn, stream_status_event_t tp) +control_event_stream_status(edge_connection_t *conn, stream_status_event_t tp) { char *msg; size_t len; char buf[256]; - tor_assert(conn->type == CONN_TYPE_AP); tor_assert(conn->socks_request); if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS)) @@ -2679,7 +2689,7 @@ control_event_stream_status(connection_t *conn, stream_status_event_t tp) len = strlen(buf); msg = tor_malloc(5+len+1); msg[0] = (uint8_t) tp; - set_uint32(msg+1, htonl(conn->global_identifier)); + set_uint32(msg+1, htonl(conn->_base.global_identifier)); strlcpy(msg+5, buf, len+1); send_control0_event(EVENT_STREAM_STATUS, (uint32_t)(5+len+1), msg); @@ -2705,7 +2715,7 @@ control_event_stream_status(connection_t *conn, stream_status_event_t tp) circ = circuit_get_by_edge_conn(conn); send_control1_event(EVENT_STREAM_STATUS, "650 STREAM %lu %s %lu %s\r\n", - (unsigned long)conn->global_identifier, status, + (unsigned long)conn->_base.global_identifier, status, circ?(unsigned long)circ->global_identifier : 0ul, buf); /* XXX need to specify its intended exit, etc? */ @@ -2716,13 +2726,11 @@ control_event_stream_status(connection_t *conn, stream_status_event_t tp) /** Something has happened to the OR connection <b>conn</b>: tell any * interested control connections. */ int -control_event_or_conn_status(connection_t *conn,or_conn_status_event_t tp) +control_event_or_conn_status(or_connection_t *conn,or_conn_status_event_t tp) { char buf[HEX_DIGEST_LEN+3]; /* status, dollar, identity, NUL */ size_t len; - tor_assert(conn->type == CONN_TYPE_OR); - if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS)) return 0; @@ -2739,7 +2747,7 @@ control_event_or_conn_status(connection_t *conn,or_conn_status_event_t tp) strlcpy(name, conn->nickname, sizeof(name)); else tor_snprintf(name, sizeof(name), "%s:%d", - conn->address, conn->port); + conn->_base.address, conn->_base.port); switch (tp) { case OR_CONN_EVENT_LAUNCHED: status = "LAUNCHED"; break; |