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