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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
23 $Date: 2010/09/10 06:43:03 $
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
31 #include <sys/ioctl.h>
46 #include "stg/common.h"
47 #include "stg/traffcounter.h"
48 #include "stg/plugin_creator.h"
49 #include "divert_cap.h"
51 #define BUFF_LEN (16384) /* max mtu -> lo=16436 TODO why?*/
53 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 DIVERT_DATA cddiv; //capture data
62 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
67 PLUGIN_CREATOR<DIVERT_CAP> dcc;
70 extern "C" PLUGIN * GetPlugin();
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
76 return dcc.GetPlugin();
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 std::string DIVERT_CAP::GetVersion() const
83 return "cap_divert v.1.0";
85 //-----------------------------------------------------------------------------
86 DIVERT_CAP::DIVERT_CAP()
89 disableForwarding(false),
95 logger(GetPluginLogger(GetStgLogger(), "cap_divert"))
98 //-----------------------------------------------------------------------------
99 int DIVERT_CAP::Start()
104 if (DivertCapOpen() < 0)
106 errorStr = "Cannot open socket!";
107 printfd(__FILE__, "Cannot open socket\n");
113 if (pthread_create(&thread, NULL, Run, this))
115 errorStr = "Cannot create thread.";
116 logger("Cannot create thread.");
117 printfd(__FILE__, "Cannot create thread\n");
123 //-----------------------------------------------------------------------------
124 int DIVERT_CAP::Stop()
133 //5 seconds to thread stops itself
135 for (i = 0; i < 25; i++)
140 struct timespec ts = {0, 200000000};
141 nanosleep(&ts, NULL);
144 //after 5 seconds waiting thread still running. now killing it
147 if (pthread_kill(thread, SIGINT))
149 errorStr = "Cannot kill thread.";
150 logger("Cannot send signal to thread.");
151 printfd(__FILE__, "Cannot kill thread\n");
158 //-----------------------------------------------------------------------------
159 void * DIVERT_CAP::Run(void * d)
162 sigfillset(&signalSet);
163 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
165 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
166 dc->isRunning = true;
168 char buffer[pcktSize + 14];
172 dc->DivertCapRead(buffer, sizeof(buffer), NULL);
174 if (buffer[12] != 0x8)
177 memcpy(rp.rawPacket.pckt, &buffer[14], pcktSize);
179 dc->traffCnt->Process(rp);
182 dc->isRunning = false;
185 //-----------------------------------------------------------------------------
186 int DIVERT_CAP::DivertCapOpen()
188 memset(&pollddiv, 0, sizeof(pollddiv));
189 memset(&cddiv, 0, sizeof(DIVERT_DATA));
191 strcpy(cddiv.iface, "foo");
195 pollddiv.events = POLLIN;
196 pollddiv.fd = cddiv.sock;
200 //-----------------------------------------------------------------------------
201 int DIVERT_CAP::DivertCapOpen(int)
204 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
207 errorStr = "Create divert socket error.";
208 logger("Cannot create a socket: %s", strerror(errno));
209 printfd(__FILE__, "Cannot create divert socket\n");
213 struct sockaddr_in divAddr;
215 memset(&divAddr, 0, sizeof(divAddr));
217 divAddr.sin_family = AF_INET;
218 divAddr.sin_port = htons(cddiv.port);
219 divAddr.sin_addr.s_addr = INADDR_ANY;
221 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
225 errorStr = "Bind divert socket error.";
226 logger("Cannot bind the scoket: %s", strerror(errno));
227 printfd(__FILE__, "Cannot bind divert socket\n");
233 //-----------------------------------------------------------------------------
234 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
236 poll(&pollddiv, 1, -1);
238 if (pollddiv.revents & POLLIN)
240 DivertCapRead(b, blen, iface, 0);
241 pollddiv.revents = 0;
247 //-----------------------------------------------------------------------------
248 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
250 static char buf[BUFF_LEN];
251 static struct sockaddr_in divertaddr;
253 static socklen_t divertaddrSize = sizeof(divertaddr);
255 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
256 0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
258 memcpy(b + 14, buf, blen - 14);
262 *iface = cddiv.iface;
264 if (!disableForwarding)
266 if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0)
267 logger("sendto error: %s", strerror(errno));
273 logger("recvfrom error: %s", strerror(errno));
278 //-----------------------------------------------------------------------------
279 int DIVERT_CAP::DivertCapClose()
284 //-----------------------------------------------------------------------------
285 int DIVERT_CAP::ParseSettings()
289 std::vector<PARAM_VALUE>::const_iterator pvi;
292 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
293 if (pvi == settings.moduleParams.end())
297 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
299 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
300 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
307 pv.param = "DisableForwarding";
308 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
309 if (pvi == settings.moduleParams.end())
311 disableForwarding = false;
313 else if (ParseYesNo(pvi->value[0], &d))
315 errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
316 printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
320 disableForwarding = d;
324 //-----------------------------------------------------------------------------