From 3f53965b71b357d3ed022cd56f98c5cf74b7ecab Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 24 May 2019 03:01:36 +0100 Subject: [PATCH] Remove all use of IBEncodeMessage --- llarp/dht/message.hpp | 9 ++- llarp/exit/policy.cpp | 4 -- llarp/exit/policy.hpp | 19 +++--- llarp/exit/transfer_traffic.cpp | 1 + llarp/messages/link_message.hpp | 16 ++++- llarp/messages/relay_commit.cpp | 2 +- llarp/net/address_info.cpp | 16 ----- llarp/net/address_info.hpp | 26 +++----- llarp/net/exit_info.cpp | 14 ----- llarp/net/exit_info.hpp | 32 ++++------ llarp/pow.hpp | 9 +-- llarp/profiling.cpp | 11 +--- llarp/profiling.hpp | 25 +++----- llarp/router/router.cpp | 2 +- llarp/router_contact.hpp | 26 +++----- llarp/routing/message.hpp | 13 +++- llarp/routing/path_latency.cpp | 1 + llarp/service/identity.cpp | 2 +- llarp/service/identity.hpp | 11 ++-- llarp/service/info.hpp | 36 +++-------- llarp/service/intro.cpp | 4 -- llarp/service/intro.hpp | 17 ++--- llarp/service/intro_set.cpp | 2 +- llarp/service/intro_set.hpp | 13 +++- llarp/service/protocol.cpp | 10 +-- llarp/service/protocol.hpp | 15 +++-- llarp/util/bencode.hpp | 107 ++++++++++++++++---------------- 27 files changed, 199 insertions(+), 244 deletions(-) diff --git a/llarp/dht/message.hpp b/llarp/dht/message.hpp index 5dff08a6e..e5aa63ff5 100644 --- a/llarp/dht/message.hpp +++ b/llarp/dht/message.hpp @@ -14,7 +14,7 @@ namespace llarp { constexpr size_t MAX_MSG_SIZE = 2048; - struct IMessage : public IBEncodeMessage + struct IMessage { virtual ~IMessage() { @@ -31,8 +31,15 @@ namespace llarp HandleMessage(struct llarp_dht_context* dht, std::vector< Ptr_t >& replies) const = 0; + virtual bool + BEncode(llarp_buffer_t* buf) const = 0; + + virtual bool + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) = 0; + Key_t From; PathID_t pathID; + uint64_t version = LLARP_PROTO_VERSION; }; IMessage::Ptr_t diff --git a/llarp/exit/policy.cpp b/llarp/exit/policy.cpp index 31cd51ef4..7ff7b9e88 100644 --- a/llarp/exit/policy.cpp +++ b/llarp/exit/policy.cpp @@ -4,10 +4,6 @@ namespace llarp { namespace exit { - Policy::~Policy() - { - } - bool Policy::BEncode(llarp_buffer_t *buf) const { diff --git a/llarp/exit/policy.hpp b/llarp/exit/policy.hpp index e8e0b1207..4a0fd9373 100644 --- a/llarp/exit/policy.hpp +++ b/llarp/exit/policy.hpp @@ -7,19 +7,24 @@ namespace llarp { namespace exit { - struct Policy final : public llarp::IBEncodeMessage + struct Policy { - ~Policy(); + uint64_t proto = 0; + uint64_t port = 0; + uint64_t drop = 0; + uint64_t version = LLARP_PROTO_VERSION; - uint64_t proto; - uint64_t port; - uint64_t drop; + bool + BDecode(llarp_buffer_t* buf) + { + return bencode_decode_dict(*this, buf); + } bool - DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override; + DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val); bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; }; } // namespace exit } // namespace llarp diff --git a/llarp/exit/transfer_traffic.cpp b/llarp/exit/transfer_traffic.cpp index b6f3caafc..b046c2b1a 100644 --- a/llarp/exit/transfer_traffic.cpp +++ b/llarp/exit/transfer_traffic.cpp @@ -1,6 +1,7 @@ #include #include +#include #include namespace llarp diff --git a/llarp/messages/link_message.hpp b/llarp/messages/link_message.hpp index 256a59d82..695487624 100644 --- a/llarp/messages/link_message.hpp +++ b/llarp/messages/link_message.hpp @@ -13,11 +13,11 @@ namespace llarp struct AbstractRouter; /// parsed link layer message - struct ILinkMessage : public IBEncodeMessage + struct ILinkMessage { /// who did this message come from or is going to ILinkSession* session = nullptr; - uint64_t version = 0; + uint64_t version = LLARP_PROTO_VERSION; ILinkMessage() = default; @@ -25,6 +25,18 @@ namespace llarp { } + virtual bool + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) = 0; + + bool + BDecode(llarp_buffer_t* buf) + { + return bencode_decode_dict(*this, buf); + } + + virtual bool + BEncode(llarp_buffer_t* buf) const = 0; + virtual bool HandleMessage(AbstractRouter* router) const = 0; diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index 05f8ae88b..82c14271e 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -152,7 +152,7 @@ namespace llarp } work = std::make_unique< PoW >(); - return work->BDecode(buffer); + return bencode_decode_dict(*work, buffer); } return read; } diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index cb998ab65..6a81f7490 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -12,22 +12,6 @@ namespace llarp { - AddressInfo::~AddressInfo() - { - } - - AddressInfo & - AddressInfo::operator=(const AddressInfo &other) - { - rank = other.rank; - dialect = other.dialect; - pubkey = other.pubkey; - memcpy(ip.s6_addr, other.ip.s6_addr, 16); - port = other.port; - version = other.version; - return *this; - } - bool operator==(const AddressInfo &lhs, const AddressInfo &rhs) { diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index 7c974b920..48ec5f6d1 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -19,38 +19,26 @@ /// address information model namespace llarp { - struct AddressInfo final : public IBEncodeMessage + struct AddressInfo { uint16_t rank; std::string dialect; llarp::PubKey pubkey; struct in6_addr ip; uint16_t port; + uint64_t version = LLARP_PROTO_VERSION; - AddressInfo() : IBEncodeMessage() - { - } - - AddressInfo(const AddressInfo& other) - : IBEncodeMessage(other.version) - , rank(other.rank) - , dialect(other.dialect) - , pubkey(other.pubkey) - , port(other.port) + bool + BDecode(llarp_buffer_t* buf) { - memcpy(ip.s6_addr, other.ip.s6_addr, 16); + return bencode_decode_dict(*this, buf); } - ~AddressInfo(); - - AddressInfo& - operator=(const AddressInfo& other); - bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; bool - DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override; + DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf); std::ostream& print(std::ostream& stream, int level, int spaces) const; diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index da1040e06..9c4435356 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -12,20 +12,6 @@ namespace llarp { - ExitInfo::~ExitInfo() - { - } - - ExitInfo& - ExitInfo::operator=(const ExitInfo& other) - { - memcpy(address.s6_addr, other.address.s6_addr, 16); - memcpy(netmask.s6_addr, other.netmask.s6_addr, 16); - pubkey = other.pubkey; - version = other.version; - return *this; - } - bool ExitInfo::BEncode(llarp_buffer_t* buf) const { diff --git a/llarp/net/exit_info.hpp b/llarp/net/exit_info.hpp index a24648093..b7355abee 100644 --- a/llarp/net/exit_info.hpp +++ b/llarp/net/exit_info.hpp @@ -16,14 +16,18 @@ /// Exit info model namespace llarp { - struct ExitInfo final : public IBEncodeMessage + struct ExitInfo { struct in6_addr address; struct in6_addr netmask; PubKey pubkey; + uint64_t version = LLARP_PROTO_VERSION; - ExitInfo(const PubKey &pk, const nuint32_t &ipv4_exit) - : IBEncodeMessage(), pubkey(pk) + ExitInfo() + { + } + + ExitInfo(const PubKey &pk, const nuint32_t &ipv4_exit) : pubkey(pk) { memset(address.s6_addr, 0, 16); address.s6_addr[11] = 0xff; @@ -32,27 +36,17 @@ namespace llarp memset(netmask.s6_addr, 0xff, 16); } - ExitInfo() : IBEncodeMessage() - { - } + bool + BEncode(llarp_buffer_t *buf) const; - ExitInfo(const ExitInfo &other) - : IBEncodeMessage(other.version), pubkey(other.pubkey) + bool + BDecode(llarp_buffer_t *buf) { - memcpy(address.s6_addr, other.address.s6_addr, 16); - memcpy(netmask.s6_addr, other.netmask.s6_addr, 16); + return bencode_decode_dict(*this, buf); } - ~ExitInfo(); - bool - BEncode(llarp_buffer_t *buf) const override; - - bool - DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf) override; - - ExitInfo & - operator=(const ExitInfo &other); + DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf); std::ostream & print(std::ostream &stream, int level, int spaces) const; diff --git a/llarp/pow.hpp b/llarp/pow.hpp index c5ede182b..4af5d397f 100644 --- a/llarp/pow.hpp +++ b/llarp/pow.hpp @@ -3,17 +3,18 @@ #include #include -#include +#include namespace llarp { /// proof of work - struct PoW final : public IBEncodeMessage + struct PoW { static constexpr size_t MaxSize = 128; uint64_t timestamp = 0; uint32_t extendedLifetime = 0; AlignedBuffer< 32 > nonce; + uint64_t version = LLARP_PROTO_VERSION; ~PoW(); @@ -21,10 +22,10 @@ namespace llarp IsValid(shorthash_func hashfunc, llarp_time_t now) const; bool - DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override; + DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val); bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; bool operator==(const PoW& other) const diff --git a/llarp/profiling.cpp b/llarp/profiling.cpp index c3367ff0f..e7058461a 100644 --- a/llarp/profiling.cpp +++ b/llarp/profiling.cpp @@ -96,12 +96,7 @@ namespace llarp return checkIsGood(pathFailCount, pathSuccessCount, chances); } - Profiling::Profiling() : IBEncodeMessage() - { - m_DisableProfiling.store(false); - } - - Profiling::~Profiling() + Profiling::Profiling() : m_DisableProfiling(false) { } @@ -267,7 +262,7 @@ namespace llarp if(k.sz != 32) return false; RouterProfile profile; - if(!profile.BDecode(buf)) + if(!bencode_decode_dict(profile, buf)) return false; RouterID pk = k.base; return m_Profiles.emplace(pk, profile).second; @@ -278,7 +273,7 @@ namespace llarp { lock_t lock(&m_ProfilesMutex); m_Profiles.clear(); - if(!BDecodeReadFile(fname, *this)) + if(!BDecodeReadFromFile(fname, *this)) { llarp::LogWarn("failed to load router profiles from ", fname); return false; diff --git a/llarp/profiling.hpp b/llarp/profiling.hpp index ed58310d5..0b0b72b35 100644 --- a/llarp/profiling.hpp +++ b/llarp/profiling.hpp @@ -11,7 +11,7 @@ namespace llarp { - struct RouterProfile final : public IBEncodeMessage + struct RouterProfile { static constexpr size_t MaxSize = 256; uint64_t connectTimeoutCount = 0; @@ -20,18 +20,13 @@ namespace llarp uint64_t pathFailCount = 0; llarp_time_t lastUpdated = 0; llarp_time_t lastDecay = 0; - - RouterProfile() : IBEncodeMessage() - { - } - - ~RouterProfile() = default; + uint64_t version = LLARP_PROTO_VERSION; bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; bool - DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override; + DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf); bool IsGood(uint64_t chances) const; @@ -51,17 +46,16 @@ namespace llarp Tick(); }; - struct Profiling final : public IBEncodeMessage + struct Profiling { Profiling(); - ~Profiling(); /// generic variant bool IsBad(const RouterID& r, uint64_t chances = 8) LOCKS_EXCLUDED(m_ProfilesMutex); - /// check if this rotuer should have paths built over it + /// check if this router should have paths built over it bool IsBadForPath(const RouterID& r, uint64_t chances = 8) LOCK_RETURNED(m_ProfilesMutex); @@ -90,11 +84,12 @@ namespace llarp Tick() LOCKS_EXCLUDED(m_ProfilesMutex); bool - BEncode(llarp_buffer_t* buf) const override LOCKS_EXCLUDED(m_ProfilesMutex); + BEncode(llarp_buffer_t* buf) const LOCKS_EXCLUDED(m_ProfilesMutex); bool - DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override - SHARED_LOCKS_REQUIRED(m_ProfilesMutex); + DecodeKey(const llarp_buffer_t& k, + llarp_buffer_t* buf) NO_THREAD_SAFETY_ANALYSIS; + // disabled because we do load -> bencode::BDecodeReadFromFile -> DecodeKey bool Load(const char* fname) LOCKS_EXCLUDED(m_ProfilesMutex); diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 0a878ac6b..89e19e778 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -506,7 +506,7 @@ namespace llarp LogDebug("verify RC signature"); if(!_rc.Verify(crypto(), Now())) { - rc().Dump< MAX_RC_SIZE >(); + Dump< MAX_RC_SIZE >(rc()); LogError("RC is invalid, not saving"); return false; } diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 5de7cf174..2376c099f 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -65,31 +65,18 @@ namespace llarp } /// RouterContact - struct RouterContact final : public IBEncodeMessage + struct RouterContact { /// for unit tests static bool IgnoreBogons; static llarp_time_t Lifetime; - RouterContact() : IBEncodeMessage() + RouterContact() { Clear(); } - RouterContact(const RouterContact &other) - : IBEncodeMessage(other.version) - , addrs(other.addrs) - , netID(other.netID) - , enckey(other.enckey) - , pubkey(other.pubkey) - , exits(other.exits) - , signature(other.signature) - , nickname(other.nickname) - , last_updated(other.last_updated) - { - } - struct Hash { size_t @@ -115,12 +102,13 @@ namespace llarp llarp::AlignedBuffer< NICKLEN > nickname; uint64_t last_updated = 0; + uint64_t version = LLARP_PROTO_VERSION; util::StatusObject ExtractStatus() const; bool - BEncode(llarp_buffer_t *buf) const override; + BEncode(llarp_buffer_t *buf) const; bool operator==(const RouterContact &other) const @@ -153,14 +141,14 @@ namespace llarp } bool - BDecode(llarp_buffer_t *buf) override + BDecode(llarp_buffer_t *buf) { Clear(); - return IBEncodeMessage::BDecode(buf); + return bencode_decode_dict(*this, buf); } bool - DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf) override; + DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf); RouterContact & operator=(const RouterContact &other); diff --git a/llarp/routing/message.hpp b/llarp/routing/message.hpp index 42d5c345a..e42909b5f 100644 --- a/llarp/routing/message.hpp +++ b/llarp/routing/message.hpp @@ -1,8 +1,8 @@ #ifndef LLARP_ROUTING_MESSAGE_HPP #define LLARP_ROUTING_MESSAGE_HPP +#include #include -#include #include namespace llarp @@ -12,12 +12,13 @@ namespace llarp { struct IMessageHandler; - struct IMessage : public llarp::IBEncodeMessage + struct IMessage { PathID_t from; uint64_t S; + uint64_t version = LLARP_PROTO_VERSION; - IMessage() : llarp::IBEncodeMessage(), S(0) + IMessage() : S(0) { } @@ -25,6 +26,12 @@ namespace llarp { } + virtual bool + BEncode(llarp_buffer_t* buf) const = 0; + + virtual bool + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) = 0; + virtual bool HandleMessage(IMessageHandler* h, AbstractRouter* r) const = 0; diff --git a/llarp/routing/path_latency.cpp b/llarp/routing/path_latency.cpp index 4ec8f7e8a..423106348 100644 --- a/llarp/routing/path_latency.cpp +++ b/llarp/routing/path_latency.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace llarp { diff --git a/llarp/service/identity.cpp b/llarp/service/identity.cpp index b29df81a7..3ecaf7c3a 100644 --- a/llarp/service/identity.cpp +++ b/llarp/service/identity.cpp @@ -120,7 +120,7 @@ namespace llarp return false; // decode inf.read((char*)buf.base, sz); - if(!BDecode(&buf)) + if(!bencode_decode_dict(*this, &buf)) return false; ServiceInfo::OptNonce van; diff --git a/llarp/service/identity.hpp b/llarp/service/identity.hpp index 7d63f9c23..2cbd315f0 100644 --- a/llarp/service/identity.hpp +++ b/llarp/service/identity.hpp @@ -1,11 +1,12 @@ #ifndef LLARP_SERVICE_IDENTITY_HPP #define LLARP_SERVICE_IDENTITY_HPP +#include #include #include #include #include -#include +#include #include @@ -16,12 +17,12 @@ namespace llarp namespace service { // private keys - struct Identity final : public IBEncodeMessage + struct Identity { SecretKey enckey; SecretKey signkey; PQKeyPair pq; - uint64_t version = 0; + uint64_t version = LLARP_PROTO_VERSION; VanityNonce vanity; // public service info @@ -32,7 +33,7 @@ namespace llarp RegenerateKeys(Crypto* c); bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; bool EnsureKeys(const std::string& fpath, Crypto* c); @@ -42,7 +43,7 @@ namespace llarp const ServiceInfo& other, const KeyExchangeNonce& N) const; bool - DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf); bool SignIntroSet(IntroSet& i, Crypto* c, llarp_time_t now) const; diff --git a/llarp/service/info.hpp b/llarp/service/info.hpp index 0cfb5d942..de92f8e89 100644 --- a/llarp/service/info.hpp +++ b/llarp/service/info.hpp @@ -14,7 +14,7 @@ namespace llarp namespace service { - struct ServiceInfo final : public IBEncodeMessage + struct ServiceInfo { private: PubKey enckey; @@ -22,30 +22,11 @@ namespace llarp public: VanityNonce vanity; + Address m_CachedAddr; + uint64_t version = LLARP_PROTO_VERSION; using OptNonce = absl::optional< VanityNonce >; - ServiceInfo() = default; - - ServiceInfo(ServiceInfo&& other) - { - enckey = std::move(other.enckey); - signkey = std::move(other.signkey); - version = std::move(other.version); - vanity = std::move(other.vanity); - m_CachedAddr = std::move(other.m_CachedAddr); - } - - ServiceInfo(const ServiceInfo& other) - : IBEncodeMessage(other.version) - , enckey(other.enckey) - , signkey(other.signkey) - , vanity(other.vanity) - , m_CachedAddr(other.m_CachedAddr) - { - version = other.version; - } - void RandomizeVanity() { @@ -126,21 +107,18 @@ namespace llarp bool CalculateAddress(std::array< byte_t, 32 >& data) const; bool - BDecode(llarp_buffer_t* buf) override + BDecode(llarp_buffer_t* buf) { - if(IBEncodeMessage::BDecode(buf)) + if(bencode_decode_dict(*this, buf)) return CalculateAddress(m_CachedAddr.as_array()); return false; } bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; bool - DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; - - private: - Address m_CachedAddr; + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf); }; inline std::ostream& diff --git a/llarp/service/intro.cpp b/llarp/service/intro.cpp index a9ee83132..fbebce544 100644 --- a/llarp/service/intro.cpp +++ b/llarp/service/intro.cpp @@ -4,10 +4,6 @@ namespace llarp { namespace service { - Introduction::~Introduction() - { - } - util::StatusObject Introduction::ExtractStatus() const { diff --git a/llarp/service/intro.hpp b/llarp/service/intro.hpp index 8eda15e73..e3c4ba628 100644 --- a/llarp/service/intro.hpp +++ b/llarp/service/intro.hpp @@ -12,14 +12,13 @@ namespace llarp { namespace service { - struct Introduction final : public IBEncodeMessage + struct Introduction { PubKey router; PathID_t pathID; uint64_t latency = 0; uint64_t expiresAt = 0; - - Introduction() = default; + uint64_t version = LLARP_PROTO_VERSION; util::StatusObject ExtractStatus() const; @@ -38,16 +37,20 @@ namespace llarp return IsExpired(now); } - ~Introduction(); - std::ostream& print(std::ostream& stream, int level, int spaces) const; bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; + + bool + BDecode(llarp_buffer_t* buf) + { + return bencode_decode_dict(*this, buf); + } bool - DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf); void Clear(); diff --git a/llarp/service/intro_set.cpp b/llarp/service/intro_set.cpp index 7955d775d..3d41c3fac 100644 --- a/llarp/service/intro_set.cpp +++ b/llarp/service/intro_set.cpp @@ -44,7 +44,7 @@ namespace llarp if(key == "w") { W = absl::make_optional< PoW >(); - return W->BDecode(buf); + return bencode_decode_dict(*W, buf); } if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) diff --git a/llarp/service/intro_set.hpp b/llarp/service/intro_set.hpp index 8eb673455..18f59da8c 100644 --- a/llarp/service/intro_set.hpp +++ b/llarp/service/intro_set.hpp @@ -25,7 +25,7 @@ namespace llarp constexpr std::size_t MAX_INTROSET_SIZE = 4096; // 10 seconds clock skew permitted for introset expiration constexpr llarp_time_t MAX_INTROSET_TIME_DELTA = (10 * 1000); - struct IntroSet final : public IBEncodeMessage + struct IntroSet { ServiceInfo A; std::vector< Introduction > I; @@ -34,6 +34,7 @@ namespace llarp llarp_time_t T = 0; absl::optional< PoW > W; Signature Z; + uint64_t version = LLARP_PROTO_VERSION; bool OtherIsNewer(const IntroSet& other) const @@ -57,10 +58,16 @@ namespace llarp IsExpired(llarp_time_t now) const; bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; bool - DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; + BDecode(llarp_buffer_t* buf) + { + return bencode_decode_dict(*this, buf); + } + + bool + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf); bool Verify(Crypto* crypto, llarp_time_t now) const; diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index 1460434fa..b33e1fa13 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -179,7 +179,7 @@ namespace llarp Encrypted_t tmp = D; auto buf = tmp.Buffer(); crypto->xchacha20(*buf, sharedkey, N); - return msg.BDecode(buf); + return bencode_decode_dict(msg, buf); } bool @@ -287,7 +287,7 @@ namespace llarp // decrypt auto buf = frame.D.Buffer(); crypto->xchacha20(*buf, K, self->frame.N); - if(!self->msg->BDecode(buf)) + if(!bencode_decode_dict(*self->msg, buf)) { LogError("failed to decode inner protocol message"); DumpBuffer(*buf); @@ -300,8 +300,8 @@ namespace llarp { LogError("intro frame has invalid signature Z=", self->frame.Z, " from ", self->msg->sender.Addr()); - self->frame.Dump< MAX_PROTOCOL_MESSAGE_SIZE >(); - self->msg->Dump< MAX_PROTOCOL_MESSAGE_SIZE >(); + Dump< MAX_PROTOCOL_MESSAGE_SIZE >(self->frame); + Dump< MAX_PROTOCOL_MESSAGE_SIZE >(*self->msg); self->msg.reset(); delete self; return; @@ -326,7 +326,7 @@ namespace llarp self->msg->sender, self->frame.N)) { LogError("x25519 key exchange failed"); - self->frame.Dump< MAX_PROTOCOL_MESSAGE_SIZE >(); + Dump< MAX_PROTOCOL_MESSAGE_SIZE >(self->frame); self->msg.reset(); delete self; return; diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index d967cc24c..780176d8c 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -38,7 +38,7 @@ namespace llarp constexpr ProtocolType eProtocolTraffic = 1UL; /// inner message - struct ProtocolMessage final : public IBEncodeMessage + struct ProtocolMessage { ProtocolMessage(const ConvoTag& tag); ProtocolMessage(); @@ -52,13 +52,14 @@ namespace llarp /// local path we got this message from PathID_t srcPath; ConvoTag tag; - uint64_t seqno = 0; + uint64_t seqno = 0; + uint64_t version = LLARP_PROTO_VERSION; bool - DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; + DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val); bool - BEncode(llarp_buffer_t* buf) const override; + BEncode(llarp_buffer_t* buf) const; void PutBuffer(const llarp_buffer_t& payload); @@ -141,6 +142,12 @@ namespace llarp bool BEncode(llarp_buffer_t* buf) const override; + bool + BDecode(llarp_buffer_t* buf) + { + return bencode_decode_dict(*this, buf); + } + void Clear() override { diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index 005a25c77..1a8081fee 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -12,6 +12,10 @@ namespace llarp { + template < typename List_t > + bool + BEncodeReadList(List_t& result, llarp_buffer_t* buf); + inline bool BEncodeWriteDictMsgType(llarp_buffer_t* buf, const char* k, const char* t) { @@ -185,6 +189,24 @@ namespace llarp return sink(buffer, nullptr); } + template < typename Sink > + bool + bencode_decode_dict(Sink&& sink, llarp_buffer_t* buff) + { + return bencode_read_dict( + [&](llarp_buffer_t* buffer, llarp_buffer_t* key) { + if(key == nullptr) + return true; + if(sink.DecodeKey(*key, buffer)) + return true; + llarp::LogWarnTag("llarp/bencode.hpp", "undefined key '", *key->cur, + "' for entry in dict"); + + return false; + }, + buff); + } + template < typename Sink > bool bencode_read_list(Sink&& sink, llarp_buffer_t* buffer) @@ -255,69 +277,50 @@ namespace llarp && BEncodeWriteList(list.begin(), list.end(), buf); } - /// bencode serializable message - struct IBEncodeMessage + template < size_t bufsz, typename T > + void + Dump(const T& val) { - virtual ~IBEncodeMessage() - { - } - - IBEncodeMessage(uint64_t v = LLARP_PROTO_VERSION) - { - version = v; - } - - virtual bool - DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) = 0; - - virtual bool - BEncode(llarp_buffer_t* buf) const = 0; - - virtual bool - BDecode(llarp_buffer_t* buf) - { - return bencode_read_dict(*this, buf); - } - - // TODO: check for shadowed values elsewhere - uint64_t version = 0; - - bool - operator()(llarp_buffer_t* buffer, llarp_buffer_t* key) - { - return HandleKey(key, buffer); - } - - bool - HandleKey(llarp_buffer_t* k, llarp_buffer_t* val) + std::array< byte_t, bufsz > tmp; + llarp_buffer_t buf(tmp); + if(val.BEncode(&buf)) { - if(k == nullptr) - return true; - if(DecodeKey(*k, val)) - return true; - llarp::LogWarnTag("llarp/bencode.hpp", "undefined key '", *k->cur, - "' for entry in dict"); - - return false; + llarp::DumpBuffer< decltype(buf), 128 >(buf); } + } - template < size_t bufsz, size_t align = 128 > - void - Dump() const + /// read entire file and decode its contents into t + template < typename T > + bool + BDecodeReadFile(const char* fpath, T& t) + { + std::vector< byte_t > ptr; { - std::array< byte_t, bufsz > tmp; - llarp_buffer_t buf(tmp); - if(BEncode(&buf)) + std::ifstream f; + f.open(fpath); + if(!f.is_open()) { - llarp::DumpBuffer< decltype(buf), align >(buf); + return false; } + f.seekg(0, std::ios::end); + const std::streampos sz = f.tellg(); + f.seekg(0, std::ios::beg); + ptr.resize(sz); + f.read((char*)ptr.data(), sz); + } + llarp_buffer_t buf(ptr); + auto result = t.BDecode(&buf); + if(!result) + { + DumpBuffer(buf); } - }; + return result; + } /// read entire file and decode its contents into t template < typename T > bool - BDecodeReadFile(const char* fpath, T& t) + BDecodeReadFromFile(const char* fpath, T& t) { std::vector< byte_t > ptr; { @@ -334,7 +337,7 @@ namespace llarp f.read((char*)ptr.data(), sz); } llarp_buffer_t buf(ptr); - auto result = t.BDecode(&buf); + auto result = bencode_decode_dict(t, &buf); if(!result) { DumpBuffer(buf);