Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error handling improvements #107

Merged
merged 29 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cfe9fb9
add a secure tunnel encode error and logging for errors during encodi…
sbSteveK Apr 12, 2024
b7e3f79
encode failure error
sbSteveK Apr 12, 2024
eac0841
more error cleanup
sbSteveK Apr 12, 2024
734e77f
log fix
sbSteveK Apr 12, 2024
0bf3638
returns when function is void
sbSteveK Apr 12, 2024
b29444b
more error stuff
sbSteveK Apr 15, 2024
d98600e
tries to remove with no error state
sbSteveK Apr 15, 2024
d6c8d14
handle more error results
sbSteveK Apr 15, 2024
4102f17
further error handling
sbSteveK Apr 16, 2024
9a204b3
test checks last error instead of return code
sbSteveK Apr 16, 2024
d50be30
assert that the send message failed
sbSteveK Apr 16, 2024
e541a99
report proper error during operation failures
sbSteveK Apr 18, 2024
1575116
Merge branch 'main' into secure-tunnel-error-handling
sbSteveK Apr 22, 2024
0150d0c
Merge branch 'main' into secure-tunnel-error-handling
sbSteveK Jun 13, 2024
efc2297
goto error
sbSteveK Jun 13, 2024
4abd149
remove unecessary error_code member
sbSteveK Jun 13, 2024
3f54ffa
change debug to warn log
sbSteveK Jun 13, 2024
9d9d835
remove unused function
sbSteveK Jun 14, 2024
cc6dee5
fully remove unused function
sbSteveK Jun 14, 2024
aad5078
cleaning
sbSteveK Jun 17, 2024
7e9f895
remove unecessary aws_raise_error()
sbSteveK Jun 17, 2024
1da2903
remove uneccessary AWS_ASSERT()
sbSteveK Jun 17, 2024
0c31c79
revert websocket order
sbSteveK Jun 18, 2024
3d25882
remove commented code
sbSteveK Jun 18, 2024
c6e8e66
cleanup
sbSteveK Jun 18, 2024
b3f2550
label needs to be followed by a statement
sbSteveK Jun 18, 2024
a90d5ef
AWS_OP_SUCCESS -> AWS_ERROR_SUCCESS where we are using error codes
sbSteveK Jul 17, 2024
df7c37c
Merge branch 'main' into secure-tunnel-error-handling
sbSteveK Jul 18, 2024
a95b385
check for correct validation error in payload exceed test
sbSteveK Jul 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/aws/iotdevice/iotdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum aws_iotdevice_error {
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_NO_ACTIVE_CONNECTION,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_PROTOCOL_VERSION_MISMATCH,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_ENCODE_FAILURE,

AWS_ERROR_END_IOTDEVICE_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_C_IOTDEVICE_PACKAGE_ID),
};
Expand Down
4 changes: 0 additions & 4 deletions include/aws/iotdevice/private/secure_tunneling_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ AWS_IOTDEVICE_API void aws_secure_tunnel_operation_complete(
int error_code,
const void *associated_view);

AWS_IOTDEVICE_API void aws_secure_tunnel_operation_assign_stream_id(
struct aws_secure_tunnel_operation *operation,
struct aws_secure_tunnel *secure_tunnel);

AWS_IOTDEVICE_API int32_t
aws_secure_tunnel_operation_get_stream_id(const struct aws_secure_tunnel_operation *operation);

Expand Down
5 changes: 4 additions & 1 deletion source/iotdevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static struct aws_error_info s_errors[] = {
"Secure Tunnel terminated by user request."),
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DECODE_FAILURE,
"Error occured while decoding an incoming message." ),
"Error occurred while decoding an incoming message." ),
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_NO_ACTIVE_CONNECTION,
"DATA message processing failed due to no active connection found." ),
Expand All @@ -102,6 +102,9 @@ static struct aws_error_info s_errors[] = {
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID,
"Secure Tunnel operation failed due to using inactive service id." ),
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_ENCODE_FAILURE,
"Error occured while encoding an outbound message." ),
};
/* clang-format on */
#undef AWS_DEFINE_ERROR_INFO_IOTDEVICE
Expand Down
407 changes: 184 additions & 223 deletions source/secure_tunneling.c

