Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_dispatcher.cc
blobae964edc8df878d2a30a53adae57d71d5946959f
1 // Copyright 2014 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/quic/quic_dispatcher.h"
7 #include <errno.h>
9 #include "base/debug/stack_trace.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "net/quic/quic_blocked_writer_interface.h"
13 #include "net/quic/quic_connection_helper.h"
14 #include "net/quic/quic_flags.h"
15 #include "net/quic/quic_time_wait_list_manager.h"
16 #include "net/quic/quic_utils.h"
18 namespace net {
20 using base::StringPiece;
21 using std::make_pair;
22 using std::find;
24 class DeleteSessionsAlarm : public QuicAlarm::Delegate {
25 public:
26 explicit DeleteSessionsAlarm(QuicDispatcher* dispatcher)
27 : dispatcher_(dispatcher) {
30 virtual QuicTime OnAlarm() OVERRIDE {
31 dispatcher_->DeleteSessions();
32 return QuicTime::Zero();
35 private:
36 QuicDispatcher* dispatcher_;
39 class QuicDispatcher::QuicFramerVisitor : public QuicFramerVisitorInterface {
40 public:
41 explicit QuicFramerVisitor(QuicDispatcher* dispatcher)
42 : dispatcher_(dispatcher),
43 connection_id_(0) {}
45 // QuicFramerVisitorInterface implementation
46 virtual void OnPacket() OVERRIDE {}
47 virtual bool OnUnauthenticatedPublicHeader(
48 const QuicPacketPublicHeader& header) OVERRIDE {
49 connection_id_ = header.connection_id;
50 return dispatcher_->OnUnauthenticatedPublicHeader(header);
52 virtual bool OnUnauthenticatedHeader(
53 const QuicPacketHeader& header) OVERRIDE {
54 dispatcher_->OnUnauthenticatedHeader(header);
55 return false;
57 virtual void OnError(QuicFramer* framer) OVERRIDE {
58 DVLOG(1) << QuicUtils::ErrorToString(framer->error());
61 virtual bool OnProtocolVersionMismatch(
62 QuicVersion /*received_version*/) OVERRIDE {
63 if (dispatcher_->time_wait_list_manager()->IsConnectionIdInTimeWait(
64 connection_id_)) {
65 // Keep processing after protocol mismatch - this will be dealt with by
66 // the TimeWaitListManager.
67 return true;
68 } else {
69 DLOG(DFATAL) << "Version mismatch, connection ID (" << connection_id_
70 << ") not in time wait list.";
71 return false;
75 // The following methods should never get called because we always return
76 // false from OnUnauthenticatedHeader(). As a result, we never process the
77 // payload of the packet.
78 virtual void OnPublicResetPacket(
79 const QuicPublicResetPacket& /*packet*/) OVERRIDE {
80 DCHECK(false);
82 virtual void OnVersionNegotiationPacket(
83 const QuicVersionNegotiationPacket& /*packet*/) OVERRIDE {
84 DCHECK(false);
86 virtual void OnDecryptedPacket(EncryptionLevel level) OVERRIDE {
87 DCHECK(false);
89 virtual bool OnPacketHeader(const QuicPacketHeader& /*header*/) OVERRIDE {
90 DCHECK(false);
91 return false;
93 virtual void OnRevivedPacket() OVERRIDE {
94 DCHECK(false);
96 virtual void OnFecProtectedPayload(StringPiece /*payload*/) OVERRIDE {
97 DCHECK(false);
99 virtual bool OnStreamFrame(const QuicStreamFrame& /*frame*/) OVERRIDE {
100 DCHECK(false);
101 return false;
103 virtual bool OnAckFrame(const QuicAckFrame& /*frame*/) OVERRIDE {
104 DCHECK(false);
105 return false;
107 virtual bool OnCongestionFeedbackFrame(
108 const QuicCongestionFeedbackFrame& /*frame*/) OVERRIDE {
109 DCHECK(false);
110 return false;
112 virtual bool OnStopWaitingFrame(
113 const QuicStopWaitingFrame& /*frame*/) OVERRIDE {
114 DCHECK(false);
115 return false;
117 virtual bool OnPingFrame(const QuicPingFrame& /*frame*/) OVERRIDE {
118 DCHECK(false);
119 return false;
121 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& /*frame*/) OVERRIDE {
122 DCHECK(false);
123 return false;
125 virtual bool OnConnectionCloseFrame(
126 const QuicConnectionCloseFrame & /*frame*/) OVERRIDE {
127 DCHECK(false);
128 return false;
130 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& /*frame*/) OVERRIDE {
131 DCHECK(false);
132 return false;
134 virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& /*frame*/)
135 OVERRIDE {
136 DCHECK(false);
137 return false;
139 virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) OVERRIDE {
140 DCHECK(false);
141 return false;
143 virtual void OnFecData(const QuicFecData& /*fec*/) OVERRIDE {
144 DCHECK(false);
146 virtual void OnPacketComplete() OVERRIDE {
147 DCHECK(false);
150 private:
151 QuicDispatcher* dispatcher_;
153 // Latched in OnUnauthenticatedPublicHeader for use later.
154 QuicConnectionId connection_id_;
157 QuicDispatcher::QuicDispatcher(const QuicConfig& config,
158 const QuicCryptoServerConfig& crypto_config,
159 const QuicVersionVector& supported_versions,
160 QuicConnectionHelperInterface* helper)
161 : config_(config),
162 crypto_config_(crypto_config),
163 helper_(helper),
164 delete_sessions_alarm_(
165 helper_->CreateAlarm(new DeleteSessionsAlarm(this))),
166 supported_versions_(supported_versions),
167 supported_versions_no_connection_flow_control_(supported_versions),
168 current_packet_(NULL),
169 framer_(supported_versions, /*unused*/ QuicTime::Zero(), true),
170 framer_visitor_(new QuicFramerVisitor(this)) {
171 framer_.set_visitor(framer_visitor_.get());
174 QuicDispatcher::~QuicDispatcher() {
175 STLDeleteValues(&session_map_);
176 STLDeleteElements(&closed_session_list_);
179 void QuicDispatcher::Initialize(QuicServerPacketWriter* writer) {
180 DCHECK(writer_ == NULL);
181 writer_.reset(writer);
182 time_wait_list_manager_.reset(CreateQuicTimeWaitListManager());
184 // Remove all versions > QUIC_VERSION_18 from the
185 // supported_versions_no_connection_flow_control_ vector.
186 QuicVersionVector::iterator connection_it =
187 find(supported_versions_no_connection_flow_control_.begin(),
188 supported_versions_no_connection_flow_control_.end(),
189 QUIC_VERSION_19);
190 if (connection_it != supported_versions_no_connection_flow_control_.end()) {
191 supported_versions_no_connection_flow_control_.erase(
192 supported_versions_no_connection_flow_control_.begin(),
193 connection_it + 1);
195 CHECK(!supported_versions_no_connection_flow_control_.empty());
198 void QuicDispatcher::ProcessPacket(const IPEndPoint& server_address,
199 const IPEndPoint& client_address,
200 const QuicEncryptedPacket& packet) {
201 current_server_address_ = server_address;
202 current_client_address_ = client_address;
203 current_packet_ = &packet;
204 // ProcessPacket will cause the packet to be dispatched in
205 // OnUnauthenticatedPublicHeader, or sent to the time wait list manager
206 // in OnAuthenticatedHeader.
207 framer_.ProcessPacket(packet);
208 // TODO(rjshade): Return a status describing if/why a packet was dropped,
209 // and log somehow. Maybe expose as a varz.
212 bool QuicDispatcher::OnUnauthenticatedPublicHeader(
213 const QuicPacketPublicHeader& header) {
214 QuicSession* session = NULL;
216 QuicConnectionId connection_id = header.connection_id;
217 SessionMap::iterator it = session_map_.find(connection_id);
218 if (it == session_map_.end()) {
219 if (header.reset_flag) {
220 return false;
222 if (time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)) {
223 return HandlePacketForTimeWait(header);
226 // Ensure the packet has a version negotiation bit set before creating a new
227 // session for it. All initial packets for a new connection are required to
228 // have the flag set. Otherwise it may be a stray packet.
229 if (header.version_flag) {
230 session = CreateQuicSession(connection_id, current_server_address_,
231 current_client_address_);
234 if (session == NULL) {
235 DVLOG(1) << "Failed to create session for " << connection_id;
236 // Add this connection_id fo the time-wait state, to safely reject future
237 // packets.
239 if (header.version_flag &&
240 !framer_.IsSupportedVersion(header.versions.front())) {
241 // TODO(ianswett): Produce a no-version version negotiation packet.
242 return false;
245 // Use the version in the packet if possible, otherwise assume the latest.
246 QuicVersion version = header.version_flag ? header.versions.front() :
247 supported_versions_.front();
248 time_wait_list_manager_->AddConnectionIdToTimeWait(
249 connection_id, version, NULL);
250 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id));
251 return HandlePacketForTimeWait(header);
253 DVLOG(1) << "Created new session for " << connection_id;
254 session_map_.insert(make_pair(connection_id, session));
255 } else {
256 session = it->second;
259 session->connection()->ProcessUdpPacket(
260 current_server_address_, current_client_address_, *current_packet_);
262 // Do not parse the packet further. The session will process it completely.
263 return false;
266 void QuicDispatcher::OnUnauthenticatedHeader(const QuicPacketHeader& header) {
267 DCHECK(time_wait_list_manager_->IsConnectionIdInTimeWait(
268 header.public_header.connection_id));
269 time_wait_list_manager_->ProcessPacket(current_server_address_,
270 current_client_address_,
271 header.public_header.connection_id,
272 header.packet_sequence_number,
273 *current_packet_);
276 void QuicDispatcher::CleanUpSession(SessionMap::iterator it) {
277 QuicConnection* connection = it->second->connection();
278 QuicEncryptedPacket* connection_close_packet =
279 connection->ReleaseConnectionClosePacket();
280 write_blocked_list_.erase(connection);
281 time_wait_list_manager_->AddConnectionIdToTimeWait(it->first,
282 connection->version(),
283 connection_close_packet);
284 session_map_.erase(it);
287 void QuicDispatcher::DeleteSessions() {
288 STLDeleteElements(&closed_session_list_);
291 void QuicDispatcher::OnCanWrite() {
292 // We finished a write: the socket should not be blocked.
293 writer_->SetWritable();
295 // Give all the blocked writers one chance to write, until we're blocked again
296 // or there's no work left.
297 while (!write_blocked_list_.empty() && !writer_->IsWriteBlocked()) {
298 QuicBlockedWriterInterface* blocked_writer =
299 write_blocked_list_.begin()->first;
300 write_blocked_list_.erase(write_blocked_list_.begin());
301 blocked_writer->OnCanWrite();
305 bool QuicDispatcher::HasPendingWrites() const {
306 return !write_blocked_list_.empty();
309 void QuicDispatcher::Shutdown() {
310 while (!session_map_.empty()) {
311 QuicSession* session = session_map_.begin()->second;
312 session->connection()->SendConnectionClose(QUIC_PEER_GOING_AWAY);
313 // Validate that the session removes itself from the session map on close.
314 DCHECK(session_map_.empty() || session_map_.begin()->second != session);
316 DeleteSessions();
319 void QuicDispatcher::OnConnectionClosed(QuicConnectionId connection_id,
320 QuicErrorCode error) {
321 SessionMap::iterator it = session_map_.find(connection_id);
322 if (it == session_map_.end()) {
323 LOG(DFATAL) << "ConnectionId " << connection_id
324 << " does not exist in the session map. "
325 << "Error: " << QuicUtils::ErrorToString(error);
326 LOG(DFATAL) << base::debug::StackTrace().ToString();
327 return;
329 DVLOG_IF(1, error != QUIC_NO_ERROR) << "Closing connection ("
330 << connection_id
331 << ") due to error: "
332 << QuicUtils::ErrorToString(error);
333 if (closed_session_list_.empty()) {
334 delete_sessions_alarm_->Set(helper_->GetClock()->ApproximateNow());
336 closed_session_list_.push_back(it->second);
337 CleanUpSession(it);
340 void QuicDispatcher::OnWriteBlocked(
341 QuicBlockedWriterInterface* blocked_writer) {
342 if (!writer_->IsWriteBlocked()) {
343 LOG(DFATAL) <<
344 "QuicDispatcher::OnWriteBlocked called when the writer is not blocked.";
345 // Return without adding the connection to the blocked list, to avoid
346 // infinite loops in OnCanWrite.
347 return;
349 write_blocked_list_.insert(make_pair(blocked_writer, true));
352 QuicSession* QuicDispatcher::CreateQuicSession(
353 QuicConnectionId connection_id,
354 const IPEndPoint& server_address,
355 const IPEndPoint& client_address) {
356 QuicPerConnectionPacketWriter* per_connection_packet_writer =
357 new QuicPerConnectionPacketWriter(writer_.get());
358 QuicConnection* connection =
359 CreateQuicConnection(connection_id,
360 server_address,
361 client_address,
362 per_connection_packet_writer);
363 QuicServerSession* session = new QuicServerSession(
364 config_,
365 connection,
366 per_connection_packet_writer,
367 this);
368 session->InitializeSession(crypto_config_);
369 return session;
372 QuicConnection* QuicDispatcher::CreateQuicConnection(
373 QuicConnectionId connection_id,
374 const IPEndPoint& server_address,
375 const IPEndPoint& client_address,
376 QuicPerConnectionPacketWriter* writer) {
377 QuicConnection* connection;
378 if (FLAGS_enable_quic_connection_flow_control_2) {
379 DVLOG(1) << "Creating QuicDispatcher with all versions.";
380 connection = new QuicConnection(connection_id, client_address, helper_,
381 writer, true, supported_versions_);
382 } else {
383 DVLOG(1) << "Connection flow control disabled, creating QuicDispatcher "
384 << "WITHOUT version 19 or higher.";
385 connection = new QuicConnection(
386 connection_id, client_address, helper_, writer, true,
387 supported_versions_no_connection_flow_control_);
389 writer->set_connection(connection);
390 return connection;
393 QuicTimeWaitListManager* QuicDispatcher::CreateQuicTimeWaitListManager() {
394 return new QuicTimeWaitListManager(
395 writer_.get(), this, helper_, supported_versions());
398 bool QuicDispatcher::HandlePacketForTimeWait(
399 const QuicPacketPublicHeader& header) {
400 if (header.reset_flag) {
401 // Public reset packets do not have sequence numbers, so ignore the packet.
402 return false;
405 // Switch the framer to the correct version, so that the sequence number can
406 // be parsed correctly.
407 framer_.set_version(time_wait_list_manager_->GetQuicVersionFromConnectionId(
408 header.connection_id));
410 // Continue parsing the packet to extract the sequence number. Then
411 // send it to the time wait manager in OnUnathenticatedHeader.
412 return true;
415 } // namespace net