blob: 0b11be3c4962c8e3c3e028952876cc4ef5cc7372 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 teama6ef0a62019-03-07 20:34:33 -050016#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"
rch034c98c2019-05-17 15:46:09 -070025#include "net/third_party/quiche/src/quic/tools/quic_spdy_server_base.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050026
27namespace quic {
28
29namespace test {
30class QuicServerPeer;
31} // namespace test
32
33class QuicDispatcher;
34class QuicPacketReader;
35
rch034c98c2019-05-17 15:46:09 -070036class QuicServer : public QuicSpdyServerBase,
37 public QuicEpollCallbackInterface {
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 public:
39 QuicServer(std::unique_ptr<ProofSource> proof_source,
40 QuicSimpleServerBackend* quic_simple_server_backend);
41 QuicServer(std::unique_ptr<ProofSource> proof_source,
dschinazic5589bd2019-09-12 14:50:11 -070042 QuicSimpleServerBackend* quic_simple_server_backend,
43 const ParsedQuicVersionVector& supported_versions);
44 QuicServer(std::unique_ptr<ProofSource> proof_source,
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 const QuicConfig& config,
dschinazic5589bd2019-09-12 14:50:11 -070046 const QuicCryptoServerConfig::ConfigOptions& crypto_config_options,
QUICHE teama6ef0a62019-03-07 20:34:33 -050047 const ParsedQuicVersionVector& supported_versions,
48 QuicSimpleServerBackend* quic_simple_server_backend,
dschinazi8ff74822019-05-28 16:37:20 -070049 uint8_t expected_server_connection_id_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -050050 QuicServer(const QuicServer&) = delete;
51 QuicServer& operator=(const QuicServer&) = delete;
52
53 ~QuicServer() override;
54
vasilvvc48c8712019-03-11 13:38:16 -070055 std::string Name() const override { return "QuicServer"; }
QUICHE teama6ef0a62019-03-07 20:34:33 -050056
57 // Start listening on the specified address.
rch034c98c2019-05-17 15:46:09 -070058 bool CreateUDPSocketAndListen(const QuicSocketAddress& address) override;
59 // Handles all events. Does not return.
60 void HandleEventsForever() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050061
62 // Wait up to 50ms, and handle any events which occur.
63 void WaitForEvents();
64
65 // Server deletion is imminent. Start cleaning up the epoll server.
66 virtual void Shutdown();
67
68 // From EpollCallbackInterface
dschinazi17d42422019-06-18 16:35:07 -070069 void OnRegistration(QuicEpollServer* /*eps*/,
70 int /*fd*/,
71 int /*event_mask*/) override {}
72 void OnModification(int /*fd*/, int /*event_mask*/) override {}
73 void OnEvent(int /*fd*/, QuicEpollEvent* /*event*/) override;
74 void OnUnregistration(int /*fd*/, bool /*replaced*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050075
dschinazi17d42422019-06-18 16:35:07 -070076 void OnShutdown(QuicEpollServer* /*eps*/, int /*fd*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050077
78 void SetChloMultiplier(size_t multiplier) {
79 crypto_config_.set_chlo_multiplier(multiplier);
80 }
81
82 void SetPreSharedKey(QuicStringPiece key) {
83 crypto_config_.set_pre_shared_key(key);
84 }
85
86 bool overflow_supported() { return overflow_supported_; }
87
88 QuicPacketCount packets_dropped() { return packets_dropped_; }
89
90 int port() { return port_; }
91
92 protected:
93 virtual QuicPacketWriter* CreateWriter(int fd);
94
95 virtual QuicDispatcher* CreateQuicDispatcher();
96
97 const QuicConfig& config() const { return config_; }
98 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
99 QuicEpollServer* epoll_server() { return &epoll_server_; }
100
101 QuicDispatcher* dispatcher() { return dispatcher_.get(); }
102
103 QuicVersionManager* version_manager() { return &version_manager_; }
104
105 QuicSimpleServerBackend* server_backend() {
106 return quic_simple_server_backend_;
107 }
108
109 void set_silent_close(bool value) { silent_close_ = value; }
110
dschinazi8ff74822019-05-28 16:37:20 -0700111 uint8_t expected_server_connection_id_length() {
112 return expected_server_connection_id_length_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500113 }
114
115 private:
116 friend class quic::test::QuicServerPeer;
117
118 // Initialize the internal state of the server.
119 void Initialize();
120
121 // Accepts data from the framer and demuxes clients to sessions.
122 std::unique_ptr<QuicDispatcher> dispatcher_;
123 // Frames incoming packets and hands them to the dispatcher.
124 QuicEpollServer epoll_server_;
125
126 // The port the server is listening on.
127 int port_;
128
129 // Listening connection. Also used for outbound client communication.
130 int fd_;
131
132 // If overflow_supported_ is true this will be the number of packets dropped
133 // during the lifetime of the server. This may overflow if enough packets
134 // are dropped.
135 QuicPacketCount packets_dropped_;
136
137 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
138 // because the socket would otherwise overflow.
139 bool overflow_supported_;
140
141 // If true, do not call Shutdown on the dispatcher. Connections will close
142 // without sending a final connection close.
143 bool silent_close_;
144
145 // config_ contains non-crypto parameters that are negotiated in the crypto
146 // handshake.
147 QuicConfig config_;
148 // crypto_config_ contains crypto parameters for the handshake.
149 QuicCryptoServerConfig crypto_config_;
150 // crypto_config_options_ contains crypto parameters for the handshake.
151 QuicCryptoServerConfig::ConfigOptions crypto_config_options_;
152
153 // Used to generate current supported versions.
154 QuicVersionManager version_manager_;
155
156 // Point to a QuicPacketReader object on the heap. The reader allocates more
157 // space than allowed on the stack.
158 std::unique_ptr<QuicPacketReader> packet_reader_;
159
160 QuicSimpleServerBackend* quic_simple_server_backend_; // unowned.
161
162 // Connection ID length expected to be read on incoming IETF short headers.
dschinazi8ff74822019-05-28 16:37:20 -0700163 uint8_t expected_server_connection_id_length_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500164};
165
166} // namespace quic
167
168#endif // QUICHE_QUIC_TOOLS_QUIC_SERVER_H_