OLD | NEW |
| (Empty) |
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 #ifndef NET_QUIC_QUIC_PROTOCOL_H_ | |
6 #define NET_QUIC_QUIC_PROTOCOL_H_ | |
7 | |
8 #include <limits> | |
9 #include <list> | |
10 #include <memory> | |
11 #include <ostream> | |
12 #include <string> | |
13 #include <utility> | |
14 #include <vector> | |
15 | |
16 #include "base/logging.h" | |
17 #include "base/macros.h" | |
18 #include "base/memory/ref_counted.h" | |
19 #include "base/strings/string_piece.h" | |
20 #include "net/base/int128.h" | |
21 #include "net/base/iovec.h" | |
22 #include "net/base/net_export.h" | |
23 #include "net/quic/core/quic_bandwidth.h" | |
24 #include "net/quic/core/quic_constants.h" | |
25 #include "net/quic/core/quic_error_codes.h" | |
26 #include "net/quic/core/quic_frames.h" | |
27 #include "net/quic/core/quic_time.h" | |
28 #include "net/quic/core/quic_types.h" | |
29 #include "net/quic/core/quic_versions.h" | |
30 #include "net/quic/platform/api/quic_socket_address.h" | |
31 | |
32 namespace net { | |
33 | |
34 class QuicPacket; | |
35 struct QuicPacketHeader; | |
36 | |
37 // Size in bytes of the data packet header. | |
38 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicVersion version, | |
39 const QuicPacketHeader& header); | |
40 | |
41 NET_EXPORT_PRIVATE size_t | |
42 GetPacketHeaderSize(QuicVersion version, | |
43 QuicConnectionIdLength connection_id_length, | |
44 bool include_version, | |
45 bool include_path_id, | |
46 bool include_diversification_nonce, | |
47 QuicPacketNumberLength packet_number_length); | |
48 | |
49 // Index of the first byte in a QUIC packet of encrypted data. | |
50 NET_EXPORT_PRIVATE size_t | |
51 GetStartOfEncryptedData(QuicVersion version, const QuicPacketHeader& header); | |
52 | |
53 NET_EXPORT_PRIVATE size_t | |
54 GetStartOfEncryptedData(QuicVersion version, | |
55 QuicConnectionIdLength connection_id_length, | |
56 bool include_version, | |
57 bool include_path_id, | |
58 bool include_diversification_nonce, | |
59 QuicPacketNumberLength packet_number_length); | |
60 | |
61 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader { | |
62 QuicPacketPublicHeader(); | |
63 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other); | |
64 ~QuicPacketPublicHeader(); | |
65 | |
66 // Universal header. All QuicPacket headers will have a connection_id and | |
67 // public flags. | |
68 QuicConnectionId connection_id; | |
69 QuicConnectionIdLength connection_id_length; | |
70 bool multipath_flag; | |
71 bool reset_flag; | |
72 bool version_flag; | |
73 QuicPacketNumberLength packet_number_length; | |
74 QuicVersionVector versions; | |
75 // nonce contains an optional, 32-byte nonce value. If not included in the | |
76 // packet, |nonce| will be empty. | |
77 DiversificationNonce* nonce; | |
78 }; | |
79 | |
80 // Header for Data packets. | |
81 struct NET_EXPORT_PRIVATE QuicPacketHeader { | |
82 QuicPacketHeader(); | |
83 explicit QuicPacketHeader(const QuicPacketPublicHeader& header); | |
84 QuicPacketHeader(const QuicPacketHeader& other); | |
85 | |
86 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
87 const QuicPacketHeader& s); | |
88 | |
89 QuicPacketPublicHeader public_header; | |
90 QuicPacketNumber packet_number; | |
91 QuicPathId path_id; | |
92 }; | |
93 | |
94 struct NET_EXPORT_PRIVATE QuicPublicResetPacket { | |
95 QuicPublicResetPacket(); | |
96 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header); | |
97 | |
98 QuicPacketPublicHeader public_header; | |
99 QuicPublicResetNonceProof nonce_proof; | |
100 // TODO(fayang): remove rejected_packet_number when deprecating | |
101 // FLAGS_quic_remove_packet_number_from_public_reset. | |
102 QuicPacketNumber rejected_packet_number; | |
103 QuicSocketAddress client_address; | |
104 }; | |
105 | |
106 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket; | |
107 | |
108 typedef std::vector<std::pair<QuicPacketNumber, QuicTime>> PacketTimeVector; | |
109 | |
110 class NET_EXPORT_PRIVATE QuicData { | |
111 public: | |
112 QuicData(const char* buffer, size_t length); | |
113 QuicData(const char* buffer, size_t length, bool owns_buffer); | |
114 virtual ~QuicData(); | |
115 | |
116 base::StringPiece AsStringPiece() const { | |
117 return base::StringPiece(data(), length()); | |
118 } | |
119 | |
120 const char* data() const { return buffer_; } | |
121 size_t length() const { return length_; } | |
122 bool owns_buffer() const { return owns_buffer_; } | |
123 | |
124 private: | |
125 const char* buffer_; | |
126 size_t length_; | |
127 bool owns_buffer_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(QuicData); | |
130 }; | |
131 | |
132 class NET_EXPORT_PRIVATE QuicPacket : public QuicData { | |
133 public: | |
134 // TODO(fayang): 4 fields from public header are passed in as arguments. | |
135 // Consider to add a convenience method which directly accepts the entire | |
136 // public header. | |
137 QuicPacket(char* buffer, | |
138 size_t length, | |
139 bool owns_buffer, | |
140 QuicConnectionIdLength connection_id_length, | |
141 bool includes_version, | |
142 bool includes_path_id, | |
143 bool includes_diversification_nonce, | |
144 QuicPacketNumberLength packet_number_length); | |
145 | |
146 base::StringPiece AssociatedData(QuicVersion version) const; | |
147 base::StringPiece Plaintext(QuicVersion version) const; | |
148 | |
149 char* mutable_data() { return buffer_; } | |
150 | |
151 private: | |
152 char* buffer_; | |
153 const QuicConnectionIdLength connection_id_length_; | |
154 const bool includes_version_; | |
155 const bool includes_path_id_; | |
156 const bool includes_diversification_nonce_; | |
157 const QuicPacketNumberLength packet_number_length_; | |
158 | |
159 DISALLOW_COPY_AND_ASSIGN(QuicPacket); | |
160 }; | |
161 | |
162 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { | |
163 public: | |
164 QuicEncryptedPacket(const char* buffer, size_t length); | |
165 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer); | |
166 | |
167 // Clones the packet into a new packet which owns the buffer. | |
168 std::unique_ptr<QuicEncryptedPacket> Clone() const; | |
169 | |
170 // By default, gtest prints the raw bytes of an object. The bool data | |
171 // member (in the base class QuicData) causes this object to have padding | |
172 // bytes, which causes the default gtest object printer to read | |
173 // uninitialize memory. So we need to teach gtest how to print this object. | |
174 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
175 std::ostream& os, | |
176 const QuicEncryptedPacket& s); | |
177 | |
178 private: | |
179 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket); | |
180 }; | |
181 | |
182 // A received encrypted QUIC packet, with a recorded time of receipt. | |
183 class NET_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket { | |
184 public: | |
185 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time); | |
186 QuicReceivedPacket(const char* buffer, | |
187 size_t length, | |
188 QuicTime receipt_time, | |
189 bool owns_buffer); | |
190 QuicReceivedPacket(const char* buffer, | |
191 size_t length, | |
192 QuicTime receipt_time, | |
193 bool owns_buffer, | |
194 int ttl, | |
195 bool ttl_valid); | |
196 | |
197 // Clones the packet into a new packet which owns the buffer. | |
198 std::unique_ptr<QuicReceivedPacket> Clone() const; | |
199 | |
200 // Returns the time at which the packet was received. | |
201 QuicTime receipt_time() const { return receipt_time_; } | |
202 | |
203 // This is the TTL of the packet, assuming ttl_vaild_ is true. | |
204 int ttl() const { return ttl_; } | |
205 | |
206 // By default, gtest prints the raw bytes of an object. The bool data | |
207 // member (in the base class QuicData) causes this object to have padding | |
208 // bytes, which causes the default gtest object printer to read | |
209 // uninitialize memory. So we need to teach gtest how to print this object. | |
210 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
211 std::ostream& os, | |
212 const QuicReceivedPacket& s); | |
213 | |
214 private: | |
215 const QuicTime receipt_time_; | |
216 int ttl_; | |
217 | |
218 DISALLOW_COPY_AND_ASSIGN(QuicReceivedPacket); | |
219 }; | |
220 | |
221 // Pure virtual class to listen for packet acknowledgements. | |
222 class NET_EXPORT_PRIVATE QuicAckListenerInterface | |
223 : public base::RefCounted<QuicAckListenerInterface> { | |
224 public: | |
225 QuicAckListenerInterface() {} | |
226 | |
227 // Called when a packet is acked. Called once per packet. | |
228 // |acked_bytes| is the number of data bytes acked. | |
229 virtual void OnPacketAcked(int acked_bytes, | |
230 QuicTime::Delta ack_delay_time) = 0; | |
231 | |
232 // Called when a packet is retransmitted. Called once per packet. | |
233 // |retransmitted_bytes| is the number of data bytes retransmitted. | |
234 virtual void OnPacketRetransmitted(int retransmitted_bytes) = 0; | |
235 | |
236 protected: | |
237 friend class base::RefCounted<QuicAckListenerInterface>; | |
238 | |
239 // Delegates are ref counted. | |
240 virtual ~QuicAckListenerInterface() {} | |
241 }; | |
242 | |
243 struct NET_EXPORT_PRIVATE AckListenerWrapper { | |
244 AckListenerWrapper(QuicAckListenerInterface* listener, | |
245 QuicPacketLength data_length); | |
246 AckListenerWrapper(const AckListenerWrapper& other); | |
247 ~AckListenerWrapper(); | |
248 | |
249 scoped_refptr<QuicAckListenerInterface> ack_listener; | |
250 QuicPacketLength length; | |
251 }; | |
252 | |
253 struct NET_EXPORT_PRIVATE SerializedPacket { | |
254 SerializedPacket(QuicPathId path_id, | |
255 QuicPacketNumber packet_number, | |
256 QuicPacketNumberLength packet_number_length, | |
257 const char* encrypted_buffer, | |
258 QuicPacketLength encrypted_length, | |
259 bool has_ack, | |
260 bool has_stop_waiting); | |
261 SerializedPacket(const SerializedPacket& other); | |
262 ~SerializedPacket(); | |
263 | |
264 // Not owned. | |
265 const char* encrypted_buffer; | |
266 QuicPacketLength encrypted_length; | |
267 QuicFrames retransmittable_frames; | |
268 IsHandshake has_crypto_handshake; | |
269 // -1: full padding to the end of a max-sized packet | |
270 // 0: no padding | |
271 // otherwise: only pad up to num_padding_bytes bytes | |
272 int16_t num_padding_bytes; | |
273 QuicPathId path_id; | |
274 QuicPacketNumber packet_number; | |
275 QuicPacketNumberLength packet_number_length; | |
276 EncryptionLevel encryption_level; | |
277 bool has_ack; | |
278 bool has_stop_waiting; | |
279 TransmissionType transmission_type; | |
280 QuicPathId original_path_id; | |
281 QuicPacketNumber original_packet_number; | |
282 | |
283 // Optional notifiers which will be informed when this packet has been ACKed. | |
284 std::list<AckListenerWrapper> listeners; | |
285 }; | |
286 | |
287 // Deletes and clears all the frames and the packet from serialized packet. | |
288 NET_EXPORT_PRIVATE void ClearSerializedPacket( | |
289 SerializedPacket* serialized_packet); | |
290 | |
291 // Allocates a new char[] of size |packet.encrypted_length| and copies in | |
292 // |packet.encrypted_buffer|. | |
293 NET_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet); | |
294 | |
295 struct NET_EXPORT_PRIVATE TransmissionInfo { | |
296 // Used by STL when assigning into a map. | |
297 TransmissionInfo(); | |
298 | |
299 // Constructs a Transmission with a new all_transmissions set | |
300 // containing |packet_number|. | |
301 TransmissionInfo(EncryptionLevel level, | |
302 QuicPacketNumberLength packet_number_length, | |
303 TransmissionType transmission_type, | |
304 QuicTime sent_time, | |
305 QuicPacketLength bytes_sent, | |
306 bool has_crypto_handshake, | |
307 int num_padding_bytes); | |
308 | |
309 TransmissionInfo(const TransmissionInfo& other); | |
310 | |
311 ~TransmissionInfo(); | |
312 | |
313 QuicFrames retransmittable_frames; | |
314 EncryptionLevel encryption_level; | |
315 QuicPacketNumberLength packet_number_length; | |
316 QuicPacketLength bytes_sent; | |
317 QuicTime sent_time; | |
318 // Reason why this packet was transmitted. | |
319 TransmissionType transmission_type; | |
320 // In flight packets have not been abandoned or lost. | |
321 bool in_flight; | |
322 // True if the packet can never be acked, so it can be removed. Occurs when | |
323 // a packet is never sent, after it is acknowledged once, or if it's a crypto | |
324 // packet we never expect to receive an ack for. | |
325 bool is_unackable; | |
326 // True if the packet contains stream data from the crypto stream. | |
327 bool has_crypto_handshake; | |
328 // Non-zero if the packet needs padding if it's retransmitted. | |
329 int16_t num_padding_bytes; | |
330 // Stores the packet number of the next retransmission of this packet. | |
331 // Zero if the packet has not been retransmitted. | |
332 QuicPacketNumber retransmission; | |
333 // Non-empty if there is a std::listener for this packet. | |
334 std::list<AckListenerWrapper> ack_listeners; | |
335 }; | |
336 | |
337 // Struct to store the pending retransmission information. | |
338 struct PendingRetransmission { | |
339 PendingRetransmission(QuicPathId path_id, | |
340 QuicPacketNumber packet_number, | |
341 TransmissionType transmission_type, | |
342 const QuicFrames& retransmittable_frames, | |
343 bool has_crypto_handshake, | |
344 int num_padding_bytes, | |
345 EncryptionLevel encryption_level, | |
346 QuicPacketNumberLength packet_number_length) | |
347 : packet_number(packet_number), | |
348 retransmittable_frames(retransmittable_frames), | |
349 transmission_type(transmission_type), | |
350 path_id(path_id), | |
351 has_crypto_handshake(has_crypto_handshake), | |
352 num_padding_bytes(num_padding_bytes), | |
353 encryption_level(encryption_level), | |
354 packet_number_length(packet_number_length) {} | |
355 | |
356 QuicPacketNumber packet_number; | |
357 const QuicFrames& retransmittable_frames; | |
358 TransmissionType transmission_type; | |
359 QuicPathId path_id; | |
360 bool has_crypto_handshake; | |
361 int num_padding_bytes; | |
362 EncryptionLevel encryption_level; | |
363 QuicPacketNumberLength packet_number_length; | |
364 }; | |
365 | |
366 } // namespace net | |
367 | |
368 #endif // NET_QUIC_QUIC_PROTOCOL_H_ | |
OLD | NEW |