Tor  0.4.7.0-alpha-dev
connection_st.h
Go to the documentation of this file.
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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * @file connection_st.h
9  * @brief Base connection structure.
10  **/
11 
12 #ifndef CONNECTION_ST_H
13 #define CONNECTION_ST_H
14 
15 struct buf_t;
16 
17 /* Values for connection_t.magic: used to make sure that downcasts (casts from
18 * connection_t to foo_connection_t) are safe. */
19 #define BASE_CONNECTION_MAGIC 0x7C3C304Eu
20 #define OR_CONNECTION_MAGIC 0x7D31FF03u
21 #define EDGE_CONNECTION_MAGIC 0xF0374013u
22 #define ENTRY_CONNECTION_MAGIC 0xbb4a5703
23 #define DIR_CONNECTION_MAGIC 0x9988ffeeu
24 #define CONTROL_CONNECTION_MAGIC 0x8abc765du
25 #define LISTENER_CONNECTION_MAGIC 0x1a1ac741u
26 
27 /** Description of a connection to another host or process, and associated
28  * data.
29  *
30  * A connection is named based on what it's connected to -- an "OR
31  * connection" has a Tor node on the other end, an "exit
32  * connection" has a website or other server on the other end, and an
33  * "AP connection" has an application proxy (and thus a user) on the
34  * other end.
35  *
36  * Every connection has a type and a state. Connections never change
37  * their type, but can go through many state changes in their lifetime.
38  *
39  * Every connection has two associated input and output buffers.
40  * Listeners don't use them. For non-listener connections, incoming
41  * data is appended to conn->inbuf, and outgoing data is taken from
42  * conn->outbuf. Connections differ primarily in the functions called
43  * to fill and drain these buffers.
44  */
45 struct connection_t {
46  uint32_t magic; /**< For memory debugging: must equal one of
47  * *_CONNECTION_MAGIC. */
48 
49  uint8_t state; /**< Current state of this connection. */
50  unsigned int type:5; /**< What kind of connection is this? */
51  unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
52 
53  /* The next fields are all one-bit booleans. Some are only applicable to
54  * connection subtypes, but we hold them here anyway, to save space.
55  */
56  unsigned int read_blocked_on_bw:1; /**< Boolean: should we start reading
57  * again once the bandwidth throttler allows it? */
58  unsigned int write_blocked_on_bw:1; /**< Boolean: should we start writing
59  * again once the bandwidth throttler allows
60  * writes? */
61  unsigned int hold_open_until_flushed:1; /**< Despite this connection's being
62  * marked for close, do we flush it
63  * before closing it? */
64  unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
65  * conn? */
66  /** Set to 1 when we're inside connection_flushed_some to keep us from
67  * calling connection_handle_write() recursively. */
68  unsigned int in_flushed_some:1;
69  /** True if connection_handle_write is currently running on this connection.
70  */
72  /** If true, then we treat this connection as remote for the purpose of
73  * rate-limiting, no matter what its address is. */
75 
76  /* For linked connections:
77  */
78  unsigned int linked:1; /**< True if there is, or has been, a linked_conn. */
79  /** True iff we'd like to be notified about read events from the
80  * linked conn. */
81  unsigned int reading_from_linked_conn:1;
82  /** True iff we're willing to write to the linked conn. */
83  unsigned int writing_to_linked_conn:1;
84  /** True iff we're currently able to read on the linked conn, and our
85  * read_event should be made active with libevent. */
86  unsigned int active_on_link:1;
87  /** True iff we've called connection_close_immediate() on this linked
88  * connection. */
89  unsigned int linked_conn_is_closed:1;
90 
91  /** CONNECT/SOCKS proxy client handshake state (for outgoing connections). */
92  unsigned int proxy_state:4;
93 
94  /** Our socket; set to TOR_INVALID_SOCKET if this connection is closed,
95  * or has no socket. */
97  int conn_array_index; /**< Index into the global connection array. */
98 
99  struct event *read_event; /**< Libevent event structure. */
100  struct event *write_event; /**< Libevent event structure. */
101  struct buf_t *inbuf; /**< Buffer holding data read over this connection. */
102  struct buf_t *outbuf; /**< Buffer holding data to write over this
103  * connection. */
104  time_t timestamp_last_read_allowed; /**< When was the last time libevent said
105  * we could read? */
106  time_t timestamp_last_write_allowed; /**< When was the last time libevent
107  * said we could write? */
108 
109  time_t timestamp_created; /**< When was this connection_t created? */
110 
111  int socket_family; /**< Address family of this connection's socket. Usually
112  * AF_INET, but it can also be AF_UNIX, or AF_INET6 */
113  /**
114  * IP address on the internet of this connection's peer, usually.
115  *
116  * This address may come from several sources. If this is an outbound
117  * connection, it is the address we are trying to connect to--either
118  * directly through `s`, or via a proxy. (If we used a proxy, then
119  * `getpeername(s)` will not give this address.)
120  *
121  * For incoming connections, this field is the address we got from
122  * getpeername() or accept(), as updated by any proxy that we
123  * are using (for example, an ExtORPort proxy).
124  *
125  * For listeners, this is the address we are trying to bind to.
126  *
127  * If this connection is using a unix socket, then this address is a null
128  * address, and the real address is in the `address` field.
129  *
130  * If this connection represents a request made somewhere other than via
131  * TCP (for example, a UDP dns request, or a controller resolve request),
132  * then this address is the address that originated the request.
133  *
134  * TECHNICAL DEBT:
135  *
136  * There are a few places in the code that modify this address,
137  * or use it in other ways that we don't currently like. Please don't add
138  * any more!
139  *
140  * The misuses of this field include:
141  * * Setting it on linked connections, possibly.
142  * * Updating it based on the Forwarded-For header-- Forwarded-For is
143  * set by a proxy, but not a local trusted proxy.
144  **/
146  uint16_t port; /**< If non-zero, port that socket "s" is directly connected
147  * to; may be the port for a proxy or pluggable transport,
148  * see "address" for the port at the final destination. */
149  uint16_t marked_for_close; /**< Should we close this conn on the next
150  * iteration of the main loop? (If true, holds
151  * the line number where this connection was
152  * marked.) */
153  const char *marked_for_close_file; /**< For debugging: in which file were
154  * we marked for close? */
155  /**
156  * String address of the peer of this connection.
157  *
158  * TECHNICAL DEBT:
159  *
160  * This field serves many purposes, and they're not all pretty. In addition
161  * to describing the peer we're connected to, it can also hold:
162  *
163  * * An address we're trying to resolve (as an exit).
164  * * A unix address we're trying to bind to (as a listener).
165  **/
166  char *address;
167  /** Another connection that's connected to this one in lieu of a socket. */
169 
170  /** Unique identifier for this connection on this Tor instance. */
172 
173  /** Bytes read since last call to control_event_conn_bandwidth_used().
174  * Only used if we're configured to emit CONN_BW events. */
175  uint32_t n_read_conn_bw;
176 
177  /** Bytes written since last call to control_event_conn_bandwidth_used().
178  * Only used if we're configured to emit CONN_BW events. */
180 };
181 
182 /** True iff <b>x</b> is an edge connection. */
183 #define CONN_IS_EDGE(x) \
184  ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
185 
186 /** True iff the purpose of <b>conn</b> means that it's a server-side
187  * directory connection. */
188 #define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER)
189 
190 #endif /* !defined(CONNECTION_ST_H) */
#define tor_socket_t
Definition: nettypes.h:36
time_t timestamp_last_read_allowed
unsigned int proxy_state
Definition: connection_st.h:92
uint8_t state
Definition: connection_st.h:49
unsigned int writing_to_linked_conn
Definition: connection_st.h:83
struct buf_t * inbuf
unsigned int in_connection_handle_write
Definition: connection_st.h:71
struct event * write_event
uint32_t n_read_conn_bw
unsigned int inbuf_reached_eof
Definition: connection_st.h:64
struct connection_t * linked_conn
unsigned int hold_open_until_flushed
Definition: connection_st.h:61
int conn_array_index
Definition: connection_st.h:97
unsigned int reading_from_linked_conn
Definition: connection_st.h:81
unsigned int type
Definition: connection_st.h:50
struct buf_t * outbuf
uint32_t magic
Definition: connection_st.h:46
uint64_t global_identifier
unsigned int read_blocked_on_bw
Definition: connection_st.h:56
unsigned int linked
Definition: connection_st.h:78
uint16_t marked_for_close
uint16_t port
const char * marked_for_close_file
uint32_t n_written_conn_bw
unsigned int linked_conn_is_closed
Definition: connection_st.h:89
unsigned int in_flushed_some
Definition: connection_st.h:68
unsigned int purpose
Definition: connection_st.h:51
tor_socket_t s
Definition: connection_st.h:96
unsigned int always_rate_limit_as_remote
Definition: connection_st.h:74
time_t timestamp_created
unsigned int active_on_link
Definition: connection_st.h:86
unsigned int write_blocked_on_bw
Definition: connection_st.h:58
struct event * read_event
Definition: connection_st.h:99
time_t timestamp_last_write_allowed
tor_addr_t addr