connection_st.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 CONNECTION_ST_H
  7. #define CONNECTION_ST_H
  8. struct buf_t;
  9. /* Values for connection_t.magic: used to make sure that downcasts (casts from
  10. * connection_t to foo_connection_t) are safe. */
  11. #define BASE_CONNECTION_MAGIC 0x7C3C304Eu
  12. #define OR_CONNECTION_MAGIC 0x7D31FF03u
  13. #define EDGE_CONNECTION_MAGIC 0xF0374013u
  14. #define ENTRY_CONNECTION_MAGIC 0xbb4a5703
  15. #define DIR_CONNECTION_MAGIC 0x9988ffeeu
  16. #define CONTROL_CONNECTION_MAGIC 0x8abc765du
  17. #define LISTENER_CONNECTION_MAGIC 0x1a1ac741u
  18. /** Description of a connection to another host or process, and associated
  19. * data.
  20. *
  21. * A connection is named based on what it's connected to -- an "OR
  22. * connection" has a Tor node on the other end, an "exit
  23. * connection" has a website or other server on the other end, and an
  24. * "AP connection" has an application proxy (and thus a user) on the
  25. * other end.
  26. *
  27. * Every connection has a type and a state. Connections never change
  28. * their type, but can go through many state changes in their lifetime.
  29. *
  30. * Every connection has two associated input and output buffers.
  31. * Listeners don't use them. For non-listener connections, incoming
  32. * data is appended to conn->inbuf, and outgoing data is taken from
  33. * conn->outbuf. Connections differ primarily in the functions called
  34. * to fill and drain these buffers.
  35. */
  36. struct connection_t {
  37. uint32_t magic; /**< For memory debugging: must equal one of
  38. * *_CONNECTION_MAGIC. */
  39. uint8_t state; /**< Current state of this connection. */
  40. unsigned int type:5; /**< What kind of connection is this? */
  41. unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
  42. /* The next fields are all one-bit booleans. Some are only applicable to
  43. * connection subtypes, but we hold them here anyway, to save space.
  44. */
  45. unsigned int read_blocked_on_bw:1; /**< Boolean: should we start reading
  46. * again once the bandwidth throttler allows it? */
  47. unsigned int write_blocked_on_bw:1; /**< Boolean: should we start writing
  48. * again once the bandwidth throttler allows
  49. * writes? */
  50. unsigned int hold_open_until_flushed:1; /**< Despite this connection's being
  51. * marked for close, do we flush it
  52. * before closing it? */
  53. unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
  54. * conn? */
  55. /** Set to 1 when we're inside connection_flushed_some to keep us from
  56. * calling connection_handle_write() recursively. */
  57. unsigned int in_flushed_some:1;
  58. /** True if connection_handle_write is currently running on this connection.
  59. */
  60. unsigned int in_connection_handle_write:1;
  61. /* For linked connections:
  62. */
  63. unsigned int linked:1; /**< True if there is, or has been, a linked_conn. */
  64. /** True iff we'd like to be notified about read events from the
  65. * linked conn. */
  66. unsigned int reading_from_linked_conn:1;
  67. /** True iff we're willing to write to the linked conn. */
  68. unsigned int writing_to_linked_conn:1;
  69. /** True iff we're currently able to read on the linked conn, and our
  70. * read_event should be made active with libevent. */
  71. unsigned int active_on_link:1;
  72. /** True iff we've called connection_close_immediate() on this linked
  73. * connection. */
  74. unsigned int linked_conn_is_closed:1;
  75. /** CONNECT/SOCKS proxy client handshake state (for outgoing connections). */
  76. unsigned int proxy_state:4;
  77. /** Our socket; set to TOR_INVALID_SOCKET if this connection is closed,
  78. * or has no socket. */
  79. tor_socket_t s;
  80. int conn_array_index; /**< Index into the global connection array. */
  81. struct event *read_event; /**< Libevent event structure. */
  82. struct event *write_event; /**< Libevent event structure. */
  83. struct buf_t *inbuf; /**< Buffer holding data read over this connection. */
  84. struct buf_t *outbuf; /**< Buffer holding data to write over this
  85. * connection. */
  86. size_t outbuf_flushlen; /**< How much data should we try to flush from the
  87. * outbuf? */
  88. time_t timestamp_last_read_allowed; /**< When was the last time libevent said
  89. * we could read? */
  90. time_t timestamp_last_write_allowed; /**< When was the last time libevent
  91. * said we could write? */
  92. time_t timestamp_created; /**< When was this connection_t created? */
  93. int socket_family; /**< Address family of this connection's socket. Usually
  94. * AF_INET, but it can also be AF_UNIX, or AF_INET6 */
  95. tor_addr_t addr; /**< IP that socket "s" is directly connected to;
  96. * may be the IP address for a proxy or pluggable transport,
  97. * see "address" for the address of the final destination.
  98. */
  99. uint16_t port; /**< If non-zero, port that socket "s" is directly connected
  100. * to; may be the port for a proxy or pluggable transport,
  101. * see "address" for the port at the final destination. */
  102. uint16_t marked_for_close; /**< Should we close this conn on the next
  103. * iteration of the main loop? (If true, holds
  104. * the line number where this connection was
  105. * marked.) */
  106. const char *marked_for_close_file; /**< For debugging: in which file were
  107. * we marked for close? */
  108. char *address; /**< FQDN (or IP) and port of the final destination for this
  109. * connection; this is always the remote address, it is
  110. * passed to a proxy or pluggable transport if one in use.
  111. * See "addr" and "port" for the address that socket "s" is
  112. * directly connected to.
  113. * strdup into this, because free_connection() frees it. */
  114. /** Another connection that's connected to this one in lieu of a socket. */
  115. struct connection_t *linked_conn;
  116. /** Unique identifier for this connection on this Tor instance. */
  117. uint64_t global_identifier;
  118. /** Bytes read since last call to control_event_conn_bandwidth_used().
  119. * Only used if we're configured to emit CONN_BW events. */
  120. uint32_t n_read_conn_bw;
  121. /** Bytes written since last call to control_event_conn_bandwidth_used().
  122. * Only used if we're configured to emit CONN_BW events. */
  123. uint32_t n_written_conn_bw;
  124. };
  125. /** True iff <b>x</b> is an edge connection. */
  126. #define CONN_IS_EDGE(x) \
  127. ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
  128. /** True iff the purpose of <b>conn</b> means that it's a server-side
  129. * directory connection. */
  130. #define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER)
  131. #endif