Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / tools / quic / quic_time_wait_list_manager.h
blob8c128698c97bca35635b4cde5217377c6b197af1
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 // Handles packets for connection_ids in time wait state by discarding the
6 // packet and sending the clients a public reset packet with exponential
7 // backoff.
9 #ifndef NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
10 #define NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
12 #include <deque>
14 #include "base/basictypes.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/quic_blocked_writer_interface.h"
17 #include "net/quic/quic_connection.h"
18 #include "net/quic/quic_framer.h"
19 #include "net/quic/quic_packet_writer.h"
20 #include "net/quic/quic_protocol.h"
22 namespace net {
23 namespace tools {
25 class QuicServerSessionVisitor;
27 namespace test {
28 class QuicTimeWaitListManagerPeer;
29 } // namespace test
31 // Maintains a list of all connection_ids that have been recently closed. A
32 // connection_id lives in this state for time_wait_period_. All packets received
33 // for connection_ids in this state are handed over to the
34 // QuicTimeWaitListManager by the QuicDispatcher. Decides whether to send a
35 // public reset packet, a copy of the previously sent connection close packet,
36 // or nothing to the client which sent a packet with the connection_id in time
37 // wait state. After the connection_id expires its time wait period, a new
38 // connection/session will be created if a packet is received for this
39 // connection_id.
40 class QuicTimeWaitListManager : public QuicBlockedWriterInterface {
41 public:
42 // writer - the entity that writes to the socket. (Owned by the dispatcher)
43 // visitor - the entity that manages blocked writers. (The dispatcher)
44 // helper - used to run clean up alarms. (Owned by the dispatcher)
45 QuicTimeWaitListManager(QuicPacketWriter* writer,
46 QuicServerSessionVisitor* visitor,
47 QuicConnectionHelperInterface* helper,
48 const QuicVersionVector& supported_versions);
49 ~QuicTimeWaitListManager() override;
51 // Adds the given connection_id to time wait state for time_wait_period_.
52 // Henceforth, any packet bearing this connection_id should not be processed
53 // while the connection_id remains in this list. If a non-nullptr
54 // |close_packet| is provided, the TimeWaitListManager takes ownership of it
55 // and sends it again when packets are received for added connection_ids. If
56 // nullptr, a public reset packet is sent with the specified |version|.
57 // DCHECKs that connection_id is not already on the list. "virtual" to
58 // override in tests.
59 virtual void AddConnectionIdToTimeWait(QuicConnectionId connection_id,
60 QuicVersion version,
61 QuicEncryptedPacket* close_packet);
63 // Returns true if the connection_id is in time wait state, false otherwise.
64 // Packets received for this connection_id should not lead to creation of new
65 // QuicSessions.
66 bool IsConnectionIdInTimeWait(QuicConnectionId connection_id) const;
68 // Called when a packet is received for a connection_id that is in time wait
69 // state. Sends a public reset packet to the client which sent this
70 // connection_id. Sending of the public reset packet is throttled by using
71 // exponential back off. DCHECKs for the connection_id to be in time wait
72 // state. virtual to override in tests.
73 virtual void ProcessPacket(const IPEndPoint& server_address,
74 const IPEndPoint& client_address,
75 QuicConnectionId connection_id,
76 QuicPacketSequenceNumber sequence_number,
77 const QuicEncryptedPacket& packet);
79 // Called by the dispatcher when the underlying socket becomes writable again,
80 // since we might need to send pending public reset packets which we didn't
81 // send because the underlying socket was write blocked.
82 void OnCanWrite() override;
84 // Used to delete connection_id entries that have outlived their time wait
85 // period.
86 void CleanUpOldConnectionIds();
88 // If necessary, trims the oldest connections from the time-wait list until
89 // the size is under the configured maximum.
90 void TrimTimeWaitListIfNeeded();
92 // Given a ConnectionId that exists in the time wait list, returns the
93 // QuicVersion associated with it.
94 QuicVersion GetQuicVersionFromConnectionId(QuicConnectionId connection_id);
96 // The number of connections on the time-wait list.
97 size_t num_connections() const { return connection_id_map_.size(); }
99 protected:
100 virtual QuicEncryptedPacket* BuildPublicReset(
101 const QuicPublicResetPacket& packet);
103 private:
104 friend class test::QuicTimeWaitListManagerPeer;
106 // Internal structure to store pending public reset packets.
107 class QueuedPacket;
109 // Decides if a packet should be sent for this connection_id based on the
110 // number of received packets.
111 bool ShouldSendResponse(int received_packet_count);
113 // Creates a public reset packet and sends it or queues it to be sent later.
114 void SendPublicReset(const IPEndPoint& server_address,
115 const IPEndPoint& client_address,
116 QuicConnectionId connection_id,
117 QuicPacketSequenceNumber rejected_sequence_number);
119 // Either sends the packet and deletes it or makes pending_packets_queue_ the
120 // owner of the packet.
121 void SendOrQueuePacket(QueuedPacket* packet);
123 // Sends the packet out. Returns true if the packet was successfully consumed.
124 // If the writer got blocked and did not buffer the packet, we'll need to keep
125 // the packet and retry sending. In case of all other errors we drop the
126 // packet.
127 bool WriteToWire(QueuedPacket* packet);
129 // Register the alarm server to wake up at appropriate time.
130 void SetConnectionIdCleanUpAlarm();
132 // Removes the oldest connection from the time-wait list if it was added prior
133 // to "expiration_time". To unconditionally remove the oldest connection, use
134 // a QuicTime::Delta:Infinity(). This function modifies the
135 // connection_id_map_. If you plan to call this function in a loop, any
136 // iterators that you hold before the call to this function may be invalid
137 // afterward. Returns true if the oldest connection was expired. Returns
138 // false if the map is empty or the oldest connection has not expired.
139 bool MaybeExpireOldestConnection(QuicTime expiration_time);
141 // A map from a recently closed connection_id to the number of packets
142 // received after the termination of the connection bound to the
143 // connection_id.
144 struct ConnectionIdData {
145 ConnectionIdData(int num_packets_,
146 QuicVersion version_,
147 QuicTime time_added_,
148 QuicEncryptedPacket* close_packet)
149 : num_packets(num_packets_),
150 version(version_),
151 time_added(time_added_),
152 close_packet(close_packet) {}
153 int num_packets;
154 QuicVersion version;
155 QuicTime time_added;
156 QuicEncryptedPacket* close_packet;
159 // linked_hash_map allows lookup by ConnectionId and traversal in add order.
160 typedef linked_hash_map<QuicConnectionId, ConnectionIdData> ConnectionIdMap;
161 ConnectionIdMap connection_id_map_;
163 // Pending public reset packets that need to be sent out to the client
164 // when we are given a chance to write by the dispatcher.
165 std::deque<QueuedPacket*> pending_packets_queue_;
167 // Time period for which connection_ids should remain in time wait state.
168 const QuicTime::Delta time_wait_period_;
170 // Alarm to clean up connection_ids that have out lived their duration in
171 // time wait state.
172 scoped_ptr<QuicAlarm> connection_id_clean_up_alarm_;
174 // Clock to efficiently measure approximate time.
175 const QuicClock* clock_;
177 // Interface that writes given buffer to the socket.
178 QuicPacketWriter* writer_;
180 // Interface that manages blocked writers.
181 QuicServerSessionVisitor* visitor_;
183 DISALLOW_COPY_AND_ASSIGN(QuicTimeWaitListManager);
186 } // namespace tools
187 } // namespace net
189 #endif // NET_TOOLS_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_