]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
Fix Divert Socket capture plugin compilation errors
[stg.git] / projects / stargazer / plugins / capture / divert_freebsd / divert_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 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
19 */
20
21 /*
22 $Revision: 1.13 $
23 $Date: 2010/09/10 06:43:03 $
24 */
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28
29 #include <sys/uio.h>
30 #include <sys/time.h>
31 #include <sys/ioctl.h>
32 #include <sys/poll.h>
33
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #include <cstdio>
38 #include <cstring>
39 #include <cerrno>
40 #include <cstdlib>
41 #include <csignal>
42
43 #include <algorithm>
44 #include <vector>
45
46 #include "stg/common.h"
47 #include "stg/traffcounter.h"
48 #include "divert_cap.h"
49
50 #define BUFF_LEN (16384) /* max mtu -> lo=16436  TODO why?*/
51
52 //-----------------------------------------------------------------------------
53 struct DIVERT_DATA {
54 int sock;
55 short int port;
56 unsigned char buffer[BUFF_LEN];
57 char iface[10];
58 };
59 //-----------------------------------------------------------------------------
60 pollfd pollddiv;
61 DIVERT_DATA cddiv;  //capture data
62 //-----------------------------------------------------------------------------
63 class DIVERT_CAP_CREATOR {
64 private:
65     DIVERT_CAP * divc;
66
67 public:
68     DIVERT_CAP_CREATOR()
69         : divc(new DIVERT_CAP())
70         {
71         }
72     ~DIVERT_CAP_CREATOR()
73         {
74         delete divc;
75         }
76
77     DIVERT_CAP * GetCapturer()
78     {
79     return divc;
80     }
81 };
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 DIVERT_CAP_CREATOR dcc;
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 PLUGIN * GetPlugin()
90 {
91 return dcc.GetCapturer();
92 }
93 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 const std::string DIVERT_CAP::GetVersion() const
97 {
98 return "Divert_cap v.1.0";
99 }
100 //-----------------------------------------------------------------------------
101 DIVERT_CAP::DIVERT_CAP()
102     : port(0),
103       nonstop(false),
104       isRunning(false),
105       traffCnt(NULL)
106 {
107 }
108 //-----------------------------------------------------------------------------
109 int DIVERT_CAP::Start()
110 {
111 if (isRunning)
112     return 0;
113
114 if (DivertCapOpen() < 0)
115     {
116     errorStr = "Cannot open socket!";
117     printfd(__FILE__, "Cannot open socket\n");
118     return -1;
119     }
120
121 nonstop = true;
122
123 if (pthread_create(&thread, NULL, Run, this) == 0)
124     {
125     return 0;
126     }
127
128 errorStr = "Cannot create thread.";
129 printfd(__FILE__, "Cannot create thread\n");
130 return -1;
131 }
132 //-----------------------------------------------------------------------------
133 int DIVERT_CAP::Stop()
134 {
135 if (!isRunning)
136     return 0;
137
138 DivertCapClose();
139
140 nonstop = false;
141
142 //5 seconds to thread stops itself
143 int i;
144 for (i = 0; i < 25; i++)
145     {
146     if (!isRunning)
147         break;
148
149     usleep(200000);
150     }
151
152 //after 5 seconds waiting thread still running. now killing it
153 if (isRunning)
154     {
155     if (pthread_kill(thread, SIGINT))
156         {
157         errorStr = "Cannot kill thread.";
158         printfd(__FILE__, "Cannot kill thread\n");
159         return -1;
160         }
161     }
162
163 return 0;
164 }
165 //-----------------------------------------------------------------------------
166 void * DIVERT_CAP::Run(void * d)
167 {
168 DIVERT_CAP * dc = (DIVERT_CAP *)d;
169 dc->isRunning = true;
170
171 char buffer[64];
172 while (dc->nonstop)
173     {
174     RAW_PACKET rp;
175     dc->DivertCapRead(buffer, 64, NULL);
176
177     if (buffer[12] != 0x8)
178         continue;
179
180     memcpy(rp.pckt, &buffer[14], pcktSize);
181
182     dc->traffCnt->Process(rp);
183     }
184
185 dc->isRunning = false;
186 return NULL;
187 }
188 //-----------------------------------------------------------------------------
189 int DIVERT_CAP::DivertCapOpen()
190 {
191 memset(&pollddiv, 0, sizeof(pollddiv));
192 memset(&cddiv, 0, sizeof(DIVERT_DATA));
193
194 strcpy(cddiv.iface, "foo");
195 cddiv.port = port;
196
197 DivertCapOpen(0);
198 pollddiv.events = POLLIN;
199 pollddiv.fd = cddiv.sock;
200
201 return 0;
202 }
203 //-----------------------------------------------------------------------------
204 int DIVERT_CAP::DivertCapOpen(int)
205 {
206 int ret;
207 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
208 if (cddiv.sock < 0)
209     {
210     errorStr = "Create divert socket error.";
211     printfd(__FILE__, "Cannot create divert socket\n");
212     return -1;
213     }
214
215 struct sockaddr_in divAddr;
216
217 memset(&divAddr, 0, sizeof(divAddr));
218
219 divAddr.sin_family = AF_INET;
220 divAddr.sin_port = htons(cddiv.port);
221 divAddr.sin_addr.s_addr = INADDR_ANY;
222
223 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
224
225 if (ret < 0)
226     {
227     errorStr = "Bind divert socket error.";
228     printfd(__FILE__, "Cannot bind divert socket\n");
229     return -1;
230     }
231
232 return cddiv.sock;
233 }
234 //-----------------------------------------------------------------------------
235 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
236 {
237 poll(&pollddiv, 1, -1);
238
239 if (pollddiv.revents & POLLIN)
240     {
241     DivertCapRead(b, blen, iface, 0);
242     pollddiv.revents = 0;
243     return 0;
244     }
245
246 return 0;
247 }
248 //-----------------------------------------------------------------------------
249 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
250 {
251 static char buf[BUFF_LEN];
252 static struct sockaddr_in divertaddr;
253 static int bytes;
254 static socklen_t divertaddrSize = sizeof(divertaddr);
255
256 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
257                        0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
258     {
259     memcpy(b + 14, buf, blen - 14);
260     b[12] = 0x8;
261
262     if (iface)
263         *iface = cddiv.iface;
264
265     sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
266     }
267
268 return 0;
269 }
270 //-----------------------------------------------------------------------------
271 int DIVERT_CAP::DivertCapClose()
272 {
273 close(cddiv.sock);
274 return 0;
275 }
276 //-----------------------------------------------------------------------------
277 int DIVERT_CAP::ParseSettings()
278 {
279 int p;
280 PARAM_VALUE pv;
281 std::vector<PARAM_VALUE>::const_iterator pvi;
282
283 pv.param = "Port";
284 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
285 if (pvi == settings.moduleParams.end())
286     {
287     port = 15701;
288     return 0;
289     }
290
291 if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
292     {
293     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
294     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
295     return -1;
296     }
297
298 port = p;
299
300 return 0;
301 }
302 //-----------------------------------------------------------------------------
303 int DIVERT_CAP::ParseIntInRange(const std::string & str, int min, int max, int * val)
304 {
305 if (str2x(str.c_str(), *val))
306     {
307     errorStr = "Incorrect value \'" + str + "\'.";
308     return -1;
309     }
310 if (*val < min || *val > max)
311     {
312     errorStr = "Value \'" + str + "\' out of range.";
313     return -1;
314     }
315 return 0;
316 }
317 //-----------------------------------------------------------------------------