Merge branch 'ticket24902_029_05' into ticket24902_033_02
[tor.git] / src / or / command.c
blob185596a65a278536e65bcecd23d0105993861cbe
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file command.c
9 * \brief Functions for processing incoming cells.
11 * When we receive a cell from a client or a relay, it arrives on some
12 * channel, and tells us what to do with it. In this module, we dispatch based
13 * on the cell type using the functions command_process_cell() and
14 * command_process_var_cell(), and deal with the cell accordingly. (These
15 * handlers are installed on a channel with the command_setup_channel()
16 * function.)
18 * Channels have a chance to handle some cell types on their own before they
19 * are ever passed here --- typically, they do this for cells that are
20 * specific to a given channel type. For example, in channeltls.c, the cells
21 * for the initial connection handshake are handled before we get here. (Of
22 * course, the fact that there _is_ only one channel type for now means that
23 * we may have gotten the factoring wrong here.)
25 * Handling other cell types is mainly farmed off to other modules, after
26 * initial sanity-checking. CREATE* cells are handled ultimately in onion.c,
27 * CREATED* cells trigger circuit creation in circuitbuild.c, DESTROY cells
28 * are handled here (since they're simple), and RELAY cells, in all their
29 * complexity, are passed off to relay.c.
30 **/
32 /* In-points to command.c:
34 * - command_process_cell(), called from
35 * incoming cell handlers of channel_t instances;
36 * callbacks registered in command_setup_channel(),
37 * called when channels are created in circuitbuild.c
39 #include "or.h"
40 #include "channel.h"
41 #include "circuitbuild.h"
42 #include "circuitlist.h"
43 #include "command.h"
44 #include "connection.h"
45 #include "connection_or.h"
46 #include "config.h"
47 #include "control.h"
48 #include "cpuworker.h"
49 #include "dos.h"
50 #include "hibernate.h"
51 #include "nodelist.h"
52 #include "onion.h"
53 #include "rephist.h"
54 #include "relay.h"
55 #include "router.h"
56 #include "routerlist.h"
58 /** How many CELL_CREATE cells have we received, ever? */
59 uint64_t stats_n_create_cells_processed = 0;
60 /** How many CELL_CREATED cells have we received, ever? */
61 uint64_t stats_n_created_cells_processed = 0;
62 /** How many CELL_RELAY cells have we received, ever? */
63 uint64_t stats_n_relay_cells_processed = 0;
64 /** How many CELL_DESTROY cells have we received, ever? */
65 uint64_t stats_n_destroy_cells_processed = 0;
67 /* Handle an incoming channel */
68 static void command_handle_incoming_channel(channel_listener_t *listener,
69 channel_t *chan);
71 /* These are the main functions for processing cells */
72 static void command_process_create_cell(cell_t *cell, channel_t *chan);
73 static void command_process_created_cell(cell_t *cell, channel_t *chan);
74 static void command_process_relay_cell(cell_t *cell, channel_t *chan);
75 static void command_process_destroy_cell(cell_t *cell, channel_t *chan);
77 /** Convert the cell <b>command</b> into a lower-case, human-readable
78 * string. */
79 const char *
80 cell_command_to_string(uint8_t command)
82 switch (command) {
83 case CELL_PADDING: return "padding";
84 case CELL_CREATE: return "create";
85 case CELL_CREATED: return "created";
86 case CELL_RELAY: return "relay";
87 case CELL_DESTROY: return "destroy";
88 case CELL_CREATE_FAST: return "create_fast";
89 case CELL_CREATED_FAST: return "created_fast";
90 case CELL_VERSIONS: return "versions";
91 case CELL_NETINFO: return "netinfo";
92 case CELL_RELAY_EARLY: return "relay_early";
93 case CELL_CREATE2: return "create2";
94 case CELL_CREATED2: return "created2";
95 case CELL_VPADDING: return "vpadding";
96 case CELL_CERTS: return "certs";
97 case CELL_AUTH_CHALLENGE: return "auth_challenge";
98 case CELL_AUTHENTICATE: return "authenticate";
99 case CELL_AUTHORIZE: return "authorize";
100 default: return "unrecognized";
104 #ifdef KEEP_TIMING_STATS
105 /** This is a wrapper function around the actual function that processes the
106 * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
107 * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
109 static void
110 command_time_process_cell(cell_t *cell, channel_t *chan, int *time,
111 void (*func)(cell_t *, channel_t *))
113 struct timeval start, end;
114 long time_passed;
116 tor_gettimeofday(&start);
118 (*func)(cell, chan);
120 tor_gettimeofday(&end);
121 time_passed = tv_udiff(&start, &end) ;
123 if (time_passed > 10000) { /* more than 10ms */
124 log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
126 if (time_passed < 0) {
127 log_info(LD_GENERAL,"That call took us back in time!");
128 time_passed = 0;
130 *time += time_passed;
132 #endif /* defined(KEEP_TIMING_STATS) */
134 /** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
135 * statistics about how many of each cell we've processed so far
136 * this second, and the total number of microseconds it took to
137 * process each type of cell.
139 void
140 command_process_cell(channel_t *chan, cell_t *cell)
142 #ifdef KEEP_TIMING_STATS
143 /* how many of each cell have we seen so far this second? needs better
144 * name. */
145 static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
146 /* how long has it taken to process each type of cell? */
147 static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
148 static time_t current_second = 0; /* from previous calls to time */
150 time_t now = time(NULL);
152 if (now > current_second) { /* the second has rolled over */
153 /* print stats */
154 log_info(LD_OR,
155 "At end of second: %d creates (%d ms), %d createds (%d ms), "
156 "%d relays (%d ms), %d destroys (%d ms)",
157 num_create, create_time/1000,
158 num_created, created_time/1000,
159 num_relay, relay_time/1000,
160 num_destroy, destroy_time/1000);
162 /* zero out stats */
163 num_create = num_created = num_relay = num_destroy = 0;
164 create_time = created_time = relay_time = destroy_time = 0;
166 /* remember which second it is, for next time */
167 current_second = now;
169 #endif /* defined(KEEP_TIMING_STATS) */
171 #ifdef KEEP_TIMING_STATS
172 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
173 ++num ## tp; \
174 command_time_process_cell(cl, cn, & tp ## time , \
175 command_process_ ## tp ## _cell); \
176 } STMT_END
177 #else /* !(defined(KEEP_TIMING_STATS)) */
178 #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
179 #endif /* defined(KEEP_TIMING_STATS) */
181 switch (cell->command) {
182 case CELL_CREATE:
183 case CELL_CREATE_FAST:
184 case CELL_CREATE2:
185 ++stats_n_create_cells_processed;
186 PROCESS_CELL(create, cell, chan);
187 break;
188 case CELL_CREATED:
189 case CELL_CREATED_FAST:
190 case CELL_CREATED2:
191 ++stats_n_created_cells_processed;
192 PROCESS_CELL(created, cell, chan);
193 break;
194 case CELL_RELAY:
195 case CELL_RELAY_EARLY:
196 ++stats_n_relay_cells_processed;
197 PROCESS_CELL(relay, cell, chan);
198 break;
199 case CELL_DESTROY:
200 ++stats_n_destroy_cells_processed;
201 PROCESS_CELL(destroy, cell, chan);
202 break;
203 default:
204 log_fn(LOG_INFO, LD_PROTOCOL,
205 "Cell of unknown or unexpected type (%d) received. "
206 "Dropping.",
207 cell->command);
208 break;
212 /** Process an incoming var_cell from a channel; in the current protocol all
213 * the var_cells are handshake-related and handled below the channel layer,
214 * so this just logs a warning and drops the cell.
217 void
218 command_process_var_cell(channel_t *chan, var_cell_t *var_cell)
220 tor_assert(chan);
221 tor_assert(var_cell);
223 log_info(LD_PROTOCOL,
224 "Received unexpected var_cell above the channel layer of type %d"
225 "; dropping it.",
226 var_cell->command);
229 /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
230 * new circuit with the p_circ_id specified in cell. Put the circuit in state
231 * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
232 * picked up again when the cpuworker finishes decrypting it.
234 static void
235 command_process_create_cell(cell_t *cell, channel_t *chan)
237 or_circuit_t *circ;
238 const or_options_t *options = get_options();
239 int id_is_high;
240 create_cell_t *create_cell;
242 tor_assert(cell);
243 tor_assert(chan);
245 log_debug(LD_OR,
246 "Got a CREATE cell for circ_id %u on channel " U64_FORMAT
247 " (%p)",
248 (unsigned)cell->circ_id,
249 U64_PRINTF_ARG(chan->global_identifier), chan);
251 /* First thing we do, even though the cell might be invalid, is inform the
252 * DoS mitigation subsystem layer of this event. Validation is done by this
253 * function. */
254 dos_cc_new_create_cell(chan);
256 /* We check for the conditions that would make us drop the cell before
257 * we check for the conditions that would make us send a DESTROY back,
258 * since those conditions would make a DESTROY nonsensical. */
259 if (cell->circ_id == 0) {
260 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
261 "Received a create cell (type %d) from %s with zero circID; "
262 " ignoring.", (int)cell->command,
263 channel_get_actual_remote_descr(chan));
264 return;
267 if (circuit_id_in_use_on_channel(cell->circ_id, chan)) {
268 const node_t *node = node_get_by_id(chan->identity_digest);
269 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
270 "Received CREATE cell (circID %u) for known circ. "
271 "Dropping (age %d).",
272 (unsigned)cell->circ_id,
273 (int)(time(NULL) - channel_when_created(chan)));
274 if (node) {
275 char *p = esc_for_log(node_get_platform(node));
276 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
277 "Details: router %s, platform %s.",
278 node_describe(node), p);
279 tor_free(p);
281 return;
284 if (we_are_hibernating()) {
285 log_info(LD_OR,
286 "Received create cell but we're shutting down. Sending back "
287 "destroy.");
288 channel_send_destroy(cell->circ_id, chan,
289 END_CIRC_REASON_HIBERNATING);
290 return;
293 /* Check if we should apply a defense for this channel. */
294 if (dos_cc_get_defense_type(chan) == DOS_CC_DEFENSE_REFUSE_CELL) {
295 channel_send_destroy(cell->circ_id, chan,
296 END_CIRC_REASON_RESOURCELIMIT);
297 return;
300 if (!server_mode(options) ||
301 (!public_server_mode(options) && channel_is_outgoing(chan))) {
302 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
303 "Received create cell (type %d) from %s, but we're connected "
304 "to it as a client. "
305 "Sending back a destroy.",
306 (int)cell->command, channel_get_canonical_remote_descr(chan));
307 channel_send_destroy(cell->circ_id, chan,
308 END_CIRC_REASON_TORPROTOCOL);
309 return;
312 /* If the high bit of the circuit ID is not as expected, close the
313 * circ. */
314 if (chan->wide_circ_ids)
315 id_is_high = cell->circ_id & (1u<<31);
316 else
317 id_is_high = cell->circ_id & (1u<<15);
318 if ((id_is_high &&
319 chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
320 (!id_is_high &&
321 chan->circ_id_type == CIRC_ID_TYPE_LOWER)) {
322 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
323 "Received create cell with unexpected circ_id %u. Closing.",
324 (unsigned)cell->circ_id);
325 channel_send_destroy(cell->circ_id, chan,
326 END_CIRC_REASON_TORPROTOCOL);
327 return;
330 circ = or_circuit_new(cell->circ_id, chan);
331 circ->base_.purpose = CIRCUIT_PURPOSE_OR;
332 circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
333 create_cell = tor_malloc_zero(sizeof(create_cell_t));
334 if (create_cell_parse(create_cell, cell) < 0) {
335 tor_free(create_cell);
336 log_fn(LOG_PROTOCOL_WARN, LD_OR,
337 "Bogus/unrecognized create cell; closing.");
338 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
339 return;
342 if (connection_or_digest_is_known_relay(chan->identity_digest)) {
343 rep_hist_note_circuit_handshake_requested(create_cell->handshake_type);
346 if (create_cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST) {
347 /* hand it off to the cpuworkers, and then return. */
349 if (assign_onionskin_to_cpuworker(circ, create_cell) < 0) {
350 log_debug(LD_GENERAL,"Failed to hand off onionskin. Closing.");
351 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
352 return;
354 log_debug(LD_OR,"success: handed off onionskin.");
355 } else {
356 /* This is a CREATE_FAST cell; we can handle it immediately without using
357 * a CPU worker. */
358 uint8_t keys[CPATH_KEY_MATERIAL_LEN];
359 uint8_t rend_circ_nonce[DIGEST_LEN];
360 int len;
361 created_cell_t created_cell;
363 memset(&created_cell, 0, sizeof(created_cell));
364 len = onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST,
365 create_cell->onionskin,
366 create_cell->handshake_len,
367 NULL,
368 created_cell.reply,
369 keys, CPATH_KEY_MATERIAL_LEN,
370 rend_circ_nonce);
371 tor_free(create_cell);
372 if (len < 0) {
373 log_warn(LD_OR,"Failed to generate key material. Closing.");
374 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
375 return;
377 created_cell.cell_type = CELL_CREATED_FAST;
378 created_cell.handshake_len = len;
380 if (onionskin_answer(circ, &created_cell,
381 (const char *)keys, sizeof(keys),
382 rend_circ_nonce)<0) {
383 log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
384 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
385 return;
387 memwipe(keys, 0, sizeof(keys));
391 /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
392 * Find the circuit
393 * that it's intended for. If we're not the origin of the circuit, package
394 * the 'created' cell in an 'extended' relay cell and pass it back. If we
395 * are the origin of the circuit, send it to circuit_finish_handshake() to
396 * finish processing keys, and then call circuit_send_next_onion_skin() to
397 * extend to the next hop in the circuit if necessary.
399 static void
400 command_process_created_cell(cell_t *cell, channel_t *chan)
402 circuit_t *circ;
403 extended_cell_t extended_cell;
405 circ = circuit_get_by_circid_channel(cell->circ_id, chan);
407 if (!circ) {
408 log_info(LD_OR,
409 "(circID %u) unknown circ (probably got a destroy earlier). "
410 "Dropping.", (unsigned)cell->circ_id);
411 return;
414 if (circ->n_circ_id != cell->circ_id || circ->n_chan != chan) {
415 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
416 "got created cell from Tor client? Closing.");
417 circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
418 return;
421 if (created_cell_parse(&extended_cell.created_cell, cell) < 0) {
422 log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unparseable created cell.");
423 circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
424 return;
427 if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
428 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
429 int err_reason = 0;
430 log_debug(LD_OR,"at OP. Finishing handshake.");
431 if ((err_reason = circuit_finish_handshake(origin_circ,
432 &extended_cell.created_cell)) < 0) {
433 circuit_mark_for_close(circ, -err_reason);
434 return;
436 log_debug(LD_OR,"Moving to next skin.");
437 if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
438 log_info(LD_OR,"circuit_send_next_onion_skin failed.");
439 /* XXX push this circuit_close lower */
440 circuit_mark_for_close(circ, -err_reason);
441 return;
443 } else { /* pack it into an extended relay cell, and send it. */
444 uint8_t command=0;
445 uint16_t len=0;
446 uint8_t payload[RELAY_PAYLOAD_SIZE];
447 log_debug(LD_OR,
448 "Converting created cell to extended relay cell, sending.");
449 memset(payload, 0, sizeof(payload));
450 if (extended_cell.created_cell.cell_type == CELL_CREATED2)
451 extended_cell.cell_type = RELAY_COMMAND_EXTENDED2;
452 else
453 extended_cell.cell_type = RELAY_COMMAND_EXTENDED;
454 if (extended_cell_format(&command, &len, payload, &extended_cell) < 0) {
455 log_fn(LOG_PROTOCOL_WARN, LD_OR, "Can't format extended cell.");
456 circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
457 return;
460 relay_send_command_from_edge(0, circ, command,
461 (const char*)payload, len, NULL);
465 /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
466 * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
467 * circuit_receive_relay_cell() for actual processing.
469 static void
470 command_process_relay_cell(cell_t *cell, channel_t *chan)
472 const or_options_t *options = get_options();
473 circuit_t *circ;
474 int reason, direction;
476 circ = circuit_get_by_circid_channel(cell->circ_id, chan);
478 if (!circ) {
479 log_debug(LD_OR,
480 "unknown circuit %u on connection from %s. Dropping.",
481 (unsigned)cell->circ_id,
482 channel_get_canonical_remote_descr(chan));
483 return;
486 if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
487 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
488 circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
489 return;
492 if (CIRCUIT_IS_ORIGIN(circ)) {
493 /* if we're a relay and treating connections with recent local
494 * traffic better, then this is one of them. */
495 channel_timestamp_client(chan);
498 if (!CIRCUIT_IS_ORIGIN(circ) &&
499 chan == TO_OR_CIRCUIT(circ)->p_chan &&
500 cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
501 direction = CELL_DIRECTION_OUT;
502 else
503 direction = CELL_DIRECTION_IN;
505 /* If we have a relay_early cell, make sure that it's outbound, and we've
506 * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
507 if (cell->command == CELL_RELAY_EARLY) {
508 if (direction == CELL_DIRECTION_IN) {
509 /* Inbound early cells could once be encountered as a result of
510 * bug 1038; but relays running versions before 0.2.1.19 are long
511 * gone from the network, so any such cells now are surprising. */
512 log_warn(LD_OR,
513 "Received an inbound RELAY_EARLY cell on circuit %u."
514 " Closing circuit. Please report this event,"
515 " along with the following message.",
516 (unsigned)cell->circ_id);
517 if (CIRCUIT_IS_ORIGIN(circ)) {
518 circuit_log_path(LOG_WARN, LD_OR, TO_ORIGIN_CIRCUIT(circ));
519 } else if (circ->n_chan) {
520 log_warn(LD_OR, " upstream=%s",
521 channel_get_actual_remote_descr(circ->n_chan));
523 circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
524 return;
525 } else {
526 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
527 if (or_circ->remaining_relay_early_cells == 0) {
528 log_fn(LOG_PROTOCOL_WARN, LD_OR,
529 "Received too many RELAY_EARLY cells on circ %u from %s."
530 " Closing circuit.",
531 (unsigned)cell->circ_id,
532 safe_str(channel_get_canonical_remote_descr(chan)));
533 circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
534 return;
536 --or_circ->remaining_relay_early_cells;
540 if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
541 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
542 "(%s) failed. Closing.",
543 direction==CELL_DIRECTION_OUT?"forward":"backward");
544 circuit_mark_for_close(circ, -reason);
547 /* If this is a cell in an RP circuit, count it as part of the
548 hidden service stats */
549 if (options->HiddenServiceStatistics &&
550 !CIRCUIT_IS_ORIGIN(circ) &&
551 TO_OR_CIRCUIT(circ)->circuit_carries_hs_traffic_stats) {
552 rep_hist_seen_new_rp_cell();
556 /** Process a 'destroy' <b>cell</b> that just arrived from
557 * <b>chan</b>. Find the circ that it refers to (if any).
559 * If the circ is in state
560 * onionskin_pending, then call onion_pending_remove() to remove it
561 * from the pending onion list (note that if it's already being
562 * processed by the cpuworker, it won't be in the list anymore; but
563 * when the cpuworker returns it, the circuit will be gone, and the
564 * cpuworker response will be dropped).
566 * Then mark the circuit for close (which marks all edges for close,
567 * and passes the destroy cell onward if necessary).
569 static void
570 command_process_destroy_cell(cell_t *cell, channel_t *chan)
572 circuit_t *circ;
573 int reason;
575 circ = circuit_get_by_circid_channel(cell->circ_id, chan);
576 if (!circ) {
577 log_info(LD_OR,"unknown circuit %u on connection from %s. Dropping.",
578 (unsigned)cell->circ_id,
579 channel_get_canonical_remote_descr(chan));
580 return;
582 log_debug(LD_OR,"Received for circID %u.",(unsigned)cell->circ_id);
584 reason = (uint8_t)cell->payload[0];
585 circ->received_destroy = 1;
587 if (!CIRCUIT_IS_ORIGIN(circ) &&
588 chan == TO_OR_CIRCUIT(circ)->p_chan &&
589 cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
590 /* the destroy came from behind */
591 circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ), 0, NULL);
592 circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
593 } else { /* the destroy came from ahead */
594 circuit_set_n_circid_chan(circ, 0, NULL);
595 if (CIRCUIT_IS_ORIGIN(circ)) {
596 circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
597 } else {
598 char payload[1];
599 log_debug(LD_OR, "Delivering 'truncated' back.");
600 payload[0] = (char)reason;
601 relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
602 payload, sizeof(payload), NULL);
607 /** Callback to handle a new channel; call command_setup_channel() to give
608 * it the right cell handlers.
611 static void
612 command_handle_incoming_channel(channel_listener_t *listener, channel_t *chan)
614 tor_assert(listener);
615 tor_assert(chan);
617 command_setup_channel(chan);
620 /** Given a channel, install the right handlers to process incoming
621 * cells on it.
624 void
625 command_setup_channel(channel_t *chan)
627 tor_assert(chan);
629 channel_set_cell_handlers(chan,
630 command_process_cell,
631 command_process_var_cell);
634 /** Given a listener, install the right handler to process incoming
635 * channels on it.
638 void
639 command_setup_listener(channel_listener_t *listener)
641 tor_assert(listener);
642 tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
644 channel_listener_set_listener_fn(listener, command_handle_incoming_channel);