Skip to content

Commit

Permalink
tls1.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
alfred2g committed May 16, 2024
1 parent 6225ebb commit 9d998ab
Show file tree
Hide file tree
Showing 6 changed files with 500 additions and 35 deletions.
2 changes: 1 addition & 1 deletion source/alpn_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct aws_channel_handler *aws_tls_alpn_handler_new(
alpn_handler->user_data = user_data;
channel_handler->impl = alpn_handler;
channel_handler->alloc = allocator;

printf("============================= seting write handler to null\n");
channel_handler->vtable = &s_alpn_handler_vtable;

return channel_handler;
Expand Down
6 changes: 5 additions & 1 deletion source/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ static void s_shutdown_task(struct aws_channel_task *task, void *arg, enum aws_t

static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool shutdown_immediately) {
bool need_to_schedule = true;
printf("s_channel_shutdown called\n");
aws_mutex_lock(&channel->cross_thread_tasks.lock);
if (channel->cross_thread_tasks.shutdown_task.task.task_fn) {
need_to_schedule = false;
Expand Down Expand Up @@ -809,6 +810,7 @@ int aws_channel_slot_send_message(
(void *)slot,
(void *)slot->adj_left,
(void *)slot->adj_left->handler);
printf("%s handler %p\n",__FUNCTION__, slot->adj_left->handler);
return aws_channel_handler_process_write_message(slot->adj_left->handler, slot->adj_left, message);
}

Expand Down Expand Up @@ -942,6 +944,7 @@ static void s_run_shutdown_write_direction(struct aws_task *task, void *arg, enu
task->fn = NULL;
task->arg = NULL;
struct aws_channel_slot *slot = shutdown_notify->slot;
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "s_run_shutdown_write_direction");
aws_channel_handler_shutdown(
slot->handler, slot, AWS_CHANNEL_DIR_WRITE, shutdown_notify->error_code, shutdown_notify->shutdown_immediately);
}
Expand All @@ -965,6 +968,7 @@ int aws_channel_slot_on_handler_shutdown_complete(

if (dir == AWS_CHANNEL_DIR_READ) {
if (slot->adj_right && slot->adj_right->handler) {
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "handler shutdown in dir completed. error_code %d", err_code);
return aws_channel_handler_shutdown(
slot->adj_right->handler, slot->adj_right, dir, err_code, free_scarce_resources_immediately);
}
Expand All @@ -982,6 +986,7 @@ int aws_channel_slot_on_handler_shutdown_complete(
}

if (slot->adj_left && slot->adj_left->handler) {
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "handler shutdown2 in dir completed. error_code %d", err_code);
return aws_channel_handler_shutdown(
slot->adj_left->handler, slot->adj_left, dir, err_code, free_scarce_resources_immediately);
}
Expand Down Expand Up @@ -1030,7 +1035,6 @@ int aws_channel_handler_process_write_message(
struct aws_channel_handler *handler,
struct aws_channel_slot *slot,
struct aws_io_message *message) {

AWS_ASSERT(handler->vtable && handler->vtable->process_write_message);
return handler->vtable->process_write_message(handler, slot, message);
}
Expand Down
5 changes: 4 additions & 1 deletion source/channel_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ static void s_connect_args_setup_callback_safe(
(args->requested_event_loop == NULL) || aws_event_loop_thread_is_callers_thread(args->requested_event_loop));

/* setup_callback is always called exactly once */
if (args->setup_called) {
return;
}
AWS_FATAL_ASSERT(!args->setup_called);

AWS_ASSERT((error_code == AWS_OP_SUCCESS) == (channel != NULL));
Expand Down Expand Up @@ -306,7 +309,7 @@ static void s_tls_client_on_negotiation_result(
int err_code,
void *user_data) {
struct client_connection_args *connection_args = user_data;

printf("entering s_tls_client_on_negotiation_resul entering\n");
if (connection_args->channel_data.user_on_negotiation_result) {
connection_args->channel_data.user_on_negotiation_result(
handler, slot, err_code, connection_args->channel_data.tls_user_data);
Expand Down
25 changes: 21 additions & 4 deletions source/socket_channel_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static int s_socket_process_write_message(
return aws_raise_error(AWS_IO_SOCKET_CLOSED);
}

printf("aws socket write\n");
struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(&message->message_data);
if (aws_socket_write(socket_handler->socket, &cursor, s_on_socket_write_complete, message)) {
return AWS_OP_ERR;
Expand Down Expand Up @@ -139,22 +140,28 @@ static void s_do_read(struct socket_handler *socket_handler) {

size_t total_read = 0;
size_t read = 0;
printf(" reading more shutdown in progress %d\n ", socket_handler->shutdown_in_progress);
while (total_read < max_to_read && !socket_handler->shutdown_in_progress) {
size_t iter_max_read = max_to_read - total_read;

printf("entering s_do_read function\n");
struct aws_io_message *message = aws_channel_acquire_message_from_pool(
socket_handler->slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, iter_max_read);

if (!message) {
printf("first break\n");
break;
}

if (aws_socket_read(socket_handler->socket, &message->message_data, &read)) {
; int ret;
if ((ret = aws_socket_read(socket_handler->socket, &message->message_data, &read))) {
aws_mem_release(message->allocator, message);
printf("second break: %d errno %d\n", ret, errno);
break;
}

printf("total read is %lu max_read is: %lu read is: %lu\n", total_read, max_to_read, read);
total_read += read;

AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET_HANDLER,
"id=%p: read %llu from socket",
Expand All @@ -163,6 +170,7 @@ static void s_do_read(struct socket_handler *socket_handler) {

if (aws_channel_slot_send_message(socket_handler->slot, message, AWS_CHANNEL_DIR_READ)) {
aws_mem_release(message->allocator, message);
printf("third break\n");
break;
}
}
Expand All @@ -178,8 +186,12 @@ static void s_do_read(struct socket_handler *socket_handler) {
/* resubscribe as long as there's no error, just return if we're in a would block scenario. */
if (total_read < max_to_read) {
int last_error = aws_last_error();
printf("last error is %d -> %d\n", last_error, AWS_IO_READ_WOULD_BLOCK);

if (last_error != AWS_IO_READ_WOULD_BLOCK && !socket_handler->shutdown_in_progress) {
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET_HANDLER, "id=%p shutting down channel",
(void *)socket_handler->slot->handler );

aws_channel_shutdown(socket_handler->slot->channel, last_error);
}

Expand Down Expand Up @@ -212,12 +224,14 @@ static void s_on_readable_notification(struct aws_socket *socket, int error_code
(void)socket;

struct socket_handler *socket_handler = user_data;
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET_HANDLER, "id=%p: socket is now readable", (void *)socket_handler->slot->handler);
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET_HANDLER, "id=%p: socket is now readable: error_codde: %d", (void *)socket_handler->slot->handler, error_code);

/* read regardless so we can pick up data that was sent prior to the close. For example, peer sends a TLS ALERT
* then immediately closes the socket. On some platforms, we'll never see the readable flag. So we want to make
* sure we read the ALERT, otherwise, we'll end up telling the user that the channel shutdown because of a socket
* closure, when in reality it was a TLS error */
printf("s_on_readable_notification entering\n");
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET_HANDLER, " caling s_do_read from s_on_readable_notification");
s_do_read(socket_handler);

if (error_code && !socket_handler->shutdown_in_progress) {
Expand All @@ -230,6 +244,7 @@ static void s_read_task(struct aws_channel_task *task, void *arg, aws_task_statu
task->task_fn = NULL;
task->arg = NULL;

AWS_LOGF_TRACE(AWS_LS_IO_SOCKET_HANDLER, " caling s_read_task");
if (status == AWS_TASK_STATUS_RUN_READY) {
struct socket_handler *socket_handler = arg;
s_do_read(socket_handler);
Expand Down Expand Up @@ -358,6 +373,8 @@ static void s_gather_statistics(struct aws_channel_handler *handler, struct aws_
static void s_trigger_read(struct aws_channel_handler *handler) {
struct socket_handler *socket_handler = (struct socket_handler *)handler->impl;

// printf("s_trigger read do \n");
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET_HANDLER, " caling s_do_read from s_trigger_read");
s_do_read(socket_handler);
}

Expand Down Expand Up @@ -402,7 +419,7 @@ struct aws_channel_handler *aws_socket_handler_new(
if (aws_crt_statistics_socket_init(&impl->stats)) {
goto cleanup_handler;
}

printf("-------------------------> setting up s_socket_process_write_message handler %p\n", handler);
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET_HANDLER,
"id=%p: Socket handler created with max_read_size of %llu",
Expand Down
2 changes: 1 addition & 1 deletion source/tls_channel_handler_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

static void s_tls_timeout_task_fn(struct aws_channel_task *channel_task, void *arg, enum aws_task_status status) {
(void)channel_task;

printf(" == == == == == == == == timeout\n ");
if (status != AWS_TASK_STATUS_RUN_READY) {
return;
}
Expand Down
Loading

0 comments on commit 9d998ab

Please sign in to comment.