]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
41817d92f7821aecf18da8c3f9443a22027dc8b0
[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 "stg/plugin_creator.h"
49 #include "divert_cap.h"
50
51 #define BUFF_LEN (16384) /* max mtu -> lo=16436  TODO why?*/
52
53 //-----------------------------------------------------------------------------
54 struct DIVERT_DATA {
55 int sock;
56 short int port;
57 char iface[10];
58 };
59 //-----------------------------------------------------------------------------
60 pollfd pollddiv;
61 DIVERT_DATA cddiv;  //capture data
62 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 namespace
66 {
67 PLUGIN_CREATOR<DIVERT_CAP> dcc;
68 }
69
70 extern "C" PLUGIN * GetPlugin();
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 PLUGIN * GetPlugin()
75 {
76 return dcc.GetPlugin();
77 }
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 std::string DIVERT_CAP::GetVersion() const
82 {
83 return "cap_divert v.1.0";
84 }
85 //-----------------------------------------------------------------------------
86 DIVERT_CAP::DIVERT_CAP()
87     : settings(),
88       port(0),
89       disableForwarding(false),
90       errorStr(),
91       thread(),
92       nonstop(false),
93       isRunning(false),
94       traffCnt(NULL),
95       logger(GetPluginLogger(GetStgLogger(), "cap_divert"))
96 {
97 }
98 //-----------------------------------------------------------------------------
99 int DIVERT_CAP::Start()
100 {
101 if (isRunning)
102     return 0;
103
104 if (DivertCapOpen() < 0)
105     {
106     errorStr = "Cannot open socket!";
107     printfd(__FILE__, "Cannot open socket\n");
108     return -1;
109     }
110
111 nonstop = true;
112
113 if (pthread_create(&thread, NULL, Run, this))
114     {
115     errorStr = "Cannot create thread.";
116     logger("Cannot create thread.");
117     printfd(__FILE__, "Cannot create thread\n");
118     return -1;
119     }
120
121 return 0;
122 }
123 //-----------------------------------------------------------------------------
124 int DIVERT_CAP::Stop()
125 {
126 if (!isRunning)
127     return 0;
128
129 DivertCapClose();
130
131 nonstop = false;
132
133 //5 seconds to thread stops itself
134 int i;
135 for (i = 0; i < 25; i++)
136     {
137     if (!isRunning)
138         break;
139
140     struct timespec ts = {0, 200000000};
141     nanosleep(&ts, NULL);
142     }
143
144 //after 5 seconds waiting thread still running. now killing it
145 if (isRunning)
146     {
147     if (pthread_kill(thread, SIGINT))
148         {
149         errorStr = "Cannot kill thread.";
150         logger("Cannot send signal to thread.");
151         printfd(__FILE__, "Cannot kill thread\n");
152         return -1;
153         }
154     }
155
156 return 0;
157 }
158 //-----------------------------------------------------------------------------
159 void * DIVERT_CAP::Run(void * d)
160 {
161 sigset_t signalSet;
162 sigfillset(&signalSet);
163 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
164
165 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
166 dc->isRunning = true;
167
168 char buffer[64];
169 while (dc->nonstop)
170     {
171     RAW_PACKET rp;
172     dc->DivertCapRead(buffer, 64, NULL);
173
174     if (buffer[12] != 0x8)
175         continue;
176
177     memcpy(rp.rawPacket.pckt, &buffer[14], pcktSize);
178
179     dc->traffCnt->Process(rp);
180     }
181
182 dc->isRunning = false;
183 return NULL;
184 }
185 //-----------------------------------------------------------------------------
186 int DIVERT_CAP::DivertCapOpen()
187 {
188 memset(&pollddiv, 0, sizeof(pollddiv));
189 memset(&cddiv, 0, sizeof(DIVERT_DATA));
190
191 strcpy(cddiv.iface, "foo");
192 cddiv.port = port;
193
194 DivertCapOpen(0);
195 pollddiv.events = POLLIN;
196 pollddiv.fd = cddiv.sock;
197
198 return 0;
199 }
200 //-----------------------------------------------------------------------------
201 int DIVERT_CAP::DivertCapOpen(int)
202 {
203 int ret;
204 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
205 if (cddiv.sock < 0)
206     {
207     errorStr = "Create divert socket error.";
208     logger("Cannot create a socket: %s", strerror(errno));
209     printfd(__FILE__, "Cannot create divert socket\n");
210     return -1;
211     }
212
213 struct sockaddr_in divAddr;
214
215 memset(&divAddr, 0, sizeof(divAddr));
216
217 divAddr.sin_family = AF_INET;
218 divAddr.sin_port = htons(cddiv.port);
219 divAddr.sin_addr.s_addr = INADDR_ANY;
220
221 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
222
223 if (ret < 0)
224     {
225     errorStr = "Bind divert socket error.";
226     logger("Cannot bind the scoket: %s", strerror(errno));
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     if (!disableForwarding)
265         {
266         if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0)
267             logger("sendto error: %s", strerror(errno));
268         }
269     }
270 else
271     {
272     if (bytes < 0)
273         logger("recvfrom error: %s", strerror(errno));
274     }
275
276 return 0;
277 }
278 //-----------------------------------------------------------------------------
279 int DIVERT_CAP::DivertCapClose()
280 {
281 close(cddiv.sock);
282 return 0;
283 }
284 //-----------------------------------------------------------------------------
285 int DIVERT_CAP::ParseSettings()
286 {
287 int p;
288 PARAM_VALUE pv;
289 std::vector<PARAM_VALUE>::const_iterator pvi;
290
291 pv.param = "Port";
292 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
293 if (pvi == settings.moduleParams.end())
294     {
295     p = 15701;
296     }
297 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
298     {
299     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
300     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
301     return -1;
302     }
303
304 port = p;
305
306 bool d = false;
307 pv.param = "DisableForwarding";
308 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
309 if (pvi == settings.moduleParams.end())
310     {
311     disableForwarding = false;
312     }
313 else if (ParseYesNo(pvi->value[0], &d))
314     {
315     errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
316     printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
317     return -1;
318     }
319
320 disableForwarding = d;
321
322 return 0;
323 }
324 //-----------------------------------------------------------------------------