Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:
BUILD_DIR: Dist
GCOVR_FLAGS: --gcov-ignore-errors all --gcov-ignore-parse-errors all --merge-mode-functions=separate --exclude-throw-branches --filter Common --filter Pcap --filter Packet --xml
CCACHE_DIR: ${{ github.workspace }}/.ccache
CPPCHECK_VERSION: 2.9
CPPCHECK_VERSION: 2.19.0

permissions:
contents: read
Expand Down
8 changes: 7 additions & 1 deletion Common++/header/PointerVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ namespace pcpp

// Release is called after the raw pointer is already inserted into the vector to prevent
// a memory leak if push_back throws.
// cppcheck-suppress danglingLifetime

// cppcheck-suppress-begin [danglingLifetime, ignoredReturnValue]
m_Vector.push_back(element.get());
element.release();
// cppcheck-suppress-end [danglingLifetime, ignoredReturnValue]
}

// cppcheck-suppress-begin danglingLifetime

/// Get the first element of the vector
/// @return An iterator object pointing to the first element of the vector
VectorIterator begin()
Expand Down Expand Up @@ -210,6 +214,8 @@ namespace pcpp
return m_Vector.end();
}

// cppcheck-suppress-end danglingLifetime

/// Get number of elements in the vector
/// @return The number of elements in the vector
size_t size() const
Expand Down
10 changes: 5 additions & 5 deletions Examples/DpdkExample-FilterTraffic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ void listDpdkPorts()
* Prepare the configuration for each core. Configuration includes: which DpdkDevices and which RX queues to receive
* packets from, where to send the matched packets, etc.
*/
void prepareCoreConfiguration(std::vector<pcpp::DpdkDevice*>& dpdkDevicesToUse,
std::vector<pcpp::SystemCore>& coresToUse, bool writePacketsToDisk,
void prepareCoreConfiguration(std::vector<pcpp::DpdkDevice*> const& dpdkDevicesToUse,
std::vector<pcpp::SystemCore> const& coresToUse, bool writePacketsToDisk,
const std::string& packetFilePath, pcpp::DpdkDevice* sendPacketsTo,
std::vector<AppWorkerConfig>& workerConfigArr, int workerConfigArrLen, uint16_t rxQueues)
{
Expand Down Expand Up @@ -269,7 +269,7 @@ void printStats(const PacketStats& threadStats, const std::string& columnName)
*/
void onApplicationInterrupted(void* cookie)
{
FilterTrafficArgs* args = (FilterTrafficArgs*)cookie;
FilterTrafficArgs* args = static_cast<FilterTrafficArgs*>(cookie);

std::cout << std::endl << std::endl << "Application stopped" << std::endl;

Expand All @@ -281,15 +281,15 @@ void onApplicationInterrupted(void* cookie)
std::vector<PacketStats> threadStatsVec;
for (const auto& iter : *(args->workerThreadsVector))
{
AppWorkerThread* thread = (AppWorkerThread*)(iter);
AppWorkerThread* thread = static_cast<AppWorkerThread*>(iter);
PacketStats threadStats = thread->getStats();
aggregatedStats.collectStats(threadStats);
threadStatsVec.push_back(threadStats);
delete thread;
}

// print stats for every worker threads
for (auto threadStats : threadStatsVec)
for (auto const& threadStats : threadStatsVec)
{
// no need to print table if no packets were received
if (threadStats.packetCount == 0)
Expand Down
4 changes: 2 additions & 2 deletions Examples/IPDefragUtil/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ int main(int argc, char* argv[])
// create a writer device for output file in the same file type as input file
pcpp::IFileWriterDevice* writer = nullptr;

if (dynamic_cast<pcpp::PcapFileReaderDevice*>(reader) != nullptr)
if (auto pcapReader = dynamic_cast<pcpp::PcapFileReaderDevice*>(reader))
{
writer = new pcpp::PcapFileWriterDevice(outputFile, ((pcpp::PcapFileReaderDevice*)reader)->getLinkLayerType());
writer = new pcpp::PcapFileWriterDevice(outputFile, pcapReader->getLinkLayerType());
}
else if (dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader) != nullptr)
{
Expand Down
12 changes: 8 additions & 4 deletions Examples/IPFragUtil/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,13 @@ void splitIPPacketToFragmentsBySize(pcpp::RawPacket* rawPacket, size_t fragmentS

// set fragment parameters in IPv4/6 layer
if (ipProto == pcpp::IPv4)
setIPv4FragmentParams((pcpp::IPv4Layer*)fragIpLayer, curOffset, lastFrag);
{
setIPv4FragmentParams(static_cast<pcpp::IPv4Layer*>(fragIpLayer), curOffset, lastFrag);
}
else // ipProto == IPv6
setIPv6FragmentParams((pcpp::IPv6Layer*)fragIpLayer, curOffset, lastFrag, randomNum);
{
setIPv6FragmentParams(static_cast<pcpp::IPv6Layer*>(fragIpLayer), curOffset, lastFrag, randomNum);
}

// compute all calculated fields of the new fragment
newFrag.computeCalculateFields();
Expand Down Expand Up @@ -508,9 +512,9 @@ int main(int argc, char* argv[])
// create a writer device for output file in the same file type as input file
pcpp::IFileWriterDevice* writer = nullptr;

if (dynamic_cast<pcpp::PcapFileReaderDevice*>(reader) != nullptr)
if (auto pcapReader = dynamic_cast<pcpp::PcapFileReaderDevice*>(reader))
{
writer = new pcpp::PcapFileWriterDevice(outputFile, ((pcpp::PcapFileReaderDevice*)reader)->getLinkLayerType());
writer = new pcpp::PcapFileWriterDevice(outputFile, pcapReader->getLinkLayerType());
}
else if (dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader) != nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion Packet++/src/IPv4Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ namespace pcpp
}
}

ScalarBuffer<uint16_t> scalar = { (uint16_t*)ipHdr, (size_t)(ipHdr->internetHeaderLength * 4) };
ScalarBuffer<uint16_t> scalar = { reinterpret_cast<uint16_t*>(ipHdr), static_cast<size_t>(ipHdr->internetHeaderLength * 4) };
ipHdr->headerChecksum = htobe16(computeChecksum(&scalar, 1));
}

Expand Down
6 changes: 4 additions & 2 deletions Packet++/src/IcmpV6Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ namespace pcpp

if (m_PrevLayer != nullptr)
{
auto prevLayerAsIPv6 = static_cast<IPv6Layer*>(m_PrevLayer);

ScalarBuffer<uint16_t> vec[2];

vec[0].buffer = (uint16_t*)m_Data;
Expand All @@ -95,8 +97,8 @@ namespace pcpp
const unsigned int bigEndianNextHeader = htobe32(PACKETPP_IPPROTO_ICMPV6);

uint16_t pseudoHeader[pseudoHeaderLen / 2];
((IPv6Layer*)m_PrevLayer)->getSrcIPv6Address().copyTo((uint8_t*)pseudoHeader);
((IPv6Layer*)m_PrevLayer)->getDstIPv6Address().copyTo((uint8_t*)(pseudoHeader + 8));
prevLayerAsIPv6->getSrcIPv6Address().copyTo(reinterpret_cast<uint8_t*>(pseudoHeader));
prevLayerAsIPv6->getDstIPv6Address().copyTo(reinterpret_cast<uint8_t*>(pseudoHeader + 8));
memcpy(&pseudoHeader[16], &bigEndianLen, sizeof(uint32_t));
memcpy(&pseudoHeader[18], &bigEndianNextHeader, sizeof(uint32_t));
vec[1].buffer = pseudoHeader;
Expand Down
4 changes: 2 additions & 2 deletions Packet++/src/IgmpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ namespace pcpp
return nullptr;

uint8_t* curGroupPtr = m_Data + sizeof(igmpv3_report_header);
return (igmpv3_group_record*)curGroupPtr;
return reinterpret_cast<igmpv3_group_record*>(curGroupPtr);
}

igmpv3_group_record* IgmpV3ReportLayer::getNextGroupRecord(igmpv3_group_record* groupRecord) const
Expand Down Expand Up @@ -386,7 +386,7 @@ namespace pcpp

uint8_t* groupRecordBuffer = new uint8_t[groupRecordSize];
memset(groupRecordBuffer, 0, groupRecordSize);
igmpv3_group_record* newGroupRecord = (igmpv3_group_record*)groupRecordBuffer;
igmpv3_group_record* newGroupRecord = reinterpret_cast<igmpv3_group_record*>(groupRecordBuffer);
newGroupRecord->multicastAddress = multicastAddress.toInt();
newGroupRecord->recordType = recordType;
newGroupRecord->auxDataLen = 0;
Expand Down
8 changes: 4 additions & 4 deletions Packet++/src/SSLHandshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ namespace pcpp
uint8_t* dataPtr = getData() + sizeof(uint16_t);
for (int i = 0; i < listLength / 2; i++)
{
result.push_back(be16toh(*(uint16_t*)dataPtr));
result.push_back(be16toh(*reinterpret_cast<uint16_t*>(dataPtr)));
dataPtr += sizeof(uint16_t);
}

Expand Down Expand Up @@ -1532,7 +1532,7 @@ namespace pcpp
return 0;

uint8_t* extensionLengthPos = m_Data + extensionLengthOffset;
return be16toh(*(uint16_t*)extensionLengthPos);
return be16toh(*reinterpret_cast<uint16_t*>(extensionLengthPos));
}

SSLExtension* SSLClientHelloMessage::getExtension(int index) const
Expand Down Expand Up @@ -1954,7 +1954,7 @@ namespace pcpp
// read certificates length
// TODO: certificates length is 3B. Currently assuming the MSB is 0 and reading only 2 LSBs
uint8_t* curPos = data + sizeof(ssl_tls_handshake_layer) + sizeof(uint8_t);
uint16_t certificatesLength = be16toh(*(uint16_t*)(curPos));
uint16_t certificatesLength = be16toh(*reinterpret_cast<uint16_t*>(curPos));
if (certificatesLength == 0)
return;

Expand All @@ -1970,7 +1970,7 @@ namespace pcpp

// read certificate length
curPos += sizeof(uint8_t);
uint16_t certificateLength = be16toh(*(uint16_t*)(curPos));
uint16_t certificateLength = be16toh(*reinterpret_cast<uint16_t*>(curPos));

// advance to start position of certificate
curPos += sizeof(uint16_t);
Expand Down
9 changes: 5 additions & 4 deletions Packet++/src/UdpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ namespace pcpp
IPv4Address srcIP = static_cast<IPv4Layer*>(m_PrevLayer)->getSrcIPv4Address();
IPv4Address dstIP = static_cast<IPv4Layer*>(m_PrevLayer)->getDstIPv4Address();

checksumRes = pcpp::computePseudoHdrChecksum((uint8_t*)udpHdr, getDataLen(), IPAddress::IPv4AddressType,
PACKETPP_IPPROTO_UDP, srcIP, dstIP);
checksumRes =
pcpp::computePseudoHdrChecksum(reinterpret_cast<uint8_t*>(udpHdr), getDataLen(),
IPAddress::IPv4AddressType, PACKETPP_IPPROTO_UDP, srcIP, dstIP);

PCPP_LOG_DEBUG("calculated IPv4 UDP checksum = 0x" << std::uppercase << std::hex << checksumRes);
}
Expand All @@ -72,8 +73,8 @@ namespace pcpp
IPv6Address srcIP = static_cast<IPv6Layer*>(m_PrevLayer)->getSrcIPv6Address();
IPv6Address dstIP = static_cast<IPv6Layer*>(m_PrevLayer)->getDstIPv6Address();

