From c39e18a014a563238231c350a52a9137be9841d6 Mon Sep 17 00:00:00 2001 From: Def Date: Wed, 9 Jul 2025 14:08:40 +0300 Subject: [PATCH] fixes to mqtt (win sockets) --- client/deps/mqtt/win32_sockets.h | 25 +++++++++++++------------ client/src/cmdmqtt.c | 8 +++----- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/client/deps/mqtt/win32_sockets.h b/client/deps/mqtt/win32_sockets.h index c7726ed20..20a774a63 100644 --- a/client/deps/mqtt/win32_sockets.h +++ b/client/deps/mqtt/win32_sockets.h @@ -1,4 +1,5 @@ #if !defined(__WIN32_SOCKET_TEMPLATE_H__) +#define __WIN32_SOCKET_TEMPLATE_H__ #include #include @@ -9,16 +10,16 @@ #include #include -void close_nb_socket(int sockfd); -int open_nb_socket(const char *addr, const char *port); +void close_nb_socket(mqtt_pal_socket_handle sockfd); +mqtt_pal_socket_handle open_nb_socket(const char *addr, const char *port); -int open_nb_socket(const char *addr, const char *port) { +mqtt_pal_socket_handle open_nb_socket(const char *addr, const char *port) { WSADATA wsaData; int res = WSAStartup(MAKEWORD(2, 2), &wsaData); if (res != 0) { fprintf(stderr, "error: WSAStartup failed with error: %i", res); - return -1; + return INVALID_SOCKET; } struct addrinfo hints; @@ -33,7 +34,7 @@ int open_nb_socket(const char *addr, const char *port) { if (rv != 0) { fprintf(stderr, "error: getaddrinfo: %s", gai_strerror(rv)); WSACleanup(); - return -1; + return INVALID_SOCKET; } /* open the first possible socket */ @@ -61,13 +62,13 @@ int open_nb_socket(const char *addr, const char *port) { if (p == NULL) { // No address succeeded fprintf(stderr, "error: Could not connect"); WSACleanup(); - return -1; + return INVALID_SOCKET; } // make non-blocking if (hSocket != INVALID_SOCKET) { - uint32_t mode = 1; // FIONBIO returns size on 32b - ioctlsocket(hSocket, FIONBIO, (u_long *)&mode); + u_long mode = 1; // FIONBIO returns size on 32b + ioctlsocket(hSocket, FIONBIO, &mode); } int flag = 1; @@ -75,15 +76,15 @@ int open_nb_socket(const char *addr, const char *port) { if (res != 0) { closesocket(hSocket); WSACleanup(); - return -1; + return INVALID_SOCKET; } return hSocket; } -void close_nb_socket(int sockfd) { - if (sockfd != -1) { - close(sockfd); +void close_nb_socket(mqtt_pal_socket_handle sockfd) { + if (sockfd != INVALID_SOCKET) { + closesocket(sockfd); } } #endif diff --git a/client/src/cmdmqtt.c b/client/src/cmdmqtt.c index ba7d93685..ace6c62c1 100644 --- a/client/src/cmdmqtt.c +++ b/client/src/cmdmqtt.c @@ -56,20 +56,18 @@ static void mqtt_publish_callback(void **unused, struct mqtt_response_publish *p static void *mqtt_client_refresher(void *client) { while (1) { + pthread_testcancel(); // check if we cancelled mqtt_sync((struct mqtt_client *) client); msleep(100); } return NULL; } -static int mqtt_exit(int status, int sockfd, pthread_t *client_daemon) { +static int mqtt_exit(int status, mqtt_pal_socket_handle sockfd, pthread_t *client_daemon) { close_nb_socket(sockfd); - if (client_daemon != NULL) { pthread_cancel(*client_daemon); -#ifndef _WIN32 pthread_join(*client_daemon, NULL); // Wait for the thread to finish -#endif } return status; } @@ -109,7 +107,7 @@ static void mqtt_reconnect_client(struct mqtt_client* client, void **reconnect_s static int mqtt_receive(const char *addr, const char *port, const char *topic, const char *fn) { // open the non-blocking TCP socket (connecting to the broker) - int sockfd = open_nb_socket(addr, port); + mqtt_pal_socket_handle sockfd = open_nb_socket(addr, port); if (sockfd == -1) { PrintAndLogEx(FAILED, "Failed to open socket"); return mqtt_exit(PM3_EFAILED, sockfd, NULL);