12c593315Sopenharmony_ci/* 22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library 32c593315Sopenharmony_ci * 42c593315Sopenharmony_ci * Copyright (c) 2021 Tatsuhiro Tsujikawa 52c593315Sopenharmony_ci * 62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the 82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 122c593315Sopenharmony_ci * the following conditions: 132c593315Sopenharmony_ci * 142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be 152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software. 162c593315Sopenharmony_ci * 172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 242c593315Sopenharmony_ci */ 252c593315Sopenharmony_ci#ifndef SHRPX_QUIC_CONNECTION_HANDLER_H 262c593315Sopenharmony_ci#define SHRPX_QUIC_CONNECTION_HANDLER_H 272c593315Sopenharmony_ci 282c593315Sopenharmony_ci#include "shrpx.h" 292c593315Sopenharmony_ci 302c593315Sopenharmony_ci#include <memory> 312c593315Sopenharmony_ci#include <unordered_map> 322c593315Sopenharmony_ci#include <string> 332c593315Sopenharmony_ci#include <vector> 342c593315Sopenharmony_ci 352c593315Sopenharmony_ci#include <ngtcp2/ngtcp2.h> 362c593315Sopenharmony_ci 372c593315Sopenharmony_ci#include <ev.h> 382c593315Sopenharmony_ci 392c593315Sopenharmony_ci#include "shrpx_quic.h" 402c593315Sopenharmony_ci#include "network.h" 412c593315Sopenharmony_ci 422c593315Sopenharmony_ciusing namespace nghttp2; 432c593315Sopenharmony_ci 442c593315Sopenharmony_cinamespace shrpx { 452c593315Sopenharmony_ci 462c593315Sopenharmony_cistruct UpstreamAddr; 472c593315Sopenharmony_ciclass ClientHandler; 482c593315Sopenharmony_ciclass Worker; 492c593315Sopenharmony_ci 502c593315Sopenharmony_ci// CloseWait handles packets received in close-wait (draining or 512c593315Sopenharmony_ci// closing period). 522c593315Sopenharmony_cistruct CloseWait { 532c593315Sopenharmony_ci CloseWait(Worker *worker, std::vector<ngtcp2_cid> scids, 542c593315Sopenharmony_ci std::vector<uint8_t> pkt, ev_tstamp period); 552c593315Sopenharmony_ci ~CloseWait(); 562c593315Sopenharmony_ci 572c593315Sopenharmony_ci int handle_packet(const UpstreamAddr *faddr, const Address &remote_addr, 582c593315Sopenharmony_ci const Address &local_addr, const ngtcp2_pkt_info &pi, 592c593315Sopenharmony_ci const uint8_t *data, size_t datalen); 602c593315Sopenharmony_ci 612c593315Sopenharmony_ci Worker *worker; 622c593315Sopenharmony_ci // Source Connection IDs of the connection. 632c593315Sopenharmony_ci std::vector<ngtcp2_cid> scids; 642c593315Sopenharmony_ci // QUIC packet which is sent in response to the incoming packet. It 652c593315Sopenharmony_ci // might be empty. 662c593315Sopenharmony_ci std::vector<uint8_t> pkt; 672c593315Sopenharmony_ci // Close-wait (draining or closing period) timer. 682c593315Sopenharmony_ci ev_timer timer; 692c593315Sopenharmony_ci // The number of bytes received during close-wait period. 702c593315Sopenharmony_ci size_t bytes_recv; 712c593315Sopenharmony_ci // The number of bytes sent during close-wait period. 722c593315Sopenharmony_ci size_t bytes_sent; 732c593315Sopenharmony_ci // The number of packets received during close-wait period. 742c593315Sopenharmony_ci size_t num_pkts_recv; 752c593315Sopenharmony_ci // If the number of packets received reaches this number, send a 762c593315Sopenharmony_ci // QUIC packet. 772c593315Sopenharmony_ci size_t next_pkts_recv; 782c593315Sopenharmony_ci}; 792c593315Sopenharmony_ci 802c593315Sopenharmony_ciclass QUICConnectionHandler { 812c593315Sopenharmony_cipublic: 822c593315Sopenharmony_ci QUICConnectionHandler(Worker *worker); 832c593315Sopenharmony_ci ~QUICConnectionHandler(); 842c593315Sopenharmony_ci int handle_packet(const UpstreamAddr *faddr, const Address &remote_addr, 852c593315Sopenharmony_ci const Address &local_addr, const ngtcp2_pkt_info &pi, 862c593315Sopenharmony_ci const uint8_t *data, size_t datalen); 872c593315Sopenharmony_ci // Send Retry packet. |ini_dcid| is the destination Connection ID 882c593315Sopenharmony_ci // which appeared in Client Initial packet and its length is 892c593315Sopenharmony_ci // |dcidlen|. |ini_scid| is the source Connection ID which appeared 902c593315Sopenharmony_ci // in Client Initial packet and its length is |scidlen|. 912c593315Sopenharmony_ci int send_retry(const UpstreamAddr *faddr, uint32_t version, 922c593315Sopenharmony_ci const uint8_t *ini_dcid, size_t ini_dcidlen, 932c593315Sopenharmony_ci const uint8_t *ini_scid, size_t ini_scidlen, 942c593315Sopenharmony_ci const Address &remote_addr, const Address &local_addr, 952c593315Sopenharmony_ci size_t max_pktlen); 962c593315Sopenharmony_ci // Send Version Negotiation packet. |ini_dcid| is the destination 972c593315Sopenharmony_ci // Connection ID which appeared in Client Initial packet and its 982c593315Sopenharmony_ci // length is |dcidlen|. |ini_scid| is the source Connection ID 992c593315Sopenharmony_ci // which appeared in Client Initial packet and its length is 1002c593315Sopenharmony_ci // |scidlen|. 1012c593315Sopenharmony_ci int send_version_negotiation(const UpstreamAddr *faddr, uint32_t version, 1022c593315Sopenharmony_ci const uint8_t *ini_dcid, size_t ini_dcidlen, 1032c593315Sopenharmony_ci const uint8_t *ini_scid, size_t ini_scidlen, 1042c593315Sopenharmony_ci const Address &remote_addr, 1052c593315Sopenharmony_ci const Address &local_addr); 1062c593315Sopenharmony_ci int send_stateless_reset(const UpstreamAddr *faddr, const uint8_t *dcid, 1072c593315Sopenharmony_ci size_t dcidlen, const Address &remote_addr, 1082c593315Sopenharmony_ci const Address &local_addr); 1092c593315Sopenharmony_ci // Send Initial CONNECTION_CLOSE. |ini_dcid| is the destination 1102c593315Sopenharmony_ci // Connection ID which appeared in Client Initial packet. 1112c593315Sopenharmony_ci // |ini_scid| is the source Connection ID which appeared in Client 1122c593315Sopenharmony_ci // Initial packet. 1132c593315Sopenharmony_ci int send_connection_close(const UpstreamAddr *faddr, uint32_t version, 1142c593315Sopenharmony_ci const ngtcp2_cid &ini_dcid, 1152c593315Sopenharmony_ci const ngtcp2_cid &ini_scid, 1162c593315Sopenharmony_ci const Address &remote_addr, 1172c593315Sopenharmony_ci const Address &local_addr, uint64_t error_code, 1182c593315Sopenharmony_ci size_t max_pktlen); 1192c593315Sopenharmony_ci ClientHandler * 1202c593315Sopenharmony_ci handle_new_connection(const UpstreamAddr *faddr, const Address &remote_addr, 1212c593315Sopenharmony_ci const Address &local_addr, const ngtcp2_pkt_hd &hd, 1222c593315Sopenharmony_ci const ngtcp2_cid *odcid, const uint8_t *token, 1232c593315Sopenharmony_ci size_t tokenlen, ngtcp2_token_type token_type); 1242c593315Sopenharmony_ci void add_connection_id(const ngtcp2_cid &cid, ClientHandler *handler); 1252c593315Sopenharmony_ci void remove_connection_id(const ngtcp2_cid &cid); 1262c593315Sopenharmony_ci 1272c593315Sopenharmony_ci void add_close_wait(CloseWait *cw); 1282c593315Sopenharmony_ci void remove_close_wait(const CloseWait *cw); 1292c593315Sopenharmony_ci 1302c593315Sopenharmony_ci void on_stateless_reset_bucket_regen(); 1312c593315Sopenharmony_ci 1322c593315Sopenharmony_ciprivate: 1332c593315Sopenharmony_ci Worker *worker_; 1342c593315Sopenharmony_ci std::unordered_map<ngtcp2_cid, ClientHandler *> connections_; 1352c593315Sopenharmony_ci std::unordered_map<ngtcp2_cid, CloseWait *> close_waits_; 1362c593315Sopenharmony_ci ev_timer stateless_reset_bucket_regen_timer_; 1372c593315Sopenharmony_ci size_t stateless_reset_bucket_; 1382c593315Sopenharmony_ci}; 1392c593315Sopenharmony_ci 1402c593315Sopenharmony_ci} // namespace shrpx 1412c593315Sopenharmony_ci 1422c593315Sopenharmony_ci#endif // SHRPX_QUIC_CONNECTION_HANDLER_H 143