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 "sg/plugin_creator.h"
49 #include "divert_cap.h"
51 #define BUFF_LEN (16384) /* max mtu -> lo=16436 TODO why?*/
53 //-----------------------------------------------------------------------------
57 unsigned char buffer[BUFF_LEN];
60 //-----------------------------------------------------------------------------
62 DIVERT_DATA cddiv; //capture data
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 PLUGIN_CREATOR<DIVERT_CAP> dcc;
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
72 return dcc.GetPlugin();
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 const std::string DIVERT_CAP::GetVersion() const
79 return "Divert_cap v.1.0";
81 //-----------------------------------------------------------------------------
82 DIVERT_CAP::DIVERT_CAP()
92 //-----------------------------------------------------------------------------
93 int DIVERT_CAP::Start()
98 if (DivertCapOpen() < 0)
100 errorStr = "Cannot open socket!";
101 printfd(__FILE__, "Cannot open socket\n");
107 if (pthread_create(&thread, NULL, Run, this) == 0)
112 errorStr = "Cannot create thread.";
113 printfd(__FILE__, "Cannot create thread\n");
116 //-----------------------------------------------------------------------------
117 int DIVERT_CAP::Stop()
126 //5 seconds to thread stops itself
128 for (i = 0; i < 25; i++)
136 //after 5 seconds waiting thread still running. now killing it
139 if (pthread_kill(thread, SIGINT))
141 errorStr = "Cannot kill thread.";
142 printfd(__FILE__, "Cannot kill thread\n");
149 //-----------------------------------------------------------------------------
150 void * DIVERT_CAP::Run(void * d)
152 DIVERT_CAP * dc = (DIVERT_CAP *)d;
153 dc->isRunning = true;
159 dc->DivertCapRead(buffer, 64, NULL);
161 if (buffer[12] != 0x8)
164 memcpy(rp.pckt, &buffer[14], pcktSize);
166 dc->traffCnt->Process(rp);
169 dc->isRunning = false;
172 //-----------------------------------------------------------------------------
173 int DIVERT_CAP::DivertCapOpen()
175 memset(&pollddiv, 0, sizeof(pollddiv));
176 memset(&cddiv, 0, sizeof(DIVERT_DATA));
178 strcpy(cddiv.iface, "foo");
182 pollddiv.events = POLLIN;
183 pollddiv.fd = cddiv.sock;
187 //-----------------------------------------------------------------------------
188 int DIVERT_CAP::DivertCapOpen(int)
191 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
194 errorStr = "Create divert socket error.";
195 printfd(__FILE__, "Cannot create divert socket\n");
199 struct sockaddr_in divAddr;
201 memset(&divAddr, 0, sizeof(divAddr));
203 divAddr.sin_family = AF_INET;
204 divAddr.sin_port = htons(cddiv.port);
205 divAddr.sin_addr.s_addr = INADDR_ANY;
207 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
211 errorStr = "Bind divert socket error.";
212 printfd(__FILE__, "Cannot bind divert socket\n");
218 //-----------------------------------------------------------------------------
219 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
221 poll(&pollddiv, 1, -1);
223 if (pollddiv.revents & POLLIN)
225 DivertCapRead(b, blen, iface, 0);
226 pollddiv.revents = 0;
232 //-----------------------------------------------------------------------------
233 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
235 static char buf[BUFF_LEN];
236 static struct sockaddr_in divertaddr;
238 static socklen_t divertaddrSize = sizeof(divertaddr);
240 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
241 0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
243 memcpy(b + 14, buf, blen - 14);
247 *iface = cddiv.iface;
249 sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
254 //-----------------------------------------------------------------------------
255 int DIVERT_CAP::DivertCapClose()
260 //-----------------------------------------------------------------------------
261 int DIVERT_CAP::ParseSettings()
265 std::vector<PARAM_VALUE>::const_iterator pvi;
268 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
269 if (pvi == settings.moduleParams.end())
275 if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
277 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
278 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
286 //-----------------------------------------------------------------------------