]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
Introduced logger for plugins.
[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) == 0)
109     {
110     return 0;
111     }
112
113 errorStr = "Cannot create thread.";
114 printfd(__FILE__, "Cannot create thread\n");
115 return -1;
116 }
117 //-----------------------------------------------------------------------------
118 int DIVERT_CAP::Stop()
119 {
120 if (!isRunning)
121     return 0;
122
123 DivertCapClose();
124
125 nonstop = false;
126
127 //5 seconds to thread stops itself
128 int i;
129 for (i = 0; i < 25; i++)
130     {
131     if (!isRunning)
132         break;
133
134     struct timespec ts = {0, 200000000};
135     nanosleep(&ts, NULL);
136     }
137
138 //after 5 seconds waiting thread still running. now killing it
139 if (isRunning)
140     {
141     if (pthread_kill(thread, SIGINT))
142         {
143         errorStr = "Cannot kill thread.";
144         printfd(__FILE__, "Cannot kill thread\n");
145         return -1;
146         }
147     }
148
149 return 0;
150 }
151 //-----------------------------------------------------------------------------
152 void * DIVERT_CAP::Run(void * d)
153 {
154 sigset_t signalSet;
155 sigfillset(&signalSet);
156 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
157
158 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
159 dc->isRunning = true;
160
161 char buffer[64];
162 while (dc->nonstop)
163     {
164     RAW_PACKET rp;
165     dc->DivertCapRead(buffer, 64, NULL);
166
167     if (buffer[12] != 0x8)
168         continue;
169
170     memcpy(rp.rawPacket.pckt, &buffer[14], pcktSize);
171
172     dc->traffCnt->Process(rp);
173     }
174
175 dc->isRunning = false;
176 return NULL;
177 }
178 //-----------------------------------------------------------------------------
179 int DIVERT_CAP::DivertCapOpen()
180 {
181 memset(&pollddiv, 0, sizeof(pollddiv));
182 memset(&cddiv, 0, sizeof(DIVERT_DATA));
183
184 strcpy(cddiv.iface, "foo");
185 cddiv.port = port;
186
187 DivertCapOpen(0);
188 pollddiv.events = POLLIN;
189 pollddiv.fd = cddiv.sock;
190
191 return 0;
192 }
193 //-----------------------------------------------------------------------------
194 int DIVERT_CAP::DivertCapOpen(int)
195 {
196 int ret;
197 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
198 if (cddiv.sock < 0)
199     {
200     errorStr = "Create divert socket error.";
201     printfd(__FILE__, "Cannot create divert socket\n");
202     return -1;
203     }
204
205 struct sockaddr_in divAddr;
206
207 memset(&divAddr, 0, sizeof(divAddr));
208
209 divAddr.sin_family = AF_INET;
210 divAddr.sin_port = htons(cddiv.port);
211 divAddr.sin_addr.s_addr = INADDR_ANY;
212
213 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
214
215 if (ret < 0)
216     {
217     errorStr = "Bind divert socket error.";
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         sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
257     }
258
259 return 0;
260 }
261 //-----------------------------------------------------------------------------
262 int DIVERT_CAP::DivertCapClose()
263 {
264 close(cddiv.sock);
265 return 0;
266 }
267 //-----------------------------------------------------------------------------
268 int DIVERT_CAP::ParseSettings()
269 {
270 int p;
271 PARAM_VALUE pv;
272 std::vector<PARAM_VALUE>::const_iterator pvi;
273
274 pv.param = "Port";
275 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
276 if (pvi == settings.moduleParams.end())
277     {
278     p = 15701;
279     }
280 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
281     {
282     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
283     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
284     return -1;
285     }
286
287 port = p;
288
289 bool d = false;
290 pv.param = "DisableForwarding";
291 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
292 if (pvi == settings.moduleParams.end())
293     {
294     disableForwarding = false;
295     }
296 else if (ParseYesNo(pvi->value[0], &d))
297     {
298     errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
299     printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
300     return -1;
301     }
302
303 disableForwarding = d;
304
305 return 0;
306 }
307 //-----------------------------------------------------------------------------