channel.h 25 KB

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