Skip to content

Commit

Permalink
error handling improvements (#107)
Browse files Browse the repository at this point in the history
Improved and more accurate reporting of errors related to Secure Tunnel.
  • Loading branch information
sbSteveK authored Jul 29, 2024
1 parent 6809809 commit be23f82
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 271 deletions.
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
3 changes: 2 additions & 1 deletion tests/secure_tunnel_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,8 @@ 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);
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_OPTIONS_VALIDATION);

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

0 comments on commit be23f82

Please sign in to comment.