-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEthFileRead.cpp
More file actions
377 lines (302 loc) · 7.97 KB
/
EthFileRead.cpp
File metadata and controls
377 lines (302 loc) · 7.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
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
#include "kdvtype.h"
#include "osp.h"
#include "EthFileRead.h"
/*文件格式24(file head)+8(pack head)+4(pack len)+4(pack len)+14(ethernet header)
+ 20(ip header) + 12(offset) + 1(tcp head len) + data
*/
/*
补:
24(pcap header)
+ 16(packet header) = (4(timestamp高位)+ 4(timestamp低位)+ 4(caplen,当前数据区的长度,即抓取到的数据帧长度,由此可以得到下一个数据帧的位置)+ 4(len))
*/
/* 头格式
#pragma pack(push)
#pragma pack(1)
struct pcap_file_header
{
DWORD magic;
WORD version_major;
WORD version_minor;
DWORD thiszone;
DWORD sigfigs;
DWORD snaplen;
DWORD linktype;
};
#pragma pack(pop);
*/
const u8 ETHREAL_FILE_HEADER[24]={0xd4,0xc3,0xb2,0xa1,
0x02,0x00,0x04,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00};
#define MAX_PACKET_SIZE 2048
CEthFile::CEthFile()
{
m_pFile = NULL;
m_dwSrcIp = 0;
m_dwDstIp = 0;
m_pbyBuff = NULL;
memset(&m_frame_hdr, 0, sizeof(m_frame_hdr));
}
CEthFile::~CEthFile()
{
Close();
}
void CEthFile::Close()
{
if (NULL != m_pFile)
{
fclose(m_pFile);
m_pFile = NULL;
}
if (NULL != m_pbyBuff)
{
delete m_pbyBuff;
}
}
BOOL32 CEthFile::Create(s8* pszFileName)
{
Close();
if (NULL == pszFileName)
{
return FALSE;
}
m_pFile = fopen(pszFileName, "rb");
if (NULL == m_pFile)
{
printf("file open error!\n");
return FALSE;
}
do
{
u8 abyHeader[sizeof(ETHREAL_FILE_HEADER)];
s32 nLen;
nLen = fread(&abyHeader, 1, sizeof(ETHREAL_FILE_HEADER), m_pFile);
if (nLen < sizeof(ETHREAL_FILE_HEADER))
{
printf("file length is error!\n");
break;
}
// if(memcmp(abyHeader, ETHREAL_FILE_HEADER, 16))
// {
// printf("file format is error!\n");
// break;
// }
// 仅支持 Ethernet
/*以下是数据值与链路层类型的对应表
0 BSD loopback devices, except for later OpenBSD
1 Ethernet, and Linux loopback devices 以太网类型,大多数的数据包为这种类型。
6 802.5 Token Ring
7 ARCnet
8 SLIP
9 PPP
10 FDDI
100 LLC/SNAP-encapsulated ATM
101 raw IP, with no link
102 BSD/OS SLIP
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108 later OpenBSD loopback devices (with the AF_value in network byte order)
113 special Linux cooked capture
114 LocalTalk
*/
u32 dwNetworkType = *((u32*)(abyHeader+20));
if (dwNetworkType != 1)
{
printf("file format is error, not ethernet!\n");
break;
}
m_pbyBuff = new u8[MAX_PACKET_SIZE];
if (NULL == m_pbyBuff)
{
printf("Buff Create Failed!\n");
break;
}
return TRUE;
} while(0);
Close();
return FALSE;
}
BOOL32 CEthFile::SetFilter(u32 dwSrcIp, u32 dwDstIp, u16 wSrcPort, u16 wDstPort)
{
m_dwSrcIp = dwSrcIp;
m_dwDstIp = dwDstIp;
m_wSrcPort = wSrcPort;
m_wDstPort = wDstPort;
return TRUE;
}
u8* CEthFile::GetData()
{
if (GetLen() > 0)
{
return m_pbyBuff;
}
else
return NULL;
}
s32 CEthFile::GetLen()
{
return m_nDataLen;
}
s32 CEthFile::FirstPacket()
{
fseek(m_pFile, sizeof(ETHREAL_FILE_HEADER), SEEK_SET);
memset(&m_frame_hdr, 0, sizeof(m_frame_hdr));
return NextPacket();
}
u16 CEthFile::GetUdpDstPort()
{
return m_e_udphdr.uh_dport;
}
frame_hdr* CEthFile::GetFrame()
{
if (GetLen() > 0)
{
return &m_frame_hdr;
}
else
return NULL;
}
//返回UDP长度
s32 CEthFile::NextPacket()
{
s32 nLen;
BOOL32 bOK = FALSE;
m_frame_hdr.pack_len = 0;
u32 dwOffsetInFrame = 0;
s32 nFrameCount = 0;
do
{
if (m_frame_hdr.pack_len - dwOffsetInFrame > 0 )
{
if (0 != fseek(m_pFile, m_frame_hdr.pack_len - dwOffsetInFrame, SEEK_CUR))
{
break;
}
}
//读帧头
nLen = fread(&m_frame_hdr, 1, sizeof(frame_hdr), m_pFile);
if (nLen < sizeof(frame_hdr))
{
break;
}
nFrameCount++;
if (m_frame_hdr.pack_len == 0 || m_frame_hdr.pack_len < m_frame_hdr.cap_len)
{
printf("Frame header error\n");
}
dwOffsetInFrame = 0;
//读以太网头
nLen = fread(&m_eth_hdr, 1, sizeof(eth_hdr), m_pFile);
if (nLen < sizeof(eth_hdr))
{
break;
}
dwOffsetInFrame += nLen;
m_eth_hdr.st_ntoh();
if (m_eth_hdr.type != 0x0800) // 网际协议(IP)
{
if (m_eth_hdr.type == 0x8864) // 以太网上的 PPP(PPP 会话阶段) (PPPoE,PPP Over Ethernet<PPP Session Stage>)
{
nLen = fread(&m_e_PPPoE, 1, sizeof(m_e_PPPoE), m_pFile);
if (sizeof(m_e_PPPoE) != nLen)
{
printf("read PPPoE header error!\n");
break;
}
dwOffsetInFrame += nLen;
nLen = fread(&m_e_P2P, 1, sizeof(m_e_P2P), m_pFile);
if(sizeof(m_e_P2P) != nLen)
{
printf("read P2P header error!\n");
break;
}
dwOffsetInFrame += nLen;
}
else
{
continue;
}
}
//读IP包头
nLen = fread(&m_e_ip, 1, sizeof(e_ip), m_pFile);
if (nLen < sizeof(e_ip))
{
break;
}
dwOffsetInFrame += nLen;
m_e_ip.st_ntoh();
if (m_e_ip.ip_v_hl != 0x45)
{
continue;
}
//UDP协议
if (m_e_ip.ip_p != 0x11)
{
continue;
}
//不进行比较源地址
if (m_dwSrcIp != 0 && m_e_ip.ip_src != m_dwSrcIp)
{
continue;
}
//比较目标地址
if (m_dwDstIp != 0 && m_e_ip.ip_dst != m_dwDstIp)
{
continue;
}
//IP分片
if (m_e_ip.ip_off != 0 && m_e_ip.ip_off!= 0x4000)
{
printf("IP fragment!\n");
continue;
}
//读UDP头
nLen = fread(&m_e_udphdr, 1, sizeof(e_udphdr), m_pFile);
if (nLen < sizeof(e_udphdr))
{
break;
}
dwOffsetInFrame += nLen;
m_e_udphdr.st_ntoh();
if (m_wSrcPort != 0 && m_wSrcPort != m_e_udphdr.uh_sport)
{
continue;
}
if (m_wDstPort != 0 && m_wDstPort != m_e_udphdr.uh_dport)
{
continue;
}
s32 nUdpDataLen;
nUdpDataLen = m_e_udphdr.uh_ulen - sizeof(e_udphdr);
//not equal because of capture cut lost data
if (m_frame_hdr.cap_len - dwOffsetInFrame < (u32)nUdpDataLen)
{
printf("Frame fragile: should: %d!, real: %d\n", nUdpDataLen, m_frame_hdr.cap_len - dwOffsetInFrame);
//continue;
}
nLen = fread(m_pbyBuff, 1, m_frame_hdr.cap_len - dwOffsetInFrame, m_pFile);
if (nLen < (s32)m_frame_hdr.cap_len - dwOffsetInFrame)
{
printf("read udp data error!\n");
break;
}
dwOffsetInFrame += nLen;
if (m_frame_hdr.cap_len - dwOffsetInFrame > 0)
{
fseek(m_pFile, m_frame_hdr.cap_len - dwOffsetInFrame, SEEK_CUR);
}
bOK = TRUE;
} while(!bOK);
if (!bOK)
{
m_nDataLen = -1;
}
else
{
m_nDataLen = nLen;
}
return m_nDataLen;
}