]> git.stg.codes - stg.git/blob - stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
Fix some BSD-related issues.
[stg.git] / 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
26 #include "divert_cap.h"
27
28 #include "stg/common.h"
29 #include "stg/traffcounter.h"
30
31 #include <algorithm>
32 #include <vector>
33
34 #include <cstdio>
35 #include <cstring>
36 #include <cerrno>
37 #include <cstdlib>
38 #include <csignal>
39
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43
44 #include <sys/uio.h>
45 #include <sys/time.h>
46 #include <sys/ioctl.h>
47 #include <sys/poll.h>
48
49 #include <fcntl.h>
50 #include <unistd.h>
51
52 #define BUFF_LEN (16384) /* max mtu -> lo=16436  TODO why?*/
53
54 //-----------------------------------------------------------------------------
55 struct DIVERT_DATA {
56 int sock;
57 short int port;
58 char iface[10];
59 };
60 //-----------------------------------------------------------------------------
61 pollfd pollddiv;
62 DIVERT_DATA cddiv;  //capture data
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66
67 extern "C" STG::Plugin* GetPlugin()
68 {
69     static DIVERT_CAP plugin;
70     return &plugin;
71 }
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 std::string DIVERT_CAP::GetVersion() const
76 {
77 return "cap_divert v.1.0";
78 }
79 //-----------------------------------------------------------------------------
80 DIVERT_CAP::DIVERT_CAP()
81     : port(0),
82       disableForwarding(false),
83       nonstop(false),
84       isRunning(false),
85       traffCnt(NULL),
86       logger(STG::PluginLogger::get("cap_divert"))
87 {
88 }
89 //-----------------------------------------------------------------------------
90 int DIVERT_CAP::Start()
91 {
92 if (isRunning)
93     return 0;
94
95 if (DivertCapOpen() < 0)
96     {
97     errorStr = "Cannot open socket!";
98     printfd(__FILE__, "Cannot open socket\n");
99     return -1;
100     }
101
102 nonstop = true;
103
104 if (pthread_create(&thread, NULL, Run, this))
105     {
106     errorStr = "Cannot create thread.";
107     logger("Cannot create thread.");
108     printfd(__FILE__, "Cannot create thread\n");
109     return -1;
110     }
111
112 return 0;
113 }
114 //-----------------------------------------------------------------------------
115 int DIVERT_CAP::Stop()
116 {
117 if (!isRunning)
118     return 0;
119
120 DivertCapClose();
121
122 nonstop = false;
123
124 //5 seconds to thread stops itself
125 int i;
126 for (i = 0; i < 25; i++)
127     {
128     if (!isRunning)
129         break;
130
131     struct timespec ts = {0, 200000000};
132     nanosleep(&ts, NULL);
133     }
134
135 //after 5 seconds waiting thread still running. now killing it
136 if (isRunning)
137     {
138     if (pthread_kill(thread, SIGINT))
139         {
140         errorStr = "Cannot kill thread.";
141         logger("Cannot send signal to thread.");
142         printfd(__FILE__, "Cannot kill thread\n");
143         return -1;
144         }
145     }
146
147 return 0;
148 }
149 //-----------------------------------------------------------------------------
150 void * DIVERT_CAP::Run(void * d)
151 {
152 sigset_t signalSet;
153 sigfillset(&signalSet);
154 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
155
156 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
157 dc->isRunning = true;
158
159 char buffer[pcktSize + 14];
160 while (dc->nonstop)
161     {
162     STG::RawPacket rp;
163     dc->DivertCapRead(buffer, sizeof(buffer), NULL);
164
165     if (buffer[12] != 0x8)
166         continue;
167
168     memcpy(rp.rawPacket.pckt, &buffer[14], pcktSize);
169
170     dc->traffCnt->process(rp);
171     }
172
173 dc->isRunning = false;
174 return NULL;
175 }
176 //-----------------------------------------------------------------------------
177 int DIVERT_CAP::DivertCapOpen()
178 {
179 memset(&pollddiv, 0, sizeof(pollddiv));
180 memset(&cddiv, 0, sizeof(DIVERT_DATA));
181
182 strcpy(cddiv.iface, "foo");
183 cddiv.port = port;
184
185 DivertCapOpen(0);
186 pollddiv.events = POLLIN;
187 pollddiv.fd = cddiv.sock;
188
189 return 0;
190 }
191 //-----------------------------------------------------------------------------
192 int DIVERT_CAP::DivertCapOpen(int)
193 {
194 int ret;
195 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
196 if (cddiv.sock < 0)
197     {
198     errorStr = "Create divert socket error.";
199     logger("Cannot create a socket: %s", strerror(errno));
200     printfd(__FILE__, "Cannot create divert socket\n");
201     return -1;
202     }
203
204 struct sockaddr_in divAddr;
205
206 memset(&divAddr, 0, sizeof(divAddr));
207
208 divAddr.sin_family = AF_INET;
209 divAddr.sin_port = htons(cddiv.port);
210 divAddr.sin_addr.s_addr = INADDR_ANY;
211
212 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
213
214 if (ret < 0)
215     {
216     errorStr = "Bind divert socket error.";
217     logger("Cannot bind the scoket: %s", strerror(errno));
218     printfd(__FILE__, "Cannot bind divert socket\n");
219     return -1;
220     }
221
222 return cddiv.sock;
223 }
224 //-----------------------------------------------------------------------------
225 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
226 {
227 poll(&pollddiv, 1, -1);
228
229 if (pollddiv.revents & POLLIN)
230     {
231     DivertCapRead(b, blen, iface, 0);
232     pollddiv.revents = 0;
233     return 0;
234     }
235
236 return 0;
237 }
238 //-----------------------------------------------------------------------------
239 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
240 {
241 static char buf[BUFF_LEN];
242 static struct sockaddr_in divertaddr;
243 static int bytes;
244 static socklen_t divertaddrSize = sizeof(divertaddr);
245
246 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
247                        0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
248     {
249     memcpy(b + 14, buf, blen - 14);
250     b[12] = 0x8;
251
252     if (iface)
253         *iface = cddiv.iface;
254
255     if (!disableForwarding)
256         {
257         if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0)
258             logger("sendto error: %s", strerror(errno));
259         }
260     }
261 else
262     {
263     if (bytes < 0)
264         logger("recvfrom error: %s", strerror(errno));
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 STG::ParamValue pv;
280 std::vector<STG::ParamValue>::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() || pvi->value.empty())
285     {
286     p = 15701;
287     }
288 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
289     {
290     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
291     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
292     return -1;
293     }
294
295 port = p;
296
297 bool d = false;
298 pv.param = "DisableForwarding";
299 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
300 if (pvi == settings.moduleParams.end() || pvi->value.empty())
301     {
302     disableForwarding = false;
303     }
304 else if (ParseYesNo(pvi->value[0], &d))
305     {
306     errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
307     printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
308     return -1;
309     }
310
311 disableForwarding = d;
312
313 return 0;
314 }
315 //-----------------------------------------------------------------------------