QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [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. |
| 7 | // |
| 8 | // Note that this server is intended to verify correctness of the client and is |
| 9 | // in no way expected to be performant. |
| 10 | |
| 11 | #ifndef QUICHE_QUIC_TOOLS_QUIC_SERVER_H_ |
| 12 | #define QUICHE_QUIC_TOOLS_QUIC_SERVER_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_server_config.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_config.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_epoll_connection_helper.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/quic_framer.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/quic_packet_writer.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/quic_version_manager.h" |
| 22 | #include "net/third_party/quiche/src/quic/platform/api/quic_epoll.h" |
| 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
| 24 | #include "net/third_party/quiche/src/quic/tools/quic_simple_server_backend.h" |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 25 | #include "net/third_party/quiche/src/quic/tools/quic_spdy_server_base.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | |
| 27 | namespace quic { |
| 28 | |
| 29 | namespace test { |
| 30 | class QuicServerPeer; |
| 31 | } // namespace test |
| 32 | |
| 33 | class QuicDispatcher; |
| 34 | class QuicPacketReader; |
| 35 | |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 36 | class QuicServer : public QuicSpdyServerBase, |
| 37 | public QuicEpollCallbackInterface { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | public: |
| 39 | QuicServer(std::unique_ptr<ProofSource> proof_source, |
| 40 | QuicSimpleServerBackend* quic_simple_server_backend); |
| 41 | QuicServer(std::unique_ptr<ProofSource> proof_source, |
| 42 | const QuicConfig& config, |
| 43 | const QuicCryptoServerConfig::ConfigOptions& server_config_options, |
| 44 | const ParsedQuicVersionVector& supported_versions, |
| 45 | QuicSimpleServerBackend* quic_simple_server_backend, |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 46 | uint8_t expected_server_connection_id_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 47 | QuicServer(const QuicServer&) = delete; |
| 48 | QuicServer& operator=(const QuicServer&) = delete; |
| 49 | |
| 50 | ~QuicServer() override; |
| 51 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 52 | std::string Name() const override { return "QuicServer"; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | |
| 54 | // Start listening on the specified address. |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 55 | bool CreateUDPSocketAndListen(const QuicSocketAddress& address) override; |
| 56 | // Handles all events. Does not return. |
| 57 | void HandleEventsForever() override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 58 | |
| 59 | // Wait up to 50ms, and handle any events which occur. |
| 60 | void WaitForEvents(); |
| 61 | |
| 62 | // Server deletion is imminent. Start cleaning up the epoll server. |
| 63 | virtual void Shutdown(); |
| 64 | |
| 65 | // From EpollCallbackInterface |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame^] | 66 | void OnRegistration(QuicEpollServer* /*eps*/, |
| 67 | int /*fd*/, |
| 68 | int /*event_mask*/) override {} |
| 69 | void OnModification(int /*fd*/, int /*event_mask*/) override {} |
| 70 | void OnEvent(int /*fd*/, QuicEpollEvent* /*event*/) override; |
| 71 | void OnUnregistration(int /*fd*/, bool /*replaced*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 72 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame^] | 73 | void OnShutdown(QuicEpollServer* /*eps*/, int /*fd*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | |
| 75 | void SetChloMultiplier(size_t multiplier) { |
| 76 | crypto_config_.set_chlo_multiplier(multiplier); |
| 77 | } |
| 78 | |
| 79 | void SetPreSharedKey(QuicStringPiece key) { |
| 80 | crypto_config_.set_pre_shared_key(key); |
| 81 | } |
| 82 | |
| 83 | bool overflow_supported() { return overflow_supported_; } |
| 84 | |
| 85 | QuicPacketCount packets_dropped() { return packets_dropped_; } |
| 86 | |
| 87 | int port() { return port_; } |
| 88 | |
| 89 | protected: |
| 90 | virtual QuicPacketWriter* CreateWriter(int fd); |
| 91 | |
| 92 | virtual QuicDispatcher* CreateQuicDispatcher(); |
| 93 | |
| 94 | const QuicConfig& config() const { return config_; } |
| 95 | const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } |
| 96 | QuicEpollServer* epoll_server() { return &epoll_server_; } |
| 97 | |
| 98 | QuicDispatcher* dispatcher() { return dispatcher_.get(); } |
| 99 | |
| 100 | QuicVersionManager* version_manager() { return &version_manager_; } |
| 101 | |
| 102 | QuicSimpleServerBackend* server_backend() { |
| 103 | return quic_simple_server_backend_; |
| 104 | } |
| 105 | |
| 106 | void set_silent_close(bool value) { silent_close_ = value; } |
| 107 | |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 108 | uint8_t expected_server_connection_id_length() { |
| 109 | return expected_server_connection_id_length_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private: |
| 113 | friend class quic::test::QuicServerPeer; |
| 114 | |
| 115 | // Initialize the internal state of the server. |
| 116 | void Initialize(); |
| 117 | |
| 118 | // Accepts data from the framer and demuxes clients to sessions. |
| 119 | std::unique_ptr<QuicDispatcher> dispatcher_; |
| 120 | // Frames incoming packets and hands them to the dispatcher. |
| 121 | QuicEpollServer epoll_server_; |
| 122 | |
| 123 | // The port the server is listening on. |
| 124 | int port_; |
| 125 | |
| 126 | // Listening connection. Also used for outbound client communication. |
| 127 | int fd_; |
| 128 | |
| 129 | // If overflow_supported_ is true this will be the number of packets dropped |
| 130 | // during the lifetime of the server. This may overflow if enough packets |
| 131 | // are dropped. |
| 132 | QuicPacketCount packets_dropped_; |
| 133 | |
| 134 | // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped |
| 135 | // because the socket would otherwise overflow. |
| 136 | bool overflow_supported_; |
| 137 | |
| 138 | // If true, do not call Shutdown on the dispatcher. Connections will close |
| 139 | // without sending a final connection close. |
| 140 | bool silent_close_; |
| 141 | |
| 142 | // config_ contains non-crypto parameters that are negotiated in the crypto |
| 143 | // handshake. |
| 144 | QuicConfig config_; |
| 145 | // crypto_config_ contains crypto parameters for the handshake. |
| 146 | QuicCryptoServerConfig crypto_config_; |
| 147 | // crypto_config_options_ contains crypto parameters for the handshake. |
| 148 | QuicCryptoServerConfig::ConfigOptions crypto_config_options_; |
| 149 | |
| 150 | // Used to generate current supported versions. |
| 151 | QuicVersionManager version_manager_; |
| 152 | |
| 153 | // Point to a QuicPacketReader object on the heap. The reader allocates more |
| 154 | // space than allowed on the stack. |
| 155 | std::unique_ptr<QuicPacketReader> packet_reader_; |
| 156 | |
| 157 | QuicSimpleServerBackend* quic_simple_server_backend_; // unowned. |
| 158 | |
| 159 | // Connection ID length expected to be read on incoming IETF short headers. |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 160 | uint8_t expected_server_connection_id_length_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | } // namespace quic |
| 164 | |
| 165 | #endif // QUICHE_QUIC_TOOLS_QUIC_SERVER_H_ |