Skip to content

Commit

Permalink
more error stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sbSteveK committed Apr 15, 2024
1 parent 0bf3638 commit b29444b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 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 Down
41 changes: 21 additions & 20 deletions source/secure_tunneling.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ static bool s_aws_secure_tunnel_stream_id_match_check(
return (stream_id == service_id_elem->stream_id);
}

static bool s_aws_secure_tunnel_active_stream_check(
static int s_aws_secure_tunnel_active_stream_check(
const struct aws_secure_tunnel *secure_tunnel,
const struct aws_secure_tunnel_message_view *message_view) {
/*
* No service id means either V1 protocol is being used or V3 protocol is being used on a tunnel without service ids
*/
if (message_view->service_id == NULL || message_view->service_id->len == 0) {
if (secure_tunnel->connections->stream_id != message_view->stream_id) {
return false;
return aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID);
}

uint32_t connection_id = message_view->connection_id;
Expand All @@ -265,25 +265,22 @@ static bool s_aws_secure_tunnel_active_stream_check(
struct aws_hash_element *connection_id_elem = NULL;
aws_hash_table_find(&secure_tunnel->connections->connection_ids, &connection_id, &connection_id_elem);
if (connection_id_elem == NULL) {
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_CONNECTION_ID);
return false;
return aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_CONNECTION_ID);
}
return true;
return AWS_OP_SUCCESS;
}

/* Check if service id is being used by the secure tunnel */
struct aws_hash_element *elem = NULL;
aws_hash_table_find(&secure_tunnel->connections->service_ids, message_view->service_id, &elem);
if (elem == NULL) {
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_SERVICE_ID);
return false;
return aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_SERVICE_ID);
}

/* Check if the stream id is the currently active one */
struct aws_service_id_element *service_id_elem = elem->value;
if (message_view->stream_id != service_id_elem->stream_id) {
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID);
return false;
return aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID);
}

/* V1 and V2 will be considered active at this point with a matching stream id but V3 streams will need to have
Expand All @@ -292,12 +289,11 @@ static bool s_aws_secure_tunnel_active_stream_check(
struct aws_hash_element *connection_id_elem = NULL;
aws_hash_table_find(&service_id_elem->connection_ids, &message_view->connection_id, &connection_id_elem);
if (connection_id_elem == NULL) {
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_CONNECTION_ID);
return false;
return aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_CONNECTION_ID);
}
}

return true;
return AWS_OP_SUCCESS;
}

static int s_aws_secure_tunnel_set_stream(
Expand Down Expand Up @@ -440,6 +436,7 @@ static int s_aws_secure_tunnel_set_connection_id(
.connection_id = connection_id,
};

// TODO unchecked return value
s_aws_secure_tunnel_remove_connection_id(secure_tunnel, &reset_message);
if (secure_tunnel->config->on_connection_reset) {
secure_tunnel->config->on_connection_reset(
Expand Down Expand Up @@ -467,6 +464,8 @@ static int s_aws_secure_tunnel_remove_connection_id(
const struct aws_secure_tunnel_message_view *message_view) {

if (s_aws_secure_tunnel_active_stream_check(secure_tunnel, message_view)) {
return AWS_OP_ERR;
} else {
struct aws_hash_table *table_to_remove_from = NULL;

if (message_view->service_id == NULL || message_view->service_id->len == 0) {
Expand Down Expand Up @@ -494,8 +493,6 @@ static int s_aws_secure_tunnel_remove_connection_id(
AWS_BYTE_CURSOR_PRI(*message_view->service_id),
message_view->connection_id);
}
} else {
return aws_last_error();
}

return AWS_OP_SUCCESS;
Expand Down Expand Up @@ -531,10 +528,6 @@ static void s_aws_secure_tunnel_on_data_received(
}

if (s_aws_secure_tunnel_active_stream_check(secure_tunnel, message_view)) {
if (secure_tunnel->config->on_message_received) {
secure_tunnel->config->on_message_received(message_view, secure_tunnel->config->user_data);
}
} else {
if (message_view->service_id->len > 0) {
AWS_LOGF_INFO(
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
Expand All @@ -552,6 +545,10 @@ static void s_aws_secure_tunnel_on_data_received(
message_view->stream_id,
message_view->connection_id);
}
} else {
if (secure_tunnel->config->on_message_received) {
secure_tunnel->config->on_message_received(message_view, secure_tunnel->config->user_data);
}
}
}

Expand Down Expand Up @@ -784,7 +781,11 @@ static void s_aws_secure_tunnel_on_connection_reset_received(
*/
s_set_absent_connection_id_to_one(message_view, &message_view->connection_id);

int result = s_aws_secure_tunnel_remove_connection_id(secure_tunnel, message_view);
int result = AWS_OP_SUCCESS;

if (s_aws_secure_tunnel_remove_connection_id(secure_tunnel, message_view)) {
result = aws_last_error();
}

if (secure_tunnel->config->on_connection_reset) {
secure_tunnel->config->on_connection_reset(message_view, result, secure_tunnel->config->user_data);
Expand Down Expand Up @@ -1769,7 +1770,7 @@ static void s_process_outbound_data_message(
}

/* If a data message attempts to be sent on an unopen stream, discard it. */
if (!s_aws_secure_tunnel_active_stream_check(secure_tunnel, current_operation->message_view)) {
if (s_aws_secure_tunnel_active_stream_check(secure_tunnel, current_operation->message_view)) {
error_code = aws_last_error();
if (current_operation->message_view->service_id && current_operation->message_view->service_id->len > 0) {
AWS_LOGF_DEBUG(
Expand Down

0 comments on commit b29444b

Please sign in to comment.