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>
40 #include <linux/if_ether.h>
41 #include <linux/if_packet.h>
44 #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"
51 #include "ether_cap.h"
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 PLUGIN_CREATOR<ETHER_CAP> ecc;
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
64 return ecc.GetPlugin();
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 const std::string ETHER_CAP::GetVersion() const
71 return "Ether_cap v.1.2";
73 //-----------------------------------------------------------------------------
74 ETHER_CAP::ETHER_CAP()
83 //-----------------------------------------------------------------------------
84 int ETHER_CAP::Start()
91 errorStr = "Cannot open socket!";
92 printfd(__FILE__, "Cannot open socket\n");
98 if (pthread_create(&thread, NULL, Run, this) == 0)
103 errorStr = "Cannot create thread.";
104 printfd(__FILE__, "Cannot create thread\n");
107 //-----------------------------------------------------------------------------
108 int ETHER_CAP::Stop()
115 //5 seconds to thread stops itself
116 for (int i = 0; i < 25 && isRunning; i++)
120 //after 5 seconds waiting thread still running. now killing it
123 if (pthread_kill(thread, SIGUSR1))
125 errorStr = "Cannot kill thread.";
128 for (int i = 0; i < 25 && isRunning; ++i)
132 errorStr = "ETHER_CAP not stopped.";
133 printfd(__FILE__, "Cannot stop thread\n");
138 pthread_join(thread, NULL);
145 //-----------------------------------------------------------------------------
146 void * ETHER_CAP::Run(void * d)
148 ETHER_CAP * dc = (ETHER_CAP *)d;
149 dc->isRunning = true;
161 char ethip[sizeof(ETH_IP)];
163 memset(ðip, 0, sizeof(ETH_IP));
165 ethIP = (ETH_IP *)ðip;
166 ethIP->rp.dataLen = -1;
172 if (dc->EthCapRead(ðip, 68 + 14, &iface))
177 if (ethIP->ethHdr[7] != 0x8)
180 dc->traffCnt->Process(ethIP->rp);
183 dc->isRunning = false;
186 //-----------------------------------------------------------------------------
187 int ETHER_CAP::EthCapOpen()
189 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
192 //-----------------------------------------------------------------------------
193 int ETHER_CAP::EthCapClose()
198 //-----------------------------------------------------------------------------
199 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
201 struct sockaddr_ll addr;
204 if (!WaitPackets(capSock))
209 addrLen = sizeof(addr);
211 res = recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen);
217 printfd(__FILE__, "Error on recvfrom: '%s'\n", strerror(errno));