OpenVPN 3 Core Library
Loading...
Searching...
No Matches
pipe.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_COMMON_PIPE_H
13#define OPENVPN_COMMON_PIPE_H
14
15#include <unistd.h>
16#include <errno.h>
17
18#include <string>
19
20#include <openvpn/io/io.hpp>
21
26
27namespace openvpn::Pipe {
28class SD
29{
30 public:
31 SD(openvpn_io::io_context &io_context, ScopedFD &fd)
32 {
33 if (fd.defined())
34 sd.reset(new openvpn_io::posix::stream_descriptor(io_context, fd.release()));
35 }
36
37 bool defined() const
38 {
39 return bool(sd);
40 }
41
42 protected:
43 std::unique_ptr<openvpn_io::posix::stream_descriptor> sd;
44};
45
46class SD_OUT : public SD
47{
48 public:
49 SD_OUT(openvpn_io::io_context &io_context, const std::string &content, ScopedFD &fd)
50 : SD(io_context, fd)
51 {
52 if (defined())
53 {
54 buf = buf_alloc_from_string(content);
56 }
57 }
58
59 private:
61 {
62 sd->async_write_some(buf.const_buffer_limit(2048),
63 [this](const openvpn_io::error_code &ec, const size_t bytes_sent)
64 {
65 if (!ec && bytes_sent < buf.size())
66 {
67 buf.advance(bytes_sent);
68 queue_write();
69 }
70 else
71 {
72 sd->close();
73 }
74 });
75 }
76
78};
79
80class SD_IN : public SD
81{
82 public:
83 SD_IN(openvpn_io::io_context &io_context, ScopedFD &fd)
84 : SD(io_context, fd)
85 {
86 if (defined())
87 queue_read();
88 }
89
90 const std::string content() const
91 {
92 return data.to_string();
93 }
94
95 private:
97 {
98 buf.reset(0, 2048, BufAllocFlags::NO_FLAGS);
99 sd->async_read_some(buf.mutable_buffer_clamp(),
100 [this](const openvpn_io::error_code &ec, const size_t bytes_recvd)
101 {
102 if (!ec)
103 {
104 buf.set_size(bytes_recvd);
105 data.put_consume(buf);
106 queue_read();
107 }
108 else
109 {
110 sd->close();
111 }
112 });
113 }
114
117};
118
119inline void make_pipe(int fd[2])
120{
121 if (::pipe(fd) < 0)
122 {
123 const int eno = errno;
124 OPENVPN_THROW_EXCEPTION("error creating pipe : " << strerror_str(eno));
125 }
126}
127
128inline void make_pipe(ScopedFD &read, ScopedFD &write)
129{
130 int fd[2];
131 make_pipe(fd);
132 read.reset(fd[0]);
133 write.reset(fd[1]);
134}
135} // namespace openvpn::Pipe
136
137#endif
openvpn_io::const_buffer const_buffer_limit(const size_t limit) const
Return a const_buffer object with a specified size limit.
Definition buffer.hpp:1324
BufferList data
Definition pipe.hpp:116
const std::string content() const
Definition pipe.hpp:90
BufferAllocated buf
Definition pipe.hpp:115
SD_IN(openvpn_io::io_context &io_context, ScopedFD &fd)
Definition pipe.hpp:83
SD_OUT(openvpn_io::io_context &io_context, const std::string &content, ScopedFD &fd)
Definition pipe.hpp:49
BufferAllocated buf
Definition pipe.hpp:77
std::unique_ptr< openvpn_io::posix::stream_descriptor > sd
Definition pipe.hpp:43
SD(openvpn_io::io_context &io_context, ScopedFD &fd)
Definition pipe.hpp:31
bool defined() const
Definition pipe.hpp:37
void reset(const int fd_arg)
Definition scoped_fd.hpp:68
bool defined() const
Definition scoped_fd.hpp:58
#define OPENVPN_THROW_EXCEPTION(stuff)
void make_pipe(int fd[2])
Definition pipe.hpp:119
BufferAllocated buf_alloc_from_string(const std::string &str)
Definition bufstr.hpp:64
std::string strerror_str(const int errnum)
Definition strerror.hpp:21