Remove all use of IBEncodeMessage

pull/622/head
Michael 5 years ago
parent 82491305dd
commit 3f53965b71
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -14,7 +14,7 @@ namespace llarp
{ {
constexpr size_t MAX_MSG_SIZE = 2048; constexpr size_t MAX_MSG_SIZE = 2048;
struct IMessage : public IBEncodeMessage struct IMessage
{ {
virtual ~IMessage() virtual ~IMessage()
{ {
@ -31,8 +31,15 @@ namespace llarp
HandleMessage(struct llarp_dht_context* dht, HandleMessage(struct llarp_dht_context* dht,
std::vector< Ptr_t >& replies) const = 0; 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; Key_t From;
PathID_t pathID; PathID_t pathID;
uint64_t version = LLARP_PROTO_VERSION;
}; };
IMessage::Ptr_t IMessage::Ptr_t

@ -4,10 +4,6 @@ namespace llarp
{ {
namespace exit namespace exit
{ {
Policy::~Policy()
{
}
bool bool
Policy::BEncode(llarp_buffer_t *buf) const Policy::BEncode(llarp_buffer_t *buf) const
{ {

@ -7,19 +7,24 @@ namespace llarp
{ {
namespace exit 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; bool
uint64_t port; BDecode(llarp_buffer_t* buf)
uint64_t drop; {
return bencode_decode_dict(*this, buf);
}
bool bool
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override; DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val);
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
}; };
} // namespace exit } // namespace exit
} // namespace llarp } // namespace llarp

@ -1,6 +1,7 @@
#include <messages/transfer_traffic.hpp> #include <messages/transfer_traffic.hpp>
#include <routing/handler.hpp> #include <routing/handler.hpp>
#include <util/bencode.hpp>
#include <util/endian.hpp> #include <util/endian.hpp>
namespace llarp namespace llarp

@ -13,11 +13,11 @@ namespace llarp
struct AbstractRouter; struct AbstractRouter;
/// parsed link layer message /// parsed link layer message
struct ILinkMessage : public IBEncodeMessage struct ILinkMessage
{ {
/// who did this message come from or is going to /// who did this message come from or is going to
ILinkSession* session = nullptr; ILinkSession* session = nullptr;
uint64_t version = 0; uint64_t version = LLARP_PROTO_VERSION;
ILinkMessage() = default; 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 virtual bool
HandleMessage(AbstractRouter* router) const = 0; HandleMessage(AbstractRouter* router) const = 0;

@ -152,7 +152,7 @@ namespace llarp
} }
work = std::make_unique< PoW >(); work = std::make_unique< PoW >();
return work->BDecode(buffer); return bencode_decode_dict(*work, buffer);
} }
return read; return read;
} }

@ -12,22 +12,6 @@
namespace llarp 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 bool
operator==(const AddressInfo &lhs, const AddressInfo &rhs) operator==(const AddressInfo &lhs, const AddressInfo &rhs)
{ {

@ -19,38 +19,26 @@
/// address information model /// address information model
namespace llarp namespace llarp
{ {
struct AddressInfo final : public IBEncodeMessage struct AddressInfo
{ {
uint16_t rank; uint16_t rank;
std::string dialect; std::string dialect;
llarp::PubKey pubkey; llarp::PubKey pubkey;
struct in6_addr ip; struct in6_addr ip;
uint16_t port; uint16_t port;
uint64_t version = LLARP_PROTO_VERSION;
AddressInfo() : IBEncodeMessage() bool
{ BDecode(llarp_buffer_t* buf)
}
AddressInfo(const AddressInfo& other)
: IBEncodeMessage(other.version)
, rank(other.rank)
, dialect(other.dialect)
, pubkey(other.pubkey)
, port(other.port)
{ {
memcpy(ip.s6_addr, other.ip.s6_addr, 16); return bencode_decode_dict(*this, buf);
} }
~AddressInfo();
AddressInfo&
operator=(const AddressInfo& other);
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
bool bool
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override; DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf);
std::ostream& std::ostream&
print(std::ostream& stream, int level, int spaces) const; print(std::ostream& stream, int level, int spaces) const;

@ -12,20 +12,6 @@
namespace llarp 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 bool
ExitInfo::BEncode(llarp_buffer_t* buf) const ExitInfo::BEncode(llarp_buffer_t* buf) const
{ {

@ -16,14 +16,18 @@
/// Exit info model /// Exit info model
namespace llarp namespace llarp
{ {
struct ExitInfo final : public IBEncodeMessage struct ExitInfo
{ {
struct in6_addr address; struct in6_addr address;
struct in6_addr netmask; struct in6_addr netmask;
PubKey pubkey; PubKey pubkey;
uint64_t version = LLARP_PROTO_VERSION;
ExitInfo(const PubKey &pk, const nuint32_t &ipv4_exit) ExitInfo()
: IBEncodeMessage(), pubkey(pk) {
}
ExitInfo(const PubKey &pk, const nuint32_t &ipv4_exit) : pubkey(pk)
{ {
memset(address.s6_addr, 0, 16); memset(address.s6_addr, 0, 16);
address.s6_addr[11] = 0xff; address.s6_addr[11] = 0xff;
@ -32,27 +36,17 @@ namespace llarp
memset(netmask.s6_addr, 0xff, 16); memset(netmask.s6_addr, 0xff, 16);
} }
ExitInfo() : IBEncodeMessage() bool
{ BEncode(llarp_buffer_t *buf) const;
}
ExitInfo(const ExitInfo &other) bool
: IBEncodeMessage(other.version), pubkey(other.pubkey) BDecode(llarp_buffer_t *buf)
{ {
memcpy(address.s6_addr, other.address.s6_addr, 16); return bencode_decode_dict(*this, buf);
memcpy(netmask.s6_addr, other.netmask.s6_addr, 16);
} }
~ExitInfo();
bool bool
BEncode(llarp_buffer_t *buf) const override; DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf);
bool
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf) override;
ExitInfo &
operator=(const ExitInfo &other);
std::ostream & std::ostream &
print(std::ostream &stream, int level, int spaces) const; print(std::ostream &stream, int level, int spaces) const;

@ -3,17 +3,18 @@
#include <crypto/crypto.hpp> #include <crypto/crypto.hpp>
#include <router_id.hpp> #include <router_id.hpp>
#include <util/bencode.hpp> #include <util/buffer.hpp>
namespace llarp namespace llarp
{ {
/// proof of work /// proof of work
struct PoW final : public IBEncodeMessage struct PoW
{ {
static constexpr size_t MaxSize = 128; static constexpr size_t MaxSize = 128;
uint64_t timestamp = 0; uint64_t timestamp = 0;
uint32_t extendedLifetime = 0; uint32_t extendedLifetime = 0;
AlignedBuffer< 32 > nonce; AlignedBuffer< 32 > nonce;
uint64_t version = LLARP_PROTO_VERSION;
~PoW(); ~PoW();
@ -21,10 +22,10 @@ namespace llarp
IsValid(shorthash_func hashfunc, llarp_time_t now) const; IsValid(shorthash_func hashfunc, llarp_time_t now) const;
bool bool
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override; DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val);
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
bool bool
operator==(const PoW& other) const operator==(const PoW& other) const

@ -96,12 +96,7 @@ namespace llarp
return checkIsGood(pathFailCount, pathSuccessCount, chances); return checkIsGood(pathFailCount, pathSuccessCount, chances);
} }
Profiling::Profiling() : IBEncodeMessage() Profiling::Profiling() : m_DisableProfiling(false)
{
m_DisableProfiling.store(false);
}
Profiling::~Profiling()
{ {
} }
@ -267,7 +262,7 @@ namespace llarp
if(k.sz != 32) if(k.sz != 32)
return false; return false;
RouterProfile profile; RouterProfile profile;
if(!profile.BDecode(buf)) if(!bencode_decode_dict(profile, buf))
return false; return false;
RouterID pk = k.base; RouterID pk = k.base;
return m_Profiles.emplace(pk, profile).second; return m_Profiles.emplace(pk, profile).second;
@ -278,7 +273,7 @@ namespace llarp
{ {
lock_t lock(&m_ProfilesMutex); lock_t lock(&m_ProfilesMutex);
m_Profiles.clear(); m_Profiles.clear();
if(!BDecodeReadFile(fname, *this)) if(!BDecodeReadFromFile(fname, *this))
{ {
llarp::LogWarn("failed to load router profiles from ", fname); llarp::LogWarn("failed to load router profiles from ", fname);
return false; return false;

@ -11,7 +11,7 @@
namespace llarp namespace llarp
{ {
struct RouterProfile final : public IBEncodeMessage struct RouterProfile
{ {
static constexpr size_t MaxSize = 256; static constexpr size_t MaxSize = 256;
uint64_t connectTimeoutCount = 0; uint64_t connectTimeoutCount = 0;
@ -20,18 +20,13 @@ namespace llarp
uint64_t pathFailCount = 0; uint64_t pathFailCount = 0;
llarp_time_t lastUpdated = 0; llarp_time_t lastUpdated = 0;
llarp_time_t lastDecay = 0; llarp_time_t lastDecay = 0;
uint64_t version = LLARP_PROTO_VERSION;
RouterProfile() : IBEncodeMessage()
{
}
~RouterProfile() = default;
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
bool bool
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override; DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf);
bool bool
IsGood(uint64_t chances) const; IsGood(uint64_t chances) const;
@ -51,17 +46,16 @@ namespace llarp
Tick(); Tick();
}; };
struct Profiling final : public IBEncodeMessage struct Profiling
{ {
Profiling(); Profiling();
~Profiling();
/// generic variant /// generic variant
bool bool
IsBad(const RouterID& r, uint64_t chances = 8) IsBad(const RouterID& r, uint64_t chances = 8)
LOCKS_EXCLUDED(m_ProfilesMutex); 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 bool
IsBadForPath(const RouterID& r, uint64_t chances = 8) IsBadForPath(const RouterID& r, uint64_t chances = 8)
LOCK_RETURNED(m_ProfilesMutex); LOCK_RETURNED(m_ProfilesMutex);
@ -90,11 +84,12 @@ namespace llarp
Tick() LOCKS_EXCLUDED(m_ProfilesMutex); Tick() LOCKS_EXCLUDED(m_ProfilesMutex);
bool bool
BEncode(llarp_buffer_t* buf) const override LOCKS_EXCLUDED(m_ProfilesMutex); BEncode(llarp_buffer_t* buf) const LOCKS_EXCLUDED(m_ProfilesMutex);
bool bool
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override DecodeKey(const llarp_buffer_t& k,
SHARED_LOCKS_REQUIRED(m_ProfilesMutex); llarp_buffer_t* buf) NO_THREAD_SAFETY_ANALYSIS;
// disabled because we do load -> bencode::BDecodeReadFromFile -> DecodeKey
bool bool
Load(const char* fname) LOCKS_EXCLUDED(m_ProfilesMutex); Load(const char* fname) LOCKS_EXCLUDED(m_ProfilesMutex);

@ -506,7 +506,7 @@ namespace llarp
LogDebug("verify RC signature"); LogDebug("verify RC signature");
if(!_rc.Verify(crypto(), Now())) if(!_rc.Verify(crypto(), Now()))
{ {
rc().Dump< MAX_RC_SIZE >(); Dump< MAX_RC_SIZE >(rc());
LogError("RC is invalid, not saving"); LogError("RC is invalid, not saving");
return false; return false;
} }

@ -65,31 +65,18 @@ namespace llarp
} }
/// RouterContact /// RouterContact
struct RouterContact final : public IBEncodeMessage struct RouterContact
{ {
/// for unit tests /// for unit tests
static bool IgnoreBogons; static bool IgnoreBogons;
static llarp_time_t Lifetime; static llarp_time_t Lifetime;
RouterContact() : IBEncodeMessage() RouterContact()
{ {
Clear(); 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 struct Hash
{ {
size_t size_t
@ -115,12 +102,13 @@ namespace llarp
llarp::AlignedBuffer< NICKLEN > nickname; llarp::AlignedBuffer< NICKLEN > nickname;
uint64_t last_updated = 0; uint64_t last_updated = 0;
uint64_t version = LLARP_PROTO_VERSION;
util::StatusObject util::StatusObject
ExtractStatus() const; ExtractStatus() const;
bool bool
BEncode(llarp_buffer_t *buf) const override; BEncode(llarp_buffer_t *buf) const;
bool bool
operator==(const RouterContact &other) const operator==(const RouterContact &other) const
@ -153,14 +141,14 @@ namespace llarp
} }
bool bool
BDecode(llarp_buffer_t *buf) override BDecode(llarp_buffer_t *buf)
{ {
Clear(); Clear();
return IBEncodeMessage::BDecode(buf); return bencode_decode_dict(*this, buf);
} }
bool bool
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf) override; DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf);
RouterContact & RouterContact &
operator=(const RouterContact &other); operator=(const RouterContact &other);

@ -1,8 +1,8 @@
#ifndef LLARP_ROUTING_MESSAGE_HPP #ifndef LLARP_ROUTING_MESSAGE_HPP
#define LLARP_ROUTING_MESSAGE_HPP #define LLARP_ROUTING_MESSAGE_HPP
#include <constants/proto.hpp>
#include <path/path_types.hpp> #include <path/path_types.hpp>
#include <util/bencode.hpp>
#include <util/buffer.hpp> #include <util/buffer.hpp>
namespace llarp namespace llarp
@ -12,12 +12,13 @@ namespace llarp
{ {
struct IMessageHandler; struct IMessageHandler;
struct IMessage : public llarp::IBEncodeMessage struct IMessage
{ {
PathID_t from; PathID_t from;
uint64_t S; 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 virtual bool
HandleMessage(IMessageHandler* h, AbstractRouter* r) const = 0; HandleMessage(IMessageHandler* h, AbstractRouter* r) const = 0;

@ -1,5 +1,6 @@
#include <messages/path_latency.hpp> #include <messages/path_latency.hpp>
#include <routing/handler.hpp> #include <routing/handler.hpp>
#include <util/bencode.hpp>
namespace llarp namespace llarp
{ {

@ -120,7 +120,7 @@ namespace llarp
return false; return false;
// decode // decode
inf.read((char*)buf.base, sz); inf.read((char*)buf.base, sz);
if(!BDecode(&buf)) if(!bencode_decode_dict(*this, &buf))
return false; return false;
ServiceInfo::OptNonce van; ServiceInfo::OptNonce van;

@ -1,11 +1,12 @@
#ifndef LLARP_SERVICE_IDENTITY_HPP #ifndef LLARP_SERVICE_IDENTITY_HPP
#define LLARP_SERVICE_IDENTITY_HPP #define LLARP_SERVICE_IDENTITY_HPP
#include <constants/proto.hpp>
#include <crypto/types.hpp> #include <crypto/types.hpp>
#include <service/info.hpp> #include <service/info.hpp>
#include <service/intro_set.hpp> #include <service/intro_set.hpp>
#include <service/vanity.hpp> #include <service/vanity.hpp>
#include <util/bencode.hpp> #include <util/buffer.hpp>
#include <tuple> #include <tuple>
@ -16,12 +17,12 @@ namespace llarp
namespace service namespace service
{ {
// private keys // private keys
struct Identity final : public IBEncodeMessage struct Identity
{ {
SecretKey enckey; SecretKey enckey;
SecretKey signkey; SecretKey signkey;
PQKeyPair pq; PQKeyPair pq;
uint64_t version = 0; uint64_t version = LLARP_PROTO_VERSION;
VanityNonce vanity; VanityNonce vanity;
// public service info // public service info
@ -32,7 +33,7 @@ namespace llarp
RegenerateKeys(Crypto* c); RegenerateKeys(Crypto* c);
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
bool bool
EnsureKeys(const std::string& fpath, Crypto* c); EnsureKeys(const std::string& fpath, Crypto* c);
@ -42,7 +43,7 @@ namespace llarp
const ServiceInfo& other, const KeyExchangeNonce& N) const; const ServiceInfo& other, const KeyExchangeNonce& N) const;
bool bool
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
bool bool
SignIntroSet(IntroSet& i, Crypto* c, llarp_time_t now) const; SignIntroSet(IntroSet& i, Crypto* c, llarp_time_t now) const;

@ -14,7 +14,7 @@ namespace llarp
namespace service namespace service
{ {
struct ServiceInfo final : public IBEncodeMessage struct ServiceInfo
{ {
private: private:
PubKey enckey; PubKey enckey;
@ -22,30 +22,11 @@ namespace llarp
public: public:
VanityNonce vanity; VanityNonce vanity;
Address m_CachedAddr;
uint64_t version = LLARP_PROTO_VERSION;
using OptNonce = absl::optional< VanityNonce >; 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 void
RandomizeVanity() RandomizeVanity()
{ {
@ -126,21 +107,18 @@ namespace llarp
bool CalculateAddress(std::array< byte_t, 32 >& data) const; bool CalculateAddress(std::array< byte_t, 32 >& data) const;
bool 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 CalculateAddress(m_CachedAddr.as_array());
return false; return false;
} }
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
bool bool
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
private:
Address m_CachedAddr;
}; };
inline std::ostream& inline std::ostream&

@ -4,10 +4,6 @@ namespace llarp
{ {
namespace service namespace service
{ {
Introduction::~Introduction()
{
}
util::StatusObject util::StatusObject
Introduction::ExtractStatus() const Introduction::ExtractStatus() const
{ {

@ -12,14 +12,13 @@ namespace llarp
{ {
namespace service namespace service
{ {
struct Introduction final : public IBEncodeMessage struct Introduction
{ {
PubKey router; PubKey router;
PathID_t pathID; PathID_t pathID;
uint64_t latency = 0; uint64_t latency = 0;
uint64_t expiresAt = 0; uint64_t expiresAt = 0;
uint64_t version = LLARP_PROTO_VERSION;
Introduction() = default;
util::StatusObject util::StatusObject
ExtractStatus() const; ExtractStatus() const;
@ -38,16 +37,20 @@ namespace llarp
return IsExpired(now); return IsExpired(now);
} }
~Introduction();
std::ostream& std::ostream&
print(std::ostream& stream, int level, int spaces) const; print(std::ostream& stream, int level, int spaces) const;
bool 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 bool
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
void void
Clear(); Clear();

@ -44,7 +44,7 @@ namespace llarp
if(key == "w") if(key == "w")
{ {
W = absl::make_optional< PoW >(); W = absl::make_optional< PoW >();
return W->BDecode(buf); return bencode_decode_dict(*W, buf);
} }
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))

@ -25,7 +25,7 @@ namespace llarp
constexpr std::size_t MAX_INTROSET_SIZE = 4096; constexpr std::size_t MAX_INTROSET_SIZE = 4096;
// 10 seconds clock skew permitted for introset expiration // 10 seconds clock skew permitted for introset expiration
constexpr llarp_time_t MAX_INTROSET_TIME_DELTA = (10 * 1000); constexpr llarp_time_t MAX_INTROSET_TIME_DELTA = (10 * 1000);
struct IntroSet final : public IBEncodeMessage struct IntroSet
{ {
ServiceInfo A; ServiceInfo A;
std::vector< Introduction > I; std::vector< Introduction > I;
@ -34,6 +34,7 @@ namespace llarp
llarp_time_t T = 0; llarp_time_t T = 0;
absl::optional< PoW > W; absl::optional< PoW > W;
Signature Z; Signature Z;
uint64_t version = LLARP_PROTO_VERSION;
bool bool
OtherIsNewer(const IntroSet& other) const OtherIsNewer(const IntroSet& other) const
@ -57,10 +58,16 @@ namespace llarp
IsExpired(llarp_time_t now) const; IsExpired(llarp_time_t now) const;
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
bool 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 bool
Verify(Crypto* crypto, llarp_time_t now) const; Verify(Crypto* crypto, llarp_time_t now) const;

@ -179,7 +179,7 @@ namespace llarp
Encrypted_t tmp = D; Encrypted_t tmp = D;
auto buf = tmp.Buffer(); auto buf = tmp.Buffer();
crypto->xchacha20(*buf, sharedkey, N); crypto->xchacha20(*buf, sharedkey, N);
return msg.BDecode(buf); return bencode_decode_dict(msg, buf);
} }
bool bool
@ -287,7 +287,7 @@ namespace llarp
// decrypt // decrypt
auto buf = frame.D.Buffer(); auto buf = frame.D.Buffer();
crypto->xchacha20(*buf, K, self->frame.N); 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"); LogError("failed to decode inner protocol message");
DumpBuffer(*buf); DumpBuffer(*buf);
@ -300,8 +300,8 @@ namespace llarp
{ {
LogError("intro frame has invalid signature Z=", self->frame.Z, LogError("intro frame has invalid signature Z=", self->frame.Z,
" from ", self->msg->sender.Addr()); " from ", self->msg->sender.Addr());
self->frame.Dump< MAX_PROTOCOL_MESSAGE_SIZE >(); Dump< MAX_PROTOCOL_MESSAGE_SIZE >(self->frame);
self->msg->Dump< MAX_PROTOCOL_MESSAGE_SIZE >(); Dump< MAX_PROTOCOL_MESSAGE_SIZE >(*self->msg);
self->msg.reset(); self->msg.reset();
delete self; delete self;
return; return;
@ -326,7 +326,7 @@ namespace llarp
self->msg->sender, self->frame.N)) self->msg->sender, self->frame.N))
{ {
LogError("x25519 key exchange failed"); LogError("x25519 key exchange failed");
self->frame.Dump< MAX_PROTOCOL_MESSAGE_SIZE >(); Dump< MAX_PROTOCOL_MESSAGE_SIZE >(self->frame);
self->msg.reset(); self->msg.reset();
delete self; delete self;
return; return;

@ -38,7 +38,7 @@ namespace llarp
constexpr ProtocolType eProtocolTraffic = 1UL; constexpr ProtocolType eProtocolTraffic = 1UL;
/// inner message /// inner message
struct ProtocolMessage final : public IBEncodeMessage struct ProtocolMessage
{ {
ProtocolMessage(const ConvoTag& tag); ProtocolMessage(const ConvoTag& tag);
ProtocolMessage(); ProtocolMessage();
@ -52,13 +52,14 @@ namespace llarp
/// local path we got this message from /// local path we got this message from
PathID_t srcPath; PathID_t srcPath;
ConvoTag tag; ConvoTag tag;
uint64_t seqno = 0; uint64_t seqno = 0;
uint64_t version = LLARP_PROTO_VERSION;
bool bool
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val);
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const;
void void
PutBuffer(const llarp_buffer_t& payload); PutBuffer(const llarp_buffer_t& payload);
@ -141,6 +142,12 @@ namespace llarp
bool bool
BEncode(llarp_buffer_t* buf) const override; BEncode(llarp_buffer_t* buf) const override;
bool
BDecode(llarp_buffer_t* buf)
{
return bencode_decode_dict(*this, buf);
}
void void
Clear() override Clear() override
{ {

@ -12,6 +12,10 @@
namespace llarp namespace llarp
{ {
template < typename List_t >
bool
BEncodeReadList(List_t& result, llarp_buffer_t* buf);
inline bool inline bool
BEncodeWriteDictMsgType(llarp_buffer_t* buf, const char* k, const char* t) BEncodeWriteDictMsgType(llarp_buffer_t* buf, const char* k, const char* t)
{ {
@ -185,6 +189,24 @@ namespace llarp
return sink(buffer, nullptr); 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 > template < typename Sink >
bool bool
bencode_read_list(Sink&& sink, llarp_buffer_t* buffer) bencode_read_list(Sink&& sink, llarp_buffer_t* buffer)
@ -255,69 +277,50 @@ namespace llarp
&& BEncodeWriteList(list.begin(), list.end(), buf); && BEncodeWriteList(list.begin(), list.end(), buf);
} }
/// bencode serializable message template < size_t bufsz, typename T >
struct IBEncodeMessage void
Dump(const T& val)
{ {
virtual ~IBEncodeMessage() std::array< byte_t, bufsz > tmp;
{ llarp_buffer_t buf(tmp);
} if(val.BEncode(&buf))
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)
{ {
if(k == nullptr) llarp::DumpBuffer< decltype(buf), 128 >(buf);
return true;
if(DecodeKey(*k, val))
return true;
llarp::LogWarnTag("llarp/bencode.hpp", "undefined key '", *k->cur,
"' for entry in dict");
return false;
} }
}
template < size_t bufsz, size_t align = 128 > /// read entire file and decode its contents into t
void template < typename T >
Dump() const bool
BDecodeReadFile(const char* fpath, T& t)
{
std::vector< byte_t > ptr;
{ {
std::array< byte_t, bufsz > tmp; std::ifstream f;
llarp_buffer_t buf(tmp); f.open(fpath);
if(BEncode(&buf)) 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 /// read entire file and decode its contents into t
template < typename T > template < typename T >
bool bool
BDecodeReadFile(const char* fpath, T& t) BDecodeReadFromFile(const char* fpath, T& t)
{ {
std::vector< byte_t > ptr; std::vector< byte_t > ptr;
{ {
@ -334,7 +337,7 @@ namespace llarp
f.read((char*)ptr.data(), sz); f.read((char*)ptr.data(), sz);
} }
llarp_buffer_t buf(ptr); llarp_buffer_t buf(ptr);
auto result = t.BDecode(&buf); auto result = bencode_decode_dict(t, &buf);
if(!result) if(!result)
{ {
DumpBuffer(buf); DumpBuffer(buf);

Loading…
Cancel
Save