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 $
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34 #include <sys/types.h>
36 #include <linux/if_ether.h>
37 #include <linux/if_packet.h>
38 #include <sys/ioctl.h>
47 #include "stg/common.h"
48 #include "stg/raw_ip_packet.h"
49 #include "stg/traffcounter.h"
50 #include "stg/plugin_creator.h"
52 #include "ether_cap.h"
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
61 PLUGIN_CREATOR<ETHER_CAP> ecc;
64 extern "C" PLUGIN * GetPlugin();
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
70 return ecc.GetPlugin();
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 std::string ETHER_CAP::GetVersion() const
77 return "Ether_cap v.1.2";
79 //-----------------------------------------------------------------------------
80 ETHER_CAP::ETHER_CAP()
87 logger(GetPluginLogger(GetStgLogger(), "cap_ether"))
90 //-----------------------------------------------------------------------------
91 int ETHER_CAP::Start()
98 errorStr = "Cannot open socket!";
99 printfd(__FILE__, "Cannot open socket\n");
105 if (pthread_create(&thread, NULL, Run, this))
107 errorStr = "Cannot create thread.";
108 logger("Cannot create thread.");
109 printfd(__FILE__, "Cannot create thread\n");
115 //-----------------------------------------------------------------------------
116 int ETHER_CAP::Stop()
123 //5 seconds to thread stops itself
124 for (int i = 0; i < 25 && isRunning; i++)
126 struct timespec ts = {0, 200000000};
127 nanosleep(&ts, NULL);
129 //after 5 seconds waiting thread still running. now killing it
132 if (pthread_kill(thread, SIGUSR1))
134 errorStr = "Cannot kill thread.";
135 logger("Cannot send signal to thread.");
138 for (int i = 0; i < 25 && isRunning; ++i)
140 struct timespec ts = {0, 200000000};
141 nanosleep(&ts, NULL);
145 errorStr = "ETHER_CAP not stopped.";
146 logger("Cannot stop thread.");
147 printfd(__FILE__, "Cannot stop thread\n");
152 pthread_join(thread, NULL);
159 //-----------------------------------------------------------------------------
160 void * ETHER_CAP::Run(void * d)
163 sigfillset(&signalSet);
164 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
166 ETHER_CAP * dc = static_cast<ETHER_CAP *>(d);
167 dc->isRunning = true;
177 char ethip[sizeof(ETH_IP)];
179 memset(ðip, 0, sizeof(ETH_IP));
181 ETH_IP * ethIP = static_cast<ETH_IP *>(static_cast<void *>(ðip));
182 ethIP->rp.dataLen = -1;
188 if (dc->EthCapRead(ðip, 68 + 14, &iface))
193 if (ethIP->ethHdr[7] != 0x8)
196 dc->traffCnt->Process(ethIP->rp);
199 dc->isRunning = false;
202 //-----------------------------------------------------------------------------
203 int ETHER_CAP::EthCapOpen()
205 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
207 logger("Cannot create socket: %s", strerror(errno));
210 //-----------------------------------------------------------------------------
211 int ETHER_CAP::EthCapClose()
216 //-----------------------------------------------------------------------------
217 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
219 struct sockaddr_ll addr;
222 if (!WaitPackets(capSock))
227 addrLen = sizeof(addr);
229 if (recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen) < 0)
231 logger("recvfrom error: %s", strerror(errno));