2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2020-02-27 23:26:58 +00:00
|
|
|
Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
|
|
|
|
The Eclipse Public License is available at
|
|
|
|
http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
|
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
2017-07-27 17:43:09 +00:00
|
|
|
Tatsuzo Osawa - Add epoll.
|
2014-05-07 22:27:00 +00:00
|
|
|
*/
|
|
|
|
|
2018-08-16 10:14:51 +00:00
|
|
|
#include "config.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2018-08-30 19:44:17 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
# define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#ifndef WIN32
|
2017-07-16 15:11:04 +00:00
|
|
|
#include <unistd.h>
|
2014-05-07 22:27:00 +00:00
|
|
|
#else
|
|
|
|
#include <process.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-10-24 21:49:48 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
2014-09-14 17:08:09 +00:00
|
|
|
#include <time.h>
|
2020-05-28 22:56:09 +00:00
|
|
|
#include <utlist.h>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-05-06 09:47:00 +00:00
|
|
|
#ifdef WITH_WEBSOCKETS
|
|
|
|
# include <libwebsockets.h>
|
|
|
|
#endif
|
|
|
|
|
2016-07-08 09:10:04 +00:00
|
|
|
#include "mosquitto_broker_internal.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "memory_mosq.h"
|
2015-04-29 20:23:59 +00:00
|
|
|
#include "packet_mosq.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "send_mosq.h"
|
2015-05-16 11:25:35 +00:00
|
|
|
#include "sys_tree.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "time_mosq.h"
|
|
|
|
#include "util_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
extern bool flag_reload;
|
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
extern bool flag_db_backup;
|
|
|
|
#endif
|
|
|
|
extern bool flag_tree_print;
|
|
|
|
extern int run;
|
|
|
|
|
2019-09-24 09:21:51 +00:00
|
|
|
#if defined(WITH_WEBSOCKETS) && LWS_LIBRARY_VERSION_NUMBER == 3002000
|
|
|
|
void lws__sul_callback(struct lws_sorted_usec_list *l)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct lws_sorted_usec_list sul;
|
|
|
|
#endif
|
|
|
|
|
2020-08-07 15:16:55 +00:00
|
|
|
static int single_publish(struct mosquitto_db *db, struct mosquitto *context, struct mosquitto_message_v5 *msg)
|
|
|
|
{
|
|
|
|
struct mosquitto_msg_store *stored;
|
2020-10-17 00:23:08 +00:00
|
|
|
uint16_t mid;
|
2020-08-07 15:16:55 +00:00
|
|
|
|
|
|
|
stored = mosquitto__calloc(1, sizeof(struct mosquitto_msg_store));
|
|
|
|
if(stored == NULL) return MOSQ_ERR_NOMEM;
|
|
|
|
|
|
|
|
stored->topic = msg->topic;
|
|
|
|
msg->topic = NULL;
|
|
|
|
stored->retain = 0;
|
2020-10-17 00:23:08 +00:00
|
|
|
stored->payloadlen = (uint32_t)msg->payloadlen;
|
2020-08-07 15:16:55 +00:00
|
|
|
if(UHPA_ALLOC(stored->payload, stored->payloadlen) == 0){
|
|
|
|
db__msg_store_free(stored);
|
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
memcpy(UHPA_ACCESS(stored->payload, stored->payloadlen), msg->payload, stored->payloadlen);
|
|
|
|
|
|
|
|
if(msg->properties){
|
|
|
|
stored->properties = msg->properties;
|
|
|
|
msg->properties = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(db__message_store(db, context, stored, 0, 0, mosq_mo_broker)) return 1;
|
|
|
|
|
|
|
|
if(msg->qos){
|
|
|
|
mid = mosquitto__mid_generate(context);
|
|
|
|
}else{
|
|
|
|
mid = 0;
|
|
|
|
}
|
2020-10-17 00:23:08 +00:00
|
|
|
return db__message_insert(db, context, mid, mosq_md_out, (uint8_t)msg->qos, 0, stored, msg->properties, true);
|
2020-08-07 15:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 22:56:09 +00:00
|
|
|
|
|
|
|
void queue_plugin_msgs(struct mosquitto_db *db)
|
|
|
|
{
|
|
|
|
struct mosquitto_message_v5 *msg, *tmp;
|
2020-08-07 15:16:55 +00:00
|
|
|
struct mosquitto *context;
|
2020-05-28 22:56:09 +00:00
|
|
|
|
|
|
|
DL_FOREACH_SAFE(db->plugin_msgs, msg, tmp){
|
|
|
|
DL_DELETE(db->plugin_msgs, msg);
|
2020-08-07 15:16:55 +00:00
|
|
|
if(msg->clientid){
|
|
|
|
HASH_FIND(hh_id, db->contexts_by_id, msg->clientid, strlen(msg->clientid), context);
|
|
|
|
if(context){
|
|
|
|
single_publish(db, context, msg);
|
|
|
|
}
|
|
|
|
}else{
|
2020-10-17 00:23:08 +00:00
|
|
|
db__messages_easy_queue(db, NULL, msg->topic, (uint8_t)msg->qos, (uint32_t)msg->payloadlen, msg->payload, msg->retain, 0, &msg->properties);
|
2020-08-07 15:16:55 +00:00
|
|
|
}
|
2020-08-07 09:25:18 +00:00
|
|
|
mosquitto__free(msg->topic);
|
|
|
|
mosquitto__free(msg->payload);
|
|
|
|
mosquitto_property_free_all(&msg->properties);
|
2020-08-07 15:16:55 +00:00
|
|
|
mosquitto__free(msg->clientid);
|
2020-05-28 22:56:09 +00:00
|
|
|
mosquitto__free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-13 14:11:50 +00:00
|
|
|
int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int listensock_count)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2014-09-15 21:13:13 +00:00
|
|
|
#ifdef WITH_SYS_TREE
|
2014-05-07 22:27:00 +00:00
|
|
|
time_t start_time = mosquitto_time();
|
2014-09-15 21:13:13 +00:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_PERSISTENCE
|
2014-05-07 22:27:00 +00:00
|
|
|
time_t last_backup = mosquitto_time();
|
2014-09-15 21:13:13 +00:00
|
|
|
#endif
|
2014-06-30 22:37:37 +00:00
|
|
|
time_t now = 0;
|
2020-02-15 14:49:53 +00:00
|
|
|
#ifdef WITH_WEBSOCKETS
|
|
|
|
int i;
|
|
|
|
#endif
|
2020-02-16 14:03:49 +00:00
|
|
|
int rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-09-24 09:21:51 +00:00
|
|
|
|
|
|
|
#if defined(WITH_WEBSOCKETS) && LWS_LIBRARY_VERSION_NUMBER == 3002000
|
|
|
|
memset(&sul, 0, sizeof(struct lws_sorted_usec_list));
|
|
|
|
#endif
|
|
|
|
|
2020-02-15 14:49:53 +00:00
|
|
|
rc = mux__init(db, listensock, listensock_count);
|
|
|
|
if(rc) return rc;
|
2017-06-18 11:52:59 +00:00
|
|
|
|
2020-02-15 14:49:53 +00:00
|
|
|
#ifdef WITH_BRIDGE
|
2019-07-11 13:43:32 +00:00
|
|
|
rc = bridge__register_local_connections(db);
|
|
|
|
if(rc) return rc;
|
2017-07-27 17:43:09 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
while(run){
|
2020-05-28 22:56:09 +00:00
|
|
|
queue_plugin_msgs(db);
|
2015-05-16 14:24:24 +00:00
|
|
|
context__free_disused(db);
|
2014-05-07 22:27:00 +00:00
|
|
|
#ifdef WITH_SYS_TREE
|
|
|
|
if(db->config->sys_interval > 0){
|
2016-07-08 08:14:11 +00:00
|
|
|
sys_tree__update(db, db->config->sys_interval, start_time);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-16 14:03:49 +00:00
|
|
|
now = mosquitto_time();
|
2020-09-05 21:59:50 +00:00
|
|
|
keepalive__check(db, now);
|
2014-06-23 16:57:35 +00:00
|
|
|
|
2020-02-16 14:03:49 +00:00
|
|
|
#ifdef WITH_BRIDGE
|
2019-07-11 13:43:32 +00:00
|
|
|
bridge_check(db);
|
2020-02-16 14:03:49 +00:00
|
|
|
#endif
|
2019-07-11 13:43:32 +00:00
|
|
|
|
2020-02-15 14:49:53 +00:00
|
|
|
rc = mux__handle(db, listensock, listensock_count);
|
|
|
|
if(rc) return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-06 14:47:16 +00:00
|
|
|
now = time(NULL);
|
|
|
|
session_expiry__check(db, now);
|
|
|
|
will_delay__check(db, now);
|
2014-05-07 22:27:00 +00:00
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
if(db->config->persistence && db->config->autosave_interval){
|
|
|
|
if(db->config->autosave_on_changes){
|
2015-06-29 16:11:16 +00:00
|
|
|
if(db->persistence_changes >= db->config->autosave_interval){
|
2015-05-16 14:24:24 +00:00
|
|
|
persist__backup(db, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
db->persistence_changes = 0;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(last_backup + db->config->autosave_interval < mosquitto_time()){
|
2015-05-16 14:24:24 +00:00
|
|
|
persist__backup(db, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
last_backup = mosquitto_time();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-17 23:46:02 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
if(flag_db_backup){
|
2015-05-16 14:24:24 +00:00
|
|
|
persist__backup(db, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
flag_db_backup = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if(flag_reload){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_INFO, "Reloading config.");
|
2018-05-02 22:56:11 +00:00
|
|
|
config__read(db, db->config, true);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosquitto_security_cleanup(db, true);
|
|
|
|
mosquitto_security_init(db, true);
|
|
|
|
mosquitto_security_apply(db);
|
2015-05-16 14:24:24 +00:00
|
|
|
log__close(db->config);
|
|
|
|
log__init(db->config);
|
2014-05-07 22:27:00 +00:00
|
|
|
flag_reload = false;
|
|
|
|
}
|
|
|
|
if(flag_tree_print){
|
2016-07-19 14:05:53 +00:00
|
|
|
sub__tree_print(db->subs, 0);
|
2014-05-07 22:27:00 +00:00
|
|
|
flag_tree_print = false;
|
|
|
|
}
|
2014-05-06 09:47:00 +00:00
|
|
|
#ifdef WITH_WEBSOCKETS
|
2014-05-26 17:01:24 +00:00
|
|
|
for(i=0; i<db->config->listener_count; i++){
|
2014-05-26 16:09:44 +00:00
|
|
|
/* Extremely hacky, should be using the lws provided external poll
|
|
|
|
* interface, but their interface has changed recently and ours
|
|
|
|
* will soon, so for now websockets clients are second class
|
|
|
|
* citizens. */
|
|
|
|
if(db->config->listeners[i].ws_context){
|
2019-09-12 12:31:01 +00:00
|
|
|
#if LWS_LIBRARY_VERSION_NUMBER > 3002000
|
2019-09-07 20:45:56 +00:00
|
|
|
libwebsocket_service(db->config->listeners[i].ws_context, -1);
|
2019-09-24 09:21:51 +00:00
|
|
|
#elif LWS_LIBRARY_VERSION_NUMBER == 3002000
|
|
|
|
lws_sul_schedule(db->config->listeners[i].ws_context, 0, &sul, lws__sul_callback, 10);
|
|
|
|
libwebsocket_service(db->config->listeners[i].ws_context, 0);
|
2019-09-12 12:31:01 +00:00
|
|
|
#else
|
2014-05-26 16:09:44 +00:00
|
|
|
libwebsocket_service(db->config->listeners[i].ws_context, 0);
|
2019-09-12 12:31:01 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-26 16:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 09:47:00 +00:00
|
|
|
#endif
|
2020-10-23 21:49:16 +00:00
|
|
|
plugin__handle_tick(db);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-05-26 16:09:44 +00:00
|
|
|
|
2020-02-15 14:49:53 +00:00
|
|
|
mux__cleanup(db);
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-04-16 10:40:07 +00:00
|
|
|
void do_disconnect(struct mosquitto_db *db, struct mosquitto *context, int reason)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-01-26 21:02:53 +00:00
|
|
|
char *id;
|
2019-05-28 21:13:22 +00:00
|
|
|
#ifdef WITH_WEBSOCKETS
|
|
|
|
bool is_duplicate = false;
|
|
|
|
#endif
|
2015-01-26 21:02:53 +00:00
|
|
|
|
2014-07-08 22:16:34 +00:00
|
|
|
if(context->state == mosq_cs_disconnected){
|
|
|
|
return;
|
|
|
|
}
|
2014-07-03 00:00:57 +00:00
|
|
|
#ifdef WITH_WEBSOCKETS
|
|
|
|
if(context->wsi){
|
2019-05-28 21:13:22 +00:00
|
|
|
if(context->state == mosq_cs_duplicate){
|
|
|
|
is_duplicate = true;
|
|
|
|
}
|
|
|
|
|
2019-04-16 10:40:07 +00:00
|
|
|
if(context->state != mosq_cs_disconnecting && context->state != mosq_cs_disconnect_with_will){
|
2019-09-24 10:54:05 +00:00
|
|
|
mosquitto__set_state(context, mosq_cs_disconnect_ws);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-07-03 00:00:57 +00:00
|
|
|
if(context->wsi){
|
|
|
|
libwebsocket_callback_on_writable(context->ws_context, context->wsi);
|
|
|
|
}
|
2017-06-18 11:52:59 +00:00
|
|
|
if(context->sock != INVALID_SOCKET){
|
|
|
|
HASH_DELETE(hh_sock, db->contexts_by_sock, context);
|
2020-02-15 14:49:53 +00:00
|
|
|
mux__delete(db, context);
|
2017-06-18 11:52:59 +00:00
|
|
|
context->sock = INVALID_SOCKET;
|
|
|
|
}
|
2019-05-28 21:13:22 +00:00
|
|
|
if(is_duplicate){
|
|
|
|
/* This occurs if another client is taking over the same client id.
|
|
|
|
* It is important to remove this from the by_id hash here, so it
|
|
|
|
* doesn't leave us with multiple clients in the hash with the same
|
|
|
|
* id. Websockets doesn't actually close the connection here,
|
|
|
|
* unlike for normal clients, which means there is extra time when
|
|
|
|
* there could be two clients with the same id in the hash. */
|
|
|
|
context__remove_from_by_id(db, context);
|
|
|
|
}
|
2015-01-07 23:33:31 +00:00
|
|
|
}else
|
2014-07-03 00:00:57 +00:00
|
|
|
#endif
|
2015-01-07 23:33:31 +00:00
|
|
|
{
|
2014-07-03 00:00:57 +00:00
|
|
|
if(db->config->connection_messages == true){
|
2015-01-26 21:02:53 +00:00
|
|
|
if(context->id){
|
|
|
|
id = context->id;
|
|
|
|
}else{
|
|
|
|
id = "<unknown>";
|
|
|
|
}
|
2019-04-16 10:40:07 +00:00
|
|
|
if(context->state != mosq_cs_disconnecting && context->state != mosq_cs_disconnect_with_will){
|
|
|
|
switch(reason){
|
|
|
|
case MOSQ_ERR_SUCCESS:
|
|
|
|
break;
|
2020-08-02 12:51:42 +00:00
|
|
|
case MOSQ_ERR_MALFORMED_PACKET:
|
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected due to malformed packet.", id);
|
|
|
|
break;
|
2019-04-16 10:40:07 +00:00
|
|
|
case MOSQ_ERR_PROTOCOL:
|
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected due to protocol error.", id);
|
|
|
|
break;
|
|
|
|
case MOSQ_ERR_CONN_LOST:
|
2020-04-28 09:42:49 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s closed its connection.", id);
|
2019-04-16 10:40:07 +00:00
|
|
|
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;
|
2020-10-27 10:04:22 +00:00
|
|
|
case MOSQ_ERR_OVERSIZE_PACKET:
|
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected due to oversize packet.", id);
|
|
|
|
break;
|
|
|
|
case MOSQ_ERR_NOMEM:
|
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected due to out of memory.", id);
|
|
|
|
break;
|
2020-09-07 12:49:42 +00:00
|
|
|
case MOSQ_ERR_ADMINISTRATIVE_ACTION:
|
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s been disconnected by administrative action.", id);
|
|
|
|
break;
|
2019-04-16 10:40:07 +00:00
|
|
|
default:
|
2020-10-27 10:04:22 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Bad socket read/write on client %s, disconnecting.", id);
|
2019-04-16 10:40:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-07-03 00:00:57 +00:00
|
|
|
}else{
|
2020-09-07 12:49:42 +00:00
|
|
|
if(reason == MOSQ_ERR_ADMINISTRATIVE_ACTION){
|
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s been disconnected by administrative action.", id);
|
|
|
|
}else{
|
|
|
|
log__printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", id);
|
|
|
|
}
|
2014-07-03 00:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-15 14:49:53 +00:00
|
|
|
mux__delete(db, context);
|
2015-05-16 14:24:24 +00:00
|
|
|
context__disconnect(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|