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