conn: Stop writing when our write bandwidth limist is exhausted
[tor.git] / src / or / connection_edge.h
blobc6583d3845a084f54938a018b1e1f261bf831c13
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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file connection_edge.h
9 * \brief Header file for connection_edge.c.
10 **/
12 #ifndef TOR_CONNECTION_EDGE_H
13 #define TOR_CONNECTION_EDGE_H
15 #include "testsupport.h"
17 #define connection_mark_unattached_ap(conn, endreason) \
18 connection_mark_unattached_ap_((conn), (endreason), __LINE__, SHORT_FILE__)
20 MOCK_DECL(void,connection_mark_unattached_ap_,
21 (entry_connection_t *conn, int endreason,
22 int line, const char *file));
23 int connection_edge_reached_eof(edge_connection_t *conn);
24 int connection_edge_process_inbuf(edge_connection_t *conn,
25 int package_partial);
26 int connection_edge_destroy(circid_t circ_id, edge_connection_t *conn);
27 int connection_edge_end(edge_connection_t *conn, uint8_t reason);
28 int connection_edge_end_errno(edge_connection_t *conn);
29 int connection_edge_flushed_some(edge_connection_t *conn);
30 int connection_edge_finished_flushing(edge_connection_t *conn);
31 int connection_edge_finished_connecting(edge_connection_t *conn);
33 void connection_ap_about_to_close(entry_connection_t *edge_conn);
34 void connection_exit_about_to_close(edge_connection_t *edge_conn);
36 MOCK_DECL(int,
37 connection_ap_handshake_send_begin,(entry_connection_t *ap_conn));
38 int connection_ap_handshake_send_resolve(entry_connection_t *ap_conn);
40 entry_connection_t *connection_ap_make_link(connection_t *partner,
41 char *address, uint16_t port,
42 const char *digest,
43 int session_group,
44 int isolation_flags,
45 int use_begindir, int want_onehop);
46 void connection_ap_handshake_socks_reply(entry_connection_t *conn, char *reply,
47 size_t replylen,
48 int endreason);
49 MOCK_DECL(void,connection_ap_handshake_socks_resolved,
50 (entry_connection_t *conn,
51 int answer_type,
52 size_t answer_len,
53 const uint8_t *answer,
54 int ttl,
55 time_t expires));
56 void connection_ap_handshake_socks_resolved_addr(entry_connection_t *conn,
57 const tor_addr_t *answer,
58 int ttl,
59 time_t expires);
61 int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
62 int connection_exit_begin_resolve(cell_t *cell, or_circuit_t *circ);
63 void connection_exit_connect(edge_connection_t *conn);
64 int connection_edge_is_rendezvous_stream(const edge_connection_t *conn);
65 int connection_ap_can_use_exit(const entry_connection_t *conn,
66 const node_t *exit);
67 void connection_ap_expire_beginning(void);
68 void connection_ap_rescan_and_attach_pending(void);
69 void connection_ap_attach_pending(int retry);
70 void connection_ap_mark_as_pending_circuit_(entry_connection_t *entry_conn,
71 const char *file, int line);
72 #define connection_ap_mark_as_pending_circuit(c) \
73 connection_ap_mark_as_pending_circuit_((c), __FILE__, __LINE__)
74 void connection_ap_mark_as_non_pending_circuit(entry_connection_t *entry_conn);
75 #define CONNECTION_AP_EXPECT_NONPENDING(c) do { \
76 if (ENTRY_TO_CONN(c)->state == AP_CONN_STATE_CIRCUIT_WAIT) { \
77 log_warn(LD_BUG, "At %s:%d: %p was unexpectedly in circuit_wait.", \
78 __FILE__, __LINE__, (c)); \
79 connection_ap_mark_as_non_pending_circuit(c); \
80 } \
81 } while (0)
82 void connection_ap_fail_onehop(const char *failed_digest,
83 cpath_build_state_t *build_state);
84 void circuit_discard_optional_exit_enclaves(extend_info_t *info);
85 int connection_ap_detach_retriable(entry_connection_t *conn,
86 origin_circuit_t *circ,
87 int reason);
88 int connection_ap_process_transparent(entry_connection_t *conn);
90 int address_is_invalid_destination(const char *address, int client);
92 MOCK_DECL(int, connection_ap_rewrite_and_attach_if_allowed,
93 (entry_connection_t *conn,
94 origin_circuit_t *circ,
95 crypt_path_t *cpath));
96 int connection_ap_handshake_rewrite_and_attach(entry_connection_t *conn,
97 origin_circuit_t *circ,
98 crypt_path_t *cpath);
100 /** Possible return values for parse_extended_hostname. */
101 typedef enum hostname_type_t {
102 NORMAL_HOSTNAME, ONION_V2_HOSTNAME, ONION_V3_HOSTNAME,
103 EXIT_HOSTNAME, BAD_HOSTNAME
104 } hostname_type_t;
105 hostname_type_t parse_extended_hostname(char *address);
107 #if defined(HAVE_NET_IF_H) && defined(HAVE_NET_PFVAR_H)
108 int get_pf_socket(void);
109 #endif
111 int connection_edge_compatible_with_circuit(const entry_connection_t *conn,
112 const origin_circuit_t *circ);
113 int connection_edge_update_circuit_isolation(const entry_connection_t *conn,
114 origin_circuit_t *circ,
115 int dry_run);
116 void circuit_clear_isolation(origin_circuit_t *circ);
117 streamid_t get_unique_stream_id_by_circ(origin_circuit_t *circ);
119 void connection_edge_free_all(void);
121 void connection_ap_warn_and_unmark_if_pending_circ(
122 entry_connection_t *entry_conn,
123 const char *where);
125 /** @name Begin-cell flags
127 * These flags are used in RELAY_BEGIN cells to change the default behavior
128 * of the cell.
130 * @{
132 /** When this flag is set, the client is willing to get connected to IPv6
133 * addresses */
134 #define BEGIN_FLAG_IPV6_OK (1u<<0)
135 /** When this flag is set, the client DOES NOT support connecting to IPv4
136 * addresses. (The sense of this flag is inverted from IPV6_OK, so that the
137 * old default behavior of Tor is equivalent to having all flags set to 0.)
139 #define BEGIN_FLAG_IPV4_NOT_OK (1u<<1)
140 /** When this flag is set, if we find both an IPv4 and an IPv6 address,
141 * we use the IPv6 address. Otherwise we use the IPv4 address. */
142 #define BEGIN_FLAG_IPV6_PREFERRED (1u<<2)
143 /**@}*/
145 #ifdef CONNECTION_EDGE_PRIVATE
147 /** A parsed BEGIN or BEGIN_DIR cell */
148 typedef struct begin_cell_t {
149 /** The address the client has asked us to connect to, or NULL if this is
150 * a BEGIN_DIR cell*/
151 char *address;
152 /** The flags specified in the BEGIN cell's body. One or more of
153 * BEGIN_FLAG_*. */
154 uint32_t flags;
155 /** The client's requested port. */
156 uint16_t port;
157 /** The client's requested Stream ID */
158 uint16_t stream_id;
159 /** True iff this is a BEGIN_DIR cell. */
160 unsigned is_begindir : 1;
161 } begin_cell_t;
163 STATIC int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell,
164 uint8_t *end_reason_out);
165 STATIC int connected_cell_format_payload(uint8_t *payload_out,
166 const tor_addr_t *addr,
167 uint32_t ttl);
169 typedef struct {
170 /** Original address, after we lowercased it but before we started
171 * mapping it.
173 char orig_address[MAX_SOCKS_ADDR_LEN];
174 /** True iff the address has been automatically remapped to a local
175 * address in VirtualAddrNetwork. (Only set true when we do a resolve
176 * and get a virtual address; not when we connect to the address.) */
177 int automap;
178 /** If this connection has a .exit address, who put it there? */
179 addressmap_entry_source_t exit_source;
180 /** If we've rewritten the address, when does this map expire? */
181 time_t map_expires;
182 /** If we should close the connection, this is the end_reason to pass
183 * to connection_mark_unattached_ap */
184 int end_reason;
185 /** True iff we should close the connection, either because of error or
186 * because of successful early RESOLVED reply. */
187 int should_close;
188 } rewrite_result_t;
190 STATIC void connection_ap_handshake_rewrite(entry_connection_t *conn,
191 rewrite_result_t *out);
193 STATIC int connection_ap_process_http_connect(entry_connection_t *conn);
194 #endif /* defined(CONNECTION_EDGE_PRIVATE) */
196 #endif /* !defined(TOR_CONNECTION_EDGE_H) */