OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tcplinkcommon.hpp
Go to the documentation of this file.
1// Copyright (C) 2012- OpenVPN Inc.
2//
3// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
4
5// Base class for TCP link objects.
6
7#ifndef OPENVPN_TRANSPORT_COMMONLINK_H
8#define OPENVPN_TRANSPORT_COMMONLINK_H
9
10#include <deque>
11#include <utility> // for std::move
12#include <memory>
13
14#include <openvpn/io/io.hpp>
15
17#include <openvpn/common/rc.hpp>
25
26#ifdef OPENVPN_GREMLIN
28#endif
29
30#if defined(OPENVPN_DEBUG_TCPLINK) && OPENVPN_DEBUG_TCPLINK >= 1
31#define OPENVPN_LOG_TCPLINK_ERROR(x) OPENVPN_LOG(x)
32#else
33#define OPENVPN_LOG_TCPLINK_ERROR(x)
34#endif
35
36#if defined(OPENVPN_DEBUG_TCPLINK) && OPENVPN_DEBUG_TCPLINK >= 3
37#define OPENVPN_LOG_TCPLINK_VERBOSE(x) OPENVPN_LOG(x)
38#else
39#define OPENVPN_LOG_TCPLINK_VERBOSE(x)
40#endif
41
42namespace openvpn::TCPTransport {
43
44template <typename Protocol,
45 typename ReadHandler,
46 bool RAW_MODE_ONLY>
47class LinkCommon : public LinkBase
48{
49 typedef std::deque<BufferPtr> Queue;
50
51 public:
55
56 // In raw mode, data is sent and received without any special encapsulation.
57 // In non-raw mode, data is packetized by prepending a 16-bit length word
58 // onto each packet. The OpenVPN and DNS protocols run in non-raw mode,
59 // while other TCP protocols such as HTTP or HTTPS would run in raw mode.
60 // This method is a no-op if RAW_MODE_ONLY is true.
61 void set_raw_mode(const bool mode)
62 {
65 }
66
67 void set_raw_mode_read(const bool mode)
68 {
69 if (RAW_MODE_ONLY)
70 raw_mode_read = true;
71 else
72 raw_mode_read = mode;
73 }
74
75 void set_raw_mode_write(const bool mode)
76 {
77 if (RAW_MODE_ONLY)
78 raw_mode_write = true;
79 else
80 raw_mode_write = mode;
81 }
82
83 void set_mutate(const TransportMutateStream::Ptr &mutate_arg)
84 {
85 mutate = mutate_arg;
86 }
87
88 bool send_queue_empty() const
89 {
90 return send_queue_size() == 0;
91 }
92
93 void inject(const Buffer &src)
94 {
95 const size_t size = src.size();
96 OPENVPN_LOG_TCPLINK_VERBOSE("TCP inject size=" << size);
97 if (size && !RAW_MODE_ONLY)
98 {
101 buf.write(src.c_data(), size);
102 BufferAllocated pkt;
103 put_pktstream(buf, pkt);
104 }
105 }
106
107 void start()
108 {
109 if (!halt)
110 queue_recv(nullptr);
111 }
112
113 void stop()
114 {
115 halt = true;
116#ifdef OPENVPN_GREMLIN
117 if (gremlin)
118 gremlin->stop();
119#endif
120 }
121
122 void reset_align_adjust(const size_t align_adjust)
123 {
124 frame_context.reset_align_adjust(align_adjust + (is_raw_mode() ? 0 : 2));
125 }
126
127 size_t send_queue_size() const
128 {
129 return queue.size()
130#ifdef OPENVPN_GREMLIN
131 + (gremlin ? gremlin->send_size() : 0)
132#endif
133 ;
134 }
135
137 {
138 if (halt)
139 {
140 return false;
141 }
143 {
145 read_handler->tcp_error_handler("TCP_OVERFLOW");
146 stop();
147 return false;
148 }
149
150 BufferPtr buf;
151 if (!free_list.empty())
152 {
153 buf = free_list.front();
154 free_list.pop_front();
155 }
156 else
157 {
159 }
160 buf->swap(b);
161 if (!is_raw_mode_write())
162 {
164 }
165 if (mutate)
166 {
167 mutate->pre_send(*buf);
168 }
169#ifdef OPENVPN_GREMLIN
170 if (gremlin)
171 {
172 gremlin_queue_send_buffer(buf);
173 }
174 else
175#endif
176 {
178 }
179 return true;
180 }
181
182 void queue_recv(PacketFrom *tcpfrom)
183 {
184 OPENVPN_LOG_TCPLINK_VERBOSE("TLSLink::queue_recv");
185 if (!tcpfrom)
186 tcpfrom = new PacketFrom();
187 frame_context.prepare(tcpfrom->buf);
188
189 socket.async_receive(frame_context.mutable_buffer_clamp(tcpfrom->buf),
190 [self = Ptr(this), tcpfrom = PacketFrom::SPtr(tcpfrom)](const openvpn_io::error_code &error, const size_t bytes_recvd) mutable
191 {
193 try
194 {
195 self->handle_recv(std::move(tcpfrom), error, bytes_recvd);
196 }
197 catch (const std::exception &e)
198 {
200 const char *msg = "TCP_SIZE_ERROR";
201 // if exception is an ExceptionCode, translate the code
202 // to return status string
203 {
204 const ExceptionCode *ec = dynamic_cast<const ExceptionCode *>(&e);
205 if (ec && ec->code_defined())
206 {
207 err = ec->code();
208 msg = ec->what();
209 }
210 }
211
212 OPENVPN_LOG_TCPLINK_ERROR("TCP packet extract exception: " << e.what());
213 self->stats->error(err);
214 self->read_handler->tcp_error_handler(msg);
215 self->stop();
216 }
217 });
218 }
219
220 protected:
221 LinkCommon(ReadHandler read_handler_arg,
222 typename Protocol::socket &socket_arg,
223 const size_t send_queue_max_size_arg, // 0 to disable
224 const size_t free_list_max_size_arg,
225 const Frame::Context &frame_context_arg,
226 const SessionStats::Ptr &stats_arg)
227 : socket(socket_arg),
228 read_handler(read_handler_arg),
229 frame_context(frame_context_arg),
230 stats(stats_arg),
231 send_queue_max_size(send_queue_max_size_arg),
232 free_list_max_size(free_list_max_size_arg)
233 {
234 set_raw_mode(false);
235 }
236
237#ifdef OPENVPN_GREMLIN
238 void gremlin_config(const Gremlin::Config::Ptr &config)
239 {
240 if (config)
241 gremlin.reset(new Gremlin::SendRecvQueue(socket.get_executor().context(), config, true));
242 }
243#endif
244
245 bool is_raw_mode() const
246 {
248 }
249
250 bool is_raw_mode_read() const
251 {
252 if (RAW_MODE_ONLY)
253 return true;
254 else
255 return raw_mode_read;
256 }
257
258 bool is_raw_mode_write() const
259 {
260 if (RAW_MODE_ONLY)
261 return true;
262 else
263 return raw_mode_write;
264 }
265
267 {
268 stop();
269 }
270
272 {
273 queue.push_back(std::move(buf));
274 if (queue.size() == 1) // send operation not currently active?
275 queue_send();
276 }
277
279 {
280 BufferAllocated &buf = *queue.front();
281 socket.async_send(buf.const_buffer_clamp(),
282 [self = Ptr(this)](const openvpn_io::error_code &error, const size_t bytes_sent)
283 {
285 self->handle_send(error, bytes_sent);
286 });
287 }
288
289 void handle_send(const openvpn_io::error_code &error, const size_t bytes_sent)
290 {
291 if (!halt)
292 {
293 if (!error)
294 {
295 OPENVPN_LOG_TCPLINK_VERBOSE("TLS-TCP send raw=" << raw_mode_write << " size=" << bytes_sent);
298
299 BufferPtr buf = queue.front();
300 if (bytes_sent == buf->size())
301 {
302 queue.pop_front();
303 if (free_list.size() < free_list_max_size)
304 {
305 buf->reset_content();
306 free_list.push_back(std::move(buf)); // recycle the buffer for later use
307 }
308 }
309 else if (bytes_sent < buf->size())
310 buf->advance(bytes_sent);
311 else
312 {
314 read_handler->tcp_error_handler("TCP_INTERNAL_ERROR"); // error sent more bytes than we asked for
315 stop();
316 return;
317 }
318 }
319 else
320 {
321 OPENVPN_LOG_TCPLINK_ERROR("TLS-TCP send error: " << error.message());
323 read_handler->tcp_error_handler("NETWORK_SEND_ERROR");
324 stop();
325 return;
326 }
327 if (!queue.empty())
328 queue_send();
329 else
331 }
332 }
333
335 {
336 bool requeue = true;
337
338 OPENVPN_LOG_TCPLINK_VERBOSE("TLSLink::process_recv_buffer: size=" << buf.size());
339
340 if (!is_raw_mode_read())
341 {
342 try
343 {
344 BufferAllocated pkt;
345 requeue = put_pktstream(buf, pkt);
346 if (!buf.allocated() && pkt.allocated()) // recycle pkt allocated buffer
347 buf.move(pkt);
348 }
349 catch ([[maybe_unused]] const std::exception &e)
350 {
351 OPENVPN_LOG_TCPLINK_ERROR("TLS-TCP packet extract error: " << e.what());
353 read_handler->tcp_error_handler("TCP_SIZE_ERROR");
354 stop();
355 return false;
356 }
357 }
358 else
359 {
360 if (mutate)
361 {
362 mutate->post_recv(buf);
363 }
364#ifdef OPENVPN_GREMLIN
365 if (gremlin)
366 {
367 requeue = gremlin_recv(buf);
368 }
369 else
370#endif
371 {
372 requeue = read_handler->tcp_read_handler(buf);
373 }
374 }
375 return requeue;
376 }
377
378 void handle_recv(PacketFrom::SPtr pfp, const openvpn_io::error_code &error, const size_t bytes_recvd)
379 {
380 OPENVPN_LOG_TCPLINK_VERBOSE("TCPLink::handle_recv: " << error.message());
381 if (!halt)
382 {
383 if (!error)
384 {
385 recv_buffer(pfp, bytes_recvd);
386 }
387 else if (error == openvpn_io::error::eof)
388 {
389 OPENVPN_LOG_TCPLINK_ERROR("TCP recv EOF");
390 read_handler->tcp_eof_handler();
391 }
392 else
393 {
394 OPENVPN_LOG_TCPLINK_ERROR("TCP recv error: " << error.message());
396 read_handler->tcp_error_handler("NETWORK_RECV_ERROR");
397 stop();
398 }
399 }
400 }
401
403 {
404 bool requeue = true;
407 if (mutate)
408 mutate->post_recv(buf);
409 while (buf.size())
410 {
412 if (pktstream.ready())
413 {
414 pktstream.get(pkt);
415#ifdef OPENVPN_GREMLIN
416 if (gremlin)
417 requeue = gremlin_recv(pkt);
418 else
419#endif
420 requeue = read_handler->tcp_read_handler(pkt);
421 }
422 }
423 return requeue;
424 }
425
426#ifdef OPENVPN_GREMLIN
427 void gremlin_queue_send_buffer(BufferPtr &buf)
428 {
429 gremlin->send_queue([self = Ptr(this), buf = std::move(buf)]() mutable
430 {
431 if (!self->halt)
432 {
433 self->queue_send_buffer(buf);
434 } });
435 }
436
437 bool gremlin_recv(BufferAllocated &buf)
438 {
439 gremlin->recv_queue([self = Ptr(this), buf = std::move(buf)]() mutable
440 {
441 if (!self->halt)
442 {
443 const bool requeue = self->read_handler->tcp_read_handler(buf);
444 if (requeue)
445 self->queue_recv(nullptr);
446 } });
447 return false;
448 }
449#endif
450
452 {
453 read_handler->tcp_write_queue_needs_send();
454 }
455
456 typename Protocol::socket &socket;
457 ReadHandler read_handler;
461 const size_t free_list_max_size;
462 Queue queue; // send queue
463 Queue free_list; // recycled free buffers for send queue
468 bool halt = false;
469
470#ifdef OPENVPN_GREMLIN
471 std::unique_ptr<Gremlin::SendRecvQueue> gremlin;
472#endif
473
474 private:
475 virtual void recv_buffer(PacketFrom::SPtr &pfp, const size_t bytes_recvd) = 0;
476 virtual void from_app_send_buffer(BufferPtr &buf) = 0;
477};
478} // namespace openvpn::TCPTransport
479
480#endif
#define OPENVPN_ASYNC_HANDLER
Definition bigmutex.hpp:36
void move(BufferAllocatedType< T_ > &other)
Moves the contents of another BufferAllocatedType object into this object.
Definition buffer.hpp:1756
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1177
T front() const
Returns the first element of the buffer.
Definition buffer.hpp:1248
bool allocated() const
Returns true if the data memory is defined (allocated).
Definition buffer.hpp:1213
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1225
openvpn_io::const_buffer const_buffer_clamp() const
Return a clamped version of const_buffer().
Definition buffer.hpp:1301
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1546
Error::Type code() const
Definition excode.hpp:54
bool code_defined() const
Definition excode.hpp:63
openvpn_io::mutable_buffer mutable_buffer_clamp(Buffer &buf) const
Definition frame.hpp:196
size_t prepare(Buffer &buf) const
Definition frame.hpp:116
void reset_align_adjust(const size_t align_adjust)
Definition frame.hpp:88
static void prepend_size(Buffer &buf)
void get(BufferAllocated &ret)
void put(BufferAllocated &buf, const Frame::Context &frame_context)
Definition pktstream.hpp:48
The smart pointer class.
Definition rc.hpp:119
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
Definition make_rc.hpp:43
virtual void error(const size_t type, const std::string *text=nullptr)
void inc_stat(const size_t type, const count_t value)
RCPtr< LinkCommon< Protocol, ReadHandler, RAW_MODE_ONLY > > Ptr
TransportMutateStream::Ptr mutate
virtual void recv_buffer(PacketFrom::SPtr &pfp, const size_t bytes_recvd)=0
void reset_align_adjust(const size_t align_adjust)
void queue_recv(PacketFrom *tcpfrom)
PacketStream< std::uint16_t > OpenVPNPacketStream
void set_raw_mode_write(const bool mode)
void handle_recv(PacketFrom::SPtr pfp, const openvpn_io::error_code &error, const size_t bytes_recvd)
LinkCommon(ReadHandler read_handler_arg, typename Protocol::socket &socket_arg, const size_t send_queue_max_size_arg, const size_t free_list_max_size_arg, const Frame::Context &frame_context_arg, const SessionStats::Ptr &stats_arg)
std::deque< BufferPtr > Queue
void set_raw_mode_read(const bool mode)
void inject(const Buffer &src)
bool send(BufferAllocated &b)
void set_mutate(const TransportMutateStream::Ptr &mutate_arg)
virtual void from_app_send_buffer(BufferPtr &buf)=0
void set_raw_mode(const bool mode)
bool put_pktstream(BufferAllocated &buf, BufferAllocated &pkt)
void handle_send(const openvpn_io::error_code &error, const size_t bytes_sent)
bool process_recv_buffer(BufferAllocated &buf)
void queue_send_buffer(BufferPtr &buf)
virtual void post_recv(BufferAllocated &buf)=0
virtual void pre_send(BufferAllocated &buf)=0
@ NETWORK_SEND_ERROR
Definition error.hpp:26
@ NETWORK_RECV_ERROR
Definition error.hpp:24
BufferAllocatedType< unsigned char > BufferAllocated
Definition buffer.hpp:1857
std::unique_ptr< PacketFrom > SPtr
#define OPENVPN_LOG_TCPLINK_VERBOSE(x)
#define OPENVPN_LOG_TCPLINK_ERROR(x)
static const char config[]
#define msg(flags,...)