7 #include <unordered_map>
145 explicit operator std::string()
const
155 constexpr
operator Value()
const
161 explicit operator bool()
const =
delete;
185 return m_MessageType;
191 return m_MessageOrigin;
216 : m_Data(data), m_DataLen(dataLen), m_MessageType(messageType), m_MessageOrigin(origin)
219 static constexpr
int basicMessageLength = 4;
220 static constexpr
int commandLength = 1;
221 static constexpr
int packetNumberIndex = 3;
222 static constexpr
int commandIndex = packetNumberIndex + 1;
224 const uint8_t* m_Data;
264 static constexpr
int statementIndex = commandIndex + 3;
289 static constexpr
int errorCodeIndex = commandIndex + 1;
290 static constexpr
int sqlStateIndex = errorCodeIndex +
sizeof(uint16_t) + 1;
291 static constexpr
int sqlStateSize = 5;
292 static constexpr
int errorMessageIndex = sqlStateIndex + sqlStateSize;
330 return m_MessageOrigin;
344 template <class TMessage, std::enable_if_t<std::is_base_of<MySqlMessage, TMessage>::value,
bool> =
nullptr>
348 for (
const auto& msg : messages)
350 auto result =
dynamic_cast<const TMessage*
>(msg);
351 if (result !=
nullptr)
387 mutable bool m_MessagesInitialized =
false;
390 :
Layer(data, dataLen, prevLayer, packet,
MySQL), m_MessageOrigin(origin)
Definition: MySqlLayer.h:234
std::vector< uint8_t > getRawPayload() const override
Returns the raw payload bytes of the message.
uint32_t getMessageLength() const override
Returns the length of the message payload.
Definition: MySqlLayer.h:270
std::string getSqlState() const
uint16_t getErrorCode() const
std::string getErrorMessage() const
Definition: MySqlLayer.h:298
void parseNextLayer() override
Does nothing for this layer, MySQL is always last.
Definition: MySqlLayer.h:368
std::string toString() const override
const PointerVector< MySqlMessage > & getMySqlMessages() const
const TMessage * getMySqlMessage() const
Get a MySQL message by its type (template version)
Definition: MySqlLayer.h:345
MySqlMessageOrigin getMySqlOrigin() const
Definition: MySqlLayer.h:328
OsiModelLayer getOsiModelLayer() const override
Definition: MySqlLayer.h:376
static MySqlLayer * parseMySqlClientMessage(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Parse a MySQL client message.
size_t getHeaderLen() const override
Definition: MySqlLayer.h:362
~MySqlLayer() override=default
Destructor for this layer.
void computeCalculateFields() override
Does nothing for this layer.
Definition: MySqlLayer.h:372
const MySqlMessage * getMySqlMessage(const MySqlMessageType &messageType) const
Get a MySQL message by its type.
static MySqlLayer * parseMySqlServerMessage(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Parse a MySQL server message.
static bool isMySqlPort(uint16_t port)
Check whether the port is considered as MySQL.
Definition: MySqlLayer.h:306
Definition: MySqlLayer.h:170
static std::unique_ptr< MySqlMessage > parseMySqlMessage(const uint8_t *data, size_t dataLen, MySqlMessageOrigin origin)
Parse a MySQL message from raw data.
virtual uint32_t getMessageLength() const
Returns the length of the message payload.
MySqlMessageOrigin getMessageOrigin() const
Definition: MySqlLayer.h:189
size_t getTotalMessageLength() const
Returns the total length of the message including the length field.
Definition: MySqlLayer.h:204
MySqlMessageType getMessageType() const
Definition: MySqlLayer.h:183
uint8_t getPacketNumber() const
Returns the packet number.
virtual std::vector< uint8_t > getRawPayload() const
Returns the raw payload bytes of the message.
Definition: MySqlLayer.h:29
constexpr MySqlMessageType(Value value)
Constructs a MySqlMessageType object from a Value enum.
Definition: MySqlLayer.h:126
MySqlMessageOrigin getOrigin() const
Returns the origin of the message (client or server)
friend std::ostream & operator<<(std::ostream &os, const MySqlMessageType &messageType)
Stream operator for MySqlMessageType.
Definition: MySqlLayer.h:137
Value
Define enum types for MySQL message types.
Definition: MySqlLayer.h:33
@ Client_FieldList
COM_FIELD_LIST command - get field information for a table.
Definition: MySqlLayer.h:45
@ Client_Query
COM_QUERY command - execute SQL query.
Definition: MySqlLayer.h:43
@ Client_Statistics
COM_STATISTICS command - get server status information.
Definition: MySqlLayer.h:55
@ Client_TableDump
COM_TABLE_DUMP command - dump table data (slave replication)
Definition: MySqlLayer.h:75
@ Client_ProcessInfo
COM_PROCESS_INFO command - get list of active threads.
Definition: MySqlLayer.h:57
@ Client_Time
COM_TIME command.
Definition: MySqlLayer.h:67
@ Client_ProcessKill
COM_PROCESS_KILL command - kill a server thread.
Definition: MySqlLayer.h:61
@ Server_Data
Server message that contains data (could be column definition, row data, result set header,...
Definition: MySqlLayer.h:114
@ Client_InitDb
COM_INIT_DB command - change default database.
Definition: MySqlLayer.h:41
@ Client_Refresh
COM_REFRESH command - flush tables, caches, or logs.
Definition: MySqlLayer.h:51
@ Client_Debug
COM_DEBUG command - send debug info to server.
Definition: MySqlLayer.h:63
@ Client_Clone
COM_CLONE command.
Definition: MySqlLayer.h:101
@ Client_BinlogDumpGtid
COM_BINLOG_DUMP_GTID command - start GTID-based binary log dump.
Definition: MySqlLayer.h:97
@ Server_EOF
EOF message.
Definition: MySqlLayer.h:110
@ Client_SetOption
COM_SET_OPTION command - set option (e.g., multi-statements)
Definition: MySqlLayer.h:91
@ Client_StmtFetch
COM_STMT_FETCH command - fetch rows from prepared statement.
Definition: MySqlLayer.h:85
@ Server_Handshake
Initial handshake.
Definition: MySqlLayer.h:104
@ Server_Ok
OK packet - successful response from server.
Definition: MySqlLayer.h:106
@ Client_Shutdown
COM_SHUTDOWN command - shutdown the server.
Definition: MySqlLayer.h:53
@ Client_RegisterSlave
COM_REGISTER_SLAVE command - register slave with master.
Definition: MySqlLayer.h:79
@ Server_AuthSwitchRequest
AuthSwitchRequest packet - server requests authentication method switch.
Definition: MySqlLayer.h:108
@ Client_ChangeUser
COM_CHANGE_USER command - change user and database.
Definition: MySqlLayer.h:71
@ Client_StmtClose
COM_STMT_CLOSE command - close prepared statement.
Definition: MySqlLayer.h:87
@ Client_StmtReset
COM_STMT_RESET command - reset prepared statement data.
Definition: MySqlLayer.h:89
@ Client_DropDb
COM_DROP_DB command.
Definition: MySqlLayer.h:49
@ Client_ResetConnection
COM_RESET_CONNECTION command - reset connection without re-authentication.
Definition: MySqlLayer.h:99
@ Client_Ping
COM_PING command - check server availability.
Definition: MySqlLayer.h:65
@ Client_HandshakeResponse
Handshake response.
Definition: MySqlLayer.h:35
@ Client_Daemon
COM_DAEMON command.
Definition: MySqlLayer.h:95
@ Client_StmtPrepare
COM_STMT_PREPARE command - prepare statement.
Definition: MySqlLayer.h:81
@ Unknown
Unknown message.
Definition: MySqlLayer.h:117
@ Client_ConnectOut
COM_CONNECT_OUT command - slave connecting to master (internal use)
Definition: MySqlLayer.h:77
@ Client_BinlogDump
COM_BINLOG_DUMP command - start binary log dump (slave replication)
Definition: MySqlLayer.h:73
@ Server_Error
Error packet - error response from server.
Definition: MySqlLayer.h:112
@ Client_StmtExecute
COM_STMT_EXECUTE command - execute prepared statement.
Definition: MySqlLayer.h:83
@ Client_Connect
COM_CONNECT command - connect to server (internal use)
Definition: MySqlLayer.h:59
@ Client_Sleep
COM_SLEEP command.
Definition: MySqlLayer.h:37
@ Client_CreateDb
COM_CREATE_DB command.
Definition: MySqlLayer.h:47
@ Client_Quit
COM_QUIT command - close connection.
Definition: MySqlLayer.h:39
@ Client_StmtSendLongData
COM_STMT_SEND_LONG_DATA command - send long data for prepared statement.
Definition: MySqlLayer.h:93
@ Client_DelayedInsert
COM_DELAYED_INSERT command.
Definition: MySqlLayer.h:69
std::string toString() const
Returns a string representation of the message type.
Definition: MySqlLayer.h:251
std::string getQuery() const
Definition: PointerVector.h:50
const ProtocolType MySQL
MySQL protocol.
Definition: ProtocolType.h:258
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
MySqlMessageOrigin
Definition: MySqlLayer.h:19
@ Client
Message is from the MySQL client.
@ Server
Message is from the MySQL server.
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:267
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:281