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/traffcounter.h"
29 #include "stg/raw_ip_packet.h"
30 #include "stg/common.h"
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
47 #include <sys/ioctl.h>
53 #define BUFF_LEN (16384) /* max mtu -> lo=16436 TODO why?*/
55 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 DIVERT_DATA cddiv; //capture data
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 extern "C" STG::Plugin* GetPlugin()
70 static DIVERT_CAP plugin;
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 std::string DIVERT_CAP::GetVersion() const
78 return "cap_divert v.1.0";
80 //-----------------------------------------------------------------------------
81 DIVERT_CAP::DIVERT_CAP()
83 disableForwarding(false),
87 logger(STG::PluginLogger::get("cap_divert"))
90 //-----------------------------------------------------------------------------
91 int DIVERT_CAP::Start()
96 if (DivertCapOpen() < 0)
98 errorStr = "Cannot open socket!";
99 printfd(__FILE__, "Cannot open socket\n");
105 if (pthread_create(&thread, NULL, Run, this))
107 errorStr = "Cannot create thread.";
108 logger("Cannot create thread.");
109 printfd(__FILE__, "Cannot create thread\n");
115 //-----------------------------------------------------------------------------
116 int DIVERT_CAP::Stop()
125 //5 seconds to thread stops itself
127 for (i = 0; i < 25; i++)
132 struct timespec ts = {0, 200000000};
133 nanosleep(&ts, NULL);
136 //after 5 seconds waiting thread still running. now killing it
139 if (pthread_kill(thread, SIGINT))
141 errorStr = "Cannot kill thread.";
142 logger("Cannot send signal to thread.");
143 printfd(__FILE__, "Cannot kill thread\n");
150 //-----------------------------------------------------------------------------
151 void * DIVERT_CAP::Run(void * d)
154 sigfillset(&signalSet);
155 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
157 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
158 dc->isRunning = true;
160 char buffer[packetSize + 14];
164 dc->DivertCapRead(buffer, sizeof(buffer), NULL);
166 if (buffer[12] != 0x8)
169 memcpy(&rp.rawPacket, &buffer[14], packetSize);
171 dc->traffCnt->process(rp);
174 dc->isRunning = false;
177 //-----------------------------------------------------------------------------
178 int DIVERT_CAP::DivertCapOpen()
180 memset(&pollddiv, 0, sizeof(pollddiv));
181 memset(&cddiv, 0, sizeof(DIVERT_DATA));
183 strcpy(cddiv.iface, "foo");
187 pollddiv.events = POLLIN;
188 pollddiv.fd = cddiv.sock;
192 //-----------------------------------------------------------------------------
193 int DIVERT_CAP::DivertCapOpen(int)
196 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
199 errorStr = "Create divert socket error.";
200 logger("Cannot create a socket: %s", strerror(errno));
201 printfd(__FILE__, "Cannot create divert socket\n");
205 struct sockaddr_in divAddr;
207 memset(&divAddr, 0, sizeof(divAddr));
209 divAddr.sin_family = AF_INET;
210 divAddr.sin_port = htons(cddiv.port);
211 divAddr.sin_addr.s_addr = INADDR_ANY;
213 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
217 errorStr = "Bind divert socket error.";
218 logger("Cannot bind the scoket: %s", strerror(errno));
219 printfd(__FILE__, "Cannot bind divert socket\n");
225 //-----------------------------------------------------------------------------
226 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
228 poll(&pollddiv, 1, -1);
230 if (pollddiv.revents & POLLIN)
232 DivertCapRead(b, blen, iface, 0);
233 pollddiv.revents = 0;
239 //-----------------------------------------------------------------------------
240 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
242 static char buf[BUFF_LEN];
243 static struct sockaddr_in divertaddr;
245 static socklen_t divertaddrSize = sizeof(divertaddr);
247 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
248 0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
250 memcpy(b + 14, buf, blen - 14);
254 *iface = cddiv.iface;
256 if (!disableForwarding)
258 if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0)
259 logger("sendto error: %s", strerror(errno));
265 logger("recvfrom error: %s", strerror(errno));
270 //-----------------------------------------------------------------------------
271 int DIVERT_CAP::DivertCapClose()
276 //-----------------------------------------------------------------------------
277 int DIVERT_CAP::ParseSettings()
281 std::vector<STG::ParamValue>::const_iterator pvi;
284 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
285 if (pvi == settings.moduleParams.end() || pvi->value.empty())
289 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
291 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
292 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
299 pv.param = "DisableForwarding";
300 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
301 if (pvi == settings.moduleParams.end() || pvi->value.empty())
303 disableForwarding = false;
305 else if (ParseYesNo(pvi->value[0], &d))
307 errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
308 printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
312 disableForwarding = d;
316 //-----------------------------------------------------------------------------