blob: 755e5e3fc960f688448d107d290c5676a40069c7 [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,
42 const QuicConfig& config,
43 const QuicCryptoServerConfig::ConfigOptions& server_config_options,
44 const ParsedQuicVersionVector& supported_versions,
45 QuicSimpleServerBackend* quic_simple_server_backend,
dschinazi8ff74822019-05-28 16:37:20 -070046 uint8_t expected_server_connection_id_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -050047 QuicServer(const QuicServer&) = delete;
48 QuicServer& operator=(const QuicServer&) = delete;
49
50 ~QuicServer() override;
51
vasilvvc48c8712019-03-11 13:38:16 -070052 std::string Name() const override { return "QuicServer"; }
QUICHE teama6ef0a62019-03-07 20:34:33 -050053
54 // Start listening on the specified address.
rch034c98c2019-05-17 15:46:09 -070055 bool CreateUDPSocketAndListen(const QuicSocketAddress& address) override;
56 // Handles all events. Does not return.
57 void HandleEventsForever() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050058
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
dschinazi17d42422019-06-18 16:35:07 -070066 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 teama6ef0a62019-03-07 20:34:33 -050072
dschinazi17d42422019-06-18 16:35:07 -070073 void OnShutdown(QuicEpollServer* /*eps*/, int /*fd*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050074
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
dschinazi8ff74822019-05-28 16:37:20 -0700108 uint8_t expected_server_connection_id_length() {
109 return expected_server_connection_id_length_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500110 }
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.
dschinazi8ff74822019-05-28 16:37:20 -0700160 uint8_t expected_server_connection_id_length_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161};
162
163} // namespace quic
164
165#endif // QUICHE_QUIC_TOOLS_QUIC_SERVER_H_