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