checksumRes = computePseudoHdrChecksum((uint8_t*)udpHdr, getDataLen(), IPAddress::IPv6AddressType,
PACKETPP_IPPROTO_UDP, srcIP, dstIP);
checksumRes = computePseudoHdrChecksum(reinterpret_cast<uint8_t*>(udpHdr), getDataLen(),
IPAddress::IPv6AddressType, PACKETPP_IPPROTO_UDP, srcIP, dstIP);

PCPP_LOG_DEBUG("calculated IPv6 UDP checksum = 0xX" << std::uppercase << std::hex << checksumRes);
}
Expand Down
12 changes: 6 additions & 6 deletions Packet++/src/VrrpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,26 +404,26 @@ namespace pcpp
uint8_t VrrpV2Layer::getAdvInt() const
{
uint16_t authAdvInt = getVrrpHeader()->authTypeAdvInt;
auto authAdvIntPtr = (vrrpv2_auth_adv*)(&authAdvInt);
auto authAdvIntPtr = reinterpret_cast<vrrpv2_auth_adv*>(&authAdvInt);
return authAdvIntPtr->advInt;
}

void VrrpV2Layer::setAdvInt(uint8_t advInt)
{
auto authAdvIntPtr = (vrrpv2_auth_adv*)&getVrrpHeader()->authTypeAdvInt;
auto authAdvIntPtr = reinterpret_cast<vrrpv2_auth_adv*>(&getVrrpHeader()->authTypeAdvInt);
authAdvIntPtr->advInt = advInt;
}

