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