channel.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #include "circuitmux.h"
  11. /* Channel handler function pointer typedefs */
  12. typedef void (*channel_listener_fn_ptr)(channel_listener_t *, channel_t *);
  13. typedef void (*channel_cell_handler_fn_ptr)(channel_t *, cell_t *);
  14. typedef void (*channel_var_cell_handler_fn_ptr)(channel_t *, var_cell_t *);
  15. /*
  16. * Channel struct; see the channel_t typedef in or.h. A channel is an
  17. * abstract interface for the OR-to-OR connection, similar to connection_or_t,
  18. * but without the strong coupling to the underlying TLS implementation. They
  19. * are constructed by calling a protocol-specific function to open a channel
  20. * to a particular node, and once constructed support the abstract operations
  21. * defined below.
  22. */
  23. struct channel_s {
  24. /* Magic number for type-checking cast macros */
  25. uint32_t magic;
  26. /* Current channel state */
  27. channel_state_t state;
  28. /* Globally unique ID number for a channel over the lifetime of a Tor
  29. * process.
  30. */
  31. uint64_t global_identifier;
  32. /* Should we expect to see this channel in the channel lists? */
  33. unsigned char registered:1;
  34. /** Why did we close?
  35. */
  36. enum {
  37. CHANNEL_NOT_CLOSING = 0,
  38. CHANNEL_CLOSE_REQUESTED,
  39. CHANNEL_CLOSE_FROM_BELOW,
  40. CHANNEL_CLOSE_FOR_ERROR
  41. } reason_for_closing;
  42. /* Timestamps for both cell channels and listeners */
  43. time_t timestamp_created; /* Channel created */
  44. time_t timestamp_active; /* Any activity */
  45. /* Methods implemented by the lower layer */
  46. /* Free a channel */
  47. void (*free)(channel_t *);
  48. /* Close an open channel */
  49. void (*close)(channel_t *);
  50. /* Describe the transport subclass for this channel */
  51. const char * (*describe_transport)(channel_t *);
  52. /* Optional method to dump transport-specific statistics on the channel */
  53. void (*dumpstats)(channel_t *, int);
  54. /* Registered handlers for incoming cells */
  55. channel_cell_handler_fn_ptr cell_handler;
  56. channel_var_cell_handler_fn_ptr var_cell_handler;
  57. /* Methods implemented by the lower layer */
  58. /*
  59. * Ask the underlying transport what the remote endpoint address is, in
  60. * a tor_addr_t. This is optional and subclasses may leave this NULL.
  61. * If they implement it, they should write the address out to the
  62. * provided tor_addr_t *, and return 1 if successful or 0 if no address
  63. * available.
  64. */
  65. int (*get_remote_addr)(channel_t *, tor_addr_t *);
  66. /*
  67. * Get a text description of the remote endpoint; canonicalized if the
  68. * arg is 0, or the one we originally connected to/received from if it's
  69. * 1.
  70. */
  71. const char * (*get_remote_descr)(channel_t *, int);
  72. /* Check if the lower layer has queued writes */
  73. int (*has_queued_writes)(channel_t *);
  74. /*
  75. * If the second param is zero, ask the lower layer if this is
  76. * 'canonical', for a transport-specific definition of canonical; if
  77. * it is 1, ask if the answer to the preceding query is safe to rely
  78. * on.
  79. */
  80. int (*is_canonical)(channel_t *, int);
  81. /* Check if this channel matches a specified extend_info_t */
  82. int (*matches_extend_info)(channel_t *, extend_info_t *);
  83. /* Check if this channel matches a target address when extending */
  84. int (*matches_target)(channel_t *, const tor_addr_t *);
  85. /* Write a cell to an open channel */
  86. int (*write_cell)(channel_t *, cell_t *);
  87. /* Write a packed cell to an open channel */
  88. int (*write_packed_cell)(channel_t *, packed_cell_t *);
  89. /* Write a variable-length cell to an open channel */
  90. int (*write_var_cell)(channel_t *, var_cell_t *);
  91. /*
  92. * Hash of the public RSA key for the other side's identity key, or
  93. * zeroes if the other side hasn't shown us a valid identity key.
  94. */
  95. char identity_digest[DIGEST_LEN];
  96. /* Nickname of the OR on the other side, or NULL if none. */
  97. char *nickname;
  98. /*
  99. * Linked list of channels with the same identity digest, for the
  100. * digest->channel map
  101. */
  102. channel_t *next_with_same_id, *prev_with_same_id;
  103. /* List of incoming cells to handle */
  104. smartlist_t *incoming_queue;
  105. /* List of queued outgoing cells */
  106. smartlist_t *outgoing_queue;
  107. /* Circuit mux for circuits sending on this channel */
  108. circuitmux_t *cmux;
  109. /* Circuit ID generation stuff for use by circuitbuild.c */
  110. /*
  111. * When we send CREATE cells along this connection, which half of the
  112. * space should we use?
  113. */
  114. circ_id_type_t circ_id_type:2;
  115. /*
  116. * Which circ_id do we try to use next on this connection? This is
  117. * always in the range 0..1<<15-1.
  118. */
  119. circid_t next_circ_id;
  120. /* For how many circuits are we n_chan? What about p_chan? */
  121. unsigned int num_n_circuits, num_p_circuits;
  122. /*
  123. * True iff this channel shouldn't get any new circs attached to it,
  124. * because the connection is too old, or because there's a better one.
  125. * More generally, this flag is used to note an unhealthy connection;
  126. * for example, if a bad connection fails we shouldn't assume that the
  127. * router itself has a problem.
  128. */
  129. unsigned int is_bad_for_new_circs:1;
  130. /** True iff we have decided that the other end of this connection
  131. * is a client. Channels with this flag set should never be used
  132. * to satisfy an EXTEND request. */
  133. unsigned int is_client:1;
  134. /** Set if the channel was initiated remotely (came from a listener) */
  135. unsigned int is_incoming:1;
  136. /** Set by lower layer if this is local; i.e., everything it communicates
  137. * with for this channel returns true for is_local_addr(). This is used
  138. * to decide whether to declare reachability when we receive something on
  139. * this channel in circuitbuild.c
  140. */
  141. unsigned int is_local:1;
  142. /** Channel timestamps for cell channels */
  143. time_t timestamp_client; /* Client used this, according to relay.c */
  144. time_t timestamp_drained; /* Output queue empty */
  145. time_t timestamp_recv; /* Cell received from lower layer */
  146. time_t timestamp_xmit; /* Cell sent to lower layer */
  147. /* Timestamp for relay.c */
  148. time_t timestamp_last_added_nonpadding;
  149. /** Unique ID for measuring direct network status requests;vtunneled ones
  150. * come over a circuit_t, which has a dirreq_id field as well, but is a
  151. * distinct namespace. */
  152. uint64_t dirreq_id;
  153. /** Channel counters for cell channels */
  154. uint64_t n_cells_recved;
  155. uint64_t n_cells_xmitted;
  156. };
  157. struct channel_listener_s {
  158. /* Current channel listener state */
  159. channel_listener_state_t state;
  160. /* Globally unique ID number for a channel over the lifetime of a Tor
  161. * process.
  162. */
  163. uint64_t global_identifier;
  164. /* Should we expect to see this channel in the channel lists? */
  165. unsigned char registered:1;
  166. /** Why did we close?
  167. */
  168. enum {
  169. CHANNEL_LISTENER_NOT_CLOSING = 0,
  170. CHANNEL_LISTENER_CLOSE_REQUESTED,
  171. CHANNEL_LISTENER_CLOSE_FROM_BELOW,
  172. CHANNEL_LISTENER_CLOSE_FOR_ERROR
  173. } reason_for_closing;
  174. /* Timestamps for both cell channels and listeners */
  175. time_t timestamp_created; /* Channel created */
  176. time_t timestamp_active; /* Any activity */
  177. /* Methods implemented by the lower layer */
  178. /* Free a channel */
  179. void (*free)(channel_listener_t *);
  180. /* Close an open channel */
  181. void (*close)(channel_listener_t *);
  182. /* Describe the transport subclass for this channel */
  183. const char * (*describe_transport)(channel_listener_t *);
  184. /* Optional method to dump transport-specific statistics on the channel */
  185. void (*dumpstats)(channel_listener_t *, int);
  186. /* Registered listen handler to call on incoming connection */
  187. channel_listener_fn_ptr listener;
  188. /* List of pending incoming connections */
  189. smartlist_t *incoming_list;
  190. /* Timestamps for listeners */
  191. time_t timestamp_accepted;
  192. /* Counters for listeners */
  193. uint64_t n_accepted;
  194. };
  195. /* Channel state manipulations */
  196. int channel_state_is_valid(channel_state_t state);
  197. int channel_listener_state_is_valid(channel_listener_state_t state);
  198. int channel_state_can_transition(channel_state_t from, channel_state_t to);
  199. int channel_listener_state_can_transition(channel_listener_state_t from,
  200. channel_listener_state_t to);
  201. const char * channel_state_to_string(channel_state_t state);
  202. const char *
  203. channel_listener_state_to_string(channel_listener_state_t state);
  204. /* Abstract channel operations */
  205. void channel_mark_for_close(channel_t *chan);
  206. void channel_write_cell(channel_t *chan, cell_t *cell);
  207. void channel_write_packed_cell(channel_t *chan, packed_cell_t *cell);
  208. void channel_write_var_cell(channel_t *chan, var_cell_t *cell);
  209. void channel_listener_mark_for_close(channel_listener_t *chan_l);
  210. /* Channel callback registrations */
  211. /* Listener callback */
  212. channel_listener_fn_ptr
  213. channel_listener_get_listener_fn(channel_listener_t *chan);
  214. void channel_listener_set_listener_fn(channel_listener_t *chan,
  215. channel_listener_fn_ptr listener);
  216. /* Incoming cell callbacks */
  217. channel_cell_handler_fn_ptr channel_get_cell_handler(channel_t *chan);
  218. channel_var_cell_handler_fn_ptr
  219. channel_get_var_cell_handler(channel_t *chan);
  220. void channel_set_cell_handlers(channel_t *chan,
  221. channel_cell_handler_fn_ptr cell_handler,
  222. channel_var_cell_handler_fn_ptr
  223. var_cell_handler);
  224. /* Clean up closed channels and channel listeners periodically; these are
  225. * called from run_scheduled_events() in main.c.
  226. */
  227. void channel_run_cleanup(void);
  228. void channel_listener_run_cleanup(void);
  229. /* Close all channels and deallocate everything */
  230. void channel_free_all(void);
  231. /* Dump some statistics in the log */
  232. void channel_dumpstats(int severity);
  233. void channel_listener_dumpstats(int severity);
  234. /* Set the cmux policy on all active channels */
  235. void channel_set_cmux_policy_everywhere(circuitmux_policy_t *pol);
  236. #ifdef TOR_CHANNEL_INTERNAL_
  237. /* Channel operations for subclasses and internal use only */
  238. /* Initialize a newly allocated channel - do this first in subclass
  239. * constructors.
  240. */
  241. void channel_init(channel_t *chan);
  242. void channel_init_listener(channel_listener_t *chan);
  243. /* Channel registration/unregistration */
  244. void channel_register(channel_t *chan);
  245. void channel_unregister(channel_t *chan);
  246. /* Channel listener registration/unregistration */
  247. void channel_listener_register(channel_listener_t *chan_l);
  248. void channel_listener_unregister(channel_listener_t *chan_l);
  249. /* Close from below */
  250. void channel_close_from_lower_layer(channel_t *chan);
  251. void channel_close_for_error(channel_t *chan);
  252. void channel_closed(channel_t *chan);
  253. void channel_listener_close_from_lower_layer(channel_listener_t *chan_l);
  254. void channel_listener_close_for_error(channel_listener_t *chan_l);
  255. void channel_listener_closed(channel_listener_t *chan_l);
  256. /* Free a channel */
  257. void channel_free(channel_t *chan);
  258. void channel_listener_free(channel_listener_t *chan_l);
  259. /* State/metadata setters */
  260. void channel_change_state(channel_t *chan, channel_state_t to_state);
  261. void channel_clear_identity_digest(channel_t *chan);
  262. void channel_clear_remote_end(channel_t *chan);
  263. void channel_mark_local(channel_t *chan);
  264. void channel_mark_incoming(channel_t *chan);
  265. void channel_mark_outgoing(channel_t *chan);
  266. void channel_set_identity_digest(channel_t *chan,
  267. const char *identity_digest);
  268. void channel_set_remote_end(channel_t *chan,
  269. const char *identity_digest,
  270. const char *nickname);
  271. void channel_listener_change_state(channel_listener_t *chan_l,
  272. channel_listener_state_t to_state);
  273. /* Timestamp updates */
  274. void channel_timestamp_created(channel_t *chan);
  275. void channel_timestamp_active(channel_t *chan);
  276. void channel_timestamp_drained(channel_t *chan);
  277. void channel_timestamp_recv(channel_t *chan);
  278. void channel_timestamp_xmit(channel_t *chan);
  279. void channel_listener_timestamp_created(channel_listener_t *chan_l);
  280. void channel_listener_timestamp_active(channel_listener_t *chan_l);
  281. void channel_listener_timestamp_accepted(channel_listener_t *chan_l);
  282. /* Incoming channel handling */
  283. void channel_listener_process_incoming(channel_listener_t *listener);
  284. void channel_listener_queue_incoming(channel_listener_t *listener,
  285. channel_t *incoming);
  286. /* Incoming cell handling */
  287. void channel_process_cells(channel_t *chan);
  288. void channel_queue_cell(channel_t *chan, cell_t *cell);
  289. void channel_queue_var_cell(channel_t *chan, var_cell_t *var_cell);
  290. /* Outgoing cell handling */
  291. void channel_flush_cells(channel_t *chan);
  292. /* Request from lower layer for more cells if available */
  293. ssize_t channel_flush_some_cells(channel_t *chan, ssize_t num_cells);
  294. /* Query if data available on this channel */
  295. int channel_more_to_flush(channel_t *chan);
  296. /* Notify flushed outgoing for dirreq handling */
  297. void channel_notify_flushed(channel_t *chan);
  298. /* Handle stuff we need to do on open like notifying circuits */
  299. void channel_do_open_actions(channel_t *chan);
  300. #endif
  301. /* Helper functions to perform operations on channels */
  302. int channel_send_destroy(circid_t circ_id, channel_t *chan,
  303. int reason);
  304. /*
  305. * Outside abstract interfaces that should eventually get turned into
  306. * something transport/address format independent.
  307. */
  308. channel_t * channel_connect(const tor_addr_t *addr, uint16_t port,
  309. const char *id_digest);
  310. channel_t * channel_get_for_extend(const char *digest,
  311. const tor_addr_t *target_addr,
  312. const char **msg_out,
  313. int *launch_out);
  314. /* Ask which of two channels is better for circuit-extension purposes */
  315. int channel_is_better(time_t now,
  316. channel_t *a, channel_t *b,
  317. int forgive_new_connections);
  318. /** Channel lookups
  319. */
  320. channel_t * channel_find_by_global_id(uint64_t global_identifier);
  321. channel_t * channel_find_by_remote_digest(const char *identity_digest);
  322. /** For things returned by channel_find_by_remote_digest(), walk the list.
  323. */
  324. channel_t * channel_next_with_digest(channel_t *chan);
  325. channel_t * channel_prev_with_digest(channel_t *chan);
  326. /*
  327. * Metadata queries/updates
  328. */
  329. const char * channel_describe_transport(channel_t *chan);
  330. void channel_dump_statistics(channel_t *chan, int severity);
  331. void channel_dump_transport_statistics(channel_t *chan, int severity);
  332. const char * channel_get_actual_remote_descr(channel_t *chan);
  333. int channel_get_addr_if_possible(channel_t *chan, tor_addr_t *addr_out);
  334. const char * channel_get_canonical_remote_descr(channel_t *chan);
  335. int channel_has_queued_writes(channel_t *chan);
  336. int channel_is_bad_for_new_circs(channel_t *chan);
  337. void channel_mark_bad_for_new_circs(channel_t *chan);
  338. int channel_is_canonical(channel_t *chan);
  339. int channel_is_canonical_is_reliable(channel_t *chan);
  340. int channel_is_client(channel_t *chan);
  341. int channel_is_local(channel_t *chan);
  342. int channel_is_incoming(channel_t *chan);
  343. int channel_is_outgoing(channel_t *chan);
  344. void channel_mark_client(channel_t *chan);
  345. int channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info);
  346. int channel_matches_target_addr_for_extend(channel_t *chan,
  347. const tor_addr_t *target);
  348. unsigned int channel_num_circuits(channel_t *chan);
  349. void channel_set_circid_type(channel_t *chan, crypto_pk_t *identity_rcvd);
  350. void channel_timestamp_client(channel_t *chan);
  351. const char * channel_listener_describe_transport(channel_listener_t *chan_l);
  352. void channel_listener_dump_statistics(channel_listener_t *chan_l,
  353. int severity);
  354. void channel_listener_dump_transport_statistics(channel_listener_t *chan_l,
  355. int severity);
  356. /* Timestamp queries */
  357. time_t channel_when_created(channel_t *chan);
  358. time_t channel_when_last_active(channel_t *chan);
  359. time_t channel_when_last_client(channel_t *chan);
  360. time_t channel_when_last_drained(channel_t *chan);
  361. time_t channel_when_last_recv(channel_t *chan);
  362. time_t channel_when_last_xmit(channel_t *chan);
  363. time_t channel_listener_when_created(channel_listener_t *chan_l);
  364. time_t channel_listener_when_last_active(channel_listener_t *chan_l);
  365. time_t channel_listener_when_last_accepted(channel_listener_t *chan_l);
  366. /* Counter queries */
  367. uint64_t channel_count_recved(channel_t *chan);
  368. uint64_t channel_count_xmitted(channel_t *chan);
  369. uint64_t channel_listener_count_accepted(channel_listener_t *chan_l);
  370. #endif