]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
Fix TRAFFCOUNTER in plugins
[stg.git] / projects / stargazer / plugins / capture / ether_linux / ether_cap.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18 Date: 18.09.2002
19 */
20
21 /*
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 */
24
25 /*
26 $Revision: 1.23 $
27 $Date: 2009/12/13 13:45:13 $
28 */
29
30
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <linux/if_ether.h>
41 #include <linux/if_packet.h>
42 #include <signal.h>
43
44 #include <sys/ioctl.h>
45 #include <net/if.h>
46
47 #include "ether_cap.h"
48 #include "common.h"
49 #include "raw_ip_packet.h"
50 #include "traffcounter.h"
51
52 //#define CAP_DEBUG 1
53
54 //-----------------------------------------------------------------------------
55 class ETHER_CAP_CREATOR {
56 private:
57     ETHER_CAP * ec;
58
59 public:
60     ETHER_CAP_CREATOR()
61         : ec(new ETHER_CAP())
62         {
63         }
64     ~ETHER_CAP_CREATOR()
65         {
66         delete ec;
67         }
68
69     ETHER_CAP * GetCapturer()
70         {
71         return ec;
72         }
73 };
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 ETHER_CAP_CREATOR ecc;
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 PLUGIN * GetPlugin()
82 {
83 return ecc.GetCapturer();
84 }
85 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------        
88 const std::string ETHER_CAP::GetVersion() const
89 {
90 return "Ether_cap v.1.2";
91 }
92 //-----------------------------------------------------------------------------
93 ETHER_CAP::ETHER_CAP()
94     : nonstop(false),
95       isRunning(false),
96       capSock(-1),
97       traffCnt(NULL)
98 {
99 }
100 //-----------------------------------------------------------------------------
101 int ETHER_CAP::Start()
102 {
103 if (isRunning)
104     return 0;
105
106 if (EthCapOpen() < 0)
107     {
108     errorStr = "Cannot open socket!";
109     printfd(__FILE__, "Cannot open socket\n");
110     return -1;
111     }
112
113 nonstop = true;
114
115 if (pthread_create(&thread, NULL, Run, this) == 0)
116     {
117     return 0;
118     }
119
120 errorStr = "Cannot create thread.";
121 printfd(__FILE__, "Cannot create thread\n");
122 return -1;
123 }
124 //-----------------------------------------------------------------------------
125 int ETHER_CAP::Stop()
126 {
127 if (!isRunning)
128     return 0;
129
130 nonstop = false;
131
132 //5 seconds to thread stops itself
133 for (int i = 0; i < 25 && isRunning; i++)
134     {
135     usleep(200000);
136     }
137 //after 5 seconds waiting thread still running. now killing it
138 if (isRunning)
139     {
140     if (pthread_kill(thread, SIGUSR1))
141         {
142         errorStr = "Cannot kill thread.";
143         return -1;
144         }
145     for (int i = 0; i < 25 && isRunning; ++i)
146         usleep(200000);
147     if (isRunning)
148         {
149         errorStr = "ETHER_CAP not stopped.";
150         printfd(__FILE__, "Cannot stop thread\n");
151         return -1;
152         }
153     else
154         {
155         pthread_join(thread, NULL);
156         }
157     }
158
159 EthCapClose();
160 return 0;
161 }
162 //-----------------------------------------------------------------------------
163 void * ETHER_CAP::Run(void * d)
164 {
165 ETHER_CAP * dc = (ETHER_CAP *)d;
166 dc->isRunning = true;
167
168 struct ETH_IP
169 {
170 uint16_t    ethHdr[8];
171 RAW_PACKET  rp;
172 char        padding[4];
173 char        padding1[8];
174 };
175
176 ETH_IP * ethIP;
177
178 char ethip[sizeof(ETH_IP)];
179
180 memset(&ethip, 0, sizeof(ETH_IP));
181
182 ethIP = (ETH_IP *)&ethip;
183 ethIP->rp.dataLen = -1;
184
185 char * iface = NULL;
186
187 while (dc->nonstop)
188     {
189     if (dc->EthCapRead(&ethip, 68 + 14, &iface))
190         {
191         continue;
192         }
193
194     if (ethIP->ethHdr[7] != 0x8)
195         continue;
196
197     dc->traffCnt->Process(ethIP->rp);
198     }
199
200 dc->isRunning = false;
201 return NULL;
202 }
203 //-----------------------------------------------------------------------------
204 int ETHER_CAP::EthCapOpen()
205 {
206 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
207 return capSock;
208 }
209 //-----------------------------------------------------------------------------
210 int ETHER_CAP::EthCapClose()
211 {
212 close(capSock);
213 return 0;
214 }
215 //-----------------------------------------------------------------------------
216 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
217 {
218 struct sockaddr_ll  addr;
219 int addrLen, res;
220
221 if (!WaitPackets(capSock))
222     {
223     return ENODATA;
224     }
225
226 addrLen = sizeof(addr);
227
228 res = recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen);
229
230 if (-1 == res)
231     {
232     if (errno != EINTR)
233         {
234         printfd(__FILE__, "Error on recvfrom: '%s'\n", strerror(errno));
235         }
236     return ENODATA;
237     }
238
239 return 0;
240 }
241 //-----------------------------------------------------------------------------
242 bool ETHER_CAP::WaitPackets(int sd) const
243 {
244 fd_set rfds;
245 FD_ZERO(&rfds);
246 FD_SET(sd, &rfds);
247
248 struct timeval tv;
249 tv.tv_sec = 0;
250 tv.tv_usec = 500000;
251
252 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
253 if (res == -1) // Error
254     {
255     if (errno != EINTR)
256         {
257         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
258         }
259     return false;
260     }
261
262 if (res == 0) // Timeout
263     {
264     return false;
265     }
266
267 return true;
268 }