Skip to content

Commit 77f657e

Browse files
dcgawsGordon Wang
authored and
Gordon Wang
committed
Use AWS IoT Core support for MQTT over port 443 (#141)
* Use AWS IoT Core support for MQTT over port 443 in the five sample projects (this is done using ALPN during TLS negotiation). Also, fix a compiler warning in the three subscribe/publish projects. * Use AWS IoT Core support for MQTT over port 443 in the tests.
1 parent d3ea2e8 commit 77f657e

19 files changed

+28
-18
lines changed

include/aws_iot_shadow_interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern "C" {
5151
*/
5252
typedef struct {
5353
char *pHost; ///< This will be unique to a customer and can be retrieved from the console
54-
uint16_t port; ///< By default the port is 8883
54+
uint16_t port; ///< Network port for TCP/IP socket
5555
char *pRootCA; ///< Location with the Filename of the Root CA
5656
char *pClientCRT; ///< Location of Device certs signed by AWS IoT service
5757
char *pClientKey; ///< Location of Device private key

platform/linux/mbedtls/network_mbedtls_wrapper.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ IoT_Error_t iot_tls_connect(Network *pNetwork, TLSConnectParams *params) {
101101
TLSDataParams *tlsDataParams = NULL;
102102
char portBuffer[6];
103103
char vrfy_buf[512];
104+
const char *alpnProtocols[] = { "x-amzn-mqtt-ca", NULL };
104105

105106
#ifdef ENABLE_IOT_DEBUG
106107
unsigned char buf[MBEDTLS_DEBUG_BUFFER_SIZE];
@@ -202,6 +203,15 @@ IoT_Error_t iot_tls_connect(Network *pNetwork, TLSConnectParams *params) {
202203

203204
mbedtls_ssl_conf_read_timeout(&(tlsDataParams->conf), pNetwork->tlsConnectParams.timeout_ms);
204205

206+
/* Use the AWS IoT ALPN extension for MQTT if port 443 is requested. */
207+
if(443 == pNetwork->tlsConnectParams.DestinationPort) {
208+
if((ret = mbedtls_ssl_conf_alpn_protocols(&(tlsDataParams->conf), alpnProtocols)) != 0) {
209+
IOT_ERROR(" failed\n ! mbedtls_ssl_conf_alpn_protocols returned -0x%x\n\n", -ret);
210+
return SSL_CONNECTION_ERROR;
211+
}
212+
}
213+
214+
/* Assign the resulting configuration to the SSL context. */
205215
if((ret = mbedtls_ssl_setup(&(tlsDataParams->ssl), &(tlsDataParams->conf))) != 0) {
206216
IOT_ERROR(" failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", -ret);
207217
return SSL_CONNECTION_ERROR;

samples/linux/jobs_sample/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Get from console
2525
// =================================================
2626
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
27-
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
27+
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
2828
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
2929
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
3030
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name

samples/linux/shadow_sample/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Get from console
2525
// =================================================
2626
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
27-
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
27+
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
2828
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
2929
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
3030
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name

samples/linux/shadow_sample_console_echo/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Get from console
2525
// =================================================
2626
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
27-
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
27+
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
2828
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
2929
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
3030
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name

samples/linux/subscribe_publish_cpp_sample/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Get from console
2525
// =================================================
2626
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
27-
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
27+
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
2828
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
2929
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
3030
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name

samples/linux/subscribe_publish_cpp_sample/subscribe_publish_cpp_sample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, ui
6262
IOT_UNUSED(pData);
6363
IOT_UNUSED(pClient);
6464
IOT_INFO("Subscribe callback");
65-
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, params->payload);
65+
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload);
6666
}
6767

6868
void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) {

samples/linux/subscribe_publish_library_sample/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Get from console
2525
// =================================================
2626
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
27-
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
27+
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
2828
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
2929
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
3030
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name

samples/linux/subscribe_publish_library_sample/subscribe_publish_library_sample.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, ui
6262
IOT_UNUSED(pData);
6363
IOT_UNUSED(pClient);
6464
IOT_INFO("Subscribe callback");
65-
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, params->payload);
65+
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload);
6666
}
6767

6868
void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) {

samples/linux/subscribe_publish_sample/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Get from console
2525
// =================================================
2626
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
27-
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
27+
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
2828
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
2929
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
3030
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name

samples/linux/subscribe_publish_sample/subscribe_publish_sample.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, ui
6262
IOT_UNUSED(pData);
6363
IOT_UNUSED(pClient);
6464
IOT_INFO("Subscribe callback");
65-
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, params->payload);
65+
IOT_INFO("%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *) params->payload);
6666
}
6767

