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";
42 QuicServer::QuicServer()
46 overflow_supported_(false),
48 crypto_config_(kSourceAddressTokenSecret
, QuicRandom::GetInstance()),
49 supported_versions_(QuicSupportedVersions()) {
53 QuicServer::QuicServer(const QuicConfig
& config
,
54 const QuicVersionVector
& supported_versions
)
58 overflow_supported_(false),
61 crypto_config_(kSourceAddressTokenSecret
, QuicRandom::GetInstance()),
62 supported_versions_(supported_versions
) {
66 void QuicServer::Initialize() {
70 epoll_server_
.set_timeout_in_us(50 * 1000);
71 // Initialize the in memory cache now.
72 QuicInMemoryCache::GetInstance();
74 QuicEpollClock
clock(&epoll_server_
);
76 scoped_ptr
<CryptoHandshakeMessage
> scfg(
77 crypto_config_
.AddDefaultConfig(
78 QuicRandom::GetInstance(), &clock
,
79 QuicCryptoServerConfig::ConfigOptions()));
82 QuicServer::~QuicServer() {
85 bool QuicServer::Listen(const IPEndPoint
& address
) {
86 port_
= address
.port();
87 int address_family
= address
.GetSockAddrFamily();
88 fd_
= socket(address_family
, SOCK_DGRAM
| SOCK_NONBLOCK
, IPPROTO_UDP
);
90 LOG(ERROR
) << "CreateSocket() failed: " << strerror(errno
);
94 // Enable the socket option that allows the local address to be
95 // returned if the socket is bound to more than one address.
96 int rc
= QuicSocketUtils::SetGetAddressInfo(fd_
, address_family
);
99 LOG(ERROR
) << "IP detection not supported" << strerror(errno
);
103 int get_overflow
= 1;
105 fd_
, SOL_SOCKET
, SO_RXQ_OVFL
, &get_overflow
, sizeof(get_overflow
));
108 DLOG(WARNING
) << "Socket overflow detection not supported";
110 overflow_supported_
= true;
113 // These send and receive buffer sizes are sized for a single connection,
114 // because the default usage of QuicServer is as a test server with one or
115 // two clients. Adjust higher for use with many clients.
116 if (!QuicSocketUtils::SetReceiveBufferSize(fd_
,
117 TcpReceiver::kReceiveWindowTCP
)) {
121 if (!QuicSocketUtils::SetSendBufferSize(fd_
,
122 TcpReceiver::kReceiveWindowTCP
)) {
126 sockaddr_storage raw_addr
;
127 socklen_t raw_addr_len
= sizeof(raw_addr
);
128 CHECK(address
.ToSockAddr(reinterpret_cast<sockaddr
*>(&raw_addr
),
131 reinterpret_cast<const sockaddr
*>(&raw_addr
),
134 LOG(ERROR
) << "Bind failed: " << strerror(errno
);
138 DVLOG(1) << "Listening on " << address
.ToString();
140 SockaddrStorage storage
;
141 IPEndPoint server_address
;
142 if (getsockname(fd_
, storage
.addr
, &storage
.addr_len
) != 0 ||
143 !server_address
.FromSockAddr(storage
.addr
, storage
.addr_len
)) {
144 LOG(ERROR
) << "Unable to get self address. Error: " << strerror(errno
);
147 port_
= server_address
.port();
148 DVLOG(1) << "Kernel assigned port is " << port_
;
151 epoll_server_
.RegisterFD(fd_
, this, kEpollFlags
);
152 dispatcher_
.reset(CreateQuicDispatcher());
153 dispatcher_
->Initialize(fd_
);
158 QuicDispatcher
* QuicServer::CreateQuicDispatcher() {
159 return new QuicDispatcher(
163 new QuicDispatcher::DefaultPacketWriterFactory(),
167 void QuicServer::WaitForEvents() {
168 epoll_server_
.WaitForEventsAndExecuteCallbacks();
171 void QuicServer::Shutdown() {
172 // Before we shut down the epoll server, give all active sessions a chance to
173 // notify clients that they're closing.
174 dispatcher_
->Shutdown();
180 void QuicServer::OnEvent(int fd
, EpollEvent
* event
) {
182 event
->out_ready_mask
= 0;
184 if (event
->in_events
& EPOLLIN
) {
185 DVLOG(1) << "EPOLLIN";
188 read
= ReadAndDispatchSinglePacket(
189 fd_
, port_
, dispatcher_
.get(),
190 overflow_supported_
? &packets_dropped_
: nullptr);
193 if (event
->in_events
& EPOLLOUT
) {
194 dispatcher_
->OnCanWrite();
195 if (dispatcher_
->HasPendingWrites()) {
196 event
->out_ready_mask
|= EPOLLOUT
;
199 if (event
->in_events
& EPOLLERR
) {
204 bool QuicServer::ReadAndDispatchSinglePacket(int fd
,
206 ProcessPacketInterface
* processor
,
207 QuicPacketCount
* packets_dropped
) {
208 // Allocate some extra space so we can send an error if the client goes over
210 char buf
[2 * kMaxPacketSize
];
212 IPEndPoint client_address
;
213 IPAddressNumber server_ip
;
215 QuicSocketUtils::ReadPacket(fd
, buf
, arraysize(buf
),
217 &server_ip
, &client_address
);
219 if (bytes_read
< 0) {
220 return false; // We failed to read.
223 QuicEncryptedPacket
packet(buf
, bytes_read
, false);
225 IPEndPoint
server_address(server_ip
, port
);
226 processor
->ProcessPacket(server_address
, client_address
, packet
);