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
|
2020-11-25 17:34:21 +00:00
|
|
|
are made available under the terms of the Eclipse Public License 2.0
|
2014-05-07 22:27:00 +00:00
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
2021-10-05 14:20:37 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
The Eclipse Public License is available at
|
2020-11-25 17:34:21 +00:00
|
|
|
https://www.eclipse.org/legal/epl-2.0/
|
2014-05-07 22:27:00 +00:00
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
2021-10-05 14:20:37 +00:00
|
|
|
|
2021-01-20 11:46:18 +00:00
|
|
|
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
2020-12-01 18:21:59 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PERSIST_H
|
|
|
|
#define PERSIST_H
|
|
|
|
|
2021-03-21 09:17:53 +00:00
|
|
|
#include "mosquitto_broker_internal.h"
|
|
|
|
|
2020-08-07 06:32:14 +00:00
|
|
|
#define MOSQ_DB_VERSION 6
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
/* DB read/write */
|
2019-03-14 11:08:30 +00:00
|
|
|
extern const unsigned char magic[15];
|
2014-05-07 22:27:00 +00:00
|
|
|
#define DB_CHUNK_CFG 1
|
|
|
|
#define DB_CHUNK_MSG_STORE 2
|
|
|
|
#define DB_CHUNK_CLIENT_MSG 3
|
|
|
|
#define DB_CHUNK_RETAIN 4
|
|
|
|
#define DB_CHUNK_SUB 5
|
|
|
|
#define DB_CHUNK_CLIENT 6
|
|
|
|
/* End DB read/write */
|
|
|
|
|
|
|
|
#define read_e(f, b, c) if(fread(b, 1, c, f) != c){ goto error; }
|
|
|
|
#define write_e(f, b, c) if(fwrite(b, 1, c, f) != c){ goto error; }
|
|
|
|
|
2019-03-16 09:42:15 +00:00
|
|
|
/* COMPATIBILITY NOTES
|
|
|
|
*
|
|
|
|
* The P_* structs (persist structs) contain all of the data for a particular
|
|
|
|
* data chunk. They are loaded in multiple parts, so can be rearranged without
|
|
|
|
* updating the db format version.
|
|
|
|
*
|
|
|
|
* The PF_* structs (persist fixed structs) contain the fixed size data for a
|
|
|
|
* particular data chunk. They are written to disk as is, so they must not be
|
|
|
|
* rearranged without updating the db format version. When adding new members,
|
|
|
|
* always use explicit sized datatypes ("uint32_t", not "long"), and check
|
|
|
|
* whether what is being added can go in an existing hole in the struct.
|
|
|
|
*/
|
|
|
|
|
2019-03-20 00:02:38 +00:00
|
|
|
struct PF_header{
|
|
|
|
uint32_t chunk;
|
|
|
|
uint32_t length;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PF_cfg{
|
|
|
|
uint64_t last_db_id;
|
|
|
|
uint8_t shutdown;
|
|
|
|
uint8_t dbid_size;
|
|
|
|
};
|
|
|
|
|
2020-08-07 06:32:14 +00:00
|
|
|
struct PF_client_v5{
|
|
|
|
int64_t session_expiry_time;
|
|
|
|
uint32_t session_expiry_interval;
|
|
|
|
uint16_t last_mid;
|
|
|
|
uint16_t id_len;
|
|
|
|
};
|
2019-03-16 09:42:15 +00:00
|
|
|
struct PF_client{
|
2020-08-07 06:32:14 +00:00
|
|
|
/* struct PF_client_v5; */
|
2019-03-26 14:14:46 +00:00
|
|
|
int64_t session_expiry_time;
|
2019-03-20 00:02:38 +00:00
|
|
|
uint32_t session_expiry_interval;
|
2019-03-16 09:42:15 +00:00
|
|
|
uint16_t last_mid;
|
|
|
|
uint16_t id_len;
|
2020-08-07 06:32:14 +00:00
|
|
|
|
|
|
|
uint16_t listener_port;
|
|
|
|
uint16_t username_len;
|
2021-10-05 14:20:37 +00:00
|
|
|
/* tail: 4 byte padding, because 64bit member
|
2020-08-07 06:32:14 +00:00
|
|
|
* forces multiple of 8 for struct size */
|
2019-03-16 09:42:15 +00:00
|
|
|
};
|
|
|
|
struct P_client{
|
|
|
|
struct PF_client F;
|
|
|
|
char *client_id;
|
2020-08-07 06:32:14 +00:00
|
|
|
char *username;
|
2019-03-16 09:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PF_client_msg{
|
|
|
|
dbid_t store_id;
|
|
|
|
uint16_t mid;
|
|
|
|
uint16_t id_len;
|
|
|
|
uint8_t qos;
|
|
|
|
uint8_t state;
|
2019-03-20 00:02:38 +00:00
|
|
|
uint8_t retain_dup;
|
2019-03-16 09:42:15 +00:00
|
|
|
uint8_t direction;
|
|
|
|
};
|
|
|
|
struct P_client_msg{
|
|
|
|
struct PF_client_msg F;
|
|
|
|
char *client_id;
|
2019-03-23 22:52:08 +00:00
|
|
|
mosquitto_property *properties;
|
2019-03-16 09:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PF_msg_store{
|
|
|
|
dbid_t store_id;
|
2019-03-26 14:14:46 +00:00
|
|
|
int64_t expiry_time;
|
2019-03-16 09:42:15 +00:00
|
|
|
uint32_t payloadlen;
|
|
|
|
uint16_t source_mid;
|
|
|
|
uint16_t source_id_len;
|
2019-03-19 16:26:29 +00:00
|
|
|
uint16_t source_username_len;
|
2019-03-16 09:42:15 +00:00
|
|
|
uint16_t topic_len;
|
|
|
|
uint16_t source_port;
|
|
|
|
uint8_t qos;
|
|
|
|
uint8_t retain;
|
|
|
|
};
|
|
|
|
struct P_msg_store{
|
|
|
|
struct PF_msg_store F;
|
2020-11-24 12:14:48 +00:00
|
|
|
void *payload;
|
2019-03-16 09:42:15 +00:00
|
|
|
struct mosquitto source;
|
|
|
|
char *topic;
|
2019-03-21 18:10:04 +00:00
|
|
|
mosquitto_property *properties;
|
2019-03-16 09:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PF_sub{
|
2019-03-21 11:53:46 +00:00
|
|
|
uint32_t identifier;
|
2019-03-16 09:42:15 +00:00
|
|
|
uint16_t id_len;
|
|
|
|
uint16_t topic_len;
|
|
|
|
uint8_t qos;
|
2019-03-21 11:53:46 +00:00
|
|
|
uint8_t options;
|
2019-03-16 09:42:15 +00:00
|
|
|
};
|
|
|
|
struct P_sub{
|
|
|
|
struct PF_sub F;
|
|
|
|
char *client_id;
|
|
|
|
char *topic;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PF_retain{
|
|
|
|
dbid_t store_id;
|
|
|
|
};
|
|
|
|
struct P_retain{
|
|
|
|
struct PF_retain F;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-03-20 00:02:38 +00:00
|
|
|
int persist__read_string_len(FILE *db_fptr, char **str, uint16_t len);
|
2019-03-16 09:42:15 +00:00
|
|
|
int persist__read_string(FILE *db_fptr, char **str);
|
|
|
|
|
2020-10-17 00:23:08 +00:00
|
|
|
int persist__chunk_header_read(FILE *db_fptr, uint32_t *chunk, uint32_t *length);
|
2020-02-06 16:43:29 +00:00
|
|
|
|
2020-10-17 00:23:08 +00:00
|
|
|
int persist__chunk_header_read_v234(FILE *db_fptr, uint32_t *chunk, uint32_t *length);
|
2019-03-20 00:02:38 +00:00
|
|
|
int persist__chunk_cfg_read_v234(FILE *db_fptr, struct PF_cfg *chunk);
|
2020-10-17 00:23:08 +00:00
|
|
|
int persist__chunk_client_read_v234(FILE *db_fptr, struct P_client *chunk, uint32_t db_version);
|
2019-03-20 00:02:38 +00:00
|
|
|
int persist__chunk_client_msg_read_v234(FILE *db_fptr, struct P_client_msg *chunk);
|
2020-10-17 00:23:08 +00:00
|
|
|
int persist__chunk_msg_store_read_v234(FILE *db_fptr, struct P_msg_store *chunk, uint32_t db_version);
|
2019-03-20 00:02:38 +00:00
|
|
|
int persist__chunk_retain_read_v234(FILE *db_fptr, struct P_retain *chunk);
|
|
|
|
int persist__chunk_sub_read_v234(FILE *db_fptr, struct P_sub *chunk);
|
|
|
|
|
2020-10-17 00:23:08 +00:00
|
|
|
int persist__chunk_header_read_v56(FILE *db_fptr, uint32_t *chunk, uint32_t *length);
|
2020-08-07 06:32:14 +00:00
|
|
|
int persist__chunk_cfg_read_v56(FILE *db_fptr, struct PF_cfg *chunk);
|
2020-10-17 00:23:08 +00:00
|
|
|
int persist__chunk_client_read_v56(FILE *db_fptr, struct P_client *chunk, uint32_t db_version);
|
2020-08-07 06:32:14 +00:00
|
|
|
int persist__chunk_client_msg_read_v56(FILE *db_fptr, struct P_client_msg *chunk, uint32_t length);
|
|
|
|
int persist__chunk_msg_store_read_v56(FILE *db_fptr, struct P_msg_store *chunk, uint32_t length);
|
|
|
|
int persist__chunk_retain_read_v56(FILE *db_fptr, struct P_retain *chunk);
|
|
|
|
int persist__chunk_sub_read_v56(FILE *db_fptr, struct P_sub *chunk);
|
|
|
|
|
|
|
|
int persist__chunk_cfg_write_v6(FILE *db_fptr, struct PF_cfg *chunk);
|
|
|
|
int persist__chunk_client_write_v6(FILE *db_fptr, struct P_client *chunk);
|
|
|
|
int persist__chunk_client_msg_write_v6(FILE *db_fptr, struct P_client_msg *chunk);
|
|
|
|
int persist__chunk_message_store_write_v6(FILE *db_fptr, struct P_msg_store *chunk);
|
|
|
|
int persist__chunk_retain_write_v6(FILE *db_fptr, struct P_retain *chunk);
|
|
|
|
int persist__chunk_sub_write_v6(FILE *db_fptr, struct P_sub *chunk);
|
2019-03-19 16:26:29 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|