Skip to content

Commit

Permalink
merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sbSteveK committed Feb 24, 2025
1 parent 5b26835 commit 25b5d40
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 119 deletions.
3 changes: 2 additions & 1 deletion source/channel_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,8 @@ struct aws_socket *aws_server_bootstrap_new_socket_listener_async(
memcpy(endpoint.address, bootstrap_options->host_name, host_name_len);
endpoint.port = bootstrap_options->port;

if (aws_socket_bind(&server_connection_args->listener, &endpoint)) {
if (aws_socket_bind(
&server_connection_args->listener, &endpoint, s_retrieve_server_tls_options, server_connection_args)) {
goto cleanup_listener;
}

Expand Down
1 change: 0 additions & 1 deletion source/darwin/dispatch_queue_event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ static void s_dispatch_event_loop_on_zero_ref_count(void *user_data) {
aws_condition_variable_notify_all(&dispatch_loop->synced_data.signal);
}

#if defined(AWS_ENABLE_DISPATCH_QUEUE)
/* Setup a dispatch_queue with a scheduler. */
struct aws_event_loop *aws_event_loop_new_with_dispatch_queue(
struct aws_allocator *alloc,
Expand Down
134 changes: 17 additions & 117 deletions source/darwin/nw_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include <aws/common/uuid.h>
#include <aws/io/logging.h>

#include "./dispatch_queue_event_loop_private.h" // private header
#include <Network/Network.h>
#include <aws/io/private/event_loop_impl.h>
#include <aws/io/private/tls_channel_handler_shared.h>

#include "aws_apple_network_framework.h"
#include <arpa/inet.h>
#include <sys/socket.h>

Expand Down Expand Up @@ -290,14 +290,6 @@ struct nw_socket {
} synced_state;
};

struct socket_address {
union sock_addr_types {
struct sockaddr_in addr_in;
struct sockaddr_in6 addr_in6;
struct sockaddr_un un_addr;
} sock_addr_types;
};

static size_t KB_16 = 16 * 1024;

static void *s_socket_acquire_internal_ref(struct nw_socket *nw_socket) {
Expand Down Expand Up @@ -392,98 +384,6 @@ static void s_set_socket_state(struct nw_socket *nw_socket, struct aws_socket *s
s_unlock_socket_state(nw_socket);
}

static void *s_socket_acquire_internal_ref(struct nw_socket *nw_socket) {
return aws_ref_count_acquire(&nw_socket->internal_ref_count);
}

static size_t s_socket_release_internal_ref(struct nw_socket *nw_socket) {
return aws_ref_count_release(&nw_socket->internal_ref_count);
}

static void *s_socket_acquire_write_ref(struct nw_socket *nw_socket) {
return aws_ref_count_acquire(&nw_socket->write_ref_count);
}

static size_t s_socket_release_write_ref(struct nw_socket *nw_socket) {
return aws_ref_count_release(&nw_socket->write_ref_count);
}

static int s_lock_socket_state(struct nw_socket *nw_socket) {
return aws_mutex_lock(&nw_socket->synced_state.lock);
}

static int s_unlock_socket_state(struct nw_socket *nw_socket) {
return aws_mutex_unlock(&nw_socket->synced_state.lock);
}

static int s_lock_socket_synced_data(struct nw_socket *nw_socket) {
return aws_mutex_lock(&nw_socket->synced_data.lock);
}

static int s_unlock_socket_synced_data(struct nw_socket *nw_socket) {
return aws_mutex_unlock(&nw_socket->synced_data.lock);
}

static bool s_validate_event_loop(struct aws_event_loop *event_loop) {
return event_loop && event_loop->vtable && event_loop->impl_data;
}

static void s_set_event_loop(struct aws_socket *aws_socket, struct aws_event_loop *event_loop) {
aws_socket->event_loop = event_loop;
struct nw_socket *nw_socket = aws_socket->impl;
// Never re-assign an event loop
AWS_FATAL_ASSERT(nw_socket->event_loop == NULL);
nw_socket->event_loop = event_loop;

AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "id=%p: s_set_event_loop: socket acquire event loop group.", (void *)nw_socket);
aws_event_loop_group_acquire(get_base_event_loop_group(event_loop));
}

static void s_release_event_loop(struct nw_socket *nw_socket) {
if (nw_socket->event_loop == NULL) {
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "id=%p: s_release_event_loop: socket has not event loop.", (void *)nw_socket);
return;
}
aws_event_loop_group_release(get_base_event_loop_group(nw_socket->event_loop));
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET, "id=%p: s_release_event_loop: socket release event loop group.", (void *)nw_socket);
nw_socket->event_loop = NULL;
}

static void s_set_socket_state(struct nw_socket *nw_socket, struct aws_socket *socket, enum aws_nw_socket_state state) {
s_lock_socket_state(nw_socket);

enum aws_nw_socket_state result_state = nw_socket->synced_state.state;

// clip the read/write bits
enum aws_nw_socket_state read_write_bits = state & (CONNECTED_WRITE | CONNECTED_READ);
result_state = result_state & ~CONNECTED_WRITE & ~CONNECTED_READ;

// If the caller would like simply flip the read/write bits, set the state to invalid, as we dont have further
// information there.
if (~CONNECTED_WRITE == (int)state || ~CONNECTED_READ == (int)state) {
state = INVALID;
}

// The state can only go increasing, except for the following two cases
// 1. LISTENING and STOPPED. They can switch between each other.
// 2. CONNECT_WRITE and CONNECT_READ: you are allow to flip the flags for these two state, while not going
// backwards to `CONNECTING` and `INIT` state.
if (result_state < state || (state == LISTENING && result_state == STOPPED)) {
result_state = state;
}

// Set CONNECTED_WRITE and CONNECTED_READ
result_state = result_state | read_write_bits;

nw_socket->synced_state.state = result_state;
if (socket) {
socket->state = result_state;
}

s_unlock_socket_state(nw_socket);
}

