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 /* Check to make sure we can send on this channel first */
2687 if (!(chan
->state
== CHANNEL_STATE_CLOSING
||
2688 chan
->state
== CHANNEL_STATE_CLOSED
||
2689 chan
->state
== CHANNEL_STATE_ERROR
) &&
2691 channel_note_destroy_pending(chan
, circ_id
);
2692 circuitmux_append_destroy_cell(chan
, chan
->cmux
, circ_id
, reason
);
2694 "Sending destroy (circID %u) on channel %p "
2695 "(global ID " U64_FORMAT
")",
2696 (unsigned)circ_id
, chan
,
2697 U64_PRINTF_ARG(chan
->global_identifier
));
2700 "Someone called channel_send_destroy() for circID %u "
2701 "on a channel " U64_FORMAT
" at %p in state %s (%d)",
2702 (unsigned)circ_id
, U64_PRINTF_ARG(chan
->global_identifier
),
2703 chan
, channel_state_to_string(chan
->state
),
2711 * Dump channel statistics to the log
2713 * This is called from dumpstats() in main.c and spams the log with
2714 * statistics on channels.
2718 channel_dumpstats(int severity
)
2720 if (all_channels
&& smartlist_len(all_channels
) > 0) {
2721 tor_log(severity
, LD_GENERAL
,
2722 "Dumping statistics about %d channels:",
2723 smartlist_len(all_channels
));
2724 tor_log(severity
, LD_GENERAL
,
2725 "%d are active, and %d are done and waiting for cleanup",
2726 (active_channels
!= NULL
) ?
2727 smartlist_len(active_channels
) : 0,
2728 (finished_channels
!= NULL
) ?
2729 smartlist_len(finished_channels
) : 0);
2731 SMARTLIST_FOREACH(all_channels
, channel_t
*, chan
,
2732 channel_dump_statistics(chan
, severity
));
2734 tor_log(severity
, LD_GENERAL
,
2735 "Done spamming about channels now");
2737 tor_log(severity
, LD_GENERAL
,
2738 "No channels to dump");
2743 * Dump channel listener statistics to the log
2745 * This is called from dumpstats() in main.c and spams the log with
2746 * statistics on channel listeners.
2750 channel_listener_dumpstats(int severity
)
2752 if (all_listeners
&& smartlist_len(all_listeners
) > 0) {
2753 tor_log(severity
, LD_GENERAL
,
2754 "Dumping statistics about %d channel listeners:",
2755 smartlist_len(all_listeners
));
2756 tor_log(severity
, LD_GENERAL
,
2757 "%d are active and %d are done and waiting for cleanup",
2758 (active_listeners
!= NULL
) ?
2759 smartlist_len(active_listeners
) : 0,
2760 (finished_listeners
!= NULL
) ?
2761 smartlist_len(finished_listeners
) : 0);
2763 SMARTLIST_FOREACH(all_listeners
, channel_listener_t
*, chan_l
,
2764 channel_listener_dump_statistics(chan_l
, severity
));
2766 tor_log(severity
, LD_GENERAL
,
2767 "Done spamming about channel listeners now");
2769 tor_log(severity
, LD_GENERAL
,
2770 "No channel listeners to dump");
2775 * Set the cmux policy on all active channels
2779 channel_set_cmux_policy_everywhere(circuitmux_policy_t
*pol
)
2781 if (!active_channels
) return;
2783 SMARTLIST_FOREACH_BEGIN(active_channels
, channel_t
*, curr
) {
2785 circuitmux_set_policy(curr
->cmux
, pol
);
2787 } SMARTLIST_FOREACH_END(curr
);
2793 * This gets called periodically from run_scheduled_events() in main.c;
2794 * it cleans up after closed channels.
2798 channel_run_cleanup(void)
2800 channel_t
*tmp
= NULL
;
2802 /* Check if we need to do anything */
2803 if (!finished_channels
|| smartlist_len(finished_channels
) == 0) return;
2805 /* Iterate through finished_channels and get rid of them */
2806 SMARTLIST_FOREACH_BEGIN(finished_channels
, channel_t
*, curr
) {
2808 /* Remove it from the list */
2809 SMARTLIST_DEL_CURRENT(finished_channels
, curr
);
2810 /* Also unregister it */
2811 channel_unregister(tmp
);
2812 /* ... and free it */
2814 } SMARTLIST_FOREACH_END(curr
);
2818 * Clean up channel listeners
2820 * This gets called periodically from run_scheduled_events() in main.c;
2821 * it cleans up after closed channel listeners.
2825 channel_listener_run_cleanup(void)
2827 channel_listener_t
*tmp
= NULL
;
2829 /* Check if we need to do anything */
2830 if (!finished_listeners
|| smartlist_len(finished_listeners
) == 0) return;
2832 /* Iterate through finished_channels and get rid of them */
2833 SMARTLIST_FOREACH_BEGIN(finished_listeners
, channel_listener_t
*, curr
) {
2835 /* Remove it from the list */
2836 SMARTLIST_DEL_CURRENT(finished_listeners
, curr
);
2837 /* Also unregister it */
2838 channel_listener_unregister(tmp
);
2839 /* ... and free it */
2840 channel_listener_free(tmp
);
2841 } SMARTLIST_FOREACH_END(curr
);
2845 * Free a list of channels for channel_free_all()
2849 channel_free_list(smartlist_t
*channels
, int mark_for_close
)
2851 if (!channels
) return;
2853 SMARTLIST_FOREACH_BEGIN(channels
, channel_t
*, curr
) {
2854 /* Deregister and free it */
2856 log_debug(LD_CHANNEL
,
2857 "Cleaning up channel %p (global ID " U64_FORMAT
") "
2859 curr
, U64_PRINTF_ARG(curr
->global_identifier
),
2860 channel_state_to_string(curr
->state
), curr
->state
);
2861 /* Detach circuits early so they can find the channel */
2863 circuitmux_detach_all_circuits(curr
->cmux
, NULL
);
2865 channel_unregister(curr
);
2866 if (mark_for_close
) {
2867 if (!(curr
->state
== CHANNEL_STATE_CLOSING
||
2868 curr
->state
== CHANNEL_STATE_CLOSED
||
2869 curr
->state
== CHANNEL_STATE_ERROR
)) {
2870 channel_mark_for_close(curr
);
2872 channel_force_free(curr
);
2873 } else channel_free(curr
);
2874 } SMARTLIST_FOREACH_END(curr
);
2878 * Free a list of channel listeners for channel_free_all()
2882 channel_listener_free_list(smartlist_t
*listeners
, int mark_for_close
)
2884 if (!listeners
) return;
2886 SMARTLIST_FOREACH_BEGIN(listeners
, channel_listener_t
*, curr
) {
2887 /* Deregister and free it */
2889 log_debug(LD_CHANNEL
,
2890 "Cleaning up channel listener %p (global ID " U64_FORMAT
") "
2892 curr
, U64_PRINTF_ARG(curr
->global_identifier
),
2893 channel_listener_state_to_string(curr
->state
), curr
->state
);
2894 channel_listener_unregister(curr
);
2895 if (mark_for_close
) {
2896 if (!(curr
->state
== CHANNEL_LISTENER_STATE_CLOSING
||
2897 curr
->state
== CHANNEL_LISTENER_STATE_CLOSED
||
2898 curr
->state
== CHANNEL_LISTENER_STATE_ERROR
)) {
2899 channel_listener_mark_for_close(curr
);
2901 channel_listener_force_free(curr
);
2902 } else channel_listener_free(curr
);
2903 } SMARTLIST_FOREACH_END(curr
);
2907 * Close all channels and free everything
2909 * This gets called from tor_free_all() in main.c to clean up on exit.
2910 * It will close all registered channels and free associated storage,
2911 * then free the all_channels, active_channels, listening_channels and
2912 * finished_channels lists and also channel_identity_map.
2916 channel_free_all(void)
2918 log_debug(LD_CHANNEL
,
2919 "Shutting down channels...");
2921 /* First, let's go for finished channels */
2922 if (finished_channels
) {
2923 channel_free_list(finished_channels
, 0);
2924 smartlist_free(finished_channels
);
2925 finished_channels
= NULL
;
2928 /* Now the finished listeners */
2929 if (finished_listeners
) {
2930 channel_listener_free_list(finished_listeners
, 0);
2931 smartlist_free(finished_listeners
);
2932 finished_listeners
= NULL
;
2935 /* Now all active channels */
2936 if (active_channels
) {
2937 channel_free_list(active_channels
, 1);
2938 smartlist_free(active_channels
);
2939 active_channels
= NULL
;
2942 /* Now all active listeners */
2943 if (active_listeners
) {
2944 channel_listener_free_list(active_listeners
, 1);
2945 smartlist_free(active_listeners
);
2946 active_listeners
= NULL
;
2949 /* Now all channels, in case any are left over */
2951 channel_free_list(all_channels
, 1);
2952 smartlist_free(all_channels
);
2953 all_channels
= NULL
;
2956 /* Now all listeners, in case any are left over */
2957 if (all_listeners
) {
2958 channel_listener_free_list(all_listeners
, 1);
2959 smartlist_free(all_listeners
);
2960 all_listeners
= NULL
;
2963 /* Now free channel_identity_map */
2964 log_debug(LD_CHANNEL
,
2965 "Freeing channel_identity_map");
2966 /* Geez, anything still left over just won't die ... let it leak then */
2967 HT_CLEAR(channel_idmap
, &channel_identity_map
);
2969 log_debug(LD_CHANNEL
,
2970 "Done cleaning up after channels");
2974 * Connect to a given addr/port/digest
2976 * This sets up a new outgoing channel; in the future if multiple
2977 * channel_t subclasses are available, this is where the selection policy
2978 * should go. It may also be desirable to fold port into tor_addr_t
2979 * or make a new type including a tor_addr_t and port, so we have a
2980 * single abstract object encapsulating all the protocol details of
2981 * how to contact an OR.
2985 channel_connect(const tor_addr_t
*addr
, uint16_t port
,
2986 const char *id_digest
)
2988 return channel_tls_connect(addr
, port
, id_digest
);
2992 * Decide which of two channels to prefer for extending a circuit
2994 * This function is called while extending a circuit and returns true iff
2995 * a is 'better' than b. The most important criterion here is that a
2996 * canonical channel is always better than a non-canonical one, but the
2997 * number of circuits and the age are used as tie-breakers.
2999 * This is based on the former connection_or_is_better() of connection_or.c
3003 channel_is_better(time_t now
, channel_t
*a
, channel_t
*b
,
3004 int forgive_new_connections
)
3006 int a_grace
, b_grace
;
3007 int a_is_canonical
, b_is_canonical
;
3008 int a_has_circs
, b_has_circs
;
3011 * Do not definitively deprecate a new channel with no circuits on it
3012 * until this much time has passed.
3014 #define NEW_CHAN_GRACE_PERIOD (15*60)
3019 /* Check if one is canonical and the other isn't first */
3020 a_is_canonical
= channel_is_canonical(a
);
3021 b_is_canonical
= channel_is_canonical(b
);
3023 if (a_is_canonical
&& !b_is_canonical
) return 1;
3024 if (!a_is_canonical
&& b_is_canonical
) return 0;
3027 * Okay, if we're here they tied on canonicity. Next we check if
3028 * they have any circuits, and if one does and the other doesn't,
3029 * we prefer the one that does, unless we are forgiving and the
3030 * one that has no circuits is in its grace period.
3033 a_has_circs
= (channel_num_circuits(a
) > 0);
3034 b_has_circs
= (channel_num_circuits(b
) > 0);
3035 a_grace
= (forgive_new_connections
&&
3036 (now
< channel_when_created(a
) + NEW_CHAN_GRACE_PERIOD
));
3037 b_grace
= (forgive_new_connections
&&
3038 (now
< channel_when_created(b
) + NEW_CHAN_GRACE_PERIOD
));
3040 if (a_has_circs
&& !b_has_circs
&& !b_grace
) return 1;
3041 if (!a_has_circs
&& b_has_circs
&& !a_grace
) return 0;
3043 /* They tied on circuits too; just prefer whichever is newer */
3045 if (channel_when_created(a
) > channel_when_created(b
)) return 1;
3050 * Get a channel to extend a circuit
3052 * Pick a suitable channel to extend a circuit to given the desired digest
3053 * the address we believe is correct for that digest; this tries to see
3054 * if we already have one for the requested endpoint, but if there is no good
3055 * channel, set *msg_out to a message describing the channel's state
3056 * and our next action, and set *launch_out to a boolean indicated whether
3057 * the caller should try to launch a new channel with channel_connect().
3061 channel_get_for_extend(const char *digest
,
3062 const tor_addr_t
*target_addr
,
3063 const char **msg_out
,
3066 channel_t
*chan
, *best
= NULL
;
3067 int n_inprogress_goodaddr
= 0, n_old
= 0;
3068 int n_noncanonical
= 0, n_possible
= 0;
3069 time_t now
= approx_time();
3071 tor_assert(msg_out
);
3072 tor_assert(launch_out
);
3074 chan
= channel_find_by_remote_digest(digest
);
3076 /* Walk the list, unrefing the old one and refing the new at each
3079 for (; chan
; chan
= channel_next_with_digest(chan
)) {
3080 tor_assert(tor_memeq(chan
->identity_digest
,
3081 digest
, DIGEST_LEN
));
3083 if (chan
->state
== CHANNEL_STATE_CLOSING
||
3084 chan
->state
== CHANNEL_STATE_CLOSED
||
3085 chan
->state
== CHANNEL_STATE_ERROR
)
3088 /* Never return a channel on which the other end appears to be
3090 if (channel_is_client(chan
)) {
3094 /* Never return a non-open connection. */
3095 if (chan
->state
!= CHANNEL_STATE_OPEN
) {
3096 /* If the address matches, don't launch a new connection for this
3098 if (channel_matches_target_addr_for_extend(chan
, target_addr
))
3099 ++n_inprogress_goodaddr
;
3103 /* Never return a connection that shouldn't be used for circs. */
3104 if (channel_is_bad_for_new_circs(chan
)) {
3109 /* Never return a non-canonical connection using a recent link protocol
3110 * if the address is not what we wanted.
3112 * The channel_is_canonical_is_reliable() function asks the lower layer
3113 * if we should trust channel_is_canonical(). The below is from the
3114 * comments of the old circuit_or_get_for_extend() and applies when
3115 * the lower-layer transport is channel_tls_t.
3117 * (For old link protocols, we can't rely on is_canonical getting
3118 * set properly if we're talking to the right address, since we might
3119 * have an out-of-date descriptor, and we will get no NETINFO cell to
3120 * tell us about the right address.)
3122 if (!channel_is_canonical(chan
) &&
3123 channel_is_canonical_is_reliable(chan
) &&
3124 !channel_matches_target_addr_for_extend(chan
, target_addr
)) {
3132 best
= chan
; /* If we have no 'best' so far, this one is good enough. */
3136 if (channel_is_better(now
, chan
, best
, 0))
3141 *msg_out
= "Connection is fine; using it.";
3144 } else if (n_inprogress_goodaddr
) {
3145 *msg_out
= "Connection in progress; waiting.";
3148 } else if (n_old
|| n_noncanonical
) {
3149 *msg_out
= "Connections all too old, or too non-canonical. "
3150 " Launching a new one.";
3154 *msg_out
= "Not connected. Connecting.";
3161 * Describe the transport subclass for a channel
3163 * Invoke a method to get a string description of the lower-layer
3164 * transport for this channel.
3168 channel_describe_transport(channel_t
*chan
)
3171 tor_assert(chan
->describe_transport
);
3173 return chan
->describe_transport(chan
);
3177 * Describe the transport subclass for a channel listener
3179 * Invoke a method to get a string description of the lower-layer
3180 * transport for this channel listener.
3184 channel_listener_describe_transport(channel_listener_t
*chan_l
)
3187 tor_assert(chan_l
->describe_transport
);
3189 return chan_l
->describe_transport(chan_l
);
3193 * Return the number of entries in <b>queue</b>
3196 chan_cell_queue_len(const chan_cell_queue_t
*queue
)
3199 cell_queue_entry_t
*cell
;
3200 TOR_SIMPLEQ_FOREACH(cell
, queue
, next
)
3206 * Dump channel statistics
3208 * Dump statistics for one channel to the log
3212 channel_dump_statistics(channel_t
*chan
, int severity
)
3214 double avg
, interval
, age
;
3215 time_t now
= time(NULL
);
3216 tor_addr_t remote_addr
;
3217 int have_remote_addr
;
3218 char *remote_addr_str
;
3222 age
= (double)(now
- chan
->timestamp_created
);
3224 tor_log(severity
, LD_GENERAL
,
3225 "Channel " U64_FORMAT
" (at %p) with transport %s is in state "
3227 U64_PRINTF_ARG(chan
->global_identifier
), chan
,
3228 channel_describe_transport(chan
),
3229 channel_state_to_string(chan
->state
), chan
->state
);
3230 tor_log(severity
, LD_GENERAL
,
3231 " * Channel " U64_FORMAT
" was created at " U64_FORMAT
3232 " (" U64_FORMAT
" seconds ago) "
3233 "and last active at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3234 U64_PRINTF_ARG(chan
->global_identifier
),
3235 U64_PRINTF_ARG(chan
->timestamp_created
),
3236 U64_PRINTF_ARG(now
- chan
->timestamp_created
),
3237 U64_PRINTF_ARG(chan
->timestamp_active
),
3238 U64_PRINTF_ARG(now
- chan
->timestamp_active
));
3240 /* Handle digest and nickname */
3241 if (!tor_digest_is_zero(chan
->identity_digest
)) {
3242 if (chan
->nickname
) {
3243 tor_log(severity
, LD_GENERAL
,
3244 " * Channel " U64_FORMAT
" says it is connected "
3245 "to an OR with digest %s and nickname %s",
3246 U64_PRINTF_ARG(chan
->global_identifier
),
3247 hex_str(chan
->identity_digest
, DIGEST_LEN
),
3250 tor_log(severity
, LD_GENERAL
,
3251 " * Channel " U64_FORMAT
" says it is connected "
3252 "to an OR with digest %s and no known nickname",
3253 U64_PRINTF_ARG(chan
->global_identifier
),
3254 hex_str(chan
->identity_digest
, DIGEST_LEN
));
3257 if (chan
->nickname
) {
3258 tor_log(severity
, LD_GENERAL
,
3259 " * Channel " U64_FORMAT
" does not know the digest"
3260 " of the OR it is connected to, but reports its nickname is %s",
3261 U64_PRINTF_ARG(chan
->global_identifier
),
3264 tor_log(severity
, LD_GENERAL
,
3265 " * Channel " U64_FORMAT
" does not know the digest"
3266 " or the nickname of the OR it is connected to",
3267 U64_PRINTF_ARG(chan
->global_identifier
));
3271 /* Handle remote address and descriptions */
3272 have_remote_addr
= channel_get_addr_if_possible(chan
, &remote_addr
);
3273 if (have_remote_addr
) {
3274 char *actual
= tor_strdup(channel_get_actual_remote_descr(chan
));
3275 remote_addr_str
= tor_dup_addr(&remote_addr
);
3276 tor_log(severity
, LD_GENERAL
,
3277 " * Channel " U64_FORMAT
" says its remote address"
3278 " is %s, and gives a canonical description of \"%s\" and an "
3279 "actual description of \"%s\"",
3280 U64_PRINTF_ARG(chan
->global_identifier
),
3281 safe_str(remote_addr_str
),
3282 safe_str(channel_get_canonical_remote_descr(chan
)),
3284 tor_free(remote_addr_str
);
3287 char *actual
= tor_strdup(channel_get_actual_remote_descr(chan
));
3288 tor_log(severity
, LD_GENERAL
,
3289 " * Channel " U64_FORMAT
" does not know its remote "
3290 "address, but gives a canonical description of \"%s\" and an "
3291 "actual description of \"%s\"",
3292 U64_PRINTF_ARG(chan
->global_identifier
),
3293 channel_get_canonical_remote_descr(chan
),
3299 tor_log(severity
, LD_GENERAL
,
3300 " * Channel " U64_FORMAT
" has these marks: %s %s %s "
3302 U64_PRINTF_ARG(chan
->global_identifier
),
3303 channel_is_bad_for_new_circs(chan
) ?
3304 "bad_for_new_circs" : "!bad_for_new_circs",
3305 channel_is_canonical(chan
) ?
3306 "canonical" : "!canonical",
3307 channel_is_canonical_is_reliable(chan
) ?
3308 "is_canonical_is_reliable" :
3309 "!is_canonical_is_reliable",
3310 channel_is_client(chan
) ?
3311 "client" : "!client",
3312 channel_is_local(chan
) ?
3314 channel_is_incoming(chan
) ?
3315 "incoming" : "outgoing");
3317 /* Describe queues */
3318 tor_log(severity
, LD_GENERAL
,
3319 " * Channel " U64_FORMAT
" has %d queued incoming cells"
3320 " and %d queued outgoing cells",
3321 U64_PRINTF_ARG(chan
->global_identifier
),
3322 chan_cell_queue_len(&chan
->incoming_queue
),
3323 chan_cell_queue_len(&chan
->outgoing_queue
));
3325 /* Describe circuits */
3326 tor_log(severity
, LD_GENERAL
,
3327 " * Channel " U64_FORMAT
" has %d active circuits out of"
3329 U64_PRINTF_ARG(chan
->global_identifier
),
3330 (chan
->cmux
!= NULL
) ?
3331 circuitmux_num_active_circuits(chan
->cmux
) : 0,
3332 (chan
->cmux
!= NULL
) ?
3333 circuitmux_num_circuits(chan
->cmux
) : 0);
3335 /* Describe timestamps */
3336 tor_log(severity
, LD_GENERAL
,
3337 " * Channel " U64_FORMAT
" was last used by a "
3338 "client at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3339 U64_PRINTF_ARG(chan
->global_identifier
),
3340 U64_PRINTF_ARG(chan
->timestamp_client
),
3341 U64_PRINTF_ARG(now
- chan
->timestamp_client
));
3342 tor_log(severity
, LD_GENERAL
,
3343 " * Channel " U64_FORMAT
" was last drained at "
3344 U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3345 U64_PRINTF_ARG(chan
->global_identifier
),
3346 U64_PRINTF_ARG(chan
->timestamp_drained
),
3347 U64_PRINTF_ARG(now
- chan
->timestamp_drained
));
3348 tor_log(severity
, LD_GENERAL
,
3349 " * Channel " U64_FORMAT
" last received a cell "
3350 "at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3351 U64_PRINTF_ARG(chan
->global_identifier
),
3352 U64_PRINTF_ARG(chan
->timestamp_recv
),
3353 U64_PRINTF_ARG(now
- chan
->timestamp_recv
));
3354 tor_log(severity
, LD_GENERAL
,
3355 " * Channel " U64_FORMAT
" last transmitted a cell "
3356 "at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3357 U64_PRINTF_ARG(chan
->global_identifier
),
3358 U64_PRINTF_ARG(chan
->timestamp_xmit
),
3359 U64_PRINTF_ARG(now
- chan
->timestamp_xmit
));
3361 /* Describe counters and rates */
3362 tor_log(severity
, LD_GENERAL
,
3363 " * Channel " U64_FORMAT
" has received "
3364 U64_FORMAT
" cells and transmitted " U64_FORMAT
,
3365 U64_PRINTF_ARG(chan
->global_identifier
),
3366 U64_PRINTF_ARG(chan
->n_cells_recved
),
3367 U64_PRINTF_ARG(chan
->n_cells_xmitted
));
3368 if (now
> chan
->timestamp_created
&&
3369 chan
->timestamp_created
> 0) {
3370 if (chan
->n_cells_recved
> 0) {
3371 avg
= (double)(chan
->n_cells_recved
) / age
;
3373 tor_log(severity
, LD_GENERAL
,
3374 " * Channel " U64_FORMAT
" has averaged %f "
3375 "cells received per second",
3376 U64_PRINTF_ARG(chan
->global_identifier
), avg
);
3377 } else if (avg
>= 0.0) {
3378 interval
= 1.0 / avg
;
3379 tor_log(severity
, LD_GENERAL
,
3380 " * Channel " U64_FORMAT
" has averaged %f "
3381 "seconds between received cells",
3382 U64_PRINTF_ARG(chan
->global_identifier
), interval
);
3385 if (chan
->n_cells_xmitted
> 0) {
3386 avg
= (double)(chan
->n_cells_xmitted
) / age
;
3388 tor_log(severity
, LD_GENERAL
,
3389 " * Channel " U64_FORMAT
" has averaged %f "
3390 "cells transmitted per second",
3391 U64_PRINTF_ARG(chan
->global_identifier
), avg
);
3392 } else if (avg
>= 0.0) {
3393 interval
= 1.0 / avg
;
3394 tor_log(severity
, LD_GENERAL
,
3395 " * Channel " U64_FORMAT
" has averaged %f "
3396 "seconds between transmitted cells",
3397 U64_PRINTF_ARG(chan
->global_identifier
), interval
);
3402 /* Dump anything the lower layer has to say */
3403 channel_dump_transport_statistics(chan
, severity
);
3407 * Dump channel listener statistics
3409 * Dump statistics for one channel listener to the log
3413 channel_listener_dump_statistics(channel_listener_t
*chan_l
, int severity
)
3415 double avg
, interval
, age
;
3416 time_t now
= time(NULL
);
3420 age
= (double)(now
- chan_l
->timestamp_created
);
3422 tor_log(severity
, LD_GENERAL
,
3423 "Channel listener " U64_FORMAT
" (at %p) with transport %s is in "
3425 U64_PRINTF_ARG(chan_l
->global_identifier
), chan_l
,
3426 channel_listener_describe_transport(chan_l
),
3427 channel_listener_state_to_string(chan_l
->state
), chan_l
->state
);
3428 tor_log(severity
, LD_GENERAL
,
3429 " * Channel listener " U64_FORMAT
" was created at " U64_FORMAT
3430 " (" U64_FORMAT
" seconds ago) "
3431 "and last active at " U64_FORMAT
" (" U64_FORMAT
" seconds ago)",
3432 U64_PRINTF_ARG(chan_l
->global_identifier
),
3433 U64_PRINTF_ARG(chan_l
->timestamp_created
),
3434 U64_PRINTF_ARG(now
- chan_l
->timestamp_created
),
3435 U64_PRINTF_ARG(chan_l
->timestamp_active
),
3436 U64_PRINTF_ARG(now
- chan_l
->timestamp_active
));
3438 tor_log(severity
, LD_GENERAL
,
3439 " * Channel listener " U64_FORMAT
" last accepted an incoming "
3440 "channel at " U64_FORMAT
" (" U64_FORMAT
" seconds ago) "
3441 "and has accepted " U64_FORMAT
" channels in total",
3442 U64_PRINTF_ARG(chan_l
->global_identifier
),
3443 U64_PRINTF_ARG(chan_l
->timestamp_accepted
),
3444 U64_PRINTF_ARG(now
- chan_l
->timestamp_accepted
),
3445 U64_PRINTF_ARG(chan_l
->n_accepted
));
3448 * If it's sensible to do so, get the rate of incoming channels on this
3451 if (now
> chan_l
->timestamp_created
&&
3452 chan_l
->timestamp_created
> 0 &&
3453 chan_l
->n_accepted
> 0) {
3454 avg
= (double)(chan_l
->n_accepted
) / age
;
3456 tor_log(severity
, LD_GENERAL
,
3457 " * Channel listener " U64_FORMAT
" has averaged %f incoming "
3458 "channels per second",
3459 U64_PRINTF_ARG(chan_l
->global_identifier
), avg
);
3460 } else if (avg
>= 0.0) {
3461 interval
= 1.0 / avg
;
3462 tor_log(severity
, LD_GENERAL
,
3463 " * Channel listener " U64_FORMAT
" has averaged %f seconds "
3464 "between incoming channels",
3465 U64_PRINTF_ARG(chan_l
->global_identifier
), interval
);
3469 /* Dump anything the lower layer has to say */
3470 channel_listener_dump_transport_statistics(chan_l
, severity
);
3474 * Invoke transport-specific stats dump for channel
3476 * If there is a lower-layer statistics dump method, invoke it
3480 channel_dump_transport_statistics(channel_t
*chan
, int severity
)
3484 if (chan
->dumpstats
) chan
->dumpstats(chan
, severity
);
3488 * Invoke transport-specific stats dump for channel listener
3490 * If there is a lower-layer statistics dump method, invoke it
3494 channel_listener_dump_transport_statistics(channel_listener_t
*chan_l
,
3499 if (chan_l
->dumpstats
) chan_l
->dumpstats(chan_l
, severity
);
3503 * Return text description of the remote endpoint
3505 * This function return a test provided by the lower layer of the remote
3506 * endpoint for this channel; it should specify the actual address connected
3509 * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
3510 * may invalidate the return value from this function.
3513 channel_get_actual_remote_descr(channel_t
*chan
)
3516 tor_assert(chan
->get_remote_descr
);
3518 /* Param 1 indicates the actual description */
3519 return chan
->get_remote_descr(chan
, GRD_FLAG_ORIGINAL
);
3523 * Return the text address of the remote endpoint.
3525 * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
3526 * may invalidate the return value from this function.
3529 channel_get_actual_remote_address(channel_t
*chan
)
3531 /* Param 1 indicates the actual description */
3532 return chan
->get_remote_descr(chan
, GRD_FLAG_ORIGINAL
|GRD_FLAG_ADDR_ONLY
);
3536 * Return text description of the remote endpoint canonical address
3538 * This function return a test provided by the lower layer of the remote
3539 * endpoint for this channel; it should use the known canonical address for
3540 * this OR's identity digest if possible.
3542 * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
3543 * may invalidate the return value from this function.
3546 channel_get_canonical_remote_descr(channel_t
*chan
)
3549 tor_assert(chan
->get_remote_descr
);
3551 /* Param 0 indicates the canonicalized description */
3552 return chan
->get_remote_descr(chan
, 0);
3556 * Get remote address if possible.
3558 * Write the remote address out to a tor_addr_t if the underlying transport
3559 * supports this operation, and return 1. Return 0 if the underlying transport
3560 * doesn't let us do this.
3563 channel_get_addr_if_possible(channel_t
*chan
, tor_addr_t
*addr_out
)
3566 tor_assert(addr_out
);
3568 if (chan
->get_remote_addr
)
3569 return chan
->get_remote_addr(chan
, addr_out
);
3570 /* Else no support, method not implemented */
3575 * Check if there are outgoing queue writes on this channel
3577 * Indicate if either we have queued cells, or if not, whether the underlying
3578 * lower-layer transport thinks it has an output queue.
3582 channel_has_queued_writes(channel_t
*chan
)
3587 tor_assert(chan
->has_queued_writes
);
3589 if (! TOR_SIMPLEQ_EMPTY(&chan
->outgoing_queue
)) {
3592 /* Check with the lower layer */
3593 has_writes
= chan
->has_queued_writes(chan
);
3600 * Check the is_bad_for_new_circs flag
3602 * This function returns the is_bad_for_new_circs flag of the specified
3607 channel_is_bad_for_new_circs(channel_t
*chan
)
3611 return chan
->is_bad_for_new_circs
;
3615 * Mark a channel as bad for new circuits
3617 * Set the is_bad_for_new_circs_flag on chan.
3621 channel_mark_bad_for_new_circs(channel_t
*chan
)
3625 chan
->is_bad_for_new_circs
= 1;
3629 * Get the client flag
3631 * This returns the client flag of a channel, which will be set if
3632 * command_process_create_cell() in command.c thinks this is a connection
3637 channel_is_client(channel_t
*chan
)
3641 return chan
->is_client
;
3645 * Set the client flag
3647 * Mark a channel as being from a client
3651 channel_mark_client(channel_t
*chan
)
3655 chan
->is_client
= 1;
3659 * Get the canonical flag for a channel
3661 * This returns the is_canonical for a channel; this flag is determined by
3662 * the lower layer and can't be set in a transport-independent way.
3666 channel_is_canonical(channel_t
*chan
)
3669 tor_assert(chan
->is_canonical
);
3671 return chan
->is_canonical(chan
, 0);
3675 * Test if the canonical flag is reliable
3677 * This function asks if the lower layer thinks it's safe to trust the
3678 * result of channel_is_canonical()
3682 channel_is_canonical_is_reliable(channel_t
*chan
)
3685 tor_assert(chan
->is_canonical
);
3687 return chan
->is_canonical(chan
, 1);
3691 * Test incoming flag
3693 * This function gets the incoming flag; this is set when a listener spawns
3694 * a channel. If this returns true the channel was remotely initiated.
3698 channel_is_incoming(channel_t
*chan
)
3702 return chan
->is_incoming
;
3706 * Set the incoming flag
3708 * This function is called when a channel arrives on a listening channel
3709 * to mark it as incoming.
3713 channel_mark_incoming(channel_t
*chan
)
3717 chan
->is_incoming
= 1;
3723 * This function gets the local flag; the lower layer should set this when
3724 * setting up the channel if is_local_addr() is true for all of the
3725 * destinations it will communicate with on behalf of this channel. It's
3726 * used to decide whether to declare the network reachable when seeing incoming
3727 * traffic on the channel.
3731 channel_is_local(channel_t
*chan
)
3735 return chan
->is_local
;
3739 * Set the local flag
3741 * This internal-only function should be called by the lower layer if the
3742 * channel is to a local address. See channel_is_local() above or the
3743 * description of the is_local bit in channel.h
3747 channel_mark_local(channel_t
*chan
)
3755 * Test outgoing flag
3757 * This function gets the outgoing flag; this is the inverse of the incoming
3758 * bit set when a listener spawns a channel. If this returns true the channel
3759 * was locally initiated.
3763 channel_is_outgoing(channel_t
*chan
)
3767 return !(chan
->is_incoming
);
3771 * Mark a channel as outgoing
3773 * This function clears the incoming flag and thus marks a channel as
3778 channel_mark_outgoing(channel_t
*chan
)
3782 chan
->is_incoming
= 0;
3785 /*********************
3786 * Timestamp updates *
3787 ********************/
3790 * Update the created timestamp for a channel
3792 * This updates the channel's created timestamp and should only be called
3793 * from channel_init().
3797 channel_timestamp_created(channel_t
*chan
)
3799 time_t now
= time(NULL
);
3803 chan
->timestamp_created
= now
;
3807 * Update the created timestamp for a channel listener
3809 * This updates the channel listener's created timestamp and should only be
3810 * called from channel_init_listener().
3814 channel_listener_timestamp_created(channel_listener_t
*chan_l
)
3816 time_t now
= time(NULL
);
3820 chan_l
->timestamp_created
= now
;
3824 * Update the last active timestamp for a channel
3826 * This function updates the channel's last active timestamp; it should be
3827 * called by the lower layer whenever there is activity on the channel which
3828 * does not lead to a cell being transmitted or received; the active timestamp
3829 * is also updated from channel_timestamp_recv() and channel_timestamp_xmit(),
3830 * but it should be updated for things like the v3 handshake and stuff that
3831 * produce activity only visible to the lower layer.
3835 channel_timestamp_active(channel_t
*chan
)
3837 time_t now
= time(NULL
);
3841 chan
->timestamp_active
= now
;
3845 * Update the last active timestamp for a channel listener
3849 channel_listener_timestamp_active(channel_listener_t
*chan_l
)
3851 time_t now
= time(NULL
);
3855 chan_l
->timestamp_active
= now
;
3859 * Update the last accepted timestamp.
3861 * This function updates the channel listener's last accepted timestamp; it
3862 * should be called whenever a new incoming channel is accepted on a
3867 channel_listener_timestamp_accepted(channel_listener_t
*chan_l
)
3869 time_t now
= time(NULL
);
3873 chan_l
->timestamp_active
= now
;
3874 chan_l
->timestamp_accepted
= now
;
3878 * Update client timestamp
3880 * This function is called by relay.c to timestamp a channel that appears to
3881 * be used as a client.
3885 channel_timestamp_client(channel_t
*chan
)
3887 time_t now
= time(NULL
);
3891 chan
->timestamp_client
= now
;
3895 * Update the last drained timestamp
3897 * This is called whenever we transmit a cell which leaves the outgoing cell
3898 * queue completely empty. It also updates the xmit time and the active time.
3902 channel_timestamp_drained(channel_t
*chan
)
3904 time_t now
= time(NULL
);
3908 chan
->timestamp_active
= now
;
3909 chan
->timestamp_drained
= now
;
3910 chan
->timestamp_xmit
= now
;
3914 * Update the recv timestamp
3916 * This is called whenever we get an incoming cell from the lower layer.
3917 * This also updates the active timestamp.
3921 channel_timestamp_recv(channel_t
*chan
)
3923 time_t now
= time(NULL
);
3927 chan
->timestamp_active
= now
;
3928 chan
->timestamp_recv
= now
;
3932 * Update the xmit timestamp
3933 * This is called whenever we pass an outgoing cell to the lower layer. This
3934 * also updates the active timestamp.
3938 channel_timestamp_xmit(channel_t
*chan
)
3940 time_t now
= time(NULL
);
3944 chan
->timestamp_active
= now
;
3945 chan
->timestamp_xmit
= now
;
3948 /***************************************************************
3949 * Timestamp queries - see above for definitions of timestamps *
3950 **************************************************************/
3953 * Query created timestamp for a channel
3957 channel_when_created(channel_t
*chan
)
3961 return chan
->timestamp_created
;
3965 * Query created timestamp for a channel listener
3969 channel_listener_when_created(channel_listener_t
*chan_l
)
3973 return chan_l
->timestamp_created
;
3977 * Query last active timestamp for a channel
3981 channel_when_last_active(channel_t
*chan
)
3985 return chan
->timestamp_active
;
3989 * Query last active timestamp for a channel listener
3993 channel_listener_when_last_active(channel_listener_t
*chan_l
)
3997 return chan_l
->timestamp_active
;
4001 * Query last accepted timestamp for a channel listener
4005 channel_listener_when_last_accepted(channel_listener_t
*chan_l
)
4009 return chan_l
->timestamp_accepted
;
4013 * Query client timestamp
4017 channel_when_last_client(channel_t
*chan
)
4021 return chan
->timestamp_client
;
4025 * Query drained timestamp
4029 channel_when_last_drained(channel_t
*chan
)
4033 return chan
->timestamp_drained
;
4037 * Query recv timestamp
4041 channel_when_last_recv(channel_t
*chan
)
4045 return chan
->timestamp_recv
;
4049 * Query xmit timestamp
4053 channel_when_last_xmit(channel_t
*chan
)
4057 return chan
->timestamp_xmit
;
4061 * Query accepted counter
4065 channel_listener_count_accepted(channel_listener_t
*chan_l
)
4069 return chan_l
->n_accepted
;
4073 * Query received cell counter
4077 channel_count_recved(channel_t
*chan
)
4081 return chan
->n_cells_recved
;
4085 * Query transmitted cell counter
4089 channel_count_xmitted(channel_t
*chan
)
4093 return chan
->n_cells_xmitted
;
4097 * Check if a channel matches an extend_info_t
4099 * This function calls the lower layer and asks if this channel matches a
4100 * given extend_info_t.
4104 channel_matches_extend_info(channel_t
*chan
, extend_info_t
*extend_info
)
4107 tor_assert(chan
->matches_extend_info
);
4108 tor_assert(extend_info
);
4110 return chan
->matches_extend_info(chan
, extend_info
);
4114 * Check if a channel matches a given target address; return true iff we do.
4116 * This function calls into the lower layer and asks if this channel thinks
4117 * it matches a given target address for circuit extension purposes.
4121 channel_matches_target_addr_for_extend(channel_t
*chan
,
4122 const tor_addr_t
*target
)
4125 tor_assert(chan
->matches_target
);
4128 return chan
->matches_target(chan
, target
);
4132 * Return the total number of circuits used by a channel
4134 * @param chan Channel to query
4135 * @return Number of circuits using this as n_chan or p_chan
4139 channel_num_circuits(channel_t
*chan
)
4143 return chan
->num_n_circuits
+
4144 chan
->num_p_circuits
;
4148 * Set up circuit ID generation
4150 * This is called when setting up a channel and replaces the old
4151 * connection_or_set_circid_type()
4154 channel_set_circid_type(channel_t
*chan
,
4155 crypto_pk_t
*identity_rcvd
,
4156 int consider_identity
)
4159 crypto_pk_t
*our_identity
;
4163 started_here
= channel_is_outgoing(chan
);
4165 if (! consider_identity
) {
4167 chan
->circ_id_type
= CIRC_ID_TYPE_HIGHER
;
4169 chan
->circ_id_type
= CIRC_ID_TYPE_LOWER
;
4173 our_identity
= started_here
?
4174 get_tlsclient_identity_key() : get_server_identity_key();
4176 if (identity_rcvd
) {
4177 if (crypto_pk_cmp_keys(our_identity
, identity_rcvd
) < 0) {
4178 chan
->circ_id_type
= CIRC_ID_TYPE_LOWER
;
4180 chan
->circ_id_type
= CIRC_ID_TYPE_HIGHER
;
4183 chan
->circ_id_type
= CIRC_ID_TYPE_NEITHER
;