Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [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 | |
| 16 | #include "absl/strings/string_view.h" |
| 17 | #include "quiche/quic/core/crypto/quic_crypto_server_config.h" |
martinduke | 35b6eac | 2022-08-17 17:13:45 -0700 | [diff] [blame] | 18 | #include "quiche/quic/core/deterministic_connection_id_generator.h" |
vasilvv | 0e7c3c0 | 2022-07-11 13:27:20 -0700 | [diff] [blame] | 19 | #include "quiche/quic/core/io/quic_event_loop.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 20 | #include "quiche/quic/core/quic_config.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 21 | #include "quiche/quic/core/quic_packet_writer.h" |
| 22 | #include "quiche/quic/core/quic_udp_socket.h" |
| 23 | #include "quiche/quic/core/quic_version_manager.h" |
vasilvv | c5d5f7a | 2022-09-19 13:40:40 -0700 | [diff] [blame] | 24 | #include "quiche/quic/core/socket_factory.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 25 | #include "quiche/quic/platform/api/quic_socket_address.h" |
| 26 | #include "quiche/quic/tools/quic_simple_server_backend.h" |
| 27 | #include "quiche/quic/tools/quic_spdy_server_base.h" |
| 28 | |
| 29 | namespace quic { |
| 30 | |
| 31 | namespace test { |
| 32 | class QuicServerPeer; |
| 33 | } // namespace test |
| 34 | |
| 35 | class QuicDispatcher; |
| 36 | class QuicPacketReader; |
| 37 | |
vasilvv | 0e7c3c0 | 2022-07-11 13:27:20 -0700 | [diff] [blame] | 38 | class QuicServer : public QuicSpdyServerBase, public QuicSocketEventListener { |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 39 | public: |
ericorth | 60abf6c | 2022-08-11 09:36:27 -0700 | [diff] [blame] | 40 | // `quic_simple_server_backend` must outlive the created QuicServer. |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 41 | QuicServer(std::unique_ptr<ProofSource> proof_source, |
| 42 | QuicSimpleServerBackend* quic_simple_server_backend); |
| 43 | QuicServer(std::unique_ptr<ProofSource> proof_source, |
| 44 | QuicSimpleServerBackend* quic_simple_server_backend, |
| 45 | const ParsedQuicVersionVector& supported_versions); |
| 46 | QuicServer(std::unique_ptr<ProofSource> proof_source, |
| 47 | const QuicConfig& config, |
| 48 | const QuicCryptoServerConfig::ConfigOptions& crypto_config_options, |
| 49 | const ParsedQuicVersionVector& supported_versions, |
| 50 | QuicSimpleServerBackend* quic_simple_server_backend, |
| 51 | uint8_t expected_server_connection_id_length); |
| 52 | QuicServer(const QuicServer&) = delete; |
| 53 | QuicServer& operator=(const QuicServer&) = delete; |
| 54 | |
| 55 | ~QuicServer() override; |
| 56 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 57 | // Start listening on the specified address. |
| 58 | bool CreateUDPSocketAndListen(const QuicSocketAddress& address) override; |
| 59 | // Handles all events. Does not return. |
| 60 | void HandleEventsForever() override; |
| 61 | |
| 62 | // Wait up to 50ms, and handle any events which occur. |
| 63 | void WaitForEvents(); |
| 64 | |
vasilvv | 0e7c3c0 | 2022-07-11 13:27:20 -0700 | [diff] [blame] | 65 | // Server deletion is imminent. Start cleaning up any pending sessions. |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 66 | virtual void Shutdown(); |
| 67 | |
vasilvv | 0e7c3c0 | 2022-07-11 13:27:20 -0700 | [diff] [blame] | 68 | // QuicSocketEventListener implementation. |
| 69 | void OnSocketEvent(QuicEventLoop* event_loop, QuicUdpSocketFd fd, |
| 70 | QuicSocketEventMask events) override; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 71 | |
| 72 | void SetChloMultiplier(size_t multiplier) { |
| 73 | crypto_config_.set_chlo_multiplier(multiplier); |
| 74 | } |
| 75 | |
| 76 | void SetPreSharedKey(absl::string_view key) { |
| 77 | crypto_config_.set_pre_shared_key(key); |
| 78 | } |
| 79 | |
| 80 | bool overflow_supported() { return overflow_supported_; } |
| 81 | |
| 82 | QuicPacketCount packets_dropped() { return packets_dropped_; } |
| 83 | |
| 84 | int port() { return port_; } |
| 85 | |
vasilvv | 0e7c3c0 | 2022-07-11 13:27:20 -0700 | [diff] [blame] | 86 | QuicEventLoop* event_loop() { return event_loop_.get(); } |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 87 | |
| 88 | protected: |
| 89 | virtual QuicPacketWriter* CreateWriter(int fd); |
| 90 | |
| 91 | virtual QuicDispatcher* CreateQuicDispatcher(); |
| 92 | |
vasilvv | 0e7c3c0 | 2022-07-11 13:27:20 -0700 | [diff] [blame] | 93 | virtual std::unique_ptr<QuicEventLoop> CreateEventLoop(); |
| 94 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 95 | const QuicConfig& config() const { return config_; } |
| 96 | const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; } |
| 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 | |
| 108 | uint8_t expected_server_connection_id_length() { |
| 109 | return expected_server_connection_id_length_; |
| 110 | } |
| 111 | |
martinduke | 35b6eac | 2022-08-17 17:13:45 -0700 | [diff] [blame] | 112 | ConnectionIdGeneratorInterface& connection_id_generator() { |
| 113 | return connection_id_generator_; |
| 114 | } |
| 115 | |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 116 | private: |
| 117 | friend class quic::test::QuicServerPeer; |
| 118 | |
| 119 | // Initialize the internal state of the server. |
| 120 | void Initialize(); |
| 121 | |
vasilvv | 0e7c3c0 | 2022-07-11 13:27:20 -0700 | [diff] [blame] | 122 | // Schedules alarms and notifies the server of the I/O events. |
| 123 | std::unique_ptr<QuicEventLoop> event_loop_; |
ericorth | 42ffc1e | 2022-08-12 15:53:29 -0700 | [diff] [blame] | 124 | // Used by some backends to create additional sockets, e.g. for upstream |
| 125 | // destination connections for proxying. |
| 126 | std::unique_ptr<SocketFactory> socket_factory_; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 127 | // Accepts data from the framer and demuxes clients to sessions. |
| 128 | std::unique_ptr<QuicDispatcher> dispatcher_; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 129 | |
| 130 | // The port the server is listening on. |
| 131 | int port_; |
| 132 | |
| 133 | // Listening connection. Also used for outbound client communication. |
| 134 | QuicUdpSocketFd fd_; |
| 135 | |
| 136 | // If overflow_supported_ is true this will be the number of packets dropped |
| 137 | // during the lifetime of the server. This may overflow if enough packets |
| 138 | // are dropped. |
| 139 | QuicPacketCount packets_dropped_; |
| 140 | |
| 141 | // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped |
| 142 | // because the socket would otherwise overflow. |
| 143 | bool overflow_supported_; |
| 144 | |
| 145 | // If true, do not call Shutdown on the dispatcher. Connections will close |
| 146 | // without sending a final connection close. |
| 147 | bool silent_close_; |
| 148 | |
| 149 | // config_ contains non-crypto parameters that are negotiated in the crypto |
| 150 | // handshake. |
| 151 | QuicConfig config_; |
| 152 | // crypto_config_ contains crypto parameters for the handshake. |
| 153 | QuicCryptoServerConfig crypto_config_; |
| 154 | // crypto_config_options_ contains crypto parameters for the handshake. |
| 155 | QuicCryptoServerConfig::ConfigOptions crypto_config_options_; |
| 156 | |
| 157 | // Used to generate current supported versions. |
| 158 | QuicVersionManager version_manager_; |
| 159 | |
| 160 | // Point to a QuicPacketReader object on the heap. The reader allocates more |
| 161 | // space than allowed on the stack. |
| 162 | std::unique_ptr<QuicPacketReader> packet_reader_; |
| 163 | |
| 164 | QuicSimpleServerBackend* quic_simple_server_backend_; // unowned. |
| 165 | |
| 166 | // Connection ID length expected to be read on incoming IETF short headers. |
| 167 | uint8_t expected_server_connection_id_length_; |
martinduke | 35b6eac | 2022-08-17 17:13:45 -0700 | [diff] [blame] | 168 | |
| 169 | DeterministicConnectionIdGenerator connection_id_generator_; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } // namespace quic |
| 173 | |
| 174 | #endif // QUICHE_QUIC_TOOLS_QUIC_SERVER_H_ |