Large diffs are not rendered by default.

34 changes: 6 additions & 28 deletions source/secure_tunneling_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,6 @@ void aws_secure_tunnel_operation_complete(
}
}

void aws_secure_tunnel_operation_assign_stream_id(
struct aws_secure_tunnel_operation *operation,
struct aws_secure_tunnel *secure_tunnel) {
AWS_FATAL_ASSERT(operation->vtable != NULL);
if (operation->vtable->aws_secure_tunnel_operation_assign_stream_id_fn != NULL) {
(*operation->vtable->aws_secure_tunnel_operation_assign_stream_id_fn)(operation, secure_tunnel);
}
}

static struct aws_secure_tunnel_operation_vtable s_empty_operation_vtable = {
.aws_secure_tunnel_operation_completion_fn = NULL,
.aws_secure_tunnel_operation_assign_stream_id_fn = NULL,
Expand Down Expand Up @@ -362,33 +353,26 @@ static int s_aws_secure_tunnel_operation_message_assign_stream_id(

struct aws_secure_tunnel_message_view *message_view = &message_op->options_storage.storage_view;

int error_code = AWS_OP_SUCCESS;

if (message_view->service_id == NULL || message_view->service_id->len == 0) {
stream_id = secure_tunnel->connections->stream_id;
} else {
struct aws_hash_element *elem = NULL;
aws_hash_table_find(&secure_tunnel->connections->service_ids, message_view->service_id, &elem);
if (elem == NULL) {
AWS_LOGF_WARN(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: invalid service id '" PRInSTR "' attempted to be assigned a stream id on an outbound message",
(void *)message_view,
AWS_BYTE_CURSOR_PRI(*message_view->service_id));
error_code = AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_SERVICE_ID;
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_SERVICE_ID);
goto error;
}
struct aws_service_id_element *service_id_elem = elem->value;
stream_id = service_id_elem->stream_id;

if (stream_id == INVALID_STREAM_ID) {
error_code = AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID;
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID);
goto error;
}
}

if (stream_id == INVALID_STREAM_ID) {
error_code = AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID;
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID);
goto error;
}

