Line data Source code
1 : #pragma once
2 :
3 : #include <chrono>
4 : #include <iostream>
5 :
6 : #include "envoy/network/io_handle.h"
7 :
8 : #include "source/common/common/assert.h"
9 : #include "source/common/network/io_socket_error_impl.h"
10 :
11 : namespace Envoy {
12 : namespace Quic {
13 :
14 : // A wrapper class around IoHandle object which doesn't close() upon destruction. It is used to
15 : // create ConnectionSocket as the actual IoHandle instance should out live connection socket.
16 : class QuicIoHandleWrapper : public Network::IoHandle {
17 : public:
18 0 : QuicIoHandleWrapper(Network::IoHandle& io_handle) : io_handle_(io_handle) {}
19 :
20 : // Network::IoHandle
21 0 : os_fd_t fdDoNotUse() const override { return io_handle_.fdDoNotUse(); }
22 0 : Api::IoCallUint64Result close() override {
23 0 : closed_ = true;
24 0 : return Api::ioCallUint64ResultNoError();
25 0 : }
26 0 : bool isOpen() const override { return !closed_; }
27 : Api::IoCallUint64Result readv(uint64_t max_length, Buffer::RawSlice* slices,
28 0 : uint64_t num_slice) override {
29 0 : if (closed_) {
30 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
31 0 : }
32 0 : return io_handle_.readv(max_length, slices, num_slice);
33 0 : }
34 : Api::IoCallUint64Result read(Buffer::Instance& buffer,
35 0 : absl::optional<uint64_t> max_length) override {
36 0 : if (closed_) {
37 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
38 0 : }
39 0 : return io_handle_.read(buffer, max_length);
40 0 : }
41 0 : Api::IoCallUint64Result writev(const Buffer::RawSlice* slices, uint64_t num_slice) override {
42 0 : if (closed_) {
43 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
44 0 : }
45 0 : return io_handle_.writev(slices, num_slice);
46 0 : }
47 0 : Api::IoCallUint64Result write(Buffer::Instance& buffer) override {
48 0 : if (closed_) {
49 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
50 0 : }
51 0 : return io_handle_.write(buffer);
52 0 : }
53 : Api::IoCallUint64Result sendmsg(const Buffer::RawSlice* slices, uint64_t num_slice, int flags,
54 : const Envoy::Network::Address::Ip* self_ip,
55 0 : const Network::Address::Instance& peer_address) override {
56 0 : if (closed_) {
57 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
58 0 : }
59 0 : return io_handle_.sendmsg(slices, num_slice, flags, self_ip, peer_address);
60 0 : }
61 : Api::IoCallUint64Result recvmsg(Buffer::RawSlice* slices, const uint64_t num_slice,
62 0 : uint32_t self_port, RecvMsgOutput& output) override {
63 0 : if (closed_) {
64 0 : ASSERT(false, "recvmmsg is called after close.");
65 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
66 0 : }
67 0 : return io_handle_.recvmsg(slices, num_slice, self_port, output);
68 0 : }
69 : Api::IoCallUint64Result recvmmsg(RawSliceArrays& slices, uint32_t self_port,
70 0 : RecvMsgOutput& output) override {
71 0 : if (closed_) {
72 0 : ASSERT(false, "recvmmsg is called after close.");
73 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
74 0 : }
75 0 : return io_handle_.recvmmsg(slices, self_port, output);
76 0 : }
77 0 : Api::IoCallUint64Result recv(void* buffer, size_t length, int flags) override {
78 0 : if (closed_) {
79 0 : ASSERT(false, "recv called after close.");
80 0 : return {0, Network::IoSocketError::getIoSocketEbadfError()};
81 0 : }
82 0 : return io_handle_.recv(buffer, length, flags);
83 0 : }
84 0 : bool supportsMmsg() const override { return io_handle_.supportsMmsg(); }
85 0 : bool supportsUdpGro() const override { return io_handle_.supportsUdpGro(); }
86 0 : Api::SysCallIntResult bind(Network::Address::InstanceConstSharedPtr address) override {
87 0 : return io_handle_.bind(address);
88 0 : }
89 0 : Api::SysCallIntResult listen(int backlog) override { return io_handle_.listen(backlog); }
90 0 : Network::IoHandlePtr accept(struct sockaddr* addr, socklen_t* addrlen) override {
91 0 : return io_handle_.accept(addr, addrlen);
92 0 : }
93 0 : Api::SysCallIntResult connect(Network::Address::InstanceConstSharedPtr address) override {
94 0 : return io_handle_.connect(address);
95 0 : }
96 : Api::SysCallIntResult setOption(int level, int optname, const void* optval,
97 0 : socklen_t optlen) override {
98 0 : return io_handle_.setOption(level, optname, optval, optlen);
99 0 : }
100 : Api::SysCallIntResult getOption(int level, int optname, void* optval,
101 0 : socklen_t* optlen) override {
102 0 : return io_handle_.getOption(level, optname, optval, optlen);
103 0 : }
104 :
105 : Api::SysCallIntResult ioctl(unsigned long control_code, void* in_buffer,
106 : unsigned long in_buffer_len, void* out_buffer,
107 : unsigned long out_buffer_len,
108 0 : unsigned long* bytes_returned) override {
109 0 : return io_handle_.ioctl(control_code, in_buffer, in_buffer_len, out_buffer, out_buffer_len,
110 0 : bytes_returned);
111 0 : }
112 :
113 0 : Api::SysCallIntResult setBlocking(bool blocking) override {
114 0 : return io_handle_.setBlocking(blocking);
115 0 : }
116 0 : absl::optional<int> domain() override { return io_handle_.domain(); }
117 0 : Network::Address::InstanceConstSharedPtr localAddress() override {
118 0 : return io_handle_.localAddress();
119 0 : }
120 0 : Network::Address::InstanceConstSharedPtr peerAddress() override {
121 0 : return io_handle_.peerAddress();
122 0 : }
123 :
124 : void initializeFileEvent(Event::Dispatcher& dispatcher, Event::FileReadyCb cb,
125 0 : Event::FileTriggerType trigger, uint32_t events) override {
126 0 : io_handle_.initializeFileEvent(dispatcher, cb, trigger, events);
127 0 : }
128 :
129 0 : Network::IoHandlePtr duplicate() override { return io_handle_.duplicate(); }
130 :
131 0 : void activateFileEvents(uint32_t events) override { io_handle_.activateFileEvents(events); }
132 0 : void enableFileEvents(uint32_t events) override { io_handle_.enableFileEvents(events); }
133 0 : void resetFileEvents() override { return io_handle_.resetFileEvents(); };
134 0 : absl::optional<std::string> interfaceName() override { return io_handle_.interfaceName(); }
135 :
136 0 : Api::SysCallIntResult shutdown(int how) override { return io_handle_.shutdown(how); }
137 0 : absl::optional<std::chrono::milliseconds> lastRoundTripTime() override { return {}; }
138 0 : absl::optional<uint64_t> congestionWindowInBytes() const override {
139 : // QUIC should get congestion window from QuicFilterManagerConnectionImpl, which implements the
140 : // Envoy::Network::Connection::congestionWindowInBytes interface.
141 0 : IS_ENVOY_BUG("QuicIoHandleWrapper does not implement congestionWindowInBytes.");
142 0 : return {};
143 0 : }
144 :
145 : private:
146 : Network::IoHandle& io_handle_;
147 : bool closed_{false};
148 : };
149 :
150 : } // namespace Quic
151 : } // namespace Envoy
|