xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/tools/quic_server.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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"
18 #include "quiche/quic/core/deterministic_connection_id_generator.h"
19 #include "quiche/quic/core/io/quic_event_loop.h"
20 #include "quiche/quic/core/quic_config.h"
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"
24 #include "quiche/quic/core/socket_factory.h"
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 
38 class QuicServer : public QuicSpdyServerBase, public QuicSocketEventListener {
39  public:
40   // `quic_simple_server_backend` must outlive the created QuicServer.
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 
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 
65   // Server deletion is imminent.  Start cleaning up any pending sessions.
66   virtual void Shutdown();
67 
68   // QuicSocketEventListener implementation.
69   void OnSocketEvent(QuicEventLoop* event_loop, QuicUdpSocketFd fd,
70                      QuicSocketEventMask events) override;
71 
SetChloMultiplier(size_t multiplier)72   void SetChloMultiplier(size_t multiplier) {
73     crypto_config_.set_chlo_multiplier(multiplier);
74   }
75 
SetPreSharedKey(absl::string_view key)76   void SetPreSharedKey(absl::string_view key) {
77     crypto_config_.set_pre_shared_key(key);
78   }
79 
overflow_supported()80   bool overflow_supported() { return overflow_supported_; }
81 
packets_dropped()82   QuicPacketCount packets_dropped() { return packets_dropped_; }
83 
port()84   int port() { return port_; }
85 
event_loop()86   QuicEventLoop* event_loop() { return event_loop_.get(); }
87 
88  protected:
89   virtual QuicPacketWriter* CreateWriter(int fd);
90 
91   virtual QuicDispatcher* CreateQuicDispatcher();
92 
93   virtual std::unique_ptr<QuicEventLoop> CreateEventLoop();
94 
config()95   const QuicConfig& config() const { return config_; }
crypto_config()96   const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
97 
dispatcher()98   QuicDispatcher* dispatcher() { return dispatcher_.get(); }
99 
version_manager()100   QuicVersionManager* version_manager() { return &version_manager_; }
101 
server_backend()102   QuicSimpleServerBackend* server_backend() {
103     return quic_simple_server_backend_;
104   }
105 
set_silent_close(bool value)106   void set_silent_close(bool value) { silent_close_ = value; }
107 
expected_server_connection_id_length()108   uint8_t expected_server_connection_id_length() {
109     return expected_server_connection_id_length_;
110   }
111 
connection_id_generator()112   ConnectionIdGeneratorInterface& connection_id_generator() {
113     return connection_id_generator_;
114   }
115 
116  private:
117   friend class quic::test::QuicServerPeer;
118 
119   // Initialize the internal state of the server.
120   void Initialize();
121 
122   // Schedules alarms and notifies the server of the I/O events.
123   std::unique_ptr<QuicEventLoop> event_loop_;
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_;
127   // Accepts data from the framer and demuxes clients to sessions.
128   std::unique_ptr<QuicDispatcher> dispatcher_;
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_;
168 
169   DeterministicConnectionIdGenerator connection_id_generator_;
170 };
171 
172 }  // namespace quic
173 
174 #endif  // QUICHE_QUIC_TOOLS_QUIC_SERVER_H_
175