PcapPlusPlus  Next
pcpp::ModbusLayer Class Reference

#include <ModbusLayer.h>

Inheritance diagram for pcpp::ModbusLayer:
pcpp::Layer pcpp::IDataContainer

Classes

struct  ModbusReadInputRegisters
 

Public Types

enum class  ModbusFunctionCode : uint8_t {
  ReadCoils = 1 , ReadDiscreteInputs = 2 , ReadHoldingRegisters = 3 , ReadInputRegisters = 4 ,
  WriteSingleCoil = 5 , WriteSingleHoldingRegister = 6 , WriteMultipleCoils = 15 , WriteMultipleHoldingRegisters = 16 ,
  ReadSlaveId = 17 , UnknownFunction = 0xFF
}
 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. More...
 

Public Member Functions

 ModbusLayer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 
 ModbusLayer (uint16_t transactionId, uint8_t unitId)
 
uint16_t getTransactionId () const
 
uint16_t getProtocolId () const
 
uint16_t getLength () const
 
uint8_t getUnitId () const
 
ModbusFunctionCode getFunctionCode () const
 
void setTransactionId (uint16_t transactionId)
 set the MODBUS transaction id More...
 
void setUnitId (uint8_t unitId)
 set the MODBUS header unit id More...
 
void setFunctionCode (ModbusFunctionCode functionCode)
 set the MODBUS header function code More...
 
void parseNextLayer () override
 Does nothing for this layer (ModbusLayer is always last)
 
size_t getHeaderLen () const override
 Get the length of the MODBUS header. More...
 
void computeCalculateFields () override
 Does nothing for this layer.
 
std::string toString () const override
 
OsiModelLayer getOsiModelLayer () const override
 
- Public Member Functions inherited from pcpp::Layer
 ~Layer () override
 
LayergetNextLayer () const
 
LayergetPrevLayer () const
 
ProtocolType getProtocol () const
 
bool isMemberOfProtocolFamily (ProtocolTypeFamily protocolTypeFamily) const
 
uint8_t * getData () const
 
size_t getDataLen () const
 
uint8_t * getLayerPayload () const
 
size_t getLayerPayloadSize () const
 
bool isAllocatedToPacket () const
 
void copyData (uint8_t *toArr) const
 
uint8_t * getDataPtr (size_t offset=0) const override
 

Static Public Member Functions

static bool isModbusPort (uint16_t port)
 Check if a port is a valid MODBUS port. More...
 

Additional Inherited Members

- Protected Member Functions inherited from pcpp::Layer
template<typename T , typename... Args>
LayerconstructNextLayer (uint8_t *data, size_t dataLen, Packet *packet, Args &&... extraArgs)
 
template<typename T , typename TFallback , typename... Args>
LayertryConstructNextLayerWithFallback (uint8_t *data, size_t dataLen, Packet *packet, Args &&... extraArgs)
 
- Static Protected Member Functions inherited from pcpp::Layer
template<typename T >
static bool canReinterpretAs (const uint8_t *data, size_t dataLen)
 Check if the data is large enough to reinterpret as a type. More...
 

Detailed Description

Represents the MODBUS Application Protocol layer

Member Enumeration Documentation

◆ ModbusFunctionCode

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.

Enumerator
ReadCoils 

Read coil status (0x01)

ReadDiscreteInputs 

Read discrete input status (0x02)

ReadHoldingRegisters 

Read holding registers (0x03)

ReadInputRegisters 

Read input registers (0x04)

WriteSingleCoil 

Write a single coil (0x05)

WriteSingleHoldingRegister 

Write a single holding register (0x06)

WriteMultipleCoils 

Write multiple coils (0x0F)

WriteMultipleHoldingRegisters 

Write multiple holding registers (0x10)

ReadSlaveId 

Report slave ID (0x11)

UnknownFunction 

Unknown or unsupported function code (0xFF)

Constructor & Destructor Documentation

◆ ModbusLayer() [1/2]

pcpp::ModbusLayer::ModbusLayer ( uint8_t *  data,
size_t  dataLen,
Layer prevLayer,
Packet packet 
)
inline

A constructor that creates the layer from an existing packet raw data

Parameters
[in]dataA pointer to the raw data
[in]dataLenSize of the data in bytes
[in]prevLayerA pointer to the previous layer
[in]packetA pointer to the Packet instance where layer will be stored in

◆ ModbusLayer() [2/2]

pcpp::ModbusLayer::ModbusLayer ( uint16_t  transactionId,
uint8_t  unitId 
)

A constructor that creates the layer from user inputs

Parameters
[in]transactionIdTransaction ID
[in]unitIdUnit ID

Member Function Documentation

◆ getFunctionCode()

ModbusFunctionCode pcpp::ModbusLayer::getFunctionCode ( ) const
Returns
MODBUS function code

◆ getHeaderLen()

size_t pcpp::ModbusLayer::getHeaderLen ( ) const
inlineoverridevirtual

Get the length of the MODBUS header.

Returns
Length of the MODBUS header in bytes

Implements pcpp::Layer.

◆ getLength()

uint16_t pcpp::ModbusLayer::getLength ( ) const
Returns
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

◆ getOsiModelLayer()

OsiModelLayer pcpp::ModbusLayer::getOsiModelLayer ( ) const
inlineoverridevirtual
Returns
The OSI Model layer this protocol belongs to

Implements pcpp::Layer.

◆ getProtocolId()

uint16_t pcpp::ModbusLayer::getProtocolId ( ) const
Returns
MODBUS protocol id

◆ getTransactionId()

uint16_t pcpp::ModbusLayer::getTransactionId ( ) const
Returns
MODBUS message type

◆ getUnitId()

uint8_t pcpp::ModbusLayer::getUnitId ( ) const
Returns
MODBUS unit id

◆ isModbusPort()

static bool pcpp::ModbusLayer::isModbusPort ( uint16_t  port)
inlinestatic

Check if a port is a valid MODBUS port.

Parameters
portPort number to check
Note
MODBUS uses port 502, so this function checks if the port is equal to 502
Returns
true if the port is valid, false otherwise

◆ setFunctionCode()

void pcpp::ModbusLayer::setFunctionCode ( ModbusFunctionCode  functionCode)

set the MODBUS header function code

Parameters
functionCodefunction code

◆ setTransactionId()

void pcpp::ModbusLayer::setTransactionId ( uint16_t  transactionId)

set the MODBUS transaction id

Parameters
transactionIdtransaction id

◆ setUnitId()

void pcpp::ModbusLayer::setUnitId ( uint8_t  unitId)

set the MODBUS header unit id

Parameters
unitIdunit id

◆ toString()

std::string pcpp::ModbusLayer::toString ( ) const
overridevirtual
Returns
A string representation of the layer most important data (should look like the layer description in Wireshark)

Implements pcpp::Layer.