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
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2009/12/13 13:45:13 $
30 #include "ether_cap.h"
32 #include "stg/common.h"
33 #include "stg/raw_ip_packet.h"
34 #include "stg/traffcounter.h"
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
45 #include <sys/types.h>
47 #include <linux/if_ether.h>
48 #include <linux/if_packet.h>
49 #include <sys/ioctl.h>
54 extern "C" STG::Plugin* GetPlugin()
56 static ETHER_CAP plugin;
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 std::string ETHER_CAP::GetVersion() const
64 return "cap_ether v.1.2";
66 //-----------------------------------------------------------------------------
67 ETHER_CAP::ETHER_CAP()
72 logger(STG::PluginLogger::get("cap_ether"))
75 //-----------------------------------------------------------------------------
76 int ETHER_CAP::Start()
83 errorStr = "Cannot open socket!";
84 printfd(__FILE__, "Cannot open socket\n");
90 if (pthread_create(&thread, NULL, Run, this))
92 errorStr = "Cannot create thread.";
93 logger("Cannot create thread.");
94 printfd(__FILE__, "Cannot create thread\n");
100 //-----------------------------------------------------------------------------
101 int ETHER_CAP::Stop()
108 //5 seconds to thread stops itself
109 for (int i = 0; i < 25 && isRunning; i++)
111 struct timespec ts = {0, 200000000};
112 nanosleep(&ts, NULL);
114 //after 5 seconds waiting thread still running. now killing it
117 if (pthread_kill(thread, SIGUSR1))
119 errorStr = "Cannot kill thread.";
120 logger("Cannot send signal to thread.");
123 for (int i = 0; i < 25 && isRunning; ++i)
125 struct timespec ts = {0, 200000000};
126 nanosleep(&ts, NULL);
130 errorStr = "ETHER_CAP not stopped.";
131 logger("Cannot stop thread.");
132 printfd(__FILE__, "Cannot stop thread\n");
137 pthread_join(thread, NULL);
144 //-----------------------------------------------------------------------------
145 void * ETHER_CAP::Run(void * d)
148 sigfillset(&signalSet);
149 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
151 ETHER_CAP * dc = static_cast<ETHER_CAP *>(d);
152 dc->isRunning = true;
162 char ethip[sizeof(ETH_IP)];
164 memset(ðip, 0, sizeof(ETH_IP));
166 ETH_IP * ethIP = static_cast<ETH_IP *>(static_cast<void *>(ðip));
167 ethIP->rp.dataLen = -1;
173 if (dc->EthCapRead(ðip, 68 + 14, &iface))
178 if (ethIP->ethHdr[7] != 0x8)
181 dc->traffCnt->process(ethIP->rp);
184 dc->isRunning = false;
187 //-----------------------------------------------------------------------------
188 int ETHER_CAP::EthCapOpen()
190 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
192 logger("Cannot create socket: %s", strerror(errno));
195 //-----------------------------------------------------------------------------
196 int ETHER_CAP::EthCapClose()
201 //-----------------------------------------------------------------------------
202 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
204 struct sockaddr_ll addr;
207 if (!WaitPackets(capSock))
212 addrLen = sizeof(addr);
214 if (recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen) < 0)
216 logger("recvfrom error: %s", strerror(errno));