Skip to content

Commit

Permalink
Run clang-format for Examples and Tutorials (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
seladb authored Jul 30, 2024
1 parent 11eb010 commit 09f479f
Show file tree
Hide file tree
Showing 49 changed files with 4,752 additions and 4,568 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: clang-format
args: ["--style=file"] # Use the .clang-format file for configuration
files: ^Common\+\+/.*\.(cpp|h)$
files: ^(Common\+\+|Tests|Examples)/.*\.(cpp|h)$
- id: cppcheck
args: ["--std=c++11", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr", "--force"]
- repo: https://github.com/codespell-project/codespell
Expand Down
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' },
{ "version", no_argument, nullptr, 'v' },
{ "help", no_argument, nullptr, 'h' },
{ nullptr, 0, nullptr, 0 }
};


/**
* 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

0 comments on commit 09f479f

Please sign in to comment.