Skip to content

Commit

Permalink
Updated Packet++ headers to use override and Cpp type casts. (selad…
Browse files Browse the repository at this point in the history
…b#1563)

* Added 'override' specifier to getOsiModelLayer implementations.

* Added `override` specifier to getDataPtr implementation in Layer.

* APRLayer - overrides + Cpp casts.

* BgpLayer - overrides + Cpp casts.

* CotpLayer - override + Cpp casts.

* DhcpLayer - Overrides + Cpp casts.

* DhcpV6Layer - Overrides + Cpp casts.

* DnsLayer - Overrides + Cpp casts

* EthDot3Layer - Overrides + Cpp casts.

* EthLayer - Overrides + Cpp casts.

* FtpLayer - Overrides

* GreLayer - Overrides + Cpp casts.

* GtpLayer - Overrides + Cpp casts.

* HttpLayer - Overrides

* IcmpLayer - Overrides + Cpp casts

* IcmpV6Layer - Overrides + Cpp casts.

* IgmpLayer - Overrides + Cpp casts.

* IPLayer - Explicitly defaulted ctor/dtor.

* IPv4Layer - Overrides + Cpp casts.

* IPv6Layer - Overrides + Cpp casts.

* Layer.h

- IDataContainer - defaulted destructor.
- Layer - destructor override.

* LdapLayer - Overrides.

* LLCLayer - Overrides + Cpp casts.

* MplsLayer - Overrides + Cpp casts.

* NdpLayer - Overrides + Cpp casts.

* NflogLayer - Overrides + Cpp casts.

* NtpLayer - Overrides.

* NullLoopbackLayer - Overrides.

* PayloadLayer - Overrides.

* PPPoELayer - Overrides + Cpp casts.

* RadiusLayer - Overrides + Cpp casts.

* SdpLayer - Overrides.

* SipLayer - Overrides.

* SllLayer - Overrides + Cpp casts.

* Sll2Layer - Overrides + Cpp casts.

* SmtpLayer - Overrides.

* SomeIpLayer - Overrides + Cpp casts.

* SomeIpSdLayer - Overrides.

* SshLayer - Overrides + Cpp casts.

* SSLLayer - Overrides + Cpp casts.

* StpLayer - Overrides + Cpp casts.

* S7CommLayer - Overrides + Cpp casts.

* TcpLayer - Overrides + Cpp casts.

* TelnetLayer - Overrides.

* TextBasedProtocol - Overrides.

* TLVData - Explicit defaults + Cpp casts.

* TpktLayer - Explicit defaults + Cpp casts.

* UdpLayer - Overrides + Cpp casts.

* VlanLayer - Overrides + Cpp casts.

* VrrpLayer - Overrides.

* VxlanLayer - Overrides + Cpp casts.

* WakeOnLanLayer - Overrides + Cpp casts.

* DnsResourceData - Overrides.

* DnsResource - Overrides.

* IPv4Layer - Fixed missed overrides.

* TLVData - Fixed missed c cast.

* DhcpLayer - Fixed missed overrides.

* DhcpV6Layer - Fixed missed overrides.

* HttpLayer - Fixed missed overrides.

* IPSecLayer - Overrides + Cpp casts

* NdpLayer - Fixed missed overrides.

* PacketTrailerLayer - Overrides

* SomeIpLayer - Fixed missed overrides.

* TcpLayer - Fixed missed overrides.

* SSLHandshake - Overrides + Cpp casts.

* Removed 'missingOverride' cppcheck suppression from Packet++ and Common++.

* DhcpV6Layer - Fixed missing override

* IPSecLayer - Fixed missing override

* IPv6Extensions - Fixed missing override

* IPReassembly - Overrides

* Simplify return literals.
  • Loading branch information
Dimi1010 authored Sep 24, 2024
1 parent 3b57224 commit 20974e6
Show file tree
Hide file tree
Showing 58 changed files with 507 additions and 578 deletions.
15 changes: 7 additions & 8 deletions Packet++/header/ArpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ namespace pcpp
ArpLayer(ArpOpcode opCode, const MacAddress& senderMacAddr, const MacAddress& targetMacAddr,
const IPv4Address& senderIpAddr, const IPv4Address& targetIpAddr);

~ArpLayer()
{}
~ArpLayer() override = default;

/**
* Get a pointer to the ARP header. Notice this points directly to the data, so every change will change the
Expand All @@ -93,7 +92,7 @@ namespace pcpp
*/
inline arphdr* getArpHeader() const
{
return (arphdr*)m_Data;
return reinterpret_cast<arphdr*>(m_Data);
}

/**
Expand Down Expand Up @@ -137,13 +136,13 @@ namespace pcpp
/**
* Does nothing for this layer (ArpLayer is always last)
*/
void parseNextLayer()
void parseNextLayer() override
{}

/**
* @return The size of @ref arphdr
*/
size_t getHeaderLen() const
size_t getHeaderLen() const override
{
return sizeof(arphdr);
}
Expand All @@ -156,7 +155,7 @@ namespace pcpp
* - @ref arphdr#protocolSize = 4 (assume IPv4 over ARP)
* - if it's an ARP request: @ref arphdr#targetMacAddr = MacAddress("00:00:00:00:00:00")
*/
void computeCalculateFields();
void computeCalculateFields() override;

/**
* Is this packet an ARP request?
Expand All @@ -168,9 +167,9 @@ namespace pcpp
*/
bool isReply() const;

std::string toString() const;
std::string toString() const override;

OsiModelLayer getOsiModelLayer() const
OsiModelLayer getOsiModelLayer() const override
{
return OsiModelNetworkLayer;
}
Expand Down
32 changes: 16 additions & 16 deletions Packet++/header/BgpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ namespace pcpp
/**
* @return The size of the BGP message
*/
size_t getHeaderLen() const;
size_t getHeaderLen() const override;

/**
* Multiple BGP messages can reside in a single packet, and the only layer that can come after a BGP message
* is another BGP message. This method checks for remaining data and parses it as another BGP layer
*/
void parseNextLayer();
void parseNextLayer() override;

std::string toString() const;
std::string toString() const override;

OsiModelLayer getOsiModelLayer() const
OsiModelLayer getOsiModelLayer() const override
{
return OsiModelApplicationLayer;
}
Expand All @@ -120,7 +120,7 @@ namespace pcpp
* - Set message type value
* - Set message length
*/
void computeCalculateFields();
void computeCalculateFields() override;

protected:
// protected c'tors, this class cannot be instantiated by users
Expand All @@ -132,7 +132,7 @@ namespace pcpp

bgp_common_header* getBasicHeader() const
{
return (bgp_common_header*)m_Data;
return reinterpret_cast<bgp_common_header*>(m_Data);
}

void setBgpFields(size_t messageLen = 0);
Expand Down Expand Up @@ -222,7 +222,7 @@ namespace pcpp
*/
bgp_open_message* getOpenMsgHeader() const
{
return (bgp_open_message*)m_Data;
return reinterpret_cast<bgp_open_message*>(m_Data);
}

/**
Expand Down Expand Up @@ -272,7 +272,7 @@ namespace pcpp

// implement abstract methods

BgpMessageType getBgpMessageType() const
BgpMessageType getBgpMessageType() const override
{
return BgpLayer::Open;
}
Expand Down Expand Up @@ -388,7 +388,7 @@ namespace pcpp
*/
bgp_common_header* getBasicMsgHeader() const
{
return (bgp_common_header*)m_Data;
return reinterpret_cast<bgp_common_header*>(m_Data);
}

/**
Expand Down Expand Up @@ -480,7 +480,7 @@ namespace pcpp

// implement abstract methods

BgpMessageType getBgpMessageType() const
BgpMessageType getBgpMessageType() const override
{
return BgpLayer::Update;
}
Expand Down Expand Up @@ -561,7 +561,7 @@ namespace pcpp
*/
bgp_notification_message* getNotificationMsgHeader() const
{
return (bgp_notification_message*)m_Data;
return reinterpret_cast<bgp_notification_message*>(m_Data);
}

/**
Expand Down Expand Up @@ -606,7 +606,7 @@ namespace pcpp

// implement abstract methods

BgpMessageType getBgpMessageType() const
BgpMessageType getBgpMessageType() const override
{
return BgpLayer::Notification;
}
Expand Down Expand Up @@ -652,12 +652,12 @@ namespace pcpp
*/
bgp_keepalive_message* getKeepaliveHeader() const
{
return (bgp_keepalive_message*)getBasicHeader();
return reinterpret_cast<bgp_keepalive_message*>(getBasicHeader());
}

// implement abstract methods

BgpMessageType getBgpMessageType() const
BgpMessageType getBgpMessageType() const override
{
return BgpLayer::Keepalive;
}
Expand Down Expand Up @@ -711,12 +711,12 @@ namespace pcpp
*/
bgp_route_refresh_message* getRouteRefreshHeader() const
{
return (bgp_route_refresh_message*)getBasicHeader();
return reinterpret_cast<bgp_route_refresh_message*>(getBasicHeader());
}

// implement abstract methods

BgpMessageType getBgpMessageType() const
BgpMessageType getBgpMessageType() const override
{
return BgpLayer::RouteRefresh;
}
Expand Down
5 changes: 2 additions & 3 deletions Packet++/header/CotpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ namespace pcpp
*/
explicit CotpLayer(uint8_t tpduNumber);

virtual ~CotpLayer()
{}
~CotpLayer() override = default;

/**
* @return COTP length
Expand Down Expand Up @@ -119,7 +118,7 @@ namespace pcpp
private:
cotphdr* getCotpHeader() const
{
return (cotphdr*)m_Data;
return reinterpret_cast<cotphdr*>(m_Data);
}
};

Expand Down
54 changes: 28 additions & 26 deletions Packet++/header/DhcpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,7 @@ namespace pcpp
/**
* A d'tor for this class, currently does nothing
*/
virtual ~DhcpOption()
{}
~DhcpOption() override = default;

/**
* Retrieve DHCP option data as IPv4 address. Relevant only if option value is indeed an IPv4 address
Expand Down Expand Up @@ -444,7 +443,8 @@ namespace pcpp
if (m_Data == nullptr || m_Data->recordLen - valueOffset < 1)
return "";

return std::string((const char*)m_Data->recordValue + valueOffset, (int)m_Data->recordLen - valueOffset);
return std::string(reinterpret_cast<const char*>(m_Data->recordValue) + valueOffset,
static_cast<int>(m_Data->recordLen) - valueOffset);
}

/**
Expand All @@ -458,7 +458,7 @@ namespace pcpp
void setValueString(const std::string& stringValue, int valueOffset = 0)
{
// calculate the maximum length of the destination buffer
size_t len = (size_t)m_Data->recordLen - (size_t)valueOffset;
size_t len = static_cast<size_t>(m_Data->recordLen) - static_cast<size_t>(valueOffset);

// use the length of input string if a buffer is large enough for whole string
if (stringValue.length() < len)
Expand All @@ -475,38 +475,41 @@ namespace pcpp
*/
static bool canAssign(const uint8_t* recordRawData, size_t tlvDataLen)
{
auto data = (TLVRawData*)recordRawData;
auto data = reinterpret_cast<TLVRawData const*>(recordRawData);
if (data == nullptr)
return false;

if (tlvDataLen < sizeof(TLVRawData::recordType))
return false;

if (data->recordType == (uint8_t)DHCPOPT_END || data->recordType == (uint8_t)DHCPOPT_PAD)
if (data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
return true;

return TLVRecord<uint8_t, uint8_t>::canAssign(recordRawData, tlvDataLen);
}

// implement abstract methods

size_t getTotalSize() const
size_t getTotalSize() const override
{
if (m_Data == nullptr)
return 0;

if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
if (m_Data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
m_Data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
return sizeof(uint8_t);

return sizeof(uint8_t) * 2 + (size_t)m_Data->recordLen;
return sizeof(uint8_t) * 2 + static_cast<size_t>(m_Data->recordLen);
}

size_t getDataSize() const
size_t getDataSize() const override
{
if (m_Data == nullptr)
return 0;

if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
if (m_Data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
m_Data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
return 0;

return m_Data->recordLen;
Expand All @@ -530,7 +533,7 @@ namespace pcpp
* @param[in] optionValueLen DHCP option value length in bytes
*/
DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen)
: TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen)
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue, optionValueLen)
{}

/**
Expand All @@ -540,7 +543,7 @@ namespace pcpp
* @param[in] optionValue A 1-byte option value
*/
DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue)
: TLVRecordBuilder((uint8_t)optionType, optionValue)
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
{}

/**
Expand All @@ -550,7 +553,7 @@ namespace pcpp
* @param[in] optionValue A 2-byte option value
*/
DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue)
: TLVRecordBuilder((uint8_t)optionType, optionValue)
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
{}

