diff --git a/Common++/header/LRUList.h b/Common++/header/LRUList.h index b11f1389e7..37b2d895b3 100644 --- a/Common++/header/LRUList.h +++ b/Common++/header/LRUList.h @@ -66,7 +66,7 @@ namespace pcpp if (m_CacheItemsMap.size() > m_MaxSize) { ListIterator lruIter = m_CacheItemsList.end(); - lruIter--; + --lruIter; if (deletedValue != NULL) #if __cplusplus > 199711L || _MSC_VER >= 1800 diff --git a/Common++/header/PointerVector.h b/Common++/header/PointerVector.h index 68eed1d1f5..2ecfb203c7 100644 --- a/Common++/header/PointerVector.h +++ b/Common++/header/PointerVector.h @@ -44,9 +44,9 @@ namespace pcpp */ ~PointerVector() { - for (VectorIterator iter = m_Vector.begin(); iter != m_Vector.end(); iter++) + for (auto iter : m_Vector) { - delete (*iter); + delete iter; } } @@ -56,9 +56,9 @@ namespace pcpp */ PointerVector(const PointerVector& other) { - for (ConstVectorIterator iter = other.begin(); iter != other.end(); iter++) + for (const auto iter : other) { - T* objCopy = new T(**iter); + T* objCopy = new T(*iter); m_Vector.push_back(objCopy); } } @@ -68,9 +68,9 @@ namespace pcpp */ void clear() { - for (VectorIterator iter = m_Vector.begin(); iter != m_Vector.end(); iter++) + for (auto iter : m_Vector) { - delete (*iter); + delete iter; } m_Vector.clear(); diff --git a/Common++/header/SystemUtils.h b/Common++/header/SystemUtils.h index afbf9a40c2..e30cce8297 100644 --- a/Common++/header/SystemUtils.h +++ b/Common++/header/SystemUtils.h @@ -207,7 +207,7 @@ namespace pcpp * @param[in] cores A vector of SystemCore instances * @return A core mask representing these cores */ - CoreMask createCoreMaskFromCoreVector(std::vector cores); + CoreMask createCoreMaskFromCoreVector(const std::vector &cores); /** @@ -215,7 +215,7 @@ namespace pcpp * @param[in] coreIds A vector of core IDs * @return A core mask representing these cores */ - CoreMask createCoreMaskFromCoreIds(std::vector coreIds); + CoreMask createCoreMaskFromCoreIds(const std::vector &coreIds); /** diff --git a/Common++/src/SystemUtils.cpp b/Common++/src/SystemUtils.cpp index 664b15a7b7..0bc9fdf0d0 100644 --- a/Common++/src/SystemUtils.cpp +++ b/Common++/src/SystemUtils.cpp @@ -144,23 +144,25 @@ CoreMask getCoreMaskForAllMachineCores() return result; } -CoreMask createCoreMaskFromCoreVector(std::vector cores) +CoreMask createCoreMaskFromCoreVector(const std::vector &cores) { CoreMask result = 0; - for (std::vector::iterator iter = cores.begin(); iter != cores.end(); iter++) + for (const auto &core : cores) { - result |= iter->Mask; + // cppcheck-suppress useStlAlgorithm + result |= core.Mask; } return result; } -CoreMask createCoreMaskFromCoreIds(std::vector coreIds) +CoreMask createCoreMaskFromCoreIds(const std::vector &coreIds) { CoreMask result = 0; - for (std::vector::iterator iter = coreIds.begin(); iter != coreIds.end(); iter++) + for (const auto &coreId : coreIds) { - result |= SystemCores::IdToSystemCore[*iter].Mask; + // cppcheck-suppress useStlAlgorithm + result |= SystemCores::IdToSystemCore[coreId].Mask; } return result; diff --git a/Common++/src/TablePrinter.cpp b/Common++/src/TablePrinter.cpp index 19812b6e4c..153801def2 100644 --- a/Common++/src/TablePrinter.cpp +++ b/Common++/src/TablePrinter.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "TablePrinter.h" #include "Logger.h" @@ -88,18 +89,8 @@ void TablePrinter::printSeparator() return; } - int totalLen = 0; - for (std::vector::iterator iter = m_ColumnWidths.begin(); iter != m_ColumnWidths.end(); iter++) - { - totalLen += 2 + (*iter) + 1; - } - - totalLen++; - - for (int index = 0; index < totalLen; index++) - std::cout << "-"; - - std::cout << std::endl; + auto totalLen = std::accumulate(m_ColumnWidths.begin(), m_ColumnWidths.end(), m_ColumnWidths.size() * 3) + 1; + std::cout << std::string(totalLen, '-') << std::endl; } void TablePrinter::closeTable() diff --git a/Examples/Arping/main.cpp b/Examples/Arping/main.cpp index d4b719e897..73277544f7 100644 --- a/Examples/Arping/main.cpp +++ b/Examples/Arping/main.cpp @@ -89,9 +89,9 @@ void listInterfaces() const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); std::cout << std::endl << "Network interfaces:" << std::endl; - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + for (const auto &dev : devList) { - std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl; + std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; } exit(0); } diff --git a/Examples/DNSResolver/main.cpp b/Examples/DNSResolver/main.cpp index cf95e684cc..c5ac92ec4a 100644 --- a/Examples/DNSResolver/main.cpp +++ b/Examples/DNSResolver/main.cpp @@ -76,9 +76,9 @@ void listInterfaces() const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); std::cout << std::endl << "Network interfaces:" << std::endl; - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + for (const auto &dev : devList) { - std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl; + std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; } exit(0); } @@ -189,13 +189,11 @@ int main(int argc, char* argv[]) { const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + auto iter = std::find_if(devList.begin(), devList.end(), + [](pcpp::PcapLiveDevice *dev) { return dev->getDefaultGateway().isValid(); }); + if (iter != devList.end()) { - if ((*iter)->getDefaultGateway().isValid()) - { - dev = *iter; - break; - } + dev = *iter; } if (dev == nullptr) diff --git a/Examples/DnsSpoofing/main.cpp b/Examples/DnsSpoofing/main.cpp index 268231a57e..7443714e93 100644 --- a/Examples/DnsSpoofing/main.cpp +++ b/Examples/DnsSpoofing/main.cpp @@ -128,9 +128,9 @@ void listInterfaces() const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); std::cout << std::endl << "Network interfaces:" << std::endl; - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + for (const auto &dev : devList) { - std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl; + std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; } exit(0); } @@ -172,9 +172,9 @@ void handleDnsRequest(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* bool hostMatch = false; // go over all hosts in dnsHostsToSpoof list and see if current query matches one of them - for (std::vector::iterator iter = args->dnsHostsToSpoof.begin(); iter != args->dnsHostsToSpoof.end(); iter++) + for (const auto &host : args->dnsHostsToSpoof) { - if (dnsLayer->getQuery(*iter, false) != nullptr) + if (dnsLayer->getQuery(host, false) != nullptr) { hostMatch = true; break; @@ -288,12 +288,10 @@ void onApplicationInterrupted(void* cookie) std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); // go over all items (hosts + count) in the sorted vector and print them - for(std::vector >::iterator iter = map2vec.begin(); - iter != map2vec.end(); - iter++) + for(const auto &iter : map2vec) { std::stringstream values; - values << iter->first << "|" << iter->second; + values << iter.first << "|" << iter.second; printer.printRow(values.str(), '|'); } } diff --git a/Examples/DpdkBridge/main.cpp b/Examples/DpdkBridge/main.cpp index dae8604880..02dec1aada 100644 --- a/Examples/DpdkBridge/main.cpp +++ b/Examples/DpdkBridge/main.cpp @@ -112,9 +112,9 @@ void listDpdkPorts() // go over all available DPDK devices and print info for each one std::vector deviceList = pcpp::DpdkDeviceList::getInstance().getDpdkDeviceList(); - for (std::vector::iterator iter = deviceList.begin(); iter != deviceList.end(); iter++) + for (const auto &iter : deviceList) { - pcpp::DpdkDevice* dev = *iter; + pcpp::DpdkDevice* dev = iter; std::cout << " " << " Port #" << dev->getDeviceId() << ":" << " MAC address='" << dev->getMacAddress() << "';" @@ -311,22 +311,22 @@ int main(int argc, char* argv[]) // collect the list of DPDK devices std::vector dpdkDevicesToUse; - for (std::vector::iterator iter = dpdkPortVec.begin(); iter != dpdkPortVec.end(); iter++) + for (const auto &port : dpdkPortVec) { - pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(*iter); + pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(port); if (dev == NULL) { - EXIT_WITH_ERROR("DPDK device for port " << *iter << " doesn't exist"); + EXIT_WITH_ERROR("DPDK device for port " << port << " doesn't exist"); } dpdkDevicesToUse.push_back(dev); } // go over all devices and open them - for (std::vector::iterator iter = dpdkDevicesToUse.begin(); iter != dpdkDevicesToUse.end(); iter++) + for (const auto &dev : dpdkDevicesToUse) { - if (!(*iter)->openMultiQueues(queueQuantity, 1)) + if (!dev->openMultiQueues(queueQuantity, 1)) { - EXIT_WITH_ERROR("Couldn't open DPDK device #" << (*iter)->getDeviceId() << ", PMD '" << (*iter)->getPMDName() << "'"); + EXIT_WITH_ERROR("Couldn't open DPDK device #" << dev->getDeviceId() << ", PMD '" << dev->getPMDName() << "'"); } } diff --git a/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h b/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h index f074cf0af0..059a108b56 100644 --- a/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h +++ b/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h @@ -74,15 +74,15 @@ class AppWorkerThread : public pcpp::DpdkWorkerThread while (!m_Stop) { // go over all DPDK devices configured for this worker/core - for (InputDataConfig::iterator iter = m_WorkerConfig.InDataCfg.begin(); iter != m_WorkerConfig.InDataCfg.end(); iter++) + for (const auto &iter : m_WorkerConfig.InDataCfg) { // for each DPDK device go over all RX queues configured for this worker/core - for (std::vector::iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++) + for (const auto &iter2 : iter.second) { - pcpp::DpdkDevice* dev = iter->first; + pcpp::DpdkDevice* dev = iter.first; // receive packets from network on the specified DPDK device and RX queue - uint16_t packetsReceived = dev->receivePackets(packetArr, MAX_RECEIVE_BURST, *iter2); + uint16_t packetsReceived = dev->receivePackets(packetArr, MAX_RECEIVE_BURST, iter2); for (int i = 0; i < packetsReceived; i++) { @@ -96,7 +96,7 @@ class AppWorkerThread : public pcpp::DpdkWorkerThread // hash the packet by 5-tuple and look in the flow table to see whether this packet belongs to an existing or new flow uint32_t hash = pcpp::hash5Tuple(&parsedPacket); - std::map::const_iterator iter3 = m_FlowTable.find(hash); + auto iter3 = m_FlowTable.find(hash); // if packet belongs to an already existing flow if (iter3 != m_FlowTable.end() && iter3->second) diff --git a/Examples/DpdkExample-FilterTraffic/main.cpp b/Examples/DpdkExample-FilterTraffic/main.cpp index 132740048b..bbeacce543 100644 --- a/Examples/DpdkExample-FilterTraffic/main.cpp +++ b/Examples/DpdkExample-FilterTraffic/main.cpp @@ -136,9 +136,8 @@ void listDpdkPorts() // go over all available DPDK devices and print info for each one std::vector deviceList = pcpp::DpdkDeviceList::getInstance().getDpdkDeviceList(); - for (std::vector::iterator iter = deviceList.begin(); iter != deviceList.end(); iter++) + for (const auto &dev : deviceList) { - pcpp::DpdkDevice* dev = *iter; std::cout << " " << " Port #" << dev->getDeviceId() << ":" << " MAC address='" << dev->getMacAddress() << "';" @@ -160,11 +159,11 @@ void prepareCoreConfiguration(std::vector& dpdkDevicesToUse, // create a list of pairs of DpdkDevice and RX queues for all RX queues in all requested devices int totalNumOfRxQueues = 0; std::vector > deviceAndRxQVec; - for (std::vector::iterator iter = dpdkDevicesToUse.begin(); iter != dpdkDevicesToUse.end(); iter++) + for (const auto &iter : dpdkDevicesToUse) { for (int rxQueueIndex = 0; rxQueueIndex < rxQueues; rxQueueIndex++) { - std::pair curPair(*iter, rxQueueIndex); + std::pair curPair(iter, rxQueueIndex); deviceAndRxQVec.push_back(curPair); } totalNumOfRxQueues += rxQueues; @@ -177,10 +176,10 @@ void prepareCoreConfiguration(std::vector& dpdkDevicesToUse, // prepare the configuration for every core: divide the devices and RX queue for each device with the various cores int i = 0; std::vector >::iterator pairVecIter = deviceAndRxQVec.begin(); - for (std::vector::iterator iter = coresToUse.begin(); iter != coresToUse.end(); iter++) + for (const auto &core : coresToUse) { - std::cout << "Using core " << (int)iter->Id << std::endl; - workerConfigArr[i].CoreId = iter->Id; + std::cout << "Using core " << (int)core.Id << std::endl; + workerConfigArr[i].CoreId = core.Id; workerConfigArr[i].WriteMatchedPacketsToFile = writePacketsToDisk; std::stringstream packetFileName; @@ -193,23 +192,23 @@ void prepareCoreConfiguration(std::vector& dpdkDevicesToUse, if (pairVecIter == deviceAndRxQVec.end()) break; workerConfigArr[i].InDataCfg[pairVecIter->first].push_back(pairVecIter->second); - pairVecIter++; + ++pairVecIter; } if (rxQueuesRemainder > 0 && (pairVecIter != deviceAndRxQVec.end())) { workerConfigArr[i].InDataCfg[pairVecIter->first].push_back(pairVecIter->second); - pairVecIter++; + ++pairVecIter; rxQueuesRemainder--; } // print configuration for core std::cout << " Core configuration:" << std::endl; - for (InputDataConfig::iterator iter2 = workerConfigArr[i].InDataCfg.begin(); iter2 != workerConfigArr[i].InDataCfg.end(); iter2++) + for (const auto &iter2 : workerConfigArr[i].InDataCfg) { - std::cout << " DPDK device#" << iter2->first->getDeviceId() << ": "; - for (std::vector::iterator iter3 = iter2->second.begin(); iter3 != iter2->second.end(); iter3++) + std::cout << " DPDK device#" << iter2.first->getDeviceId() << ": "; + for (const auto &iter3 : iter2.second) { - std::cout << "RX-Queue#" << *iter3 << "; "; + std::cout << "RX-Queue#" << iter3 << "; "; } std::cout << std::endl; } @@ -250,9 +249,9 @@ void onApplicationInterrupted(void* cookie) // print final stats for every worker thread plus sum of all threads and free worker threads memory PacketStats aggregatedStats; - for (std::vector::iterator iter = args->workerThreadsVector->begin(); iter != args->workerThreadsVector->end(); iter++) + for (const auto &iter : *(args->workerThreadsVector)) { - AppWorkerThread* thread = (AppWorkerThread*)(*iter); + AppWorkerThread* thread = (AppWorkerThread*)(iter); PacketStats threadStats = thread->getStats(); aggregatedStats.collectStats(threadStats); printer.printRow(threadStats.getStatValuesAsString("|"), '|'); @@ -489,39 +488,39 @@ int main(int argc, char* argv[]) // collect the list of DPDK devices std::vector dpdkDevicesToUse; - for (std::vector::iterator iter = dpdkPortVec.begin(); iter != dpdkPortVec.end(); iter++) + for (const auto &port : dpdkPortVec) { - pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(*iter); + pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(port); if (dev == NULL) { - EXIT_WITH_ERROR("DPDK device for port " << *iter << " doesn't exist"); + EXIT_WITH_ERROR("DPDK device for port " << port << " doesn't exist"); } dpdkDevicesToUse.push_back(dev); } // go over all devices and open them - for (std::vector::iterator iter = dpdkDevicesToUse.begin(); iter != dpdkDevicesToUse.end(); iter++) + for (const auto &dev : dpdkDevicesToUse) { - if (rxQueues > (*iter)->getTotalNumOfRxQueues()) + if (rxQueues > dev->getTotalNumOfRxQueues()) { - EXIT_WITH_ERROR("Number of RX errors cannot exceed the max allowed by the device which is " << (*iter)->getTotalNumOfRxQueues()); + EXIT_WITH_ERROR("Number of RX errors cannot exceed the max allowed by the device which is " << dev->getTotalNumOfRxQueues()); } - if (txQueues > (*iter)->getTotalNumOfTxQueues()) + if (txQueues > dev->getTotalNumOfTxQueues()) { - EXIT_WITH_ERROR("Number of TX errors cannot exceed the max allowed by the device which is " << (*iter)->getTotalNumOfTxQueues()); + EXIT_WITH_ERROR("Number of TX errors cannot exceed the max allowed by the device which is " << dev->getTotalNumOfTxQueues()); } - if (!(*iter)->openMultiQueues(rxQueues, txQueues)) + if (!dev->openMultiQueues(rxQueues, txQueues)) { - EXIT_WITH_ERROR("Couldn't open DPDK device #" << (*iter)->getDeviceId() << ", PMD '" << (*iter)->getPMDName() << "'"); + EXIT_WITH_ERROR("Couldn't open DPDK device #" << dev->getDeviceId() << ", PMD '" << dev->getPMDName() << "'"); } std::cout - << "Opened device #" << (*iter)->getDeviceId() + << "Opened device #" << dev->getDeviceId() << " with " << rxQueues << " RX queues and " << txQueues << " TX queues." << " RSS hash functions:" << std::endl; - std::vector rssHashFunctions = (*iter)->rssHashFunctionMaskToString((*iter)->getConfiguredRssHashFunction()); - for(std::vector::iterator it = rssHashFunctions.begin(); it != rssHashFunctions.end(); ++it) + std::vector rssHashFunctions = dev->rssHashFunctionMaskToString(dev->getConfiguredRssHashFunction()); + for(const auto &hashFunc : rssHashFunctions) { - std::cout << " " << (*it) << std::endl; + std::cout << " " << hashFunc << std::endl; } } @@ -541,7 +540,7 @@ int main(int argc, char* argv[]) // create worker thread for every core std::vector workerThreadVec; int i = 0; - for (std::vector::iterator iter = coresToUse.begin(); iter != coresToUse.end(); iter++) + for (auto iter = coresToUse.begin(); iter != coresToUse.end(); ++iter) { AppWorkerThread* newWorker = new AppWorkerThread(workerConfigArr[i], matchingEngine); workerThreadVec.push_back(newWorker); diff --git a/Examples/IcmpFileTransfer/Common.cpp b/Examples/IcmpFileTransfer/Common.cpp index 8998862d8f..f990e6eacf 100644 --- a/Examples/IcmpFileTransfer/Common.cpp +++ b/Examples/IcmpFileTransfer/Common.cpp @@ -91,9 +91,9 @@ void listInterfaces() const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); std::cout << std::endl << "Network interfaces:" << std::endl; - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + for (const auto &dev : devList) { - std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl; + std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; } exit(0); } diff --git a/Examples/PcapSearch/main.cpp b/Examples/PcapSearch/main.cpp index 1d7b7c0ed5..b294cec71e 100644 --- a/Examples/PcapSearch/main.cpp +++ b/Examples/PcapSearch/main.cpp @@ -166,8 +166,8 @@ int searchPcap(const std::string& pcapFilePath, std::string searchCriteria, std: // print layer by layer by layer as we want to add a few spaces before each layer std::vector packetLayers; parsedPacket.toStringList(packetLayers); - for (std::vector::iterator iter = packetLayers.begin(); iter != packetLayers.end(); iter++) - (*detailedReportFile) << "\n " << (*iter); + for (const auto &layer : packetLayers) + (*detailedReportFile) << "\n " << layer; (*detailedReportFile) << std::endl; } @@ -270,16 +270,16 @@ void searchDirectories(const std::string &directory, bool includeSubDirectories, // when we get to here we already covered all sub-directories and collected all the files in this directory that are required for search // go over each such file and search its packets to find the search criteria - for (std::vector::iterator iter = pcapList.begin(); iter != pcapList.end(); iter++) + for (const auto &iter : pcapList) { // do the actual search - int packetsFound = searchPcap(*iter, searchCriteria, detailedReportFile); + int packetsFound = searchPcap(iter, searchCriteria, detailedReportFile); // add to total matched packets totalFilesSearched++; if (packetsFound > 0) { - std::cout << packetsFound << " packets found in '" << *iter << "'" << std::endl; + std::cout << packetsFound << " packets found in '" << iter << "'" << std::endl; totalPacketsFound += packetsFound; } } diff --git a/Examples/PcapSplitter/main.cpp b/Examples/PcapSplitter/main.cpp index 7d48ee41b6..7cd742a3b3 100644 --- a/Examples/PcapSplitter/main.cpp +++ b/Examples/PcapSplitter/main.cpp @@ -446,21 +446,21 @@ int main(int argc, char* argv[]) outputFiles[fileNum]->writePacket(*parsedPacket.getRawPacket()); // if splitter wants us to close files - go over the file numbers and close them - for (std::vector::iterator it = filesToClose.begin(); it != filesToClose.end(); it++) + for (const auto &fileId : filesToClose) { // check if that file number is in the map - if (outputFiles.find(*it) != outputFiles.end()) + if (outputFiles.find(fileId) != outputFiles.end()) { // close the writer - outputFiles[*it]->close(); + outputFiles[fileId]->close(); // free the writer memory and put null in the map record - delete outputFiles[*it]; - outputFiles[*it] = nullptr; + delete outputFiles[fileId]; + outputFiles[fileId] = nullptr; } } - packetCountSoFar++; + ++packetCountSoFar; } std::cout << "Finished. Read and written " << packetCountSoFar << " packets to " << numOfFiles << " files" << std::endl; @@ -473,12 +473,12 @@ int main(int argc, char* argv[]) delete splitter; // close the writer files which are still open - for(std::map::iterator it = outputFiles.begin(); it != outputFiles.end(); ++it) + for(const auto &it : outputFiles) { - if (it->second != NULL) + if (it.second != NULL) { - it->second->close(); - delete it->second; + it.second->close(); + delete it.second; } } diff --git a/Examples/PfRingExample-FilterTraffic/main.cpp b/Examples/PfRingExample-FilterTraffic/main.cpp index 7fe3ef9070..0469936d9f 100644 --- a/Examples/PfRingExample-FilterTraffic/main.cpp +++ b/Examples/PfRingExample-FilterTraffic/main.cpp @@ -135,12 +135,12 @@ void listPfRingDevices() pcpp::Logger::getInstance().suppressLogs(); const std::vector& devList = pcpp::PfRingDeviceList::getInstance().getPfRingDevicesList(); - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + for (const auto &dev : devList) { std::ostringstream interfaceIndex; - if ((*iter)->getInterfaceIndex() <= 9999) + if (dev->getInterfaceIndex() <= 9999) { - interfaceIndex << (*iter)->getInterfaceIndex(); + interfaceIndex << dev->getInterfaceIndex(); } else { @@ -148,11 +148,11 @@ void listPfRingDevices() } std::cout - << " -> Name: " << std::left << std::setw(8) << (*iter)->getDeviceName() + << " -> Name: " << std::left << std::setw(8) << dev->getDeviceName() << " Index: " << std::setw(5) << interfaceIndex.str() - << " MAC address: " << std::setw(19) << ((*iter)->getMacAddress() == pcpp::MacAddress::Zero ? "N/A" : (*iter)->getMacAddress().toString()) - << " Available RX channels: " << std::setw(3) << (int)(*iter)->getTotalNumOfRxChannels() - << " MTU: " << (*iter)->getMtu() + << " MAC address: " << std::setw(19) << (dev->getMacAddress() == pcpp::MacAddress::Zero ? "N/A" : dev->getMacAddress().toString()) + << " Available RX channels: " << std::setw(3) << (int)dev->getTotalNumOfRxChannels() + << " MTU: " << dev->getMtu() << std::endl; } diff --git a/Examples/SSLAnalyzer/main.cpp b/Examples/SSLAnalyzer/main.cpp index 8ed220c976..3cc6ad9a70 100644 --- a/Examples/SSLAnalyzer/main.cpp +++ b/Examples/SSLAnalyzer/main.cpp @@ -124,9 +124,9 @@ void listInterfaces() const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); std::cout << std::endl << "Network interfaces:" << std::endl; - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + for (const auto &dev : devList) { - std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl; + std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; } exit(0); } @@ -211,12 +211,10 @@ void printServerNames(ClientHelloStats& clientHelloStatsCollector) std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); // go over all items (names + count) in the sorted vector and print them - for(std::vector >::iterator iter = map2vec.begin(); - iter != map2vec.end(); - iter++) + for(const auto &iter : map2vec) { std::stringstream values; - values << iter->first << "|" << iter->second; + values << iter.first << "|" << iter.second; printer.printRow(values.str(), '|'); } } @@ -242,12 +240,10 @@ void printVersions(std::map& versionMap, const std::string& headl std::sort(map2vec.begin(),map2vec.end(), &uint16CountComparer); // go over all items (names + count) in the sorted vector and print them - for(std::vector >::iterator iter = map2vec.begin(); - iter != map2vec.end(); - iter++) + for(const auto &iter : map2vec) { std::stringstream values; - values << pcpp::SSLVersion(iter->first).toString() << "|" << iter->second; + values << pcpp::SSLVersion(iter.first).toString() << "|" << iter.second; printer.printRow(values.str(), '|'); } } @@ -273,12 +269,10 @@ void printCipherSuites(ServerHelloStats& serverHelloStats) std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); // go over all items (names + count) in the sorted vector and print them - for(std::vector >::iterator iter = map2vec.begin(); - iter != map2vec.end(); - iter++) + for(const auto &iter : map2vec) { std::stringstream values; - values << iter->first << "|" << iter->second; + values << iter.first << "|" << iter.second; printer.printRow(values.str(), '|'); } } @@ -301,12 +295,10 @@ void printPorts(SSLGeneralStats& stats) std::sort(map2vec.begin(),map2vec.end(), &uint16CountComparer); // go over all items (names + count) in the sorted vector and print them - for(std::vector >::iterator iter = map2vec.begin(); - iter != map2vec.end(); - iter++) + for(const auto &iter : map2vec) { std::stringstream values; - values << iter->first << "|" << iter->second; + values << iter.first << "|" << iter.second; printer.printRow(values.str(), '|'); } } diff --git a/Examples/TLSFingerprinting/main.cpp b/Examples/TLSFingerprinting/main.cpp index b6913f8b7f..a85fcb238d 100644 --- a/Examples/TLSFingerprinting/main.cpp +++ b/Examples/TLSFingerprinting/main.cpp @@ -128,9 +128,9 @@ void listInterfaces() const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); std::cout << std::endl << "Network interfaces:" << std::endl; - for (std::vector::const_iterator iter = devList.begin(); iter != devList.end(); iter++) + for (const auto &dev : devList) { - std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl; + std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; } exit(0); } @@ -256,9 +256,7 @@ void printCommonTLSFingerprints(const std::map& tlsFinger std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); // go over all items (fingerprints + count) in the sorted vector and print them - for(std::vector >::iterator iter = map2vec.begin(); - iter != map2vec.end(); - iter++) + for(auto iter = map2vec.begin(); iter != map2vec.end(); ++iter) { if (iter - map2vec.begin() >= printCountItems) break; diff --git a/Packet++/header/SdpLayer.h b/Packet++/header/SdpLayer.h index 9ae16e6559..256f741fee 100644 --- a/Packet++/header/SdpLayer.h +++ b/Packet++/header/SdpLayer.h @@ -149,7 +149,7 @@ namespace pcpp * translated into a 'media-attribute' field (a=) * @return True if all fields were added properly or false if at least one field was failed to be added */ - bool addMediaDescription(const std::string& mediaType, uint16_t mediaPort, const std::string& mediaProtocol, const std::string& mediaFormat, std::vector mediaAttributes); + bool addMediaDescription(const std::string& mediaType, uint16_t mediaPort, const std::string& mediaProtocol, const std::string& mediaFormat, const std::vector &mediaAttributes); // overridden methods diff --git a/Packet++/src/BgpLayer.cpp b/Packet++/src/BgpLayer.cpp index ddf4c91b2d..bff92be631 100644 --- a/Packet++/src/BgpLayer.cpp +++ b/Packet++/src/BgpLayer.cpp @@ -163,26 +163,26 @@ size_t BgpOpenMessageLayer::optionalParamsToByteArray(const std::vector::const_iterator iter = optionalParams.begin(); iter != optionalParams.end(); iter++) + for (const auto ¶m : optionalParams) { - if (iter->length > 32) + if (param.length > 32) { - PCPP_LOG_ERROR("Illegal optional parameter length " << (int)iter->length << ", must be 32 bytes or less"); + PCPP_LOG_ERROR("Illegal optional parameter length " << (int)param.length << ", must be 32 bytes or less"); break; // illegal value } - size_t curDataSize = 2*sizeof(uint8_t) + (size_t)iter->length; + size_t curDataSize = 2*sizeof(uint8_t) + (size_t)param.length; if (dataLen + curDataSize > maxByteArrSize) { break; } - resultByteArr[0] = iter->type; - resultByteArr[1] = iter->length; - if (iter->length > 0) + resultByteArr[0] = param.type; + resultByteArr[1] = param.length; + if (param.length > 0) { - memcpy(resultByteArr + 2*sizeof(uint8_t), iter->value, iter->length); + memcpy(resultByteArr + 2*sizeof(uint8_t), param.value, param.length); } dataLen += curDataSize; @@ -416,13 +416,13 @@ size_t BgpUpdateMessageLayer::prefixAndIPDataToByteArray(const std::vector::const_iterator iter = prefixAndIpData.begin(); iter != prefixAndIpData.end(); iter++) + for (const auto &prefixAndIp : prefixAndIpData) { uint8_t curData[5]; - curData[0] = iter->prefix; + curData[0] = prefixAndIp.prefix; size_t curDataSize = 1; - const uint8_t* octets = iter->ipAddr.toBytes(); - if (iter->prefix == 32) + const uint8_t* octets = prefixAndIp.ipAddr.toBytes(); + if (prefixAndIp.prefix == 32) { curDataSize += 4; curData[1] = octets[0]; @@ -430,27 +430,27 @@ size_t BgpUpdateMessageLayer::prefixAndIPDataToByteArray(const std::vectorprefix == 24) + else if (prefixAndIp.prefix == 24) { curDataSize += 3; curData[1] = octets[0]; curData[2] = octets[1]; curData[3] = octets[2]; } - else if (iter->prefix == 16) + else if (prefixAndIp.prefix == 16) { curDataSize += 2; curData[1] = octets[0]; curData[2] = octets[1]; } - else if (iter->prefix == 8) + else if (prefixAndIp.prefix == 8) { curDataSize += 1; curData[1] = octets[0]; } else { - PCPP_LOG_ERROR("Illegal prefix value " << (int)iter->prefix); + PCPP_LOG_ERROR("Illegal prefix value " << (int)prefixAndIp.prefix); break; // illegal value } @@ -477,27 +477,27 @@ size_t BgpUpdateMessageLayer::pathAttributesToByteArray(const std::vector::const_iterator iter = pathAttributes.begin(); iter != pathAttributes.end(); iter++) + for (const auto &attribute : pathAttributes) { - if (iter->length > 32) + if (attribute.length > 32) { - PCPP_LOG_ERROR("Illegal path attribute length " << (int)iter->length); + PCPP_LOG_ERROR("Illegal path attribute length " << (int)attribute.length); break; // illegal value } - size_t curDataSize = 3*sizeof(uint8_t) + (size_t)iter->length; + size_t curDataSize = 3*sizeof(uint8_t) + (size_t)attribute.length; if (dataLen + curDataSize > maxByteArrSize) { break; } - resultByteArr[0] = iter->flags; - resultByteArr[1] = iter->type; - resultByteArr[2] = iter->length; - if (iter->length > 0) + resultByteArr[0] = attribute.flags; + resultByteArr[1] = attribute.type; + resultByteArr[2] = attribute.length; + if (attribute.length > 0) { - memcpy(resultByteArr + 3*sizeof(uint8_t), iter->data, iter->length); + memcpy(resultByteArr + 3*sizeof(uint8_t), attribute.data, attribute.length); } dataLen += curDataSize; diff --git a/Packet++/src/IPv4Layer.cpp b/Packet++/src/IPv4Layer.cpp index c1a2de8004..777af11e96 100644 --- a/Packet++/src/IPv4Layer.cpp +++ b/Packet++/src/IPv4Layer.cpp @@ -37,9 +37,9 @@ IPv4OptionBuilder::IPv4OptionBuilder(IPv4OptionTypes optionType, const std::vect m_RecValue[curOffset++] = 0; // init pointer value bool firstZero = false; - for (std::vector::const_iterator iter = ipList.begin(); iter != ipList.end(); iter++) + for (const auto &ipAddr : ipList) { - uint32_t ipAddrAsInt = iter->toInt(); + uint32_t ipAddrAsInt = ipAddr.toInt(); if (!firstZero) m_RecValue[0] += (uint8_t)4; diff --git a/Packet++/src/IPv6Extensions.cpp b/Packet++/src/IPv6Extensions.cpp index 2ce69ab704..21274f854c 100644 --- a/Packet++/src/IPv6Extensions.cpp +++ b/Packet++/src/IPv6Extensions.cpp @@ -149,9 +149,9 @@ IPv6TLVOptionHeader::IPv6TLVOptionHeader(const std::vector size_t totalSize = sizeof(uint16_t); // nextHeader + headerLen - for (std::vector::const_iterator iter = options.begin(); iter != options.end(); iter++) + for (const auto &iter : options) { - IPv6Option option = iter->build(); + IPv6Option option = iter.build(); totalSize += option.getTotalSize(); option.purgeRecordData(); } @@ -166,9 +166,9 @@ IPv6TLVOptionHeader::IPv6TLVOptionHeader(const std::vector size_t offset = sizeof(uint16_t); - for (std::vector::const_iterator iter = options.begin(); iter != options.end(); iter++) + for (const auto &iter : options) { - IPv6Option option = iter->build(); + IPv6Option option = iter.build(); memcpy(getDataPtr() + offset, option.getRecordBasePtr(), option.getTotalSize()); offset += option.getTotalSize(); option.purgeRecordData(); diff --git a/Packet++/src/IcmpLayer.cpp b/Packet++/src/IcmpLayer.cpp index 061c9ed8e2..564ad4bfd3 100644 --- a/Packet++/src/IcmpLayer.cpp +++ b/Packet++/src/IcmpLayer.cpp @@ -344,10 +344,10 @@ icmp_router_advertisement* IcmpLayer::setRouterAdvertisementData(uint8_t code, u header->header->addressEntrySize = 2; icmp_router_address_structure* curPos = (icmp_router_address_structure*)((uint8_t*)header->header + sizeof(icmp_router_advertisement_hdr)); - for (std::vector::const_iterator iter = routerAddresses.begin(); iter != routerAddresses.end(); iter++) + for (const auto &iter : routerAddresses) { - curPos->routerAddress = iter->routerAddress; - curPos->preferenceLevel = iter->preferenceLevel; + curPos->routerAddress = iter.routerAddress; + curPos->preferenceLevel = iter.preferenceLevel; curPos += 1; } diff --git a/Packet++/src/IgmpLayer.cpp b/Packet++/src/IgmpLayer.cpp index d1df38e237..96253b1248 100644 --- a/Packet++/src/IgmpLayer.cpp +++ b/Packet++/src/IgmpLayer.cpp @@ -420,9 +420,9 @@ igmpv3_group_record* IgmpV3ReportLayer::addGroupRecordAt(uint8_t recordType, con newGroupRecord->numOfSources = htobe16(sourceAddresses.size()); int srcAddrOffset = 0; - for (std::vector::const_iterator iter = sourceAddresses.begin(); iter != sourceAddresses.end(); iter++) + for (const auto &address : sourceAddresses) { - memcpy(newGroupRecord->sourceAddresses + srcAddrOffset, iter->toBytes(), sizeof(uint32_t)); + memcpy(newGroupRecord->sourceAddresses + srcAddrOffset, address.toBytes(), sizeof(uint32_t)); srcAddrOffset += sizeof(uint32_t); } diff --git a/Packet++/src/Packet.cpp b/Packet++/src/Packet.cpp index 339efeba5c..d4e8953f1a 100644 --- a/Packet++/src/Packet.cpp +++ b/Packet++/src/Packet.cpp @@ -14,6 +14,7 @@ #include "Logger.h" #include "EndianPortable.h" #include +#include #include #include #ifdef _MSC_VER @@ -791,14 +792,9 @@ Layer* Packet::createFirstLayer(LinkLayerType linkType) std::string Packet::toString(bool timeAsLocalTime) const { std::vector stringList; - std::string result; toStringList(stringList, timeAsLocalTime); - for (std::vector::iterator iter = stringList.begin(); iter != stringList.end(); iter++) - { - result += *iter + '\n'; - } - - return result; + return std::accumulate(stringList.begin(), stringList.end(), std::string(), + [](std::string a, const std::string &b) { return std::move(a) + b + '\n'; }); } void Packet::toStringList(std::vector& result, bool timeAsLocalTime) const diff --git a/Packet++/src/SSLHandshake.cpp b/Packet++/src/SSLHandshake.cpp index 39601eecea..4ffc73d8b6 100644 --- a/Packet++/src/SSLHandshake.cpp +++ b/Packet++/src/SSLHandshake.cpp @@ -1502,9 +1502,9 @@ SSLClientHelloMessage::ClientHelloTLSFingerprint SSLClientHelloMessage::generate if (supportedGroupsExt != nullptr) { std::vector supportedGroups = supportedGroupsExt->getSupportedGroups(); - for (std::vector::const_iterator iter = supportedGroups.begin(); iter != supportedGroups.end(); iter++) - if (GreaseSet.find(*iter) == GreaseSet.end()) - result.supportedGroups.push_back(*iter); + for (const auto &iter : supportedGroups) + if (GreaseSet.find(iter) == GreaseSet.end()) + result.supportedGroups.push_back(iter); } // extract EC point formats @@ -1536,36 +1536,36 @@ std::string SSLClientHelloMessage::ClientHelloTLSFingerprint::toString() // add cipher suites bool firstCipher = true; - for (std::vector::const_iterator iter = cipherSuites.begin(); iter != cipherSuites.end(); iter++) + for (const auto &iter : cipherSuites) { - tlsFingerprint << (firstCipher ? "" : "-") << *iter; + tlsFingerprint << (firstCipher ? "" : "-") << iter; firstCipher = false; } tlsFingerprint << ","; // add extensions bool firstExtension = true; - for (std::vector::const_iterator iter = extensions.begin(); iter != extensions.end(); iter++) + for (const auto &iter : extensions) { - tlsFingerprint << (firstExtension ? "" : "-") << *iter; + tlsFingerprint << (firstExtension ? "" : "-") << iter; firstExtension = false; } tlsFingerprint << ","; // add supported groups bool firstGroup = true; - for (std::vector::const_iterator iter = supportedGroups.begin(); iter != supportedGroups.end(); iter++) + for (const auto &iter : supportedGroups) { - tlsFingerprint << (firstGroup ? "" : "-") << (*iter); + tlsFingerprint << (firstGroup ? "" : "-") << iter; firstGroup = false; } tlsFingerprint << ","; // add EC point formats bool firstPointFormat = true; - for (std::vector::iterator iter = ecPointFormats.begin(); iter != ecPointFormats.end(); iter++) + for (auto iter : ecPointFormats) { - tlsFingerprint << (firstPointFormat ? "" : "-") << (int)(*iter); + tlsFingerprint << (firstPointFormat ? "" : "-") << (int)iter; firstPointFormat = false; } @@ -1793,9 +1793,9 @@ std::string SSLServerHelloMessage::ServerHelloTLSFingerprint::toString() // add extensions bool firstExtension = true; - for (std::vector::const_iterator iter = extensions.begin(); iter != extensions.end(); iter++) + for (const auto &iter : extensions) { - tlsFingerprint << (firstExtension ? "" : "-") << *iter; + tlsFingerprint << (firstExtension ? "" : "-") << iter; firstExtension = false; } diff --git a/Packet++/src/SdpLayer.cpp b/Packet++/src/SdpLayer.cpp index b90d61c095..9a346175ea 100644 --- a/Packet++/src/SdpLayer.cpp +++ b/Packet++/src/SdpLayer.cpp @@ -107,7 +107,7 @@ uint16_t SdpLayer::getMediaPort(const std::string& mediaType) const return 0; } -bool SdpLayer::addMediaDescription(const std::string& mediaType, uint16_t mediaPort, const std::string& mediaProtocol, const std::string& mediaFormat, std::vector mediaAttributes) +bool SdpLayer::addMediaDescription(const std::string& mediaType, uint16_t mediaPort, const std::string& mediaProtocol, const std::string& mediaFormat, const std::vector &mediaAttributes) { std::stringstream portStream; portStream << mediaPort; @@ -120,11 +120,11 @@ bool SdpLayer::addMediaDescription(const std::string& mediaType, uint16_t mediaP } - for (std::vector::iterator iter = mediaAttributes.begin(); iter != mediaAttributes.end(); iter++) + for (const auto &iter : mediaAttributes) { - if (addField(PCPP_SDP_MEDIA_ATTRIBUTE_FIELD, *iter) == nullptr) + if (addField(PCPP_SDP_MEDIA_ATTRIBUTE_FIELD, iter) == nullptr) { - PCPP_LOG_ERROR("Failed to add media attribute '" << *iter << "'"); + PCPP_LOG_ERROR("Failed to add media attribute '" << iter << "'"); return false; } } diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index 063ae58aa6..408bda677c 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -64,9 +64,9 @@ DpdkDeviceList::DpdkDeviceList() DpdkDeviceList::~DpdkDeviceList() { - for (std::vector::iterator iter = m_DpdkDeviceList.begin(); iter != m_DpdkDeviceList.end(); iter++) + for (auto dev : m_DpdkDeviceList) { - delete (*iter); + delete dev; } m_DpdkDeviceList.clear(); @@ -223,13 +223,16 @@ DpdkDevice* DpdkDeviceList::getDeviceByPciAddress(const std::string& pciAddr) co return NULL; } - for (std::vector::const_iterator iter = m_DpdkDeviceList.begin(); iter != m_DpdkDeviceList.end(); iter++) + auto devIter = std::find_if(m_DpdkDeviceList.begin(), m_DpdkDeviceList.end(), + [&pciAddr](const DpdkDevice *dev) { return dev->getPciAddress() == pciAddr; }); + + if (devIter == m_DpdkDeviceList.end()) { - if ((*iter)->getPciAddress() == pciAddr) - return (*iter); + PCPP_LOG_DEBUG("Found no DPDK devices with PCI address '" << pciAddr << "'"); + return nullptr; } - return NULL; + return *devIter; } bool DpdkDeviceList::verifyHugePagesAndDpdkDriver() @@ -369,7 +372,7 @@ bool DpdkDeviceList::startDpdkWorkerThreads(CoreMask coreMask, std::vector::iterator iter = workerThreadsVec.begin(); + auto iter = workerThreadsVec.begin(); while (iter != workerThreadsVec.end()) { SystemCore core = SystemCores::IdToSystemCore[index]; @@ -382,19 +385,19 @@ bool DpdkDeviceList::startDpdkWorkerThreads(CoreMask coreMask, std::vector::iterator iter2 = workerThreadsVec.begin(); iter2 != iter; iter2++) + for (const auto &thread : workerThreadsVec) { - (*iter)->stop(); - rte_eal_wait_lcore((*iter)->getCoreId()); - PCPP_LOG_DEBUG("Thread on core [" << (*iter)->getCoreId() << "] stopped"); + thread->stop(); + rte_eal_wait_lcore(thread->getCoreId()); + PCPP_LOG_DEBUG("Thread on core [" << thread->getCoreId() << "] stopped"); } PCPP_LOG_ERROR("Cannot create worker thread #" << core.Id << ". Error was: [" << strerror(err) << "]"); return false; } m_WorkerThreads.push_back(*iter); - index++; - iter++; + ++index; + ++iter; } return true; @@ -408,11 +411,11 @@ void DpdkDeviceList::stopDpdkWorkerThreads() return; } - for (std::vector::iterator iter = m_WorkerThreads.begin(); iter != m_WorkerThreads.end(); iter++) + for (const auto &thread : m_WorkerThreads) { - (*iter)->stop(); - rte_eal_wait_lcore((*iter)->getCoreId()); - PCPP_LOG_DEBUG("Thread on core [" << (*iter)->getCoreId() << "] stopped"); + thread->stop(); + rte_eal_wait_lcore(thread->getCoreId()); + PCPP_LOG_DEBUG("Thread on core [" << thread->getCoreId() << "] stopped"); } m_WorkerThreads.clear(); diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index de7f50ca39..c90f39001f 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -1018,16 +1018,16 @@ void PcapLiveDevice::setDefaultGateway() IPv4Address PcapLiveDevice::getIPv4Address() const { - for(std::vector::const_iterator addrIter = m_Addresses.begin(); addrIter != m_Addresses.end(); addrIter++) + for(const auto &addrIter : m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter->addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter->addr, addrAsString); + internal::sockaddr2string(addrIter.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in_addr* currAddr = internal::sockaddr2in_addr(addrIter->addr); + in_addr* currAddr = internal::sockaddr2in_addr(addrIter.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); @@ -1042,16 +1042,15 @@ IPv4Address PcapLiveDevice::getIPv4Address() const IPv6Address PcapLiveDevice::getIPv6Address() const { - for (std::vector::const_iterator addrIter = m_Addresses.begin(); addrIter != m_Addresses.end(); - addrIter++) + for (const auto &addrIter : m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter->addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter->addr, addrAsString); + internal::sockaddr2string(addrIter.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in6_addr *currAddr = internal::sockaddr2in6_addr(addrIter->addr); + in6_addr *currAddr = internal::sockaddr2in6_addr(addrIter.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index c6da146dfe..c2012429e6 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -30,9 +30,9 @@ PcapLiveDeviceList::PcapLiveDeviceList() PcapLiveDeviceList::~PcapLiveDeviceList() { - for(std::vector::iterator devIter = m_LiveDeviceList.begin(); devIter != m_LiveDeviceList.end(); devIter++) + for(const auto &devIter : m_LiveDeviceList) { - delete (*devIter); + delete devIter; } } @@ -231,19 +231,19 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAdd PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(std::vector::const_iterator devIter = m_LiveDeviceList.begin(); devIter != m_LiveDeviceList.end(); devIter++) + for(const auto &devIter : m_LiveDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); - for(std::vector::iterator addrIter = (*devIter)->m_Addresses.begin(); addrIter != (*devIter)->m_Addresses.end(); addrIter++) + PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); + for(const auto &addrIter : devIter->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter->addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter->addr, addrAsString); + internal::sockaddr2string(addrIter.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in_addr* currAddr = internal::sockaddr2in_addr(addrIter->addr); + in_addr* currAddr = internal::sockaddr2in_addr(addrIter.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); @@ -253,7 +253,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA if (currAddr->s_addr == ipAddr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return (*devIter); + return devIter; } } } @@ -264,19 +264,19 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(std::vector::const_iterator devIter = m_LiveDeviceList.begin(); devIter != m_LiveDeviceList.end(); devIter++) + for(const auto &devIter : m_LiveDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); - for(std::vector::iterator addrIter = (*devIter)->m_Addresses.begin(); addrIter != (*devIter)->m_Addresses.end(); addrIter++) + PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); + for(const auto &addrIter : devIter->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter->addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter->addr, addrAsString); + internal::sockaddr2string(addrIter.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in6_addr* currAddr = internal::sockaddr2in6_addr(addrIter->addr); + in6_addr* currAddr = internal::sockaddr2in6_addr(addrIter.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); @@ -289,7 +289,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 { PCPP_LOG_DEBUG("Found matched address!"); delete [] addrAsArr; - return (*devIter); + return devIter; } delete [] addrAsArr; @@ -316,13 +316,16 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipA PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(std::vector::const_iterator devIter = m_LiveDeviceList.begin(); devIter != m_LiveDeviceList.end(); devIter++) + auto devIter = std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), + [&name](const PcapLiveDevice *dev) { return dev->getName() == name; }); + + if (devIter == m_LiveDeviceList.end()) { - if (name == (*devIter)->getName()) - return (*devIter); + PCPP_LOG_DEBUG("Found no live device with name '" << name << "'"); + return nullptr; } - return nullptr; + return *devIter; } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const @@ -345,9 +348,9 @@ PcapLiveDeviceList* PcapLiveDeviceList::clone() void PcapLiveDeviceList::reset() { - for(std::vector::iterator devIter = m_LiveDeviceList.begin(); devIter != m_LiveDeviceList.end(); devIter++) + for(auto devIter : m_LiveDeviceList) { - delete (*devIter); + delete devIter; } m_LiveDeviceList.clear(); diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 94197d3513..7fa25e0984 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -109,16 +109,16 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i for(ConstRemoteDeviceListIterator devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); devIter++) { PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); - for(std::vector::iterator addrIter = (*devIter)->m_Addresses.begin(); addrIter != (*devIter)->m_Addresses.end(); addrIter++) + for(const auto &addrIter : (*devIter)->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter->addr != NULL) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter->addr, addrAsString); + internal::sockaddr2string(addrIter.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in_addr* currAddr = internal::sockaddr2in_addr(addrIter->addr); + in_addr* currAddr = internal::sockaddr2in_addr(addrIter.addr); if (currAddr == NULL) { PCPP_LOG_DEBUG("Address is NULL"); @@ -143,16 +143,16 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& i for(ConstRemoteDeviceListIterator devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); devIter++) { PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); - for(std::vector::iterator addrIter = (*devIter)->m_Addresses.begin(); addrIter != (*devIter)->m_Addresses.end(); addrIter++) + for(const auto &addrIter : (*devIter)->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter->addr != NULL) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter->addr, addrAsString); + internal::sockaddr2string(addrIter.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in6_addr* currAddr = internal::sockaddr2in6_addr(addrIter->addr); + in6_addr* currAddr = internal::sockaddr2in6_addr(addrIter.addr); if (currAddr == NULL) { PCPP_LOG_DEBUG("Address is NULL"); diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index 5fe1a575b7..8102f901a3 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -59,23 +59,25 @@ PfRingDeviceList::PfRingDeviceList() PfRingDeviceList::~PfRingDeviceList() { - for(std::vector::iterator devIter = m_PfRingDeviceList.begin(); devIter != m_PfRingDeviceList.end(); devIter++) + for(auto devIter : m_PfRingDeviceList) { - delete (*devIter); + delete devIter; } } PfRingDevice* PfRingDeviceList::getPfRingDeviceByName(const std::string &devName) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(std::vector::const_iterator devIter = m_PfRingDeviceList.begin(); devIter != m_PfRingDeviceList.end(); devIter++) + auto devIter = std::find_if(m_PfRingDeviceList.begin(), m_PfRingDeviceList.end(), + [&devName](const PfRingDevice *dev) { return dev->getDeviceName() == devName; }); + + if (devIter == m_PfRingDeviceList.end()) { - if ((*devIter)->getDeviceName() == devName) - return (*devIter); + PCPP_LOG_DEBUG("Found no PF_RING devices with name '" << devName << "'"); + return nullptr; } - PCPP_LOG_DEBUG("Found no PF_RING devices with name '" << devName << "'"); - return NULL; + return *devIter; } void PfRingDeviceList::calcPfRingVersion(void* ring) diff --git a/Tests/Packet++Test/Tests/PacketTests.cpp b/Tests/Packet++Test/Tests/PacketTests.cpp index a5005c2938..8c48a4d141 100644 --- a/Tests/Packet++Test/Tests/PacketTests.cpp +++ b/Tests/Packet++Test/Tests/PacketTests.cpp @@ -1019,7 +1019,7 @@ PTF_TEST_CASE(PrintPacketAndLayers) std::ostringstream layerStream; layerStream << *layer; PTF_ASSERT_EQUAL(layerStream.str(), *iter); - iter++; + ++iter; } PTF_ASSERT_TRUE(iter == expectedLayerStrings.end()); diff --git a/Tests/Pcap++Test/Tests/DpdkTests.cpp b/Tests/Pcap++Test/Tests/DpdkTests.cpp index ffe7c1fc61..417b714a65 100644 --- a/Tests/Pcap++Test/Tests/DpdkTests.cpp +++ b/Tests/Pcap++Test/Tests/DpdkTests.cpp @@ -485,25 +485,25 @@ PTF_TEST_CASE(TestDpdkMultiThread) PTF_ASSERT_EQUAL(res.size(), 0); if (PTF_IS_VERBOSE_MODE) { - for (std::map >::iterator iter = res.begin(); iter != res.end(); iter++) + for (auto &iter : res) { - PTF_PRINT_VERBOSE("Same flow exists in core " << firstCoreId << " and core " << secondCoreId << ". Flow key = " << iter->first); + PTF_PRINT_VERBOSE("Same flow exists in core " << firstCoreId << " and core " << secondCoreId << ". Flow key = " << iter.first); std::ostringstream stream; - stream << "Core" << firstCoreId << "_Flow_" << std::hex << iter->first << ".pcap"; + stream << "Core" << firstCoreId << "_Flow_" << std::hex << iter.first << ".pcap"; pcpp::PcapFileWriterDevice writerDev(stream.str()); writerDev.open(); - writerDev.writePackets(iter->second.first); + writerDev.writePackets(iter.second.first); writerDev.close(); std::ostringstream stream2; - stream2 << "Core" << secondCoreId << "_Flow_" << std::hex << iter->first << ".pcap"; + stream2 << "Core" << secondCoreId << "_Flow_" << std::hex << iter.first << ".pcap"; pcpp::PcapFileWriterDevice writerDev2(stream2.str()); writerDev2.open(); - writerDev2.writePackets(iter->second.second); + writerDev2.writePackets(iter.second.second); writerDev2.close(); - iter->second.first.clear(); - iter->second.second.clear(); + iter.second.first.clear(); + iter.second.second.clear(); } } @@ -513,10 +513,10 @@ PTF_TEST_CASE(TestDpdkMultiThread) if (PTF_IS_VERBOSE_MODE) { - for(std::map::iterator iter = packetDataMultiThread[firstCoreId].FlowKeys.begin(); iter != packetDataMultiThread[firstCoreId].FlowKeys.end(); iter++) + for(auto &iter : packetDataMultiThread[firstCoreId].FlowKeys) { - PTF_PRINT_VERBOSE("Key=0x" << std::hex << iter->first << "; Value=" << std::dec << iter->second.size()); - iter->second.clear(); + PTF_PRINT_VERBOSE("Key=0x" << std::hex << iter.first << "; Value=" << std::dec << iter.second.size()); + iter.second.clear(); } } @@ -804,9 +804,9 @@ PTF_TEST_CASE(TestDpdkDeviceWorkerThreads) // that total amount of packets received by all threads is greater than zero int packetCount = 0; - for (std::vector::iterator iter = workerThreadVec.begin(); iter != workerThreadVec.end(); iter++) + for (auto &iter : workerThreadVec) { - DpdkTestWorkerThread* thread = (DpdkTestWorkerThread*)(*iter); + DpdkTestWorkerThread* thread = (DpdkTestWorkerThread*)iter; PTF_ASSERT_TRUE(thread->threadRanAndStopped()); packetCount += thread->getPacketCount(); PTF_PRINT_VERBOSE("Worker thread on core " << thread->getCoreId() << " captured " << thread->getPacketCount() << " packets"); diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 337c209fc2..c2bb2be257 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -154,11 +154,11 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) PTF_ASSERT_FALSE(devList.empty()); pcpp::IPv4Address defaultGateway = pcpp::IPv4Address::Zero; - for(std::vector::iterator iter = devList.begin(); iter != devList.end(); iter++) + for(const auto &iter : devList) { - PTF_ASSERT_FALSE((*iter)->getName().empty()); + PTF_ASSERT_FALSE(iter->getName().empty()); if (defaultGateway == pcpp::IPv4Address::Zero) - defaultGateway = (*iter)->getDefaultGateway(); + defaultGateway = iter->getDefaultGateway(); } PTF_ASSERT_NOT_EQUAL(defaultGateway, pcpp::IPv4Address::Zero); @@ -172,9 +172,9 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); PTF_ASSERT_FALSE(devList.empty()); - for(std::vector::iterator iter = devList.begin(); iter != devList.end(); iter++) + for(const auto &iter : devList) { - PTF_ASSERT_FALSE((*iter)->getName().empty()); + PTF_ASSERT_FALSE(iter->getName().empty()); } pcpp::PcapLiveDeviceList *clonedDevList = pcpp::PcapLiveDeviceList::getInstance().clone(); @@ -183,8 +183,8 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) std::vector clonedDevListVector = clonedDevList->getPcapLiveDevicesList(); PTF_ASSERT_EQUAL(clonedDevListVector.size(), devList.size()); - std::vector::iterator iterCloned = clonedDevListVector.begin(); - for(std::vector::iterator iter = devList.begin(); iter != devList.end(); iter++, iterCloned++) + auto iterCloned = clonedDevListVector.begin(); + for(auto iter = devList.begin(); iter != devList.end(); ++iter, ++iterCloned) { PTF_ASSERT_EQUAL((*iter)->getName(), (*iterCloned)->getName()); } @@ -312,13 +312,12 @@ PTF_TEST_CASE(TestPcapLiveDeviceNoNetworking) std::vector devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); PTF_ASSERT_FALSE(devList.empty()); - for(std::vector::iterator iter = devList.begin(); iter != devList.end(); iter++) + auto iter = std::find_if(devList.begin(), devList.end(), [](const pcpp::PcapLiveDevice *dev) { + return !dev->getLoopback() && dev->getIPv4Address() != pcpp::IPv4Address::Zero; + }); + if (iter != devList.end()) { - if (!(*iter)->getLoopback() && (*iter)->getIPv4Address() != pcpp::IPv4Address::Zero) - { - liveDev = *iter; - break; - } + liveDev = *iter; } PTF_ASSERT_NOT_NULL(liveDev); diff --git a/Tests/Pcap++Test/Tests/PfRingTests.cpp b/Tests/Pcap++Test/Tests/PfRingTests.cpp index 014f637dd0..1d4c529995 100644 --- a/Tests/Pcap++Test/Tests/PfRingTests.cpp +++ b/Tests/Pcap++Test/Tests/PfRingTests.cpp @@ -346,11 +346,9 @@ PTF_TEST_CASE(TestPfRingDeviceMultiThread) PTF_PRINT_VERBOSE("Packets captured: " << stats.recv); PTF_PRINT_VERBOSE("Packets dropped: " << stats.drop); PTF_PRINT_VERBOSE("Total flows: " << packetDataMultiThread[i].FlowKeys.size()); - for(std::map::iterator iter = packetDataMultiThread[i].FlowKeys.begin(); - iter != packetDataMultiThread[i].FlowKeys.end(); - iter++) + for(const auto &iter : packetDataMultiThread[i].FlowKeys) { - PTF_PRINT_VERBOSE("Key=0x" << std::hex << iter->first << "; Value=" << std::dec << iter->second.size()); + PTF_PRINT_VERBOSE("Key=0x" << std::hex << iter.first << "; Value=" << std::dec << iter.second.size()); } } diff --git a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp index f17a32c17f..09a9dde472 100644 --- a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp +++ b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp @@ -174,7 +174,7 @@ static void tcpReassemblyConnectionEndCallback(const pcpp::ConnectionData& conne // tcpReassemblyTest() // ~~~~~~~~~~~~~~~~~~~ -static bool tcpReassemblyTest(std::vector& packetStream, TcpReassemblyMultipleConnStats& results, bool monitorOpenCloseConns, bool closeConnsManually) +static bool tcpReassemblyTest(const std::vector& packetStream, TcpReassemblyMultipleConnStats& results, bool monitorOpenCloseConns, bool closeConnsManually) { pcpp::TcpReassembly* tcpReassembly = nullptr; @@ -183,9 +183,9 @@ static bool tcpReassemblyTest(std::vector& packetStream, TcpRea else tcpReassembly = new pcpp::TcpReassembly(tcpReassemblyMsgReadyCallback, &results); - for (std::vector::iterator iter = packetStream.begin(); iter != packetStream.end(); iter++) + for (auto iter : packetStream) { - pcpp::Packet packet(&(*iter)); + pcpp::Packet packet(&iter); tcpReassembly->reassemblePacket(packet); } @@ -668,9 +668,9 @@ PTF_TEST_CASE(TestTcpReassemblyMultipleConns) int statusIndex = 0; - for (std::vector::iterator iter = packetStream.begin(); iter != packetStream.end(); iter++) + for (auto iter : packetStream) { - pcpp::Packet packet(&(*iter)); + pcpp::Packet packet(&iter); pcpp::TcpReassembly::ReassemblyStatus status = tcpReassembly.reassemblePacket(packet); PTF_ASSERT_EQUAL(status, expectedStatuses[statusIndex++], enum); } @@ -690,7 +690,7 @@ PTF_TEST_CASE(TestTcpReassemblyMultipleConns) expectedReassemblyData = readFileIntoString(std::string("PcapExamples/three_http_streams_conn_1_output.txt")); PTF_ASSERT_EQUAL(expectedReassemblyData, iter->second.reassembledData); - iter++; + ++iter; PTF_ASSERT_EQUAL(iter->second.numOfDataPackets, 2); PTF_ASSERT_EQUAL(iter->second.numOfMessagesFromSide[0], 1); @@ -701,7 +701,7 @@ PTF_TEST_CASE(TestTcpReassemblyMultipleConns) expectedReassemblyData = readFileIntoString(std::string("PcapExamples/three_http_streams_conn_2_output.txt")); PTF_ASSERT_EQUAL(expectedReassemblyData, iter->second.reassembledData); - iter++; + ++iter; PTF_ASSERT_EQUAL(iter->second.numOfDataPackets, 2); PTF_ASSERT_EQUAL(iter->second.numOfMessagesFromSide[0], 1); @@ -840,7 +840,7 @@ PTF_TEST_CASE(TestTcpReassemblyIPv6MultConns) expectedReassemblyData = readFileIntoString(std::string("PcapExamples/one_ipv6_http_stream4.txt")); PTF_ASSERT_EQUAL(expectedReassemblyData, iter->second.reassembledData); - iter++; + ++iter; PTF_ASSERT_EQUAL(iter->second.numOfDataPackets, 10); PTF_ASSERT_EQUAL(iter->second.numOfMessagesFromSide[0], 1); @@ -858,7 +858,7 @@ PTF_TEST_CASE(TestTcpReassemblyIPv6MultConns) PTF_ASSERT_EQUAL(stats.begin()->second.connData.endTime.tv_sec, 0); PTF_ASSERT_EQUAL(stats.begin()->second.connData.endTime.tv_usec, 0); - iter++; + ++iter; PTF_ASSERT_EQUAL(iter->second.numOfDataPackets, 2); PTF_ASSERT_EQUAL(iter->second.numOfMessagesFromSide[0], 1); @@ -878,7 +878,7 @@ PTF_TEST_CASE(TestTcpReassemblyIPv6MultConns) expectedReassemblyData = readFileIntoString(std::string("PcapExamples/one_ipv6_http_stream3.txt")); PTF_ASSERT_EQUAL(expectedReassemblyData, iter->second.reassembledData); - iter++; + ++iter; PTF_ASSERT_EQUAL(iter->second.numOfDataPackets, 13); PTF_ASSERT_EQUAL(iter->second.numOfMessagesFromSide[0], 4); @@ -961,9 +961,9 @@ PTF_TEST_CASE(TestTcpReassemblyCleanup) packetStream.pop_back(); - for(std::vector::iterator iter = packetStream.begin(); iter != packetStream.end(); iter++) + for(auto iter : packetStream) { - pcpp::Packet packet(&(*iter)); + pcpp::Packet packet(&iter); tcpReassembly.reassemblePacket(packet); } @@ -1020,9 +1020,9 @@ PTF_TEST_CASE(TestTcpReassemblyMaxOOOFrags) std::vector packetStream; PTF_ASSERT_TRUE(readPcapIntoPacketVec("PcapExamples/unidirectional_tcp_stream_with_missing_packet.pcap", packetStream, errMsg)); - for(std::vector::iterator iter = packetStream.begin(); iter != packetStream.end(); iter++) + for(auto iter : packetStream) { - pcpp::Packet packet(&(*iter)); + pcpp::Packet packet(&iter); tcpReassembly1.reassemblePacket(packet); tcpReassembly2.reassemblePacket(packet); } @@ -1104,9 +1104,9 @@ PTF_TEST_CASE(TestTcpReassemblyDisableOOOCleanup) // TestTcpReassemblyDisableBas std::swap(packetStream[13], packetStream[18]); TcpReassemblyMultipleConnStats tcpReassemblyResults; - for (std::vector::iterator iter = packetStream.begin(); iter != packetStream.end(); iter++) + for (auto iter : packetStream) { - pcpp::Packet packet(&(*iter)); + pcpp::Packet packet(&iter); tcpReassembly1.reassemblePacket(packet); tcpReassembly2.reassemblePacket(packet); } diff --git a/Tests/PcppTestFramework/PcppTestFrameworkRun.h b/Tests/PcppTestFramework/PcppTestFrameworkRun.h index 5cd72ddd9b..4b46b49536 100644 --- a/Tests/PcppTestFramework/PcppTestFrameworkRun.h +++ b/Tests/PcppTestFramework/PcppTestFrameworkRun.h @@ -1,6 +1,7 @@ #ifndef PCPP_TEST_FRAMEWORK_RUN #define PCPP_TEST_FRAMEWORK_RUN +#include #include #include #include @@ -32,14 +33,11 @@ static bool __ptfCheckTags(const std::string &tagSet, const std::string &tagSetT __ptfSplitString(tagSet, tagSetVec); __ptfSplitString(tagSetToCompareWith, tagSetToCompareWithVec); - for (std::vector::const_iterator tagSetToCompareWithIter = tagSetToCompareWithVec.begin(); tagSetToCompareWithIter != tagSetToCompareWithVec.end(); tagSetToCompareWithIter++) + for (const auto &tagSetToCompareWithIter : tagSetToCompareWithVec) { - for (std::vector::const_iterator tagSetIter = tagSetVec.begin(); tagSetIter != tagSetVec.end(); tagSetIter++) + if (std::any_of(tagSetVec.begin(), tagSetVec.end(), [tagSetToCompareWithIter](const std::string &val){ return val == tagSetToCompareWithIter;})) { - if (*tagSetIter == *tagSetToCompareWithIter) - { - return true; - } + return true; } } diff --git a/cppcheckSuppressions.txt b/cppcheckSuppressions.txt index 5b6bc202b7..bab0d70856 100644 --- a/cppcheckSuppressions.txt +++ b/cppcheckSuppressions.txt @@ -12,5 +12,4 @@ noExplicitConstructor:Common++/header/MacAddress.h noExplicitConstructor:Pcap++/header/PcapFileDevice.h missingOverride:* -postfixOperator:* cstyleCast:*