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 using Queue = std::deque<BufferPtr>;
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 return raw_mode_read;
255 }
256
257 bool is_raw_mode_write() const
258 {
259 if (RAW_MODE_ONLY)
260 return true;
261 return raw_mode_write;
262 }
263
265 {
266 stop();
267 }
268
270 {
271 queue.push_back(std::move(buf));
272 if (queue.size() == 1) // send operation not currently active?
273 queue_send();
274 }
275
277 {
278 BufferAllocated &buf = *queue.front();
279 socket.async_send(buf.const_buffer_clamp(),
280 [self = Ptr(this)](const openvpn_io::error_code &error, const size_t bytes_sent)
281 {
283 self->handle_send(error, bytes_sent);
284 });
285 }
286
287 void handle_send(const openvpn_io::error_code &error, const size_t bytes_sent)
288 {
289 if (!halt)
290 {
291 if (!error)
292 {
293 OPENVPN_LOG_TCPLINK_VERBOSE("TLS-TCP send raw=" << raw_mode_write << " size=" << bytes_sent);
296
297 BufferPtr buf = queue.front();
298 if (bytes_sent == buf->size())
299 {
300 queue.pop_front();
301 if (free_list.size() < free_list_max_size)
302 {
303 buf->reset_content();
304 free_list.push_back(std::move(buf)); // recycle the buffer for later use
305 }
306 }
307 else if (bytes_sent < buf->size())
308 buf->advance(bytes_sent);
309 else
310 {
312 read_handler->tcp_error_handler("TCP_INTERNAL_ERROR"); // error sent more bytes than we asked for
313 stop();
314 return;
315 }
316 }
317 else
318 {
319 OPENVPN_LOG_TCPLINK_ERROR("TLS-TCP send error: " << error.message());
321 read_handler->tcp_error_handler("NETWORK_SEND_ERROR");
322 stop();
323 return;
324 }
325 if (!queue.empty())
326 queue_send();
327 else
329 }
330 }
331
333 {
334 bool requeue = true;
335
336 OPENVPN_LOG_TCPLINK_VERBOSE("TLSLink::process_recv_buffer: size=" << buf.size());
337
338 if (!is_raw_mode_read())
339 {
340 try
341 {
342 BufferAllocated pkt;
343 requeue = put_pktstream(buf, pkt);
344 if (!buf.allocated() && pkt.allocated()) // recycle pkt allocated buffer
345 buf.move(pkt);
346 }
347 catch ([[maybe_unused]] const std::exception &e)
348 {
349 OPENVPN_LOG_TCPLINK_ERROR("TLS-TCP packet extract error: " << e.what());
351 read_handler->tcp_error_handler("TCP_SIZE_ERROR");
352 stop();
353 return false;
354 }
355 }
356 else
357 {
358 if (mutate)
359 {
360 mutate->post_recv(buf);
361 }
362#ifdef OPENVPN_GREMLIN
363 if (gremlin)
364 {
365 requeue = gremlin_recv(buf);
366 }
367 else
368#endif
369 {
370 requeue = read_handler->tcp_read_handler(buf);
371 }
372 }
373 return requeue;
374 }
375
376 void handle_recv(PacketFrom::SPtr pfp, const openvpn_io::error_code &error, const size_t bytes_recvd)
377 {
378 OPENVPN_LOG_TCPLINK_VERBOSE("TCPLink::handle_recv: " << error.message());
379 if (!halt)
380 {
381 if (!error)
382 {
383 recv_buffer(pfp, bytes_recvd);
384 }
385 else if (error == openvpn_io::error::eof)
386 {
387 OPENVPN_LOG_TCPLINK_ERROR("TCP recv EOF");
388 read_handler->tcp_eof_handler();
389 }
390 else
391 {
392 OPENVPN_LOG_TCPLINK_ERROR("TCP recv error: " << error.message());
394 read_handler->tcp_error_handler("NETWORK_RECV_ERROR");
395 stop();
396 }
397 }
398 }
399
401 {
402 bool requeue = true;
405 if (mutate)
406 mutate->post_recv(buf);
407 while (!buf.empty())
408 {
410 if (pktstream.ready())
411 {
412 pktstream.get(pkt);
413#ifdef OPENVPN_GREMLIN
414 if (gremlin)
415 requeue = gremlin_recv(pkt);
416 else
417#endif
418 requeue = read_handler->tcp_read_handler(pkt);
419 }
420 }
421 return requeue;
422 }
423
424#ifdef OPENVPN_GREMLIN
425 void gremlin_queue_send_buffer(BufferPtr &buf)
426 {
427 gremlin->send_queue([self = Ptr(this), buf = std::move(buf)]() mutable
428 {
429 if (!self->halt)
430 {
431 self->queue_send_buffer(buf);
432 } });
433 }
434
435 bool gremlin_recv(BufferAllocated &buf)
436 {
437 gremlin->recv_queue([self = Ptr(this), buf = std::move(buf)]() mutable
438 {
439 if (!self->halt)
440 {
441 const bool requeue = self->read_handler->tcp_read_handler(buf);
442 if (requeue)
443 self->queue_recv(nullptr);
444 } });
445 return false;
446 }
447#endif
448
450 {
451 read_handler->tcp_write_queue_needs_send();
452 }
453
454 typename Protocol::socket &socket;
455 ReadHandler read_handler;
459 const size_t free_list_max_size;
460 Queue queue; // send queue
461 Queue free_list; // recycled free buffers for send queue
466 bool halt = false;
467
468#ifdef OPENVPN_GREMLIN
469 std::unique_ptr<Gremlin::SendRecvQueue> gremlin;
470#endif
471
472 private:
473 virtual void recv_buffer(PacketFrom::SPtr &pfp, const size_t bytes_recvd) = 0;
474 virtual void from_app_send_buffer(BufferPtr &buf) = 0;
475};
476} // namespace openvpn::TCPTransport
477
478#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:1787
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1193
T front() const
Returns the first element of the buffer.
Definition buffer.hpp:1264
bool allocated() const
Returns true if the data memory is defined (allocated).
Definition buffer.hpp:1229
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1241
openvpn_io::const_buffer const_buffer_clamp() const
Return a clamped version of const_buffer().
Definition buffer.hpp:1317
bool empty() const
Returns true if the buffer is empty.
Definition buffer.hpp:1235
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1560
Error::Type code() const
Definition excode.hpp:47
bool code_defined() const
Definition excode.hpp:56
openvpn_io::mutable_buffer mutable_buffer_clamp(Buffer &buf) const
Definition frame.hpp:195
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)
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)
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)
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)
RCPtr< LinkCommon< Protocol, ReadHandler, RAW_MODE_ONLY > > Ptr
std::deque< BufferPtr > Queue
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:1894
std::unique_ptr< PacketFrom > SPtr
#define OPENVPN_LOG_TCPLINK_VERBOSE(x)
#define OPENVPN_LOG_TCPLINK_ERROR(x)
static const char config[]
#define msg(flags,...)