uint8_t VrrpV2Layer::getAuthType() const
{
uint16_t authAdvInt = getVrrpHeader()->authTypeAdvInt;
auto* authAdvIntPtr = (vrrpv2_auth_adv*)(&authAdvInt);
auto* authAdvIntPtr = reinterpret_cast<vrrpv2_auth_adv*>(&authAdvInt);
return authAdvIntPtr->authType;
}

void VrrpV2Layer::setAuthType(uint8_t authType)
{
auto authAdvIntPtr = (vrrpv2_auth_adv*)&getVrrpHeader()->authTypeAdvInt;
auto authAdvIntPtr = reinterpret_cast<vrrpv2_auth_adv*>(&getVrrpHeader()->authTypeAdvInt);
authAdvIntPtr->authType = authType;
}

Expand Down Expand Up @@ -460,7 +460,7 @@ namespace pcpp
uint16_t VrrpV3Layer::getMaxAdvInt() const
{
uint16_t authAdvInt = getVrrpHeader()->authTypeAdvInt;
auto rsvdAdv = (vrrpv3_rsvd_adv*)(&authAdvInt);
auto rsvdAdv = reinterpret_cast<vrrpv3_rsvd_adv*>(&authAdvInt);
return be16toh(rsvdAdv->maxAdvInt);
}

