2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2020-02-27 23:26:58 +00:00
|
|
|
Copyright (c) 2010-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.
|
2020-01-29 12:30:24 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
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.
|
2020-01-29 12:30:24 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "config.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2014-09-14 17:08:09 +00:00
|
|
|
#include <time.h>
|
2019-04-03 13:13:12 +00:00
|
|
|
#include <utlist.h>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
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"
|
|
|
|
#include "persist.h"
|
|
|
|
#include "time_mosq.h"
|
2020-02-04 16:05:58 +00:00
|
|
|
#include "misc_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
#include "util_mosq.h"
|
|
|
|
|
2019-08-01 14:49:07 +00:00
|
|
|
uint32_t db_version;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-14 11:08:30 +00:00
|
|
|
const unsigned char magic[15] = {0x00, 0xB5, 0x00, 'm','o','s','q','u','i','t','t','o',' ','d','b'};
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__restore_sub(const char *client_id, const char *sub, uint8_t qos, uint32_t identifier, int options);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static struct mosquitto *persist__find_or_add_context(const char *client_id, uint16_t last_mid)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto *context;
|
|
|
|
|
2019-03-20 00:02:38 +00:00
|
|
|
if(!client_id) return NULL;
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
context = NULL;
|
2020-11-06 15:16:07 +00:00
|
|
|
HASH_FIND(hh_id, db.contexts_by_id, client_id, strlen(client_id), context);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context){
|
2020-11-06 15:16:07 +00:00
|
|
|
context = context__init(-1);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context) return NULL;
|
2015-04-19 21:10:59 +00:00
|
|
|
context->id = mosquitto__strdup(client_id);
|
2014-06-29 22:16:10 +00:00
|
|
|
if(!context->id){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context);
|
2014-06-23 16:57:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2018-11-27 10:02:10 +00:00
|
|
|
context->clean_start = false;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
HASH_ADD_KEYPTR(hh_id, db.contexts_by_id, context->id, strlen(context->id), context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
if(last_mid){
|
|
|
|
context->last_mid = last_mid;
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2019-03-20 00:02:38 +00:00
|
|
|
int persist__read_string_len(FILE *db_fptr, char **str, uint16_t len)
|
2019-01-31 21:50:42 +00:00
|
|
|
{
|
|
|
|
char *s = NULL;
|
|
|
|
|
2019-03-20 00:02:38 +00:00
|
|
|
if(len){
|
2020-10-17 00:23:08 +00:00
|
|
|
s = mosquitto__malloc(len+1U);
|
2019-01-31 21:50:42 +00:00
|
|
|
if(!s){
|
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
2019-03-20 00:02:38 +00:00
|
|
|
if(fread(s, 1, len, db_fptr) != len){
|
2019-01-31 21:50:42 +00:00
|
|
|
mosquitto__free(s);
|
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
2019-03-20 00:02:38 +00:00
|
|
|
s[len] = '\0';
|
2019-01-31 21:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*str = s;
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-20 00:02:38 +00:00
|
|
|
int persist__read_string(FILE *db_fptr, char **str)
|
|
|
|
{
|
|
|
|
uint16_t i16temp;
|
|
|
|
uint16_t slen;
|
|
|
|
|
|
|
|
if(fread(&i16temp, 1, sizeof(uint16_t), db_fptr) != sizeof(uint16_t)){
|
|
|
|
return MOSQ_ERR_INVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
slen = ntohs(i16temp);
|
|
|
|
return persist__read_string_len(db_fptr, str, slen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__client_msg_restore(struct P_client_msg *chunk)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto_client_msg *cmsg;
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load;
|
2014-05-07 22:27:00 +00:00
|
|
|
struct mosquitto *context;
|
2019-04-13 21:59:29 +00:00
|
|
|
struct mosquitto_msg_data *msg_data;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
HASH_FIND(hh, db.msg_store_load, &chunk->F.store_id, sizeof(dbid_t), load);
|
2019-10-31 12:54:20 +00:00
|
|
|
if(!load){
|
|
|
|
/* Can't find message - probably expired */
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
context = persist__find_or_add_context(chunk->client_id, 0);
|
2020-04-23 13:45:55 +00:00
|
|
|
if(!context){
|
|
|
|
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Persistence file contains client message with no matching client. File may be corrupt.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-21 11:53:46 +00:00
|
|
|
cmsg = mosquitto__calloc(1, sizeof(struct mosquitto_client_msg));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!cmsg){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2014-11-17 21:58:53 +00:00
|
|
|
cmsg->next = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
cmsg->store = NULL;
|
2019-03-23 22:52:08 +00:00
|
|
|
cmsg->mid = chunk->F.mid;
|
|
|
|
cmsg->qos = chunk->F.qos;
|
|
|
|
cmsg->retain = (chunk->F.retain_dup&0xF0)>>4;
|
2014-11-17 21:58:53 +00:00
|
|
|
cmsg->timestamp = 0;
|
2019-03-23 22:52:08 +00:00
|
|
|
cmsg->direction = chunk->F.direction;
|
|
|
|
cmsg->state = chunk->F.state;
|
|
|
|
cmsg->dup = chunk->F.retain_dup&0x0F;
|
|
|
|
cmsg->properties = chunk->properties;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-11-18 19:12:08 +00:00
|
|
|
cmsg->store = load->store;
|
2019-05-22 12:20:45 +00:00
|
|
|
db__msg_store_ref_inc(cmsg->store);
|
2014-11-17 22:54:39 +00:00
|
|
|
|
2019-04-13 21:59:29 +00:00
|
|
|
if(cmsg->direction == mosq_md_out){
|
|
|
|
msg_data = &context->msgs_out;
|
2016-04-18 13:41:47 +00:00
|
|
|
}else{
|
2019-04-13 21:59:29 +00:00
|
|
|
msg_data = &context->msgs_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chunk->F.state == mosq_ms_queued || (chunk->F.qos > 0 && msg_data->inflight_quota == 0)){
|
|
|
|
DL_APPEND(msg_data->queued, cmsg);
|
|
|
|
}else{
|
|
|
|
DL_APPEND(msg_data->inflight, cmsg);
|
|
|
|
if(chunk->F.qos > 0 && msg_data->inflight_quota > 0){
|
|
|
|
msg_data->inflight_quota--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg_data->msg_count++;
|
|
|
|
msg_data->msg_bytes += cmsg->store->payloadlen;
|
|
|
|
if(chunk->F.qos > 0){
|
|
|
|
msg_data->msg_count12++;
|
|
|
|
msg_data->msg_bytes12 += cmsg->store->payloadlen;
|
2016-04-18 13:41:47 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__client_chunk_restore(FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2020-08-07 06:32:14 +00:00
|
|
|
int i, rc = 0;
|
2014-05-07 22:27:00 +00:00
|
|
|
struct mosquitto *context;
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_client chunk;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_client));
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 6 || db_version == 5){
|
|
|
|
rc = persist__chunk_client_read_v56(db_fptr, &chunk, db_version);
|
2019-03-20 00:02:38 +00:00
|
|
|
}else{
|
|
|
|
rc = persist__chunk_client_read_v234(db_fptr, &chunk, db_version);
|
|
|
|
}
|
2020-04-23 13:45:55 +00:00
|
|
|
if(rc > 0){
|
2019-03-16 09:42:15 +00:00
|
|
|
return rc;
|
2020-04-23 13:45:55 +00:00
|
|
|
}else if(rc < 0){
|
|
|
|
/* Client not loaded, but otherwise not an error */
|
|
|
|
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Empty client entry found in persistence database, it may be corrupt.");
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
context = persist__find_or_add_context(chunk.client_id, chunk.F.last_mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(context){
|
2019-03-20 00:02:38 +00:00
|
|
|
context->session_expiry_time = chunk.F.session_expiry_time;
|
|
|
|
context->session_expiry_interval = chunk.F.session_expiry_interval;
|
2020-08-07 06:32:14 +00:00
|
|
|
if(chunk.username && !context->username){
|
|
|
|
/* username is not freed here, it is now owned by context */
|
|
|
|
context->username = chunk.username;
|
|
|
|
chunk.username = NULL;
|
2020-11-12 17:36:49 +00:00
|
|
|
}
|
|
|
|
/* in per_listener_settings mode, try to find the listener by persisted port */
|
|
|
|
if(db.config->per_listener_settings && !context->listener && chunk.F.listener_port > 0){
|
|
|
|
for(i=0; i < db.config->listener_count; i++){
|
|
|
|
if(db.config->listeners[i].port == chunk.F.listener_port){
|
|
|
|
context->listener = &db.config->listeners[i];
|
|
|
|
break;
|
2020-08-07 06:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 00:02:38 +00:00
|
|
|
/* FIXME - we should expire clients here if they have exceeded their time */
|
2014-05-07 22:27:00 +00:00
|
|
|
}else{
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
mosquitto__free(chunk.client_id);
|
2020-08-07 06:32:14 +00:00
|
|
|
if(chunk.username){
|
|
|
|
mosquitto__free(chunk.username);
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__client_msg_chunk_restore(FILE *db_fptr, uint32_t length)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_client_msg chunk;
|
2014-05-07 22:27:00 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_client_msg));
|
|
|
|
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 6 || db_version == 5){
|
|
|
|
rc = persist__chunk_client_msg_read_v56(db_fptr, &chunk, length);
|
2019-03-20 00:02:38 +00:00
|
|
|
}else{
|
|
|
|
rc = persist__chunk_client_msg_read_v234(db_fptr, &chunk);
|
|
|
|
}
|
2019-03-16 09:42:15 +00:00
|
|
|
if(rc){
|
|
|
|
return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
rc = persist__client_msg_restore(&chunk);
|
2019-03-16 09:42:15 +00:00
|
|
|
mosquitto__free(chunk.client_id);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__msg_store_chunk_restore(FILE *db_fptr, uint32_t length)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_msg_store chunk;
|
2014-05-07 22:27:00 +00:00
|
|
|
struct mosquitto_msg_store *stored = NULL;
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load;
|
2019-03-26 14:14:46 +00:00
|
|
|
int64_t message_expiry_interval64;
|
2019-03-25 08:09:24 +00:00
|
|
|
uint32_t message_expiry_interval;
|
2019-03-16 09:42:15 +00:00
|
|
|
int rc = 0;
|
2019-02-09 13:52:09 +00:00
|
|
|
int i;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_msg_store));
|
2019-01-31 21:50:42 +00:00
|
|
|
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 6 || db_version == 5){
|
|
|
|
rc = persist__chunk_msg_store_read_v56(db_fptr, &chunk, length);
|
2019-03-20 00:02:38 +00:00
|
|
|
}else{
|
|
|
|
rc = persist__chunk_msg_store_read_v234(db_fptr, &chunk, db_version);
|
|
|
|
}
|
2019-01-31 21:50:42 +00:00
|
|
|
if(rc){
|
|
|
|
return rc;
|
|
|
|
}
|
2019-03-16 09:42:15 +00:00
|
|
|
|
|
|
|
if(chunk.F.source_port){
|
2020-11-06 15:16:07 +00:00
|
|
|
for(i=0; i<db.config->listener_count; i++){
|
|
|
|
if(db.config->listeners[i].port == chunk.F.source_port){
|
|
|
|
chunk.source.listener = &db.config->listeners[i];
|
2019-03-16 09:42:15 +00:00
|
|
|
break;
|
2019-01-31 21:50:42 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 11:53:46 +00:00
|
|
|
load = mosquitto__calloc(1, sizeof(struct mosquitto_msg_store_load));
|
2019-03-16 09:42:15 +00:00
|
|
|
if(!load){
|
|
|
|
mosquitto__free(chunk.source.id);
|
|
|
|
mosquitto__free(chunk.source.username);
|
|
|
|
mosquitto__free(chunk.topic);
|
|
|
|
UHPA_FREE(chunk.payload, chunk.F.payloadlen);
|
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
|
|
|
return MOSQ_ERR_NOMEM;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2019-01-31 21:50:42 +00:00
|
|
|
|
2019-03-25 08:09:24 +00:00
|
|
|
if(chunk.F.expiry_time > 0){
|
2019-03-26 14:14:46 +00:00
|
|
|
message_expiry_interval64 = chunk.F.expiry_time - time(NULL);
|
2019-03-26 22:20:04 +00:00
|
|
|
if(message_expiry_interval64 < 0 || message_expiry_interval64 > UINT32_MAX){
|
2019-10-31 12:54:20 +00:00
|
|
|
/* Expired message */
|
|
|
|
mosquitto__free(chunk.source.id);
|
|
|
|
mosquitto__free(chunk.source.username);
|
|
|
|
mosquitto__free(chunk.topic);
|
|
|
|
UHPA_FREE(chunk.payload, chunk.F.payloadlen);
|
|
|
|
mosquitto__free(load);
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
2019-03-26 14:14:46 +00:00
|
|
|
}else{
|
|
|
|
message_expiry_interval = (uint32_t)message_expiry_interval64;
|
|
|
|
}
|
2019-03-25 08:09:24 +00:00
|
|
|
}else{
|
|
|
|
message_expiry_interval = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:09:33 +00:00
|
|
|
stored = mosquitto__calloc(1, sizeof(struct mosquitto_msg_store));
|
|
|
|
if(stored == NULL){
|
2020-11-23 21:12:13 +00:00
|
|
|
mosquitto__free(load);
|
2020-07-10 15:09:33 +00:00
|
|
|
mosquitto__free(chunk.source.id);
|
|
|
|
mosquitto__free(chunk.source.username);
|
|
|
|
mosquitto__free(chunk.topic);
|
|
|
|
UHPA_FREE(chunk.payload, chunk.F.payloadlen);
|
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
stored->source_mid = chunk.F.source_mid;
|
|
|
|
stored->topic = chunk.topic;
|
|
|
|
stored->qos = chunk.F.qos;
|
|
|
|
stored->payloadlen = chunk.F.payloadlen;
|
|
|
|
stored->retain = chunk.F.retain;
|
|
|
|
stored->properties = chunk.properties;
|
|
|
|
UHPA_MOVE(stored->payload, chunk.payload, stored->payloadlen);
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
rc = db__message_store(&chunk.source, stored, message_expiry_interval,
|
2020-07-10 15:09:33 +00:00
|
|
|
chunk.F.store_id, mosq_mo_client);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
mosquitto__free(chunk.source.id);
|
|
|
|
mosquitto__free(chunk.source.username);
|
|
|
|
chunk.source.id = NULL;
|
|
|
|
chunk.source.username = NULL;
|
2014-11-18 19:12:08 +00:00
|
|
|
|
2015-06-28 20:16:48 +00:00
|
|
|
if(rc == MOSQ_ERR_SUCCESS){
|
2019-03-26 14:00:42 +00:00
|
|
|
stored->source_listener = chunk.source.listener;
|
2015-06-28 20:16:48 +00:00
|
|
|
load->db_id = stored->db_id;
|
|
|
|
load->store = stored;
|
2014-11-18 19:12:08 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
HASH_ADD(hh, db.msg_store_load, db_id, sizeof(dbid_t), load);
|
2016-03-18 11:54:36 +00:00
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}else{
|
|
|
|
mosquitto__free(load);
|
|
|
|
return rc;
|
2015-06-28 20:16:48 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__retain_chunk_restore(FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load;
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_retain chunk;
|
|
|
|
int rc;
|
2019-12-10 16:18:27 +00:00
|
|
|
char **split_topics;
|
|
|
|
char *local_topic;
|
2019-03-16 09:42:15 +00:00
|
|
|
|
|
|
|
memset(&chunk, 0, sizeof(struct P_retain));
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 6 || db_version == 5){
|
|
|
|
rc = persist__chunk_retain_read_v56(db_fptr, &chunk);
|
2019-03-20 00:02:38 +00:00
|
|
|
}else{
|
|
|
|
rc = persist__chunk_retain_read_v234(db_fptr, &chunk);
|
|
|
|
}
|
2019-03-16 09:42:15 +00:00
|
|
|
if(rc){
|
|
|
|
return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2019-03-16 09:42:15 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
HASH_FIND(hh, db.msg_store_load, &chunk.F.store_id, sizeof(dbid_t), load);
|
2014-11-18 19:12:08 +00:00
|
|
|
if(load){
|
2019-12-10 16:18:27 +00:00
|
|
|
if(sub__topic_tokenise(load->store->topic, &local_topic, &split_topics, NULL)) return 1;
|
2020-11-06 15:16:07 +00:00
|
|
|
retain__store(load->store->topic, load->store, split_topics);
|
2019-12-10 16:18:27 +00:00
|
|
|
mosquitto__free(local_topic);
|
|
|
|
mosquitto__free(split_topics);
|
2014-11-17 22:54:39 +00:00
|
|
|
}else{
|
2019-10-31 12:54:20 +00:00
|
|
|
/* Can't find the message - probably expired */
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__sub_chunk_restore(FILE *db_fptr)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2019-03-16 09:42:15 +00:00
|
|
|
struct P_sub chunk;
|
|
|
|
int rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
memset(&chunk, 0, sizeof(struct P_sub));
|
2014-11-17 21:58:53 +00:00
|
|
|
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 6 || db_version == 5){
|
|
|
|
rc = persist__chunk_sub_read_v56(db_fptr, &chunk);
|
2019-03-20 00:02:38 +00:00
|
|
|
}else{
|
|
|
|
rc = persist__chunk_sub_read_v234(db_fptr, &chunk);
|
|
|
|
}
|
2019-01-31 21:50:42 +00:00
|
|
|
if(rc){
|
|
|
|
return rc;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-11-17 21:58:53 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
rc = persist__restore_sub(chunk.client_id, chunk.topic, chunk.F.qos, chunk.F.identifier, chunk.F.options);
|
2019-03-16 09:42:15 +00:00
|
|
|
|
|
|
|
mosquitto__free(chunk.client_id);
|
|
|
|
mosquitto__free(chunk.topic);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:13:08 +00:00
|
|
|
|
2020-10-17 00:23:08 +00:00
|
|
|
int persist__chunk_header_read(FILE *db_fptr, uint32_t *chunk, uint32_t *length)
|
2019-03-19 17:13:08 +00:00
|
|
|
{
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 6 || db_version == 5){
|
|
|
|
return persist__chunk_header_read_v56(db_fptr, chunk, length);
|
2019-03-20 00:02:38 +00:00
|
|
|
}else{
|
|
|
|
return persist__chunk_header_read_v234(db_fptr, chunk, length);
|
|
|
|
}
|
2019-03-19 17:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
int persist__restore(void)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
FILE *fptr;
|
|
|
|
char header[15];
|
|
|
|
int rc = 0;
|
|
|
|
uint32_t crc;
|
2019-03-19 17:13:08 +00:00
|
|
|
uint32_t i32temp;
|
2020-10-17 00:23:08 +00:00
|
|
|
uint32_t chunk, length;
|
|
|
|
size_t rlen;
|
2018-11-07 17:43:21 +00:00
|
|
|
char *err;
|
2014-11-18 19:12:08 +00:00
|
|
|
struct mosquitto_msg_store_load *load, *load_tmp;
|
2019-03-20 00:02:38 +00:00
|
|
|
struct PF_cfg cfg_chunk;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
assert(db.config);
|
2019-03-14 20:57:25 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
if(!db.config->persistence || db.config->persistence_filepath == NULL){
|
2019-03-14 20:57:25 +00:00
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
db.msg_store_load = NULL;
|
2014-11-18 19:12:08 +00:00
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
fptr = mosquitto__fopen(db.config->persistence_filepath, "rb", false);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(fptr == NULL) return MOSQ_ERR_SUCCESS;
|
2017-01-06 00:44:17 +00:00
|
|
|
rlen = fread(&header, 1, 15, fptr);
|
|
|
|
if(rlen == 0){
|
|
|
|
fclose(fptr);
|
2017-03-06 21:19:53 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Persistence file is empty.");
|
2017-01-06 00:44:17 +00:00
|
|
|
return 0;
|
|
|
|
}else if(rlen != 15){
|
|
|
|
goto error;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!memcmp(header, magic, 15)){
|
2020-03-12 10:29:11 +00:00
|
|
|
/* Restore DB as normal */
|
2014-05-07 22:27:00 +00:00
|
|
|
read_e(fptr, &crc, sizeof(uint32_t));
|
|
|
|
read_e(fptr, &i32temp, sizeof(uint32_t));
|
|
|
|
db_version = ntohl(i32temp);
|
|
|
|
/* IMPORTANT - this is where compatibility checks are made.
|
|
|
|
* Is your DB change still compatible with previous versions?
|
|
|
|
*/
|
2020-05-15 21:11:26 +00:00
|
|
|
if(db_version != MOSQ_DB_VERSION){
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 5){
|
|
|
|
/* Addition of username and listener_port to client chunk in v6 */
|
|
|
|
}else if(db_version == 4){
|
2019-03-20 00:02:38 +00:00
|
|
|
}else if(db_version == 3){
|
2019-01-31 21:50:42 +00:00
|
|
|
/* Addition of source_username and source_port to msg_store chunk in v4, v1.5.6 */
|
|
|
|
}else if(db_version == 2){
|
2014-05-07 22:27:00 +00:00
|
|
|
/* Addition of disconnect_t to client chunk in v3. */
|
|
|
|
}else{
|
|
|
|
fclose(fptr);
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unsupported persistent database format version %d (need version %d).", db_version, MOSQ_DB_VERSION);
|
2014-05-07 22:27:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:13:08 +00:00
|
|
|
while(persist__chunk_header_read(fptr, &chunk, &length) == MOSQ_ERR_SUCCESS){
|
2014-05-07 22:27:00 +00:00
|
|
|
switch(chunk){
|
|
|
|
case DB_CHUNK_CFG:
|
2020-08-07 06:32:14 +00:00
|
|
|
if(db_version == 6 || db_version == 5){
|
|
|
|
if(persist__chunk_cfg_read_v56(fptr, &cfg_chunk)){
|
2020-01-29 12:30:24 +00:00
|
|
|
fclose(fptr);
|
2019-03-20 00:02:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(persist__chunk_cfg_read_v234(fptr, &cfg_chunk)){
|
2020-01-29 12:30:24 +00:00
|
|
|
fclose(fptr);
|
2019-03-20 00:02:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(cfg_chunk.dbid_size != sizeof(dbid_t)){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Incompatible database configuration (dbid size is %d bytes, expected %lu)",
|
2019-03-20 00:02:38 +00:00
|
|
|
cfg_chunk.dbid_size, (unsigned long)sizeof(dbid_t));
|
2014-05-07 22:27:00 +00:00
|
|
|
fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-11-06 15:16:07 +00:00
|
|
|
db.last_db_id = cfg_chunk.last_db_id;
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_MSG_STORE:
|
2020-11-23 21:12:13 +00:00
|
|
|
if(persist__msg_store_chunk_restore(fptr, length)){
|
|
|
|
fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_CLIENT_MSG:
|
2020-11-23 21:12:13 +00:00
|
|
|
if(persist__client_msg_chunk_restore(fptr, length)){
|
|
|
|
fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_RETAIN:
|
2020-11-23 21:12:13 +00:00
|
|
|
if(persist__retain_chunk_restore(fptr)){
|
|
|
|
fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_SUB:
|
2020-11-23 21:12:13 +00:00
|
|
|
if(persist__sub_chunk_restore(fptr)){
|
|
|
|
fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DB_CHUNK_CLIENT:
|
2020-11-23 21:12:13 +00:00
|
|
|
if(persist__client_chunk_restore(fptr)){
|
|
|
|
fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Unsupported chunk \"%d\" in persistent database file. Ignoring.", chunk);
|
2014-05-07 22:27:00 +00:00
|
|
|
fseek(fptr, length, SEEK_CUR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to restore persistent database. Unrecognised file format.");
|
2014-05-07 22:27:00 +00:00
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fptr);
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
HASH_ITER(hh, db.msg_store_load, load, load_tmp){
|
|
|
|
HASH_DELETE(hh, db.msg_store_load, load);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(load);
|
2014-11-18 19:12:08 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
error:
|
2018-11-07 17:43:21 +00:00
|
|
|
err = strerror(errno);
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(fptr) fclose(fptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
static int persist__restore_sub(const char *client_id, const char *sub, uint8_t qos, uint32_t identifier, int options)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto *context;
|
|
|
|
|
|
|
|
assert(client_id);
|
|
|
|
assert(sub);
|
|
|
|
|
2020-11-06 15:16:07 +00:00
|
|
|
context = persist__find_or_add_context(client_id, 0);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context) return 1;
|
2020-11-06 15:16:07 +00:00
|
|
|
return sub__add(context, sub, qos, identifier, options, &db.subs);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|