Skip to content

Commit be23f82

Browse files
authored
error handling improvements (#107)
Improved and more accurate reporting of errors related to Secure Tunnel.
1 parent 6809809 commit be23f82

File tree

7 files changed

+254
-271
lines changed

7 files changed

+254
-271
lines changed

include/aws/iotdevice/iotdevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum aws_iotdevice_error {
4747
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_NO_ACTIVE_CONNECTION,
4848
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_PROTOCOL_VERSION_MISMATCH,
4949
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID,
50+
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_ENCODE_FAILURE,
5051

5152
AWS_ERROR_END_IOTDEVICE_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_C_IOTDEVICE_PACKAGE_ID),
5253
};

include/aws/iotdevice/private/secure_tunneling_operations.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ AWS_IOTDEVICE_API void aws_secure_tunnel_operation_complete(
113113
int error_code,
114114
const void *associated_view);
115115

116-
AWS_IOTDEVICE_API void aws_secure_tunnel_operation_assign_stream_id(
117-
struct aws_secure_tunnel_operation *operation,
118-
struct aws_secure_tunnel *secure_tunnel);
119-
120116
AWS_IOTDEVICE_API int32_t
121117
aws_secure_tunnel_operation_get_stream_id(const struct aws_secure_tunnel_operation *operation);
122118

source/iotdevice.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static struct aws_error_info s_errors[] = {
9292
"Secure Tunnel terminated by user request."),
9393
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
9494
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DECODE_FAILURE,
95-
"Error occured while decoding an incoming message." ),
95+
"Error occurred while decoding an incoming message." ),
9696
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
9797
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_NO_ACTIVE_CONNECTION,
9898
"DATA message processing failed due to no active connection found." ),
@@ -102,6 +102,9 @@ static struct aws_error_info s_errors[] = {
102102
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
103103
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID,
104104
"Secure Tunnel operation failed due to using inactive service id." ),
105+
AWS_DEFINE_ERROR_INFO_IOTDEVICE(
106+
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_ENCODE_FAILURE,
107+
"Error occured while encoding an outbound message." ),
105108
};
106109
/* clang-format on */
107110
#undef AWS_DEFINE_ERROR_INFO_IOTDEVICE

source/secure_tunneling.c

Lines changed: 184 additions & 223 deletions
Large diffs are not rendered by default.

source/secure_tunneling_operations.c

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,6 @@ void aws_secure_tunnel_operation_complete(
128128
}
129129
}
130130

131-
void aws_secure_tunnel_operation_assign_stream_id(
132-
struct aws_secure_tunnel_operation *operation,
133-
struct aws_secure_tunnel *secure_tunnel) {
134-
AWS_FATAL_ASSERT(operation->vtable != NULL);
135-
if (operation->vtable->aws_secure_tunnel_operation_assign_stream_id_fn != NULL) {
136-
(*operation->vtable->aws_secure_tunnel_operation_assign_stream_id_fn)(operation, secure_tunnel);
137-
}
138-
}
139-
140131
static struct aws_secure_tunnel_operation_vtable s_empty_operation_vtable = {
141132
.aws_secure_tunnel_operation_completion_fn = NULL,
142133
.aws_secure_tunnel_operation_assign_stream_id_fn = NULL,
@@ -362,33 +353,26 @@ static int s_aws_secure_tunnel_operation_message_assign_stream_id(
362353

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

365-
int error_code = AWS_OP_SUCCESS;
366-
367356
if (message_view->service_id == NULL || message_view->service_id->len == 0) {
368357
stream_id = secure_tunnel->connections->stream_id;
369358
} else {
370359
struct aws_hash_element *elem = NULL;
371360
aws_hash_table_find(&secure_tunnel->connections->service_ids, message_view->service_id, &elem);
372361
if (elem == NULL) {
373-
AWS_LOGF_WARN(
374-
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
375-
"id=%p: invalid service id '" PRInSTR "' attempted to be assigned a stream id on an outbound message",
376-
(void *)message_view,
377-
AWS_BYTE_CURSOR_PRI(*message_view->service_id));
378-
error_code = AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_SERVICE_ID;
362+
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_SERVICE_ID);
379363
goto error;
380364
}
381365
struct aws_service_id_element *service_id_elem = elem->value;
382366
stream_id = service_id_elem->stream_id;
383367

384368
if (stream_id == INVALID_STREAM_ID) {
385-
error_code = AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID;
369+
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INACTIVE_SERVICE_ID);
386370
goto error;
387371
}
388372
}
389373

