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. 4 // 5 // A toy server, which listens on a specified address for QUIC traffic and 6 // handles incoming responses. 7 8 #ifndef NET_TOOLS_QUIC_QUIC_SERVER_H_ 9 #define NET_TOOLS_QUIC_QUIC_SERVER_H_ 10 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" 18 19 namespace net { 20 21 namespace tools { 22 23 namespace test { 24 class QuicServerPeer; 25 } // namespace test 26 27 class QuicDispatcher; 28 29 class QuicServer : public EpollCallbackInterface { 30 public: 31 QuicServer(); 32 QuicServer(const QuicConfig& config, 33 const QuicVersionVector& supported_versions); 34 35 virtual ~QuicServer(); 36 37 // Start listening on the specified address. 38 bool Listen(const IPEndPoint& address); 39 40 // Wait up to 50ms, and handle any events which occur. 41 void WaitForEvents(); 42 43 // Server deletion is imminent. Start cleaning up the epoll server. 44 void Shutdown(); 45 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 {} 52 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 55 // otherwise. 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 58 // dropped packets. 59 static bool ReadAndDispatchSinglePacket(int fd, int port, 60 QuicDispatcher* dispatcher, 61 int* packets_dropped); 62 63 virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {} 64 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); 72 73 void SetStrikeRegisterNoStartupPeriod() { 74 crypto_config_.set_strike_register_no_startup_period(); 75 } 76 77 bool overflow_supported() { return overflow_supported_; } 78 79 int packets_dropped() { return packets_dropped_; } 80 81 int port() { return port_; } 82 83 private: 84 friend class net::tools::test::QuicServerPeer; 85 86 // Initialize the internal state of the server. 87 void Initialize(); 88 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_; 93 94 // The port the server is listening on. 95 int port_; 96 97 // Listening connection. Also used for outbound client communication. 98 int fd_; 99 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 102 // are dropped. 103 int packets_dropped_; 104 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_; 108 109 // If true, use recvmmsg for reading. 110 bool use_recvmmsg_; 111 112 // config_ contains non-crypto parameters that are negotiated in the crypto 113 // handshake. 114 QuicConfig config_; 115 // crypto_config_ contains crypto parameters for the handshake. 116 QuicCryptoServerConfig crypto_config_; 117 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_; 123 124 DISALLOW_COPY_AND_ASSIGN(QuicServer); 125 }; 126 127 } // namespace tools 128 } // namespace net 129 130 #endif // NET_TOOLS_QUIC_QUIC_SERVER_H_ 131