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 <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
33 #include <sys/ioctl.h>
43 #include "divert_cap.h"
45 #define BUFF_LEN (16384) /* max mtu -> lo=16436 TODO why?*/
47 //-----------------------------------------------------------------------------
51 unsigned char buffer[BUFF_LEN];
54 //-----------------------------------------------------------------------------
56 DIVERT_DATA cddiv; //capture data
57 //-----------------------------------------------------------------------------
58 class DIVERT_CAP_CREATOR {
64 : divc(new DIVERT_CAP())
72 DIVERT_CAP * GetCapturer()
77 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
80 DIVERT_CAP_CREATOR dcc;
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
86 return dcc.GetCapturer();
88 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
91 const std::string DIVERT_CAP::GetVersion() const
93 return "Divert_cap v.1.0";
95 //-----------------------------------------------------------------------------
96 DIVERT_CAP::DIVERT_CAP()
103 //-----------------------------------------------------------------------------
104 void DIVERT_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
108 //-----------------------------------------------------------------------------
109 const std::string & DIVERT_CAP::GetStrError() const
113 //-----------------------------------------------------------------------------
114 int DIVERT_CAP::Start()
119 if (DivertCapOpen() < 0)
121 errorStr = "Cannot open socket!";
122 printfd(__FILE__, "Cannot open socket\n");
128 if (pthread_create(&thread, NULL, Run, this) == 0)
133 errorStr = "Cannot create thread.";
134 printfd(__FILE__, "Cannot create thread\n");
137 //-----------------------------------------------------------------------------
138 int DIVERT_CAP::Stop()
147 //5 seconds to thread stops itself
149 for (i = 0; i < 25; i++)
157 //after 5 seconds waiting thread still running. now killing it
160 if (pthread_kill(thread, SIGINT))
162 errorStr = "Cannot kill thread.";
163 printfd(__FILE__, "Cannot kill thread\n");
170 //-----------------------------------------------------------------------------
171 bool DIVERT_CAP::IsRunning()
175 //-----------------------------------------------------------------------------
176 void * DIVERT_CAP::Run(void * d)
178 DIVERT_CAP * dc = (DIVERT_CAP *)d;
179 dc->isRunning = true;
191 char ethip[sizeof(ETH_IP)];
193 //memset(ðIP, 0, sizeof(ethIP));
194 memset(ðip, 0, sizeof(ETH_IP));
196 ethIP = (ETH_IP *)ðip;
197 ethIP->rp.dataLen = -1;
199 //char * iface = NULL;
204 dc->DivertCapRead(buffer, 64, NULL);
206 //printf("%x %x %x %x \n", buffer[0], buffer[4], buffer[8], buffer[12]);
207 //printf("%x %x %x %x \n", buffer[16], buffer[20], buffer[24], buffer[28]);
208 //printf("%x %x %x %x \n", buffer[32], buffer[36], buffer[40], buffer[44]);
210 if (buffer[12] != 0x8)
213 memcpy(rp.pckt, &buffer[14], pcktSize);
215 //dc->traffCnt->Process(*((RAW_PACKET*)( &buffer[14] ))); // - too dirty!
216 dc->traffCnt->Process(rp);
219 dc->isRunning = false;
222 //-----------------------------------------------------------------------------
223 uint16_t DIVERT_CAP::GetStartPosition() const
227 //-----------------------------------------------------------------------------
228 uint16_t DIVERT_CAP::GetStopPosition() const
232 //-----------------------------------------------------------------------------
233 /*****************************************************************************/
234 /*****************************************************************************/
235 /*****************************************************************************/
236 /*****************************************************************************/
237 /*****************************************************************************/
238 //-----------------------------------------------------------------------------
239 int DIVERT_CAP::DivertCapOpen()
241 memset(&pollddiv, 0, sizeof(pollddiv));
242 memset(&cddiv, 0, sizeof(DIVERT_DATA));
244 strcpy(cddiv.iface, "foo");
248 pollddiv.events = POLLIN;
249 pollddiv.fd = cddiv.sock;
253 //-----------------------------------------------------------------------------
254 int DIVERT_CAP::DivertCapOpen(int)
257 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
260 errorStr = "Create divert socket error.";
261 printfd(__FILE__, "Cannot create divert socket\n");
265 struct sockaddr_in divAddr;
267 memset(&divAddr, 0, sizeof(divAddr));
269 divAddr.sin_family = AF_INET;
270 divAddr.sin_port = htons(cddiv.port);
271 divAddr.sin_addr.s_addr = INADDR_ANY;
273 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
277 errorStr = "Bind divert socket error.";
278 printfd(__FILE__, "Cannot bind divert socket\n");
284 //-----------------------------------------------------------------------------
285 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
287 poll(&pollddiv, 1, -1);
289 if (pollddiv.revents & POLLIN)
291 DivertCapRead(b, blen, iface, 0);
292 pollddiv.revents = 0;
298 //-----------------------------------------------------------------------------
299 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
301 static char buf[BUFF_LEN];
302 static struct sockaddr_in divertaddr;
304 static socklen_t divertaddrSize = sizeof(divertaddr);
306 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
307 0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
309 memcpy(b + 14, buf, blen - 14);
313 *iface = cddiv.iface;
315 sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
320 //-----------------------------------------------------------------------------
321 int DIVERT_CAP::DivertCapClose()
326 //-----------------------------------------------------------------------------
327 int DIVERT_CAP::ParseSettings()
331 vector<PARAM_VALUE>::const_iterator pvi;
334 pvi = find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
335 if (pvi == settings.moduleParams.end())
341 if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
343 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
344 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
352 //-----------------------------------------------------------------------------
353 int DIVERT_CAP::ParseIntInRange(const std::string & str, int min, int max, int * val)
355 if (str2x(str.c_str(), *val))
357 errorStr = "Incorrect value \'" + str + "\'.";
360 if (*val < min || *val > max)
362 errorStr = "Value \'" + str + "\' out of range.";
367 //-----------------------------------------------------------------------------
368 void DIVERT_CAP::SetSettings(const MODULE_SETTINGS & s)
372 //-----------------------------------------------------------------------------