connection_st.h 6.2 KB

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