| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "aws/s3/private/s3_client_impl.h" |
| 7 |
#include "aws/s3/private/s3_meta_request_impl.h" |
| 8 |
#include "aws/s3/private/s3_util.h" |
| 9 |
|
| 10 |
#include <aws/auth/credentials.h> |
| 11 |
#include <aws/common/assert.h> |
| 12 |
#include <aws/common/device_random.h> |
| 13 |
#include <aws/common/string.h> |
| 14 |
#include <aws/http/connection.h> |
| 15 |
#include <aws/http/connection_manager.h> |
| 16 |
#include <aws/io/channel_bootstrap.h> |
| 17 |
#include <aws/io/event_loop.h> |
| 18 |
#include <aws/io/host_resolver.h> |
| 19 |
#include <aws/io/socket.h> |
| 20 |
#include <aws/io/tls_channel_handler.h> |
| 21 |
#include <aws/io/uri.h> |
| 22 |
|
| 23 |
#include <inttypes.h> |
| 24 |
|
| 25 |
static const uint32_t s_connection_timeout_ms = 3000; |
| 26 |
static const uint32_t s_http_port = 80; |
| 27 |
static const uint32_t s_https_port = 443; |
| 28 |
|
| 29 |
static void s_s3_endpoint_on_host_resolver_address_resolved( |
| 30 |
struct aws_host_resolver *resolver, |
| 31 |
const struct aws_string *host_name, |
| 32 |
int err_code, |
| 33 |
const struct aws_array_list *host_addresses, |
| 34 |
void *user_data); |
| 35 |
|
| 36 |
static struct aws_http_connection_manager *s_s3_endpoint_create_http_connection_manager( |
| 37 |
struct aws_s3_endpoint *endpoint, |
| 38 |
const struct aws_string *host_name, |
| 39 |
struct aws_client_bootstrap *client_bootstrap, |
| 40 |
const struct aws_tls_connection_options *tls_connection_options, |
| 41 |
uint32_t max_connections, |
| 42 |
uint32_t port, |
| 43 |
const struct aws_http_proxy_config *proxy_config, |
| 44 |
const struct proxy_env_var_settings *proxy_ev_settings, |
| 45 |
uint32_t connect_timeout_ms, |
| 46 |
const struct aws_s3_tcp_keep_alive_options *tcp_keep_alive_options, |
| 47 |
const struct aws_http_connection_monitoring_options *monitoring_options, |
| 48 |
const struct aws_byte_cursor *network_interface_names_array, |
| 49 |
size_t num_network_interface_names); |
| 50 |
|
| 51 |
static void s_s3_endpoint_http_connection_manager_shutdown_callback(void *user_data); |
| 52 |
|
| 53 |
static void s_s3_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock); |
| 54 |
|
| 55 |
static void s_s3_endpoint_release(struct aws_s3_endpoint *endpoint); |
| 56 |
|
| 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 |
|
| 62 |
static const struct aws_s3_endpoint_system_vtable *s_s3_endpoint_system_vtable = &s_s3_endpoint_default_system_vtable; |
| 63 |
|
| 64 |
void aws_s3_endpoint_set_system_vtable(const struct aws_s3_endpoint_system_vtable *vtable) { |
| 65 |
s_s3_endpoint_system_vtable = vtable; |
| 66 |
} |
| 67 |
|
| 68 |
struct aws_s3_endpoint *aws_s3_endpoint_new( |
| 69 |
struct aws_allocator *allocator, |
| 70 |
const struct aws_s3_endpoint_options *options) { |
| 71 |
AWS_PRECONDITION(allocator); |
| 72 |
AWS_PRECONDITION(options); |
| 73 |
AWS_PRECONDITION(options->host_name); |
| 74 |
|
| 75 |
struct aws_s3_endpoint *endpoint = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_endpoint)); |
| 76 |
endpoint->client_synced_data.ref_count = 1; |
| 77 |
|
| 78 |
endpoint->allocator = allocator; |
| 79 |
endpoint->host_name = options->host_name; |
| 80 |
|
| 81 |
struct aws_host_resolution_config host_resolver_config; |
| 82 |
AWS_ZERO_STRUCT(host_resolver_config); |
| 83 |
host_resolver_config.impl = aws_default_dns_resolve; |
| 84 |
host_resolver_config.max_ttl = options->dns_host_address_ttl_seconds; |
| 85 |
host_resolver_config.impl_data = NULL; |
| 86 |
|
| 87 |
if (aws_host_resolver_resolve_host( |
| 88 |
options->client_bootstrap->host_resolver, |
| 89 |
endpoint->host_name, |
| 90 |
s_s3_endpoint_on_host_resolver_address_resolved, |
| 91 |
&host_resolver_config, |
| 92 |
NULL)) { |
| 93 |
|
| 94 |
AWS_LOGF_ERROR( |
| 95 |
AWS_LS_S3_ENDPOINT, |
| 96 |
"id=%p: Error trying to resolve host for endpoint %s", |
| 97 |
(void *)endpoint, |
| 98 |
(const char *)endpoint->host_name->bytes); |
| 99 |
|
| 100 |
goto error_cleanup; |
| 101 |
} |
| 102 |
|
| 103 |
endpoint->http_connection_manager = s_s3_endpoint_create_http_connection_manager( |
| 104 |
endpoint, |
| 105 |
options->host_name, |
| 106 |
options->client_bootstrap, |
| 107 |
options->tls_connection_options, |
| 108 |
options->max_connections, |
| 109 |
options->port, |
| 110 |
options->proxy_config, |
| 111 |
options->proxy_ev_settings, |
| 112 |
options->connect_timeout_ms, |
| 113 |
options->tcp_keep_alive_options, |
| 114 |
options->monitoring_options, |
| 115 |
options->network_interface_names_array, |
| 116 |
options->num_network_interface_names); |
| 117 |
|
| 118 |
if (endpoint->http_connection_manager == NULL) { |
| 119 |
goto error_cleanup; |
| 120 |
} |
| 121 |
|
| 122 |
endpoint->client = options->client; |
| 123 |
|
| 124 |
return endpoint; |
| 125 |
|
| 126 |
error_cleanup: |
| 127 |
|
| 128 |
aws_mem_release(allocator, endpoint); |
| 129 |
|
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
static struct aws_http_connection_manager *s_s3_endpoint_create_http_connection_manager( |
| 134 |
struct aws_s3_endpoint *endpoint, |
| 135 |
const struct aws_string *host_name, |
| 136 |
struct aws_client_bootstrap *client_bootstrap, |
| 137 |
const struct aws_tls_connection_options *tls_connection_options, |
| 138 |
uint32_t max_connections, |
| 139 |
uint32_t port, |
| 140 |
const struct aws_http_proxy_config *proxy_config, |
| 141 |
const struct proxy_env_var_settings *proxy_ev_settings, |
| 142 |
uint32_t connect_timeout_ms, |
| 143 |
const struct aws_s3_tcp_keep_alive_options *tcp_keep_alive_options, |
| 144 |
const struct aws_http_connection_monitoring_options *monitoring_options, |
| 145 |
const struct aws_byte_cursor *network_interface_names_array, |
| 146 |
size_t num_network_interface_names) { |
| 147 |
|
| 148 |
AWS_PRECONDITION(endpoint); |
| 149 |
AWS_PRECONDITION(client_bootstrap); |
| 150 |
AWS_PRECONDITION(host_name); |
| 151 |
|
| 152 |
struct aws_byte_cursor host_name_cursor = aws_byte_cursor_from_string(host_name); |
| 153 |
|
| 154 |
|
| 155 |
struct aws_socket_options socket_options; |
| 156 |
AWS_ZERO_STRUCT(socket_options); |
| 157 |
socket_options.type = AWS_SOCKET_STREAM; |
| 158 |
socket_options.domain = AWS_SOCKET_IPV4; |
| 159 |
socket_options.connect_timeout_ms = connect_timeout_ms == 0 ? s_connection_timeout_ms : connect_timeout_ms; |
| 160 |
if (tcp_keep_alive_options != NULL) { |
| 161 |
socket_options.keepalive = true; |
| 162 |
socket_options.keep_alive_interval_sec = tcp_keep_alive_options->keep_alive_interval_sec; |
| 163 |
socket_options.keep_alive_timeout_sec = tcp_keep_alive_options->keep_alive_timeout_sec; |
| 164 |
socket_options.keep_alive_max_failed_probes = tcp_keep_alive_options->keep_alive_max_failed_probes; |
| 165 |
} |
| 166 |
struct proxy_env_var_settings proxy_ev_settings_default; |
| 167 |
|
| 168 |
if (proxy_ev_settings == NULL) { |
| 169 |
AWS_ZERO_STRUCT(proxy_ev_settings_default); |
| 170 |
proxy_ev_settings_default.env_var_type = AWS_HPEV_ENABLE; |
| 171 |
proxy_ev_settings = &proxy_ev_settings_default; |
| 172 |
} |
| 173 |
|
| 174 |
struct aws_http_connection_manager_options manager_options; |
| 175 |
AWS_ZERO_STRUCT(manager_options); |
| 176 |
manager_options.bootstrap = client_bootstrap; |
| 177 |
manager_options.initial_window_size = SIZE_MAX; |
| 178 |
manager_options.socket_options = &socket_options; |
| 179 |
manager_options.host = host_name_cursor; |
| 180 |
manager_options.max_connections = max_connections; |
| 181 |
manager_options.shutdown_complete_callback = s_s3_endpoint_http_connection_manager_shutdown_callback; |
| 182 |
manager_options.shutdown_complete_user_data = endpoint; |
| 183 |
manager_options.proxy_ev_settings = proxy_ev_settings; |
| 184 |
manager_options.network_interface_names_array = network_interface_names_array; |
| 185 |
manager_options.num_network_interface_names = num_network_interface_names; |
| 186 |
if (monitoring_options != NULL) { |
| 187 |
manager_options.monitoring_options = monitoring_options; |
| 188 |
} |
| 189 |
|
| 190 |
struct aws_http_proxy_options proxy_options; |
| 191 |
if (proxy_config != NULL) { |
| 192 |
aws_http_proxy_options_init_from_config(&proxy_options, proxy_config); |
| 193 |
manager_options.proxy_options = &proxy_options; |
| 194 |
} |
| 195 |
|
| 196 |
struct aws_tls_connection_options *manager_tls_options = NULL; |
| 197 |
|
| 198 |
if (tls_connection_options != NULL) { |
| 199 |
manager_tls_options = aws_mem_calloc(endpoint->allocator, 1, sizeof(struct aws_tls_connection_options)); |
| 200 |
aws_tls_connection_options_copy(manager_tls_options, tls_connection_options); |
| 201 |
|
| 202 |
|
| 203 |
if (manager_tls_options->server_name != NULL) { |
| 204 |
aws_string_destroy(manager_tls_options->server_name); |
| 205 |
manager_tls_options->server_name = NULL; |
| 206 |
} |
| 207 |
|
| 208 |
aws_tls_connection_options_set_server_name(manager_tls_options, endpoint->allocator, &host_name_cursor); |
| 209 |
|
| 210 |
manager_options.tls_connection_options = manager_tls_options; |
| 211 |
manager_options.port = port == 0 ? s_https_port : port; |
| 212 |
} else { |
| 213 |
manager_options.port = port == 0 ? s_http_port : port; |
| 214 |
} |
| 215 |
|
| 216 |
struct aws_http_connection_manager *http_connection_manager = |
| 217 |
aws_http_connection_manager_new(endpoint->allocator, &manager_options); |
| 218 |
|
| 219 |
if (manager_tls_options != NULL) { |
| 220 |
aws_tls_connection_options_clean_up(manager_tls_options); |
| 221 |
aws_mem_release(endpoint->allocator, manager_tls_options); |
| 222 |
manager_tls_options = NULL; |
| 223 |
} |
| 224 |
|
| 225 |
if (http_connection_manager == NULL) { |
| 226 |
AWS_LOGF_ERROR(AWS_LS_S3_ENDPOINT, "id=%p: Could not create http connection manager.", (void *)endpoint); |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
|
| 230 |
AWS_LOGF_DEBUG( |
| 231 |
AWS_LS_S3_ENDPOINT, |
| 232 |
"id=%p: Created connection manager %p for endpoint", |
| 233 |
(void *)endpoint, |
| 234 |
(void *)http_connection_manager); |
| 235 |
|
| 236 |
return http_connection_manager; |
| 237 |
} |
| 238 |
|
| 239 |
struct aws_s3_endpoint *aws_s3_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock) { |
| 240 |
if (endpoint) { |
| 241 |
s_s3_endpoint_system_vtable->acquire(endpoint, already_holding_lock); |
| 242 |
} |
| 243 |
return endpoint; |
| 244 |
} |
| 245 |
|
| 246 |
static void s_s3_endpoint_acquire(struct aws_s3_endpoint *endpoint, bool already_holding_lock) { |
| 247 |
AWS_PRECONDITION(endpoint); |
| 248 |
|
| 249 |
if (!already_holding_lock) { |
| 250 |
aws_s3_client_lock_synced_data(endpoint->client); |
| 251 |
} |
| 252 |
|
| 253 |
++endpoint->client_synced_data.ref_count; |
| 254 |
|
| 255 |
if (!already_holding_lock) { |
| 256 |
aws_s3_client_unlock_synced_data(endpoint->client); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
void aws_s3_endpoint_release(struct aws_s3_endpoint *endpoint) { |
| 261 |
if (endpoint) { |
| 262 |
s_s3_endpoint_system_vtable->release(endpoint); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
static void s_s3_endpoint_release(struct aws_s3_endpoint *endpoint) { |
| 267 |
AWS_PRECONDITION(endpoint); |
| 268 |
AWS_PRECONDITION(endpoint->client); |
| 269 |
|
| 270 |
|
| 271 |
aws_s3_client_lock_synced_data(endpoint->client); |
| 272 |
|
| 273 |
bool should_destroy = endpoint->client_synced_data.ref_count == 1 && !endpoint->client->synced_data.active; |
| 274 |
if (should_destroy) { |
| 275 |
aws_hash_table_remove(&endpoint->client->synced_data.endpoints, endpoint->host_name, NULL, NULL); |
| 276 |
} |
| 277 |
--endpoint->client_synced_data.ref_count; |
| 278 |
|
| 279 |
aws_s3_client_unlock_synced_data(endpoint->client); |
| 280 |
|
| 281 |
|
| 282 |
if (should_destroy) { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
aws_s3_endpoint_destroy(endpoint); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
void aws_s3_endpoint_destroy(struct aws_s3_endpoint *endpoint) { |
| 294 |
AWS_PRECONDITION(endpoint); |
| 295 |
AWS_PRECONDITION(endpoint->http_connection_manager); |
| 296 |
|
| 297 |
AWS_FATAL_ASSERT(endpoint->client_synced_data.ref_count == 0); |
| 298 |
|
| 299 |
struct aws_http_connection_manager *http_connection_manager = endpoint->http_connection_manager; |
| 300 |
endpoint->http_connection_manager = NULL; |
| 301 |
|
| 302 |
|
| 303 |
aws_http_connection_manager_release(http_connection_manager); |
| 304 |
} |
| 305 |
|
| 306 |
static void s_s3_endpoint_http_connection_manager_shutdown_callback(void *user_data) { |
| 307 |
struct aws_s3_endpoint *endpoint = user_data; |
| 308 |
AWS_ASSERT(endpoint); |
| 309 |
|
| 310 |
struct aws_s3_client *client = endpoint->client; |
| 311 |
|
| 312 |
aws_mem_release(endpoint->allocator, endpoint); |
| 313 |
|
| 314 |
client->vtable->endpoint_shutdown_callback(client); |
| 315 |
} |
| 316 |
|
| 317 |
static void s_s3_endpoint_on_host_resolver_address_resolved( |
| 318 |
struct aws_host_resolver *resolver, |
| 319 |
const struct aws_string *host_name, |
| 320 |
int err_code, |
| 321 |
const struct aws_array_list *host_addresses, |
| 322 |
void *user_data) { |
| 323 |
(void)resolver; |
| 324 |
(void)host_name; |
| 325 |
(void)err_code; |
| 326 |
(void)host_addresses; |
| 327 |
(void)user_data; |
| 328 |
|
| 329 |
} |