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 //-----------------------------------------------------------------------------
52 unsigned char buffer[BUFF_LEN];
55 //-----------------------------------------------------------------------------
57 DIVERT_DATA cddiv; //capture data
58 //-----------------------------------------------------------------------------
59 class DIVERT_CAP_CREATOR
66 : divc(new DIVERT_CAP())
74 DIVERT_CAP * GetCapturer()
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 DIVERT_CAP_CREATOR dcc;
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 BASE_PLUGIN * GetPlugin()
88 return dcc.GetCapturer();
90 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
93 const string DIVERT_CAP::GetVersion() const
95 return "Divert_cap v.1.0";
97 //-----------------------------------------------------------------------------
98 DIVERT_CAP::DIVERT_CAP()
105 //-----------------------------------------------------------------------------
106 void DIVERT_CAP::SetTraffcounter(TRAFFCOUNTER * tc)
110 //-----------------------------------------------------------------------------
111 const string & DIVERT_CAP::GetStrError() const
115 //-----------------------------------------------------------------------------
116 int DIVERT_CAP::Start()
121 if (DivertCapOpen() < 0)
123 errorStr = "Cannot open socket!";
124 printfd(__FILE__, "Cannot open socket\n");
130 if (pthread_create(&thread, NULL, Run, this) == 0)
135 errorStr = "Cannot create thread.";
136 printfd(__FILE__, "Cannot create thread\n");
139 //-----------------------------------------------------------------------------
140 int DIVERT_CAP::Stop()
149 //5 seconds to thread stops itself
151 for (i = 0; i < 25; i++)
159 //after 5 seconds waiting thread still running. now killing it
162 if (pthread_kill(thread, SIGINT))
164 errorStr = "Cannot kill thread.";
165 printfd(__FILE__, "Cannot kill thread\n");
172 //-----------------------------------------------------------------------------
173 bool DIVERT_CAP::IsRunning()
177 //-----------------------------------------------------------------------------
178 void * DIVERT_CAP::Run(void * d)
180 DIVERT_CAP * dc = (DIVERT_CAP *)d;
181 dc->isRunning = true;
193 char ethip[sizeof(ETH_IP)];
195 //memset(ðIP, 0, sizeof(ethIP));
196 memset(ðip, 0, sizeof(ETH_IP));
198 ethIP = (ETH_IP *)ðip;
199 ethIP->rp.dataLen = -1;
201 //char * iface = NULL;
206 dc->DivertCapRead(buffer, 64, NULL);
208 //printf("%x %x %x %x \n", buffer[0], buffer[4], buffer[8], buffer[12]);
209 //printf("%x %x %x %x \n", buffer[16], buffer[20], buffer[24], buffer[28]);
210 //printf("%x %x %x %x \n", buffer[32], buffer[36], buffer[40], buffer[44]);
212 if (buffer[12] != 0x8)
215 memcpy(rp.pckt, &buffer[14], pcktSize);
217 //dc->traffCnt->Process(*((RAW_PACKET*)( &buffer[14] ))); // - too dirty!
218 dc->traffCnt->Process(rp);
221 dc->isRunning = false;
224 //-----------------------------------------------------------------------------
225 uint16_t DIVERT_CAP::GetStartPosition() const
229 //-----------------------------------------------------------------------------
230 uint16_t DIVERT_CAP::GetStopPosition() const
234 //-----------------------------------------------------------------------------
235 /*****************************************************************************/
236 /*****************************************************************************/
237 /*****************************************************************************/
238 /*****************************************************************************/
239 /*****************************************************************************/
240 //-----------------------------------------------------------------------------
241 int DIVERT_CAP::DivertCapOpen()
243 memset(&pollddiv, 0, sizeof(pollddiv));
244 memset(&cddiv, 0, sizeof(DIVERT_DATA));
246 strcpy(cddiv.iface, "foo");
250 pollddiv.events = POLLIN;
251 pollddiv.fd = cddiv.sock;
255 //-----------------------------------------------------------------------------
256 int DIVERT_CAP::DivertCapOpen(int)
259 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
262 errorStr = "Create divert socket error.";
263 printfd(__FILE__, "Cannot create divert socket\n");
267 struct sockaddr_in divAddr;
269 memset(&divAddr, 0, sizeof(divAddr));
271 divAddr.sin_family = AF_INET;
272 divAddr.sin_port = htons(cddiv.port);
273 divAddr.sin_addr.s_addr = INADDR_ANY;
275 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
279 errorStr = "Bind divert socket error.";
280 printfd(__FILE__, "Cannot bind divert socket\n");
286 //-----------------------------------------------------------------------------
287 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
289 poll(&pollddiv, 1, -1);
291 if (pollddiv.revents & POLLIN)
293 DivertCapRead(b, blen, iface, 0);
294 pollddiv.revents = 0;
300 //-----------------------------------------------------------------------------
301 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
303 static char buf[BUFF_LEN];
304 static struct sockaddr_in divertaddr;
306 static socklen_t divertaddrSize = sizeof(divertaddr);
308 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
309 0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
311 memcpy(b + 14, buf, blen - 14);
315 *iface = cddiv.iface;
317 sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
322 //-----------------------------------------------------------------------------
323 int DIVERT_CAP::DivertCapClose()
328 //-----------------------------------------------------------------------------
329 int DIVERT_CAP::ParseSettings()
333 vector<PARAM_VALUE>::const_iterator pvi;
336 pvi = find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
337 if (pvi == settings.moduleParams.end())
343 if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
345 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
346 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
354 //-----------------------------------------------------------------------------
355 int DIVERT_CAP::ParseIntInRange(const string & str, int min, int max, int * val)
357 if (str2x(str.c_str(), *val))
359 errorStr = "Incorrect value \'" + str + "\'.";
362 if (*val < min || *val > max)
364 errorStr = "Value \'" + str + "\' out of range.";
369 //-----------------------------------------------------------------------------
370 void DIVERT_CAP::SetSettings(const MODULE_SETTINGS & s)
374 //-----------------------------------------------------------------------------