aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2005-03-22 19:36:38 +0000
committerNick Mathewson <nickm@torproject.org>2005-03-22 19:36:38 +0000
commit7a0072cc1a70b42dd38c58ac20fe873c48beeace (patch)
treec3a79af68b99022e1045f62de02e0d76cfce8a82 /src
parentec81f870181940909507fd5356fa5ecc11c7440e (diff)
downloadtor-7a0072cc1a70b42dd38c58ac20fe873c48beeace.tar.gz
tor-7a0072cc1a70b42dd38c58ac20fe873c48beeace.zip
Specify and implement close-stream and close-circuit control messages
svn:r3814
Diffstat (limited to 'src')
-rw-r--r--src/or/control.c83
-rw-r--r--src/or/or.h2
-rw-r--r--src/or/relay.c28
3 files changed, 97 insertions, 16 deletions
diff --git a/src/or/control.c b/src/or/control.c
index a655521ec9..3114dbf2d3 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -48,8 +48,10 @@ const char control_c_id[] = "$Id$";
#define CONTROL_CMD_POSTDESCRIPTOR 0x000F
#define CONTROL_CMD_FRAGMENTHEADER 0x0010
#define CONTROL_CMD_FRAGMENT 0x0011
-#define CONTROL_CMD_REDIRECTSTREAM 0x0012
-#define _CONTROL_CMD_MAX_RECOGNIZED 0x0012
+#define CONTROL_CMD_REDIRECTSTREAM 0x0012
+#define CONTROL_CMD_CLOSESTREAM 0x0013
+#define CONTROL_CMD_CLOSECIRCUIT 0x0014
+#define _CONTROL_CMD_MAX_RECOGNIZED 0x0014
/* Recognized error codes. */
#define ERR_UNSPECIFIED 0x0000
@@ -152,6 +154,10 @@ static int handle_control_postdescriptor(connection_t *conn, uint32_t len,
const char *body);
static int handle_control_redirectstream(connection_t *conn, uint32_t len,
const char *body);
+static int handle_control_closestream(connection_t *conn, uint32_t len,
+ const char *body);
+static int handle_control_closecircuit(connection_t *conn, uint32_t len,
+ const char *body);
/** Given a possibly invalid message type code <b>cmd</b>, return a
* human-readable string equivalent. */
@@ -772,7 +778,72 @@ handle_control_redirectstream(connection_t *conn, uint32_t len,
send_control_done(conn);
return 0;
}
+static int
+handle_control_closestream(connection_t *conn, uint32_t len,
+ const char *body)
+{
+ uint32_t conn_id;
+ connection_t *ap_conn;
+ uint8_t reason;
+ int hold_open;
+
+ if (len < 6) {
+ send_control_error(conn, ERR_SYNTAX, "closestream message too short");
+ return 0;
+ }
+
+ conn_id = ntohl(get_uint32(body));
+ reason = *(uint8_t*)(body+4);
+ hold_open = (*(uint8_t*)(body+5)) & 1;
+
+ if (!(ap_conn = connection_get_by_global_id(conn_id))
+ || ap_conn->state != CONN_TYPE_AP
+ || !ap_conn->socks_request) {
+ send_control_error(conn, ERR_NO_STREAM,
+ "No AP connection found with given ID");
+ return 0;
+ }
+
+ if (!ap_conn->socks_request->has_finished) {
+ socks5_reply_status_t status =
+ connection_edge_end_reason_socks5_response(reason);
+ connection_ap_handshake_socks_reply(ap_conn, NULL, 0, status);
+ }
+ if (hold_open)
+ ap_conn->hold_open_until_flushed = 1;
+ connection_mark_for_close(ap_conn);
+
+ send_control_done(conn);
+ return 0;
+}
+static int
+handle_control_closecircuit(connection_t *conn, uint32_t len,
+ const char *body)
+{
+ uint32_t circ_id;
+ circuit_t *circ;
+ int safe;
+ if (len < 5) {
+ send_control_error(conn, ERR_SYNTAX, "closecircuit message too short");
+ return 0;
+ }
+ circ_id = ntohl(get_uint32(body));
+ safe = (*(uint8_t*)(body+4)) & 1;
+
+ if (!(circ = circuit_get_by_global_id(circ_id))) {
+ send_control_error(conn, ERR_NO_CIRC,
+ "No circuit found with given ID");
+ return 0;
+ }
+
+ if (!safe || !circ->p_streams) {
+ circuit_mark_for_close(circ);
+ }
+
+ send_control_done(conn);
+ return 0;
+}
/** Called when <b>conn</b> has no more bytes left on its outbuf. */
int
@@ -882,6 +953,14 @@ connection_control_process_inbuf(connection_t *conn) {
if (handle_control_redirectstream(conn, body_len, body))
return -1;
break;
+ case CONTROL_CMD_CLOSESTREAM:
+ if (handle_control_closestream(conn, body_len, body))
+ return -1;
+ break;
+ case CONTROL_CMD_CLOSECIRCUIT:
+ if (handle_control_closecircuit(conn, body_len, body))
+ return -1;
+ break;
case CONTROL_CMD_ERROR:
case CONTROL_CMD_DONE:
case CONTROL_CMD_CONFVALUE:
diff --git a/src/or/or.h b/src/or/or.h
index 19efee2760..c4e7f4f28d 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1560,7 +1560,7 @@ int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
size_t payload_len, crypt_path_t *cpath_layer);
int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial);
void connection_edge_consider_sending_sendme(connection_t *conn);
-socks5_reply_status_t connection_edge_end_reason_sock5_response(char *payload, uint16_t length);
+socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
int errno_to_end_reason(int e);
extern uint64_t stats_n_data_cells_packaged;
diff --git a/src/or/relay.c b/src/or/relay.c
index 5fd5be9097..5651fd35e1 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -476,16 +476,13 @@ connection_edge_end_reason_str(char *payload, uint16_t length) {
}
}
-/** Translate the <b>payload</b> of length <b>length</b>, which
- * came from a relay 'end' cell, into an appropriate SOCKS5 reply code.
+/** Translate <b>reason</b> (as from a relay 'end' cell) into an
+ * appropriate SOCKS5 reply code.
*/
-static socks5_reply_status_t
-connection_edge_end_reason_socks5_response(char *payload, uint16_t length) {
- if (length < 1) {
- log_fn(LOG_WARN,"End cell arrived with length 0. Should be at least 1.");
- return SOCKS5_GENERAL_ERROR;
- }
- switch (*payload) {
+socks5_reply_status_t
+connection_edge_end_reason_socks5_response(int reason)
+{
+ switch (reason) {
case END_STREAM_REASON_MISC:
return SOCKS5_GENERAL_ERROR;
case END_STREAM_REASON_RESOLVEFAILED:
@@ -511,7 +508,7 @@ connection_edge_end_reason_socks5_response(char *payload, uint16_t length) {
case END_STREAM_REASON_TORPROTOCOL:
return SOCKS5_GENERAL_ERROR;
default:
- log_fn(LOG_WARN,"Reason for ending (%d) not recognized.",*payload);
+ log_fn(LOG_WARN,"Reason for ending (%d) not recognized.",reason);
return SOCKS5_GENERAL_ERROR;
}
}
@@ -817,9 +814,14 @@ connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
rh.length),
conn->stream_id, (int)conn->stream_size);
if (conn->socks_request && !conn->socks_request->has_finished) {
- socks5_reply_status_t status =
- connection_edge_end_reason_socks5_response(
- cell->payload+RELAY_HEADER_SIZE, rh.length);
+ socks5_reply_status_t status;
+ if (rh.length < 1) {
+ log_fn(LOG_WARN,"End cell arrived with length 0. Should be at least 1.");
+ status = SOCKS5_GENERAL_ERROR;
+ } else {
+ status = connection_edge_end_reason_socks5_response(
+ *(uint8_t*)cell->payload+RELAY_HEADER_SIZE);
+ }
connection_ap_handshake_socks_reply(conn, NULL, 0, status);
}
#ifdef HALF_OPEN