xref: /trafficserver/iocore/net/quic/QUICFrame.h (revision a80d7794)
1 /** @file
2  *
3  *  A brief file description
4  *
5  *  @section license License
6  *
7  *  Licensed to the Apache Software Foundation (ASF) under one
8  *  or more contributor license agreements.  See the NOTICE file
9  *  distributed with this work for additional information
10  *  regarding copyright ownership.  The ASF licenses this file
11  *  to you under the Apache License, Version 2.0 (the
12  *  "License"); you may not use this file except in compliance
13  *  with the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include <memory>
27 #include "tscore/Allocator.h"
28 #include "tscore/List.h"
29 #include "tscore/Ptr.h"
30 #include "I_IOBuffer.h"
31 #include <vector>
32 #include <iterator>
33 
34 #include "QUICTypes.h"
35 
36 class QUICFrame;
37 class QUICStreamFrame;
38 class QUICCryptoFrame;
39 class QUICPacket;
40 class QUICFrameGenerator;
41 
42 using QUICFrameId = uint64_t;
43 
44 class QUICFrame
45 {
46 public:
47   constexpr static int MAX_INSTANCE_SIZE = 256;
48 
~QUICFrame()49   virtual ~QUICFrame() {}
50   static QUICFrameType type(const uint8_t *buf);
51 
52   QUICFrameId id() const;
53 
54   virtual QUICFrameType type() const;
55   virtual size_t size() const = 0;
56   virtual bool is_probing_frame() const;
57   virtual bool is_flow_controlled() const;
58   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const = 0;
59   virtual int debug_msg(char *msg, size_t msg_len) const;
parse(const uint8_t * buf,size_t len,const QUICPacket * packet)60   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet){};
61   virtual QUICFrameGenerator *generated_by();
62   bool valid() const;
63   bool ack_eliciting() const;
64   const QUICPacket *packet() const;
65   LINK(QUICFrame, link);
66 
67 protected:
_reset()68   virtual void _reset(){};
QUICFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr,const QUICPacket * packet=nullptr)69   QUICFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr, const QUICPacket *packet = nullptr)
70     : _id(id), _owner(owner), _packet(packet)
71   {
72   }
73   size_t _size               = 0;
74   bool _valid                = false;
75   QUICFrameId _id            = 0;
76   QUICFrameGenerator *_owner = nullptr;
77   const QUICPacket *_packet  = nullptr;
78 };
79 
80 //
81 // STREAM Frame
82 //
83 
84 class QUICStreamFrame : public QUICFrame
85 {
86 public:
QUICStreamFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)87   QUICStreamFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
88   QUICStreamFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
89   QUICStreamFrame(Ptr<IOBufferBlock> &block, QUICStreamId streamid, QUICOffset offset, bool last = false,
90                   bool has_offset_field = true, bool has_length_field = true, QUICFrameId id = 0,
91                   QUICFrameGenerator *owner = nullptr);
92   QUICStreamFrame(const QUICStreamFrame &o);
93 
94   virtual QUICFrameType type() const override;
95   virtual size_t size() const override;
96   virtual bool is_flow_controlled() const override;
97   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
98   virtual int debug_msg(char *msg, size_t msg_len) const override;
99   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
100 
101   QUICStreamId stream_id() const;
102   QUICOffset offset() const;
103   IOBufferBlock *data() const;
104   uint64_t data_length() const;
105   bool has_offset_field() const;
106   bool has_length_field() const;
107   bool has_fin_flag() const;
108 
109   LINK(QUICStreamFrame, link);
110 
111 private:
112   static constexpr uint8_t MAX_HEADER_SIZE = 32;
113 
114   virtual void _reset() override;
115 
116   size_t _store_header(uint8_t *buf, size_t *len, bool include_length_field) const;
117 
118   Ptr<IOBufferBlock> _block;
119   QUICStreamId _stream_id = 0;
120   QUICOffset _offset      = 0;
121   bool _fin               = false;
122   bool _has_offset_field  = true;
123   bool _has_length_field  = true;
124 };
125 
126 //
127 // CRYPTO Frame
128 //
129 
130 class QUICCryptoFrame : public QUICFrame
131 {
132 public:
QUICCryptoFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)133   QUICCryptoFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
134   QUICCryptoFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
135   QUICCryptoFrame(Ptr<IOBufferBlock> &block, QUICOffset offset, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
136   QUICCryptoFrame(const QUICCryptoFrame &o);
137 
138   virtual QUICFrameType type() const override;
139   virtual size_t size() const override;
140   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
141   virtual int debug_msg(char *msg, size_t msg_len) const override;
142   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
143 
144   QUICOffset offset() const;
145   uint64_t data_length() const;
146   IOBufferBlock *data() const;
147 
148   LINK(QUICCryptoFrame, link);
149 
150 private:
151   static constexpr uint8_t MAX_HEADER_SIZE = 16;
152 
153   virtual void _reset() override;
154 
155   size_t _store_header(uint8_t *buf, size_t *len) const;
156 
157   QUICOffset _offset = 0;
158   Ptr<IOBufferBlock> _block;
159 };
160 
161 //
162 // ACK Frame
163 //
164 
165 class QUICAckFrame : public QUICFrame
166 {
167 public:
168   class PacketNumberRange
169   {
170   public:
PacketNumberRange(QUICPacketNumber first,QUICPacketNumber last)171     PacketNumberRange(QUICPacketNumber first, QUICPacketNumber last) : _first(first), _last(last) {}
172     PacketNumberRange(PacketNumberRange &&a) noexcept;
173     QUICPacketNumber first() const;
174     QUICPacketNumber last() const;
175     uint64_t size() const;
176     bool contains(QUICPacketNumber x) const;
177     bool
operator <(const PacketNumberRange & b) const178     operator<(const PacketNumberRange &b) const
179     {
180       return static_cast<uint64_t>(this->first()) < static_cast<uint64_t>(b.first());
181     }
182 
183   private:
184     QUICPacketNumber _first;
185     QUICPacketNumber _last;
186   };
187 
188   class AckBlock
189   {
190   public:
AckBlock(uint64_t g,uint64_t l)191     AckBlock(uint64_t g, uint64_t l) : _gap(g), _length(l) {}
192     uint64_t gap() const;
193     uint64_t length() const;
194     size_t size() const;
195     LINK(QUICAckFrame::AckBlock, link);
196 
197   private:
198     size_t _get_gap_size() const;
199     size_t _get_length_size() const;
200 
201     uint64_t _gap    = 0;
202     uint64_t _length = 0;
203   };
204 
205   class AckBlockSection
206   {
207   public:
208     class const_iterator : public std::iterator<std::input_iterator_tag, QUICAckFrame::AckBlock>
209     {
210     public:
211       const_iterator(uint8_t index, const std::vector<QUICAckFrame::AckBlock> *ack_blocks);
212 
operator *() const213       const QUICAckFrame::AckBlock &operator*() const { return this->_current_block; };
operator ->() const214       const QUICAckFrame::AckBlock *operator->() const { return &this->_current_block; };
215       const QUICAckFrame::AckBlock &operator++();
216       const bool operator!=(const const_iterator &ite) const;
217       const bool operator==(const const_iterator &ite) const;
218 
219     private:
220       uint8_t _index                                         = 0;
221       QUICAckFrame::AckBlock _current_block                  = {UINT64_C(0), UINT64_C(0)};
222       const std::vector<QUICAckFrame::AckBlock> *_ack_blocks = nullptr;
223     };
224 
AckBlockSection(uint64_t first_ack_block)225     AckBlockSection(uint64_t first_ack_block) : _first_ack_block(first_ack_block) {}
226     uint8_t count() const;
227     size_t size() const;
228     Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const;
229     uint64_t first_ack_block() const;
230     void add_ack_block(const AckBlock block);
231     const_iterator begin() const;
232     const_iterator end() const;
233     bool has_protected() const;
234 
235   private:
236     uint64_t _first_ack_block = 0;
237     uint8_t _ack_block_count  = 0;
238     std::vector<QUICAckFrame::AckBlock> _ack_blocks;
239   };
240 
241   class EcnSection
242   {
243   public:
244     EcnSection(const uint8_t *buf, size_t len);
245     size_t size() const;
246     bool valid() const;
247     uint64_t ect0_count() const;
248     uint64_t ect1_count() const;
249     uint64_t ecn_ce_count() const;
250 
251   private:
252     bool _valid            = false;
253     size_t _size           = 0;
254     uint64_t _ect0_count   = 0;
255     uint64_t _ect1_count   = 0;
256     uint64_t _ecn_ce_count = 0;
257   };
258 
QUICAckFrame(QUICFrameId id=0)259   QUICAckFrame(QUICFrameId id = 0) : QUICFrame(id) {}
260   QUICAckFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
261   QUICAckFrame(QUICPacketNumber largest_acknowledged, uint64_t ack_delay, uint64_t first_ack_block, QUICFrameId id = 0,
262                QUICFrameGenerator *owner = nullptr);
263 
264   // There's no reasont restrict copy, but we need to write the copy constructor. Otherwise it will crash on destruct.
265   QUICAckFrame(const QUICAckFrame &) = delete;
266 
267   virtual ~QUICAckFrame();
268   virtual QUICFrameType type() const override;
269   virtual size_t size() const override;
270   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
271   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
272   virtual int debug_msg(char *msg, size_t msg_len) const override;
273 
274   QUICPacketNumber largest_acknowledged() const;
275   uint64_t ack_delay() const;
276   uint64_t ack_block_count() const;
277   const AckBlockSection *ack_block_section() const;
278   AckBlockSection *ack_block_section();
279   const EcnSection *ecn_section() const;
280   EcnSection *ecn_section();
281 
282 private:
283   virtual void _reset() override;
284 
285   QUICPacketNumber _largest_acknowledged = 0;
286   uint64_t _ack_delay                    = 0;
287   AckBlockSection *_ack_block_section    = nullptr;
288   EcnSection *_ecn_section               = nullptr;
289 };
290 
291 //
292 // RESET_STREAM
293 //
294 
295 class QUICRstStreamFrame : public QUICFrame
296 {
297 public:
QUICRstStreamFrame(QUICFrameId id=0)298   QUICRstStreamFrame(QUICFrameId id = 0) : QUICFrame(id) {}
299   QUICRstStreamFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
300   QUICRstStreamFrame(QUICStreamId stream_id, QUICAppErrorCode error_code, QUICOffset final_offset, QUICFrameId id = 0,
301                      QUICFrameGenerator *owner = nullptr);
302 
303   virtual QUICFrameType type() const override;
304   virtual size_t size() const override;
305   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
306   virtual int debug_msg(char *msg, size_t msg_len) const override;
307   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
308 
309   QUICStreamId stream_id() const;
310   QUICAppErrorCode error_code() const;
311   QUICOffset final_offset() const;
312 
313 private:
314   virtual void _reset() override;
315 
316   QUICStreamId _stream_id      = 0;
317   QUICAppErrorCode _error_code = 0;
318   QUICOffset _final_offset     = 0;
319 };
320 
321 //
322 // PING
323 //
324 
325 class QUICPingFrame : public QUICFrame
326 {
327 public:
QUICPingFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)328   QUICPingFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
329   QUICPingFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
330   virtual QUICFrameType type() const override;
331   virtual size_t size() const override;
332   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
333   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
334 
335 private:
336 };
337 
338 //
339 // PADDING
340 //
341 
342 class QUICPaddingFrame : public QUICFrame
343 {
344 public:
QUICPaddingFrame(size_t size)345   QUICPaddingFrame(size_t size) : _size(size) {}
346   QUICPaddingFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
347   virtual QUICFrameType type() const override;
348   virtual size_t size() const override;
349   virtual bool is_probing_frame() const override;
350   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
351   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
352 
353 private:
354   // padding frame is a resident of padding frames
355   // size indicate how many padding frames in this QUICPaddingFrame
356   size_t _size = 0;
357 };
358 
359 //
360 // CONNECTION_CLOSE
361 //
362 
363 class QUICConnectionCloseFrame : public QUICFrame
364 {
365 public:
QUICConnectionCloseFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)366   QUICConnectionCloseFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
367   QUICConnectionCloseFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
368   // Constructor for transport error codes
369   QUICConnectionCloseFrame(uint64_t error_code, QUICFrameType frame_type, uint64_t reason_phrase_length, const char *reason_phrase,
370                            QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
371   // Constructor for application protocol error codes
372   QUICConnectionCloseFrame(uint64_t error_code, uint64_t reason_phrase_length, const char *reason_phrase, QUICFrameId id = 0,
373                            QUICFrameGenerator *owner = nullptr);
374   virtual QUICFrameType type() const override;
375   virtual size_t size() const override;
376   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
377   virtual int debug_msg(char *msg, size_t msg_len) const override;
378   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
379 
380   uint16_t error_code() const;
381   QUICFrameType frame_type() const;
382   uint64_t reason_phrase_length() const;
383   const char *reason_phrase() const;
384 
385 private:
386   virtual void _reset() override;
387 
388   uint8_t _type = 0;
389   uint64_t _error_code;
390   QUICFrameType _frame_type      = QUICFrameType::UNKNOWN;
391   uint64_t _reason_phrase_length = 0;
392   const char *_reason_phrase     = nullptr;
393 };
394 
395 //
396 // MAX_DATA
397 //
398 
399 class QUICMaxDataFrame : public QUICFrame
400 {
401 public:
QUICMaxDataFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)402   QUICMaxDataFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
403   QUICMaxDataFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
404   QUICMaxDataFrame(uint64_t maximum_data, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
405   virtual QUICFrameType type() const override;
406   virtual size_t size() const override;
407   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
408   virtual int debug_msg(char *msg, size_t msg_len) const override;
409   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
410 
411   uint64_t maximum_data() const;
412 
413 private:
414   virtual void _reset() override;
415 
416   uint64_t _maximum_data = 0;
417 };
418 
419 //
420 // MAX_STREAM_DATA
421 //
422 
423 class QUICMaxStreamDataFrame : public QUICFrame
424 {
425 public:
QUICMaxStreamDataFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)426   QUICMaxStreamDataFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
427   QUICMaxStreamDataFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
428   QUICMaxStreamDataFrame(QUICStreamId stream_id, uint64_t maximum_stream_data, QUICFrameId id = 0,
429                          QUICFrameGenerator *owner = nullptr);
430   virtual QUICFrameType type() const override;
431   virtual size_t size() const override;
432   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
433   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
434   virtual int debug_msg(char *msg, size_t msg_len) const override;
435 
436   QUICStreamId stream_id() const;
437   uint64_t maximum_stream_data() const;
438 
439 private:
440   virtual void _reset() override;
441 
442   QUICStreamId _stream_id       = 0;
443   uint64_t _maximum_stream_data = 0;
444 };
445 
446 //
447 // MAX_STREAMS
448 //
449 
450 class QUICMaxStreamsFrame : public QUICFrame
451 {
452 public:
QUICMaxStreamsFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)453   QUICMaxStreamsFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
454   QUICMaxStreamsFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
455   QUICMaxStreamsFrame(QUICStreamId maximum_streams, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
456   virtual QUICFrameType type() const override;
457   virtual size_t size() const override;
458   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
459   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
460   uint64_t maximum_streams() const;
461 
462 private:
463   virtual void _reset() override;
464 
465   uint64_t _maximum_streams = 0;
466 };
467 
468 //
469 // BLOCKED
470 //
471 class QUICDataBlockedFrame : public QUICFrame
472 {
473 public:
QUICDataBlockedFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)474   QUICDataBlockedFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
475   QUICDataBlockedFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICDataBlockedFrame(QUICOffset offset,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)476   QUICDataBlockedFrame(QUICOffset offset, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
477     : QUICFrame(id, owner), _offset(offset){};
478 
479   virtual QUICFrameType type() const override;
480   virtual size_t size() const override;
481   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
482   virtual int debug_msg(char *msg, size_t msg_len) const override;
483   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
484 
485   QUICOffset offset() const;
486 
487 private:
488   virtual void _reset() override;
489 
490   QUICOffset _offset = 0;
491 };
492 
493 //
494 // STREAM_DATA_BLOCKED
495 //
496 
497 class QUICStreamDataBlockedFrame : public QUICFrame
498 {
499 public:
QUICStreamDataBlockedFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)500   QUICStreamDataBlockedFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
501   QUICStreamDataBlockedFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICStreamDataBlockedFrame(QUICStreamId s,QUICOffset o,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)502   QUICStreamDataBlockedFrame(QUICStreamId s, QUICOffset o, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
503     : QUICFrame(id, owner), _stream_id(s), _offset(o){};
504 
505   virtual QUICFrameType type() const override;
506   virtual size_t size() const override;
507   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
508   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
509   virtual int debug_msg(char *msg, size_t msg_len) const override;
510 
511   QUICStreamId stream_id() const;
512   QUICOffset offset() const;
513 
514 private:
515   virtual void _reset() override;
516 
517   QUICStreamId _stream_id = 0;
518   QUICOffset _offset      = 0;
519 };
520 
521 //
522 // STREAMS_BLOCKED
523 //
524 class QUICStreamIdBlockedFrame : public QUICFrame
525 {
526 public:
QUICStreamIdBlockedFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)527   QUICStreamIdBlockedFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
528   QUICStreamIdBlockedFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICStreamIdBlockedFrame(QUICStreamId s,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)529   QUICStreamIdBlockedFrame(QUICStreamId s, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
530     : QUICFrame(id, owner), _stream_id(s)
531   {
532   }
533   virtual QUICFrameType type() const override;
534   virtual size_t size() const override;
535   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
536   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
537 
538   QUICStreamId stream_id() const;
539 
540 private:
541   virtual void _reset() override;
542 
543   QUICStreamId _stream_id = 0;
544 };
545 
546 //
547 // NEW_CONNECTION_ID
548 //
549 
550 class QUICNewConnectionIdFrame : public QUICFrame
551 {
552 public:
QUICNewConnectionIdFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)553   QUICNewConnectionIdFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
554   QUICNewConnectionIdFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICNewConnectionIdFrame(uint64_t seq,uint64_t ret,const QUICConnectionId & cid,QUICStatelessResetToken token,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)555   QUICNewConnectionIdFrame(uint64_t seq, uint64_t ret, const QUICConnectionId &cid, QUICStatelessResetToken token,
556                            QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
557     : QUICFrame(id, owner), _sequence(seq), _retire_prior_to(ret), _connection_id(cid), _stateless_reset_token(token){};
558 
559   virtual QUICFrameType type() const override;
560   virtual size_t size() const override;
561   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
562   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
563   virtual int debug_msg(char *msg, size_t msg_len) const override;
564 
565   uint64_t sequence() const;
566   uint64_t retire_prior_to() const;
567   QUICConnectionId connection_id() const;
568   QUICStatelessResetToken stateless_reset_token() const;
569 
570 private:
571   virtual void _reset() override;
572 
573   uint64_t _sequence              = 0;
574   uint64_t _retire_prior_to       = 0;
575   QUICConnectionId _connection_id = QUICConnectionId::ZERO();
576   QUICStatelessResetToken _stateless_reset_token;
577 };
578 
579 //
580 // STOP_SENDING
581 //
582 
583 class QUICStopSendingFrame : public QUICFrame
584 {
585 public:
QUICStopSendingFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)586   QUICStopSendingFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
587   QUICStopSendingFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
588   QUICStopSendingFrame(QUICStreamId stream_id, QUICAppErrorCode error_code, QUICFrameId id = 0,
589                        QUICFrameGenerator *owner = nullptr);
590 
591   virtual QUICFrameType type() const override;
592   virtual size_t size() const override;
593   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
594   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
595 
596   QUICStreamId stream_id() const;
597   QUICAppErrorCode error_code() const;
598 
599 private:
600   virtual void _reset() override;
601 
602   QUICStreamId _stream_id      = 0;
603   QUICAppErrorCode _error_code = 0;
604 };
605 
606 //
607 // PATH_CHALLENGE
608 //
609 
610 class QUICPathChallengeFrame : public QUICFrame
611 {
612 public:
613   static constexpr uint8_t DATA_LEN = 8;
QUICPathChallengeFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)614   QUICPathChallengeFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
615   QUICPathChallengeFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICPathChallengeFrame(ats_unique_buf data,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)616   QUICPathChallengeFrame(ats_unique_buf data, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
617     : QUICFrame(id, owner), _data(std::move(data))
618   {
619   }
620   virtual QUICFrameType type() const override;
621   virtual size_t size() const override;
622   virtual bool is_probing_frame() const override;
623   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
624   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
625   virtual int debug_msg(char *msg, size_t msg_len) const override;
626 
627   const uint8_t *data() const;
628 
629 private:
630   virtual void _reset() override;
631 
632   ats_unique_buf _data = {nullptr};
633 };
634 
635 //
636 // PATH_RESPONSE
637 //
638 
639 class QUICPathResponseFrame : public QUICFrame
640 {
641 public:
642   static constexpr uint8_t DATA_LEN = 8;
QUICPathResponseFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)643   QUICPathResponseFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
644   QUICPathResponseFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICPathResponseFrame(ats_unique_buf data,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)645   QUICPathResponseFrame(ats_unique_buf data, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
646     : QUICFrame(id, owner), _data(std::move(data))
647   {
648   }
649   virtual QUICFrameType type() const override;
650   virtual size_t size() const override;
651   virtual bool is_probing_frame() const override;
652   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
653   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
654   virtual int debug_msg(char *msg, size_t msg_len) const override;
655 
656   const uint8_t *data() const;
657 
658 private:
659   virtual void _reset() override;
660 
661   ats_unique_buf _data = {nullptr};
662 };
663 
664 //
665 // NEW_TOKEN
666 //
667 
668 class QUICNewTokenFrame : public QUICFrame
669 {
670 public:
QUICNewTokenFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)671   QUICNewTokenFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
672   QUICNewTokenFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICNewTokenFrame(ats_unique_buf token,size_t token_length,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)673   QUICNewTokenFrame(ats_unique_buf token, size_t token_length, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
674     : QUICFrame(id, owner), _token_length(token_length), _token(std::move(token))
675   {
676   }
677   virtual QUICFrameType type() const override;
678   virtual size_t size() const override;
679   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
680   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
681 
682   uint64_t token_length() const;
683   const uint8_t *token() const;
684 
685 private:
686   virtual void _reset() override;
687 
688   uint64_t _token_length = 0;
689   ats_unique_buf _token  = {nullptr};
690 };
691 
692 //
693 // RETIRE_CONNECTION_ID
694 //
695 
696 class QUICRetireConnectionIdFrame : public QUICFrame
697 {
698 public:
QUICRetireConnectionIdFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)699   QUICRetireConnectionIdFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
700   QUICRetireConnectionIdFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICRetireConnectionIdFrame(uint64_t seq_num,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)701   QUICRetireConnectionIdFrame(uint64_t seq_num, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
702     : QUICFrame(id, owner), _seq_num(seq_num)
703   {
704   }
705   virtual QUICFrameType type() const override;
706   virtual size_t size() const override;
707   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
708   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
709   virtual int debug_msg(char *msg, size_t msg_len) const override;
710 
711   uint64_t seq_num() const;
712 
713 private:
714   virtual void _reset() override;
715 
716   uint64_t _seq_num = 0;
717 };
718 
719 //
720 // UNKNOWN
721 //
722 
723 class QUICUnknownFrame : public QUICFrame
724 {
725   QUICFrameType type() const override;
726   size_t size() const override;
727   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
728   void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
729   int debug_msg(char *msg, size_t msg_len) const override;
730 };
731 
732 //
733 // QUICFrameFactory
734 //
735 class QUICFrameFactory
736 {
737 public:
738   /*
739    * This is used for creating a QUICFrame object based on received data.
740    */
741   static QUICFrame *create(uint8_t *buf, const uint8_t *src, size_t len, const QUICPacket *packet);
742 
743   /*
744    * This works almost the same as create() but it reuses created objects for performance.
745    * If you create a frame object which has the same frame type that you created before, the object will be reset by new data.
746    */
747   const QUICFrame &fast_create(const uint8_t *buf, size_t len, const QUICPacket *packet);
748 
749   /*
750    * Creates a STREAM frame.
751    * You have to make sure that the data size won't exceed the maximum size of QUIC packet.
752    */
753   static QUICStreamFrame *create_stream_frame(uint8_t *buf, Ptr<IOBufferBlock> &block, QUICStreamId stream_id, QUICOffset offset,
754                                               bool last = false, bool has_offset_field = true, bool has_length_field = true,
755                                               QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
756 
757   /*
758    * Creates a CRYPTO frame.
759    * You have to make sure that the data size won't exceed the maximum size of QUIC packet.
760    */
761   static QUICCryptoFrame *create_crypto_frame(uint8_t *buf, Ptr<IOBufferBlock> &block, QUICOffset offset, QUICFrameId id = 0,
762                                               QUICFrameGenerator *owner = nullptr);
763 
764   /*
765    * Creates a ACK frame.
766    * You shouldn't call this directly but through QUICAckFrameCreator because QUICAckFrameCreator manages packet numbers that we
767    * need to ack.
768    */
769   static QUICAckFrame *create_ack_frame(uint8_t *buf, QUICPacketNumber largest_acknowledged, uint64_t ack_delay,
770                                         uint64_t first_ack_block, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
771   /*
772    * Creates a CONNECTION_CLOSE frame.
773    */
774   static QUICConnectionCloseFrame *create_connection_close_frame(uint8_t *buf, uint16_t error_code, QUICFrameType frame_type,
775                                                                  uint16_t reason_phrase_length = 0,
776                                                                  const char *reason_phrase = nullptr, QUICFrameId id = 0,
777                                                                  QUICFrameGenerator *owner = nullptr);
778 
779   static QUICConnectionCloseFrame *create_connection_close_frame(uint8_t *buf, QUICConnectionError &error, QUICFrameId id = 0,
780                                                                  QUICFrameGenerator *owner = nullptr);
781 
782   /*
783    * Creates a MAX_DATA frame.
784    */
785   static QUICMaxDataFrame *create_max_data_frame(uint8_t *buf, uint64_t maximum_data, QUICFrameId id = 0,
786                                                  QUICFrameGenerator *owner = nullptr);
787 
788   /*
789  /  * Creates a MAX_STREAM_DATA frame.
790    */
791   static QUICMaxStreamDataFrame *create_max_stream_data_frame(uint8_t *buf, QUICStreamId stream_id, uint64_t maximum_stream_data,
792                                                               QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
793   /*
794    * Creates a MAX_STREAMS frame.
795    */
796   static QUICMaxStreamsFrame *create_max_streams_frame(uint8_t *buf, QUICStreamId maximum_streams, QUICFrameId id = 0,
797                                                        QUICFrameGenerator *owner = nullptr);
798 
799   /*
800    * Creates a PING frame
801    */
802   static QUICPingFrame *create_ping_frame(uint8_t *buf, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
803 
804   /*
805    * Creates a PATH_CHALLENGE frame
806    */
807   static QUICPathChallengeFrame *create_path_challenge_frame(uint8_t *buf, const uint8_t *data, QUICFrameId id = 0,
808                                                              QUICFrameGenerator *owner = nullptr);
809 
810   /*
811    * Creates a PATH_RESPONSE frame
812    */
813   static QUICPathResponseFrame *create_path_response_frame(uint8_t *buf, const uint8_t *data, QUICFrameId id = 0,
814                                                            QUICFrameGenerator *owner = nullptr);
815 
816   /*
817    * Creates a BLOCKED frame.
818    */
819   static QUICDataBlockedFrame *create_data_blocked_frame(uint8_t *buf, QUICOffset offset, QUICFrameId id = 0,
820                                                          QUICFrameGenerator *owner = nullptr);
821 
822   /*
823    * Creates a STREAM_DATA_BLOCKED frame.
824    */
825   static QUICStreamDataBlockedFrame *create_stream_data_blocked_frame(uint8_t *buf, QUICStreamId stream_id, QUICOffset offset,
826                                                                       QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
827 
828   /*
829    * Creates a STREAMS_BLOCKED frame.
830    */
831   static QUICStreamIdBlockedFrame *create_stream_id_blocked_frame(uint8_t *buf, QUICStreamId stream_id, QUICFrameId id = 0,
832                                                                   QUICFrameGenerator *owner = nullptr);
833 
834   /*
835    * Creates a RESET_STREAM frame.
836    */
837   static QUICRstStreamFrame *create_rst_stream_frame(uint8_t *buf, QUICStreamId stream_id, QUICAppErrorCode error_code,
838                                                      QUICOffset final_offset, QUICFrameId id = 0,
839                                                      QUICFrameGenerator *owner = nullptr);
840   static QUICRstStreamFrame *create_rst_stream_frame(uint8_t *buf, QUICStreamError &error, QUICFrameId id = 0,
841                                                      QUICFrameGenerator *owner = nullptr);
842 
843   /*
844    * Creates a STOP_SENDING frame.
845    */
846   static QUICStopSendingFrame *create_stop_sending_frame(uint8_t *buf, QUICStreamId stream_id, QUICAppErrorCode error_code,
847                                                          QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
848 
849   /*
850    * Creates a NEW_CONNECTION_ID frame.
851    */
852   static QUICNewConnectionIdFrame *create_new_connection_id_frame(uint8_t *buf, uint64_t sequence, uint64_t retire_prior_to,
853                                                                   QUICConnectionId connectoin_id,
854                                                                   QUICStatelessResetToken stateless_reset_token, QUICFrameId id = 0,
855                                                                   QUICFrameGenerator *owner = nullptr);
856 
857   /*
858    * Creates a NEW_TOKEN frame
859    */
860   static QUICNewTokenFrame *create_new_token_frame(uint8_t *buf, const QUICResumptionToken &token, QUICFrameId id = 0,
861                                                    QUICFrameGenerator *owner = nullptr);
862 
863   /*
864    * Creates a RETIRE_CONNECTION_ID frame
865    */
866   static QUICRetireConnectionIdFrame *create_retire_connection_id_frame(uint8_t *buf, uint64_t seq_num, QUICFrameId id = 0,
867                                                                         QUICFrameGenerator *owner = nullptr);
868 
869   /*
870    * Creates a PADDING frame
871    */
872   static QUICPaddingFrame *create_padding_frame(uint8_t *buf, size_t size, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
873 
874 private:
875   // FIXME Actual number of frame types is several but some of the values are not sequential.
876   QUICFrame *_reusable_frames[256] = {nullptr};
877   uint8_t _buf_for_fast_create[256 * QUICFrame::MAX_INSTANCE_SIZE];
878   QUICUnknownFrame _unknown_frame;
879 };
880 
881 class QUICFrameInfo
882 {
883 public:
QUICFrameInfo(QUICFrameId id,QUICFrameGenerator * generator)884   QUICFrameInfo(QUICFrameId id, QUICFrameGenerator *generator) : _id(id), _generator(generator) {}
885   QUICFrameId id() const;
886   QUICFrameGenerator *generated_by() const;
887 
888 private:
889   QUICFrameId _id = 0;
890   QUICFrameGenerator *_generator;
891 };
892