diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 887ff03b7..9d4072c7c 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -107,7 +107,7 @@ namespace llarp net::IPRangeMap m_ExitMap; net::IPRangeMap m_LNSExitMap; - std::unordered_map m_ExitAuths; + std::unordered_map m_ExitAuths; std::unordered_map m_LNSExitAuths; std::unordered_map m_mapAddrs; @@ -115,7 +115,7 @@ namespace llarp service::AuthType m_AuthType = service::AuthType::eAuthTypeNone; std::optional m_AuthUrl; std::optional m_AuthMethod; - std::unordered_set m_AuthWhitelist; + std::unordered_set m_AuthWhitelist; std::vector m_SRVRecords; diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index 2c0d3711a..225be8278 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -243,3 +243,10 @@ namespace llarp /// SH(result, body) using shorthash_func = std::function; } // namespace llarp + +namespace std +{ + template <> + struct hash : hash> + {}; +}; // namespace std diff --git a/llarp/dht/context.hpp b/llarp/dht/context.hpp index 9d00beec9..1b184555f 100644 --- a/llarp/dht/context.hpp +++ b/llarp/dht/context.hpp @@ -34,9 +34,9 @@ namespace llarp struct AbstractContext { - using PendingIntrosetLookups = TXHolder; - using PendingRouterLookups = TXHolder; - using PendingExploreLookups = TXHolder; + using PendingIntrosetLookups = TXHolder; + using PendingRouterLookups = TXHolder; + using PendingExploreLookups = TXHolder; virtual ~AbstractContext() = 0; diff --git a/llarp/dht/txholder.hpp b/llarp/dht/txholder.hpp index 2c7100146..b17586174 100644 --- a/llarp/dht/txholder.hpp +++ b/llarp/dht/txholder.hpp @@ -13,16 +13,16 @@ namespace llarp { namespace dht { - template + template struct TXHolder { using TXPtr = std::unique_ptr>; // tx who are waiting for a reply for each key - std::unordered_multimap waiting; + std::unordered_multimap waiting; // tx timesouts by key - std::unordered_map timeouts; + std::unordered_map timeouts; // maps remote peer with tx to handle reply from them - std::unordered_map tx; + std::unordered_map tx; const TX* GetPendingLookupFrom(const TXOwner& owner) const; @@ -106,9 +106,9 @@ namespace llarp Expire(llarp_time_t now); }; - template + template const TX* - TXHolder::GetPendingLookupFrom(const TXOwner& owner) const + TXHolder::GetPendingLookupFrom(const TXOwner& owner) const { auto itr = tx.find(owner); if (itr == tx.end()) @@ -119,9 +119,9 @@ namespace llarp return itr->second.get(); } - template + template void - TXHolder::NewTX( + TXHolder::NewTX( const TXOwner& askpeer, const TXOwner& whoasked, const K& k, @@ -144,9 +144,9 @@ namespace llarp } } - template + template void - TXHolder::NotFound(const TXOwner& from, const std::unique_ptr&) + TXHolder::NotFound(const TXOwner& from, const std::unique_ptr&) { auto txitr = tx.find(from); if (txitr == tx.end()) @@ -156,9 +156,9 @@ namespace llarp Inform(from, txitr->second->target, {}, true, true); } - template + template void - TXHolder::Inform( + TXHolder::Inform( TXOwner from, K key, std::vector values, bool sendreply, bool removeTimeouts) { auto range = waiting.equal_range(key); @@ -192,9 +192,9 @@ namespace llarp } } - template + template void - TXHolder::Expire(llarp_time_t now) + TXHolder::Expire(llarp_time_t now) { auto itr = timeouts.begin(); while (itr != timeouts.end()) diff --git a/llarp/dht/txowner.hpp b/llarp/dht/txowner.hpp index d334064bb..e21c878a1 100644 --- a/llarp/dht/txowner.hpp +++ b/llarp/dht/txowner.hpp @@ -44,17 +44,21 @@ namespace llarp { return std::tie(txid, node) < std::tie(other.txid, other.node); } - - struct Hash - { - std::size_t - operator()(const TXOwner& o) const noexcept - { - std::size_t sz2; - memcpy(&sz2, o.node.data(), sizeof(std::size_t)); - return o.txid ^ (sz2 << 1); - } - }; }; } // namespace dht } // namespace llarp + +namespace std +{ + template <> + struct hash + { + std::size_t + operator()(const llarp::dht::TXOwner& o) const noexcept + { + std::size_t sz2; + memcpy(&sz2, o.node.data(), sizeof(std::size_t)); + return o.txid ^ (sz2 << 1); + } + }; +} // namespace std diff --git a/llarp/exit/context.hpp b/llarp/exit/context.hpp index cfc71ea97..91398efa9 100644 --- a/llarp/exit/context.hpp +++ b/llarp/exit/context.hpp @@ -39,7 +39,7 @@ namespace llarp FindEndpointForPath(const PathID_t& path) const; /// calculate (pk, tx, rx) for all exit traffic - using TrafficStats = std::unordered_map, PubKey::Hash>; + using TrafficStats = std::unordered_map>; void CalculateExitTraffic(TrafficStats& stats); diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index cc408a3f7..7b066a2e4 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -134,13 +134,13 @@ namespace llarp bool m_ShouldInitTun; std::string m_Name; bool m_PermitExit; - std::unordered_map m_Paths; + std::unordered_map m_Paths; - std::unordered_map m_ChosenExits; + std::unordered_map m_ChosenExits; - std::unordered_multimap, PubKey::Hash> m_ActiveExits; + std::unordered_multimap> m_ActiveExits; - using KeyMap_t = std::unordered_map; + using KeyMap_t = std::unordered_map; KeyMap_t m_KeyToIP; @@ -148,8 +148,7 @@ namespace llarp /// set of pubkeys we treat as snodes SNodes_t m_SNodeKeys; - using SNodeSessions_t = - std::unordered_map, RouterID::Hash>; + using SNodeSessions_t = std::unordered_map>; /// snode sessions we are talking to directly SNodeSessions_t m_SNodeSessions; diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 2cf56835d..43ca3d490 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -61,8 +61,8 @@ namespace llarp hdr->ttl = 64; hdr->frag_off = htons(0b0100000000000000); - hdr->saddr = from.getIPv4(); - hdr->daddr = to.getIPv4(); + hdr->saddr = from.getIPv4().n; + hdr->daddr = to.getIPv4().n; // make udp packet uint8_t* ptr = pkt.buf + 20; @@ -98,8 +98,8 @@ namespace llarp const uint8_t* ptr = pkt.buf + ip_header_size; const auto dst = ToNet(pkt.dstv4()); const auto src = ToNet(pkt.srcv4()); - const SockAddr laddr{src.n, *reinterpret_cast(ptr)}; - const SockAddr raddr{dst.n, *reinterpret_cast(ptr + 2)}; + const SockAddr laddr{src, nuint16_t{*reinterpret_cast(ptr)}}; + const SockAddr raddr{dst, nuint16_t{*reinterpret_cast(ptr + 2)}}; OwnedBuffer buf{pkt.sz - (udp_header_size + ip_header_size)}; std::copy_n(ptr + udp_header_size, buf.sz, buf.buf.get()); @@ -931,7 +931,7 @@ namespace llarp { (void)ip; SendToServiceOrQueue( - service::Address{addr.as_array()}, pkt.ConstBuffer(), service::eProtocolExit); + service::Address{addr.as_array()}, pkt.ConstBuffer(), service::ProtocolType::Exit); } return; } @@ -966,7 +966,7 @@ namespace llarp { ctx->sendTimeout = 5s; } - self->SendToServiceOrQueue(addr, pkt.ConstBuffer(), service::eProtocolExit); + self->SendToServiceOrQueue(addr, pkt.ConstBuffer(), service::ProtocolType::Exit); }, 1s); } @@ -989,7 +989,7 @@ namespace llarp this, service::Address(itr->second.as_array()), std::placeholders::_1, - service::eProtocolExit); + service::ProtocolType::Exit); } else { @@ -1025,8 +1025,8 @@ namespace llarp service::ProtocolType t, uint64_t seqno) { - if (t != service::eProtocolTrafficV4 && t != service::eProtocolTrafficV6 - && t != service::eProtocolExit) + if (t != service::ProtocolType::TrafficV4 && t != service::ProtocolType::TrafficV6 + && t != service::ProtocolType::Exit) return false; AlignedBuffer<32> addr; bool snode = false; @@ -1042,7 +1042,7 @@ namespace llarp { // exit side from exit src = ObtainIPForAddr(addr, snode); - if (t == service::eProtocolExit) + if (t == service::ProtocolType::Exit) { if (pkt.IsV4()) dst = pkt.dst4to6(); @@ -1058,7 +1058,7 @@ namespace llarp dst = m_OurIP; } } - else if (t == service::eProtocolExit) + else if (t == service::ProtocolType::Exit) { // client side exit traffic from exit if (pkt.IsV4()) diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index bb6d6fe9a..31399e86a 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -200,11 +200,11 @@ namespace llarp /// maps ip to key (host byte order) std::unordered_map> m_IPToAddr; /// maps key to ip (host byte order) - std::unordered_map, huint128_t, AlignedBuffer<32>::Hash> m_AddrToIP; + std::unordered_map, huint128_t> m_AddrToIP; /// maps key to true if key is a service node, maps key to false if key is /// a hidden service - std::unordered_map, bool, AlignedBuffer<32>::Hash> m_SNodes; + std::unordered_map, bool> m_SNodes; private: template diff --git a/llarp/iwp/linklayer.hpp b/llarp/iwp/linklayer.hpp index 0bdd5d7f5..52d81ee2e 100644 --- a/llarp/iwp/linklayer.hpp +++ b/llarp/iwp/linklayer.hpp @@ -61,8 +61,8 @@ namespace llarp::iwp HandleWakeupPlaintext(); const std::shared_ptr m_Wakeup; - std::unordered_map, SockAddr::Hash> m_PlaintextRecv; - std::unordered_map m_AuthedAddrs; + std::unordered_map> m_PlaintextRecv; + std::unordered_map m_AuthedAddrs; const bool permitInbound; }; diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index a680488ac..0d987c3b1 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -296,7 +296,7 @@ namespace llarp bool LinkManager::GetRandomConnectedRouter(RouterContact& router) const { - std::unordered_map connectedRouters; + std::unordered_map connectedRouters; ForEachPeer( [&connectedRouters](const ILinkSession* peer, bool unused) { diff --git a/llarp/link/link_manager.hpp b/llarp/link/link_manager.hpp index 8cf7d58ba..e05debb1e 100644 --- a/llarp/link/link_manager.hpp +++ b/llarp/link/link_manager.hpp @@ -104,10 +104,9 @@ namespace llarp LinkSet inboundLinks; // sessions to persist -> timestamp to end persist at - std::unordered_map m_PersistingSessions - GUARDED_BY(_mutex); + std::unordered_map m_PersistingSessions GUARDED_BY(_mutex); - std::unordered_map m_lastRouterStats; + std::unordered_map m_lastRouterStats; IOutboundSessionMaker* _sessionMaker; }; diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index 78f8099fe..6e40b0df4 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -172,7 +172,7 @@ namespace llarp void ILinkLayer::Pump() { - std::unordered_set closedSessions; + std::unordered_set closedSessions; std::vector> closedPending; auto _now = Now(); { diff --git a/llarp/link/server.hpp b/llarp/link/server.hpp index 6fe75c68b..cc3b5e84e 100644 --- a/llarp/link/server.hpp +++ b/llarp/link/server.hpp @@ -244,16 +244,14 @@ namespace llarp std::shared_ptr m_udp; SecretKey m_SecretKey; - using AuthedLinks = - std::unordered_multimap, RouterID::Hash>; - using Pending = - std::unordered_multimap, SockAddr::Hash>; + using AuthedLinks = std::unordered_multimap>; + using Pending = std::unordered_multimap>; mutable DECLARE_LOCK(Mutex_t, m_AuthedLinksMutex, ACQUIRED_BEFORE(m_PendingMutex)); AuthedLinks m_AuthedLinks GUARDED_BY(m_AuthedLinksMutex); mutable DECLARE_LOCK(Mutex_t, m_PendingMutex, ACQUIRED_AFTER(m_AuthedLinksMutex)); Pending m_Pending GUARDED_BY(m_PendingMutex); - std::unordered_map m_RecentlyClosed; + std::unordered_map m_RecentlyClosed; private: std::shared_ptr m_repeater_keepalive; diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index 0a972b011..5851ad07c 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -49,15 +49,6 @@ namespace llarp std::ostream& print(std::ostream& stream, int level, int spaces) const; - - struct Hash - { - size_t - operator()(const AddressInfo& addr) const - { - return AlignedBuffer::Hash()(addr.pubkey); - } - }; }; void @@ -76,3 +67,16 @@ namespace llarp operator<(const AddressInfo& lhs, const AddressInfo& rhs); } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::AddressInfo& addr) const + { + return std::hash{}(addr.pubkey); + } + }; +} // namespace std diff --git a/llarp/net/ip_address.hpp b/llarp/net/ip_address.hpp index 3e7139645..2414f4f8e 100644 --- a/llarp/net/ip_address.hpp +++ b/llarp/net/ip_address.hpp @@ -132,7 +132,7 @@ namespace llarp // TODO: other utility functions left over from Addr which may be useful // IsBogon() const; // isPrivate() const; - // struct Hash + // std::hash // to string / stream / etc bool @@ -141,15 +141,6 @@ namespace llarp bool operator==(const IpAddress& other) const; - struct Hash - { - std::size_t - operator()(const IpAddress& address) const noexcept - { - return std::hash{}(address.toString()); - } - }; - private: bool m_empty = true; std::string m_ipAddress; @@ -160,3 +151,16 @@ namespace llarp operator<<(std::ostream& out, const IpAddress& address); } // namespace llarp + +namespace std +{ + template <> + struct hash + { + std::size_t + operator()(const llarp::IpAddress& address) const noexcept + { + return std::hash{}(address.toString()); + } + }; +} // namespace std diff --git a/llarp/net/ip_packet.hpp b/llarp/net/ip_packet.hpp index b013b7f36..6375bbb3a 100644 --- a/llarp/net/ip_packet.hpp +++ b/llarp/net/ip_packet.hpp @@ -226,11 +226,11 @@ namespace llarp ServiceProtocol() const { if (IsV4()) - return service::eProtocolTrafficV4; + return service::ProtocolType::TrafficV4; if (IsV6()) - return service::eProtocolTrafficV6; + return service::ProtocolType::TrafficV6; - return service::eProtocolControl; + return service::ProtocolType::Control; } huint128_t diff --git a/llarp/net/net_int.cpp b/llarp/net/net_int.cpp index 8db35d889..caecb7ab4 100644 --- a/llarp/net/net_int.cpp +++ b/llarp/net/net_int.cpp @@ -4,27 +4,42 @@ namespace llarp { - template <> + huint16_t + ToHost(nuint16_t n) + { + return xntohs(n); + } + huint32_t ToHost(nuint32_t n) { return xntohl(n); } - template <> + huint128_t + ToHost(nuint128_t n) + { + return {ntoh128(n.n)}; + } + nuint16_t ToNet(huint16_t h) { return xhtons(h); } - template <> nuint32_t ToNet(huint32_t h) { return xhtonl(h); } + nuint128_t + ToNet(huint128_t h) + { + return {hton128(h.h)}; + } + template <> void huint32_t::ToV6(V6Container& c) diff --git a/llarp/net/net_int.hpp b/llarp/net/net_int.hpp index 2faf244e6..61d5ff589 100644 --- a/llarp/net/net_int.hpp +++ b/llarp/net/net_int.hpp @@ -220,14 +220,13 @@ namespace llarp return huint16_t{ntohs(x.n)}; } - template - huint_t - ToHost(nuint_t h); - - template - nuint_t - ToNet(huint_t h); + huint16_t ToHost(nuint16_t); + huint32_t ToHost(nuint32_t); + huint128_t ToHost(nuint128_t); + nuint16_t ToNet(huint16_t); + nuint32_t ToNet(huint32_t); + nuint128_t ToNet(huint128_t); } // namespace llarp namespace std diff --git a/llarp/net/sock_addr.cpp b/llarp/net/sock_addr.cpp index d8a9644fc..37d3a3586 100644 --- a/llarp/net/sock_addr.cpp +++ b/llarp/net/sock_addr.cpp @@ -39,23 +39,32 @@ namespace llarp init(); } - SockAddr::SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d) + SockAddr::SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, huint16_t port) { init(); setIPv4(a, b, c, d); + setPort(port); } - SockAddr::SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port) - : SockAddr{a, b, c, d} + SockAddr::SockAddr(nuint32_t ip, nuint16_t port) { + init(); + setIPv4(ip); setPort(port); } - SockAddr::SockAddr(uint32_t ip, uint16_t port) + SockAddr::SockAddr(huint128_t ip, huint16_t port) { init(); - setIPv4(ip); - setPort(ntohs(port)); + setIPv6(ip); + setPort(port); + } + + SockAddr::SockAddr(nuint128_t ip, nuint16_t port) + { + init(); + setIPv6(ip); + setPort(port); } SockAddr::SockAddr(std::string_view addr) @@ -66,13 +75,13 @@ namespace llarp SockAddr::SockAddr(std::string_view addr, uint16_t port) { init(); - setPort(port); + setPort(huint16_t{port}); fromString(addr, false); } SockAddr::SockAddr(const AddressInfo& info) : SockAddr{info.ip} { - setPort(info.port); + setPort(huint16_t{info.port}); } SockAddr::SockAddr(const SockAddr& other) @@ -138,12 +147,14 @@ namespace llarp memcpy(&m_addr, &other, sizeof(sockaddr_in6)); if (ipv6_is_mapped_ipv4(other.sin6_addr)) + { setIPv4( other.sin6_addr.s6_addr[12], other.sin6_addr.s6_addr[13], other.sin6_addr.s6_addr[14], other.sin6_addr.s6_addr[15]); - setPort(ntohs(other.sin6_port)); + m_addr4.sin_port = m_addr.sin6_port; + } m_empty = false; return *this; @@ -161,7 +172,10 @@ namespace llarp memcpy(&m_addr.sin6_addr.s6_addr, &other.s6_addr, sizeof(m_addr.sin6_addr.s6_addr)); if (ipv6_is_mapped_ipv4(other)) + { setIPv4(other.s6_addr[12], other.s6_addr[13], other.s6_addr[14], other.s6_addr[15]); + m_addr4.sin_port = m_addr.sin6_port; + } m_empty = false; return *this; @@ -302,14 +316,14 @@ namespace llarp return m_empty; } - uint32_t + nuint32_t SockAddr::getIPv4() const { - return m_addr4.sin_addr.s_addr; + return {m_addr4.sin_addr.s_addr}; } void - SockAddr::setIPv4(uint32_t ip) + SockAddr::setIPv4(nuint32_t ip) { uint8_t* ip6 = m_addr.sin6_addr.s6_addr; llarp::Zero(ip6, sizeof(m_addr.sin6_addr.s6_addr)); @@ -317,10 +331,16 @@ namespace llarp applyIPv4MapBytes(); std::memcpy(ip6 + 12, &ip, 4); - m_addr4.sin_addr.s_addr = ip; + m_addr4.sin_addr.s_addr = ip.n; m_empty = false; } + void + SockAddr::setIPv4(huint32_t ip) + { + setIPv4(ToNet(ip)); + } + void SockAddr::setIPv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { @@ -339,10 +359,37 @@ namespace llarp } void - SockAddr::setPort(uint16_t port) + SockAddr::setIPv6(huint128_t ip) + { + return setIPv6(ToNet(ip)); + } + + void + SockAddr::setIPv6(nuint128_t ip) + { + std::memcpy(&m_addr.sin6_addr, &ip, sizeof(m_addr.sin6_addr)); + if (ipv6_is_mapped_ipv4(m_addr.sin6_addr)) + { + setIPv4( + m_addr.sin6_addr.s6_addr[12], + m_addr.sin6_addr.s6_addr[13], + m_addr.sin6_addr.s6_addr[14], + m_addr.sin6_addr.s6_addr[15]); + m_addr4.sin_port = m_addr.sin6_port; + } + } + + void + SockAddr::setPort(nuint16_t port) + { + m_addr.sin6_port = port.n; + m_addr4.sin_port = port.n; + } + + void + SockAddr::setPort(huint16_t port) { - m_addr.sin6_port = htons(port); - m_addr4.sin_port = htons(port); + setPort(ToNet(port)); } uint16_t diff --git a/llarp/net/sock_addr.hpp b/llarp/net/sock_addr.hpp index 336ef426b..303d71752 100644 --- a/llarp/net/sock_addr.hpp +++ b/llarp/net/sock_addr.hpp @@ -27,11 +27,20 @@ namespace llarp struct SockAddr { SockAddr(); - SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d); - SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port); + // IPv4 constructors: + SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, huint16_t port = {0}); + SockAddr(nuint32_t ip, nuint16_t port); + SockAddr(huint32_t ip, huint16_t port); + + // IPv6 (or IPv4 if given a special IPv4-mapped IPv6 addr) in host order (including port). + SockAddr(huint128_t ip, huint16_t port = {0}); + // IPv6 (or IPv4 if given a special IPv4-mapped IPv6 addr) in network order. NB: port is also + // in network order! + SockAddr(nuint128_t ip, nuint16_t port = {0}); + + // String ctors SockAddr(std::string_view addr); - SockAddr(std::string_view addr, uint16_t port); - SockAddr(uint32_t ip, uint16_t port); + SockAddr(std::string_view addr, uint16_t port); // port is in native (host) order SockAddr(const AddressInfo&); @@ -81,37 +90,47 @@ namespace llarp void setIPv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d); - /// port is in host order void - setIPv4(uint32_t ip); + setIPv4(nuint32_t ip); void - setPort(uint16_t port); + setIPv4(huint32_t ip); - /// port is in host order + void + setIPv6(huint128_t ip); + + void + setIPv6(nuint128_t ip); + + void + setPort(huint16_t port); + + void + setPort(nuint16_t port); + + // Port is a native (host) value + void + setPort(uint16_t port) + { + setPort(huint16_t{port}); + } + + /// port is always returned in native (host) order uint16_t getPort() const; - huint128_t - asIPv6() const; /// in network order - uint32_t + nuint128_t + getIPv6() const; + nuint32_t getIPv4() const; + /// in host order + huint128_t + asIPv6() const; huint32_t asIPv4() const; - struct Hash - { - size_t - operator()(const SockAddr& addr) const noexcept - { - const std::hash port{}; - const std::hash ip{}; - return (port(addr.getPort()) << 3) ^ ip(addr.asIPv6()); - } - }; - private: bool m_empty = true; sockaddr_in6 m_addr; @@ -128,3 +147,18 @@ namespace llarp operator<<(std::ostream& out, const SockAddr& address); } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::SockAddr& addr) const noexcept + { + const std::hash port{}; + const std::hash ip{}; + return (port(addr.getPort()) << 3) ^ ip(addr.asIPv6()); + } + }; +} // namespace std diff --git a/llarp/net/uint128.hpp b/llarp/net/uint128.hpp index 33aa8bd96..aa1c96d08 100644 --- a/llarp/net/uint128.hpp +++ b/llarp/net/uint128.hpp @@ -314,3 +314,9 @@ ntoh128(llarp::uint128_t i) return {loSwapped, hiSwapped}; #endif } + +inline llarp::uint128_t +hton128(llarp::uint128_t i) +{ + return ntoh128(i); // Same bit flipping as n-to-h +} diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 867673a99..5253f5f26 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -115,16 +115,16 @@ namespace llarp const auto& tx = p.hops[0].txID; const auto& rx = p.hops[0].rxID; const auto& r = p.hops[0].upstream; - const size_t rhash = std::accumulate(r.begin(), r.end(), 0, std::bit_xor()); + const size_t rhash = std::accumulate(r.begin(), r.end(), 0, std::bit_xor{}); return std::accumulate( rx.begin(), rx.begin(), - std::accumulate(tx.begin(), tx.end(), rhash, std::bit_xor()), - std::bit_xor()); + std::accumulate(tx.begin(), tx.end(), rhash, std::bit_xor{}), + std::bit_xor{}); } }; - /// hash for std::shared_ptr + /// hash for std::shared_ptr struct Ptr_Hash { size_t @@ -136,7 +136,7 @@ namespace llarp } }; - /// hash for std::shared_ptr by path endpoint + /// hash for std::shared_ptr by path endpoint struct Endpoint_Hash { size_t @@ -144,7 +144,7 @@ namespace llarp { if (p == nullptr) return 0; - return RouterID::Hash{}(p->Endpoint()); + return std::hash{}(p->Endpoint()); } }; diff --git a/llarp/path/path_context.hpp b/llarp/path/path_context.hpp index a0eec0a5a..ad3c7f204 100644 --- a/llarp/path/path_context.hpp +++ b/llarp/path/path_context.hpp @@ -109,7 +109,7 @@ namespace llarp void RemovePathSet(PathSet_ptr set); - using TransitHopsMap_t = std::unordered_multimap; + using TransitHopsMap_t = std::unordered_multimap; struct SyncTransitMap_t { @@ -129,7 +129,7 @@ namespace llarp }; // maps path id -> pathset owner of path - using OwnedPathsMap_t = std::unordered_map; + using OwnedPathsMap_t = std::unordered_map; struct SyncOwnedPathsMap_t { diff --git a/llarp/path/path_types.hpp b/llarp/path/path_types.hpp index eb3bc5306..917379ad4 100644 --- a/llarp/path/path_types.hpp +++ b/llarp/path/path_types.hpp @@ -6,8 +6,13 @@ namespace llarp { struct PathID_t final : public AlignedBuffer - { - using Hash = AlignedBuffer::Hash; - }; + {}; } // namespace llarp + +namespace std +{ + template <> + struct hash : hash> + {}; +} // namespace std diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 6105bc5b4..5cb787053 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -13,6 +13,19 @@ #include #include +namespace std +{ + template <> + struct hash> + { + size_t + operator()(const std::pair& i) const + { + return hash{}(i.first) ^ hash{}(i.second); + } + }; +} // namespace std + namespace llarp { struct RouterContact; @@ -294,29 +307,9 @@ namespace llarp void TickPaths(AbstractRouter* r); - using PathInfo_t = std::pair; - - struct PathInfoHash - { - size_t - operator()(const PathInfo_t& i) const - { - return RouterID::Hash()(i.first) ^ PathID_t::Hash()(i.second); - } - }; - - struct PathInfoEquals - { - bool - operator()(const PathInfo_t& left, const PathInfo_t& right) const - { - return left.first == right.first && left.second == right.second; - } - }; - using Mtx_t = util::NullMutex; using Lock_t = util::NullLock; - using PathMap_t = std::unordered_map; + using PathMap_t = std::unordered_map, Path_ptr>; mutable Mtx_t m_PathsMutex; PathMap_t m_Paths; }; diff --git a/llarp/path/transit_hop.hpp b/llarp/path/transit_hop.hpp index 052b8b9a1..315cf83d8 100644 --- a/llarp/path/transit_hop.hpp +++ b/llarp/path/transit_hop.hpp @@ -30,28 +30,6 @@ namespace llarp std::ostream& print(std::ostream& stream, int level, int spaces) const; - - struct PathIDHash - { - std::size_t - operator()(const PathID_t& a) const - { - return AlignedBuffer::Hash()(a); - } - }; - - struct Hash - { - std::size_t - operator()(TransitHopInfo const& a) const - { - std::size_t idx0 = RouterID::Hash()(a.upstream); - std::size_t idx1 = RouterID::Hash()(a.downstream); - std::size_t idx2 = PathIDHash()(a.txID); - std::size_t idx3 = PathIDHash()(a.rxID); - return idx0 ^ idx1 ^ idx2 ^ idx3; - } - }; }; inline bool @@ -235,3 +213,18 @@ namespace llarp } } // namespace path } // namespace llarp + +namespace std +{ + template <> + struct hash + { + std::size_t + operator()(llarp::path::TransitHopInfo const& a) const + { + hash RHash{}; + hash PHash{}; + return RHash(a.upstream) ^ RHash(a.downstream) ^ PHash(a.txID) ^ PHash(a.rxID); + } + }; +} // namespace std diff --git a/llarp/peerstats/peer_db.hpp b/llarp/peerstats/peer_db.hpp index ecbb739ab..b669b233c 100644 --- a/llarp/peerstats/peer_db.hpp +++ b/llarp/peerstats/peer_db.hpp @@ -127,7 +127,7 @@ namespace llarp ExtractStatus() const; private: - std::unordered_map m_peerStats; + std::unordered_map m_peerStats; mutable std::mutex m_statsLock; std::unique_ptr m_storage; diff --git a/llarp/router/outbound_message_handler.hpp b/llarp/router/outbound_message_handler.hpp index 95c31a95b..8769b80d6 100644 --- a/llarp/router/outbound_message_handler.hpp +++ b/llarp/router/outbound_message_handler.hpp @@ -128,10 +128,9 @@ namespace llarp mutable util::Mutex _mutex; // protects pendingSessionMessageQueues - std::unordered_map pendingSessionMessageQueues - GUARDED_BY(_mutex); + std::unordered_map pendingSessionMessageQueues GUARDED_BY(_mutex); - std::unordered_map outboundMessageQueues; + std::unordered_map outboundMessageQueues; std::queue roundRobinOrder; diff --git a/llarp/router/outbound_session_maker.hpp b/llarp/router/outbound_session_maker.hpp index da52fd474..1906868ee 100644 --- a/llarp/router/outbound_session_maker.hpp +++ b/llarp/router/outbound_session_maker.hpp @@ -100,11 +100,10 @@ namespace llarp mutable util::Mutex _mutex; // protects pendingSessions, pendingCallbacks - std::unordered_map, RouterID::Hash> pendingSessions + std::unordered_map> pendingSessions GUARDED_BY(_mutex); - std::unordered_map pendingCallbacks - GUARDED_BY(_mutex); + std::unordered_map pendingCallbacks GUARDED_BY(_mutex); AbstractRouter* _router = nullptr; ILinkManager* _linkManager = nullptr; diff --git a/llarp/router/rc_lookup_handler.hpp b/llarp/router/rc_lookup_handler.hpp index 8cc7cafe1..a3b88e924 100644 --- a/llarp/router/rc_lookup_handler.hpp +++ b/llarp/router/rc_lookup_handler.hpp @@ -114,8 +114,7 @@ namespace llarp std::set _bootstrapRCList; std::unordered_set _bootstrapRouterIDList; - std::unordered_map pendingCallbacks - GUARDED_BY(_mutex); + std::unordered_map pendingCallbacks GUARDED_BY(_mutex); bool useWhitelist = false; bool isServiceNode = false; @@ -123,7 +122,7 @@ namespace llarp std::unordered_set whitelistRouters GUARDED_BY(_mutex); using TimePoint = std::chrono::steady_clock::time_point; - std::unordered_map _routerLookupTimes; + std::unordered_map _routerLookupTimes; }; } // namespace llarp diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index abd7ed366..769565bdb 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -797,7 +797,7 @@ namespace llarp }); // find all deregistered relays - std::unordered_set closePeers; + std::unordered_set closePeers; _linkManager.ForEachPeer([&](auto session) { if (whitelistRouters and not gotWhitelist) diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 2d93ed5a8..48442a202 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -83,15 +83,6 @@ namespace llarp Clear(); } - struct Hash - { - size_t - operator()(const RouterContact& r) const - { - return PubKey::Hash()(r.pubkey); - } - }; - // advertised addresses std::vector addrs; // network identifier @@ -240,3 +231,16 @@ namespace llarp using RouterLookupHandler = std::function&)>; } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::RouterContact& r) const + { + return std::hash{}(r.pubkey); + } + }; +} // namespace std diff --git a/llarp/router_id.hpp b/llarp/router_id.hpp index ccc1c74c4..70c4dfbc3 100644 --- a/llarp/router_id.hpp +++ b/llarp/router_id.hpp @@ -11,8 +11,7 @@ namespace llarp using Data = std::array; - RouterID() - {} + RouterID() = default; RouterID(const byte_t* buf) : AlignedBuffer(buf) {} @@ -44,8 +43,6 @@ namespace llarp { return out << id.ToString(); } - - using Hash = AlignedBuffer::Hash; }; inline bool @@ -59,13 +56,6 @@ namespace llarp namespace std { template <> - struct hash - { - size_t - operator()(const llarp::RouterID& id) const - { - const llarp::RouterID::Hash h{}; - return h(id); - } - }; + struct hash : hash> + {}; } // namespace std diff --git a/llarp/rpc/endpoint_rpc.cpp b/llarp/rpc/endpoint_rpc.cpp index c546c8f2f..8f59576c6 100644 --- a/llarp/rpc/endpoint_rpc.cpp +++ b/llarp/rpc/endpoint_rpc.cpp @@ -65,7 +65,7 @@ namespace llarp::rpc return; } - if (msg->proto != llarp::service::eProtocolAuth) + if (msg->proto != llarp::service::ProtocolType::Auth) { // not an auth message, reject reply(service::AuthResult{service::AuthResultCode::eAuthRejected, "protocol error"}); diff --git a/llarp/rpc/endpoint_rpc.hpp b/llarp/rpc/endpoint_rpc.hpp index a7a699b28..c84cf6183 100644 --- a/llarp/rpc/endpoint_rpc.hpp +++ b/llarp/rpc/endpoint_rpc.hpp @@ -15,7 +15,7 @@ namespace llarp::rpc { using LMQ_ptr = std::shared_ptr; using Endpoint_ptr = std::shared_ptr; - using Whitelist_t = std::unordered_set; + using Whitelist_t = std::unordered_set; explicit EndpointAuthRPC( std::string url, diff --git a/llarp/service/address.hpp b/llarp/service/address.hpp index aed9a1c79..31f15cb76 100644 --- a/llarp/service/address.hpp +++ b/llarp/service/address.hpp @@ -89,15 +89,6 @@ namespace llarp { return RouterID(as_array()); } - - struct Hash - { - size_t - operator()(const Address& buf) const - { - return std::accumulate(buf.begin(), buf.end(), 0, std::bit_xor()); - } - }; }; } // namespace service @@ -111,7 +102,7 @@ namespace std size_t operator()(const llarp::service::Address& addr) const { - return llarp::service::Address::Hash{}(addr); + return std::accumulate(addr.begin(), addr.end(), 0, std::bit_xor{}); } }; } // namespace std diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 6d958550e..ba37c6260 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -958,14 +958,14 @@ namespace llarp bool Endpoint::ProcessDataMessage(std::shared_ptr msg) { - if ((msg->proto == eProtocolExit + if ((msg->proto == ProtocolType::Exit && (m_state->m_ExitEnabled || m_ExitMap.ContainsValue(msg->sender.Addr()))) - || msg->proto == eProtocolTrafficV4 || msg->proto == eProtocolTrafficV6) + || msg->proto == ProtocolType::TrafficV4 || msg->proto == ProtocolType::TrafficV6) { m_InboundTrafficQueue.tryPushBack(std::move(msg)); return true; } - if (msg->proto == eProtocolControl) + if (msg->proto == ProtocolType::Control) { // TODO: implement me (?) // right now it's just random noise @@ -1014,7 +1014,7 @@ namespace llarp msg.PutBuffer(reason); f.N.Randomize(); f.C.Zero(); - msg.proto = eProtocolAuth; + msg.proto = ProtocolType::Auth; if (not GetReplyIntroFor(tag, msg.introReply)) { LogError("Failed to send auth reply: no reply intro"); @@ -1252,7 +1252,7 @@ namespace llarp return false; pkt.UpdateIPv4Address(src, dst); /// TODO: V6 - return HandleInboundPacket(tag, pkt.ConstBuffer(), eProtocolTrafficV4, 0); + return HandleInboundPacket(tag, pkt.ConstBuffer(), ProtocolType::TrafficV4, 0); }, Router(), numDesiredPaths, @@ -1294,31 +1294,25 @@ namespace llarp void Endpoint::Pump(llarp_time_t) { - const auto& sessions = m_state->m_SNodeSessions; - auto& queue = m_InboundTrafficQueue; - - auto epPump = [&]() { - FlushRecvData(); - // send downstream packets to user for snode - for (const auto& item : sessions) - item.second.first->FlushDownstream(); - // send downstream traffic to user for hidden service - while (not queue.empty()) - { - auto msg = queue.popFront(); - const llarp_buffer_t buf(msg->payload); - HandleInboundPacket(msg->tag, buf, msg->proto, msg->seqno); - } - }; + FlushRecvData(); + // send downstream packets to user for snode + for (const auto& [router, session] : m_state->m_SNodeSessions) + session.first->FlushDownstream(); + // send downstream traffic to user for hidden service + while (not m_InboundTrafficQueue.empty()) + { + auto msg = m_InboundTrafficQueue.popFront(); + const llarp_buffer_t buf(msg->payload); + HandleInboundPacket(msg->tag, buf, msg->proto, msg->seqno); + } - epPump(); auto router = Router(); // TODO: locking on this container - for (const auto& item : m_state->m_RemoteSessions) - item.second->FlushUpstream(); + for (const auto& [addr, outctx] : m_state->m_RemoteSessions) + outctx->FlushUpstream(); // TODO: locking on this container - for (const auto& item : sessions) - item.second.first->FlushUpstream(); + for (const auto& [router, session] : m_state->m_SNodeSessions) + session.first->FlushUpstream(); // send queue flush while (not m_SendQueue.empty()) @@ -1332,17 +1326,6 @@ namespace llarp router->linkManager().PumpLinks(); } - bool - Endpoint::EnsureConvo( - const AlignedBuffer<32> /*addr*/, bool snode, ConvoEventListener_ptr /*ev*/) - { - if (snode) - {} - - // TODO: something meaningful - return false; - } - std::optional Endpoint::GetBestConvoTagForService(Address remote) const { diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 12e5d3567..cabde8dc9 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -247,9 +247,6 @@ namespace llarp void HandlePathBuilt(path::Path_ptr path) override; - bool - EnsureConvo(const AlignedBuffer<32> addr, bool snode, ConvoEventListener_ptr ev); - bool SendTo(ConvoTag tag, const llarp_buffer_t& pkt, ProtocolType t); @@ -445,7 +442,7 @@ namespace llarp bool m_PublishIntroSet = true; std::unique_ptr m_state; std::shared_ptr m_AuthPolicy; - std::unordered_map m_RemoteAuthInfos; + std::unordered_map m_RemoteAuthInfos; /// (lns name, optional exit range, optional auth info) for looking up on startup std::unordered_map, std::optional>> @@ -466,7 +463,7 @@ namespace llarp const IntroSet& introSet() const; IntroSet& introSet(); - using ConvoMap = std::unordered_map< ConvoTag, Session, ConvoTag::Hash >; + using ConvoMap = std::unordered_map; const ConvoMap& Sessions() const; ConvoMap& Sessions(); // clang-format on diff --git a/llarp/service/endpoint_state.hpp b/llarp/service/endpoint_state.hpp index 9bae738b9..f33e848b7 100644 --- a/llarp/service/endpoint_state.hpp +++ b/llarp/service/endpoint_state.hpp @@ -43,10 +43,10 @@ namespace llarp SNodeSessions m_SNodeSessions; - std::unordered_multimap m_PendingServiceLookups; - std::unordered_map m_LastServiceLookupTimes; + std::unordered_multimap m_PendingServiceLookups; + std::unordered_map m_LastServiceLookupTimes; - std::unordered_map m_ServiceLookupFails; + std::unordered_map m_ServiceLookupFails; PendingRouters m_PendingRouters; diff --git a/llarp/service/endpoint_types.hpp b/llarp/service/endpoint_types.hpp index 07be14333..7eee10a68 100644 --- a/llarp/service/endpoint_types.hpp +++ b/llarp/service/endpoint_types.hpp @@ -30,26 +30,25 @@ namespace llarp using SendMessageQueue_t = thread::Queue; using PendingBufferQueue = std::deque; - using PendingTraffic = std::unordered_map; + using PendingTraffic = std::unordered_map; using ProtocolMessagePtr = std::shared_ptr; using RecvPacketQueue_t = thread::Queue; - using PendingRouters = std::unordered_map; + using PendingRouters = std::unordered_map; using PendingLookups = std::unordered_map>; - using Sessions = - std::unordered_multimap, Address::Hash>; + using Sessions = std::unordered_multimap>; using SNodeSessionValue = std::pair, ConvoTag>; - using SNodeSessions = std::unordered_multimap; + using SNodeSessions = std::unordered_multimap; - using ConvoMap = std::unordered_map; + using ConvoMap = std::unordered_map; /// set of outbound addresses to maintain to - using OutboundSessions_t = std::unordered_set; + using OutboundSessions_t = std::unordered_set
; using PathEnsureHook = std::function; diff --git a/llarp/service/handler.hpp b/llarp/service/handler.hpp index f5c3528e6..d4d39370d 100644 --- a/llarp/service/handler.hpp +++ b/llarp/service/handler.hpp @@ -12,7 +12,8 @@ namespace llarp { namespace service { - using ConvoTag = AlignedBuffer<16>; + struct ConvoTag final : AlignedBuffer<16> + {}; struct ProtocolMessage; struct RecvDataEvent diff --git a/llarp/service/intro.hpp b/llarp/service/intro.hpp index 936af0738..1fa5fd790 100644 --- a/llarp/service/intro.hpp +++ b/llarp/service/intro.hpp @@ -70,15 +70,6 @@ namespace llarp { return pathID != other.pathID || router != other.router; } - - struct Hash - { - size_t - operator()(const Introduction& i) const - { - return PubKey::Hash()(i.router) ^ PathID_t::Hash()(i.pathID); - } - }; }; inline std::ostream& @@ -88,3 +79,16 @@ namespace llarp } } // namespace service } // namespace llarp + +namespace std +{ + template <> + struct hash + { + size_t + operator()(const llarp::service::Introduction& i) const + { + return std::hash{}(i.router) ^ std::hash{}(i.pathID); + } + }; +} // namespace std diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index 86369bddb..ad6a83e14 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -327,7 +327,7 @@ namespace llarp Encrypted<64> tmp; tmp.Randomize(); llarp_buffer_t buf(tmp.data(), tmp.size()); - AsyncEncryptAndSendTo(buf, eProtocolControl); + AsyncEncryptAndSendTo(buf, ProtocolType::Control); } } } @@ -549,7 +549,7 @@ namespace llarp ProtocolMessage msg{}; if (frame.DecryptPayloadInto(sessionKey, msg)) { - if (msg.proto == eProtocolAuth and not msg.payload.empty()) + if (msg.proto == ProtocolType::Auth and not msg.payload.empty()) { result.reason = std::string{ reinterpret_cast(msg.payload.data()), msg.payload.size()}; @@ -572,7 +572,7 @@ namespace llarp authResultListener = nullptr; hook = [handler](std::shared_ptr msg) { AuthResult result{AuthResultCode::eAuthAccepted, "OK"}; - if (msg->proto == eProtocolAuth and not msg->payload.empty()) + if (msg->proto == ProtocolType::Auth and not msg->payload.empty()) { result.reason = std::string{ reinterpret_cast(msg->payload.data()), msg->payload.size()}; @@ -603,7 +603,7 @@ namespace llarp void OutboundContext::SendPacketToRemote(const llarp_buffer_t& buf) { - AsyncEncryptAndSendTo(buf, eProtocolExit); + AsyncEncryptAndSendTo(buf, ProtocolType::Exit); } } // namespace service diff --git a/llarp/service/outbound_context.hpp b/llarp/service/outbound_context.hpp index af9896463..570f70f7d 100644 --- a/llarp/service/outbound_context.hpp +++ b/llarp/service/outbound_context.hpp @@ -135,7 +135,7 @@ namespace llarp uint64_t m_UpdateIntrosetTX = 0; IntroSet currentIntroSet; Introduction m_NextIntro; - std::unordered_map m_BadIntros; + std::unordered_map m_BadIntros; llarp_time_t lastShift = 0s; uint16_t m_LookupFails = 0; uint16_t m_BuildFails = 0; diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index 51f126fac..24b56ec82 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -37,7 +37,7 @@ namespace llarp ProtocolMessage(const ConvoTag& tag); ProtocolMessage(); ~ProtocolMessage(); - ProtocolType proto = eProtocolTrafficV4; + ProtocolType proto = ProtocolType::TrafficV4; llarp_time_t queued = 0s; std::vector payload; Introduction introReply; diff --git a/llarp/service/protocol_type.hpp b/llarp/service/protocol_type.hpp index 695629a34..37521c520 100644 --- a/llarp/service/protocol_type.hpp +++ b/llarp/service/protocol_type.hpp @@ -4,11 +4,14 @@ namespace llarp::service { - using ProtocolType = uint64_t; - - constexpr ProtocolType eProtocolControl = 0UL; - constexpr ProtocolType eProtocolTrafficV4 = 1UL; - constexpr ProtocolType eProtocolTrafficV6 = 2UL; - constexpr ProtocolType eProtocolExit = 3UL; - constexpr ProtocolType eProtocolAuth = 4UL; + // Supported protocol types; the values are given explicitly because they are specifically used + // when sending over the wire. + enum class ProtocolType : uint64_t + { + Control = 0UL, + TrafficV4 = 1UL, + TrafficV6 = 2UL, + Exit = 3UL, + Auth = 4UL, + }; } // namespace llarp::service diff --git a/llarp/service/sendcontext.cpp b/llarp/service/sendcontext.cpp index 6d433b642..ede6b6ff9 100644 --- a/llarp/service/sendcontext.cpp +++ b/llarp/service/sendcontext.cpp @@ -114,7 +114,7 @@ namespace llarp { // send auth message const llarp_buffer_t authdata{maybe->token}; - AsyncGenIntro(authdata, eProtocolAuth); + AsyncGenIntro(authdata, ProtocolType::Auth); authResultListener = resultHandler; } else @@ -134,7 +134,7 @@ namespace llarp { // send auth message const llarp_buffer_t authdata(maybe->token); - AsyncGenIntro(authdata, eProtocolAuth); + AsyncGenIntro(authdata, ProtocolType::Auth); } else { diff --git a/llarp/service/tag.hpp b/llarp/service/tag.hpp index 219885af9..a4e3dba4e 100644 --- a/llarp/service/tag.hpp +++ b/llarp/service/tag.hpp @@ -49,8 +49,13 @@ namespace llarp { return data()[0] == 0; } - - using Hash = AlignedBuffer::Hash; }; } // namespace service } // namespace llarp + +namespace std +{ + template <> + struct hash : hash> + {}; +} // namespace std diff --git a/llarp/tooling/router_hive.hpp b/llarp/tooling/router_hive.hpp index 5428bb790..54826a7f5 100644 --- a/llarp/tooling/router_hive.hpp +++ b/llarp/tooling/router_hive.hpp @@ -83,8 +83,8 @@ namespace tooling GetRelayRCs(); std::mutex routerMutex; - std::unordered_map relays; - std::unordered_map clients; + std::unordered_map relays; + std::unordered_map clients; std::vector routerMainThreads; diff --git a/llarp/util/aligned.hpp b/llarp/util/aligned.hpp index aba9b83e7..f2507e1ba 100644 --- a/llarp/util/aligned.hpp +++ b/llarp/util/aligned.hpp @@ -289,18 +289,22 @@ namespace llarp return stream; } - struct Hash - { - std::size_t - operator()(const AlignedBuffer& buf) const noexcept - { - std::size_t h = 0; - std::memcpy(&h, buf.data(), sizeof(std::size_t)); - return h; - } - }; - private: Data m_data; }; } // namespace llarp + +namespace std +{ + template + struct hash> + { + std::size_t + operator()(const llarp::AlignedBuffer& buf) const noexcept + { + std::size_t h = 0; + std::memcpy(&h, buf.data(), sizeof(std::size_t)); + return h; + } + }; +} // namespace std diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index c7bebbcbc..48c977ee3 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -6,6 +6,7 @@ #include #include "mem.hpp" +#include #include #include #include @@ -41,7 +42,12 @@ namespace llarp bool BEncodeWriteDictInt(const char* k, const Int_t& i, llarp_buffer_t* buf) { - return bencode_write_bytestring(buf, k, 1) && bencode_write_uint64(buf, i); + if (!bencode_write_bytestring(buf, k, 1)) + return false; + if constexpr (std::is_enum_v) + return bencode_write_uint64(buf, static_cast>(i)); + else + return bencode_write_uint64(buf, i); } template @@ -92,7 +98,7 @@ namespace llarp return false; } - i = Int_t(read_i); + i = static_cast(read_i); read = true; } return true; diff --git a/llarp/util/decaying_hashset.hpp b/llarp/util/decaying_hashset.hpp index a941440f5..82c395ef4 100644 --- a/llarp/util/decaying_hashset.hpp +++ b/llarp/util/decaying_hashset.hpp @@ -7,7 +7,7 @@ namespace llarp { namespace util { - template + template > struct DecayingHashSet { using Time_t = std::chrono::milliseconds; diff --git a/llarp/util/decaying_hashtable.hpp b/llarp/util/decaying_hashtable.hpp index 2f7ea7c03..09fc1c787 100644 --- a/llarp/util/decaying_hashtable.hpp +++ b/llarp/util/decaying_hashtable.hpp @@ -5,7 +5,7 @@ namespace llarp::util { - template + template > struct DecayingHashTable { DecayingHashTable(std::chrono::milliseconds cacheInterval = 1h) : m_CacheInterval(cacheInterval) diff --git a/pybind/llarp/context.cpp b/pybind/llarp/context.cpp index 6ebcf97d4..7fb369468 100644 --- a/pybind/llarp/context.cpp +++ b/pybind/llarp/context.cpp @@ -1,8 +1,9 @@ #include #include #include -#include +#include #include +#include "service/protocol_type.hpp" namespace llarp { void @@ -28,7 +29,8 @@ namespace llarp std::vector buf; buf.resize(pkt.size()); std::copy_n(pkt.c_str(), pkt.size(), buf.data()); - return ep and ep->SendToServiceOrQueue(to, std::move(buf), service::eProtocolControl); + return ep + and ep->SendToServiceOrQueue(to, std::move(buf), service::ProtocolType::Control); }) .def( "AddEndpoint", diff --git a/test/net/test_llarp_net.cpp b/test/net/test_llarp_net.cpp index 9c504e83b..aea061bd4 100644 --- a/test/net/test_llarp_net.cpp +++ b/test/net/test_llarp_net.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -102,3 +103,30 @@ TEST_CASE("Bogon") REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4))); } } + +TEST_CASE("uint128_t") +{ + SECTION("layout") + { + llarp::uint128_t i{0x0011223f44556677ULL, 0x8899aabbc3ddeeffULL}; + REQUIRE(oxenmq::to_hex(std::string_view{reinterpret_cast(&i), sizeof(i)}) == +#ifdef __BIG_ENDIAN__ + "0011223f445566778899aabbc3ddeeff" +#else + "ffeeddc3bbaa9988776655443f221100" +#endif + ); + } + SECTION("ntoh") + { + llarp::uint128_t i{0x0011223f44556677ULL, 0x8899aabbc3ddeeffULL}; + auto be = ntoh128(i); + REQUIRE(be == llarp::uint128_t{0xffeeddc3bbaa9988ULL, 0x776655443f221100ULL}); + } + SECTION("hton") + { + llarp::uint128_t i{0x0011223f44556677ULL, 0x8899aabbc3ddeeffULL}; + auto be = ntoh128(i); + REQUIRE(be == llarp::uint128_t{0xffeeddc3bbaa9988ULL, 0x776655443f221100ULL}); + } +} diff --git a/test/util/test_llarp_util_aligned.cpp b/test/util/test_llarp_util_aligned.cpp index b84fa40a9..8cda24eff 100644 --- a/test/util/test_llarp_util_aligned.cpp +++ b/test/util/test_llarp_util_aligned.cpp @@ -162,7 +162,7 @@ TEMPLATE_LIST_TEST_CASE("AlignedBuffer", "[AlignedBuffer]", TestSizes) SECTION("TestHash") { - using Map_t = std::unordered_map; + using Map_t = std::unordered_map; Buffer k, other_k; k.Randomize();