Skip to content

Commit 20974e6

Browse files
authored
Updated Packet++ headers to use override and Cpp type casts. (#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.
1 parent 3b57224 commit 20974e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+507
-578
lines changed

Packet++/header/ArpLayer.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ namespace pcpp
8383
ArpLayer(ArpOpcode opCode, const MacAddress& senderMacAddr, const MacAddress& targetMacAddr,
8484
const IPv4Address& senderIpAddr, const IPv4Address& targetIpAddr);
8585

86-
~ArpLayer()
87-
{}
86+
~ArpLayer() override = default;
8887

8988
/**
9089
* Get a pointer to the ARP header. Notice this points directly to the data, so every change will change the
@@ -93,7 +92,7 @@ namespace pcpp
9392
*/
9493
inline arphdr* getArpHeader() const
9594
{
96-
return (arphdr*)m_Data;
95+
return reinterpret_cast<arphdr*>(m_Data);
9796
}
9897

9998
/**
@@ -137,13 +136,13 @@ namespace pcpp
137136
/**
138137
* Does nothing for this layer (ArpLayer is always last)
139138
*/
140-
void parseNextLayer()
139+
void parseNextLayer() override
141140
{}
142141

143142
/**
144143
* @return The size of @ref arphdr
145144
*/
146-
size_t getHeaderLen() const
145+
size_t getHeaderLen() const override
147146
{
148147
return sizeof(arphdr);
149148
}
@@ -156,7 +155,7 @@ namespace pcpp
156155
* - @ref arphdr#protocolSize = 4 (assume IPv4 over ARP)
157156
* - if it's an ARP request: @ref arphdr#targetMacAddr = MacAddress("00:00:00:00:00:00")
158157
*/
159-
void computeCalculateFields();
158+
void computeCalculateFields() override;
160159

161160
/**
162161
* Is this packet an ARP request?
@@ -168,9 +167,9 @@ namespace pcpp
168167
*/
169168
bool isReply() const;
170169

171-
std::string toString() const;
170+
std::string toString() const override;
172171

173-
OsiModelLayer getOsiModelLayer() const
172+
OsiModelLayer getOsiModelLayer() const override
174173
{
175174
return OsiModelNetworkLayer;
176175
}

Packet++/header/BgpLayer.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ namespace pcpp
9999
/**
100100
* @return The size of the BGP message
101101
*/
102-
size_t getHeaderLen() const;
102+
size_t getHeaderLen() const override;
103103

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

110-
std::string toString() const;
110+
std::string toString() const override;
111111

112-
OsiModelLayer getOsiModelLayer() const
112+
OsiModelLayer getOsiModelLayer() const override
113113
{
114114
return OsiModelApplicationLayer;
115115
}
@@ -120,7 +120,7 @@ namespace pcpp
120120
* - Set message type value
121121
* - Set message length
122122
*/
123-
void computeCalculateFields();
123+
void computeCalculateFields() override;
124124

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

133133
bgp_common_header* getBasicHeader() const
134134
{
135-
return (bgp_common_header*)m_Data;
135+
return reinterpret_cast<bgp_common_header*>(m_Data);
136136
}
137137

138138
void setBgpFields(size_t messageLen = 0);
@@ -222,7 +222,7 @@ namespace pcpp
222222
*/
223223
bgp_open_message* getOpenMsgHeader() const
224224
{
225-
return (bgp_open_message*)m_Data;
225+
return reinterpret_cast<bgp_open_message*>(m_Data);
226226
}
227227

228228
/**
@@ -272,7 +272,7 @@ namespace pcpp
272272

273273
// implement abstract methods
274274

275-
BgpMessageType getBgpMessageType() const
275+
BgpMessageType getBgpMessageType() const override
276276
{
277277
return BgpLayer::Open;
278278
}
@@ -388,7 +388,7 @@ namespace pcpp
388388
*/
389389
bgp_common_header* getBasicMsgHeader() const
390390
{
391-
return (bgp_common_header*)m_Data;
391+
return reinterpret_cast<bgp_common_header*>(m_Data);
392392
}
393393

394394
/**
@@ -480,7 +480,7 @@ namespace pcpp
480480

481481
// implement abstract methods
482482

483-
BgpMessageType getBgpMessageType() const
483+
BgpMessageType getBgpMessageType() const override
484484
{
485485
return BgpLayer::Update;
486486
}
@@ -561,7 +561,7 @@ namespace pcpp
561561
*/
562562
bgp_notification_message* getNotificationMsgHeader() const
563563
{
564-
return (bgp_notification_message*)m_Data;
564+
return reinterpret_cast<bgp_notification_message*>(m_Data);
565565
}
566566

567567
/**
@@ -606,7 +606,7 @@ namespace pcpp
606606

607607
// implement abstract methods
608608

609-
BgpMessageType getBgpMessageType() const
609+
BgpMessageType getBgpMessageType() const override
610610
{
611611
return BgpLayer::Notification;
612612
}
@@ -652,12 +652,12 @@ namespace pcpp
652652
*/
653653
bgp_keepalive_message* getKeepaliveHeader() const
654654
{
655-
return (bgp_keepalive_message*)getBasicHeader();
655+
return reinterpret_cast<bgp_keepalive_message*>(getBasicHeader());
656656
}
657657

658658
// implement abstract methods
659659

660-
BgpMessageType getBgpMessageType() const
660+
BgpMessageType getBgpMessageType() const override
661661
{
662662
return BgpLayer::Keepalive;
663663
}
@@ -711,12 +711,12 @@ namespace pcpp
711711
*/
712712
bgp_route_refresh_message* getRouteRefreshHeader() const
713713
{
714-
return (bgp_route_refresh_message*)getBasicHeader();
714+
return reinterpret_cast<bgp_route_refresh_message*>(getBasicHeader());
715715
}
716716

717717
// implement abstract methods
718718

719-
BgpMessageType getBgpMessageType() const
719+
BgpMessageType getBgpMessageType() const override
720720
{
721721
return BgpLayer::RouteRefresh;
722722
}

Packet++/header/CotpLayer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ namespace pcpp
4646
*/
4747
explicit CotpLayer(uint8_t tpduNumber);
4848

49-
virtual ~CotpLayer()
50-
{}
49+
~CotpLayer() override = default;
5150

5251
/**
5352
* @return COTP length
@@ -119,7 +118,7 @@ namespace pcpp
119118
private:
120119
cotphdr* getCotpHeader() const
121120
{
122-
return (cotphdr*)m_Data;
121+
return reinterpret_cast<cotphdr*>(m_Data);
123122
}
124123
};
125124

Packet++/header/DhcpLayer.h

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ namespace pcpp
408408
/**
409409
* A d'tor for this class, currently does nothing
410410
*/
411-
virtual ~DhcpOption()
412-
{}
411+
~DhcpOption() override = default;
413412

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

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

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

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

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

485-
if (data->recordType == (uint8_t)DHCPOPT_END || data->recordType == (uint8_t)DHCPOPT_PAD)
485+
if (data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
486+
data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
486487
return true;
487488

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

491492
// implement abstract methods
492493

493-
size_t getTotalSize() const
494+
size_t getTotalSize() const override
494495
{
495496
if (m_Data == nullptr)
496497
return 0;
497498

498-
if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
499+
if (m_Data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
500+
m_Data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
499501
return sizeof(uint8_t);
500502

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

504-
size_t getDataSize() const
506+
size_t getDataSize() const override
505507
{
506508
if (m_Data == nullptr)
507509
return 0;
508510

509-
if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
511+
if (m_Data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
512+
m_Data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
510513
return 0;
511514

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

536539
/**
@@ -540,7 +543,7 @@ namespace pcpp
540543
* @param[in] optionValue A 1-byte option value
541544
*/
542545
DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue)
543-
: TLVRecordBuilder((uint8_t)optionType, optionValue)
546+
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
544547
{}
545548

546549
/**
@@ -550,7 +553,7 @@ namespace pcpp
550553
* @param[in] optionValue A 2-byte option value
551554
*/
552555
DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue)
553-
: TLVRecordBuilder((uint8_t)optionType, optionValue)
556+
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
554557
{}
555558

556559
/**
@@ -560,7 +563,7 @@ namespace pcpp
560563
* @param[in] optionValue A 4-byte option value
561564
*/
562565
DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue)
563-
: TLVRecordBuilder((uint8_t)optionType, optionValue)
566+
: TLVRecordBuilder(static_cast<uint8_t>(optionType), optionValue)
564567
{}
565568

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

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

586589
/**
@@ -640,8 +643,7 @@ namespace pcpp
640643
/**
641644
* A destructor for this layer
642645
*/
643-
virtual ~DhcpLayer()
644-
{}
646+
~DhcpLayer() override = default;
645647

646648
/**
647649
* Get a pointer to the DHCP header. Notice this points directly to the data, so every change will change the
@@ -650,15 +652,15 @@ namespace pcpp
650652
*/
651653
dhcp_header* getDhcpHeader() const
652654
{
653-
return (dhcp_header*)m_Data;
655+
return reinterpret_cast<dhcp_header*>(m_Data);
654656
}
655657

656658
/**
657659
* @return The BootP opcode of this message
658660
*/
659661
BootpOpCodes getOpCode() const
660662
{
661-
return (BootpOpCodes)getDhcpHeader()->opCode;
663+
return static_cast<BootpOpCodes>(getDhcpHeader()->opCode);
662664
}
663665

664666
/**
@@ -834,13 +836,13 @@ namespace pcpp
834836
/**
835837
* Does nothing for this layer (DhcpLayer is always last)
836838
*/
837-
void parseNextLayer()
839+
void parseNextLayer() override
838840
{}
839841

840842
/**
841843
* @return The size of @ref dhcp_header + size of options
842844
*/
843-
size_t getHeaderLen() const
845+
size_t getHeaderLen() const override
844846
{
845847
return m_DataLen;
846848
}
@@ -855,11 +857,11 @@ namespace pcpp
855857
* - @ref dhcp_header#hardwareType = 1 (Ethernet)
856858
* - @ref dhcp_header#hardwareAddressLength = 6 (MAC address length)
857859
*/
858-
void computeCalculateFields();
860+
void computeCalculateFields() override;
859861

860-
std::string toString() const;
862+
std::string toString() const override;
861863

862-
OsiModelLayer getOsiModelLayer() const
864+
OsiModelLayer getOsiModelLayer() const override
863865
{
864866
return OsiModelApplicationLayer;
865867
}

0 commit comments

Comments
 (0)