diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f306d0c..92e5efea 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 anl) diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index 7587ea61..9e3268c2 100644 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -452,36 +452,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 = (int)min(maxInterval, currentIntervalBase) * 1.2; // (e.g. 72 if base > 60) - int min_sleep = (int)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 4ce6ebb8..ab9107a3 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -1521,7 +1521,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; diff --git a/src/Socket.h b/src/Socket.h index eec16346..a6e7fcb6 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 diff --git a/src/WebSocket.c b/src/WebSocket.c index 112471c2..1c0abcf3 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) _byteswap_uint64(x) +# define ntohll(x) _byteswap_uint64(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 */ @@ -657,7 +657,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)