From 71b8c4d892da64336272b4420e0e5775acea8b49 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 7 Nov 2018 21:08:10 +0000 Subject: [PATCH] Fix TLS connections when using an external event loop. Affects the use of mosquitto_loop_read() and mosquitto_write(). Closes #990. --- ChangeLog.txt | 4 +++- lib/loop.c | 26 ++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 138af7980c..cf94b1c06d 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -15,7 +15,9 @@ Broker: Library: - Fix memory leak that occurred if mosquitto_reconnect() was used when TLS -errors were present. Closes #592. + errors were present. Closes #592. +- Fix TLS connections when using an external event loop with + mosquitto_loop_read() and mosquitto_write(). Closes #990. Build: - Fix clients not being compiled with threading support when using CMake. diff --git a/lib/loop.c b/lib/loop.c index 0725d227d0..23e60825e0 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -147,20 +147,12 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) }else{ if(mosq->sock != INVALID_SOCKET){ if(FD_ISSET(mosq->sock, &readfds)){ -#ifdef WITH_TLS - if(mosq->want_connect){ - rc = net__socket_connect_tls(mosq); - if(rc) return rc; - }else -#endif - { - do{ - rc = mosquitto_loop_read(mosq, max_packets); - if(rc || mosq->sock == INVALID_SOCKET){ - return rc; - } - }while(SSL_DATA_PENDING(mosq)); - } + do{ + rc = mosquitto_loop_read(mosq, max_packets); + if(rc || mosq->sock == INVALID_SOCKET){ + return rc; + } + }while(SSL_DATA_PENDING(mosq)); } if(mosq->sockpairR != INVALID_SOCKET && FD_ISSET(mosq->sockpairR, &readfds)){ #ifndef WIN32 @@ -354,6 +346,12 @@ int mosquitto_loop_read(struct mosquitto *mosq, int max_packets) int i; if(max_packets < 1) return MOSQ_ERR_INVAL; +#ifdef WITH_TLS + if(mosq->want_connect){ + return net__socket_connect_tls(mosq); + } +#endif + pthread_mutex_lock(&mosq->out_message_mutex); max_packets = mosq->out_queue_len; pthread_mutex_unlock(&mosq->out_message_mutex);