From 5e6e38947c6136e71e0bae8c06a98629fcf51803 Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Fri, 28 Jan 2022 20:30:19 +0000 Subject: [PATCH] Set default port for WSS to 443 #1048 --- src/MQTTAsyncUtils.c | 10 +++++----- src/MQTTClient.c | 8 ++++---- src/MQTTProtocolOut.c | 8 +++++--- src/MQTTProtocolOut.h | 3 ++- src/WebSocket.c | 5 +++-- src/WebSocket.h | 4 ++-- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/MQTTAsyncUtils.c b/src/MQTTAsyncUtils.c index f57ad051..1a37b561 100644 --- a/src/MQTTAsyncUtils.c +++ b/src/MQTTAsyncUtils.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2021 IBM Corp., Ian Craggs and others + * Copyright (c) 2009, 2022 IBM Corp., Ian Craggs and others * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -2719,7 +2719,7 @@ static int MQTTAsync_connecting(MQTTAsyncs* m) else if (strncmp(URI_WSS, serverURI, strlen(URI_WSS)) == 0) { serverURI += strlen(URI_WSS); - default_port = WS_DEFAULT_PORT; + default_port = WSS_DEFAULT_PORT; } #endif } @@ -2779,7 +2779,7 @@ static int MQTTAsync_connecting(MQTTAsyncs* m) if ( m->websocket ) { m->c->connect_state = WEBSOCKET_IN_PROGRESS; - if ((rc = WebSocket_connect(&m->c->net, serverURI)) == SOCKET_ERROR ) + if ((rc = WebSocket_connect(&m->c->net, m->ssl, serverURI)) == SOCKET_ERROR ) goto exit; } else @@ -2815,7 +2815,7 @@ static int MQTTAsync_connecting(MQTTAsyncs* m) if ( m->websocket ) { m->c->connect_state = WEBSOCKET_IN_PROGRESS; - if ((rc = WebSocket_connect(&m->c->net, serverURI)) == SOCKET_ERROR ) + if ((rc = WebSocket_connect(&m->c->net, 0, serverURI)) == SOCKET_ERROR ) goto exit; } else @@ -2846,7 +2846,7 @@ static int MQTTAsync_connecting(MQTTAsyncs* m) if ( m->websocket ) { m->c->connect_state = WEBSOCKET_IN_PROGRESS; - if ((rc = WebSocket_connect(&m->c->net, serverURI)) == SOCKET_ERROR ) + if ((rc = WebSocket_connect(&m->c->net, 1, serverURI)) == SOCKET_ERROR ) goto exit; } else diff --git a/src/MQTTClient.c b/src/MQTTClient.c index 275e075e..8202c467 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2021 IBM Corp., Ian Craggs and others + * Copyright (c) 2009, 2022 IBM Corp., Ian Craggs and others * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -1267,7 +1267,7 @@ static MQTTResponse MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_c if (m->websocket) { m->c->connect_state = WEBSOCKET_IN_PROGRESS; - rc = WebSocket_connect(&m->c->net, serverURI); + rc = WebSocket_connect(&m->c->net, 1, serverURI); if ( rc == SOCKET_ERROR ) goto exit; } @@ -1303,7 +1303,7 @@ static MQTTResponse MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_c if (m->websocket) { m->c->connect_state = WEBSOCKET_IN_PROGRESS; - if ( WebSocket_connect(&m->c->net, serverURI) == SOCKET_ERROR ) + if ( WebSocket_connect(&m->c->net, 0, serverURI) == SOCKET_ERROR ) { rc = SOCKET_ERROR; goto exit; @@ -1339,7 +1339,7 @@ static MQTTResponse MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_c { /* wait for websocket connect */ m->c->connect_state = WEBSOCKET_IN_PROGRESS; - rc = WebSocket_connect( &m->c->net, serverURI ); + rc = WebSocket_connect( &m->c->net, 1, serverURI); if ( rc != 1 ) { rc = SOCKET_ERROR; diff --git a/src/MQTTProtocolOut.c b/src/MQTTProtocolOut.c index 184ff85b..1d6aab83 100644 --- a/src/MQTTProtocolOut.c +++ b/src/MQTTProtocolOut.c @@ -285,9 +285,11 @@ int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int websocket #endif else { #if defined(OPENSSL) - addr_len = MQTTProtocol_addressPort(ip_address, &port, NULL, ssl ? SECURE_MQTT_DEFAULT_PORT : MQTT_DEFAULT_PORT); + addr_len = MQTTProtocol_addressPort(ip_address, &port, NULL, ssl ? + (websocket ? WSS_DEFAULT_PORT : SECURE_MQTT_DEFAULT_PORT) : + (websocket ? WS_DEFAULT_PORT : MQTT_DEFAULT_PORT) ); #else - addr_len = MQTTProtocol_addressPort(ip_address, &port, NULL, MQTT_DEFAULT_PORT); + addr_len = MQTTProtocol_addressPort(ip_address, &port, NULL, websocket ? WS_DEFAULT_PORT : MQTT_DEFAULT_PORT); #endif #if defined(__GNUC__) && defined(__linux__) if (timeout < 0) @@ -331,7 +333,7 @@ int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int websocket } if ( websocket ) { - rc = WebSocket_connect( &aClient->net, ip_address ); + rc = WebSocket_connect( &aClient->net, 0, ip_address ); if ( rc == TCPSOCKET_INTERRUPTED ) aClient->connect_state = WEBSOCKET_IN_PROGRESS; /* Websocket connect called - wait for completion */ } diff --git a/src/MQTTProtocolOut.h b/src/MQTTProtocolOut.h index d8f43d73..784a5094 100644 --- a/src/MQTTProtocolOut.h +++ b/src/MQTTProtocolOut.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2021 IBM Corp., Ian Craggs + * Copyright (c) 2009, 2022 IBM Corp., Ian Craggs * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -33,6 +33,7 @@ #define MQTT_DEFAULT_PORT 1883 #define SECURE_MQTT_DEFAULT_PORT 8883 #define WS_DEFAULT_PORT 80 +#define WSS_DEFAULT_PORT 443 #define PROXY_DEFAULT_PORT 8080 size_t MQTTProtocol_addressPort(const char* uri, int* port, const char **topic, int default_port); diff --git a/src/WebSocket.c b/src/WebSocket.c index 42671d4c..25b1eecd 100644 --- a/src/WebSocket.c +++ b/src/WebSocket.c @@ -369,6 +369,7 @@ static void WebSocket_unmaskData(size_t idx, PacketBuffers* bufs) * sends out a websocket request on the given uri * * @param[in] net network connection + * @param[in] ssl ssl flag * @param[in] uri uri to connect to * * @retval SOCKET_ERROR on failure @@ -376,7 +377,7 @@ static void WebSocket_unmaskData(size_t idx, PacketBuffers* bufs) * * @see WebSocket_upgrade */ -int WebSocket_connect( networkHandles *net, const char *uri) +int WebSocket_connect( networkHandles *net, int ssl, const char *uri) { int rc; char *buf = NULL; @@ -413,7 +414,7 @@ int WebSocket_connect( networkHandles *net, const char *uri) Base64_encode( net->websocket_key, 25u, uuid, sizeof(uuid_t) ); #endif /* else if defined(_WIN32) || defined(_WIN64) */ - hostname_len = MQTTProtocol_addressPort(uri, &port, &topic, WS_DEFAULT_PORT); + hostname_len = MQTTProtocol_addressPort(uri, &port, &topic, ssl ? WSS_DEFAULT_PORT : WS_DEFAULT_PORT); /* if no topic, use default */ if ( !topic ) diff --git a/src/WebSocket.h b/src/WebSocket.h index 1183c969..4e437d4a 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2018, 2020 Wind River Systems, Inc. and others. All Rights Reserved. + * Copyright (c) 2018, 2022 Wind River Systems, Inc. and others. All Rights Reserved. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -55,7 +55,7 @@ void WebSocket_close(networkHandles *net, int status_code, const char *reason); /* sends upgrade request */ -int WebSocket_connect(networkHandles *net, const char *uri); +int WebSocket_connect(networkHandles *net, int ssl, const char *uri); /* obtain data from network socket */ int WebSocket_getch(networkHandles *net, char* c);