tor  0.4.1.0-alpha-dev
channel.c
Go to the documentation of this file.
1 
2 /* * Copyright (c) 2012-2019, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
4 
51 /*
52  * Define this so channel.h gives us things only channel_t subclasses
53  * should touch.
54  */
55 #define TOR_CHANNEL_INTERNAL_
56 
57 /* This one's for stuff only channel.c and the test suite should see */
58 #define CHANNEL_PRIVATE_
59 
60 #include "core/or/or.h"
61 #include "app/config/config.h"
62 #include "core/mainloop/mainloop.h"
63 #include "core/or/channel.h"
64 #include "core/or/channelpadding.h"
65 #include "core/or/channeltls.h"
66 #include "core/or/circuitbuild.h"
67 #include "core/or/circuitlist.h"
68 #include "core/or/circuitmux.h"
69 #include "core/or/circuitstats.h"
70 #include "core/or/connection_or.h" /* For var_cell_free() */
71 #include "core/or/dos.h"
72 #include "core/or/relay.h"
73 #include "core/or/scheduler.h"
78 #include "feature/relay/router.h"
81 #include "feature/stats/rephist.h"
82 #include "lib/evloop/timers.h"
83 #include "lib/time/compat_time.h"
84 
85 #include "core/or/cell_queue_st.h"
86 
87 /* Global lists of channels */
88 
89 /* All channel_t instances */
90 static smartlist_t *all_channels = NULL;
91 
92 /* All channel_t instances not in ERROR or CLOSED states */
93 static smartlist_t *active_channels = NULL;
94 
95 /* All channel_t instances in ERROR or CLOSED states */
96 static smartlist_t *finished_channels = NULL;
97 
98 /* All channel_listener_t instances */
99 static smartlist_t *all_listeners = NULL;
100 
101 /* All channel_listener_t instances in LISTENING state */
102 static smartlist_t *active_listeners = NULL;
103 
104 /* All channel_listener_t instances in LISTENING state */
105 static smartlist_t *finished_listeners = NULL;
106 
109 static HT_HEAD(channel_gid_map, channel_s) channel_gid_map = HT_INITIALIZER();
110 
111 static unsigned
112 channel_id_hash(const channel_t *chan)
113 {
114  return (unsigned) chan->global_identifier;
115 }
116 static int
117 channel_id_eq(const channel_t *a, const channel_t *b)
118 {
119  return a->global_identifier == b->global_identifier;
120 }
121 HT_PROTOTYPE(channel_gid_map, channel_s, gidmap_node,
122  channel_id_hash, channel_id_eq)
123 HT_GENERATE2(channel_gid_map, channel_s, gidmap_node,
124  channel_id_hash, channel_id_eq,
126 
127 HANDLE_IMPL(channel, channel_s,)
128 
129 /* Counter for ID numbers */
130 static uint64_t n_channels_allocated = 0;
131 
132 /* Digest->channel map
133  *
134  * Similar to the one used in connection_or.c, this maps from the identity
135  * digest of a remote endpoint to a channel_t to that endpoint. Channels
136  * should be placed here when registered and removed when they close or error.
137  * If more than one channel exists, follow the next_with_same_id pointer
138  * as a linked list.
139  */
140 static HT_HEAD(channel_idmap, channel_idmap_entry_s) channel_identity_map =
141  HT_INITIALIZER();
142 
143 typedef struct channel_idmap_entry_s {
144  HT_ENTRY(channel_idmap_entry_s) node;
145  uint8_t digest[DIGEST_LEN];
146  TOR_LIST_HEAD(channel_list_s, channel_s) channel_list;
147 } channel_idmap_entry_t;
148 
149 static inline unsigned
150 channel_idmap_hash(const channel_idmap_entry_t *ent)
151 {
152  return (unsigned) siphash24g(ent->digest, DIGEST_LEN);
153 }
154 
155 static inline int
156 channel_idmap_eq(const channel_idmap_entry_t *a,
157  const channel_idmap_entry_t *b)
158 {
159  return tor_memeq(a->digest, b->digest, DIGEST_LEN);
160 }
161 
162 HT_PROTOTYPE(channel_idmap, channel_idmap_entry_s, node, channel_idmap_hash,
163  channel_idmap_eq)
164 HT_GENERATE2(channel_idmap, channel_idmap_entry_s, node, channel_idmap_hash,
165  channel_idmap_eq, 0.5, tor_reallocarray_, tor_free_)
166 
167 /* Functions to maintain the digest map */
168 static void channel_remove_from_digest_map(channel_t *chan);
169 
170 static void channel_force_xfree(channel_t *chan);
171 static void channel_free_list(smartlist_t *channels,
172  int mark_for_close);
173 static void channel_listener_free_list(smartlist_t *channels,
174  int mark_for_close);
176 
177 /***********************************
178  * Channel state utility functions *
179  **********************************/
180 
184 int
185 channel_state_is_valid(channel_state_t state)
186 {
187  int is_valid;
188 
189  switch (state) {
190  case CHANNEL_STATE_CLOSED:
191  case CHANNEL_STATE_CLOSING:
192  case CHANNEL_STATE_ERROR:
193  case CHANNEL_STATE_MAINT:
194  case CHANNEL_STATE_OPENING:
195  case CHANNEL_STATE_OPEN:
196  is_valid = 1;
197  break;
198  case CHANNEL_STATE_LAST:
199  default:
200  is_valid = 0;
201  }
202 
203  return is_valid;
204 }
205 
209 int
210 channel_listener_state_is_valid(channel_listener_state_t state)
211 {
212  int is_valid;
213 
214  switch (state) {
215  case CHANNEL_LISTENER_STATE_CLOSED:
216  case CHANNEL_LISTENER_STATE_LISTENING:
217  case CHANNEL_LISTENER_STATE_CLOSING:
218  case CHANNEL_LISTENER_STATE_ERROR:
219  is_valid = 1;
220  break;
221  case CHANNEL_LISTENER_STATE_LAST:
222  default:
223  is_valid = 0;
224  }
225 
226  return is_valid;
227 }
228 
236 int
237 channel_state_can_transition(channel_state_t from, channel_state_t to)
238 {
239  int is_valid;
240 
241  switch (from) {
242  case CHANNEL_STATE_CLOSED:
243  is_valid = (to == CHANNEL_STATE_OPENING);
244  break;
245  case CHANNEL_STATE_CLOSING:
246  is_valid = (to == CHANNEL_STATE_CLOSED ||
247  to == CHANNEL_STATE_ERROR);
248  break;
249  case CHANNEL_STATE_ERROR:
250  is_valid = 0;
251  break;
252  case CHANNEL_STATE_MAINT:
253  is_valid = (to == CHANNEL_STATE_CLOSING ||
254  to == CHANNEL_STATE_ERROR ||
255  to == CHANNEL_STATE_OPEN);
256  break;
257  case CHANNEL_STATE_OPENING:
258  is_valid = (to == CHANNEL_STATE_CLOSING ||
259  to == CHANNEL_STATE_ERROR ||
260  to == CHANNEL_STATE_OPEN);
261  break;
262  case CHANNEL_STATE_OPEN:
263  is_valid = (to == CHANNEL_STATE_CLOSING ||
264  to == CHANNEL_STATE_ERROR ||
265  to == CHANNEL_STATE_MAINT);
266  break;
267  case CHANNEL_STATE_LAST:
268  default:
269  is_valid = 0;
270  }
271 
272  return is_valid;
273 }
274 
282 int
283 channel_listener_state_can_transition(channel_listener_state_t from,
284  channel_listener_state_t to)
285 {
286  int is_valid;
287 
288  switch (from) {
289  case CHANNEL_LISTENER_STATE_CLOSED:
290  is_valid = (to == CHANNEL_LISTENER_STATE_LISTENING);
291  break;
292  case CHANNEL_LISTENER_STATE_CLOSING:
293  is_valid = (to == CHANNEL_LISTENER_STATE_CLOSED ||
294  to == CHANNEL_LISTENER_STATE_ERROR);
295  break;
296  case CHANNEL_LISTENER_STATE_ERROR:
297  is_valid = 0;
298  break;
299  case CHANNEL_LISTENER_STATE_LISTENING:
300  is_valid = (to == CHANNEL_LISTENER_STATE_CLOSING ||
301  to == CHANNEL_LISTENER_STATE_ERROR);
302  break;
303  case CHANNEL_LISTENER_STATE_LAST:
304  default:
305  is_valid = 0;
306  }
307 
308  return is_valid;
309 }
310 
314 const char *
315 channel_state_to_string(channel_state_t state)
316 {
317  const char *descr;
318 
319  switch (state) {
320  case CHANNEL_STATE_CLOSED:
321  descr = "closed";
322  break;
323  case CHANNEL_STATE_CLOSING:
324  descr = "closing";
325  break;
326  case CHANNEL_STATE_ERROR:
327  descr = "channel error";
328  break;
329  case CHANNEL_STATE_MAINT:
330  descr = "temporarily suspended for maintenance";
331  break;
332  case CHANNEL_STATE_OPENING:
333  descr = "opening";
334  break;
335  case CHANNEL_STATE_OPEN:
336  descr = "open";
337  break;
338  case CHANNEL_STATE_LAST:
339  default:
340  descr = "unknown or invalid channel state";
341  }
342 
343  return descr;
344 }
345 
349 const char *
350 channel_listener_state_to_string(channel_listener_state_t state)
351 {
352  const char *descr;
353 
354  switch (state) {
355  case CHANNEL_LISTENER_STATE_CLOSED:
356  descr = "closed";
357  break;
358  case CHANNEL_LISTENER_STATE_CLOSING:
359  descr = "closing";
360  break;
361  case CHANNEL_LISTENER_STATE_ERROR:
362  descr = "channel listener error";
363  break;
364  case CHANNEL_LISTENER_STATE_LISTENING:
365  descr = "listening";
366  break;
367  case CHANNEL_LISTENER_STATE_LAST:
368  default:
369  descr = "unknown or invalid channel listener state";
370  }
371 
372  return descr;
373 }
374 
375 /***************************************
376  * Channel registration/unregistration *
377  ***************************************/
378 
385 void
387 {
388  tor_assert(chan);
390 
391  /* No-op if already registered */
392  if (chan->registered) return;
393 
394  log_debug(LD_CHANNEL,
395  "Registering channel %p (ID %"PRIu64 ") "
396  "in state %s (%d) with digest %s",
397  chan, (chan->global_identifier),
398  channel_state_to_string(chan->state), chan->state,
400 
401  /* Make sure we have all_channels, then add it */
402  if (!all_channels) all_channels = smartlist_new();
403  smartlist_add(all_channels, chan);
404  channel_t *oldval = HT_REPLACE(channel_gid_map, &channel_gid_map, chan);
405  tor_assert(! oldval);
406 
407  /* Is it finished? */
408  if (CHANNEL_FINISHED(chan)) {
409  /* Put it in the finished list, creating it if necessary */
410  if (!finished_channels) finished_channels = smartlist_new();
411  smartlist_add(finished_channels, chan);
413  } else {
414  /* Put it in the active list, creating it if necessary */
415  if (!active_channels) active_channels = smartlist_new();
416  smartlist_add(active_channels, chan);
417 
418  if (!CHANNEL_IS_CLOSING(chan)) {
419  /* It should have a digest set */
420  if (!tor_digest_is_zero(chan->identity_digest)) {
421  /* Yeah, we're good, add it to the map */
423  } else {
424  log_info(LD_CHANNEL,
425  "Channel %p (global ID %"PRIu64 ") "
426  "in state %s (%d) registered with no identity digest",
427  chan, (chan->global_identifier),
428  channel_state_to_string(chan->state), chan->state);
429  }
430  }
431  }
432 
433  /* Mark it as registered */
434  chan->registered = 1;
435 }
436 
443 void
445 {
446  tor_assert(chan);
447 
448  /* No-op if not registered */
449  if (!(chan->registered)) return;
450 
451  /* Is it finished? */
452  if (CHANNEL_FINISHED(chan)) {
453  /* Get it out of the finished list */
454  if (finished_channels) smartlist_remove(finished_channels, chan);
455  } else {
456  /* Get it out of the active list */
457  if (active_channels) smartlist_remove(active_channels, chan);
458  }
459 
460  /* Get it out of all_channels */
461  if (all_channels) smartlist_remove(all_channels, chan);
462  channel_t *oldval = HT_REMOVE(channel_gid_map, &channel_gid_map, chan);
463  tor_assert(oldval == NULL || oldval == chan);
464 
465  /* Mark it as unregistered */
466  chan->registered = 0;
467 
468  /* Should it be in the digest map? */
469  if (!tor_digest_is_zero(chan->identity_digest) &&
470  !(CHANNEL_CONDEMNED(chan))) {
471  /* Remove it */
473  }
474 }
475 
482 void
484 {
485  tor_assert(chan_l);
486 
487  /* No-op if already registered */
488  if (chan_l->registered) return;
489 
490  log_debug(LD_CHANNEL,
491  "Registering channel listener %p (ID %"PRIu64 ") "
492  "in state %s (%d)",
493  chan_l, (chan_l->global_identifier),
494  channel_listener_state_to_string(chan_l->state),
495  chan_l->state);
496 
497  /* Make sure we have all_listeners, then add it */
498  if (!all_listeners) all_listeners = smartlist_new();
499  smartlist_add(all_listeners, chan_l);
500 
501  /* Is it finished? */
502  if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
503  chan_l->state == CHANNEL_LISTENER_STATE_ERROR) {
504  /* Put it in the finished list, creating it if necessary */
505  if (!finished_listeners) finished_listeners = smartlist_new();
506  smartlist_add(finished_listeners, chan_l);
507  } else {
508  /* Put it in the active list, creating it if necessary */
509  if (!active_listeners) active_listeners = smartlist_new();
510  smartlist_add(active_listeners, chan_l);
511  }
512 
513  /* Mark it as registered */
514  chan_l->registered = 1;
515 }
516 
523 void
525 {
526  tor_assert(chan_l);
527 
528  /* No-op if not registered */
529  if (!(chan_l->registered)) return;
530 
531  /* Is it finished? */
532  if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
533  chan_l->state == CHANNEL_LISTENER_STATE_ERROR) {
534  /* Get it out of the finished list */
535  if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
536  } else {
537  /* Get it out of the active list */
538  if (active_listeners) smartlist_remove(active_listeners, chan_l);
539  }
540 
541  /* Get it out of all_listeners */
542  if (all_listeners) smartlist_remove(all_listeners, chan_l);
543 
544  /* Mark it as unregistered */
545  chan_l->registered = 0;
546 }
547 
548 /*********************************
549  * Channel digest map maintenance
550  *********************************/
551 
559 STATIC void
561 {
562  channel_idmap_entry_t *ent, search;
563 
564  tor_assert(chan);
565 
566  /* Assert that the state makes sense */
567  tor_assert(!CHANNEL_CONDEMNED(chan));
568 
569  /* Assert that there is a digest */
571 
572  memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
573  ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
574  if (! ent) {
575  ent = tor_malloc(sizeof(channel_idmap_entry_t));
576  memcpy(ent->digest, chan->identity_digest, DIGEST_LEN);
577  TOR_LIST_INIT(&ent->channel_list);
578  HT_INSERT(channel_idmap, &channel_identity_map, ent);
579  }
580  TOR_LIST_INSERT_HEAD(&ent->channel_list, chan, next_with_same_id);
581 
582  log_debug(LD_CHANNEL,
583  "Added channel %p (global ID %"PRIu64 ") "
584  "to identity map in state %s (%d) with digest %s",
585  chan, (chan->global_identifier),
586  channel_state_to_string(chan->state), chan->state,
588 }
589 
596 static void
598 {
599  channel_idmap_entry_t *ent, search;
600 
601  tor_assert(chan);
602 
603  /* Assert that there is a digest */
605 
606  /* Pull it out of its list, wherever that list is */
607  TOR_LIST_REMOVE(chan, next_with_same_id);
608 
609  memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
610  ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
611 
612  /* Look for it in the map */
613  if (ent) {
614  /* Okay, it's here */
615 
616  if (TOR_LIST_EMPTY(&ent->channel_list)) {
617  HT_REMOVE(channel_idmap, &channel_identity_map, ent);
618  tor_free(ent);
619  }
620 
621  log_debug(LD_CHANNEL,
622  "Removed channel %p (global ID %"PRIu64 ") from "
623  "identity map in state %s (%d) with digest %s",
624  chan, (chan->global_identifier),
625  channel_state_to_string(chan->state), chan->state,
627  } else {
628  /* Shouldn't happen */
629  log_warn(LD_BUG,
630  "Trying to remove channel %p (global ID %"PRIu64 ") with "
631  "digest %s from identity map, but couldn't find any with "
632  "that digest",
633  chan, (chan->global_identifier),
635  }
636 }
637 
638 /****************************
639  * Channel lookup functions *
640  ***************************/
641 
649 channel_t *
650 channel_find_by_global_id(uint64_t global_identifier)
651 {
652  channel_t lookup;
653  channel_t *rv = NULL;
654 
655  lookup.global_identifier = global_identifier;
656  rv = HT_FIND(channel_gid_map, &channel_gid_map, &lookup);
657  if (rv) {
658  tor_assert(rv->global_identifier == global_identifier);
659  }
660 
661  return rv;
662 }
663 
666 static int
668  const char *rsa_id_digest,
669  const ed25519_public_key_t *ed_id)
670 {
671  if (BUG(!chan))
672  return 0;
673  if (rsa_id_digest) {
674  if (tor_memneq(rsa_id_digest, chan->identity_digest, DIGEST_LEN))
675  return 0;
676  }
677  if (ed_id) {
678  if (tor_memneq(ed_id->pubkey, chan->ed25519_identity.pubkey,
679  ED25519_PUBKEY_LEN))
680  return 0;
681  }
682  return 1;
683 }
684 
696 channel_t *
697 channel_find_by_remote_identity(const char *rsa_id_digest,
698  const ed25519_public_key_t *ed_id)
699 {
700  channel_t *rv = NULL;
701  channel_idmap_entry_t *ent, search;
702 
703  tor_assert(rsa_id_digest); /* For now, we require that every channel have
704  * an RSA identity, and that every lookup
705  * contain an RSA identity */
706  if (ed_id && ed25519_public_key_is_zero(ed_id)) {
707  /* Treat zero as meaning "We don't care about the presence or absence of
708  * an Ed key", not "There must be no Ed key". */
709  ed_id = NULL;
710  }
711 
712  memcpy(search.digest, rsa_id_digest, DIGEST_LEN);
713  ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
714  if (ent) {
715  rv = TOR_LIST_FIRST(&ent->channel_list);
716  }
717  while (rv && ! channel_remote_identity_matches(rv, rsa_id_digest, ed_id)) {
719  }
720 
721  return rv;
722 }
723 
730 channel_t *
732 {
733  tor_assert(chan);
734 
735  return TOR_LIST_NEXT(chan, next_with_same_id);
736 }
737 
747 void
749 {
750  channel_idmap_entry_t **iter;
751  channel_t *chan;
752  int total_relay_connections = 0, total_relays = 0, total_canonical = 0;
753  int total_half_canonical = 0;
754  int total_gt_one_connection = 0, total_gt_two_connections = 0;
755  int total_gt_four_connections = 0;
756 
757  HT_FOREACH(iter, channel_idmap, &channel_identity_map) {
758  int connections_to_relay = 0;
759 
760  /* Only consider relay connections */
761  if (!connection_or_digest_is_known_relay((char*)(*iter)->digest))
762  continue;
763 
764  total_relays++;
765 
766  for (chan = TOR_LIST_FIRST(&(*iter)->channel_list); chan;
767  chan = channel_next_with_rsa_identity(chan)) {
768 
769  if (CHANNEL_CONDEMNED(chan) || !CHANNEL_IS_OPEN(chan))
770  continue;
771 
772  connections_to_relay++;
773  total_relay_connections++;
774 
775  if (chan->is_canonical(chan, 0)) total_canonical++;
776 
777  if (!chan->is_canonical_to_peer && chan->is_canonical(chan, 0)
778  && chan->is_canonical(chan, 1)) {
779  total_half_canonical++;
780  }
781  }
782 
783  if (connections_to_relay > 1) total_gt_one_connection++;
784  if (connections_to_relay > 2) total_gt_two_connections++;
785  if (connections_to_relay > 4) total_gt_four_connections++;
786  }
787 
788 #define MIN_RELAY_CONNECTIONS_TO_WARN 5
789 
790  /* If we average 1.5 or more connections per relay, something is wrong */
791  if (total_relays > MIN_RELAY_CONNECTIONS_TO_WARN &&
792  total_relay_connections >= 1.5*total_relays) {
793  log_notice(LD_OR,
794  "Your relay has a very large number of connections to other relays. "
795  "Is your outbound address the same as your relay address? "
796  "Found %d connections to %d relays. Found %d current canonical "
797  "connections, in %d of which we were a non-canonical peer. "
798  "%d relays had more than 1 connection, %d had more than 2, and "
799  "%d had more than 4 connections.",
800  total_relay_connections, total_relays, total_canonical,
801  total_half_canonical, total_gt_one_connection,
802  total_gt_two_connections, total_gt_four_connections);
803  } else {
804  log_info(LD_OR, "Performed connection pruning. "
805  "Found %d connections to %d relays. Found %d current canonical "
806  "connections, in %d of which we were a non-canonical peer. "
807  "%d relays had more than 1 connection, %d had more than 2, and "
808  "%d had more than 4 connections.",
809  total_relay_connections, total_relays, total_canonical,
810  total_half_canonical, total_gt_one_connection,
811  total_gt_two_connections, total_gt_four_connections);
812  }
813 }
814 
822 void
824 {
825  tor_assert(chan);
826 
827  /* Assign an ID and bump the counter */
828  chan->global_identifier = ++n_channels_allocated;
829 
830  /* Init timestamp */
831  chan->timestamp_last_had_circuits = time(NULL);
832 
833  /* Warn about exhausted circuit IDs no more than hourly. */
834  chan->last_warned_circ_ids_exhausted.rate = 3600;
835 
836  /* Initialize list entries. */
837  memset(&chan->next_with_same_id, 0, sizeof(chan->next_with_same_id));
838 
839  /* Timestamp it */
841 
842  /* It hasn't been open yet. */
843  chan->has_been_open = 0;
844 
845  /* Scheduler state is idle */
846  chan->scheduler_state = SCHED_CHAN_IDLE;
847 
848  /* Channel is not in the scheduler heap. */
849  chan->sched_heap_idx = -1;
850 }
851 
859 void
861 {
862  tor_assert(chan_l);
863 
864  /* Assign an ID and bump the counter */
865  chan_l->global_identifier = ++n_channels_allocated;
866 
867  /* Timestamp it */
869 }
870 
875 void
877 {
878  if (!chan) return;
879 
880  /* It must be closed or errored */
881  tor_assert(CHANNEL_FINISHED(chan));
882 
883  /* It must be deregistered */
884  tor_assert(!(chan->registered));
885 
886  log_debug(LD_CHANNEL,
887  "Freeing channel %"PRIu64 " at %p",
888  (chan->global_identifier), chan);
889 
890  /* Get this one out of the scheduler */
891  scheduler_release_channel(chan);
892 
893  /*
894  * Get rid of cmux policy before we do anything, so cmux policies don't
895  * see channels in weird half-freed states.
896  */
897  if (chan->cmux) {
898  circuitmux_set_policy(chan->cmux, NULL);
899  }
900 
901  /* Remove all timers and associated handle entries now */
902  timer_free(chan->padding_timer);
903  channel_handle_free(chan->timer_handle);
904  channel_handles_clear(chan);
905 
906  /* Call a free method if there is one */
907  if (chan->free_fn) chan->free_fn(chan);
908 
910 
911  /* Get rid of cmux */
912  if (chan->cmux) {
915  circuitmux_free(chan->cmux);
916  chan->cmux = NULL;
917  }
918 
919  tor_free(chan);
920 }
921 
927 void
929 {
930  if (!chan_l) return;
931 
932  log_debug(LD_CHANNEL,
933  "Freeing channel_listener_t %"PRIu64 " at %p",
934  (chan_l->global_identifier),
935  chan_l);
936 
937  /* It must be closed or errored */
938  tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
939  chan_l->state == CHANNEL_LISTENER_STATE_ERROR);
940  /* It must be deregistered */
941  tor_assert(!(chan_l->registered));
942 
943  /* Call a free method if there is one */
944  if (chan_l->free_fn) chan_l->free_fn(chan_l);
945 
946  tor_free(chan_l);
947 }
948 
954 static void
956 {
957  tor_assert(chan);
958 
959  log_debug(LD_CHANNEL,
960  "Force-freeing channel %"PRIu64 " at %p",
961  (chan->global_identifier), chan);
962 
963  /* Get this one out of the scheduler */
964  scheduler_release_channel(chan);
965 
966  /*
967  * Get rid of cmux policy before we do anything, so cmux policies don't
968  * see channels in weird half-freed states.
969  */
970  if (chan->cmux) {
971  circuitmux_set_policy(chan->cmux, NULL);
972  }
973 
974  /* Remove all timers and associated handle entries now */
975  timer_free(chan->padding_timer);
976  channel_handle_free(chan->timer_handle);
977  channel_handles_clear(chan);
978 
979  /* Call a free method if there is one */
980  if (chan->free_fn) chan->free_fn(chan);
981 
983 
984  /* Get rid of cmux */
985  if (chan->cmux) {
986  circuitmux_free(chan->cmux);
987  chan->cmux = NULL;
988  }
989 
990  tor_free(chan);
991 }
992 
998 static void
1000 {
1001  tor_assert(chan_l);
1002 
1003  log_debug(LD_CHANNEL,
1004  "Force-freeing channel_listener_t %"PRIu64 " at %p",
1005  (chan_l->global_identifier),
1006  chan_l);
1007 
1008  /* Call a free method if there is one */
1009  if (chan_l->free_fn) chan_l->free_fn(chan_l);
1010 
1011  /*
1012  * The incoming list just gets emptied and freed; we request close on
1013  * any channels we find there, but since we got called while shutting
1014  * down they will get deregistered and freed elsewhere anyway.
1015  */
1016  if (chan_l->incoming_list) {
1018  channel_t *, qchan) {
1019  channel_mark_for_close(qchan);
1020  } SMARTLIST_FOREACH_END(qchan);
1021 
1022  smartlist_free(chan_l->incoming_list);
1023  chan_l->incoming_list = NULL;
1024  }
1025 
1026  tor_free(chan_l);
1027 }
1028 
1035 void
1037  channel_listener_fn_ptr listener)
1038 {
1039  tor_assert(chan_l);
1040  tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_LISTENING);
1041 
1042  log_debug(LD_CHANNEL,
1043  "Setting listener callback for channel listener %p "
1044  "(global ID %"PRIu64 ") to %p",
1045  chan_l, (chan_l->global_identifier),
1046  listener);
1047 
1048  chan_l->listener = listener;
1049  if (chan_l->listener) channel_listener_process_incoming(chan_l);
1050 }
1051 
1058 channel_cell_handler_fn_ptr
1060 {
1061  tor_assert(chan);
1062 
1063  if (CHANNEL_CAN_HANDLE_CELLS(chan))
1064  return chan->cell_handler;
1065 
1066  return NULL;
1067 }
1068 
1075 channel_var_cell_handler_fn_ptr
1077 {
1078  tor_assert(chan);
1079 
1080  if (CHANNEL_CAN_HANDLE_CELLS(chan))
1081  return chan->var_cell_handler;
1082 
1083  return NULL;
1084 }
1085 
1092 void
1094  channel_cell_handler_fn_ptr cell_handler,
1095  channel_var_cell_handler_fn_ptr
1096  var_cell_handler)
1097 {
1098  tor_assert(chan);
1099  tor_assert(CHANNEL_CAN_HANDLE_CELLS(chan));
1100 
1101  log_debug(LD_CHANNEL,
1102  "Setting cell_handler callback for channel %p to %p",
1103  chan, cell_handler);
1104  log_debug(LD_CHANNEL,
1105  "Setting var_cell_handler callback for channel %p to %p",
1106  chan, var_cell_handler);
1107 
1108  /* Change them */
1109  chan->cell_handler = cell_handler;
1110  chan->var_cell_handler = var_cell_handler;
1111 }
1112 
1113 /*
1114  * On closing channels
1115  *
1116  * There are three functions that close channels, for use in
1117  * different circumstances:
1118  *
1119  * - Use channel_mark_for_close() for most cases
1120  * - Use channel_close_from_lower_layer() if you are connection_or.c
1121  * and the other end closes the underlying connection.
1122  * - Use channel_close_for_error() if you are connection_or.c and
1123  * some sort of error has occurred.
1124  */
1125 
1133 void
1135 {
1136  tor_assert(chan != NULL);
1137  tor_assert(chan->close != NULL);
1138 
1139  /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1140  if (CHANNEL_CONDEMNED(chan))
1141  return;
1142 
1143  log_debug(LD_CHANNEL,
1144  "Closing channel %p (global ID %"PRIu64 ") "
1145  "by request",
1146  chan, (chan->global_identifier));
1147 
1148  /* Note closing by request from above */
1149  chan->reason_for_closing = CHANNEL_CLOSE_REQUESTED;
1150 
1151  /* Change state to CLOSING */
1152  channel_change_state(chan, CHANNEL_STATE_CLOSING);
1153 
1154  /* Tell the lower layer */
1155  chan->close(chan);
1156 
1157  /*
1158  * It's up to the lower layer to change state to CLOSED or ERROR when we're
1159  * ready; we'll try to free channels that are in the finished list from
1160  * channel_run_cleanup(). The lower layer should do this by calling
1161  * channel_closed().
1162  */
1163 }
1164 
1172 void
1174 {
1175  tor_assert(chan_l != NULL);
1176  tor_assert(chan_l->close != NULL);
1177 
1178  /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1179  if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
1180  chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
1181  chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
1182 
1183  log_debug(LD_CHANNEL,
1184  "Closing channel listener %p (global ID %"PRIu64 ") "
1185  "by request",
1186  chan_l, (chan_l->global_identifier));
1187 
1188  /* Note closing by request from above */
1189  chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_REQUESTED;
1190 
1191  /* Change state to CLOSING */
1192  channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
1193 
1194  /* Tell the lower layer */
1195  chan_l->close(chan_l);
1196 
1197  /*
1198  * It's up to the lower layer to change state to CLOSED or ERROR when we're
1199  * ready; we'll try to free channels that are in the finished list from
1200  * channel_run_cleanup(). The lower layer should do this by calling
1201  * channel_listener_closed().
1202  */
1203 }
1204 
1212 void
1214 {
1215  tor_assert(chan != NULL);
1216 
1217  /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1218  if (CHANNEL_CONDEMNED(chan))
1219  return;
1220 
1221  log_debug(LD_CHANNEL,
1222  "Closing channel %p (global ID %"PRIu64 ") "
1223  "due to lower-layer event",
1224  chan, (chan->global_identifier));
1225 
1226  /* Note closing by event from below */
1227  chan->reason_for_closing = CHANNEL_CLOSE_FROM_BELOW;
1228 
1229  /* Change state to CLOSING */
1230  channel_change_state(chan, CHANNEL_STATE_CLOSING);
1231 }
1232 
1240 void
1242 {
1243  tor_assert(chan != NULL);
1244 
1245  /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
1246  if (CHANNEL_CONDEMNED(chan))
1247  return;
1248 
1249  log_debug(LD_CHANNEL,
1250  "Closing channel %p due to lower-layer error",
1251  chan);
1252 
1253  /* Note closing by event from below */
1254  chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
1255 
1256  /* Change state to CLOSING */
1257  channel_change_state(chan, CHANNEL_STATE_CLOSING);
1258 }
1259 
1267 void
1269 {
1270  tor_assert(chan);
1271  tor_assert(CHANNEL_CONDEMNED(chan));
1272 
1273  /* No-op if already inactive */
1274  if (CHANNEL_FINISHED(chan))
1275  return;
1276 
1277  /* Inform any pending (not attached) circs that they should
1278  * give up. */
1279  if (! chan->has_been_open)
1280  circuit_n_chan_done(chan, 0, 0);
1281 
1282  /* Now close all the attached circuits on it. */
1283  circuit_unlink_all_from_channel(chan, END_CIRC_REASON_CHANNEL_CLOSED);
1284 
1285  if (chan->reason_for_closing != CHANNEL_CLOSE_FOR_ERROR) {
1286  channel_change_state(chan, CHANNEL_STATE_CLOSED);
1287  } else {
1288  channel_change_state(chan, CHANNEL_STATE_ERROR);
1289  }
1290 }
1291 
1298 void
1300 {
1301  int state_not_in_map;
1302 
1303  tor_assert(chan);
1304 
1305  log_debug(LD_CHANNEL,
1306  "Clearing remote endpoint digest on channel %p with "
1307  "global ID %"PRIu64,
1308  chan, (chan->global_identifier));
1309 
1310  state_not_in_map = CHANNEL_CONDEMNED(chan);
1311 
1312  if (!state_not_in_map && chan->registered &&
1314  /* if it's registered get it out of the digest map */
1316 
1317  memset(chan->identity_digest, 0,
1318  sizeof(chan->identity_digest));
1319 }
1320 
1327 void
1329  const char *identity_digest,
1330  const ed25519_public_key_t *ed_identity)
1331 {
1332  int was_in_digest_map, should_be_in_digest_map, state_not_in_map;
1333 
1334  tor_assert(chan);
1335 
1336  log_debug(LD_CHANNEL,
1337  "Setting remote endpoint digest on channel %p with "
1338  "global ID %"PRIu64 " to digest %s",
1339  chan, (chan->global_identifier),
1340  identity_digest ?
1341  hex_str(identity_digest, DIGEST_LEN) : "(null)");
1342 
1343  state_not_in_map = CHANNEL_CONDEMNED(chan);
1344 
1345  was_in_digest_map =
1346  !state_not_in_map &&
1347  chan->registered &&
1349  should_be_in_digest_map =
1350  !state_not_in_map &&
1351  chan->registered &&
1352  (identity_digest &&
1353  !tor_digest_is_zero(identity_digest));
1354 
1355  if (was_in_digest_map)
1356  /* We should always remove it; we'll add it back if we're writing
1357  * in a new digest.
1358  */
1360 
1361  if (identity_digest) {
1362  memcpy(chan->identity_digest,
1363  identity_digest,
1364  sizeof(chan->identity_digest));
1365  } else {
1366  memset(chan->identity_digest, 0,
1367  sizeof(chan->identity_digest));
1368  }
1369  if (ed_identity) {
1370  memcpy(&chan->ed25519_identity, ed_identity, sizeof(*ed_identity));
1371  } else {
1372  memset(&chan->ed25519_identity, 0, sizeof(*ed_identity));
1373  }
1374 
1375  /* Put it in the digest map if we should */
1376  if (should_be_in_digest_map)
1378 }
1379 
1386 void
1388 {
1389  int state_not_in_map;
1390 
1391  tor_assert(chan);
1392 
1393  log_debug(LD_CHANNEL,
1394  "Clearing remote endpoint identity on channel %p with "
1395  "global ID %"PRIu64,
1396  chan, (chan->global_identifier));
1397 
1398  state_not_in_map = CHANNEL_CONDEMNED(chan);
1399 
1400  if (!state_not_in_map && chan->registered &&
1402  /* if it's registered get it out of the digest map */
1404 
1405  memset(chan->identity_digest, 0,
1406  sizeof(chan->identity_digest));
1407 }
1408 
1416 static int
1418 {
1419  int ret = -1;
1420  size_t cell_bytes;
1421 
1422  tor_assert(chan);
1423  tor_assert(cell);
1424 
1425  /* Assert that the state makes sense for a cell write */
1426  tor_assert(CHANNEL_CAN_HANDLE_CELLS(chan));
1427 
1428  {
1429  circid_t circ_id;
1430  if (packed_cell_is_destroy(chan, cell, &circ_id)) {
1431  channel_note_destroy_not_pending(chan, circ_id);
1432  }
1433  }
1434 
1435  /* For statistical purposes, figure out how big this cell is */
1436  cell_bytes = get_cell_network_size(chan->wide_circ_ids);
1437 
1438  /* Can we send it right out? If so, try */
1439  if (!CHANNEL_IS_OPEN(chan)) {
1440  goto done;
1441  }
1442 
1443  /* Write the cell on the connection's outbuf. */
1444  if (chan->write_packed_cell(chan, cell) < 0) {
1445  goto done;
1446  }
1447  /* Timestamp for transmission */
1448  channel_timestamp_xmit(chan);
1449  /* Update the counter */
1450  ++(chan->n_cells_xmitted);
1451  chan->n_bytes_xmitted += cell_bytes;
1452  /* Successfully sent the cell. */
1453  ret = 0;
1454 
1455  done:
1456  return ret;
1457 }
1458 
1469 int
1471 {
1472  int ret = -1;
1473 
1474  tor_assert(chan);
1475  tor_assert(cell);
1476 
1477  if (CHANNEL_IS_CLOSING(chan)) {
1478  log_debug(LD_CHANNEL, "Discarding %p on closing channel %p with "
1479  "global ID %"PRIu64, cell, chan,
1480  (chan->global_identifier));
1481  goto end;
1482  }
1483  log_debug(LD_CHANNEL,
1484  "Writing %p to channel %p with global ID "
1485  "%"PRIu64, cell, chan, (chan->global_identifier));
1486 
1487  ret = write_packed_cell(chan, cell);
1488 
1489  end:
1490  /* Whatever happens, we free the cell. Either an error occurred or the cell
1491  * was put on the connection outbuf, both cases we have ownership of the
1492  * cell and we free it. */
1493  packed_cell_free(cell);
1494  return ret;
1495 }
1496 
1504 static void
1505 channel_change_state_(channel_t *chan, channel_state_t to_state)
1506 {
1507  channel_state_t from_state;
1508  unsigned char was_active, is_active;
1509  unsigned char was_in_id_map, is_in_id_map;
1510 
1511  tor_assert(chan);
1512  from_state = chan->state;
1513 
1514  tor_assert(channel_state_is_valid(from_state));
1515  tor_assert(channel_state_is_valid(to_state));
1516  tor_assert(channel_state_can_transition(chan->state, to_state));
1517 
1518  /* Check for no-op transitions */
1519  if (from_state == to_state) {
1520  log_debug(LD_CHANNEL,
1521  "Got no-op transition from \"%s\" to itself on channel %p"
1522  "(global ID %"PRIu64 ")",
1523  channel_state_to_string(to_state),
1524  chan, (chan->global_identifier));
1525  return;
1526  }
1527 
1528  /* If we're going to a closing or closed state, we must have a reason set */
1529  if (to_state == CHANNEL_STATE_CLOSING ||
1530  to_state == CHANNEL_STATE_CLOSED ||
1531  to_state == CHANNEL_STATE_ERROR) {
1532  tor_assert(chan->reason_for_closing != CHANNEL_NOT_CLOSING);
1533  }
1534 
1535  log_debug(LD_CHANNEL,
1536  "Changing state of channel %p (global ID %"PRIu64
1537  ") from \"%s\" to \"%s\"",
1538  chan,
1539  (chan->global_identifier),
1541  channel_state_to_string(to_state));
1542 
1543  chan->state = to_state;
1544 
1545  /* Need to add to the right lists if the channel is registered */
1546  if (chan->registered) {
1547  was_active = !(from_state == CHANNEL_STATE_CLOSED ||
1548  from_state == CHANNEL_STATE_ERROR);
1549  is_active = !(to_state == CHANNEL_STATE_CLOSED ||
1550  to_state == CHANNEL_STATE_ERROR);
1551 
1552  /* Need to take off active list and put on finished list? */
1553  if (was_active && !is_active) {
1554  if (active_channels) smartlist_remove(active_channels, chan);
1555  if (!finished_channels) finished_channels = smartlist_new();
1556  smartlist_add(finished_channels, chan);
1558  }
1559  /* Need to put on active list? */
1560  else if (!was_active && is_active) {
1561  if (finished_channels) smartlist_remove(finished_channels, chan);
1562  if (!active_channels) active_channels = smartlist_new();
1563  smartlist_add(active_channels, chan);
1564  }
1565 
1566  if (!tor_digest_is_zero(chan->identity_digest)) {
1567  /* Now we need to handle the identity map */
1568  was_in_id_map = !(from_state == CHANNEL_STATE_CLOSING ||
1569  from_state == CHANNEL_STATE_CLOSED ||
1570  from_state == CHANNEL_STATE_ERROR);
1571  is_in_id_map = !(to_state == CHANNEL_STATE_CLOSING ||
1572  to_state == CHANNEL_STATE_CLOSED ||
1573  to_state == CHANNEL_STATE_ERROR);
1574 
1575  if (!was_in_id_map && is_in_id_map) channel_add_to_digest_map(chan);
1576  else if (was_in_id_map && !is_in_id_map)
1578  }
1579  }
1580 
1581  /*
1582  * If we're going to a closed/closing state, we don't need scheduling any
1583  * more; in CHANNEL_STATE_MAINT we can't accept writes.
1584  */
1585  if (to_state == CHANNEL_STATE_CLOSING ||
1586  to_state == CHANNEL_STATE_CLOSED ||
1587  to_state == CHANNEL_STATE_ERROR) {
1588  scheduler_release_channel(chan);
1589  } else if (to_state == CHANNEL_STATE_MAINT) {
1590  scheduler_channel_doesnt_want_writes(chan);
1591  }
1592 }
1593 
1597 void
1598 channel_change_state(channel_t *chan, channel_state_t to_state)
1599 {
1600  tor_assert(to_state != CHANNEL_STATE_OPEN);
1601  channel_change_state_(chan, to_state);
1602 }
1603 
1607 void
1609 {
1610  channel_change_state_(chan, CHANNEL_STATE_OPEN);
1611 
1612  /* Tell circuits if we opened and stuff */
1614  chan->has_been_open = 1;
1615 }
1616 
1624 void
1626  channel_listener_state_t to_state)
1627 {
1628  channel_listener_state_t from_state;
1629  unsigned char was_active, is_active;
1630 
1631  tor_assert(chan_l);
1632  from_state = chan_l->state;
1633 
1636  tor_assert(channel_listener_state_can_transition(chan_l->state, to_state));
1637 
1638  /* Check for no-op transitions */
1639  if (from_state == to_state) {
1640  log_debug(LD_CHANNEL,
1641  "Got no-op transition from \"%s\" to itself on channel "
1642  "listener %p (global ID %"PRIu64 ")",
1644  chan_l, (chan_l->global_identifier));
1645  return;
1646  }
1647 
1648  /* If we're going to a closing or closed state, we must have a reason set */
1649  if (to_state == CHANNEL_LISTENER_STATE_CLOSING ||
1650  to_state == CHANNEL_LISTENER_STATE_CLOSED ||
1651  to_state == CHANNEL_LISTENER_STATE_ERROR) {
1652  tor_assert(chan_l->reason_for_closing != CHANNEL_LISTENER_NOT_CLOSING);
1653  }
1654 
1655  log_debug(LD_CHANNEL,
1656  "Changing state of channel listener %p (global ID %"PRIu64
1657  "from \"%s\" to \"%s\"",
1658  chan_l, (chan_l->global_identifier),
1659  channel_listener_state_to_string(chan_l->state),
1661 
1662  chan_l->state = to_state;
1663 
1664  /* Need to add to the right lists if the channel listener is registered */
1665  if (chan_l->registered) {
1666  was_active = !(from_state == CHANNEL_LISTENER_STATE_CLOSED ||
1667  from_state == CHANNEL_LISTENER_STATE_ERROR);
1668  is_active = !(to_state == CHANNEL_LISTENER_STATE_CLOSED ||
1669  to_state == CHANNEL_LISTENER_STATE_ERROR);
1670 
1671  /* Need to take off active list and put on finished list? */
1672  if (was_active && !is_active) {
1673  if (active_listeners) smartlist_remove(active_listeners, chan_l);
1674  if (!finished_listeners) finished_listeners = smartlist_new();
1675  smartlist_add(finished_listeners, chan_l);
1677  }
1678  /* Need to put on active list? */
1679  else if (!was_active && is_active) {
1680  if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
1681  if (!active_listeners) active_listeners = smartlist_new();
1682  smartlist_add(active_listeners, chan_l);
1683  }
1684  }
1685 
1686  if (to_state == CHANNEL_LISTENER_STATE_CLOSED ||
1687  to_state == CHANNEL_LISTENER_STATE_ERROR) {
1688  tor_assert(!(chan_l->incoming_list) ||
1689  smartlist_len(chan_l->incoming_list) == 0);
1690  }
1691 }
1692 
1693 /* Maximum number of cells that is allowed to flush at once within
1694  * channel_flush_some_cells(). */
1695 #define MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED 256
1696 
1715 MOCK_IMPL(ssize_t,
1716 channel_flush_some_cells, (channel_t *chan, ssize_t num_cells))
1717 {
1718  unsigned int unlimited = 0;
1719  ssize_t flushed = 0;
1720  int clamped_num_cells;
1721 
1722  tor_assert(chan);
1723 
1724  if (num_cells < 0) unlimited = 1;
1725  if (!unlimited && num_cells <= flushed) goto done;
1726 
1727  /* If we aren't in CHANNEL_STATE_OPEN, nothing goes through */
1728  if (CHANNEL_IS_OPEN(chan)) {
1729  if (circuitmux_num_cells(chan->cmux) > 0) {
1730  /* Calculate number of cells, including clamp */
1731  if (unlimited) {
1732  clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
1733  } else {
1734  if (num_cells - flushed >
1735  MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED) {
1736  clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
1737  } else {
1738  clamped_num_cells = (int)(num_cells - flushed);
1739  }
1740  }
1741 
1742  /* Try to get more cells from any active circuits */
1743  flushed = channel_flush_from_first_active_circuit(
1744  chan, clamped_num_cells);
1745  }
1746  }
1747 
1748  done:
1749  return flushed;
1750 }
1751 
1759 channel_more_to_flush, (channel_t *chan))
1760 {
1761  tor_assert(chan);
1762 
1763  if (circuitmux_num_cells(chan->cmux) > 0) return 1;
1764 
1765  /* Else no */
1766  return 0;
1767 }
1768 
1775 void
1777 {
1778  tor_assert(chan);
1779 
1780  if (chan->dirreq_id != 0)
1782  DIRREQ_TUNNELED,
1784 }
1785 
1792 void
1794 {
1795  tor_assert(listener);
1796 
1797  /*
1798  * CHANNEL_LISTENER_STATE_CLOSING permitted because we drain the queue
1799  * while closing a listener.
1800  */
1801  tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING ||
1802  listener->state == CHANNEL_LISTENER_STATE_CLOSING);
1803  tor_assert(listener->listener);
1804 
1805  log_debug(LD_CHANNEL,
1806  "Processing queue of incoming connections for channel "
1807  "listener %p (global ID %"PRIu64 ")",
1808  listener, (listener->global_identifier));
1809 
1810  if (!(listener->incoming_list)) return;
1811 
1813  channel_t *, chan) {
1814  tor_assert(chan);
1815 
1816  log_debug(LD_CHANNEL,
1817  "Handling incoming channel %p (%"PRIu64 ") "
1818  "for listener %p (%"PRIu64 ")",
1819  chan,
1820  (chan->global_identifier),
1821  listener,
1822  (listener->global_identifier));
1823  /* Make sure this is set correctly */
1824  channel_mark_incoming(chan);
1825  listener->listener(listener, chan);
1826  } SMARTLIST_FOREACH_END(chan);
1827 
1828  smartlist_free(listener->incoming_list);
1829  listener->incoming_list = NULL;
1830 }
1831 
1844 void
1846 {
1847  tor_addr_t remote_addr;
1848  int started_here;
1849  time_t now = time(NULL);
1850  int close_origin_circuits = 0;
1851 
1852  tor_assert(chan);
1853 
1854  started_here = channel_is_outgoing(chan);
1855 
1856  if (started_here) {
1859  } else {
1860  /* only report it to the geoip module if it's a client */
1861  if (channel_is_client(chan)) {
1862  if (channel_get_addr_if_possible(chan, &remote_addr)) {
1863  char *transport_name = NULL;
1864  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
1865  if (chan->get_transport_name(chan, &transport_name) < 0)
1866  transport_name = NULL;
1867 
1869  &remote_addr, transport_name,
1870  now);
1871  tor_free(transport_name);
1872  /* Notify the DoS subsystem of a new client. */
1873  if (tlschan && tlschan->conn) {
1874  dos_new_client_conn(tlschan->conn);
1875  }
1876  }
1877  /* Otherwise the underlying transport can't tell us this, so skip it */
1878  }
1879  }
1880 
1881  /* Disable or reduce padding according to user prefs. */
1882  if (chan->padding_enabled || get_options()->ConnectionPadding == 1) {
1883  if (!get_options()->ConnectionPadding) {
1884  /* Disable if torrc disabled */
1885  channelpadding_disable_padding_on_channel(chan);
1886  } else if (rend_service_allow_non_anonymous_connection(get_options()) &&
1887  !networkstatus_get_param(NULL,
1888  CHANNELPADDING_SOS_PARAM,
1889  CHANNELPADDING_SOS_DEFAULT, 0, 1)) {
1890  /* Disable if we're using RSOS and the consensus disabled padding
1891  * for RSOS */
1892  channelpadding_disable_padding_on_channel(chan);
1893  } else if (get_options()->ReducedConnectionPadding) {
1894  /* Padding can be forced and/or reduced by clients, regardless of if
1895  * the channel supports it */
1896  channelpadding_reduce_padding_on_channel(chan);
1897  }
1898  }
1899 
1900  circuit_n_chan_done(chan, 1, close_origin_circuits);
1901 }
1902 
1910 void
1912  channel_t *incoming)
1913 {
1914  int need_to_queue = 0;
1915 
1916  tor_assert(listener);
1917  tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
1918  tor_assert(incoming);
1919 
1920  log_debug(LD_CHANNEL,
1921  "Queueing incoming channel %p (global ID %"PRIu64 ") on "
1922  "channel listener %p (global ID %"PRIu64 ")",
1923  incoming, (incoming->global_identifier),
1924  listener, (listener->global_identifier));
1925 
1926  /* Do we need to queue it, or can we just call the listener right away? */
1927  if (!(listener->listener)) need_to_queue = 1;
1928  if (listener->incoming_list &&
1929  (smartlist_len(listener->incoming_list) > 0))
1930  need_to_queue = 1;
1931 
1932  /* If we need to queue and have no queue, create one */
1933  if (need_to_queue && !(listener->incoming_list)) {
1934  listener->incoming_list = smartlist_new();
1935  }
1936 
1937  /* Bump the counter and timestamp it */
1940  ++(listener->n_accepted);
1941 
1942  /* If we don't need to queue, process it right away */
1943  if (!need_to_queue) {
1944  tor_assert(listener->listener);
1945  listener->listener(listener, incoming);
1946  }
1947  /*
1948  * Otherwise, we need to queue; queue and then process the queue if
1949  * we can.
1950  */
1951  else {
1952  tor_assert(listener->incoming_list);
1953  smartlist_add(listener->incoming_list, incoming);
1954  if (listener->listener) channel_listener_process_incoming(listener);
1955  }
1956 }
1957 
1961 void
1963 {
1964  tor_assert(chan);
1965  tor_assert(CHANNEL_IS_CLOSING(chan) || CHANNEL_IS_MAINT(chan) ||
1966  CHANNEL_IS_OPEN(chan));
1967  tor_assert(cell);
1968 
1969  /* Nothing we can do if we have no registered cell handlers */
1970  if (!chan->cell_handler)
1971  return;
1972 
1973  /* Timestamp for receiving */
1974  channel_timestamp_recv(chan);
1975  /* Update received counter. */
1976  ++(chan->n_cells_recved);
1977  chan->n_bytes_recved += get_cell_network_size(chan->wide_circ_ids);
1978 
1979  log_debug(LD_CHANNEL,
1980  "Processing incoming cell_t %p for channel %p (global ID "
1981  "%"PRIu64 ")", cell, chan,
1982  (chan->global_identifier));
1983  chan->cell_handler(chan, cell);
1984 }
1985 
1989 /* XXXX Move this function. */
1990 int
1992  const packed_cell_t *packed_cell,
1993  circid_t *circid_out)
1994 {
1995  if (chan->wide_circ_ids) {
1996  if (packed_cell->body[4] == CELL_DESTROY) {
1997  *circid_out = ntohl(get_uint32(packed_cell->body));
1998  return 1;
1999  }
2000  } else {
2001  if (packed_cell->body[2] == CELL_DESTROY) {
2002  *circid_out = ntohs(get_uint16(packed_cell->body));
2003  return 1;
2004  }
2005  }
2006  return 0;
2007 }
2008 
2016 int
2017 channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
2018 {
2019  tor_assert(chan);
2020  if (circ_id == 0) {
2021  log_warn(LD_BUG, "Attempted to send a destroy cell for circID 0 "
2022  "on a channel %"PRIu64 " at %p in state %s (%d)",
2023  (chan->global_identifier),
2024  chan, channel_state_to_string(chan->state),
2025  chan->state);
2026  return 0;
2027  }
2028 
2029  /* Check to make sure we can send on this channel first */
2030  if (!CHANNEL_CONDEMNED(chan) && chan->cmux) {
2031  channel_note_destroy_pending(chan, circ_id);
2032  circuitmux_append_destroy_cell(chan, chan->cmux, circ_id, reason);
2033  log_debug(LD_OR,
2034  "Sending destroy (circID %u) on channel %p "
2035  "(global ID %"PRIu64 ")",
2036  (unsigned)circ_id, chan,
2037  (chan->global_identifier));
2038  } else {
2039  log_warn(LD_BUG,
2040  "Someone called channel_send_destroy() for circID %u "
2041  "on a channel %"PRIu64 " at %p in state %s (%d)",
2042  (unsigned)circ_id, (chan->global_identifier),
2043  chan, channel_state_to_string(chan->state),
2044  chan->state);
2045  }
2046 
2047  return 0;
2048 }
2049 
2056 void
2057 channel_dumpstats(int severity)
2058 {
2059  if (all_channels && smartlist_len(all_channels) > 0) {
2060  tor_log(severity, LD_GENERAL,
2061  "Dumping statistics about %d channels:",
2062  smartlist_len(all_channels));
2063  tor_log(severity, LD_GENERAL,
2064  "%d are active, and %d are done and waiting for cleanup",
2065  (active_channels != NULL) ?
2066  smartlist_len(active_channels) : 0,
2067  (finished_channels != NULL) ?
2068  smartlist_len(finished_channels) : 0);
2069 
2070  SMARTLIST_FOREACH(all_channels, channel_t *, chan,
2071  channel_dump_statistics(chan, severity));
2072 
2073  tor_log(severity, LD_GENERAL,
2074  "Done spamming about channels now");
2075  } else {
2076  tor_log(severity, LD_GENERAL,
2077  "No channels to dump");
2078  }
2079 }
2080 
2087 void
2089 {
2090  if (all_listeners && smartlist_len(all_listeners) > 0) {
2091  tor_log(severity, LD_GENERAL,
2092  "Dumping statistics about %d channel listeners:",
2093  smartlist_len(all_listeners));
2094  tor_log(severity, LD_GENERAL,
2095  "%d are active and %d are done and waiting for cleanup",
2096  (active_listeners != NULL) ?
2097  smartlist_len(active_listeners) : 0,
2098  (finished_listeners != NULL) ?
2099  smartlist_len(finished_listeners) : 0);
2100 
2101  SMARTLIST_FOREACH(all_listeners, channel_listener_t *, chan_l,
2102  channel_listener_dump_statistics(chan_l, severity));
2103 
2104  tor_log(severity, LD_GENERAL,
2105  "Done spamming about channel listeners now");
2106  } else {
2107  tor_log(severity, LD_GENERAL,
2108  "No channel listeners to dump");
2109  }
2110 }
2111 
2118 void
2120 {
2121  channel_t *tmp = NULL;
2122 
2123  /* Check if we need to do anything */
2124  if (!finished_channels || smartlist_len(finished_channels) == 0) return;
2125 
2126  /* Iterate through finished_channels and get rid of them */
2127  SMARTLIST_FOREACH_BEGIN(finished_channels, channel_t *, curr) {
2128  tmp = curr;
2129  /* Remove it from the list */
2130  SMARTLIST_DEL_CURRENT(finished_channels, curr);
2131  /* Also unregister it */
2132  channel_unregister(tmp);
2133  /* ... and free it */
2134  channel_free(tmp);
2135  } SMARTLIST_FOREACH_END(curr);
2136 }
2137 
2144 void
2146 {
2147  channel_listener_t *tmp = NULL;
2148 
2149  /* Check if we need to do anything */
2150  if (!finished_listeners || smartlist_len(finished_listeners) == 0) return;
2151 
2152  /* Iterate through finished_channels and get rid of them */
2153  SMARTLIST_FOREACH_BEGIN(finished_listeners, channel_listener_t *, curr) {
2154  tmp = curr;
2155  /* Remove it from the list */
2156  SMARTLIST_DEL_CURRENT(finished_listeners, curr);
2157  /* Also unregister it */
2159  /* ... and free it */
2160  channel_listener_free(tmp);
2161  } SMARTLIST_FOREACH_END(curr);
2162 }
2163 
2167 static void
2168 channel_free_list(smartlist_t *channels, int mark_for_close)
2169 {
2170  if (!channels) return;
2171 
2172  SMARTLIST_FOREACH_BEGIN(channels, channel_t *, curr) {
2173  /* Deregister and free it */
2174  tor_assert(curr);
2175  log_debug(LD_CHANNEL,
2176  "Cleaning up channel %p (global ID %"PRIu64 ") "
2177  "in state %s (%d)",
2178  curr, (curr->global_identifier),
2179  channel_state_to_string(curr->state), curr->state);
2180  /* Detach circuits early so they can find the channel */
2181  if (curr->cmux) {
2182  circuitmux_detach_all_circuits(curr->cmux, NULL);
2183  }
2184  SMARTLIST_DEL_CURRENT(channels, curr);
2185  channel_unregister(curr);
2186  if (mark_for_close) {
2187  if (!CHANNEL_CONDEMNED(curr)) {
2188  channel_mark_for_close(curr);
2189  }
2190  channel_force_xfree(curr);
2191  } else channel_free(curr);
2192  } SMARTLIST_FOREACH_END(curr);
2193 }
2194 
2198 static void
2199 channel_listener_free_list(smartlist_t *listeners, int mark_for_close)
2200 {
2201  if (!listeners) return;
2202 
2203  SMARTLIST_FOREACH_BEGIN(listeners, channel_listener_t *, curr) {
2204  /* Deregister and free it */
2205  tor_assert(curr);
2206  log_debug(LD_CHANNEL,
2207  "Cleaning up channel listener %p (global ID %"PRIu64 ") "
2208  "in state %s (%d)",
2209  curr, (curr->global_identifier),
2210  channel_listener_state_to_string(curr->state), curr->state);
2212  if (mark_for_close) {
2213  if (!(curr->state == CHANNEL_LISTENER_STATE_CLOSING ||
2214  curr->state == CHANNEL_LISTENER_STATE_CLOSED ||
2215  curr->state == CHANNEL_LISTENER_STATE_ERROR)) {
2217  }
2219  } else channel_listener_free(curr);
2220  } SMARTLIST_FOREACH_END(curr);
2221 }
2222 
2231 void
2233 {
2234  log_debug(LD_CHANNEL,
2235  "Shutting down channels...");
2236 
2237  /* First, let's go for finished channels */
2238  if (finished_channels) {
2239  channel_free_list(finished_channels, 0);
2240  smartlist_free(finished_channels);
2241  finished_channels = NULL;
2242  }
2243 
2244  /* Now the finished listeners */
2245  if (finished_listeners) {
2246  channel_listener_free_list(finished_listeners, 0);
2247  smartlist_free(finished_listeners);
2248  finished_listeners = NULL;
2249  }
2250 
2251  /* Now all active channels */
2252  if (active_channels) {
2253  channel_free_list(active_channels, 1);
2254  smartlist_free(active_channels);
2255  active_channels = NULL;
2256  }
2257 
2258  /* Now all active listeners */
2259  if (active_listeners) {
2260  channel_listener_free_list(active_listeners, 1);
2261  smartlist_free(active_listeners);
2262  active_listeners = NULL;
2263  }
2264 
2265  /* Now all channels, in case any are left over */
2266  if (all_channels) {
2267  channel_free_list(all_channels, 1);
2268  smartlist_free(all_channels);
2269  all_channels = NULL;
2270  }
2271 
2272  /* Now all listeners, in case any are left over */
2273  if (all_listeners) {
2274  channel_listener_free_list(all_listeners, 1);
2275  smartlist_free(all_listeners);
2276  all_listeners = NULL;
2277  }
2278 
2279  /* Now free channel_identity_map */
2280  log_debug(LD_CHANNEL,
2281  "Freeing channel_identity_map");
2282  /* Geez, anything still left over just won't die ... let it leak then */
2283  HT_CLEAR(channel_idmap, &channel_identity_map);
2284 
2285  /* Same with channel_gid_map */
2286  log_debug(LD_CHANNEL,
2287  "Freeing channel_gid_map");
2288  HT_CLEAR(channel_gid_map, &channel_gid_map);
2289 
2290  log_debug(LD_CHANNEL,
2291  "Done cleaning up after channels");
2292 }
2293 
2304 channel_t *
2305 channel_connect(const tor_addr_t *addr, uint16_t port,
2306  const char *id_digest,
2307  const ed25519_public_key_t *ed_id)
2308 {
2309  return channel_tls_connect(addr, port, id_digest, ed_id);
2310 }
2311 
2322 int
2324 {
2325  int a_is_canonical, b_is_canonical;
2326 
2327  tor_assert(a);
2328  tor_assert(b);
2329 
2330  /* If one channel is bad for new circuits, and the other isn't,
2331  * use the one that is still good. */
2333  return 1;
2335  return 0;
2336 
2337  /* Check if one is canonical and the other isn't first */
2338  a_is_canonical = channel_is_canonical(a);
2339  b_is_canonical = channel_is_canonical(b);
2340 
2341  if (a_is_canonical && !b_is_canonical) return 1;
2342  if (!a_is_canonical && b_is_canonical) return 0;
2343 
2344  /* Check if we suspect that one of the channels will be preferred
2345  * by the peer */
2346  if (a->is_canonical_to_peer && !b->is_canonical_to_peer) return 1;
2347  if (!a->is_canonical_to_peer && b->is_canonical_to_peer) return 0;
2348 
2349  /*
2350  * Okay, if we're here they tied on canonicity, the prefer the older
2351  * connection, so that the adversary can't create a new connection
2352  * and try to switch us over to it (which will leak information
2353  * about long-lived circuits). Additionally, switching connections
2354  * too often makes us more vulnerable to attacks like Torscan and
2355  * passive netflow-based equivalents.
2356  *
2357  * Connections will still only live for at most a week, due to
2358  * the check in connection_or_group_set_badness() against
2359  * TIME_BEFORE_OR_CONN_IS_TOO_OLD, which marks old connections as
2360  * unusable for new circuits after 1 week. That check sets
2361  * is_bad_for_new_circs, which is checked in channel_get_for_extend().
2362  *
2363  * We check channel_is_bad_for_new_circs() above here anyway, for safety.
2364  */
2365  if (channel_when_created(a) < channel_when_created(b)) return 1;
2366  else if (channel_when_created(a) > channel_when_created(b)) return 0;
2367 
2368  if (channel_num_circuits(a) > channel_num_circuits(b)) return 1;
2369  else return 0;
2370 }
2371 
2382 channel_t *
2383 channel_get_for_extend(const char *rsa_id_digest,
2384  const ed25519_public_key_t *ed_id,
2385  const tor_addr_t *target_addr,
2386  const char **msg_out,
2387  int *launch_out)
2388 {
2389  channel_t *chan, *best = NULL;
2390  int n_inprogress_goodaddr = 0, n_old = 0;
2391  int n_noncanonical = 0;
2392 
2393  tor_assert(msg_out);
2394  tor_assert(launch_out);
2395 
2396  chan = channel_find_by_remote_identity(rsa_id_digest, ed_id);
2397 
2398  /* Walk the list, unrefing the old one and refing the new at each
2399  * iteration.
2400  */
2401  for (; chan; chan = channel_next_with_rsa_identity(chan)) {
2403  rsa_id_digest, DIGEST_LEN));
2404 
2405  if (CHANNEL_CONDEMNED(chan))
2406  continue;
2407 
2408  /* Never return a channel on which the other end appears to be
2409  * a client. */
2410  if (channel_is_client(chan)) {
2411  continue;
2412  }
2413 
2414  /* The Ed25519 key has to match too */
2415  if (!channel_remote_identity_matches(chan, rsa_id_digest, ed_id)) {
2416  continue;
2417  }
2418 
2419  /* Never return a non-open connection. */
2420  if (!CHANNEL_IS_OPEN(chan)) {
2421  /* If the address matches, don't launch a new connection for this
2422  * circuit. */
2423  if (channel_matches_target_addr_for_extend(chan, target_addr))
2424  ++n_inprogress_goodaddr;
2425  continue;
2426  }
2427 
2428  /* Never return a connection that shouldn't be used for circs. */
2429  if (channel_is_bad_for_new_circs(chan)) {
2430  ++n_old;
2431  continue;
2432  }
2433 
2434  /* Never return a non-canonical connection using a recent link protocol
2435  * if the address is not what we wanted.
2436  *
2437  * The channel_is_canonical_is_reliable() function asks the lower layer
2438  * if we should trust channel_is_canonical(). The below is from the
2439  * comments of the old circuit_or_get_for_extend() and applies when
2440  * the lower-layer transport is channel_tls_t.
2441  *
2442  * (For old link protocols, we can't rely on is_canonical getting
2443  * set properly if we're talking to the right address, since we might
2444  * have an out-of-date descriptor, and we will get no NETINFO cell to
2445  * tell us about the right address.)
2446  */
2447  if (!channel_is_canonical(chan) &&
2449  !channel_matches_target_addr_for_extend(chan, target_addr)) {
2450  ++n_noncanonical;
2451  continue;
2452  }
2453 
2454  if (!best) {
2455  best = chan; /* If we have no 'best' so far, this one is good enough. */
2456  continue;
2457  }
2458 
2459  if (channel_is_better(chan, best))
2460  best = chan;
2461  }
2462 
2463  if (best) {
2464  *msg_out = "Connection is fine; using it.";
2465  *launch_out = 0;
2466  return best;
2467  } else if (n_inprogress_goodaddr) {
2468  *msg_out = "Connection in progress; waiting.";
2469  *launch_out = 0;
2470  return NULL;
2471  } else if (n_old || n_noncanonical) {
2472  *msg_out = "Connections all too old, or too non-canonical. "
2473  " Launching a new one.";
2474  *launch_out = 1;
2475  return NULL;
2476  } else {
2477  *msg_out = "Not connected. Connecting.";
2478  *launch_out = 1;
2479  return NULL;
2480  }
2481 }
2482 
2489 const char *
2491 {
2492  tor_assert(chan);
2494 
2495  return chan->describe_transport(chan);
2496 }
2497 
2504 const char *
2506 {
2507  tor_assert(chan_l);
2508  tor_assert(chan_l->describe_transport);
2509 
2510  return chan_l->describe_transport(chan_l);
2511 }
2512 
2519 channel_dump_statistics, (channel_t *chan, int severity))
2520 {
2521  double avg, interval, age;
2522  time_t now = time(NULL);
2523  tor_addr_t remote_addr;
2524  int have_remote_addr;
2525  char *remote_addr_str;
2526 
2527  tor_assert(chan);
2528 
2529  age = (double)(now - chan->timestamp_created);
2530 
2531  tor_log(severity, LD_GENERAL,
2532  "Channel %"PRIu64 " (at %p) with transport %s is in state "
2533  "%s (%d)",
2534  (chan->global_identifier), chan,
2536  channel_state_to_string(chan->state), chan->state);
2537  tor_log(severity, LD_GENERAL,
2538  " * Channel %"PRIu64 " was created at %"PRIu64
2539  " (%"PRIu64 " seconds ago) "
2540  "and last active at %"PRIu64 " (%"PRIu64 " seconds ago)",
2541  (chan->global_identifier),
2542  (uint64_t)(chan->timestamp_created),
2543  (uint64_t)(now - chan->timestamp_created),
2544  (uint64_t)(chan->timestamp_active),
2545  (uint64_t)(now - chan->timestamp_active));
2546 
2547  /* Handle digest. */
2548  if (!tor_digest_is_zero(chan->identity_digest)) {
2549  tor_log(severity, LD_GENERAL,
2550  " * Channel %"PRIu64 " says it is connected "
2551  "to an OR with digest %s",
2552  (chan->global_identifier),
2554  } else {
2555  tor_log(severity, LD_GENERAL,
2556  " * Channel %"PRIu64 " does not know the digest"
2557  " of the OR it is connected to",
2558  (chan->global_identifier));
2559  }
2560 
2561  /* Handle remote address and descriptions */
2562  have_remote_addr = channel_get_addr_if_possible(chan, &remote_addr);
2563  if (have_remote_addr) {
2564  char *actual = tor_strdup(channel_get_actual_remote_descr(chan));
2565  remote_addr_str = tor_addr_to_str_dup(&remote_addr);
2566  tor_log(severity, LD_GENERAL,
2567  " * Channel %"PRIu64 " says its remote address"
2568  " is %s, and gives a canonical description of \"%s\" and an "
2569  "actual description of \"%s\"",
2570  (chan->global_identifier),
2571  safe_str(remote_addr_str),
2572  safe_str(channel_get_canonical_remote_descr(chan)),
2573  safe_str(actual));
2574  tor_free(remote_addr_str);
2575  tor_free(actual);
2576  } else {
2577  char *actual = tor_strdup(channel_get_actual_remote_descr(chan));
2578  tor_log(severity, LD_GENERAL,
2579  " * Channel %"PRIu64 " does not know its remote "
2580  "address, but gives a canonical description of \"%s\" and an "
2581  "actual description of \"%s\"",
2582  (chan->global_identifier),
2584  actual);
2585  tor_free(actual);
2586  }
2587 
2588  /* Handle marks */
2589  tor_log(severity, LD_GENERAL,
2590  " * Channel %"PRIu64 " has these marks: %s %s %s "
2591  "%s %s %s",
2592  (chan->global_identifier),
2594  "bad_for_new_circs" : "!bad_for_new_circs",
2595  channel_is_canonical(chan) ?
2596  "canonical" : "!canonical",
2598  "is_canonical_is_reliable" :
2599  "!is_canonical_is_reliable",
2600  channel_is_client(chan) ?
2601  "client" : "!client",
2602  channel_is_local(chan) ?
2603  "local" : "!local",
2604  channel_is_incoming(chan) ?
2605  "incoming" : "outgoing");
2606 
2607  /* Describe circuits */
2608  tor_log(severity, LD_GENERAL,
2609  " * Channel %"PRIu64 " has %d active circuits out of"
2610  " %d in total",
2611  (chan->global_identifier),
2612  (chan->cmux != NULL) ?
2614  (chan->cmux != NULL) ?
2615  circuitmux_num_circuits(chan->cmux) : 0);
2616 
2617  /* Describe timestamps */
2618  tor_log(severity, LD_GENERAL,
2619  " * Channel %"PRIu64 " was last used by a "
2620  "client at %"PRIu64 " (%"PRIu64 " seconds ago)",
2621  (chan->global_identifier),
2622  (uint64_t)(chan->timestamp_client),
2623  (uint64_t)(now - chan->timestamp_client));
2624  tor_log(severity, LD_GENERAL,
2625  " * Channel %"PRIu64 " last received a cell "
2626  "at %"PRIu64 " (%"PRIu64 " seconds ago)",
2627  (chan->global_identifier),
2628  (uint64_t)(chan->timestamp_recv),
2629  (uint64_t)(now - chan->timestamp_recv));
2630  tor_log(severity, LD_GENERAL,
2631  " * Channel %"PRIu64 " last transmitted a cell "
2632  "at %"PRIu64 " (%"PRIu64 " seconds ago)",
2633  (chan->global_identifier),
2634  (uint64_t)(chan->timestamp_xmit),
2635  (uint64_t)(now - chan->timestamp_xmit));
2636 
2637  /* Describe counters and rates */
2638  tor_log(severity, LD_GENERAL,
2639  " * Channel %"PRIu64 " has received "
2640  "%"PRIu64 " bytes in %"PRIu64 " cells and transmitted "
2641  "%"PRIu64 " bytes in %"PRIu64 " cells",
2642  (chan->global_identifier),
2643  (chan->n_bytes_recved),
2644  (chan->n_cells_recved),
2645  (chan->n_bytes_xmitted),
2646  (chan->n_cells_xmitted));
2647  if (now > chan->timestamp_created &&
2648  chan->timestamp_created > 0) {
2649  if (chan->n_bytes_recved > 0) {
2650  avg = (double)(chan->n_bytes_recved) / age;
2651  tor_log(severity, LD_GENERAL,
2652  " * Channel %"PRIu64 " has averaged %f "
2653  "bytes received per second",
2654  (chan->global_identifier), avg);
2655  }
2656  if (chan->n_cells_recved > 0) {
2657  avg = (double)(chan->n_cells_recved) / age;
2658  if (avg >= 1.0) {
2659  tor_log(severity, LD_GENERAL,
2660  " * Channel %"PRIu64 " has averaged %f "
2661  "cells received per second",
2662  (chan->global_identifier), avg);
2663  } else if (avg >= 0.0) {
2664  interval = 1.0 / avg;
2665  tor_log(severity, LD_GENERAL,
2666  " * Channel %"PRIu64 " has averaged %f "
2667  "seconds between received cells",
2668  (chan->global_identifier), interval);
2669  }
2670  }
2671  if (chan->n_bytes_xmitted > 0) {
2672  avg = (double)(chan->n_bytes_xmitted) / age;
2673  tor_log(severity, LD_GENERAL,
2674  " * Channel %"PRIu64 " has averaged %f "
2675  "bytes transmitted per second",
2676  (chan->global_identifier), avg);
2677  }
2678  if (chan->n_cells_xmitted > 0) {
2679  avg = (double)(chan->n_cells_xmitted) / age;
2680  if (avg >= 1.0) {
2681  tor_log(severity, LD_GENERAL,
2682  " * Channel %"PRIu64 " has averaged %f "
2683  "cells transmitted per second",
2684  (chan->global_identifier), avg);
2685  } else if (avg >= 0.0) {
2686  interval = 1.0 / avg;
2687  tor_log(severity, LD_GENERAL,
2688  " * Channel %"PRIu64 " has averaged %f "
2689  "seconds between transmitted cells",
2690  (chan->global_identifier), interval);
2691  }
2692  }
2693  }
2694 
2695  /* Dump anything the lower layer has to say */
2696  channel_dump_transport_statistics(chan, severity);
2697 }
2698 
2704 void
2706 {
2707  double avg, interval, age;
2708  time_t now = time(NULL);
2709 
2710  tor_assert(chan_l);
2711 
2712  age = (double)(now - chan_l->timestamp_created);
2713 
2714  tor_log(severity, LD_GENERAL,
2715  "Channel listener %"PRIu64 " (at %p) with transport %s is in "
2716  "state %s (%d)",
2717  (chan_l->global_identifier), chan_l,
2719  channel_listener_state_to_string(chan_l->state), chan_l->state);
2720  tor_log(severity, LD_GENERAL,
2721  " * Channel listener %"PRIu64 " was created at %"PRIu64
2722  " (%"PRIu64 " seconds ago) "
2723  "and last active at %"PRIu64 " (%"PRIu64 " seconds ago)",
2724  (chan_l->global_identifier),
2725  (uint64_t)(chan_l->timestamp_created),
2726  (uint64_t)(now - chan_l->timestamp_created),
2727  (uint64_t)(chan_l->timestamp_active),
2728  (uint64_t)(now - chan_l->timestamp_active));
2729 
2730  tor_log(severity, LD_GENERAL,
2731  " * Channel listener %"PRIu64 " last accepted an incoming "
2732  "channel at %"PRIu64 " (%"PRIu64 " seconds ago) "
2733  "and has accepted %"PRIu64 " channels in total",
2734  (chan_l->global_identifier),
2735  (uint64_t)(chan_l->timestamp_accepted),
2736  (uint64_t)(now - chan_l->timestamp_accepted),
2737  (uint64_t)(chan_l->n_accepted));
2738 
2739  /*
2740  * If it's sensible to do so, get the rate of incoming channels on this
2741  * listener
2742  */
2743  if (now > chan_l->timestamp_created &&
2744  chan_l->timestamp_created > 0 &&
2745  chan_l->n_accepted > 0) {
2746  avg = (double)(chan_l->n_accepted) / age;
2747  if (avg >= 1.0) {
2748  tor_log(severity, LD_GENERAL,
2749  " * Channel listener %"PRIu64 " has averaged %f incoming "
2750  "channels per second",
2751  (chan_l->global_identifier), avg);
2752  } else if (avg >= 0.0) {
2753  interval = 1.0 / avg;
2754  tor_log(severity, LD_GENERAL,
2755  " * Channel listener %"PRIu64 " has averaged %f seconds "
2756  "between incoming channels",
2757  (chan_l->global_identifier), interval);
2758  }
2759  }
2760 
2761  /* Dump anything the lower layer has to say */
2763 }
2764 
2770 void
2772 {
2773  tor_assert(chan);
2774 
2775  if (chan->dumpstats) chan->dumpstats(chan, severity);
2776 }
2777 
2783 void
2785  int severity)
2786 {
2787  tor_assert(chan_l);
2788 
2789  if (chan_l->dumpstats) chan_l->dumpstats(chan_l, severity);
2790 }
2791 
2802 const char *
2804 {
2805  tor_assert(chan);
2807 
2808  /* Param 1 indicates the actual description */
2809  return chan->get_remote_descr(chan, GRD_FLAG_ORIGINAL);
2810 }
2811 
2818 const char *
2820 {
2821  /* Param 1 indicates the actual description */
2822  return chan->get_remote_descr(chan, GRD_FLAG_ORIGINAL|GRD_FLAG_ADDR_ONLY);
2823 }
2824 
2835 const char *
2837 {
2838  tor_assert(chan);
2840 
2841  /* Param 0 indicates the canonicalized description */
2842  return chan->get_remote_descr(chan, 0);
2843 }
2844 
2853 channel_get_addr_if_possible,(channel_t *chan, tor_addr_t *addr_out))
2854 {
2855  tor_assert(chan);
2856  tor_assert(addr_out);
2857 
2858  if (chan->get_remote_addr)
2859  return chan->get_remote_addr(chan, addr_out);
2860  /* Else no support, method not implemented */
2861  else return 0;
2862 }
2863 
2868 int
2870 {
2871  tor_assert(chan);
2873 
2874  /* Check with the lower layer */
2875  return chan->has_queued_writes(chan);
2876 }
2877 
2884 int
2886 {
2887  tor_assert(chan);
2888 
2889  return chan->is_bad_for_new_circs;
2890 }
2891 
2897 void
2899 {
2900  tor_assert(chan);
2901 
2902  chan->is_bad_for_new_circs = 1;
2903 }
2904 
2912 int
2914 {
2915  tor_assert(chan);
2916 
2917  return chan->is_client;
2918 }
2919 
2925 void
2927 {
2928  tor_assert(chan);
2929 
2930  chan->is_client = 1;
2931 }
2932 
2938 void
2940 {
2941  tor_assert(chan);
2942 
2943  chan->is_client = 0;
2944 }
2945 
2952 int
2954 {
2955  tor_assert(chan);
2956  tor_assert(chan->is_canonical);
2957 
2958  return chan->is_canonical(chan, 0);
2959 }
2960 
2967 int
2969 {
2970  tor_assert(chan);
2971  tor_assert(chan->is_canonical);
2972 
2973  return chan->is_canonical(chan, 1);
2974 }
2975 
2982 int
2984 {
2985  tor_assert(chan);
2986 
2987  return chan->is_incoming;
2988 }
2989 
2996 void
2998 {
2999  tor_assert(chan);
3000 
3001  chan->is_incoming = 1;
3002 }
3003 
3013 int
3015 {
3016  tor_assert(chan);
3017 
3018  return chan->is_local;
3019 }
3020 
3028 void
3030 {
3031  tor_assert(chan);
3032 
3033  chan->is_local = 1;
3034 }
3035 
3044 void
3046 {
3047  tor_assert(chan);
3048 
3049  chan->is_local = 0;
3050 }
3051 
3059 int
3061 {
3062  tor_assert(chan);
3063 
3064  return !(chan->is_incoming);
3065 }
3066 
3073 void
3075 {
3076  tor_assert(chan);
3077 
3078  chan->is_incoming = 0;
3079 }
3080 
3081 /************************
3082  * Flow control queries *
3083  ***********************/
3084 
3090 int
3092 {
3093  int result;
3094 
3095  tor_assert(chan);
3096  tor_assert(chan->num_cells_writeable);
3097 
3098  if (chan->state == CHANNEL_STATE_OPEN) {
3099  /* Query lower layer */
3100  result = chan->num_cells_writeable(chan);
3101  if (result < 0) result = 0;
3102  } else {
3103  /* No cells are writeable in any other state */
3104  result = 0;
3105  }
3106 
3107  return result;
3108 }
3109 
3110 /*********************
3111  * Timestamp updates *
3112  ********************/
3113 
3120 void
3122 {
3123  time_t now = time(NULL);
3124 
3125  tor_assert(chan);
3126 
3127  chan->timestamp_created = now;
3128 }
3129 
3136 void
3138 {
3139  time_t now = time(NULL);
3140 
3141  tor_assert(chan_l);
3142 
3143  chan_l->timestamp_created = now;
3144 }
3145 
3156 void
3158 {
3159  time_t now = time(NULL);
3160 
3161  tor_assert(chan);
3162  monotime_coarse_get(&chan->timestamp_xfer);
3163 
3164  chan->timestamp_active = now;
3165 
3166  /* Clear any potential netflow padding timer. We're active */
3167  monotime_coarse_zero(&chan->next_padding_time);
3168 }
3169 
3173 void
3175 {
3176  time_t now = time(NULL);
3177 
3178  tor_assert(chan_l);
3179 
3180  chan_l->timestamp_active = now;
3181 }
3182 
3190 void
3192 {
3193  time_t now = time(NULL);
3194 
3195  tor_assert(chan_l);
3196 
3197  chan_l->timestamp_active = now;
3198  chan_l->timestamp_accepted = now;
3199 }
3200 
3207 void
3209 {
3210  time_t now = time(NULL);
3211 
3212  tor_assert(chan);
3213 
3214  chan->timestamp_client = now;
3215 }
3216 
3223 void
3225 {
3226  time_t now = time(NULL);
3227  tor_assert(chan);
3228  monotime_coarse_get(&chan->timestamp_xfer);
3229 
3230  chan->timestamp_active = now;
3231  chan->timestamp_recv = now;
3232 
3233  /* Clear any potential netflow padding timer. We're active */
3234  monotime_coarse_zero(&chan->next_padding_time);
3235 }
3236 
3243 void
3245 {
3246  time_t now = time(NULL);
3247  tor_assert(chan);
3248 
3249  monotime_coarse_get(&chan->timestamp_xfer);
3250 
3251  chan->timestamp_active = now;
3252  chan->timestamp_xmit = now;
3253 
3254  /* Clear any potential netflow padding timer. We're active */
3255  monotime_coarse_zero(&chan->next_padding_time);
3256 }
3257 
3258 /***************************************************************
3259  * Timestamp queries - see above for definitions of timestamps *
3260  **************************************************************/
3261 
3265 time_t
3267 {
3268  tor_assert(chan);
3269 
3270  return chan->timestamp_created;
3271 }
3272 
3276 time_t
3278 {
3279  tor_assert(chan);
3280 
3281  return chan->timestamp_client;
3282 }
3283 
3287 time_t
3289 {
3290  tor_assert(chan);
3291 
3292  return chan->timestamp_xmit;
3293 }
3294 
3301 int
3303 {
3304  tor_assert(chan);
3306  tor_assert(extend_info);
3307 
3308  return chan->matches_extend_info(chan, extend_info);
3309 }
3310 
3317 int
3319  const tor_addr_t *target)
3320 {
3321  tor_assert(chan);
3322  tor_assert(chan->matches_target);
3323  tor_assert(target);
3324 
3325  return chan->matches_target(chan, target);
3326 }
3327 
3334 unsigned int
3336 {
3337  tor_assert(chan);
3338 
3339  return chan->num_n_circuits +
3340  chan->num_p_circuits;
3341 }
3342 
3350 channel_set_circid_type,(channel_t *chan,
3351  crypto_pk_t *identity_rcvd,
3352  int consider_identity))
3353 {
3354  int started_here;
3355  crypto_pk_t *our_identity;
3356 
3357  tor_assert(chan);
3358 
3359  started_here = channel_is_outgoing(chan);
3360 
3361  if (! consider_identity) {
3362  if (started_here)
3364  else
3366  return;
3367  }
3368 
3369  our_identity = started_here ?
3371 
3372  if (identity_rcvd) {
3373  if (crypto_pk_cmp_keys(our_identity, identity_rcvd) < 0) {
3375  } else {
3377  }
3378  } else {
3380  }
3381 }
3382 
3383 static int
3384 channel_sort_by_ed25519_identity(const void **a_, const void **b_)
3385 {
3386  const channel_t *a = *a_,
3387  *b = *b_;
3388  return fast_memcmp(&a->ed25519_identity.pubkey,
3389  &b->ed25519_identity.pubkey,
3390  sizeof(a->ed25519_identity.pubkey));
3391 }
3392 
3397 static void
3398 channel_rsa_id_group_set_badness(struct channel_list_s *lst, int force)
3399 {
3400  /*XXXX This function should really be about channels. 15056 */
3401  channel_t *chan = TOR_LIST_FIRST(lst);
3402 
3403  if (!chan)
3404  return;
3405 
3406  /* if there is only one channel, don't bother looping */
3407  if (PREDICT_LIKELY(!TOR_LIST_NEXT(chan, next_with_same_id))) {
3409  time(NULL), BASE_CHAN_TO_TLS(chan)->conn, force);
3410  return;
3411  }
3412 
3413  smartlist_t *channels = smartlist_new();
3414 
3415  TOR_LIST_FOREACH(chan, lst, next_with_same_id) {
3416  if (BASE_CHAN_TO_TLS(chan)->conn) {
3417  smartlist_add(channels, chan);
3418  }
3419  }
3420 
3421  smartlist_sort(channels, channel_sort_by_ed25519_identity);
3422 
3423  const ed25519_public_key_t *common_ed25519_identity = NULL;
3424  /* it would be more efficient to do a slice, but this case is rare */
3425  smartlist_t *or_conns = smartlist_new();
3426  SMARTLIST_FOREACH_BEGIN(channels, channel_t *, channel) {
3427  tor_assert(channel); // Suppresses some compiler warnings.
3428 
3429  if (!common_ed25519_identity)
3430  common_ed25519_identity = &channel->ed25519_identity;
3431 
3432  if (! ed25519_pubkey_eq(&channel->ed25519_identity,
3433  common_ed25519_identity)) {
3434  connection_or_group_set_badness_(or_conns, force);
3435  smartlist_clear(or_conns);
3436  common_ed25519_identity = &channel->ed25519_identity;
3437  }
3438 
3439  smartlist_add(or_conns, BASE_CHAN_TO_TLS(channel)->conn);
3440  } SMARTLIST_FOREACH_END(channel);
3441 
3442  connection_or_group_set_badness_(or_conns, force);
3443 
3444  /* XXXX 15056 we may want to do something special with connections that have
3445  * no set Ed25519 identity! */
3446 
3447  smartlist_free(or_conns);
3448  smartlist_free(channels);
3449 }
3450 
3456 void
3457 channel_update_bad_for_new_circs(const char *digest, int force)
3458 {
3459  if (digest) {
3460  channel_idmap_entry_t *ent;
3461  channel_idmap_entry_t search;
3462  memset(&search, 0, sizeof(search));
3463  memcpy(search.digest, digest, DIGEST_LEN);
3464  ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
3465  if (ent) {
3466  channel_rsa_id_group_set_badness(&ent->channel_list, force);
3467  }
3468  return;
3469  }
3470 
3471  /* no digest; just look at everything. */
3472  channel_idmap_entry_t **iter;
3473  HT_FOREACH(iter, channel_idmap, &channel_identity_map) {
3474  channel_rsa_id_group_set_badness(&(*iter)->channel_list, force);
3475  }
3476 }
channel_state_t state
Definition: channel.h:192
void(* close)(channel_t *)
Definition: channel.h:315
static void channel_listener_force_xfree(channel_listener_t *chan_l)
Definition: channel.c:999
int channel_is_incoming(channel_t *chan)
Definition: channel.c:2983
Header file for circuitstats.c.
int channel_is_client(const channel_t *chan)
Definition: channel.c:2913
void channel_timestamp_recv(channel_t *chan)
Definition: channel.c:3224
const char *(* describe_transport)(channel_listener_t *)
Definition: channel.h:498
HT_PROTOTYPE(HT_GENERATE2(channel_gid_map, HT_GENERATE2(channel_s, HT_GENERATE2(gidmap_node, HT_GENERATE2(channel_id_hash, HT_GENERATE2(channel_id_eq)
Definition: channel.c:121
static void channel_free_list(smartlist_t *channels, int mark_for_close)
Definition: channel.c:2168
const char * channel_listener_describe_transport(channel_listener_t *chan_l)
Definition: channel.c:2505
crypto_pk_t * get_server_identity_key(void)
Definition: router.c:378
circ_id_type_bitfield_t circ_id_type
Definition: channel.h:411
void geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type, dirreq_state_t new_state)
Definition: geoip_stats.c:550
void channel_clear_client(channel_t *chan)
Definition: channel.c:2939
const char * channel_get_canonical_remote_descr(channel_t *chan)
Definition: channel.c:2836
enum channel_s::@8 reason_for_closing
Header file for channeltls.c.
unsigned int channel_num_circuits(channel_t *chan)
Definition: channel.c:3335
int channel_is_outgoing(channel_t *chan)
Definition: channel.c:3060
void channel_mark_bad_for_new_circs(channel_t *chan)
Definition: channel.c:2898
int(* has_queued_writes)(channel_t *)
Definition: channel.h:352
Header file for circuitbuild.c.
void geoip_note_client_seen(geoip_client_action_t action, const tor_addr_t *addr, const char *transport_name, time_t now)
Definition: geoip_stats.c:227
uint64_t n_cells_recved
Definition: channel.h:462
static void channel_listener_free_list(smartlist_t *listeners, int mark_for_close)
Definition: channel.c:2199
void channel_change_state(channel_t *chan, channel_state_t to_state)
Definition: channel.c:1598
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
time_t channel_when_last_client(channel_t *chan)
Definition: channel.c:3277
Header file for geoip_stats.c.
channel_t * channel_get_for_extend(const char *rsa_id_digest, const ed25519_public_key_t *ed_id, const tor_addr_t *target_addr, const char **msg_out, int *launch_out)
Definition: channel.c:2383
void channel_timestamp_active(channel_t *chan)
Definition: channel.c:3157
static void channel_change_state_(channel_t *chan, channel_state_t to_state)
Definition: channel.c:1505
void channel_change_state_open(channel_t *chan)
Definition: channel.c:1608
void channel_close_for_error(channel_t *chan)
Definition: channel.c:1241
void circuit_n_chan_done(channel_t *chan, int status, int close_origin_circuits)
Definition: circuitbuild.c:629
channel_t * channel_next_with_rsa_identity(channel_t *chan)
Definition: channel.c:731
Definition: cell_st.h:12
#define LD_GENERAL
Definition: log.h:58
time_t timestamp_created
Definition: channel.h:295
time_t channel_when_last_xmit(channel_t *chan)
Definition: channel.c:3288
int crypto_pk_cmp_keys(const crypto_pk_t *a, const crypto_pk_t *b)
struct channel_handle_t * timer_handle
Definition: channel.h:237
Functions and types for monotonic times.
void channel_dumpstats(int severity)
Definition: channel.c:2057
Header file for nodelist.c.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:631
const char * channel_get_actual_remote_descr(channel_t *chan)
Definition: channel.c:2803
uint64_t n_accepted
Definition: channel.h:512
const char * channel_describe_transport(channel_t *chan)
Definition: channel.c:2490
void smartlist_add(smartlist_t *sl, void *element)
int channel_is_better(channel_t *a, channel_t *b)
Definition: channel.c:2323
void channel_listener_timestamp_active(channel_listener_t *chan_l)
Definition: channel.c:3174
void(* dumpstats)(channel_listener_t *, int)
Definition: channel.h:500
void channel_close_from_lower_layer(channel_t *chan)
Definition: channel.c:1213
int connection_or_digest_is_known_relay(const char *id_digest)
Header file for config.c.
void router_set_status(const char *digest, int up)
Definition: nodelist.c:2135
int(* write_packed_cell)(channel_t *, packed_cell_t *)
Definition: channel.h:371
int channel_is_canonical(channel_t *chan)
Definition: channel.c:2953
int channel_listener_state_can_transition(channel_listener_state_t from, channel_listener_state_t to)
Definition: channel.c:283
unsigned int is_local
Definition: channel.h:440
channel_t * channel_find_by_global_id(uint64_t global_identifier)
Definition: channel.c:650
void circuitmux_detach_all_circuits(circuitmux_t *cmux, smartlist_t *detached_out)
Definition: circuitmux.c:263
const char * channel_state_to_string(channel_state_t state)
Definition: channel.c:315
void channel_dump_transport_statistics(channel_t *chan, int severity)
Definition: channel.c:2771
unsigned int num_n_circuits
Definition: channel.h:416
void mainloop_schedule_postloop_cleanup(void)
Definition: mainloop.c:1738
int channel_write_packed_cell(channel_t *chan, packed_cell_t *cell)
Definition: channel.c:1470
#define tor_free(p)
Definition: malloc.h:52
void channel_free_(channel_t *chan)
Definition: channel.c:876
int channel_is_local(channel_t *chan)
Definition: channel.c:3014
channel_t * channel_tls_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id)
Definition: channeltls.c:189
void channel_mark_for_close(channel_t *chan)
Definition: channel.c:1134
static int channel_remote_identity_matches(const channel_t *chan, const char *rsa_id_digest, const ed25519_public_key_t *ed_id)
Definition: channel.c:667
crypto_pk_t * get_tlsclient_identity_key(void)
Definition: router.c:407
#define SMARTLIST_DEL_CURRENT(sl, var)
int(* matches_extend_info)(channel_t *, extend_info_t *)
Definition: channel.h:361
Header file for mainloop.c.
time_t timestamp_accepted
Definition: channel.h:509
void(* dumpstats)(channel_t *, int)
Definition: channel.h:319
circuitmux_t * cmux
Definition: channel.h:403
void circuit_build_times_network_is_live(circuit_build_times_t *cbt)
void channel_listener_timestamp_created(channel_listener_t *chan_l)
Definition: channel.c:3137
char body[CELL_MAX_NETWORK_SIZE]
Definition: cell_queue_st.h:16
void channel_update_bad_for_new_circs(const char *digest, int force)
Definition: channel.c:3457
time_t channel_when_created(channel_t *chan)
Definition: channel.c:3266
unsigned char registered
Definition: channel.h:200
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
Definition: malloc.c:146
Header file for scheduler*.c.
struct tor_timer_t * padding_timer
Definition: channel.h:235
unsigned int is_canonical_to_peer
Definition: channel.h:224
MOCK_IMPL(ssize_t, channel_flush_some_cells,(channel_t *chan, ssize_t num_cells))
Definition: channel.c:1715
channel_cell_handler_fn_ptr channel_get_cell_handler(channel_t *chan)
Definition: channel.c:1059
time_t timestamp_created
Definition: channel.h:488
unsigned int is_incoming
Definition: channel.h:433
struct ed25519_public_key_t ed25519_identity
Definition: channel.h:394
static uint32_t get_uint32(const void *cp)
Definition: bytes.h:49
int sched_heap_idx
Definition: channel.h:292
int channel_num_cells_writeable(channel_t *chan)
Definition: channel.c:3091
const char *(* get_remote_descr)(channel_t *, int)
Definition: channel.h:350
int channel_is_canonical_is_reliable(channel_t *chan)
Definition: channel.c:2968
static int write_packed_cell(channel_t *chan, packed_cell_t *cell)
Definition: channel.c:1417
void channel_process_cell(channel_t *chan, cell_t *cell)
Definition: channel.c:1962
void channel_note_destroy_pending(channel_t *chan, circid_t id)
Definition: circuitlist.c:406
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
void(* free_fn)(channel_listener_t *)
Definition: channel.h:494
void channel_mark_incoming(channel_t *chan)
Definition: channel.c:2997
Header file for channel.c.
const char *(* describe_transport)(channel_t *)
Definition: channel.h:317
channel_t * channel_find_by_remote_identity(const char *rsa_id_digest, const ed25519_public_key_t *ed_id)
Definition: channel.c:697
tor_assert(buffer)
void channel_timestamp_client(channel_t *chan)
Definition: channel.c:3208
static void channel_remove_from_digest_map(channel_t *chan)
Definition: channel.c:597
void connection_or_group_set_badness_(smartlist_t *group, int force)
unsigned int padding_enabled
Definition: channel.h:214
int channel_listener_state_is_valid(channel_listener_state_t state)
Definition: channel.c:210
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
int packed_cell_is_destroy(channel_t *chan, const packed_cell_t *packed_cell, circid_t *circid_out)
Definition: channel.c:1991
STATIC void channel_add_to_digest_map(channel_t *chan)
Definition: channel.c:560
void channel_listener_dump_statistics(channel_listener_t *chan_l, int severity)
Definition: channel.c:2705
void channel_listener_set_listener_fn(channel_listener_t *chan_l, channel_listener_fn_ptr listener)
Definition: channel.c:1036
enum channel_listener_s::@10 reason_for_closing
#define LD_CHANNEL
Definition: log.h:101
void channel_run_cleanup(void)
Definition: channel.c:2119
#define DIGEST_LEN
Definition: digest_sizes.h:20
void channel_listener_queue_incoming(channel_listener_t *listener, channel_t *incoming)
Definition: channel.c:1911
void channel_check_for_duplicates(void)
Definition: channel.c:748
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
void channel_listener_dump_transport_statistics(channel_listener_t *chan_l, int severity)
Definition: channel.c:2784
Master header file for Tor-specific functionality.
int channel_matches_target_addr_for_extend(channel_t *chan, const tor_addr_t *target)
Definition: channel.c:3318
channel_var_cell_handler_fn_ptr channel_get_var_cell_handler(channel_t *chan)
Definition: channel.c:1076
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
Header file for circuitbuild.c.
const char * channel_get_actual_remote_address(channel_t *chan)
Definition: channel.c:2819
void channel_listener_free_(channel_listener_t *chan_l)
Definition: channel.c:928
void circuitmux_set_policy(circuitmux_t *cmux, const circuitmux_policy_t *pol)
Definition: circuitmux.c:486
unsigned int is_bad_for_new_circs
Definition: channel.h:425
Header file for rephist.c.
void smartlist_remove(smartlist_t *sl, const void *element)
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:37
void channel_mark_remote(channel_t *chan)
Definition: channel.c:3045
void channel_listener_change_state(channel_listener_t *chan_l, channel_listener_state_t to_state)
Definition: channel.c:1625
void channel_set_cell_handlers(channel_t *chan, channel_cell_handler_fn_ptr cell_handler, channel_var_cell_handler_fn_ptr var_cell_handler)
Definition: channel.c:1093
static void channel_force_xfree(channel_t *chan)
Definition: channel.c:955
void channel_listener_timestamp_accepted(channel_listener_t *chan_l)
Definition: channel.c:3191
time_t timestamp_client
Definition: channel.h:447
void tor_free_(void *mem)
Definition: malloc.c:227
time_t timestamp_last_had_circuits
Definition: channel.h:454
Header file for circuitlist.c.
Header file for rendservice.c.
void channel_listener_mark_for_close(channel_listener_t *chan_l)
Definition: channel.c:1173
#define LD_OR
Definition: log.h:88
int channel_state_can_transition(channel_state_t from, channel_state_t to)
Definition: channel.c:237
void channel_clear_identity_digest(channel_t *chan)
Definition: channel.c:1299
int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
Definition: channel.c:3302
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
unsigned int has_been_open
Definition: channel.h:203
int channel_is_bad_for_new_circs(channel_t *chan)
Definition: channel.c:2885
void channel_listener_process_incoming(channel_listener_t *listener)
Definition: channel.c:1793
void channel_clear_remote_end(channel_t *chan)
Definition: channel.c:1387
void channel_do_open_actions(channel_t *chan)
Definition: channel.c:1845
void channel_init_listener(channel_listener_t *chan_l)
Definition: channel.c:860
void channel_listener_run_cleanup(void)
Definition: channel.c:2145
void channel_notify_flushed(channel_t *chan)
Definition: channel.c:1776
void channel_register(channel_t *chan)
Definition: channel.c:386
void channel_mark_local(channel_t *chan)
Definition: channel.c:3029
void channel_free_all(void)
Definition: channel.c:2232
const char * channel_listener_state_to_string(channel_listener_state_t state)
Definition: channel.c:350
monotime_coarse_t next_padding_time
Definition: channel.h:232
int(* is_canonical)(channel_t *, int)
Definition: channel.h:359
void channel_init(channel_t *chan)
Definition: channel.c:823
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1122
void channel_mark_outgoing(channel_t *chan)
Definition: channel.c:3074
void channel_listener_dumpstats(int severity)
Definition: channel.c:2088
Header for timers.c.
Header file for relay.c.
Header file for circuitmux.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
void circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux, channel_t *chan)
Definition: circuitmux.c:382
enum channel_s::@9 scheduler_state
Header file for router.c.
static HT_HEAD(channel_gid_map, channel_s)
Definition: channel.c:109
uint64_t dirreq_id
Definition: channel.h:459
circuit_build_times_t * get_circuit_build_times_mutable(void)
Definition: circuitstats.c:88
smartlist_t * incoming_list
Definition: channel.h:506
void channel_listener_unregister(channel_listener_t *chan_l)
Definition: channel.c:524
uint32_t circid_t
Definition: or.h:608
ratelim_t last_warned_circ_ids_exhausted
Definition: channel.h:444
monotime_coarse_t timestamp_xfer
Definition: channel.h:308
int(* matches_target)(channel_t *, const tor_addr_t *)
Definition: channel.h:363
unsigned int circuitmux_num_circuits(circuitmux_t *cmux)
Definition: circuitmux.c:772
#define fast_memcmp(a, b, c)
Definition: di_ops.h:26
channel_t * channel_connect(const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id)
Definition: channel.c:2305
unsigned char registered
Definition: channel.h:476
void(* free_fn)(channel_t *)
Definition: channel.h:313
unsigned int is_client
Definition: channel.h:430
Header file for connection_or.c.
void channel_unregister(channel_t *chan)
Definition: channel.c:444
void circuit_unlink_all_from_channel(channel_t *chan, int reason)
Definition: circuitlist.c:1593
void channel_timestamp_created(channel_t *chan)
Definition: channel.c:3121
void channel_timestamp_xmit(channel_t *chan)
Definition: channel.c:3244
unsigned int circuitmux_num_active_circuits(circuitmux_t *cmux)
Definition: circuitmux.c:760
char identity_digest[DIGEST_LEN]
Definition: channel.h:384
void channel_set_identity_digest(channel_t *chan, const char *identity_digest, const ed25519_public_key_t *ed_identity)
Definition: channel.c:1328
static void channel_rsa_id_group_set_badness(struct channel_list_s *lst, int force)
Definition: channel.c:3398
channel_listener_fn_ptr listener
Definition: channel.h:503
void smartlist_clear(smartlist_t *sl)
int connection_or_single_set_badness_(time_t now, or_connection_t *or_conn, int force)
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
void channel_closed(channel_t *chan)
Definition: channel.c:1268
void(* close)(channel_listener_t *)
Definition: channel.h:496
int channel_has_queued_writes(channel_t *chan)
Definition: channel.c:2869
void channel_mark_client(channel_t *chan)
Definition: channel.c:2926
int channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
Definition: channel.c:2017
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:82
channel_cell_handler_fn_ptr cell_handler
Definition: channel.h:322
Header file for routerlist.c.
uint64_t global_identifier
Definition: channel.h:197
void channel_listener_register(channel_listener_t *chan_l)
Definition: channel.c:483