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 <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <array> | |
12 #include <limits> | |
13 #include <list> | |
14 #include <map> | |
15 #include <memory> | |
16 #include <ostream> | |
17 #include <set> | |
18 #include <string> | |
19 #include <utility> | |
20 #include <vector> | |
21 | |
22 #include "base/logging.h" | |
23 #include "base/macros.h" | |
24 #include "base/memory/ref_counted.h" | |
25 #include "base/strings/string_piece.h" | |
26 #include "net/base/int128.h" | |
27 #include "net/base/iovec.h" | |
28 #include "net/base/net_export.h" | |
29 #include "net/quic/core/interval_set.h" | |
30 #include "net/quic/core/quic_bandwidth.h" | |
31 #include "net/quic/core/quic_buffer_allocator.h" | |
32 #include "net/quic/core/quic_constants.h" | |
33 #include "net/quic/core/quic_error_codes.h" | |
34 #include "net/quic/core/quic_time.h" | |
35 #include "net/quic/core/quic_types.h" | |
36 #include "net/quic/core/quic_versions.h" | |
37 #include "net/quic/platform/api/quic_socket_address.h" | |
38 | |
39 namespace net { | |
40 | |
41 class QuicPacket; | |
42 struct QuicPacketHeader; | |
43 | |
44 // Size in bytes of the data packet header. | |
45 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicVersion version, | |
46 const QuicPacketHeader& header); | |
47 | |
48 NET_EXPORT_PRIVATE size_t | |
49 GetPacketHeaderSize(QuicVersion version, | |
50 QuicConnectionIdLength connection_id_length, | |
51 bool include_version, | |
52 bool include_path_id, | |
53 bool include_diversification_nonce, | |
54 QuicPacketNumberLength packet_number_length); | |
55 | |
56 // Index of the first byte in a QUIC packet of encrypted data. | |
57 NET_EXPORT_PRIVATE size_t | |
58 GetStartOfEncryptedData(QuicVersion version, const QuicPacketHeader& header); | |
59 | |
60 NET_EXPORT_PRIVATE size_t | |
61 GetStartOfEncryptedData(QuicVersion version, | |
62 QuicConnectionIdLength connection_id_length, | |
63 bool include_version, | |
64 bool include_path_id, | |
65 bool include_diversification_nonce, | |
66 QuicPacketNumberLength packet_number_length); | |
67 | |
68 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader { | |
69 QuicPacketPublicHeader(); | |
70 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other); | |
71 ~QuicPacketPublicHeader(); | |
72 | |
73 // Universal header. All QuicPacket headers will have a connection_id and | |
74 // public flags. | |
75 QuicConnectionId connection_id; | |
76 QuicConnectionIdLength connection_id_length; | |
77 bool multipath_flag; | |
78 bool reset_flag; | |
79 bool version_flag; | |
80 QuicPacketNumberLength packet_number_length; | |
81 QuicVersionVector versions; | |
82 // nonce contains an optional, 32-byte nonce value. If not included in the | |
83 // packet, |nonce| will be empty. | |
84 DiversificationNonce* nonce; | |
85 }; | |
86 | |
87 // Header for Data packets. | |
88 struct NET_EXPORT_PRIVATE QuicPacketHeader { | |
89 QuicPacketHeader(); | |
90 explicit QuicPacketHeader(const QuicPacketPublicHeader& header); | |
91 QuicPacketHeader(const QuicPacketHeader& other); | |
92 | |
93 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
94 const QuicPacketHeader& s); | |
95 | |
96 QuicPacketPublicHeader public_header; | |
97 QuicPacketNumber packet_number; | |
98 QuicPathId path_id; | |
99 }; | |
100 | |
101 struct NET_EXPORT_PRIVATE QuicPublicResetPacket { | |
102 QuicPublicResetPacket(); | |
103 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header); | |
104 | |
105 QuicPacketPublicHeader public_header; | |
106 QuicPublicResetNonceProof nonce_proof; | |
107 // TODO(fayang): remove rejected_packet_number when deprecating | |
108 // FLAGS_quic_remove_packet_number_from_public_reset. | |
109 QuicPacketNumber rejected_packet_number; | |
110 QuicSocketAddress client_address; | |
111 }; | |
112 | |
113 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket; | |
114 | |
115 // A padding frame contains no payload. | |
116 struct NET_EXPORT_PRIVATE QuicPaddingFrame { | |
117 QuicPaddingFrame() : num_padding_bytes(-1) {} | |
118 explicit QuicPaddingFrame(int num_padding_bytes) | |
119 : num_padding_bytes(num_padding_bytes) {} | |
120 | |
121 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
122 const QuicPaddingFrame& s); | |
123 | |
124 // -1: full padding to the end of a max-sized packet | |
125 // otherwise: only pad up to num_padding_bytes bytes | |
126 int num_padding_bytes; | |
127 }; | |
128 | |
129 // A ping frame contains no payload, though it is retransmittable, | |
130 // and ACK'd just like other normal frames. | |
131 struct NET_EXPORT_PRIVATE QuicPingFrame {}; | |
132 | |
133 // A path MTU discovery frame contains no payload and is serialized as a ping | |
134 // frame. | |
135 struct NET_EXPORT_PRIVATE QuicMtuDiscoveryFrame {}; | |
136 | |
137 // Deleter for stream buffers. Copyable to support platforms where the deleter | |
138 // of a unique_ptr must be copyable. Otherwise it would be nice for this to be | |
139 // move-only. | |
140 class NET_EXPORT_PRIVATE StreamBufferDeleter { | |
141 public: | |
142 StreamBufferDeleter() : allocator_(nullptr) {} | |
143 explicit StreamBufferDeleter(QuicBufferAllocator* allocator) | |
144 : allocator_(allocator) {} | |
145 | |
146 // Deletes |buffer| using |allocator_|. | |
147 void operator()(char* buffer) const; | |
148 | |
149 private: | |
150 // Not owned; must be valid so long as the buffer stored in the unique_ptr | |
151 // that owns |this| is valid. | |
152 QuicBufferAllocator* allocator_; | |
153 }; | |
154 | |
155 using UniqueStreamBuffer = std::unique_ptr<char[], StreamBufferDeleter>; | |
156 | |
157 // Allocates memory of size |size| using |allocator| for a QUIC stream buffer. | |
158 NET_EXPORT_PRIVATE UniqueStreamBuffer | |
159 NewStreamBuffer(QuicBufferAllocator* allocator, size_t size); | |
160 | |
161 struct NET_EXPORT_PRIVATE QuicStreamFrame { | |
162 QuicStreamFrame(); | |
163 QuicStreamFrame(QuicStreamId stream_id, | |
164 bool fin, | |
165 QuicStreamOffset offset, | |
166 base::StringPiece data); | |
167 QuicStreamFrame(QuicStreamId stream_id, | |
168 bool fin, | |
169 QuicStreamOffset offset, | |
170 QuicPacketLength data_length, | |
171 UniqueStreamBuffer buffer); | |
172 ~QuicStreamFrame(); | |
173 | |
174 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
175 const QuicStreamFrame& s); | |
176 | |
177 QuicStreamId stream_id; | |
178 bool fin; | |
179 QuicPacketLength data_length; | |
180 const char* data_buffer; | |
181 QuicStreamOffset offset; // Location of this data in the stream. | |
182 // nullptr when the QuicStreamFrame is received, and non-null when sent. | |
183 UniqueStreamBuffer buffer; | |
184 | |
185 private: | |
186 QuicStreamFrame(QuicStreamId stream_id, | |
187 bool fin, | |
188 QuicStreamOffset offset, | |
189 const char* data_buffer, | |
190 QuicPacketLength data_length, | |
191 UniqueStreamBuffer buffer); | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(QuicStreamFrame); | |
194 }; | |
195 static_assert(sizeof(QuicStreamFrame) <= 64, | |
196 "Keep the QuicStreamFrame size to a cacheline."); | |
197 | |
198 typedef std::vector<std::pair<QuicPacketNumber, QuicTime>> PacketTimeVector; | |
199 | |
200 struct NET_EXPORT_PRIVATE QuicStopWaitingFrame { | |
201 QuicStopWaitingFrame(); | |
202 ~QuicStopWaitingFrame(); | |
203 | |
204 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
205 std::ostream& os, | |
206 const QuicStopWaitingFrame& s); | |
207 // Path which this stop waiting frame belongs to. | |
208 QuicPathId path_id; | |
209 // The lowest packet we've sent which is unacked, and we expect an ack for. | |
210 QuicPacketNumber least_unacked; | |
211 }; | |
212 | |
213 // A sequence of packet numbers where each number is unique. Intended to be used | |
214 // in a sliding window fashion, where smaller old packet numbers are removed and | |
215 // larger new packet numbers are added, with the occasional random access. | |
216 class NET_EXPORT_PRIVATE PacketNumberQueue { | |
217 public: | |
218 using const_iterator = IntervalSet<QuicPacketNumber>::const_iterator; | |
219 using const_reverse_iterator = | |
220 IntervalSet<QuicPacketNumber>::const_reverse_iterator; | |
221 | |
222 PacketNumberQueue(); | |
223 PacketNumberQueue(const PacketNumberQueue& other); | |
224 // TODO(rtenneti): on windows RValue reference gives errors. | |
225 // PacketNumberQueue(PacketNumberQueue&& other); | |
226 ~PacketNumberQueue(); | |
227 | |
228 PacketNumberQueue& operator=(const PacketNumberQueue& other); | |
229 // PacketNumberQueue& operator=(PacketNumberQueue&& other); | |
230 | |
231 // Adds |packet_number| to the set of packets in the queue. | |
232 void Add(QuicPacketNumber packet_number); | |
233 | |
234 // Adds packets between [lower, higher) to the set of packets in the queue. It | |
235 // is undefined behavior to call this with |higher| < |lower|. | |
236 void Add(QuicPacketNumber lower, QuicPacketNumber higher); | |
237 | |
238 // Removes |packet_number| from the set of packets in the queue. | |
239 void Remove(QuicPacketNumber packet_number); | |
240 | |
241 // Removes packets numbers between [lower, higher) to the set of packets in | |
242 // the queue. It is undefined behavior to call this with |higher| < |lower|. | |
243 void Remove(QuicPacketNumber lower, QuicPacketNumber higher); | |
244 | |
245 // Removes packets with values less than |higher| from the set of packets in | |
246 // the queue. Returns true if packets were removed. | |
247 bool RemoveUpTo(QuicPacketNumber higher); | |
248 | |
249 // Mutates packet number set so that it contains only those packet numbers | |
250 // from minimum to maximum packet number not currently in the set. Do nothing | |
251 // if packet number set is empty. | |
252 void Complement(); | |
253 | |
254 // Returns true if the queue contains |packet_number|. | |
255 bool Contains(QuicPacketNumber packet_number) const; | |
256 | |
257 // Returns true if the queue is empty. | |
258 bool Empty() const; | |
259 | |
260 // Returns the minimum packet number stored in the queue. It is undefined | |
261 // behavior to call this if the queue is empty. | |
262 QuicPacketNumber Min() const; | |
263 | |
264 // Returns the maximum packet number stored in the queue. It is undefined | |
265 // behavior to call this if the queue is empty. | |
266 QuicPacketNumber Max() const; | |
267 | |
268 // Returns the number of unique packets stored in the queue. Inefficient; only | |
269 // exposed for testing. | |
270 size_t NumPacketsSlow() const; | |
271 | |
272 // Returns the number of disjoint packet number intervals contained in the | |
273 // queue. | |
274 size_t NumIntervals() const; | |
275 | |
276 // Returns the length of last interval. | |
277 QuicPacketNumber LastIntervalLength() const; | |
278 | |
279 // Returns iterators over the packet number intervals. | |
280 const_iterator begin() const; | |
281 const_iterator end() const; | |
282 const_reverse_iterator rbegin() const; | |
283 const_reverse_iterator rend() const; | |
284 const_iterator lower_bound(QuicPacketNumber packet_number) const; | |
285 | |
286 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
287 std::ostream& os, | |
288 const PacketNumberQueue& q); | |
289 | |
290 private: | |
291 IntervalSet<QuicPacketNumber> packet_number_intervals_; | |
292 }; | |
293 | |
294 struct NET_EXPORT_PRIVATE QuicAckFrame { | |
295 QuicAckFrame(); | |
296 QuicAckFrame(const QuicAckFrame& other); | |
297 ~QuicAckFrame(); | |
298 | |
299 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
300 const QuicAckFrame& s); | |
301 | |
302 // The highest packet number we've observed from the peer. | |
303 QuicPacketNumber largest_observed; | |
304 | |
305 // Time elapsed since largest_observed was received until this Ack frame was | |
306 // sent. | |
307 QuicTime::Delta ack_delay_time; | |
308 | |
309 // Vector of <packet_number, time> for when packets arrived. | |
310 PacketTimeVector received_packet_times; | |
311 | |
312 // Set of packets. | |
313 PacketNumberQueue packets; | |
314 | |
315 // Path which this ack belongs to. | |
316 QuicPathId path_id; | |
317 }; | |
318 | |
319 // True if the packet number is greater than largest_observed or is listed | |
320 // as missing. | |
321 // Always returns false for packet numbers less than least_unacked. | |
322 bool NET_EXPORT_PRIVATE | |
323 IsAwaitingPacket(const QuicAckFrame& ack_frame, | |
324 QuicPacketNumber packet_number, | |
325 QuicPacketNumber peer_least_packet_awaiting_ack); | |
326 | |
327 struct NET_EXPORT_PRIVATE QuicRstStreamFrame { | |
328 QuicRstStreamFrame(); | |
329 QuicRstStreamFrame(QuicStreamId stream_id, | |
330 QuicRstStreamErrorCode error_code, | |
331 QuicStreamOffset bytes_written); | |
332 | |
333 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
334 std::ostream& os, | |
335 const QuicRstStreamFrame& r); | |
336 | |
337 QuicStreamId stream_id; | |
338 QuicRstStreamErrorCode error_code; | |
339 | |
340 // Used to update flow control windows. On termination of a stream, both | |
341 // endpoints must inform the peer of the number of bytes they have sent on | |
342 // that stream. This can be done through normal termination (data packet with | |
343 // FIN) or through a RST. | |
344 QuicStreamOffset byte_offset; | |
345 }; | |
346 | |
347 struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame { | |
348 QuicConnectionCloseFrame(); | |
349 | |
350 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
351 std::ostream& os, | |
352 const QuicConnectionCloseFrame& c); | |
353 | |
354 QuicErrorCode error_code; | |
355 std::string error_details; | |
356 }; | |
357 | |
358 struct NET_EXPORT_PRIVATE QuicGoAwayFrame { | |
359 QuicGoAwayFrame(); | |
360 QuicGoAwayFrame(QuicErrorCode error_code, | |
361 QuicStreamId last_good_stream_id, | |
362 const std::string& reason); | |
363 | |
364 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
365 const QuicGoAwayFrame& g); | |
366 | |
367 QuicErrorCode error_code; | |
368 QuicStreamId last_good_stream_id; | |
369 std::string reason_phrase; | |
370 }; | |
371 | |
372 // Flow control updates per-stream and at the connection levoel. | |
373 // Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather | |
374 // than a window delta. | |
375 // TODO(rjshade): A possible future optimization is to make stream_id and | |
376 // byte_offset variable length, similar to stream frames. | |
377 struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame { | |
378 QuicWindowUpdateFrame() {} | |
379 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset); | |
380 | |
381 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
382 std::ostream& os, | |
383 const QuicWindowUpdateFrame& w); | |
384 | |
385 // The stream this frame applies to. 0 is a special case meaning the overall | |
386 // connection rather than a specific stream. | |
387 QuicStreamId stream_id; | |
388 | |
389 // Byte offset in the stream or connection. The receiver of this frame must | |
390 // not send data which would result in this offset being exceeded. | |
391 QuicStreamOffset byte_offset; | |
392 }; | |
393 | |
394 // The BLOCKED frame is used to indicate to the remote endpoint that this | |
395 // endpoint believes itself to be flow-control blocked but otherwise ready to | |
396 // send data. The BLOCKED frame is purely advisory and optional. | |
397 // Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28). | |
398 struct NET_EXPORT_PRIVATE QuicBlockedFrame { | |
399 QuicBlockedFrame() {} | |
400 explicit QuicBlockedFrame(QuicStreamId stream_id); | |
401 | |
402 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
403 const QuicBlockedFrame& b); | |
404 | |
405 // The stream this frame applies to. 0 is a special case meaning the overall | |
406 // connection rather than a specific stream. | |
407 QuicStreamId stream_id; | |
408 }; | |
409 | |
410 // The PATH_CLOSE frame is used to explicitly close a path. Both endpoints can | |
411 // send a PATH_CLOSE frame to initiate a path termination. A path is considered | |
412 // to be closed either a PATH_CLOSE frame is sent or received. An endpoint drops | |
413 // receive side of a closed path, and packets with retransmittable frames on a | |
414 // closed path are marked as retransmissions which will be transmitted on other | |
415 // paths. | |
416 struct NET_EXPORT_PRIVATE QuicPathCloseFrame { | |
417 QuicPathCloseFrame() {} | |
418 explicit QuicPathCloseFrame(QuicPathId path_id); | |
419 | |
420 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
421 std::ostream& os, | |
422 const QuicPathCloseFrame& p); | |
423 | |
424 QuicPathId path_id; | |
425 }; | |
426 | |
427 struct NET_EXPORT_PRIVATE QuicFrame { | |
428 QuicFrame(); | |
429 explicit QuicFrame(QuicPaddingFrame padding_frame); | |
430 explicit QuicFrame(QuicMtuDiscoveryFrame frame); | |
431 explicit QuicFrame(QuicPingFrame frame); | |
432 | |
433 explicit QuicFrame(QuicStreamFrame* stream_frame); | |
434 explicit QuicFrame(QuicAckFrame* frame); | |
435 explicit QuicFrame(QuicRstStreamFrame* frame); | |
436 explicit QuicFrame(QuicConnectionCloseFrame* frame); | |
437 explicit QuicFrame(QuicStopWaitingFrame* frame); | |
438 explicit QuicFrame(QuicGoAwayFrame* frame); | |
439 explicit QuicFrame(QuicWindowUpdateFrame* frame); | |
440 explicit QuicFrame(QuicBlockedFrame* frame); | |
441 explicit QuicFrame(QuicPathCloseFrame* frame); | |
442 | |
443 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
444 const QuicFrame& frame); | |
445 | |
446 QuicFrameType type; | |
447 union { | |
448 // Frames smaller than a pointer are inline. | |
449 QuicPaddingFrame padding_frame; | |
450 QuicMtuDiscoveryFrame mtu_discovery_frame; | |
451 QuicPingFrame ping_frame; | |
452 | |
453 // Frames larger than a pointer. | |
454 QuicStreamFrame* stream_frame; | |
455 QuicAckFrame* ack_frame; | |
456 QuicStopWaitingFrame* stop_waiting_frame; | |
457 QuicRstStreamFrame* rst_stream_frame; | |
458 QuicConnectionCloseFrame* connection_close_frame; | |
459 QuicGoAwayFrame* goaway_frame; | |
460 QuicWindowUpdateFrame* window_update_frame; | |
461 QuicBlockedFrame* blocked_frame; | |
462 QuicPathCloseFrame* path_close_frame; | |
463 }; | |
464 }; | |
465 // QuicFrameType consumes 8 bytes with padding. | |
466 static_assert(sizeof(QuicFrame) <= 16, | |
467 "Frames larger than 8 bytes should be referenced by pointer."); | |
468 | |
469 typedef std::vector<QuicFrame> QuicFrames; | |
470 | |
471 // Deletes all the sub-frames contained in |frames|. | |
472 NET_EXPORT_PRIVATE void DeleteFrames(QuicFrames* frames); | |
473 | |
474 // Deletes all the QuicStreamFrames for the specified |stream_id|. | |
475 NET_EXPORT_PRIVATE void RemoveFramesForStream(QuicFrames* frames, | |
476 QuicStreamId stream_id); | |
477 | |
478 class NET_EXPORT_PRIVATE QuicData { | |
479 public: | |
480 QuicData(const char* buffer, size_t length); | |
481 QuicData(const char* buffer, size_t length, bool owns_buffer); | |
482 virtual ~QuicData(); | |
483 | |
484 base::StringPiece AsStringPiece() const { | |
485 return base::StringPiece(data(), length()); | |
486 } | |
487 | |
488 const char* data() const { return buffer_; } | |
489 size_t length() const { return length_; } | |
490 bool owns_buffer() const { return owns_buffer_; } | |
491 | |
492 private: | |
493 const char* buffer_; | |
494 size_t length_; | |
495 bool owns_buffer_; | |
496 | |
497 DISALLOW_COPY_AND_ASSIGN(QuicData); | |
498 }; | |
499 | |
500 class NET_EXPORT_PRIVATE QuicPacket : public QuicData { | |
501 public: | |
502 // TODO(fayang): 4 fields from public header are passed in as arguments. | |
503 // Consider to add a convenience method which directly accepts the entire | |
504 // public header. | |
505 QuicPacket(char* buffer, | |
506 size_t length, | |
507 bool owns_buffer, | |
508 QuicConnectionIdLength connection_id_length, | |
509 bool includes_version, | |
510 bool includes_path_id, | |
511 bool includes_diversification_nonce, | |
512 QuicPacketNumberLength packet_number_length); | |
513 | |
514 base::StringPiece AssociatedData(QuicVersion version) const; | |
515 base::StringPiece Plaintext(QuicVersion version) const; | |
516 | |
517 char* mutable_data() { return buffer_; } | |
518 | |
519 private: | |
520 char* buffer_; | |
521 const QuicConnectionIdLength connection_id_length_; | |
522 const bool includes_version_; | |
523 const bool includes_path_id_; | |
524 const bool includes_diversification_nonce_; | |
525 const QuicPacketNumberLength packet_number_length_; | |
526 | |
527 DISALLOW_COPY_AND_ASSIGN(QuicPacket); | |
528 }; | |
529 | |
530 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { | |
531 public: | |
532 QuicEncryptedPacket(const char* buffer, size_t length); | |
533 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer); | |
534 | |
535 // Clones the packet into a new packet which owns the buffer. | |
536 std::unique_ptr<QuicEncryptedPacket> Clone() const; | |
537 | |
538 // By default, gtest prints the raw bytes of an object. The bool data | |
539 // member (in the base class QuicData) causes this object to have padding | |
540 // bytes, which causes the default gtest object printer to read | |
541 // uninitialize memory. So we need to teach gtest how to print this object. | |
542 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
543 std::ostream& os, | |
544 const QuicEncryptedPacket& s); | |
545 | |
546 private: | |
547 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket); | |
548 }; | |
549 | |
550 // A received encrypted QUIC packet, with a recorded time of receipt. | |
551 class NET_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket { | |
552 public: | |
553 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time); | |
554 QuicReceivedPacket(const char* buffer, | |
555 size_t length, | |
556 QuicTime receipt_time, | |
557 bool owns_buffer); | |
558 QuicReceivedPacket(const char* buffer, | |
559 size_t length, | |
560 QuicTime receipt_time, | |
561 bool owns_buffer, | |
562 int ttl, | |
563 bool ttl_valid); | |
564 | |
565 // Clones the packet into a new packet which owns the buffer. | |
566 std::unique_ptr<QuicReceivedPacket> Clone() const; | |
567 | |
568 // Returns the time at which the packet was received. | |
569 QuicTime receipt_time() const { return receipt_time_; } | |
570 | |
571 // This is the TTL of the packet, assuming ttl_vaild_ is true. | |
572 int ttl() const { return ttl_; } | |
573 | |
574 // By default, gtest prints the raw bytes of an object. The bool data | |
575 // member (in the base class QuicData) causes this object to have padding | |
576 // bytes, which causes the default gtest object printer to read | |
577 // uninitialize memory. So we need to teach gtest how to print this object. | |
578 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
579 std::ostream& os, | |
580 const QuicReceivedPacket& s); | |
581 | |
582 private: | |
583 const QuicTime receipt_time_; | |
584 int ttl_; | |
585 | |
586 DISALLOW_COPY_AND_ASSIGN(QuicReceivedPacket); | |
587 }; | |
588 | |
589 // Pure virtual class to listen for packet acknowledgements. | |
590 class NET_EXPORT_PRIVATE QuicAckListenerInterface | |
591 : public base::RefCounted<QuicAckListenerInterface> { | |
592 public: | |
593 QuicAckListenerInterface() {} | |
594 | |
595 // Called when a packet is acked. Called once per packet. | |
596 // |acked_bytes| is the number of data bytes acked. | |
597 virtual void OnPacketAcked(int acked_bytes, | |
598 QuicTime::Delta ack_delay_time) = 0; | |
599 | |
600 // Called when a packet is retransmitted. Called once per packet. | |
601 // |retransmitted_bytes| is the number of data bytes retransmitted. | |
602 virtual void OnPacketRetransmitted(int retransmitted_bytes) = 0; | |
603 | |
604 protected: | |
605 friend class base::RefCounted<QuicAckListenerInterface>; | |
606 | |
607 // Delegates are ref counted. | |
608 virtual ~QuicAckListenerInterface() {} | |
609 }; | |
610 | |
611 // Used to generate filtered supported versions based on flags. | |
612 class NET_EXPORT_PRIVATE QuicVersionManager { | |
613 public: | |
614 explicit QuicVersionManager(QuicVersionVector supported_versions); | |
615 virtual ~QuicVersionManager(); | |
616 | |
617 // Returns currently supported QUIC versions. | |
618 const QuicVersionVector& GetSupportedVersions(); | |
619 | |
620 protected: | |
621 // Maybe refilter filtered_supported_versions_ based on flags. | |
622 void MaybeRefilterSupportedVersions(); | |
623 | |
624 // Refilters filtered_supported_versions_. | |
625 virtual void RefilterSupportedVersions(); | |
626 | |
627 const QuicVersionVector& filtered_supported_versions() const { | |
628 return filtered_supported_versions_; | |
629 } | |
630 | |
631 private: | |
632 // FLAGS_quic_enable_version_36_v3 | |
633 bool enable_version_36_; | |
634 // The list of versions that may be supported. | |
635 QuicVersionVector allowed_supported_versions_; | |
636 // This vector contains QUIC versions which are currently supported based | |
637 // on flags. | |
638 QuicVersionVector filtered_supported_versions_; | |
639 }; | |
640 | |
641 struct NET_EXPORT_PRIVATE AckListenerWrapper { | |
642 AckListenerWrapper(QuicAckListenerInterface* listener, | |
643 QuicPacketLength data_length); | |
644 AckListenerWrapper(const AckListenerWrapper& other); | |
645 ~AckListenerWrapper(); | |
646 | |
647 scoped_refptr<QuicAckListenerInterface> ack_listener; | |
648 QuicPacketLength length; | |
649 }; | |
650 | |
651 struct NET_EXPORT_PRIVATE SerializedPacket { | |
652 SerializedPacket(QuicPathId path_id, | |
653 QuicPacketNumber packet_number, | |
654 QuicPacketNumberLength packet_number_length, | |
655 const char* encrypted_buffer, | |
656 QuicPacketLength encrypted_length, | |
657 bool has_ack, | |
658 bool has_stop_waiting); | |
659 SerializedPacket(const SerializedPacket& other); | |
660 ~SerializedPacket(); | |
661 | |
662 // Not owned. | |
663 const char* encrypted_buffer; | |
664 QuicPacketLength encrypted_length; | |
665 QuicFrames retransmittable_frames; | |
666 IsHandshake has_crypto_handshake; | |
667 // -1: full padding to the end of a max-sized packet | |
668 // 0: no padding | |
669 // otherwise: only pad up to num_padding_bytes bytes | |
670 int16_t num_padding_bytes; | |
671 QuicPathId path_id; | |
672 QuicPacketNumber packet_number; | |
673 QuicPacketNumberLength packet_number_length; | |
674 EncryptionLevel encryption_level; | |
675 bool has_ack; | |
676 bool has_stop_waiting; | |
677 TransmissionType transmission_type; | |
678 QuicPathId original_path_id; | |
679 QuicPacketNumber original_packet_number; | |
680 | |
681 // Optional notifiers which will be informed when this packet has been ACKed. | |
682 std::list<AckListenerWrapper> listeners; | |
683 }; | |
684 | |
685 // Deletes and clears all the frames and the packet from serialized packet. | |
686 NET_EXPORT_PRIVATE void ClearSerializedPacket( | |
687 SerializedPacket* serialized_packet); | |
688 | |
689 // Allocates a new char[] of size |packet.encrypted_length| and copies in | |
690 // |packet.encrypted_buffer|. | |
691 NET_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet); | |
692 | |
693 struct NET_EXPORT_PRIVATE TransmissionInfo { | |
694 // Used by STL when assigning into a map. | |
695 TransmissionInfo(); | |
696 | |
697 // Constructs a Transmission with a new all_transmissions set | |
698 // containing |packet_number|. | |
699 TransmissionInfo(EncryptionLevel level, | |
700 QuicPacketNumberLength packet_number_length, | |
701 TransmissionType transmission_type, | |
702 QuicTime sent_time, | |
703 QuicPacketLength bytes_sent, | |
704 bool has_crypto_handshake, | |
705 int num_padding_bytes); | |
706 | |
707 TransmissionInfo(const TransmissionInfo& other); | |
708 | |
709 ~TransmissionInfo(); | |
710 | |
711 QuicFrames retransmittable_frames; | |
712 EncryptionLevel encryption_level; | |
713 QuicPacketNumberLength packet_number_length; | |
714 QuicPacketLength bytes_sent; | |
715 QuicTime sent_time; | |
716 // Reason why this packet was transmitted. | |
717 TransmissionType transmission_type; | |
718 // In flight packets have not been abandoned or lost. | |
719 bool in_flight; | |
720 // True if the packet can never be acked, so it can be removed. Occurs when | |
721 // a packet is never sent, after it is acknowledged once, or if it's a crypto | |
722 // packet we never expect to receive an ack for. | |
723 bool is_unackable; | |
724 // True if the packet contains stream data from the crypto stream. | |
725 bool has_crypto_handshake; | |
726 // Non-zero if the packet needs padding if it's retransmitted. | |
727 int16_t num_padding_bytes; | |
728 // Stores the packet number of the next retransmission of this packet. | |
729 // Zero if the packet has not been retransmitted. | |
730 QuicPacketNumber retransmission; | |
731 // Non-empty if there is a std::listener for this packet. | |
732 std::list<AckListenerWrapper> ack_listeners; | |
733 }; | |
734 | |
735 // Struct to store the pending retransmission information. | |
736 struct PendingRetransmission { | |
737 PendingRetransmission(QuicPathId path_id, | |
738 QuicPacketNumber packet_number, | |
739 TransmissionType transmission_type, | |
740 const QuicFrames& retransmittable_frames, | |
741 bool has_crypto_handshake, | |
742 int num_padding_bytes, | |
743 EncryptionLevel encryption_level, | |
744 QuicPacketNumberLength packet_number_length) | |
745 : packet_number(packet_number), | |
746 retransmittable_frames(retransmittable_frames), | |
747 transmission_type(transmission_type), | |
748 path_id(path_id), | |
749 has_crypto_handshake(has_crypto_handshake), | |
750 num_padding_bytes(num_padding_bytes), | |
751 encryption_level(encryption_level), | |
752 packet_number_length(packet_number_length) {} | |
753 | |
754 QuicPacketNumber packet_number; | |
755 const QuicFrames& retransmittable_frames; | |
756 TransmissionType transmission_type; | |
757 QuicPathId path_id; | |
758 bool has_crypto_handshake; | |
759 int num_padding_bytes; | |
760 EncryptionLevel encryption_level; | |
761 QuicPacketNumberLength packet_number_length; | |
762 }; | |
763 | |
764 } // namespace net | |
765 | |
766 #endif // NET_QUIC_QUIC_PROTOCOL_H_ | |
OLD | NEW |