-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPacket.cpp
720 lines (645 loc) · 13.3 KB
/
Packet.cpp
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "stdafx.h"
#include "Packet.h"
#include "pcap.h"
Packet::Packet()
{
ethh = NULL;
iph = NULL;
arph = NULL;
icmph = NULL;
udph = NULL;
tcph = NULL;
dnsh = NULL;
dhcph = NULL;
httpmsg = NULL;
pkt_data = NULL;
num = -1;
header = NULL;
}
Packet::Packet(const Packet &p)
{
ethh = NULL;
iph = NULL;
arph = NULL;
icmph = NULL;
udph = NULL;
tcph = NULL;
dnsh = NULL;
dhcph = NULL;
httpmsg = NULL;
if (!p.isEmpty())
{
u_int caplen = p.header->caplen;
pkt_data = (u_char*)malloc( caplen );
memcpy(pkt_data, p.pkt_data, caplen);
header = (struct pcap_pkthdr *)malloc(sizeof( *(p.header) ));
memcpy(header, p.header, sizeof(*(p.header)));
num = p.num;
decodeEthernet();
}
else
{
pkt_data = NULL;
header = NULL;
num = -1;
}
}
Packet::Packet(const struct pcap_pkthdr *header,const u_char *pkt_data, const u_short &packetNum)
{
ethh = NULL;
iph = NULL;
arph = NULL;
icmph = NULL;
udph = NULL;
tcph = NULL;
dnsh = NULL;
dhcph = NULL;
httpmsg = NULL;
num = packetNum;
if (pkt_data != NULL && header != NULL)
{
this->pkt_data = (u_char*)malloc(header->caplen);
memcpy(this->pkt_data, pkt_data, header->caplen);
this->header = (struct pcap_pkthdr *)malloc(sizeof(*header));
memcpy(this->header, header, sizeof(*header));
decodeEthernet();
}
else
{
this->pkt_data = NULL;
this->header = NULL;
}
}
/**
* @brief assignment operator function
* @param p data pack
* @return instance itself
*/
Packet & Packet::operator=(const Packet & p)
{
if (this == &p)
{
return *this;
}
ethh = NULL;
iph = NULL;
arph = NULL;
icmph = NULL;
udph = NULL;
tcph = NULL;
dnsh = NULL;
dhcph = NULL;
if (!p.isEmpty())
{
u_int caplen = p.header->caplen;
if (pkt_data == NULL)
{
pkt_data = (u_char*)malloc(caplen);
}
memcpy(pkt_data, p.pkt_data, caplen);
if (header == NULL)
{
header = (struct pcap_pkthdr *)malloc(sizeof(*(p.header)));
}
memcpy(header, p.header, sizeof(*(p.header)));
num = p.num;
decodeEthernet();
}
else
{
pkt_data = NULL;
header = NULL;
httpmsg = NULL;
num = -1;
}
return *this;
}
Packet::~Packet()
{
ethh = NULL;
iph = NULL;
arph = NULL;
icmph = NULL;
udph = NULL;
tcph = NULL;
dnsh = NULL;
dhcph = NULL;
httpmsg = NULL;
num = -1;
free(pkt_data);
pkt_data = NULL;
free(header);
header = NULL;
protocol.Empty();
}
/**
* @brief Determine whether the data packet is empty
* @param -
* @return true pkt_data or header is empty false pkt_data and header are both
*/
bool Packet::isEmpty() const
{
if (pkt_data == NULL || header == NULL)
{
return true;
}
return false;
}
/**
* @brief Parse the Ethernet frame, save it with the member variable ethh, and call the next parser based on the eth_type value
* @param -
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeEthernet()
{
if (isEmpty())
{
return -1;
}
protocol = "Ethernet";
ethh = (Ethernet_Header*)pkt_data;
switch (ntohs(ethh->eth_type))
{
case ETHERNET_TYPE_IP:
decodeIP(pkt_data + ETHERNET_HEADER_LENGTH);
break;
case ETHERNET_TYPE_ARP:
decodeARP(pkt_data + ETHERNET_HEADER_LENGTH);
break;
default:
break;
}
return 0;
}
/**
* @brief Parse the IP packet header, save it with the member variable iph, and call the next parser based on the protocol value
* @param L2payload Pointer to IP packet
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeIP(u_char * L2payload)
{
if (L2payload == NULL)
{
return -1;
}
protocol = "IPv4";
iph = (IP_Header*)(L2payload);
u_short IPHeaderLen = (iph->ver_headerlen & 0x0f) * 4;
switch (iph->protocol)
{
case PROTOCOL_ICMP:
decodeICMP(L2payload + IPHeaderLen);
break;
case PROTOCOL_TCP:
decodeTCP(L2payload + IPHeaderLen);
break;
case PROTOCOL_UDP:
decodeUDP(L2payload + IPHeaderLen);
break;
default:
break;
}
return 0;
}
/**
* @brief Parse the ARP message header and save it with the member variable arph
* @param L2payload Pointer to ARP packet
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeARP(u_char * L2payload)
{
if (L2payload == NULL)
{
return -1;
}
protocol = "ARP";
arph = (ARP_Header*)(L2payload);
return 0;
}
/**
* @brief Parse the ICMP message header and save it with the member variable icmph
* @param L2payload Pointer to ICMP message
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeICMP(u_char * L3payload)
{
if (L3payload == NULL)
{
return -1;
}
protocol = "ICMP";
icmph = (ICMP_Header*)(L3payload);
return 0;
}
/**
* @brief Parse the TCP segment header, save it with the member variable tcph, and select the next parser based on the source and destination ports.
* @param L3payload Pointer to TCP segment
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeTCP(u_char * L3payload)
{
if (L3payload == NULL)
{
return -1;
}
protocol = "TCP";
tcph = (TCP_Header*)(L3payload);
u_short TCPHeaderLen = (ntohs(tcph->headerlen_rsv_flags) >> 12) * 4;
if (ntohs(tcph->srcport) == PORT_DNS || ntohs(tcph->dstport) == PORT_DNS)
{
decodeDNS(L3payload + TCPHeaderLen);
}
else if (ntohs(tcph->srcport) == PORT_HTTP || ntohs(tcph->dstport) == PORT_HTTP)
{
int HTTPMsgLen = getL4PayloadLength();
if (HTTPMsgLen > 0)
{
decodeHTTP(L3payload + TCPHeaderLen);
}
}
return 0;
}
/**
* @brief Parse the UDP user datagram header, save it with the member variable udph, and select the next parser based on the source and destination ports.
* @param L2payload Pointer to UDP user datagram
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeUDP(u_char *L3payload)
{
if (L3payload == NULL)
{
return -1;
}
protocol = "UDP";
udph = (UDP_Header*)(L3payload);
if (ntohs(udph->srcport) == PORT_DNS || ntohs(udph->dstport) == PORT_DNS)
{
decodeDNS(L3payload + UDP_HEADER_LENGTH);
}
else if ((ntohs(udph->srcport) == PORT_DHCP_CLIENT && ntohs(udph->dstport) == PORT_DHCP_SERVER) || (ntohs(udph->srcport) == PORT_DHCP_SERVER && ntohs(udph->dstport) == PORT_DHCP_CLIENT))
{
decodeDHCP(L3payload + UDP_HEADER_LENGTH);
}
return 0;
}
/**
* @brief Parse the DNS message header and save it with the member variable dnsh
* @param L4payload ָPointer to DNS message
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeDNS(u_char * L4payload)
{
if (L4payload == NULL)
{
return -1;
}
protocol = "DNS";
dnsh = (DNS_Header*)(L4payload);
return 0;
}
/**
* @brief Parse the DHCP message header and save it with the member variable dhcph
* @param L4payload Pointer to DHCP message
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeDHCP(u_char * L4payload)
{
if (L4payload == NULL)
{
return -1;
}
protocol = "DHCP";
dhcph = (DHCP_Header*)L4payload;
return 0;
}
/**
* @brief Parse the HTTP message header and save it with the member variable httpmsg
* @param L4payload Pointer to httpmsg message
* @return 0 Indicates parsing success -1 indicates parsing failure
*/
int Packet::decodeHTTP(u_char * L4payload)
{
if (L4payload == NULL)
{
return -1;
}
protocol = "HTTP";
httpmsg = L4payload;
return 0;
}
/**
* @brief Get IP header length
* @param -
* @return IP header length
*/
int Packet::getIPHeaderLegnth() const
{
if (iph == NULL)
return -1;
else
return (iph->ver_headerlen & 0x0F) * 4;
}
/**
* @brief Get the original value of the IP header length
* @param -
* @return Original value of IP header length -1 IP header is empty
*/
int Packet::getIPHeaderLengthRaw() const
{
if (iph == NULL)
return -1;
else
return (iph->ver_headerlen & 0x0F);
}
/**
* @brief Get IP header flag
* @param -
* @return IP header flag -1 IP header is empty
*/
int Packet::getIPFlags() const
{
if (iph == NULL)
return -1;
else
return ntohs(iph->flags_offset) >> 13;
}
/**
* @brief Get the IP header flag DF bit
* @param -
* @return IP header flag DF bit -1 IP header is empty
*/
int Packet::getIPFlagDF() const
{
if (iph == NULL)
return -1;
else
return (ntohs(iph->flags_offset) >> 13) & 0x0001;
}
/**
* @brief Get the IP header flag MF bit
* @param -
* @return IP header flag MF bit -1 IP header is empty
*/
int Packet::getIPFlagsMF() const
{
if (iph == NULL)
return -1;
else
return (ntohs(iph->flags_offset) >> 14) & 0x0001;
}
/**
* @brief Get IP header slice offset
* @param -
* @return IP header piece offset -1 IP header is empty
*/
int Packet::getIPOffset() const
{
if (iph == NULL)
return -1;
else
return ntohs(iph->flags_offset) & 0x1FFF;
}
/**
* @brief Get the Id in the Other field of the ICMP header
* @param -
* @return Id -1 in the Other field of the ICMP header The ICMP header is empty
*/
u_short Packet::getICMPID() const
{
if (icmph == NULL)
return -1;
else
return (u_short)(ntohl(icmph->others) >> 16);
}
/**
* @brief Get the Seq in the Other field of the ICMP header
* @param -
* @return Seq -1 in the Other field of the ICMP header is empty.
*/
u_short Packet::getICMPSeq() const
{
if (icmph == NULL)
return -1;
else
return (u_short)(ntohl(icmph->others) & 0x0000FFFF);
}
/**
* @brief Get TCP header length
* @param -
* @return TCP header length -1 TCP header is empty
*/
int Packet::getTCPHeaderLength() const
{
if (tcph == NULL)
return -1;
else
return (ntohs(tcph->headerlen_rsv_flags) >> 12) * 4;
}
/**
* @brief Get the original value of TCP header length
* @param -
* @return Original value of TCP header length -1 TCP header is empty
*/
int Packet::getTCPHeaderLengthRaw() const
{
if (tcph == NULL)
return -1;
else
return (ntohs(tcph->headerlen_rsv_flags) >> 12) ;
}
/**
* @brief Get TCP header flags
* @param -
* @return TCP header flag -1 TCP header
*/
u_short Packet::getTCPFlags() const
{
if (tcph == NULL)
return -1;
else
return ntohs(tcph->headerlen_rsv_flags) & 0x0FFF;
}
/**
* @brief Get TCP header flags
* @param -
* @return TCP header flag -1 TCP header is empty
*/
int Packet::getTCPFlagsURG() const
{
if (tcph == NULL)
return -1;
else
return (ntohs(tcph->headerlen_rsv_flags) >> 5) & 0x0001;
}
/**
* @brief Get TCP header flag ACK
* @param -
* @return TCP header flag ACK -1 TCP header is empty
*/
int Packet::getTCPFlagsACK() const
{
if (tcph == NULL)
return -1;
else
return (ntohs(tcph->headerlen_rsv_flags) >> 4) & 0x0001;
}
/**
* @brief Get the TCP header flag PSH
* @param -
* @return TCP header flag PSH -1 TCP header is empty
*/
int Packet::getTCPFlagsPSH() const
{
if (tcph == NULL)
return -1;
else
return (ntohs(tcph->headerlen_rsv_flags) >> 3) & 0x0001;
}
/**
* @brief Get TCP header flag RST
* @param -
* @return TCP header flag RST -1 TCP header is empty
*/
int Packet::getTCPFlagsRST() const
{
if (tcph == NULL)
return -1;
else
return (ntohs(tcph->headerlen_rsv_flags) >> 2) & 0x0001;
}
/**
* @brief Get the TCP header flag SYN
* @param -
* @return TCP header flag SYN -1 TCP header is empty
*/
int Packet::getTCPFlagsSYN() const
{
if (tcph == NULL)
return -1;
else
return (ntohs(tcph->headerlen_rsv_flags) >> 1) & 0x0001;
}
/**
* @brief Get the TCP header flag FIN
* @param -
* @return TCP header flag FIN -1 TCP header is empty
*/
int Packet::getTCPFlagsFIN() const
{
if (tcph == NULL)
return -1;
else
return ntohs(tcph->headerlen_rsv_flags) & 0x0001;
}
/**
* @brief Get the TCP header flag FIN
* @param -
* @return TCP header flag FIN -1 TCP header is empty
*/
int Packet::getL4PayloadLength() const
{
if (iph == NULL || tcph == NULL)
{
return 0;
}
int IPTotalLen = ntohs(iph->totallen);
int IPHeaderLen = (iph->ver_headerlen & 0x0F) * 4;
int TCPHeaderLen = (ntohs(tcph->headerlen_rsv_flags) >> 12 ) * 4;
return IPTotalLen - IPHeaderLen - TCPHeaderLen ;
}
/**
* @brief Get the DNS header flag QR
* @param -
* @return DNS header flag QR -1 DNS header is empty
*/
int Packet::getDNSFlagsQR() const
{
if (dnsh == NULL)
return -1;
else
return dnsh->flags >> 15;
}
/**
* @brief Get the DNS header flag OPCODE
* @param -
* @return DNS header flag OPCODE -1 DNS header is empty
*/
int Packet::getDNSFlagsOPCODE() const
{
if (dnsh == NULL)
return -1;
else
return (ntohs(dnsh->flags) >> 11) & 0x000F;
}
/**
* @brief Get DNS header flag AA
* @param -
* @return DNS header flag AA -1 DNS header is empty
*/
int Packet::getDNSFlagsAA() const
{
if (dnsh == NULL)
return -1;
else
return (ntohs(dnsh->flags) >> 10) & 0x0001;
}
/**
* @brief Get the DNS header flag TC
* @param -
* @return DNS header flag TC -1 DNS header is empty
*/
int Packet::getDNSFlagsTC() const
{
if (dnsh == NULL)
return -1;
else
return (ntohs(dnsh->flags) >> 9) & 0x0001;
}
/**
* @brief Get the DNS header flag RD
* @param -
* @return DNS header flag RD -1 DNS header is empty
*/
int Packet::getDNSFlagsRD() const
{
if (dnsh == NULL)
return -1;
else
return (ntohs(dnsh->flags) >> 8) & 0x0001;
}
/**
* @brief Get the DNS header flag RA
* @param -
* @return DNS header flag RA -1 DNS header is empty
*/
int Packet::getDNSFlagsRA() const
{
if (dnsh == NULL)
return -1;
else
return (ntohs(dnsh->flags) >> 7) & 0x0001;
}
/**
* @brief Get DNS header flag Z
* @param -
* @return DNS header flag Z -1 DNS header is empty
*/
int Packet::getDNSFlagsZ() const
{
if (dnsh == NULL)
return -1;
else
return (ntohs(dnsh->flags) >> 4) & 0x0007;
}
/**
* @brief Get the DNS header flag RCODE
* @param -
* @return DNS header flag RCODE -1 DNS header is empty
*/
int Packet::getDNSFlagsRCODE() const
{
if (dnsh == NULL)
return -1;
else
return ntohs(dnsh->flags) & 0x000F;
}