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.
5 // A toy server, which listens on a specified address for QUIC traffic and
6 // handles incoming responses.
8 #ifndef NET_TOOLS_QUIC_QUIC_SERVER_H_
9 #define NET_TOOLS_QUIC_QUIC_SERVER_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/quic/crypto/quic_crypto_server_config.h"
14 #include "net/quic/quic_config.h"
15 #include "net/quic/quic_framer.h"
16 #include "net/tools/epoll_server/epoll_server.h"
17 #include "net/tools/quic/quic_dispatcher.h"
29 class QuicServer : public EpollCallbackInterface {
32 QuicServer(const QuicConfig& config,
33 const QuicVersionVector& supported_versions);
35 virtual ~QuicServer();
37 // Start listening on the specified address.
38 bool Listen(const IPEndPoint& address);
40 // Wait up to 50ms, and handle any events which occur.
43 // Server deletion is imminent. Start cleaning up the epoll server.
46 // From EpollCallbackInterface
47 virtual void OnRegistration(
48 EpollServer* eps, int fd, int event_mask) OVERRIDE {}
49 virtual void OnModification(int fd, int event_mask) OVERRIDE {}
50 virtual void OnEvent(int fd, EpollEvent* event) OVERRIDE;
51 virtual void OnUnregistration(int fd, bool replaced) OVERRIDE {}
53 // Reads a packet from the given fd, and then passes it off to
54 // the QuicDispatcher. Returns true if a packet is read, false
56 // If packets_dropped is non-null, the socket is configured to track
57 // dropped packets, and some packets are read, it will be set to the number of
59 static bool ReadAndDispatchSinglePacket(int fd, int port,
60 QuicDispatcher* dispatcher,
61 int* packets_dropped);
63 virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {}
65 // Dispatches the given packet only if it looks like a valid QUIC packet.
66 // TODO(rjshade): Return a status describing why a packet was dropped, and log
67 // somehow. Maybe expose as a varz.
68 static void MaybeDispatchPacket(QuicDispatcher* dispatcher,
69 const QuicEncryptedPacket& packet,
70 const IPEndPoint& server_address,
71 const IPEndPoint& client_address);
73 void SetStrikeRegisterNoStartupPeriod() {
74 crypto_config_.set_strike_register_no_startup_period();
77 bool overflow_supported() { return overflow_supported_; }
79 int packets_dropped() { return packets_dropped_; }
81 int port() { return port_; }
84 friend class net::tools::test::QuicServerPeer;
86 // Initialize the internal state of the server.
89 // Accepts data from the framer and demuxes clients to sessions.
90 scoped_ptr<QuicDispatcher> dispatcher_;
91 // Frames incoming packets and hands them to the dispatcher.
92 EpollServer epoll_server_;
94 // The port the server is listening on.
97 // Listening connection. Also used for outbound client communication.
100 // If overflow_supported_ is true this will be the number of packets dropped
101 // during the lifetime of the server. This may overflow if enough packets
103 int packets_dropped_;
105 // True if the kernel supports SO_RXQ_OVFL, the number of packets dropped
106 // because the socket would otherwise overflow.
107 bool overflow_supported_;
109 // If true, use recvmmsg for reading.
112 // config_ contains non-crypto parameters that are negotiated in the crypto
115 // crypto_config_ contains crypto parameters for the handshake.
116 QuicCryptoServerConfig crypto_config_;
118 // This vector contains QUIC versions which we currently support.
119 // This should be ordered such that the highest supported version is the first
120 // element, with subsequent elements in descending order (versions can be
121 // skipped as necessary).
122 QuicVersionVector supported_versions_;
124 DISALLOW_COPY_AND_ASSIGN(QuicServer);
130 #endif // NET_TOOLS_QUIC_QUIC_SERVER_H_