connection_st.h 7.0 KB

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