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

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
BasedOnStyle: Microsoft
UseTab: AlignWithSpaces
UseTab: ForIndentation
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 I think we should change this, it looks nicer, what do you think?
If you agree I can open a PR for Common++ as well

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't have a strong opinion about it. it's fine to me.

Copy link
Owner Author

Choose a reason for hiding this comment

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

ok, I'll open a separate PR for this change

AllowShortFunctionsOnASingleLine: None
NamespaceIndentation: All
SortIncludes: false
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
Loading