1 /* * Copyright (c) 2012-2013, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief OR-to-OR channel abstraction layer
10 * Define this so channel.h gives us things only channel_t subclasses
14 #define TOR_CHANNEL_INTERNAL_
18 #include "channeltls.h"
19 #include "circuitbuild.h"
20 #include "circuitlist.h"
21 #include "circuitstats.h"
23 #include "connection_or.h" /* For var_cell_free() */
24 #include "circuitmux.h"
25 #include "entrynodes.h"
31 #include "routerlist.h"
33 /* Cell queue structure */
35 typedef struct cell_queue_entry_s cell_queue_entry_t
;
36 struct cell_queue_entry_s
{
37 TOR_SIMPLEQ_ENTRY(cell_queue_entry_s
) next
;
51 packed_cell_t
*packed_cell
;
56 /* Global lists of channels */
58 /* All channel_t instances */
59 static smartlist_t
*all_channels
= NULL
;
61 /* All channel_t instances not in ERROR or CLOSED states */
62 static smartlist_t
*active_channels
= NULL
;
64 /* All channel_t instances in ERROR or CLOSED states */
65 static smartlist_t
*finished_channels
= NULL
;
67 /* All channel_listener_t instances */
68 static smartlist_t
*all_listeners
= NULL
;
70 /* All channel_listener_t instances in LISTENING state */
71 static smartlist_t
*active_listeners
= NULL
;
73 /* All channel_listener_t instances in LISTENING state */
74 static smartlist_t
*finished_listeners
= NULL
;
76 /* Counter for ID numbers */
77 static uint64_t n_channels_allocated
= 0;
79 /* Digest->channel map
81 * Similar to the one used in connection_or.c, this maps from the identity
82 * digest of a remote endpoint to a channel_t to that endpoint. Channels
83 * should be placed here when registered and removed when they close or error.
84 * If more than one channel exists, follow the next_with_same_id pointer
87 HT_HEAD(channel_idmap
, channel_idmap_entry_s
) channel_identity_map
=
90 typedef struct channel_idmap_entry_s
{
91 HT_ENTRY(channel_idmap_entry_s
) node
;
92 uint8_t digest
[DIGEST_LEN
];
93 TOR_LIST_HEAD(channel_list_s
, channel_s
) channel_list
;
94 } channel_idmap_entry_t
;
96 static INLINE
unsigned
97 channel_idmap_hash(const channel_idmap_entry_t
*ent
)
99 return (unsigned) siphash24g(ent
->digest
, DIGEST_LEN
);
103 channel_idmap_eq(const channel_idmap_entry_t
*a
,
104 const channel_idmap_entry_t
*b
)
106 return tor_memeq(a
->digest
, b
->digest
, DIGEST_LEN
);
109 HT_PROTOTYPE(channel_idmap
, channel_idmap_entry_s
, node
, channel_idmap_hash
,
111 HT_GENERATE(channel_idmap
, channel_idmap_entry_s
, node
, channel_idmap_hash
,
112 channel_idmap_eq
, 0.5, tor_malloc
, tor_realloc
, tor_free_
);
114 static cell_queue_entry_t
* cell_queue_entry_dup(cell_queue_entry_t
*q
);
115 static void cell_queue_entry_free(cell_queue_entry_t
*q
, int handed_off
);
117 static int cell_queue_entry_is_padding(cell_queue_entry_t
*q
);
119 static cell_queue_entry_t
*
120 cell_queue_entry_new_fixed(cell_t
*cell
);
121 static cell_queue_entry_t
*
122 cell_queue_entry_new_var(var_cell_t
*var_cell
);
123 static int is_destroy_cell(channel_t
*chan
,
124 const cell_queue_entry_t
*q
, circid_t
*circid_out
);
126 /* Functions to maintain the digest map */
127 static void channel_add_to_digest_map(channel_t
*chan
);
128 static void channel_remove_from_digest_map(channel_t
*chan
);
131 * Flush cells from just the outgoing queue without trying to get them
132 * from circuits; used internall by channel_flush_some_cells().
135 channel_flush_some_cells_from_outgoing_queue(channel_t
*chan
,
137 static void channel_force_free(channel_t
*chan
);
139 channel_free_list(smartlist_t
*channels
, int mark_for_close
);
141 channel_listener_free_list(smartlist_t
*channels
, int mark_for_close
);
142 static void channel_listener_force_free(channel_listener_t
*chan_l
);
144 channel_write_cell_queue_entry(channel_t
*chan
, cell_queue_entry_t
*q
);
146 /***********************************
147 * Channel state utility functions *
148 **********************************/
151 * Indicate whether a given channel state is valid
155 channel_state_is_valid(channel_state_t state
)
160 case CHANNEL_STATE_CLOSED
:
161 case CHANNEL_STATE_CLOSING
:
162 case CHANNEL_STATE_ERROR
:
163 case CHANNEL_STATE_MAINT
:
164 case CHANNEL_STATE_OPENING
:
165 case CHANNEL_STATE_OPEN
:
168 case CHANNEL_STATE_LAST
:
177 * Indicate whether a given channel listener state is valid
181 channel_listener_state_is_valid(channel_listener_state_t state
)
186 case CHANNEL_LISTENER_STATE_CLOSED
:
187 case CHANNEL_LISTENER_STATE_LISTENING
:
188 case CHANNEL_LISTENER_STATE_CLOSING
:
189 case CHANNEL_LISTENER_STATE_ERROR
:
192 case CHANNEL_LISTENER_STATE_LAST
:
201 * Indicate whether a channel state transition is valid
203 * This function takes two channel states and indicates whether a
204 * transition between them is permitted (see the state definitions and
205 * transition table in or.h at the channel_state_t typedef).
209 channel_state_can_transition(channel_state_t from
, channel_state_t to
)
214 case CHANNEL_STATE_CLOSED
:
215 is_valid
= (to
== CHANNEL_STATE_OPENING
);
217 case CHANNEL_STATE_CLOSING
:
218 is_valid
= (to
== CHANNEL_STATE_CLOSED
||
219 to
== CHANNEL_STATE_ERROR
);
221 case CHANNEL_STATE_ERROR
:
224 case CHANNEL_STATE_MAINT
:
225 is_valid
= (to
== CHANNEL_STATE_CLOSING
||
226 to
== CHANNEL_STATE_ERROR
||
227 to
== CHANNEL_STATE_OPEN
);
229 case CHANNEL_STATE_OPENING
:
230 is_valid
= (to
== CHANNEL_STATE_CLOSING
||
231 to
== CHANNEL_STATE_ERROR
||
232 to
== CHANNEL_STATE_OPEN
);
234 case CHANNEL_STATE_OPEN
:
235 is_valid
= (to
== CHANNEL_STATE_CLOSING
||
236 to
== CHANNEL_STATE_ERROR
||
237 to
== CHANNEL_STATE_MAINT
);
239 case CHANNEL_STATE_LAST
:
248 * Indicate whether a channel listener state transition is valid
250 * This function takes two channel listener states and indicates whether a
251 * transition between them is permitted (see the state definitions and
252 * transition table in or.h at the channel_listener_state_t typedef).
256 channel_listener_state_can_transition(channel_listener_state_t from
,
257 channel_listener_state_t to
)
262 case CHANNEL_LISTENER_STATE_CLOSED
:
263 is_valid
= (to
== CHANNEL_LISTENER_STATE_LISTENING
);
265 case CHANNEL_LISTENER_STATE_CLOSING
:
266 is_valid
= (to
== CHANNEL_LISTENER_STATE_CLOSED
||
267 to
== CHANNEL_LISTENER_STATE_ERROR
);
269 case CHANNEL_LISTENER_STATE_ERROR
:
272 case CHANNEL_LISTENER_STATE_LISTENING
:
273 is_valid
= (to
== CHANNEL_LISTENER_STATE_CLOSING
||
274 to
== CHANNEL_LISTENER_STATE_ERROR
);
276 case CHANNEL_LISTENER_STATE_LAST
:
285 * Return a human-readable description for a channel state
289 channel_state_to_string(channel_state_t state
)
294 case CHANNEL_STATE_CLOSED
:
297 case CHANNEL_STATE_CLOSING
:
300 case CHANNEL_STATE_ERROR
:
301 descr
= "channel error";
303 case CHANNEL_STATE_MAINT
:
304 descr
= "temporarily suspended for maintenance";
306 case CHANNEL_STATE_OPENING
:
309 case CHANNEL_STATE_OPEN
:
312 case CHANNEL_STATE_LAST
:
314 descr
= "unknown or invalid channel state";
321 * Return a human-readable description for a channel listenier state
325 channel_listener_state_to_string(channel_listener_state_t state
)
330 case CHANNEL_LISTENER_STATE_CLOSED
:
333 case CHANNEL_LISTENER_STATE_CLOSING
:
336 case CHANNEL_LISTENER_STATE_ERROR
:
337 descr
= "channel listener error";
339 case CHANNEL_LISTENER_STATE_LISTENING
:
342 case CHANNEL_LISTENER_STATE_LAST
:
344 descr
= "unknown or invalid channel listener state";
350 /***************************************
351 * Channel registration/unregistration *
352 ***************************************/
357 * This function registers a newly created channel in the global lists/maps
358 * of active channels.
362 channel_register(channel_t
*chan
)
366 /* No-op if already registered */
367 if (chan
->registered
) return;
369 log_debug(LD_CHANNEL
,
370 "Registering channel %p (ID " U64_FORMAT
") "
371 "in state %s (%d) with digest %s",
372 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
373 channel_state_to_string(chan
->state
), chan
->state
,
374 hex_str(chan
->identity_digest
, DIGEST_LEN
));
376 /* Make sure we have all_channels, then add it */
377 if (!all_channels
) all_channels
= smartlist_new();
378 smartlist_add(all_channels
, chan
);
380 /* Is it finished? */
381 if (chan
->state
== CHANNEL_STATE_CLOSED
||
382 chan
->state
== CHANNEL_STATE_ERROR
) {
383 /* Put it in the finished list, creating it if necessary */
384 if (!finished_channels
) finished_channels
= smartlist_new();
385 smartlist_add(finished_channels
, chan
);
387 /* Put it in the active list, creating it if necessary */
388 if (!active_channels
) active_channels
= smartlist_new();
389 smartlist_add(active_channels
, chan
);
391 if (chan
->state
!= CHANNEL_STATE_CLOSING
) {
392 /* It should have a digest set */
393 if (!tor_digest_is_zero(chan
->identity_digest
)) {
394 /* Yeah, we're good, add it to the map */
395 channel_add_to_digest_map(chan
);
398 "Channel %p (global ID " U64_FORMAT
") "
399 "in state %s (%d) registered with no identity digest",
400 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
401 channel_state_to_string(chan
->state
), chan
->state
);
406 /* Mark it as registered */
407 chan
->registered
= 1;
411 * Unregister a channel
413 * This function removes a channel from the global lists and maps and is used
414 * when freeing a closed/errored channel.
418 channel_unregister(channel_t
*chan
)
422 /* No-op if not registered */
423 if (!(chan
->registered
)) return;
425 /* Is it finished? */
426 if (chan
->state
== CHANNEL_STATE_CLOSED
||
427 chan
->state
== CHANNEL_STATE_ERROR
) {
428 /* Get it out of the finished list */
429 if (finished_channels
) smartlist_remove(finished_channels
, chan
);
431 /* Get it out of the active list */
432 if (active_channels
) smartlist_remove(active_channels
, chan
);
435 /* Get it out of all_channels */
436 if (all_channels
) smartlist_remove(all_channels
, chan
);
438 /* Mark it as unregistered */
439 chan
->registered
= 0;
441 /* Should it be in the digest map? */
442 if (!tor_digest_is_zero(chan
->identity_digest
) &&
443 !(chan
->state
== CHANNEL_STATE_CLOSING
||
444 chan
->state
== CHANNEL_STATE_CLOSED
||
445 chan
->state
== CHANNEL_STATE_ERROR
)) {
447 channel_remove_from_digest_map(chan
);
452 * Register a channel listener
454 * This function registers a newly created channel listner in the global
455 * lists/maps of active channel listeners.
459 channel_listener_register(channel_listener_t
*chan_l
)
463 /* No-op if already registered */
464 if (chan_l
->registered
) return;
466 log_debug(LD_CHANNEL
,
467 "Registering channel listener %p (ID " U64_FORMAT
") "
469 chan_l
, U64_PRINTF_ARG(chan_l
->global_identifier
),
470 channel_listener_state_to_string(chan_l
->state
),
473 /* Make sure we have all_channels, then add it */
474 if (!all_listeners
) all_listeners
= smartlist_new();
475 smartlist_add(all_listeners
, chan_l
);
477 /* Is it finished? */
478 if (chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
479 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
) {
480 /* Put it in the finished list, creating it if necessary */
481 if (!finished_listeners
) finished_listeners
= smartlist_new();
482 smartlist_add(finished_listeners
, chan_l
);
484 /* Put it in the active list, creating it if necessary */
485 if (!active_listeners
) active_listeners
= smartlist_new();
486 smartlist_add(active_listeners
, chan_l
);
489 /* Mark it as registered */
490 chan_l
->registered
= 1;
494 * Unregister a channel listener
496 * This function removes a channel listener from the global lists and maps
497 * and is used when freeing a closed/errored channel listener.
501 channel_listener_unregister(channel_listener_t
*chan_l
)
505 /* No-op if not registered */
506 if (!(chan_l
->registered
)) return;
508 /* Is it finished? */
509 if (chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
510 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
) {
511 /* Get it out of the finished list */
512 if (finished_listeners
) smartlist_remove(finished_listeners
, chan_l
);
514 /* Get it out of the active list */
515 if (active_listeners
) smartlist_remove(active_listeners
, chan_l
);
518 /* Get it out of all_channels */
519 if (all_listeners
) smartlist_remove(all_listeners
, chan_l
);
521 /* Mark it as unregistered */
522 chan_l
->registered
= 0;
525 /*********************************
526 * Channel digest map maintenance
527 *********************************/
530 * Add a channel to the digest map
532 * This function adds a channel to the digest map and inserts it into the
533 * correct linked list if channels with that remote endpoint identity digest
538 channel_add_to_digest_map(channel_t
*chan
)
540 channel_idmap_entry_t
*ent
, search
;
544 /* Assert that the state makes sense */
545 tor_assert(!(chan
->state
== CHANNEL_STATE_CLOSING
||
546 chan
->state
== CHANNEL_STATE_CLOSED
||
547 chan
->state
== CHANNEL_STATE_ERROR
));
549 /* Assert that there is a digest */
550 tor_assert(!tor_digest_is_zero(chan
->identity_digest
));
552 memcpy(search
.digest
, chan
->identity_digest
, DIGEST_LEN
);
553 ent
= HT_FIND(channel_idmap
, &channel_identity_map
, &search
);
555 ent
= tor_malloc(sizeof(channel_idmap_entry_t
));
556 memcpy(ent
->digest
, chan
->identity_digest
, DIGEST_LEN
);
557 TOR_LIST_INIT(&ent
->channel_list
);
558 HT_INSERT(channel_idmap
, &channel_identity_map
, ent
);
560 TOR_LIST_INSERT_HEAD(&ent
->channel_list
, chan
, next_with_same_id
);
562 log_debug(LD_CHANNEL
,
563 "Added channel %p (global ID " U64_FORMAT
") "
564 "to identity map in state %s (%d) with digest %s",
565 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
566 channel_state_to_string(chan
->state
), chan
->state
,
567 hex_str(chan
->identity_digest
, DIGEST_LEN
));
571 * Remove a channel from the digest map
573 * This function removes a channel from the digest map and the linked list of
574 * channels for that digest if more than one exists.
578 channel_remove_from_digest_map(channel_t
*chan
)
580 channel_idmap_entry_t
*ent
, search
;
584 /* Assert that there is a digest */
585 tor_assert(!tor_digest_is_zero(chan
->identity_digest
));
588 /* Make sure we have a map */
589 if (!channel_identity_map
) {
591 * No identity map, so we can't find it by definition. This
592 * case is similar to digestmap_get() failing below.
595 "Trying to remove channel %p (global ID " U64_FORMAT
") "
596 "with digest %s from identity map, but didn't have any identity "
598 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
599 hex_str(chan
->identity_digest
, DIGEST_LEN
));
600 /* Clear out its next/prev pointers */
601 if (chan
->next_with_same_id
) {
602 chan
->next_with_same_id
->prev_with_same_id
= chan
->prev_with_same_id
;
604 if (chan
->prev_with_same_id
) {
605 chan
->prev_with_same_id
->next_with_same_id
= chan
->next_with_same_id
;
607 chan
->next_with_same_id
= NULL
;
608 chan
->prev_with_same_id
= NULL
;
614 /* Pull it out of its list, wherever that list is */
615 TOR_LIST_REMOVE(chan
, next_with_same_id
);
617 memcpy(search
.digest
, chan
->identity_digest
, DIGEST_LEN
);
618 ent
= HT_FIND(channel_idmap
, &channel_identity_map
, &search
);
620 /* Look for it in the map */
622 /* Okay, it's here */
624 if (TOR_LIST_EMPTY(&ent
->channel_list
)) {
625 HT_REMOVE(channel_idmap
, &channel_identity_map
, ent
);
629 log_debug(LD_CHANNEL
,
630 "Removed channel %p (global ID " U64_FORMAT
") from "
631 "identity map in state %s (%d) with digest %s",
632 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
633 channel_state_to_string(chan
->state
), chan
->state
,
634 hex_str(chan
->identity_digest
, DIGEST_LEN
));
636 /* Shouldn't happen */
638 "Trying to remove channel %p (global ID " U64_FORMAT
") with "
639 "digest %s from identity map, but couldn't find any with "
641 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
642 hex_str(chan
->identity_digest
, DIGEST_LEN
));
646 /****************************
647 * Channel lookup functions *
648 ***************************/
651 * Find channel by global ID
653 * This function searches for a channel by the global_identifier assigned
654 * at initialization time. This identifier is unique for the lifetime of the
659 channel_find_by_global_id(uint64_t global_identifier
)
661 channel_t
*rv
= NULL
;
663 if (all_channels
&& smartlist_len(all_channels
) > 0) {
664 SMARTLIST_FOREACH_BEGIN(all_channels
, channel_t
*, curr
) {
665 if (curr
->global_identifier
== global_identifier
) {
669 } SMARTLIST_FOREACH_END(curr
);
676 * Find channel by digest of the remote endpoint
678 * This function looks up a channel by the digest of its remote endpoint in
679 * the channel digest map. It's possible that more than one channel to a
680 * given endpoint exists. Use channel_next_with_digest() to walk the list.
684 channel_find_by_remote_digest(const char *identity_digest
)
686 channel_t
*rv
= NULL
;
687 channel_idmap_entry_t
*ent
, search
;
689 tor_assert(identity_digest
);
691 memcpy(search
.digest
, identity_digest
, DIGEST_LEN
);
692 ent
= HT_FIND(channel_idmap
, &channel_identity_map
, &search
);
694 rv
= TOR_LIST_FIRST(&ent
->channel_list
);
701 * Get next channel with digest
703 * This function takes a channel and finds the next channel in the list
704 * with the same digest.
708 channel_next_with_digest(channel_t
*chan
)
712 return TOR_LIST_NEXT(chan
, next_with_same_id
);
716 * Initialize a channel
718 * This function should be called by subclasses to set up some per-channel
719 * variables. I.e., this is the superclass constructor. Before this, the
720 * channel should be allocated with tor_malloc_zero().
724 channel_init(channel_t
*chan
)
728 /* Assign an ID and bump the counter */
729 chan
->global_identifier
= n_channels_allocated
++;
732 chan
->timestamp_last_had_circuits
= time(NULL
);
734 /* Warn about exhausted circuit IDs no more than hourly. */
735 chan
->last_warned_circ_ids_exhausted
.rate
= 3600;
737 /* Initialize queues. */
738 TOR_SIMPLEQ_INIT(&chan
->incoming_queue
);
739 TOR_SIMPLEQ_INIT(&chan
->outgoing_queue
);
741 /* Initialize list entries. */
742 memset(&chan
->next_with_same_id
, 0, sizeof(chan
->next_with_same_id
));
745 channel_timestamp_created(chan
);
747 /* It hasn't been open yet. */
748 chan
->has_been_open
= 0;
752 * Initialize a channel listener
754 * This function should be called by subclasses to set up some per-channel
755 * variables. I.e., this is the superclass constructor. Before this, the
756 * channel listener should be allocated with tor_malloc_zero().
760 channel_init_listener(channel_listener_t
*chan_l
)
764 /* Assign an ID and bump the counter */
765 chan_l
->global_identifier
= n_channels_allocated
++;
768 channel_listener_timestamp_created(chan_l
);
772 * Free a channel; nothing outside of channel.c and subclasses should call
773 * this - it frees channels after they have closed and been unregistered.
777 channel_free(channel_t
*chan
)
781 /* It must be closed or errored */
782 tor_assert(chan
->state
== CHANNEL_STATE_CLOSED
||
783 chan
->state
== CHANNEL_STATE_ERROR
);
784 /* It must be deregistered */
785 tor_assert(!(chan
->registered
));
787 log_debug(LD_CHANNEL
,
788 "Freeing channel " U64_FORMAT
" at %p",
789 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
792 * Get rid of cmux policy before we do anything, so cmux policies don't
793 * see channels in weird half-freed states.
796 circuitmux_set_policy(chan
->cmux
, NULL
);
799 /* Call a free method if there is one */
800 if (chan
->free
) chan
->free(chan
);
802 channel_clear_remote_end(chan
);
804 /* Get rid of cmux */
806 circuitmux_detach_all_circuits(chan
->cmux
, NULL
);
807 circuitmux_mark_destroyed_circids_usable(chan
->cmux
, chan
);
808 circuitmux_free(chan
->cmux
);
812 /* We're in CLOSED or ERROR, so the cell queue is already empty */
818 * Free a channel listener; nothing outside of channel.c and subclasses
819 * should call this - it frees channel listeners after they have closed and
824 channel_listener_free(channel_listener_t
*chan_l
)
828 log_debug(LD_CHANNEL
,
829 "Freeing channel_listener_t " U64_FORMAT
" at %p",
830 U64_PRINTF_ARG(chan_l
->global_identifier
),
833 /* It must be closed or errored */
834 tor_assert(chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
835 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
);
836 /* It must be deregistered */
837 tor_assert(!(chan_l
->registered
));
839 /* Call a free method if there is one */
840 if (chan_l
->free
) chan_l
->free(chan_l
);
843 * We're in CLOSED or ERROR, so the incoming channel queue is already
851 * Free a channel and skip the state/registration asserts; this internal-
852 * use-only function should be called only from channel_free_all() when
853 * shutting down the Tor process.
857 channel_force_free(channel_t
*chan
)
859 cell_queue_entry_t
*cell
, *cell_tmp
;
862 log_debug(LD_CHANNEL
,
863 "Force-freeing channel " U64_FORMAT
" at %p",
864 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
867 * Get rid of cmux policy before we do anything, so cmux policies don't
868 * see channels in weird half-freed states.
871 circuitmux_set_policy(chan
->cmux
, NULL
);
874 /* Call a free method if there is one */
875 if (chan
->free
) chan
->free(chan
);
877 channel_clear_remote_end(chan
);
879 /* Get rid of cmux */
881 circuitmux_free(chan
->cmux
);
885 /* We might still have a cell queue; kill it */
886 TOR_SIMPLEQ_FOREACH_SAFE(cell
, &chan
->incoming_queue
, next
, cell_tmp
) {
887 cell_queue_entry_free(cell
, 0);
889 TOR_SIMPLEQ_INIT(&chan
->incoming_queue
);
891 /* Outgoing cell queue is similar, but we can have to free packed cells */
892 TOR_SIMPLEQ_FOREACH_SAFE(cell
, &chan
->outgoing_queue
, next
, cell_tmp
) {
893 cell_queue_entry_free(cell
, 0);
895 TOR_SIMPLEQ_INIT(&chan
->outgoing_queue
);
901 * Free a channel listener and skip the state/reigstration asserts; this
902 * internal-use-only function should be called only from channel_free_all()
903 * when shutting down the Tor process.
907 channel_listener_force_free(channel_listener_t
*chan_l
)
911 log_debug(LD_CHANNEL
,
912 "Force-freeing channel_listener_t " U64_FORMAT
" at %p",
913 U64_PRINTF_ARG(chan_l
->global_identifier
),
916 /* Call a free method if there is one */
917 if (chan_l
->free
) chan_l
->free(chan_l
);
920 * The incoming list just gets emptied and freed; we request close on
921 * any channels we find there, but since we got called while shutting
922 * down they will get deregistered and freed elsewhere anyway.
924 if (chan_l
->incoming_list
) {
925 SMARTLIST_FOREACH_BEGIN(chan_l
->incoming_list
,
926 channel_t
*, qchan
) {
927 channel_mark_for_close(qchan
);
928 } SMARTLIST_FOREACH_END(qchan
);
930 smartlist_free(chan_l
->incoming_list
);
931 chan_l
->incoming_list
= NULL
;
938 * Return the current registered listener for a channel listener
940 * This function returns a function pointer to the current registered
941 * handler for new incoming channels on a channel listener.
944 channel_listener_fn_ptr
945 channel_listener_get_listener_fn(channel_listener_t
*chan_l
)
949 if (chan_l
->state
== CHANNEL_LISTENER_STATE_LISTENING
)
950 return chan_l
->listener
;
956 * Set the listener for a channel listener
958 * This function sets the handler for new incoming channels on a channel
963 channel_listener_set_listener_fn(channel_listener_t
*chan_l
,
964 channel_listener_fn_ptr listener
)
967 tor_assert(chan_l
->state
== CHANNEL_LISTENER_STATE_LISTENING
);
969 log_debug(LD_CHANNEL
,
970 "Setting listener callback for channel listener %p "
971 "(global ID " U64_FORMAT
") to %p",
972 chan_l
, U64_PRINTF_ARG(chan_l
->global_identifier
),
975 chan_l
->listener
= listener
;
976 if (chan_l
->listener
) channel_listener_process_incoming(chan_l
);
980 * Return the fixed-length cell handler for a channel
982 * This function gets the handler for incoming fixed-length cells installed
986 channel_cell_handler_fn_ptr
987 channel_get_cell_handler(channel_t
*chan
)
991 if (chan
->state
== CHANNEL_STATE_OPENING
||
992 chan
->state
== CHANNEL_STATE_OPEN
||
993 chan
->state
== CHANNEL_STATE_MAINT
)
994 return chan
->cell_handler
;
1000 * Return the variable-length cell handler for a channel
1002 * This function gets the handler for incoming variable-length cells
1003 * installed on a channel.
1006 channel_var_cell_handler_fn_ptr
1007 channel_get_var_cell_handler(channel_t
*chan
)
1011 if (chan
->state
== CHANNEL_STATE_OPENING
||
1012 chan
->state
== CHANNEL_STATE_OPEN
||
1013 chan
->state
== CHANNEL_STATE_MAINT
)
1014 return chan
->var_cell_handler
;
1020 * Set both cell handlers for a channel
1022 * This function sets both the fixed-length and variable length cell handlers
1023 * for a channel and processes any incoming cells that had been blocked in the
1024 * queue because none were available.
1028 channel_set_cell_handlers(channel_t
*chan
,
1029 channel_cell_handler_fn_ptr cell_handler
,
1030 channel_var_cell_handler_fn_ptr
1036 tor_assert(chan
->state
== CHANNEL_STATE_OPENING
||
1037 chan
->state
== CHANNEL_STATE_OPEN
||
1038 chan
->state
== CHANNEL_STATE_MAINT
);
1040 log_debug(LD_CHANNEL
,
1041 "Setting cell_handler callback for channel %p to %p",
1042 chan
, cell_handler
);
1043 log_debug(LD_CHANNEL
,
1044 "Setting var_cell_handler callback for channel %p to %p",
1045 chan
, var_cell_handler
);
1047 /* Should we try the queue? */
1049 cell_handler
!= chan
->cell_handler
) try_again
= 1;
1050 if (var_cell_handler
&&
1051 var_cell_handler
!= chan
->var_cell_handler
) try_again
= 1;
1054 chan
->cell_handler
= cell_handler
;
1055 chan
->var_cell_handler
= var_cell_handler
;
1057 /* Re-run the queue if we have one and there's any reason to */
1058 if (! TOR_SIMPLEQ_EMPTY(&chan
->incoming_queue
) &&
1060 (chan
->cell_handler
||
1061 chan
->var_cell_handler
)) channel_process_cells(chan
);
1065 * On closing channels
1067 * There are three functions that close channels, for use in
1068 * different circumstances:
1070 * - Use channel_mark_for_close() for most cases
1071 * - Use channel_close_from_lower_layer() if you are connection_or.c
1072 * and the other end closes the underlying connection.
1073 * - Use channel_close_for_error() if you are connection_or.c and
1074 * some sort of error has occurred.
1078 * Mark a channel for closure
1080 * This function tries to close a channel_t; it will go into the CLOSING
1081 * state, and eventually the lower layer should put it into the CLOSED or
1082 * ERROR state. Then, channel_run_cleanup() will eventually free it.
1086 channel_mark_for_close(channel_t
*chan
)
1088 tor_assert(chan
!= NULL
);
1089 tor_assert(chan
->close
!= NULL
);
1091 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1092 if (chan
->state
== CHANNEL_STATE_CLOSING
||
1093 chan
->state
== CHANNEL_STATE_CLOSED
||
1094 chan
->state
== CHANNEL_STATE_ERROR
) return;
1096 log_debug(LD_CHANNEL
,
1097 "Closing channel %p (global ID " U64_FORMAT
") "
1099 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
1101 /* Note closing by request from above */
1102 chan
->reason_for_closing
= CHANNEL_CLOSE_REQUESTED
;
1104 /* Change state to CLOSING */
1105 channel_change_state(chan
, CHANNEL_STATE_CLOSING
);
1107 /* Tell the lower layer */
1111 * It's up to the lower layer to change state to CLOSED or ERROR when we're
1112 * ready; we'll try to free channels that are in the finished list from
1113 * channel_run_cleanup(). The lower layer should do this by calling
1119 * Mark a channel listener for closure
1121 * This function tries to close a channel_listener_t; it will go into the
1122 * CLOSING state, and eventually the lower layer should put it into the CLOSED
1123 * or ERROR state. Then, channel_run_cleanup() will eventually free it.
1127 channel_listener_mark_for_close(channel_listener_t
*chan_l
)
1129 tor_assert(chan_l
!= NULL
);
1130 tor_assert(chan_l
->close
!= NULL
);
1132 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1133 if (chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
1134 chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
1135 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
) return;
1137 log_debug(LD_CHANNEL
,
1138 "Closing channel listener %p (global ID " U64_FORMAT
") "
1140 chan_l
, U64_PRINTF_ARG(chan_l
->global_identifier
));
1142 /* Note closing by request from above */
1143 chan_l
->reason_for_closing
= CHANNEL_LISTENER_CLOSE_REQUESTED
;
1145 /* Change state to CLOSING */
1146 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSING
);
1148 /* Tell the lower layer */
1149 chan_l
->close(chan_l
);
1152 * It's up to the lower layer to change state to CLOSED or ERROR when we're
1153 * ready; we'll try to free channels that are in the finished list from
1154 * channel_run_cleanup(). The lower layer should do this by calling
1155 * channel_listener_closed().
1160 * Close a channel from the lower layer
1162 * Notify the channel code that the channel is being closed due to a non-error
1163 * condition in the lower layer. This does not call the close() method, since
1164 * the lower layer already knows.
1168 channel_close_from_lower_layer(channel_t
*chan
)
1170 tor_assert(chan
!= NULL
);
1172 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1173 if (chan
->state
== CHANNEL_STATE_CLOSING
||
1174 chan
->state
== CHANNEL_STATE_CLOSED
||
1175 chan
->state
== CHANNEL_STATE_ERROR
) return;
1177 log_debug(LD_CHANNEL
,
1178 "Closing channel %p (global ID " U64_FORMAT
") "
1179 "due to lower-layer event",
1180 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
1182 /* Note closing by event from below */
1183 chan
->reason_for_closing
= CHANNEL_CLOSE_FROM_BELOW
;
1185 /* Change state to CLOSING */
1186 channel_change_state(chan
, CHANNEL_STATE_CLOSING
);
1190 * Close a channel listener from the lower layer
1192 * Notify the channel code that the channel listener is being closed due to a
1193 * non-error condition in the lower layer. This does not call the close()
1194 * method, since the lower layer already knows.
1198 channel_listener_close_from_lower_layer(channel_listener_t
*chan_l
)
1200 tor_assert(chan_l
!= NULL
);
1202 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1203 if (chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
1204 chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
1205 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
) return;
1207 log_debug(LD_CHANNEL
,
1208 "Closing channel listener %p (global ID " U64_FORMAT
") "
1209 "due to lower-layer event",
1210 chan_l
, U64_PRINTF_ARG(chan_l
->global_identifier
));
1212 /* Note closing by event from below */
1213 chan_l
->reason_for_closing
= CHANNEL_LISTENER_CLOSE_FROM_BELOW
;
1215 /* Change state to CLOSING */
1216 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSING
);
1220 * Notify that the channel is being closed due to an error condition
1222 * This function is called by the lower layer implementing the transport
1223 * when a channel must be closed due to an error condition. This does not
1224 * call the channel's close method, since the lower layer already knows.
1228 channel_close_for_error(channel_t
*chan
)
1230 tor_assert(chan
!= NULL
);
1232 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1233 if (chan
->state
== CHANNEL_STATE_CLOSING
||
1234 chan
->state
== CHANNEL_STATE_CLOSED
||
1235 chan
->state
== CHANNEL_STATE_ERROR
) return;
1237 log_debug(LD_CHANNEL
,
1238 "Closing channel %p due to lower-layer error",
1241 /* Note closing by event from below */
1242 chan
->reason_for_closing
= CHANNEL_CLOSE_FOR_ERROR
;
1244 /* Change state to CLOSING */
1245 channel_change_state(chan
, CHANNEL_STATE_CLOSING
);
1249 * Notify that the channel listener is being closed due to an error condition
1251 * This function is called by the lower layer implementing the transport
1252 * when a channel listener must be closed due to an error condition. This
1253 * does not call the channel listener's close method, since the lower layer
1258 channel_listener_close_for_error(channel_listener_t
*chan_l
)
1260 tor_assert(chan_l
!= NULL
);
1262 /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1263 if (chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
1264 chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
1265 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
) return;
1267 log_debug(LD_CHANNEL
,
1268 "Closing channel listener %p (global ID " U64_FORMAT
") "
1269 "due to lower-layer error",
1270 chan_l
, U64_PRINTF_ARG(chan_l
->global_identifier
));
1272 /* Note closing by event from below */
1273 chan_l
->reason_for_closing
= CHANNEL_LISTENER_CLOSE_FOR_ERROR
;
1275 /* Change state to CLOSING */
1276 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSING
);
1280 * Notify that the lower layer is finished closing the channel
1282 * This function should be called by the lower layer when a channel
1283 * is finished closing and it should be regarded as inactive and
1284 * freed by the channel code.
1288 channel_closed(channel_t
*chan
)
1291 tor_assert(chan
->state
== CHANNEL_STATE_CLOSING
||
1292 chan
->state
== CHANNEL_STATE_CLOSED
||
1293 chan
->state
== CHANNEL_STATE_ERROR
);
1295 /* No-op if already inactive */
1296 if (chan
->state
== CHANNEL_STATE_CLOSED
||
1297 chan
->state
== CHANNEL_STATE_ERROR
) return;
1299 /* Inform any pending (not attached) circs that they should
1301 if (! chan
->has_been_open
)
1302 circuit_n_chan_done(chan
, 0);
1304 /* Now close all the attached circuits on it. */
1305 circuit_unlink_all_from_channel(chan
, END_CIRC_REASON_CHANNEL_CLOSED
);
1307 if (chan
->reason_for_closing
!= CHANNEL_CLOSE_FOR_ERROR
) {
1308 channel_change_state(chan
, CHANNEL_STATE_CLOSED
);
1310 channel_change_state(chan
, CHANNEL_STATE_ERROR
);
1315 * Notify that the lower layer is finished closing the channel listener
1317 * This function should be called by the lower layer when a channel listener
1318 * is finished closing and it should be regarded as inactive and
1319 * freed by the channel code.
1323 channel_listener_closed(channel_listener_t
*chan_l
)
1326 tor_assert(chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
1327 chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
1328 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
);
1330 /* No-op if already inactive */
1331 if (chan_l
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
1332 chan_l
->state
== CHANNEL_LISTENER_STATE_ERROR
) return;
1334 if (chan_l
->reason_for_closing
!= CHANNEL_LISTENER_CLOSE_FOR_ERROR
) {
1335 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_CLOSED
);
1337 channel_listener_change_state(chan_l
, CHANNEL_LISTENER_STATE_ERROR
);
1342 * Clear the identity_digest of a channel
1344 * This function clears the identity digest of the remote endpoint for a
1345 * channel; this is intended for use by the lower layer.
1349 channel_clear_identity_digest(channel_t
*chan
)
1351 int state_not_in_map
;
1355 log_debug(LD_CHANNEL
,
1356 "Clearing remote endpoint digest on channel %p with "
1357 "global ID " U64_FORMAT
,
1358 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
1361 (chan
->state
== CHANNEL_STATE_CLOSING
||
1362 chan
->state
== CHANNEL_STATE_CLOSED
||
1363 chan
->state
== CHANNEL_STATE_ERROR
);
1365 if (!state_not_in_map
&& chan
->registered
&&
1366 !tor_digest_is_zero(chan
->identity_digest
))
1367 /* if it's registered get it out of the digest map */
1368 channel_remove_from_digest_map(chan
);
1370 memset(chan
->identity_digest
, 0,
1371 sizeof(chan
->identity_digest
));
1375 * Set the identity_digest of a channel
1377 * This function sets the identity digest of the remote endpoint for a
1378 * channel; this is intended for use by the lower layer.
1382 channel_set_identity_digest(channel_t
*chan
,
1383 const char *identity_digest
)
1385 int was_in_digest_map
, should_be_in_digest_map
, state_not_in_map
;
1389 log_debug(LD_CHANNEL
,
1390 "Setting remote endpoint digest on channel %p with "
1391 "global ID " U64_FORMAT
" to digest %s",
1392 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
1394 hex_str(identity_digest
, DIGEST_LEN
) : "(null)");
1397 (chan
->state
== CHANNEL_STATE_CLOSING
||
1398 chan
->state
== CHANNEL_STATE_CLOSED
||
1399 chan
->state
== CHANNEL_STATE_ERROR
);
1401 !state_not_in_map
&&
1403 !tor_digest_is_zero(chan
->identity_digest
);
1404 should_be_in_digest_map
=
1405 !state_not_in_map
&&
1408 !tor_digest_is_zero(identity_digest
));
1410 if (was_in_digest_map
)
1411 /* We should always remove it; we'll add it back if we're writing
1414 channel_remove_from_digest_map(chan
);
1416 if (identity_digest
) {
1417 memcpy(chan
->identity_digest
,
1419 sizeof(chan
->identity_digest
));
1421 memset(chan
->identity_digest
, 0,
1422 sizeof(chan
->identity_digest
));
1425 /* Put it in the digest map if we should */
1426 if (should_be_in_digest_map
)
1427 channel_add_to_digest_map(chan
);
1431 * Clear the remote end metadata (identity_digest/nickname) of a channel
1433 * This function clears all the remote end info from a channel; this is
1434 * intended for use by the lower layer.
1438 channel_clear_remote_end(channel_t
*chan
)
1440 int state_not_in_map
;
1444 log_debug(LD_CHANNEL
,
1445 "Clearing remote endpoint identity on channel %p with "
1446 "global ID " U64_FORMAT
,
1447 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
1450 (chan
->state
== CHANNEL_STATE_CLOSING
||
1451 chan
->state
== CHANNEL_STATE_CLOSED
||
1452 chan
->state
== CHANNEL_STATE_ERROR
);
1454 if (!state_not_in_map
&& chan
->registered
&&
1455 !tor_digest_is_zero(chan
->identity_digest
))
1456 /* if it's registered get it out of the digest map */
1457 channel_remove_from_digest_map(chan
);
1459 memset(chan
->identity_digest
, 0,
1460 sizeof(chan
->identity_digest
));
1461 tor_free(chan
->nickname
);
1465 * Set the remote end metadata (identity_digest/nickname) of a channel
1467 * This function sets new remote end info on a channel; this is intended
1468 * for use by the lower layer.
1472 channel_set_remote_end(channel_t
*chan
,
1473 const char *identity_digest
,
1474 const char *nickname
)
1476 int was_in_digest_map
, should_be_in_digest_map
, state_not_in_map
;
1480 log_debug(LD_CHANNEL
,
1481 "Setting remote endpoint identity on channel %p with "
1482 "global ID " U64_FORMAT
" to nickname %s, digest %s",
1483 chan
, U64_PRINTF_ARG(chan
->global_identifier
),
1484 nickname
? nickname
: "(null)",
1486 hex_str(identity_digest
, DIGEST_LEN
) : "(null)");
1489 (chan
->state
== CHANNEL_STATE_CLOSING
||
1490 chan
->state
== CHANNEL_STATE_CLOSED
||
1491 chan
->state
== CHANNEL_STATE_ERROR
);
1493 !state_not_in_map
&&
1495 !tor_digest_is_zero(chan
->identity_digest
);
1496 should_be_in_digest_map
=
1497 !state_not_in_map
&&
1500 !tor_digest_is_zero(identity_digest
));
1502 if (was_in_digest_map
)
1503 /* We should always remove it; we'll add it back if we're writing
1506 channel_remove_from_digest_map(chan
);
1508 if (identity_digest
) {
1509 memcpy(chan
->identity_digest
,
1511 sizeof(chan
->identity_digest
));
1514 memset(chan
->identity_digest
, 0,
1515 sizeof(chan
->identity_digest
));
1518 tor_free(chan
->nickname
);
1520 chan
->nickname
= tor_strdup(nickname
);
1522 /* Put it in the digest map if we should */
1523 if (should_be_in_digest_map
)
1524 channel_add_to_digest_map(chan
);
1528 * Duplicate a cell queue entry; this is a shallow copy intended for use
1529 * in channel_write_cell_queue_entry().
1532 static cell_queue_entry_t
*
1533 cell_queue_entry_dup(cell_queue_entry_t
*q
)
1535 cell_queue_entry_t
*rv
= NULL
;
1539 rv
= tor_malloc(sizeof(*rv
));
1540 memcpy(rv
, q
, sizeof(*rv
));
1546 * Free a cell_queue_entry_t; the handed_off parameter indicates whether
1547 * the contents were passed to the lower layer (it is responsible for
1548 * them) or not (we should free).
1552 cell_queue_entry_free(cell_queue_entry_t
*q
, int handed_off
)
1558 * If we handed it off, the recipient becomes responsible (or
1559 * with packed cells the channel_t subclass calls packed_cell
1560 * free after writing out its contents; see, e.g.,
1561 * channel_tls_write_packed_cell_method(). Otherwise, we have
1562 * to take care of it here if possible.
1565 case CELL_QUEUE_FIXED
:
1566 if (q
->u
.fixed
.cell
) {
1568 * There doesn't seem to be a cell_free() function anywhere in the
1569 * pre-channel code; just use tor_free()
1571 tor_free(q
->u
.fixed
.cell
);
1574 case CELL_QUEUE_PACKED
:
1575 if (q
->u
.packed
.packed_cell
) {
1576 packed_cell_free(q
->u
.packed
.packed_cell
);
1579 case CELL_QUEUE_VAR
:
1580 if (q
->u
.var
.var_cell
) {
1582 * This one's in connection_or.c; it'd be nice to figure out the
1583 * whole flow of cells from one end to the other and factor the
1584 * cell memory management functions like this out of the specific
1587 var_cell_free(q
->u
.var
.var_cell
);
1592 * Nothing we can do if we don't know the type; this will
1593 * have been warned about elsewhere.
1603 * Check whether a cell queue entry is padding; this is a helper function
1604 * for channel_write_cell_queue_entry()
1608 cell_queue_entry_is_padding(cell_queue_entry_t
*q
)
1612 if (q
->type
== CELL_QUEUE_FIXED
) {
1613 if (q
->u
.fixed
.cell
) {
1614 if (q
->u
.fixed
.cell
->command
== CELL_PADDING
||
1615 q
->u
.fixed
.cell
->command
== CELL_VPADDING
) {
1619 } else if (q
->type
== CELL_QUEUE_VAR
) {
1620 if (q
->u
.var
.var_cell
) {
1621 if (q
->u
.var
.var_cell
->command
== CELL_PADDING
||
1622 q
->u
.var
.var_cell
->command
== CELL_VPADDING
) {
1633 * Allocate a new cell queue entry for a fixed-size cell
1636 static cell_queue_entry_t
*
1637 cell_queue_entry_new_fixed(cell_t
*cell
)
1639 cell_queue_entry_t
*q
= NULL
;
1643 q
= tor_malloc(sizeof(*q
));
1644 q
->type
= CELL_QUEUE_FIXED
;
1645 q
->u
.fixed
.cell
= cell
;
1651 * Allocate a new cell queue entry for a variable-size cell
1654 static cell_queue_entry_t
*
1655 cell_queue_entry_new_var(var_cell_t
*var_cell
)
1657 cell_queue_entry_t
*q
= NULL
;
1659 tor_assert(var_cell
);
1661 q
= tor_malloc(sizeof(*q
));
1662 q
->type
= CELL_QUEUE_VAR
;
1663 q
->u
.var
.var_cell
= var_cell
;
1669 * Write to a channel based on a cell_queue_entry_t
1671 * Given a cell_queue_entry_t filled out by the caller, try to send the cell
1672 * and queue it if we can't.
1676 channel_write_cell_queue_entry(channel_t
*chan
, cell_queue_entry_t
*q
)
1678 int result
= 0, sent
= 0;
1679 cell_queue_entry_t
*tmp
= NULL
;
1684 /* Assert that the state makes sense for a cell write */
1685 tor_assert(chan
->state
== CHANNEL_STATE_OPENING
||
1686 chan
->state
== CHANNEL_STATE_OPEN
||
1687 chan
->state
== CHANNEL_STATE_MAINT
);
1691 if (is_destroy_cell(chan
, q
, &circ_id
)) {
1692 channel_note_destroy_not_pending(chan
, circ_id
);
1696 /* Can we send it right out? If so, try */
1697 if (TOR_SIMPLEQ_EMPTY(&chan
->outgoing_queue
) &&
1698 chan
->state
== CHANNEL_STATE_OPEN
) {
1699 /* Pick the right write function for this cell type and save the result */
1701 case CELL_QUEUE_FIXED
:
1702 tor_assert(chan
->write_cell
);
1703 tor_assert(q
->u
.fixed
.cell
);
1704 result
= chan
->write_cell(chan
, q
->u
.fixed
.cell
);
1706 case CELL_QUEUE_PACKED
:
1707 tor_assert(chan
->write_packed_cell
);
1708 tor_assert(q
->u
.packed
.packed_cell
);
1709 result
= chan
->write_packed_cell(chan
, q
->u
.packed
.packed_cell
);
1711 case CELL_QUEUE_VAR
:
1712 tor_assert(chan
->write_var_cell
);
1713 tor_assert(q
->u
.var
.var_cell
);
1714 result
= chan
->write_var_cell(chan
, q
->u
.var
.var_cell
);
1720 /* Check if we got it out */
1723 /* Timestamp for transmission */
1724 channel_timestamp_xmit(chan
);
1725 /* If we're here the queue is empty, so it's drained too */
1726 channel_timestamp_drained(chan
);
1727 /* Update the counter */
1728 ++(chan
->n_cells_xmitted
);
1733 /* Not sent, queue it */
1735 * We have to copy the queue entry passed in, since the caller probably
1738 tmp
= cell_queue_entry_dup(q
);
1739 TOR_SIMPLEQ_INSERT_TAIL(&chan
->outgoing_queue
, tmp
, next
);
1740 /* Try to process the queue? */
1741 if (chan
->state
== CHANNEL_STATE_OPEN
) channel_flush_cells(chan
);
1746 * Write a cell to a channel
1748 * Write a fixed-length cell to a channel using the write_cell() method.
1749 * This is equivalent to the pre-channels connection_or_write_cell_to_buf();
1750 * it is called by the transport-independent code to deliver a cell to a
1751 * channel for transmission.
1755 channel_write_cell(channel_t
*chan
, cell_t
*cell
)
1757 cell_queue_entry_t q
;
1762 if (chan
->state
== CHANNEL_STATE_CLOSING
) {
1763 log_debug(LD_CHANNEL
, "Discarding cell_t %p on closing channel %p with "
1764 "global ID "U64_FORMAT
, cell
, chan
,
1765 U64_PRINTF_ARG(chan
->global_identifier
));
1770 log_debug(LD_CHANNEL
,
1771 "Writing cell_t %p to channel %p with global ID "
1773 cell
, chan
, U64_PRINTF_ARG(chan
->global_identifier
));
1775 q
.type
= CELL_QUEUE_FIXED
;
1776 q
.u
.fixed
.cell
= cell
;
1777 channel_write_cell_queue_entry(chan
, &q
);
1781 * Write a packed cell to a channel
1783 * Write a packed cell to a channel using the write_cell() method. This is
1784 * called by the transport-independent code to deliver a packed cell to a
1785 * channel for transmission.
1789 channel_write_packed_cell(channel_t
*chan
, packed_cell_t
*packed_cell
)
1791 cell_queue_entry_t q
;
1794 tor_assert(packed_cell
);
1796 if (chan
->state
== CHANNEL_STATE_CLOSING
) {
1797 log_debug(LD_CHANNEL
, "Discarding packed_cell_t %p on closing channel %p "
1798 "with global ID "U64_FORMAT
, packed_cell
, chan
,
1799 U64_PRINTF_ARG(chan
->global_identifier
));
1800 packed_cell_free(packed_cell
);
1804 log_debug(LD_CHANNEL
,
1805 "Writing packed_cell_t %p to channel %p with global ID "
1808 U64_PRINTF_ARG(chan
->global_identifier
));
1810 q
.type
= CELL_QUEUE_PACKED
;
1811 q
.u
.packed
.packed_cell
= packed_cell
;
1812 channel_write_cell_queue_entry(chan
, &q
);
1816 * Write a variable-length cell to a channel
1818 * Write a variable-length cell to a channel using the write_cell() method.
1819 * This is equivalent to the pre-channels
1820 * connection_or_write_var_cell_to_buf(); it's called by the transport-
1821 * independent code to deliver a var_cell to a channel for transmission.
1825 channel_write_var_cell(channel_t
*chan
, var_cell_t
*var_cell
)
1827 cell_queue_entry_t q
;
1830 tor_assert(var_cell
);
1832 if (chan
->state
== CHANNEL_STATE_CLOSING
) {
1833 log_debug(LD_CHANNEL
, "Discarding var_cell_t %p on closing channel %p "
1834 "with global ID "U64_FORMAT
, var_cell
, chan
,
1835 U64_PRINTF_ARG(chan
->global_identifier
));
1836 var_cell_free(var_cell
);
1840 log_debug(LD_CHANNEL
,
1841 "Writing var_cell_t %p to channel %p with global ID "
1844 U64_PRINTF_ARG(chan
->global_identifier
));
1846 q
.type
= CELL_QUEUE_VAR
;
1847 q
.u
.var
.var_cell
= var_cell
;
1848 channel_write_cell_queue_entry(chan
, &q
);
1852 * Change channel state
1854 * This internal and subclass use only function is used to change channel
1855 * state, performing all transition validity checks and whatever actions
1856 * are appropriate to the state transition in question.
1860 channel_change_state(channel_t
*chan
, channel_state_t to_state
)
1862 channel_state_t from_state
;
1863 unsigned char was_active
, is_active
;
1864 unsigned char was_in_id_map
, is_in_id_map
;
1867 from_state
= chan
->state
;
1869 tor_assert(channel_state_is_valid(from_state
));
1870 tor_assert(channel_state_is_valid(to_state
));
1871 tor_assert(channel_state_can_transition(chan
->state
, to_state
));
1873 /* Check for no-op transitions */
1874 if (from_state
== to_state
) {
1875 log_debug(LD_CHANNEL
,
1876 "Got no-op transition from \"%s\" to itself on channel %p"
1877 "(global ID " U64_FORMAT
")",
1878 channel_state_to_string(to_state
),
1879 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
1883 /* If we're going to a closing or closed state, we must have a reason set */
1884 if (to_state
== CHANNEL_STATE_CLOSING
||
1885 to_state
== CHANNEL_STATE_CLOSED
||
1886 to_state
== CHANNEL_STATE_ERROR
) {
1887 tor_assert(chan
->reason_for_closing
!= CHANNEL_NOT_CLOSING
);
1891 * We need to maintain the queues here for some transitions:
1892 * when we enter CHANNEL_STATE_OPEN (especially from CHANNEL_STATE_MAINT)
1893 * we may have a backlog of cells to transmit, so drain the queues in
1894 * that case, and when going to CHANNEL_STATE_CLOSED the subclass
1895 * should have made sure to finish sending things (or gone to
1896 * CHANNEL_STATE_ERROR if not possible), so we assert for that here.
1899 log_debug(LD_CHANNEL
,
1900 "Changing state of channel %p (global ID " U64_FORMAT
1901 ") from \"%s\" to \"%s\"",
1903 U64_PRINTF_ARG(chan
->global_identifier
),
1904 channel_state_to_string(chan
->state
),
1905 channel_state_to_string(to_state
));
1907 chan
->state
= to_state
;
1909 /* Need to add to the right lists if the channel is registered */
1910 if (chan
->registered
) {
1911 was_active
= !(from_state
== CHANNEL_STATE_CLOSED
||
1912 from_state
== CHANNEL_STATE_ERROR
);
1913 is_active
= !(to_state
== CHANNEL_STATE_CLOSED
||
1914 to_state
== CHANNEL_STATE_ERROR
);
1916 /* Need to take off active list and put on finished list? */
1917 if (was_active
&& !is_active
) {
1918 if (active_channels
) smartlist_remove(active_channels
, chan
);
1919 if (!finished_channels
) finished_channels
= smartlist_new();
1920 smartlist_add(finished_channels
, chan
);
1922 /* Need to put on active list? */
1923 else if (!was_active
&& is_active
) {
1924 if (finished_channels
) smartlist_remove(finished_channels
, chan
);
1925 if (!active_channels
) active_channels
= smartlist_new();
1926 smartlist_add(active_channels
, chan
);
1929 if (!tor_digest_is_zero(chan
->identity_digest
)) {
1930 /* Now we need to handle the identity map */
1931 was_in_id_map
= !(from_state
== CHANNEL_STATE_CLOSING
||
1932 from_state
== CHANNEL_STATE_CLOSED
||
1933 from_state
== CHANNEL_STATE_ERROR
);
1934 is_in_id_map
= !(to_state
== CHANNEL_STATE_CLOSING
||
1935 to_state
== CHANNEL_STATE_CLOSED
||
1936 to_state
== CHANNEL_STATE_ERROR
);
1938 if (!was_in_id_map
&& is_in_id_map
) channel_add_to_digest_map(chan
);
1939 else if (was_in_id_map
&& !is_in_id_map
)
1940 channel_remove_from_digest_map(chan
);
1944 /* Tell circuits if we opened and stuff */
1945 if (to_state
== CHANNEL_STATE_OPEN
) {
1946 channel_do_open_actions(chan
);
1947 chan
->has_been_open
= 1;
1949 /* Check for queued cells to process */
1950 if (! TOR_SIMPLEQ_EMPTY(&chan
->incoming_queue
))
1951 channel_process_cells(chan
);
1952 if (! TOR_SIMPLEQ_EMPTY(&chan
->outgoing_queue
))
1953 channel_flush_cells(chan
);
1954 } else if (to_state
== CHANNEL_STATE_CLOSED
||
1955 to_state
== CHANNEL_STATE_ERROR
) {
1956 /* Assert that all queues are empty */
1957 tor_assert(TOR_SIMPLEQ_EMPTY(&chan
->incoming_queue
));
1958 tor_assert(TOR_SIMPLEQ_EMPTY(&chan
->outgoing_queue
));
1963 * Change channel listener state
1965 * This internal and subclass use only function is used to change channel
1966 * listener state, performing all transition validity checks and whatever
1967 * actions are appropriate to the state transition in question.
1971 channel_listener_change_state(channel_listener_t
*chan_l
,
1972 channel_listener_state_t to_state
)
1974 channel_listener_state_t from_state
;
1975 unsigned char was_active
, is_active
;
1978 from_state
= chan_l
->state
;
1980 tor_assert(channel_listener_state_is_valid(from_state
));
1981 tor_assert(channel_listener_state_is_valid(to_state
));
1982 tor_assert(channel_listener_state_can_transition(chan_l
->state
, to_state
));
1984 /* Check for no-op transitions */
1985 if (from_state
== to_state
) {
1986 log_debug(LD_CHANNEL
,
1987 "Got no-op transition from \"%s\" to itself on channel "
1988 "listener %p (global ID " U64_FORMAT
")",
1989 channel_listener_state_to_string(to_state
),
1990 chan_l
, U64_PRINTF_ARG(chan_l
->global_identifier
));
1994 /* If we're going to a closing or closed state, we must have a reason set */
1995 if (to_state
== CHANNEL_LISTENER_STATE_CLOSING
||
1996 to_state
== CHANNEL_LISTENER_STATE_CLOSED
||
1997 to_state
== CHANNEL_LISTENER_STATE_ERROR
) {
1998 tor_assert(chan_l
->reason_for_closing
!= CHANNEL_LISTENER_NOT_CLOSING
);
2002 * We need to maintain the queues here for some transitions:
2003 * when we enter CHANNEL_STATE_OPEN (especially from CHANNEL_STATE_MAINT)
2004 * we may have a backlog of cells to transmit, so drain the queues in
2005 * that case, and when going to CHANNEL_STATE_CLOSED the subclass
2006 * should have made sure to finish sending things (or gone to
2007 * CHANNEL_STATE_ERROR if not possible), so we assert for that here.
2010 log_debug(LD_CHANNEL
,
2011 "Changing state of channel listener %p (global ID " U64_FORMAT
2012 "from \"%s\" to \"%s\"",
2013 chan_l
, U64_PRINTF_ARG(chan_l
->global_identifier
),
2014 channel_listener_state_to_string(chan_l
->state
),
2015 channel_listener_state_to_string(to_state
));
2017 chan_l
->state
= to_state
;
2019 /* Need to add to the right lists if the channel listener is registered */
2020 if (chan_l
->registered
) {
2021 was_active
= !(from_state
== CHANNEL_LISTENER_STATE_CLOSED
||
2022 from_state
== CHANNEL_LISTENER_STATE_ERROR
);
2023 is_active
= !(to_state
== CHANNEL_LISTENER_STATE_CLOSED
||
2024 to_state
== CHANNEL_LISTENER_STATE_ERROR
);
2026 /* Need to take off active list and put on finished list? */
2027 if (was_active
&& !is_active
) {
2028 if (active_listeners
) smartlist_remove(active_listeners
, chan_l
);
2029 if (!finished_listeners
) finished_listeners
= smartlist_new();
2030 smartlist_add(finished_listeners
, chan_l
);
2032 /* Need to put on active list? */
2033 else if (!was_active
&& is_active
) {
2034 if (finished_listeners
) smartlist_remove(finished_listeners
, chan_l
);
2035 if (!active_listeners
) active_listeners
= smartlist_new();
2036 smartlist_add(active_listeners
, chan_l
);
2040 if (to_state
== CHANNEL_LISTENER_STATE_CLOSED
||
2041 to_state
== CHANNEL_LISTENER_STATE_ERROR
) {
2042 /* Assert that the queue is empty */
2043 tor_assert(!(chan_l
->incoming_list
) ||
2044 smartlist_len(chan_l
->incoming_list
) == 0);
2049 * Try to flush cells to the lower layer
2051 * this is called by the lower layer to indicate that it wants more cells;
2052 * it will try to write up to num_cells cells from the channel's cell queue or
2053 * from circuits active on that channel, or as many as it has available if
2057 #define MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED 256
2060 channel_flush_some_cells(channel_t
*chan
, ssize_t num_cells
)
2062 unsigned int unlimited
= 0;
2063 ssize_t flushed
= 0;
2064 int num_cells_from_circs
, clamped_num_cells
;
2068 if (num_cells
< 0) unlimited
= 1;
2069 if (!unlimited
&& num_cells
<= flushed
) goto done
;
2071 /* If we aren't in CHANNEL_STATE_OPEN, nothing goes through */
2072 if (chan
->state
== CHANNEL_STATE_OPEN
) {
2073 /* Try to flush as much as we can that's already queued */
2074 flushed
+= channel_flush_some_cells_from_outgoing_queue(chan
,
2075 (unlimited
? -1 : num_cells
- flushed
));
2076 if (!unlimited
&& num_cells
<= flushed
) goto done
;
2078 if (circuitmux_num_cells(chan
->cmux
) > 0) {
2079 /* Calculate number of cells, including clamp */
2081 clamped_num_cells
= MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED
;
2083 if (num_cells
- flushed
>
2084 MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED
) {
2085 clamped_num_cells
= MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED
;
2087 clamped_num_cells
= (int)(num_cells
- flushed
);
2090 /* Try to get more cells from any active circuits */
2091 num_cells_from_circs
= channel_flush_from_first_active_circuit(
2092 chan
, clamped_num_cells
);
2094 /* If it claims we got some, process the queue again */
2095 if (num_cells_from_circs
> 0) {
2096 flushed
+= channel_flush_some_cells_from_outgoing_queue(chan
,
2097 (unlimited
? -1 : num_cells
- flushed
));
2107 * Flush cells from just the channel's outgoing cell queue
2109 * This gets called from channel_flush_some_cells() above to flush cells
2110 * just from the queue without trying for active_circuits.
2114 channel_flush_some_cells_from_outgoing_queue(channel_t
*chan
,
2117 unsigned int unlimited
= 0;
2118 ssize_t flushed
= 0;
2119 cell_queue_entry_t
*q
= NULL
;
2122 tor_assert(chan
->write_cell
);
2123 tor_assert(chan
->write_packed_cell
);
2124 tor_assert(chan
->write_var_cell
);
2126 if (num_cells
< 0) unlimited
= 1;
2127 if (!unlimited
&& num_cells
<= flushed
) return 0;
2129 /* If we aren't in CHANNEL_STATE_OPEN, nothing goes through */
2130 if (chan
->state
== CHANNEL_STATE_OPEN
) {
2131 while ((unlimited
|| num_cells
> flushed
) &&
2132 NULL
!= (q
= TOR_SIMPLEQ_FIRST(&chan
->outgoing_queue
))) {
2136 * Okay, we have a good queue entry, try to give it to the lower
2140 case CELL_QUEUE_FIXED
:
2141 if (q
->u
.fixed
.cell
) {
2142 if (chan
->write_cell(chan
,
2145 channel_timestamp_xmit(chan
);
2146 ++(chan
->n_cells_xmitted
);
2147 cell_queue_entry_free(q
, 1);
2150 /* Else couldn't write it; leave it on the queue */
2152 /* This shouldn't happen */
2153 log_info(LD_CHANNEL
,
2154 "Saw broken cell queue entry of type CELL_QUEUE_FIXED "
2155 "with no cell on channel %p "
2156 "(global ID " U64_FORMAT
").",
2157 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
2159 cell_queue_entry_free(q
, 0);
2163 case CELL_QUEUE_PACKED
:
2164 if (q
->u
.packed
.packed_cell
) {
2165 if (chan
->write_packed_cell(chan
,
2166 q
->u
.packed
.packed_cell
)) {
2168 channel_timestamp_xmit(chan
);
2169 ++(chan
->n_cells_xmitted
);
2170 cell_queue_entry_free(q
, 1);
2173 /* Else couldn't write it; leave it on the queue */
2175 /* This shouldn't happen */
2176 log_info(LD_CHANNEL
,
2177 "Saw broken cell queue entry of type CELL_QUEUE_PACKED "
2178 "with no cell on channel %p "
2179 "(global ID " U64_FORMAT
").",
2180 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
2182 cell_queue_entry_free(q
, 0);
2186 case CELL_QUEUE_VAR
:
2187 if (q
->u
.var
.var_cell
) {
2188 if (chan
->write_var_cell(chan
,
2189 q
->u
.var
.var_cell
)) {
2191 channel_timestamp_xmit(chan
);
2192 ++(chan
->n_cells_xmitted
);
2193 cell_queue_entry_free(q
, 1);
2196 /* Else couldn't write it; leave it on the queue */
2198 /* This shouldn't happen */
2199 log_info(LD_CHANNEL
,
2200 "Saw broken cell queue entry of type CELL_QUEUE_VAR "
2201 "with no cell on channel %p "
2202 "(global ID " U64_FORMAT
").",
2203 chan
, U64_PRINTF_ARG(chan
->global_identifier
));
2205 cell_queue_entry_free(q
, 0);
2210 /* Unknown type, log and free it */
2211 log_info(LD_CHANNEL
,
2212 "Saw an unknown cell queue entry type %d on channel %p "
2213 "(global ID " U64_FORMAT
"; ignoring it."
2214 " Someone should fix this.",
2215 q
->type
, chan
, U64_PRINTF_ARG(chan
->global_identifier
));
2216 cell_queue_entry_free(q
, 0);
2220 /* if q got NULLed out, we used it and should remove the queue entry */
2221 if (!q
) TOR_SIMPLEQ_REMOVE_HEAD(&chan
->outgoing_queue
, next
);
2222 /* No cell removed from list, so we can't go on any further */
2228 /* Did we drain the queue? */
2229 if (TOR_SIMPLEQ_EMPTY(&chan
->outgoing_queue
)) {
2230 channel_timestamp_drained(chan
);
2237 * Flush as many cells as we possibly can from the queue
2239 * This tries to flush as many cells from the queue as the lower layer
2240 * will take. It just calls channel_flush_some_cells_from_outgoing_queue()
2241 * in unlimited mode.
2245 channel_flush_cells(channel_t
*chan
)
2247 channel_flush_some_cells_from_outgoing_queue(chan
, -1);
2251 * Check if any cells are available
2253 * This gets used from the lower layer to check if any more cells are
2258 channel_more_to_flush(channel_t
*chan
)
2262 /* Check if we have any queued */
2263 if (! TOR_SIMPLEQ_EMPTY(&chan
->incoming_queue
))
2266 /* Check if any circuits would like to queue some */
2267 if (circuitmux_num_cells(chan
->cmux
) > 0) return 1;
2274 * Notify the channel we're done flushing the output in the lower layer
2276 * Connection.c will call this when we've flushed the output; there's some
2277 * dirreq-related maintenance to do.
2281 channel_notify_flushed(channel_t
*chan
)
2285 if (chan
->dirreq_id
!= 0)
2286 geoip_change_dirreq_state(chan
->dirreq_id
,
2288 DIRREQ_CHANNEL_BUFFER_FLUSHED
);
2292 * Process the queue of incoming channels on a listener
2294 * Use a listener's registered callback to process as many entries in the
2295 * queue of incoming channels as possible.
2299 channel_listener_process_incoming(channel_listener_t
*listener
)
2301 tor_assert(listener
);
2304 * CHANNEL_LISTENER_STATE_CLOSING permitted because we drain the queue
2305 * while closing a listener.
2307 tor_assert(listener
->state
== CHANNEL_LISTENER_STATE_LISTENING
||
2308 listener
->state
== CHANNEL_LISTENER_STATE_CLOSING
);
2309 tor_assert(listener
->listener
);
2311 log_debug(LD_CHANNEL
,
2312 "Processing queue of incoming connections for channel "
2313 "listener %p (global ID " U64_FORMAT
")",
2314 listener
, U64_PRINTF_ARG(listener
->global_identifier
));
2316 if (!(listener
->incoming_list
)) return;
2318 SMARTLIST_FOREACH_BEGIN(listener
->incoming_list
,
2319 channel_t
*, chan
) {
2322 log_debug(LD_CHANNEL
,
2323 "Handling incoming channel %p (" U64_FORMAT
") "
2324 "for listener %p (" U64_FORMAT
")",
2326 U64_PRINTF_ARG(chan
->global_identifier
),
2328 U64_PRINTF_ARG(listener
->global_identifier
));
2329 /* Make sure this is set correctly */
2330 channel_mark_incoming(chan
);
2331 listener
->listener(listener
, chan
);
2332 } SMARTLIST_FOREACH_END(chan
);
2334 smartlist_free(listener
->incoming_list
);
2335 listener
->incoming_list
= NULL
;
2339 * Take actions required when a channel becomes open
2341 * Handle actions we should do when we know a channel is open; a lot of
2342 * this comes from the old connection_or_set_state_open() of connection_or.c.
2344 * Because of this mechanism, future channel_t subclasses should take care
2345 * not to change a channel to from CHANNEL_STATE_OPENING to CHANNEL_STATE_OPEN
2346 * until there is positive confirmation that the network is operational.
2347 * In particular, anything UDP-based should not make this transition until a
2348 * packet is received from the other side.
2352 channel_do_open_actions(channel_t
*chan
)
2354 tor_addr_t remote_addr
;
2355 int started_here
, not_using
= 0;
2356 time_t now
= time(NULL
);
2360 started_here
= channel_is_outgoing(chan
);
2363 circuit_build_times_network_is_live(get_circuit_build_times_mutable());
2364 rep_hist_note_connect_succeeded(chan
->identity_digest
, now
);
2365 if (entry_guard_register_connect_status(
2366 chan
->identity_digest
, 1, 0, now
) < 0) {
2367 /* Close any circuits pending on this channel. We leave it in state
2368 * 'open' though, because it didn't actually *fail* -- we just
2369 * chose not to use it. */
2371 "New entry guard was reachable, but closing this "
2372 "connection so we can retry the earlier entry guards.");
2373 circuit_n_chan_done(chan
, 0);
2376 router_set_status(chan
->identity_digest
, 1);
2378 /* only report it to the geoip module if it's not a known router */
2379 if (!router_get_by_id_digest(chan
->identity_digest
)) {
2380 if (channel_get_addr_if_possible(chan
, &remote_addr
)) {
2381 char *transport_name
= NULL
;
2382 if (chan
->get_transport_name(chan
, &transport_name
) < 0)
2383 transport_name
= NULL
;
2385 geoip_note_client_seen(GEOIP_CLIENT_CONNECT
,
2386 &remote_addr
, transport_name
,
2388 tor_free(transport_name
);
2390 /* Otherwise the underlying transport can't tell us this, so skip it */
2394 if (!not_using
) circuit_n_chan_done(chan
, 1);
2398 * Queue an incoming channel on a listener
2400 * Internal and subclass use only function to queue an incoming channel from
2401 * a listener. A subclass of channel_listener_t should call this when a new
2402 * incoming channel is created.
2406 channel_listener_queue_incoming(channel_listener_t
*listener
,
2407 channel_t
*incoming
)
2409 int need_to_queue
= 0;
2411 tor_assert(listener
);
2412 tor_assert(listener
->state
== CHANNEL_LISTENER_STATE_LISTENING
);
2413 tor_assert(incoming
);
2415 log_debug(LD_CHANNEL
,
2416 "Queueing incoming channel %p (global ID " U64_FORMAT
") on "
2417 "channel listener %p (global ID " U64_FORMAT
")",
2418 incoming
, U64_PRINTF_ARG(incoming
->global_identifier
),
2419 listener
, U64_PRINTF_ARG(listener
->global_identifier
));
2421 /* Do we need to queue it, or can we just call the listener right away? */
2422 if (!(listener
->listener
)) need_to_queue
= 1;
2423 if (listener
->incoming_list
&&
2424 (smartlist_len(listener
->incoming_list
) > 0))
2427 /* If we need to queue and have no queue, create one */
2428 if (need_to_queue
&& !(listener
->incoming_list
)) {
2429 listener
->incoming_list
= smartlist_new();
2432 /* Bump the counter and timestamp it */
2433 channel_listener_timestamp_active(listener
);
2434 channel_listener_timestamp_accepted(listener
);
2435 ++(listener
->n_accepted
);
2437 /* If we don't need to queue, process it right away */
2438 if (!need_to_queue
) {
2439 tor_assert(listener
->listener
);
2440 listener
->listener(listener
, incoming
);
2443 * Otherwise, we need to queue; queue and then process the queue if
2447 tor_assert(listener
->incoming_list
);
2448 smartlist_add(listener
->incoming_list
, incoming
);
2449 if (listener
->listener
) channel_listener_process_incoming(listener
);
2454 * Process queued incoming cells
2456 * Process as many queued cells as we can from the incoming
2461 channel_process_cells(channel_t
*chan
)
2463 cell_queue_entry_t
*q
;
2465 tor_assert(chan
->state
== CHANNEL_STATE_CLOSING
||
2466 chan
->state
== CHANNEL_STATE_MAINT
||
2467 chan
->state
== CHANNEL_STATE_OPEN
);
2469 log_debug(LD_CHANNEL
,
2470 "Processing as many incoming cells as we can for channel %p",
2473 /* Nothing we can do if we have no registered cell handlers */
2474 if (!(chan
->cell_handler
||
2475 chan
->var_cell_handler
)) return;
2476 /* Nothing we can do if we have no cells */
2477 if (TOR_SIMPLEQ_EMPTY(&chan
->incoming_queue
)) return;
2480 * Process cells until we're done or find one we have no current handler
2483 while (NULL
!= (q
= TOR_SIMPLEQ_FIRST(&chan
->incoming_queue
))) {
2485 tor_assert(q
->type
== CELL_QUEUE_FIXED
||
2486 q
->type
== CELL_QUEUE_VAR
);
2488 if (q
->type
== CELL_QUEUE_FIXED
&&
2489 chan
->cell_handler
) {
2490 /* Handle a fixed-length cell */
2491 TOR_SIMPLEQ_REMOVE_HEAD(&chan
->incoming_queue
, next
);
2492 tor_assert(q
->u
.fixed
.cell
);
2493 log_debug(LD_CHANNEL
,
2494 "Processing incoming cell_t %p for channel %p (global ID "
2496 q
->u
.fixed
.cell
, chan
,
2497 U64_PRINTF_ARG(chan
->global_identifier
));
2498 chan
->cell_handler(chan
, q
->u
.fixed
.cell
);
2500 } else if (q
->type
== CELL_QUEUE_VAR
&&
2501 chan
->var_cell_handler
) {
2502 /* Handle a variable-length cell */
2503 TOR_SIMPLEQ_REMOVE_HEAD(&chan
->incoming_queue
, next
);
2504 tor_assert(q
->u
.var
.var_cell
);
2505 log_debug(LD_CHANNEL
,
2506 "Processing incoming var_cell_t %p for channel %p (global ID "
2508 q
->u
.var
.var_cell
, chan
,
2509 U64_PRINTF_ARG(chan
->global_identifier
));
2510 chan
->var_cell_handler(chan
, q
->u
.var
.var_cell
);
2513 /* Can't handle this one */
2520 * Queue incoming cell
2522 * This should be called by a channel_t subclass to queue an incoming fixed-
2523 * length cell for processing, and process it if possible.
2527 channel_queue_cell(channel_t
*chan
, cell_t
*cell
)
2529 int need_to_queue
= 0;
2530 cell_queue_entry_t
*q
;
2534 tor_assert(chan
->state
== CHANNEL_STATE_OPEN
);
2536 /* Do we need to queue it, or can we just call the handler right away? */
2537 if (!(chan
->cell_handler
)) need_to_queue
= 1;
2538 if (! TOR_SIMPLEQ_EMPTY(&chan
->incoming_queue
))
2541 /* Timestamp for receiving */
2542 channel_timestamp_recv(chan
);
2544 /* Update the counter */
2545 ++(chan
->n_cells_recved
);
2547 /* If we don't need to queue we can just call cell_handler */
2548 if (!need_to_queue
) {
2549 tor_assert(chan
->cell_handler
);
2550 log_debug(LD_CHANNEL
,
2551 "Directly handling incoming cell_t %p for channel %p "
2552 "(global ID " U64_FORMAT
")",
2554 U64_PRINTF_ARG(chan
->global_identifier
));
2555 chan
->cell_handler(chan
, cell
);
2557 /* Otherwise queue it and then process the queue if possible. */
2558 q
= cell_queue_entry_new_fixed(cell
);
2559 log_debug(LD_CHANNEL
,
2560 "Queueing incoming cell_t %p for channel %p "
2561 "(global ID " U64_FORMAT
")",
2563 U64_PRINTF_ARG(chan
->global_identifier
));
2564 TOR_SIMPLEQ_INSERT_TAIL(&chan
->incoming_queue
, q
, next
);
2565 if (chan
->cell_handler
||
2566 chan
->var_cell_handler
) {
2567 channel_process_cells(chan
);
2573 * Queue incoming variable-length cell
2575 * This should be called by a channel_t subclass to queue an incoming
2576 * variable-length cell for processing, and process it if possible.
2580 channel_queue_var_cell(channel_t
*chan
, var_cell_t
*var_cell
)
2582 int need_to_queue
= 0;
2583 cell_queue_entry_t
*q
;
2586 tor_assert(var_cell
);
2587 tor_assert(chan
->state
== CHANNEL_STATE_OPEN
);
2589 /* Do we need to queue it, or can we just call the handler right away? */
2590 if (!(chan
->var_cell_handler
)) need_to_queue
= 1;
2591 if (! TOR_SIMPLEQ_EMPTY(&chan
->incoming_queue
))
2594 /* Timestamp for receiving */
2595 channel_timestamp_recv(chan
);
2597 /* Update the counter */
2598 ++(chan
->n_cells_recved
);
2600 /* If we don't need to queue we can just call cell_handler */
2601 if (!need_to_queue
) {
2602 tor_assert(chan
->var_cell_handler
);
2603 log_debug(LD_CHANNEL
,
2604 "Directly handling incoming var_cell_t %p for channel %p "
2605 "(global ID " U64_FORMAT
")",
2607 U64_PRINTF_ARG(chan
->global_identifier
));
2608 chan
->var_cell_handler(chan
, var_cell
);
2610 /* Otherwise queue it and then process the queue if possible. */
2611 q
= cell_queue_entry_new_var(var_cell
);
2612 log_debug(LD_CHANNEL
,
2613 "Queueing incoming var_cell_t %p for channel %p "
2614 "(global ID " U64_FORMAT
")",
2616 U64_PRINTF_ARG(chan
->global_identifier
));
2617 TOR_SIMPLEQ_INSERT_TAIL(&chan
->incoming_queue
, q
, next
);
2618 if (chan
->cell_handler
||
2619 chan
->var_cell_handler
) {
2620 channel_process_cells(chan
);
2625 /** If <b>packed_cell</b> on <b>chan</b> is a destroy cell, then set
2626 * *<b>circid_out</b> to its circuit ID, and return true. Otherwise, return
2628 /* XXXX Move this function. */
2630 packed_cell_is_destroy(channel_t
*chan
,
2631 const packed_cell_t
*packed_cell
,
2632 circid_t
*circid_out
)
2634 if (chan
->wide_circ_ids
) {
2635 if (packed_cell
->body
[4] == CELL_DESTROY
) {
2636 *circid_out
= ntohl(get_uint32(packed_cell
->body
));
2640 if (packed_cell
->body
[2] == CELL_DESTROY
) {
2641 *circid_out
= ntohs(get_uint16(packed_cell
->body
));
2650 is_destroy_cell(channel_t
*chan
,
2651 const cell_queue_entry_t
*q
, circid_t
*circid_out
)
2655 case CELL_QUEUE_FIXED
:
2656 if (q
->u
.fixed
.cell
->command
== CELL_DESTROY
) {
2657 *circid_out
= q
->u
.fixed
.cell
->circ_id
;
2661 case CELL_QUEUE_VAR
:
2662 if (q
->u
.var
.var_cell
->command
== CELL_DESTROY
) {
2663 *circid_out
= q
->u
.var
.var_cell
->circ_id
;
2667 case CELL_QUEUE_PACKED
:
2668 return packed_cell_is_destroy(chan
, q
->u
.packed
.packed_cell
, circid_out
);
2674 * Send destroy cell on a channel
2676 * Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
2677 * onto channel <b>chan</b>. Don't perform range-checking on reason:
2678 * we may want to propagate reasons from other cells.
2682 channel_send_destroy(circid_t circ_id
, channel_t
*chan
, int reason
)
2686 log_warn(LD_BUG
, "Attempted to send a destroy cell for circID 0 "
2687 "on a channel " U64_FORMAT
" at %p in state %s (%d)",
2688 U64_PRINTF_ARG(chan
->global_identifier
),
2689 chan
, channel_state_to_string(chan
->state
),
2694 /* Check to make sure we can send on this channel first */
2695 if (!(chan
->state
== CHANNEL_STATE_CLOSING
||
2696 chan
->state
== CHANNEL_STATE_CLOSED
||
2697 chan
->state
== CHANNEL_STATE_ERROR
) &&
2699 channel_note_destroy_pending(chan
, circ_id
);
2700 circuitmux_append_destroy_cell(chan
, chan
->cmux
, circ_id
, reason
);
2702 "Sending destroy (circID %u) on channel %p "
2703 "(global ID " U64_FORMAT
")",
2704 (unsigned)circ_id
, chan
,
2705 U64_PRINTF_ARG(chan
->global_identifier
));
2708 "Someone called channel_send_destroy() for circID %u "
2709 "on a channel " U64_FORMAT
" at %p in state %s (%d)",
2710 (unsigned)circ_id
, U64_PRINTF_ARG(chan
->global_identifier
),
2711 chan
, channel_state_to_string(chan
->state
),
2719 * Dump channel statistics to the log
2721 * This is called from dumpstats() in main.c and spams the log with
2722 * statistics on channels.
2726 channel_dumpstats(int severity
)
2728 if (all_channels
&& smartlist_len(all_channels
) > 0) {
2729 tor_log(severity
, LD_GENERAL
,
2730 "Dumping statistics about %d channels:",
2731 smartlist_len(all_channels
));
2732 tor_log(severity
, LD_GENERAL
,
2733 "%d are active, and %d are done and waiting for cleanup",
2734 (active_channels
!= NULL
) ?
2735 smartlist_len(active_channels
) : 0,
2736 (finished_channels
!= NULL
) ?
2737 smartlist_len(finished_channels
) : 0);
2739 SMARTLIST_FOREACH(all_channels
, channel_t
*, chan
,
2740 channel_dump_statistics(chan
, severity
));
2742 tor_log(severity
, LD_GENERAL
,
2743 "Done spamming about channels now");
2745 tor_log(severity
, LD_GENERAL
,
2746 "No channels to dump");
2751 * Dump channel listener statistics to the log
2753 * This is called from dumpstats() in main.c and spams the log with
2754 * statistics on channel listeners.
2758 channel_listener_dumpstats(int severity
)
2760 if (all_listeners
&& smartlist_len(all_listeners
) > 0) {
2761 tor_log(severity
, LD_GENERAL
,
2762 "Dumping statistics about %d channel listeners:",
2763 smartlist_len(all_listeners
));
2764 tor_log(severity
, LD_GENERAL
,
2765 "%d are active and %d are done and waiting for cleanup",
2766 (active_listeners
!= NULL
) ?
2767 smartlist_len(active_listeners
) : 0,
2768 (finished_listeners
!= NULL
) ?
2769 smartlist_len(finished_listeners
) : 0);
2771 SMARTLIST_FOREACH(all_listeners
, channel_listener_t
*, chan_l
,
2772 channel_listener_dump_statistics(chan_l
, severity
));
2774 tor_log(severity
, LD_GENERAL
,
2775 "Done spamming about channel listeners now");
2777 tor_log(severity
, LD_GENERAL
,
2778 "No channel listeners to dump");
2783 * Set the cmux policy on all active channels
2787 channel_set_cmux_policy_everywhere(circuitmux_policy_t
*pol
)
2789 if (!active_channels
) return;
2791 SMARTLIST_FOREACH_BEGIN(active_channels
, channel_t
*, curr
) {
2793 circuitmux_set_policy(curr
->cmux
, pol
);
2795 } SMARTLIST_FOREACH_END(curr
);
2801 * This gets called periodically from run_scheduled_events() in main.c;
2802 * it cleans up after closed channels.
2806 channel_run_cleanup(void)
2808 channel_t
*tmp
= NULL
;
2810 /* Check if we need to do anything */
2811 if (!finished_channels
|| smartlist_len(finished_channels
) == 0) return;
2813 /* Iterate through finished_channels and get rid of them */
2814 SMARTLIST_FOREACH_BEGIN(finished_channels
, channel_t
*, curr
) {
2816 /* Remove it from the list */
2817 SMARTLIST_DEL_CURRENT(finished_channels
, curr
);
2818 /* Also unregister it */
2819 channel_unregister(tmp
);
2820 /* ... and free it */
2822 } SMARTLIST_FOREACH_END(curr
);
2826 * Clean up channel listeners
2828 * This gets called periodically from run_scheduled_events() in main.c;
2829 * it cleans up after closed channel listeners.
2833 channel_listener_run_cleanup(void)
2835 channel_listener_t
*tmp
= NULL
;
2837 /* Check if we need to do anything */
2838 if (!finished_listeners
|| smartlist_len(finished_listeners
) == 0) return;
2840 /* Iterate through finished_channels and get rid of them */
2841 SMARTLIST_FOREACH_BEGIN(finished_listeners
, channel_listener_t
*, curr
) {
2843 /* Remove it from the list */
2844 SMARTLIST_DEL_CURRENT(finished_listeners
, curr
);
2845 /* Also unregister it */
2846 channel_listener_unregister(tmp
);
2847 /* ... and free it */
2848 channel_listener_free(tmp
);
2849 } SMARTLIST_FOREACH_END(curr
);
2853 * Free a list of channels for channel_free_all()
2857 channel_free_list(smartlist_t
*channels
, int mark_for_close
)
2859 if (!channels
) return;
2861 SMARTLIST_FOREACH_BEGIN(channels
, channel_t
*, curr
) {
2862 /* Deregister and free it */
2864 log_debug(LD_CHANNEL
,
2865 "Cleaning up channel %p (global ID " U64_FORMAT
") "
2867 curr
, U64_PRINTF_ARG(curr
->global_identifier
),
2868 channel_state_to_string(curr
->state
), curr
->state
);
2869 /* Detach circuits early so they can find the channel */
2871 circuitmux_detach_all_circuits(curr
->cmux
, NULL
);
2873 channel_unregister(curr
);
2874 if (mark_for_close
) {
2875 if (!(curr
->state
== CHANNEL_STATE_CLOSING
||
2876 curr
->state
== CHANNEL_STATE_CLOSED
||
2877 curr
->state
== CHANNEL_STATE_ERROR
)) {
2878 channel_mark_for_close(curr
);
2880 channel_force_free(curr
);
2881 } else channel_free(curr
);
2882 } SMARTLIST_FOREACH_END(curr
);
2886 * Free a list of channel listeners for channel_free_all()
2890 channel_listener_free_list(smartlist_t
*listeners
, int mark_for_close
)
2892 if (!listeners
) return;
2894 SMARTLIST_FOREACH_BEGIN(listeners
, channel_listener_t
*, curr
) {
2895 /* Deregister and free it */
2897 log_debug(LD_CHANNEL
,
2898 "Cleaning up channel listener %p (global ID " U64_FORMAT
") "
2900 curr
, U64_PRINTF_ARG(curr
->global_identifier
),
2901 channel_listener_state_to_string(curr
->state
), curr
->state
);
2902 channel_listener_unregister(curr
);
2903 if (mark_for_close
) {
2904 if (!(curr
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
2905 curr
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
2906 curr
->state
== CHANNEL_LISTENER_STATE_ERROR
)) {
2907 channel_listener_mark_for_close(curr
);
2909 channel_listener_force_free(curr
);
2910 } else channel_listener_free(curr
);
2911 } SMARTLIST_FOREACH_END(curr
);
2915 * Close all channels and free everything
2917 * This gets called from tor_free_all() in main.c to clean up on exit.
2918 * It will close all registered channels and free associated storage,
2919 * then free the all_channels, active_channels, listening_channels and
2920 * finished_channels lists and also channel_identity_map.
2924 channel_free_all(void)
2926 log_debug(LD_CHANNEL
,
2927 "Shutting down channels...");
2929 /* First, let's go for finished channels */
2930 if (finished_channels
) {
2931 channel_free_list(finished_channels
, 0);
2932 smartlist_free(finished_channels
);
2933 finished_channels
= NULL
;
2936 /* Now the finished listeners */
2937 if (finished_listeners
) {
2938 channel_listener_free_list(finished_listeners
, 0);
2939 smartlist_free(finished_listeners
);
2940 finished_listeners
= NULL
;
2943 /* Now all active channels */
2944 if (active_channels
) {
2945 channel_free_list(active_channels
, 1);
2946 smartlist_free(active_channels
);
2947 active_channels
= NULL
;
2950 /* Now all active listeners */
2951 if (active_listeners
) {
2952 channel_listener_free_list(active_listeners
, 1);
2953 smartlist_free(active_listeners
);
2954 active_listeners
= NULL
;
2957 /* Now all channels, in case any are left over */
2959 channel_free_list(all_channels
, 1);
2960 smartlist_free(all_channels
);
2961 all_channels
= NULL
;
2964 /* Now all listeners, in case any are left over */
2965 if (all_listeners
) {
2966 channel_listener_free_list(all_listeners
, 1);
2967 smartlist_free(all_listeners
);
2968 all_listeners
= NULL
;
2971 /* Now free channel_identity_map */
2972 log_debug(LD_CHANNEL
,
2973 "Freeing channel_identity_map");
2974 /* Geez, anything still left over just won't die ... let it leak then */
2975 HT_CLEAR(channel_idmap
, &channel_identity_map
);
2977 log_debug(LD_CHANNEL
,
2978 "Done cleaning up after channels");
2982 * Connect to a given addr/port/digest
2984 * This sets up a new outgoing channel; in the future if multiple
2985 * channel_t subclasses are available, this is where the selection policy
2986 * should go. It may also be desirable to fold port into tor_addr_t
2987 * or make a new type including a tor_addr_t and port, so we have a
2988 * single abstract object encapsulating all the protocol details of
2989 * how to contact an OR.
2993 channel_connect(const tor_addr_t
*addr
, uint16_t port
,
2994 const char *id_digest
)
2996 return channel_tls_connect(addr
, port
, id_digest
);
3000 * Decide which of two channels to prefer for extending a circuit
3002 * This function is called while extending a circuit and returns true iff
3003 * a is 'better' than b. The most important criterion here is that a
3004 * canonical channel is always better than a non-canonical one, but the
3005 * number of circuits and the age are used as tie-breakers.
3007 * This is based on the former connection_or_is_better() of connection_or.c
3011 channel_is_better(time_t now
, channel_t
*a
, channel_t
*b
,
3012 int forgive_new_connections
)
3014 int a_grace
, b_grace
;
3015 int a_is_canonical
, b_is_canonical
;
3016 int a_has_circs
, b_has_circs
;
3019 * Do not definitively deprecate a new channel with no circuits on it
3020 * until this much time has passed.
3022 #define NEW_CHAN_GRACE_PERIOD (15*60)
3027 /* Check if one is canonical and the other isn't first */
3028 a_is_canonical
= channel_is_canonical(a
);
3029 b_is_canonical
= channel_is_canonical(b
);
3031 if (a_is_canonical
&& !b_is_canonical
) return 1;
3032 if (!a_is_canonical
&& b_is_canonical
) return 0;
3035 * Okay, if we're here they tied on canonicity. Next we check if
3036 * they have any circuits, and if one does and the other doesn't,
3037 * we prefer the one that does, unless we are forgiving and the
3038 * one that has no circuits is in its grace period.
3041 a_has_circs
= (channel_num_circuits(a
) > 0);
3042 b_has_circs
= (channel_num_circuits(b
) > 0);
3043 a_grace
= (forgive_new_connections
&&
3044 (now
< channel_when_created(a
) + NEW_CHAN_GRACE_PERIOD
));
3045 b_grace
= (forgive_new_connections
&&
3046 (now
< channel_when_created(b
) + NEW_CHAN_GRACE_PERIOD
));
3048 if (a_has_circs
&& !b_has_circs
&& !b_grace
) return 1;
3049 if (!a_has_circs
&& b_has_circs
&& !a_grace
) return 0;
3051 /* They tied on circuits too; just prefer whichever is newer */
3053 if (channel_when_created(a
) > channel_when_created(b
)) return 1;
3058 * Get a channel to extend a circuit
3060 * Pick a suitable channel to extend a circuit to given the desired digest
3061 * the address we believe is correct for that digest; this tries to see
3062 * if we already have one for the requested endpoint, but if there is no good
3063 * channel, set *msg_out to a message describing the channel's state
3064 * and our next action, and set *launch_out to a boolean indicated whether
3065 * the caller should try to launch a new channel with channel_connect().
3069 channel_get_for_extend(const char *digest
,
3070 const tor_addr_t
*target_addr
,
3071 const char **msg_out
,
3074 channel_t
*chan
, *best
= NULL
;
3075 int n_inprogress_goodaddr
= 0, n_old
= 0;
3076 int n_noncanonical
= 0, n_possible
= 0;
3077 time_t now
= approx_time();
3079 tor_assert(msg_out
);
3080 tor_assert(launch_out
);
3082 chan
= channel_find_by_remote_digest(digest
);
3084 /* Walk the list, unrefing the old one and refing the new at each
3087 for (; chan
; chan
= channel_next_with_digest(chan
)) {
3088 tor_assert(tor_memeq(chan
->identity_digest
,
3089 digest
, DIGEST_LEN
));
3091 if (chan
->state
== CHANNEL_STATE_CLOSING
||
3092 chan
->state
== CHANNEL_STATE_CLOSED
||
3093 chan
->state
== CHANNEL_STATE_ERROR
)
3096 /* Never return a channel on which the other end appears to be
3098 if (channel_is_client(chan
)) {
3102 /* Never return a non-open connection. */
3103 if (chan
->state
!= CHANNEL_STATE_OPEN
) {
3104 /* If the address matches, don't launch a new connection for this
3106 if (channel_matches_target_addr_for_extend(chan
, target_addr
))
3107 ++n_inprogress_goodaddr
;
3111 /* Never return a connection that shouldn't be used for circs. */
3112 if (channel_is_bad_for_new_circs(chan
)) {
3117 /* Never return a non-canonical connection using a recent link protocol
3118 * if the address is not what we wanted.
3120 * The channel_is_canonical_is_reliable() function asks the lower layer
3121 * if we should trust channel_is_canonical(). The below is from the
3122 * comments of the old circuit_or_get_for_extend() and applies when
3123 * the lower-layer transport is channel_tls_t.
3125 * (For old link protocols, we can't rely on is_canonical getting
3126 * set properly if we're talking to the right address, since we might
3127 * have an out-of-date descriptor, and we will get no NETINFO cell to
3128 * tell us about the right address.)
3130 if (!channel_is_canonical(chan
) &&
3131 channel_is_canonical_is_reliable(chan
) &&
3132 !channel_matches_target_addr_for_extend(chan
, target_addr
)) {
3140 best
= chan
; /* If we have no 'best' so far, this one is good enough. */
3144 if (channel_is_better(now
, chan
, best
, 0))
3149 *msg_out
= "Connection is fine; using it.";
3152 } else if (n_inprogress_goodaddr
) {
3153 *msg_out
= "Connection in progress; waiting.";
3156 } else if (n_old
|| n_noncanonical
) {
3157 *msg_out
= "Connections all too old, or too non-canonical. "
3158 " Launching a new one.";
3162 *msg_out
= "Not connected. Connecting.";
3169 * Describe the transport subclass for a channel
3171 * Invoke a method to get a string description of the lower-layer
3172 * transport for this channel.
3176 channel_describe_transport(channel_t
*chan
)
3179 tor_assert(chan
->describe_transport
);
3181 return chan
->describe_transport(chan
);
3185 * Describe the transport subclass for a channel listener
3187 * Invoke a method to get a string description of the lower-layer
3188 * transport for this channel listener.
3192 channel_listener_describe_transport(channel_listener_t
*chan_l
)
3195 tor_assert(chan_l
->describe_transport
);
3197 return chan_l
->describe_transport(chan_l
);
3201 * Return the number of entries in <b>queue</b>
3204 chan_cell_queue_len(const chan_cell_queue_t
*queue
)
3207 cell_queue_entry_t
*cell
;
3208 TOR_SIMPLEQ_FOREACH(cell
, queue
, next
)
3214 * Dump channel statistics
3216 * Dump statistics for one channel to the log
3220 channel_dump_statistics(channel_t
*chan
, int severity
)
3222 double avg
, interval
, age
;
3223 time_t now
= time(NULL
);
3224 tor_addr_t remote_addr
;
3225 int have_remote_addr
;
3226 char *remote_addr_str
;
3230 age
= (double)(now
- chan
->timestamp_created
);
3232 tor_log(severity
, LD_GENERAL
,
3233 "Channel " U64_FORMAT
" (at %p) with transport %s is in state "
3235 U64_PRINTF_ARG(chan
->global_identifier
), chan
,
3236 channel_describe_transport(chan
),
3237 channel_state_to_string(chan
->state
), chan
->state
);
3238 tor_log(severity
, LD_GENERAL
,
3239 " * Channel " U64_FORMAT
" was created at " U64_FORMAT
3240 " (" U64_FORMAT
" seconds ago) "
3241 "and last active at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3242 U64_PRINTF_ARG(chan
->global_identifier
),
3243 U64_PRINTF_ARG(chan
->timestamp_created
),
3244 U64_PRINTF_ARG(now
- chan
->timestamp_created
),
3245 U64_PRINTF_ARG(chan
->timestamp_active
),
3246 U64_PRINTF_ARG(now
- chan
->timestamp_active
));
3248 /* Handle digest and nickname */
3249 if (!tor_digest_is_zero(chan
->identity_digest
)) {
3250 if (chan
->nickname
) {
3251 tor_log(severity
, LD_GENERAL
,
3252 " * Channel " U64_FORMAT
" says it is connected "
3253 "to an OR with digest %s and nickname %s",
3254 U64_PRINTF_ARG(chan
->global_identifier
),
3255 hex_str(chan
->identity_digest
, DIGEST_LEN
),
3258 tor_log(severity
, LD_GENERAL
,
3259 " * Channel " U64_FORMAT
" says it is connected "
3260 "to an OR with digest %s and no known nickname",
3261 U64_PRINTF_ARG(chan
->global_identifier
),
3262 hex_str(chan
->identity_digest
, DIGEST_LEN
));
3265 if (chan
->nickname
) {
3266 tor_log(severity
, LD_GENERAL
,
3267 " * Channel " U64_FORMAT
" does not know the digest"
3268 " of the OR it is connected to, but reports its nickname is %s",
3269 U64_PRINTF_ARG(chan
->global_identifier
),
3272 tor_log(severity
, LD_GENERAL
,
3273 " * Channel " U64_FORMAT
" does not know the digest"
3274 " or the nickname of the OR it is connected to",
3275 U64_PRINTF_ARG(chan
->global_identifier
));
3279 /* Handle remote address and descriptions */
3280 have_remote_addr
= channel_get_addr_if_possible(chan
, &remote_addr
);
3281 if (have_remote_addr
) {
3282 char *actual
= tor_strdup(channel_get_actual_remote_descr(chan
));
3283 remote_addr_str
= tor_dup_addr(&remote_addr
);
3284 tor_log(severity
, LD_GENERAL
,
3285 " * Channel " U64_FORMAT
" says its remote address"
3286 " is %s, and gives a canonical description of \"%s\" and an "
3287 "actual description of \"%s\"",
3288 U64_PRINTF_ARG(chan
->global_identifier
),
3289 safe_str(remote_addr_str
),
3290 safe_str(channel_get_canonical_remote_descr(chan
)),
3292 tor_free(remote_addr_str
);
3295 char *actual
= tor_strdup(channel_get_actual_remote_descr(chan
));
3296 tor_log(severity
, LD_GENERAL
,
3297 " * Channel " U64_FORMAT
" does not know its remote "
3298 "address, but gives a canonical description of \"%s\" and an "
3299 "actual description of \"%s\"",
3300 U64_PRINTF_ARG(chan
->global_identifier
),
3301 channel_get_canonical_remote_descr(chan
),
3307 tor_log(severity
, LD_GENERAL
,
3308 " * Channel " U64_FORMAT
" has these marks: %s %s %s "
3310 U64_PRINTF_ARG(chan
->global_identifier
),
3311 channel_is_bad_for_new_circs(chan
) ?
3312 "bad_for_new_circs" : "!bad_for_new_circs",
3313 channel_is_canonical(chan
) ?
3314 "canonical" : "!canonical",
3315 channel_is_canonical_is_reliable(chan
) ?
3316 "is_canonical_is_reliable" :
3317 "!is_canonical_is_reliable",
3318 channel_is_client(chan
) ?
3319 "client" : "!client",
3320 channel_is_local(chan
) ?
3322 channel_is_incoming(chan
) ?
3323 "incoming" : "outgoing");
3325 /* Describe queues */
3326 tor_log(severity
, LD_GENERAL
,
3327 " * Channel " U64_FORMAT
" has %d queued incoming cells"
3328 " and %d queued outgoing cells",
3329 U64_PRINTF_ARG(chan
->global_identifier
),
3330 chan_cell_queue_len(&chan
->incoming_queue
),
3331 chan_cell_queue_len(&chan
->outgoing_queue
));
3333 /* Describe circuits */
3334 tor_log(severity
, LD_GENERAL
,
3335 " * Channel " U64_FORMAT
" has %d active circuits out of"
3337 U64_PRINTF_ARG(chan
->global_identifier
),
3338 (chan
->cmux
!= NULL
) ?
3339 circuitmux_num_active_circuits(chan
->cmux
) : 0,
3340 (chan
->cmux
!= NULL
) ?
3341 circuitmux_num_circuits(chan
->cmux
) : 0);
3343 /* Describe timestamps */
3344 tor_log(severity
, LD_GENERAL
,
3345 " * Channel " U64_FORMAT
" was last used by a "
3346 "client at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3347 U64_PRINTF_ARG(chan
->global_identifier
),
3348 U64_PRINTF_ARG(chan
->timestamp_client
),
3349 U64_PRINTF_ARG(now
- chan
->timestamp_client
));
3350 tor_log(severity
, LD_GENERAL
,
3351 " * Channel " U64_FORMAT
" was last drained at "
3352 U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3353 U64_PRINTF_ARG(chan
->global_identifier
),
3354 U64_PRINTF_ARG(chan
->timestamp_drained
),
3355 U64_PRINTF_ARG(now
- chan
->timestamp_drained
));
3356 tor_log(severity
, LD_GENERAL
,
3357 " * Channel " U64_FORMAT
" last received a cell "
3358 "at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3359 U64_PRINTF_ARG(chan
->global_identifier
),
3360 U64_PRINTF_ARG(chan
->timestamp_recv
),
3361 U64_PRINTF_ARG(now
- chan
->timestamp_recv
));
3362 tor_log(severity
, LD_GENERAL
,
3363 " * Channel " U64_FORMAT
" last transmitted a cell "
3364 "at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3365 U64_PRINTF_ARG(chan
->global_identifier
),
3366 U64_PRINTF_ARG(chan
->timestamp_xmit
),
3367 U64_PRINTF_ARG(now
- chan
->timestamp_xmit
));
3369 /* Describe counters and rates */
3370 tor_log(severity
, LD_GENERAL
,
3371 " * Channel " U64_FORMAT
" has received "
3372 U64_FORMAT
" cells and transmitted " U64_FORMAT
,
3373 U64_PRINTF_ARG(chan
->global_identifier
),
3374 U64_PRINTF_ARG(chan
->n_cells_recved
),
3375 U64_PRINTF_ARG(chan
->n_cells_xmitted
));
3376 if (now
> chan
->timestamp_created
&&
3377 chan
->timestamp_created
> 0) {
3378 if (chan
->n_cells_recved
> 0) {
3379 avg
= (double)(chan
->n_cells_recved
) / age
;
3381 tor_log(severity
, LD_GENERAL
,
3382 " * Channel " U64_FORMAT
" has averaged %f "
3383 "cells received per second",
3384 U64_PRINTF_ARG(chan
->global_identifier
), avg
);
3385 } else if (avg
>= 0.0) {
3386 interval
= 1.0 / avg
;
3387 tor_log(severity
, LD_GENERAL
,
3388 " * Channel " U64_FORMAT
" has averaged %f "
3389 "seconds between received cells",
3390 U64_PRINTF_ARG(chan
->global_identifier
), interval
);
3393 if (chan
->n_cells_xmitted
> 0) {
3394 avg
= (double)(chan
->n_cells_xmitted
) / age
;
3396 tor_log(severity
, LD_GENERAL
,
3397 " * Channel " U64_FORMAT
" has averaged %f "
3398 "cells transmitted per second",
3399 U64_PRINTF_ARG(chan
->global_identifier
), avg
);
3400 } else if (avg
>= 0.0) {
3401 interval
= 1.0 / avg
;
3402 tor_log(severity
, LD_GENERAL
,
3403 " * Channel " U64_FORMAT
" has averaged %f "
3404 "seconds between transmitted cells",
3405 U64_PRINTF_ARG(chan
->global_identifier
), interval
);
3410 /* Dump anything the lower layer has to say */
3411 channel_dump_transport_statistics(chan
, severity
);
3415 * Dump channel listener statistics
3417 * Dump statistics for one channel listener to the log
3421 channel_listener_dump_statistics(channel_listener_t
*chan_l
, int severity
)
3423 double avg
, interval
, age
;
3424 time_t now
= time(NULL
);
3428 age
= (double)(now
- chan_l
->timestamp_created
);
3430 tor_log(severity
, LD_GENERAL
,
3431 "Channel listener " U64_FORMAT
" (at %p) with transport %s is in "
3433 U64_PRINTF_ARG(chan_l
->global_identifier
), chan_l
,
3434 channel_listener_describe_transport(chan_l
),
3435 channel_listener_state_to_string(chan_l
->state
), chan_l
->state
);
3436 tor_log(severity
, LD_GENERAL
,
3437 " * Channel listener " U64_FORMAT
" was created at " U64_FORMAT
3438 " (" U64_FORMAT
" seconds ago) "
3439 "and last active at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3440 U64_PRINTF_ARG(chan_l
->global_identifier
),
3441 U64_PRINTF_ARG(chan_l
->timestamp_created
),
3442 U64_PRINTF_ARG(now
- chan_l
->timestamp_created
),
3443 U64_PRINTF_ARG(chan_l
->timestamp_active
),
3444 U64_PRINTF_ARG(now
- chan_l
->timestamp_active
));
3446 tor_log(severity
, LD_GENERAL
,
3447 " * Channel listener " U64_FORMAT
" last accepted an incoming "
3448 "channel at " U64_FORMAT
" (" U64_FORMAT
" seconds ago) "
3449 "and has accepted " U64_FORMAT
" channels in total",
3450 U64_PRINTF_ARG(chan_l
->global_identifier
),
3451 U64_PRINTF_ARG(chan_l
->timestamp_accepted
),
3452 U64_PRINTF_ARG(now
- chan_l
->timestamp_accepted
),
3453 U64_PRINTF_ARG(chan_l
->n_accepted
));
3456 * If it's sensible to do so, get the rate of incoming channels on this
3459 if (now
> chan_l
->timestamp_created
&&
3460 chan_l
->timestamp_created
> 0 &&
3461 chan_l
->n_accepted
> 0) {
3462 avg
= (double)(chan_l
->n_accepted
) / age
;
3464 tor_log(severity
, LD_GENERAL
,
3465 " * Channel listener " U64_FORMAT
" has averaged %f incoming "
3466 "channels per second",
3467 U64_PRINTF_ARG(chan_l
->global_identifier
), avg
);
3468 } else if (avg
>= 0.0) {
3469 interval
= 1.0 / avg
;
3470 tor_log(severity
, LD_GENERAL
,
3471 " * Channel listener " U64_FORMAT
" has averaged %f seconds "
3472 "between incoming channels",
3473 U64_PRINTF_ARG(chan_l
->global_identifier
), interval
);
3477 /* Dump anything the lower layer has to say */
3478 channel_listener_dump_transport_statistics(chan_l
, severity
);
3482 * Invoke transport-specific stats dump for channel
3484 * If there is a lower-layer statistics dump method, invoke it
3488 channel_dump_transport_statistics(channel_t
*chan
, int severity
)
3492 if (chan
->dumpstats
) chan
->dumpstats(chan
, severity
);
3496 * Invoke transport-specific stats dump for channel listener
3498 * If there is a lower-layer statistics dump method, invoke it
3502 channel_listener_dump_transport_statistics(channel_listener_t
*chan_l
,
3507 if (chan_l
->dumpstats
) chan_l
->dumpstats(chan_l
, severity
);
3511 * Return text description of the remote endpoint
3513 * This function return a test provided by the lower layer of the remote
3514 * endpoint for this channel; it should specify the actual address connected
3517 * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
3518 * may invalidate the return value from this function.
3521 channel_get_actual_remote_descr(channel_t
*chan
)
3524 tor_assert(chan
->get_remote_descr
);
3526 /* Param 1 indicates the actual description */
3527 return chan
->get_remote_descr(chan
, GRD_FLAG_ORIGINAL
);
3531 * Return the text address of the remote endpoint.
3533 * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
3534 * may invalidate the return value from this function.
3537 channel_get_actual_remote_address(channel_t
*chan
)
3539 /* Param 1 indicates the actual description */
3540 return chan
->get_remote_descr(chan
, GRD_FLAG_ORIGINAL
|GRD_FLAG_ADDR_ONLY
);
3544 * Return text description of the remote endpoint canonical address
3546 * This function return a test provided by the lower layer of the remote
3547 * endpoint for this channel; it should use the known canonical address for
3548 * this OR's identity digest if possible.
3550 * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
3551 * may invalidate the return value from this function.
3554 channel_get_canonical_remote_descr(channel_t
*chan
)
3557 tor_assert(chan
->get_remote_descr
);
3559 /* Param 0 indicates the canonicalized description */
3560 return chan
->get_remote_descr(chan
, 0);
3564 * Get remote address if possible.
3566 * Write the remote address out to a tor_addr_t if the underlying transport
3567 * supports this operation, and return 1. Return 0 if the underlying transport
3568 * doesn't let us do this.
3571 channel_get_addr_if_possible(channel_t
*chan
, tor_addr_t
*addr_out
)
3574 tor_assert(addr_out
);
3576 if (chan
->get_remote_addr
)
3577 return chan
->get_remote_addr(chan
, addr_out
);
3578 /* Else no support, method not implemented */
3583 * Check if there are outgoing queue writes on this channel
3585 * Indicate if either we have queued cells, or if not, whether the underlying
3586 * lower-layer transport thinks it has an output queue.
3590 channel_has_queued_writes(channel_t
*chan
)
3595 tor_assert(chan
->has_queued_writes
);
3597 if (! TOR_SIMPLEQ_EMPTY(&chan
->outgoing_queue
)) {
3600 /* Check with the lower layer */
3601 has_writes
= chan
->has_queued_writes(chan
);
3608 * Check the is_bad_for_new_circs flag
3610 * This function returns the is_bad_for_new_circs flag of the specified
3615 channel_is_bad_for_new_circs(channel_t
*chan
)
3619 return chan
->is_bad_for_new_circs
;
3623 * Mark a channel as bad for new circuits
3625 * Set the is_bad_for_new_circs_flag on chan.
3629 channel_mark_bad_for_new_circs(channel_t
*chan
)
3633 chan
->is_bad_for_new_circs
= 1;
3637 * Get the client flag
3639 * This returns the client flag of a channel, which will be set if
3640 * command_process_create_cell() in command.c thinks this is a connection
3645 channel_is_client(channel_t
*chan
)
3649 return chan
->is_client
;
3653 * Set the client flag
3655 * Mark a channel as being from a client
3659 channel_mark_client(channel_t
*chan
)
3663 chan
->is_client
= 1;
3667 * Get the canonical flag for a channel
3669 * This returns the is_canonical for a channel; this flag is determined by
3670 * the lower layer and can't be set in a transport-independent way.
3674 channel_is_canonical(channel_t
*chan
)
3677 tor_assert(chan
->is_canonical
);
3679 return chan
->is_canonical(chan
, 0);
3683 * Test if the canonical flag is reliable
3685 * This function asks if the lower layer thinks it's safe to trust the
3686 * result of channel_is_canonical()
3690 channel_is_canonical_is_reliable(channel_t
*chan
)
3693 tor_assert(chan
->is_canonical
);
3695 return chan
->is_canonical(chan
, 1);
3699 * Test incoming flag
3701 * This function gets the incoming flag; this is set when a listener spawns
3702 * a channel. If this returns true the channel was remotely initiated.
3706 channel_is_incoming(channel_t
*chan
)
3710 return chan
->is_incoming
;
3714 * Set the incoming flag
3716 * This function is called when a channel arrives on a listening channel
3717 * to mark it as incoming.
3721 channel_mark_incoming(channel_t
*chan
)
3725 chan
->is_incoming
= 1;
3731 * This function gets the local flag; the lower layer should set this when
3732 * setting up the channel if is_local_addr() is true for all of the
3733 * destinations it will communicate with on behalf of this channel. It's
3734 * used to decide whether to declare the network reachable when seeing incoming
3735 * traffic on the channel.
3739 channel_is_local(channel_t
*chan
)
3743 return chan
->is_local
;
3747 * Set the local flag
3749 * This internal-only function should be called by the lower layer if the
3750 * channel is to a local address. See channel_is_local() above or the
3751 * description of the is_local bit in channel.h
3755 channel_mark_local(channel_t
*chan
)
3763 * Mark a channel as remote
3765 * This internal-only function should be called by the lower layer if the
3766 * channel is not to a local address but has previously been marked local.
3767 * See channel_is_local() above or the description of the is_local bit in
3772 channel_mark_remote(channel_t
*chan
)
3780 * Test outgoing flag
3782 * This function gets the outgoing flag; this is the inverse of the incoming
3783 * bit set when a listener spawns a channel. If this returns true the channel
3784 * was locally initiated.
3788 channel_is_outgoing(channel_t
*chan
)
3792 return !(chan
->is_incoming
);
3796 * Mark a channel as outgoing
3798 * This function clears the incoming flag and thus marks a channel as
3803 channel_mark_outgoing(channel_t
*chan
)
3807 chan
->is_incoming
= 0;
3810 /*********************
3811 * Timestamp updates *
3812 ********************/
3815 * Update the created timestamp for a channel
3817 * This updates the channel's created timestamp and should only be called
3818 * from channel_init().
3822 channel_timestamp_created(channel_t
*chan
)
3824 time_t now
= time(NULL
);
3828 chan
->timestamp_created
= now
;
3832 * Update the created timestamp for a channel listener
3834 * This updates the channel listener's created timestamp and should only be
3835 * called from channel_init_listener().
3839 channel_listener_timestamp_created(channel_listener_t
*chan_l
)
3841 time_t now
= time(NULL
);
3845 chan_l
->timestamp_created
= now
;
3849 * Update the last active timestamp for a channel
3851 * This function updates the channel's last active timestamp; it should be
3852 * called by the lower layer whenever there is activity on the channel which
3853 * does not lead to a cell being transmitted or received; the active timestamp
3854 * is also updated from channel_timestamp_recv() and channel_timestamp_xmit(),
3855 * but it should be updated for things like the v3 handshake and stuff that
3856 * produce activity only visible to the lower layer.
3860 channel_timestamp_active(channel_t
*chan
)
3862 time_t now
= time(NULL
);
3866 chan
->timestamp_active
= now
;
3870 * Update the last active timestamp for a channel listener
3874 channel_listener_timestamp_active(channel_listener_t
*chan_l
)
3876 time_t now
= time(NULL
);
3880 chan_l
->timestamp_active
= now
;
3884 * Update the last accepted timestamp.
3886 * This function updates the channel listener's last accepted timestamp; it
3887 * should be called whenever a new incoming channel is accepted on a
3892 channel_listener_timestamp_accepted(channel_listener_t
*chan_l
)
3894 time_t now
= time(NULL
);
3898 chan_l
->timestamp_active
= now
;
3899 chan_l
->timestamp_accepted
= now
;
3903 * Update client timestamp
3905 * This function is called by relay.c to timestamp a channel that appears to
3906 * be used as a client.
3910 channel_timestamp_client(channel_t
*chan
)
3912 time_t now
= time(NULL
);
3916 chan
->timestamp_client
= now
;
3920 * Update the last drained timestamp
3922 * This is called whenever we transmit a cell which leaves the outgoing cell
3923 * queue completely empty. It also updates the xmit time and the active time.
3927 channel_timestamp_drained(channel_t
*chan
)
3929 time_t now
= time(NULL
);
3933 chan
->timestamp_active
= now
;
3934 chan
->timestamp_drained
= now
;
3935 chan
->timestamp_xmit
= now
;
3939 * Update the recv timestamp
3941 * This is called whenever we get an incoming cell from the lower layer.
3942 * This also updates the active timestamp.
3946 channel_timestamp_recv(channel_t
*chan
)
3948 time_t now
= time(NULL
);
3952 chan
->timestamp_active
= now
;
3953 chan
->timestamp_recv
= now
;
3957 * Update the xmit timestamp
3958 * This is called whenever we pass an outgoing cell to the lower layer. This
3959 * also updates the active timestamp.
3963 channel_timestamp_xmit(channel_t
*chan
)
3965 time_t now
= time(NULL
);
3969 chan
->timestamp_active
= now
;
3970 chan
->timestamp_xmit
= now
;
3973 /***************************************************************
3974 * Timestamp queries - see above for definitions of timestamps *
3975 **************************************************************/
3978 * Query created timestamp for a channel
3982 channel_when_created(channel_t
*chan
)
3986 return chan
->timestamp_created
;
3990 * Query created timestamp for a channel listener
3994 channel_listener_when_created(channel_listener_t
*chan_l
)
3998 return chan_l
->timestamp_created
;
4002 * Query last active timestamp for a channel
4006 channel_when_last_active(channel_t
*chan
)
4010 return chan
->timestamp_active
;
4014 * Query last active timestamp for a channel listener
4018 channel_listener_when_last_active(channel_listener_t
*chan_l
)
4022 return chan_l
->timestamp_active
;
4026 * Query last accepted timestamp for a channel listener
4030 channel_listener_when_last_accepted(channel_listener_t
*chan_l
)
4034 return chan_l
->timestamp_accepted
;
4038 * Query client timestamp
4042 channel_when_last_client(channel_t
*chan
)
4046 return chan
->timestamp_client
;
4050 * Query drained timestamp
4054 channel_when_last_drained(channel_t
*chan
)
4058 return chan
->timestamp_drained
;
4062 * Query recv timestamp
4066 channel_when_last_recv(channel_t
*chan
)
4070 return chan
->timestamp_recv
;
4074 * Query xmit timestamp
4078 channel_when_last_xmit(channel_t
*chan
)
4082 return chan
->timestamp_xmit
;
4086 * Query accepted counter
4090 channel_listener_count_accepted(channel_listener_t
*chan_l
)
4094 return chan_l
->n_accepted
;
4098 * Query received cell counter
4102 channel_count_recved(channel_t
*chan
)
4106 return chan
->n_cells_recved
;
4110 * Query transmitted cell counter
4114 channel_count_xmitted(channel_t
*chan
)
4118 return chan
->n_cells_xmitted
;
4122 * Check if a channel matches an extend_info_t
4124 * This function calls the lower layer and asks if this channel matches a
4125 * given extend_info_t.
4129 channel_matches_extend_info(channel_t
*chan
, extend_info_t
*extend_info
)
4132 tor_assert(chan
->matches_extend_info
);
4133 tor_assert(extend_info
);
4135 return chan
->matches_extend_info(chan
, extend_info
);
4139 * Check if a channel matches a given target address; return true iff we do.
4141 * This function calls into the lower layer and asks if this channel thinks
4142 * it matches a given target address for circuit extension purposes.
4146 channel_matches_target_addr_for_extend(channel_t
*chan
,
4147 const tor_addr_t
*target
)
4150 tor_assert(chan
->matches_target
);
4153 return chan
->matches_target(chan
, target
);
4157 * Return the total number of circuits used by a channel
4159 * @param chan Channel to query
4160 * @return Number of circuits using this as n_chan or p_chan
4164 channel_num_circuits(channel_t
*chan
)
4168 return chan
->num_n_circuits
+
4169 chan
->num_p_circuits
;
4173 * Set up circuit ID generation
4175 * This is called when setting up a channel and replaces the old
4176 * connection_or_set_circid_type()
4179 channel_set_circid_type(channel_t
*chan
,
4180 crypto_pk_t
*identity_rcvd
,
4181 int consider_identity
)
4184 crypto_pk_t
*our_identity
;
4188 started_here
= channel_is_outgoing(chan
);
4190 if (! consider_identity
) {
4192 chan
->circ_id_type
= CIRC_ID_TYPE_HIGHER
;
4194 chan
->circ_id_type
= CIRC_ID_TYPE_LOWER
;
4198 our_identity
= started_here
?
4199 get_tlsclient_identity_key() : get_server_identity_key();
4201 if (identity_rcvd
) {
4202 if (crypto_pk_cmp_keys(our_identity
, identity_rcvd
) < 0) {
4203 chan
->circ_id_type
= CIRC_ID_TYPE_LOWER
;
4205 chan
->circ_id_type
= CIRC_ID_TYPE_HIGHER
;
4208 chan
->circ_id_type
= CIRC_ID_TYPE_NEITHER
;