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