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" 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 //-----------------------------------------------------------------------------
67 logger(PluginLogger::get("pcap_cap"))
70 //-----------------------------------------------------------------------------
71 int PCAP_CAP::ParseSettings()
73 devices.erase(devices.begin(), devices.end());
75 if (settings.moduleParams.empty())
77 devices.push_back(DEV());
78 logger("Defaulting to pseudo-device 'any'.");
82 for (size_t i = 0; i < settings.moduleParams.size(); i++)
83 if (settings.moduleParams[i].param == "interfaces")
84 for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
85 devices.push_back(DEV(settings.moduleParams[i].value[j]));
87 for (size_t i = 0; i < settings.moduleParams.size(); i++)
88 if (settings.moduleParams[i].param == "filters")
89 for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
90 if (j < devices.size())
91 devices[j].filterExpression = settings.moduleParams[i].value[j];
95 devices.push_back(DEV());
96 logger("Defaulting to pseudo-device 'all'.");
102 //-----------------------------------------------------------------------------
103 int PCAP_CAP::Start()
108 DEV_MAP::iterator it(devices.begin());
109 while (it != devices.end())
113 char errbuf[PCAP_ERRBUF_SIZE];
115 /* get network number and mask associated with capture device */
116 if (pcap_lookupnet(it->device.c_str(), &net, &mask, errbuf) == -1)
118 errorStr = "Couldn't get netmask for device " + it->device + ": " + errbuf;
120 printfd(__FILE__, "%s\n", errorStr.c_str());
124 /* open capture device */
125 it->handle = pcap_open_live(it->device.c_str(), SNAP_LEN, 1, 1000, errbuf);
126 if (it->handle == NULL)
128 errorStr = "Couldn't open device " + it->device + ": " + errbuf;
130 printfd(__FILE__, "%s\n", errorStr.c_str());
134 if (pcap_setnonblock(it->handle, true, errbuf) == -1)
136 errorStr = "Couldn't put device " + it->device + " into non-blocking mode: " + errbuf;
138 printfd(__FILE__, "%s\n", errorStr.c_str());
142 /* make sure we're capturing on an Ethernet device [2] */
143 if (pcap_datalink(it->handle) != DLT_EN10MB)
145 errorStr = it->device + " is not an Ethernet";
147 printfd(__FILE__, "%s\n", errorStr.c_str());
151 /* compile the filter expression */
152 if (pcap_compile(it->handle, &it->filter, it->filterExpression.c_str(), 0, net) == -1)
154 errorStr = "Couldn't parse filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
156 printfd(__FILE__, "%s\n", errorStr.c_str());
160 /* apply the compiled filter */
161 if (pcap_setfilter(it->handle, &it->filter) == -1)
163 errorStr = "Couldn't install filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
165 printfd(__FILE__, "%s\n", errorStr.c_str());
169 it->fd = pcap_get_selectable_fd(it->handle);
172 errorStr = "Couldn't get a file descriptor for " + it->device + ": " + pcap_geterr(it->handle);
174 printfd(__FILE__, "%s\n", errorStr.c_str());
183 if (pthread_create(&thread, NULL, Run, this))
185 errorStr = "Cannot create thread.";
186 logger("Cannot create thread.");
187 printfd(__FILE__, "Cannot create thread\n");
193 //-----------------------------------------------------------------------------
201 //5 seconds to thread stops itself
202 for (int i = 0; i < 25 && isRunning; i++)
204 struct timespec ts = {0, 200000000};
205 nanosleep(&ts, NULL);
207 //after 5 seconds waiting thread still running. now killing it
210 if (pthread_kill(thread, SIGUSR1))
212 errorStr = "Cannot kill thread.";
213 logger("Cannot send signal to thread.");
216 for (int i = 0; i < 25 && isRunning; ++i)
218 struct timespec ts = {0, 200000000};
219 nanosleep(&ts, NULL);
223 errorStr = "PCAP_CAP not stopped.";
224 logger("Cannot stop thread.");
225 printfd(__FILE__, "Cannot stop thread\n");
230 pthread_join(thread, NULL);
232 for (DEV_MAP::iterator it(devices.begin()); it != devices.end(); ++it)
234 pcap_freecode(&it->filter);
235 pcap_close(it->handle);
240 //-----------------------------------------------------------------------------
241 void * PCAP_CAP::Run(void * d)
244 sigfillset(&signalSet);
245 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
247 PCAP_CAP * dc = static_cast<PCAP_CAP *>(d);
248 dc->isRunning = true;
253 for (DEV_MAP::const_iterator it(dc->devices.begin()); it != dc->devices.end(); ++it)
255 FD_SET(it->fd, &fds);
256 maxFd = std::max(maxFd, it->fd);
262 struct timeval tv = {0, 500000};
264 if (select(maxFd + 1, &rfds, NULL, NULL, &tv) > 0)
268 dc->isRunning = false;
272 void PCAP_CAP::TryRead(const fd_set & set)
274 for (DEV_MAP::const_iterator it(devices.begin()); it != devices.end(); ++it)
275 if (FD_ISSET(it->fd, &set))
279 void PCAP_CAP::TryReadDev(const DEV & dev)
281 struct pcap_pkthdr * header;
282 const u_char * packet;
283 if (pcap_next_ex(dev.handle, &header, &packet) == -1)
285 printfd(__FILE__, "Failed to read data from '%s': %s\n", dev.device.c_str(), pcap_geterr(dev.handle));
289 const ETH * eth = reinterpret_cast<const ETH *>(packet);
290 if (eth->ether_type != 0x8)
294 memcpy(&ip.rawPacket, packet + 14, sizeof(ip.rawPacket));
295 traffCnt->Process(ip);