copy wangle back into folly
[folly.git] / folly / wangle / acceptor / ServerSocketConfig.h
1 /*
2  *  Copyright (c) 2015, Facebook, Inc.
3  *  All rights reserved.
4  *
5  *  This source code is licensed under the BSD-style license found in the
6  *  LICENSE file in the root directory of this source tree. An additional grant
7  *  of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11
12 #include <folly/wangle/ssl/SSLCacheOptions.h>
13 #include <folly/wangle/ssl/SSLContextConfig.h>
14 #include <folly/wangle/ssl/TLSTicketKeySeeds.h>
15 #include <folly/wangle/ssl/SSLUtil.h>
16 #include <folly/wangle/acceptor/SocketOptions.h>
17
18 #include <boost/optional.hpp>
19 #include <chrono>
20 #include <fcntl.h>
21 #include <folly/Random.h>
22 #include <folly/SocketAddress.h>
23 #include <folly/String.h>
24 #include <folly/io/async/SSLContext.h>
25 #include <list>
26 #include <string>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <folly/io/async/AsyncSocket.h>
30 #include <folly/io/async/SSLContext.h>
31 #include <folly/SocketAddress.h>
32
33 namespace folly {
34
35 /**
36  * Configuration for a single Acceptor.
37  *
38  * This configures not only accept behavior, but also some types of SSL
39  * behavior that may make sense to configure on a per-VIP basis (e.g. which
40  * cert(s) we use, etc).
41  */
42 struct ServerSocketConfig {
43   ServerSocketConfig() {
44     // generate a single random current seed
45     uint8_t seed[32];
46     folly::Random::secureRandom(seed, sizeof(seed));
47     initialTicketSeeds.currentSeeds.push_back(
48       SSLUtil::hexlify(std::string((char *)seed, sizeof(seed))));
49   }
50
51   bool isSSL() const { return !(sslContextConfigs.empty()); }
52
53   /**
54    * Set/get the socket options to apply on all downstream connections.
55    */
56   void setSocketOptions(
57     const AsyncSocket::OptionMap& opts) {
58     socketOptions_ = filterIPSocketOptions(opts, bindAddress.getFamily());
59   }
60   AsyncSocket::OptionMap&
61   getSocketOptions() {
62     return socketOptions_;
63   }
64   const AsyncSocket::OptionMap&
65   getSocketOptions() const {
66     return socketOptions_;
67   }
68
69   bool hasExternalPrivateKey() const {
70     for (const auto& cfg : sslContextConfigs) {
71       if (!cfg.isLocalPrivateKey) {
72         return true;
73       }
74     }
75     return false;
76   }
77
78   /**
79    * The name of this acceptor; used for stats/reporting purposes.
80    */
81   std::string name;
82
83   /**
84    * The depth of the accept queue backlog.
85    */
86   uint32_t acceptBacklog{1024};
87
88   /**
89    * The number of milliseconds a connection can be idle before we close it.
90    */
91   std::chrono::milliseconds connectionIdleTimeout{600000};
92
93   /**
94    * The address to bind to.
95    */
96   SocketAddress bindAddress;
97
98   /**
99    * Options for controlling the SSL cache.
100    */
101   SSLCacheOptions sslCacheOptions{std::chrono::seconds(600), 20480, 200};
102
103   /**
104    * The initial TLS ticket seeds.
105    */
106   TLSTicketKeySeeds initialTicketSeeds;
107
108   /**
109    * The configs for all the SSL_CTX for use by this Acceptor.
110    */
111   std::vector<SSLContextConfig> sslContextConfigs;
112
113   /**
114    * Determines if the Acceptor does strict checking when loading the SSL
115    * contexts.
116    */
117   bool strictSSL{true};
118
119   /**
120    * Maximum number of concurrent pending SSL handshakes
121    */
122   uint32_t maxConcurrentSSLHandshakes{30720};
123
124  private:
125   AsyncSocket::OptionMap socketOptions_;
126 };
127
128 } // folly