]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
Fix raw packet access in cap_divert
[stg.git] / projects / stargazer / plugins / capture / divert_freebsd / divert_cap.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
19 */
20
21 /*
22 $Revision: 1.13 $
23 $Date: 2010/09/10 06:43:03 $
24 */
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28
29 #include <sys/uio.h>
30 #include <sys/time.h>
31 #include <sys/ioctl.h>
32 #include <sys/poll.h>
33
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #include <cstdio>
38 #include <cstring>
39 #include <cerrno>
40 #include <cstdlib>
41 #include <csignal>
42
43 #include <algorithm>
44 #include <vector>
45
46 #include "stg/common.h"
47 #include "stg/traffcounter.h"
48 #include "stg/plugin_creator.h"
49 #include "divert_cap.h"
50
51 #define BUFF_LEN (16384) /* max mtu -> lo=16436  TODO why?*/
52
53 //-----------------------------------------------------------------------------
54 struct DIVERT_DATA {
55 int sock;
56 short int port;
57 unsigned char buffer[BUFF_LEN];
58 char iface[10];
59 };
60 //-----------------------------------------------------------------------------
61 pollfd pollddiv;
62 DIVERT_DATA cddiv;  //capture data
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 PLUGIN_CREATOR<DIVERT_CAP> dcc;
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 PLUGIN * GetPlugin()
71 {
72 return dcc.GetPlugin();
73 }
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 const std::string DIVERT_CAP::GetVersion() const
78 {
79 return "Divert_cap v.1.0";
80 }
81 //-----------------------------------------------------------------------------
82 DIVERT_CAP::DIVERT_CAP()
83     : settings(),
84       port(0),
85       errorStr(),
86       thread(),
87       nonstop(false),
88       isRunning(false),
89       traffCnt(NULL)
90 {
91 }
92 //-----------------------------------------------------------------------------
93 int DIVERT_CAP::Start()
94 {
95 if (isRunning)
96     return 0;
97
98 if (DivertCapOpen() < 0)
99     {
100     errorStr = "Cannot open socket!";
101     printfd(__FILE__, "Cannot open socket\n");
102     return -1;
103     }
104
105 nonstop = true;
106
107 if (pthread_create(&thread, NULL, Run, this) == 0)
108     {
109     return 0;
110     }
111
112 errorStr = "Cannot create thread.";
113 printfd(__FILE__, "Cannot create thread\n");
114 return -1;
115 }
116 //-----------------------------------------------------------------------------
117 int DIVERT_CAP::Stop()
118 {
119 if (!isRunning)
120     return 0;
121
122 DivertCapClose();
123
124 nonstop = false;
125
126 //5 seconds to thread stops itself
127 int i;
128 for (i = 0; i < 25; i++)
129     {
130     if (!isRunning)
131         break;
132
133     struct timespec ts = {0, 200000000};
134     nanosleep(&ts, NULL);
135     }
136
137 //after 5 seconds waiting thread still running. now killing it
138 if (isRunning)
139     {
140     if (pthread_kill(thread, SIGINT))
141         {
142         errorStr = "Cannot kill thread.";
143         printfd(__FILE__, "Cannot kill thread\n");
144         return -1;
145         }
146     }
147
148 return 0;
149 }
150 //-----------------------------------------------------------------------------
151 void * DIVERT_CAP::Run(void * d)
152 {
153 sigset_t signalSet;
154 sigfillset(&signalSet);
155 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
156
157 DIVERT_CAP * dc = static_cast<DIVERT_CAP *>(d);
158 dc->isRunning = true;
159
160 char buffer[64];
161 while (dc->nonstop)
162     {
163     RAW_PACKET rp;
164     dc->DivertCapRead(buffer, 64, NULL);
165
166     if (buffer[12] != 0x8)
167         continue;
168
169     memcpy(rp.rawPacket.pckt, &buffer[14], pcktSize);
170
171     dc->traffCnt->Process(rp);
172     }
173
174 dc->isRunning = false;
175 return NULL;
176 }
177 //-----------------------------------------------------------------------------
178 int DIVERT_CAP::DivertCapOpen()
179 {
180 memset(&pollddiv, 0, sizeof(pollddiv));
181 memset(&cddiv, 0, sizeof(DIVERT_DATA));
182
183 strcpy(cddiv.iface, "foo");
184 cddiv.port = port;
185
186 DivertCapOpen(0);
187 pollddiv.events = POLLIN;
188 pollddiv.fd = cddiv.sock;
189
190 return 0;
191 }
192 //-----------------------------------------------------------------------------
193 int DIVERT_CAP::DivertCapOpen(int)
194 {
195 int ret;
196 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
197 if (cddiv.sock < 0)
198     {
199     errorStr = "Create divert socket error.";
200     printfd(__FILE__, "Cannot create divert socket\n");
201     return -1;
202     }
203
204 struct sockaddr_in divAddr;
205
206 memset(&divAddr, 0, sizeof(divAddr));
207
208 divAddr.sin_family = AF_INET;
209 divAddr.sin_port = htons(cddiv.port);
210 divAddr.sin_addr.s_addr = INADDR_ANY;
211
212 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
213
214 if (ret < 0)
215     {
216     errorStr = "Bind divert socket error.";
217     printfd(__FILE__, "Cannot bind divert socket\n");
218     return -1;
219     }
220
221 return cddiv.sock;
222 }
223 //-----------------------------------------------------------------------------
224 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
225 {
226 poll(&pollddiv, 1, -1);
227
228 if (pollddiv.revents & POLLIN)
229     {
230     DivertCapRead(b, blen, iface, 0);
231     pollddiv.revents = 0;
232     return 0;
233     }
234
235 return 0;
236 }
237 //-----------------------------------------------------------------------------
238 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
239 {
240 static char buf[BUFF_LEN];
241 static struct sockaddr_in divertaddr;
242 static int bytes;
243 static socklen_t divertaddrSize = sizeof(divertaddr);
244
245 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
246                        0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
247     {
248     memcpy(b + 14, buf, blen - 14);
249     b[12] = 0x8;
250
251     if (iface)
252         *iface = cddiv.iface;
253
254     sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
255     }
256
257 return 0;
258 }
259 //-----------------------------------------------------------------------------
260 int DIVERT_CAP::DivertCapClose()
261 {
262 close(cddiv.sock);
263 return 0;
264 }
265 //-----------------------------------------------------------------------------
266 int DIVERT_CAP::ParseSettings()
267 {
268 int p;
269 PARAM_VALUE pv;
270 std::vector<PARAM_VALUE>::const_iterator pvi;
271
272 pv.param = "Port";
273 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
274 if (pvi == settings.moduleParams.end())
275     {
276     port = 15701;
277     return 0;
278     }
279
280 if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
281     {
282     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
283     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
284     return -1;
285     }
286
287 port = p;
288
289 return 0;
290 }
291 //-----------------------------------------------------------------------------