6868
void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) {

tests/integration/include/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// Get from console
2020
// =================================================
2121
#define AWS_IOT_MQTT_HOST "" ///< Customer specific MQTT HOST. The same will be used for Thing Shadow
22-
#define AWS_IOT_MQTT_PORT 8883 ///< default port for MQTT/S
22+
#define AWS_IOT_MQTT_PORT 443 ///< default port for MQTT/S
2323
#define AWS_IOT_MQTT_CLIENT_ID "c-sdk-client-id" ///< MQTT client ID should be unique for every device
2424
#define AWS_IOT_MY_THING_NAME "AWS-IoT-C-SDK" ///< Thing Name of the Shadow this device is associated with
2525
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt" ///< Root CA file name

tests/integration/multithreadingTest/aws_iot_test_multithreading_validation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ int aws_iot_mqtt_tests_multi_threading_validation() {
226226

227227
IOT_DEBUG(" Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
228228
initParams.pHostURL = AWS_IOT_MQTT_HOST;
229-
initParams.port = 8883;
229+
initParams.port = AWS_IOT_MQTT_PORT;
230230
initParams.pRootCALocation = root_CA;
231231
initParams.pDeviceCertLocation = clientCRT;
232232
initParams.pDevicePrivateKeyLocation = clientKey;

tests/integration/src/aws_iot_test_auto_reconnect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int aws_iot_mqtt_tests_auto_reconnect() {
101101
printf(" Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
102102
IoT_Client_Init_Params initParams;
103103
initParams.pHostURL = AWS_IOT_MQTT_HOST;
104-
initParams.port = 8883;
104+
initParams.port = AWS_IOT_MQTT_PORT;
105105
initParams.pRootCALocation = root_CA;
106106
initParams.pDeviceCertLocation = clientCRT;
107107
initParams.pDevicePrivateKeyLocation = clientKey;

tests/integration/src/aws_iot_test_basic_connectivity.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ int aws_iot_mqtt_tests_basic_connectivity() {
190190

191191
IOT_DEBUG("Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
192192
initParams.pHostURL = AWS_IOT_MQTT_HOST;
193-
initParams.port = 8883;
193+
initParams.port = AWS_IOT_MQTT_PORT;
194194
initParams.pRootCALocation = root_CA;
195195
initParams.pDeviceCertLocation = clientCRT;
196196
initParams.pDevicePrivateKeyLocation = clientKey;

tests/integration/src/aws_iot_test_jobs_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ int aws_iot_jobs_basic_test() {
191191

192192
IOT_DEBUG("Root CA Path : %s\n clientCRT : %s\n clientKey : %s\n", root_CA, clientCRT, clientKey);
193193
initParams.pHostURL = AWS_IOT_MQTT_HOST;
194-
initParams.port = 8883;
194+
initParams.port = AWS_IOT_MQTT_PORT;
195195
initParams.pRootCALocation = root_CA;
196196
initParams.pDeviceCertLocation = clientCRT;
197197
initParams.pDevicePrivateKeyLocation = clientKey;

tests/integration/src/aws_iot_test_multiple_clients.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static IoT_Error_t aws_iot_mqtt_tests_connect_client_to_service(AWS_IoT_Client *
6666
struct timeval start, end;
6767

6868
initParams.pHostURL = AWS_IOT_MQTT_HOST;
69-
initParams.port = 8883;
69+
initParams.port = AWS_IOT_MQTT_PORT;
7070
initParams.pRootCALocation = rootCA;
7171
initParams.pDeviceCertLocation = clientCRT;
7272
initParams.pDevicePrivateKeyLocation = clientKey;

tests/unit/include/aws_iot_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Get from console
2525
// =================================================
2626
#define AWS_IOT_MQTT_HOST "localhost"
27-
#define AWS_IOT_MQTT_PORT 8883
27+
#define AWS_IOT_MQTT_PORT 443
2828
#define AWS_IOT_MQTT_CLIENT_ID "C-SDK_UnitTestClient"
2929
#define AWS_IOT_MY_THING_NAME "C-SDK_UnitTestThing"
3030
#define AWS_IOT_ROOT_CA_FILENAME "rootCA.crt"

tests/unit/src/aws_iot_tests_unit_disconnect_helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ TEST_C(DisconnectTests, SetHandlerAndInvokedOnDisconnect) {
179179
IOT_DEBUG("-->Running Disconnect Tests - F:7 - Disconnect, with set handler and invoked on disconnect \n");
180180

181181
handlerInvoked = false;
182-
InitMQTTParamsSetup(&initParams, "localhost", 8883, false, NULL);
182+
InitMQTTParamsSetup(&initParams, "localhost", AWS_IOT_MQTT_PORT, false, NULL);
183183
rc = aws_iot_mqtt_init(&iotClient, &initParams);
184184
CHECK_EQUAL_C_INT(SUCCESS, rc);
185185

0 commit comments

Comments
 (0)