connection_st.h (8568B)
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 */ 71 unsigned int in_connection_handle_write:1; 72 /** If true, then we treat this connection as remote for the purpose of 73 * rate-limiting, no matter what its address is. */ 74 unsigned int always_rate_limit_as_remote:1; 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 /** True iff this connection was opened from a listener and thus we've 91 * received this connection. Else, it means we've initiated an outbound 92 * connection. */ 93 unsigned int from_listener:1; 94 95 /** CONNECT/SOCKS proxy client handshake state (for outgoing connections). */ 96 unsigned int proxy_state:4; 97 98 /** Our socket; set to TOR_INVALID_SOCKET if this connection is closed, 99 * or has no socket. */ 100 tor_socket_t s; 101 int conn_array_index; /**< Index into the global connection array. */ 102 103 struct event *read_event; /**< Libevent event structure. */ 104 struct event *write_event; /**< Libevent event structure. */ 105 struct buf_t *inbuf; /**< Buffer holding data read over this connection. */ 106 struct buf_t *outbuf; /**< Buffer holding data to write over this 107 * connection. */ 108 time_t timestamp_last_read_allowed; /**< When was the last time libevent said 109 * we could read? */ 110 time_t timestamp_last_write_allowed; /**< When was the last time libevent 111 * said we could write? */ 112 113 time_t timestamp_created; /**< When was this connection_t created? */ 114 115 int socket_family; /**< Address family of this connection's socket. Usually 116 * AF_INET, but it can also be AF_UNIX, or AF_INET6 */ 117 /** 118 * IP address on the internet of this connection's peer, usually. 119 * 120 * This address may come from several sources. If this is an outbound 121 * connection, it is the address we are trying to connect to--either 122 * directly through `s`, or via a proxy. (If we used a proxy, then 123 * `getpeername(s)` will not give this address.) 124 * 125 * For incoming connections, this field is the address we got from 126 * getpeername() or accept(), as updated by any proxy that we 127 * are using (for example, an ExtORPort proxy). 128 * 129 * For listeners, this is the address we are trying to bind to. 130 * 131 * If this connection is using a unix socket, then this address is a null 132 * address, and the real address is in the `address` field. 133 * 134 * If this connection represents a request made somewhere other than via 135 * TCP (for example, a UDP dns request, or a controller resolve request), 136 * then this address is the address that originated the request. 137 * 138 * TECHNICAL DEBT: 139 * 140 * There are a few places in the code that modify this address, 141 * or use it in other ways that we don't currently like. Please don't add 142 * any more! 143 * 144 * The misuses of this field include: 145 * * Setting it on linked connections, possibly. 146 * * Updating it based on the Forwarded-For header-- Forwarded-For is 147 * set by a proxy, but not a local trusted proxy. 148 **/ 149 tor_addr_t addr; 150 uint16_t port; /**< If non-zero, port that socket "s" is directly connected 151 * to; may be the port for a proxy or pluggable transport, 152 * see "address" for the port at the final destination. */ 153 uint16_t marked_for_close; /**< Should we close this conn on the next 154 * iteration of the main loop? (If true, holds 155 * the line number where this connection was 156 * marked.) */ 157 const char *marked_for_close_file; /**< For debugging: in which file were 158 * we marked for close? */ 159 /** 160 * String address of the peer of this connection. 161 * 162 * TECHNICAL DEBT: 163 * 164 * This field serves many purposes, and they're not all pretty. In addition 165 * to describing the peer we're connected to, it can also hold: 166 * 167 * * An address we're trying to resolve (as an exit). 168 * * A unix address we're trying to bind to (as a listener). 169 **/ 170 char *address; 171 /** Another connection that's connected to this one in lieu of a socket. */ 172 struct connection_t *linked_conn; 173 174 /** Unique identifier for this connection on this Tor instance. */ 175 uint64_t global_identifier; 176 177 /** Bytes read since last call to control_event_conn_bandwidth_used(). 178 * Only used if we're configured to emit CONN_BW events. */ 179 uint32_t n_read_conn_bw; 180 181 /** Bytes written since last call to control_event_conn_bandwidth_used(). 182 * Only used if we're configured to emit CONN_BW events. */ 183 uint32_t n_written_conn_bw; 184 }; 185 186 /** True iff <b>x</b> is an edge connection. */ 187 #define CONN_IS_EDGE(x) \ 188 ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP) 189 190 /** True iff the purpose of <b>conn</b> means that it's a server-side 191 * directory connection. */ 192 #define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER) 193 194 #endif /* !defined(CONNECTION_ST_H) */