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 //-----------------------------------------------------------------------------
65 PLUGIN_CREATOR<DIVERT_CAP> dcc;
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
71 return dcc.GetPlugin();
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 const std::string DIVERT_CAP::GetVersion() const
78 return "Divert_cap v.1.0";
80 //-----------------------------------------------------------------------------
81 DIVERT_CAP::DIVERT_CAP()
84 disableForwarding(false),
90 logger(GetPluginLogger(GetStgLogger(), "cap_divert"))
93 //-----------------------------------------------------------------------------
94 int DIVERT_CAP::Start()
99 if (DivertCapOpen() < 0)
101 errorStr = "Cannot open socket!";
102 printfd(__FILE__, "Cannot open socket\n");
108 if (pthread_create(&thread, NULL, Run, this) == 0)
113 errorStr = "Cannot create thread.";
114 printfd(__FILE__, "Cannot create thread\n");
117 //-----------------------------------------------------------------------------
118 int DIVERT_CAP::Stop()
127 //5 seconds to thread stops itself
129 for (i = 0; i < 25; i++)
134 struct timespec ts = {0, 200000000};
135 nanosleep(&ts, NULL);
138 //after 5 seconds waiting thread still running. now killing it
141 if (pthread_kill(thread, SIGINT))
143 errorStr = "Cannot kill thread.";
144 printfd(__FILE__, "Cannot kill thread\n");
151 //-----------------------------------------------------------------------------
152 void * DIVERT_CAP::Run(void * d)
155 sigfillset(&signalSet);
156 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
158 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
159 dc->isRunning = true;
165 dc->DivertCapRead(buffer, 64, NULL);
167 if (buffer[12] != 0x8)
170 memcpy(rp.rawPacket.pckt, &buffer[14], pcktSize);
172 dc->traffCnt->Process(rp);
175 dc->isRunning = false;
178 //-----------------------------------------------------------------------------
179 int DIVERT_CAP::DivertCapOpen()
181 memset(&pollddiv, 0, sizeof(pollddiv));
182 memset(&cddiv, 0, sizeof(DIVERT_DATA));
184 strcpy(cddiv.iface, "foo");
188 pollddiv.events = POLLIN;
189 pollddiv.fd = cddiv.sock;
193 //-----------------------------------------------------------------------------
194 int DIVERT_CAP::DivertCapOpen(int)
197 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
200 errorStr = "Create divert socket error.";
201 printfd(__FILE__, "Cannot create divert socket\n");
205 struct sockaddr_in divAddr;
207 memset(&divAddr, 0, sizeof(divAddr));
209 divAddr.sin_family = AF_INET;
210 divAddr.sin_port = htons(cddiv.port);
211 divAddr.sin_addr.s_addr = INADDR_ANY;
213 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
217 errorStr = "Bind divert socket error.";
218 printfd(__FILE__, "Cannot bind divert socket\n");
224 //-----------------------------------------------------------------------------
225 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
227 poll(&pollddiv, 1, -1);
229 if (pollddiv.revents & POLLIN)
231 DivertCapRead(b, blen, iface, 0);
232 pollddiv.revents = 0;
238 //-----------------------------------------------------------------------------
239 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
241 static char buf[BUFF_LEN];
242 static struct sockaddr_in divertaddr;
244 static socklen_t divertaddrSize = sizeof(divertaddr);
246 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
247 0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
249 memcpy(b + 14, buf, blen - 14);
253 *iface = cddiv.iface;
255 if (!disableForwarding)
256 sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
261 //-----------------------------------------------------------------------------
262 int DIVERT_CAP::DivertCapClose()
267 //-----------------------------------------------------------------------------
268 int DIVERT_CAP::ParseSettings()
272 std::vector<PARAM_VALUE>::const_iterator pvi;
275 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
276 if (pvi == settings.moduleParams.end())
280 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
282 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
283 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
290 pv.param = "DisableForwarding";
291 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
292 if (pvi == settings.moduleParams.end())
294 disableForwarding = false;
296 else if (ParseYesNo(pvi->value[0], &d))
298 errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
299 printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
303 disableForwarding = d;
307 //-----------------------------------------------------------------------------