]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
Code deduplication (plugin creation via template PLUGIN_CREATOR)
[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 "stg/common.h"
48 #include "stg/raw_ip_packet.h"
49 #include "stg/traffcounter.h"
50 #include "stg/plugin_creator.h"
51 #include "ether_cap.h"
52
53 //#define CAP_DEBUG 1
54
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 PLUGIN_CREATOR<ETHER_CAP> ecc;
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 PLUGIN * GetPlugin()
63 {
64 return ecc.GetPlugin();
65 }
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------        
69 const std::string ETHER_CAP::GetVersion() const
70 {
71 return "Ether_cap v.1.2";
72 }
73 //-----------------------------------------------------------------------------
74 ETHER_CAP::ETHER_CAP()
75     : nonstop(false),
76       isRunning(false),
77       capSock(-1),
78       traffCnt(NULL)
79 {
80 }
81 //-----------------------------------------------------------------------------
82 int ETHER_CAP::Start()
83 {
84 if (isRunning)
85     return 0;
86
87 if (EthCapOpen() < 0)
88     {
89     errorStr = "Cannot open socket!";
90     printfd(__FILE__, "Cannot open socket\n");
91     return -1;
92     }
93
94 nonstop = true;
95
96 if (pthread_create(&thread, NULL, Run, this) == 0)
97     {
98     return 0;
99     }
100
101 errorStr = "Cannot create thread.";
102 printfd(__FILE__, "Cannot create thread\n");
103 return -1;
104 }
105 //-----------------------------------------------------------------------------
106 int ETHER_CAP::Stop()
107 {
108 if (!isRunning)
109     return 0;
110
111 nonstop = false;
112
113 //5 seconds to thread stops itself
114 for (int i = 0; i < 25 && isRunning; i++)
115     {
116     usleep(200000);
117     }
118 //after 5 seconds waiting thread still running. now killing it
119 if (isRunning)
120     {
121     if (pthread_kill(thread, SIGUSR1))
122         {
123         errorStr = "Cannot kill thread.";
124         return -1;
125         }
126     for (int i = 0; i < 25 && isRunning; ++i)
127         usleep(200000);
128     if (isRunning)
129         {
130         errorStr = "ETHER_CAP not stopped.";
131         printfd(__FILE__, "Cannot stop thread\n");
132         return -1;
133         }
134     else
135         {
136         pthread_join(thread, NULL);
137         }
138     }
139
140 EthCapClose();
141 return 0;
142 }
143 //-----------------------------------------------------------------------------
144 void * ETHER_CAP::Run(void * d)
145 {
146 ETHER_CAP * dc = (ETHER_CAP *)d;
147 dc->isRunning = true;
148
149 struct ETH_IP
150 {
151 uint16_t    ethHdr[8];
152 RAW_PACKET  rp;
153 char        padding[4];
154 char        padding1[8];
155 };
156
157 ETH_IP * ethIP;
158
159 char ethip[sizeof(ETH_IP)];
160
161 memset(&ethip, 0, sizeof(ETH_IP));
162
163 ethIP = (ETH_IP *)&ethip;
164 ethIP->rp.dataLen = -1;
165
166 char * iface = NULL;
167
168 while (dc->nonstop)
169     {
170     if (dc->EthCapRead(&ethip, 68 + 14, &iface))
171         {
172         continue;
173         }
174
175     if (ethIP->ethHdr[7] != 0x8)
176         continue;
177
178     dc->traffCnt->Process(ethIP->rp);
179     }
180
181 dc->isRunning = false;
182 return NULL;
183 }
184 //-----------------------------------------------------------------------------
185 int ETHER_CAP::EthCapOpen()
186 {
187 capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
188 return capSock;
189 }
190 //-----------------------------------------------------------------------------
191 int ETHER_CAP::EthCapClose()
192 {
193 close(capSock);
194 return 0;
195 }
196 //-----------------------------------------------------------------------------
197 int ETHER_CAP::EthCapRead(void * buffer, int blen, char **)
198 {
199 struct sockaddr_ll  addr;
200 int addrLen, res;
201
202 if (!WaitPackets(capSock))
203     {
204     return ENODATA;
205     }
206
207 addrLen = sizeof(addr);
208
209 res = recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen);
210
211 if (-1 == res)
212     {
213     if (errno != EINTR)
214         {
215         printfd(__FILE__, "Error on recvfrom: '%s'\n", strerror(errno));
216         }
217     return ENODATA;
218     }
219
220 return 0;
221 }
222 //-----------------------------------------------------------------------------
223 bool ETHER_CAP::WaitPackets(int sd) const
224 {
225 fd_set rfds;
226 FD_ZERO(&rfds);
227 FD_SET(sd, &rfds);
228
229 struct timeval tv;
230 tv.tv_sec = 0;
231 tv.tv_usec = 500000;
232
233 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
234 if (res == -1) // Error
235     {
236     if (errno != EINTR)
237         {
238         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
239         }
240     return false;
241     }
242
243 if (res == 0) // Timeout
244     {
245     return false;
246     }
247
248 return true;
249 }