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 #include "net/tools/quic/quic_server.h"
9 #include <netinet/in.h>
11 #include <sys/epoll.h>
12 #include <sys/socket.h>
14 #include "net/base/ip_endpoint.h"
15 #include "net/quic/congestion_control/tcp_receiver.h"
16 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/crypto/quic_random.h"
18 #include "net/quic/quic_clock.h"
19 #include "net/quic/quic_crypto_stream.h"
20 #include "net/quic/quic_data_reader.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/tools/quic/quic_dispatcher.h"
23 #include "net/tools/quic/quic_in_memory_cache.h"
24 #include "net/tools/quic/quic_socket_utils.h"
29 #define SO_RXQ_OVFL 40
37 const int kEpollFlags = EPOLLIN | EPOLLOUT | EPOLLET;
38 const char kSourceAddressTokenSecret[] = "secret";
39 const uint32 kServerInitialFlowControlWindow = 100 * net::kMaxPacketSize;
43 QuicServer::QuicServer()
47 overflow_supported_(false),
49 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()),
50 supported_versions_(QuicSupportedVersions()) {
51 // Use hardcoded crypto parameters for now.
52 config_.SetDefaults();
56 QuicServer::QuicServer(const QuicConfig& config,
57 const QuicVersionVector& supported_versions)
61 overflow_supported_(false),
64 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()),
65 supported_versions_(supported_versions) {
69 void QuicServer::Initialize() {
73 epoll_server_.set_timeout_in_us(50 * 1000);
74 // Initialize the in memory cache now.
75 QuicInMemoryCache::GetInstance();
77 QuicEpollClock clock(&epoll_server_);
79 scoped_ptr<CryptoHandshakeMessage> scfg(
80 crypto_config_.AddDefaultConfig(
81 QuicRandom::GetInstance(), &clock,
82 QuicCryptoServerConfig::ConfigOptions()));
84 // Set flow control options in the config.
85 config_.SetInitialCongestionWindowToSend(kServerInitialFlowControlWindow);
88 QuicServer::~QuicServer() {
91 bool QuicServer::Listen(const IPEndPoint& address) {
92 port_ = address.port();
93 int address_family = address.GetSockAddrFamily();
94 fd_ = socket(address_family, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
96 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno);
100 // Enable the socket option that allows the local address to be
101 // returned if the socket is bound to more than one address.
102 int rc = QuicSocketUtils::SetGetAddressInfo(fd_, address_family);
105 LOG(ERROR) << "IP detection not supported" << strerror(errno);
109 int get_overflow = 1;
111 fd_, SOL_SOCKET, SO_RXQ_OVFL, &get_overflow, sizeof(get_overflow));
114 DLOG(WARNING) << "Socket overflow detection not supported";
116 overflow_supported_ = true;
119 // These send and receive buffer sizes are sized for a single connection,
120 // because the default usage of QuicServer is as a test server with one or
121 // two clients. Adjust higher for use with many clients.
122 if (!QuicSocketUtils::SetReceiveBufferSize(fd_,
123 TcpReceiver::kReceiveWindowTCP)) {
127 if (!QuicSocketUtils::SetSendBufferSize(fd_,
128 TcpReceiver::kReceiveWindowTCP)) {
132 sockaddr_storage raw_addr;
133 socklen_t raw_addr_len = sizeof(raw_addr);
134 CHECK(address.ToSockAddr(reinterpret_cast<sockaddr*>(&raw_addr),
137 reinterpret_cast<const sockaddr*>(&raw_addr),
140 LOG(ERROR) << "Bind failed: " << strerror(errno);
144 DVLOG(1) << "Listening on " << address.ToString();
146 SockaddrStorage storage;
147 IPEndPoint server_address;
148 if (getsockname(fd_, storage.addr, &storage.addr_len) != 0 ||
149 !server_address.FromSockAddr(storage.addr, storage.addr_len)) {
150 LOG(ERROR) << "Unable to get self address. Error: " << strerror(errno);
153 port_ = server_address.port();
154 DVLOG(1) << "Kernel assigned port is " << port_;
157 epoll_server_.RegisterFD(fd_, this, kEpollFlags);
158 dispatcher_.reset(CreateQuicDispatcher());
159 dispatcher_->Initialize(fd_);
164 QuicDispatcher* QuicServer::CreateQuicDispatcher() {
165 return new QuicDispatcher(
169 new QuicDispatcher::DefaultPacketWriterFactory(),
173 void QuicServer::WaitForEvents() {
174 epoll_server_.WaitForEventsAndExecuteCallbacks();
177 void QuicServer::Shutdown() {
178 // Before we shut down the epoll server, give all active sessions a chance to
179 // notify clients that they're closing.
180 dispatcher_->Shutdown();
186 void QuicServer::OnEvent(int fd, EpollEvent* event) {
188 event->out_ready_mask = 0;
190 if (event->in_events & EPOLLIN) {
191 DVLOG(1) << "EPOLLIN";
194 read = ReadAndDispatchSinglePacket(
195 fd_, port_, dispatcher_.get(),
196 overflow_supported_ ? &packets_dropped_ : NULL);
199 if (event->in_events & EPOLLOUT) {
200 dispatcher_->OnCanWrite();
201 if (dispatcher_->HasPendingWrites()) {
202 event->out_ready_mask |= EPOLLOUT;
205 if (event->in_events & EPOLLERR) {
210 bool QuicServer::ReadAndDispatchSinglePacket(int fd,
212 ProcessPacketInterface* processor,
213 uint32* packets_dropped) {
214 // Allocate some extra space so we can send an error if the client goes over
216 char buf[2 * kMaxPacketSize];
218 IPEndPoint client_address;
219 IPAddressNumber server_ip;
221 QuicSocketUtils::ReadPacket(fd, buf, arraysize(buf),
223 &server_ip, &client_address);
225 if (bytes_read < 0) {
226 return false; // We failed to read.
229 QuicEncryptedPacket packet(buf, bytes_read, false);
231 IPEndPoint server_address(server_ip, port);
232 processor->ProcessPacket(server_address, client_address, packet);