Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / quic_time_wait_list_manager.h
blob0fff1588e3fb9b9b1423924ddc004b0ce935e89f
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.
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_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
10 #define NET_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_
12 #include <deque>
14 #include "base/basictypes.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/strings/string_piece.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_framer.h"
21 #include "net/quic/quic_packet_writer.h"
22 #include "net/quic/quic_protocol.h"
24 namespace net {
26 class ConnectionIdCleanUpAlarm;
27 class QuicServerSessionVisitor;
29 namespace test {
30 class QuicTimeWaitListManagerPeer;
31 } // namespace test
33 // Maintains a list of all connection_ids that have been recently closed. A
34 // connection_id lives in this state for kTimeWaitPeriod. All packets received
35 // for connection_ids in this state are handed over to the
36 // QuicTimeWaitListManager by the QuicDispatcher. Decides whether to send a
37 // public reset packet, a copy of the previously sent connection close packet,
38 // or nothing to the client which sent a packet with the connection_id in time
39 // wait state. After the connection_id expires its time wait period, a new
40 // connection/session will be created if a packet is received for this
41 // connection_id.
42 class QuicTimeWaitListManager : public QuicBlockedWriterInterface {
43 public:
44 // writer - the entity that writes to the socket. (Owned by the dispatcher)
45 // visitor - the entity that manages blocked writers. (The dispatcher)
46 // helper - used to run clean up alarms. (Owned by the owner of the server)
47 QuicTimeWaitListManager(QuicPacketWriter* writer,
48 QuicServerSessionVisitor* visitor,
49 QuicConnectionHelperInterface* helper,
50 const QuicVersionVector& supported_versions);
51 ~QuicTimeWaitListManager() override;
53 // Adds the given connection_id to time wait state for kTimeWaitPeriod.
54 // Henceforth, any packet bearing this connection_id should not be processed
55 // while the connection_id remains in this list. If a non-nullptr
56 // |close_packet| is provided, it is sent again when packets are received for
57 // added connection_ids. If nullptr, a public reset packet is sent with the
58 // specified |version|. DCHECKs that connection_id is not already on the list.
59 void AddConnectionIdToTimeWait(QuicConnectionId connection_id,
60 QuicVersion version,
61 QuicEncryptedPacket* close_packet); // Owned.
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 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 // Used to schedule alarms to delete old connection_ids which have been in the
168 // list for too long.
169 QuicConnectionHelperInterface* helper_;
171 // Time period for which connection_ids should remain in time wait state.
172 const QuicTime::Delta kTimeWaitPeriod_;
174 // Alarm registered with the connection helper to clean up connection_ids that
175 // have
176 // out lived their duration in time wait state.
177 scoped_ptr<QuicAlarm> connection_id_clean_up_alarm_;
179 // Interface that writes given buffer to the socket.
180 QuicPacketWriter* writer_;
182 // Interface that manages blocked writers.
183 QuicServerSessionVisitor* visitor_;
185 DISALLOW_COPY_AND_ASSIGN(QuicTimeWaitListManager);
188 } // namespace net
190 #endif // NET_QUIC_QUIC_TIME_WAIT_LIST_MANAGER_H_