Expand All @@ -470,7 +470,7 @@ namespace pcpp
{
throw std::invalid_argument("maxAdvInt must not exceed 12 bits length");
}
auto rsvdAdv = (vrrpv3_rsvd_adv*)&getVrrpHeader()->authTypeAdvInt;
auto rsvdAdv = reinterpret_cast<vrrpv3_rsvd_adv*>(&getVrrpHeader()->authTypeAdvInt);
rsvdAdv->maxAdvInt = htobe16(maxAdvInt);
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Packet++Test/Tests/SllNullLoopbackTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ PTF_TEST_CASE(NullLoopbackTest)
nextLayer = nullLoopbackLayer->getNextLayer();
PTF_ASSERT_NOT_NULL(nextLayer);
PTF_ASSERT_EQUAL(nextLayer->getProtocol(), pcpp::IPv4, enum);
PTF_ASSERT_EQUAL(((pcpp::IPv4Layer*)nextLayer)->getSrcIPAddress(), pcpp::IPv4Address("172.16.1.117"));
PTF_ASSERT_EQUAL(static_cast<pcpp::IPv4Layer*>(nextLayer)->getSrcIPAddress(), pcpp::IPv4Address("172.16.1.117"));
PTF_ASSERT_EQUAL(nullLoopbackLayer->getFamily(), PCPP_BSD_AF_INET);

PTF_ASSERT_TRUE(nullPacket3.isPacketOfType(pcpp::NULL_LOOPBACK));
Expand Down
41 changes: 35 additions & 6 deletions Tests/Pcap++Test/Common/TestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,42 @@ class DeviceTeardown
: m_Device(device), m_CancelTeardown(false), m_DeleteDevice(deleteDevice)
{}

DeviceTeardown(DeviceTeardown const&) = delete;
DeviceTeardown(DeviceTeardown&& other) noexcept
: m_Device(other.m_Device), m_CancelTeardown(other.m_CancelTeardown), m_DeleteDevice(other.m_DeleteDevice)
{
other.m_Device = nullptr;
}
DeviceTeardown& operator=(DeviceTeardown const&) = delete;
DeviceTeardown& operator=(DeviceTeardown&& other) noexcept
{
if (this != &other)
{
doTeardown();
m_Device = other.m_Device;
m_CancelTeardown = other.m_CancelTeardown;
m_DeleteDevice = other.m_DeleteDevice;
other.m_Device = nullptr;
}
return *this;
}

~DeviceTeardown()
{
doTeardown();
}

void cancelTeardown()
{
m_CancelTeardown = true;
}

private:
void doTeardown() noexcept
{
if (m_Device == nullptr)
return;

if (!m_CancelTeardown && m_Device != nullptr && m_Device->isOpened())
{
m_Device->close();
Expand All @@ -31,11 +65,6 @@ class DeviceTeardown
delete m_Device;
}
}

void cancelTeardown()
{
m_CancelTeardown = true;
}
};

class SuppressLogs
Expand Down Expand Up @@ -115,7 +144,7 @@ class TempFile
return *this;
}

std::string getFileName() const
std::string const& getFileName() const
{
return m_Filename;
}
Expand Down
1 change: 1 addition & 0 deletions cppcheckSuppressions.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*:3rdParty/*

checkersReport:*
normalCheckLevelMaxBranches:*
ConfigurationNotChecked:*
ctuOneDefinitionRuleViolation:Examples/*
missingInclude:*
Expand Down
Loading