connection_edge.h 8.3 KB

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