/
PcapInterface.cpp
147 lines (115 loc) · 4.03 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
/*
*
* (C) 2013 - 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];
if((pcap_handle = pcap_open_live(ifname, ntop->getGlobals()->getSnaplen(),
ntop->getGlobals()->getPromiscuousMode(),
500, pcap_error_buffer)) == NULL) {
pcap_handle = pcap_open_offline(ifname, pcap_error_buffer);
if(pcap_handle == NULL) {
printf("ERROR: could not open pcap file: %s\n", pcap_error_buffer);
exit(0);
} 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;
}
} else {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Reading packets from interface %s...", ifname);
read_pkts_from_pcap_dump = false;
}
pcap_datalink_type = pcap_datalink(pcap_handle);
}
/* **************************************************** */
PcapInterface::~PcapInterface() {
shutdown();
if(pcap_handle)
pcap_close(pcap_handle);
deleteDataStructures();
}
/* **************************************************** */
static void* packetPollLoop(void* ptr) {
PcapInterface *iface = (PcapInterface*)ptr;
pcap_t *pd = iface->get_pcap_handle();
/* Wait until the initialization competes */
while(!iface->isRunning()) sleep(1);
while(iface->isRunning()
&& (!ntop->getGlobals()->isShutdown())) {
const u_char *pkt;
struct pcap_pkthdr hdr;
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 */
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_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;
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);
}
};