connection_st.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. event_source_t *event_source;
  43. uint8_t state; /**< Current state of this connection. */
  44. unsigned int type:5; /**< What kind of connection is this? */
  45. unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
  46. /* The next fields are all one-bit booleans. Some are only applicable to
  47. * connection subtypes, but we hold them here anyway, to save space.
  48. */
  49. unsigned int read_blocked_on_bw:1; /**< Boolean: should we start reading
  50. * again once the bandwidth throttler allows it? */
  51. unsigned int write_blocked_on_bw:1; /**< Boolean: should we start writing
  52. * again once the bandwidth throttler allows
  53. * writes? */
  54. unsigned int hold_open_until_flushed:1; /**< Despite this connection's being
  55. * marked for close, do we flush it
  56. * before closing it? */
  57. unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
  58. * conn? */
  59. /** Set to 1 when we're inside connection_flushed_some to keep us from
  60. * calling connection_handle_write() recursively. */
  61. unsigned int in_flushed_some:1;
  62. /** True if connection_handle_write is currently running on this connection.
  63. */
  64. unsigned int in_connection_handle_write:1;
  65. /* For linked connections:
  66. */
  67. unsigned int linked:1; /**< True if there is, or has been, a linked_conn. */
  68. /** True iff we'd like to be notified about read events from the
  69. * linked conn. */
  70. unsigned int reading_from_linked_conn:1;
  71. /** True iff we're willing to write to the linked conn. */
  72. unsigned int writing_to_linked_conn:1;
  73. /** True iff we're currently able to read on the linked conn, and our
  74. * read_event should be made active with libevent. */
  75. unsigned int active_on_link:1;
  76. /** True iff we've called connection_close_immediate() on this linked
  77. * connection. */
  78. unsigned int linked_conn_is_closed:1;
  79. /** CONNECT/SOCKS proxy client handshake state (for outgoing connections). */
  80. unsigned int proxy_state:4;
  81. /** Our socket; set to TOR_INVALID_SOCKET if this connection is closed,
  82. * or has no socket. */
  83. tor_socket_t s;
  84. tor_socket_t scheduler_socket_cache;
  85. int conn_array_index; /**< Index into the global connection array. */
  86. struct event *read_event; /**< Libevent event structure. */
  87. struct event *write_event; /**< Libevent event structure. */
  88. struct buf_t *inbuf; /**< Buffer holding data read over this connection. */
  89. struct buf_t *outbuf; /**< Buffer holding data to write over this
  90. * connection. */
  91. size_t outbuf_flushlen; /**< How much data should we try to flush from the
  92. * outbuf? */
  93. time_t timestamp_last_read_allowed; /**< When was the last time libevent said
  94. * we could read? */
  95. time_t timestamp_last_write_allowed; /**< When was the last time libevent
  96. * said we could write? */
  97. time_t timestamp_created; /**< When was this connection_t created? */
  98. int socket_family; /**< Address family of this connection's socket. Usually
  99. * AF_INET, but it can also be AF_UNIX, or AF_INET6 */
  100. tor_addr_t addr; /**< IP that socket "s" is directly connected to;
  101. * may be the IP address for a proxy or pluggable transport,
  102. * see "address" for the address of the final destination.
  103. */
  104. uint16_t port; /**< If non-zero, port that socket "s" is directly connected
  105. * to; may be the port for a proxy or pluggable transport,
  106. * see "address" for the port at the final destination. */
  107. uint16_t marked_for_close; /**< Should we close this conn on the next
  108. * iteration of the main loop? (If true, holds
  109. * the line number where this connection was
  110. * marked.) */
  111. const char *marked_for_close_file; /**< For debugging: in which file were
  112. * we marked for close? */
  113. char *address; /**< FQDN (or IP) and port of the final destination for this
  114. * connection; this is always the remote address, it is
  115. * passed to a proxy or pluggable transport if one in use.
  116. * See "addr" and "port" for the address that socket "s" is
  117. * directly connected to.
  118. * strdup into this, because free_connection() frees it. */
  119. /** Another connection that's connected to this one in lieu of a socket. */
  120. struct connection_t *linked_conn;
  121. /** Unique identifier for this connection on this Tor instance. */
  122. uint64_t global_identifier;
  123. /** Bytes read since last call to control_event_conn_bandwidth_used().
  124. * Only used if we're configured to emit CONN_BW events. */
  125. uint32_t n_read_conn_bw;
  126. /** Bytes written since last call to control_event_conn_bandwidth_used().
  127. * Only used if we're configured to emit CONN_BW events. */
  128. uint32_t n_written_conn_bw;
  129. };
  130. /** True iff <b>x</b> is an edge connection. */
  131. #define CONN_IS_EDGE(x) \
  132. ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
  133. /** True iff the purpose of <b>conn</b> means that it's a server-side
  134. * directory connection. */
  135. #define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER)
  136. #endif /* !defined(CONNECTION_ST_H) */