circuit_st.h 6.7 KB

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