/**
Expand All @@ -560,7 +563,7 @@ namespace pcpp
* @param[in] optionValue A 4-byte option value
*/
DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue)
: TLVRecordBuilder((uint8_t)optionType, optionValue)
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
{}

/**
Expand All @@ -570,7 +573,7 @@ namespace pcpp
* @param[in] optionValue The IPv4 address option value
*/
DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address& optionValue)
: TLVRecordBuilder((uint8_t)optionType, optionValue)
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
{}

/**
Expand All @@ -580,7 +583,7 @@ namespace pcpp
* @param[in] optionValue The string option value
*/
DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string& optionValue)
: TLVRecordBuilder((uint8_t)optionType, optionValue)
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
{}

/**
Expand Down Expand Up @@ -640,8 +643,7 @@ namespace pcpp
/**
* A destructor for this layer
*/
virtual ~DhcpLayer()
{}
~DhcpLayer() override = default;

/**
* Get a pointer to the DHCP header. Notice this points directly to the data, so every change will change the
Expand All @@ -650,15 +652,15 @@ namespace pcpp
*/
dhcp_header* getDhcpHeader() const
{
return (dhcp_header*)m_Data;
return reinterpret_cast<dhcp_header*>(m_Data);
}

/**
* @return The BootP opcode of this message
*/
BootpOpCodes getOpCode() const
{
return (BootpOpCodes)getDhcpHeader()->opCode;
return static_cast<BootpOpCodes>(getDhcpHeader()->opCode);
}

/**
Expand Down Expand Up @@ -834,13 +836,13 @@ namespace pcpp
/**
* Does nothing for this layer (DhcpLayer is always last)
*/
void parseNextLayer()
void parseNextLayer() override
{}

/**
* @return The size of @ref dhcp_header + size of options
*/
size_t getHeaderLen() const
size_t getHeaderLen() const override
{
return m_DataLen;
}
Expand All @@ -855,11 +857,11 @@ namespace pcpp
* - @ref dhcp_header#hardwareType = 1 (Ethernet)
* - @ref dhcp_header#hardwareAddressLength = 6 (MAC address length)
*/
void computeCalculateFields();
void computeCalculateFields() override;

std::string toString() const;
std::string toString() const override;

OsiModelLayer getOsiModelLayer() const
OsiModelLayer getOsiModelLayer() const override
{
return OsiModelApplicationLayer;
}
Expand Down
Loading

0 comments on commit 20974e6

Please sign in to comment.