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 "ether_cap.h"
49 #include "raw_ip_packet.h"
54 //-----------------------------------------------------------------------------
55 class ETHER_CAP_CREATOR
70 ETHER_CAP * GetCapturer()
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 ETHER_CAP_CREATOR ecc;
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 BASE_PLUGIN * GetPlugin()
84 return ecc.GetCapturer();
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 const string ETHER_CAP::GetVersion() const
91 return "Ether_cap v.1.2";
93 //-----------------------------------------------------------------------------
94 ETHER_CAP::ETHER_CAP()
101 //-----------------------------------------------------------------------------
102 void ETHER_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
106 //-----------------------------------------------------------------------------
107 const string & ETHER_CAP::GetStrError() const
111 //-----------------------------------------------------------------------------
112 int ETHER_CAP::Start()
117 if (EthCapOpen() < 0)
119 errorStr = "Cannot open socket!";
120 printfd(__FILE__, "Cannot open socket\n");
126 if (pthread_create(&thread, NULL, Run, this) == 0)
131 errorStr = "Cannot create thread.";
132 printfd(__FILE__, "Cannot create thread\n");
135 //-----------------------------------------------------------------------------
136 int ETHER_CAP::Stop()
143 //5 seconds to thread stops itself
144 for (int i = 0; i < 25 && isRunning; i++)
148 //after 5 seconds waiting thread still running. now killing it
151 if (pthread_kill(thread, SIGUSR1))
153 errorStr = "Cannot kill thread.";
156 for (int i = 0; i < 25 && isRunning; ++i)
160 errorStr = "ETHER_CAP not stopped.";
161 printfd(__FILE__, "Cannot stop thread\n");
166 pthread_join(thread, NULL);
173 //-----------------------------------------------------------------------------
174 bool ETHER_CAP::IsRunning()
178 //-----------------------------------------------------------------------------
179 void * ETHER_CAP::Run(void * d)
181 ETHER_CAP * dc = (ETHER_CAP *)d;
182 dc->isRunning = true;
194 char ethip[sizeof(ETH_IP)];
196 memset(ðip, 0, sizeof(ETH_IP));
198 ethIP = (ETH_IP *)ðip;
199 ethIP->rp.dataLen = -1;
205 if (dc->EthCapRead(ðip, 68 + 14, &iface))
210 if (ethIP->ethHdr[7] != 0x8)
213 dc->traffCnt->Process(ethIP->rp);
216 dc->isRunning = false;
219 //-----------------------------------------------------------------------------
220 uint16_t ETHER_CAP::GetStartPosition() const
224 //-----------------------------------------------------------------------------
225 uint16_t ETHER_CAP::GetStopPosition() const
229 //-----------------------------------------------------------------------------
230 int ETHER_CAP::EthCapOpen()
232 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
235 //-----------------------------------------------------------------------------
236 int ETHER_CAP::EthCapClose()
241 //-----------------------------------------------------------------------------
242 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
244 struct sockaddr_ll addr;
247 if (!WaitPackets(capSock))
252 addrLen = sizeof(addr);
254 res = recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen);
260 printfd(__FILE__, "Error on recvfrom: '%s'\n", strerror(errno));
267 //-----------------------------------------------------------------------------
268 bool ETHER_CAP::WaitPackets(int sd) const
278 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
279 if (res == -1) // Error
283 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
288 if (res == 0) // Timeout