-
Notifications
You must be signed in to change notification settings - Fork 8
/
PcapInterface.cpp
220 lines (175 loc) · 5.91 KB
/
PcapInterface.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
/*
*
* (C) 2013-14 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "ntop_includes.h"
#ifdef __APPLE__
#include <uuid/uuid.h>
#endif
/* **************************************************** */
PcapInterface::PcapInterface(const char *name) : NetworkInterface(name) {
char pcap_error_buffer[PCAP_ERRBUF_SIZE];
struct stat buf;
pcap_handle = NULL, pcap_list = NULL;
if(stat(name, &buf) == 0) {
/*
The file exists so we need to check if it's a
text file or a pcap file
*/
if((pcap_handle = pcap_open_offline(ifname, pcap_error_buffer)) == NULL) {
if((pcap_list = fopen(name, "r")) == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to open file %s", name);
_exit(0);
} else
read_pkts_from_pcap_dump = true;
} else {
char *slash = strrchr(ifname, '/');
if(slash) {
char *old = ifname;
ifname = strdup(&slash[1]);
free(old);
}
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Reading packets from pcap file %s...", ifname);
read_pkts_from_pcap_dump = true, purge_idle_flows_hosts = false;
pcap_datalink_type = pcap_datalink(pcap_handle);
}
} else {
pcap_handle = pcap_open_live(ifname, ntop->getGlobals()->getSnaplen(),
ntop->getGlobals()->getPromiscuousMode(),
500, pcap_error_buffer);
if(pcap_handle) {
char *bl = strrchr(ifname,
#ifdef WIN32
'\\'
#else
'/'
#endif
);
if(bl != NULL) {
char *tmp = ifname;
ifname = strdup(&bl[1]);
free(tmp);
}
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Reading packets from interface %s...", ifname);
read_pkts_from_pcap_dump = false;
pcap_datalink_type = pcap_datalink(pcap_handle);
}
}
if(ntop->getPrefs()->are_ixia_timestamps_enabled())
ntop->getTrace()->traceEvent(TRACE_WARNING, "Hardware timestamps are supported only on PF_RING capture interfaces");
}
/* **************************************************** */
PcapInterface::~PcapInterface() {
shutdown();
if(pcap_handle) {
pcap_close(pcap_handle);
pcap_handle = NULL;
}
}
/* **************************************************** */
static void* packetPollLoop(void* ptr) {
PcapInterface *iface = (PcapInterface*)ptr;
pcap_t *pd;
FILE *pcap_list = iface->get_pcap_list();
/* Wait until the initialization completes */
while(!iface->isRunning()) sleep(1);
do {
if(pcap_list != NULL) {
char path[256], *fname;
pcap_t *pcap_handle;
while((fname = fgets(path, sizeof(path), pcap_list)) != NULL) {
char pcap_error_buffer[PCAP_ERRBUF_SIZE];
int l = (int)strlen(path)-1;
if((l <= 1) || (path[0] == '#')) continue;
path[l--] = '\0';
/* Remove trailer white spaces */
while((l > 0) && (path[l] == ' ')) path[l--] = '\0';
if((pcap_handle = pcap_open_offline(path, pcap_error_buffer)) == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to open file '%s': %s",
path, pcap_error_buffer);
} else {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Reading packes from pcap file %s", path);
iface->set_pcap_handle(pcap_handle);
break;
}
}
if(fname == NULL) {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "No more pcap files to read");
fclose(pcap_list);
return(NULL);
} else
iface->set_datalink(pcap_datalink(pcap_handle));
}
pd = iface->get_pcap_handle();
while((pd != NULL)
&& iface->isRunning()
&& (!ntop->getGlobals()->isShutdown())) {
const u_char *pkt;
struct pcap_pkthdr hdr;
while(iface->idle()) { iface->purgeIdle(time(NULL)); sleep(1); }
if((pkt = pcap_next(pd, &hdr)) != NULL) {
if((hdr.caplen > 0) && (hdr.len > 0))
iface->packet_dissector(&hdr, pkt);
} else {
if(iface->read_from_pcap_dump())
break;
iface->purgeIdle(time(NULL));
}
} /* while */
} while(pcap_list != NULL);
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Terminated packet polling for %s", iface->get_name());
return(NULL);
}
/* **************************************************** */
void PcapInterface::startPacketPolling() {
pthread_create(&pollLoop, NULL, packetPollLoop, (void*)this);
NetworkInterface::startPacketPolling();
}
/* **************************************************** */
void PcapInterface::shutdown() {
if(running) {
void *res;
NetworkInterface::shutdown();
if(pcap_handle) pcap_breakloop(pcap_handle);
pthread_join(pollLoop, &res);
}
}
/* **************************************************** */
u_int PcapInterface::getNumDroppedPackets() {
struct pcap_stat pcapStat;
if(pcap_handle && (pcap_stats(pcap_handle, &pcapStat) >= 0)) {
return(pcapStat.ps_drop);
} else
return(0);
}
/* **************************************************** */
bool PcapInterface::set_packet_filter(char *filter) {
struct bpf_program fcode;
struct in_addr netmask;
if(!pcap_handle) return(false);
netmask.s_addr = htonl(0xFFFFFF00);
if((pcap_compile(pcap_handle, &fcode, filter, 1, netmask.s_addr) < 0)
|| (pcap_setfilter(pcap_handle, &fcode) < 0)) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to set filter %s. Filter ignored.\n", filter);
return(false);
} else {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Packet capture filter set to \"%s\"", filter);
return(true);
}
};