From 10a4d216a0676bffa1baef7426843b00718d7bb3 Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Fri, 8 Dec 2023 15:45:43 -0800 Subject: [PATCH 1/5] Adapt to aws-c-http changing `port` from 16 to 32bits --- source/v5/mqtt5_options_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/v5/mqtt5_options_storage.c b/source/v5/mqtt5_options_storage.c index 68a06a88..0be90d42 100644 --- a/source/v5/mqtt5_options_storage.c +++ b/source/v5/mqtt5_options_storage.c @@ -3603,7 +3603,7 @@ void aws_mqtt5_client_options_storage_log( log_handle, level, AWS_LS_MQTT5_GENERAL, - "id=%p: aws_mqtt5_client_options_storage http proxy port set to %" PRIu16, + "id=%p: aws_mqtt5_client_options_storage http proxy port set to %" PRIu32, (void *)options_storage, options_storage->http_proxy_options.port); From e0e9019bd0a395dbc084b56e05364865078019ee Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Mon, 11 Dec 2023 08:56:42 -0800 Subject: [PATCH 2/5] 32bit ports for everything, even MQTT --- bin/elastipubsub5/main.c | 2 +- bin/mqtt5canary/main.c | 4 ++-- include/aws/mqtt/client.h | 2 +- include/aws/mqtt/private/client_impl.h | 2 +- include/aws/mqtt/private/v5/mqtt5_options_storage.h | 2 +- include/aws/mqtt/v5/mqtt5_client.h | 2 +- source/v5/mqtt5_options_storage.c | 2 +- source/v5/mqtt5_to_mqtt3_adapter.c | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bin/elastipubsub5/main.c b/bin/elastipubsub5/main.c index f3ac686b..2c84e796 100644 --- a/bin/elastipubsub5/main.c +++ b/bin/elastipubsub5/main.c @@ -44,7 +44,7 @@ struct app_ctx { struct aws_mutex lock; struct aws_condition_variable signal; struct aws_uri uri; - uint16_t port; + uint32_t port; const char *cacert; const char *cert; const char *key; diff --git a/bin/mqtt5canary/main.c b/bin/mqtt5canary/main.c index cf7c50bc..4431d8f0 100644 --- a/bin/mqtt5canary/main.c +++ b/bin/mqtt5canary/main.c @@ -45,7 +45,7 @@ struct app_ctx { struct aws_mutex lock; struct aws_condition_variable signal; struct aws_uri uri; - uint16_t port; + uint32_t port; const char *cacert; const char *cert; const char *key; @@ -181,7 +181,7 @@ static void s_parse_options( ctx->use_websockets = true; break; case 'p': - ctx->port = (uint16_t)atoi(aws_cli_optarg); + ctx->port = (uint32_t)atoi(aws_cli_optarg); break; case 't': tester_options->elg_max_threads = (uint16_t)atoi(aws_cli_optarg); diff --git a/include/aws/mqtt/client.h b/include/aws/mqtt/client.h index de2934a7..8d85bfe8 100644 --- a/include/aws/mqtt/client.h +++ b/include/aws/mqtt/client.h @@ -254,7 +254,7 @@ struct aws_mqtt_topic_subscription { */ struct aws_mqtt_connection_options { struct aws_byte_cursor host_name; - uint16_t port; + uint32_t port; struct aws_socket_options *socket_options; struct aws_tls_connection_options *tls_options; struct aws_byte_cursor client_id; diff --git a/include/aws/mqtt/private/client_impl.h b/include/aws/mqtt/private/client_impl.h index 4dc13cf4..1d0dd67a 100644 --- a/include/aws/mqtt/private/client_impl.h +++ b/include/aws/mqtt/private/client_impl.h @@ -199,7 +199,7 @@ struct aws_mqtt_client_connection_311_impl { /* The host information, changed by user when state is AWS_MQTT_CLIENT_STATE_DISCONNECTED */ struct aws_string *host_name; - uint16_t port; + uint32_t port; struct aws_tls_connection_options tls_options; struct aws_socket_options socket_options; struct aws_http_proxy_config *http_proxy_config; diff --git a/include/aws/mqtt/private/v5/mqtt5_options_storage.h b/include/aws/mqtt/private/v5/mqtt5_options_storage.h index 9fe14817..22e39e83 100644 --- a/include/aws/mqtt/private/v5/mqtt5_options_storage.h +++ b/include/aws/mqtt/private/v5/mqtt5_options_storage.h @@ -141,7 +141,7 @@ struct aws_mqtt5_client_options_storage { struct aws_allocator *allocator; struct aws_string *host_name; - uint16_t port; + uint32_t port; struct aws_client_bootstrap *bootstrap; struct aws_socket_options socket_options; diff --git a/include/aws/mqtt/v5/mqtt5_client.h b/include/aws/mqtt/v5/mqtt5_client.h index b14d7790..3f137f23 100644 --- a/include/aws/mqtt/v5/mqtt5_client.h +++ b/include/aws/mqtt/v5/mqtt5_client.h @@ -534,7 +534,7 @@ struct aws_mqtt5_client_options { /** * Port to establish mqtt connections to */ - uint16_t port; + uint32_t port; /** * Client bootstrap to use whenever this client establishes a connection diff --git a/source/v5/mqtt5_options_storage.c b/source/v5/mqtt5_options_storage.c index 0be90d42..5e41bb20 100644 --- a/source/v5/mqtt5_options_storage.c +++ b/source/v5/mqtt5_options_storage.c @@ -3542,7 +3542,7 @@ void aws_mqtt5_client_options_storage_log( log_handle, level, AWS_LS_MQTT5_GENERAL, - "id=%p: aws_mqtt5_client_options_storage port set to %" PRIu16, + "id=%p: aws_mqtt5_client_options_storage port set to %" PRIu32, (void *)options_storage, options_storage->port); diff --git a/source/v5/mqtt5_to_mqtt3_adapter.c b/source/v5/mqtt5_to_mqtt3_adapter.c index e48ca3d3..129c0132 100644 --- a/source/v5/mqtt5_to_mqtt3_adapter.c +++ b/source/v5/mqtt5_to_mqtt3_adapter.c @@ -199,7 +199,7 @@ struct aws_mqtt_adapter_connect_task { struct aws_mqtt_client_connection_5_impl *adapter; struct aws_byte_buf host_name; - uint16_t port; + uint32_t port; struct aws_socket_options socket_options; struct aws_tls_connection_options *tls_options_ptr; struct aws_tls_connection_options tls_options; From b13eabb7ced835c6646c0991963c8b499b07e4c1 Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Tue, 12 Dec 2023 16:05:50 -0800 Subject: [PATCH 3/5] missed 16bit port in sample code --- bin/elastipubsub/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/elastipubsub/main.c b/bin/elastipubsub/main.c index 04085169..1745fe9a 100644 --- a/bin/elastipubsub/main.c +++ b/bin/elastipubsub/main.c @@ -37,7 +37,7 @@ struct app_ctx { struct aws_mutex lock; struct aws_condition_variable signal; struct aws_uri uri; - uint16_t port; + uint32_t port; const char *cacert; const char *cert; const char *key; From b6e677387c5a92d0e0c9e93b0c79570f50510cb2 Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Fri, 15 Dec 2023 16:13:08 -0800 Subject: [PATCH 4/5] validate port --- source/v5/mqtt5_options_storage.c | 10 ++++++++-- tests/CMakeLists.txt | 1 + tests/v5/mqtt5_operation_validation_failure_tests.c | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/source/v5/mqtt5_options_storage.c b/source/v5/mqtt5_options_storage.c index 5e41bb20..975e52e3 100644 --- a/source/v5/mqtt5_options_storage.c +++ b/source/v5/mqtt5_options_storage.c @@ -3356,14 +3356,20 @@ int aws_mqtt5_client_options_validate(const struct aws_mqtt5_client_options *opt } } + if (aws_socket_validate_port_for_connect( + options->port, options->socket_options ? options->socket_options->domain : AWS_SOCKET_IPV4)) { + AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid port in mqtt5 client configuration"); + return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); + } + if (options->http_proxy_options != NULL) { if (options->http_proxy_options->host.len == 0) { AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "proxy host name not set in mqtt5 client configuration"); return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); } - if (options->http_proxy_options->port == 0) { - AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "proxy port not set in mqtt5 client configuration"); + if (aws_socket_validate_port_for_connect(options->http_proxy_options->port, AWS_SOCKET_IPV4)) { + AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid proxy port in mqtt5 client configuration"); return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3494aa2e..b8ef2587 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -244,6 +244,7 @@ add_test_case(mqtt5_client_options_validation_failure_no_publish_received) add_test_case(mqtt5_client_options_validation_failure_invalid_socket_options) add_test_case(mqtt5_client_options_validation_failure_invalid_connect) add_test_case(mqtt5_client_options_validation_failure_invalid_keep_alive) +add_test_case(mqtt5_client_options_validation_failure_invalid_port) add_test_case(mqtt5_operation_subscribe_connection_settings_validation_failure_exceeds_maximum_packet_size) add_test_case(mqtt5_operation_unsubscribe_connection_settings_validation_failure_exceeds_maximum_packet_size) add_test_case(mqtt5_operation_publish_connection_settings_validation_failure_exceeds_maximum_packet_size) diff --git a/tests/v5/mqtt5_operation_validation_failure_tests.c b/tests/v5/mqtt5_operation_validation_failure_tests.c index 1bb2b814..6573eeaf 100644 --- a/tests/v5/mqtt5_operation_validation_failure_tests.c +++ b/tests/v5/mqtt5_operation_validation_failure_tests.c @@ -1113,6 +1113,7 @@ static struct aws_mqtt5_client_options s_good_client_options = { .ptr = s_server_reference, .len = AWS_ARRAY_SIZE(s_server_reference) - 1, }, + .port = 1883, .socket_options = &s_good_socket_options, .connect_options = &s_good_connect, .ping_timeout_ms = 5000, @@ -1183,6 +1184,15 @@ AWS_CLIENT_CREATION_VALIDATION_FAILURE( s_good_client_options, s_make_invalid_keep_alive_client_options) +static void s_make_invalid_port_client_options(struct aws_mqtt5_client_options *options) { + options->port = 0xFFFFFFFF; +} + +AWS_CLIENT_CREATION_VALIDATION_FAILURE( + invalid_port, + s_good_client_options, + s_make_invalid_port_client_options) + #define AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_PREFIX(packet_type, failure_reason, init_success_settings_fn) \ static int s_mqtt5_operation_##packet_type##_connection_settings_validation_failure_##failure_reason##_fn( \ struct aws_allocator *allocator, void *ctx) { \ From 0b7bc6787c26b09e3bad6837e93db39ffc10059f Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Fri, 15 Dec 2023 16:32:22 -0800 Subject: [PATCH 5/5] clang-format --- tests/v5/mqtt5_operation_validation_failure_tests.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/v5/mqtt5_operation_validation_failure_tests.c b/tests/v5/mqtt5_operation_validation_failure_tests.c index 6573eeaf..80c0f978 100644 --- a/tests/v5/mqtt5_operation_validation_failure_tests.c +++ b/tests/v5/mqtt5_operation_validation_failure_tests.c @@ -1188,10 +1188,7 @@ static void s_make_invalid_port_client_options(struct aws_mqtt5_client_options * options->port = 0xFFFFFFFF; } -AWS_CLIENT_CREATION_VALIDATION_FAILURE( - invalid_port, - s_good_client_options, - s_make_invalid_port_client_options) +AWS_CLIENT_CREATION_VALIDATION_FAILURE(invalid_port, s_good_client_options, s_make_invalid_port_client_options) #define AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_PREFIX(packet_type, failure_reason, init_success_settings_fn) \ static int s_mqtt5_operation_##packet_type##_connection_settings_validation_failure_##failure_reason##_fn( \