Repository navigation
Expand file tree
/
Copy pathCommon.cpp
More file actions
285 lines (250 loc) · 10.1 KB
/
Copy pathCommon.cpp
File metadata and controls
285 lines (250 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "Common.h"
#include <iostream>
#include <vector>
#include <getopt.h>
#include "EthLayer.h"
#include "IPv4Layer.h"
#include "IcmpLayer.h"
#include "PcapLiveDeviceList.h"
#include "SystemUtils.h"
#include "PcapPlusPlusVersion.h"
#if defined(_WIN32)
# define SEPARATOR '\\'
#else
# define SEPARATOR '/'
#endif
#define DEFAULT_BLOCK_SIZE 1400
static struct option IcmpFTOptions[] = {
{ "interface", required_argument, nullptr, 'i' },
{ "dest-ip", required_argument, nullptr, 'd' },
{ "send-file", required_argument, nullptr, 's' },
{ "receive-file", no_argument, nullptr, 'r' },
{ "speed", required_argument, nullptr, 'p' },
{ "block-size", required_argument, nullptr, 'b' },
{ "list-interfaces", no_argument, nullptr, 'l' },
{ "version", no_argument, nullptr, 'v' },
{ "help", no_argument, nullptr, 'h' },
{ nullptr, no_argument, nullptr, no_argument }
};
#define EXIT_WITH_ERROR_PRINT_USAGE(reason) \
do \
{ \
printUsage(thisSide, otherSide); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
exit(1); \
} while (0)
void printUsage(const std::string& thisSide, const std::string& otherSide)
{
std::string messagesPerSecShort = (thisSide == "pitcher") ? "[-p messages_per_sec] " : "";
std::string messagesPerSecLong = (thisSide == "pitcher") ? " -p messages_per_sec : Set number of messages to "
"be sent per seconds. Default is max possible speed\n"
: "";
std::string thisSideInterface = thisSide + "_interface";
std::string otherSideIP = otherSide + "_ip";
std::cout
<< std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-h] [-v] [-l] -i " << thisSideInterface << " -d " << otherSideIP
<< " -s file_path -r " << messagesPerSecShort << "[-b block_size]" << std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -i " << thisSideInterface
<< " : Use the specified interface. Can be interface name (e.g eth0) or interface IPv4 address" << std::endl
<< " -d " << otherSideIP << " : " << otherSide << " IPv4 address" << std::endl
<< " -s file_path : Send file mode: send file_path to " << otherSide << std::endl
<< " -r : Receive file mode: receive file from " << otherSide << std::endl
<< messagesPerSecLong
<< " -b block_size : Set the size of data chunk sent in each ICMP message (in bytes). Default is "
<< DEFAULT_BLOCK_SIZE << " bytes. Relevant only" << std::endl
<< " in send file mode (when -s is set)" << std::endl
<< " -l : Print the list of interfaces and exit" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< " -h : Display this help message and exit" << 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;
exit(0);
}
/**
* Go over all interfaces and output their names
*/
void listInterfaces()
{
const std::vector<pcpp::PcapLiveDevice*>& devList =
pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
std::cout << std::endl << "Network interfaces:" << std::endl;
for (const auto& dev : devList)
{
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString()
<< std::endl;
}
exit(0);
}
void readCommandLineArguments(int argc, char* argv[], const std::string& thisSide, const std::string& otherSide,
bool& sender, bool& receiver, pcpp::IPv4Address& myIP, pcpp::IPv4Address& otherSideIP,
std::string& fileNameToSend, int& packetsPerSec, size_t& blockSize)
{
std::string interfaceNameOrIP;
std::string otherSideIPAsString;
fileNameToSend.clear();
packetsPerSec = -1;
bool packetsPerSecSet = false;
receiver = false;
sender = false;
blockSize = DEFAULT_BLOCK_SIZE;
bool blockSizeSet = false;
int optionIndex = 0;
int opt = 0;
while ((opt = getopt_long(argc, argv, "i:d:s:rp:b:hvl", IcmpFTOptions, &optionIndex)) != -1)
{
switch (opt)
{
case 0:
break;
case 'i':
interfaceNameOrIP = optarg;
break;
case 'd':
otherSideIPAsString = optarg;
break;
case 's':
fileNameToSend = optarg;
sender = true;
break;
case 'r':
receiver = true;
break;
case 'p':
if (thisSide == "catcher")
EXIT_WITH_ERROR_PRINT_USAGE("Unknown option -p");
packetsPerSec = atoi(optarg);
packetsPerSecSet = true;
break;
case 'b':
blockSize = atoi(optarg);
blockSizeSet = true;
break;
case 'h':
printUsage(thisSide, otherSide);
exit(0);
break;
case 'v':
printAppVersion();
break;
case 'l':
listInterfaces();
break;
default:
printUsage(thisSide, otherSide);
exit(-1);
}
}
// extract my IP address by interface name or IP address string
if (interfaceNameOrIP.empty())
EXIT_WITH_ERROR_PRINT_USAGE("Please provide " << thisSide << " interface name or IP");
pcpp::IPv4Address interfaceIP;
try
{
interfaceIP = pcpp::IPv4Address(interfaceNameOrIP);
myIP = interfaceIP;
}
catch (const std::exception&)
{
pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(interfaceNameOrIP);
if (dev == nullptr)
EXIT_WITH_ERROR_PRINT_USAGE("Cannot find interface by provided name");
myIP = dev->getIPv4Address();
}
// validate pitcher/catcher IP address
if (otherSideIPAsString.empty())
EXIT_WITH_ERROR_PRINT_USAGE("Please provide " << otherSide << " IP address");
pcpp::IPv4Address tempIP;
try
{
tempIP = pcpp::IPv4Address(otherSideIPAsString);
}
catch (const std::exception&)
{
EXIT_WITH_ERROR_PRINT_USAGE("Invalid " << otherSide << " IP address");
}
otherSideIP = tempIP;
// verify only one of sender and receiver switches are set
if (sender && receiver)
EXIT_WITH_ERROR_PRINT_USAGE("Cannot set both send file mode (-s) and receive file mode (-r) switches");
if (!sender && !receiver)
EXIT_WITH_ERROR_PRINT_USAGE("Must set either send file mode (-s) or receive file mode (-r) switches");
// cannot set block size if in receiving file mode
if (!sender && blockSizeSet)
EXIT_WITH_ERROR_PRINT_USAGE("Setting block size (-b switch) is relevant for sending files only");
// validate block size
if (blockSize < 1 || blockSize > 1464)
EXIT_WITH_ERROR_PRINT_USAGE("Block size must be a positive integer lower or equal to 1464 bytes (which is the "
"maximum size for a standard packet)");
// validate packets per sec
if (packetsPerSecSet && packetsPerSec < 1)
EXIT_WITH_ERROR_PRINT_USAGE("message_per_sec must be a positive value greater or equal to 1");
}
bool sendIcmpMessage(pcpp::PcapLiveDevice* dev, pcpp::MacAddress srcMacAddr, pcpp::MacAddress dstMacAddr,
pcpp::IPv4Address srcIPAddr, pcpp::IPv4Address dstIPAddr, size_t icmpMsgId, uint64_t msgType,
uint8_t* data, size_t dataLen, bool sendRequest)
{
// a static variable that holds an incrementing IP ID
static uint16_t ipID = 0x1234;
// keep IP ID in the range of 0x1234-0xfff0
if (ipID == 0xfff0)
ipID = 0x1234;
// create the different layers
// Eth first
pcpp::EthLayer ethLayer(srcMacAddr, dstMacAddr, PCPP_ETHERTYPE_IP);
// then IPv4 (IPv6 is not supported)
pcpp::IPv4Layer ipLayer(srcIPAddr, dstIPAddr);
ipLayer.getIPv4Header()->timeToLive = 128;
// set and increment the IP ID
ipLayer.getIPv4Header()->ipId = pcpp::hostToNet16(ipID++);
// then ICMP
pcpp::IcmpLayer icmpLayer;
if (sendRequest && icmpLayer.setEchoRequestData(icmpMsgId, 0, msgType, data, dataLen) == nullptr)
EXIT_WITH_ERROR("Cannot set ICMP echo request data");
else if (!sendRequest && icmpLayer.setEchoReplyData(icmpMsgId, 0, msgType, data, dataLen) == nullptr)
EXIT_WITH_ERROR("Cannot set ICMP echo response data");
// create an new packet and add all layers to it
pcpp::Packet packet;
packet.addLayer(ðLayer);
packet.addLayer(&ipLayer);
packet.addLayer(&icmpLayer);
packet.computeCalculateFields();
// send the packet through the device
return dev->sendPacket(&packet);
}
bool sendIcmpRequest(pcpp::PcapLiveDevice* dev, pcpp::MacAddress srcMacAddr, const pcpp::MacAddress dstMacAddr,
pcpp::IPv4Address srcIPAddr, const pcpp::IPv4Address dstIPAddr, size_t icmpMsgId, uint64_t msgType,
uint8_t* data, size_t dataLen)
{
return sendIcmpMessage(dev, srcMacAddr, dstMacAddr, srcIPAddr, dstIPAddr, icmpMsgId, msgType, data, dataLen, true);
}
bool sendIcmpResponse(pcpp::PcapLiveDevice* dev, pcpp::MacAddress srcMacAddr, pcpp::MacAddress dstMacAddr,
pcpp::IPv4Address srcIPAddr, pcpp::IPv4Address dstIPAddr, size_t icmpMsgId, uint64_t msgType,
uint8_t* data, size_t dataLen)
{
return sendIcmpMessage(dev, srcMacAddr, dstMacAddr, srcIPAddr, dstIPAddr, icmpMsgId, msgType, data, dataLen, false);
}
std::string getFileNameFromPath(const std::string& filePath)
{
// find the last "\\" or "/" (depends on the os) - where path ends and filename starts
size_t i = filePath.rfind(SEPARATOR, filePath.length());
if (i != std::string::npos)
{
// extract filename from path
return filePath.substr(i + 1, filePath.length() - i);
}
return filePath;
}