forked from KTZ-Goel/PacketSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharp.cpp
More file actions
executable file
·312 lines (275 loc) · 9.49 KB
/
arp.cpp
File metadata and controls
executable file
·312 lines (275 loc) · 9.49 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "arp.h"
#include <arpa/inet.h>
#include <stdint.h>
#include <stdio.h>
#include "colors.h"
#include "modelcolumnindexes.h"
#include "shared.h"
#include "tags.h"
void handle_arp(QList<QStandardItem*>* row, const struct sniff_arp* arp)
{
static const char* opcodeStrings[] = { "ARP REQUEST", "ARP REPLY",
"RARP REQUEST", "RARP RAPLY",
"DRARP REQUEST", "DRARP REPLY",
"DRARP ERROR", "INARP REQUEST",
"INARP REPLY" };
uint16_t hardwareType = ntohs(arp->ah_hardware);
uint16_t protocolType = ntohs(arp->ah_protocol);
uint16_t opcode = ntohs(arp->ah_opcode);
uint8_t* data = ((uint8_t*)arp) + 8;
printf(CYAN " ARP:\n" RESET);
if (opcode >= 1 && opcode <= 9) {
printf(" Operation ---- %s\n", opcodeStrings[opcode - 1]);
} else {
printf(YELLOW
" ARP operation [%u] not implemented yet.\n" RESET,
opcode);
return;
}
switch (hardwareType) {
case ARP_HARDWARE_TYPE_ETHERNET: {
printf(" Hardware ----- Ethernet\n");
break;
}
default: {
printf(YELLOW
" ARP hardware type [%u] not implemented yet.\n" RESET,
hardwareType);
return;
}
}
switch (protocolType) {
case ARP_PROTOCOL_TYPE_IPV4: {
printf(" Protocol ----- IPv4\n");
break;
}
default: {
printf(YELLOW
" ARP protocol type [%u] not implemented yet.\n" RESET,
protocolType);
return;
}
}
printf(" Hardware len - %u\n", arp->ah_hlen);
printf(" Protocol len - %u\n", arp->ah_plen);
uint8_t* senderip = data;
uint8_t* destinationHardware = data;
uint8_t* destinationip = data;
switch (arp->ah_hlen) {
case ARP_MAC_LENGTH: {
printf(" Sender MAC --- %02X:%02X:%02X:%02X:%02X:%02X\n",
data[0],
data[1],
data[2],
data[3],
data[4],
data[5]);
senderip += ARP_MAC_LENGTH;
break;
}
default: {
printf(YELLOW
" Hardware length [%u] not implemented yet.\n" RESET,
arp->ah_hlen);
return;
}
}
switch (arp->ah_plen) {
case ARP_IPv4_LENGTH: {
char sourceipBuffer[INET_ADDRSTRLEN];
char destinationipBuffer[INET_ADDRSTRLEN];
// 源IP
inet_ntop(AF_INET,
senderip,
sourceipBuffer,
sizeof(sourceipBuffer)); // 将网络字节顺序排列的IP转换为字符串IP
printf(" Sender IP ---- %s\n", sourceipBuffer);
row->append(new QStandardItem(QString(sourceipBuffer)));
// 计算指向目标的MAC地址和目标ip地址的指针
destinationHardware += arp->ah_hlen + ARP_IPv4_LENGTH;
destinationip += arp->ah_hlen + ARP_IPv4_LENGTH + arp->ah_hlen;
// 目的IP
inet_ntop(
AF_INET,
destinationip,
destinationipBuffer,
sizeof(destinationipBuffer)); // 将网络字节顺序排列的IP转换为字符串IP
printf(" Dest. MAC ---- %02X:%02X:%02X:%02X:%02X:%02X\n",
destinationHardware[0],
destinationHardware[1],
destinationHardware[2],
destinationHardware[3],
destinationHardware[4],
destinationHardware[5]);
printf(" Dest. IP ----- %s\n", destinationipBuffer);
row->append(new QStandardItem(QString(destinationipBuffer)));
row->append(new QStandardItem("ARP"));
QString infoString;
if (opcode == ARP_OPCODE_REQUEST) {
infoString = QString("Who has %1, tell %2")
.arg(QString(destinationipBuffer))
.arg(QString(sourceipBuffer));
} else if (opcode == ARP_OPCODE_REPLY) {
char sourceMacBuffer[18];
snprintf(sourceMacBuffer,
sizeof(sourceMacBuffer),
"%02X:%02X:%02X:%02X:%02X:%02X",
data[0],
data[1],
data[2],
data[3],
data[4],
data[5]);
infoString = QString("%1 is at %2")
.arg(QString(sourceipBuffer))
.arg(QString(sourceMacBuffer));
} else {
infoString = QString("Unknown opcode: %1").arg(opcode);
}
row->append(new QStandardItem(infoString));
break;
}
default: {
printf(YELLOW
" Network length [%u] not implemented yet.\n" RESET,
arp->ah_plen);
return;
}
}
}
void handle_arp_fill(QString* infoStr, const struct sniff_arp* arp)
{
static const char* opcodeStrings[] = { "ARP REQUEST", "ARP REPLY",
"RARP REQUEST", "RARP RAPLY",
"DRARP REQUEST", "DRARP REPLY",
"DRARP ERROR", "INARP REQUEST",
"INARP REPLY" };
uint16_t hardwareType = ntohs(arp->ah_hardware);
uint16_t protocolType = ntohs(arp->ah_protocol);
uint16_t opcode = ntohs(arp->ah_opcode);
uint8_t* data = ((uint8_t*)arp) + 8;
infoStr->append(HEADER_TAG_START "ARP:" HEADER_TAG_END NEWLINE);
if (opcode >= 1 && opcode <= 9) {
infoStr->append(QString(TAB BOLD_TAG_START "Operation" BOLD_TAG_END
" ---- %1 (%2)" NEWLINE)
.arg(opcode)
.arg(opcodeStrings[opcode - 1]));
} else {
infoStr->append(
QString(TAB YELLOW_FONT_START
"ARP operation [%1] not implemented yet." YELLOW_FONT_END NEWLINE)
.arg(opcode));
return;
}
switch (hardwareType) {
case ARP_HARDWARE_TYPE_ETHERNET: {
infoStr->append(QString(TAB BOLD_TAG_START "Hardware" BOLD_TAG_END
" ----- %1 (Ethernet)" NEWLINE)
.arg(hardwareType));
break;
}
default: {
infoStr->append(
QString(
TAB YELLOW_FONT_START
"ARP hardware type [%1] not implemented yet." YELLOW_FONT_END NEWLINE)
.arg(hardwareType));
return;
}
}
switch (protocolType) {
case ARP_PROTOCOL_TYPE_IPV4: {
infoStr->append(TAB BOLD_TAG_START "Protocol" BOLD_TAG_END
" ----- IPv4" NEWLINE);
break;
}
default: {
infoStr->append(
QString(
TAB YELLOW_FONT_START
"ARP protocol type [%1] not implemented yet." YELLOW_FONT_END NEWLINE)
.arg(protocolType));
return;
}
}
infoStr->append(
QString(TAB BOLD_TAG_START "Hardware len" BOLD_TAG_END " - %1" NEWLINE)
.arg(arp->ah_hlen));
infoStr->append(
QString(TAB BOLD_TAG_START "Protocol len" BOLD_TAG_END " - %1" NEWLINE)
.arg(arp->ah_plen));
uint8_t* senderHardware = data;
uint8_t* senderip = data;
uint8_t* destinationHardware = data;
uint8_t* destinationip = data;
switch (arp->ah_hlen) {
case ARP_MAC_LENGTH: {
char senderMACbuffer[MAC_ADDRESS_STRLEN];
snprintf(senderMACbuffer,
sizeof(senderMACbuffer),
"%02X:%02X:%02X:%02X:%02X:%02X",
senderHardware[0],
senderHardware[1],
senderHardware[2],
senderHardware[3],
senderHardware[4],
senderHardware[5]);
infoStr->append(
QString(TAB BOLD_TAG_START "Sender MAC" BOLD_TAG_END " --- %1" NEWLINE)
.arg(senderMACbuffer));
senderip += ARP_MAC_LENGTH;
break;
}
default: {
infoStr->append(
QString(
TAB YELLOW_FONT_START
"Hardware length [%1] not implemented yet." YELLOW_FONT_END NEWLINE)
.arg(arp->ah_hlen));
return;
}
}
switch (arp->ah_plen) {
case ARP_IPv4_LENGTH: {
char sourceipBuffer[INET_ADDRSTRLEN];
char destinationipBuffer[INET_ADDRSTRLEN];
char destinationMACbuffer[MAC_ADDRESS_STRLEN];
inet_ntop(AF_INET,
senderip,
sourceipBuffer,
sizeof(sourceipBuffer)); // 将网络字节顺序排列的IP转换为字符串IP
infoStr->append(
QString(TAB BOLD_TAG_START "Sender IP" BOLD_TAG_END " ---- %1" NEWLINE)
.arg(sourceipBuffer));
destinationHardware += arp->ah_hlen + ARP_IPv4_LENGTH;
destinationip += arp->ah_hlen + ARP_IPv4_LENGTH + arp->ah_hlen;
snprintf(destinationMACbuffer,
sizeof(destinationMACbuffer),
"%02X:%02X:%02X:%02X:%02X:%02X",
destinationHardware[0],
destinationHardware[1],
destinationHardware[2],
destinationHardware[3],
destinationHardware[4],
destinationHardware[5]);
infoStr->append(
QString(TAB BOLD_TAG_START "Dest. MAC" BOLD_TAG_END " ---- %1" NEWLINE)
.arg(destinationMACbuffer));
inet_ntop(AF_INET,
destinationip,
destinationipBuffer,
sizeof(destinationipBuffer));
infoStr->append(
QString(TAB BOLD_TAG_START "Dest. IP" BOLD_TAG_END " ----- %1" NEWLINE)
.arg(destinationipBuffer));
break;
}
default: {
infoStr->append(
QString(TAB YELLOW_FONT_START
"Network length [%1] not implemented yet." YELLOW_FONT_END)
.arg(arp->ah_plen));
return;
}
}
}