Categorygithub.com/iotopen/mosquittodb
repositorypackage
0.0.0-20241106140423-152750bcfa36
Repository: https://github.com/iotopen/mosquittodb.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

Go MosquittoDB

This project aims to create a library for reading and writing mosquitto databases and perhaps provide some documentation about the mosquitto db internals.

  • Reading (partially)
  • Writing

Database file layout

The database always begins with a DBHeader, following with chunk headers and their data in between.

File Layout
DbHeader
Chunk Header0
Chunk Data0
Chunk Header1
Chunk Data1
Chunk HeaderN
Chunk DataN
.....

The database file layout looks like it has been stable and not changed anything over versions. Most differences are slight changes to how chunks looks like.

Chunk IDs:

  1. Configuration Chunk
  2. Message Store
  3. Client Message
  4. Retain
  5. Subscription
  6. Client

DBHeader

This piece contains a database file magic, a CRC and a Version. These fields should always be first and always be present in a mosquitto database.

NameTypeNotes
Magic[15]byteAlways "\x00\xB5\x00mosquitto db"
CRCuint32I don't see this crc used anywhere
Versionuint32Version of database. At time of writing 6 is the latest

ChunkHeader

This header contains a chunk type id, and the chunk length. Having the chunk length enables skipping unknown / unwanted chunks.

V5, V6

NameTypeNotes
Typeuint32One of the Chunk type IDs
Lengthuint32Length of the chunk

V2, V3, V4

NameTypeNotes
Typeuint16One of the Chunk type IDs
Lengthuint32Length of the chunk

Config Chunk

I'm guessing this chunk always comes right after the DBHeader, and only one of this chunk is ever present in the file.

This chunk contains information about last allocated StoreID, whether the broker shut down properly and the size of a StoreID.

V5, V6

NameTypeNotes
LastStoreIDuint64Varies in size?
Shutdownuint8this field is 1 if mosquitto was shut down properly
StoreIDSizeuint8Size of a StoreID

V2, V3, V4

NameTypeNotes
Shutdownuint8this field is 1 if mosquitto was shut down properly
StoreIDSizeuint8Size of a StoreID
LastStoreIDuint64Varies in size?

Message Store Chunk

This chunk contains one stored message.

V5, V6

NameTypeNotes
StoreIDuint64Message store ID
ExpiryTimeint64Timestamp when this message is expired
PayloadLenuint32Payload length
SourceMiduint16Source message id
SourceIDLenuint16Source client id length
SourceUsernameLenuint16Source username length
TopicLenuint16Topic length
SourcePortuint16Source port
QoSuint8Quality of service
Retainuint8is 1 if this message is retained
SourceIDstringcontains the source client id. size comes from SourceIDLen field
SourceUsernamestringcontains the source username. size comes from SourceUsernameLen
TopicstringMessage topic. size comes from TopicLen field
Payload[]bytepayload data. size comes from PayloadLen field
Properties-this field covers the rest of the chunk length. This version of the library skips this field

V4

NameTypeNotes
StoreIDuint64Message store ID
SourceIDLenuint16Source client id length
SourceIDstringcontains the source client id. size comes from SourceIDLen field
SourceUsernameLenuint16V4 only! Source username length
SourceUsernamestringV4 only! Source username. size comes from SourceUsernameLen field
SourcePortuint16V4 only! Source port
SourceMiduint16Source message id
MIDuint16This field is never used and is not stored anywhere when read
TopicLenuint16Topic length
TopicstringMessage topic. size comes from TopicLen field
QoSuint8Quality of service
Retainuint8is 1 if this message is retained
PayloadLenuint32Payload length
Payload[]bytepayload data. size comes from PayloadLen field

V2, V3

NameTypeNotes
StoreIDuint64Message store ID
SourceIDLenuint16Source client id length
SourceIDstringcontains the source client id. size comes from SourceIDLen field
SourceMiduint16Source message id
MIDuint16This field is never used and is not stored anywhere when read
TopicLenuint16Topic length
TopicstringMessage topic. size comes from TopicLen field
QoSuint8Quality of service
Retainuint8is 1 if this message is retained
PayloadLenuint32Payload length
Payload[]bytepayload data. size comes from PayloadLen field

Client Message Chunk

V5, V6

NameTypeNotes
StoreIDuint64Message store ID
MIDuint16Message ID
IDLenuint16Client id length
QoSuint8Quality of Service
Stateuint8Some internal message state for mosquitto. TODO: enumerate
RetainDupuint8Retention: (flags & 0xF0 >> 4) Dup: (flags & 0x0F)
Directionuint8Direction of message. TODO: enumerate
ClientIDstringClientID for message. size comes from IDLen field

V2, V3, V4

NameTypeNotes
IDLenuint16Client id length
ClientIDstringClientID for message. size comes from IDLen field
StoreIDuint64Message store ID
MIDuint16Message ID
QoSuint8Quality of Service
Retainuint8Retain flag
Directionuint8Direction of message. TODO: enumerate
Stateuint8Some internal message state for mosquitto. TODO: enumerate
Dupuint8Duplicate flag

Retain Chunk

This chunk contains an ID for a stored message, that is retained.

I'm unsure why this chunk is necessary. Both the Stored Message chunk, and the Client message chunk contains a retain-flag.

The StoreID seems to refer to a Message in a store chunk.

V2,V3, V4, V5, V6

NameTypeNotes
StoreIDuint64Message store ID

Subscription Chunk

V5, V6

NameTypeNotes
Identifieruint32mqttv5 subscription identifier
IDLenuint16Length of client id
TopicLenuint16Length of topic
QoSuint8Quality of service
Optionsuint8Subscription options
ClientIDstringClient id for subscription. size comes from IDLen field
TopicstringSubscription topic. size comes from TopicLen field

V2, V3, V4

NameTypeNotes
IDLenuint16Length of client id
ClientIDstringClient id for subscription. size comes from IDLen field
TopicLenuint16Length of topic
TopicstringSubscription topic. size comes from TopicLen field
QoSuint8Quality of service

Client Chunk

This chunk keeps information about one client.

V6

NameTypeNotes
SessionExpiryTimeint64Timestamp when the client is considered expired
SessionExpiryIntervaluint32Used for mqttv5 clients, set by "Session Expiry Interval" property
LastMIDuint16Last MessageID for client
IDLenuint16Length of client id
ListenerPortuint16Listener port. new in v6
UsernameLenuint16Username length. new in v6
Paddinguint32Quote from mosquitto source: "tail: 4 byte padding, because 64bit member forces multiple of 8 for struct size"
ClientIDstringClient id. size comes from IDLen field
UsernamestringUsername of client. size comes from UsernameLen field. new in v6

V5

NameTypeNotes
SessionExpiryTimeint64Timestamp when the client is considered expired
SessionExpiryIntervaluint32Used for mqttv5 clients, set by "Session Expiry Interval" property
LastMIDuint16Last MessageID for client
IDLenuint16Length of client id
ClientIDstringClient id. size comes from IDLen field

V3, V4

NameTypeNotes
IDLenuint16Length of client id
ClientIDstringClient id. size comes from IDLen field
Timetime_tPresumably the time this message was sent. This field appears not to be saved anywhere and just read then ignored.

V2

NameTypeNotes
IDLenuint16Length of client id
ClientIDstringClient id. size comes from IDLen field