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 "tscore/ink_platform.h"
27 #include "P_Connection.h"
28 #include "P_NetAccept.h"
29 #include "quic/QUICTypes.h"
30 #include "quic/QUICConnectionTable.h"
31 #include "quic/QUICResetTokenTable.h"
32 
33 class QUICClosedConCollector;
34 class QUICNetVConnection;
35 class QUICPacket;
36 class QUICPacketHeaderProtector;
37 
38 class QUICPacketHandler
39 {
40 public:
41   QUICPacketHandler(QUICResetTokenTable &rtable);
42   ~QUICPacketHandler();
43 
44   void send_packet(const QUICPacket &packet, QUICNetVConnection *vc, const QUICPacketHeaderProtector &pn_protector);
45   void send_packet(QUICNetVConnection *vc, const Ptr<IOBufferBlock> &udp_payload);
46 
47   void close_connection(QUICNetVConnection *conn);
48 
49 protected:
50   void _send_packet(const QUICPacket &packet, UDPConnection *udp_con, IpEndpoint &addr, uint32_t pmtu,
51                     const QUICPacketHeaderProtector *ph_protector, int dcil);
52   void _send_packet(UDPConnection *udp_con, IpEndpoint &addr, Ptr<IOBufferBlock> udp_payload);
53   QUICConnection *_check_stateless_reset(const uint8_t *buf, size_t buf_len);
54 
55   // FIXME Remove this
56   // QUICPacketHandler could be a continuation, but NetAccept is a continuation too.
57   virtual Continuation *_get_continuation() = 0;
58 
59   Event *_collector_event                       = nullptr;
60   QUICClosedConCollector *_closed_con_collector = nullptr;
61 
62   virtual void _recv_packet(int event, UDPPacket *udpPacket) = 0;
63 
64   QUICResetTokenTable &_rtable;
65 };
66 
67 /*
68  * @class QUICPacketHandlerIn
69  * @brief QUIC Packet Handler for incoming connections
70  */
71 class QUICPacketHandlerIn : public NetAccept, public QUICPacketHandler
72 {
73 public:
74   QUICPacketHandlerIn(const NetProcessor::AcceptOptions &opt, QUICConnectionTable &ctable, QUICResetTokenTable &rtable);
75   ~QUICPacketHandlerIn();
76 
77   // NetAccept
78   virtual NetProcessor *getNetProcessor() const override;
79   virtual NetAccept *clone() const override;
80   virtual int acceptEvent(int event, void *e) override;
81   void init_accept(EThread *t) override;
82 
83 protected:
84   // QUICPacketHandler
85   Continuation *_get_continuation() override;
86 
87 private:
88   void _recv_packet(int event, UDPPacket *udp_packet) override;
89   int _stateless_retry(const uint8_t *buf, uint64_t buf_len, UDPConnection *connection, IpEndpoint from, QUICConnectionId dcid,
90                        QUICConnectionId scid, QUICConnectionId *original_cid, QUICConnectionId *retry_cid, QUICVersion version);
91   bool _send_stateless_reset(QUICConnectionId dcid, uint32_t instance_id, UDPConnection *udp_con, IpEndpoint &addr,
92                              size_t maximum_size);
93   void _send_invalid_token_error(const uint8_t *initial_packet, uint64_t initial_packet_len, UDPConnection *connection,
94                                  IpEndpoint from);
95 
96   QUICConnectionTable &_ctable;
97 };
98 
99 /*
100  * @class QUICPacketHandlerOut
101  * @brief QUIC Packet Handler for outgoing connections
102  */
103 class QUICPacketHandlerOut : public Continuation, public QUICPacketHandler
104 {
105 public:
106   QUICPacketHandlerOut(QUICResetTokenTable &rtable);
~QUICPacketHandlerOut()107   ~QUICPacketHandlerOut(){};
108 
109   void init(QUICNetVConnection *vc);
110   int event_handler(int event, Event *data);
111 
112 protected:
113   // QUICPacketHandler
114   Continuation *_get_continuation() override;
115 
116 private:
117   void _recv_packet(int event, UDPPacket *udp_packet) override;
118 
119   QUICNetVConnection *_vc = nullptr;
120 };
121