connection.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.h
  8. * \brief Header file for connection.c.
  9. **/
  10. #ifndef TOR_CONNECTION_H
  11. #define TOR_CONNECTION_H
  12. listener_connection_t *TO_LISTENER_CONN(connection_t *);
  13. struct buf_t;
  14. #define CONN_TYPE_MIN_ 3
  15. /** Type for sockets listening for OR connections. */
  16. #define CONN_TYPE_OR_LISTENER 3
  17. /** A bidirectional TLS connection transmitting a sequence of cells.
  18. * May be from an OR to an OR, or from an OP to an OR. */
  19. #define CONN_TYPE_OR 4
  20. /** A TCP connection from an onion router to a stream's destination. */
  21. #define CONN_TYPE_EXIT 5
  22. /** Type for sockets listening for SOCKS connections. */
  23. #define CONN_TYPE_AP_LISTENER 6
  24. /** A SOCKS proxy connection from the user application to the onion
  25. * proxy. */
  26. #define CONN_TYPE_AP 7
  27. /** Type for sockets listening for HTTP connections to the directory server. */
  28. #define CONN_TYPE_DIR_LISTENER 8
  29. /** Type for HTTP connections to the directory server. */
  30. #define CONN_TYPE_DIR 9
  31. /* Type 10 is unused. */
  32. /** Type for listening for connections from user interface process. */
  33. #define CONN_TYPE_CONTROL_LISTENER 11
  34. /** Type for connections from user interface process. */
  35. #define CONN_TYPE_CONTROL 12
  36. /** Type for sockets listening for transparent connections redirected by pf or
  37. * netfilter. */
  38. #define CONN_TYPE_AP_TRANS_LISTENER 13
  39. /** Type for sockets listening for transparent connections redirected by
  40. * natd. */
  41. #define CONN_TYPE_AP_NATD_LISTENER 14
  42. /** Type for sockets listening for DNS requests. */
  43. #define CONN_TYPE_AP_DNS_LISTENER 15
  44. /** Type for connections from the Extended ORPort. */
  45. #define CONN_TYPE_EXT_OR 16
  46. /** Type for sockets listening for Extended ORPort connections. */
  47. #define CONN_TYPE_EXT_OR_LISTENER 17
  48. /** Type for sockets listening for HTTP CONNECT tunnel connections. */
  49. #define CONN_TYPE_AP_HTTP_CONNECT_LISTENER 18
  50. #define CONN_TYPE_MAX_ 19
  51. /* !!!! If _CONN_TYPE_MAX is ever over 31, we must grow the type field in
  52. * connection_t. */
  53. /* Proxy client handshake states */
  54. /* We use a proxy but we haven't even connected to it yet. */
  55. #define PROXY_INFANT 1
  56. /* We use an HTTP proxy and we've sent the CONNECT command. */
  57. #define PROXY_HTTPS_WANT_CONNECT_OK 2
  58. /* We use a SOCKS4 proxy and we've sent the CONNECT command. */
  59. #define PROXY_SOCKS4_WANT_CONNECT_OK 3
  60. /* We use a SOCKS5 proxy and we try to negotiate without
  61. any authentication . */
  62. #define PROXY_SOCKS5_WANT_AUTH_METHOD_NONE 4
  63. /* We use a SOCKS5 proxy and we try to negotiate with
  64. Username/Password authentication . */
  65. #define PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929 5
  66. /* We use a SOCKS5 proxy and we just sent our credentials. */
  67. #define PROXY_SOCKS5_WANT_AUTH_RFC1929_OK 6
  68. /* We use a SOCKS5 proxy and we just sent our CONNECT command. */
  69. #define PROXY_SOCKS5_WANT_CONNECT_OK 7
  70. /* We use a proxy and we CONNECTed successfully!. */
  71. #define PROXY_CONNECTED 8
  72. /** State for any listener connection. */
  73. #define LISTENER_STATE_READY 0
  74. /**
  75. * This struct associates an old listener connection to be replaced
  76. * by new connection described by port configuration. Only used when
  77. * moving listeners to/from wildcard IP address.
  78. */
  79. typedef struct
  80. {
  81. connection_t *old_conn; /* Old listener connection to be replaced */
  82. const port_cfg_t *new_port; /* New port configuration */
  83. } listener_replacement_t;
  84. const char *conn_type_to_string(int type);
  85. const char *conn_state_to_string(int type, int state);
  86. int conn_listener_type_supports_af_unix(int type);
  87. dir_connection_t *dir_connection_new(int socket_family);
  88. or_connection_t *or_connection_new(int type, int socket_family);
  89. edge_connection_t *edge_connection_new(int type, int socket_family);
  90. entry_connection_t *entry_connection_new(int type, int socket_family);
  91. control_connection_t *control_connection_new(int socket_family);
  92. listener_connection_t *listener_connection_new(int type, int socket_family);
  93. connection_t *connection_new(int type, int socket_family);
  94. int connection_init_accepted_conn(connection_t *conn,
  95. const listener_connection_t *listener);
  96. void connection_link_connections(connection_t *conn_a, connection_t *conn_b);
  97. MOCK_DECL(void,connection_free_,(connection_t *conn));
  98. #define connection_free(conn) \
  99. FREE_AND_NULL(connection_t, connection_free_, (conn))
  100. void connection_free_all(void);
  101. void connection_about_to_close_connection(connection_t *conn);
  102. void connection_close_immediate(connection_t *conn);
  103. void connection_mark_for_close_(connection_t *conn,
  104. int line, const char *file);
  105. MOCK_DECL(void, connection_mark_for_close_internal_,
  106. (connection_t *conn, int line, const char *file));
  107. #define connection_mark_for_close(c) \
  108. connection_mark_for_close_((c), __LINE__, SHORT_FILE__)
  109. #define connection_mark_for_close_internal(c) \
  110. connection_mark_for_close_internal_((c), __LINE__, SHORT_FILE__)
  111. /**
  112. * Mark 'c' for close, but try to hold it open until all the data is written.
  113. * Use the _internal versions of connection_mark_for_close; this should be
  114. * called when you either are sure that if this is an or_connection_t the
  115. * controlling channel has been notified (e.g. with
  116. * connection_or_notify_error()), or you actually are the
  117. * connection_or_close_for_error() or connection_or_close_normally function.
  118. * For all other cases, use connection_mark_and_flush() instead, which
  119. * checks for or_connection_t properly, instead. See below.
  120. */
  121. #define connection_mark_and_flush_internal_(c,line,file) \
  122. do { \
  123. connection_t *tmp_conn__ = (c); \
  124. connection_mark_for_close_internal_(tmp_conn__, (line), (file)); \
  125. tmp_conn__->hold_open_until_flushed = 1; \
  126. } while (0)
  127. #define connection_mark_and_flush_internal(c) \
  128. connection_mark_and_flush_internal_((c), __LINE__, SHORT_FILE__)
  129. /**
  130. * Mark 'c' for close, but try to hold it open until all the data is written.
  131. */
  132. #define connection_mark_and_flush_(c,line,file) \
  133. do { \
  134. connection_t *tmp_conn_ = (c); \
  135. if (tmp_conn_->type == CONN_TYPE_OR) { \
  136. log_warn(LD_CHANNEL | LD_BUG, \
  137. "Something tried to close (and flush) an or_connection_t" \
  138. " without going through channels at %s:%d", \
  139. file, line); \
  140. connection_or_close_for_error(TO_OR_CONN(tmp_conn_), 1); \
  141. } else { \
  142. connection_mark_and_flush_internal_(c, line, file); \
  143. } \
  144. } while (0)
  145. #define connection_mark_and_flush(c) \
  146. connection_mark_and_flush_((c), __LINE__, SHORT_FILE__)
  147. void connection_expire_held_open(void);
  148. int connection_connect(connection_t *conn, const char *address,
  149. const tor_addr_t *addr,
  150. uint16_t port, int *socket_error);
  151. #ifdef HAVE_SYS_UN_H
  152. int connection_connect_unix(connection_t *conn, const char *socket_path,
  153. int *socket_error);
  154. #endif /* defined(HAVE_SYS_UN_H) */
  155. /** Maximum size of information that we can fit into SOCKS5 username
  156. or password fields. */
  157. #define MAX_SOCKS5_AUTH_FIELD_SIZE 255
  158. /** Total maximum size of information that we can fit into SOCKS5
  159. username and password fields. */
  160. #define MAX_SOCKS5_AUTH_SIZE_TOTAL 2*MAX_SOCKS5_AUTH_FIELD_SIZE
  161. int connection_proxy_connect(connection_t *conn, int type);
  162. int connection_read_proxy_handshake(connection_t *conn);
  163. void log_failed_proxy_connection(connection_t *conn);
  164. int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
  165. const connection_t *conn);
  166. int retry_all_listeners(smartlist_t *new_conns,
  167. int close_all_noncontrol);
  168. void connection_mark_all_noncontrol_listeners(void);
  169. void connection_mark_all_noncontrol_connections(void);
  170. ssize_t connection_bucket_write_limit(connection_t *conn, time_t now);
  171. int global_write_bucket_low(connection_t *conn, size_t attempt, int priority);
  172. void connection_bucket_init(void);
  173. void connection_bucket_adjust(const or_options_t *options);
  174. void connection_bucket_refill_all(time_t now,
  175. uint32_t now_ts);
  176. void connection_read_bw_exhausted(connection_t *conn, bool is_global_bw);
  177. void connection_write_bw_exhausted(connection_t *conn, bool is_global_bw);
  178. void connection_consider_empty_read_buckets(connection_t *conn);
  179. void connection_consider_empty_write_buckets(connection_t *conn);
  180. int connection_handle_read(connection_t *conn);
  181. int connection_buf_get_bytes(char *string, size_t len, connection_t *conn);
  182. int connection_buf_get_line(connection_t *conn, char *data,
  183. size_t *data_len);
  184. int connection_fetch_from_buf_http(connection_t *conn,
  185. char **headers_out, size_t max_headerlen,
  186. char **body_out, size_t *body_used,
  187. size_t max_bodylen, int force_complete);
  188. int connection_wants_to_flush(connection_t *conn);
  189. int connection_outbuf_too_full(connection_t *conn);
  190. int connection_handle_write(connection_t *conn, int force);
  191. int connection_flush(connection_t *conn);
  192. MOCK_DECL(void, connection_write_to_buf_impl_,
  193. (const char *string, size_t len, connection_t *conn, int zlib));
  194. /* DOCDOC connection_write_to_buf */
  195. static void connection_buf_add(const char *string, size_t len,
  196. connection_t *conn);
  197. static inline void
  198. connection_buf_add(const char *string, size_t len, connection_t *conn)
  199. {
  200. connection_write_to_buf_impl_(string, len, conn, 0);
  201. }
  202. void connection_buf_add_compress(const char *string, size_t len,
  203. dir_connection_t *conn, int done);
  204. void connection_buf_add_buf(connection_t *conn, struct buf_t *buf);
  205. size_t connection_get_inbuf_len(connection_t *conn);
  206. size_t connection_get_outbuf_len(connection_t *conn);
  207. connection_t *connection_get_by_global_id(uint64_t id);
  208. connection_t *connection_get_by_type(int type);
  209. MOCK_DECL(connection_t *,connection_get_by_type_addr_port_purpose,(int type,
  210. const tor_addr_t *addr,
  211. uint16_t port, int purpose));
  212. connection_t *connection_get_by_type_state(int type, int state);
  213. connection_t *connection_get_by_type_state_rendquery(int type, int state,
  214. const char *rendquery);
  215. smartlist_t *connection_list_by_type_state(int type, int state);
  216. smartlist_t *connection_list_by_type_purpose(int type, int purpose);
  217. smartlist_t *connection_dir_list_by_purpose_and_resource(
  218. int purpose,
  219. const char *resource);
  220. smartlist_t *connection_dir_list_by_purpose_resource_and_state(
  221. int purpose,
  222. const char *resource,
  223. int state);
  224. #define CONN_LEN_AND_FREE_TEMPLATE(sl) \
  225. STMT_BEGIN \
  226. int len = smartlist_len(sl); \
  227. smartlist_free(sl); \
  228. return len; \
  229. STMT_END
  230. /** Return a count of directory connections that are fetching the item
  231. * described by <b>purpose</b>/<b>resource</b>. */
  232. static inline int
  233. connection_dir_count_by_purpose_and_resource(
  234. int purpose,
  235. const char *resource)
  236. {
  237. smartlist_t *conns = connection_dir_list_by_purpose_and_resource(
  238. purpose,
  239. resource);
  240. CONN_LEN_AND_FREE_TEMPLATE(conns);
  241. }
  242. /** Return a count of directory connections that are fetching the item
  243. * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. */
  244. static inline int
  245. connection_dir_count_by_purpose_resource_and_state(
  246. int purpose,
  247. const char *resource,
  248. int state)
  249. {
  250. smartlist_t *conns =
  251. connection_dir_list_by_purpose_resource_and_state(
  252. purpose,
  253. resource,
  254. state);
  255. CONN_LEN_AND_FREE_TEMPLATE(conns);
  256. }
  257. #undef CONN_LEN_AND_FREE_TEMPLATE
  258. int any_other_active_or_conns(const or_connection_t *this_conn);
  259. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  260. #define connection_speaks_cells(conn) (((conn)->type == CONN_TYPE_OR) || 0)
  261. int connection_is_listener(connection_t *conn);
  262. int connection_state_is_open(connection_t *conn);
  263. int connection_state_is_connecting(connection_t *conn);
  264. char *alloc_http_authenticator(const char *authenticator);
  265. void assert_connection_ok(connection_t *conn, time_t now);
  266. int connection_or_nonopen_was_started_here(or_connection_t *conn);
  267. void connection_dump_buffer_mem_stats(int severity);
  268. MOCK_DECL(void, clock_skew_warning,
  269. (const connection_t *conn, long apparent_skew, int trusted,
  270. log_domain_mask_t domain, const char *received,
  271. const char *source));
  272. int connection_is_moribund(connection_t *conn);
  273. void connection_check_oos(int n_socks, int failed);
  274. /** Execute the statement <b>stmt</b>, which may log events concerning the
  275. * connection <b>conn</b>. To prevent infinite loops, disable log messages
  276. * being sent to controllers if <b>conn</b> is a control connection.
  277. *
  278. * Stmt must not contain any return or goto statements.
  279. */
  280. #define CONN_LOG_PROTECT(conn, stmt) \
  281. STMT_BEGIN \
  282. int _log_conn_is_control; \
  283. tor_assert(conn); \
  284. _log_conn_is_control = (conn->type == CONN_TYPE_CONTROL); \
  285. if (_log_conn_is_control) \
  286. disable_control_logging(); \
  287. STMT_BEGIN stmt; STMT_END; \
  288. if (_log_conn_is_control) \
  289. enable_control_logging(); \
  290. STMT_END
  291. #ifdef CONNECTION_PRIVATE
  292. STATIC void connection_free_minimal(connection_t *conn);
  293. /* Used only by connection.c and test*.c */
  294. MOCK_DECL(STATIC int,connection_connect_sockaddr,
  295. (connection_t *conn,
  296. const struct sockaddr *sa,
  297. socklen_t sa_len,
  298. const struct sockaddr *bindaddr,
  299. socklen_t bindaddr_len,
  300. int *socket_error));
  301. MOCK_DECL(STATIC void, kill_conn_list_for_oos, (smartlist_t *conns));
  302. MOCK_DECL(STATIC smartlist_t *, pick_oos_victims, (int n));
  303. #endif /* defined(CONNECTION_PRIVATE) */
  304. #endif /* !defined(TOR_CONNECTION_H) */