From 61399f0201f62f0a4acce893879804ceb24ce072 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 23 Jan 2024 17:15:40 +0900 Subject: [PATCH 1/2] use std::unordered_map --- Common++/header/LRUList.h | 6 +++--- Examples/DnsSpoofing/main.cpp | 6 +++--- Examples/DpdkBridge/Common.h | 2 +- .../AppWorkerThread.h | 2 +- Examples/DpdkExample-FilterTraffic/Common.h | 4 ++-- Examples/HttpAnalyzer/main.cpp | 2 +- Examples/IPDefragUtil/main.cpp | 6 +++--- Examples/IPFragUtil/main.cpp | 6 +++--- Examples/PcapSearch/main.cpp | 6 +++--- Examples/PcapSplitter/ConnectionSplitters.h | 2 +- Examples/PcapSplitter/Splitters.h | 6 +++--- Examples/PcapSplitter/main.cpp | 4 ++-- Examples/PfRingExample-FilterTraffic/main.cpp | 8 ++++---- Examples/SSLAnalyzer/SSLStatsCollector.h | 12 ++++++------ Examples/SSLAnalyzer/main.cpp | 10 +++++----- Examples/TLSFingerprinting/main.cpp | 10 +++++----- Packet++/header/IPReassembly.h | 4 ++-- Packet++/header/SomeIpSdLayer.h | 2 +- Packet++/header/TcpReassembly.h | 5 +++-- Packet++/src/GtpLayer.cpp | 10 +++++----- Packet++/src/IPReassembly.cpp | 8 ++++---- Packet++/src/PPPoELayer.cpp | 10 +++++----- Packet++/src/SSLHandshake.cpp | 18 +++++++++--------- Tests/Pcap++Test/Common/TestUtils.h | 12 ++++++------ Tests/Pcap++Test/Tests/DpdkTests.cpp | 6 +++--- Tests/Pcap++Test/Tests/PfRingTests.cpp | 4 ++-- Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp | 2 +- 27 files changed, 87 insertions(+), 86 deletions(-) diff --git a/Common++/header/LRUList.h b/Common++/header/LRUList.h index 1d2e645273..89eb1c9470 100644 --- a/Common++/header/LRUList.h +++ b/Common++/header/LRUList.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #if __cplusplus > 199711L || _MSC_VER >= 1800 @@ -29,7 +29,7 @@ namespace pcpp public: typedef typename std::list::iterator ListIterator; - typedef typename std::map::iterator MapIterator; + typedef typename std::unordered_map::iterator MapIterator; /** * A c'tor for this class @@ -125,7 +125,7 @@ namespace pcpp private: std::list m_CacheItemsList; - std::map m_CacheItemsMap; + std::unordered_map m_CacheItemsMap; size_t m_MaxSize; }; diff --git a/Examples/DnsSpoofing/main.cpp b/Examples/DnsSpoofing/main.cpp index 7443714e93..dca0c3273d 100644 --- a/Examples/DnsSpoofing/main.cpp +++ b/Examples/DnsSpoofing/main.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #if !defined(_WIN32) #include #endif @@ -59,7 +59,7 @@ static struct option DnsSpoofingOptions[] = struct DnsSpoofStats { int numOfSpoofedDnsRequests; - std::map spoofedHosts; + std::unordered_map spoofedHosts; DnsSpoofStats() : numOfSpoofedDnsRequests(0) {} }; @@ -283,7 +283,7 @@ void onApplicationInterrupted(void* cookie) pcpp::TablePrinter printer(columnNames, columnsWidths); // sort the spoofed hosts map so the most spoofed hosts will be first - // since it's not possible to sort a std::map you must copy it to a std::vector and sort it then + // since it's not possible to sort a std::unordered_map you must copy it to a std::vector and sort it then std::vector > map2vec(args->stats.spoofedHosts.begin(), args->stats.spoofedHosts.end()); std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); diff --git a/Examples/DpdkBridge/Common.h b/Examples/DpdkBridge/Common.h index c7c8a09387..77cc4a44ea 100644 --- a/Examples/DpdkBridge/Common.h +++ b/Examples/DpdkBridge/Common.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include diff --git a/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h b/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h index d465418bfb..ec9056df18 100644 --- a/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h +++ b/Examples/DpdkExample-FilterTraffic/AppWorkerThread.h @@ -21,7 +21,7 @@ class AppWorkerThread : public pcpp::DpdkWorkerThread uint32_t m_CoreId; PacketStats m_Stats; PacketMatchingEngine& m_PacketMatchingEngine; - std::map m_FlowTable; + std::unordered_map m_FlowTable; public: AppWorkerThread(AppWorkerConfig& workerConfig, PacketMatchingEngine& matchingEngine) : diff --git a/Examples/DpdkExample-FilterTraffic/Common.h b/Examples/DpdkExample-FilterTraffic/Common.h index d3c882e96b..15c7d6c1aa 100644 --- a/Examples/DpdkExample-FilterTraffic/Common.h +++ b/Examples/DpdkExample-FilterTraffic/Common.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include @@ -30,7 +30,7 @@ exit(1); \ } while(0) -typedef std::map > InputDataConfig; +typedef std::unordered_map > InputDataConfig; /** diff --git a/Examples/HttpAnalyzer/main.cpp b/Examples/HttpAnalyzer/main.cpp index c4240ee888..35a7bffaf8 100644 --- a/Examples/HttpAnalyzer/main.cpp +++ b/Examples/HttpAnalyzer/main.cpp @@ -239,7 +239,7 @@ void printHostnames(HttpRequestStats& reqStatscollector) pcpp::TablePrinter printer(columnNames, columnsWidths); // sort the hostname count map so the most popular hostnames will be first - // since it's not possible to sort a std::map you must copy it to a std::vector and sort it then + // since it's not possible to sort a std::unordered_map you must copy it to a std::vector and sort it then std::vector > map2vec(reqStatscollector.hostnameCount.begin(), reqStatscollector.hostnameCount.end()); std::sort(map2vec.begin(), map2vec.end(), &hostnameComparer); diff --git a/Examples/IPDefragUtil/main.cpp b/Examples/IPDefragUtil/main.cpp index 4a84bd8854..76d5ff532a 100644 --- a/Examples/IPDefragUtil/main.cpp +++ b/Examples/IPDefragUtil/main.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -96,7 +96,7 @@ void printAppVersion() */ void processPackets(pcpp::IFileReaderDevice* reader, pcpp::IFileWriterDevice* writer, bool filterByBpf, const std::string& bpfFilter, - bool filterByIpID, std::map fragIDs, + bool filterByIpID, std::unordered_map fragIDs, bool copyAllPacketsToOutputFile, DefragStats& stats) { @@ -282,7 +282,7 @@ int main(int argc, char* argv[]) bool filterByBpfFilter = false; std::string bpfFilter = ""; bool filterByFragID = false; - std::map fragIDMap; + std::unordered_map fragIDMap; bool copyAllPacketsToOutputFile = false; diff --git a/Examples/IPFragUtil/main.cpp b/Examples/IPFragUtil/main.cpp index 89fcde8389..98ad9ac8af 100644 --- a/Examples/IPFragUtil/main.cpp +++ b/Examples/IPFragUtil/main.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -234,7 +234,7 @@ void splitIPPacketToFragmentsBySize(pcpp::RawPacket* rawPacket, size_t fragmentS void processPackets(pcpp::IFileReaderDevice* reader, pcpp::IFileWriterDevice* writer, int fragSize, bool filterByBpf, const std::string& bpfFilter, - bool filterByIpID, std::map ipIDs, + bool filterByIpID, std::unordered_map ipIDs, bool copyAllPacketsToOutputFile, FragStats& stats) { @@ -380,7 +380,7 @@ int main(int argc, char* argv[]) bool filterByBpfFilter = false; std::string bpfFilter = ""; bool filterByIpID = false; - std::map ipIDMap; + std::unordered_map ipIDMap; bool copyAllPacketsToOutputFile = false; while((opt = getopt_long(argc, argv, "o:s:d:f:ahv", FragUtilOptions, &optionIndex)) != -1) diff --git a/Examples/PcapSearch/main.cpp b/Examples/PcapSearch/main.cpp index b294cec71e..427ea27de8 100644 --- a/Examples/PcapSearch/main.cpp +++ b/Examples/PcapSearch/main.cpp @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include @@ -201,7 +201,7 @@ int searchPcap(const std::string& pcapFilePath, std::string searchCriteria, std: * search criteria. This method outputs how many directories were searched, how many files were searched and how many packets were matched */ void searchDirectories(const std::string &directory, bool includeSubDirectories, const std::string &searchCriteria, std::ofstream* detailedReportFile, - std::map extensionsToSearch, + std::unordered_map extensionsToSearch, int& totalDirSearched, int& totalFilesSearched, int& totalPacketsFound) { // open the directory @@ -302,7 +302,7 @@ int main(int argc, char* argv[]) std::string detailedReportFileName = ""; - std::map extensionsToSearch; + std::unordered_map extensionsToSearch; // the default (unless set otherwise) is to search in '.pcap' and '.pcapng' extensions extensionsToSearch["pcap"] = true; diff --git a/Examples/PcapSplitter/ConnectionSplitters.h b/Examples/PcapSplitter/ConnectionSplitters.h index 0c326f91ae..e241cbabc6 100644 --- a/Examples/PcapSplitter/ConnectionSplitters.h +++ b/Examples/PcapSplitter/ConnectionSplitters.h @@ -58,7 +58,7 @@ class FiveTupleSplitter : public ValueBasedSplitter // a flow table for saving TCP state per flow. Currently the only data that is saved is whether // the last packet seen on the flow was a TCP SYN packet - std::map m_TcpFlowTable; + std::unordered_map m_TcpFlowTable; /** * A utility method that takes a packet and returns true if it's a TCP SYN packet diff --git a/Examples/PcapSplitter/Splitters.h b/Examples/PcapSplitter/Splitters.h index e4ae645dc8..debaf6f2ac 100644 --- a/Examples/PcapSplitter/Splitters.h +++ b/Examples/PcapSplitter/Splitters.h @@ -9,7 +9,7 @@ #include "UdpLayer.h" #include "DnsLayer.h" #include "PacketUtils.h" -#include +#include #include #include #include @@ -166,9 +166,9 @@ class ValueBasedSplitter : public SplitterWithMaxFiles { protected: // A flow table that keeps track of all flows (a flow is usually identified by 5-tuple) - std::map m_FlowTable; + std::unordered_map m_FlowTable; // a map between the relevant packet value (e.g client-ip) and the file to write the packet to - std::map m_ValueToFileTable; + std::unordered_map m_ValueToFileTable; /** * A protected c'tor for this class that only propagate the maxFiles to its ancestor diff --git a/Examples/PcapSplitter/main.cpp b/Examples/PcapSplitter/main.cpp index 7cd742a3b3..858d7903ca 100644 --- a/Examples/PcapSplitter/main.cpp +++ b/Examples/PcapSplitter/main.cpp @@ -49,7 +49,7 @@ #include #include #include -#include +#include #include #include #include @@ -380,7 +380,7 @@ int main(int argc, char* argv[]) pcpp::RawPacket rawPacket; // prepare a map of file number to IFileWriterDevice - std::map outputFiles; + std::unordered_map outputFiles; // read all packets from input file, for each packet do: while (reader->getNextPacket(rawPacket)) diff --git a/Examples/PfRingExample-FilterTraffic/main.cpp b/Examples/PfRingExample-FilterTraffic/main.cpp index 2cfe44da86..2751008704 100644 --- a/Examples/PfRingExample-FilterTraffic/main.cpp +++ b/Examples/PfRingExample-FilterTraffic/main.cpp @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include @@ -70,7 +70,7 @@ struct CaptureThreadArgs { PacketStats* packetStatArr; PacketMatchingEngine* matchingEngine; - std::map* flowTables; + std::unordered_map* flowTables; pcpp::PfRingDevice* sendPacketsTo; pcpp::PcapFileWriterDevice** pcapWriters; @@ -181,7 +181,7 @@ void packetArrived(pcpp::RawPacket* packets, uint32_t numOfPackets, uint8_t thre // 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(&packet); - std::map::const_iterator iter = args->flowTables[threadId].find(hash); + std::unordered_map::const_iterator iter = args->flowTables[threadId].find(hash); // if packet belongs to an already existing flow if (iter !=args->flowTables[threadId].end() && iter->second) @@ -419,7 +419,7 @@ int main(int argc, char* argv[]) PacketMatchingEngine matchingEngine(srcIPToMatch, dstIPToMatch, srcPortToMatch, dstPortToMatch, protocolToMatch); // create a flow table for each core - std::map flowTables[totalNumOfCores]; + std::unordered_map flowTables[totalNumOfCores]; pcpp::PcapFileWriterDevice** pcapWriters = NULL; diff --git a/Examples/SSLAnalyzer/SSLStatsCollector.h b/Examples/SSLAnalyzer/SSLStatsCollector.h index 20fb942d33..ebb036c73d 100644 --- a/Examples/SSLAnalyzer/SSLStatsCollector.h +++ b/Examples/SSLAnalyzer/SSLStatsCollector.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include "TcpLayer.h" #include "IPv4Layer.h" @@ -35,8 +35,8 @@ struct SSLGeneralStats double sampleTime; // total stats collection time int numOfHandshakeCompleteFlows; // number of flows which handshake was complete int numOfFlowsWithAlerts; // number of flows that were terminated because of SSL/TLS alert - std::map sslVersionCount; // number of flows per SSL/TLS version - std::map sslPortCount; // number of flows per TCP port + std::unordered_map sslVersionCount; // number of flows per SSL/TLS version + std::unordered_map sslPortCount; // number of flows per TCP port void clear() { @@ -67,7 +67,7 @@ struct ClientHelloStats { int numOfMessages; // total number of client-hello messages Rate messageRate; // rate of client-hello messages - std::map serverNameCount; // a map for counting the server names seen in traffic + std::unordered_map serverNameCount; // a map for counting the server names seen in traffic virtual ~ClientHelloStats() {} @@ -87,7 +87,7 @@ struct ServerHelloStats { int numOfMessages; // total number of server-hello messages Rate messageRate; // rate of server-hello messages - std::map cipherSuiteCount; // count of the different chosen cipher-suites + std::unordered_map cipherSuiteCount; // count of the different chosen cipher-suites virtual ~ServerHelloStats() {} @@ -378,7 +378,7 @@ class SSLStatsCollector ServerHelloStats m_ServerHelloStats; ServerHelloStats m_PrevServerHelloStats; - std::map m_FlowTable; + std::unordered_map m_FlowTable; double m_LastCalcRateTime; double m_StartTime; diff --git a/Examples/SSLAnalyzer/main.cpp b/Examples/SSLAnalyzer/main.cpp index 3cc6ad9a70..0bba4d8155 100644 --- a/Examples/SSLAnalyzer/main.cpp +++ b/Examples/SSLAnalyzer/main.cpp @@ -206,7 +206,7 @@ void printServerNames(ClientHelloStats& clientHelloStatsCollector) pcpp::TablePrinter printer(columnNames, columnsWidths); // sort the server-name count map so the most popular names will be first - // since it's not possible to sort a std::map you must copy it to a std::vector and sort it then + // since it's not possible to sort a std::unordered_map you must copy it to a std::vector and sort it then std::vector > map2vec(clientHelloStatsCollector.serverNameCount.begin(), clientHelloStatsCollector.serverNameCount.end()); std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); @@ -223,7 +223,7 @@ void printServerNames(ClientHelloStats& clientHelloStatsCollector) /** * Print SSL record version map */ -void printVersions(std::map& versionMap, const std::string& headline) +void printVersions(std::unordered_map& versionMap, const std::string& headline) { // create the table std::vector columnNames; @@ -235,7 +235,7 @@ void printVersions(std::map& versionMap, const std::string& headl pcpp::TablePrinter printer(columnNames, columnsWidths); // sort the version map so the most popular version will be first - // since it's not possible to sort a std::map you must copy it to a std::vector and sort it then + // since it's not possible to sort a std::unordered_map you must copy it to a std::vector and sort it then std::vector > map2vec(versionMap.begin(), versionMap.end()); std::sort(map2vec.begin(),map2vec.end(), &uint16CountComparer); @@ -264,7 +264,7 @@ void printCipherSuites(ServerHelloStats& serverHelloStats) pcpp::TablePrinter printer(columnNames, columnsWidths); // sort the cipher-suite count map so the most popular names will be first - // since it's not possible to sort a std::map you must copy it to a std::vector and sort it then + // since it's not possible to sort a std::unordered_map you must copy it to a std::vector and sort it then std::vector > map2vec(serverHelloStats.cipherSuiteCount.begin(), serverHelloStats.cipherSuiteCount.end()); std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); @@ -290,7 +290,7 @@ void printPorts(SSLGeneralStats& stats) pcpp::TablePrinter printer(columnNames, columnsWidths); // sort the port count map so the most popular names will be first - // since it's not possible to sort a std::map you must copy it to a std::vector and sort it then + // since it's not possible to sort a std::unordered_map you must copy it to a std::vector and sort it then std::vector > map2vec(stats.sslPortCount.begin(), stats.sslPortCount.end()); std::sort(map2vec.begin(),map2vec.end(), &uint16CountComparer); diff --git a/Examples/TLSFingerprinting/main.cpp b/Examples/TLSFingerprinting/main.cpp index a85fcb238d..683f480a2d 100644 --- a/Examples/TLSFingerprinting/main.cpp +++ b/Examples/TLSFingerprinting/main.cpp @@ -8,7 +8,7 @@ * You can also run `TLSFingerprinting -h` for modes of operation and parameters. */ -#include +#include #include #include #include @@ -222,8 +222,8 @@ struct TLSFingerprintingStats uint64_t numOfPacketsTotal; uint64_t numOfCHPackets; uint64_t numOfSHPackets; - std::map chFingerprints; - std::map shFingerprints; + std::unordered_map chFingerprints; + std::unordered_map shFingerprints; }; struct HandlePacketData @@ -239,7 +239,7 @@ struct HandlePacketData /** * Print cipher-suite map in a table sorted by number of occurrences (most common cipher-suite will be first) */ -void printCommonTLSFingerprints(const std::map& tlsFingerprintMap, int printCountItems) +void printCommonTLSFingerprints(const std::unordered_map& tlsFingerprintMap, int printCountItems) { // create the table std::vector columnNames; @@ -251,7 +251,7 @@ void printCommonTLSFingerprints(const std::map& tlsFinger pcpp::TablePrinter printer(columnNames, columnsWidths); // sort the TLS fingerprint map so the most popular will be first - // since it's not possible to sort a std::map you must copy it to a std::vector and sort it then + // since it's not possible to sort a std::unordered_map you must copy it to a std::vector and sort it then std::vector > map2vec(tlsFingerprintMap.begin(), tlsFingerprintMap.end()); std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer); diff --git a/Packet++/header/IPReassembly.h b/Packet++/header/IPReassembly.h index 64798102e7..a666464e72 100644 --- a/Packet++/header/IPReassembly.h +++ b/Packet++/header/IPReassembly.h @@ -4,7 +4,7 @@ #include "LRUList.h" #include "IpAddress.h" #include "PointerVector.h" -#include +#include /** * @file @@ -448,7 +448,7 @@ namespace pcpp }; LRUList m_PacketLRU; - std::map m_FragmentMap; + std::unordered_map m_FragmentMap; OnFragmentsClean m_OnFragmentsCleanCallback; void* m_CallbackUserCookie; diff --git a/Packet++/header/SomeIpSdLayer.h b/Packet++/header/SomeIpSdLayer.h index e913c409c9..0b8076e42f 100644 --- a/Packet++/header/SomeIpSdLayer.h +++ b/Packet++/header/SomeIpSdLayer.h @@ -6,7 +6,7 @@ #include "SomeIpLayer.h" #include #include -#include +#include #include #include #include diff --git a/Packet++/header/TcpReassembly.h b/Packet++/header/TcpReassembly.h index 8dcf6409de..c6e4e5824b 100644 --- a/Packet++/header/TcpReassembly.h +++ b/Packet++/header/TcpReassembly.h @@ -3,6 +3,7 @@ #include "Packet.h" #include "IpAddress.h" #include "PointerVector.h" +#include #include #include #include @@ -307,7 +308,7 @@ class TcpReassembly /** * The type for storing the connection information */ - typedef std::map ConnectionInfoList; + typedef std::unordered_map ConnectionInfoList; /** * @typedef OnTcpMessageReady @@ -428,7 +429,7 @@ class TcpReassembly TcpReassemblyData() : closed(false), numOfSides(0), prevSide(-1) {} }; - typedef std::map ConnectionList; + typedef std::unordered_map ConnectionList; typedef std::map > CleanupList; OnTcpMessageReady m_OnMessageReadyCallback; diff --git a/Packet++/src/GtpLayer.cpp b/Packet++/src/GtpLayer.cpp index acf099d5d1..32c82471bf 100644 --- a/Packet++/src/GtpLayer.cpp +++ b/Packet++/src/GtpLayer.cpp @@ -1,6 +1,6 @@ #define LOG_MODULE PacketLogModuleGtpLayer -#include +#include #include #include "Logger.h" #include "GtpLayer.h" @@ -441,9 +441,9 @@ GtpV1MessageType GtpV1Layer::getMessageType() const return (GtpV1MessageType)header->messageType; } -std::map createGtpV1MessageTypeToStringMap() +std::unordered_map createGtpV1MessageTypeToStringMap() { - std::map tempMap; + std::unordered_map tempMap; tempMap[0] = "GTPv1 Message Type Unknown"; tempMap[1] = "Echo Request"; @@ -518,7 +518,7 @@ std::map createGtpV1MessageTypeToStringMap() return tempMap; } -const std::map GTPv1MsgTypeToStringMap = createGtpV1MessageTypeToStringMap(); +const std::unordered_map GTPv1MsgTypeToStringMap = createGtpV1MessageTypeToStringMap(); std::string GtpV1Layer::getMessageTypeAsString() const { @@ -529,7 +529,7 @@ std::string GtpV1Layer::getMessageTypeAsString() const return GTPv1MsgTypeToStringMap.find(0)->second; } - std::map::const_iterator iter = GTPv1MsgTypeToStringMap.find(header->messageType); + std::unordered_map::const_iterator iter = GTPv1MsgTypeToStringMap.find(header->messageType); if (iter != GTPv1MsgTypeToStringMap.end()) { return iter->second; diff --git a/Packet++/src/IPReassembly.cpp b/Packet++/src/IPReassembly.cpp index 92608046b6..95bbb48bab 100644 --- a/Packet++/src/IPReassembly.cpp +++ b/Packet++/src/IPReassembly.cpp @@ -310,7 +310,7 @@ Packet* IPReassembly::processPacket(Packet* fragment, ReassemblyStatus& status, IPFragmentData* fragData = nullptr; // check whether this packet already exists in the map - std::map::iterator iter = m_FragmentMap.find(hash); + std::unordered_map::iterator iter = m_FragmentMap.find(hash); // this is the first fragment seen for this packet if (iter == m_FragmentMap.end()) @@ -499,7 +499,7 @@ Packet* IPReassembly::getCurrentPacket(const PacketKey& key) uint32_t hash = key.getHashValue(); // look for this hash value in the map - std::map::iterator iter = m_FragmentMap.find(hash); + std::unordered_map::iterator iter = m_FragmentMap.find(hash); // hash was found if (iter != m_FragmentMap.end()) @@ -556,7 +556,7 @@ void IPReassembly::removePacket(const PacketKey& key) uint32_t hash = key.getHashValue(); // look for this hash value in the map - std::map::iterator iter = m_FragmentMap.find(hash); + std::unordered_map::iterator iter = m_FragmentMap.find(hash); // hash was found if (iter != m_FragmentMap.end()) @@ -578,7 +578,7 @@ void IPReassembly::addNewFragment(uint32_t hash, IPFragmentData* fragData) if (m_PacketLRU.put(hash, &packetRemoved) == 1) // this means LRU list was full and the least recently used item was removed { // remove this item from the fragment map - std::map::iterator iter = m_FragmentMap.find(packetRemoved); + std::unordered_map::iterator iter = m_FragmentMap.find(packetRemoved); IPFragmentData* dataRemoved = iter->second; PacketKey* key = nullptr; diff --git a/Packet++/src/PPPoELayer.cpp b/Packet++/src/PPPoELayer.cpp index 34a6d856b5..b5a7b190f8 100644 --- a/Packet++/src/PPPoELayer.cpp +++ b/Packet++/src/PPPoELayer.cpp @@ -5,7 +5,7 @@ #include "IPv6Layer.h" #include "PayloadLayer.h" #include "Logger.h" -#include +#include #include #include "EndianPortable.h" @@ -94,9 +94,9 @@ void PPPoESessionLayer::setPPPNextProtocol(uint16_t nextProtocol) *pppProto = htobe16(nextProtocol); } -std::map createPPPNextProtoToStringMap() +std::unordered_map createPPPNextProtoToStringMap() { - std::map tempMap; + std::unordered_map tempMap; tempMap[PCPP_PPP_PADDING] = "Padding Protocol"; tempMap[PCPP_PPP_ROHC_SCID] = "ROHC small-CID"; tempMap[PCPP_PPP_ROHC_LCID] = "ROHC large-CID"; @@ -233,11 +233,11 @@ std::map createPPPNextProtoToStringMap() return tempMap; } -const std::map PPPNextProtoToString = createPPPNextProtoToStringMap(); +const std::unordered_map PPPNextProtoToString = createPPPNextProtoToStringMap(); std::string PPPoESessionLayer::toString() const { - std::map::const_iterator iter = PPPNextProtoToString.find(getPPPNextProtocol()); + std::unordered_map::const_iterator iter = PPPNextProtoToString.find(getPPPNextProtocol()); std::string nextProtocol; if (iter != PPPNextProtoToString.end()) nextProtocol = iter->second; diff --git a/Packet++/src/SSLHandshake.cpp b/Packet++/src/SSLHandshake.cpp index 4ffc73d8b6..c32a1abccd 100644 --- a/Packet++/src/SSLHandshake.cpp +++ b/Packet++/src/SSLHandshake.cpp @@ -4,7 +4,7 @@ #include "md5.h" #include #include -#include +#include #include #include #include "Logger.h" @@ -349,9 +349,9 @@ static const SSLCipherSuite Cipher328 = SSLCipherSuite(0x1304, SSL_KEYX_NULL, SS static const SSLCipherSuite Cipher329 = SSLCipherSuite(0x1305, SSL_KEYX_NULL, SSL_AUTH_NULL, SSL_SYM_AES_128_CCM_8, SSL_HASH_SHA256, "TLS_AES_128_CCM_8_SHA256"); -static std::map createCipherSuiteIdToObjectMap() +static std::unordered_map createCipherSuiteIdToObjectMap() { - std::map result; + std::unordered_map result; result[0x0000] = (SSLCipherSuite*)&Cipher1; result[0x0001] = (SSLCipherSuite*)&Cipher2; @@ -699,9 +699,9 @@ static uint32_t hashString(std::string str) return h; } -static std::map createCipherSuiteStringToObjectMap() +static std::unordered_map createCipherSuiteStringToObjectMap() { - std::map result; + std::unordered_map result; result[0x9F180F43] = (SSLCipherSuite*)&Cipher1; result[0x97D9341F] = (SSLCipherSuite*)&Cipher2; @@ -1042,15 +1042,15 @@ std::set createGreaseSet() return std::set(greaseExtensions, greaseExtensions + 16); } -static const std::map CipherSuiteIdToObjectMap = createCipherSuiteIdToObjectMap(); +static const std::unordered_map CipherSuiteIdToObjectMap = createCipherSuiteIdToObjectMap(); -static const std::map CipherSuiteStringToObjectMap = createCipherSuiteStringToObjectMap(); +static const std::unordered_map CipherSuiteStringToObjectMap = createCipherSuiteStringToObjectMap(); static const std::set GreaseSet = createGreaseSet(); SSLCipherSuite* SSLCipherSuite::getCipherSuiteByID(uint16_t id) { - std::map::const_iterator pos = CipherSuiteIdToObjectMap.find(id); + std::unordered_map::const_iterator pos = CipherSuiteIdToObjectMap.find(id); if (pos == CipherSuiteIdToObjectMap.end()) return nullptr; else @@ -1060,7 +1060,7 @@ SSLCipherSuite* SSLCipherSuite::getCipherSuiteByID(uint16_t id) SSLCipherSuite* SSLCipherSuite::getCipherSuiteByName(std::string name) { uint32_t nameHash = hashString(std::move(name)); - std::map::const_iterator pos = CipherSuiteStringToObjectMap.find(nameHash); + std::unordered_map::const_iterator pos = CipherSuiteStringToObjectMap.find(nameHash); if (pos == CipherSuiteStringToObjectMap.end()) return nullptr; else diff --git a/Tests/Pcap++Test/Common/TestUtils.h b/Tests/Pcap++Test/Common/TestUtils.h index 5a5cce332f..94625923ba 100644 --- a/Tests/Pcap++Test/Common/TestUtils.h +++ b/Tests/Pcap++Test/Common/TestUtils.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include "RawPacket.h" #include "Device.h" @@ -46,12 +46,12 @@ uint8_t* readFileIntoBuffer(const std::string &filename, int& bufferLength); template void intersectMaps( - const std::map & left, - const std::map & right, - std::map >& result) + const std::unordered_map & left, + const std::unordered_map & right, + std::unordered_map >& result) { - typename std::map::const_iterator il = left.begin(); - typename std::map::const_iterator ir = right.begin(); + typename std::unordered_map::const_iterator il = left.begin(); + typename std::unordered_map::const_iterator ir = right.begin(); while (il != left.end() && ir != right.end()) { if (il->first < ir->first) diff --git a/Tests/Pcap++Test/Tests/DpdkTests.cpp b/Tests/Pcap++Test/Tests/DpdkTests.cpp index 417b714a65..cf36c6c51c 100644 --- a/Tests/Pcap++Test/Tests/DpdkTests.cpp +++ b/Tests/Pcap++Test/Tests/DpdkTests.cpp @@ -2,7 +2,7 @@ #include "../Common/GlobalTestArgs.h" #include "../Common/TestUtils.h" #include "../Common/PcapFileNamesDef.h" -#include +#include #include #include @@ -33,7 +33,7 @@ struct DpdkPacketData int UdpCount; int HttpCount; - std::map FlowKeys; + std::unordered_map FlowKeys; DpdkPacketData() : ThreadId(-1), PacketCount(0), EthCount(0), ArpCount(0), Ip4Count(0), Ip6Count(0), TcpCount(0), UdpCount(0), HttpCount(0) {} void clear() { ThreadId = -1; PacketCount = 0; EthCount = 0; ArpCount = 0; Ip4Count = 0; Ip6Count = 0; TcpCount = 0; UdpCount = 0; HttpCount = 0; FlowKeys.clear(); } @@ -480,7 +480,7 @@ PTF_TEST_CASE(TestDpdkMultiThread) if ((pcpp::SystemCores::IdToSystemCore[secondCoreId].Mask & coreMask) == 0) continue; - std::map > res; + std::unordered_map > res; intersectMaps(packetDataMultiThread[firstCoreId].FlowKeys, packetDataMultiThread[secondCoreId].FlowKeys, res); PTF_ASSERT_EQUAL(res.size(), 0); if (PTF_IS_VERBOSE_MODE) diff --git a/Tests/Pcap++Test/Tests/PfRingTests.cpp b/Tests/Pcap++Test/Tests/PfRingTests.cpp index 1d4c529995..ed4705f92e 100644 --- a/Tests/Pcap++Test/Tests/PfRingTests.cpp +++ b/Tests/Pcap++Test/Tests/PfRingTests.cpp @@ -25,7 +25,7 @@ struct PfRingPacketData int IpCount; int TcpCount; int UdpCount; - std::map FlowKeys; + std::unordered_map FlowKeys; PfRingPacketData() : ThreadId(-1), PacketCount(0), EthCount(0), IpCount(0), TcpCount(0), UdpCount(0) {} void clear() { ThreadId = -1; PacketCount = 0; EthCount = 0; IpCount = 0; TcpCount = 0; UdpCount = 0; FlowKeys.clear(); } @@ -363,7 +363,7 @@ PTF_TEST_CASE(TestPfRingDeviceMultiThread) { for (int secondCoreId = firstCoreId+1; secondCoreId < totalnumOfCores; secondCoreId++) { - std::map > res; + std::unordered_map > res; intersectMaps(packetDataMultiThread[firstCoreId].FlowKeys, packetDataMultiThread[secondCoreId].FlowKeys, res); PTF_ASSERT_EQUAL(res.size(), 0); } diff --git a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp index 09a9dde472..d2d783c7df 100644 --- a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp +++ b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp @@ -41,7 +41,7 @@ struct TcpReassemblyStats struct TcpReassemblyMultipleConnStats { typedef std::vector FlowKeysList; - typedef std::map Stats; + typedef std::unordered_map Stats; Stats stats; FlowKeysList flowKeysList; From 58234a0e7fe2a99a72d267a2d7138408b620d34c Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 24 Jan 2024 09:53:07 +0900 Subject: [PATCH 2/2] convert back to map if making test failed. --- Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp index d2d783c7df..09a9dde472 100644 --- a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp +++ b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp @@ -41,7 +41,7 @@ struct TcpReassemblyStats struct TcpReassemblyMultipleConnStats { typedef std::vector FlowKeysList; - typedef std::unordered_map Stats; + typedef std::map Stats; Stats stats; FlowKeysList flowKeysList;