Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::unordered_map instead of std::map #1285

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Common++/header/LRUList.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <map>
#include <unordered_map>
#include <list>

#if __cplusplus > 199711L || _MSC_VER >= 1800
Expand Down Expand Up @@ -29,7 +29,7 @@ namespace pcpp
public:

typedef typename std::list<T>::iterator ListIterator;
typedef typename std::map<T, ListIterator>::iterator MapIterator;
typedef typename std::unordered_map<T, ListIterator>::iterator MapIterator;

/**
* A c'tor for this class
Expand Down Expand Up @@ -125,7 +125,7 @@ namespace pcpp

private:
std::list<T> m_CacheItemsList;
std::map<T, ListIterator> m_CacheItemsMap;
std::unordered_map<T, ListIterator> m_CacheItemsMap;
size_t m_MaxSize;
};

Expand Down
6 changes: 3 additions & 3 deletions Examples/DnsSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <algorithm>
#include <sstream>
#include <utility>
#include <map>
#include <unordered_map>
#if !defined(_WIN32)
#include <errno.h>
#endif
Expand Down Expand Up @@ -59,7 +59,7 @@ static struct option DnsSpoofingOptions[] =
struct DnsSpoofStats
{
int numOfSpoofedDnsRequests;
std::map<std::string, int> spoofedHosts;
std::unordered_map<std::string, int> spoofedHosts;

DnsSpoofStats() : numOfSpoofedDnsRequests(0) {}
};
Expand Down Expand Up @@ -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<std::pair<std::string, int> > map2vec(args->stats.spoofedHosts.begin(), args->stats.spoofedHosts.end());
std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer);

Expand Down
2 changes: 1 addition & 1 deletion Examples/DpdkBridge/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <SystemUtils.h>

#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion Examples/DpdkExample-FilterTraffic/AppWorkerThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AppWorkerThread : public pcpp::DpdkWorkerThread
uint32_t m_CoreId;
PacketStats m_Stats;
PacketMatchingEngine& m_PacketMatchingEngine;
std::map<uint32_t, bool> m_FlowTable;
std::unordered_map<uint32_t, bool> m_FlowTable;

public:
AppWorkerThread(AppWorkerConfig& workerConfig, PacketMatchingEngine& matchingEngine) :
Expand Down
4 changes: 2 additions & 2 deletions Examples/DpdkExample-FilterTraffic/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <SystemUtils.h>

#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <iostream>
Expand All @@ -30,7 +30,7 @@
exit(1); \
} while(0)

typedef std::map<pcpp::DpdkDevice*, std::vector<int> > InputDataConfig;
typedef std::unordered_map<pcpp::DpdkDevice*, std::vector<int> > InputDataConfig;


/**
Expand Down
2 changes: 1 addition & 1 deletion Examples/HttpAnalyzer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::pair<std::string, int> > map2vec(reqStatscollector.hostnameCount.begin(), reqStatscollector.hostnameCount.end());
std::sort(map2vec.begin(), map2vec.end(), &hostnameComparer);

Expand Down
6 changes: 3 additions & 3 deletions Examples/IPDefragUtil/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include <map>
#include <unordered_map>
#include <sstream>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -96,7 +96,7 @@ void printAppVersion()
*/
void processPackets(pcpp::IFileReaderDevice* reader, pcpp::IFileWriterDevice* writer,
bool filterByBpf, const std::string& bpfFilter,
bool filterByIpID, std::map<uint32_t, bool> fragIDs,
bool filterByIpID, std::unordered_map<uint32_t, bool> fragIDs,
bool copyAllPacketsToOutputFile,
DefragStats& stats)
{
Expand Down Expand Up @@ -282,7 +282,7 @@ int main(int argc, char* argv[])
bool filterByBpfFilter = false;
std::string bpfFilter = "";
bool filterByFragID = false;
std::map<uint32_t, bool> fragIDMap;
std::unordered_map<uint32_t, bool> fragIDMap;
bool copyAllPacketsToOutputFile = false;


Expand Down
6 changes: 3 additions & 3 deletions Examples/IPFragUtil/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include <map>
#include <unordered_map>
#include <sstream>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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<uint16_t, bool> ipIDs,
bool filterByIpID, std::unordered_map<uint16_t, bool> ipIDs,
bool copyAllPacketsToOutputFile,
FragStats& stats)
{
Expand Down Expand Up @@ -380,7 +380,7 @@ int main(int argc, char* argv[])
bool filterByBpfFilter = false;
std::string bpfFilter = "";
bool filterByIpID = false;
std::map<uint16_t, bool> ipIDMap;
std::unordered_map<uint16_t, bool> ipIDMap;
bool copyAllPacketsToOutputFile = false;

while((opt = getopt_long(argc, argv, "o:s:d:f:ahv", FragUtilOptions, &optionIndex)) != -1)
Expand Down
6 changes: 3 additions & 3 deletions Examples/PcapSearch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <dirent.h>
#include <utility>
#include <vector>
#include <map>
#include <unordered_map>
#include <Logger.h>
#include <PcapPlusPlusVersion.h>
#include <SystemUtils.h>
Expand Down Expand Up @@ -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<std::string, bool> extensionsToSearch,
std::unordered_map<std::string, bool> extensionsToSearch,
int& totalDirSearched, int& totalFilesSearched, int& totalPacketsFound)
{
// open the directory
Expand Down Expand Up @@ -302,7 +302,7 @@ int main(int argc, char* argv[])

std::string detailedReportFileName = "";

std::map<std::string, bool> extensionsToSearch;
std::unordered_map<std::string, bool> extensionsToSearch;

// the default (unless set otherwise) is to search in '.pcap' and '.pcapng' extensions
extensionsToSearch["pcap"] = true;
Expand Down
2 changes: 1 addition & 1 deletion Examples/PcapSplitter/ConnectionSplitters.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t, bool> m_TcpFlowTable;
std::unordered_map<uint32_t, bool> m_TcpFlowTable;

/**
* A utility method that takes a packet and returns true if it's a TCP SYN packet
Expand Down
6 changes: 3 additions & 3 deletions Examples/PcapSplitter/Splitters.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "UdpLayer.h"
#include "DnsLayer.h"
#include "PacketUtils.h"
#include <map>
#include <unordered_map>
#include <algorithm>
#include <iomanip>
#include <sstream>
Expand Down Expand Up @@ -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<uint32_t, int> m_FlowTable;
std::unordered_map<uint32_t, int> m_FlowTable;
// a map between the relevant packet value (e.g client-ip) and the file to write the packet to
std::map<uint32_t, int> m_ValueToFileTable;
std::unordered_map<uint32_t, int> m_ValueToFileTable;

/**
* A protected c'tor for this class that only propagate the maxFiles to its ancestor
Expand Down
4 changes: 2 additions & 2 deletions Examples/PcapSplitter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include <sstream>
#include <string>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <RawPacket.h>
#include <Packet.h>
#include <PcapFileDevice.h>
Expand Down Expand Up @@ -380,7 +380,7 @@ int main(int argc, char* argv[])
pcpp::RawPacket rawPacket;

// prepare a map of file number to IFileWriterDevice
std::map<int, pcpp::IFileWriterDevice*> outputFiles;
std::unordered_map<int, pcpp::IFileWriterDevice*> outputFiles;

// read all packets from input file, for each packet do:
while (reader->getNextPacket(rawPacket))
Expand Down
8 changes: 4 additions & 4 deletions Examples/PfRingExample-FilterTraffic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <stdlib.h>
#include <vector>
#include <getopt.h>
#include <map>
#include <unordered_map>
#include <sstream>
#include <unistd.h>

Expand Down Expand Up @@ -70,7 +70,7 @@ struct CaptureThreadArgs
{
PacketStats* packetStatArr;
PacketMatchingEngine* matchingEngine;
std::map<uint32_t, bool>* flowTables;
std::unordered_map<uint32_t, bool>* flowTables;
pcpp::PfRingDevice* sendPacketsTo;
pcpp::PcapFileWriterDevice** pcapWriters;

Expand Down Expand Up @@ -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<uint32_t, bool>::const_iterator iter = args->flowTables[threadId].find(hash);
std::unordered_map<uint32_t, bool>::const_iterator iter = args->flowTables[threadId].find(hash);

// if packet belongs to an already existing flow
if (iter !=args->flowTables[threadId].end() && iter->second)
Expand Down Expand Up @@ -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<uint32_t, bool> flowTables[totalNumOfCores];
std::unordered_map<uint32_t, bool> flowTables[totalNumOfCores];

pcpp::PcapFileWriterDevice** pcapWriters = NULL;

Expand Down
12 changes: 6 additions & 6 deletions Examples/SSLAnalyzer/SSLStatsCollector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <map>
#include <unordered_map>
#include <sstream>
#include "TcpLayer.h"
#include "IPv4Layer.h"
Expand Down Expand Up @@ -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<uint16_t, int> sslVersionCount; // number of flows per SSL/TLS version
std::map<uint16_t, int> sslPortCount; // number of flows per TCP port
std::unordered_map<uint16_t, int> sslVersionCount; // number of flows per SSL/TLS version
std::unordered_map<uint16_t, int> sslPortCount; // number of flows per TCP port

void clear()
{
Expand Down Expand Up @@ -67,7 +67,7 @@ struct ClientHelloStats
{
int numOfMessages; // total number of client-hello messages
Rate messageRate; // rate of client-hello messages
std::map<std::string, int> serverNameCount; // a map for counting the server names seen in traffic
std::unordered_map<std::string, int> serverNameCount; // a map for counting the server names seen in traffic

virtual ~ClientHelloStats() {}

Expand All @@ -87,7 +87,7 @@ struct ServerHelloStats
{
int numOfMessages; // total number of server-hello messages
Rate messageRate; // rate of server-hello messages
std::map<std::string, int> cipherSuiteCount; // count of the different chosen cipher-suites
std::unordered_map<std::string, int> cipherSuiteCount; // count of the different chosen cipher-suites

virtual ~ServerHelloStats() {}

Expand Down Expand Up @@ -378,7 +378,7 @@ class SSLStatsCollector
ServerHelloStats m_ServerHelloStats;
ServerHelloStats m_PrevServerHelloStats;

std::map<uint32_t, SSLFlowData> m_FlowTable;
std::unordered_map<uint32_t, SSLFlowData> m_FlowTable;

double m_LastCalcRateTime;
double m_StartTime;
Expand Down
10 changes: 5 additions & 5 deletions Examples/SSLAnalyzer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::pair<std::string, int> > map2vec(clientHelloStatsCollector.serverNameCount.begin(), clientHelloStatsCollector.serverNameCount.end());
std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer);

Expand All @@ -223,7 +223,7 @@ void printServerNames(ClientHelloStats& clientHelloStatsCollector)
/**
* Print SSL record version map
*/
void printVersions(std::map<uint16_t, int>& versionMap, const std::string& headline)
void printVersions(std::unordered_map<uint16_t, int>& versionMap, const std::string& headline)
{
// create the table
std::vector<std::string> columnNames;
Expand All @@ -235,7 +235,7 @@ void printVersions(std::map<uint16_t, int>& 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<std::pair<uint16_t, int> > map2vec(versionMap.begin(), versionMap.end());
std::sort(map2vec.begin(),map2vec.end(), &uint16CountComparer);

Expand Down Expand Up @@ -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<std::pair<std::string, int> > map2vec(serverHelloStats.cipherSuiteCount.begin(), serverHelloStats.cipherSuiteCount.end());
std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer);

Expand All @@ -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<std::pair<uint16_t, int> > map2vec(stats.sslPortCount.begin(), stats.sslPortCount.end());
std::sort(map2vec.begin(),map2vec.end(), &uint16CountComparer);

Expand Down
10 changes: 5 additions & 5 deletions Examples/TLSFingerprinting/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* You can also run `TLSFingerprinting -h` for modes of operation and parameters.
*/

#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <iostream>
Expand Down Expand Up @@ -222,8 +222,8 @@ struct TLSFingerprintingStats
uint64_t numOfPacketsTotal;
uint64_t numOfCHPackets;
uint64_t numOfSHPackets;
std::map<std::string, uint64_t> chFingerprints;
std::map<std::string, uint64_t> shFingerprints;
std::unordered_map<std::string, uint64_t> chFingerprints;
std::unordered_map<std::string, uint64_t> shFingerprints;
};

struct HandlePacketData
Expand All @@ -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<std::string, uint64_t>& tlsFingerprintMap, int printCountItems)
void printCommonTLSFingerprints(const std::unordered_map<std::string, uint64_t>& tlsFingerprintMap, int printCountItems)
{
// create the table
std::vector<std::string> columnNames;
Expand All @@ -251,7 +251,7 @@ void printCommonTLSFingerprints(const std::map<std::string, uint64_t>& 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<std::pair<std::string, int> > map2vec(tlsFingerprintMap.begin(), tlsFingerprintMap.end());
std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer);

Expand Down
Loading
Loading