]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
Из плагина захвата трафика cap_ether убраны неиспользуемые типы данных и
[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
51 //#define CAP_DEBUG 1
52
53
54 //-----------------------------------------------------------------------------
55 class ETHER_CAP_CREATOR
56 {
57 private:
58     ETHER_CAP * ec;
59
60 public:
61     ETHER_CAP_CREATOR()
62         : ec(new ETHER_CAP())
63         {
64         };
65     ~ETHER_CAP_CREATOR()
66         {
67         delete ec;
68         };
69
70     ETHER_CAP * GetCapturer()
71         {
72         return ec;
73         };
74 };
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 ETHER_CAP_CREATOR ecc;
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 BASE_PLUGIN * GetPlugin()
83 {
84 return ecc.GetCapturer();
85 }
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------        
89 const string ETHER_CAP::GetVersion() const
90 {
91 return "Ether_cap v.1.2";
92 }
93 //-----------------------------------------------------------------------------
94 ETHER_CAP::ETHER_CAP()
95     : nonstop(false),
96       isRunning(false),
97       capSock(-1),
98       traffCnt(NULL)
99 {
100 }
101 //-----------------------------------------------------------------------------
102 void ETHER_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
103 {
104 traffCnt = tc;
105 }
106 //-----------------------------------------------------------------------------
107 const string & ETHER_CAP::GetStrError() const
108 {
109 return errorStr;
110 }
111 //-----------------------------------------------------------------------------
112 int ETHER_CAP::Start()
113 {
114 if (isRunning)
115     return 0;
116
117 if (EthCapOpen() < 0)
118     {
119     errorStr = "Cannot open socket!";
120     printfd(__FILE__, "Cannot open socket\n");
121     return -1;
122     }
123
124 nonstop = true;
125
126 if (pthread_create(&thread, NULL, Run, this) == 0)
127     {
128     return 0;
129     }
130
131 errorStr = "Cannot create thread.";
132 printfd(__FILE__, "Cannot create thread\n");
133 return -1;
134 }
135 //-----------------------------------------------------------------------------
136 int ETHER_CAP::Stop()
137 {
138 if (!isRunning)
139     return 0;
140
141 nonstop = false;
142
143 //5 seconds to thread stops itself
144 for (int i = 0; i < 25 && isRunning; i++)
145     {
146     usleep(200000);
147     }
148 //after 5 seconds waiting thread still running. now killing it
149 if (isRunning)
150     {
151     if (pthread_kill(thread, SIGUSR1))
152         {
153         errorStr = "Cannot kill thread.";
154         return -1;
155         }
156     for (int i = 0; i < 25 && isRunning; ++i)
157         usleep(200000);
158     if (isRunning)
159         {
160         errorStr = "ETHER_CAP not stopped.";
161         printfd(__FILE__, "Cannot stop thread\n");
162         return -1;
163         }
164     else
165         {
166         pthread_join(thread, NULL);
167         }
168     }
169
170 EthCapClose();
171 return 0;
172 }
173 //-----------------------------------------------------------------------------
174 bool ETHER_CAP::IsRunning()
175 {
176 return isRunning;
177 }
178 //-----------------------------------------------------------------------------
179 void * ETHER_CAP::Run(void * d)
180 {
181 ETHER_CAP * dc = (ETHER_CAP *)d;
182 dc->isRunning = true;
183
184 struct ETH_IP
185 {
186 uint16_t    ethHdr[8];
187 RAW_PACKET  rp;
188 char        padding[4];
189 char        padding1[8];
190 };
191
192 ETH_IP * ethIP;
193
194 char ethip[sizeof(ETH_IP)];
195
196 memset(&ethip, 0, sizeof(ETH_IP));
197
198 ethIP = (ETH_IP *)&ethip;
199 ethIP->rp.dataLen = -1;
200
201 char * iface = NULL;
202
203 while (dc->nonstop)
204     {
205     if (dc->EthCapRead(&ethip, 68 + 14, &iface))
206         {
207         continue;
208         }
209
210     if (ethIP->ethHdr[7] != 0x8)
211         continue;
212
213     dc->traffCnt->Process(ethIP->rp);
214     }
215
216 dc->isRunning = false;
217 return NULL;
218 }
219 //-----------------------------------------------------------------------------
220 uint16_t ETHER_CAP::GetStartPosition() const
221 {
222 return 10;
223 }
224 //-----------------------------------------------------------------------------
225 uint16_t ETHER_CAP::GetStopPosition() const
226 {
227 return 10;
228 }
229 //-----------------------------------------------------------------------------
230 int ETHER_CAP::EthCapOpen()
231 {
232 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
233 return capSock;
234 }
235 //-----------------------------------------------------------------------------
236 int ETHER_CAP::EthCapClose()
237 {
238 close(capSock);
239 return 0;
240 }
241 //-----------------------------------------------------------------------------
242 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
243 {
244 struct sockaddr_ll  addr;
245 int addrLen, res;
246
247 if (!WaitPackets(capSock))
248     {
249     return ENODATA;
250     }
251
252 addrLen = sizeof(addr);
253
254 res = recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen);
255
256 if (-1 == res)
257     {
258     if (errno != EINTR)
259         {
260         printfd(__FILE__, "Error on recvfrom: '%s'\n", strerror(errno));
261         }
262     return ENODATA;
263     }
264
265 return 0;
266 }
267 //-----------------------------------------------------------------------------
268 bool ETHER_CAP::WaitPackets(int sd) const
269 {
270 fd_set rfds;
271 FD_ZERO(&rfds);
272 FD_SET(sd, &rfds);
273
274 struct timeval tv;
275 tv.tv_sec = 0;
276 tv.tv_usec = 500000;
277
278 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
279 if (res == -1) // Error
280     {
281     if (errno != EINTR)
282         {
283         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
284         }
285     return false;
286     }
287
288 if (res == 0) // Timeout
289     {
290     return false;
291     }
292
293 return true;
294 }