]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
Use std::lock_guard instead of STG_LOCKER.
[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
26 #include "divert_cap.h"
27
28 #include "stg/traffcounter.h"
29 #include "stg/raw_ip_packet.h"
30 #include "stg/common.h"
31
32 #include <algorithm>
33 #include <vector>
34
35 #include <cstdio>
36 #include <cstring>
37 #include <cerrno>
38 #include <cstdlib>
39 #include <csignal>
40
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44
45 #include <sys/uio.h>
46 #include <sys/time.h>
47 #include <sys/ioctl.h>
48 #include <sys/poll.h>
49
50 #include <fcntl.h>
51 #include <unistd.h>
52
53 #define BUFF_LEN (16384) /* max mtu -> lo=16436  TODO why?*/
54
55 //-----------------------------------------------------------------------------
56 struct DIVERT_DATA {
57 int sock;
58 short int port;
59 char iface[10];
60 };
61 //-----------------------------------------------------------------------------
62 pollfd pollddiv;
63 DIVERT_DATA cddiv;  //capture data
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67
68 extern "C" STG::Plugin* GetPlugin()
69 {
70     static DIVERT_CAP plugin;
71     return &plugin;
72 }
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 std::string DIVERT_CAP::GetVersion() const
77 {
78 return "cap_divert v.1.0";
79 }
80 //-----------------------------------------------------------------------------
81 DIVERT_CAP::DIVERT_CAP()
82     : port(0),
83       disableForwarding(false),
84       isRunning(false),
85       traffCnt(NULL),
86       logger(STG::PluginLogger::get("cap_divert"))
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 m_thread = std::jthread([](auto token){ Run(std::move(token)); });
103
104 return 0;
105 }
106 //-----------------------------------------------------------------------------
107 int DIVERT_CAP::Stop()
108 {
109 if (!isRunning)
110     return 0;
111
112 DivertCapClose();
113
114 m_thread.request_stop();
115 //5 seconds to thread stops itself
116 for (size_t i = 0; i < 25; i++)
117     {
118     if (!isRunning)
119         break;
120
121     struct timespec ts = {0, 200000000};
122     nanosleep(&ts, NULL);
123     }
124
125 //after 5 seconds waiting thread still running. now killing it
126 if (isRunning)
127     m_thread.detach();
128 else
129     m_thread.join();
130
131 return 0;
132 }
133 //-----------------------------------------------------------------------------
134 void DIVERT_CAP::Run(std::stop_token token) noexcept
135 {
136 sigset_t signalSet;
137 sigfillset(&signalSet);
138 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
139
140 isRunning = true;
141
142 char buffer[STG::packetSize + 14];
143 while (!token.stop_requested())
144     {
145     STG::RawPacket rp;
146     DivertCapRead(buffer, sizeof(buffer), NULL);
147
148     if (buffer[12] != 0x8)
149         continue;
150
151     memcpy(&rp.rawPacket, &buffer[14], STG::packetSize);
152
153     traffCnt->process(rp);
154     }
155
156 isRunning = false;
157 }
158 //-----------------------------------------------------------------------------
159 int DIVERT_CAP::DivertCapOpen()
160 {
161 memset(&pollddiv, 0, sizeof(pollddiv));
162 memset(&cddiv, 0, sizeof(DIVERT_DATA));
163
164 strcpy(cddiv.iface, "foo");
165 cddiv.port = port;
166
167 DivertCapOpen(0);
168 pollddiv.events = POLLIN;
169 pollddiv.fd = cddiv.sock;
170
171 return 0;
172 }
173 //-----------------------------------------------------------------------------
174 int DIVERT_CAP::DivertCapOpen(int)
175 {
176 int ret;
177 cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
178 if (cddiv.sock < 0)
179     {
180     errorStr = "Create divert socket error.";
181     logger("Cannot create a socket: %s", strerror(errno));
182     printfd(__FILE__, "Cannot create divert socket\n");
183     return -1;
184     }
185
186 struct sockaddr_in divAddr;
187
188 memset(&divAddr, 0, sizeof(divAddr));
189
190 divAddr.sin_family = AF_INET;
191 divAddr.sin_port = htons(cddiv.port);
192 divAddr.sin_addr.s_addr = INADDR_ANY;
193
194 ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
195
196 if (ret < 0)
197     {
198     errorStr = "Bind divert socket error.";
199     logger("Cannot bind the scoket: %s", strerror(errno));
200     printfd(__FILE__, "Cannot bind divert socket\n");
201     return -1;
202     }
203
204 return cddiv.sock;
205 }
206 //-----------------------------------------------------------------------------
207 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface)
208 {
209 poll(&pollddiv, 1, -1);
210
211 if (pollddiv.revents & POLLIN)
212     {
213     DivertCapRead(b, blen, iface, 0);
214     pollddiv.revents = 0;
215     return 0;
216     }
217
218 return 0;
219 }
220 //-----------------------------------------------------------------------------
221 int DIVERT_CAP::DivertCapRead(char * b, int blen, char ** iface, int)
222 {
223 static char buf[BUFF_LEN];
224 static struct sockaddr_in divertaddr;
225 static int bytes;
226 static socklen_t divertaddrSize = sizeof(divertaddr);
227
228 if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
229                        0, (struct sockaddr*) &divertaddr, &divertaddrSize)) > 50)
230     {
231     memcpy(b + 14, buf, blen - 14);
232     b[12] = 0x8;
233
234     if (iface)
235         *iface = cddiv.iface;
236
237     if (!disableForwarding)
238         {
239         if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0)
240             logger("sendto error: %s", strerror(errno));
241         }
242     }
243 else
244     {
245     if (bytes < 0)
246         logger("recvfrom error: %s", strerror(errno));
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 STG::ParamValue pv;
262 std::vector<STG::ParamValue>::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() || pvi->value.empty())
267     {
268     p = 15701;
269     }
270 else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
271     {
272     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
273     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
274     return -1;
275     }
276
277 port = p;
278
279 bool d = false;
280 pv.param = "DisableForwarding";
281 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
282 if (pvi == settings.moduleParams.end() || pvi->value.empty())
283     {
284     disableForwarding = false;
285     }
286 else if (ParseYesNo(pvi->value[0], &d))
287     {
288     errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
289     printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
290     return -1;
291     }
292
293 disableForwarding = d;
294
295 return 0;
296 }
297 //-----------------------------------------------------------------------------