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; 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 2f92e124..d04d2981 100644 --- a/include/aws/mqtt/v5/mqtt5_client.h +++ b/include/aws/mqtt/v5/mqtt5_client.h @@ -526,7 +526,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 68a06a88..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); } } @@ -3542,7 +3548,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); @@ -3603,7 +3609,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); 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; 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..80c0f978 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,12 @@ 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) { \