channel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* * Copyright (c) 2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file channel.h
  5. * \brief Header file for channel.c
  6. **/
  7. #ifndef _TOR_CHANNEL_H
  8. #define _TOR_CHANNEL_H
  9. #include "or.h"
  10. /*
  11. * Channel struct; see thw channel_t typedef in or.h. A channel is an
  12. * abstract interface for the OR-to-OR connection, similar to connection_or_t,
  13. * but without the strong coupling to the underlying TLS implementation. They
  14. * are constructed by calling a protocol-specific function to open a channel
  15. * to a particular node, and once constructed support the abstract operations
  16. * defined below.
  17. */
  18. struct channel_s {
  19. /* Current channel state */
  20. channel_state_t state;
  21. /* Globally unique ID number for a channel over the lifetime of a Tor
  22. * process.
  23. */
  24. uint64_t global_identifier;
  25. /* Should we expect to see this channel in the channel lists? */
  26. unsigned char registered:1;
  27. /** Set this if this channel is created in CHANNEL_STATE_LISTEN, so
  28. * lower-layer close methods that see the channel in CHANNEL_STATE_CLOSING
  29. * know.
  30. */
  31. unsigned int is_listener:1;
  32. /** Unique ID for measuring direct network status requests;vtunneled ones
  33. * come over a circuit_t, which has a dirreq_id field as well, but is a
  34. * distinct namespace. */
  35. uint64_t dirreq_id;
  36. /** Why did we close?
  37. */
  38. enum {
  39. CHANNEL_NOT_CLOSING = 0,
  40. CHANNEL_CLOSE_REQUESTED,
  41. CHANNEL_CLOSE_FROM_BELOW,
  42. CHANNEL_CLOSE_FOR_ERROR
  43. } reason_for_closing;
  44. /* Timestamps for both cell channels and listeners */
  45. time_t timestamp_created; /* Channel created */
  46. time_t timestamp_active; /* Any activity */
  47. /* Methods implemented by the lower layer */
  48. /* Free a channel */
  49. void (*free)(channel_t *);
  50. /* Close an open channel */
  51. void (*close)(channel_t *);
  52. union {
  53. struct {
  54. /* Registered listen handler to call on incoming connection */
  55. void (*listener)(channel_t *, channel_t *);
  56. /* List of pending incoming connections */
  57. smartlist_t *incoming_list;
  58. } listener;
  59. struct {
  60. /* Registered handlers for incoming cells */
  61. void (*cell_handler)(channel_t *, cell_t *);
  62. void (*var_cell_handler)(channel_t *, var_cell_t *);
  63. /* Methods implemented by the lower layer */
  64. /*
  65. * Ask the underlying transport what the remote endpoint address is, in
  66. * a tor_addr_t. This is optional and subclasses may leave this NULL.
  67. * If they implement it, they should write the address out to the
  68. * provided tor_addr_t *, and return 1 if successful or 0 if no address
  69. * available.
  70. */
  71. int (*get_remote_addr)(channel_t *, tor_addr_t *);
  72. /*
  73. * Get a text description of the remote endpoint; canonicalized if the
  74. * arg is 0, or the one we originally connected to/received from if it's
  75. * 1.
  76. */
  77. const char * (*get_remote_descr)(channel_t *, int);
  78. /* Check if the lower layer has queued writes */
  79. int (*has_queued_writes)(channel_t *);
  80. /*
  81. * If the second param is zero, ask the lower layer if this is
  82. * 'canonical', for a transport-specific definition of canonical; if
  83. * it is 1, ask if the answer to the preceding query is safe to rely
  84. * on.
  85. */
  86. int (*is_canonical)(channel_t *, int);
  87. /* Check if this channel matches a specified extend_info_t */
  88. int (*matches_extend_info)(channel_t *, extend_info_t *);
  89. /* Check if this channel matches a target address when extending */
  90. int (*matches_target)(channel_t *, const tor_addr_t *);
  91. /* Write a cell to an open channel */
  92. int (*write_cell)(channel_t *, cell_t *);
  93. /* Write a packed cell to an open channel */
  94. int (*write_packed_cell)(channel_t *, packed_cell_t *);
  95. /* Write a variable-length cell to an open channel */
  96. int (*write_var_cell)(channel_t *, var_cell_t *);
  97. /*
  98. * Hash of the public RSA key for the other side's identity key, or
  99. * zeroes if the other side hasn't shown us a valid identity key.
  100. */
  101. char identity_digest[DIGEST_LEN];
  102. /* Nickname of the OR on the other side, or NULL if none. */
  103. char *nickname;
  104. /*
  105. * Linked list of channels with the same identity digest, for the
  106. * digest->channel map
  107. */
  108. channel_t *next_with_same_id, *prev_with_same_id;
  109. /* List of incoming cells to handle */
  110. smartlist_t *cell_queue;
  111. /* List of queued outgoing cells */
  112. smartlist_t *outgoing_queue;
  113. /*
  114. * When we last used this conn for any client traffic. If not
  115. * recent, we can rate limit it further.
  116. */
  117. time_t client_used;
  118. /* Circuit stuff for use by relay.c */
  119. /*
  120. * Double-linked ring of circuits with queued cells waiting for room to
  121. * free up on this connection's outbuf. Every time we pull cells from
  122. * a circuit, we advance this pointer to the next circuit in the ring.
  123. */
  124. struct circuit_t *active_circuits;
  125. /*
  126. * Priority queue of cell_ewma_t for circuits with queued cells waiting
  127. * for room to free up on this connection's outbuf. Kept in heap order
  128. * according to EWMA.
  129. *
  130. * This is redundant with active_circuits; if we ever decide only to use
  131. * the cell_ewma algorithm for choosing circuits, we can remove
  132. * active_circuits.
  133. */
  134. smartlist_t *active_circuit_pqueue;
  135. /*
  136. * The tick on which the cell_ewma_ts in active_circuit_pqueue last had
  137. * their ewma values rescaled.
  138. */
  139. unsigned active_circuit_pqueue_last_recalibrated;
  140. /* Circuit ID generation stuff for use by circuitbuild.c */
  141. /*
  142. * When we send CREATE cells along this connection, which half of the
  143. * space should we use?
  144. */
  145. circ_id_type_t circ_id_type:2;
  146. /*
  147. * Which circ_id do we try to use next on this connection? This is
  148. * always in the range 0..1<<15-1.
  149. */
  150. circid_t next_circ_id;
  151. /* How many circuits use this connection as p_chan or n_chan? */
  152. int n_circuits;
  153. /*
  154. * True iff this channel shouldn't get any new circs attached to it,
  155. * because the connection is too old, or because there's a better one.
  156. * More generally, this flag is used to note an unhealthy connection;
  157. * for example, if a bad connection fails we shouldn't assume that the
  158. * router itself has a problem.
  159. */
  160. unsigned int is_bad_for_new_circs:1;
  161. /** True iff we have decided that the other end of this connection
  162. * is a client. Channels with this flag set should never be used
  163. * to satisfy an EXTEND request. */
  164. unsigned int is_client:1;
  165. /** Set if the channel was initiated remotely (came from a listener) */
  166. unsigned int is_incoming:1;
  167. /** Set by lower layer if this is local; i.e., everything it communicates
  168. * with for this channel returns true for is_local_addr(). This is used
  169. * to decide whether to declare reachability when we receive something on
  170. * this channel in circuitbuild.c
  171. */
  172. unsigned int is_local:1;
  173. /** Channel timestamps for cell channels */
  174. time_t timestamp_client; /* Client used this, according to relay.c */
  175. time_t timestamp_drained; /* Output queue empty */
  176. time_t timestamp_recv; /* Cell received from lower layer */
  177. time_t timestamp_xmit; /* Cell sent to lower layer */
  178. /* Timestamp for relay.c */
  179. time_t timestamp_last_added_nonpadding;
  180. /** Unique ID for measuring direct network status requests;vtunneled ones
  181. * come over a circuit_t, which has a dirreq_id field as well, but is a
  182. * distinct namespace. */
  183. uint64_t dirreq_id;
  184. } cell_chan;
  185. } u;
  186. };
  187. /* Channel state manipulations */
  188. int channel_state_is_valid(channel_state_t state);
  189. int channel_state_can_transition(channel_state_t from, channel_state_t to);
  190. const char * channel_state_to_string(channel_state_t state);
  191. /* Abstract channel operations */
  192. void channel_request_close(channel_t *chan);
  193. void channel_write_cell(channel_t *chan, cell_t *cell);
  194. void channel_write_packed_cell(channel_t *chan, packed_cell_t *cell);
  195. void channel_write_var_cell(channel_t *chan, var_cell_t *cell);
  196. /* Channel callback registrations */
  197. /* Listener callback */
  198. void (* channel_get_listener(channel_t *chan))(channel_t *, channel_t *);
  199. void channel_set_listener(channel_t *chan,
  200. void (*listener)(channel_t *, channel_t *) );
  201. /* Incoming cell callbacks */
  202. void (* channel_get_cell_handler(channel_t *chan))
  203. (channel_t *, cell_t *);
  204. void (* channel_get_var_cell_handler(channel_t *chan))
  205. (channel_t *, var_cell_t *);
  206. void channel_set_cell_handler(channel_t *chan,
  207. void (*cell_handler)(channel_t *, cell_t *));
  208. void channel_set_cell_handlers(channel_t *chan,
  209. void (*cell_handler)(channel_t *, cell_t *),
  210. void (*var_cell_handler)(channel_t *,
  211. var_cell_t *));
  212. void channel_set_var_cell_handler(channel_t *chan,
  213. void (*var_cell_handler)(channel_t *,
  214. var_cell_t *));
  215. /* Clean up closed channels periodically; called from run_scheduled_events()
  216. * in main.c
  217. */
  218. void channel_run_cleanup(void);
  219. /* Close all channels and deallocate everything */
  220. void channel_free_all(void);
  221. #ifdef _TOR_CHANNEL_INTERNAL
  222. /* Channel operations for subclasses and internal use only */
  223. /* Initialize a newly allocated channel - do this first in subclass
  224. * constructors.
  225. */
  226. void channel_init_for_cells(channel_t *chan);
  227. void channel_init_listener(channel_t *chan);
  228. /* Channel registration/unregistration */
  229. void channel_register(channel_t *chan);
  230. void channel_unregister(channel_t *chan);
  231. /* Close from below */
  232. void channel_close_from_lower_layer(channel_t *chan);
  233. void channel_close_for_error(channel_t *chan);
  234. void channel_closed(channel_t *chan);
  235. /* Free a channel */
  236. void channel_free(channel_t *chan);
  237. void channel_force_free(channel_t *chan);
  238. /* State/metadata setters */
  239. void channel_change_state(channel_t *chan, channel_state_t to_state);
  240. void channel_clear_identity_digest(channel_t *chan);
  241. void channel_clear_remote_end(channel_t *chan);
  242. void channel_mark_local(channel_t *chan);
  243. void channel_mark_incoming(channel_t *chan);
  244. void channel_mark_outgoing(channel_t *chan);
  245. void channel_set_identity_digest(channel_t *chan,
  246. const char *identity_digest);
  247. void channel_set_remote_end(channel_t *chan,
  248. const char *identity_digest,
  249. const char *nickname);
  250. /* Timestamp updates */
  251. void channel_timestamp_created(channel_t *chan);
  252. void channel_timestamp_active(channel_t *chan);
  253. void channel_timestamp_drained(channel_t *chan);
  254. void channel_timestamp_recv(channel_t *chan);
  255. void channel_timestamp_xmit(channel_t *chan);
  256. /* Incoming channel handling */
  257. void channel_process_incoming(channel_t *listener);
  258. void channel_queue_incoming(channel_t *listener, channel_t *incoming);
  259. /* Incoming cell handling */
  260. void channel_process_cells(channel_t *chan);
  261. void channel_queue_cell(channel_t *chan, cell_t *cell);
  262. void channel_queue_var_cell(channel_t *chan, var_cell_t *var_cell);
  263. /* Outgoing cell handling */
  264. void channel_flush_cells(channel_t *chan);
  265. /* Request from lower layer for more cells if available */
  266. ssize_t channel_flush_some_cells(channel_t *chan, ssize_t num_cells);
  267. /* Query if data available on this channel */
  268. int channel_more_to_flush(channel_t *chan);
  269. /* Notify flushed outgoing for dirreq handling */
  270. void channel_notify_flushed(channel_t *chan);
  271. /* Handle stuff we need to do on open like notifying circuits */
  272. void channel_do_open_actions(channel_t *chan);
  273. #endif
  274. /* Helper functions to perform operations on channels */
  275. int channel_send_destroy(circid_t circ_id, channel_t *chan,
  276. int reason);
  277. /*
  278. * Outside abstract interfaces that should eventually get turned into
  279. * something transport/address format independent.
  280. */
  281. channel_t * channel_connect(const tor_addr_t *addr, uint16_t port,
  282. const char *id_digest);
  283. channel_t * channel_get_for_extend(const char *digest,
  284. const tor_addr_t *target_addr,
  285. const char **msg_out,
  286. int *launch_out);
  287. /* Ask which of two channels is better for circuit-extension purposes */
  288. int channel_is_better(time_t now,
  289. channel_t *a, channel_t *b,
  290. int forgive_new_connections);
  291. /** Channel lookups
  292. */
  293. channel_t * channel_find_by_global_id(uint64_t global_identifier);
  294. channel_t * channel_find_by_remote_digest(const char *identity_digest);
  295. channel_t * channel_find_by_remote_nickname(const char *nickname);
  296. /** For things returned by channel_find_by_remote_digest(), walk the list.
  297. */
  298. channel_t * channel_next_with_digest(channel_t *chan);
  299. channel_t * channel_prev_with_digest(channel_t *chan);
  300. /*
  301. * Metadata queries/updates
  302. */
  303. const char * channel_get_actual_remote_descr(channel_t *chan);
  304. int channel_get_addr_if_possible(channel_t *chan, tor_addr_t *addr_out);
  305. const char * channel_get_canonical_remote_descr(channel_t *chan);
  306. int channel_has_queued_writes(channel_t *chan);
  307. int channel_is_bad_for_new_circs(channel_t *chan);
  308. void channel_mark_bad_for_new_circs(channel_t *chan);
  309. int channel_is_canonical(channel_t *chan);
  310. int channel_is_canonical_is_reliable(channel_t *chan);
  311. int channel_is_client(channel_t *chan);
  312. int channel_is_local(channel_t *chan);
  313. int channel_is_incoming(channel_t *chan);
  314. int channel_is_outgoing(channel_t *chan);
  315. void channel_mark_client(channel_t *chan);
  316. int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info);
  317. int channel_matches_target_addr_for_extend(channel_t *chan,
  318. const tor_addr_t *target);
  319. void channel_set_circid_type(channel_t *chan, crypto_pk_t *identity_rcvd);
  320. void channel_timestamp_client(channel_t *chan);
  321. /* Timestamp queries */
  322. time_t channel_when_created(channel_t *chan);
  323. time_t channel_when_last_active(channel_t *chan);
  324. time_t channel_when_last_client(channel_t *chan);
  325. time_t channel_when_last_drained(channel_t *chan);
  326. time_t channel_when_last_recv(channel_t *chan);
  327. time_t channel_when_last_xmit(channel_t *chan);
  328. #endif