390374
if (stream_id == INVALID_STREAM_ID) {
391-
error_code = AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID;
375+
aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID);
392376
goto error;
393377
}
394378

@@ -397,21 +381,21 @@ static int s_aws_secure_tunnel_operation_message_assign_stream_id(
397381

398382
error:
399383
if (message_view->service_id == NULL || message_view->service_id->len == 0) {
400-
AWS_LOGF_DEBUG(
384+
AWS_LOGF_WARN(
401385
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
402386
"id=%p: No active stream to assign outbound %s message a stream id",
403387
(void *)secure_tunnel,
404388
aws_secure_tunnel_message_type_to_c_string(message_view->type));
405389
} else {
406-
AWS_LOGF_DEBUG(
390+
AWS_LOGF_WARN(
407391
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
408392
"id=%p: No active stream with service id '" PRInSTR "' to assign outbound %s message a stream id",
409393
(void *)secure_tunnel,
410394
AWS_BYTE_CURSOR_PRI(*message_view->service_id),
411395
aws_secure_tunnel_message_type_to_c_string(message_view->type));
412396
}
413397

414-
return aws_raise_error(error_code);
398+
return AWS_OP_ERR;
415399
}
416400

417401
/*
@@ -624,9 +608,6 @@ struct aws_secure_tunnel_operation_message *aws_secure_tunnel_operation_message_
624608

625609
struct aws_secure_tunnel_operation_message *message_op =
626610
aws_mem_calloc(allocator, 1, sizeof(struct aws_secure_tunnel_operation_message));
627-
if (message_op == NULL) {
628-
return NULL;
629-
}
630611

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

647628
aws_secure_tunnel_operation_release(&message_op->base);
648-
649629
return NULL;
650630
}
651631

@@ -991,7 +971,6 @@ struct data_tunnel_pair *aws_secure_tunnel_data_tunnel_pair_new(
991971
pair->type = message_view->type;
992972
pair->length_prefix_written = false;
993973
if (aws_iot_st_msg_serialize_from_view(&pair->buf, allocator, message_view)) {
994-
AWS_LOGF_ERROR(AWS_LS_IOTDEVICE_SECURE_TUNNELING, "Failure serializing message");
995974
goto error;
996975
}
997976

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

1002981
error:
1003-
1004982
aws_secure_tunnel_data_tunnel_pair_destroy(pair);
1005983
return NULL;
1006984
}

source/serializer.c

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ int aws_iot_st_msg_serialize_from_view(
247247
const struct aws_secure_tunnel_message_view *message_view) {
248248
size_t message_total_length = 0;
249249
if (s_iot_st_compute_message_length(message_view, &message_total_length)) {
250+
AWS_LOGF_ERROR(
251+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
252+
"id=%p: Failure computing message length while serializing message",
253+
(void *)message_view);
250254
return AWS_OP_ERR;
251255
}
252256

@@ -256,70 +260,109 @@ int aws_iot_st_msg_serialize_from_view(
256260
(void *)message_view,
257261
message_total_length);
258262

259-
if (aws_byte_buf_init(buffer, allocator, message_total_length) != AWS_OP_SUCCESS) {
263+
if (aws_byte_buf_init(buffer, allocator, message_total_length)) {
260264
return AWS_OP_ERR;
261265
}
262266

263267
if (message_view->type != AWS_SECURE_TUNNEL_MT_UNKNOWN) {
264268
if (s_iot_st_encode_type(message_view->type, buffer)) {
265-
goto cleanup;
269+
AWS_LOGF_ERROR(
270+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
271+
"id=%p: Failure encoding message type while serializing message",
272+
(void *)message_view);
273+
goto error;
266274
}
267275
} else {
268-
AWS_LOGF_ERROR(AWS_LS_IOTDEVICE_SECURE_TUNNELING, "Message missing type during encoding");
269-
goto cleanup;
276+
AWS_LOGF_ERROR(
277+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
278+
"id=%p: Message type missing while serializing message",
279+
(void *)message_view);
280+
goto error;
270281
}
271282

272283
if (message_view->stream_id != 0) {
273284
if (s_iot_st_encode_stream_id(message_view->stream_id, buffer)) {
274-
goto cleanup;
285+
AWS_LOGF_ERROR(
286+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
287+
"id=%p: Failure encoding stream id while serializing message",
288+
(void *)message_view);
289+
goto error;
275290
}
276291
}
277292

278293
if (message_view->connection_id != 0) {
279294
if (s_iot_st_encode_connection_id(message_view->connection_id, buffer)) {
280-
goto cleanup;
295+
AWS_LOGF_ERROR(
296+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
297+
"id=%p: Failure encoding connection id while serializing message",
298+
(void *)message_view);
299+
goto error;
281300
}
282301
}
283302

284303
if (message_view->ignorable != 0) {
285304
if (s_iot_st_encode_ignorable(message_view->ignorable, buffer)) {
286-
goto cleanup;
305+
AWS_LOGF_ERROR(
306+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
307+
"id=%p: Failure encoding ignorable while serializing message",
308+
(void *)message_view);
309+
goto error;
287310
}
288311
}
289312

290313
if (message_view->payload != NULL) {
291314
if (s_iot_st_encode_payload(message_view->payload, buffer)) {
292-
goto cleanup;
315+
AWS_LOGF_ERROR(
316+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
317+
"id=%p: Failure encoding payload while serializing message",
318+
(void *)message_view);
319+
goto error;
293320
}
294321
}
295322

296323
if (message_view->type == AWS_SECURE_TUNNEL_MT_SERVICE_IDS) {
297324
if (message_view->service_id != 0) {
298325
if (s_iot_st_encode_service_ids(message_view->service_id, buffer)) {
299-
goto cleanup;
326+
AWS_LOGF_ERROR(
327+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
328+
"id=%p: Failure encoding service id while serializing message",
329+
(void *)message_view);
330+
goto error;
300331
}
301332
}
302333
if (message_view->service_id_2 != 0) {
303334
if (s_iot_st_encode_service_ids(message_view->service_id_2, buffer)) {
304-
goto cleanup;
335+
AWS_LOGF_ERROR(
336+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
337+
"id=%p: Failure encoding service id 2 while serializing message",
338+
(void *)message_view);
339+
goto error;
305340
}
306341
}
307342
if (message_view->service_id_3 != 0) {
308343
if (s_iot_st_encode_service_ids(message_view->service_id_3, buffer)) {
309-
goto cleanup;
344+
AWS_LOGF_ERROR(
345+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
346+
"id=%p: Failure encoding service id 3 while serializing message",
347+
(void *)message_view);
348+
goto error;
310349
}
311350
}
312351
} else if (message_view->service_id != NULL) {
313352
if (s_iot_st_encode_service_id(message_view->service_id, buffer)) {
314-
goto cleanup;
353+
AWS_LOGF_ERROR(
354+
AWS_LS_IOTDEVICE_SECURE_TUNNELING,
355+
"id=%p: Failure encoding service id while serializing message",
356+
(void *)message_view);
357+
goto error;
315358
}
316359
}
317360

318361
return AWS_OP_SUCCESS;
319362

320-
cleanup:
363+
error:
321364
aws_byte_buf_clean_up(buffer);
322-
return AWS_OP_ERR;
365+
return aws_raise_error(AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_ENCODE_FAILURE);
323366
}
324367

325368
/*****************************************************************************************************************

tests/secure_tunnel_tests.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,8 @@ static int s_secure_tunneling_max_payload_exceed_test_fn(struct aws_allocator *a
14501450

14511451
int result = aws_secure_tunnel_send_message(secure_tunnel, &data_message_view);
14521452

1453-
ASSERT_INT_EQUALS(result, AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_OPTIONS_VALIDATION);
1453+
ASSERT_INT_EQUALS(result, AWS_OP_ERR);
1454+
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_OPTIONS_VALIDATION);
14541455

14551456
ASSERT_SUCCESS(aws_secure_tunnel_stop(secure_tunnel));
14561457
s_wait_for_connection_shutdown(&test_fixture);

0 commit comments

Comments
 (0)