[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | // |
| 5 | // A toy server, which listens on a specified address for QUIC traffic and |
| 6 | // handles incoming responses. |
rtenneti | e0ee6eb | 2015-05-01 00:55:09 | [diff] [blame] | 7 | // |
| 8 | // Note that this server is intended to verify correctness of the client and is |
| 9 | // in no way expected to be performant. |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 10 | |
| 11 | #ifndef NET_TOOLS_QUIC_QUIC_SERVER_H_ |
| 12 | #define NET_TOOLS_QUIC_QUIC_SERVER_H_ |
| 13 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 18 | #include "base/macros.h" |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 19 | #include "net/base/ip_endpoint.h" |
rch | 675757b | 2016-07-29 16:40:11 | [diff] [blame^] | 20 | #include "net/quic/chromium/quic_chromium_connection_helper.h" |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 21 | #include "net/quic/crypto/quic_crypto_server_config.h" |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 22 | #include "net/quic/quic_config.h" |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 23 | #include "net/quic/quic_framer.h" |
[email protected] | 6c7e652 | 2013-09-29 15:27:44 | [diff] [blame] | 24 | #include "net/tools/epoll_server/epoll_server.h" |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 25 | #include "net/tools/quic/quic_default_packet_writer.h" |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 26 | |
| 27 | namespace net { |
| 28 | |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 29 | namespace test { |
| 30 | class QuicServerPeer; |
| 31 | } // namespace test |
| 32 | |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 33 | class QuicDispatcher; |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 34 | class QuicPacketReader; |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 35 | |
| 36 | class QuicServer : public EpollCallbackInterface { |
| 37 | public: |
fayang | 199cfb0 | 2016-07-13 03:56:39 | [diff] [blame] | 38 | explicit QuicServer(std::unique_ptr<ProofSource> proof_source); |
| 39 | QuicServer(std::unique_ptr<ProofSource> proof_source, |
rch | 1fe2eeb | 2015-10-26 14:45:57 | [diff] [blame] | 40 | const QuicConfig& config, |
nharper | d5cddca | 2016-02-27 03:37:52 | [diff] [blame] | 41 | const QuicCryptoServerConfig::ConfigOptions& server_config_options, |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 42 | const QuicVersionVector& supported_versions); |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 43 | |
dcheng | ff0bb8c | 2014-10-27 21:58:51 | [diff] [blame] | 44 | ~QuicServer() override; |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 45 | |
| 46 | // Start listening on the specified address. |
danzh | 33407f1 | 2016-03-04 21:58:16 | [diff] [blame] | 47 | bool CreateUDPSocketAndListen(const IPEndPoint& address); |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 48 | |
| 49 | // Wait up to 50ms, and handle any events which occur. |
| 50 | void WaitForEvents(); |
| 51 | |
| 52 | // Server deletion is imminent. Start cleaning up the epoll server. |
| 53 | void Shutdown(); |
| 54 | |
| 55 | // From EpollCallbackInterface |
dcheng | ff0bb8c | 2014-10-27 21:58:51 | [diff] [blame] | 56 | void OnRegistration(EpollServer* eps, int fd, int event_mask) override {} |
| 57 | void OnModification(int fd, int event_mask) override {} |
| 58 | void OnEvent(int fd, EpollEvent* event) override; |
| 59 | void OnUnregistration(int fd, bool replaced) override {} |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 60 | |
dcheng | ff0bb8c | 2014-10-27 21:58:51 | [diff] [blame] | 61 | void OnShutdown(EpollServer* eps, int fd) override {} |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 62 | |
[email protected] | 97cf302 | 2013-09-05 14:30:16 | [diff] [blame] | 63 | void SetStrikeRegisterNoStartupPeriod() { |
| 64 | crypto_config_.set_strike_register_no_startup_period(); |
| 65 | } |
| 66 | |
jri | 3285d93a | 2015-12-14 08:53:19 | [diff] [blame] | 67 | void SetChloMultiplier(size_t multiplier) { |
| 68 | crypto_config_.set_chlo_multiplier(multiplier); |
| 69 | } |
| 70 | |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 71 | bool overflow_supported() { return overflow_supported_; } |
| 72 | |
pkasting | 57b688a3 | 2014-12-02 21:38:01 | [diff] [blame] | 73 | QuicPacketCount packets_dropped() { return packets_dropped_; } |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 74 | |
[email protected] | e45d6889 | 2013-03-30 17:50:10 | [diff] [blame] | 75 | int port() { return port_; } |
| 76 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 77 | protected: |
rch | c99f380c | 2015-03-26 19:50:56 | [diff] [blame] | 78 | virtual QuicDefaultPacketWriter* CreateWriter(int fd); |
| 79 | |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 80 | virtual QuicDispatcher* CreateQuicDispatcher(); |
| 81 | |
| 82 | const QuicConfig& config() const { return config_; } |
rjshade | d5ced07 | 2015-12-18 19:26:02 | [diff] [blame] | 83 | const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } |
[email protected] | a5b9817 | 2014-06-18 07:01:59 | [diff] [blame] | 84 | const QuicVersionVector& supported_versions() const { |
| 85 | return supported_versions_; |
| 86 | } |
| 87 | EpollServer* epoll_server() { return &epoll_server_; } |
| 88 | |
rtenneti | 3183ac4 | 2015-02-28 03:39:41 | [diff] [blame] | 89 | QuicDispatcher* dispatcher() { return dispatcher_.get(); } |
| 90 | |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 91 | private: |
rch | 750447f9 | 2016-01-31 02:54:53 | [diff] [blame] | 92 | friend class net::test::QuicServerPeer; |
[email protected] | cbd731e | 2013-10-24 00:20:39 | [diff] [blame] | 93 | |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 94 | // Initialize the internal state of the server. |
| 95 | void Initialize(); |
| 96 | |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 97 | // Accepts data from the framer and demuxes clients to sessions. |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 98 | std::unique_ptr<QuicDispatcher> dispatcher_; |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 99 | // Frames incoming packets and hands them to the dispatcher. |
| 100 | EpollServer epoll_server_; |
| 101 | |
| 102 | // The port the server is listening on. |
| 103 | int port_; |
| 104 | |
| 105 | // Listening connection. Also used for outbound client communication. |
| 106 | int fd_; |
| 107 | |
| 108 | // If overflow_supported_ is true this will be the number of packets dropped |
| 109 | // during the lifetime of the server. This may overflow if enough packets |
| 110 | // are dropped. |
pkasting | 57b688a3 | 2014-12-02 21:38:01 | [diff] [blame] | 111 | QuicPacketCount packets_dropped_; |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 112 | |
| 113 | // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped |
| 114 | // because the socket would otherwise overflow. |
| 115 | bool overflow_supported_; |
| 116 | |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 117 | // config_ contains non-crypto parameters that are negotiated in the crypto |
| 118 | // handshake. |
| 119 | QuicConfig config_; |
| 120 | // crypto_config_ contains crypto parameters for the handshake. |
| 121 | QuicCryptoServerConfig crypto_config_; |
nharper | d5cddca | 2016-02-27 03:37:52 | [diff] [blame] | 122 | // crypto_config_options_ contains crypto parameters for the handshake. |
| 123 | QuicCryptoServerConfig::ConfigOptions crypto_config_options_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 124 | |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 125 | // This vector contains QUIC versions which we currently support. |
| 126 | // This should be ordered such that the highest supported version is the first |
| 127 | // element, with subsequent elements in descending order (versions can be |
| 128 | // skipped as necessary). |
| 129 | QuicVersionVector supported_versions_; |
| 130 | |
danzh | 33407f1 | 2016-03-04 21:58:16 | [diff] [blame] | 131 | // Point to a QuicPacketReader object on the heap. The reader allocates more |
| 132 | // space than allowed on the stack. |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 133 | std::unique_ptr<QuicPacketReader> packet_reader_; |
rtenneti | fb3fa6c | 2015-03-16 23:04:55 | [diff] [blame] | 134 | |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(QuicServer); |
| 136 | }; |
| 137 | |
[email protected] | 519e4988 | 2013-03-27 08:45:32 | [diff] [blame] | 138 | } // namespace net |
| 139 | |
| 140 | #endif // NET_TOOLS_QUIC_QUIC_SERVER_H_ |