-
Notifications
You must be signed in to change notification settings - Fork 717
#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
Draft
yahyayozo
wants to merge
30
commits into
seladb:dev
Choose a base branch
from
yahyayozo:feature/modbus
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
#1754 Add Modbus Support #1823
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
a36e594
Add ModbusLayer first implementation with header parsing only
yahyayozo 88f69d4
fix static_assert
yahyayozo c257494
Merge branch 'dev' into feature/modbus
yahyayozo 348afc6
Fix formatting in ModbusLayer header file
yahyayozo 1f65c24
Add setter methods for MODBUS header fields in ModbusLayer
yahyayozo 4159c6c
Merge branch 'dev' into feature/modbus
yahyayozo 58c18ec
Refactor ModbusLayer class documentation and fix syntax error
yahyayozo d50db3b
Merge branch 'feature/modbus' of https://github.com/yahyayozo/PcapPlu…
yahyayozo cfcbc69
Fix endianess handling in ModbusLayer getters and setters
yahyayozo 28dd176
Add const qualifier to ModbusLayer getter methods
yahyayozo 34d65a2
Add constructors to ModbusLayer for initializing from user inputs
yahyayozo 74ab79d
Add Modbus protocol constant to ProtocolType
yahyayozo 166c15c
Add const qualifier to ModbusLayer getter methods in .h file
yahyayozo 3624286
Add ModbusLayer and corresponding tests to Packet++Test
yahyayozo bcd14b5
Fix length assignment in ModbusLayer constructor to use htobe16
yahyayozo 9f4ebbd
Refactor ModbusLayerParsingTest to create ModbusLayer from a dat file…
yahyayozo 4ceb41d
Fix formatting in ModbusLayer constructor for improved readability
yahyayozo adb3f51
Remove redundant ModbusLayerParsingTest case from ModbusTests.cpp
yahyayozo 839aa49
Remove ModbusLayerParsingTest from test definitions and main execution
yahyayozo dc04c0c
Fix newline at end of file in ModbusLayerCreationTest and TestDefinit…
yahyayozo f4f5291
Add ModbusLayer parsing test and utility function for port validation
yahyayozo 24c0924
Merge branch 'dev' into feature/modbus
yahyayozo 32da5e5
Add missing includes for iostream and iomanip in ModbusLayer.cpp
yahyayozo da52bf7
Refactor ModbusLayerCreationTest to include additional assertions for…
yahyayozo c8bd099
Merge branch 'feature/modbus' of https://github.com/yahyayozo/PcapPlu…
yahyayozo b96671c
Merge branch 'dev' into feature/modbus
yahyayozo 5dd18fb
Merge branch 'dev' into feature/modbus
yahyayozo 4b38a65
Refactor Modbus header structure and update related methods for consi…
yahyayozo 65b37f6
Merge branch 'feature/modbus' of https://github.com/yahyayozo/PcapPlu…
yahyayozo 32211b8
Fix ModbusLayer::toString method declaration for consistency
yahyayozo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#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: | ||
/// 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, uint8_t functionCode); | ||
|
||
/// @brief Check if a port is a valid MODBUS port | ||
/// @param port | ||
/// @return true if the port is valid, false otherwise | ||
static bool isModbusPort(uint16_t port) | ||
{ | ||
return port == 502; | ||
} | ||
|
||
/// @return A pointer to the MODBUS header | ||
modbus_header* getModbusHeader() const; | ||
|
||
/// @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 | ||
uint8_t 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(uint8_t 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); | ||
} | ||
|
||
/// Each layer can compute field values automatically using this method. This is an abstract method | ||
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; | ||
} | ||
}; | ||
|
||
} // namespace pcpp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "ModbusLayer.h" | ||
#include "EndianPortable.h" | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <cstring> | ||
|
||
namespace pcpp | ||
{ | ||
ModbusLayer::ModbusLayer(uint16_t transactionId, uint8_t unitId, uint8_t functionCode) | ||
{ | ||
const size_t headerLen = sizeof(modbus_header); | ||
m_DataLen = headerLen; | ||
m_Data = new uint8_t[headerLen]; | ||
memset(m_Data, 0, headerLen); | ||
|
||
// 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(2); // minimum length of the MODBUS payload + unit_id | ||
header->unitId = unitId; | ||
header->functionCode = 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; | ||
} | ||
|
||
uint8_t ModbusLayer::getFunctionCode() const | ||
{ | ||
return getModbusHeader()->functionCode; | ||
} | ||
|
||
void ModbusLayer::setTransactionId(uint16_t transactionId) | ||
{ | ||
getModbusHeader()->transactionId = htobe16(transactionId); | ||
} | ||
|
||
void ModbusLayer::setUnitId(uint8_t unitId) | ||
{ | ||
getModbusHeader()->unitId = unitId; | ||
} | ||
|
||
void ModbusLayer::setFunctionCode(uint8_t functionCode) | ||
{ | ||
getModbusHeader()->functionCode = 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(getFunctionCode()); | ||
} | ||
|
||
} // namespace pcpp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0002b3ce705100207800620d08004500003085a14000800660eb0a0000390a0000030a1201f66197f1e370f1ad6f5018fa9c18f500000000000000020a11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "../TestDefinition.h" | ||
#include "../Utils/TestUtils.h" | ||
#include "Packet.h" | ||
#include "ModbusLayer.h" | ||
#include "EndianPortable.h" | ||
#include "SystemUtils.h" | ||
|
||
PTF_TEST_CASE(ModbusLayerCreationTest) | ||
{ | ||
pcpp::ModbusLayer modbusLayer(12345, 1, 3); | ||
|
||
PTF_ASSERT_EQUAL(modbusLayer.getTransactionId(), 12345); | ||
PTF_ASSERT_EQUAL(modbusLayer.getProtocolId(), 0); | ||
PTF_ASSERT_EQUAL(modbusLayer.getLength(), 2); // minimum length of the MODBUS payload + unit_id | ||
PTF_ASSERT_EQUAL(modbusLayer.getUnitId(), 1); | ||
PTF_ASSERT_EQUAL(modbusLayer.getFunctionCode(), 3); | ||
PTF_ASSERT_EQUAL(modbusLayer.getHeaderLen(), sizeof(pcpp::modbus_header)); | ||
|
||
PTF_ASSERT_EQUAL(modbusLayer.getOsiModelLayer(), pcpp::OsiModelApplicationLayer); | ||
|
||
modbusLayer.setTransactionId(54321); | ||
PTF_ASSERT_EQUAL(modbusLayer.getTransactionId(), 54321); | ||
modbusLayer.setUnitId(2); | ||
PTF_ASSERT_EQUAL(modbusLayer.getUnitId(), 2); | ||
modbusLayer.setFunctionCode(6); | ||
PTF_ASSERT_EQUAL(modbusLayer.getFunctionCode(), 6); | ||
|
||
// just to pass the codecov | ||
modbusLayer.computeCalculateFields(); | ||
|
||
} // ModbusLayerCreationTest | ||
|
||
PTF_TEST_CASE(ModbusLayerParsingTest) | ||
{ | ||
timeval time; | ||
gettimeofday(&time, nullptr); | ||
|
||
READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/ModbusRequest.dat"); | ||
|
||
pcpp::Packet packet(&rawPacket1); | ||
PTF_ASSERT_TRUE(packet.isPacketOfType(pcpp::Modbus)); | ||
|
||
pcpp::ModbusLayer* modbusLayer = packet.getLayerOfType<pcpp::ModbusLayer>(); | ||
PTF_ASSERT_NOT_NULL(modbusLayer); | ||
PTF_ASSERT_EQUAL(modbusLayer->getTransactionId(), 0); | ||
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. Can you get a packet where transaction ID is not 0? |
||
PTF_ASSERT_EQUAL(modbusLayer->getProtocolId(), 0); | ||
PTF_ASSERT_EQUAL(modbusLayer->getLength(), 2); | ||
PTF_ASSERT_EQUAL(modbusLayer->getUnitId(), 10); | ||
PTF_ASSERT_EQUAL(modbusLayer->getFunctionCode(), 17); | ||
|
||
PTF_ASSERT_EQUAL(modbusLayer->toString(), | ||
"Modbus Layer, Transaction ID: 12345, Protocol ID: 0, Length: 2, Unit ID: 1, Function Code: 3"); | ||
} // ModbusLayerParsingTest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best way to test layer creation is to compare it to a real packet. You can load
ModbusRequest.dat
, extract the modbus layer and compare it to the created packet in this test