1
#pragma once
2

            
3
#include <cstddef>
4

            
5
#include "envoy/common/platform.h"
6
#include "envoy/common/pure.h"
7
#include "envoy/network/socket.h"
8

            
9
namespace Envoy {
10
namespace Network {
11

            
12
/**
13
 * Options for creating a socket.
14
 */
15
struct SocketCreationOptions {
16
  // Specifies whether MPTCP should be enabled on the socket. This is only valid for Stream sockets,
17
  // and only valid on Linux.
18
  bool mptcp_enabled_{false};
19

            
20
  // Specifies the maximum size of the cache of the address instances associated with
21
  // packets received by this socket.
22
  // If this is 0, no addresses will be cached.
23
  // Is only valid for datagram sockets.
24
  size_t max_addresses_cache_size_{0};
25

            
26
2
  bool operator==(const SocketCreationOptions& rhs) const {
27
2
    return mptcp_enabled_ == rhs.mptcp_enabled_ &&
28
2
           max_addresses_cache_size_ == rhs.max_addresses_cache_size_;
29
2
  }
30
};
31

            
32
class SocketInterface {
33
public:
34
486
  virtual ~SocketInterface() = default;
35

            
36
  /**
37
   * Low level api to create a socket in the underlying host stack. Does not create a
38
   * @ref Network::SocketImpl
39
   * @param type type of socket requested
40
   * @param addr_type type of address used with the socket
41
   * @param version IP version if address type is IP
42
   * @param socket_v6only if the socket is ipv6 version only
43
   * @param options additional options for how to create the socket
44
   * @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor
45
   */
46
  virtual IoHandlePtr socket(Socket::Type type, Address::Type addr_type, Address::IpVersion version,
47
                             bool socket_v6only, const SocketCreationOptions& options) const PURE;
48

            
49
  /**
50
   * Low level api to create a socket in the underlying host stack. Does not create an
51
   * @ref Network::SocketImpl
52
   * @param socket_type type of socket requested
53
   * @param addr address that is gleaned for address type and version if needed
54
   * @param options additional options for how to create the socket
55
   * @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor
56
   */
57
  virtual IoHandlePtr socket(Socket::Type socket_type, const Address::InstanceConstSharedPtr addr,
58
                             const SocketCreationOptions& options) const PURE;
59

            
60
  /**
61
   * Returns true if the given family is supported on this machine.
62
   * @param domain the IP family.
63
   */
64
  virtual bool ipFamilySupported(int domain) PURE;
65
};
66

            
67
using SocketInterfacePtr = std::unique_ptr<SocketInterface>;
68

            
69
/**
70
 * Create IoHandle for given address.
71
 * @param type type of socket to be requested
72
 * @param addr address that is gleaned for address type, version and socket interface name
73
 * @return @ref Network::IoHandlePtr that wraps the underlying socket file descriptor
74
 */
75
static inline IoHandlePtr ioHandleForAddr(Socket::Type type,
76
                                          const Address::InstanceConstSharedPtr addr,
77
97134
                                          const SocketCreationOptions& options) {
78
97134
  return addr->socketInterface().socket(type, addr, options);
79
97134
}
80

            
81
} // namespace Network
82
} // namespace Envoy