summaryrefslogtreecommitdiff
path: root/pcapplusplus.cpp
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2023-09-28 06:23:26 +0200
committerToni Uhlig <matzeton@googlemail.com>2023-09-29 06:23:26 +0200
commita739ae878b909412bb098a27f5c5fe35a7702b27 (patch)
tree19919ebc1136ff9b0883d5ace53d36077a4fe091 /pcapplusplus.cpp
parentbee123e9811d53d4f852839b7dd3e6bfdba5245e (diff)
Added IP address randomizer.
* added "Save Selection" menu action Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'pcapplusplus.cpp')
-rw-r--r--pcapplusplus.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/pcapplusplus.cpp b/pcapplusplus.cpp
index 743e845..004db71 100644
--- a/pcapplusplus.cpp
+++ b/pcapplusplus.cpp
@@ -1,9 +1,13 @@
#include "pcapplusplus.h"
+#include <chrono>
#include <EthLayer.h>
#include <exception>
#include <IPLayer.h>
+#include <IPv4Layer.h>
+#include <IPv6Layer.h>
#include <TcpLayer.h>
+#include <random>
#include <tuple>
#include <UdpLayer.h>
@@ -117,6 +121,46 @@ std::vector<pcpp::Packet>::iterator PcapPlusPlus::parsedPacketsEnd()
return parsedPackets.end();
}
+bool PcapPlusPlus::randomizeIp(size_t index, bool isSourceIp)
+{
+ std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
+ auto retval = false;
+ pcpp::RawPacket rawPacket(getRawPacket(index));
+ pcpp::Packet parsedPacket(&rawPacket);
+ auto * ip4Layer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
+ auto * ip6Layer = parsedPacket.getLayerOfType<pcpp::IPv6Layer>();
+
+ if (ip4Layer) {
+ std::uniform_int_distribution<unsigned int> ip4Distribution(0, 0xFFFFFFFF);
+
+ if (isSourceIp)
+ ip4Layer->setSrcIPv4Address(pcpp::IPv4Address(ip4Distribution(generator)));
+ else
+ ip4Layer->setDstIPv4Address(pcpp::IPv4Address(ip4Distribution(generator)));
+
+ retval = true;
+ }
+
+ if (ip6Layer) {
+ std::uniform_int_distribution<unsigned long long int> ip6Distribution(0, 0xFFFFFFFFFFFFFFFF);
+ unsigned long long int newIp6Addr[2] { ip6Distribution(generator), ip6Distribution(generator) };
+
+ if (isSourceIp)
+ ip6Layer->setSrcIPv6Address(reinterpret_cast<uint8_t *>(newIp6Addr));
+ else
+ ip6Layer->setDstIPv6Address(reinterpret_cast<uint8_t *>(newIp6Addr));
+
+ retval = true;
+ }
+
+ rawPackets.erase(rawPackets.cbegin() + index);
+ rawPackets.insert(rawPackets.cbegin() + index, rawPacket);
+ parsedPackets.erase(parsedPackets.cbegin() + index);
+ parsedPackets.insert(parsedPackets.cbegin() + index, parsedPacket);
+
+ return retval;
+}
+
bool PcapPlusPlus::getPcapStatistics(pcpp::IFileDevice::PcapStats & stats)
{
if (reader) {