This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPacket.cpp
110 lines (86 loc) · 2.54 KB
/
CPacket.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
#include "CPacket.h"
#include <iostream>
#include <netinet/if_ether.h>
void CPacket::parseIPv4Protocol(unsigned char *buffer, ssize_t total_len)
{
this->m_pIPHeader = reinterpret_cast<struct iphdr *>(buffer);
//Passer le header IP
buffer += (this->m_pIPHeader->ihl * 4);
switch(this->m_pIPHeader->protocol)
{
case 1:
this->setICMPHeader(reinterpret_cast<struct icmphdr *>(buffer));
break;
case 6: {
this->setTCPHeader(reinterpret_cast<struct tcphdr *>(buffer));
ssize_t data_len = total_len - sizeof(struct ethhdr) - (this->m_pIPHeader->ihl * 4) - (this->getTCPHeader()->doff * 4);
if (data_len == 0)
return;
//Passer le header TCP
buffer = buffer + (this->getTCPHeader()->doff * 4);
// print_bytes(buffer, data_len);
this->dnsParser.parseData(buffer, data_len);
this->detectorHTTP.parseData(buffer, data_len);
break;
}
case 17:
this->setUDPHeader(reinterpret_cast<struct udphdr *>(buffer));
this->dnsParser.parseData(buffer + sizeof(struct udphdr), ntohs(this->getUDPHeader()->len) - sizeof(struct udphdr));
this->detectorHTTP.parseData(buffer + sizeof(struct udphdr), ntohs(this->getUDPHeader()->len) - sizeof(struct udphdr));
break;
}
}
struct icmphdr *CPacket::getICMPHeader() const
{
return this->m_pICMPHeader;
}
struct tcphdr *CPacket::getTCPHeader() const
{
return this->m_pTCPHeader;
}
struct udphdr *CPacket::getUDPHeader() const
{
return this->m_pUDPHeader;
}
void CPacket::setICMPHeader(struct icmphdr *header)
{
this->m_pICMPHeader = header;
}
void CPacket::setTCPHeader(struct tcphdr *header)
{
this->m_pTCPHeader = header;
}
void CPacket::setUDPHeader(struct udphdr *header)
{
this->m_pUDPHeader = header;
}
bool CPacket::isICMPv4Protocol() const
{
if (this->m_pIPHeader != nullptr) {
return this->m_pIPHeader->protocol == 1;
} else {
return false;
}
}
bool CPacket::isTCPProtocol() const
{
if (this->m_pIPHeader != nullptr) {
return this->m_pIPHeader->protocol == 6;
} else {
return false;
}
}
bool CPacket::isUDPProtocol() const
{
if (this->m_pIPHeader != nullptr) {
return this->m_pIPHeader->protocol == 17;
} else {
return false;
}
}
const DNSParser &CPacket::getDNSParser() const {
return this->dnsParser;
}
const httpDetector &CPacket::getHTTPDetector() const {
return this->detectorHTTP;
}