Skip to content

Commit

Permalink
Port tutorial codes to C++11, fixing issue #977 partially (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
merttozer authored Apr 18, 2024
1 parent 0c75603 commit a0dac50
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 93 deletions.
28 changes: 7 additions & 21 deletions Examples/Tutorials/Tutorial-DpdkL2Fwd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
#include "SystemUtils.h"
#include "DpdkDeviceList.h"
#include "TablePrinter.h"

#include "WorkerThread.h"


#define MBUF_POOL_SIZE 16*1024-1
#define DEVICE_ID_1 0
#define DEVICE_ID_2 1

#define COLLECT_STATS_EVERY_SEC 2


// Keep running flag
bool keepRunning = true;

Expand All @@ -32,19 +28,8 @@ void printStats(pcpp::DpdkDevice* rxDevice, pcpp::DpdkDevice* txDevice)
rxDevice->getStatistics(rxStats);
txDevice->getStatistics(txStats);

std::vector<std::string> columnNames;
columnNames.push_back(" ");
columnNames.push_back("Total Packets");
columnNames.push_back("Packets/sec");
columnNames.push_back("Bytes");
columnNames.push_back("Bits/sec");

std::vector<int> columnLengths;
columnLengths.push_back(10);
columnLengths.push_back(15);
columnLengths.push_back(15);
columnLengths.push_back(15);
columnLengths.push_back(15);
std::vector<std::string> columnNames = {" ", "Total Packets", "Packets/sec", "Bytes", "Bits/sec"};
std::vector<int> columnLengths = {10, 15, 15, 15, 15};

pcpp::TablePrinter printer(columnNames, columnLengths);

Expand All @@ -69,14 +54,14 @@ int main(int argc, char* argv[])

// Find DPDK devices
pcpp::DpdkDevice* device1 = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(DEVICE_ID_1);
if (device1 == NULL)
if (device1 == nullptr)
{
std::cerr << "Cannot find device1 with port '" << DEVICE_ID_1 << "'" << std::endl;
return 1;
}

