From 4bacbecb1bda61afe74d9ca5fa7023d4e9c7dd45 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 2 Aug 2018 14:33:29 +0100 Subject: [PATCH] Fix some places where return codes were incorrect. Closes #850. Signed-off-by: Roger A. Light --- ChangeLog.txt | 5 +++++ lib/loop.c | 3 ++- lib/mosquitto.h | 4 +++- lib/net_mosq.c | 8 ++++---- lib/packet_mosq.c | 4 ++-- lib/srv_mosq.c | 2 +- lib/util_mosq.c | 2 +- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index f267221e..c6b2b3fe 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -13,6 +13,11 @@ Broker: - All clients now time out if they exceed their keepalive*1.5, rather than just reach it. This was inconsistent in two places. +Library: +- Fix some places where return codes were incorrect, including to the + on_disconnect() callback. This has resulted in two new error codes, + MOSQ_ERR_KEEPALIVE and MOSQ_ERR_LOOKUP. + 1.5 - 20180502 ============== diff --git a/lib/loop.c b/lib/loop.c index 52d7a319..0725d227 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -300,6 +300,7 @@ int mosquitto_loop_misc(struct mosquitto *mosq) mosquitto__check_keepalive(mosq); now = mosquitto_time(); + if(mosq->ping_t && now - mosq->ping_t >= mosq->keepalive){ /* mosq->ping_t != 0 means we are waiting for a pingresp. * This hasn't happened in the keepalive time so we should disconnect. @@ -309,7 +310,7 @@ int mosquitto_loop_misc(struct mosquitto *mosq) if(mosq->state == mosq_cs_disconnecting){ rc = MOSQ_ERR_SUCCESS; }else{ - rc = 1; + rc = MOSQ_ERR_KEEPALIVE; } pthread_mutex_unlock(&mosq->state_mutex); pthread_mutex_lock(&mosq->callback_mutex); diff --git a/lib/mosquitto.h b/lib/mosquitto.h index e67a2b34..a8de0942 100644 --- a/lib/mosquitto.h +++ b/lib/mosquitto.h @@ -84,7 +84,9 @@ enum mosq_err_t { MOSQ_ERR_EAI = 15, MOSQ_ERR_PROXY = 16, MOSQ_ERR_PLUGIN_DEFER = 17, - MOSQ_ERR_MALFORMED_UTF8 = 18 + MOSQ_ERR_MALFORMED_UTF8 = 18, + MOSQ_ERR_KEEPALIVE = 19, + MOSQ_ERR_LOOKUP = 20, }; /* Error values */ diff --git a/lib/net_mosq.c b/lib/net_mosq.c index 4473eb7c..891d231a 100644 --- a/lib/net_mosq.c +++ b/lib/net_mosq.c @@ -748,21 +748,21 @@ int net__socket_nonblock(mosq_sock_t sock) opt = fcntl(sock, F_GETFL, 0); if(opt == -1){ COMPAT_CLOSE(sock); - return 1; + return MOSQ_ERR_ERRNO; } if(fcntl(sock, F_SETFL, opt | O_NONBLOCK) == -1){ /* If either fcntl fails, don't want to allow this client to connect. */ COMPAT_CLOSE(sock); - return 1; + return MOSQ_ERR_ERRNO; } #else unsigned long opt = 1; if(ioctlsocket(sock, FIONBIO, &opt)){ COMPAT_CLOSE(sock); - return 1; + return MOSQ_ERR_ERRNO; } #endif - return 0; + return MOSQ_ERR_SUCCESS; } diff --git a/lib/packet_mosq.c b/lib/packet_mosq.c index efc524ea..e956adeb 100644 --- a/lib/packet_mosq.c +++ b/lib/packet_mosq.c @@ -119,7 +119,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet) # ifdef WITH_WEBSOCKETS if(mosq->wsi){ libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi); - return 0; + return MOSQ_ERR_SUCCESS; }else{ return packet__write(mosq); } @@ -347,7 +347,7 @@ int packet__write(struct mosquitto *mosq) pthread_mutex_lock(&mosq->callback_mutex); if(mosq->on_disconnect){ mosq->in_callback = true; - mosq->on_disconnect(mosq, mosq->userdata, 0); + mosq->on_disconnect(mosq, mosq->userdata, MOSQ_ERR_SUCCESS); mosq->in_callback = false; } pthread_mutex_unlock(&mosq->callback_mutex); diff --git a/lib/srv_mosq.c b/lib/srv_mosq.c index a8339048..e54f9d45 100644 --- a/lib/srv_mosq.c +++ b/lib/srv_mosq.c @@ -44,7 +44,7 @@ static void srv_callback(void *arg, int status, int timeouts, unsigned char *abu pthread_mutex_lock(&mosq->callback_mutex); if(mosq->on_disconnect){ mosq->in_callback = true; - mosq->on_disconnect(mosq, mosq->userdata, 2); + mosq->on_disconnect(mosq, mosq->userdata, MOSQ_ERR_LOOKUP); mosq->in_callback = false; } pthread_mutex_unlock(&mosq->callback_mutex); diff --git a/lib/util_mosq.c b/lib/util_mosq.c index b85b95ce..f72a830d 100644 --- a/lib/util_mosq.c +++ b/lib/util_mosq.c @@ -96,7 +96,7 @@ void mosquitto__check_keepalive(struct mosquitto *mosq) if(mosq->state == mosq_cs_disconnecting){ rc = MOSQ_ERR_SUCCESS; }else{ - rc = 1; + rc = MOSQ_ERR_KEEPALIVE; } pthread_mutex_unlock(&mosq->state_mutex); pthread_mutex_lock(&mosq->callback_mutex);