Line data Source code
1 : #include "source/common/network/socket_option_impl.h" 2 : 3 : #include "envoy/common/exception.h" 4 : #include "envoy/config/core/v3/base.pb.h" 5 : 6 : #include "source/common/api/os_sys_calls_impl.h" 7 : #include "source/common/common/assert.h" 8 : #include "source/common/common/scalar_to_byte_vector.h" 9 : #include "source/common/common/utility.h" 10 : #include "source/common/network/address_impl.h" 11 : 12 : namespace Envoy { 13 : namespace Network { 14 : 15 : // Socket::Option 16 : bool SocketOptionImpl::setOption(Socket& socket, 17 2737 : envoy::config::core::v3::SocketOption::SocketState state) const { 18 2737 : if (in_state_ == state) { 19 1475 : if (!optname_.hasValue()) { 20 0 : ENVOY_LOG(warn, "Failed to set unsupported option on socket"); 21 0 : return false; 22 0 : } 23 : 24 1475 : if (socket_type_.has_value() && *socket_type_ != socket.socketType()) { 25 0 : ENVOY_LOG(info, "Skipping inapplicable socket option {}", optname_.name()); 26 0 : return true; 27 0 : } 28 : 29 1475 : const Api::SysCallIntResult result = 30 1475 : SocketOptionImpl::setSocketOption(socket, optname_, value_.data(), value_.size()); 31 1475 : if (result.return_value_ != 0) { 32 0 : ENVOY_LOG(warn, "Setting {} option on socket failed: {}", optname_.name(), 33 0 : errorDetails(result.errno_)); 34 0 : return false; 35 0 : } 36 1475 : } 37 : 38 2737 : return true; 39 2737 : } 40 : 41 0 : void SocketOptionImpl::hashKey(std::vector<uint8_t>& hash_key) const { 42 0 : if (optname_.hasValue()) { 43 0 : pushScalarToByteVector(optname_.level(), hash_key); 44 0 : pushScalarToByteVector(optname_.option(), hash_key); 45 0 : hash_key.insert(hash_key.end(), value_.begin(), value_.end()); 46 0 : } 47 0 : } 48 : 49 : absl::optional<Socket::Option::Details> 50 : SocketOptionImpl::getOptionDetails(const Socket&, 51 0 : envoy::config::core::v3::SocketOption::SocketState state) const { 52 0 : if (state != in_state_ || !isSupported()) { 53 0 : return absl::nullopt; 54 0 : } 55 : 56 0 : Socket::Option::Details info; 57 0 : info.name_ = optname_; 58 0 : info.value_ = {value_.begin(), value_.end()}; 59 0 : return absl::make_optional(std::move(info)); 60 0 : } 61 : 62 1030 : bool SocketOptionImpl::isSupported() const { return optname_.hasValue(); } 63 : 64 : Api::SysCallIntResult SocketOptionImpl::setSocketOption(Socket& socket, 65 : const Network::SocketOptionName& optname, 66 1475 : const void* value, size_t size) { 67 1475 : if (!optname.hasValue()) { 68 0 : return {-1, SOCKET_ERROR_NOT_SUP}; 69 0 : } 70 : 71 1475 : return socket.setSocketOption(optname.level(), optname.option(), value, size); 72 1475 : } 73 : 74 : } // namespace Network 75 : } // namespace Envoy