Expand All @@ -397,21 +381,21 @@ static int s_aws_secure_tunnel_operation_message_assign_stream_id(

error:
if (message_view->service_id == NULL || message_view->service_id->len == 0) {
AWS_LOGF_DEBUG(
AWS_LOGF_WARN(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: No active stream to assign outbound %s message a stream id",
(void *)secure_tunnel,
aws_secure_tunnel_message_type_to_c_string(message_view->type));
} else {
AWS_LOGF_DEBUG(
AWS_LOGF_WARN(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: No active stream with service id '" PRInSTR "' to assign outbound %s message a stream id",
(void *)secure_tunnel,
AWS_BYTE_CURSOR_PRI(*message_view->service_id),
aws_secure_tunnel_message_type_to_c_string(message_view->type));
}

return aws_raise_error(error_code);
return AWS_OP_ERR;
}

/*
Expand Down Expand Up @@ -624,9 +608,6 @@ struct aws_secure_tunnel_operation_message *aws_secure_tunnel_operation_message_

struct aws_secure_tunnel_operation_message *message_op =
aws_mem_calloc(allocator, 1, sizeof(struct aws_secure_tunnel_operation_message));
if (message_op == NULL) {
return NULL;
}

message_op->allocator = allocator;
message_op->base.vtable = &s_message_operation_vtable;
Expand All @@ -645,7 +626,6 @@ struct aws_secure_tunnel_operation_message *aws_secure_tunnel_operation_message_
error:

aws_secure_tunnel_operation_release(&message_op->base);

return NULL;
}

Expand Down Expand Up @@ -991,7 +971,6 @@ struct data_tunnel_pair *aws_secure_tunnel_data_tunnel_pair_new(
pair->type = message_view->type;
pair->length_prefix_written = false;
if (aws_iot_st_msg_serialize_from_view(&pair->buf, allocator, message_view)) {
AWS_LOGF_ERROR(AWS_LS_IOTDEVICE_SECURE_TUNNELING, "Failure serializing message");
goto error;
}

Expand All @@ -1000,7 +979,6 @@ struct data_tunnel_pair *aws_secure_tunnel_data_tunnel_pair_new(
return pair;

error:

aws_secure_tunnel_data_tunnel_pair_destroy(pair);
return NULL;
}
Expand Down
71 changes: 57 additions & 14 deletions source/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ int aws_iot_st_msg_serialize_from_view(
const struct aws_secure_tunnel_message_view *message_view) {
size_t message_total_length = 0;
if (s_iot_st_compute_message_length(message_view, &message_total_length)) {
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure computing message length while serializing message",
(void *)message_view);
return AWS_OP_ERR;
}

Expand All @@ -256,70 +260,109 @@ int aws_iot_st_msg_serialize_from_view(
(void *)message_view,
message_total_length);

if (aws_byte_buf_init(buffer, allocator, message_total_length) != AWS_OP_SUCCESS) {
if (aws_byte_buf_init(buffer, allocator, message_total_length)) {
return AWS_OP_ERR;
}

if (message_view->type != AWS_SECURE_TUNNEL_MT_UNKNOWN) {
if (s_iot_st_encode_type(message_view->type, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding message type while serializing message",
(void *)message_view);
goto error;
}
} else {
AWS_LOGF_ERROR(AWS_LS_IOTDEVICE_SECURE_TUNNELING, "Message missing type during encoding");
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Message type missing while serializing message",
(void *)message_view);
goto error;
}

if (message_view->stream_id != 0) {
if (s_iot_st_encode_stream_id(message_view->stream_id, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding stream id while serializing message",
(void *)message_view);
goto error;
}
}

if (message_view->connection_id != 0) {
if (s_iot_st_encode_connection_id(message_view->connection_id, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding connection id while serializing message",
(void *)message_view);
goto error;
}
}

if (message_view->ignorable != 0) {
if (s_iot_st_encode_ignorable(message_view->ignorable, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding ignorable while serializing message",
(void *)message_view);
goto error;
}
}

if (message_view->payload != NULL) {
if (s_iot_st_encode_payload(message_view->payload, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding payload while serializing message",
(void *)message_view);
goto error;
}
}

if (message_view->type == AWS_SECURE_TUNNEL_MT_SERVICE_IDS) {
if (message_view->service_id != 0) {
if (s_iot_st_encode_service_ids(message_view->service_id, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding service id while serializing message",
(void *)message_view);
goto error;
}
}
if (message_view->service_id_2 != 0) {
if (s_iot_st_encode_service_ids(message_view->service_id_2, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding service id 2 while serializing message",
(void *)message_view);
goto error;
}
}
if (message_view->service_id_3 != 0) {
if (s_iot_st_encode_service_ids(message_view->service_id_3, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding service id 3 while serializing message",
(void *)message_view);
goto error;
}
}
} else if (message_view->service_id != NULL) {
if (s_iot_st_encode_service_id(message_view->service_id, buffer)) {
goto cleanup;
AWS_LOGF_ERROR(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
"id=%p: Failure encoding service id while serializing message",
(void *)message_view);
goto error;
}
}

return AWS_OP_SUCCESS;

cleanup:
error:
aws_byte_buf_clean_up(buffer);
return AWS_OP_ERR;
return aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_ENCODE_FAILURE);
}

/*****************************************************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion tests/secure_tunnel_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ static int s_secure_tunneling_max_payload_exceed_test_fn(struct aws_allocator *a

int result = aws_secure_tunnel_send_message(secure_tunnel, &data_message_view);

ASSERT_INT_EQUALS(result, AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_OPTIONS_VALIDATION);
ASSERT_INT_EQUALS(result, AWS_OP_ERR);
sbSteveK marked this conversation as resolved.
Show resolved Hide resolved

ASSERT_SUCCESS(aws_secure_tunnel_stop(secure_tunnel));
s_wait_for_connection_shutdown(&test_fixture);
Expand Down