-
Notifications
You must be signed in to change notification settings - Fork 18
/
PacketFileReader.h
176 lines (147 loc) · 4.13 KB
/
PacketFileReader.h
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
// Copyright 2013 Velodyne Acoustics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPacketFileReader.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkPacketFileReader -
// .SECTION Description
//
#ifndef __vtkPacketFileReader_h
#define __vtkPacketFileReader_h
#include <pcap.h>
#include <string>
// Some versions of libpcap do not have PCAP_NETMASK_UNKNOWN
#if !defined(PCAP_NETMASK_UNKNOWN)
#define PCAP_NETMASK_UNKNOWN 0xffffffff
#endif
class vtkPacketFileReader
{
public:
vtkPacketFileReader()
{
this->PCAPFile = 0;
}
~vtkPacketFileReader()
{
this->Close();
}
bool Open(const std::string& filename)
{
char errbuff[PCAP_ERRBUF_SIZE];
pcap_t *pcapFile = pcap_open_offline(filename.c_str (), errbuff);
if (!pcapFile)
{
this->LastError = errbuff;
return false;
}
struct bpf_program filter;
if (pcap_compile(pcapFile, &filter, "udp", 0, PCAP_NETMASK_UNKNOWN) == -1)
{
this->LastError = pcap_geterr(pcapFile);
return false;
}
if (pcap_setfilter(pcapFile, &filter) == -1)
{
this->LastError = pcap_geterr(pcapFile);
return false;
}
this->FileName = filename;
this->PCAPFile = pcapFile;
this->StartTime.tv_sec = this->StartTime.tv_usec = 0;
return true;
}
bool IsOpen()
{
return (this->PCAPFile != 0);
}
void Close()
{
if (this->PCAPFile)
{
pcap_close(this->PCAPFile);
this->PCAPFile = 0;
this->FileName.clear();
}
}
const std::string& GetLastError()
{
return this->LastError;
}
const std::string& GetFileName()
{
return this->FileName;
}
void GetFilePosition(fpos_t* position)
{
#ifdef _MSC_VER
pcap_fgetpos(this->PCAPFile, position);
#else
FILE* f = pcap_file(this->PCAPFile);
fgetpos(f, position);
#endif
}
void SetFilePosition(fpos_t* position)
{
#ifdef _MSC_VER
pcap_fsetpos(this->PCAPFile, position);
#else
FILE* f = pcap_file(this->PCAPFile);
fsetpos(f, position);
#endif
}
bool NextPacket(const unsigned char*& data, unsigned int& dataLength, double& timeSinceStart, pcap_pkthdr** headerReference=NULL)
{
if (!this->PCAPFile)
{
return false;
}
struct pcap_pkthdr *header;
int returnValue = pcap_next_ex(this->PCAPFile, &header, &data);
if (returnValue < 0)
{
this->Close();
return false;
}
if (headerReference != NULL)
{
*headerReference = header;
dataLength = header->len;
timeSinceStart = GetElapsedTime(header->ts, this->StartTime);
return true;
}
// The ethernet header is 42 bytes long; unnecessary
const unsigned int bytesToSkip = 42;
dataLength = header->len - bytesToSkip;
data = data + bytesToSkip;
timeSinceStart = GetElapsedTime(header->ts, this->StartTime);
return true;
}
protected:
double GetElapsedTime(const struct timeval& end, const struct timeval& start)
{
return (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.00;
}
pcap_t* PCAPFile;
std::string FileName;
std::string LastError;
struct timeval StartTime;
};
#endif