blob: f97a4593082cebeb88f5778ca181179618862262 [file] [log] [blame]
Bence Békybac04052022-04-07 15:44:29 -04001// 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"
martinduke35b6eac2022-08-17 17:13:45 -070018#include "quiche/quic/core/deterministic_connection_id_generator.h"
vasilvv0e7c3c02022-07-11 13:27:20 -070019#include "quiche/quic/core/io/quic_event_loop.h"
Bence Békybac04052022-04-07 15:44:29 -040020#include "quiche/quic/core/quic_config.h"
Bence Békybac04052022-04-07 15:44:29 -040021#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"
vasilvvc5d5f7a2022-09-19 13:40:40 -070024#include "quiche/quic/core/socket_factory.h"
Bence Békybac04052022-04-07 15:44:29 -040025#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
29namespace quic {
30
31namespace test {
32class QuicServerPeer;
33} // namespace test
34
35class QuicDispatcher;
36class QuicPacketReader;
37
vasilvv0e7c3c02022-07-11 13:27:20 -070038class QuicServer : public QuicSpdyServerBase, public QuicSocketEventListener {
Bence Békybac04052022-04-07 15:44:29 -040039 public:
ericorth60abf6c2022-08-11 09:36:27 -070040 // `quic_simple_server_backend` must outlive the created QuicServer.
Bence Békybac04052022-04-07 15:44:29 -040041 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ékybac04052022-04-07 15:44:29 -040057 // 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
vasilvv0e7c3c02022-07-11 13:27:20 -070065 // Server deletion is imminent. Start cleaning up any pending sessions.
Bence Békybac04052022-04-07 15:44:29 -040066 virtual void Shutdown();
67
vasilvv0e7c3c02022-07-11 13:27:20 -070068 // QuicSocketEventListener implementation.
69 void OnSocketEvent(QuicEventLoop* event_loop, QuicUdpSocketFd fd,
70 QuicSocketEventMask events) override;
Bence Békybac04052022-04-07 15:44:29 -040071
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
vasilvv0e7c3c02022-07-11 13:27:20 -070086 QuicEventLoop* event_loop() { return event_loop_.get(); }
Bence Békybac04052022-04-07 15:44:29 -040087
88 protected:
89 virtual QuicPacketWriter* CreateWriter(int fd);
90
91 virtual QuicDispatcher* CreateQuicDispatcher();
92
vasilvv0e7c3c02022-07-11 13:27:20 -070093 virtual std::unique_ptr<QuicEventLoop> CreateEventLoop();
94
Bence Békybac04052022-04-07 15:44:29 -040095 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
martinduke35b6eac2022-08-17 17:13:45 -0700112 ConnectionIdGeneratorInterface& connection_id_generator() {
113 return connection_id_generator_;
114 }
115
Bence Békybac04052022-04-07 15:44:29 -0400116 private:
117 friend class quic::test::QuicServerPeer;
118
119 // Initialize the internal state of the server.
120 void Initialize();
121
vasilvv0e7c3c02022-07-11 13:27:20 -0700122 // Schedules alarms and notifies the server of the I/O events.
123 std::unique_ptr<QuicEventLoop> event_loop_;
ericorth42ffc1e2022-08-12 15:53:29 -0700124 // 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ékybac04052022-04-07 15:44:29 -0400127 // Accepts data from the framer and demuxes clients to sessions.
128 std::unique_ptr<QuicDispatcher> dispatcher_;
Bence Békybac04052022-04-07 15:44:29 -0400129
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_;
martinduke35b6eac2022-08-17 17:13:45 -0700168
169 DeterministicConnectionIdGenerator connection_id_generator_;
Bence Békybac04052022-04-07 15:44:29 -0400170};
171
172} // namespace quic
173
174#endif // QUICHE_QUIC_TOOLS_QUIC_SERVER_H_