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