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 $
26 #include "divert_cap.h"
28 #include "stg/common.h"
29 #include "stg/traffcounter.h"
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
46 #include <sys/ioctl.h>
52 #define BUFF_LEN (16384) /* max mtu -> lo=16436 TODO why?*/
54 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 DIVERT_DATA cddiv; //capture data
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 extern "C" Plugin* GetPlugin()
69 static DIVERT_CAP plugin;
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 std::string DIVERT_CAP::GetVersion() const
77 return "cap_divert v.1.0";
79 //-----------------------------------------------------------------------------
80 DIVERT_CAP::DIVERT_CAP()
82 disableForwarding(false),
86 logger(PluginLogger::get("cap_divert"))
89 //-----------------------------------------------------------------------------
90 int DIVERT_CAP::Start()
95 if (DivertCapOpen() < 0)
97 errorStr = "Cannot open socket!";
98 printfd(__FILE__, "Cannot open socket\n");
104 if (pthread_create(&thread, NULL, Run, this))
106 errorStr = "Cannot create thread.";
107 logger("Cannot create thread.");
108 printfd(__FILE__, "Cannot create thread\n");
114 //-----------------------------------------------------------------------------
115 int DIVERT_CAP::Stop()
124 //5 seconds to thread stops itself
126 for (i = 0; i < 25; i++)
131 struct timespec ts = {0, 200000000};
132 nanosleep(&ts, NULL);
135 //after 5 seconds waiting thread still running. now killing it
138 if (pthread_kill(thread, SIGINT))
140 errorStr = "Cannot kill thread.";
141 logger("Cannot send signal to thread.");
142 printfd(__FILE__, "Cannot kill thread\n");
149 //-----------------------------------------------------------------------------
150 void * DIVERT_CAP::Run(void * d)
153 sigfillset(&signalSet);
154 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
156 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
157 dc->isRunning = true;
159 char buffer[pcktSize + 14];
163 dc->DivertCapRead(buffer, sizeof(buffer), NULL);
165 if (buffer[12] != 0x8)
168 memcpy(rp.rawPacket.pckt, &buffer[14], pcktSize);
170 dc->traffCnt->Process(rp);
173 dc->isRunning = false;
176 //-----------------------------------------------------------------------------
177 int DIVERT_CAP::DivertCapOpen()
179 memset(&pollddiv, 0, sizeof(pollddiv));
180 memset(&cddiv, 0, sizeof(DIVERT_DATA));
182 strcpy(cddiv.iface, "foo");
186 pollddiv.events = POLLIN;
187 pollddiv.fd = cddiv.sock;
191 //-----------------------------------------------------------------------------
192 int DIVERT_CAP::DivertCapOpen(int)
195 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
198 errorStr = "Create divert socket error.";
199 logger("Cannot create a socket: %s", strerror(errno));
200 printfd(__FILE__, "Cannot create divert socket\n");
204 struct sockaddr_in divAddr;
206 memset(&divAddr, 0, sizeof(divAddr));
208 divAddr.sin_family = AF_INET;
209 divAddr.sin_port = htons(cddiv.port);
210 divAddr.sin_addr.s_addr = INADDR_ANY;
212 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
216 errorStr = "Bind divert socket error.";
217 logger("Cannot bind the scoket: %s", strerror(errno));
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)
257 if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0)
258 logger("sendto error: %s", strerror(errno));
264 logger("recvfrom error: %s", strerror(errno));
269 //-----------------------------------------------------------------------------
270 int DIVERT_CAP::DivertCapClose()
275 //-----------------------------------------------------------------------------
276 int DIVERT_CAP::ParseSettings()
280 std::vector<PARAM_VALUE>::const_iterator pvi;
283 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
284 if (pvi == settings.moduleParams.end() || pvi->value.empty())
288 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
290 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
291 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
298 pv.param = "DisableForwarding";
299 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
300 if (pvi == settings.moduleParams.end() || pvi->value.empty())
302 disableForwarding = false;
304 else if (ParseYesNo(pvi->value[0], &d))
306 errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
307 printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
311 disableForwarding = d;
315 //-----------------------------------------------------------------------------