Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/network/listener.h" 4 : #include "envoy/network/socket.h" 5 : #include "envoy/network/socket_interface.h" 6 : 7 : #include "source/common/common/assert.h" 8 : #include "source/common/common/dump_state_utils.h" 9 : 10 : namespace Envoy { 11 : namespace Network { 12 : 13 : class ConnectionInfoSetterImpl : public ConnectionInfoSetter { 14 : public: 15 : ConnectionInfoSetterImpl(const Address::InstanceConstSharedPtr& local_address, 16 : const Address::InstanceConstSharedPtr& remote_address) 17 : : local_address_(local_address), remote_address_(remote_address), 18 5824 : direct_remote_address_(remote_address) {} 19 : 20 939 : void setDirectRemoteAddressForTest(const Address::InstanceConstSharedPtr& direct_remote_address) { 21 939 : direct_remote_address_ = direct_remote_address; 22 939 : } 23 : 24 0 : void dumpState(std::ostream& os, int indent_level) const override { 25 0 : const char* spaces = spacesForLevel(indent_level); 26 0 : os << spaces << "ConnectionInfoSetterImpl " << this 27 0 : << DUMP_NULLABLE_MEMBER(remote_address_, remote_address_->asStringView()) 28 0 : << DUMP_NULLABLE_MEMBER(direct_remote_address_, direct_remote_address_->asStringView()) 29 0 : << DUMP_NULLABLE_MEMBER(local_address_, local_address_->asStringView()) 30 0 : << DUMP_MEMBER(server_name_) << "\n"; 31 0 : } 32 : 33 : // ConnectionInfoSetter 34 18749 : const Address::InstanceConstSharedPtr& localAddress() const override { return local_address_; } 35 3922 : void setLocalAddress(const Address::InstanceConstSharedPtr& local_address) override { 36 3922 : local_address_ = local_address; 37 3922 : } 38 107 : void restoreLocalAddress(const Address::InstanceConstSharedPtr& local_address) override { 39 107 : setLocalAddress(local_address); 40 107 : local_address_restored_ = true; 41 107 : } 42 0 : bool localAddressRestored() const override { return local_address_restored_; } 43 10516 : const Address::InstanceConstSharedPtr& remoteAddress() const override { return remote_address_; } 44 961 : void setRemoteAddress(const Address::InstanceConstSharedPtr& remote_address) override { 45 961 : remote_address_ = remote_address; 46 961 : } 47 784 : const Address::InstanceConstSharedPtr& directRemoteAddress() const override { 48 784 : return direct_remote_address_; 49 784 : } 50 784 : absl::string_view requestedServerName() const override { return server_name_; } 51 166 : void setRequestedServerName(const absl::string_view requested_server_name) override { 52 166 : server_name_ = std::string(requested_server_name); 53 166 : } 54 808 : absl::optional<uint64_t> connectionID() const override { return connection_id_; } 55 3027 : void setConnectionID(uint64_t id) override { connection_id_ = id; } 56 1372 : absl::optional<absl::string_view> interfaceName() const override { return interface_name_; } 57 242 : void enableSettingInterfaceName(const bool enable) override { 58 242 : allow_syscall_for_interface_name_ = enable; 59 242 : } 60 1170 : void maybeSetInterfaceName(IoHandle& io_handle) override { 61 1170 : if (allow_syscall_for_interface_name_) { 62 0 : interface_name_ = io_handle.interfaceName(); 63 0 : } 64 1170 : } 65 2997 : Ssl::ConnectionInfoConstSharedPtr sslConnection() const override { return ssl_info_; } 66 3050 : void setSslConnection(const Ssl::ConnectionInfoConstSharedPtr& ssl_connection_info) override { 67 3050 : ssl_info_ = ssl_connection_info; 68 3050 : } 69 0 : absl::string_view ja3Hash() const override { return ja3_hash_; } 70 0 : void setJA3Hash(const absl::string_view ja3_hash) override { ja3_hash_ = std::string(ja3_hash); } 71 0 : const absl::optional<std::chrono::milliseconds>& roundTripTime() const override { 72 0 : return round_trip_time_; 73 0 : } 74 1315 : void setRoundTripTime(std::chrono::milliseconds round_trip_time) override { 75 1315 : round_trip_time_ = round_trip_time; 76 1315 : } 77 0 : OptRef<const FilterChainInfo> filterChainInfo() const override { 78 0 : return makeOptRefFromPtr<const FilterChainInfo>(filter_chain_info_.get()); 79 0 : } 80 1042 : void setFilterChainInfo(FilterChainInfoConstSharedPtr filter_chain_info) override { 81 1042 : filter_chain_info_ = std::move(filter_chain_info); 82 1042 : } 83 0 : OptRef<const ListenerInfo> listenerInfo() const override { 84 0 : return makeOptRefFromPtr<const ListenerInfo>(listener_info_.get()); 85 0 : } 86 1042 : void setListenerInfo(ListenerInfoConstSharedPtr listener_info) override { 87 1042 : listener_info_ = std::move(listener_info); 88 1042 : } 89 : 90 : private: 91 : Address::InstanceConstSharedPtr local_address_; 92 : bool local_address_restored_{false}; 93 : Address::InstanceConstSharedPtr remote_address_; 94 : Address::InstanceConstSharedPtr direct_remote_address_; 95 : std::string server_name_; 96 : absl::optional<uint64_t> connection_id_; 97 : bool allow_syscall_for_interface_name_{false}; 98 : absl::optional<std::string> interface_name_; 99 : Ssl::ConnectionInfoConstSharedPtr ssl_info_; 100 : std::string ja3_hash_; 101 : absl::optional<std::chrono::milliseconds> round_trip_time_; 102 : FilterChainInfoConstSharedPtr filter_chain_info_; 103 : ListenerInfoConstSharedPtr listener_info_; 104 : }; 105 : 106 : class SocketImpl : public virtual Socket { 107 : public: 108 : SocketImpl(Socket::Type socket_type, const Address::InstanceConstSharedPtr& address_for_io_handle, 109 : const Address::InstanceConstSharedPtr& remote_address, 110 : const SocketCreationOptions& options); 111 : 112 : // Network::Socket 113 23933 : ConnectionInfoSetter& connectionInfoProvider() override { return *connection_info_provider_; } 114 4088 : const ConnectionInfoProvider& connectionInfoProvider() const override { 115 4088 : return *connection_info_provider_; 116 4088 : } 117 6268 : ConnectionInfoProviderSharedPtr connectionInfoProviderSharedPtr() const override { 118 6268 : return connection_info_provider_; 119 6268 : } 120 0 : SocketPtr duplicate() override { 121 : // Implementing the functionality here for all sockets is tricky because it leads 122 : // into object slicing issues. 123 0 : return nullptr; 124 0 : } 125 : 126 63225 : IoHandle& ioHandle() override { return *io_handle_; } 127 0 : const IoHandle& ioHandle() const override { return *io_handle_; } 128 2885 : void close() override { 129 2885 : if (io_handle_->isOpen()) { 130 2885 : io_handle_->close(); 131 2885 : } 132 2885 : } 133 0 : bool isOpen() const override { return io_handle_->isOpen(); } 134 1508 : void ensureOptions() { 135 1508 : if (!options_) { 136 902 : options_ = std::make_shared<std::vector<OptionConstSharedPtr>>(); 137 902 : } 138 1508 : } 139 0 : void addOption(const OptionConstSharedPtr& option) override { 140 0 : ensureOptions(); 141 0 : options_->emplace_back(std::move(option)); 142 0 : } 143 1508 : void addOptions(const OptionsSharedPtr& options) override { 144 1508 : ensureOptions(); 145 1508 : Network::Socket::appendOptions(options_, options); 146 1508 : } 147 : 148 : Api::SysCallIntResult bind(Network::Address::InstanceConstSharedPtr address) override; 149 : Api::SysCallIntResult listen(int backlog) override; 150 : Api::SysCallIntResult connect(const Address::InstanceConstSharedPtr addr) override; 151 : Api::SysCallIntResult setSocketOption(int level, int optname, const void* optval, 152 : socklen_t optlen) override; 153 : Api::SysCallIntResult getSocketOption(int level, int optname, void* optval, 154 : socklen_t* optlen) const override; 155 : Api::SysCallIntResult ioctl(unsigned long control_code, void* in_buffer, 156 : unsigned long in_buffer_len, void* out_buffer, 157 : unsigned long out_buffer_len, unsigned long* bytes_returned) override; 158 : 159 : Api::SysCallIntResult setBlockingForTest(bool blocking) override; 160 : 161 1414 : const OptionsSharedPtr& options() const override { return options_; } 162 0 : Socket::Type socketType() const override { return sock_type_; } 163 1353 : Address::Type addressType() const override { return addr_type_; } 164 : absl::optional<Address::IpVersion> ipVersion() const override; 165 : 166 : protected: 167 : SocketImpl(IoHandlePtr&& io_handle, const Address::InstanceConstSharedPtr& local_address, 168 : const Address::InstanceConstSharedPtr& remote_address); 169 : 170 : const IoHandlePtr io_handle_; 171 : const std::shared_ptr<ConnectionInfoSetterImpl> connection_info_provider_; 172 : OptionsSharedPtr options_; 173 : Socket::Type sock_type_; 174 : Address::Type addr_type_; 175 : }; 176 : 177 : } // namespace Network 178 : } // namespace Envoy