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>
47 #include "divert_cap.h"
48 #include "traffcounter.h"
50 #define BUFF_LEN (16384) /* max mtu -> lo=16436 TODO why?*/
52 //-----------------------------------------------------------------------------
56 unsigned char buffer[BUFF_LEN];
59 //-----------------------------------------------------------------------------
61 DIVERT_DATA cddiv; //capture data
62 //-----------------------------------------------------------------------------
63 class DIVERT_CAP_CREATOR {
69 : divc(new DIVERT_CAP())
77 DIVERT_CAP * GetCapturer()
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 DIVERT_CAP_CREATOR dcc;
86 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
91 return dcc.GetCapturer();
93 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 const std::string DIVERT_CAP::GetVersion() const
98 return "Divert_cap v.1.0";
100 //-----------------------------------------------------------------------------
101 DIVERT_CAP::DIVERT_CAP()
108 //-----------------------------------------------------------------------------
109 int DIVERT_CAP::Start()
114 if (DivertCapOpen() < 0)
116 errorStr = "Cannot open socket!";
117 printfd(__FILE__, "Cannot open socket\n");
123 if (pthread_create(&thread, NULL, Run, this) == 0)
128 errorStr = "Cannot create thread.";
129 printfd(__FILE__, "Cannot create thread\n");
132 //-----------------------------------------------------------------------------
133 int DIVERT_CAP::Stop()
142 //5 seconds to thread stops itself
144 for (i = 0; i < 25; i++)
152 //after 5 seconds waiting thread still running. now killing it
155 if (pthread_kill(thread, SIGINT))
157 errorStr = "Cannot kill thread.";
158 printfd(__FILE__, "Cannot kill thread\n");
165 //-----------------------------------------------------------------------------
166 void * DIVERT_CAP::Run(void * d)
168 DIVERT_CAP * dc = (DIVERT_CAP *)d;
169 dc->isRunning = true;
175 dc->DivertCapRead(buffer, 64, NULL);
177 if (buffer[12] != 0x8)
180 memcpy(rp.pckt, &buffer[14], pcktSize);
182 dc->traffCnt->Process(rp);
185 dc->isRunning = false;
188 //-----------------------------------------------------------------------------
189 int DIVERT_CAP::DivertCapOpen()
191 memset(&pollddiv, 0, sizeof(pollddiv));
192 memset(&cddiv, 0, sizeof(DIVERT_DATA));
194 strcpy(cddiv.iface, "foo");
198 pollddiv.events = POLLIN;
199 pollddiv.fd = cddiv.sock;
203 //-----------------------------------------------------------------------------
204 int DIVERT_CAP::DivertCapOpen(int)
207 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
210 errorStr = "Create divert socket error.";
211 printfd(__FILE__, "Cannot create divert socket\n");
215 struct sockaddr_in divAddr;
217 memset(&divAddr, 0, sizeof(divAddr));
219 divAddr.sin_family = AF_INET;
220 divAddr.sin_port = htons(cddiv.port);
221 divAddr.sin_addr.s_addr = INADDR_ANY;
223 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
227 errorStr = "Bind divert socket error.";
228 printfd(__FILE__, "Cannot bind divert socket\n");
234 //-----------------------------------------------------------------------------
235 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
237 poll(&pollddiv, 1, -1);
239 if (pollddiv.revents & POLLIN)
241 DivertCapRead(b, blen, iface, 0);
242 pollddiv.revents = 0;
248 //-----------------------------------------------------------------------------
249 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
251 static char buf[BUFF_LEN];
252 static struct sockaddr_in divertaddr;
254 static socklen_t divertaddrSize = sizeof(divertaddr);
256 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
257 0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
259 memcpy(b + 14, buf, blen - 14);
263 *iface = cddiv.iface;
265 sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
270 //-----------------------------------------------------------------------------
271 int DIVERT_CAP::DivertCapClose()
276 //-----------------------------------------------------------------------------
277 int DIVERT_CAP::ParseSettings()
281 std::vector<PARAM_VALUE>::const_iterator pvi;
284 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
285 if (pvi == settings.moduleParams.end())
291 if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
293 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
294 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
302 //-----------------------------------------------------------------------------
303 int DIVERT_CAP::ParseIntInRange(const std::string & str, int min, int max, int * val)
305 if (str2x(str.c_str(), *val))
307 errorStr = "Incorrect value \'" + str + "\'.";
310 if (*val < min || *val > max)
312 errorStr = "Value \'" + str + "\' out of range.";
317 //-----------------------------------------------------------------------------