Merge remote-tracking branch 'tor-gitlab/mr/189' into maint-0.3.5
[tor.git] / src / core / or / channel.h
blobd41f0d70bbc1d41dd4f922e916dbff0862d30430
1 /* * Copyright (c) 2012-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file channel.h
6 * \brief Header file for channel.c
7 **/
9 #ifndef TOR_CHANNEL_H
10 #define TOR_CHANNEL_H
12 #include "core/or/or.h"
13 #include "core/or/circuitmux.h"
14 #include "lib/container/handles.h"
15 #include "lib/crypt_ops/crypto_ed25519.h"
17 #include "tor_queue.h"
19 #define tor_timer_t timeout
20 struct tor_timer_t;
22 /* Channel handler function pointer typedefs */
23 typedef void (*channel_listener_fn_ptr)(channel_listener_t *, channel_t *);
24 typedef void (*channel_cell_handler_fn_ptr)(channel_t *, cell_t *);
25 typedef void (*channel_var_cell_handler_fn_ptr)(channel_t *, var_cell_t *);
27 /**
28 * This enum is used by channelpadding to decide when to pad channels.
29 * Don't add values to it without updating the checks in
30 * channelpadding_decide_to_pad_channel().
32 typedef enum {
33 CHANNEL_USED_NOT_USED_FOR_FULL_CIRCS = 0,
34 CHANNEL_USED_FOR_FULL_CIRCS,
35 CHANNEL_USED_FOR_USER_TRAFFIC,
36 } channel_usage_info_t;
38 /** Possible rules for generating circuit IDs on an OR connection. */
39 typedef enum {
40 CIRC_ID_TYPE_LOWER=0, /**< Pick from 0..1<<15-1. */
41 CIRC_ID_TYPE_HIGHER=1, /**< Pick from 1<<15..1<<16-1. */
42 /** The other side of a connection is an OP: never create circuits to it,
43 * and let it use any circuit ID it wants. */
44 CIRC_ID_TYPE_NEITHER=2
45 } circ_id_type_t;
46 #define circ_id_type_bitfield_t ENUM_BF(circ_id_type_t)
48 /* channel states for channel_t */
50 typedef enum {
52 * Closed state - channel is inactive
54 * Permitted transitions from:
55 * - CHANNEL_STATE_CLOSING
56 * Permitted transitions to:
57 * - CHANNEL_STATE_OPENING
59 CHANNEL_STATE_CLOSED = 0,
61 * Opening state - channel is trying to connect
63 * Permitted transitions from:
64 * - CHANNEL_STATE_CLOSED
65 * Permitted transitions to:
66 * - CHANNEL_STATE_CLOSING
67 * - CHANNEL_STATE_ERROR
68 * - CHANNEL_STATE_OPEN
70 CHANNEL_STATE_OPENING,
72 * Open state - channel is active and ready for use
74 * Permitted transitions from:
75 * - CHANNEL_STATE_MAINT
76 * - CHANNEL_STATE_OPENING
77 * Permitted transitions to:
78 * - CHANNEL_STATE_CLOSING
79 * - CHANNEL_STATE_ERROR
80 * - CHANNEL_STATE_MAINT
82 CHANNEL_STATE_OPEN,
84 * Maintenance state - channel is temporarily offline for subclass specific
85 * maintenance activities such as TLS renegotiation.
87 * Permitted transitions from:
88 * - CHANNEL_STATE_OPEN
89 * Permitted transitions to:
90 * - CHANNEL_STATE_CLOSING
91 * - CHANNEL_STATE_ERROR
92 * - CHANNEL_STATE_OPEN
94 CHANNEL_STATE_MAINT,
96 * Closing state - channel is shutting down
98 * Permitted transitions from:
99 * - CHANNEL_STATE_MAINT
100 * - CHANNEL_STATE_OPEN
101 * Permitted transitions to:
102 * - CHANNEL_STATE_CLOSED,
103 * - CHANNEL_STATE_ERROR
105 CHANNEL_STATE_CLOSING,
107 * Error state - channel has experienced a permanent error
109 * Permitted transitions from:
110 * - CHANNEL_STATE_CLOSING
111 * - CHANNEL_STATE_MAINT
112 * - CHANNEL_STATE_OPENING
113 * - CHANNEL_STATE_OPEN
114 * Permitted transitions to:
115 * - None
117 CHANNEL_STATE_ERROR,
119 * Placeholder for maximum state value
121 CHANNEL_STATE_LAST
122 } channel_state_t;
124 /* channel listener states for channel_listener_t */
126 typedef enum {
128 * Closed state - channel listener is inactive
130 * Permitted transitions from:
131 * - CHANNEL_LISTENER_STATE_CLOSING
132 * Permitted transitions to:
133 * - CHANNEL_LISTENER_STATE_LISTENING
135 CHANNEL_LISTENER_STATE_CLOSED = 0,
137 * Listening state - channel listener is listening for incoming
138 * connections
140 * Permitted transitions from:
141 * - CHANNEL_LISTENER_STATE_CLOSED
142 * Permitted transitions to:
143 * - CHANNEL_LISTENER_STATE_CLOSING
144 * - CHANNEL_LISTENER_STATE_ERROR
146 CHANNEL_LISTENER_STATE_LISTENING,
148 * Closing state - channel listener is shutting down
150 * Permitted transitions from:
151 * - CHANNEL_LISTENER_STATE_LISTENING
152 * Permitted transitions to:
153 * - CHANNEL_LISTENER_STATE_CLOSED,
154 * - CHANNEL_LISTENER_STATE_ERROR
156 CHANNEL_LISTENER_STATE_CLOSING,
158 * Error state - channel listener has experienced a permanent error
160 * Permitted transitions from:
161 * - CHANNEL_STATE_CLOSING
162 * - CHANNEL_STATE_LISTENING
163 * Permitted transitions to:
164 * - None
166 CHANNEL_LISTENER_STATE_ERROR,
168 * Placeholder for maximum state value
170 CHANNEL_LISTENER_STATE_LAST
171 } channel_listener_state_t;
174 * Channel struct; see the channel_t typedef in or.h. A channel is an
175 * abstract interface for the OR-to-OR connection, similar to connection_or_t,
176 * but without the strong coupling to the underlying TLS implementation. They
177 * are constructed by calling a protocol-specific function to open a channel
178 * to a particular node, and once constructed support the abstract operations
179 * defined below.
181 struct channel_s {
182 /** Magic number for type-checking cast macros */
183 uint32_t magic;
185 /** List entry for hashtable for global-identifier lookup. */
186 HT_ENTRY(channel_s) gidmap_node;
188 /** Handle entry for handle-based lookup */
189 HANDLE_ENTRY(channel, channel_s);
191 /** Current channel state */
192 channel_state_t state;
194 /** Globally unique ID number for a channel over the lifetime of a Tor
195 * process. This may not be 0.
197 uint64_t global_identifier;
199 /** Should we expect to see this channel in the channel lists? */
200 unsigned char registered:1;
202 /** has this channel ever been open? */
203 unsigned int has_been_open:1;
206 * This field indicates if the other side has enabled or disabled
207 * padding via either the link protocol version or
208 * channelpadding_negotiate cells.
210 * Clients can override this with ConnectionPadding in torrc to
211 * disable or force padding to relays, but relays cannot override the
212 * client's request.
214 unsigned int padding_enabled:1;
216 /** Cached value of our decision to pad (to avoid expensive
217 * checks during critical path statistics counting). */
218 unsigned int currently_padding:1;
220 /** Is there a pending netflow padding callback? */
221 unsigned int pending_padding_callback:1;
223 /** Is our peer likely to consider this channel canonical? */
224 unsigned int is_canonical_to_peer:1;
226 /** Has this channel ever been used for non-directory traffic?
227 * Used to decide what channels to pad, and when. */
228 channel_usage_info_t channel_usage;
230 /** When should we send a cell for netflow padding? 0 means no padding is
231 * scheduled. */
232 monotime_coarse_t next_padding_time;
234 /** The callback pointer for the padding callbacks */
235 struct tor_timer_t *padding_timer;
236 /** The handle to this channel (to free on canceled timers) */
237 struct channel_handle_t *timer_handle;
240 * These two fields specify the minimum and maximum negotiated timeout
241 * values for inactivity (send or receive) before we decide to pad a
242 * channel. These fields can be set either via a PADDING_NEGOTIATE cell,
243 * or the torrc option ReducedConnectionPadding. The consensus parameters
244 * nf_ito_low and nf_ito_high are used to ensure that padding can only be
245 * negotiated to be less frequent than what is specified in the consensus.
246 * (This is done to prevent wingnut clients from requesting excessive
247 * padding).
249 * The actual timeout value is randomly chosen between these two values
250 * as per the table in channelpadding_get_netflow_inactive_timeout_ms(),
251 * after ensuring that these values do not specify lower timeouts than
252 * the consensus parameters.
254 * If these are 0, we have not negotiated or specified custom padding
255 * times, and instead use consensus defaults. */
256 uint16_t padding_timeout_low_ms;
257 uint16_t padding_timeout_high_ms;
259 /** Why did we close?
261 enum {
262 CHANNEL_NOT_CLOSING = 0,
263 CHANNEL_CLOSE_REQUESTED,
264 CHANNEL_CLOSE_FROM_BELOW,
265 CHANNEL_CLOSE_FOR_ERROR
266 } reason_for_closing;
268 /** State variable for use by the scheduler */
269 enum {
271 * The channel is not open, or it has a full output buffer but no queued
272 * cells.
274 SCHED_CHAN_IDLE = 0,
276 * The channel has space on its output buffer to write, but no queued
277 * cells.
279 SCHED_CHAN_WAITING_FOR_CELLS,
281 * The scheduler has queued cells but no output buffer space to write.
283 SCHED_CHAN_WAITING_TO_WRITE,
285 * The scheduler has both queued cells and output buffer space, and is
286 * eligible for the scheduler loop.
288 SCHED_CHAN_PENDING
289 } scheduler_state;
291 /** Heap index for use by the scheduler */
292 int sched_heap_idx;
294 /** Timestamps for both cell channels and listeners */
295 time_t timestamp_created; /* Channel created */
296 time_t timestamp_active; /* Any activity */
299 * This is a monotonic timestamp that marks when we
300 * believe the channel has actually sent or received data to/from
301 * the wire. Right now, it is used to determine when we should send
302 * a padding cell for channelpadding.
304 * XXX: Are we setting timestamp_xfer_ms in the right places to
305 * accurately reflect actual network data transfer? Or might this be
306 * very wrong wrt when bytes actually go on the wire?
308 monotime_coarse_t timestamp_xfer;
310 /* Methods implemented by the lower layer */
312 /** Free a channel */
313 void (*free_fn)(channel_t *);
314 /** Close an open channel */
315 void (*close)(channel_t *);
316 /** Describe the transport subclass for this channel */
317 const char * (*describe_transport)(channel_t *);
318 /** Optional method to dump transport-specific statistics on the channel */
319 void (*dumpstats)(channel_t *, int);
321 /** Registered handlers for incoming cells */
322 channel_cell_handler_fn_ptr cell_handler;
323 channel_var_cell_handler_fn_ptr var_cell_handler;
325 /* Methods implemented by the lower layer */
328 * Ask the lower layer for an estimate of the average overhead for
329 * transmissions on this channel.
331 double (*get_overhead_estimate)(channel_t *);
333 * Ask the underlying transport what the remote endpoint address is, in
334 * a tor_addr_t. This is optional and subclasses may leave this NULL.
335 * If they implement it, they should write the address out to the
336 * provided tor_addr_t *, and return 1 if successful or 0 if no address
337 * available.
339 int (*get_remote_addr)(channel_t *, tor_addr_t *);
340 int (*get_transport_name)(channel_t *chan, char **transport_out);
342 #define GRD_FLAG_ORIGINAL 1
343 #define GRD_FLAG_ADDR_ONLY 2
345 * Get a text description of the remote endpoint; canonicalized if the flag
346 * GRD_FLAG_ORIGINAL is not set, or the one we originally connected
347 * to/received from if it is. If GRD_FLAG_ADDR_ONLY is set, we return only
348 * the original address.
350 const char * (*get_remote_descr)(channel_t *, int);
351 /** Check if the lower layer has queued writes */
352 int (*has_queued_writes)(channel_t *);
354 * Ask the lower layer if this is 'canonical', for a transport-specific
355 * definition of canonical.
357 int (*is_canonical)(channel_t *);
358 /** Check if this channel matches a specified extend_info_t */
359 int (*matches_extend_info)(channel_t *, extend_info_t *);
360 /** Check if this channel matches a target address when extending */
361 int (*matches_target)(channel_t *, const tor_addr_t *);
362 /* Ask the lower layer how many bytes it has queued but not yet sent */
363 size_t (*num_bytes_queued)(channel_t *);
364 /* Ask the lower layer how many cells can be written */
365 int (*num_cells_writeable)(channel_t *);
366 /* Write a cell to an open channel */
367 int (*write_cell)(channel_t *, cell_t *);
368 /** Write a packed cell to an open channel */
369 int (*write_packed_cell)(channel_t *, packed_cell_t *);
370 /** Write a variable-length cell to an open channel */
371 int (*write_var_cell)(channel_t *, var_cell_t *);
374 * Hash of the public RSA key for the other side's RSA identity key -- or
375 * zeroes if we don't have an RSA identity in mind for the other side, and
376 * it hasn't shown us one.
378 * Note that this is the RSA identity that we hope the other side has -- not
379 * necessarily its true identity. Don't believe this identity unless
380 * authentication has happened.
382 char identity_digest[DIGEST_LEN];
384 * Ed25519 key for the other side of this channel -- or zeroes if we don't
385 * have an Ed25519 identity in mind for the other side, and it hasn't shown
386 * us one.
388 * Note that this is the identity that we hope the other side has -- not
389 * necessarily its true identity. Don't believe this identity unless
390 * authentication has happened.
392 struct ed25519_public_key_t ed25519_identity;
395 * Linked list of channels with the same RSA identity digest, for use with
396 * the digest->channel map
398 TOR_LIST_ENTRY(channel_s) next_with_same_id;
400 /** Circuit mux for circuits sending on this channel */
401 circuitmux_t *cmux;
403 /** Circuit ID generation stuff for use by circuitbuild.c */
406 * When we send CREATE cells along this connection, which half of the
407 * space should we use?
409 circ_id_type_bitfield_t circ_id_type:2;
410 /* DOCDOC */
411 unsigned wide_circ_ids:1;
413 /** For how many circuits are we n_chan? What about p_chan? */
414 unsigned int num_n_circuits, num_p_circuits;
417 * True iff this channel shouldn't get any new circs attached to it,
418 * because the connection is too old, or because there's a better one.
419 * More generally, this flag is used to note an unhealthy connection;
420 * for example, if a bad connection fails we shouldn't assume that the
421 * router itself has a problem.
423 unsigned int is_bad_for_new_circs:1;
425 /** True iff we have decided that the other end of this connection
426 * is a client or bridge relay. Connections with this flag set should never
427 * be used to satisfy an EXTEND request. */
428 unsigned int is_client:1;
430 /** Set if the channel was initiated remotely (came from a listener) */
431 unsigned int is_incoming:1;
433 /** Set by lower layer if this is local; i.e., everything it communicates
434 * with for this channel returns true for is_local_addr(). This is used
435 * to decide whether to declare reachability when we receive something on
436 * this channel in circuitbuild.c
438 unsigned int is_local:1;
440 /** Have we logged a warning about circID exhaustion on this channel?
441 * If so, when? */
442 ratelim_t last_warned_circ_ids_exhausted;
444 /** Channel timestamps for cell channels */
445 time_t timestamp_client; /* Client used this, according to relay.c */
446 time_t timestamp_recv; /* Cell received from lower layer */
447 time_t timestamp_xmit; /* Cell sent to lower layer */
449 /** Timestamp for run_connection_housekeeping(). We update this once a
450 * second when we run housekeeping and find a circuit on this channel, and
451 * whenever we add a circuit to the channel. */
452 time_t timestamp_last_had_circuits;
454 /** Unique ID for measuring direct network status requests;vtunneled ones
455 * come over a circuit_t, which has a dirreq_id field as well, but is a
456 * distinct namespace. */
457 uint64_t dirreq_id;
459 /** Channel counters for cell channels */
460 uint64_t n_cells_recved, n_bytes_recved;
461 uint64_t n_cells_xmitted, n_bytes_xmitted;
464 struct channel_listener_s {
465 /* Current channel listener state */
466 channel_listener_state_t state;
468 /* Globally unique ID number for a channel over the lifetime of a Tor
469 * process.
471 uint64_t global_identifier;
473 /** Should we expect to see this channel in the channel lists? */
474 unsigned char registered:1;
476 /** Why did we close?
478 enum {
479 CHANNEL_LISTENER_NOT_CLOSING = 0,
480 CHANNEL_LISTENER_CLOSE_REQUESTED,
481 CHANNEL_LISTENER_CLOSE_FROM_BELOW,
482 CHANNEL_LISTENER_CLOSE_FOR_ERROR
483 } reason_for_closing;
485 /** Timestamps for both cell channels and listeners */
486 time_t timestamp_created; /* Channel created */
487 time_t timestamp_active; /* Any activity */
489 /* Methods implemented by the lower layer */
491 /** Free a channel */
492 void (*free_fn)(channel_listener_t *);
493 /** Close an open channel */
494 void (*close)(channel_listener_t *);
495 /** Describe the transport subclass for this channel */
496 const char * (*describe_transport)(channel_listener_t *);
497 /** Optional method to dump transport-specific statistics on the channel */
498 void (*dumpstats)(channel_listener_t *, int);
500 /** Registered listen handler to call on incoming connection */
501 channel_listener_fn_ptr listener;
503 /** List of pending incoming connections */
504 smartlist_t *incoming_list;
506 /** Timestamps for listeners */
507 time_t timestamp_accepted;
509 /** Counters for listeners */
510 uint64_t n_accepted;
513 /* Channel state manipulations */
515 int channel_state_is_valid(channel_state_t state);
516 int channel_listener_state_is_valid(channel_listener_state_t state);
518 int channel_state_can_transition(channel_state_t from, channel_state_t to);
519 int channel_listener_state_can_transition(channel_listener_state_t from,
520 channel_listener_state_t to);
522 const char * channel_state_to_string(channel_state_t state);
523 const char *
524 channel_listener_state_to_string(channel_listener_state_t state);
526 /* Abstract channel operations */
528 void channel_mark_for_close(channel_t *chan);
529 int channel_write_packed_cell(channel_t *chan, packed_cell_t *cell);
531 void channel_listener_mark_for_close(channel_listener_t *chan_l);
533 /* Channel callback registrations */
535 /* Listener callback */
536 void channel_listener_set_listener_fn(channel_listener_t *chan,
537 channel_listener_fn_ptr listener);
539 /* Incoming cell callbacks */
540 channel_cell_handler_fn_ptr channel_get_cell_handler(channel_t *chan);
542 channel_var_cell_handler_fn_ptr
543 channel_get_var_cell_handler(channel_t *chan);
545 void channel_set_cell_handlers(channel_t *chan,
546 channel_cell_handler_fn_ptr cell_handler,
547 channel_var_cell_handler_fn_ptr
548 var_cell_handler);
550 /* Clean up closed channels and channel listeners periodically; these are
551 * called from run_scheduled_events() in main.c.
553 void channel_run_cleanup(void);
554 void channel_listener_run_cleanup(void);
556 /* Close all channels and deallocate everything */
557 void channel_free_all(void);
559 /* Dump some statistics in the log */
560 void channel_dumpstats(int severity);
561 void channel_listener_dumpstats(int severity);
563 #ifdef TOR_CHANNEL_INTERNAL_
565 #ifdef CHANNEL_PRIVATE_
567 STATIC void channel_add_to_digest_map(channel_t *chan);
569 #endif /* defined(CHANNEL_PRIVATE_) */
571 /* Channel operations for subclasses and internal use only */
573 /* Initialize a newly allocated channel - do this first in subclass
574 * constructors.
577 void channel_init(channel_t *chan);
578 void channel_init_listener(channel_listener_t *chan);
580 /* Channel registration/unregistration */
581 void channel_register(channel_t *chan);
582 void channel_unregister(channel_t *chan);
584 /* Channel listener registration/unregistration */
585 void channel_listener_register(channel_listener_t *chan_l);
586 void channel_listener_unregister(channel_listener_t *chan_l);
588 /* Close from below */
589 void channel_close_from_lower_layer(channel_t *chan);
590 void channel_close_for_error(channel_t *chan);
591 void channel_closed(channel_t *chan);
593 /* Free a channel */
594 void channel_free_(channel_t *chan);
595 #define channel_free(chan) FREE_AND_NULL(channel_t, channel_free_, (chan))
596 void channel_listener_free_(channel_listener_t *chan_l);
597 #define channel_listener_free(chan_l) \
598 FREE_AND_NULL(channel_listener_t, channel_listener_free_, (chan_l))
600 /* State/metadata setters */
602 void channel_change_state(channel_t *chan, channel_state_t to_state);
603 void channel_change_state_open(channel_t *chan);
604 void channel_clear_identity_digest(channel_t *chan);
605 void channel_clear_remote_end(channel_t *chan);
606 void channel_mark_local(channel_t *chan);
607 void channel_mark_incoming(channel_t *chan);
608 void channel_mark_outgoing(channel_t *chan);
609 void channel_mark_remote(channel_t *chan);
610 void channel_set_identity_digest(channel_t *chan,
611 const char *identity_digest,
612 const struct ed25519_public_key_t *ed_identity);
614 void channel_listener_change_state(channel_listener_t *chan_l,
615 channel_listener_state_t to_state);
617 /* Timestamp updates */
618 void channel_timestamp_created(channel_t *chan);
619 void channel_timestamp_active(channel_t *chan);
620 void channel_timestamp_recv(channel_t *chan);
621 void channel_timestamp_xmit(channel_t *chan);
623 void channel_listener_timestamp_created(channel_listener_t *chan_l);
624 void channel_listener_timestamp_active(channel_listener_t *chan_l);
625 void channel_listener_timestamp_accepted(channel_listener_t *chan_l);
627 /* Incoming channel handling */
628 void channel_listener_process_incoming(channel_listener_t *listener);
629 void channel_listener_queue_incoming(channel_listener_t *listener,
630 channel_t *incoming);
632 /* Incoming cell handling */
633 void channel_process_cell(channel_t *chan, cell_t *cell);
635 /* Request from lower layer for more cells if available */
636 MOCK_DECL(ssize_t, channel_flush_some_cells,
637 (channel_t *chan, ssize_t num_cells));
639 /* Query if data available on this channel */
640 MOCK_DECL(int, channel_more_to_flush, (channel_t *chan));
642 /* Notify flushed outgoing for dirreq handling */
643 void channel_notify_flushed(channel_t *chan);
645 /* Handle stuff we need to do on open like notifying circuits */
646 void channel_do_open_actions(channel_t *chan);
648 #endif /* defined(TOR_CHANNEL_INTERNAL_) */
650 /* Helper functions to perform operations on channels */
652 int channel_send_destroy(circid_t circ_id, channel_t *chan,
653 int reason);
656 * Outside abstract interfaces that should eventually get turned into
657 * something transport/address format independent.
660 channel_t * channel_connect(const tor_addr_t *addr, uint16_t port,
661 const char *rsa_id_digest,
662 const struct ed25519_public_key_t *ed_id);
664 channel_t * channel_get_for_extend(const char *rsa_id_digest,
665 const struct ed25519_public_key_t *ed_id,
666 const tor_addr_t *target_addr,
667 const char **msg_out,
668 int *launch_out);
670 /* Ask which of two channels is better for circuit-extension purposes */
671 int channel_is_better(channel_t *a, channel_t *b);
673 /** Channel lookups
676 channel_t * channel_find_by_global_id(uint64_t global_identifier);
677 channel_t * channel_find_by_remote_identity(const char *rsa_id_digest,
678 const struct ed25519_public_key_t *ed_id);
680 /** For things returned by channel_find_by_remote_digest(), walk the list.
681 * The RSA key will match for all returned elements; the Ed25519 key might not.
683 channel_t * channel_next_with_rsa_identity(channel_t *chan);
686 * Helper macros to lookup state of given channel.
689 #define CHANNEL_IS_CLOSED(chan) (channel_is_in_state((chan), \
690 CHANNEL_STATE_CLOSED))
691 #define CHANNEL_IS_OPENING(chan) (channel_is_in_state((chan), \
692 CHANNEL_STATE_OPENING))
693 #define CHANNEL_IS_OPEN(chan) (channel_is_in_state((chan), \
694 CHANNEL_STATE_OPEN))
695 #define CHANNEL_IS_MAINT(chan) (channel_is_in_state((chan), \
696 CHANNEL_STATE_MAINT))
697 #define CHANNEL_IS_CLOSING(chan) (channel_is_in_state((chan), \
698 CHANNEL_STATE_CLOSING))
699 #define CHANNEL_IS_ERROR(chan) (channel_is_in_state((chan), \
700 CHANNEL_STATE_ERROR))
702 #define CHANNEL_FINISHED(chan) (CHANNEL_IS_CLOSED(chan) || \
703 CHANNEL_IS_ERROR(chan))
705 #define CHANNEL_CONDEMNED(chan) (CHANNEL_IS_CLOSING(chan) || \
706 CHANNEL_FINISHED(chan))
708 #define CHANNEL_CAN_HANDLE_CELLS(chan) (CHANNEL_IS_OPENING(chan) || \
709 CHANNEL_IS_OPEN(chan) || \
710 CHANNEL_IS_MAINT(chan))
712 static inline int
713 channel_is_in_state(channel_t *chan, channel_state_t state)
715 return chan->state == state;
719 * Metadata queries/updates
722 const char * channel_describe_transport(channel_t *chan);
723 MOCK_DECL(void, channel_dump_statistics, (channel_t *chan, int severity));
724 void channel_dump_transport_statistics(channel_t *chan, int severity);
725 const char * channel_get_actual_remote_descr(channel_t *chan);
726 const char * channel_get_actual_remote_address(channel_t *chan);
727 MOCK_DECL(int, channel_get_addr_if_possible, (channel_t *chan,
728 tor_addr_t *addr_out));
729 const char * channel_get_canonical_remote_descr(channel_t *chan);
730 int channel_has_queued_writes(channel_t *chan);
731 int channel_is_bad_for_new_circs(channel_t *chan);
732 void channel_mark_bad_for_new_circs(channel_t *chan);
733 int channel_is_canonical(channel_t *chan);
734 int channel_is_client(const channel_t *chan);
735 int channel_is_local(channel_t *chan);
736 int channel_is_incoming(channel_t *chan);
737 int channel_is_outgoing(channel_t *chan);
738 void channel_mark_client(channel_t *chan);
739 void channel_clear_client(channel_t *chan);
740 int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info);
741 int channel_remote_identity_matches(const channel_t *chan,
742 const char *rsa_id_digest,
743 const ed25519_public_key_t *ed_id);
744 int channel_matches_target_addr_for_extend(channel_t *chan,
745 const tor_addr_t *target);
746 unsigned int channel_num_circuits(channel_t *chan);
747 MOCK_DECL(void,channel_set_circid_type,(channel_t *chan,
748 crypto_pk_t *identity_rcvd,
749 int consider_identity));
750 void channel_timestamp_client(channel_t *chan);
752 const char * channel_listener_describe_transport(channel_listener_t *chan_l);
753 void channel_listener_dump_statistics(channel_listener_t *chan_l,
754 int severity);
755 void channel_listener_dump_transport_statistics(channel_listener_t *chan_l,
756 int severity);
757 void channel_check_for_duplicates(void);
759 void channel_update_bad_for_new_circs(const char *digest, int force);
761 /* Flow control queries */
762 int channel_num_cells_writeable(channel_t *chan);
764 /* Timestamp queries */
765 time_t channel_when_created(channel_t *chan);
766 time_t channel_when_last_client(channel_t *chan);
767 time_t channel_when_last_xmit(channel_t *chan);
769 /* Counter queries */
770 int packed_cell_is_destroy(channel_t *chan,
771 const packed_cell_t *packed_cell,
772 circid_t *circid_out);
774 /* Declare the handle helpers */
775 HANDLE_DECL(channel, channel_s,)
776 #define channel_handle_free(h) \
777 FREE_AND_NULL(channel_handle_t, channel_handle_free_, (h))
778 #undef tor_timer_t
780 #endif /* !defined(TOR_CHANNEL_H) */