Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/common/platform.h" 4 : #include "envoy/common/pure.h" 5 : #include "envoy/network/socket.h" 6 : 7 : namespace Envoy { 8 : namespace Network { 9 : 10 : /** 11 : * Options for creating a socket. 12 : */ 13 : struct SocketCreationOptions { 14 : // Specifies whether MPTCP should be enabled on the socket. This is only valid for Stream sockets, 15 : // and only valid on Linux. 16 : bool mptcp_enabled_{false}; 17 : 18 0 : bool operator==(const SocketCreationOptions& rhs) const { 19 0 : return mptcp_enabled_ == rhs.mptcp_enabled_; 20 0 : } 21 : }; 22 : 23 : class SocketInterface { 24 : public: 25 0 : virtual ~SocketInterface() = default; 26 : 27 : /** 28 : * Low level api to create a socket in the underlying host stack. Does not create a 29 : * @ref Network::SocketImpl 30 : * @param type type of socket requested 31 : * @param addr_type type of address used with the socket 32 : * @param version IP version if address type is IP 33 : * @param socket_v6only if the socket is ipv6 version only 34 : * @param options additional options for how to create the socket 35 : * @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor 36 : */ 37 : virtual IoHandlePtr socket(Socket::Type type, Address::Type addr_type, Address::IpVersion version, 38 : bool socket_v6only, const SocketCreationOptions& options) const PURE; 39 : 40 : /** 41 : * Low level api to create a socket in the underlying host stack. Does not create an 42 : * @ref Network::SocketImpl 43 : * @param socket_type type of socket requested 44 : * @param addr address that is gleaned for address type and version if needed 45 : * @param options additional options for how to create the socket 46 : * @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor 47 : */ 48 : virtual IoHandlePtr socket(Socket::Type socket_type, const Address::InstanceConstSharedPtr addr, 49 : const SocketCreationOptions& options) const PURE; 50 : 51 : /** 52 : * Returns true if the given family is supported on this machine. 53 : * @param domain the IP family. 54 : */ 55 : virtual bool ipFamilySupported(int domain) PURE; 56 : }; 57 : 58 : using SocketInterfacePtr = std::unique_ptr<SocketInterface>; 59 : 60 : /** 61 : * Create IoHandle for given address. 62 : * @param type type of socket to be requested 63 : * @param addr address that is gleaned for address type, version and socket interface name 64 : * @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor 65 : */ 66 : static inline IoHandlePtr ioHandleForAddr(Socket::Type type, 67 : const Address::InstanceConstSharedPtr addr, 68 2645 : const SocketCreationOptions& options) { 69 2645 : return addr->socketInterface().socket(type, addr, options); 70 2645 : } 71 : 72 : } // namespace Network 73 : } // namespace Envoy