/* setup the TCP options Block for use in socket parameters */
static void s_setup_tcp_options(nw_protocol_options_t tcp_options, const struct aws_socket_options *options) {
if (options->connect_timeout_ms) {
Expand Down Expand Up @@ -517,7 +417,8 @@ static void s_setup_tls_options(
nw_protocol_options_t tls_options,
const struct aws_socket_options *options,
struct nw_socket *nw_socket,
struct secure_transport_ctx *transport_ctx) {
struct secure_transport_ctx *transport_ctx,
struct aws_event_loop *event_loop) {
/* Obtain the security protocol options from the tls_options. Changes made directly
* to the copy will impact the protocol options within the tls_options */
sec_protocol_options_t sec_options = nw_tls_copy_sec_protocol_options(tls_options);
Expand Down Expand Up @@ -584,9 +485,7 @@ static void s_setup_tls_options(
aws_array_list_clean_up(&alpn_list_array);
}

aws_mutex_lock(&nw_socket->synced_data.lock);
struct dispatch_loop *dispatch_loop = nw_socket->synced_data.event_loop->impl_data;
aws_mutex_unlock(&nw_socket->synced_data.lock);
struct aws_dispatch_loop *dispatch_loop = event_loop->impl_data;

/* We handle the verification of the remote end here. */
sec_protocol_options_set_verify_block(
Expand Down Expand Up @@ -718,7 +617,10 @@ static void s_setup_tls_options(
dispatch_loop->dispatch_queue);
}

static int s_setup_socket_params(struct nw_socket *nw_socket, const struct aws_socket_options *options) {
static int s_setup_socket_params(
struct nw_socket *nw_socket,
const struct aws_socket_options *options,
struct aws_event_loop *event_loop) {

/* If we already have parameters set, release them before re-establishing new parameters */
if (nw_socket->nw_parameters != NULL) {
Expand Down Expand Up @@ -757,7 +659,7 @@ static int s_setup_socket_params(struct nw_socket *nw_socket, const struct aws_s
nw_socket->nw_parameters = nw_parameters_create_secure_tcp(
// TLS options block
^(nw_protocol_options_t tls_options) {
s_setup_tls_options(tls_options, options, nw_socket, transport_ctx);
s_setup_tls_options(tls_options, options, nw_socket, transport_ctx, event_loop);
},
// TCP options block
^(nw_protocol_options_t tcp_options) {
Expand All @@ -778,7 +680,7 @@ static int s_setup_socket_params(struct nw_socket *nw_socket, const struct aws_s
nw_socket->nw_parameters = nw_parameters_create_secure_tcp(
// TLS options block
^(nw_protocol_options_t tls_options) {
s_setup_tls_options(tls_options, options, nw_socket, transport_ctx);
s_setup_tls_options(tls_options, options, nw_socket, transport_ctx, event_loop);
},
// TCP options block
^(nw_protocol_options_t tcp_options) {
Expand Down Expand Up @@ -1086,6 +988,7 @@ static void s_socket_internal_destroy(void *sock_ptr) {
s_release_event_loop(nw_socket);
aws_ref_count_release(&nw_socket->external_ref_count);
}

int aws_socket_init_apple_nw_socket(
struct aws_socket *socket,
struct aws_allocator *alloc,
Expand Down Expand Up @@ -1130,7 +1033,6 @@ int aws_socket_init_apple_nw_socket(

return AWS_OP_SUCCESS;
}
#endif // AWS_ENABLE_DISPATCH_QUEUE

static void s_client_set_dispatch_queue(struct aws_io_handle *handle, void *queue) {
nw_connection_set_queue(handle->data.handle, queue);
Expand Down Expand Up @@ -1739,7 +1641,7 @@ static int s_socket_connect_fn(
}

s_set_event_loop(socket, event_loop);
if (s_setup_socket_params(nw_socket, &socket->options)) {
if (s_setup_socket_params(nw_socket, &socket->options, event_loop)) {
goto clean_up;
}

Expand Down Expand Up @@ -1935,6 +1837,8 @@ static int s_socket_bind_fn(
(int)local_endpoint->port);

if (nw_socket->nw_parameters == NULL) {
struct aws_event_loop *event_loop = NULL;

if (retrieve_tls_options) {
struct tls_connection_context tls_connection_context;
AWS_ZERO_STRUCT(tls_connection_context);
Expand All @@ -1947,14 +1851,10 @@ static int s_socket_bind_fn(
(void *)socket);
return aws_last_error();
}

if (tls_connection_context.event_loop) {
aws_mutex_lock(&nw_socket->synced_data.lock);
nw_socket->synced_data.event_loop = tls_connection_context.event_loop;
aws_mutex_unlock(&nw_socket->synced_data.lock);
}
event_loop = tls_connection_context.event_loop;
}
s_setup_socket_params(nw_socket, &socket->options);

s_setup_socket_params(nw_socket, &socket->options, event_loop);
}

struct socket_address address;
Expand Down

0 comments on commit 25b5d40

Please sign in to comment.