circuit_st.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #ifndef CIRCUIT_ST_H
  7. #define CIRCUIT_ST_H
  8. #include "core/or/or.h"
  9. #include "core/or/cell_queue_st.h"
  10. struct hs_token_t;
  11. /** "magic" value for an origin_circuit_t */
  12. #define ORIGIN_CIRCUIT_MAGIC 0x35315243u
  13. /** "magic" value for an or_circuit_t */
  14. #define OR_CIRCUIT_MAGIC 0x98ABC04Fu
  15. /** "magic" value for a circuit that would have been freed by circuit_free,
  16. * but which we're keeping around until a cpuworker reply arrives. See
  17. * circuit_free() for more documentation. */
  18. #define DEAD_CIRCUIT_MAGIC 0xdeadc14c
  19. /**
  20. * A circuit is a path over the onion routing
  21. * network. Applications can connect to one end of the circuit, and can
  22. * create exit connections at the other end of the circuit. AP and exit
  23. * connections have only one circuit associated with them (and thus these
  24. * connection types are closed when the circuit is closed), whereas
  25. * OR connections multiplex many circuits at once, and stay standing even
  26. * when there are no circuits running over them.
  27. *
  28. * A circuit_t structure can fill one of two roles. First, a or_circuit_t
  29. * links two connections together: either an edge connection and an OR
  30. * connection, or two OR connections. (When joined to an OR connection, a
  31. * circuit_t affects only cells sent to a particular circID on that
  32. * connection. When joined to an edge connection, a circuit_t affects all
  33. * data.)
  34. * Second, an origin_circuit_t holds the cipher keys and state for sending data
  35. * along a given circuit. At the OP, it has a sequence of ciphers, each
  36. * of which is shared with a single OR along the circuit. Separate
  37. * ciphers are used for data going "forward" (away from the OP) and
  38. * "backward" (towards the OP). At the OR, a circuit has only two stream
  39. * ciphers: one for data going forward, and one for data going backward.
  40. */
  41. struct circuit_t {
  42. uint32_t magic; /**< For memory and type debugging: must equal
  43. * ORIGIN_CIRCUIT_MAGIC or OR_CIRCUIT_MAGIC. */
  44. /** The channel that is next in this circuit. */
  45. channel_t *n_chan;
  46. /**
  47. * The circuit_id used in the next (forward) hop of this circuit;
  48. * this is unique to n_chan, but this ordered pair is globally
  49. * unique:
  50. *
  51. * (n_chan->global_identifier, n_circ_id)
  52. */
  53. circid_t n_circ_id;
  54. /**
  55. * Circuit mux associated with n_chan to which this circuit is attached;
  56. * NULL if we have no n_chan.
  57. */
  58. circuitmux_t *n_mux;
  59. /** Queue of cells waiting to be transmitted on n_chan */
  60. cell_queue_t n_chan_cells;
  61. /**
  62. * The hop to which we want to extend this circuit. Should be NULL if
  63. * the circuit has attached to a channel.
  64. */
  65. extend_info_t *n_hop;
  66. /** True iff we are waiting for n_chan_cells to become less full before
  67. * allowing p_streams to add any more cells. (Origin circuit only.) */
  68. unsigned int streams_blocked_on_n_chan : 1;
  69. /** True iff we are waiting for p_chan_cells to become less full before
  70. * allowing n_streams to add any more cells. (OR circuit only.) */
  71. unsigned int streams_blocked_on_p_chan : 1;
  72. /** True iff we have queued a delete backwards on this circuit, but not put
  73. * it on the output buffer. */
  74. unsigned int p_delete_pending : 1;
  75. /** True iff we have queued a delete forwards on this circuit, but not put
  76. * it on the output buffer. */
  77. unsigned int n_delete_pending : 1;
  78. /** True iff this circuit has received a DESTROY cell in either direction */
  79. unsigned int received_destroy : 1;
  80. uint8_t state; /**< Current status of this circuit. */
  81. uint8_t purpose; /**< Why are we creating this circuit? */
  82. /** How many relay data cells can we package (read from edge streams)
  83. * on this circuit before we receive a circuit-level sendme cell asking
  84. * for more? */
  85. int package_window;
  86. /** How many relay data cells will we deliver (write to edge streams)
  87. * on this circuit? When deliver_window gets low, we send some
  88. * circuit-level sendme cells to indicate that we're willing to accept
  89. * more. */
  90. int deliver_window;
  91. /** Temporary field used during circuits_handle_oom. */
  92. uint32_t age_tmp;
  93. /** For storage while n_chan is pending (state CIRCUIT_STATE_CHAN_WAIT). */
  94. struct create_cell_t *n_chan_create_cell;
  95. /** When did circuit construction actually begin (ie send the
  96. * CREATE cell or begin cannibalization).
  97. *
  98. * Note: This timer will get reset if we decide to cannibalize
  99. * a circuit. It may also get reset during certain phases of hidden
  100. * service circuit use.
  101. *
  102. * We keep this timestamp with a higher resolution than most so that the
  103. * circuit-build-time tracking code can get millisecond resolution.
  104. */
  105. struct timeval timestamp_began;
  106. /** This timestamp marks when the init_circuit_base constructor ran. */
  107. struct timeval timestamp_created;
  108. /** When the circuit was first used, or 0 if the circuit is clean.
  109. *
  110. * XXXX Note that some code will artificially adjust this value backward
  111. * in time in order to indicate that a circuit shouldn't be used for new
  112. * streams, but that it can stay alive as long as it has streams on it.
  113. * That's a kludge we should fix.
  114. *
  115. * XXX The CBT code uses this field to record when HS-related
  116. * circuits entered certain states. This usage probably won't
  117. * interfere with this field's primary purpose, but we should
  118. * document it more thoroughly to make sure of that.
  119. *
  120. * XXX The SocksPort option KeepaliveIsolateSOCKSAuth will artificially
  121. * adjust this value forward each time a suitable stream is attached to an
  122. * already constructed circuit, potentially keeping the circuit alive
  123. * indefinitely.
  124. */
  125. time_t timestamp_dirty;
  126. uint16_t marked_for_close; /**< Should we close this circuit at the end of
  127. * the main loop? (If true, holds the line number
  128. * where this circuit was marked.) */
  129. const char *marked_for_close_file; /**< For debugging: in which file was this
  130. * circuit marked for close? */
  131. /** For what reason (See END_CIRC_REASON...) is this circuit being closed?
  132. * This field is set in circuit_mark_for_close and used later in
  133. * circuit_about_to_free. */
  134. int marked_for_close_reason;
  135. /** As marked_for_close_reason, but reflects the underlying reason for
  136. * closing this circuit.
  137. */
  138. int marked_for_close_orig_reason;
  139. /** Unique ID for measuring tunneled network status requests. */
  140. uint64_t dirreq_id;
  141. /** Index in smartlist of all circuits (global_circuitlist). */
  142. int global_circuitlist_idx;
  143. /** Various statistics about cells being added to or removed from this
  144. * circuit's queues; used only if CELL_STATS events are enabled and
  145. * cleared after being sent to control port. */
  146. smartlist_t *testing_cell_stats;
  147. /** If set, points to an HS token that this circuit might be carrying.
  148. * Used by the HS circuitmap. */
  149. struct hs_token_t *hs_token;
  150. /** Hashtable node: used to look up the circuit by its HS token using the HS
  151. circuitmap. */
  152. HT_ENTRY(circuit_t) hs_circuitmap_node;
  153. };
  154. #endif