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/plugin_creator.h"
25 #include "stg/common.h"
26 #include "stg/raw_ip_packet.h"
30 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
35 PLUGIN_CREATOR<PCAP_CAP> pcc;
37 const size_t SNAP_LEN = 1518;
38 const size_t ETHER_ADDR_LEN = 6;
42 u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */
43 u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */
44 u_short ether_type; /* IP? ARP? RARP? etc */
49 extern "C" PLUGIN * GetPlugin();
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
55 return pcc.GetPlugin();
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 std::string PCAP_CAP::GetVersion() const
62 return "pcap_cap v.1.0";
64 //-----------------------------------------------------------------------------
69 logger(GetPluginLogger(GetStgLogger(), "pcap_cap"))
72 //-----------------------------------------------------------------------------
73 int PCAP_CAP::ParseSettings()
75 devices.erase(devices.begin(), devices.end());
77 if (settings.moduleParams.empty())
79 devices.push_back(DEV());
80 logger("Defaulting to pseudo-device 'any'.");
84 for (size_t i = 0; i < settings.moduleParams.size(); i++)
85 if (settings.moduleParams[i].param == "interfaces")
86 for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
87 devices.push_back(DEV(settings.moduleParams[i].value[j]));
89 for (size_t i = 0; i < settings.moduleParams.size(); i++)
90 if (settings.moduleParams[i].param == "filters")
91 for (size_t j = 0; j < settings.moduleParams[i].value.size(); j++)
92 if (j < devices.size())
93 devices[j].filterExpression = settings.moduleParams[i].value[j];
97 devices.push_back(DEV());
98 logger("Defaulting to pseudo-device 'all'.");
104 //-----------------------------------------------------------------------------
105 int PCAP_CAP::Start()
110 DEV_MAP::iterator it(devices.begin());
111 while (it != devices.end())
115 char errbuf[PCAP_ERRBUF_SIZE];
117 /* get network number and mask associated with capture device */
118 if (pcap_lookupnet(it->device.c_str(), &net, &mask, errbuf) == -1)
120 errorStr = "Couldn't get netmask for device " + it->device + ": " + errbuf;
122 printfd(__FILE__, "%s\n", errorStr.c_str());
126 /* open capture device */
127 it->handle = pcap_open_live(it->device.c_str(), SNAP_LEN, 1, 1000, errbuf);
128 if (it->handle == NULL)
130 errorStr = "Couldn't open device " + it->device + ": " + errbuf;
132 printfd(__FILE__, "%s\n", errorStr.c_str());
136 if (pcap_setnonblock(it->handle, true, errbuf) == -1)
138 errorStr = "Couldn't put device " + it->device + " into non-blocking mode: " + errbuf;
140 printfd(__FILE__, "%s\n", errorStr.c_str());
144 /* make sure we're capturing on an Ethernet device [2] */
145 if (pcap_datalink(it->handle) != DLT_EN10MB)
147 errorStr = it->device + " is not an Ethernet";
149 printfd(__FILE__, "%s\n", errorStr.c_str());
153 /* compile the filter expression */
154 if (pcap_compile(it->handle, &it->filter, it->filterExpression.c_str(), 0, net) == -1)
156 errorStr = "Couldn't parse filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
158 printfd(__FILE__, "%s\n", errorStr.c_str());
162 /* apply the compiled filter */
163 if (pcap_setfilter(it->handle, &it->filter) == -1)
165 errorStr = "Couldn't install filter " + it->filterExpression + ": " + pcap_geterr(it->handle);
167 printfd(__FILE__, "%s\n", errorStr.c_str());
171 it->fd = pcap_get_selectable_fd(it->handle);
174 errorStr = "Couldn't get a file descriptor for " + it->device + ": " + pcap_geterr(it->handle);
176 printfd(__FILE__, "%s\n", errorStr.c_str());
185 if (pthread_create(&thread, NULL, Run, this))
187 errorStr = "Cannot create thread.";
188 logger("Cannot create thread.");
189 printfd(__FILE__, "Cannot create thread\n");
195 //-----------------------------------------------------------------------------
203 //5 seconds to thread stops itself
204 for (int i = 0; i < 25 && isRunning; i++)
206 struct timespec ts = {0, 200000000};
207 nanosleep(&ts, NULL);
209 //after 5 seconds waiting thread still running. now killing it
212 if (pthread_kill(thread, SIGUSR1))
214 errorStr = "Cannot kill thread.";
215 logger("Cannot send signal to thread.");
218 for (int i = 0; i < 25 && isRunning; ++i)
220 struct timespec ts = {0, 200000000};
221 nanosleep(&ts, NULL);
225 errorStr = "PCAP_CAP not stopped.";
226 logger("Cannot stop thread.");
227 printfd(__FILE__, "Cannot stop thread\n");
232 pthread_join(thread, NULL);
234 for (DEV_MAP::iterator it(devices.begin()); it != devices.end(); ++it)
236 pcap_freecode(&it->filter);
237 pcap_close(it->handle);
242 //-----------------------------------------------------------------------------
243 void * PCAP_CAP::Run(void * d)
246 sigfillset(&signalSet);
247 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
249 PCAP_CAP * dc = static_cast<PCAP_CAP *>(d);
250 dc->isRunning = true;
255 for (DEV_MAP::const_iterator it(dc->devices.begin()); it != dc->devices.end(); ++it)
257 FD_SET(it->fd, &fds);
258 maxFd = std::max(maxFd, it->fd);
264 struct timeval tv = {0, 500000};
266 if (select(maxFd + 1, &rfds, NULL, NULL, &tv) > 0)
270 dc->isRunning = false;
274 void PCAP_CAP::TryRead(const fd_set & set)
276 for (DEV_MAP::const_iterator it(devices.begin()); it != devices.end(); ++it)
277 if (FD_ISSET(it->fd, &set))
281 void PCAP_CAP::TryReadDev(const DEV & dev)
283 struct pcap_pkthdr * header;
284 const u_char * packet;
285 if (pcap_next_ex(dev.handle, &header, &packet) == -1)
287 printfd(__FILE__, "Failed to read data from '%s': %s\n", dev.device.c_str(), pcap_geterr(dev.handle));
291 const ETH * eth = reinterpret_cast<const ETH *>(packet);
292 if (eth->ether_type != 0x8)
296 memcpy(&ip.rawPacket, packet + 14, sizeof(ip.rawPacket));
297 traffCnt->Process(ip);