2021-08-15 14:36:43 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef SHRPX_QUIC_CONNECTION_HANDLER_H
|
|
|
|
#define SHRPX_QUIC_CONNECTION_HANDLER_H
|
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
2021-09-05 11:01:27 +02:00
|
|
|
#include <vector>
|
2021-08-15 14:36:43 +02:00
|
|
|
|
2021-08-16 08:11:18 +02:00
|
|
|
#include <ngtcp2/ngtcp2.h>
|
|
|
|
|
2021-09-05 11:01:27 +02:00
|
|
|
#include <ev.h>
|
|
|
|
|
2021-09-05 11:53:31 +02:00
|
|
|
#include "shrpx_quic.h"
|
2021-08-15 14:36:43 +02:00
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
struct UpstreamAddr;
|
|
|
|
class ClientHandler;
|
|
|
|
class Worker;
|
|
|
|
|
2021-09-05 11:01:27 +02:00
|
|
|
// CloseWait handles packets received in close-wait (draining or
|
|
|
|
// closing period).
|
|
|
|
struct CloseWait {
|
|
|
|
CloseWait(Worker *worker, std::vector<ngtcp2_cid> scids,
|
2021-09-17 10:52:05 +02:00
|
|
|
std::vector<uint8_t> pkt, ev_tstamp period);
|
2021-09-05 11:01:27 +02:00
|
|
|
~CloseWait();
|
|
|
|
|
|
|
|
int handle_packet(const UpstreamAddr *faddr, const Address &remote_addr,
|
2021-11-05 11:19:23 +01:00
|
|
|
const Address &local_addr, const ngtcp2_pkt_info &pi,
|
|
|
|
const uint8_t *data, size_t datalen);
|
2021-09-05 11:01:27 +02:00
|
|
|
|
|
|
|
Worker *worker;
|
|
|
|
// Source Connection IDs of the connection.
|
|
|
|
std::vector<ngtcp2_cid> scids;
|
2021-09-17 10:52:05 +02:00
|
|
|
// QUIC packet which is sent in response to the incoming packet. It
|
|
|
|
// might be empty.
|
|
|
|
std::vector<uint8_t> pkt;
|
2021-09-05 11:01:27 +02:00
|
|
|
// Close-wait (draining or closing period) timer.
|
|
|
|
ev_timer timer;
|
|
|
|
// The number of bytes received during close-wait period.
|
|
|
|
size_t bytes_recv;
|
|
|
|
// The number of bytes sent during close-wait period.
|
|
|
|
size_t bytes_sent;
|
|
|
|
// The number of packets received during close-wait period.
|
|
|
|
size_t num_pkts_recv;
|
|
|
|
// If the number of packets received reaches this number, send a
|
|
|
|
// QUIC packet.
|
|
|
|
size_t next_pkts_recv;
|
|
|
|
};
|
|
|
|
|
2021-08-15 14:36:43 +02:00
|
|
|
class QUICConnectionHandler {
|
|
|
|
public:
|
|
|
|
QUICConnectionHandler(Worker *worker);
|
|
|
|
~QUICConnectionHandler();
|
|
|
|
int handle_packet(const UpstreamAddr *faddr, const Address &remote_addr,
|
2021-11-05 11:19:23 +01:00
|
|
|
const Address &local_addr, const ngtcp2_pkt_info &pi,
|
|
|
|
const uint8_t *data, size_t datalen);
|
2021-08-26 10:11:19 +02:00
|
|
|
// Send Retry packet. |ini_dcid| is the destination Connection ID
|
|
|
|
// which appeared in Client Initial packet and its length is
|
|
|
|
// |dcidlen|. |ini_scid| is the source Connection ID which appeared
|
|
|
|
// in Client Initial packet and its length is |scidlen|.
|
|
|
|
int send_retry(const UpstreamAddr *faddr, uint32_t version,
|
|
|
|
const uint8_t *ini_dcid, size_t ini_dcidlen,
|
|
|
|
const uint8_t *ini_scid, size_t ini_scidlen,
|
2022-03-12 11:42:19 +01:00
|
|
|
const Address &remote_addr, const Address &local_addr,
|
|
|
|
size_t max_pktlen);
|
2021-08-26 10:31:56 +02:00
|
|
|
// Send Version Negotiation packet. |ini_dcid| is the destination
|
|
|
|
// Connection ID which appeared in Client Initial packet and its
|
|
|
|
// length is |dcidlen|. |ini_scid| is the source Connection ID
|
|
|
|
// which appeared in Client Initial packet and its length is
|
|
|
|
// |scidlen|.
|
2021-08-15 14:36:43 +02:00
|
|
|
int send_version_negotiation(const UpstreamAddr *faddr, uint32_t version,
|
2021-08-26 10:31:56 +02:00
|
|
|
const uint8_t *ini_dcid, size_t ini_dcidlen,
|
|
|
|
const uint8_t *ini_scid, size_t ini_scidlen,
|
2021-08-15 14:36:43 +02:00
|
|
|
const Address &remote_addr,
|
|
|
|
const Address &local_addr);
|
2021-08-17 14:48:11 +02:00
|
|
|
int send_stateless_reset(const UpstreamAddr *faddr, const uint8_t *dcid,
|
|
|
|
size_t dcidlen, const Address &remote_addr,
|
|
|
|
const Address &local_addr);
|
2021-08-31 06:23:52 +02:00
|
|
|
// Send Initial CONNECTION_CLOSE. |ini_dcid| is the destination
|
|
|
|
// Connection ID which appeared in Client Initial packet.
|
|
|
|
// |ini_scid| is the source Connection ID which appeared in Client
|
|
|
|
// Initial packet.
|
|
|
|
int send_connection_close(const UpstreamAddr *faddr, uint32_t version,
|
2021-09-17 11:33:23 +02:00
|
|
|
const ngtcp2_cid &ini_dcid,
|
|
|
|
const ngtcp2_cid &ini_scid,
|
2021-08-31 06:23:52 +02:00
|
|
|
const Address &remote_addr,
|
2022-03-12 11:42:19 +01:00
|
|
|
const Address &local_addr, uint64_t error_code,
|
|
|
|
size_t max_pktlen);
|
2021-08-16 08:11:18 +02:00
|
|
|
ClientHandler *handle_new_connection(const UpstreamAddr *faddr,
|
|
|
|
const Address &remote_addr,
|
|
|
|
const Address &local_addr,
|
2021-08-26 10:11:19 +02:00
|
|
|
const ngtcp2_pkt_hd &hd,
|
2021-08-26 11:01:53 +02:00
|
|
|
const ngtcp2_cid *odcid,
|
|
|
|
const uint8_t *token, size_t tokenlen);
|
2021-09-17 11:33:23 +02:00
|
|
|
void add_connection_id(const ngtcp2_cid &cid, ClientHandler *handler);
|
|
|
|
void remove_connection_id(const ngtcp2_cid &cid);
|
2021-08-15 14:36:43 +02:00
|
|
|
|
2021-09-05 11:01:27 +02:00
|
|
|
void add_close_wait(CloseWait *cw);
|
|
|
|
void remove_close_wait(const CloseWait *cw);
|
|
|
|
|
2021-09-05 12:23:50 +02:00
|
|
|
void on_stateless_reset_bucket_regen();
|
|
|
|
|
2021-08-15 14:36:43 +02:00
|
|
|
private:
|
|
|
|
Worker *worker_;
|
2021-09-05 11:53:31 +02:00
|
|
|
std::unordered_map<ngtcp2_cid, ClientHandler *> connections_;
|
|
|
|
std::unordered_map<ngtcp2_cid, CloseWait *> close_waits_;
|
2021-09-05 12:23:50 +02:00
|
|
|
ev_timer stateless_reset_bucket_regen_timer_;
|
|
|
|
size_t stateless_reset_bucket_;
|
2021-08-15 14:36:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_QUIC_CONNECTION_HANDLER_H
|