xref: /trafficserver/iocore/net/quic/QUICFrame.h (revision 72299397)
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 
213       const QUICAckFrame::AckBlock &
operator *() const214       operator*() const
215       {
216         return this->_current_block;
217       };
218       const QUICAckFrame::AckBlock *
operator ->() const219       operator->() const
220       {
221         return &this->_current_block;
222       };
223       const QUICAckFrame::AckBlock &operator++();
224       const bool operator!=(const const_iterator &ite) const;
225       const bool operator==(const const_iterator &ite) const;
226 
227     private:
228       uint8_t _index                                         = 0;
229       QUICAckFrame::AckBlock _current_block                  = {UINT64_C(0), UINT64_C(0)};
230       const std::vector<QUICAckFrame::AckBlock> *_ack_blocks = nullptr;
231     };
232 
AckBlockSection(uint64_t first_ack_block)233     AckBlockSection(uint64_t first_ack_block) : _first_ack_block(first_ack_block) {}
234     uint8_t count() const;
235     size_t size() const;
236     Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const;
237     uint64_t first_ack_block() const;
238     void add_ack_block(const AckBlock block);
239     const_iterator begin() const;
240     const_iterator end() const;
241     bool has_protected() const;
242 
243   private:
244     uint64_t _first_ack_block = 0;
245     uint8_t _ack_block_count  = 0;
246     std::vector<QUICAckFrame::AckBlock> _ack_blocks;
247   };
248 
249   class EcnSection
250   {
251   public:
252     EcnSection(const uint8_t *buf, size_t len);
253     size_t size() const;
254     bool valid() const;
255     uint64_t ect0_count() const;
256     uint64_t ect1_count() const;
257     uint64_t ecn_ce_count() const;
258 
259   private:
260     bool _valid            = false;
261     size_t _size           = 0;
262     uint64_t _ect0_count   = 0;
263     uint64_t _ect1_count   = 0;
264     uint64_t _ecn_ce_count = 0;
265   };
266 
QUICAckFrame(QUICFrameId id=0)267   QUICAckFrame(QUICFrameId id = 0) : QUICFrame(id) {}
268   QUICAckFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
269   QUICAckFrame(QUICPacketNumber largest_acknowledged, uint64_t ack_delay, uint64_t first_ack_block, QUICFrameId id = 0,
270                QUICFrameGenerator *owner = nullptr);
271 
272   // There's no reasont restrict copy, but we need to write the copy constructor. Otherwise it will crash on destruct.
273   QUICAckFrame(const QUICAckFrame &) = delete;
274 
275   virtual ~QUICAckFrame();
276   virtual QUICFrameType type() const override;
277   virtual size_t size() const override;
278   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
279   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
280   virtual int debug_msg(char *msg, size_t msg_len) const override;
281 
282   QUICPacketNumber largest_acknowledged() const;
283   uint64_t ack_delay() const;
284   uint64_t ack_block_count() const;
285   const AckBlockSection *ack_block_section() const;
286   AckBlockSection *ack_block_section();
287   const EcnSection *ecn_section() const;
288   EcnSection *ecn_section();
289 
290 private:
291   virtual void _reset() override;
292 
293   QUICPacketNumber _largest_acknowledged = 0;
294   uint64_t _ack_delay                    = 0;
295   AckBlockSection *_ack_block_section    = nullptr;
296   EcnSection *_ecn_section               = nullptr;
297 };
298 
299 //
300 // RESET_STREAM
301 //
302 
303 class QUICRstStreamFrame : public QUICFrame
304 {
305 public:
QUICRstStreamFrame(QUICFrameId id=0)306   QUICRstStreamFrame(QUICFrameId id = 0) : QUICFrame(id) {}
307   QUICRstStreamFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
308   QUICRstStreamFrame(QUICStreamId stream_id, QUICAppErrorCode error_code, QUICOffset final_offset, QUICFrameId id = 0,
309                      QUICFrameGenerator *owner = nullptr);
310 
311   virtual QUICFrameType type() const override;
312   virtual size_t size() const override;
313   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
314   virtual int debug_msg(char *msg, size_t msg_len) const override;
315   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
316 
317   QUICStreamId stream_id() const;
318   QUICAppErrorCode error_code() const;
319   QUICOffset final_offset() const;
320 
321 private:
322   virtual void _reset() override;
323 
324   QUICStreamId _stream_id      = 0;
325   QUICAppErrorCode _error_code = 0;
326   QUICOffset _final_offset     = 0;
327 };
328 
329 //
330 // PING
331 //
332 
333 class QUICPingFrame : public QUICFrame
334 {
335 public:
QUICPingFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)336   QUICPingFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
337   QUICPingFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
338   virtual QUICFrameType type() const override;
339   virtual size_t size() const override;
340   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
341   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
342 
343 private:
344 };
345 
346 //
347 // PADDING
348 //
349 
350 class QUICPaddingFrame : public QUICFrame
351 {
352 public:
QUICPaddingFrame(size_t size)353   QUICPaddingFrame(size_t size) : _size(size) {}
354   QUICPaddingFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
355   virtual QUICFrameType type() const override;
356   virtual size_t size() const override;
357   virtual bool is_probing_frame() const override;
358   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
359   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
360 
361 private:
362   // padding frame is a resident of padding frames
363   // size indicate how many padding frames in this QUICPaddingFrame
364   size_t _size = 0;
365 };
366 
367 //
368 // CONNECTION_CLOSE
369 //
370 
371 class QUICConnectionCloseFrame : public QUICFrame
372 {
373 public:
QUICConnectionCloseFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)374   QUICConnectionCloseFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
375   QUICConnectionCloseFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
376   // Constructor for transport error codes
377   QUICConnectionCloseFrame(uint64_t error_code, QUICFrameType frame_type, uint64_t reason_phrase_length, const char *reason_phrase,
378                            QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
379   // Constructor for application protocol error codes
380   QUICConnectionCloseFrame(uint64_t error_code, uint64_t reason_phrase_length, const char *reason_phrase, QUICFrameId id = 0,
381                            QUICFrameGenerator *owner = nullptr);
382   virtual QUICFrameType type() const override;
383   virtual size_t size() const override;
384   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
385   virtual int debug_msg(char *msg, size_t msg_len) const override;
386   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
387 
388   uint16_t error_code() const;
389   QUICFrameType frame_type() const;
390   uint64_t reason_phrase_length() const;
391   const char *reason_phrase() const;
392 
393 private:
394   virtual void _reset() override;
395 
396   uint8_t _type = 0;
397   uint64_t _error_code;
398   QUICFrameType _frame_type      = QUICFrameType::UNKNOWN;
399   uint64_t _reason_phrase_length = 0;
400   const char *_reason_phrase     = nullptr;
401 };
402 
403 //
404 // MAX_DATA
405 //
406 
407 class QUICMaxDataFrame : public QUICFrame
408 {
409 public:
QUICMaxDataFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)410   QUICMaxDataFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
411   QUICMaxDataFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
412   QUICMaxDataFrame(uint64_t maximum_data, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
413   virtual QUICFrameType type() const override;
414   virtual size_t size() const override;
415   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
416   virtual int debug_msg(char *msg, size_t msg_len) const override;
417   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
418 
419   uint64_t maximum_data() const;
420 
421 private:
422   virtual void _reset() override;
423 
424   uint64_t _maximum_data = 0;
425 };
426 
427 //
428 // MAX_STREAM_DATA
429 //
430 
431 class QUICMaxStreamDataFrame : public QUICFrame
432 {
433 public:
QUICMaxStreamDataFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)434   QUICMaxStreamDataFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
435   QUICMaxStreamDataFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
436   QUICMaxStreamDataFrame(QUICStreamId stream_id, uint64_t maximum_stream_data, QUICFrameId id = 0,
437                          QUICFrameGenerator *owner = nullptr);
438   virtual QUICFrameType type() const override;
439   virtual size_t size() const override;
440   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
441   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
442   virtual int debug_msg(char *msg, size_t msg_len) const override;
443 
444   QUICStreamId stream_id() const;
445   uint64_t maximum_stream_data() const;
446 
447 private:
448   virtual void _reset() override;
449 
450   QUICStreamId _stream_id       = 0;
451   uint64_t _maximum_stream_data = 0;
452 };
453 
454 //
455 // MAX_STREAMS
456 //
457 
458 class QUICMaxStreamsFrame : public QUICFrame
459 {
460 public:
QUICMaxStreamsFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)461   QUICMaxStreamsFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
462   QUICMaxStreamsFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
463   QUICMaxStreamsFrame(QUICStreamId maximum_streams, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
464   virtual QUICFrameType type() const override;
465   virtual size_t size() const override;
466   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
467   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
468   uint64_t maximum_streams() const;
469 
470 private:
471   virtual void _reset() override;
472 
473   uint64_t _maximum_streams = 0;
474 };
475 
476 //
477 // BLOCKED
478 //
479 class QUICDataBlockedFrame : public QUICFrame
480 {
481 public:
QUICDataBlockedFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)482   QUICDataBlockedFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
483   QUICDataBlockedFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICDataBlockedFrame(QUICOffset offset,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)484   QUICDataBlockedFrame(QUICOffset offset, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
485     : QUICFrame(id, owner), _offset(offset){};
486 
487   virtual QUICFrameType type() const override;
488   virtual size_t size() const override;
489   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
490   virtual int debug_msg(char *msg, size_t msg_len) const override;
491   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
492 
493   QUICOffset offset() const;
494 
495 private:
496   virtual void _reset() override;
497 
498   QUICOffset _offset = 0;
499 };
500 
501 //
502 // STREAM_DATA_BLOCKED
503 //
504 
505 class QUICStreamDataBlockedFrame : public QUICFrame
506 {
507 public:
QUICStreamDataBlockedFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)508   QUICStreamDataBlockedFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
509   QUICStreamDataBlockedFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICStreamDataBlockedFrame(QUICStreamId s,QUICOffset o,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)510   QUICStreamDataBlockedFrame(QUICStreamId s, QUICOffset o, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
511     : QUICFrame(id, owner), _stream_id(s), _offset(o){};
512 
513   virtual QUICFrameType type() const override;
514   virtual size_t size() const override;
515   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
516   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
517   virtual int debug_msg(char *msg, size_t msg_len) const override;
518 
519   QUICStreamId stream_id() const;
520   QUICOffset offset() const;
521 
522 private:
523   virtual void _reset() override;
524 
525   QUICStreamId _stream_id = 0;
526   QUICOffset _offset      = 0;
527 };
528 
529 //
530 // STREAMS_BLOCKED
531 //
532 class QUICStreamIdBlockedFrame : public QUICFrame
533 {
534 public:
QUICStreamIdBlockedFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)535   QUICStreamIdBlockedFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
536   QUICStreamIdBlockedFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICStreamIdBlockedFrame(QUICStreamId s,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)537   QUICStreamIdBlockedFrame(QUICStreamId s, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
538     : QUICFrame(id, owner), _stream_id(s)
539   {
540   }
541   virtual QUICFrameType type() const override;
542   virtual size_t size() const override;
543   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
544   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
545 
546   QUICStreamId stream_id() const;
547 
548 private:
549   virtual void _reset() override;
550 
551   QUICStreamId _stream_id = 0;
552 };
553 
554 //
555 // NEW_CONNECTION_ID
556 //
557 
558 class QUICNewConnectionIdFrame : public QUICFrame
559 {
560 public:
QUICNewConnectionIdFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)561   QUICNewConnectionIdFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
562   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)563   QUICNewConnectionIdFrame(uint64_t seq, uint64_t ret, const QUICConnectionId &cid, QUICStatelessResetToken token,
564                            QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
565     : QUICFrame(id, owner), _sequence(seq), _retire_prior_to(ret), _connection_id(cid), _stateless_reset_token(token){};
566 
567   virtual QUICFrameType type() const override;
568   virtual size_t size() const override;
569   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
570   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
571   virtual int debug_msg(char *msg, size_t msg_len) const override;
572 
573   uint64_t sequence() const;
574   uint64_t retire_prior_to() const;
575   QUICConnectionId connection_id() const;
576   QUICStatelessResetToken stateless_reset_token() const;
577 
578 private:
579   virtual void _reset() override;
580 
581   uint64_t _sequence              = 0;
582   uint64_t _retire_prior_to       = 0;
583   QUICConnectionId _connection_id = QUICConnectionId::ZERO();
584   QUICStatelessResetToken _stateless_reset_token;
585 };
586 
587 //
588 // STOP_SENDING
589 //
590 
591 class QUICStopSendingFrame : public QUICFrame
592 {
593 public:
QUICStopSendingFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)594   QUICStopSendingFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
595   QUICStopSendingFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
596   QUICStopSendingFrame(QUICStreamId stream_id, QUICAppErrorCode error_code, QUICFrameId id = 0,
597                        QUICFrameGenerator *owner = nullptr);
598 
599   virtual QUICFrameType type() const override;
600   virtual size_t size() const override;
601   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
602   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
603 
604   QUICStreamId stream_id() const;
605   QUICAppErrorCode error_code() const;
606 
607 private:
608   virtual void _reset() override;
609 
610   QUICStreamId _stream_id      = 0;
611   QUICAppErrorCode _error_code = 0;
612 };
613 
614 //
615 // PATH_CHALLENGE
616 //
617 
618 class QUICPathChallengeFrame : public QUICFrame
619 {
620 public:
621   static constexpr uint8_t DATA_LEN = 8;
QUICPathChallengeFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)622   QUICPathChallengeFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
623   QUICPathChallengeFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICPathChallengeFrame(ats_unique_buf data,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)624   QUICPathChallengeFrame(ats_unique_buf data, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
625     : QUICFrame(id, owner), _data(std::move(data))
626   {
627   }
628   virtual QUICFrameType type() const override;
629   virtual size_t size() const override;
630   virtual bool is_probing_frame() const override;
631   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
632   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
633   virtual int debug_msg(char *msg, size_t msg_len) const override;
634 
635   const uint8_t *data() const;
636 
637 private:
638   virtual void _reset() override;
639 
640   ats_unique_buf _data = {nullptr};
641 };
642 
643 //
644 // PATH_RESPONSE
645 //
646 
647 class QUICPathResponseFrame : public QUICFrame
648 {
649 public:
650   static constexpr uint8_t DATA_LEN = 8;
QUICPathResponseFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)651   QUICPathResponseFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
652   QUICPathResponseFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICPathResponseFrame(ats_unique_buf data,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)653   QUICPathResponseFrame(ats_unique_buf data, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
654     : QUICFrame(id, owner), _data(std::move(data))
655   {
656   }
657   virtual QUICFrameType type() const override;
658   virtual size_t size() const override;
659   virtual bool is_probing_frame() const override;
660   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
661   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
662   virtual int debug_msg(char *msg, size_t msg_len) const override;
663 
664   const uint8_t *data() const;
665 
666 private:
667   virtual void _reset() override;
668 
669   ats_unique_buf _data = {nullptr};
670 };
671 
672 //
673 // NEW_TOKEN
674 //
675 
676 class QUICNewTokenFrame : public QUICFrame
677 {
678 public:
QUICNewTokenFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)679   QUICNewTokenFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
680   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)681   QUICNewTokenFrame(ats_unique_buf token, size_t token_length, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
682     : QUICFrame(id, owner), _token_length(token_length), _token(std::move(token))
683   {
684   }
685   virtual QUICFrameType type() const override;
686   virtual size_t size() const override;
687   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
688   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
689 
690   uint64_t token_length() const;
691   const uint8_t *token() const;
692 
693 private:
694   virtual void _reset() override;
695 
696   uint64_t _token_length = 0;
697   ats_unique_buf _token  = {nullptr};
698 };
699 
700 //
701 // RETIRE_CONNECTION_ID
702 //
703 
704 class QUICRetireConnectionIdFrame : public QUICFrame
705 {
706 public:
QUICRetireConnectionIdFrame(QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)707   QUICRetireConnectionIdFrame(QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr) : QUICFrame(id, owner) {}
708   QUICRetireConnectionIdFrame(const uint8_t *buf, size_t len, const QUICPacket *packet = nullptr);
QUICRetireConnectionIdFrame(uint64_t seq_num,QUICFrameId id=0,QUICFrameGenerator * owner=nullptr)709   QUICRetireConnectionIdFrame(uint64_t seq_num, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr)
710     : QUICFrame(id, owner), _seq_num(seq_num)
711   {
712   }
713   virtual QUICFrameType type() const override;
714   virtual size_t size() const override;
715   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
716   virtual void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
717   virtual int debug_msg(char *msg, size_t msg_len) const override;
718 
719   uint64_t seq_num() const;
720 
721 private:
722   virtual void _reset() override;
723 
724   uint64_t _seq_num = 0;
725 };
726 
727 //
728 // UNKNOWN
729 //
730 
731 class QUICUnknownFrame : public QUICFrame
732 {
733   QUICFrameType type() const override;
734   size_t size() const override;
735   virtual Ptr<IOBufferBlock> to_io_buffer_block(size_t limit) const override;
736   void parse(const uint8_t *buf, size_t len, const QUICPacket *packet) override;
737   int debug_msg(char *msg, size_t msg_len) const override;
738 };
739 
740 //
741 // QUICFrameFactory
742 //
743 class QUICFrameFactory
744 {
745 public:
746   /*
747    * This is used for creating a QUICFrame object based on received data.
748    */
749   static QUICFrame *create(uint8_t *buf, const uint8_t *src, size_t len, const QUICPacket *packet);
750 
751   /*
752    * This works almost the same as create() but it reuses created objects for performance.
753    * If you create a frame object which has the same frame type that you created before, the object will be reset by new data.
754    */
755   const QUICFrame &fast_create(const uint8_t *buf, size_t len, const QUICPacket *packet);
756 
757   /*
758    * Creates a STREAM frame.
759    * You have to make sure that the data size won't exceed the maximum size of QUIC packet.
760    */
761   static QUICStreamFrame *create_stream_frame(uint8_t *buf, Ptr<IOBufferBlock> &block, QUICStreamId stream_id, QUICOffset offset,
762                                               bool last = false, bool has_offset_field = true, bool has_length_field = true,
763                                               QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
764 
765   /*
766    * Creates a CRYPTO frame.
767    * You have to make sure that the data size won't exceed the maximum size of QUIC packet.
768    */
769   static QUICCryptoFrame *create_crypto_frame(uint8_t *buf, Ptr<IOBufferBlock> &block, QUICOffset offset, QUICFrameId id = 0,
770                                               QUICFrameGenerator *owner = nullptr);
771 
772   /*
773    * Creates a ACK frame.
774    * You shouldn't call this directly but through QUICAckFrameCreator because QUICAckFrameCreator manages packet numbers that we
775    * need to ack.
776    */
777   static QUICAckFrame *create_ack_frame(uint8_t *buf, QUICPacketNumber largest_acknowledged, uint64_t ack_delay,
778                                         uint64_t first_ack_block, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
779   /*
780    * Creates a CONNECTION_CLOSE frame.
781    */
782   static QUICConnectionCloseFrame *create_connection_close_frame(uint8_t *buf, uint16_t error_code, QUICFrameType frame_type,
783                                                                  uint16_t reason_phrase_length = 0,
784                                                                  const char *reason_phrase = nullptr, QUICFrameId id = 0,
785                                                                  QUICFrameGenerator *owner = nullptr);
786 
787   static QUICConnectionCloseFrame *create_connection_close_frame(uint8_t *buf, QUICConnectionError &error, QUICFrameId id = 0,
788                                                                  QUICFrameGenerator *owner = nullptr);
789 
790   /*
791    * Creates a MAX_DATA frame.
792    */
793   static QUICMaxDataFrame *create_max_data_frame(uint8_t *buf, uint64_t maximum_data, QUICFrameId id = 0,
794                                                  QUICFrameGenerator *owner = nullptr);
795 
796   /*
797  /  * Creates a MAX_STREAM_DATA frame.
798    */
799   static QUICMaxStreamDataFrame *create_max_stream_data_frame(uint8_t *buf, QUICStreamId stream_id, uint64_t maximum_stream_data,
800                                                               QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
801   /*
802    * Creates a MAX_STREAMS frame.
803    */
804   static QUICMaxStreamsFrame *create_max_streams_frame(uint8_t *buf, QUICStreamId maximum_streams, QUICFrameId id = 0,
805                                                        QUICFrameGenerator *owner = nullptr);
806 
807   /*
808    * Creates a PING frame
809    */
810   static QUICPingFrame *create_ping_frame(uint8_t *buf, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
811 
812   /*
813    * Creates a PATH_CHALLENGE frame
814    */
815   static QUICPathChallengeFrame *create_path_challenge_frame(uint8_t *buf, const uint8_t *data, QUICFrameId id = 0,
816                                                              QUICFrameGenerator *owner = nullptr);
817 
818   /*
819    * Creates a PATH_RESPONSE frame
820    */
821   static QUICPathResponseFrame *create_path_response_frame(uint8_t *buf, const uint8_t *data, QUICFrameId id = 0,
822                                                            QUICFrameGenerator *owner = nullptr);
823 
824   /*
825    * Creates a BLOCKED frame.
826    */
827   static QUICDataBlockedFrame *create_data_blocked_frame(uint8_t *buf, QUICOffset offset, QUICFrameId id = 0,
828                                                          QUICFrameGenerator *owner = nullptr);
829 
830   /*
831    * Creates a STREAM_DATA_BLOCKED frame.
832    */
833   static QUICStreamDataBlockedFrame *create_stream_data_blocked_frame(uint8_t *buf, QUICStreamId stream_id, QUICOffset offset,
834                                                                       QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
835 
836   /*
837    * Creates a STREAMS_BLOCKED frame.
838    */
839   static QUICStreamIdBlockedFrame *create_stream_id_blocked_frame(uint8_t *buf, QUICStreamId stream_id, QUICFrameId id = 0,
840                                                                   QUICFrameGenerator *owner = nullptr);
841 
842   /*
843    * Creates a RESET_STREAM frame.
844    */
845   static QUICRstStreamFrame *create_rst_stream_frame(uint8_t *buf, QUICStreamId stream_id, QUICAppErrorCode error_code,
846                                                      QUICOffset final_offset, QUICFrameId id = 0,
847                                                      QUICFrameGenerator *owner = nullptr);
848   static QUICRstStreamFrame *create_rst_stream_frame(uint8_t *buf, QUICStreamError &error, QUICFrameId id = 0,
849                                                      QUICFrameGenerator *owner = nullptr);
850 
851   /*
852    * Creates a STOP_SENDING frame.
853    */
854   static QUICStopSendingFrame *create_stop_sending_frame(uint8_t *buf, QUICStreamId stream_id, QUICAppErrorCode error_code,
855                                                          QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
856 
857   /*
858    * Creates a NEW_CONNECTION_ID frame.
859    */
860   static QUICNewConnectionIdFrame *create_new_connection_id_frame(uint8_t *buf, uint64_t sequence, uint64_t retire_prior_to,
861                                                                   QUICConnectionId connectoin_id,
862                                                                   QUICStatelessResetToken stateless_reset_token, QUICFrameId id = 0,
863                                                                   QUICFrameGenerator *owner = nullptr);
864 
865   /*
866    * Creates a NEW_TOKEN frame
867    */
868   static QUICNewTokenFrame *create_new_token_frame(uint8_t *buf, const QUICResumptionToken &token, QUICFrameId id = 0,
869                                                    QUICFrameGenerator *owner = nullptr);
870 
871   /*
872    * Creates a RETIRE_CONNECTION_ID frame
873    */
874   static QUICRetireConnectionIdFrame *create_retire_connection_id_frame(uint8_t *buf, uint64_t seq_num, QUICFrameId id = 0,
875                                                                         QUICFrameGenerator *owner = nullptr);
876 
877   /*
878    * Creates a PADDING frame
879    */
880   static QUICPaddingFrame *create_padding_frame(uint8_t *buf, size_t size, QUICFrameId id = 0, QUICFrameGenerator *owner = nullptr);
881 
882 private:
883   // FIXME Actual number of frame types is several but some of the values are not sequential.
884   QUICFrame *_reusable_frames[256] = {nullptr};
885   uint8_t _buf_for_fast_create[256 * QUICFrame::MAX_INSTANCE_SIZE];
886   QUICUnknownFrame _unknown_frame;
887 };
888 
889 class QUICFrameInfo
890 {
891 public:
QUICFrameInfo(QUICFrameId id,QUICFrameGenerator * generator)892   QUICFrameInfo(QUICFrameId id, QUICFrameGenerator *generator) : _id(id), _generator(generator) {}
893   QUICFrameId id() const;
894   QUICFrameGenerator *generated_by() const;
895 
896 private:
897   QUICFrameId _id = 0;
898   QUICFrameGenerator *_generator;
899 };
900