command.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file command.c
  8. * \brief Functions for processing incoming cells.
  9. **/
  10. /* In-points to command.c:
  11. *
  12. * - command_process_cell(), called from
  13. * incoming cell handlers of channel_t instances;
  14. * callbacks registered in command_setup_channel(),
  15. * called when channels are created in circuitbuild.c
  16. */
  17. #include "or.h"
  18. #include "channel.h"
  19. #include "circuitbuild.h"
  20. #include "circuitlist.h"
  21. #include "command.h"
  22. #include "connection.h"
  23. #include "connection_or.h"
  24. #include "config.h"
  25. #include "control.h"
  26. #include "cpuworker.h"
  27. #include "hibernate.h"
  28. #include "nodelist.h"
  29. #include "onion.h"
  30. #include "relay.h"
  31. #include "router.h"
  32. #include "routerlist.h"
  33. /** How many CELL_CREATE cells have we received, ever? */
  34. uint64_t stats_n_create_cells_processed = 0;
  35. /** How many CELL_CREATED cells have we received, ever? */
  36. uint64_t stats_n_created_cells_processed = 0;
  37. /** How many CELL_RELAY cells have we received, ever? */
  38. uint64_t stats_n_relay_cells_processed = 0;
  39. /** How many CELL_DESTROY cells have we received, ever? */
  40. uint64_t stats_n_destroy_cells_processed = 0;
  41. /* Handle an incoming channel */
  42. static void command_handle_incoming_channel(channel_listener_t *listener,
  43. channel_t *chan);
  44. /* These are the main functions for processing cells */
  45. static void command_process_create_cell(cell_t *cell, channel_t *chan);
  46. static void command_process_created_cell(cell_t *cell, channel_t *chan);
  47. static void command_process_relay_cell(cell_t *cell, channel_t *chan);
  48. static void command_process_destroy_cell(cell_t *cell, channel_t *chan);
  49. /** Convert the cell <b>command</b> into a lower-case, human-readable
  50. * string. */
  51. const char *
  52. cell_command_to_string(uint8_t command)
  53. {
  54. switch (command) {
  55. case CELL_PADDING: return "padding";
  56. case CELL_CREATE: return "create";
  57. case CELL_CREATED: return "created";
  58. case CELL_RELAY: return "relay";
  59. case CELL_DESTROY: return "destroy";
  60. case CELL_CREATE_FAST: return "create_fast";
  61. case CELL_CREATED_FAST: return "created_fast";
  62. case CELL_VERSIONS: return "versions";
  63. case CELL_NETINFO: return "netinfo";
  64. case CELL_RELAY_EARLY: return "relay_early";
  65. case CELL_CREATE2: return "create2";
  66. case CELL_CREATED2: return "created2";
  67. case CELL_VPADDING: return "vpadding";
  68. case CELL_CERTS: return "certs";
  69. case CELL_AUTH_CHALLENGE: return "auth_challenge";
  70. case CELL_AUTHENTICATE: return "authenticate";
  71. case CELL_AUTHORIZE: return "authorize";
  72. default: return "unrecognized";
  73. }
  74. }
  75. #ifdef KEEP_TIMING_STATS
  76. /** This is a wrapper function around the actual function that processes the
  77. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  78. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  79. */
  80. static void
  81. command_time_process_cell(cell_t *cell, channel_t *chan, int *time,
  82. void (*func)(cell_t *, channel_t *))
  83. {
  84. struct timeval start, end;
  85. long time_passed;
  86. tor_gettimeofday(&start);
  87. (*func)(cell, chan);
  88. tor_gettimeofday(&end);
  89. time_passed = tv_udiff(&start, &end) ;
  90. if (time_passed > 10000) { /* more than 10ms */
  91. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  92. }
  93. if (time_passed < 0) {
  94. log_info(LD_GENERAL,"That call took us back in time!");
  95. time_passed = 0;
  96. }
  97. *time += time_passed;
  98. }
  99. #endif
  100. /** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
  101. * statistics about how many of each cell we've processed so far
  102. * this second, and the total number of microseconds it took to
  103. * process each type of cell.
  104. */
  105. void
  106. command_process_cell(channel_t *chan, cell_t *cell)
  107. {
  108. #ifdef KEEP_TIMING_STATS
  109. /* how many of each cell have we seen so far this second? needs better
  110. * name. */
  111. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  112. /* how long has it taken to process each type of cell? */
  113. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  114. static time_t current_second = 0; /* from previous calls to time */
  115. time_t now = time(NULL);
  116. if (now > current_second) { /* the second has rolled over */
  117. /* print stats */
  118. log_info(LD_OR,
  119. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  120. "%d relays (%d ms), %d destroys (%d ms)",
  121. num_create, create_time/1000,
  122. num_created, created_time/1000,
  123. num_relay, relay_time/1000,
  124. num_destroy, destroy_time/1000);
  125. /* zero out stats */
  126. num_create = num_created = num_relay = num_destroy = 0;
  127. create_time = created_time = relay_time = destroy_time = 0;
  128. /* remember which second it is, for next time */
  129. current_second = now;
  130. }
  131. #endif
  132. #ifdef KEEP_TIMING_STATS
  133. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  134. ++num ## tp; \
  135. command_time_process_cell(cl, cn, & tp ## time , \
  136. command_process_ ## tp ## _cell); \
  137. } STMT_END
  138. #else
  139. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  140. #endif
  141. switch (cell->command) {
  142. case CELL_CREATE:
  143. case CELL_CREATE_FAST:
  144. case CELL_CREATE2:
  145. ++stats_n_create_cells_processed;
  146. PROCESS_CELL(create, cell, chan);
  147. break;
  148. case CELL_CREATED:
  149. case CELL_CREATED_FAST:
  150. case CELL_CREATED2:
  151. ++stats_n_created_cells_processed;
  152. PROCESS_CELL(created, cell, chan);
  153. break;
  154. case CELL_RELAY:
  155. case CELL_RELAY_EARLY:
  156. ++stats_n_relay_cells_processed;
  157. PROCESS_CELL(relay, cell, chan);
  158. break;
  159. case CELL_DESTROY:
  160. ++stats_n_destroy_cells_processed;
  161. PROCESS_CELL(destroy, cell, chan);
  162. break;
  163. default:
  164. log_fn(LOG_INFO, LD_PROTOCOL,
  165. "Cell of unknown or unexpected type (%d) received. "
  166. "Dropping.",
  167. cell->command);
  168. break;
  169. }
  170. }
  171. /** Process an incoming var_cell from a channel; in the current protocol all
  172. * the var_cells are handshake-related and handled below the channel layer,
  173. * so this just logs a warning and drops the cell.
  174. */
  175. void
  176. command_process_var_cell(channel_t *chan, var_cell_t *var_cell)
  177. {
  178. tor_assert(chan);
  179. tor_assert(var_cell);
  180. log_info(LD_PROTOCOL,
  181. "Received unexpected var_cell above the channel layer of type %d"
  182. "; dropping it.",
  183. var_cell->command);
  184. }
  185. /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
  186. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  187. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  188. * picked up again when the cpuworker finishes decrypting it.
  189. */
  190. static void
  191. command_process_create_cell(cell_t *cell, channel_t *chan)
  192. {
  193. or_circuit_t *circ;
  194. const or_options_t *options = get_options();
  195. int id_is_high;
  196. create_cell_t *create_cell;
  197. tor_assert(cell);
  198. tor_assert(chan);
  199. log_debug(LD_OR,
  200. "Got a CREATE cell for circ_id %u on channel " U64_FORMAT
  201. " (%p)",
  202. (unsigned)cell->circ_id,
  203. U64_PRINTF_ARG(chan->global_identifier), chan);
  204. if (we_are_hibernating()) {
  205. log_info(LD_OR,
  206. "Received create cell but we're shutting down. Sending back "
  207. "destroy.");
  208. channel_send_destroy(cell->circ_id, chan,
  209. END_CIRC_REASON_HIBERNATING);
  210. return;
  211. }
  212. if (!server_mode(options) ||
  213. (!public_server_mode(options) && channel_is_outgoing(chan))) {
  214. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  215. "Received create cell (type %d) from %s, but we're connected "
  216. "to it as a client. "
  217. "Sending back a destroy.",
  218. (int)cell->command, channel_get_canonical_remote_descr(chan));
  219. channel_send_destroy(cell->circ_id, chan,
  220. END_CIRC_REASON_TORPROTOCOL);
  221. return;
  222. }
  223. if (cell->circ_id == 0) {
  224. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  225. "Received a create cell (type %d) from %s with zero circID; "
  226. " ignoring.", (int)cell->command,
  227. channel_get_actual_remote_descr(chan));
  228. return;
  229. }
  230. /* If the high bit of the circuit ID is not as expected, close the
  231. * circ. */
  232. if (chan->wide_circ_ids)
  233. id_is_high = cell->circ_id & (1u<<31);
  234. else
  235. id_is_high = cell->circ_id & (1u<<15);
  236. if ((id_is_high &&
  237. chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  238. (!id_is_high &&
  239. chan->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  240. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  241. "Received create cell with unexpected circ_id %u. Closing.",
  242. (unsigned)cell->circ_id);
  243. channel_send_destroy(cell->circ_id, chan,
  244. END_CIRC_REASON_TORPROTOCOL);
  245. return;
  246. }
  247. if (circuit_id_in_use_on_channel(cell->circ_id, chan)) {
  248. const node_t *node = node_get_by_id(chan->identity_digest);
  249. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  250. "Received CREATE cell (circID %u) for known circ. "
  251. "Dropping (age %d).",
  252. (unsigned)cell->circ_id,
  253. (int)(time(NULL) - channel_when_created(chan)));
  254. if (node) {
  255. char *p = esc_for_log(node_get_platform(node));
  256. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  257. "Details: router %s, platform %s.",
  258. node_describe(node), p);
  259. tor_free(p);
  260. }
  261. return;
  262. }
  263. circ = or_circuit_new(cell->circ_id, chan);
  264. circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  265. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  266. create_cell = tor_malloc_zero(sizeof(create_cell_t));
  267. if (create_cell_parse(create_cell, cell) < 0) {
  268. tor_free(create_cell);
  269. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  270. "Bogus/unrecognized create cell; closing.");
  271. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  272. return;
  273. }
  274. if (create_cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST) {
  275. /* hand it off to the cpuworkers, and then return. */
  276. if (assign_onionskin_to_cpuworker(NULL, circ, create_cell) < 0) {
  277. log_debug(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  278. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  279. return;
  280. }
  281. log_debug(LD_OR,"success: handed off onionskin.");
  282. } else {
  283. /* This is a CREATE_FAST cell; we can handle it immediately without using
  284. * a CPU worker. */
  285. uint8_t keys[CPATH_KEY_MATERIAL_LEN];
  286. uint8_t rend_circ_nonce[DIGEST_LEN];
  287. int len;
  288. created_cell_t created_cell;
  289. /* Make sure we never try to use the OR connection on which we
  290. * received this cell to satisfy an EXTEND request, */
  291. channel_mark_client(chan);
  292. memset(&created_cell, 0, sizeof(created_cell));
  293. len = onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST,
  294. create_cell->onionskin,
  295. create_cell->handshake_len,
  296. NULL,
  297. created_cell.reply,
  298. keys, CPATH_KEY_MATERIAL_LEN,
  299. rend_circ_nonce);
  300. tor_free(create_cell);
  301. if (len < 0) {
  302. log_warn(LD_OR,"Failed to generate key material. Closing.");
  303. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  304. tor_free(create_cell);
  305. return;
  306. }
  307. created_cell.cell_type = CELL_CREATED_FAST;
  308. created_cell.handshake_len = len;
  309. if (onionskin_answer(circ, &created_cell,
  310. (const char *)keys, rend_circ_nonce)<0) {
  311. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  312. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  313. return;
  314. }
  315. memwipe(keys, 0, sizeof(keys));
  316. }
  317. }
  318. /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
  319. * Find the circuit
  320. * that it's intended for. If we're not the origin of the circuit, package
  321. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  322. * are the origin of the circuit, send it to circuit_finish_handshake() to
  323. * finish processing keys, and then call circuit_send_next_onion_skin() to
  324. * extend to the next hop in the circuit if necessary.
  325. */
  326. static void
  327. command_process_created_cell(cell_t *cell, channel_t *chan)
  328. {
  329. circuit_t *circ;
  330. extended_cell_t extended_cell;
  331. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  332. if (!circ) {
  333. log_info(LD_OR,
  334. "(circID %u) unknown circ (probably got a destroy earlier). "
  335. "Dropping.", (unsigned)cell->circ_id);
  336. return;
  337. }
  338. if (circ->n_circ_id != cell->circ_id) {
  339. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  340. "got created cell from Tor client? Closing.");
  341. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  342. return;
  343. }
  344. if (created_cell_parse(&extended_cell.created_cell, cell) < 0) {
  345. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unparseable created cell.");
  346. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  347. return;
  348. }
  349. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  350. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  351. int err_reason = 0;
  352. log_debug(LD_OR,"at OP. Finishing handshake.");
  353. if ((err_reason = circuit_finish_handshake(origin_circ,
  354. &extended_cell.created_cell)) < 0) {
  355. log_warn(LD_OR,"circuit_finish_handshake failed.");
  356. circuit_mark_for_close(circ, -err_reason);
  357. return;
  358. }
  359. log_debug(LD_OR,"Moving to next skin.");
  360. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  361. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  362. /* XXX push this circuit_close lower */
  363. circuit_mark_for_close(circ, -err_reason);
  364. return;
  365. }
  366. } else { /* pack it into an extended relay cell, and send it. */
  367. uint8_t command=0;
  368. uint16_t len=0;
  369. uint8_t payload[RELAY_PAYLOAD_SIZE];
  370. log_debug(LD_OR,
  371. "Converting created cell to extended relay cell, sending.");
  372. memset(payload, 0, sizeof(payload));
  373. if (extended_cell.created_cell.cell_type == CELL_CREATED2)
  374. extended_cell.cell_type = RELAY_COMMAND_EXTENDED2;
  375. else
  376. extended_cell.cell_type = RELAY_COMMAND_EXTENDED;
  377. if (extended_cell_format(&command, &len, payload, &extended_cell) < 0) {
  378. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Can't format extended cell.");
  379. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  380. return;
  381. }
  382. relay_send_command_from_edge(0, circ, command,
  383. (const char*)payload, len, NULL);
  384. }
  385. }
  386. /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
  387. * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
  388. * circuit_receive_relay_cell() for actual processing.
  389. */
  390. static void
  391. command_process_relay_cell(cell_t *cell, channel_t *chan)
  392. {
  393. circuit_t *circ;
  394. int reason, direction;
  395. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  396. if (!circ) {
  397. log_debug(LD_OR,
  398. "unknown circuit %u on connection from %s. Dropping.",
  399. (unsigned)cell->circ_id,
  400. channel_get_canonical_remote_descr(chan));
  401. return;
  402. }
  403. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  404. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  405. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  406. return;
  407. }
  408. if (CIRCUIT_IS_ORIGIN(circ)) {
  409. /* if we're a relay and treating connections with recent local
  410. * traffic better, then this is one of them. */
  411. channel_timestamp_client(chan);
  412. }
  413. if (!CIRCUIT_IS_ORIGIN(circ) &&
  414. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  415. direction = CELL_DIRECTION_OUT;
  416. else
  417. direction = CELL_DIRECTION_IN;
  418. /* If we have a relay_early cell, make sure that it's outbound, and we've
  419. * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
  420. if (cell->command == CELL_RELAY_EARLY) {
  421. if (direction == CELL_DIRECTION_IN) {
  422. /* Allow an unlimited number of inbound relay_early cells,
  423. * for hidden service compatibility. There isn't any way to make
  424. * a long circuit through inbound relay_early cells anyway. See
  425. * bug 1038. -RD */
  426. } else {
  427. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  428. if (or_circ->remaining_relay_early_cells == 0) {
  429. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  430. "Received too many RELAY_EARLY cells on circ %u from %s."
  431. " Closing circuit.",
  432. (unsigned)cell->circ_id,
  433. safe_str(channel_get_canonical_remote_descr(chan)));
  434. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  435. return;
  436. }
  437. --or_circ->remaining_relay_early_cells;
  438. }
  439. }
  440. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  441. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  442. "(%s) failed. Closing.",
  443. direction==CELL_DIRECTION_OUT?"forward":"backward");
  444. circuit_mark_for_close(circ, -reason);
  445. }
  446. }
  447. /** Process a 'destroy' <b>cell</b> that just arrived from
  448. * <b>chan</b>. Find the circ that it refers to (if any).
  449. *
  450. * If the circ is in state
  451. * onionskin_pending, then call onion_pending_remove() to remove it
  452. * from the pending onion list (note that if it's already being
  453. * processed by the cpuworker, it won't be in the list anymore; but
  454. * when the cpuworker returns it, the circuit will be gone, and the
  455. * cpuworker response will be dropped).
  456. *
  457. * Then mark the circuit for close (which marks all edges for close,
  458. * and passes the destroy cell onward if necessary).
  459. */
  460. static void
  461. command_process_destroy_cell(cell_t *cell, channel_t *chan)
  462. {
  463. circuit_t *circ;
  464. int reason;
  465. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  466. if (!circ) {
  467. log_info(LD_OR,"unknown circuit %u on connection from %s. Dropping.",
  468. (unsigned)cell->circ_id,
  469. channel_get_canonical_remote_descr(chan));
  470. return;
  471. }
  472. log_debug(LD_OR,"Received for circID %u.",(unsigned)cell->circ_id);
  473. reason = (uint8_t)cell->payload[0];
  474. if (!CIRCUIT_IS_ORIGIN(circ) &&
  475. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  476. /* the destroy came from behind */
  477. circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ), 0, NULL);
  478. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  479. } else { /* the destroy came from ahead */
  480. circuit_set_n_circid_chan(circ, 0, NULL);
  481. if (CIRCUIT_IS_ORIGIN(circ)) {
  482. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  483. } else {
  484. char payload[1];
  485. log_debug(LD_OR, "Delivering 'truncated' back.");
  486. payload[0] = (char)reason;
  487. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  488. payload, sizeof(payload), NULL);
  489. }
  490. }
  491. }
  492. /** Callback to handle a new channel; call command_setup_channel() to give
  493. * it the right cell handlers.
  494. */
  495. static void
  496. command_handle_incoming_channel(channel_listener_t *listener, channel_t *chan)
  497. {
  498. tor_assert(listener);
  499. tor_assert(chan);
  500. command_setup_channel(chan);
  501. }
  502. /** Given a channel, install the right handlers to process incoming
  503. * cells on it.
  504. */
  505. void
  506. command_setup_channel(channel_t *chan)
  507. {
  508. tor_assert(chan);
  509. channel_set_cell_handlers(chan,
  510. command_process_cell,
  511. command_process_var_cell);
  512. }
  513. /** Given a listener, install the right handler to process incoming
  514. * channels on it.
  515. */
  516. void
  517. command_setup_listener(channel_listener_t *listener)
  518. {
  519. tor_assert(listener);
  520. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
  521. channel_listener_set_listener_fn(listener, command_handle_incoming_channel);
  522. }