From 5ea28e0823ee2e9e554ed7ba8cb90c3d207640e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Macho=C5=84?= Date: Fri, 18 Oct 2019 05:53:19 +0200 Subject: [PATCH 1/6] Skip frame_buffer appending when no data received on websocket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rafał Machoń --- src/WebSocket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocket.c b/src/WebSocket.c index 9cb72c3d..e7af24e9 100644 --- a/src/WebSocket.c +++ b/src/WebSocket.c @@ -654,7 +654,7 @@ char *WebSocket_getRawSocketData( frame_buffer = NULL; } // append data to the buffer - else if (rv != NULL) + else if (rv != NULL && *actual_len != 0U) { // no buffer allocated if (!frame_buffer) From 6bc3ec63bd99954aa2075a017ee63d655fca21bd Mon Sep 17 00:00:00 2001 From: tbeu Date: Wed, 29 Aug 2018 14:00:26 +0200 Subject: [PATCH 2/6] Fix VS2010 macro redefinition warnings ``` ClCompile: SSLSocket.c C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\errno.h(43): warning C4005: 'EINTR': macro redefinition C:\Projects\paho.mqtt.c\src\Socket.h(31): see previous definition of 'EINTR' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\errno.h(50): warning C4005: 'EAGAIN': macro redefinition C:\Projects\paho.mqtt.c\src\Socket.h(29): see previous definition of 'EAGAIN' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\errno.h(100): warning C4005: 'ECONNRESET': macro redefinition C:\Projects\paho.mqtt.c\src\Socket.h(39): see previous definition of 'ECONNRESET' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\errno.h(104): warning C4005: 'EINPROGRESS': macro redefinition C:\Projects\paho.mqtt.c\src\Socket.h(33): see previous definition of 'EINPROGRESS' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\errno.h(118): warning C4005: 'ENOTCONN': macro redefinition C:\Projects\paho.mqtt.c\src\Socket.h(37): see previous definition of 'ENOTCONN' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\errno.h(130): warning C4005: 'ETIMEDOUT': macro redefinition C:\Projects\paho.mqtt.c\src\Thread.h(35): see previous definition of 'ETIMEDOUT' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\errno.h(132): warning C4005: 'EWOULDBLOCK': macro redefinition C:\Projects\paho.mqtt.c\src\Socket.h(35): see previous definition of 'EWOULDBLOCK' ``` Reason is that VS2010 (implicitly) includes errno.h by inclusion of openssl/err.h, hence after inclusion of Socket.h. Whereas, VS2015 (implicitly) includes errno.h by inclusion of SocketBuffer.h, hence before Socket.h. Hint: Compilation with `/showIncludes` reveals the inclusion hierarchy. Signed-off-by: tbeu --- src/Socket.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Socket.h b/src/Socket.h index e0420693..7c384af5 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -21,6 +21,7 @@ #include #if defined(WIN32) || defined(WIN64) +#include #include #include #define MAXHOSTNAMELEN 256 From 62f726928e27c6ab11dc5792321fe4f1922e3199 Mon Sep 17 00:00:00 2001 From: tbeu Date: Thu, 16 Aug 2018 15:07:58 +0200 Subject: [PATCH 3/6] Fix MSVC build errors Signed-off-by: tbeu --- src/WebSocket.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/WebSocket.c b/src/WebSocket.c index c447f97d..ad1a3ed6 100644 --- a/src/WebSocket.c +++ b/src/WebSocket.c @@ -45,6 +45,9 @@ # pragma comment(lib, "rpcrt4.lib") # include # define strncasecmp(s1,s2,c) _strnicmp(s1,s2,c) +# define htonll(x) ( ( (uint64_t)(htonl( (uint32_t)((x << 32) >> 32)))<< 32) | htonl( ((uint32_t)(x >> 32)) )) +# define ntohll(x) htonll(x) + # if BYTE_ORDER == LITTLE_ENDIAN # define htobe16(x) htons(x) # define htobe32(x) htonl(x) @@ -62,14 +65,15 @@ # else # error "unknown endian" # endif - /* For Microsoft Visual Studio 2013 */ -# if !defined( snprintf ) + /* For Microsoft Visual Studio < 2015 */ +# if defined(_MSC_VER) && _MSC_VER < 1900 # define snprintf _snprintf -# endif /* if !defined( snprintf ) */ +# endif #endif #if defined(OPENSSL) #include "SSLSocket.h" +#include #endif /* defined(OPENSSL) */ #include "Socket.h" @@ -81,10 +85,6 @@ #include #include -#if defined(OPENSSL) -#include -#endif /* if defined(OPENSSL) */ - #include "Heap.h" /** @brief raw uuid type */ From 7a79520e5c7403df3ab986f7a749ea958a920be5 Mon Sep 17 00:00:00 2001 From: tbeu Date: Tue, 28 Aug 2018 13:39:21 +0200 Subject: [PATCH 4/6] Use _byteswap_uint64 instead Signed-off-by: tbeu --- src/WebSocket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WebSocket.c b/src/WebSocket.c index ad1a3ed6..04e51c6f 100644 --- a/src/WebSocket.c +++ b/src/WebSocket.c @@ -45,8 +45,8 @@ # pragma comment(lib, "rpcrt4.lib") # include # define strncasecmp(s1,s2,c) _strnicmp(s1,s2,c) -# define htonll(x) ( ( (uint64_t)(htonl( (uint32_t)((x << 32) >> 32)))<< 32) | htonl( ((uint32_t)(x >> 32)) )) -# define ntohll(x) htonll(x) +# define htonll(x) _byteswap_uint64(x) +# define ntohll(x) _byteswap_uint64(x) # if BYTE_ORDER == LITTLE_ENDIAN # define htobe16(x) htons(x) From 422ebce0cbc8aea0408b84447c1176840d0f9213 Mon Sep 17 00:00:00 2001 From: tbeu Date: Thu, 13 Sep 2018 13:09:46 +0200 Subject: [PATCH 5/6] Fix recently introduced MSVC build errors Signed-off-by: tbeu --- src/MQTTAsync.c | 50 ++++++++++++++++++++++++------------------- src/MQTTClient.c | 2 +- src/MQTTPersistence.c | 2 +- src/SSLSocket.c | 14 ++++++------ 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index f6973d9a..837dbc62 100644 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -453,36 +453,42 @@ void MQTTAsync_sleep(long milliseconds) // http://ee.lbl.gov/papers/sync_94.pdf int MQTTAsync_randomJitter(int currentIntervalBase, int minInterval, int maxInterval) { - int max_sleep = min(maxInterval, currentIntervalBase) * 1.2; // (e.g. 72 if base > 60) - int min_sleep = max(minInterval, currentIntervalBase) / 1.2; // (e.g. 48 if base > 60) + const int max_sleep = (int)(min(maxInterval, currentIntervalBase) * 1.2); // (e.g. 72 if base > 60) + const int min_sleep = (int)(max(minInterval, currentIntervalBase) / 1.2); // (e.g. 48 if base > 60) if (min_sleep >= max_sleep) // shouldn't happen, but just incase { return min_sleep; } - // random_between(min_sleep, max_sleep) - // http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range - int r; - int range = max_sleep - min_sleep + 1; - if (range > RAND_MAX) { - range = RAND_MAX; + // random_between(min_sleep, max_sleep) + // http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range + int r; + int range = max_sleep - min_sleep + 1; + if (range > RAND_MAX) + { + range = RAND_MAX; + } + + { + const int buckets = RAND_MAX / range; + const int limit = buckets * range; + + /* Create equal size buckets all in a row, then fire randomly towards + * the buckets until you land in one of them. All buckets are equally + * likely. If you land off the end of the line of buckets, try again. */ + do + { + r = rand(); + } while (r >= limit); + + { + const int randResult = r / buckets; + return min_sleep + randResult; + } + } } - - int buckets = RAND_MAX / range; - int limit = buckets * range; - - /* Create equal size buckets all in a row, then fire randomly towards - * the buckets until you land in one of them. All buckets are equally - * likely. If you land off the end of the line of buckets, try again. */ - do - { - r = rand(); - } while (r >= limit); - - int randResult = r / buckets; - return min_sleep + randResult; } diff --git a/src/MQTTClient.c b/src/MQTTClient.c index 256aa6f4..83fe91aa 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -1510,7 +1510,7 @@ MQTTResponse MQTTClient_connectAll(MQTTClient handle, MQTTClient_connectOptions* int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options) { MQTTClients* m = handle; - MQTTResponse response; + MQTTResponse response; if (m->c->MQTTVersion >= MQTTVERSION_5) return MQTTCLIENT_WRONG_MQTT_VERSION; diff --git a/src/MQTTPersistence.c b/src/MQTTPersistence.c index 7c6adedc..5fdf9e65 100644 --- a/src/MQTTPersistence.c +++ b/src/MQTTPersistence.c @@ -201,7 +201,7 @@ int MQTTPersistence_restore(Clients *c) { int data_MQTTVersion = MQTTVERSION_3_1_1; char* cur_key = msgkeys[i]; - MQTTPacket* pack = NULL; + MQTTPacket* pack = NULL; if ( strncmp(cur_key, PERSISTENCE_V5_PUBLISH_RECEIVED, strlen(PERSISTENCE_V5_PUBLISH_RECEIVED)) == 0) diff --git a/src/SSLSocket.c b/src/SSLSocket.c index 92fc5e98..bd4883ec 100644 --- a/src/SSLSocket.c +++ b/src/SSLSocket.c @@ -525,14 +525,16 @@ static unsigned int call_ssl_psk_cb(SSL *ssl, const char *hint, char *identity, FUNC_ENTRY; - SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); - MQTTClient_SSLOptions* opts = SSL_CTX_get_ex_data(ctx, tls_ex_index_ssl_opts); + { + SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); + MQTTClient_SSLOptions* opts = SSL_CTX_get_ex_data(ctx, tls_ex_index_ssl_opts); - if (opts == NULL) - goto exit; + if (opts == NULL) + goto exit; - if (opts->ssl_psk_cb != NULL) - rc = opts->ssl_psk_cb(hint, identity, max_identity_len, psk, max_psk_len, opts->ssl_psk_context); + if (opts->ssl_psk_cb != NULL) + rc = opts->ssl_psk_cb(hint, identity, max_identity_len, psk, max_psk_len, opts->ssl_psk_context); + } exit: FUNC_EXIT_RC(rc); return rc; From 0a27f9730f99a4a66301ba78e2df39e3cc55095a Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Wed, 4 Dec 2019 10:31:32 +0000 Subject: [PATCH 6/6] Allow linking under some versions of Windows #576 --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 14846abe..90413b5f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -55,7 +55,7 @@ SET(common_src ) IF (WIN32) - SET(LIBS_SYSTEM ws2_32) + SET(LIBS_SYSTEM ws2_32 crypt32 RpcRT4) ELSEIF (UNIX) IF(CMAKE_SYSTEM_NAME MATCHES "Linux") SET(LIBS_SYSTEM c dl pthread)