s3_endpoint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include "aws/s3/private/s3_auto_ranged_get.h"
  6. #include "aws/s3/private/s3_auto_ranged_put.h"
  7. #include "aws/s3/private/s3_client_impl.h"
  8. #include "aws/s3/private/s3_default_meta_request.h"
  9. #include "aws/s3/private/s3_meta_request_impl.h"
  10. #include "aws/s3/private/s3_util.h"
  11. #include <aws/auth/credentials.h>
  12. #include <aws/common/assert.h>
  13. #include <aws/common/atomics.h>
  14. #include <aws/common/clock.h>
  15. #include <aws/common/device_random.h>
  16. #include <aws/common/environment.h>
  17. #include <aws/common/string.h>
  18. #include <aws/common/system_info.h>
  19. #include <aws/http/connection.h>
  20. #include <aws/http/connection_manager.h>
  21. #include <aws/http/request_response.h>
  22. #include <aws/io/channel_bootstrap.h>
  23. #include <aws/io/event_loop.h>
  24. #include <aws/io/host_resolver.h>
  25. #include <aws/io/retry_strategy.h>
  26. #include <aws/io/socket.h>
  27. #include <aws/io/stream.h>
  28. #include <aws/io/tls_channel_handler.h>
  29. #include <aws/io/uri.h>
  30. #include <inttypes.h>
  31. #include <math.h>
  32. static const uint32_t s_connection_timeout_ms = 3000;
  33. static const uint16_t s_http_port = 80;
  34. static const uint16_t s_https_port = 443;
  35. static void s_s3_endpoint_on_host_resolver_address_resolved(
  36. struct aws_host_resolver *resolver,
  37. const struct aws_string *host_name,
  38. int err_code,
  39. const struct aws_array_list *host_addresses,
  40. void *user_data);
  41. static struct aws_http_connection_manager *s_s3_endpoint_create_http_connection_manager(
  42. struct aws_s3_endpoint *endpoint,
  43. const struct aws_string *host_name,
  44. struct aws_client_bootstrap *client_bootstrap,
  45. const struct aws_tls_connection_options *tls_connection_options,
  46. uint32_t max_connections,
  47. uint16_t port,
  48. const struct aws_http_proxy_config *proxy_config,
  49. const struct proxy_env_var_settings *proxy_ev_settings,
  50. uint32_t connect_timeout_ms,
  51. const struct aws_s3_tcp_keep_alive_options *tcp_keep_alive_options,
  52. const struct aws_http_connection_monitoring_options *monitoring_options);
  53. static void s_s3_endpoint_http_connection_manager_shutdown_callback(void *user_data);
  54. static void s_s3_endpoint_ref_count_zero(struct aws_s3_endpoint *endpoint);
  55. static void s_s3_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock);
  56. static void s_s3_endpoint_release(struct aws_s3_endpoint *endpoint);
  57. static const struct aws_s3_endpoint_system_vtable s_s3_endpoint_default_system_vtable = {
  58. .acquire = s_s3_endpoint_acquire,
  59. .release = s_s3_endpoint_release,
  60. };
  61. static const struct aws_s3_endpoint_system_vtable *s_s3_endpoint_system_vtable = &s_s3_endpoint_default_system_vtable;
  62. void aws_s3_endpoint_set_system_vtable(const struct aws_s3_endpoint_system_vtable *vtable) {
  63. s_s3_endpoint_system_vtable = vtable;
  64. }
  65. struct aws_s3_endpoint *aws_s3_endpoint_new(
  66. struct aws_allocator *allocator,
  67. const struct aws_s3_endpoint_options *options) {
  68. AWS_PRECONDITION(allocator);
  69. AWS_PRECONDITION(options);
  70. AWS_PRECONDITION(options->host_name);
  71. struct aws_s3_endpoint *endpoint = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_endpoint));
  72. endpoint->client_synced_data.ref_count = 1;
  73. endpoint->allocator = allocator;
  74. endpoint->host_name = options->host_name;
  75. struct aws_host_resolution_config host_resolver_config;
  76. AWS_ZERO_STRUCT(host_resolver_config);
  77. host_resolver_config.impl = aws_default_dns_resolve;
  78. host_resolver_config.max_ttl = options->dns_host_address_ttl_seconds;
  79. host_resolver_config.impl_data = NULL;
  80. if (aws_host_resolver_resolve_host(
  81. options->client_bootstrap->host_resolver,
  82. endpoint->host_name,
  83. s_s3_endpoint_on_host_resolver_address_resolved,
  84. &host_resolver_config,
  85. NULL)) {
  86. AWS_LOGF_ERROR(
  87. AWS_LS_S3_ENDPOINT,
  88. "id=%p: Error trying to resolve host for endpoint %s",
  89. (void *)endpoint,
  90. (const char *)endpoint->host_name->bytes);
  91. goto error_cleanup;
  92. }
  93. endpoint->http_connection_manager = s_s3_endpoint_create_http_connection_manager(
  94. endpoint,
  95. options->host_name,
  96. options->client_bootstrap,
  97. options->tls_connection_options,
  98. options->max_connections,
  99. options->port,
  100. options->proxy_config,
  101. options->proxy_ev_settings,
  102. options->connect_timeout_ms,
  103. options->tcp_keep_alive_options,
  104. options->monitoring_options);
  105. if (endpoint->http_connection_manager == NULL) {
  106. goto error_cleanup;
  107. }
  108. endpoint->client = options->client;
  109. return endpoint;
  110. error_cleanup:
  111. aws_string_destroy(options->host_name);
  112. aws_mem_release(allocator, endpoint);
  113. return NULL;
  114. }
  115. static struct aws_http_connection_manager *s_s3_endpoint_create_http_connection_manager(
  116. struct aws_s3_endpoint *endpoint,
  117. const struct aws_string *host_name,
  118. struct aws_client_bootstrap *client_bootstrap,
  119. const struct aws_tls_connection_options *tls_connection_options,
  120. uint32_t max_connections,
  121. uint16_t port,
  122. const struct aws_http_proxy_config *proxy_config,
  123. const struct proxy_env_var_settings *proxy_ev_settings,
  124. uint32_t connect_timeout_ms,
  125. const struct aws_s3_tcp_keep_alive_options *tcp_keep_alive_options,
  126. const struct aws_http_connection_monitoring_options *monitoring_options) {
  127. AWS_PRECONDITION(endpoint);
  128. AWS_PRECONDITION(client_bootstrap);
  129. AWS_PRECONDITION(host_name);
  130. struct aws_byte_cursor host_name_cursor = aws_byte_cursor_from_string(host_name);
  131. /* Try to set up an HTTP connection manager. */
  132. struct aws_socket_options socket_options;
  133. AWS_ZERO_STRUCT(socket_options);
  134. socket_options.type = AWS_SOCKET_STREAM;
  135. socket_options.domain = AWS_SOCKET_IPV4;
  136. socket_options.connect_timeout_ms = connect_timeout_ms == 0 ? s_connection_timeout_ms : connect_timeout_ms;
  137. if (tcp_keep_alive_options != NULL) {
  138. socket_options.keepalive = true;
  139. socket_options.keep_alive_interval_sec = tcp_keep_alive_options->keep_alive_interval_sec;
  140. socket_options.keep_alive_timeout_sec = tcp_keep_alive_options->keep_alive_timeout_sec;
  141. socket_options.keep_alive_max_failed_probes = tcp_keep_alive_options->keep_alive_max_failed_probes;
  142. }
  143. struct proxy_env_var_settings proxy_ev_settings_default;
  144. /* Turn on environment variable for proxy by default */
  145. if (proxy_ev_settings == NULL) {
  146. AWS_ZERO_STRUCT(proxy_ev_settings_default);
  147. proxy_ev_settings_default.env_var_type = AWS_HPEV_ENABLE;
  148. proxy_ev_settings = &proxy_ev_settings_default;
  149. }
  150. struct aws_http_connection_manager_options manager_options;
  151. AWS_ZERO_STRUCT(manager_options);
  152. manager_options.bootstrap = client_bootstrap;
  153. manager_options.initial_window_size = SIZE_MAX;
  154. manager_options.socket_options = &socket_options;
  155. manager_options.host = host_name_cursor;
  156. manager_options.max_connections = max_connections;
  157. manager_options.shutdown_complete_callback = s_s3_endpoint_http_connection_manager_shutdown_callback;
  158. manager_options.shutdown_complete_user_data = endpoint;
  159. manager_options.proxy_ev_settings = proxy_ev_settings;
  160. if (monitoring_options != NULL) {
  161. manager_options.monitoring_options = monitoring_options;
  162. }
  163. struct aws_http_proxy_options proxy_options;
  164. if (proxy_config != NULL) {
  165. aws_http_proxy_options_init_from_config(&proxy_options, proxy_config);
  166. manager_options.proxy_options = &proxy_options;
  167. }
  168. struct aws_tls_connection_options *manager_tls_options = NULL;
  169. if (tls_connection_options != NULL) {
  170. manager_tls_options = aws_mem_calloc(endpoint->allocator, 1, sizeof(struct aws_tls_connection_options));
  171. aws_tls_connection_options_copy(manager_tls_options, tls_connection_options);
  172. /* TODO fix this in the actual aws_tls_connection_options_set_server_name function. */
  173. if (manager_tls_options->server_name != NULL) {
  174. aws_string_destroy(manager_tls_options->server_name);
  175. manager_tls_options->server_name = NULL;
  176. }
  177. aws_tls_connection_options_set_server_name(manager_tls_options, endpoint->allocator, &host_name_cursor);
  178. manager_options.tls_connection_options = manager_tls_options;
  179. manager_options.port = port == 0 ? s_https_port : port;
  180. } else {
  181. manager_options.port = port == 0 ? s_http_port : port;
  182. }
  183. struct aws_http_connection_manager *http_connection_manager =
  184. aws_http_connection_manager_new(endpoint->allocator, &manager_options);
  185. if (manager_tls_options != NULL) {
  186. aws_tls_connection_options_clean_up(manager_tls_options);
  187. aws_mem_release(endpoint->allocator, manager_tls_options);
  188. manager_tls_options = NULL;
  189. }
  190. if (http_connection_manager == NULL) {
  191. AWS_LOGF_ERROR(AWS_LS_S3_ENDPOINT, "id=%p: Could not create http connection manager.", (void *)endpoint);
  192. return NULL;
  193. }
  194. AWS_LOGF_DEBUG(
  195. AWS_LS_S3_ENDPOINT,
  196. "id=%p: Created connection manager %p for endpoint",
  197. (void *)endpoint,
  198. (void *)http_connection_manager);
  199. return http_connection_manager;
  200. }
  201. struct aws_s3_endpoint *aws_s3_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock) {
  202. if (endpoint) {
  203. s_s3_endpoint_system_vtable->acquire(endpoint, already_holding_lock);
  204. }
  205. return endpoint;
  206. }
  207. static void s_s3_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock) {
  208. AWS_PRECONDITION(endpoint);
  209. if (!already_holding_lock) {
  210. aws_s3_client_lock_synced_data(endpoint->client);
  211. }
  212. AWS_ASSERT(endpoint->client_synced_data.ref_count > 0);
  213. ++endpoint->client_synced_data.ref_count;
  214. if (!already_holding_lock) {
  215. aws_s3_client_unlock_synced_data(endpoint->client);
  216. }
  217. }
  218. void aws_s3_endpoint_release(struct aws_s3_endpoint *endpoint) {
  219. if (endpoint) {
  220. s_s3_endpoint_system_vtable->release(endpoint);
  221. }
  222. }
  223. static void s_s3_endpoint_release(struct aws_s3_endpoint *endpoint) {
  224. AWS_PRECONDITION(endpoint);
  225. AWS_PRECONDITION(endpoint->client);
  226. /* BEGIN CRITICAL SECTION */
  227. aws_s3_client_lock_synced_data(endpoint->client);
  228. bool should_destroy = (endpoint->client_synced_data.ref_count == 1);
  229. if (should_destroy) {
  230. aws_hash_table_remove(&endpoint->client->synced_data.endpoints, endpoint->host_name, NULL, NULL);
  231. } else {
  232. --endpoint->client_synced_data.ref_count;
  233. }
  234. aws_s3_client_unlock_synced_data(endpoint->client);
  235. /* END CRITICAL SECTION */
  236. if (should_destroy) {
  237. /* The endpoint may have async cleanup to do (connection manager).
  238. * When that's all done we'll invoke a completion callback.
  239. * Since it's a crime to hold a lock while invoking a callback,
  240. * we make sure that we've released the client's lock before proceeding... */
  241. s_s3_endpoint_ref_count_zero(endpoint);
  242. }
  243. }
  244. static void s_s3_endpoint_ref_count_zero(struct aws_s3_endpoint *endpoint) {
  245. AWS_PRECONDITION(endpoint);
  246. AWS_PRECONDITION(endpoint->http_connection_manager);
  247. struct aws_http_connection_manager *http_connection_manager = endpoint->http_connection_manager;
  248. endpoint->http_connection_manager = NULL;
  249. /* Cleanup continues once the manager's shutdown callback is invoked */
  250. aws_http_connection_manager_release(http_connection_manager);
  251. }
  252. static void s_s3_endpoint_http_connection_manager_shutdown_callback(void *user_data) {
  253. struct aws_s3_endpoint *endpoint = user_data;
  254. AWS_ASSERT(endpoint);
  255. struct aws_s3_client *client = endpoint->client;
  256. aws_mem_release(endpoint->allocator, endpoint);
  257. client->vtable->endpoint_shutdown_callback(client);
  258. }
  259. static void s_s3_endpoint_on_host_resolver_address_resolved(
  260. struct aws_host_resolver *resolver,
  261. const struct aws_string *host_name,
  262. int err_code,
  263. const struct aws_array_list *host_addresses,
  264. void *user_data) {
  265. (void)resolver;
  266. (void)host_name;
  267. (void)err_code;
  268. (void)host_addresses;
  269. (void)user_data;
  270. /* DO NOT add any logic here, unless you also ensure the endpoint lives long enough */
  271. }