pcpp::DpdkDevice* device2 = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(DEVICE_ID_2);
if (device2 == NULL)
if (device2 == nullptr)
{
std::cerr << "Cannot find device2 with port '" << DEVICE_ID_2 << "'" << std::endl;
return 1;
Expand All @@ -97,8 +82,9 @@ int main(int argc, char* argv[])

// Create worker threads
std::vector<pcpp::DpdkWorkerThread*> workers;
workers.push_back(new L2FwdWorkerThread(device1, device2));
workers.push_back(new L2FwdWorkerThread(device2, device1));
// Constructs a DpdkWorkerThread* directly within the vector's storage
workers.emplace_back(new L2FwdWorkerThread(device1, device2));
workers.emplace_back(new L2FwdWorkerThread(device2, device1));

// Create core mask - use core 1 and 2 for the two threads
int workersCoreMask = 0;
Expand Down
62 changes: 30 additions & 32 deletions Examples/Tutorials/Tutorial-LiveTraffic/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <algorithm>
#include "stdlib.h"
#include "PcapLiveDeviceList.h"
#include "SystemUtils.h"
Expand All @@ -7,25 +8,23 @@
*/
struct PacketStats
{
int ethPacketCount;
int ipv4PacketCount;
int ipv6PacketCount;
int tcpPacketCount;
int udpPacketCount;
int dnsPacketCount;
int httpPacketCount;
int sslPacketCount;
int ethPacketCount = 0;
int ipv4PacketCount = 0;
int ipv6PacketCount = 0;
int tcpPacketCount = 0;
int udpPacketCount = 0;
int dnsPacketCount = 0;
int httpPacketCount = 0;
int sslPacketCount = 0;


/**
* Clear all stats
*/
void clear() { ethPacketCount = 0; ipv4PacketCount = 0; ipv6PacketCount = 0; tcpPacketCount = 0; udpPacketCount = 0; tcpPacketCount = 0; dnsPacketCount = 0; httpPacketCount = 0; sslPacketCount = 0; }
void clear() { ethPacketCount = ipv4PacketCount = ipv6PacketCount = tcpPacketCount = udpPacketCount = dnsPacketCount = httpPacketCount = sslPacketCount = 0; }

/**
* C'tor
*/
PacketStats() { clear(); }
// Constructor is optional here since the members are already initialized
PacketStats() = default;

/**
* Collect stats from a packet
Expand Down Expand Up @@ -74,7 +73,7 @@ struct PacketStats
static void onPacketArrives(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie)
{
// extract the stats object form the cookie
PacketStats* stats = (PacketStats*)cookie;
auto* stats = static_cast<PacketStats*>(cookie);

// parsed the raw packet
pcpp::Packet parsedPacket(packet);
Expand All @@ -89,8 +88,8 @@ static void onPacketArrives(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev,
*/
static bool onPacketArrivesBlockingMode(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie)
{
// extract the stats object form the cookie
PacketStats* stats = (PacketStats*)cookie;
// extract the stats object from the cookie
auto* stats = static_cast<PacketStats*>(cookie);

// parsed the raw packet
pcpp::Packet parsedPacket(packet);
Expand All @@ -112,7 +111,7 @@ int main(int argc, char* argv[])
std::string interfaceIPAddr = "10.0.0.1";

// find the interface by IP address
pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIPAddr);
auto* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIPAddr);
if (dev == nullptr)
{
std::cerr << "Cannot find interface with IPv4 address of '" << interfaceIPAddr << "'" << std::endl;
Expand All @@ -131,8 +130,10 @@ int main(int argc, char* argv[])
<< " Default gateway: " << dev->getDefaultGateway() << std::endl // get default gateway
<< " Interface MTU: " << dev->getMtu() << std::endl; // get interface MTU

if (dev->getDnsServers().size() > 0)
std::cout << " DNS server: " << dev->getDnsServers().at(0) << std::endl;
if (!dev->getDnsServers().empty())
{
std::cout << " DNS server: " << dev->getDnsServers().front() << std::endl;
}

// open the device before start capturing/sending packets
if (!dev->open())
Expand Down Expand Up @@ -185,12 +186,9 @@ int main(int argc, char* argv[])
dev->stopCapture();

// go over the packet vector and feed all packets to the stats object
for (pcpp::RawPacketVector::ConstVectorIterator iter = packetVec.begin(); iter != packetVec.end(); iter++)
for (const auto& packet : packetVec)
{
// parse raw packet
pcpp::Packet parsedPacket(*iter);

// feed packet to the stats object
pcpp::Packet parsedPacket(packet);
stats.consumePacket(parsedPacket);
}

Expand Down Expand Up @@ -225,15 +223,16 @@ int main(int argc, char* argv[])
std::cout << std::endl << "Sending " << packetVec.size() << " packets one by one..." << std::endl;

// go over the vector of packets and send them one by one
for (pcpp::RawPacketVector::ConstVectorIterator iter = packetVec.begin(); iter != packetVec.end(); iter++)
bool allSent = std::all_of(packetVec.begin(), packetVec.end(), [dev](pcpp::RawPacket* packet) {
return dev->sendPacket(*packet);
});

if (!allSent)
{
// send the packet. If fails exit the application
if (!dev->sendPacket(**iter))
{
std::cerr << "Couldn't send packet" << std::endl;
return 1;
}
std::cerr << "Couldn't send packet" << std::endl;
return 1;
}

std::cout << packetVec.size() << " packets sent" << std::endl;


Expand Down Expand Up @@ -280,7 +279,6 @@ int main(int argc, char* argv[])
std::cout << "Results:" << std::endl;
stats.printToConsole();


// close the device before application ends
dev->close();
}
27 changes: 16 additions & 11 deletions Examples/Tutorials/Tutorial-PacketCraftAndEdit/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <memory>
#include "stdlib.h"
#include "SystemUtils.h"
#include "Packet.h"
Expand All @@ -18,7 +19,7 @@ int main(int argc, char* argv[])

// use the IFileReaderDevice interface to automatically identify file type (pcap/pcap-ng)
// and create an interface instance that both readers implement
pcpp::IFileReaderDevice* reader = pcpp::IFileReaderDevice::getReader("1_http_packet.pcap");
std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader("1_http_packet.pcap"));

