From baf1909ffb12d4b65410497d4d8c34a5d726df61 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 16 Apr 2019 11:40:07 +0100 Subject: [PATCH] Improve some messages when client disconnects. --- lib/mosquitto.c | 8 ++++++ src/handle_connect.c | 2 +- src/handle_disconnect.c | 4 +-- src/loop.c | 50 ++++++++++++++++++++------------- src/mosquitto_broker_internal.h | 2 +- src/security_default.c | 6 ++-- 6 files changed, 45 insertions(+), 27 deletions(-) diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 2efbe829..b8386a53 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -333,6 +333,12 @@ bool mosquitto_want_write(struct mosquitto *mosq) const char *mosquitto_strerror(int mosq_errno) { switch(mosq_errno){ + case MOSQ_ERR_AUTH_CONTINUE: + return "Continue with authentication."; + case MOSQ_ERR_NO_SUBSCRIBERS: + return "No subscribers."; + case MOSQ_ERR_SUB_EXISTS: + return "Subscription already exists."; case MOSQ_ERR_CONN_PENDING: return "Connection pending."; case MOSQ_ERR_SUCCESS: @@ -379,6 +385,8 @@ const char *mosquitto_strerror(int mosq_errno) return "Requested QoS not supported on server."; case MOSQ_ERR_OVERSIZE_PACKET: return "Packet larger than supported by the server."; + case MOSQ_ERR_OCSP: + return "OCSP error."; default: return "Unknown error."; } diff --git a/src/handle_connect.c b/src/handle_connect.c index 6761bf2c..cd7084c8 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -160,7 +160,7 @@ int connect__on_authorised(struct mosquitto_db *db, struct mosquitto *context, v found_context->clean_start = true; found_context->session_expiry_interval = 0; context__set_state(found_context, mosq_cs_duplicate); - do_disconnect(db, found_context); + do_disconnect(db, found_context, MOSQ_ERR_SUCCESS); } rc = acl__find_acls(db, context); diff --git a/src/handle_disconnect.c b/src/handle_disconnect.c index 2ec23278..7eccfe8d 100644 --- a/src/handle_disconnect.c +++ b/src/handle_disconnect.c @@ -59,7 +59,7 @@ int handle__disconnect(struct mosquitto_db *db, struct mosquitto *context) log__printf(NULL, MOSQ_LOG_DEBUG, "Received DISCONNECT from %s", context->id); if(context->protocol == mosq_p_mqtt311 || context->protocol == mosq_p_mqtt5){ if((context->in_packet.command&0x0F) != 0x00){ - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_PROTOCOL); return MOSQ_ERR_PROTOCOL; } } @@ -68,6 +68,6 @@ int handle__disconnect(struct mosquitto_db *db, struct mosquitto *context) }else{ context__set_state(context, mosq_cs_disconnecting); } - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_SUCCESS); return MOSQ_ERR_SUCCESS; } diff --git a/src/loop.c b/src/loop.c index 02081be1..fc18399e 100644 --- a/src/loop.c +++ b/src/loop.c @@ -92,7 +92,7 @@ static void temp__expire_websockets_clients(struct mosquitto_db *db) } } /* Client has exceeded keepalive*1.5 */ - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_KEEPALIVE); } } } @@ -314,19 +314,11 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li pollfd_index++; #endif }else{ - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_CONN_LOST); } }else{ - if(db->config->connection_messages == true){ - if(context->id){ - id = context->id; - }else{ - id = ""; - } - log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s has exceeded timeout, disconnecting.", id); - } /* Client has exceeded keepalive*1.5 */ - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_KEEPALIVE); } } } @@ -485,7 +477,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li G_CLIENTS_EXPIRED_INC(); context->session_expiry_interval = 0; context__set_state(context, mosq_cs_expiring); - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_SUCCESS); } } } @@ -616,7 +608,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li return MOSQ_ERR_SUCCESS; } -void do_disconnect(struct mosquitto_db *db, struct mosquitto *context) +void do_disconnect(struct mosquitto_db *db, struct mosquitto *context, int reason) { char *id; #ifdef WITH_EPOLL @@ -628,7 +620,7 @@ void do_disconnect(struct mosquitto_db *db, struct mosquitto *context) } #ifdef WITH_WEBSOCKETS if(context->wsi){ - if(context->state != mosq_cs_disconnecting){ + if(context->state != mosq_cs_disconnecting && context->state != mosq_cs_disconnect_with_will){ context__set_state(context, mosq_cs_disconnect_ws); } if(context->wsi){ @@ -654,8 +646,26 @@ void do_disconnect(struct mosquitto_db *db, struct mosquitto *context) }else{ id = ""; } - if(context->state != mosq_cs_disconnecting){ - log__printf(NULL, MOSQ_LOG_NOTICE, "Socket error on client %s, disconnecting.", id); + if(context->state != mosq_cs_disconnecting && context->state != mosq_cs_disconnect_with_will){ + switch(reason){ + case MOSQ_ERR_SUCCESS: + break; + case MOSQ_ERR_PROTOCOL: + log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected due to protocol error.", id); + break; + case MOSQ_ERR_CONN_LOST: + log__printf(NULL, MOSQ_LOG_NOTICE, "Socket error on client %s, disconnecting.", id); + break; + case MOSQ_ERR_AUTH: + log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected, no longer authorised.", id); + break; + case MOSQ_ERR_KEEPALIVE: + log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s has exceeded timeout, disconnecting.", id); + break; + default: + log__printf(NULL, MOSQ_LOG_NOTICE, "Socket error on client %s, disconnecting.", id); + break; + } }else{ log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", id); } @@ -751,12 +761,12 @@ static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pol #endif } }else{ - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_CONN_LOST); continue; } } if(packet__write(context)){ - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_CONN_LOST); continue; } } @@ -798,7 +808,7 @@ static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pol #endif do{ if(packet__read(db, context)){ - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_CONN_LOST); continue; } }while(SSL_DATA_PENDING(context)); @@ -808,7 +818,7 @@ static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pol #else if(context->pollfd_index >= 0 && pollfds[context->pollfd_index].revents & (POLLERR | POLLNVAL | POLLHUP)){ #endif - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_CONN_LOST); continue; } } diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index e7560a1e..e6ad67ab 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -725,7 +725,7 @@ struct lws_context *mosq_websockets_init(struct mosquitto__listener *listener, c struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *listener, const struct mosquitto__config *conf); # endif #endif -void do_disconnect(struct mosquitto_db *db, struct mosquitto *context); +void do_disconnect(struct mosquitto_db *db, struct mosquitto *context, int reason); /* ============================================================ * Will delay diff --git a/src/security_default.c b/src/security_default.c index bd6c5b31..cf5d81b6 100644 --- a/src/security_default.c +++ b/src/security_default.c @@ -970,13 +970,13 @@ int mosquitto_security_apply_default(struct mosquitto_db *db) if(!allow_anonymous && !context->username){ context__set_state(context, mosq_cs_disconnecting); - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_AUTH); continue; } /* Check for connected clients that are no longer authorised */ if(mosquitto_unpwd_check(db, context, context->username, context->password) != MOSQ_ERR_SUCCESS){ context__set_state(context, mosq_cs_disconnecting); - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_AUTH); continue; } /* Check for ACLs and apply to user. */ @@ -986,7 +986,7 @@ int mosquitto_security_apply_default(struct mosquitto_db *db) }else{ if(context->state != mosq_cs_connected){ context__set_state(context, mosq_cs_disconnecting); - do_disconnect(db, context); + do_disconnect(db, context, MOSQ_ERR_AUTH); continue; } }