2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 #include "stg/traffcounter.h"
24 #include "stg/common.h"
25 #include "stg/raw_ip_packet.h"
29 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
35 const size_t SNAP_LEN = 1518;
36 const size_t ETHER_ADDR_LEN = 6;
40 u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */
41 u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */
42 u_short ether_type; /* IP? ARP? RARP? etc */
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 extern "C" STG::Plugin* GetPlugin()
52 static PCAP_CAP plugin;
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 std::string PCAP_CAP::GetVersion() const
60 return "pcap_cap v.1.0";
62 //-----------------------------------------------------------------------------
66 logger(STG::PluginLogger::get("pcap_cap"))
69 //-----------------------------------------------------------------------------
70 int PCAP_CAP::ParseSettings()
72 devices.erase(devices.begin(), devices.end());
74 if (settings.moduleParams.empty())
76 devices.push_back(DEV());
77 logger("Defaulting to pseudo-device 'any'.");
81 for (size_t i = 0; i < settings.moduleParams.size(); i++)
82 if (settings.moduleParams[i].param == "interfaces")
83 for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
84 devices.push_back(DEV(settings.moduleParams[i].value[j]));
86 for (size_t i = 0; i < settings.moduleParams.size(); i++)
87 if (settings.moduleParams[i].param == "filters")
88 for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
89 if (j < devices.size())
90 devices[j].filterExpression = settings.moduleParams[i].value[j];
94 devices.push_back(DEV());
95 logger("Defaulting to pseudo-device 'all'.");
101 //-----------------------------------------------------------------------------
102 int PCAP_CAP::Start()
107 DEV_MAP::iterator it(devices.begin());
108 while (it != devices.end())
112 char errbuf[PCAP_ERRBUF_SIZE];
114 /* get network number and mask associated with capture device */
115 if (pcap_lookupnet(it->device.c_str(), &net, &mask, errbuf) == -1)
117 errorStr = "Couldn't get netmask for device " + it->device + ": " + errbuf;
119 printfd(__FILE__, "%s\n", errorStr.c_str());
123 /* open capture device */
124 it->handle = pcap_open_live(it->device.c_str(), SNAP_LEN, 1, 1000, errbuf);
125 if (it->handle == NULL)
127 errorStr = "Couldn't open device " + it->device + ": " + errbuf;
129 printfd(__FILE__, "%s\n", errorStr.c_str());
133 if (pcap_setnonblock(it->handle, true, errbuf) == -1)
135 errorStr = "Couldn't put device " + it->device + " into non-blocking mode: " + errbuf;
137 printfd(__FILE__, "%s\n", errorStr.c_str());
141 /* make sure we're capturing on an Ethernet device [2] */
142 if (pcap_datalink(it->handle) != DLT_EN10MB)
144 errorStr = it->device + " is not an Ethernet";
146 printfd(__FILE__, "%s\n", errorStr.c_str());
150 /* compile the filter expression */
151 if (pcap_compile(it->handle, &it->filter, it->filterExpression.c_str(), 0, net) == -1)
153 errorStr = "Couldn't parse filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
155 printfd(__FILE__, "%s\n", errorStr.c_str());
159 /* apply the compiled filter */
160 if (pcap_setfilter(it->handle, &it->filter) == -1)
162 errorStr = "Couldn't install filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
164 printfd(__FILE__, "%s\n", errorStr.c_str());
168 it->fd = pcap_get_selectable_fd(it->handle);
171 errorStr = "Couldn't get a file descriptor for " + it->device + ": " + pcap_geterr(it->handle);
173 printfd(__FILE__, "%s\n", errorStr.c_str());
180 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
184 //-----------------------------------------------------------------------------
190 m_thread.request_stop();
192 //5 seconds to thread stops itself
193 for (int i = 0; i < 25 && isRunning; i++)
195 struct timespec ts = {0, 200000000};
196 nanosleep(&ts, NULL);
198 //after 5 seconds waiting thread still running. now killing it
204 for (DEV_MAP::iterator it(devices.begin()); it != devices.end(); ++it)
206 pcap_freecode(&it->filter);
207 pcap_close(it->handle);
212 //-----------------------------------------------------------------------------
213 void PCAP_CAP::Run(std::stop_token token)
216 sigfillset(&signalSet);
217 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
224 for (DEV_MAP::const_iterator it(devices.begin()); it != devices.end(); ++it)
226 FD_SET(it->fd, &fds);
227 maxFd = std::max(maxFd, it->fd);
230 while (!token.stop_requested())
233 struct timeval tv = {0, 500000};
235 if (select(maxFd + 1, &rfds, NULL, NULL, &tv) > 0)
242 void PCAP_CAP::TryRead(const fd_set & set)
244 for (DEV_MAP::const_iterator it(devices.begin()); it != devices.end(); ++it)
245 if (FD_ISSET(it->fd, &set))
249 void PCAP_CAP::TryReadDev(const DEV & dev)
251 struct pcap_pkthdr * header;
252 const u_char * packet;
253 if (pcap_next_ex(dev.handle, &header, &packet) == -1)
255 printfd(__FILE__, "Failed to read data from '%s': %s\n", dev.device.c_str(), pcap_geterr(dev.handle));
259 const ETH * eth = reinterpret_cast<const ETH *>(packet);
260 if (eth->ether_type != 0x8)
264 memcpy(&ip.rawPacket, packet + 14, sizeof(ip.rawPacket));
265 traffCnt->process(ip);