-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.cpp
More file actions
179 lines (161 loc) · 3.97 KB
/
Copy pathpacket.cpp
File metadata and controls
179 lines (161 loc) · 3.97 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
/*
* packet.cpp
*
* Created on: 2015-11-21
* Author: Yao Yuan
*/
#include <string.h>
#include <stdint.h>
#include <WinSock2.h>
#ifdef WIN32
#else
#include <arpa/inet.h>
#endif
#include "packet.h"
#include "trace.h"
const int RUNT_LEN = 34;//Refer http://www.erg.abdn.ac.uk/users/gorry/course/lan-pages/mac.html
const int OVERSIZE_LEN = 1518;
TDSPkt::TDSPkt(int nSize)
{
if (nSize > 0)
{
tdspkt = new unsigned char[nSize];
m_nSize = nSize;
}
else
{
tdspkt = NULL;
m_nSize = 0;
}
}
TDSPkt::~TDSPkt()
{
if (tdspkt != NULL)
{
delete[] tdspkt;
tdspkt = NULL;
}
}
////////////////////////////////////////////////////////////////////////////////
// class Packet implementation
//
Packet::Packet(const struct pcap_pkthdr *h, const uint8_t *sp)
{
pcapHeader = *h;
packetData = new unsigned char[h->caplen + 1];
if (packetData)
{
memcpy(packetData, sp, h->caplen);
packetData[h->caplen] = 0; // for safe string opertion like strstr
}
}
Packet::~Packet()
{
if (packetData != NULL)
{
delete[] packetData;
packetData = NULL;
}
}
int Packet::parsePacketHead()
{
uint32_t capLen = pcapHeader.caplen;
if (capLen < RUNT_LEN || capLen > OVERSIZE_LEN)
{
TRACE_WARNING("The packet length(%d) is not valid!\n", capLen);
return PROTOCOL_UNKNOWN;
}
uint32_t offset = 14;
short ethType = *(short*)(packetData + 12);
#define ETH_TYPE_IP 8
#define ETH_TYPE_VLAN 0x81
if (ethType != ETH_TYPE_IP && ethType != ETH_TYPE_VLAN)
{ // Not an (IP or 802.1q) Packet
TRACE_WARNING("The type(%04X) of packet is not ETH or ETH_VLAN!\n", ethType);
return PROTOCOL_UNKNOWN;
}
uint8_t protocol;
if (ethType == ETH_TYPE_VLAN)
{
if (offset + IP_HDR_FIX_LEN > capLen)
{
TRACE_WARNING("The packet(VLAN) length(%d) is not valid!\n", capLen);
return PROTOCOL_UNKNOWN;
}
//TRACE_WARNING("ETH_TYPE_VLAN \n");
offset += 4;
protocol = *(packetData + 27);
}
else
{
protocol = *(packetData + 23);
}
memcpy((void*)&ipHeader, packetData + offset, IP_HDR_FIX_LEN);
uint32_t ipHeadLen = (*(packetData + offset) & 0xF) * 4; //IHL
uint32_t ipBodyLen = ntohs(ipHeader.ip_len);
ipBodyLen -= ipHeadLen;
offset += ipHeadLen; //14+IHL
// uint32 tcpBodyLen = ipBodyLen;
if (ipBodyLen > capLen - offset)
{
ipBodyLen = capLen - offset;
/* if (ipBodyLen < 0) {
TRACE_WARNING("ipBodyLen(%d) < 0\n", ipBodyLen);
return PROTOCOL_UNKNOWN;
}*/
}
/* if (tcpBodyLen + offset < RUNT_LEN
|| tcpBodyLen + offset > OVERSIZE_LEN) {
TRACE_WARNING("error packet, tcpbodylen=%d, offset=%d\n", tcpBodyLen,
offset);
return PROTOCOL_UNKNOWN;
}
*/
ipHeader.ip_src = ntohl(ipHeader.ip_src);
ipHeader.ip_dst = ntohl(ipHeader.ip_dst);
if (protocol == 0x6)
{ // TCP
if (offset + TCP_HEADER_LEN > capLen)
{
TRACE_WARNING("The packet(tcp) length(%d) is not valid!\n", capLen);
return PROTOCOL_UNKNOWN;
}
memcpy((void*)&tcpHeader, packetData + offset, TCP_HEADER_LEN);
tcpHeader.th_sport = ntohs(tcpHeader.th_sport);
tcpHeader.th_dport = ntohs(tcpHeader.th_dport);
tcpHeader.th_seq = ntohl(tcpHeader.th_seq);
tcpHeader.th_ack = ntohl(tcpHeader.th_ack);
uint32_t tcpHeadLen = (*(packetData + offset + 12) >> 4) * 4; //TCPHdrLen
offset += tcpHeadLen; //EthFrmLen(14)+IHL+THL
if (ipBodyLen < tcpHeadLen)
{
TRACE_WARNING("The packet(tcp) is not valid, capLen=%d, ipBodyLen=%d, tcpHeadLen=%d\n",
capLen, ipBodyLen, tcpHeadLen);
return PROTOCOL_UNKNOWN;
}
tcpBody = packetData + offset;
tcpBodySize = ipBodyLen - tcpHeadLen;
return PROTOCOL_TCP;
}
else if (protocol == 0x11)
{ // UDP
if (offset + UDP_HEADER_LEN > capLen)
{
TRACE_WARNING("The packet(udp) length(%d) is not valid!\n", capLen);
return PROTOCOL_UNKNOWN;
}
struct udphdr hdrUDP;
memcpy((void*)&hdrUDP, packetData + offset, UDP_HEADER_LEN);
hdrUDP.sport = ntohs(hdrUDP.sport);
hdrUDP.dport = ntohs(hdrUDP.dport);
return PROTOCOL_UDP;
}
else
{
return PROTOCOL_UNKNOWN; //not TCP packets;
}
}
uint8_t *Packet::getPacketData()
{
return packetData;
}