update copyright notices.
[tor.git] / src / or / command.c
bloba8caed528c54b2f90a068c6780570c7a0d8e7b5d
1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char command_c_id[] = "$Id$";
8 /**
9 * \file command.c
10 * \brief Functions for processing incoming cells
11 **/
13 /* In-points to command.c:
15 * - command_process_cell(), called from
16 * connection_or_process_cells_from_inbuf() in connection_or.c
19 #include "or.h"
21 /** Keep statistics about how many of each type of cell we've received. */
22 unsigned long stats_n_padding_cells_processed = 0;
23 unsigned long stats_n_create_cells_processed = 0;
24 unsigned long stats_n_created_cells_processed = 0;
25 unsigned long stats_n_relay_cells_processed = 0;
26 unsigned long stats_n_destroy_cells_processed = 0;
28 /** These are the main four functions for processing cells */
29 static void command_process_create_cell(cell_t *cell, connection_t *conn);
30 static void command_process_created_cell(cell_t *cell, connection_t *conn);
31 static void command_process_relay_cell(cell_t *cell, connection_t *conn);
32 static void command_process_destroy_cell(cell_t *cell, connection_t *conn);
34 #ifdef KEEP_TIMING_STATS
35 /** This is a wrapper function around the actual function that processes the
36 * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
37 * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
39 static void command_time_process_cell(cell_t *cell, connection_t *conn, int *time,
40 void (*func)(cell_t *, connection_t *)) {
41 struct timeval start, end;
42 long time_passed;
44 tor_gettimeofday(&start);
46 (*func)(cell, conn);
48 tor_gettimeofday(&end);
49 time_passed = tv_udiff(&start, &end) ;
51 if (time_passed > 10000) { /* more than 10ms */
52 log_fn(LOG_DEBUG,"That call just took %ld ms.",time_passed/1000);
54 if (time_passed < 0) {
55 log_fn(LOG_INFO,"That call took us back in time!");
56 time_passed = 0;
58 *time += time_passed;
60 #endif
62 /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
63 * statistics about how many of each cell we've processed so far
64 * this second, and the total number of microseconds it took to
65 * process each type of cell.
67 void command_process_cell(cell_t *cell, connection_t *conn) {
68 #ifdef KEEP_TIMING_STATS
69 /* how many of each cell have we seen so far this second? needs better
70 * name. */
71 static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
72 /* how long has it taken to process each type of cell? */
73 static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
74 static time_t current_second = 0; /* from previous calls to time */
76 time_t now = time(NULL);
78 if (now > current_second) { /* the second has rolled over */
79 /* print stats */
80 log(LOG_INFO,"At end of second: %d creates (%d ms), %d createds (%d ms), %d relays (%d ms), %d destroys (%d ms)",
81 num_create, create_time/1000,
82 num_created, created_time/1000,
83 num_relay, relay_time/1000,
84 num_destroy, destroy_time/1000);
86 /* zero out stats */
87 num_create = num_created = num_relay = num_destroy = 0;
88 create_time = created_time = relay_time = destroy_time = 0;
90 /* remember which second it is, for next time */
91 current_second = now;
93 #endif
95 switch (cell->command) {
96 case CELL_PADDING:
97 ++stats_n_padding_cells_processed;
98 /* do nothing */
99 break;
100 case CELL_CREATE:
101 ++stats_n_create_cells_processed;
102 #ifdef KEEP_TIMING_STATS
103 ++num_create;
104 command_time_process_cell(cell, conn, &create_time,
105 command_process_create_cell);
106 #else
107 command_process_create_cell(cell, conn);
108 #endif
109 break;
110 case CELL_CREATED:
111 ++stats_n_created_cells_processed;
112 #ifdef KEEP_TIMING_STATS
113 ++num_created;
114 command_time_process_cell(cell, conn, &created_time,
115 command_process_created_cell);
116 #else
117 command_process_created_cell(cell, conn);
118 #endif
119 break;
120 case CELL_RELAY:
121 ++stats_n_relay_cells_processed;
122 #ifdef KEEP_TIMING_STATS
123 ++num_relay;
124 command_time_process_cell(cell, conn, &relay_time,
125 command_process_relay_cell);
126 #else
127 command_process_relay_cell(cell, conn);
128 #endif
129 break;
130 case CELL_DESTROY:
131 ++stats_n_destroy_cells_processed;
132 #ifdef KEEP_TIMING_STATS
133 ++num_destroy;
134 command_time_process_cell(cell, conn, &destroy_time,
135 command_process_destroy_cell);
136 #else
137 command_process_destroy_cell(cell, conn);
138 #endif
139 break;
140 default:
141 log_fn(LOG_WARN,"Cell of unknown type (%d) received. Dropping.", cell->command);
142 break;
146 /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a new circuit
147 * with the p_circ_id specified in cell. Put the circuit in state
148 * onionskin_pending, and pass the onionskin to the cpuworker. Circ will
149 * get picked up again when the cpuworker finishes decrypting it.
151 static void command_process_create_cell(cell_t *cell, connection_t *conn) {
152 circuit_t *circ;
154 if (we_are_hibernating()) {
155 log_fn(LOG_INFO,"Received create cell but we're shutting down. Sending back destroy.");
156 connection_send_destroy(cell->circ_id, conn);
157 return;
160 /* If the high bit of the circuit ID is not as expected, then switch
161 * which half of the space we'll use for our own CREATE cells.
163 * This can happen because Tor 0.0.9pre5 and earlier decide which
164 * half to use based on nickname, and we now use identity keys.
166 if ((cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) {
167 log_fn(LOG_INFO, "Got a high circuit ID from %s (%d); switching to low circuit IDs.",
168 conn->nickname ? conn->nickname : "client", conn->s);
169 conn->circ_id_type = CIRC_ID_TYPE_LOWER;
170 } else if (!(cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_LOWER) {
171 log_fn(LOG_INFO, "Got a low circuit ID from %s (%d); switching to high circuit IDs.",
172 conn->nickname ? conn->nickname : "client", conn->s);
173 conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
176 circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
178 if (circ) {
179 log_fn(LOG_WARN,"received CREATE cell (circID %d) for known circ. Dropping.", cell->circ_id);
180 return;
183 circ = circuit_new(cell->circ_id, conn);
184 circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
185 circ->purpose = CIRCUIT_PURPOSE_OR;
187 memcpy(circ->onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
189 /* hand it off to the cpuworkers, and then return */
190 if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
191 log_fn(LOG_WARN,"Failed to hand off onionskin. Closing.");
192 circuit_mark_for_close(circ);
193 return;
195 log_fn(LOG_DEBUG,"success: handed off onionskin.");
198 /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>. Find the circuit
199 * that it's intended for. If we're not the origin of the circuit, package
200 * the 'created' cell in an 'extended' relay cell and pass it back. If we
201 * are the origin of the circuit, send it to circuit_finish_handshake() to
202 * finish processing keys, and then call circuit_send_next_onion_skin() to
203 * extend to the next hop in the circuit if necessary.
205 static void command_process_created_cell(cell_t *cell, connection_t *conn) {
206 circuit_t *circ;
208 circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
210 if (!circ) {
211 log_fn(LOG_INFO,"(circID %d) unknown circ (probably got a destroy earlier). Dropping.", cell->circ_id);
212 return;
215 if (circ->n_circ_id != cell->circ_id) {
216 log_fn(LOG_WARN,"got created cell from OPward? Closing.");
217 circuit_mark_for_close(circ);
218 return;
221 if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
222 log_fn(LOG_DEBUG,"at OP. Finishing handshake.");
223 if (circuit_finish_handshake(circ, cell->payload) < 0) {
224 log_fn(LOG_WARN,"circuit_finish_handshake failed.");
225 circuit_mark_for_close(circ);
226 return;
228 log_fn(LOG_DEBUG,"Moving to next skin.");
229 if (circuit_send_next_onion_skin(circ) < 0) {
230 log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
231 circuit_mark_for_close(circ); /* XXX push this circuit_close lower */
232 return;
234 } else { /* pack it into an extended relay cell, and send it. */
235 log_fn(LOG_DEBUG,"Converting created cell to extended relay cell, sending.");
236 connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
237 cell->payload, ONIONSKIN_REPLY_LEN, NULL);
241 /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
242 * it came in with a recognized circ_id. Pass it on to
243 * circuit_receive_relay_cell() for actual processing.
245 static void command_process_relay_cell(cell_t *cell, connection_t *conn) {
246 circuit_t *circ;
248 circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
250 if (!circ) {
251 log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
252 cell->circ_id, conn->address, conn->port);
253 return;
256 if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
257 log_fn(LOG_WARN,"circuit in create_wait. Closing.");
258 circuit_mark_for_close(circ);
259 return;
262 if (cell->circ_id == circ->p_circ_id) { /* it's an outgoing cell */
263 if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
264 log_fn(LOG_WARN,"circuit_receive_relay_cell (forward) failed. Closing.");
265 circuit_mark_for_close(circ);
266 return;
268 } else { /* it's an ingoing cell */
269 if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
270 log_fn(LOG_WARN,"circuit_receive_relay_cell (backward) failed. Closing.");
271 circuit_mark_for_close(circ);
272 return;
277 /** Process a 'destroy' <b>cell</b> that just arrived from
278 * <b>conn</b>. Find the circ that it refers to (if any).
280 * If the circ is in state
281 * onionskin_pending, then call onion_pending_remove() to remove it
282 * from the pending onion list (note that if it's already being
283 * processed by the cpuworker, it won't be in the list anymore; but
284 * when the cpuworker returns it, the circuit will be gone, and the
285 * cpuworker response will be dropped).
287 * Then mark the circuit for close (which marks all edges for close,
288 * and passes the destroy cell onward if necessary).
290 static void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
291 circuit_t *circ;
293 circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
295 if (!circ) {
296 log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
297 cell->circ_id, conn->address, conn->port);
298 return;
301 log_fn(LOG_INFO,"Received for circID %d.",cell->circ_id);
302 if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
303 onion_pending_remove(circ);
306 if (cell->circ_id == circ->p_circ_id) {
307 /* the destroy came from behind */
308 circ->p_conn = NULL;
309 circuit_mark_for_close(circ);
310 } else { /* the destroy came from ahead */
311 circ->n_conn = NULL;
312 #if 0
313 if (!CIRCUIT_IS_ORIGIN(circ)) {
314 log_fn(LOG_DEBUG, "Delivering 'truncated' back.");
315 connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
316 NULL, 0, NULL);
318 #endif
319 circuit_mark_for_close(circ);