channel.h 17 KB

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