1
#pragma once
2

            
3
#include "envoy/common/platform.h"
4
#include "envoy/config/core/v3/base.pb.h"
5
#include "envoy/network/socket.h"
6

            
7
#include "source/common/common/logger.h"
8
#include "source/common/protobuf/protobuf.h"
9
#include "source/common/protobuf/utility.h"
10

            
11
#include "absl/types/optional.h"
12

            
13
namespace Envoy {
14
namespace Network {
15

            
16
struct TcpKeepaliveConfig {
17
  absl::optional<uint32_t>
18
      keepalive_probes_; // Number of unanswered probes before the connection is dropped
19
  absl::optional<uint32_t> keepalive_time_; // Connection idle time before probing will start, in ms
20
  absl::optional<uint32_t> keepalive_interval_; // Interval between probes, in ms
21
};
22

            
23
static inline Network::TcpKeepaliveConfig
24
29
parseTcpKeepaliveConfig(const envoy::config::core::v3::TcpKeepalive& options) {
25
29
  return Network::TcpKeepaliveConfig{PROTOBUF_GET_OPTIONAL_WRAPPED(options, keepalive_probes),
26
29
                                     PROTOBUF_GET_OPTIONAL_WRAPPED(options, keepalive_time),
27
29
                                     PROTOBUF_GET_OPTIONAL_WRAPPED(options, keepalive_interval)};
28
29
}
29

            
30
31
static inline bool isTcpKeepaliveConfigDisabled(const Network::TcpKeepaliveConfig& config) {
31
31
  return (config.keepalive_probes_.has_value() && config.keepalive_probes_.value() == 0) ||
32
31
         (config.keepalive_time_.has_value() && config.keepalive_time_.value() == 0) ||
33
31
         (config.keepalive_interval_.has_value() && config.keepalive_interval_.value() == 0);
34
31
}
35

            
36
class SocketOptionFactory : Logger::Loggable<Logger::Id::connection> {
37
public:
38
  static std::unique_ptr<Socket::Options>
39
  buildTcpKeepaliveOptions(Network::TcpKeepaliveConfig keepalive_config);
40
  static std::unique_ptr<Socket::Options> buildIpFreebindOptions();
41
  static std::unique_ptr<Socket::Options> buildIpTransparentOptions();
42
  static std::unique_ptr<Socket::Options>
43
  buildWFPRedirectRecordsOptions(const Win32RedirectRecords& redirect_records);
44
  static std::unique_ptr<Socket::Options> buildSocketMarkOptions(uint32_t mark);
45
  static std::unique_ptr<Socket::Options> buildSocketNoSigpipeOptions();
46
  static std::unique_ptr<Socket::Options> buildTcpFastOpenOptions(uint32_t queue_length);
47
  static std::unique_ptr<Socket::Options> buildLiteralOptions(
48
      const Protobuf::RepeatedPtrField<envoy::config::core::v3::SocketOption>& socket_options);
49
  static std::unique_ptr<Socket::Options> buildIpPacketInfoOptions();
50
  static std::unique_ptr<Socket::Options> buildRxQueueOverFlowOptions();
51
  static std::unique_ptr<Socket::Options> buildReusePortOptions();
52
  static std::unique_ptr<Socket::Options> buildUdpGroOptions();
53
  static std::unique_ptr<Socket::Options> buildZeroSoLingerOptions();
54
  static std::unique_ptr<Socket::Options> buildIpRecvTosOptions();
55
  static std::unique_ptr<Socket::Options> buildBindAddressNoPort();
56
  /**
57
   * @param supports_v4_mapped_v6_addresses true if this option is to be applied to a v6 socket with
58
   * v4-mapped v6 address(i.e. ::ffff:172.21.0.6) support.
59
   */
60
  static std::unique_ptr<Socket::Options>
61
  buildDoNotFragmentOptions(bool supports_v4_mapped_v6_addresses);
62
};
63
} // namespace Network
64
} // namespace Envoy