-
Notifications
You must be signed in to change notification settings - Fork 722
#1754 Add Modbus Support #1823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1754 Add Modbus Support #1823
Changes from 50 commits
a36e594
88f69d4
c257494
348afc6
1f65c24
4159c6c
58c18ec
d50db3b
cfcbc69
28dd176
34d65a2
74ab79d
166c15c
3624286
bcd14b5
9f4ebbd
4ceb41d
adb3f51
839aa49
dc04c0c
f4f5291
24c0924
32da5e5
da52bf7
c8bd099
b96671c
5dd18fb
4b38a65
65b37f6
32211b8
0f001b6
adea8b5
530c8cc
73228fb
12b1650
494d84a
737e6de
9b50332
75dff34
a5b13c7
3e56991
18a80df
3c746e9
ec3e582
4a8f115
c50f58b
ea86788
0cd7502
fc595a7
951fe03
93fb5e7
a7b926d
8bc7c47
e3461d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| #pragma once | ||
|
|
||
| #include "Layer.h" | ||
|
|
||
| /// @file | ||
| /// This file contains classes for parsing, creating and editing Modbus packets. | ||
|
|
||
| /// @namespace pcpp | ||
| /// @brief The main namespace for the PcapPlusPlus lib | ||
| namespace pcpp | ||
| { | ||
|
|
||
| #pragma pack(push, 1) | ||
| /// @struct modbus_header | ||
| /// MODBUS Application Protocol header | ||
| struct modbus_header | ||
| { | ||
| /// For synchronization between messages of server and client | ||
| uint16_t transactionId; | ||
| /// 0 for Modbus/TCP | ||
| uint16_t protocolId; | ||
| /// Number of remaining bytes in this frame starting from the unit id | ||
| uint16_t length; | ||
| /// Unit identifier | ||
| uint8_t unitId; | ||
| /// Function code | ||
| uint8_t functionCode; | ||
| }; | ||
| #pragma pack(pop) | ||
| static_assert(sizeof(modbus_header) == 8, "modbus_header size is not 8 bytes"); | ||
|
|
||
| /// @class ModbusLayer | ||
| /// Represents the MODBUS Application Protocol layer | ||
| class ModbusLayer : public Layer | ||
| { | ||
| public: | ||
| // @brief Enum class representing Modbus function codes. | ||
| // This enumeration defines the standard Modbus function codes used in request and response PDUs. | ||
| // Each value corresponds to a specific operation defined by the Modbus protocol. | ||
| enum class ModbusFunctionCode : uint8_t | ||
| { | ||
| // Read coil status (0x01) | ||
| READ_COILS = 1, | ||
|
|
||
| // Read discrete input status (0x02) | ||
| READ_DISCRETE_INPUTS = 2, | ||
|
|
||
| // Read holding registers (0x03) | ||
| READ_HOLDING_REGISTERS = 3, | ||
|
|
||
| // Read input registers (0x04) | ||
| READ_INPUT_REGISTERS = 4, | ||
|
|
||
| // Write a single coil (0x05) | ||
| WRITE_SINGLE_COIL = 5, | ||
|
|
||
| // Write a single holding register (0x06) | ||
| WRITE_SINGLE_REGISTER = 6, | ||
|
|
||
| // Write multiple coils (0x0F) | ||
| WRITE_MULTIPLE_COILS = 15, | ||
|
|
||
| // Write multiple holding registers (0x10) | ||
| WRITE_MULTIPLE_REGISTERS = 16, | ||
|
|
||
| // Report slave ID (0x11) | ||
| REPORT_SLAVE_ID = 17, | ||
|
|
||
| // Limit to check if the function code is valid | ||
| FUNCTION_CODE_LIMIT, | ||
|
|
||
| // Unknown or unsupported function code (0xFF) | ||
| UNKNOWN_FUNCTION = 0xFF | ||
| }; | ||
|
|
||
| /// @struct ModbusReadInputRegisters | ||
| /// Represents a Modbus request to read input registers. | ||
| struct ModbusReadInputRegisters | ||
| { | ||
| uint16_t startingAddress; ///< Starting address of the input registers to read | ||
| uint16_t quantity; ///< Number of input registers to read | ||
| }; | ||
|
Comment on lines
+73
to
+79
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This struct is only used in |
||
|
|
||
| /// A constructor that creates the layer from an existing packet raw data | ||
| /// @param[in] data A pointer to the raw data | ||
| /// @param[in] dataLen Size of the data in bytes | ||
| /// @param[in] prevLayer A pointer to the previous layer | ||
| /// @param[in] packet A pointer to the Packet instance where layer will be stored in | ||
| ModbusLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) | ||
| : Layer(data, dataLen, prevLayer, packet, Modbus) | ||
| {} | ||
|
|
||
| /// A constructor that creates the layer from user inputs | ||
| /// @param[in] transactionId Transaction ID | ||
| /// @param[in] unitId Unit ID | ||
| /// @param[in] functionCode Function code | ||
| ModbusLayer(uint16_t transactionId, uint8_t unitId, ModbusFunctionCode functionCode); | ||
|
|
||
| /// @brief Check if a port is a valid MODBUS port | ||
| /// @param port Port number to check | ||
| /// @note MODBUS uses port 502, so this function checks if the port is equal to 502 | ||
| /// @return true if the port is valid, false otherwise | ||
| static bool isModbusPort(uint16_t port) | ||
| { | ||
| return port == 502; | ||
| } | ||
|
|
||
| /// @return MODBUS message type | ||
| uint16_t getTransactionId() const; | ||
|
|
||
| /// @return MODBUS protocol id | ||
| uint16_t getProtocolId() const; | ||
|
|
||
| /// @return MODBUS remaining bytes in frame starting from the unit id | ||
| /// @note This is the length of the MODBUS payload + unit_id, not the entire packet | ||
| uint16_t getLength() const; | ||
|
|
||
| /// @return MODBUS unit id | ||
| uint8_t getUnitId() const; | ||
|
|
||
| /// @return MODBUS function code | ||
| ModbusFunctionCode getFunctionCode() const; | ||
|
|
||
| /// @brief set the MODBUS transaction id | ||
| /// @param transactionId transaction id | ||
| void setTransactionId(uint16_t transactionId); | ||
|
|
||
| /// @brief set the MODBUS header unit id | ||
| /// @param unitId unit id | ||
| void setUnitId(uint8_t unitId); | ||
|
|
||
| /// @brief set the MODBUS header function code | ||
| /// @param functionCode function code | ||
| void setFunctionCode(ModbusFunctionCode functionCode); | ||
|
|
||
| // Overridden methods | ||
|
|
||
| /// Does nothing for this layer (ModbusLayer is always last) | ||
| void parseNextLayer() override | ||
| {} | ||
|
|
||
| /// @brief Get the length of the MODBUS header | ||
| /// @return Length of the MODBUS header in bytes | ||
| size_t getHeaderLen() const override | ||
| { | ||
| return sizeof(modbus_header); | ||
| } | ||
|
|
||
| /// Does nothing for this layer | ||
| void computeCalculateFields() override | ||
| {} | ||
|
|
||
| /// @return A string representation of the layer most important data (should look like the layer description in | ||
| /// Wireshark) | ||
| std::string toString() const override; | ||
|
|
||
| /// @return The OSI Model layer this protocol belongs to | ||
| OsiModelLayer getOsiModelLayer() const override | ||
| { | ||
| return OsiModelApplicationLayer; | ||
| } | ||
|
|
||
| private: | ||
| /// @return A pointer to the MODBUS header | ||
| modbus_header* getModbusHeader() const; | ||
|
|
||
| /// @brief Get the size of the function data based on the function code | ||
| /// @param functionCode The MODBUS function code | ||
| /// @return The size of the function data in bytes, or -1 if unsupported | ||
| int16_t getFunctionDataSize(ModbusFunctionCode functionCode) const; | ||
| }; | ||
|
|
||
| } // namespace pcpp | ||
seladb marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| #include "ModbusLayer.h" | ||
| #include "EndianPortable.h" | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cstring> | ||
| #include "Logger.h" | ||
|
|
||
| namespace pcpp | ||
| { | ||
| ModbusLayer::ModbusLayer(uint16_t transactionId, uint8_t unitId, ModbusLayer::ModbusFunctionCode functionCode) | ||
| { | ||
| const int16_t pduSize = getFunctionDataSize(functionCode); | ||
|
||
| if (pduSize < 0) | ||
| { | ||
| PCPP_LOG_ERROR("Unsupported function code: " << static_cast<int>(functionCode)); | ||
| return; | ||
| } | ||
|
|
||
| const size_t headerLen = sizeof(modbus_header); | ||
|
|
||
| m_DataLen = headerLen + pduSize; | ||
| m_Data = new uint8_t[m_DataLen]{}; | ||
| memset(m_Data, 0, m_DataLen); | ||
|
|
||
| // Initialize the header fields to default values | ||
| modbus_header* header = getModbusHeader(); | ||
| header->transactionId = htobe16(transactionId); | ||
| header->protocolId = 0; // 0 for Modbus/TCP | ||
| header->length = htobe16(pduSize + 2); // Length includes unitId and functionCode | ||
| header->unitId = unitId; | ||
| header->functionCode = static_cast<uint8_t>(functionCode); | ||
| } | ||
|
|
||
| modbus_header* ModbusLayer::getModbusHeader() const | ||
| { | ||
| return (modbus_header*)m_Data; | ||
| } | ||
|
|
||
| uint16_t ModbusLayer::getTransactionId() const | ||
| { | ||
| return be16toh(getModbusHeader()->transactionId); | ||
| } | ||
|
|
||
| uint16_t ModbusLayer::getProtocolId() const | ||
| { | ||
| return be16toh(getModbusHeader()->protocolId); | ||
| } | ||
|
|
||
| uint16_t ModbusLayer::getLength() const | ||
| { | ||
| return be16toh(getModbusHeader()->length); | ||
| } | ||
|
|
||
| uint8_t ModbusLayer::getUnitId() const | ||
| { | ||
| return getModbusHeader()->unitId; | ||
| } | ||
|
|
||
| ModbusLayer::ModbusFunctionCode ModbusLayer::getFunctionCode() const | ||
| { | ||
| ModbusLayer::ModbusFunctionCode functionCode = | ||
| static_cast<ModbusLayer::ModbusFunctionCode>(getModbusHeader()->functionCode); | ||
| if (functionCode >= ModbusLayer::ModbusFunctionCode::FUNCTION_CODE_LIMIT) | ||
| { | ||
| return ModbusLayer::ModbusFunctionCode::UNKNOWN_FUNCTION; | ||
| } | ||
seladb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return functionCode; | ||
| } | ||
|
|
||
| void ModbusLayer::setTransactionId(uint16_t transactionId) | ||
| { | ||
| getModbusHeader()->transactionId = htobe16(transactionId); | ||
| } | ||
|
|
||
| void ModbusLayer::setUnitId(uint8_t unitId) | ||
| { | ||
| getModbusHeader()->unitId = unitId; | ||
| } | ||
|
|
||
| void ModbusLayer::setFunctionCode(ModbusLayer::ModbusFunctionCode functionCode) | ||
| { | ||
| if (functionCode >= ModbusLayer::ModbusFunctionCode::FUNCTION_CODE_LIMIT) | ||
| { | ||
| PCPP_LOG_ERROR("Invalid Modbus function code: " << static_cast<int>(functionCode)); | ||
| return; | ||
yahyayozo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| getModbusHeader()->functionCode = static_cast<uint8_t>(functionCode); | ||
| } | ||
|
|
||
| std::string ModbusLayer::toString() const | ||
| { | ||
| return "Modbus Layer, Transaction ID: " + std::to_string(getTransactionId()) + | ||
| ", Protocol ID: " + std::to_string(getProtocolId()) + ", Length: " + std::to_string(getLength()) + | ||
| ", Unit ID: " + std::to_string(getUnitId()) + | ||
| ", Function Code: " + std::to_string(static_cast<uint8_t>(getFunctionCode())); | ||
| } | ||
|
|
||
| int16_t ModbusLayer::getFunctionDataSize(ModbusFunctionCode functionCode) const | ||
| { | ||
| switch (functionCode) | ||
| { | ||
| // currently supported function codes | ||
| case ModbusFunctionCode::READ_INPUT_REGISTERS: | ||
| return sizeof(ModbusReadInputRegisters); | ||
| default: | ||
| return -1; // For unsupported or unknown function codes | ||
| } | ||
| } | ||
| } // namespace pcpp | ||
seladb marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 000c29af7ffe109add4e060d080045000040c6e64000400658880a0101ea0a0a0555c8d301f6e072efbac405c33a8018ffffd5d900000101080a37c3fee900ba2a0f001100000006ff0402580064 |
Uh oh!
There was an error while loading. Please reload this page.