tor  0.4.1.0-alpha-dev
connection_edge.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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
12 #ifndef TOR_CONNECTION_EDGE_H
13 #define TOR_CONNECTION_EDGE_H
14 
16 
17 #include "feature/hs/hs_service.h"
18 
20 entry_connection_t *TO_ENTRY_CONN(connection_t *);
21 entry_connection_t *EDGE_TO_ENTRY_CONN(edge_connection_t *);
22 
23 #define EXIT_CONN_STATE_MIN_ 1
24 
25 #define EXIT_CONN_STATE_RESOLVING 1
26 
27 #define EXIT_CONN_STATE_CONNECTING 2
28 
29 #define EXIT_CONN_STATE_OPEN 3
30 
31 #define EXIT_CONN_STATE_RESOLVEFAILED 4
32 #define EXIT_CONN_STATE_MAX_ 4
33 
34 /* The AP state values must be disjoint from the EXIT state values. */
35 #define AP_CONN_STATE_MIN_ 5
36 
37 #define AP_CONN_STATE_SOCKS_WAIT 5
38 
40 #define AP_CONN_STATE_RENDDESC_WAIT 6
41 
43 #define AP_CONN_STATE_CONTROLLER_WAIT 7
44 
45 #define AP_CONN_STATE_CIRCUIT_WAIT 8
46 
47 #define AP_CONN_STATE_CONNECT_WAIT 9
48 
49 #define AP_CONN_STATE_RESOLVE_WAIT 10
50 
51 #define AP_CONN_STATE_OPEN 11
52 
54 #define AP_CONN_STATE_NATD_WAIT 12
55 
56 #define AP_CONN_STATE_HTTP_CONNECT_WAIT 13
57 #define AP_CONN_STATE_MAX_ 13
58 
59 #define EXIT_PURPOSE_MIN_ 1
60 
61 #define EXIT_PURPOSE_CONNECT 1
62 
63 #define EXIT_PURPOSE_RESOLVE 2
64 #define EXIT_PURPOSE_MAX_ 2
65 
68 #define AP_CONN_STATE_IS_UNATTACHED(s) \
69  ((s) <= AP_CONN_STATE_CIRCUIT_WAIT || (s) == AP_CONN_STATE_NATD_WAIT)
70 
71 #define connection_mark_unattached_ap(conn, endreason) \
72  connection_mark_unattached_ap_((conn), (endreason), __LINE__, SHORT_FILE__)
73 
74 MOCK_DECL(void,connection_mark_unattached_ap_,
75  (entry_connection_t *conn, int endreason,
76  int line, const char *file));
79  int package_partial);
81 int connection_edge_end(edge_connection_t *conn, uint8_t reason);
86 
89 
90 MOCK_DECL(int,
91  connection_ap_handshake_send_begin,(entry_connection_t *ap_conn));
93 
95  char *address, uint16_t port,
96  const char *digest,
97  int session_group,
98  int isolation_flags,
99  int use_begindir, int want_onehop);
101  size_t replylen,
102  int endreason);
103 MOCK_DECL(void,connection_ap_handshake_socks_resolved,
104  (entry_connection_t *conn,
105  int answer_type,
106  size_t answer_len,
107  const uint8_t *answer,
108  int ttl,
109  time_t expires));
111  const tor_addr_t *answer,
112  int ttl,
113  time_t expires);
114 
120  const node_t *exit);
123 void connection_ap_attach_pending(int retry);
125  const char *file, int line);
126 #define connection_ap_mark_as_pending_circuit(c) \
127  connection_ap_mark_as_pending_circuit_((c), __FILE__, __LINE__)
130  entry_connection_t *entry_conn);
131 
132 #define CONNECTION_AP_EXPECT_NONPENDING(c) do { \
133  if (ENTRY_TO_CONN(c)->state == AP_CONN_STATE_CIRCUIT_WAIT) { \
134  log_warn(LD_BUG, "At %s:%d: %p was unexpectedly in circuit_wait.", \
135  __FILE__, __LINE__, (c)); \
136  connection_ap_mark_as_non_pending_circuit(c); \
137  } \
138  } while (0)
139 void connection_ap_fail_onehop(const char *failed_digest,
140  cpath_build_state_t *build_state);
143  origin_circuit_t *circ,
144  int reason);
146 
147 int address_is_invalid_destination(const char *address, int client);
148 
149 MOCK_DECL(int, connection_ap_rewrite_and_attach_if_allowed,
150  (entry_connection_t *conn,
151  origin_circuit_t *circ,
152  crypt_path_t *cpath));
154  origin_circuit_t *circ,
155  crypt_path_t *cpath);
156 
158 typedef enum hostname_type_t {
159  NORMAL_HOSTNAME, ONION_V2_HOSTNAME, ONION_V3_HOSTNAME,
160  EXIT_HOSTNAME, BAD_HOSTNAME
163 
164 #if defined(HAVE_NET_IF_H) && defined(HAVE_NET_PFVAR_H)
165 int get_pf_socket(void);
166 #endif
167 
169  const origin_circuit_t *circ);
171  origin_circuit_t *circ,
172  int dry_run);
175 
176 void connection_edge_free_all(void);
177 
178 void connection_ap_warn_and_unmark_if_pending_circ(
179  entry_connection_t *entry_conn,
180  const char *where);
181 
182 int connection_half_edge_is_valid_data(const smartlist_t *half_conns,
183  streamid_t stream_id);
185  streamid_t stream_id);
187  streamid_t stream_id);
189  streamid_t stream_id);
191  streamid_t stream_id);
192 
194 struct half_edge_t;
195 void half_edge_free_(struct half_edge_t *he);
196 #define half_edge_free(he) \
197  FREE_AND_NULL(half_edge_t, half_edge_free_, (he))
198 
208 #define BEGIN_FLAG_IPV6_OK (1u<<0)
209 
213 #define BEGIN_FLAG_IPV4_NOT_OK (1u<<1)
214 
216 #define BEGIN_FLAG_IPV6_PREFERRED (1u<<2)
217 
219 #ifdef CONNECTION_EDGE_PRIVATE
220 
222 typedef struct begin_cell_t {
225  char *address;
228  uint32_t flags;
230  uint16_t port;
232  uint16_t stream_id;
234  unsigned is_begindir : 1;
235 } begin_cell_t;
236 
237 STATIC int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell,
238  uint8_t *end_reason_out);
239 STATIC int connected_cell_format_payload(uint8_t *payload_out,
240  const tor_addr_t *addr,
241  uint32_t ttl);
242 
243 typedef struct {
247  char orig_address[MAX_SOCKS_ADDR_LEN];
251  int automap;
253  addressmap_entry_source_t exit_source;
255  time_t map_expires;
258  int end_reason;
261  int should_close;
262 } rewrite_result_t;
263 
264 STATIC void connection_ap_handshake_rewrite(entry_connection_t *conn,
265  rewrite_result_t *out);
266 
268 STATIC void export_hs_client_circuit_id(edge_connection_t *edge_conn,
269  hs_circuit_id_protocol_t protocol);
270 
271 struct half_edge_t;
272 STATIC void connection_half_edge_add(const edge_connection_t *conn,
273  origin_circuit_t *circ);
275  const smartlist_t *half_conns,
277 #endif /* defined(CONNECTION_EDGE_PRIVATE) */
278 
279 #endif /* !defined(TOR_CONNECTION_EDGE_H) */
int connection_edge_destroy(circid_t circ_id, edge_connection_t *conn)
void connection_ap_handshake_socks_resolved_addr(entry_connection_t *conn, const tor_addr_t *answer, int ttl, time_t expires)
STATIC half_edge_t * connection_half_edge_find_stream_id(const smartlist_t *half_conns, streamid_t stream_id)
void connection_ap_fail_onehop(const char *failed_digest, cpath_build_state_t *build_state)
void connection_ap_mark_as_pending_circuit_(entry_connection_t *entry_conn, const char *file, int line)
Definition: node_st.h:28
Header file containing service data for the HS subsytem.
int connection_edge_reached_eof(edge_connection_t *conn)
edge_connection_t * TO_EDGE_CONN(connection_t *)
Definition: cell_st.h:12
hostname_type_t parse_extended_hostname(char *address)
void circuit_clear_isolation(origin_circuit_t *circ)
int connection_half_edge_is_valid_end(smartlist_t *half_conns, streamid_t stream_id)
int connection_edge_end(edge_connection_t *conn, uint8_t reason)
void connection_edge_free_all(void)
int connection_half_edge_is_valid_data(const smartlist_t *half_conns, streamid_t stream_id)
int connection_edge_end_errno(edge_connection_t *conn)
int connection_half_edge_is_valid_sendme(const smartlist_t *half_conns, streamid_t stream_id)
int connection_edge_process_inbuf(edge_connection_t *conn, int package_partial)
int connection_half_edge_is_valid_resolved(smartlist_t *half_conns, streamid_t stream_id)
addressmap_entry_source_t
Definition: or.h:1019
void connection_exit_about_to_close(edge_connection_t *edge_conn)
void connection_ap_expire_beginning(void)
STATIC void connection_half_edge_add(const edge_connection_t *conn, origin_circuit_t *circ)
void connection_ap_rescan_and_attach_pending(void)
STATIC int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell, uint8_t *end_reason_out)
int connection_half_edge_is_valid_connected(const smartlist_t *half_conns, streamid_t stream_id)
streamid_t get_unique_stream_id_by_circ(origin_circuit_t *circ)
int connection_edge_flushed_some(edge_connection_t *conn)
int connection_ap_handshake_rewrite_and_attach(entry_connection_t *conn, origin_circuit_t *circ, crypt_path_t *cpath)
entry_connection_t * connection_ap_make_link(connection_t *partner, char *address, uint16_t port, const char *digest, int session_group, int isolation_flags, int use_begindir, int want_onehop)
int address_is_invalid_destination(const char *address, int client)
Definition: addressmap.c:1078
streamid_t stream_id
Definition: half_edge_st.h:19
int connection_ap_handshake_send_resolve(entry_connection_t *ap_conn)
uint16_t streamid_t
Definition: or.h:610
int connection_edge_finished_connecting(edge_connection_t *conn)
void connection_ap_handshake_socks_reply(entry_connection_t *conn, char *reply, size_t replylen, int endreason)
int connection_edge_update_circuit_isolation(const entry_connection_t *conn, origin_circuit_t *circ, int dry_run)
void connection_exit_connect(edge_connection_t *conn)
hs_circuit_id_protocol_t
Definition: hs_service.h:181
int connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
int connection_ap_detach_retriable(entry_connection_t *conn, origin_circuit_t *circ, int reason)
hostname_type_t
void half_edge_free_(struct half_edge_t *he)
int connection_edge_compatible_with_circuit(const entry_connection_t *conn, const origin_circuit_t *circ)
void circuit_discard_optional_exit_enclaves(extend_info_t *info)
uint32_t circid_t
Definition: or.h:608
Macros to implement mocking and selective exposure for the test code.
int connection_ap_process_transparent(entry_connection_t *conn)
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
STATIC int connected_cell_format_payload(uint8_t *payload_out, const tor_addr_t *addr, uint32_t ttl)
size_t half_streams_get_total_allocation(void)
void connection_ap_mark_as_waiting_for_renddesc(entry_connection_t *entry_conn)
int connection_edge_is_rendezvous_stream(const edge_connection_t *conn)
int connection_ap_can_use_exit(const entry_connection_t *conn, const node_t *exit)
void connection_ap_mark_as_non_pending_circuit(entry_connection_t *entry_conn)
int connection_exit_begin_resolve(cell_t *cell, or_circuit_t *circ)
void connection_ap_attach_pending(int retry)
int connection_edge_finished_flushing(edge_connection_t *conn)
void connection_ap_about_to_close(entry_connection_t *edge_conn)
STATIC int connection_ap_process_http_connect(entry_connection_t *conn)