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 // A server side dispatcher which dispatches a given client's data to their
8 #ifndef NET_QUIC_QUIC_DISPATCHER_H_
9 #define NET_QUIC_QUIC_DISPATCHER_H_
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/base/linked_hash_map.h"
18 #include "net/quic/quic_blocked_writer_interface.h"
19 #include "net/quic/quic_connection_helper.h"
20 #include "net/quic/quic_protocol.h"
21 #include "net/quic/quic_server_packet_writer.h"
22 #include "net/tools/quic/quic_server_session.h"
23 #include "net/tools/quic/quic_time_wait_list_manager.h"
27 class QuicDispatcherPeer
;
30 class DeleteSessionsAlarm
;
32 class QuicCryptoServerConfig
;
34 class ProcessPacketInterface
{
36 virtual ~ProcessPacketInterface() {}
37 virtual void ProcessPacket(const IPEndPoint
& server_address
,
38 const IPEndPoint
& client_address
,
39 const QuicEncryptedPacket
& packet
) = 0;
42 class QuicDispatcher
: public QuicBlockedWriterInterface
,
43 public tools::QuicServerSessionVisitor
,
44 public ProcessPacketInterface
{
46 // Creates per-connection packet writers out of the QuicDispatcher's shared
47 // QuicPacketWriter. The per-connection writers' IsWriteBlocked() state must
48 // always be the same as the shared writer's IsWriteBlocked(), or else the
49 // QuicDispatcher::OnCanWrite logic will not work. (This will hopefully be
50 // cleaned up for bug 16950226.)
51 class PacketWriterFactory
{
53 virtual ~PacketWriterFactory() {}
55 virtual QuicPacketWriter
* Create(QuicServerPacketWriter
* writer
,
56 QuicConnection
* connection
) = 0;
59 // Creates ordinary QuicPerConnectionPacketWriter instances.
60 class DefaultPacketWriterFactory
: public PacketWriterFactory
{
62 ~DefaultPacketWriterFactory() override
{}
64 QuicPacketWriter
* Create(QuicServerPacketWriter
* writer
,
65 QuicConnection
* connection
) override
;
68 // Ideally we'd have a linked_hash_set: the boolean is unused.
69 typedef linked_hash_map
<QuicBlockedWriterInterface
*, bool> WriteBlockedList
;
71 // Due to the way delete_sessions_closure_ is registered, the Dispatcher must
72 // live until epoll_server Shutdown. |supported_versions| specifies the list
73 // of supported QUIC versions. Takes ownership of |packet_writer_factory|,
74 // which is used to create per-connection writers.
75 QuicDispatcher(const QuicConfig
& config
,
76 const QuicCryptoServerConfig
& crypto_config
,
77 const QuicVersionVector
& supported_versions
,
78 PacketWriterFactory
* packet_writer_factory
,
79 QuicConnectionHelperInterface
* helper
);
81 ~QuicDispatcher() override
;
83 // Takes ownership of the packet writer.
84 virtual void Initialize(QuicServerPacketWriter
* writer
);
86 // Process the incoming packet by creating a new session, passing it to
87 // an existing session, or passing it to the TimeWaitListManager.
88 void ProcessPacket(const IPEndPoint
& server_address
,
89 const IPEndPoint
& client_address
,
90 const QuicEncryptedPacket
& packet
) override
;
92 // Returns true if there's anything in the blocked writer list.
93 virtual bool HasPendingWrites() const;
95 // Sends ConnectionClose frames to all connected clients.
98 // QuicBlockedWriterInterface implementation:
99 // Called when the socket becomes writable to allow queued writes to happen.
100 void OnCanWrite() override
;
102 // QuicServerSessionVisitor interface implementation:
103 // Ensure that the closed connection is cleaned up asynchronously.
104 void OnConnectionClosed(QuicConnectionId connection_id
,
105 QuicErrorCode error
) override
;
107 // Queues the blocked writer for later resumption.
108 void OnWriteBlocked(QuicBlockedWriterInterface
* blocked_writer
) override
;
110 // Called whenever the QuicTimeWaitListManager adds a new connection to the
112 void OnConnectionAddedToTimeWaitList(QuicConnectionId connection_id
) override
;
114 // Called whenever the QuicTimeWaitListManager removes an old connection from
115 // the time-wait list.
116 void OnConnectionRemovedFromTimeWaitList(
117 QuicConnectionId connection_id
) override
;
119 typedef base::hash_map
<QuicConnectionId
,
120 tools::QuicServerSession
*> SessionMap
;
122 // Deletes all sessions on the closed session list and clears the list.
123 void DeleteSessions();
125 const SessionMap
& session_map() const { return session_map_
; }
128 virtual tools::QuicServerSession
* CreateQuicSession(
129 QuicConnectionId connection_id
,
130 const IPEndPoint
& server_address
,
131 const IPEndPoint
& client_address
);
133 // Called by |framer_visitor_| when the public header has been parsed.
134 virtual bool OnUnauthenticatedPublicHeader(
135 const QuicPacketPublicHeader
& header
);
137 // Called by OnUnauthenticatedPublicHeader when the packet is not for a
138 // connection that the dispatcher has a record of, but is not handled by
139 // certain simple processing rules. This method may apply validity checks to
140 // reject stray packets. If the packet appears to be valid, it calls
141 // CreateQuicSession to create a new session for the packet. Returns the
142 // QuicServerSession that was created, or nullptr if the packet failed the
144 virtual tools::QuicServerSession
* AdditionalValidityChecksThenCreateSession(
145 const QuicPacketPublicHeader
& header
,
146 QuicConnectionId connection_id
);
148 // Create and return the time wait list manager for this dispatcher, which
149 // will be owned by the dispatcher as time_wait_list_manager_
150 virtual tools::QuicTimeWaitListManager
* CreateQuicTimeWaitListManager();
152 tools::QuicTimeWaitListManager
* time_wait_list_manager() {
153 return time_wait_list_manager_
.get();
156 const QuicVersionVector
& supported_versions() const {
157 return supported_versions_
;
160 const IPEndPoint
& current_server_address() {
161 return current_server_address_
;
163 const IPEndPoint
& current_client_address() {
164 return current_client_address_
;
166 const QuicEncryptedPacket
& current_packet() {
167 return *current_packet_
;
170 const QuicConfig
& config() const { return config_
; }
172 const QuicCryptoServerConfig
& crypto_config() const { return crypto_config_
; }
174 QuicFramer
* framer() { return &framer_
; }
176 QuicConnectionHelperInterface
* helper() { return helper_
; }
178 QuicServerPacketWriter
* writer() { return writer_
.get(); }
180 const QuicConnection::PacketWriterFactory
& connection_writer_factory() {
181 return connection_writer_factory_
;
185 class QuicFramerVisitor
;
186 friend class net::test::QuicDispatcherPeer
;
188 // An adapter that creates packet writers using the dispatcher's
189 // PacketWriterFactory and shared writer. Essentially, it just curries the
190 // writer argument away from QuicDispatcher::PacketWriterFactory.
191 class PacketWriterFactoryAdapter
:
192 public QuicConnection::PacketWriterFactory
{
194 explicit PacketWriterFactoryAdapter(QuicDispatcher
* dispatcher
);
195 ~PacketWriterFactoryAdapter() override
;
197 QuicPacketWriter
* Create(QuicConnection
* connection
) const override
;
200 QuicDispatcher
* dispatcher_
;
203 // Called by |framer_visitor_| when the private header has been parsed
204 // of a data packet that is destined for the time wait manager.
205 void OnUnauthenticatedHeader(const QuicPacketHeader
& header
);
207 // Removes the session from the session map and write blocked list, and
208 // adds the ConnectionId to the time-wait list.
209 void CleanUpSession(SessionMap::iterator it
);
211 bool HandlePacketForTimeWait(const QuicPacketPublicHeader
& header
);
213 const QuicConfig
& config_
;
215 const QuicCryptoServerConfig
& crypto_config_
;
217 // The list of connections waiting to write.
218 WriteBlockedList write_blocked_list_
;
220 SessionMap session_map_
;
222 // Entity that manages connection_ids in time wait state.
223 scoped_ptr
<tools::QuicTimeWaitListManager
> time_wait_list_manager_
;
225 // The helper used for all connections. Owned by the server.
226 QuicConnectionHelperInterface
* helper_
;
228 // An alarm which deletes closed sessions.
229 scoped_ptr
<QuicAlarm
> delete_sessions_alarm_
;
231 // The list of closed but not-yet-deleted sessions.
232 std::list
<tools::QuicServerSession
*> closed_session_list_
;
234 // The writer to write to the socket with.
235 scoped_ptr
<QuicServerPacketWriter
> writer_
;
237 // Used to create per-connection packet writers, not |writer_| itself.
238 scoped_ptr
<PacketWriterFactory
> packet_writer_factory_
;
240 // Passed in to QuicConnection for it to create the per-connection writers
241 PacketWriterFactoryAdapter connection_writer_factory_
;
243 // This vector contains QUIC versions which we currently support.
244 // This should be ordered such that the highest supported version is the first
245 // element, with subsequent elements in descending order (versions can be
246 // skipped as necessary).
247 const QuicVersionVector supported_versions_
;
249 // Information about the packet currently being handled.
250 IPEndPoint current_client_address_
;
251 IPEndPoint current_server_address_
;
252 const QuicEncryptedPacket
* current_packet_
;
255 scoped_ptr
<QuicFramerVisitor
> framer_visitor_
;
257 DISALLOW_COPY_AND_ASSIGN(QuicDispatcher
);
262 #endif // NET_QUIC_QUIC_DISPATCHER_H_