channel.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* * Copyright (c) 2012-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file channel.h
  5. * \brief Header file for channel.c
  6. **/
  7. #ifndef TOR_CHANNEL_H
  8. #define TOR_CHANNEL_H
  9. #include "or.h"
  10. #include "circuitmux.h"
  11. #include "timers.h"
  12. #include "handles.h"
  13. /* Channel handler function pointer typedefs */
  14. typedef void (*channel_listener_fn_ptr)(channel_listener_t *, channel_t *);
  15. typedef void (*channel_cell_handler_fn_ptr)(channel_t *, cell_t *);
  16. typedef void (*channel_var_cell_handler_fn_ptr)(channel_t *, var_cell_t *);
  17. struct cell_queue_entry_s;
  18. TOR_SIMPLEQ_HEAD(chan_cell_queue, cell_queue_entry_s);
  19. typedef struct chan_cell_queue chan_cell_queue_t;
  20. /**
  21. * This enum is used by channelpadding to decide when to pad channels.
  22. * Don't add values to it without updating the checks in
  23. * channelpadding_decide_to_pad_channel().
  24. */
  25. typedef enum {
  26. CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS = 0,
  27. CHANNEL_USED_FOR_FULL_CIRCS,
  28. CHANNEL_USED_FOR_USER_TRAFFIC,
  29. } channel_usage_info_t;
  30. /**
  31. * Channel struct; see the channel_t typedef in or.h. A channel is an
  32. * abstract interface for the OR-to-OR connection, similar to connection_or_t,
  33. * but without the strong coupling to the underlying TLS implementation. They
  34. * are constructed by calling a protocol-specific function to open a channel
  35. * to a particular node, and once constructed support the abstract operations
  36. * defined below.
  37. */
  38. struct channel_s {
  39. /** Magic number for type-checking cast macros */
  40. uint32_t magic;
  41. /** List entry for hashtable for global-identifier lookup. */
  42. HT_ENTRY(channel_s) gidmap_node;
  43. /** Handle entry for handle-based lookup */
  44. HANDLE_ENTRY(channel, channel_s);
  45. /** Current channel state */
  46. channel_state_t state;
  47. /** Globally unique ID number for a channel over the lifetime of a Tor
  48. * process. This may not be 0.
  49. */
  50. uint64_t global_identifier;
  51. /** Should we expect to see this channel in the channel lists? */
  52. unsigned char registered:1;
  53. /** has this channel ever been open? */
  54. unsigned int has_been_open:1;
  55. /**
  56. * This field indicates if the other side has enabled or disabled
  57. * padding via either the link protocol version or
  58. * channelpadding_negotiate cells.
  59. *
  60. * Clients can override this with ConnectionPadding in torrc to
  61. * disable or force padding to relays, but relays cannot override the
  62. * client's request.
  63. */
  64. unsigned int padding_enabled:1;
  65. /** Cached value of our decision to pad (to avoid expensive
  66. * checks during critical path statistics counting). */
  67. unsigned int currently_padding:1;
  68. /** Is there a pending netflow padding callback? */
  69. unsigned int pending_padding_callback:1;
  70. /** Has this channel ever been used for non-directory traffic?
  71. * Used to decide what channels to pad, and when. */
  72. channel_usage_info_t channel_usage;
  73. /** When should we send a cell for netflow padding, in absolute
  74. * milliseconds since monotime system start. 0 means no padding
  75. * is scheduled. */
  76. uint64_t next_padding_time_ms;
  77. /** The callback pointer for the padding callbacks */
  78. tor_timer_t *padding_timer;
  79. /** The handle to this channel (to free on canceled timers) */
  80. struct channel_handle_t *timer_handle;
  81. /**
  82. * These two fields specify the minimum and maximum negotiated timeout
  83. * values for inactivity (send or receive) before we decide to pad a
  84. * channel. These fields can be set either via a PADDING_NEGOTIATE cell,
  85. * or the torrc option ReducedConnectionPadding. The consensus parameters
  86. * nf_ito_low and nf_ito_high are used to ensure that padding can only be
  87. * negotiated to be less frequent than what is specified in the consensus.
  88. * (This is done to prevent wingnut clients from requesting excessive
  89. * padding).
  90. *
  91. * The actual timeout value is randomly chosen between these two values
  92. * as per the table in channelpadding_get_netflow_inactive_timeout_ms(),
  93. * after ensuring that these values do not specify lower timeouts than
  94. * the consensus parameters.
  95. *
  96. * If these are 0, we have not negotiated or specified custom padding
  97. * times, and instead use consensus defaults. */
  98. uint16_t padding_timeout_low_ms;
  99. uint16_t padding_timeout_high_ms;
  100. /** Why did we close?
  101. */
  102. enum {
  103. CHANNEL_NOT_CLOSING = 0,
  104. CHANNEL_CLOSE_REQUESTED,
  105. CHANNEL_CLOSE_FROM_BELOW,
  106. CHANNEL_CLOSE_FOR_ERROR
  107. } reason_for_closing;
  108. /** State variable for use by the scheduler */
  109. enum {
  110. /*
  111. * The channel is not open, or it has a full output buffer but no queued
  112. * cells.
  113. */
  114. SCHED_CHAN_IDLE = 0,
  115. /*
  116. * The channel has space on its output buffer to write, but no queued
  117. * cells.
  118. */
  119. SCHED_CHAN_WAITING_FOR_CELLS,
  120. /*
  121. * The scheduler has queued cells but no output buffer space to write.
  122. */
  123. SCHED_CHAN_WAITING_TO_WRITE,
  124. /*
  125. * The scheduler has both queued cells and output buffer space, and is
  126. * eligible for the scheduler loop.
  127. */
  128. SCHED_CHAN_PENDING
  129. } scheduler_state;
  130. /** Heap index for use by the scheduler */
  131. int sched_heap_idx;
  132. /** Timestamps for both cell channels and listeners */
  133. time_t timestamp_created; /* Channel created */
  134. time_t timestamp_active; /* Any activity */
  135. /**
  136. * This is a high-resolution monotonic timestamp that marks when we
  137. * believe the channel has actually sent or received data to/from
  138. * the wire. Right now, it is used to determine when we should send
  139. * a padding cell for channelpadding.
  140. *
  141. * XXX: Are we setting timestamp_xfer_ms in the right places to
  142. * accurately reflect actual network data transfer? Or might this be
  143. * very wrong wrt when bytes actually go on the wire?
  144. */
  145. uint64_t timestamp_xfer_ms;
  146. /* Methods implemented by the lower layer */
  147. /** Free a channel */
  148. void (*free_fn)(channel_t *);
  149. /** Close an open channel */
  150. void (*close)(channel_t *);
  151. /** Describe the transport subclass for this channel */
  152. const char * (*describe_transport)(channel_t *);
  153. /** Optional method to dump transport-specific statistics on the channel */
  154. void (*dumpstats)(channel_t *, int);
  155. /** Registered handlers for incoming cells */
  156. channel_cell_handler_fn_ptr cell_handler;
  157. channel_var_cell_handler_fn_ptr var_cell_handler;
  158. /* Methods implemented by the lower layer */
  159. /**
  160. * Ask the lower layer for an estimate of the average overhead for
  161. * transmissions on this channel.
  162. */
  163. double (*get_overhead_estimate)(channel_t *);
  164. /*
  165. * Ask the underlying transport what the remote endpoint address is, in
  166. * a tor_addr_t. This is optional and subclasses may leave this NULL.
  167. * If they implement it, they should write the address out to the
  168. * provided tor_addr_t *, and return 1 if successful or 0 if no address
  169. * available.
  170. */
  171. int (*get_remote_addr)(channel_t *, tor_addr_t *);
  172. int (*get_transport_name)(channel_t *chan, char **transport_out);
  173. #define GRD_FLAG_ORIGINAL 1
  174. #define GRD_FLAG_ADDR_ONLY 2
  175. /**
  176. * Get a text description of the remote endpoint; canonicalized if the flag
  177. * GRD_FLAG_ORIGINAL is not set, or the one we originally connected
  178. * to/received from if it is. If GRD_FLAG_ADDR_ONLY is set, we return only
  179. * the original address.
  180. */
  181. const char * (*get_remote_descr)(channel_t *, int);
  182. /** Check if the lower layer has queued writes */
  183. int (*has_queued_writes)(channel_t *);
  184. /**
  185. * If the second param is zero, ask the lower layer if this is
  186. * 'canonical', for a transport-specific definition of canonical; if
  187. * it is 1, ask if the answer to the preceding query is safe to rely
  188. * on.
  189. */
  190. int (*is_canonical)(channel_t *, int);
  191. /** Check if this channel matches a specified extend_info_t */
  192. int (*matches_extend_info)(channel_t *, extend_info_t *);
  193. /** Check if this channel matches a target address when extending */
  194. int (*matches_target)(channel_t *, const tor_addr_t *);
  195. /* Ask the lower layer how many bytes it has queued but not yet sent */
  196. size_t (*num_bytes_queued)(channel_t *);
  197. /* Ask the lower layer how many cells can be written */
  198. int (*num_cells_writeable)(channel_t *);
  199. /* Write a cell to an open channel */
  200. int (*write_cell)(channel_t *, cell_t *);
  201. /** Write a packed cell to an open channel */
  202. int (*write_packed_cell)(channel_t *, packed_cell_t *);
  203. /** Write a variable-length cell to an open channel */
  204. int (*write_var_cell)(channel_t *, var_cell_t *);
  205. /**
  206. * Hash of the public RSA key for the other side's RSA identity key -- or
  207. * zeroes if we don't have an RSA identity in mind for the other side, and
  208. * it hasn't shown us one.
  209. *
  210. * Note that this is the RSA identity that we hope the other side has -- not
  211. * necessarily its true identity. Don't believe this identity unless
  212. * authentication has happened.
  213. */
  214. char identity_digest[DIGEST_LEN];
  215. /**
  216. * Ed25519 key for the other side of this channel -- or zeroes if we don't
  217. * have an Ed25519 identity in mind for the other side, and it hasn't shown
  218. * us one.
  219. *
  220. * Note that this is the identity that we hope the other side has -- not
  221. * necessarily its true identity. Don't believe this identity unless
  222. * authentication has happened.
  223. */
  224. ed25519_public_key_t ed25519_identity;
  225. /** Nickname of the OR on the other side, or NULL if none. */
  226. char *nickname;
  227. /**
  228. * Linked list of channels with the same RSA identity digest, for use with
  229. * the digest->channel map
  230. */
  231. TOR_LIST_ENTRY(channel_s) next_with_same_id;
  232. /** List of incoming cells to handle */
  233. chan_cell_queue_t incoming_queue;
  234. /** List of queued outgoing cells */
  235. chan_cell_queue_t outgoing_queue;
  236. /** Circuit mux for circuits sending on this channel */
  237. circuitmux_t *cmux;
  238. /** Circuit ID generation stuff for use by circuitbuild.c */
  239. /**
  240. * When we send CREATE cells along this connection, which half of the
  241. * space should we use?
  242. */
  243. circ_id_type_bitfield_t circ_id_type:2;
  244. /* DOCDOC */
  245. unsigned wide_circ_ids:1;
  246. /** For how many circuits are we n_chan? What about p_chan? */
  247. unsigned int num_n_circuits, num_p_circuits;
  248. /**
  249. * True iff this channel shouldn't get any new circs attached to it,
  250. * because the connection is too old, or because there's a better one.
  251. * More generally, this flag is used to note an unhealthy connection;
  252. * for example, if a bad connection fails we shouldn't assume that the
  253. * router itself has a problem.
  254. */
  255. unsigned int is_bad_for_new_circs:1;
  256. /** True iff we have decided that the other end of this connection
  257. * is a client. Channels with this flag set should never be used
  258. * to satisfy an EXTEND request. */
  259. unsigned int is_client:1;
  260. /** Set if the channel was initiated remotely (came from a listener) */
  261. unsigned int is_incoming:1;
  262. /** Set by lower layer if this is local; i.e., everything it communicates
  263. * with for this channel returns true for is_local_addr(). This is used
  264. * to decide whether to declare reachability when we receive something on
  265. * this channel in circuitbuild.c
  266. */
  267. unsigned int is_local:1;
  268. /** Have we logged a warning about circID exhaustion on this channel?
  269. * If so, when? */
  270. ratelim_t last_warned_circ_ids_exhausted;
  271. /** Channel timestamps for cell channels */
  272. time_t timestamp_client; /* Client used this, according to relay.c */
  273. time_t timestamp_drained; /* Output queue empty */
  274. time_t timestamp_recv; /* Cell received from lower layer */
  275. time_t timestamp_xmit; /* Cell sent to lower layer */
  276. /** Timestamp for run_connection_housekeeping(). We update this once a
  277. * second when we run housekeeping and find a circuit on this channel, and
  278. * whenever we add a circuit to the channel. */
  279. time_t timestamp_last_had_circuits;
  280. /** Unique ID for measuring direct network status requests;vtunneled ones
  281. * come over a circuit_t, which has a dirreq_id field as well, but is a
  282. * distinct namespace. */
  283. uint64_t dirreq_id;
  284. /** Channel counters for cell channels */
  285. uint64_t n_cells_recved, n_bytes_recved;
  286. uint64_t n_cells_xmitted, n_bytes_xmitted;
  287. /** Our current contribution to the scheduler's total xmit queue */
  288. uint64_t bytes_queued_for_xmit;
  289. /** Number of bytes in this channel's cell queue; does not include
  290. * lower-layer queueing.
  291. */
  292. uint64_t bytes_in_queue;
  293. };
  294. struct channel_listener_s {
  295. /* Current channel listener state */
  296. channel_listener_state_t state;
  297. /* Globally unique ID number for a channel over the lifetime of a Tor
  298. * process.
  299. */
  300. uint64_t global_identifier;
  301. /** Should we expect to see this channel in the channel lists? */
  302. unsigned char registered:1;
  303. /** Why did we close?
  304. */
  305. enum {
  306. CHANNEL_LISTENER_NOT_CLOSING = 0,
  307. CHANNEL_LISTENER_CLOSE_REQUESTED,
  308. CHANNEL_LISTENER_CLOSE_FROM_BELOW,
  309. CHANNEL_LISTENER_CLOSE_FOR_ERROR
  310. } reason_for_closing;
  311. /** Timestamps for both cell channels and listeners */
  312. time_t timestamp_created; /* Channel created */
  313. time_t timestamp_active; /* Any activity */
  314. /* Methods implemented by the lower layer */
  315. /** Free a channel */
  316. void (*free_fn)(channel_listener_t *);
  317. /** Close an open channel */
  318. void (*close)(channel_listener_t *);
  319. /** Describe the transport subclass for this channel */
  320. const char * (*describe_transport)(channel_listener_t *);
  321. /** Optional method to dump transport-specific statistics on the channel */
  322. void (*dumpstats)(channel_listener_t *, int);
  323. /** Registered listen handler to call on incoming connection */
  324. channel_listener_fn_ptr listener;
  325. /** List of pending incoming connections */
  326. smartlist_t *incoming_list;
  327. /** Timestamps for listeners */
  328. time_t timestamp_accepted;
  329. /** Counters for listeners */
  330. uint64_t n_accepted;
  331. };
  332. /* Channel state manipulations */
  333. int channel_state_is_valid(channel_state_t state);
  334. int channel_listener_state_is_valid(channel_listener_state_t state);
  335. int channel_state_can_transition(channel_state_t from, channel_state_t to);
  336. int channel_listener_state_can_transition(channel_listener_state_t from,
  337. channel_listener_state_t to);
  338. const char * channel_state_to_string(channel_state_t state);
  339. const char *
  340. channel_listener_state_to_string(channel_listener_state_t state);
  341. /* Abstract channel operations */
  342. void channel_mark_for_close(channel_t *chan);
  343. void channel_write_cell(channel_t *chan, cell_t *cell);
  344. void channel_write_packed_cell(channel_t *chan, packed_cell_t *cell);
  345. void channel_write_var_cell(channel_t *chan, var_cell_t *cell);
  346. void channel_listener_mark_for_close(channel_listener_t *chan_l);
  347. /* Channel callback registrations */
  348. /* Listener callback */
  349. channel_listener_fn_ptr
  350. channel_listener_get_listener_fn(channel_listener_t *chan);
  351. void channel_listener_set_listener_fn(channel_listener_t *chan,
  352. channel_listener_fn_ptr listener);
  353. /* Incoming cell callbacks */
  354. channel_cell_handler_fn_ptr channel_get_cell_handler(channel_t *chan);
  355. channel_var_cell_handler_fn_ptr
  356. channel_get_var_cell_handler(channel_t *chan);
  357. void channel_set_cell_handlers(channel_t *chan,
  358. channel_cell_handler_fn_ptr cell_handler,
  359. channel_var_cell_handler_fn_ptr
  360. var_cell_handler);
  361. /* Clean up closed channels and channel listeners periodically; these are
  362. * called from run_scheduled_events() in main.c.
  363. */
  364. void channel_run_cleanup(void);
  365. void channel_listener_run_cleanup(void);
  366. /* Close all channels and deallocate everything */
  367. void channel_free_all(void);
  368. /* Dump some statistics in the log */
  369. void channel_dumpstats(int severity);
  370. void channel_listener_dumpstats(int severity);
  371. /* Set the cmux policy on all active channels */
  372. void channel_set_cmux_policy_everywhere(circuitmux_policy_t *pol);
  373. #ifdef TOR_CHANNEL_INTERNAL_
  374. #ifdef CHANNEL_PRIVATE_
  375. /* Cell queue structure (here rather than channel.c for test suite use) */
  376. typedef struct cell_queue_entry_s cell_queue_entry_t;
  377. struct cell_queue_entry_s {
  378. TOR_SIMPLEQ_ENTRY(cell_queue_entry_s) next;
  379. enum {
  380. CELL_QUEUE_FIXED,
  381. CELL_QUEUE_VAR,
  382. CELL_QUEUE_PACKED
  383. } type;
  384. union {
  385. struct {
  386. cell_t *cell;
  387. } fixed;
  388. struct {
  389. var_cell_t *var_cell;
  390. } var;
  391. struct {
  392. packed_cell_t *packed_cell;
  393. } packed;
  394. } u;
  395. };
  396. /* Cell queue functions for benefit of test suite */
  397. STATIC int chan_cell_queue_len(const chan_cell_queue_t *queue);
  398. STATIC void cell_queue_entry_free(cell_queue_entry_t *q, int handed_off);
  399. void channel_write_cell_generic_(channel_t *chan, const char *cell_type,
  400. void *cell, cell_queue_entry_t *q);
  401. #endif
  402. /* Channel operations for subclasses and internal use only */
  403. /* Initialize a newly allocated channel - do this first in subclass
  404. * constructors.
  405. */
  406. void channel_init(channel_t *chan);
  407. void channel_init_listener(channel_listener_t *chan);
  408. /* Channel registration/unregistration */
  409. void channel_register(channel_t *chan);
  410. void channel_unregister(channel_t *chan);
  411. /* Channel listener registration/unregistration */
  412. void channel_listener_register(channel_listener_t *chan_l);
  413. void channel_listener_unregister(channel_listener_t *chan_l);
  414. /* Close from below */
  415. void channel_close_from_lower_layer(channel_t *chan);
  416. void channel_close_for_error(channel_t *chan);
  417. void channel_closed(channel_t *chan);
  418. void channel_listener_close_from_lower_layer(channel_listener_t *chan_l);
  419. void channel_listener_close_for_error(channel_listener_t *chan_l);
  420. void channel_listener_closed(channel_listener_t *chan_l);
  421. /* Free a channel */
  422. void channel_free(channel_t *chan);
  423. void channel_listener_free(channel_listener_t *chan_l);
  424. /* State/metadata setters */
  425. void channel_change_state(channel_t *chan, channel_state_t to_state);
  426. void channel_clear_identity_digest(channel_t *chan);
  427. void channel_clear_remote_end(channel_t *chan);
  428. void channel_mark_local(channel_t *chan);
  429. void channel_mark_incoming(channel_t *chan);
  430. void channel_mark_outgoing(channel_t *chan);
  431. void channel_mark_remote(channel_t *chan);
  432. void channel_set_identity_digest(channel_t *chan,
  433. const char *identity_digest,
  434. const ed25519_public_key_t *ed_identity);
  435. void channel_set_remote_end(channel_t *chan,
  436. const char *identity_digest,
  437. const char *nickname);
  438. void channel_listener_change_state(channel_listener_t *chan_l,
  439. channel_listener_state_t to_state);
  440. /* Timestamp updates */
  441. void channel_timestamp_created(channel_t *chan);
  442. void channel_timestamp_active(channel_t *chan);
  443. void channel_timestamp_drained(channel_t *chan);
  444. void channel_timestamp_recv(channel_t *chan);
  445. void channel_timestamp_xmit(channel_t *chan);
  446. void channel_listener_timestamp_created(channel_listener_t *chan_l);
  447. void channel_listener_timestamp_active(channel_listener_t *chan_l);
  448. void channel_listener_timestamp_accepted(channel_listener_t *chan_l);
  449. /* Incoming channel handling */
  450. void channel_listener_process_incoming(channel_listener_t *listener);
  451. void channel_listener_queue_incoming(channel_listener_t *listener,
  452. channel_t *incoming);
  453. /* Incoming cell handling */
  454. void channel_process_cells(channel_t *chan);
  455. void channel_queue_cell(channel_t *chan, cell_t *cell);
  456. void channel_queue_var_cell(channel_t *chan, var_cell_t *var_cell);
  457. /* Outgoing cell handling */
  458. void channel_flush_cells(channel_t *chan);
  459. /* Request from lower layer for more cells if available */
  460. MOCK_DECL(ssize_t, channel_flush_some_cells,
  461. (channel_t *chan, ssize_t num_cells));
  462. /* Query if data available on this channel */
  463. int channel_more_to_flush(channel_t *chan);
  464. /* Notify flushed outgoing for dirreq handling */
  465. void channel_notify_flushed(channel_t *chan);
  466. /* Handle stuff we need to do on open like notifying circuits */
  467. void channel_do_open_actions(channel_t *chan);
  468. #ifdef TOR_UNIT_TESTS
  469. extern uint64_t estimated_total_queue_size;
  470. #endif
  471. #endif
  472. /* Helper functions to perform operations on channels */
  473. int channel_send_destroy(circid_t circ_id, channel_t *chan,
  474. int reason);
  475. /*
  476. * Outside abstract interfaces that should eventually get turned into
  477. * something transport/address format independent.
  478. */
  479. channel_t * channel_connect(const tor_addr_t *addr, uint16_t port,
  480. const char *rsa_id_digest,
  481. const ed25519_public_key_t *ed_id);
  482. channel_t * channel_get_for_extend(const char *rsa_id_digest,
  483. const ed25519_public_key_t *ed_id,
  484. const tor_addr_t *target_addr,
  485. const char **msg_out,
  486. int *launch_out);
  487. /* Ask which of two channels is better for circuit-extension purposes */
  488. int channel_is_better(time_t now,
  489. channel_t *a, channel_t *b,
  490. int forgive_new_connections);
  491. /** Channel lookups
  492. */
  493. channel_t * channel_find_by_global_id(uint64_t global_identifier);
  494. channel_t * channel_find_by_remote_identity(const char *rsa_id_digest,
  495. const ed25519_public_key_t *ed_id);
  496. /** For things returned by channel_find_by_remote_digest(), walk the list.
  497. * The RSA key will match for all returned elements; the Ed25519 key might not.
  498. */
  499. channel_t * channel_next_with_rsa_identity(channel_t *chan);
  500. /*
  501. * Helper macros to lookup state of given channel.
  502. */
  503. #define CHANNEL_IS_CLOSED(chan) (channel_is_in_state((chan), \
  504. CHANNEL_STATE_CLOSED))
  505. #define CHANNEL_IS_OPENING(chan) (channel_is_in_state((chan), \
  506. CHANNEL_STATE_OPENING))
  507. #define CHANNEL_IS_OPEN(chan) (channel_is_in_state((chan), \
  508. CHANNEL_STATE_OPEN))
  509. #define CHANNEL_IS_MAINT(chan) (channel_is_in_state((chan), \
  510. CHANNEL_STATE_MAINT))
  511. #define CHANNEL_IS_CLOSING(chan) (channel_is_in_state((chan), \
  512. CHANNEL_STATE_CLOSING))
  513. #define CHANNEL_IS_ERROR(chan) (channel_is_in_state((chan), \
  514. CHANNEL_STATE_ERROR))
  515. #define CHANNEL_FINISHED(chan) (CHANNEL_IS_CLOSED(chan) || \
  516. CHANNEL_IS_ERROR(chan))
  517. #define CHANNEL_CONDEMNED(chan) (CHANNEL_IS_CLOSING(chan) || \
  518. CHANNEL_FINISHED(chan))
  519. #define CHANNEL_CAN_HANDLE_CELLS(chan) (CHANNEL_IS_OPENING(chan) || \
  520. CHANNEL_IS_OPEN(chan) || \
  521. CHANNEL_IS_MAINT(chan))
  522. static inline int
  523. channel_is_in_state(channel_t *chan, channel_state_t state)
  524. {
  525. return chan->state == state;
  526. }
  527. /*
  528. * Metadata queries/updates
  529. */
  530. const char * channel_describe_transport(channel_t *chan);
  531. MOCK_DECL(void, channel_dump_statistics, (channel_t *chan, int severity));
  532. void channel_dump_transport_statistics(channel_t *chan, int severity);
  533. const char * channel_get_actual_remote_descr(channel_t *chan);
  534. const char * channel_get_actual_remote_address(channel_t *chan);
  535. int channel_get_addr_if_possible(channel_t *chan, tor_addr_t *addr_out);
  536. const char * channel_get_canonical_remote_descr(channel_t *chan);
  537. int channel_has_queued_writes(channel_t *chan);
  538. int channel_is_bad_for_new_circs(channel_t *chan);
  539. void channel_mark_bad_for_new_circs(channel_t *chan);
  540. int channel_is_canonical(channel_t *chan);
  541. int channel_is_canonical_is_reliable(channel_t *chan);
  542. int channel_is_client(channel_t *chan);
  543. int channel_is_local(channel_t *chan);
  544. int channel_is_incoming(channel_t *chan);
  545. int channel_is_outgoing(channel_t *chan);
  546. void channel_mark_client(channel_t *chan);
  547. int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info);
  548. int channel_matches_target_addr_for_extend(channel_t *chan,
  549. const tor_addr_t *target);
  550. unsigned int channel_num_circuits(channel_t *chan);
  551. MOCK_DECL(void,channel_set_circid_type,(channel_t *chan,
  552. crypto_pk_t *identity_rcvd,
  553. int consider_identity));
  554. void channel_timestamp_client(channel_t *chan);
  555. void channel_update_xmit_queue_size(channel_t *chan);
  556. const char * channel_listener_describe_transport(channel_listener_t *chan_l);
  557. void channel_listener_dump_statistics(channel_listener_t *chan_l,
  558. int severity);
  559. void channel_listener_dump_transport_statistics(channel_listener_t *chan_l,
  560. int severity);
  561. void channel_update_bad_for_new_circs(const char *digest, int force);
  562. /* Flow control queries */
  563. uint64_t channel_get_global_queue_estimate(void);
  564. int channel_num_cells_writeable(channel_t *chan);
  565. /* Timestamp queries */
  566. time_t channel_when_created(channel_t *chan);
  567. time_t channel_when_last_active(channel_t *chan);
  568. time_t channel_when_last_client(channel_t *chan);
  569. time_t channel_when_last_drained(channel_t *chan);
  570. time_t channel_when_last_recv(channel_t *chan);
  571. time_t channel_when_last_xmit(channel_t *chan);
  572. time_t channel_listener_when_created(channel_listener_t *chan_l);
  573. time_t channel_listener_when_last_active(channel_listener_t *chan_l);
  574. time_t channel_listener_when_last_accepted(channel_listener_t *chan_l);
  575. /* Counter queries */
  576. uint64_t channel_count_recved(channel_t *chan);
  577. uint64_t channel_count_xmitted(channel_t *chan);
  578. uint64_t channel_listener_count_accepted(channel_listener_t *chan_l);
  579. int packed_cell_is_destroy(channel_t *chan,
  580. const packed_cell_t *packed_cell,
  581. circid_t *circid_out);
  582. /* Declare the handle helpers */
  583. HANDLE_DECL(channel, channel_s,);
  584. #endif