connection_edge.h 8.7 KB

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