|
@@ -28,21 +28,8 @@ int connection_edge_process_inbuf(connection_t *conn) {
|
|
|
conn->marked_for_close = 1;
|
|
|
|
|
|
|
|
|
- circ = circuit_get_by_conn(conn);
|
|
|
- if (!circ)
|
|
|
- return -1;
|
|
|
-
|
|
|
- memset(&cell, 0, sizeof(cell_t));
|
|
|
- cell.command = CELL_RELAY;
|
|
|
- cell.length = RELAY_HEADER_SIZE;
|
|
|
- SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_END);
|
|
|
- SET_CELL_STREAM_ID(cell, conn->stream_id);
|
|
|
- cell.aci = circ->n_aci;
|
|
|
-
|
|
|
- if (circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION(conn->type), conn->cpath_layer) < 0) {
|
|
|
- log_fn(LOG_WARNING,"(fd %d) circuit_deliver_relay_cell failed. Closing.", conn->s);
|
|
|
- circuit_close(circ);
|
|
|
- }
|
|
|
+ connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_END,
|
|
|
+ NULL, 0, conn->cpath_layer);
|
|
|
return 0;
|
|
|
#else
|
|
|
|
|
@@ -67,15 +54,20 @@ int connection_edge_process_inbuf(connection_t *conn) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command) {
|
|
|
+void connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
|
|
|
+ void *payload, int payload_len, crypt_path_t *cpath_layer) {
|
|
|
cell_t cell;
|
|
|
int cell_direction;
|
|
|
+ int is_control_cell=0;
|
|
|
|
|
|
if(!circ) {
|
|
|
log_fn(LOG_WARNING,"no circ. Closing.");
|
|
|
- return -1;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
+ if(!fromconn || relay_command == RELAY_COMMAND_BEGIN)
|
|
|
+ is_control_cell = 1;
|
|
|
+
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
|
|
if(fromconn && fromconn->type == CONN_TYPE_AP) {
|
|
|
cell.aci = circ->n_aci;
|
|
@@ -88,20 +80,22 @@ int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int re
|
|
|
|
|
|
cell.command = CELL_RELAY;
|
|
|
SET_CELL_RELAY_COMMAND(cell, relay_command);
|
|
|
- if(fromconn)
|
|
|
- SET_CELL_STREAM_ID(cell, fromconn->stream_id);
|
|
|
- else
|
|
|
+ if(is_control_cell)
|
|
|
SET_CELL_STREAM_ID(cell, ZERO_STREAM);
|
|
|
+ else
|
|
|
+ SET_CELL_STREAM_ID(cell, fromconn->stream_id);
|
|
|
|
|
|
- cell.length = RELAY_HEADER_SIZE;
|
|
|
- log_fn(LOG_INFO,"delivering %d cell %s.", relay_command, cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
|
|
|
+ cell.length = RELAY_HEADER_SIZE + payload_len;
|
|
|
+ if(payload_len) {
|
|
|
+ memcpy(cell.payload+RELAY_HEADER_SIZE,payload,payload_len);
|
|
|
+ }
|
|
|
+ log_fn(LOG_INFO,"delivering %d cell %s.", relay_command,
|
|
|
+ cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
|
|
|
|
|
|
- if(circuit_deliver_relay_cell(&cell, circ, cell_direction, fromconn ? fromconn->cpath_layer : NULL) < 0) {
|
|
|
+ if(circuit_deliver_relay_cell(&cell, circ, cell_direction, cpath_layer) < 0) {
|
|
|
log_fn(LOG_WARNING,"circuit_deliver_relay_cell failed. Closing.");
|
|
|
circuit_close(circ);
|
|
|
- return 0;
|
|
|
}
|
|
|
- return 0;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -217,7 +211,9 @@ int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection
|
|
|
circ->n_conn = NULL;
|
|
|
}
|
|
|
log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
|
|
|
- return connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED);
|
|
|
+ connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
|
|
|
+ NULL, 0, NULL);
|
|
|
+ return 0;
|
|
|
case RELAY_COMMAND_TRUNCATED:
|
|
|
if(edge_type == EDGE_EXIT) {
|
|
|
log_fn(LOG_WARNING,"'truncated' unsupported at exit. Dropping.");
|
|
@@ -292,9 +288,10 @@ int connection_edge_finished_flushing(connection_t *conn) {
|
|
|
connection_watch_events(conn, POLLIN);
|
|
|
if(connection_wants_to_flush(conn))
|
|
|
connection_start_writing(conn);
|
|
|
- return
|
|
|
- connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED) ||
|
|
|
- connection_process_inbuf(conn);
|
|
|
+
|
|
|
+ connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED,
|
|
|
+ NULL, 0, conn->cpath_layer);
|
|
|
+ return connection_process_inbuf(conn);
|
|
|
case AP_CONN_STATE_OPEN:
|
|
|
case EXIT_CONN_STATE_OPEN:
|
|
|
connection_stop_writing(conn);
|
|
@@ -316,8 +313,8 @@ uint64_t stats_n_data_cells_received = 0;
|
|
|
uint64_t stats_n_data_bytes_received = 0;
|
|
|
|
|
|
int connection_package_raw_inbuf(connection_t *conn) {
|
|
|
- int amount_to_process;
|
|
|
- cell_t cell;
|
|
|
+ int amount_to_process, length;
|
|
|
+ char payload[CELL_PAYLOAD_SIZE];
|
|
|
circuit_t *circ;
|
|
|
|
|
|
assert(conn);
|
|
@@ -345,49 +342,31 @@ repeat_connection_package_raw_inbuf:
|
|
|
if(!amount_to_process)
|
|
|
return 0;
|
|
|
|
|
|
-
|
|
|
- memset(&cell, 0, sizeof(cell_t));
|
|
|
-
|
|
|
if(amount_to_process > CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE) {
|
|
|
- cell.length = CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE;
|
|
|
+ length = CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE;
|
|
|
} else {
|
|
|
- cell.length = amount_to_process;
|
|
|
+ length = amount_to_process;
|
|
|
}
|
|
|
- stats_n_data_bytes_packaged += cell.length;
|
|
|
+ stats_n_data_bytes_packaged += length;
|
|
|
stats_n_data_cells_packaged += 1;
|
|
|
|
|
|
- connection_fetch_from_buf(cell.payload+RELAY_HEADER_SIZE, cell.length, conn);
|
|
|
-
|
|
|
- log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).",conn->s,cell.length,
|
|
|
+ connection_fetch_from_buf(payload, length, conn);
|
|
|
+
|
|
|
+ log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).",conn->s,length,
|
|
|
(int)buf_datalen(conn->inbuf));
|
|
|
|
|
|
- cell.command = CELL_RELAY;
|
|
|
- SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_DATA);
|
|
|
- SET_CELL_STREAM_ID(cell, conn->stream_id);
|
|
|
- cell.length += RELAY_HEADER_SIZE;
|
|
|
-
|
|
|
+ connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
|
|
|
+ payload, length, conn->cpath_layer);
|
|
|
+
|
|
|
if(conn->type == CONN_TYPE_EXIT) {
|
|
|
- cell.aci = circ->p_aci;
|
|
|
- if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, NULL) < 0) {
|
|
|
- log_fn(LOG_WARNING,"circuit_deliver_relay_cell (backward) failed. Closing.");
|
|
|
- circuit_close(circ);
|
|
|
- return 0;
|
|
|
- }
|
|
|
assert(circ->package_window > 0);
|
|
|
circ->package_window--;
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
assert(conn->type == CONN_TYPE_AP);
|
|
|
- cell.aci = circ->n_aci;
|
|
|
- if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, conn->cpath_layer) < 0) {
|
|
|
- log_fn(LOG_WARNING,"circuit_deliver_relay_cell (forward) failed. Closing.");
|
|
|
- circuit_close(circ);
|
|
|
- return 0;
|
|
|
- }
|
|
|
assert(conn->cpath_layer->package_window > 0);
|
|
|
conn->cpath_layer->package_window--;
|
|
|
}
|
|
|
|
|
|
- assert(conn->package_window > 0);
|
|
|
if(--conn->package_window <= 0) {
|
|
|
connection_stop_reading(conn);
|
|
|
log_fn(LOG_DEBUG,"conn->package_window reached 0.");
|
|
@@ -493,27 +472,23 @@ static int connection_ap_handshake_process_socks(connection_t *conn) {
|
|
|
|
|
|
static int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ,
|
|
|
char *destaddr, uint16_t destport) {
|
|
|
- cell_t cell;
|
|
|
- memset(&cell, 0, sizeof(cell_t));
|
|
|
+ char payload[CELL_PAYLOAD_SIZE];
|
|
|
+ int payload_len;
|
|
|
|
|
|
- cell.command = CELL_RELAY;
|
|
|
- cell.aci = circ->n_aci;
|
|
|
- SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_BEGIN);
|
|
|
if(crypto_pseudo_rand(STREAM_ID_SIZE, ap_conn->stream_id) < 0)
|
|
|
return -1;
|
|
|
|
|
|
- SET_CELL_STREAM_ID(cell, ZERO_STREAM);
|
|
|
-
|
|
|
- memcpy(cell.payload+RELAY_HEADER_SIZE, ap_conn->stream_id, STREAM_ID_SIZE);
|
|
|
- cell.length =
|
|
|
- snprintf(cell.payload+RELAY_HEADER_SIZE+STREAM_ID_SIZE, CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE-STREAM_ID_SIZE,
|
|
|
- "%s:%d", destaddr, destport) +
|
|
|
- 1 + STREAM_ID_SIZE + RELAY_HEADER_SIZE;
|
|
|
- log_fn(LOG_DEBUG,"Sending relay cell (id %d) to begin stream %d.", *(int *)(cell.payload+1),*(int *)ap_conn->stream_id);
|
|
|
- if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, ap_conn->cpath_layer) < 0) {
|
|
|
- log_fn(LOG_WARNING,"failed to deliver begin cell. Closing.");
|
|
|
- return -1;
|
|
|
- }
|
|
|
+
|
|
|
+ memcpy(payload, ap_conn->stream_id, STREAM_ID_SIZE);
|
|
|
+ payload_len = STREAM_ID_SIZE + 1 +
|
|
|
+ snprintf(payload+STREAM_ID_SIZE,CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE-STREAM_ID_SIZE,
|
|
|
+ "%s:%d", destaddr, destport);
|
|
|
+
|
|
|
+ log_fn(LOG_DEBUG,"Sending relay cell to begin stream %d.",*(int *)ap_conn->stream_id);
|
|
|
+
|
|
|
+ connection_edge_send_command(ap_conn, circ, RELAY_COMMAND_BEGIN,
|
|
|
+ payload, payload_len, ap_conn->cpath_layer);
|
|
|
+
|
|
|
ap_conn->package_window = STREAMWINDOW_START;
|
|
|
ap_conn->deliver_window = STREAMWINDOW_START;
|
|
|
ap_conn->state = AP_CONN_STATE_OPEN;
|
|
@@ -640,7 +615,9 @@ int connection_exit_connect(connection_t *conn) {
|
|
|
connection_watch_events(conn, POLLIN);
|
|
|
|
|
|
|
|
|
- return connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED);
|
|
|
+ connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED,
|
|
|
+ NULL, 0, conn->cpath_layer);
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
|