// verify that a reader interface was indeed created
if (reader == nullptr)
Expand Down Expand Up @@ -49,12 +50,12 @@ int main(int argc, char* argv[])
pcpp::Packet parsedPacket(&rawPacket);

// now let's get the Ethernet layer
pcpp::EthLayer* ethernetLayer = parsedPacket.getLayerOfType<pcpp::EthLayer>();
auto* ethernetLayer = parsedPacket.getLayerOfType<pcpp::EthLayer>();
// change the source dest MAC address
ethernetLayer->setDestMac(pcpp::MacAddress("aa:bb:cc:dd:ee:ff"));

// let's get the IPv4 layer
pcpp::IPv4Layer* ipLayer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
auto* ipLayer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
// change source IP address
ipLayer->setSrcIPv4Address(pcpp::IPv4Address("1.1.1.1"));
// change IP ID
Expand All @@ -63,7 +64,7 @@ int main(int argc, char* argv[])
ipLayer->getIPv4Header()->timeToLive = 12;

// let's get the TCP layer
pcpp::TcpLayer* tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
auto* tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
// change source port
tcpLayer->getTcpHeader()->portSrc = pcpp::hostToNet16(12345);
// add URG flag
Expand All @@ -72,7 +73,7 @@ int main(int argc, char* argv[])
tcpLayer->addTcpOptionAfter(pcpp::TcpOptionBuilder(pcpp::TCPOPT_MSS, (uint16_t)1460));

// let's get the HTTP layer
pcpp::HttpRequestLayer* httpRequestLayer = parsedPacket.getLayerOfType<pcpp::HttpRequestLayer>();
auto* httpRequestLayer = parsedPacket.getLayerOfType<pcpp::HttpRequestLayer>();
// change the request method from GET to TRACE
httpRequestLayer->getFirstLine()->setMethod(pcpp::HttpRequestLayer::HttpTRACE);
// change host to www.google.com
Expand All @@ -97,9 +98,11 @@ int main(int argc, char* argv[])

// write the modified packet to a pcap file
pcpp::PcapFileWriterDevice writer("1_modified_packet.pcap");
writer.open();
writer.writePacket(*(parsedPacket.getRawPacket()));
writer.close();
if (writer.open())
{
writer.writePacket(*(parsedPacket.getRawPacket()));
writer.close();
}


// Packet Creation
Expand Down Expand Up @@ -134,7 +137,9 @@ int main(int argc, char* argv[])

// write the new packet to a pcap file
pcpp::PcapFileWriterDevice writer2("1_new_packet.pcap");
writer2.open();
writer2.writePacket(*(newPacket.getRawPacket()));
writer2.close();
if (writer2.open())
{
writer2.writePacket(*(newPacket.getRawPacket()));
writer2.close();
}
}
41 changes: 17 additions & 24 deletions Examples/Tutorials/Tutorial-PacketParsing/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <memory>
#include "stdlib.h"
#include "SystemUtils.h"
#include "Packet.h"
Expand Down Expand Up @@ -28,24 +29,16 @@ std::string getProtocolTypeAsString(pcpp::ProtocolType protocolType)

