channel.h 17 KB

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