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

Run clang-format for Examples and Tutorials #1464

Merged
merged 23 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6990c13
Apply `clang-format` for the Examples and Tutorials
seladb Jun 21, 2024
6a07fdc
Merge branch 'dev' into clang-format-for-examples
seladb Jun 24, 2024
f4226d5
Merge branch 'dev' into clang-format-for-examples
seladb Jun 24, 2024
9e3a0d9
Change `UseTab` to `ForIndentation`
seladb Jun 24, 2024
6c3a05b
Merge branch 'dev' into clang-format-for-examples
seladb Jun 26, 2024
cb62742
Ignore formatting for Arping
seladb Jun 26, 2024
fe1dfb0
Ignore formatting for DpdkExample-FilterTraffic
seladb Jun 26, 2024
9773453
Merge branch 'dev' into clang-format-for-examples
seladb Jul 21, 2024
e5443d1
Merge branch 'dev' into clang-format-for-examples
seladb Jul 29, 2024
a96e653
Fix comment in `HttpStatsCollector.h`
seladb Jul 29, 2024
1cf1743
Fix in `HttpAnalyzer/main.cpp`
seladb Jul 29, 2024
841318c
Fix `PcapSearch/main.cpp`
seladb Jul 29, 2024
8558933
Fix `PcapSplitter/main.cpp`
seladb Jul 29, 2024
0dc6f1a
Fix `PfRingExample-FilterTraffic/main.cpp`
seladb Jul 29, 2024
0089ca2
Fix `SSLAnalyzer/SSLStatsCollector.h`
seladb Jul 29, 2024
7c75c9f
Fix `SSLAnalyzer/main.cpp`
seladb Jul 29, 2024
ead259c
Fix `TLSFingerprinting/main.cpp`
seladb Jul 29, 2024
5c2b4c1
Fix `TcpReassembly/main.cpp`
seladb Jul 29, 2024
c1cbf34
Fix `Tutorial-PacketParsing/main.cpp`
seladb Jul 29, 2024
b757967
Fix `XdpExample-FilterTraffic/main.cpp`
seladb Jul 29, 2024
63bab8d
Merge branch 'dev' into clang-format-for-examples
seladb Jul 29, 2024
d611013
Update clang-format pre-commit hook to go over Common++, Tests and Ex…
seladb Jul 29, 2024
adc538d
clang-format for LiveDeviceTests.cpp
seladb Jul 29, 2024
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
156 changes: 70 additions & 86 deletions Examples/ArpSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,54 @@
#include <Logger.h>
#include <getopt.h>


#define EXIT_WITH_ERROR(reason) do { \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
exit(1); \
} while(0)


static struct option L3FwdOptions[] =
{
{"interface", required_argument, nullptr, 'i'},
{"victim", required_argument, nullptr, 'c'},
{"gateway", required_argument, nullptr, 'g'},
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, nullptr, 'v'},
{nullptr, 0, nullptr, 0}
#define EXIT_WITH_ERROR(reason) \
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
exit(1); \
} while (0)

static struct option L3FwdOptions[] = {
{ "interface", required_argument, nullptr, 'i' },
{ "victim", required_argument, nullptr, 'c' },
{ "gateway", required_argument, nullptr, 'g' },
{ "help", no_argument, nullptr, 'h' },
{ "version", no_argument, nullptr, 'v' },
{ nullptr, 0, nullptr, 0 }
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tigercosmos for some reason clang-format mixes tabs and spaces here, I have no idea why 🤷‍♂️
I tried different things but couldn't make it to work. Maybe you know?

It happens in other files also, like here:

static struct option IcmpFTOptions[] = {

But in others it doesn't happen:

static struct option DNSResolverOptions[] = {

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently in some of the files, changing the order of the arguments fixes the issue, this is very weird 😕

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seladb whenever you think clang-format is stupid, you can turn off the format for that part.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's concerning that we can't rely on clang-format and need to go over the code ourselves. This kind of beats the purpose of a code formatter 😕

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seladb this kind of stuff rarely happens. We are formatting a bunch of code at once so there might be a few weird formatting. But overall, we will get the fruits after all formatting. :)

};


/**
* Print application usage
*/
void printUsage()
{
std::cout << std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-hv] -i interface_ip -c victim_ip -g gateway_ip" << std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -i interface_ip : The IPv4 address of interface to use" << std::endl
<< " -c victim_ip : The IPv4 address of the victim" << std::endl
<< " -g gateway_ip : The IPv4 address of the gateway" << std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< std::endl;
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-hv] -i interface_ip -c victim_ip -g gateway_ip" << std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -i interface_ip : The IPv4 address of interface to use" << std::endl
<< " -c victim_ip : The IPv4 address of the victim" << std::endl
<< " -g gateway_ip : The IPv4 address of the gateway" << std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< std::endl;
}


/**
* Print application version
*/
void printAppVersion()
{
std::cout
<< pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
exit(0);
}


pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDevice* pDevice)
{
// Create an ARP packet and change its fields
Expand All @@ -75,26 +70,22 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
pcpp::MacAddress macSrc = pDevice->getMacAddress();
pcpp::MacAddress macDst(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
pcpp::EthLayer ethLayer(macSrc, macDst, (uint16_t)PCPP_ETHERTYPE_ARP);
pcpp::ArpLayer arpLayer(pcpp::ARP_REQUEST,
pDevice->getMacAddress(),
pDevice->getMacAddress(),
pDevice->getIPv4Address(),
ipAddr);

pcpp::ArpLayer arpLayer(pcpp::ARP_REQUEST, pDevice->getMacAddress(), pDevice->getMacAddress(),
pDevice->getIPv4Address(), ipAddr);

arpRequest.addLayer(&ethLayer);
arpRequest.addLayer(&arpLayer);
arpRequest.computeCalculateFields();

//setup arp reply filter
// setup arp reply filter
pcpp::ArpFilter arpFilter(pcpp::ARP_REPLY);
if (!pDevice->setFilter(arpFilter))
{
std::cerr << "Could not set ARP filter on device" << std::endl;
return pcpp::MacAddress::Zero;
}

//send the arp request and wait for arp reply
// send the arp request and wait for arp reply
pDevice->sendPacket(&arpRequest);
pcpp::RawPacketVector capturedPackets;
pDevice->startCapture(capturedPackets);
Expand All @@ -107,7 +98,7 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
return pcpp::MacAddress::Zero;
}

//parse arp reply and extract the MAC address
// parse arp reply and extract the MAC address
pcpp::Packet arpReply(capturedPackets.front());
if (arpReply.isPacketOfType(pcpp::ARP))
{
Expand All @@ -117,8 +108,8 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
return pcpp::MacAddress::Zero;
}


void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatewayAddr, const pcpp::IPv4Address& victimAddr)
void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatewayAddr,
const pcpp::IPv4Address& victimAddr)
{
pcpp::MacAddress gatewayMacAddr;

Expand Down Expand Up @@ -150,84 +141,77 @@ void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatew
// Create ARP reply for the gateway
pcpp::Packet gwArpReply(500);
pcpp::EthLayer gwEthLayer(deviceMacAddress, gatewayMacAddr, (uint16_t)PCPP_ETHERTYPE_ARP);
pcpp::ArpLayer gwArpLayer(pcpp::ARP_REPLY,
pDevice->getMacAddress(),
gatewayMacAddr,
victimAddr,
gatewayAddr);
pcpp::ArpLayer gwArpLayer(pcpp::ARP_REPLY, pDevice->getMacAddress(), gatewayMacAddr, victimAddr, gatewayAddr);
gwArpReply.addLayer(&gwEthLayer);
gwArpReply.addLayer(&gwArpLayer);
gwArpReply.computeCalculateFields();

// Create ARP reply for the victim
pcpp::Packet victimArpReply(500);
pcpp::EthLayer victimEthLayer(deviceMacAddress, victimMacAddr, (uint16_t)PCPP_ETHERTYPE_ARP);
pcpp::ArpLayer victimArpLayer(pcpp::ARP_REPLY,
pDevice->getMacAddress(),
victimMacAddr,
gatewayAddr,
victimAddr);
pcpp::ArpLayer victimArpLayer(pcpp::ARP_REPLY, pDevice->getMacAddress(), victimMacAddr, gatewayAddr, victimAddr);
victimArpReply.addLayer(&victimEthLayer);
victimArpReply.addLayer(&victimArpLayer);
victimArpReply.computeCalculateFields();

// Send ARP replies to gateway and to victim every 5 seconds
std::cout << "Sending ARP replies to victim and to gateway every 5 seconds..." << std::endl << std::endl;
while(1)
while (1)
{
pDevice->sendPacket(&gwArpReply);
std::cout << "Sent ARP reply: " << gatewayAddr << " [gateway] is at MAC address " << deviceMacAddress << " [me]" << std::endl;
std::cout << "Sent ARP reply: " << gatewayAddr << " [gateway] is at MAC address " << deviceMacAddress << " [me]"
<< std::endl;
pDevice->sendPacket(&victimArpReply);
std::cout << "Sent ARP reply: " << victimAddr << " [victim] is at MAC address " << deviceMacAddress << " [me]" << std::endl;
std::cout << "Sent ARP reply: " << victimAddr << " [victim] is at MAC address " << deviceMacAddress << " [me]"
<< std::endl;
pcpp::multiPlatformSleep(5);
}
}


int main(int argc, char* argv[])
{
pcpp::AppName::init(argc, argv);

//Get arguments from user for incoming interface and outgoing interface
// Get arguments from user for incoming interface and outgoing interface

std::string iface = "", victim = "", gateway = "";
int optionIndex = 0;
int opt = 0;
while((opt = getopt_long(argc, argv, "i:c:g:hv", L3FwdOptions, &optionIndex)) != -1)
while ((opt = getopt_long(argc, argv, "i:c:g:hv", L3FwdOptions, &optionIndex)) != -1)
{
switch (opt)
{
case 0:
break;
case 'i':
iface = optarg;
break;
case 'c':
victim = optarg;
break;
case 'g':
gateway = optarg;
break;
case 'h':
printUsage();
exit(0);
break;
case 'v':
printAppVersion();
break;
default:
printUsage();
exit(-1);
case 0:
break;
case 'i':
iface = optarg;
break;
case 'c':
victim = optarg;
break;
case 'g':
gateway = optarg;
break;
case 'h':
printUsage();
exit(0);
break;
case 'v':
printAppVersion();
break;
default:
printUsage();
exit(-1);
}
}

//Both incoming and outgoing interfaces must be provided by user
if(iface == "" || victim == "" || gateway == "")
// Both incoming and outgoing interfaces must be provided by user
if (iface == "" || victim == "" || gateway == "")
{
EXIT_WITH_ERROR("Please specify both interface IP, victim IP and gateway IP");
}

//Currently supports only IPv4 addresses
// Currently supports only IPv4 addresses
pcpp::IPv4Address ifaceAddr;
pcpp::IPv4Address victimAddr;
pcpp::IPv4Address gatewayAddr;
Expand Down
Loading
Loading