2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2021 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #ifndef SHRPX_QUIC_CONNECTION_HANDLER_H
26 #define SHRPX_QUIC_CONNECTION_HANDLER_H
31 #include <unordered_map>
35 #include <ngtcp2/ngtcp2.h>
39 #include "shrpx_quic.h"
42 using namespace nghttp2;
50 // CloseWait handles packets received in close-wait (draining or
53 CloseWait(Worker *worker, std::vector<ngtcp2_cid> scids,
54 std::vector<uint8_t> pkt, ev_tstamp period);
57 int handle_packet(const UpstreamAddr *faddr, const Address &remote_addr,
58 const Address &local_addr, const uint8_t *data,
62 // Source Connection IDs of the connection.
63 std::vector<ngtcp2_cid> scids;
64 // QUIC packet which is sent in response to the incoming packet. It
66 std::vector<uint8_t> pkt;
67 // Close-wait (draining or closing period) timer.
69 // The number of bytes received during close-wait period.
71 // The number of bytes sent during close-wait period.
73 // The number of packets received during close-wait period.
75 // If the number of packets received reaches this number, send a
77 size_t next_pkts_recv;
80 class QUICConnectionHandler {
82 QUICConnectionHandler(Worker *worker);
83 ~QUICConnectionHandler();
84 int handle_packet(const UpstreamAddr *faddr, const Address &remote_addr,
85 const Address &local_addr, const uint8_t *data,
87 // Send Retry packet. |ini_dcid| is the destination Connection ID
88 // which appeared in Client Initial packet and its length is
89 // |dcidlen|. |ini_scid| is the source Connection ID which appeared
90 // in Client Initial packet and its length is |scidlen|.
91 int send_retry(const UpstreamAddr *faddr, uint32_t version,
92 const uint8_t *ini_dcid, size_t ini_dcidlen,
93 const uint8_t *ini_scid, size_t ini_scidlen,
94 const Address &remote_addr, const Address &local_addr);
95 // Send Version Negotiation packet. |ini_dcid| is the destination
96 // Connection ID which appeared in Client Initial packet and its
97 // length is |dcidlen|. |ini_scid| is the source Connection ID
98 // which appeared in Client Initial packet and its length is
100 int send_version_negotiation(const UpstreamAddr *faddr, uint32_t version,
101 const uint8_t *ini_dcid, size_t ini_dcidlen,
102 const uint8_t *ini_scid, size_t ini_scidlen,
103 const Address &remote_addr,
104 const Address &local_addr);
105 int send_stateless_reset(const UpstreamAddr *faddr, const uint8_t *dcid,
106 size_t dcidlen, const Address &remote_addr,
107 const Address &local_addr);
108 // Send Initial CONNECTION_CLOSE. |ini_dcid| is the destination
109 // Connection ID which appeared in Client Initial packet.
110 // |ini_scid| is the source Connection ID which appeared in Client
112 int send_connection_close(const UpstreamAddr *faddr, uint32_t version,
113 const ngtcp2_cid &ini_dcid,
114 const ngtcp2_cid &ini_scid,
115 const Address &remote_addr,
116 const Address &local_addr, uint64_t error_code);
117 ClientHandler *handle_new_connection(const UpstreamAddr *faddr,
118 const Address &remote_addr,
119 const Address &local_addr,
120 const ngtcp2_pkt_hd &hd,
121 const ngtcp2_cid *odcid,
122 const uint8_t *token, size_t tokenlen);
123 void add_connection_id(const ngtcp2_cid &cid, ClientHandler *handler);
124 void remove_connection_id(const ngtcp2_cid &cid);
126 void add_close_wait(CloseWait *cw);
127 void remove_close_wait(const CloseWait *cw);
129 void on_stateless_reset_bucket_regen();
133 std::unordered_map<ngtcp2_cid, ClientHandler *> connections_;
134 std::unordered_map<ngtcp2_cid, CloseWait *> close_waits_;
135 ev_timer stateless_reset_bucket_regen_timer_;
136 size_t stateless_reset_bucket_;
141 #endif // SHRPX_QUIC_CONNECTION_HANDLER_H