blob: 9ef5f0c79703837c566e37365db7ed517dfcd8f6 [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
16#include "base/macros.h"
17#include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_server_config.h"
18#include "net/third_party/quiche/src/quic/core/quic_config.h"
19#include "net/third_party/quiche/src/quic/core/quic_epoll_connection_helper.h"
20#include "net/third_party/quiche/src/quic/core/quic_framer.h"
21#include "net/third_party/quiche/src/quic/core/quic_packet_writer.h"
22#include "net/third_party/quiche/src/quic/core/quic_version_manager.h"
23#include "net/third_party/quiche/src/quic/platform/api/quic_epoll.h"
24#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
25#include "net/third_party/quiche/src/quic/tools/quic_simple_server_backend.h"
26
27namespace quic {
28
29namespace test {
30class QuicServerPeer;
31} // namespace test
32
33class QuicDispatcher;
34class QuicPacketReader;
35
36class QuicServer : public QuicEpollCallbackInterface {
37 public:
38 QuicServer(std::unique_ptr<ProofSource> proof_source,
39 QuicSimpleServerBackend* quic_simple_server_backend);
40 QuicServer(std::unique_ptr<ProofSource> proof_source,
41 const QuicConfig& config,
42 const QuicCryptoServerConfig::ConfigOptions& server_config_options,
43 const ParsedQuicVersionVector& supported_versions,
44 QuicSimpleServerBackend* quic_simple_server_backend,
45 uint8_t expected_connection_id_length);
46 QuicServer(const QuicServer&) = delete;
47 QuicServer& operator=(const QuicServer&) = delete;
48
49 ~QuicServer() override;
50
51 QuicString Name() const override { return "QuicServer"; }
52
53 // Start listening on the specified address.
54 bool CreateUDPSocketAndListen(const QuicSocketAddress& address);
55
56 // Wait up to 50ms, and handle any events which occur.
57 void WaitForEvents();
58
59 // Server deletion is imminent. Start cleaning up the epoll server.
60 virtual void Shutdown();
61
62 // From EpollCallbackInterface
63 void OnRegistration(QuicEpollServer* eps, int fd, int event_mask) override {}
64 void OnModification(int fd, int event_mask) override {}
65 void OnEvent(int fd, QuicEpollEvent* event) override;
66 void OnUnregistration(int fd, bool replaced) override {}
67
68 void OnShutdown(QuicEpollServer* eps, int fd) override {}
69
70 void SetChloMultiplier(size_t multiplier) {
71 crypto_config_.set_chlo_multiplier(multiplier);
72 }
73
74 void SetPreSharedKey(QuicStringPiece key) {
75 crypto_config_.set_pre_shared_key(key);
76 }
77
78 bool overflow_supported() { return overflow_supported_; }
79
80 QuicPacketCount packets_dropped() { return packets_dropped_; }
81
82 int port() { return port_; }
83
84 protected:
85 virtual QuicPacketWriter* CreateWriter(int fd);
86
87 virtual QuicDispatcher* CreateQuicDispatcher();
88
89 const QuicConfig& config() const { return config_; }
90 const QuicCryptoServerConfig& crypto_config() const { return crypto_config_; }
91 QuicEpollServer* epoll_server() { return &epoll_server_; }
92
93 QuicDispatcher* dispatcher() { return dispatcher_.get(); }
94
95 QuicVersionManager* version_manager() { return &version_manager_; }
96
97 QuicSimpleServerBackend* server_backend() {
98 return quic_simple_server_backend_;
99 }
100
101 void set_silent_close(bool value) { silent_close_ = value; }
102
103 uint8_t expected_connection_id_length() {
104 return expected_connection_id_length_;
105 }
106
107 private:
108 friend class quic::test::QuicServerPeer;
109
110 // Initialize the internal state of the server.
111 void Initialize();
112
113 // Accepts data from the framer and demuxes clients to sessions.
114 std::unique_ptr<QuicDispatcher> dispatcher_;
115 // Frames incoming packets and hands them to the dispatcher.
116 QuicEpollServer epoll_server_;
117
118 // The port the server is listening on.
119 int port_;
120
121 // Listening connection. Also used for outbound client communication.
122 int fd_;
123
124 // If overflow_supported_ is true this will be the number of packets dropped
125 // during the lifetime of the server. This may overflow if enough packets
126 // are dropped.
127 QuicPacketCount packets_dropped_;
128
129 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
130 // because the socket would otherwise overflow.
131 bool overflow_supported_;
132
133 // If true, do not call Shutdown on the dispatcher. Connections will close
134 // without sending a final connection close.
135 bool silent_close_;
136
137 // config_ contains non-crypto parameters that are negotiated in the crypto
138 // handshake.
139 QuicConfig config_;
140 // crypto_config_ contains crypto parameters for the handshake.
141 QuicCryptoServerConfig crypto_config_;
142 // crypto_config_options_ contains crypto parameters for the handshake.
143 QuicCryptoServerConfig::ConfigOptions crypto_config_options_;
144
145 // Used to generate current supported versions.
146 QuicVersionManager version_manager_;
147
148 // Point to a QuicPacketReader object on the heap. The reader allocates more
149 // space than allowed on the stack.
150 std::unique_ptr<QuicPacketReader> packet_reader_;
151
152 QuicSimpleServerBackend* quic_simple_server_backend_; // unowned.
153
154 // Connection ID length expected to be read on incoming IETF short headers.
155 uint8_t expected_connection_id_length_;
156};
157
158} // namespace quic
159
160#endif // QUICHE_QUIC_TOOLS_QUIC_SERVER_H_