diff --git a/src/bridge.c b/src/bridge.c index 03eebbe6..ec2e9d46 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -82,7 +82,7 @@ int mqtt3_bridge_new(struct mosquitto_db *db, struct _mqtt3_bridge *bridge) /* (possible from persistent db) */ }else{ /* id wasn't found, so generate a new context */ - new_context = mqtt3_context_init(-1); + new_context = mqtt3_context_init(db, -1); if(!new_context){ return MOSQ_ERR_NOMEM; } diff --git a/src/context.c b/src/context.c index 44523ef9..0f219471 100644 --- a/src/context.c +++ b/src/context.c @@ -24,7 +24,7 @@ Contributors: #include "uthash.h" -struct mosquitto *mqtt3_context_init(int sock) +struct mosquitto *mqtt3_context_init(struct mosquitto_db *db, int sock) { struct mosquitto *context; char address[1024]; @@ -76,6 +76,9 @@ struct mosquitto *mqtt3_context_init(int sock) context->ssl = NULL; #endif + if(context->sock != INVALID_SOCKET){ + HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(context->sock), context); + } return context; } diff --git a/src/mosquitto_broker.h b/src/mosquitto_broker.h index 7765016c..c39d76e3 100644 --- a/src/mosquitto_broker.h +++ b/src/mosquitto_broker.h @@ -381,7 +381,7 @@ int mqtt3_subs_clean_session(struct mosquitto_db *db, struct mosquitto *context, /* ============================================================ * Context functions * ============================================================ */ -struct mosquitto *mqtt3_context_init(int sock); +struct mosquitto *mqtt3_context_init(struct mosquitto_db *db, int sock); void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free); void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *context); diff --git a/src/net.c b/src/net.c index 6a46b2d5..8d7f35c1 100644 --- a/src/net.c +++ b/src/net.c @@ -104,7 +104,7 @@ int mqtt3_socket_accept(struct mosquitto_db *db, int listensock) return -1; } #endif - new_context = mqtt3_context_init(new_sock); + new_context = mqtt3_context_init(db, new_sock); if(!new_context){ COMPAT_CLOSE(new_sock); return -1; @@ -171,7 +171,6 @@ int mqtt3_socket_accept(struct mosquitto_db *db, int listensock) #endif _mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "New connection from %s on port %d.", new_context->address, new_context->listener->port); - HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(new_context->sock), new_context); return new_sock; } diff --git a/src/persist.c b/src/persist.c index 6757c896..0e276190 100644 --- a/src/persist.c +++ b/src/persist.c @@ -46,7 +46,7 @@ static struct mosquitto *_db_find_or_add_context(struct mosquitto_db *db, const context = NULL; HASH_FIND(hh_id, db->contexts_by_id, client_id, strlen(client_id), context); if(!context){ - context = mqtt3_context_init(-1); + context = mqtt3_context_init(db, -1); if(!context) return NULL; context->id = _mosquitto_strdup(client_id); if(!context){ diff --git a/src/websockets.c b/src/websockets.c index 1ff77761..8594e18f 100644 --- a/src/websockets.c +++ b/src/websockets.c @@ -119,7 +119,7 @@ static int callback_mqtt(struct libwebsocket_context *context, switch (reason) { case LWS_CALLBACK_ESTABLISHED: - mosq = mqtt3_context_init(-1); + mosq = mqtt3_context_init(db, -1); if(mosq){ mosq->ws_context = context; mosq->wsi = wsi;