std::string printTcpFlags(pcpp::TcpLayer* tcpLayer)
{
std::string result = "";
if (tcpLayer->getTcpHeader()->synFlag == 1)
result += "SYN ";
if (tcpLayer->getTcpHeader()->ackFlag == 1)
result += "ACK ";
if (tcpLayer->getTcpHeader()->pshFlag == 1)
result += "PSH ";
if (tcpLayer->getTcpHeader()->cwrFlag == 1)
result += "CWR ";
if (tcpLayer->getTcpHeader()->urgFlag == 1)
result += "URG ";
if (tcpLayer->getTcpHeader()->eceFlag == 1)
result += "ECE ";
if (tcpLayer->getTcpHeader()->rstFlag == 1)
result += "RST ";
if (tcpLayer->getTcpHeader()->finFlag == 1)
result += "FIN ";

std::string result;
auto* tcpHeader = tcpLayer->getTcpHeader();
if (tcpHeader->synFlag) result += "SYN ";
if (tcpHeader->ackFlag) result += "ACK ";
if (tcpHeader->pshFlag) result += "PSH ";
if (tcpHeader->cwrFlag) result += "CWR ";
if (tcpHeader->urgFlag) result += "URG ";
if (tcpHeader->eceFlag) result += "ECE ";
if (tcpHeader->rstFlag) result += "RST ";
if (tcpHeader->finFlag) result += "FIN ";
return result;
}

Expand Down Expand Up @@ -79,7 +72,7 @@ int main(int argc, char* argv[])
{
// use the IFileReaderDevice interface to automatically identify file type (pcap/pcap-ng)
// and create an interface instance that both readers implement
pcpp::IFileReaderDevice* reader = pcpp::IFileReaderDevice::getReader("1_http_packet.pcap");
std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader("1_http_packet.pcap"));

// verify that a reader interface was indeed created
if (reader == nullptr)
Expand Down Expand Up @@ -110,7 +103,7 @@ int main(int argc, char* argv[])
pcpp::Packet parsedPacket(&rawPacket);

// first let's go over the layers one by one and find out its type, its total length, its header length and its payload length
for (pcpp::Layer* curLayer = parsedPacket.getFirstLayer(); curLayer != nullptr; curLayer = curLayer->getNextLayer())
for (auto* curLayer = parsedPacket.getFirstLayer(); curLayer != nullptr; curLayer = curLayer->getNextLayer())
{
std::cout
<< "Layer type: " << getProtocolTypeAsString(curLayer->getProtocol()) << "; " // get layer type
Expand All @@ -121,7 +114,7 @@ int main(int argc, char* argv[])
}

// now let's get the Ethernet layer
pcpp::EthLayer* ethernetLayer = parsedPacket.getLayerOfType<pcpp::EthLayer>();
auto* ethernetLayer = parsedPacket.getLayerOfType<pcpp::EthLayer>();
if (ethernetLayer == nullptr)
{
std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl;
Expand All @@ -135,7 +128,7 @@ int main(int argc, char* argv[])
<< "Ether type = 0x" << std::hex << pcpp::netToHost16(ethernetLayer->getEthHeader()->etherType) << std::endl;

// let's get the IPv4 layer
pcpp::IPv4Layer* ipLayer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
auto* ipLayer = parsedPacket.getLayerOfType<pcpp::IPv4Layer>();
if (ipLayer == nullptr)
{
std::cerr << "Something went wrong, couldn't find IPv4 layer" << std::endl;
Expand All @@ -150,7 +143,7 @@ int main(int argc, char* argv[])
<< "TTL: " << std::dec << (int)ipLayer->getIPv4Header()->timeToLive << std::endl;

// let's get the TCP layer
pcpp::TcpLayer* tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
auto* tcpLayer = parsedPacket.getLayerOfType<pcpp::TcpLayer>();
if (tcpLayer == nullptr)
{
std::cerr << "Something went wrong, couldn't find TCP layer" << std::endl;
Expand All @@ -172,7 +165,7 @@ int main(int argc, char* argv[])
std::cout << std::endl;

// let's get the HTTP request layer
pcpp::HttpRequestLayer* httpRequestLayer = parsedPacket.getLayerOfType<pcpp::HttpRequestLayer>();
auto* httpRequestLayer = parsedPacket.getLayerOfType<pcpp::HttpRequestLayer>();
if (httpRequestLayer == nullptr)
{
std::cerr << "Something went wrong, couldn't find HTTP request layer" << std::endl;
Expand Down
Loading

0 comments on commit a0dac50

Please sign in to comment.