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