| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/io/channel_bootstrap.h> |
| 6 |
|
| 7 |
#include <aws/common/ref_count.h> |
| 8 |
#include <aws/common/string.h> |
| 9 |
#include <aws/io/event_loop.h> |
| 10 |
#include <aws/io/logging.h> |
| 11 |
#include <aws/io/private/tls_channel_handler_shared.h> |
| 12 |
#include <aws/io/socket.h> |
| 13 |
#include <aws/io/socket_channel_handler.h> |
| 14 |
#include <aws/io/tls_channel_handler.h> |
| 15 |
|
| 16 |
#ifdef _MSC_VER |
| 17 |
|
| 18 |
# pragma warning(disable : 4204) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
# pragma warning(disable : 4221) |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
#define SETUP_SOCKET_SHUTDOWN_CALLBACKS(allocator, socket, struct_type, init_function, ...) \ |
| 27 |
struct struct_type *shutdown_args = struct_type##_new(allocator, __VA_ARGS__); \ |
| 28 |
aws_socket_set_cleanup_complete_callback(socket, init_function, shutdown_args); |
| 29 |
|
| 30 |
static void s_client_bootstrap_destroy_impl(void *user_data) { |
| 31 |
struct aws_client_bootstrap *bootstrap = user_data; |
| 32 |
AWS_ASSERT(bootstrap); |
| 33 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: bootstrap destroying", (void *)bootstrap); |
| 34 |
aws_client_bootstrap_shutdown_complete_fn *on_shutdown_complete = bootstrap->on_shutdown_complete; |
| 35 |
void *shutdown_user_data = bootstrap->user_data; |
| 36 |
|
| 37 |
aws_event_loop_group_release(bootstrap->event_loop_group); |
| 38 |
aws_host_resolver_release(bootstrap->host_resolver); |
| 39 |
|
| 40 |
aws_mem_release(bootstrap->allocator, bootstrap); |
| 41 |
|
| 42 |
if (on_shutdown_complete) { |
| 43 |
on_shutdown_complete(shutdown_user_data); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
struct aws_client_bootstrap *aws_client_bootstrap_acquire(struct aws_client_bootstrap *bootstrap) { |
| 48 |
if (bootstrap != NULL) { |
| 49 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: acquiring bootstrap reference", (void *)bootstrap); |
| 50 |
aws_ref_count_acquire(&bootstrap->ref_count); |
| 51 |
} |
| 52 |
|
| 53 |
return bootstrap; |
| 54 |
} |
| 55 |
|
| 56 |
void aws_client_bootstrap_release(struct aws_client_bootstrap *bootstrap) { |
| 57 |
if (bootstrap != NULL) { |
| 58 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: releasing bootstrap reference", (void *)bootstrap); |
| 59 |
aws_ref_count_release(&bootstrap->ref_count); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
struct aws_client_bootstrap *aws_client_bootstrap_new( |
| 64 |
struct aws_allocator *allocator, |
| 65 |
const struct aws_client_bootstrap_options *options) { |
| 66 |
AWS_ASSERT(allocator); |
| 67 |
AWS_ASSERT(options); |
| 68 |
AWS_ASSERT(options->event_loop_group); |
| 69 |
|
| 70 |
struct aws_client_bootstrap *bootstrap = aws_mem_calloc(allocator, 1, sizeof(struct aws_client_bootstrap)); |
| 71 |
if (!bootstrap) { |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
AWS_LOGF_INFO( |
| 76 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 77 |
"id=%p: Initializing client bootstrap with event-loop group %p", |
| 78 |
(void *)bootstrap, |
| 79 |
(void *)options->event_loop_group); |
| 80 |
|
| 81 |
bootstrap->allocator = allocator; |
| 82 |
bootstrap->event_loop_group = aws_event_loop_group_acquire(options->event_loop_group); |
| 83 |
bootstrap->on_protocol_negotiated = NULL; |
| 84 |
aws_ref_count_init( |
| 85 |
&bootstrap->ref_count, bootstrap, (aws_simple_completion_callback *)s_client_bootstrap_destroy_impl); |
| 86 |
bootstrap->host_resolver = aws_host_resolver_acquire(options->host_resolver); |
| 87 |
bootstrap->on_shutdown_complete = options->on_shutdown_complete; |
| 88 |
bootstrap->user_data = options->user_data; |
| 89 |
|
| 90 |
if (options->host_resolution_config) { |
| 91 |
bootstrap->host_resolver_config = *options->host_resolution_config; |
| 92 |
} else { |
| 93 |
bootstrap->host_resolver_config = aws_host_resolver_init_default_resolution_config(); |
| 94 |
} |
| 95 |
|
| 96 |
return bootstrap; |
| 97 |
} |
| 98 |
|
| 99 |
int aws_client_bootstrap_set_alpn_callback( |
| 100 |
struct aws_client_bootstrap *bootstrap, |
| 101 |
aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated) { |
| 102 |
AWS_ASSERT(on_protocol_negotiated); |
| 103 |
|
| 104 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: Setting ALPN callback", (void *)bootstrap); |
| 105 |
bootstrap->on_protocol_negotiated = on_protocol_negotiated; |
| 106 |
return AWS_OP_SUCCESS; |
| 107 |
} |
| 108 |
|
| 109 |
struct client_channel_data { |
| 110 |
struct aws_channel *channel; |
| 111 |
struct aws_socket *socket; |
| 112 |
struct aws_tls_connection_options tls_options; |
| 113 |
aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated; |
| 114 |
aws_tls_on_data_read_fn *user_on_data_read; |
| 115 |
aws_tls_on_negotiation_result_fn *user_on_negotiation_result; |
| 116 |
aws_tls_on_error_fn *user_on_error; |
| 117 |
void *tls_user_data; |
| 118 |
bool use_tls; |
| 119 |
}; |
| 120 |
|
| 121 |
struct client_connection_args { |
| 122 |
struct aws_client_bootstrap *bootstrap; |
| 123 |
aws_client_bootstrap_on_channel_event_fn *creation_callback; |
| 124 |
aws_client_bootstrap_on_channel_event_fn *setup_callback; |
| 125 |
aws_client_bootstrap_on_channel_event_fn *shutdown_callback; |
| 126 |
struct client_channel_data channel_data; |
| 127 |
struct aws_socket_options outgoing_options; |
| 128 |
uint32_t outgoing_port; |
| 129 |
struct aws_string *host_name; |
| 130 |
void *user_data; |
| 131 |
uint8_t addresses_count; |
| 132 |
uint8_t failed_count; |
| 133 |
bool connection_chosen; |
| 134 |
bool setup_called; |
| 135 |
bool enable_read_back_pressure; |
| 136 |
struct aws_event_loop *requested_event_loop; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
int tls_error_code; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
struct aws_ref_count ref_count; |
| 152 |
}; |
| 153 |
|
| 154 |
static struct client_connection_args *s_client_connection_args_acquire(struct client_connection_args *args) { |
| 155 |
if (args != NULL) { |
| 156 |
AWS_LOGF_TRACE(AWS_LS_IO_CHANNEL_BOOTSTRAP, "acquiring client connection args, args=%p", (void *)args); |
| 157 |
aws_ref_count_acquire(&args->ref_count); |
| 158 |
} |
| 159 |
|
| 160 |
return args; |
| 161 |
} |
| 162 |
|
| 163 |
static void s_client_connection_args_destroy(void *user_data) { |
| 164 |
struct client_connection_args *args = user_data; |
| 165 |
AWS_ASSERT(args); |
| 166 |
AWS_LOGF_TRACE(AWS_LS_IO_CHANNEL_BOOTSTRAP, "destroying client connection args, args=%p", (void *)args); |
| 167 |
|
| 168 |
struct aws_allocator *allocator = args->bootstrap->allocator; |
| 169 |
aws_client_bootstrap_release(args->bootstrap); |
| 170 |
if (args->host_name) { |
| 171 |
aws_string_destroy(args->host_name); |
| 172 |
} |
| 173 |
|
| 174 |
if (args->channel_data.use_tls) { |
| 175 |
aws_tls_connection_options_clean_up(&args->channel_data.tls_options); |
| 176 |
} |
| 177 |
|
| 178 |
aws_mem_release(allocator, args); |
| 179 |
} |
| 180 |
|
| 181 |
static void s_client_connection_args_release(struct client_connection_args *args) { |
| 182 |
if (args != NULL) { |
| 183 |
AWS_LOGF_TRACE(AWS_LS_IO_CHANNEL_BOOTSTRAP, "releasing client connection args, args=%p", (void *)args); |
| 184 |
aws_ref_count_release(&args->ref_count); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static struct aws_event_loop *s_get_connection_event_loop(struct client_connection_args *args) { |
| 189 |
if (args == NULL) { |
| 190 |
return NULL; |
| 191 |
} |
| 192 |
|
| 193 |
if (args->requested_event_loop != NULL) { |
| 194 |
return args->requested_event_loop; |
| 195 |
} |
| 196 |
|
| 197 |
return aws_event_loop_group_get_next_loop(args->bootstrap->event_loop_group); |
| 198 |
} |
| 199 |
|
| 200 |
static void s_connect_args_setup_callback_safe( |
| 201 |
struct client_connection_args *args, |
| 202 |
int error_code, |
| 203 |
struct aws_channel *channel) { |
| 204 |
|
| 205 |
AWS_FATAL_ASSERT( |
| 206 |
(args->requested_event_loop == NULL) || aws_event_loop_thread_is_callers_thread(args->requested_event_loop)); |
| 207 |
|
| 208 |
|
| 209 |
AWS_FATAL_ASSERT(!args->setup_called); |
| 210 |
|
| 211 |
AWS_ASSERT((error_code == AWS_OP_SUCCESS) == (channel != NULL)); |
| 212 |
aws_client_bootstrap_on_channel_event_fn *setup_callback = args->setup_callback; |
| 213 |
setup_callback(args->bootstrap, error_code, channel, args->user_data); |
| 214 |
args->setup_called = true; |
| 215 |
|
| 216 |
if (error_code) { |
| 217 |
args->shutdown_callback = NULL; |
| 218 |
} |
| 219 |
s_client_connection_args_release(args); |
| 220 |
} |
| 221 |
|
| 222 |
struct aws_connection_args_setup_callback_task { |
| 223 |
struct aws_allocator *allocator; |
| 224 |
struct aws_task task; |
| 225 |
struct client_connection_args *args; |
| 226 |
int error_code; |
| 227 |
struct aws_channel *channel; |
| 228 |
}; |
| 229 |
|
| 230 |
static void s_aws_connection_args_setup_callback_task_delete(struct aws_connection_args_setup_callback_task *task) { |
| 231 |
if (task == NULL) { |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
s_client_connection_args_release(task->args); |
| 236 |
if (task->channel) { |
| 237 |
aws_channel_release_hold(task->channel); |
| 238 |
} |
| 239 |
|
| 240 |
aws_mem_release(task->allocator, task); |
| 241 |
} |
| 242 |
|
| 243 |
void s_aws_connection_args_setup_callback_task_fn(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 244 |
(void)task; |
| 245 |
|
| 246 |
struct aws_connection_args_setup_callback_task *callback_task = arg; |
| 247 |
|
| 248 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 249 |
s_connect_args_setup_callback_safe(callback_task->args, callback_task->error_code, callback_task->channel); |
| 250 |
} |
| 251 |
|
| 252 |
s_aws_connection_args_setup_callback_task_delete(callback_task); |
| 253 |
} |
| 254 |
|
| 255 |
static struct aws_connection_args_setup_callback_task *s_aws_connection_args_setup_callback_task_new( |
| 256 |
struct aws_allocator *allocator, |
| 257 |
struct client_connection_args *args, |
| 258 |
int error_code, |
| 259 |
struct aws_channel *channel) { |
| 260 |
|
| 261 |
struct aws_connection_args_setup_callback_task *task = |
| 262 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_connection_args_setup_callback_task)); |
| 263 |
task->allocator = allocator; |
| 264 |
task->args = s_client_connection_args_acquire(args); |
| 265 |
task->error_code = error_code; |
| 266 |
task->channel = channel; |
| 267 |
if (channel != NULL) { |
| 268 |
aws_channel_acquire_hold(channel); |
| 269 |
} |
| 270 |
|
| 271 |
aws_task_init( |
| 272 |
&task->task, s_aws_connection_args_setup_callback_task_fn, task, "safe connection args setup callback"); |
| 273 |
|
| 274 |
return task; |
| 275 |
} |
| 276 |
|
| 277 |
static void s_connection_args_setup_callback( |
| 278 |
struct client_connection_args *args, |
| 279 |
int error_code, |
| 280 |
struct aws_channel *channel) { |
| 281 |
|
| 282 |
if (args->requested_event_loop == NULL || aws_event_loop_thread_is_callers_thread(args->requested_event_loop)) { |
| 283 |
s_connect_args_setup_callback_safe(args, error_code, channel); |
| 284 |
} else { |
| 285 |
struct aws_connection_args_setup_callback_task *callback_task = |
| 286 |
s_aws_connection_args_setup_callback_task_new(args->bootstrap->allocator, args, error_code, channel); |
| 287 |
aws_event_loop_schedule_task_now(args->requested_event_loop, &callback_task->task); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
static void s_connection_args_creation_callback(struct client_connection_args *args, struct aws_channel *channel) { |
| 292 |
|
| 293 |
AWS_FATAL_ASSERT(channel != NULL); |
| 294 |
|
| 295 |
if (args->creation_callback) { |
| 296 |
args->creation_callback(args->bootstrap, AWS_ERROR_SUCCESS, channel, args->user_data); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
static void s_connection_args_shutdown_callback( |
| 301 |
struct client_connection_args *args, |
| 302 |
int error_code, |
| 303 |
struct aws_channel *channel) { |
| 304 |
|
| 305 |
if (!args->setup_called) { |
| 306 |
|
| 307 |
error_code = (error_code) ? error_code : AWS_ERROR_UNKNOWN; |
| 308 |
s_connection_args_setup_callback(args, error_code, NULL); |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
aws_client_bootstrap_on_channel_event_fn *shutdown_callback = args->shutdown_callback; |
| 313 |
if (shutdown_callback) { |
| 314 |
shutdown_callback(args->bootstrap, error_code, channel, args->user_data); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
static void s_tls_client_on_negotiation_result( |
| 319 |
struct aws_channel_handler *handler, |
| 320 |
struct aws_channel_slot *slot, |
| 321 |
int err_code, |
| 322 |
void *user_data) { |
| 323 |
struct client_connection_args *connection_args = user_data; |
| 324 |
|
| 325 |
if (connection_args->channel_data.user_on_negotiation_result) { |
| 326 |
connection_args->channel_data.user_on_negotiation_result( |
| 327 |
handler, slot, err_code, connection_args->channel_data.tls_user_data); |
| 328 |
} |
| 329 |
|
| 330 |
AWS_LOGF_DEBUG( |
| 331 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 332 |
"id=%p: tls negotiation result %d on channel %p", |
| 333 |
(void *)connection_args->bootstrap, |
| 334 |
err_code, |
| 335 |
(void *)slot->channel); |
| 336 |
|
| 337 |
|
| 338 |
if (err_code) { |
| 339 |
aws_channel_shutdown(slot->channel, err_code); |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
struct aws_channel *channel = connection_args->channel_data.channel; |
| 344 |
s_connection_args_setup_callback(connection_args, AWS_ERROR_SUCCESS, channel); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static void s_tls_client_on_data_read( |
| 350 |
struct aws_channel_handler *handler, |
| 351 |
struct aws_channel_slot *slot, |
| 352 |
struct aws_byte_buf *buffer, |
| 353 |
void *user_data) { |
| 354 |
struct client_connection_args *connection_args = user_data; |
| 355 |
|
| 356 |
if (connection_args->channel_data.user_on_data_read) { |
| 357 |
connection_args->channel_data.user_on_data_read( |
| 358 |
handler, slot, buffer, connection_args->channel_data.tls_user_data); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
static void s_tls_client_on_error( |
| 365 |
struct aws_channel_handler *handler, |
| 366 |
struct aws_channel_slot *slot, |
| 367 |
int err, |
| 368 |
const char *message, |
| 369 |
void *user_data) { |
| 370 |
struct client_connection_args *connection_args = user_data; |
| 371 |
|
| 372 |
if (connection_args->channel_data.user_on_error) { |
| 373 |
connection_args->channel_data.user_on_error( |
| 374 |
handler, slot, err, message, connection_args->channel_data.tls_user_data); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
static inline int s_setup_client_tls(struct client_connection_args *connection_args, struct aws_channel *channel) { |
| 379 |
struct aws_channel_slot *tls_slot = aws_channel_slot_new(channel); |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
if (!tls_slot) { |
| 384 |
return AWS_OP_ERR; |
| 385 |
} |
| 386 |
|
| 387 |
struct aws_channel_handler *tls_handler = aws_tls_client_handler_new( |
| 388 |
connection_args->bootstrap->allocator, &connection_args->channel_data.tls_options, tls_slot); |
| 389 |
|
| 390 |
if (!tls_handler) { |
| 391 |
aws_mem_release(connection_args->bootstrap->allocator, (void *)tls_slot); |
| 392 |
return AWS_OP_ERR; |
| 393 |
} |
| 394 |
|
| 395 |
aws_channel_slot_insert_end(channel, tls_slot); |
| 396 |
AWS_LOGF_TRACE( |
| 397 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 398 |
"id=%p: Setting up client TLS on channel %p with handler %p on slot %p", |
| 399 |
(void *)connection_args->bootstrap, |
| 400 |
(void *)channel, |
| 401 |
(void *)tls_handler, |
| 402 |
(void *)tls_slot); |
| 403 |
|
| 404 |
if (aws_channel_slot_set_handler(tls_slot, tls_handler) != AWS_OP_SUCCESS) { |
| 405 |
return AWS_OP_ERR; |
| 406 |
} |
| 407 |
|
| 408 |
if (connection_args->channel_data.on_protocol_negotiated) { |
| 409 |
struct aws_channel_slot *alpn_slot = aws_channel_slot_new(channel); |
| 410 |
|
| 411 |
if (!alpn_slot) { |
| 412 |
return AWS_OP_ERR; |
| 413 |
} |
| 414 |
|
| 415 |
struct aws_channel_handler *alpn_handler = aws_tls_alpn_handler_new( |
| 416 |
connection_args->bootstrap->allocator, |
| 417 |
connection_args->channel_data.on_protocol_negotiated, |
| 418 |
connection_args->user_data); |
| 419 |
|
| 420 |
if (!alpn_handler) { |
| 421 |
aws_mem_release(connection_args->bootstrap->allocator, (void *)alpn_slot); |
| 422 |
return AWS_OP_ERR; |
| 423 |
} |
| 424 |
|
| 425 |
AWS_LOGF_TRACE( |
| 426 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 427 |
"id=%p: Setting up ALPN handler on channel " |
| 428 |
"%p with handler %p on slot %p", |
| 429 |
(void *)connection_args->bootstrap, |
| 430 |
(void *)channel, |
| 431 |
(void *)alpn_handler, |
| 432 |
(void *)alpn_slot); |
| 433 |
|
| 434 |
aws_channel_slot_insert_right(tls_slot, alpn_slot); |
| 435 |
if (aws_channel_slot_set_handler(alpn_slot, alpn_handler) != AWS_OP_SUCCESS) { |
| 436 |
return AWS_OP_ERR; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
if (aws_tls_client_handler_start_negotiation(tls_handler) != AWS_OP_SUCCESS) { |
| 441 |
return AWS_OP_ERR; |
| 442 |
} |
| 443 |
|
| 444 |
return AWS_OP_SUCCESS; |
| 445 |
} |
| 446 |
|
| 447 |
static void s_on_client_channel_on_setup_completed(struct aws_channel *channel, int error_code, void *user_data) { |
| 448 |
struct client_connection_args *connection_args = user_data; |
| 449 |
int err_code = error_code; |
| 450 |
|
| 451 |
if (!err_code) { |
| 452 |
AWS_LOGF_DEBUG( |
| 453 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 454 |
"id=%p: channel %p setup succeeded: bootstrapping.", |
| 455 |
(void *)connection_args->bootstrap, |
| 456 |
(void *)channel); |
| 457 |
|
| 458 |
struct aws_channel_slot *socket_slot = aws_channel_slot_new(channel); |
| 459 |
|
| 460 |
if (!socket_slot) { |
| 461 |
err_code = aws_last_error(); |
| 462 |
goto error; |
| 463 |
} |
| 464 |
|
| 465 |
struct aws_channel_handler *socket_channel_handler = aws_socket_handler_new( |
| 466 |
connection_args->bootstrap->allocator, |
| 467 |
connection_args->channel_data.socket, |
| 468 |
socket_slot, |
| 469 |
g_aws_channel_max_fragment_size); |
| 470 |
|
| 471 |
if (!socket_channel_handler) { |
| 472 |
err_code = aws_last_error(); |
| 473 |
aws_channel_slot_remove(socket_slot); |
| 474 |
socket_slot = NULL; |
| 475 |
goto error; |
| 476 |
} |
| 477 |
|
| 478 |
AWS_LOGF_TRACE( |
| 479 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 480 |
"id=%p: Setting up socket handler on channel " |
| 481 |
"%p with handler %p on slot %p.", |
| 482 |
(void *)connection_args->bootstrap, |
| 483 |
(void *)channel, |
| 484 |
(void *)socket_channel_handler, |
| 485 |
(void *)socket_slot); |
| 486 |
|
| 487 |
if (aws_channel_slot_set_handler(socket_slot, socket_channel_handler)) { |
| 488 |
err_code = aws_last_error(); |
| 489 |
goto error; |
| 490 |
} |
| 491 |
|
| 492 |
if (connection_args->channel_data.use_tls) { |
| 493 |
if (aws_is_using_secitem()) { |
| 494 |
|
| 495 |
rk |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
s_tls_client_on_negotiation_result(socket_channel_handler, socket_slot, err_code, connection_args); |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
if (s_setup_client_tls(connection_args, channel)) { |
| 508 |
err_code = aws_last_error(); |
| 509 |
goto error; |
| 510 |
} |
| 511 |
} else { |
| 512 |
s_connection_args_setup_callback(connection_args, AWS_OP_SUCCESS, channel); |
| 513 |
} |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
error: |
| 518 |
AWS_LOGF_ERROR( |
| 519 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 520 |
"id=%p: channel %p setup failed with error %d.", |
| 521 |
(void *)connection_args->bootstrap, |
| 522 |
(void *)channel, |
| 523 |
err_code); |
| 524 |
aws_channel_shutdown(channel, err_code); |
| 525 |
|
| 526 |
} |
| 527 |
|
| 528 |
static void s_socket_shutdown_complete_release_client_connection_fn(void *user_data) { |
| 529 |
struct client_connection_args *connection_args = user_data; |
| 530 |
s_client_connection_args_release(connection_args); |
| 531 |
} |
| 532 |
|
| 533 |
static void s_on_client_channel_on_shutdown(struct aws_channel *channel, int error_code, void *user_data) { |
| 534 |
struct client_connection_args *connection_args = user_data; |
| 535 |
|
| 536 |
AWS_LOGF_DEBUG( |
| 537 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 538 |
"id=%p: channel %p shutdown with error %d.", |
| 539 |
(void *)connection_args->bootstrap, |
| 540 |
(void *)channel, |
| 541 |
error_code); |
| 542 |
|
| 543 |
struct aws_socket *socket = connection_args->channel_data.socket; |
| 544 |
struct aws_allocator *allocator = connection_args->bootstrap->allocator; |
| 545 |
|
| 546 |
s_connection_args_shutdown_callback(connection_args, error_code, channel); |
| 547 |
|
| 548 |
|
| 549 |
aws_channel_destroy(channel); |
| 550 |
|
| 551 |
aws_socket_set_cleanup_complete_callback( |
| 552 |
socket, s_socket_shutdown_complete_release_client_connection_fn, connection_args); |
| 553 |
|
| 554 |
aws_socket_clean_up(socket); |
| 555 |
|
| 556 |
aws_mem_release(allocator, socket); |
| 557 |
} |
| 558 |
|
| 559 |
static bool s_aws_socket_domain_uses_dns(enum aws_socket_domain domain) { |
| 560 |
return domain == AWS_SOCKET_IPV4 || domain == AWS_SOCKET_IPV6; |
| 561 |
} |
| 562 |
|
| 563 |
struct socket_shutdown_setup_channel_args { |
| 564 |
struct aws_allocator *allocator; |
| 565 |
struct client_connection_args *connection_args; |
| 566 |
int error_code; |
| 567 |
bool release_connection_args; |
| 568 |
}; |
| 569 |
|
| 570 |
struct socket_shutdown_setup_channel_args *socket_shutdown_setup_channel_args_new( |
| 571 |
struct aws_allocator *allocator, |
| 572 |
struct client_connection_args *connection_args, |
| 573 |
int error_code, |
| 574 |
bool release_connection_args) { |
| 575 |
struct socket_shutdown_setup_channel_args *shutdown_args = |
| 576 |
aws_mem_calloc(allocator, 1, sizeof(struct socket_shutdown_setup_channel_args)); |
| 577 |
shutdown_args->allocator = allocator; |
| 578 |
shutdown_args->connection_args = connection_args; |
| 579 |
shutdown_args->error_code = error_code; |
| 580 |
shutdown_args->release_connection_args = release_connection_args; |
| 581 |
return shutdown_args; |
| 582 |
} |
| 583 |
|
| 584 |
static void socket_shutdown_setup_channel_args_destroy(struct socket_shutdown_setup_channel_args *args) { |
| 585 |
aws_mem_release(args->allocator, args); |
| 586 |
} |
| 587 |
|
| 588 |
static void s_socket_shutdown_complete_setup_connection_args_fn(void *user_data) { |
| 589 |
struct socket_shutdown_setup_channel_args *shutdown_args = user_data; |
| 590 |
struct client_connection_args *connection_args = shutdown_args->connection_args; |
| 591 |
|
| 592 |
|
| 593 |
if (shutdown_args->error_code || !connection_args->channel_data.channel) { |
| 594 |
connection_args->failed_count++; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
if (connection_args->failed_count == connection_args->addresses_count) { |
| 599 |
if (connection_args->tls_error_code) { |
| 600 |
AWS_LOGF_ERROR( |
| 601 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 602 |
"id=%p: Connection failed with error_code %d : %s.", |
| 603 |
(void *)connection_args->bootstrap, |
| 604 |
connection_args->tls_error_code, |
| 605 |
aws_error_name(connection_args->tls_error_code)); |
| 606 |
|
| 607 |
s_connection_args_setup_callback(connection_args, connection_args->tls_error_code, NULL); |
| 608 |
} else { |
| 609 |
AWS_LOGF_ERROR( |
| 610 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 611 |
"id=%p: Connection failed with error_code %d : %s.", |
| 612 |
(void *)connection_args->bootstrap, |
| 613 |
shutdown_args->error_code, |
| 614 |
aws_error_name(shutdown_args->error_code)); |
| 615 |
|
| 616 |
s_connection_args_setup_callback(connection_args, shutdown_args->error_code, NULL); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
if (shutdown_args->release_connection_args) { |
| 621 |
|
| 622 |
s_client_connection_args_release(connection_args); |
| 623 |
} |
| 624 |
socket_shutdown_setup_channel_args_destroy(shutdown_args); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
static void s_on_client_connection_established(struct aws_socket *socket, int error_code, void *user_data) { |
| 632 |
struct client_connection_args *connection_args = user_data; |
| 633 |
|
| 634 |
AWS_LOGF_DEBUG( |
| 635 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 636 |
"id=%p: client connection on socket %p completed with error %d : %s", |
| 637 |
(void *)connection_args->bootstrap, |
| 638 |
(void *)socket, |
| 639 |
error_code, |
| 640 |
aws_error_name(error_code)); |
| 641 |
|
| 642 |
struct aws_allocator *allocator = connection_args->bootstrap->allocator; |
| 643 |
if (s_aws_socket_domain_uses_dns(connection_args->outgoing_options.domain) && error_code) { |
| 644 |
struct aws_host_address host_address; |
| 645 |
host_address.host = connection_args->host_name; |
| 646 |
host_address.address = aws_string_new_from_c_str(allocator, socket->remote_endpoint.address); |
| 647 |
host_address.record_type = connection_args->outgoing_options.domain == AWS_SOCKET_IPV6 |
| 648 |
? AWS_ADDRESS_RECORD_TYPE_AAAA |
| 649 |
: AWS_ADDRESS_RECORD_TYPE_A; |
| 650 |
|
| 651 |
if (host_address.address) { |
| 652 |
AWS_LOGF_DEBUG( |
| 653 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 654 |
"id=%p: recording bad address %s.", |
| 655 |
(void *)connection_args->bootstrap, |
| 656 |
socket->remote_endpoint.address); |
| 657 |
aws_host_resolver_record_connection_failure(connection_args->bootstrap->host_resolver, &host_address); |
| 658 |
aws_string_destroy((void *)host_address.address); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
if (error_code || connection_args->connection_chosen) { |
| 663 |
if (error_code) { |
| 664 |
AWS_LOGF_DEBUG( |
| 665 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 666 |
"id=%p: releasing socket %p due to error_code %d : %s", |
| 667 |
(void *)connection_args->bootstrap, |
| 668 |
(void *)socket, |
| 669 |
error_code, |
| 670 |
aws_error_name(error_code)); |
| 671 |
if (aws_is_using_secitem()) { |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
if (aws_error_code_is_tls(error_code)) { |
| 680 |
AWS_LOGF_DEBUG( |
| 681 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 682 |
"id=%p: Storing socket %p error_code %d as this socket's TCP connection has succeeded but was " |
| 683 |
"followed up by a TLS neotiation error.", |
| 684 |
(void *)connection_args->bootstrap, |
| 685 |
(void *)socket, |
| 686 |
error_code); |
| 687 |
connection_args->tls_error_code = error_code; |
| 688 |
connection_args->connection_chosen = true; |
| 689 |
connection_args->channel_data.socket = socket; |
| 690 |
} |
| 691 |
} |
| 692 |
} else { |
| 693 |
AWS_LOGF_DEBUG( |
| 694 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 695 |
"id=%p: releasing socket %p because we already have a successful connection.", |
| 696 |
(void *)connection_args->bootstrap, |
| 697 |
(void *)socket); |
| 698 |
} |
| 699 |
|
| 700 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 701 |
allocator, |
| 702 |
socket, |
| 703 |
socket_shutdown_setup_channel_args, |
| 704 |
s_socket_shutdown_complete_setup_connection_args_fn, |
| 705 |
connection_args, |
| 706 |
error_code, |
| 707 |
true) |
| 708 |
aws_socket_close(socket); |
| 709 |
aws_socket_clean_up(socket); |
| 710 |
aws_mem_release(allocator, socket); |
| 711 |
return; |
| 712 |
} |
| 713 |
|
| 714 |
connection_args->connection_chosen = true; |
| 715 |
connection_args->channel_data.socket = socket; |
| 716 |
|
| 717 |
struct aws_channel_options args = { |
| 718 |
.on_setup_completed = s_on_client_channel_on_setup_completed, |
| 719 |
.setup_user_data = connection_args, |
| 720 |
.shutdown_user_data = connection_args, |
| 721 |
.on_shutdown_completed = s_on_client_channel_on_shutdown, |
| 722 |
}; |
| 723 |
|
| 724 |
args.enable_read_back_pressure = connection_args->enable_read_back_pressure; |
| 725 |
args.event_loop = aws_socket_get_event_loop(socket); |
| 726 |
|
| 727 |
AWS_LOGF_TRACE( |
| 728 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 729 |
"id=%p: Successful connection, creating a new channel using socket %p.", |
| 730 |
(void *)connection_args->bootstrap, |
| 731 |
(void *)socket); |
| 732 |
|
| 733 |
connection_args->channel_data.channel = aws_channel_new(connection_args->bootstrap->allocator, &args); |
| 734 |
|
| 735 |
if (!connection_args->channel_data.channel) { |
| 736 |
|
| 737 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 738 |
connection_args->bootstrap->allocator, |
| 739 |
socket, |
| 740 |
socket_shutdown_setup_channel_args, |
| 741 |
s_socket_shutdown_complete_setup_connection_args_fn, |
| 742 |
connection_args, |
| 743 |
error_code, |
| 744 |
false) |
| 745 |
aws_socket_clean_up(socket); |
| 746 |
aws_mem_release(connection_args->bootstrap->allocator, connection_args->channel_data.socket); |
| 747 |
} else { |
| 748 |
s_connection_args_creation_callback(connection_args, connection_args->channel_data.channel); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
struct connection_task_data { |
| 753 |
struct aws_task task; |
| 754 |
struct aws_socket_endpoint endpoint; |
| 755 |
struct aws_socket_options options; |
| 756 |
struct aws_host_address host_address; |
| 757 |
struct client_connection_args *args; |
| 758 |
struct aws_event_loop *connect_loop; |
| 759 |
}; |
| 760 |
|
| 761 |
struct socket_shutdown_attempt_connection_args { |
| 762 |
struct aws_allocator *allocator; |
| 763 |
struct connection_task_data *task_data; |
| 764 |
int error_code; |
| 765 |
}; |
| 766 |
|
| 767 |
struct socket_shutdown_attempt_connection_args *socket_shutdown_attempt_connection_args_new( |
| 768 |
struct aws_allocator *allocator, |
| 769 |
struct connection_task_data *task_data, |
| 770 |
int error_code) { |
| 771 |
struct socket_shutdown_attempt_connection_args *close_args = |
| 772 |
aws_mem_calloc(allocator, 1, sizeof(struct socket_shutdown_attempt_connection_args)); |
| 773 |
close_args->allocator = allocator; |
| 774 |
close_args->task_data = task_data; |
| 775 |
close_args->error_code = error_code; |
| 776 |
return close_args; |
| 777 |
} |
| 778 |
|
| 779 |
static void s_socket_shutdown_complete_attempt_connection_fn(void *user_data) { |
| 780 |
struct socket_shutdown_attempt_connection_args *shutdown_args = user_data; |
| 781 |
struct connection_task_data *task_data = shutdown_args->task_data; |
| 782 |
int err_code = shutdown_args->error_code; |
| 783 |
|
| 784 |
|
| 785 |
if (++task_data->args->failed_count == task_data->args->addresses_count) { |
| 786 |
AWS_LOGF_ERROR( |
| 787 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 788 |
"id=%p: Last attempt failed to create socket with error %d", |
| 789 |
(void *)task_data->args->bootstrap, |
| 790 |
err_code); |
| 791 |
s_connection_args_setup_callback(task_data->args, err_code, NULL); |
| 792 |
} else { |
| 793 |
AWS_LOGF_DEBUG( |
| 794 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 795 |
"id=%p: Socket connect attempt %d/%d failed with error %d. More attempts ongoing...", |
| 796 |
(void *)task_data->args->bootstrap, |
| 797 |
task_data->args->failed_count, |
| 798 |
task_data->args->addresses_count, |
| 799 |
err_code); |
| 800 |
} |
| 801 |
|
| 802 |
s_client_connection_args_release(task_data->args); |
| 803 |
|
| 804 |
aws_host_address_clean_up(&task_data->host_address); |
| 805 |
|
| 806 |
aws_mem_release(shutdown_args->allocator, task_data); |
| 807 |
aws_mem_release(shutdown_args->allocator, shutdown_args); |
| 808 |
} |
| 809 |
|
| 810 |
static void s_attempt_connection(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 811 |
(void)task; |
| 812 |
struct connection_task_data *task_data = arg; |
| 813 |
struct aws_allocator *allocator = task_data->args->bootstrap->allocator; |
| 814 |
int err_code = 0; |
| 815 |
|
| 816 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 817 |
goto task_cancelled; |
| 818 |
} |
| 819 |
|
| 820 |
struct aws_socket *outgoing_socket = aws_mem_calloc(allocator, 1, sizeof(struct aws_socket)); |
| 821 |
if (aws_socket_init(outgoing_socket, allocator, &task_data->options)) { |
| 822 |
goto socket_init_failed; |
| 823 |
} |
| 824 |
|
| 825 |
struct aws_socket_connect_options connect_options = { |
| 826 |
.remote_endpoint = &task_data->endpoint, |
| 827 |
.event_loop = task_data->connect_loop, |
| 828 |
.on_connection_result = s_on_client_connection_established, |
| 829 |
.user_data = task_data->args}; |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
if (aws_is_using_secitem()) { |
| 835 |
struct client_connection_args *connection_args = task_data->args; |
| 836 |
if (connection_args->channel_data.use_tls) { |
| 837 |
connect_options.tls_connection_options = &connection_args->channel_data.tls_options; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
if (aws_socket_connect(outgoing_socket, &connect_options)) { |
| 842 |
goto socket_connect_failed; |
| 843 |
} |
| 844 |
|
| 845 |
goto cleanup_task; |
| 846 |
|
| 847 |
socket_connect_failed: |
| 848 |
aws_host_resolver_record_connection_failure(task_data->args->bootstrap->host_resolver, &task_data->host_address); |
| 849 |
|
| 850 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 851 |
allocator, |
| 852 |
outgoing_socket, |
| 853 |
socket_shutdown_attempt_connection_args, |
| 854 |
s_socket_shutdown_complete_attempt_connection_fn, |
| 855 |
task_data, |
| 856 |
aws_last_error()) |
| 857 |
|
| 858 |
aws_socket_clean_up(outgoing_socket); |
| 859 |
aws_mem_release(allocator, outgoing_socket); |
| 860 |
|
| 861 |
|
| 862 |
return; |
| 863 |
|
| 864 |
socket_init_failed: |
| 865 |
aws_mem_release(allocator, outgoing_socket); |
| 866 |
|
| 867 |
task_cancelled: |
| 868 |
err_code = aws_last_error(); |
| 869 |
task_data->args->failed_count++; |
| 870 |
|
| 871 |
if (task_data->args->failed_count == task_data->args->addresses_count) { |
| 872 |
AWS_LOGF_ERROR( |
| 873 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 874 |
"id=%p: Last attempt failed to create socket with error %d", |
| 875 |
(void *)task_data->args->bootstrap, |
| 876 |
err_code); |
| 877 |
s_connection_args_setup_callback(task_data->args, err_code, NULL); |
| 878 |
} else { |
| 879 |
AWS_LOGF_DEBUG( |
| 880 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 881 |
"id=%p: Socket connect attempt %d/%d failed with error %d. More attempts ongoing...", |
| 882 |
(void *)task_data->args->bootstrap, |
| 883 |
task_data->args->failed_count, |
| 884 |
task_data->args->addresses_count, |
| 885 |
err_code); |
| 886 |
} |
| 887 |
|
| 888 |
s_client_connection_args_release(task_data->args); |
| 889 |
|
| 890 |
cleanup_task: |
| 891 |
aws_host_address_clean_up(&task_data->host_address); |
| 892 |
aws_mem_release(allocator, task_data); |
| 893 |
} |
| 894 |
|
| 895 |
static void s_on_host_resolved( |
| 896 |
struct aws_host_resolver *resolver, |
| 897 |
const struct aws_string *host_name, |
| 898 |
int err_code, |
| 899 |
const struct aws_array_list *host_addresses, |
| 900 |
void *user_data) { |
| 901 |
(void)resolver; |
| 902 |
(void)host_name; |
| 903 |
|
| 904 |
struct client_connection_args *client_connection_args = user_data; |
| 905 |
struct aws_allocator *allocator = client_connection_args->bootstrap->allocator; |
| 906 |
|
| 907 |
if (err_code) { |
| 908 |
AWS_LOGF_ERROR( |
| 909 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 910 |
"id=%p: dns resolution failed, or all socket connections to the endpoint failed.", |
| 911 |
(void *)client_connection_args->bootstrap); |
| 912 |
s_connection_args_setup_callback(client_connection_args, err_code, NULL); |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
size_t host_addresses_len = aws_array_list_length(host_addresses); |
| 917 |
AWS_FATAL_ASSERT(host_addresses_len > 0); |
| 918 |
AWS_LOGF_TRACE( |
| 919 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 920 |
"id=%p: dns resolution completed. Kicking off connections" |
| 921 |
" on %llu addresses. First one back wins.", |
| 922 |
(void *)client_connection_args->bootstrap, |
| 923 |
(unsigned long long)host_addresses_len); |
| 924 |
|
| 925 |
struct aws_event_loop *connect_loop = s_get_connection_event_loop(client_connection_args); |
| 926 |
client_connection_args->addresses_count = (uint8_t)host_addresses_len; |
| 927 |
|
| 928 |
|
| 929 |
AWS_VARIABLE_LENGTH_ARRAY(struct connection_task_data *, tasks, host_addresses_len); |
| 930 |
for (size_t i = 0; i < host_addresses_len; ++i) { |
| 931 |
struct connection_task_data *task_data = tasks[i] = |
| 932 |
aws_mem_calloc(allocator, 1, sizeof(struct connection_task_data)); |
| 933 |
bool failed = task_data == NULL; |
| 934 |
if (!failed) { |
| 935 |
struct aws_host_address *host_address_ptr = NULL; |
| 936 |
aws_array_list_get_at_ptr(host_addresses, (void **)&host_address_ptr, i); |
| 937 |
|
| 938 |
task_data->endpoint.port = client_connection_args->outgoing_port; |
| 939 |
AWS_ASSERT(sizeof(task_data->endpoint.address) >= host_address_ptr->address->len + 1); |
| 940 |
memcpy( |
| 941 |
task_data->endpoint.address, |
| 942 |
aws_string_bytes(host_address_ptr->address), |
| 943 |
host_address_ptr->address->len); |
| 944 |
task_data->endpoint.address[host_address_ptr->address->len] = 0; |
| 945 |
|
| 946 |
task_data->options = client_connection_args->outgoing_options; |
| 947 |
task_data->options.domain = |
| 948 |
host_address_ptr->record_type == AWS_ADDRESS_RECORD_TYPE_AAAA ? AWS_SOCKET_IPV6 : AWS_SOCKET_IPV4; |
| 949 |
|
| 950 |
failed = aws_host_address_copy(host_address_ptr, &task_data->host_address) != AWS_OP_SUCCESS; |
| 951 |
task_data->args = client_connection_args; |
| 952 |
task_data->connect_loop = connect_loop; |
| 953 |
} |
| 954 |
|
| 955 |
if (failed) { |
| 956 |
for (size_t j = 0; j <= i; ++j) { |
| 957 |
if (tasks[j]) { |
| 958 |
aws_host_address_clean_up(&tasks[j]->host_address); |
| 959 |
aws_mem_release(allocator, tasks[j]); |
| 960 |
} |
| 961 |
} |
| 962 |
int alloc_err_code = aws_last_error(); |
| 963 |
AWS_LOGF_ERROR( |
| 964 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 965 |
"id=%p: failed to allocate connection task data: err=%d", |
| 966 |
(void *)client_connection_args->bootstrap, |
| 967 |
alloc_err_code); |
| 968 |
s_connection_args_setup_callback(client_connection_args, alloc_err_code, NULL); |
| 969 |
return; |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
for (size_t i = 0; i < host_addresses_len; ++i) { |
| 975 |
struct connection_task_data *task_data = tasks[i]; |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
s_client_connection_args_acquire(task_data->args); |
| 985 |
} |
| 986 |
|
| 987 |
for (size_t i = 0; i < host_addresses_len; ++i) { |
| 988 |
struct connection_task_data *task_data = tasks[i]; |
| 989 |
aws_task_init(&task_data->task, s_attempt_connection, task_data, "attempt_connection"); |
| 990 |
aws_event_loop_schedule_task_now(connect_loop, &task_data->task); |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
static bool s_does_event_loop_belong_to_event_loop_group( |
| 995 |
struct aws_event_loop *loop, |
| 996 |
struct aws_event_loop_group *elg) { |
| 997 |
if (loop == NULL || elg == NULL) { |
| 998 |
return false; |
| 999 |
} |
| 1000 |
|
| 1001 |
size_t loop_count = aws_event_loop_group_get_loop_count(elg); |
| 1002 |
for (size_t i = 0; i < loop_count; ++i) { |
| 1003 |
struct aws_event_loop *elg_loop = aws_event_loop_group_get_loop_at(elg, i); |
| 1004 |
if (elg_loop == loop) { |
| 1005 |
return true; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
return false; |
| 1010 |
} |
| 1011 |
|
| 1012 |
int aws_client_bootstrap_new_socket_channel(struct aws_socket_channel_bootstrap_options *options) { |
| 1013 |
|
| 1014 |
struct aws_client_bootstrap *bootstrap = options->bootstrap; |
| 1015 |
AWS_FATAL_ASSERT(options->setup_callback); |
| 1016 |
AWS_FATAL_ASSERT(options->shutdown_callback); |
| 1017 |
AWS_FATAL_ASSERT(bootstrap); |
| 1018 |
|
| 1019 |
const struct aws_socket_options *socket_options = options->socket_options; |
| 1020 |
AWS_FATAL_ASSERT(socket_options != NULL); |
| 1021 |
|
| 1022 |
const struct aws_tls_connection_options *tls_options = options->tls_options; |
| 1023 |
|
| 1024 |
AWS_FATAL_ASSERT(tls_options == NULL || socket_options->type == AWS_SOCKET_STREAM); |
| 1025 |
aws_io_fatal_assert_library_initialized(); |
| 1026 |
|
| 1027 |
if (options->requested_event_loop != NULL) { |
| 1028 |
|
| 1029 |
if (!(s_does_event_loop_belong_to_event_loop_group( |
| 1030 |
options->requested_event_loop, bootstrap->event_loop_group))) { |
| 1031 |
return aws_raise_error(AWS_ERROR_IO_PINNED_EVENT_LOOP_MISMATCH); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
struct client_connection_args *client_connection_args = |
| 1036 |
aws_mem_calloc(bootstrap->allocator, 1, sizeof(struct client_connection_args)); |
| 1037 |
|
| 1038 |
const char *host_name = options->host_name; |
| 1039 |
uint32_t port = options->port; |
| 1040 |
|
| 1041 |
AWS_LOGF_TRACE( |
| 1042 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1043 |
"id=%p: attempting to initialize a new client channel to %s:%u", |
| 1044 |
(void *)bootstrap, |
| 1045 |
host_name, |
| 1046 |
port); |
| 1047 |
|
| 1048 |
aws_ref_count_init( |
| 1049 |
&client_connection_args->ref_count, |
| 1050 |
client_connection_args, |
| 1051 |
(aws_simple_completion_callback *)s_client_connection_args_destroy); |
| 1052 |
client_connection_args->user_data = options->user_data; |
| 1053 |
client_connection_args->bootstrap = aws_client_bootstrap_acquire(bootstrap); |
| 1054 |
client_connection_args->creation_callback = options->creation_callback; |
| 1055 |
client_connection_args->setup_callback = options->setup_callback; |
| 1056 |
client_connection_args->shutdown_callback = options->shutdown_callback; |
| 1057 |
client_connection_args->outgoing_options = *socket_options; |
| 1058 |
client_connection_args->outgoing_port = port; |
| 1059 |
client_connection_args->enable_read_back_pressure = options->enable_read_back_pressure; |
| 1060 |
client_connection_args->requested_event_loop = options->requested_event_loop; |
| 1061 |
client_connection_args->tls_error_code = AWS_ERROR_SUCCESS; |
| 1062 |
|
| 1063 |
if (tls_options) { |
| 1064 |
if (aws_tls_connection_options_copy(&client_connection_args->channel_data.tls_options, tls_options)) { |
| 1065 |
goto error; |
| 1066 |
} |
| 1067 |
client_connection_args->channel_data.use_tls = true; |
| 1068 |
|
| 1069 |
client_connection_args->channel_data.on_protocol_negotiated = bootstrap->on_protocol_negotiated; |
| 1070 |
client_connection_args->channel_data.tls_user_data = tls_options->user_data; |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
if (bootstrap->on_protocol_negotiated) { |
| 1075 |
client_connection_args->channel_data.tls_options.advertise_alpn_message = true; |
| 1076 |
} |
| 1077 |
|
| 1078 |
if (tls_options->on_data_read) { |
| 1079 |
client_connection_args->channel_data.user_on_data_read = tls_options->on_data_read; |
| 1080 |
client_connection_args->channel_data.tls_options.on_data_read = s_tls_client_on_data_read; |
| 1081 |
} |
| 1082 |
|
| 1083 |
if (tls_options->on_error) { |
| 1084 |
client_connection_args->channel_data.user_on_error = tls_options->on_error; |
| 1085 |
client_connection_args->channel_data.tls_options.on_error = s_tls_client_on_error; |
| 1086 |
} |
| 1087 |
|
| 1088 |
if (tls_options->on_negotiation_result) { |
| 1089 |
client_connection_args->channel_data.user_on_negotiation_result = tls_options->on_negotiation_result; |
| 1090 |
} |
| 1091 |
|
| 1092 |
client_connection_args->channel_data.tls_options.on_negotiation_result = s_tls_client_on_negotiation_result; |
| 1093 |
client_connection_args->channel_data.tls_options.user_data = client_connection_args; |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (s_aws_socket_domain_uses_dns(socket_options->domain)) { |
| 1097 |
client_connection_args->host_name = aws_string_new_from_c_str(bootstrap->allocator, host_name); |
| 1098 |
|
| 1099 |
if (!client_connection_args->host_name) { |
| 1100 |
goto error; |
| 1101 |
} |
| 1102 |
|
| 1103 |
const struct aws_host_resolution_config *host_resolution_config = &bootstrap->host_resolver_config; |
| 1104 |
if (options->host_resolution_override_config) { |
| 1105 |
host_resolution_config = options->host_resolution_override_config; |
| 1106 |
} |
| 1107 |
|
| 1108 |
if (aws_host_resolver_resolve_host( |
| 1109 |
bootstrap->host_resolver, |
| 1110 |
client_connection_args->host_name, |
| 1111 |
s_on_host_resolved, |
| 1112 |
host_resolution_config, |
| 1113 |
client_connection_args)) { |
| 1114 |
goto error; |
| 1115 |
} |
| 1116 |
} else { |
| 1117 |
|
| 1118 |
const size_t host_name_len = strlen(host_name); |
| 1119 |
if (host_name_len >= AWS_ADDRESS_MAX_LEN) { |
| 1120 |
aws_raise_error(AWS_IO_SOCKET_INVALID_ADDRESS); |
| 1121 |
goto error; |
| 1122 |
} |
| 1123 |
|
| 1124 |
struct aws_socket_endpoint endpoint; |
| 1125 |
AWS_ZERO_STRUCT(endpoint); |
| 1126 |
memcpy(endpoint.address, host_name, host_name_len); |
| 1127 |
if (socket_options->domain == AWS_SOCKET_VSOCK) { |
| 1128 |
endpoint.port = port; |
| 1129 |
} else { |
| 1130 |
endpoint.port = 0; |
| 1131 |
} |
| 1132 |
|
| 1133 |
struct aws_socket *outgoing_socket = aws_mem_acquire(bootstrap->allocator, sizeof(struct aws_socket)); |
| 1134 |
|
| 1135 |
if (!outgoing_socket) { |
| 1136 |
goto error; |
| 1137 |
} |
| 1138 |
|
| 1139 |
if (aws_socket_init(outgoing_socket, bootstrap->allocator, socket_options)) { |
| 1140 |
aws_mem_release(bootstrap->allocator, outgoing_socket); |
| 1141 |
goto error; |
| 1142 |
} |
| 1143 |
|
| 1144 |
client_connection_args->addresses_count = 1; |
| 1145 |
|
| 1146 |
struct aws_event_loop *connect_loop = s_get_connection_event_loop(client_connection_args); |
| 1147 |
|
| 1148 |
s_client_connection_args_acquire(client_connection_args); |
| 1149 |
|
| 1150 |
struct aws_socket_connect_options connect_options = { |
| 1151 |
.remote_endpoint = &endpoint, |
| 1152 |
.event_loop = connect_loop, |
| 1153 |
.on_connection_result = s_on_client_connection_established, |
| 1154 |
.user_data = client_connection_args}; |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
if (aws_is_using_secitem()) { |
| 1160 |
if (client_connection_args->channel_data.use_tls) { |
| 1161 |
connect_options.tls_connection_options = &client_connection_args->channel_data.tls_options; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (aws_socket_connect(outgoing_socket, &connect_options)) { |
| 1166 |
|
| 1167 |
aws_socket_set_cleanup_complete_callback( |
| 1168 |
outgoing_socket, s_socket_shutdown_complete_release_client_connection_fn, client_connection_args); |
| 1169 |
|
| 1170 |
aws_socket_clean_up(outgoing_socket); |
| 1171 |
aws_mem_release(client_connection_args->bootstrap->allocator, outgoing_socket); |
| 1172 |
goto error; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
return AWS_OP_SUCCESS; |
| 1177 |
|
| 1178 |
error: |
| 1179 |
if (client_connection_args) { |
| 1180 |
|
| 1181 |
s_client_connection_args_release(client_connection_args); |
| 1182 |
} |
| 1183 |
return AWS_OP_ERR; |
| 1184 |
} |
| 1185 |
|
| 1186 |
void s_server_bootstrap_destroy_impl(void *user_data) { |
| 1187 |
struct aws_server_bootstrap *bootstrap = user_data; |
| 1188 |
AWS_ASSERT(bootstrap); |
| 1189 |
aws_event_loop_group_release(bootstrap->event_loop_group); |
| 1190 |
aws_mem_release(bootstrap->allocator, bootstrap); |
| 1191 |
} |
| 1192 |
|
| 1193 |
struct aws_server_bootstrap *aws_server_bootstrap_acquire(struct aws_server_bootstrap *bootstrap) { |
| 1194 |
if (bootstrap != NULL) { |
| 1195 |
aws_ref_count_acquire(&bootstrap->ref_count); |
| 1196 |
} |
| 1197 |
|
| 1198 |
return bootstrap; |
| 1199 |
} |
| 1200 |
|
| 1201 |
void aws_server_bootstrap_release(struct aws_server_bootstrap *bootstrap) { |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: releasing server bootstrap reference", (void *)bootstrap); |
| 1206 |
if (bootstrap != NULL) { |
| 1207 |
aws_ref_count_release(&bootstrap->ref_count); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
struct aws_server_bootstrap *aws_server_bootstrap_new( |
| 1212 |
struct aws_allocator *allocator, |
| 1213 |
struct aws_event_loop_group *el_group) { |
| 1214 |
AWS_ASSERT(allocator); |
| 1215 |
AWS_ASSERT(el_group); |
| 1216 |
|
| 1217 |
struct aws_server_bootstrap *bootstrap = aws_mem_calloc(allocator, 1, sizeof(struct aws_server_bootstrap)); |
| 1218 |
if (!bootstrap) { |
| 1219 |
return NULL; |
| 1220 |
} |
| 1221 |
|
| 1222 |
AWS_LOGF_INFO( |
| 1223 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1224 |
"id=%p: Initializing server bootstrap with event-loop group %p", |
| 1225 |
(void *)bootstrap, |
| 1226 |
(void *)el_group); |
| 1227 |
|
| 1228 |
bootstrap->allocator = allocator; |
| 1229 |
bootstrap->event_loop_group = aws_event_loop_group_acquire(el_group); |
| 1230 |
bootstrap->on_protocol_negotiated = NULL; |
| 1231 |
aws_ref_count_init( |
| 1232 |
&bootstrap->ref_count, bootstrap, (aws_simple_completion_callback *)s_server_bootstrap_destroy_impl); |
| 1233 |
|
| 1234 |
return bootstrap; |
| 1235 |
} |
| 1236 |
struct server_connection_args { |
| 1237 |
struct aws_server_bootstrap *bootstrap; |
| 1238 |
struct aws_socket listener; |
| 1239 |
aws_server_bootstrap_on_accept_channel_setup_fn *incoming_callback; |
| 1240 |
aws_server_bootstrap_on_accept_channel_shutdown_fn *shutdown_callback; |
| 1241 |
aws_server_bootstrap_on_server_listener_destroy_fn *destroy_callback; |
| 1242 |
aws_server_bootstrap_on_listener_setup_fn *setup_callback; |
| 1243 |
struct aws_tls_connection_options tls_options; |
| 1244 |
aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated; |
| 1245 |
aws_tls_on_data_read_fn *user_on_data_read; |
| 1246 |
aws_tls_on_negotiation_result_fn *user_on_negotiation_result; |
| 1247 |
aws_tls_on_error_fn *user_on_error; |
| 1248 |
struct aws_task listener_destroy_task; |
| 1249 |
void *tls_user_data; |
| 1250 |
void *user_data; |
| 1251 |
bool use_tls; |
| 1252 |
bool enable_read_back_pressure; |
| 1253 |
struct aws_ref_count ref_count; |
| 1254 |
}; |
| 1255 |
|
| 1256 |
enum incoming_callback_status_type { |
| 1257 |
AWS_ICST_NOT_CALLED, |
| 1258 |
AWS_ICST_CALLED_WITH_SUCCESS, |
| 1259 |
AWS_ICST_CALLED_WITH_ERROR, |
| 1260 |
}; |
| 1261 |
|
| 1262 |
struct server_channel_data { |
| 1263 |
struct aws_channel *channel; |
| 1264 |
struct aws_socket *socket; |
| 1265 |
struct server_connection_args *server_connection_args; |
| 1266 |
enum incoming_callback_status_type incoming_callback_status; |
| 1267 |
}; |
| 1268 |
|
| 1269 |
static struct server_connection_args *s_server_connection_args_acquire(struct server_connection_args *args) { |
| 1270 |
if (args != NULL) { |
| 1271 |
aws_ref_count_acquire(&args->ref_count); |
| 1272 |
} |
| 1273 |
|
| 1274 |
return args; |
| 1275 |
} |
| 1276 |
|
| 1277 |
static void s_server_connection_args_destroy(void *user_data) { |
| 1278 |
struct server_connection_args *args = user_data; |
| 1279 |
if (args == NULL) { |
| 1280 |
return; |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
if (args->destroy_callback) { |
| 1285 |
args->destroy_callback(args->bootstrap, args->user_data); |
| 1286 |
} |
| 1287 |
|
| 1288 |
struct aws_allocator *allocator = args->bootstrap->allocator; |
| 1289 |
aws_server_bootstrap_release(args->bootstrap); |
| 1290 |
if (args->use_tls) { |
| 1291 |
aws_tls_connection_options_clean_up(&args->tls_options); |
| 1292 |
} |
| 1293 |
|
| 1294 |
aws_mem_release(allocator, args); |
| 1295 |
} |
| 1296 |
|
| 1297 |
static void s_server_connection_args_release(struct server_connection_args *args) { |
| 1298 |
if (args != NULL) { |
| 1299 |
aws_ref_count_release(&args->ref_count); |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
struct socket_shutdown_release_server_connection_args { |
| 1304 |
struct aws_allocator *allocator; |
| 1305 |
struct server_connection_args *connection_args; |
| 1306 |
}; |
| 1307 |
|
| 1308 |
struct socket_shutdown_release_server_connection_args *socket_shutdown_release_server_connection_args_new( |
| 1309 |
struct aws_allocator *allocator, |
| 1310 |
struct server_connection_args *connection_args) { |
| 1311 |
struct socket_shutdown_release_server_connection_args *shutdown_args = |
| 1312 |
aws_mem_calloc(allocator, 1, sizeof(struct socket_shutdown_release_server_connection_args)); |
| 1313 |
shutdown_args->allocator = allocator; |
| 1314 |
shutdown_args->connection_args = connection_args; |
| 1315 |
return shutdown_args; |
| 1316 |
} |
| 1317 |
|
| 1318 |
static void s_socket_shutdown_complete_release_server_connection_fn(void *user_data) { |
| 1319 |
struct socket_shutdown_release_server_connection_args *shutdown_args = user_data; |
| 1320 |
struct server_connection_args *connection_args = shutdown_args->connection_args; |
| 1321 |
struct aws_allocator *allocator = shutdown_args->allocator; |
| 1322 |
|
| 1323 |
s_server_connection_args_release(connection_args); |
| 1324 |
|
| 1325 |
aws_mem_release(allocator, shutdown_args); |
| 1326 |
} |
| 1327 |
|
| 1328 |
static void s_server_incoming_callback( |
| 1329 |
struct server_channel_data *channel_data, |
| 1330 |
int error_code, |
| 1331 |
struct aws_channel *channel) { |
| 1332 |
|
| 1333 |
AWS_ASSERT(channel_data->incoming_callback_status == AWS_ICST_NOT_CALLED); |
| 1334 |
struct server_connection_args *args = channel_data->server_connection_args; |
| 1335 |
args->incoming_callback(args->bootstrap, error_code, channel, args->user_data); |
| 1336 |
channel_data->incoming_callback_status = |
| 1337 |
error_code == AWS_ERROR_SUCCESS ? AWS_ICST_CALLED_WITH_SUCCESS : AWS_ICST_CALLED_WITH_ERROR; |
| 1338 |
} |
| 1339 |
|
| 1340 |
static void s_tls_server_on_negotiation_result( |
| 1341 |
struct aws_channel_handler *handler, |
| 1342 |
struct aws_channel_slot *slot, |
| 1343 |
int err_code, |
| 1344 |
void *user_data) { |
| 1345 |
struct server_channel_data *channel_data = user_data; |
| 1346 |
struct server_connection_args *connection_args = channel_data->server_connection_args; |
| 1347 |
|
| 1348 |
if (connection_args->user_on_negotiation_result) { |
| 1349 |
connection_args->user_on_negotiation_result(handler, slot, err_code, connection_args->tls_user_data); |
| 1350 |
} |
| 1351 |
|
| 1352 |
AWS_LOGF_DEBUG( |
| 1353 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1354 |
"id=%p: tls negotiation result %d on channel %p", |
| 1355 |
(void *)connection_args->bootstrap, |
| 1356 |
err_code, |
| 1357 |
(void *)slot->channel); |
| 1358 |
|
| 1359 |
struct aws_channel *channel = slot->channel; |
| 1360 |
if (err_code) { |
| 1361 |
|
| 1362 |
aws_channel_shutdown(channel, err_code); |
| 1363 |
} else { |
| 1364 |
s_server_incoming_callback(channel_data, err_code, channel); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
static void s_tls_server_on_data_read( |
| 1371 |
struct aws_channel_handler *handler, |
| 1372 |
struct aws_channel_slot *slot, |
| 1373 |
struct aws_byte_buf *buffer, |
| 1374 |
void *user_data) { |
| 1375 |
struct server_connection_args *connection_args = user_data; |
| 1376 |
|
| 1377 |
if (connection_args->user_on_data_read) { |
| 1378 |
connection_args->user_on_data_read(handler, slot, buffer, connection_args->tls_user_data); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
static void s_tls_server_on_error( |
| 1385 |
struct aws_channel_handler *handler, |
| 1386 |
struct aws_channel_slot *slot, |
| 1387 |
int err, |
| 1388 |
const char *message, |
| 1389 |
void *user_data) { |
| 1390 |
struct server_connection_args *connection_args = user_data; |
| 1391 |
|
| 1392 |
if (connection_args->user_on_error) { |
| 1393 |
connection_args->user_on_error(handler, slot, err, message, connection_args->tls_user_data); |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
static inline int s_setup_server_tls(struct server_channel_data *channel_data, struct aws_channel *channel) { |
| 1398 |
struct aws_channel_slot *tls_slot = NULL; |
| 1399 |
struct aws_channel_handler *tls_handler = NULL; |
| 1400 |
struct server_connection_args *connection_args = channel_data->server_connection_args; |
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
tls_slot = aws_channel_slot_new(channel); |
| 1406 |
|
| 1407 |
if (!tls_slot) { |
| 1408 |
return AWS_OP_ERR; |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
struct aws_tls_connection_options tls_options = connection_args->tls_options; |
| 1413 |
tls_options.user_data = channel_data; |
| 1414 |
tls_handler = aws_tls_server_handler_new(connection_args->bootstrap->allocator, &tls_options, tls_slot); |
| 1415 |
|
| 1416 |
if (!tls_handler) { |
| 1417 |
aws_mem_release(connection_args->bootstrap->allocator, tls_slot); |
| 1418 |
return AWS_OP_ERR; |
| 1419 |
} |
| 1420 |
|
| 1421 |
AWS_LOGF_TRACE( |
| 1422 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1423 |
"id=%p: Setting up server TLS on channel %p with handler %p on slot %p", |
| 1424 |
(void *)connection_args->bootstrap, |
| 1425 |
(void *)channel, |
| 1426 |
(void *)tls_handler, |
| 1427 |
(void *)tls_slot); |
| 1428 |
|
| 1429 |
aws_channel_slot_insert_end(channel, tls_slot); |
| 1430 |
|
| 1431 |
if (aws_channel_slot_set_handler(tls_slot, tls_handler)) { |
| 1432 |
return AWS_OP_ERR; |
| 1433 |
} |
| 1434 |
|
| 1435 |
if (connection_args->on_protocol_negotiated) { |
| 1436 |
struct aws_channel_slot *alpn_slot = NULL; |
| 1437 |
struct aws_channel_handler *alpn_handler = NULL; |
| 1438 |
alpn_slot = aws_channel_slot_new(channel); |
| 1439 |
|
| 1440 |
if (!alpn_slot) { |
| 1441 |
return AWS_OP_ERR; |
| 1442 |
} |
| 1443 |
|
| 1444 |
alpn_handler = aws_tls_alpn_handler_new( |
| 1445 |
connection_args->bootstrap->allocator, connection_args->on_protocol_negotiated, connection_args->user_data); |
| 1446 |
|
| 1447 |
if (!alpn_handler) { |
| 1448 |
aws_channel_slot_remove(alpn_slot); |
| 1449 |
return AWS_OP_ERR; |
| 1450 |
} |
| 1451 |
|
| 1452 |
AWS_LOGF_TRACE( |
| 1453 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1454 |
"id=%p: Setting up ALPN handler on channel " |
| 1455 |
"%p with handler %p on slot %p", |
| 1456 |
(void *)connection_args->bootstrap, |
| 1457 |
(void *)channel, |
| 1458 |
(void *)alpn_handler, |
| 1459 |
(void *)alpn_slot); |
| 1460 |
|
| 1461 |
aws_channel_slot_insert_right(tls_slot, alpn_slot); |
| 1462 |
|
| 1463 |
if (aws_channel_slot_set_handler(alpn_slot, alpn_handler)) { |
| 1464 |
return AWS_OP_ERR; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
if (aws_channel_trigger_read(channel)) { |
| 1475 |
return AWS_OP_ERR; |
| 1476 |
} |
| 1477 |
|
| 1478 |
return AWS_OP_SUCCESS; |
| 1479 |
} |
| 1480 |
|
| 1481 |
struct socket_shutdown_server_channel_setup_complete_args { |
| 1482 |
struct aws_allocator *allocator; |
| 1483 |
struct server_channel_data *channel_data; |
| 1484 |
int error_code; |
| 1485 |
}; |
| 1486 |
|
| 1487 |
struct socket_shutdown_server_channel_setup_complete_args *socket_shutdown_server_channel_setup_complete_args_new( |
| 1488 |
struct aws_allocator *allocator, |
| 1489 |
struct server_channel_data *channel_data, |
| 1490 |
int error_code) { |
| 1491 |
struct socket_shutdown_server_channel_setup_complete_args *shutdown_args = |
| 1492 |
aws_mem_calloc(allocator, 1, sizeof(struct socket_shutdown_server_channel_setup_complete_args)); |
| 1493 |
shutdown_args->allocator = allocator; |
| 1494 |
shutdown_args->channel_data = channel_data; |
| 1495 |
shutdown_args->error_code = error_code; |
| 1496 |
return shutdown_args; |
| 1497 |
} |
| 1498 |
|
| 1499 |
static void socket_shutdown_server_channel_setup_complete_fn(void *user_data) { |
| 1500 |
struct socket_shutdown_server_channel_setup_complete_args *shutdown_args = user_data; |
| 1501 |
struct server_channel_data *channel_data = shutdown_args->channel_data; |
| 1502 |
struct server_connection_args *connection_args = channel_data->server_connection_args; |
| 1503 |
struct aws_allocator *allocator = shutdown_args->allocator; |
| 1504 |
|
| 1505 |
s_server_incoming_callback(shutdown_args->channel_data, shutdown_args->error_code, NULL); |
| 1506 |
s_server_connection_args_release(connection_args); |
| 1507 |
aws_mem_release(allocator, shutdown_args->channel_data); |
| 1508 |
|
| 1509 |
aws_mem_release(allocator, shutdown_args); |
| 1510 |
} |
| 1511 |
|
| 1512 |
static void s_on_server_channel_on_setup_completed(struct aws_channel *channel, int error_code, void *user_data) { |
| 1513 |
struct server_channel_data *channel_data = user_data; |
| 1514 |
|
| 1515 |
int err_code = error_code; |
| 1516 |
if (err_code) { |
| 1517 |
|
| 1518 |
|
| 1519 |
AWS_LOGF_ERROR( |
| 1520 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1521 |
"id=%p: channel %p setup failed with error %d.", |
| 1522 |
(void *)channel_data->server_connection_args->bootstrap, |
| 1523 |
(void *)channel, |
| 1524 |
err_code); |
| 1525 |
|
| 1526 |
aws_channel_destroy(channel); |
| 1527 |
|
| 1528 |
struct aws_allocator *allocator = channel_data->socket->allocator; |
| 1529 |
struct aws_socket *socket = channel_data->socket; |
| 1530 |
|
| 1531 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 1532 |
allocator, |
| 1533 |
socket, |
| 1534 |
socket_shutdown_server_channel_setup_complete_args, |
| 1535 |
socket_shutdown_server_channel_setup_complete_fn, |
| 1536 |
channel_data, |
| 1537 |
aws_last_error()) |
| 1538 |
|
| 1539 |
aws_socket_clean_up(channel_data->socket); |
| 1540 |
aws_mem_release(socket->allocator, socket); |
| 1541 |
return; |
| 1542 |
} |
| 1543 |
|
| 1544 |
AWS_LOGF_DEBUG( |
| 1545 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1546 |
"id=%p: channel %p setup succeeded: bootstrapping.", |
| 1547 |
(void *)channel_data->server_connection_args->bootstrap, |
| 1548 |
(void *)channel); |
| 1549 |
|
| 1550 |
struct aws_channel_slot *socket_slot = aws_channel_slot_new(channel); |
| 1551 |
|
| 1552 |
if (!socket_slot) { |
| 1553 |
err_code = aws_last_error(); |
| 1554 |
goto error; |
| 1555 |
} |
| 1556 |
|
| 1557 |
struct aws_channel_handler *socket_channel_handler = aws_socket_handler_new( |
| 1558 |
channel_data->server_connection_args->bootstrap->allocator, |
| 1559 |
channel_data->socket, |
| 1560 |
socket_slot, |
| 1561 |
g_aws_channel_max_fragment_size); |
| 1562 |
|
| 1563 |
if (!socket_channel_handler) { |
| 1564 |
err_code = aws_last_error(); |
| 1565 |
aws_channel_slot_remove(socket_slot); |
| 1566 |
socket_slot = NULL; |
| 1567 |
goto error; |
| 1568 |
} |
| 1569 |
|
| 1570 |
AWS_LOGF_TRACE( |
| 1571 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1572 |
"id=%p: Setting up socket handler on channel " |
| 1573 |
"%p with handler %p on slot %p.", |
| 1574 |
(void *)channel_data->server_connection_args->bootstrap, |
| 1575 |
(void *)channel, |
| 1576 |
(void *)socket_channel_handler, |
| 1577 |
(void *)socket_slot); |
| 1578 |
|
| 1579 |
if (aws_channel_slot_set_handler(socket_slot, socket_channel_handler)) { |
| 1580 |
err_code = aws_last_error(); |
| 1581 |
goto error; |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (channel_data->server_connection_args->use_tls) { |
| 1585 |
if (aws_is_using_secitem()) { |
| 1586 |
|
| 1587 |
rk |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
s_tls_server_on_negotiation_result(socket_channel_handler, socket_slot, err_code, channel_data); |
| 1594 |
return; |
| 1595 |
} |
| 1596 |
|
| 1597 |
|
| 1598 |
if (s_setup_server_tls(channel_data, channel)) { |
| 1599 |
err_code = aws_last_error(); |
| 1600 |
goto error; |
| 1601 |
} |
| 1602 |
} else { |
| 1603 |
s_server_incoming_callback(channel_data, AWS_OP_SUCCESS, channel); |
| 1604 |
} |
| 1605 |
|
| 1606 |
return; |
| 1607 |
|
| 1608 |
error: |
| 1609 |
|
| 1610 |
aws_channel_shutdown(channel, err_code); |
| 1611 |
} |
| 1612 |
|
| 1613 |
struct socket_shutdown_server_channel_shutdown_args { |
| 1614 |
struct aws_allocator *allocator; |
| 1615 |
struct server_channel_data *channel_data; |
| 1616 |
struct aws_channel *channel; |
| 1617 |
int error_code; |
| 1618 |
}; |
| 1619 |
|
| 1620 |
struct socket_shutdown_server_channel_shutdown_args *socket_shutdown_server_channel_shutdown_args_new( |
| 1621 |
struct aws_allocator *allocator, |
| 1622 |
struct server_channel_data *channel_data, |
| 1623 |
struct aws_channel *channel, |
| 1624 |
int error_code) { |
| 1625 |
struct socket_shutdown_server_channel_shutdown_args *shutdown_args = |
| 1626 |
aws_mem_calloc(allocator, 1, sizeof(struct socket_shutdown_server_channel_shutdown_args)); |
| 1627 |
shutdown_args->allocator = allocator; |
| 1628 |
shutdown_args->channel_data = channel_data; |
| 1629 |
shutdown_args->channel = channel; |
| 1630 |
shutdown_args->error_code = error_code; |
| 1631 |
return shutdown_args; |
| 1632 |
} |
| 1633 |
|
| 1634 |
static void socket_shutdown_server_channel_shutdown_fn(void *user_data) { |
| 1635 |
struct socket_shutdown_server_channel_shutdown_args *shutdown_args = user_data; |
| 1636 |
struct server_channel_data *channel_data = shutdown_args->channel_data; |
| 1637 |
struct server_connection_args *connection_args = channel_data->server_connection_args; |
| 1638 |
struct aws_allocator *allocator = shutdown_args->allocator; |
| 1639 |
|
| 1640 |
AWS_LOGF_DEBUG( |
| 1641 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1642 |
"id=%p: channel %p shutdown with error %d.", |
| 1643 |
(void *)connection_args->bootstrap, |
| 1644 |
(void *)shutdown_args->channel, |
| 1645 |
shutdown_args->error_code); |
| 1646 |
|
| 1647 |
void *server_shutdown_user_data = connection_args->user_data; |
| 1648 |
struct aws_server_bootstrap *server_bootstrap = connection_args->bootstrap; |
| 1649 |
|
| 1650 |
int error_code = shutdown_args->error_code; |
| 1651 |
if (channel_data->incoming_callback_status == AWS_ICST_CALLED_WITH_SUCCESS) { |
| 1652 |
connection_args->shutdown_callback( |
| 1653 |
server_bootstrap, error_code, shutdown_args->channel, server_shutdown_user_data); |
| 1654 |
} |
| 1655 |
|
| 1656 |
aws_channel_destroy(shutdown_args->channel); |
| 1657 |
s_server_connection_args_release(channel_data->server_connection_args); |
| 1658 |
aws_mem_release(allocator, channel_data); |
| 1659 |
|
| 1660 |
aws_mem_release(allocator, shutdown_args); |
| 1661 |
} |
| 1662 |
|
| 1663 |
static void s_on_server_channel_on_shutdown(struct aws_channel *channel, int error_code, void *user_data) { |
| 1664 |
struct server_channel_data *channel_data = user_data; |
| 1665 |
struct server_connection_args *args = channel_data->server_connection_args; |
| 1666 |
struct aws_allocator *allocator = args->bootstrap->allocator; |
| 1667 |
|
| 1668 |
if (channel_data->incoming_callback_status == AWS_ICST_NOT_CALLED) { |
| 1669 |
error_code = (error_code) ? error_code : AWS_ERROR_UNKNOWN; |
| 1670 |
s_server_incoming_callback(channel_data, error_code, NULL); |
| 1671 |
} |
| 1672 |
|
| 1673 |
struct aws_socket *socket = channel_data->socket; |
| 1674 |
|
| 1675 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 1676 |
allocator, |
| 1677 |
socket, |
| 1678 |
socket_shutdown_server_channel_shutdown_args, |
| 1679 |
socket_shutdown_server_channel_shutdown_fn, |
| 1680 |
channel_data, |
| 1681 |
channel, |
| 1682 |
error_code) |
| 1683 |
|
| 1684 |
aws_socket_clean_up(socket); |
| 1685 |
aws_mem_release(allocator, socket); |
| 1686 |
} |
| 1687 |
|
| 1688 |
struct socket_shutdown_server_connection_result_args { |
| 1689 |
struct aws_allocator *allocator; |
| 1690 |
struct server_connection_args *connection_args; |
| 1691 |
int error_code; |
| 1692 |
}; |
| 1693 |
|
| 1694 |
struct socket_shutdown_server_connection_result_args *socket_shutdown_server_connection_result_args_new( |
| 1695 |
struct aws_allocator *allocator, |
| 1696 |
struct server_connection_args *connection_args, |
| 1697 |
int error_code) { |
| 1698 |
struct socket_shutdown_server_connection_result_args *shutdown_args = |
| 1699 |
aws_mem_calloc(allocator, 1, sizeof(struct socket_shutdown_server_connection_result_args)); |
| 1700 |
shutdown_args->allocator = allocator; |
| 1701 |
shutdown_args->connection_args = connection_args; |
| 1702 |
shutdown_args->error_code = error_code; |
| 1703 |
return shutdown_args; |
| 1704 |
} |
| 1705 |
|
| 1706 |
static void s_socket_shutdown_server_connection_result_fn(void *user_data) { |
| 1707 |
struct socket_shutdown_server_connection_result_args *shutdown_args = user_data; |
| 1708 |
struct server_connection_args *connection_args = shutdown_args->connection_args; |
| 1709 |
struct aws_allocator *allocator = shutdown_args->allocator; |
| 1710 |
|
| 1711 |
|
| 1712 |
connection_args->incoming_callback( |
| 1713 |
connection_args->bootstrap, shutdown_args->error_code, NULL, connection_args->user_data); |
| 1714 |
|
| 1715 |
s_server_connection_args_release(connection_args); |
| 1716 |
|
| 1717 |
aws_mem_release(allocator, shutdown_args); |
| 1718 |
} |
| 1719 |
|
| 1720 |
void s_on_server_connection_result( |
| 1721 |
struct aws_socket *socket, |
| 1722 |
int error_code, |
| 1723 |
struct aws_socket *new_socket, |
| 1724 |
void *user_data) { |
| 1725 |
(void)socket; |
| 1726 |
struct server_connection_args *connection_args = user_data; |
| 1727 |
|
| 1728 |
s_server_connection_args_acquire(connection_args); |
| 1729 |
AWS_LOGF_DEBUG( |
| 1730 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1731 |
"id=%p: server connection on socket %p completed with error %d.", |
| 1732 |
(void *)connection_args->bootstrap, |
| 1733 |
(void *)socket, |
| 1734 |
error_code); |
| 1735 |
|
| 1736 |
if (!error_code) { |
| 1737 |
AWS_LOGF_TRACE( |
| 1738 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1739 |
"id=%p: creating a new channel for incoming " |
| 1740 |
"connection using socket %p.", |
| 1741 |
(void *)connection_args->bootstrap, |
| 1742 |
(void *)socket); |
| 1743 |
struct server_channel_data *channel_data = |
| 1744 |
aws_mem_calloc(connection_args->bootstrap->allocator, 1, sizeof(struct server_channel_data)); |
| 1745 |
|
| 1746 |
channel_data->incoming_callback_status = AWS_ICST_NOT_CALLED; |
| 1747 |
channel_data->socket = new_socket; |
| 1748 |
channel_data->server_connection_args = connection_args; |
| 1749 |
|
| 1750 |
struct aws_event_loop *event_loop = |
| 1751 |
aws_event_loop_group_get_next_loop(connection_args->bootstrap->event_loop_group); |
| 1752 |
|
| 1753 |
struct aws_channel_options channel_args = { |
| 1754 |
.on_setup_completed = s_on_server_channel_on_setup_completed, |
| 1755 |
.setup_user_data = channel_data, |
| 1756 |
.shutdown_user_data = channel_data, |
| 1757 |
.on_shutdown_completed = s_on_server_channel_on_shutdown, |
| 1758 |
.event_loop = event_loop, |
| 1759 |
.enable_read_back_pressure = channel_data->server_connection_args->enable_read_back_pressure, |
| 1760 |
}; |
| 1761 |
|
| 1762 |
if (aws_socket_assign_to_event_loop(new_socket, event_loop)) { |
| 1763 |
aws_mem_release(connection_args->bootstrap->allocator, (void *)channel_data); |
| 1764 |
goto error_cleanup; |
| 1765 |
} |
| 1766 |
|
| 1767 |
channel_data->channel = aws_channel_new(connection_args->bootstrap->allocator, &channel_args); |
| 1768 |
|
| 1769 |
if (!channel_data->channel) { |
| 1770 |
aws_mem_release(connection_args->bootstrap->allocator, (void *)channel_data); |
| 1771 |
goto error_cleanup; |
| 1772 |
} |
| 1773 |
} else { |
| 1774 |
|
| 1775 |
connection_args->incoming_callback(connection_args->bootstrap, error_code, NULL, connection_args->user_data); |
| 1776 |
s_server_connection_args_release(connection_args); |
| 1777 |
} |
| 1778 |
|
| 1779 |
return; |
| 1780 |
|
| 1781 |
error_cleanup: |
| 1782 |
|
| 1783 |
; |
| 1784 |
struct aws_allocator *allocator = new_socket->allocator; |
| 1785 |
|
| 1786 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 1787 |
allocator, |
| 1788 |
socket, |
| 1789 |
socket_shutdown_server_connection_result_args, |
| 1790 |
s_socket_shutdown_server_connection_result_fn, |
| 1791 |
connection_args, |
| 1792 |
aws_last_error()) |
| 1793 |
|
| 1794 |
aws_socket_clean_up(new_socket); |
| 1795 |
aws_mem_release(allocator, (void *)new_socket); |
| 1796 |
} |
| 1797 |
|
| 1798 |
static void s_listener_destroy_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 1799 |
(void)status; |
| 1800 |
(void)task; |
| 1801 |
struct server_connection_args *server_connection_args = arg; |
| 1802 |
|
| 1803 |
aws_socket_stop_accept(&server_connection_args->listener); |
| 1804 |
|
| 1805 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 1806 |
server_connection_args->bootstrap->allocator, |
| 1807 |
&server_connection_args->listener, |
| 1808 |
socket_shutdown_release_server_connection_args, |
| 1809 |
s_socket_shutdown_complete_release_server_connection_fn, |
| 1810 |
server_connection_args) |
| 1811 |
|
| 1812 |
aws_socket_clean_up(&server_connection_args->listener); |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
static void s_on_listener_connection_established(struct aws_socket *socket, int error_code, void *user_data) { |
| 1818 |
struct server_connection_args *server_connection_args = user_data; |
| 1819 |
|
| 1820 |
AWS_LOGF_DEBUG( |
| 1821 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1822 |
"id=%p: listener connection on socket %p completed with error %d.", |
| 1823 |
(void *)server_connection_args->bootstrap, |
| 1824 |
(void *)socket, |
| 1825 |
error_code); |
| 1826 |
|
| 1827 |
if (error_code) { |
| 1828 |
|
| 1829 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 1830 |
server_connection_args->bootstrap->allocator, |
| 1831 |
&server_connection_args->listener, |
| 1832 |
socket_shutdown_release_server_connection_args, |
| 1833 |
s_socket_shutdown_complete_release_server_connection_fn, |
| 1834 |
server_connection_args) |
| 1835 |
|
| 1836 |
aws_socket_clean_up(&server_connection_args->listener); |
| 1837 |
} |
| 1838 |
|
| 1839 |
if (server_connection_args->setup_callback) { |
| 1840 |
server_connection_args->setup_callback( |
| 1841 |
server_connection_args->bootstrap, error_code, server_connection_args->user_data); |
| 1842 |
} |
| 1843 |
|
| 1844 |
s_server_connection_args_release(server_connection_args); |
| 1845 |
|
| 1846 |
return; |
| 1847 |
} |
| 1848 |
|
| 1849 |
struct aws_socket *aws_server_bootstrap_new_socket_listener( |
| 1850 |
const struct aws_server_socket_channel_bootstrap_options *bootstrap_options) { |
| 1851 |
AWS_PRECONDITION(bootstrap_options); |
| 1852 |
AWS_PRECONDITION(bootstrap_options->bootstrap); |
| 1853 |
AWS_PRECONDITION(bootstrap_options->incoming_callback); |
| 1854 |
AWS_PRECONDITION(bootstrap_options->shutdown_callback); |
| 1855 |
|
| 1856 |
bool async_setup = bootstrap_options->setup_callback != NULL; |
| 1857 |
|
| 1858 |
struct server_connection_args *server_connection_args = |
| 1859 |
aws_mem_calloc(bootstrap_options->bootstrap->allocator, 1, sizeof(struct server_connection_args)); |
| 1860 |
if (!server_connection_args) { |
| 1861 |
return NULL; |
| 1862 |
} |
| 1863 |
|
| 1864 |
AWS_LOGF_INFO( |
| 1865 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, |
| 1866 |
"id=%p: attempting to initialize a new " |
| 1867 |
"server socket listener for %s:%u", |
| 1868 |
(void *)bootstrap_options->bootstrap, |
| 1869 |
bootstrap_options->host_name, |
| 1870 |
bootstrap_options->port); |
| 1871 |
|
| 1872 |
aws_ref_count_init( |
| 1873 |
&server_connection_args->ref_count, |
| 1874 |
server_connection_args, |
| 1875 |
(aws_simple_completion_callback *)s_server_connection_args_destroy); |
| 1876 |
server_connection_args->user_data = bootstrap_options->user_data; |
| 1877 |
server_connection_args->bootstrap = aws_server_bootstrap_acquire(bootstrap_options->bootstrap); |
| 1878 |
server_connection_args->shutdown_callback = bootstrap_options->shutdown_callback; |
| 1879 |
server_connection_args->incoming_callback = bootstrap_options->incoming_callback; |
| 1880 |
server_connection_args->destroy_callback = bootstrap_options->destroy_callback; |
| 1881 |
server_connection_args->on_protocol_negotiated = bootstrap_options->bootstrap->on_protocol_negotiated; |
| 1882 |
server_connection_args->enable_read_back_pressure = bootstrap_options->enable_read_back_pressure; |
| 1883 |
server_connection_args->setup_callback = bootstrap_options->setup_callback; |
| 1884 |
|
| 1885 |
aws_task_init( |
| 1886 |
&server_connection_args->listener_destroy_task, |
| 1887 |
s_listener_destroy_task, |
| 1888 |
server_connection_args, |
| 1889 |
"listener socket destroy"); |
| 1890 |
|
| 1891 |
if (bootstrap_options->tls_options) { |
| 1892 |
AWS_LOGF_INFO( |
| 1893 |
AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: using tls on listener", (void *)bootstrap_options->tls_options); |
| 1894 |
if (aws_tls_connection_options_copy(&server_connection_args->tls_options, bootstrap_options->tls_options)) { |
| 1895 |
goto cleanup_server_connection_args; |
| 1896 |
} |
| 1897 |
|
| 1898 |
server_connection_args->use_tls = true; |
| 1899 |
|
| 1900 |
server_connection_args->tls_user_data = bootstrap_options->tls_options->user_data; |
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
if (bootstrap_options->bootstrap->on_protocol_negotiated) { |
| 1905 |
server_connection_args->tls_options.advertise_alpn_message = true; |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (bootstrap_options->tls_options->on_data_read) { |
| 1909 |
server_connection_args->user_on_data_read = bootstrap_options->tls_options->on_data_read; |
| 1910 |
server_connection_args->tls_options.on_data_read = s_tls_server_on_data_read; |
| 1911 |
} |
| 1912 |
|
| 1913 |
if (bootstrap_options->tls_options->on_error) { |
| 1914 |
server_connection_args->user_on_error = bootstrap_options->tls_options->on_error; |
| 1915 |
server_connection_args->tls_options.on_error = s_tls_server_on_error; |
| 1916 |
} |
| 1917 |
|
| 1918 |
if (bootstrap_options->tls_options->on_negotiation_result) { |
| 1919 |
server_connection_args->user_on_negotiation_result = bootstrap_options->tls_options->on_negotiation_result; |
| 1920 |
} |
| 1921 |
|
| 1922 |
server_connection_args->tls_options.on_negotiation_result = s_tls_server_on_negotiation_result; |
| 1923 |
server_connection_args->tls_options.user_data = server_connection_args; |
| 1924 |
} |
| 1925 |
|
| 1926 |
struct aws_event_loop *connection_loop = |
| 1927 |
aws_event_loop_group_get_next_loop(bootstrap_options->bootstrap->event_loop_group); |
| 1928 |
|
| 1929 |
if (aws_socket_init( |
| 1930 |
&server_connection_args->listener, |
| 1931 |
bootstrap_options->bootstrap->allocator, |
| 1932 |
bootstrap_options->socket_options)) { |
| 1933 |
goto cleanup_server_connection_args; |
| 1934 |
} |
| 1935 |
|
| 1936 |
struct aws_socket_endpoint endpoint; |
| 1937 |
AWS_ZERO_STRUCT(endpoint); |
| 1938 |
size_t host_name_len = 0; |
| 1939 |
if (aws_secure_strlen(bootstrap_options->host_name, sizeof(endpoint.address), &host_name_len)) { |
| 1940 |
goto cleanup_server_connection_args; |
| 1941 |
} |
| 1942 |
|
| 1943 |
memcpy(endpoint.address, bootstrap_options->host_name, host_name_len); |
| 1944 |
endpoint.port = bootstrap_options->port; |
| 1945 |
|
| 1946 |
struct aws_socket_bind_options socket_bind_options = { |
| 1947 |
.local_endpoint = &endpoint, .user_data = server_connection_args}; |
| 1948 |
|
| 1949 |
if (aws_is_using_secitem()) { |
| 1950 |
socket_bind_options.event_loop = connection_loop; |
| 1951 |
socket_bind_options.tls_connection_options = &server_connection_args->tls_options; |
| 1952 |
} |
| 1953 |
|
| 1954 |
if (aws_socket_bind(&server_connection_args->listener, &socket_bind_options)) { |
| 1955 |
goto cleanup_listener; |
| 1956 |
} |
| 1957 |
|
| 1958 |
if (aws_socket_listen(&server_connection_args->listener, 1024)) { |
| 1959 |
goto cleanup_listener; |
| 1960 |
} |
| 1961 |
|
| 1962 |
struct aws_socket_listener_options options = { |
| 1963 |
.on_accept_result = s_on_server_connection_result, |
| 1964 |
.on_accept_result_user_data = server_connection_args, |
| 1965 |
.on_accept_start = NULL, |
| 1966 |
.on_accept_start_user_data = NULL, |
| 1967 |
}; |
| 1968 |
|
| 1969 |
if (async_setup) { |
| 1970 |
|
| 1971 |
|
| 1972 |
s_server_connection_args_acquire(server_connection_args); |
| 1973 |
options.on_accept_start = s_on_listener_connection_established; |
| 1974 |
options.on_accept_start_user_data = server_connection_args; |
| 1975 |
} |
| 1976 |
|
| 1977 |
if (aws_socket_start_accept(&server_connection_args->listener, connection_loop, options)) { |
| 1978 |
if (async_setup) { |
| 1979 |
|
| 1980 |
s_server_connection_args_release(server_connection_args); |
| 1981 |
} |
| 1982 |
goto cleanup_listener; |
| 1983 |
} |
| 1984 |
|
| 1985 |
return &server_connection_args->listener; |
| 1986 |
|
| 1987 |
cleanup_listener: |
| 1988 |
|
| 1989 |
; |
| 1990 |
|
| 1991 |
SETUP_SOCKET_SHUTDOWN_CALLBACKS( |
| 1992 |
bootstrap_options->bootstrap->allocator, |
| 1993 |
&server_connection_args->listener, |
| 1994 |
socket_shutdown_release_server_connection_args, |
| 1995 |
s_socket_shutdown_complete_release_server_connection_fn, |
| 1996 |
server_connection_args) |
| 1997 |
|
| 1998 |
aws_socket_clean_up(&server_connection_args->listener); |
| 1999 |
return NULL; |
| 2000 |
|
| 2001 |
cleanup_server_connection_args: |
| 2002 |
s_server_connection_args_release(server_connection_args); |
| 2003 |
|
| 2004 |
return NULL; |
| 2005 |
} |
| 2006 |
|
| 2007 |
void aws_server_bootstrap_destroy_socket_listener(struct aws_server_bootstrap *bootstrap, struct aws_socket *listener) { |
| 2008 |
struct server_connection_args *server_connection_args = |
| 2009 |
AWS_CONTAINER_OF(listener, struct server_connection_args, listener); |
| 2010 |
|
| 2011 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: releasing bootstrap reference", (void *)bootstrap); |
| 2012 |
aws_event_loop_schedule_task_now(listener->event_loop, &server_connection_args->listener_destroy_task); |
| 2013 |
} |
| 2014 |
|
| 2015 |
int aws_server_bootstrap_set_alpn_callback( |
| 2016 |
struct aws_server_bootstrap *bootstrap, |
| 2017 |
aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated) { |
| 2018 |
AWS_ASSERT(on_protocol_negotiated); |
| 2019 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL_BOOTSTRAP, "id=%p: Setting ALPN callback", (void *)bootstrap); |
| 2020 |
bootstrap->on_protocol_negotiated = on_protocol_negotiated; |
| 2021 |
return AWS_OP_SUCCESS; |
| 2022 |
} |