diff --git a/src/shrpx_quic.cc b/src/shrpx_quic.cc index 0e3fbb17..360b61c0 100644 --- a/src/shrpx_quic.cc +++ b/src/shrpx_quic.cc @@ -45,6 +45,10 @@ using namespace nghttp2; +bool operator==(const ngtcp2_cid &lhs, const ngtcp2_cid &rhs) { + return ngtcp2_cid_eq(&lhs, &rhs); +} + namespace shrpx { ngtcp2_tstamp quic_timestamp() { diff --git a/src/shrpx_quic.h b/src/shrpx_quic.h index 94483194..66254a91 100644 --- a/src/shrpx_quic.h +++ b/src/shrpx_quic.h @@ -29,8 +29,30 @@ #include +#include + #include +namespace std { +template <> struct hash { + std::size_t operator()(const ngtcp2_cid &cid) const noexcept { + // FNV-1a 64bits variant + constexpr uint64_t basis = 0xCBF29CE484222325ULL; + const uint8_t *p = cid.data, *end = cid.data + cid.datalen; + uint64_t h = basis; + + for (; p != end;) { + h ^= *p++; + h *= basis; + } + + return static_cast(h); + } +}; +} // namespace std + +bool operator==(const ngtcp2_cid &lhs, const ngtcp2_cid &rhs); + namespace shrpx { struct UpstreamAddr; diff --git a/src/shrpx_quic_connection_handler.cc b/src/shrpx_quic_connection_handler.cc index f583738d..1b21513d 100644 --- a/src/shrpx_quic_connection_handler.cc +++ b/src/shrpx_quic_connection_handler.cc @@ -32,7 +32,6 @@ #include "shrpx_worker.h" #include "shrpx_client_handler.h" #include "shrpx_log.h" -#include "shrpx_quic.h" #include "shrpx_http3_upstream.h" #include "shrpx_connection_handler.h" @@ -43,18 +42,6 @@ QUICConnectionHandler::QUICConnectionHandler(Worker *worker) QUICConnectionHandler::~QUICConnectionHandler() {} -namespace { -std::string make_cid_key(const uint8_t *dcid, size_t dcidlen) { - return std::string{dcid, dcid + dcidlen}; -} -} // namespace - -namespace { -std::string make_cid_key(const ngtcp2_cid *cid) { - return make_cid_key(cid->data, cid->datalen); -} -} // namespace - int QUICConnectionHandler::handle_packet(const UpstreamAddr *faddr, const Address &remote_addr, const Address &local_addr, @@ -80,7 +67,8 @@ int QUICConnectionHandler::handle_packet(const UpstreamAddr *faddr, auto config = get_config(); - auto dcid_key = make_cid_key(dcid, dcidlen); + ngtcp2_cid dcid_key; + ngtcp2_cid_init(&dcid_key, dcid, dcidlen); auto conn_handler = worker_->get_connection_handler(); @@ -462,24 +450,22 @@ int QUICConnectionHandler::send_connection_close( void QUICConnectionHandler::add_connection_id(const ngtcp2_cid *cid, ClientHandler *handler) { - auto key = make_cid_key(cid); - connections_.emplace(key, handler); + connections_.emplace(*cid, handler); } void QUICConnectionHandler::remove_connection_id(const ngtcp2_cid *cid) { - auto key = make_cid_key(cid); - connections_.erase(key); + connections_.erase(*cid); } void QUICConnectionHandler::add_close_wait(CloseWait *cw) { for (auto &cid : cw->scids) { - close_waits_.emplace(make_cid_key(&cid), cw); + close_waits_.emplace(cid, cw); } } void QUICConnectionHandler::remove_close_wait(const CloseWait *cw) { for (auto &cid : cw->scids) { - close_waits_.erase(make_cid_key(&cid)); + close_waits_.erase(cid); } } diff --git a/src/shrpx_quic_connection_handler.h b/src/shrpx_quic_connection_handler.h index b1888ef7..84530173 100644 --- a/src/shrpx_quic_connection_handler.h +++ b/src/shrpx_quic_connection_handler.h @@ -36,6 +36,7 @@ #include +#include "shrpx_quic.h" #include "network.h" using namespace nghttp2; @@ -127,8 +128,8 @@ public: private: Worker *worker_; - std::unordered_map connections_; - std::unordered_map close_waits_; + std::unordered_map connections_; + std::unordered_map close_waits_; }; } // namespace shrpx