fixes to mqtt (win sockets)

This commit is contained in:
Def 2025-07-09 14:08:40 +03:00
commit c39e18a014
2 changed files with 16 additions and 17 deletions

View file

@ -1,4 +1,5 @@
#if !defined(__WIN32_SOCKET_TEMPLATE_H__) #if !defined(__WIN32_SOCKET_TEMPLATE_H__)
#define __WIN32_SOCKET_TEMPLATE_H__
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -9,16 +10,16 @@
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
void close_nb_socket(int sockfd); void close_nb_socket(mqtt_pal_socket_handle sockfd);
int open_nb_socket(const char *addr, const char *port); 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; WSADATA wsaData;
int res = WSAStartup(MAKEWORD(2, 2), &wsaData); int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (res != 0) { if (res != 0) {
fprintf(stderr, "error: WSAStartup failed with error: %i", res); fprintf(stderr, "error: WSAStartup failed with error: %i", res);
return -1; return INVALID_SOCKET;
} }
struct addrinfo hints; struct addrinfo hints;
@ -33,7 +34,7 @@ int open_nb_socket(const char *addr, const char *port) {
if (rv != 0) { if (rv != 0) {
fprintf(stderr, "error: getaddrinfo: %s", gai_strerror(rv)); fprintf(stderr, "error: getaddrinfo: %s", gai_strerror(rv));
WSACleanup(); WSACleanup();
return -1; return INVALID_SOCKET;
} }
/* open the first possible 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 if (p == NULL) { // No address succeeded
fprintf(stderr, "error: Could not connect"); fprintf(stderr, "error: Could not connect");
WSACleanup(); WSACleanup();
return -1; return INVALID_SOCKET;
} }
// make non-blocking // make non-blocking
if (hSocket != INVALID_SOCKET) { if (hSocket != INVALID_SOCKET) {
uint32_t mode = 1; // FIONBIO returns size on 32b u_long mode = 1; // FIONBIO returns size on 32b
ioctlsocket(hSocket, FIONBIO, (u_long *)&mode); ioctlsocket(hSocket, FIONBIO, &mode);
} }
int flag = 1; int flag = 1;
@ -75,15 +76,15 @@ int open_nb_socket(const char *addr, const char *port) {
if (res != 0) { if (res != 0) {
closesocket(hSocket); closesocket(hSocket);
WSACleanup(); WSACleanup();
return -1; return INVALID_SOCKET;
} }
return hSocket; return hSocket;
} }
void close_nb_socket(int sockfd) { void close_nb_socket(mqtt_pal_socket_handle sockfd) {
if (sockfd != -1) { if (sockfd != INVALID_SOCKET) {
close(sockfd); closesocket(sockfd);
} }
} }
#endif #endif

View file

@ -56,20 +56,18 @@ static void mqtt_publish_callback(void **unused, struct mqtt_response_publish *p
static void *mqtt_client_refresher(void *client) { static void *mqtt_client_refresher(void *client) {
while (1) { while (1) {
pthread_testcancel(); // check if we cancelled
mqtt_sync((struct mqtt_client *) client); mqtt_sync((struct mqtt_client *) client);
msleep(100); msleep(100);
} }
return NULL; 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); close_nb_socket(sockfd);
if (client_daemon != NULL) { if (client_daemon != NULL) {
pthread_cancel(*client_daemon); pthread_cancel(*client_daemon);
#ifndef _WIN32
pthread_join(*client_daemon, NULL); // Wait for the thread to finish pthread_join(*client_daemon, NULL); // Wait for the thread to finish
#endif
} }
return status; 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) { 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) // 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) { if (sockfd == -1) {
PrintAndLogEx(FAILED, "Failed to open socket"); PrintAndLogEx(FAILED, "Failed to open socket");
return mqtt_exit(PM3_EFAILED, sockfd, NULL); return mqtt_exit(PM3_EFAILED, sockfd, NULL);