Disable copy constructing llarp_buffer_t

pull/266/head
Michael 5 years ago
parent 1ae91e8236
commit 2de621b0ad
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -28,7 +28,8 @@ namespace llarp
SharedSecret &, const PubKey &, const SecretKey &, const TunnelNonce &) >;
/// SH(result, body)
using shorthash_func = std::function< bool(ShortHash &, llarp_buffer_t) >;
using shorthash_func =
std::function< bool(ShortHash &, const llarp_buffer_t &) >;
/// library crypto configuration
struct Crypto
@ -37,12 +38,13 @@ namespace llarp
/// xchacha symmetric cipher
virtual bool
xchacha20(llarp_buffer_t, const SharedSecret &, const TunnelNonce &) = 0;
xchacha20(const llarp_buffer_t &, const SharedSecret &,
const TunnelNonce &) = 0;
/// xchacha symmetric cipher (multibuffer)
virtual bool
xchacha20_alt(llarp_buffer_t, llarp_buffer_t, const SharedSecret &,
const byte_t *) = 0;
xchacha20_alt(const llarp_buffer_t &, const llarp_buffer_t &,
const SharedSecret &, const byte_t *) = 0;
/// path dh creator's side
virtual bool
@ -62,24 +64,25 @@ namespace llarp
const TunnelNonce &) = 0;
/// blake2b 512 bit
virtual bool
hash(byte_t *, llarp_buffer_t) = 0;
hash(byte_t *, const llarp_buffer_t &) = 0;
/// blake2b 256 bit
virtual bool
shorthash(ShortHash &, llarp_buffer_t) = 0;
shorthash(ShortHash &, const llarp_buffer_t &) = 0;
/// blake2s 256 bit hmac
virtual bool
hmac(byte_t *, llarp_buffer_t, const SharedSecret &) = 0;
hmac(byte_t *, const llarp_buffer_t &, const SharedSecret &) = 0;
/// ed25519 sign
virtual bool
sign(Signature &, const SecretKey &, llarp_buffer_t) = 0;
sign(Signature &, const SecretKey &, const llarp_buffer_t &) = 0;
/// ed25519 verify
virtual bool
verify(const PubKey &, llarp_buffer_t, const Signature &) = 0;
verify(const PubKey &, const llarp_buffer_t &, const Signature &) = 0;
/// seed to secretkey
virtual bool
seed_to_secretkey(llarp::SecretKey &, const llarp::IdentitySecret &) = 0;
/// randomize buffer
virtual void randomize(llarp_buffer_t) = 0;
virtual void
randomize(const llarp_buffer_t &) = 0;
/// randomizer memory
virtual void
randbytes(void *, size_t) = 0;

@ -89,8 +89,8 @@ namespace llarp
}
bool
CryptoLibSodium::xchacha20(llarp_buffer_t buff, const SharedSecret &k,
const TunnelNonce &n)
CryptoLibSodium::xchacha20(const llarp_buffer_t &buff,
const SharedSecret &k, const TunnelNonce &n)
{
return crypto_stream_xchacha20_xor(buff.base, buff.base, buff.sz,
n.data(), k.data())
@ -98,7 +98,8 @@ namespace llarp
}
bool
CryptoLibSodium::xchacha20_alt(llarp_buffer_t out, llarp_buffer_t in,
CryptoLibSodium::xchacha20_alt(const llarp_buffer_t &out,
const llarp_buffer_t &in,
const SharedSecret &k, const byte_t *n)
{
if(in.sz > out.sz)
@ -138,7 +139,7 @@ namespace llarp
}
bool
CryptoLibSodium::hash(uint8_t *result, llarp_buffer_t buff)
CryptoLibSodium::hash(uint8_t *result, const llarp_buffer_t &buff)
{
return crypto_generichash_blake2b(result, HASHSIZE, buff.base, buff.sz,
nullptr, 0)
@ -146,7 +147,7 @@ namespace llarp
}
bool
CryptoLibSodium::shorthash(ShortHash &result, llarp_buffer_t buff)
CryptoLibSodium::shorthash(ShortHash &result, const llarp_buffer_t &buff)
{
return crypto_generichash_blake2b(result.data(), ShortHash::SIZE,
buff.base, buff.sz, nullptr, 0)
@ -154,7 +155,7 @@ namespace llarp
}
bool
CryptoLibSodium::hmac(byte_t *result, llarp_buffer_t buff,
CryptoLibSodium::hmac(byte_t *result, const llarp_buffer_t &buff,
const SharedSecret &secret)
{
return crypto_generichash_blake2b(result, HMACSIZE, buff.base, buff.sz,
@ -164,7 +165,7 @@ namespace llarp
bool
CryptoLibSodium::sign(Signature &result, const SecretKey &secret,
llarp_buffer_t buff)
const llarp_buffer_t &buff)
{
int rc = crypto_sign_detached(result.data(), nullptr, buff.base, buff.sz,
secret.data());
@ -172,7 +173,7 @@ namespace llarp
}
bool
CryptoLibSodium::verify(const PubKey &pub, llarp_buffer_t buff,
CryptoLibSodium::verify(const PubKey &pub, const llarp_buffer_t &buff,
const Signature &sig)
{
int rc = crypto_sign_verify_detached(sig.data(), buff.base, buff.sz,
@ -190,7 +191,7 @@ namespace llarp
}
void
CryptoLibSodium::randomize(llarp_buffer_t buff)
CryptoLibSodium::randomize(const llarp_buffer_t &buff)
{
randombytes((unsigned char *)buff.base, buff.sz);
}

@ -17,13 +17,13 @@ namespace llarp
/// xchacha symmetric cipher
bool
xchacha20(llarp_buffer_t, const SharedSecret &,
xchacha20(const llarp_buffer_t &, const SharedSecret &,
const TunnelNonce &) override;
/// xchacha symmetric cipher (multibuffer)
bool
xchacha20_alt(llarp_buffer_t, llarp_buffer_t, const SharedSecret &,
const byte_t *) override;
xchacha20_alt(const llarp_buffer_t &, const llarp_buffer_t &,
const SharedSecret &, const byte_t *) override;
/// path dh creator's side
bool
@ -43,25 +43,27 @@ namespace llarp
const TunnelNonce &) override;
/// blake2b 512 bit
bool
hash(byte_t *, llarp_buffer_t) override;
hash(byte_t *, const llarp_buffer_t &) override;
/// blake2b 256 bit
bool
shorthash(ShortHash &, llarp_buffer_t) override;
shorthash(ShortHash &, const llarp_buffer_t &) override;
/// blake2s 256 bit hmac
bool
hmac(byte_t *, llarp_buffer_t, const SharedSecret &) override;
hmac(byte_t *, const llarp_buffer_t &, const SharedSecret &) override;
/// ed25519 sign
bool
sign(Signature &, const SecretKey &, llarp_buffer_t) override;
sign(Signature &, const SecretKey &, const llarp_buffer_t &) override;
/// ed25519 verify
bool
verify(const PubKey &, llarp_buffer_t, const Signature &) override;
verify(const PubKey &, const llarp_buffer_t &,
const Signature &) override;
/// seed to secretkey
bool
seed_to_secretkey(llarp::SecretKey &,
const llarp::IdentitySecret &) override;
/// randomize buffer
void randomize(llarp_buffer_t) override;
void
randomize(const llarp_buffer_t &) override;
/// randomizer memory
void
randbytes(void *, size_t) override;

@ -138,7 +138,7 @@ namespace llarp
llarp_buffer_t
Buffer() const
{
return m_Buffer;
return m_Buffer.clone();
}
size_t

@ -12,7 +12,7 @@ namespace llarp
}
bool
FindIntroMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* val)
FindIntroMessage::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* val)
{
bool read = false;

@ -51,7 +51,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
bool
HandleMessage(

@ -94,7 +94,7 @@ namespace llarp
}
bool
FindRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
FindRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val)
{
llarp_buffer_t strbuf;

@ -31,7 +31,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
virtual bool
HandleMessage(

@ -80,7 +80,7 @@ namespace llarp
}
bool
GotIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
GotIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
if(llarp_buffer_eq(key, "I"))
{

@ -40,7 +40,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
virtual bool
HandleMessage(

@ -49,7 +49,7 @@ namespace llarp
}
bool
GotRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
GotRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val)
{
if(llarp_buffer_eq(key, "K"))
{

@ -38,7 +38,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
virtual bool
HandleMessage(

@ -15,7 +15,7 @@ namespace llarp
}
bool
PublishIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
PublishIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val)
{
bool read = false;
if(llarp_buffer_eq(key, "E"))

@ -35,7 +35,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
virtual bool
HandleMessage(

@ -629,12 +629,12 @@ extern "C"
if(hdr.qr)
{
llarp::LogDebug("handling as dnsc answer");
llarp_handle_dnsc_recvfrom(udp, addr, buf);
llarp_handle_dnsc_recvfrom(udp, addr, buf.clone());
}
else
{
llarp::LogDebug("handling as dnsd question");
llarp_handle_dnsd_recvfrom(udp, addr, buf);
llarp_handle_dnsd_recvfrom(udp, addr, buf.clone());
}
}
}

@ -151,7 +151,7 @@ answer_request_alloc(struct dnsc_context *dnsc, void *sock, const char *url,
/// generic dnsc handler
void
generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
llarp_buffer_t buffer, dns_msg_header *hdr)
const llarp_buffer_t &buffer, dns_msg_header *hdr)
{
if(!request)
{

@ -71,7 +71,7 @@ raw_handle_recvfrom(int *sockfd, const struct sockaddr *addr, const void *buf,
// removed saddr, if needed get through request
void
generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
llarp_buffer_t buffer, dns_msg_header *hdr);
const llarp_buffer_t &buffer, dns_msg_header *hdr);
/// DNS client context (one needed per upstream DNS server)
struct dnsc_context

@ -115,7 +115,7 @@ llarp_ev_loop_stop(struct llarp_ev_loop *loop)
int
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
llarp_buffer_t buf)
const llarp_buffer_t &buf)
{
auto ret =
static_cast< llarp::ev_io * >(udp->impl)->sendto(to, buf.base, buf.sz);

@ -88,7 +88,7 @@ llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp,
/// send a UDP packet
int
llarp_ev_udp_sendto(struct llarp_udp_io *udp, const struct sockaddr *to,
llarp_buffer_t pkt);
const llarp_buffer_t &pkt);
/// close UDP handler
int
@ -203,7 +203,7 @@ struct llarp_tun_io
void (*before_write)(struct llarp_tun_io *);
/// called every event loop tick after reads
void (*tick)(struct llarp_tun_io *);
void (*recvpkt)(struct llarp_tun_io *, llarp_buffer_t);
void (*recvpkt)(struct llarp_tun_io *, const llarp_buffer_t &);
};
/// create tun interface with network interface name ifname

@ -24,7 +24,7 @@ namespace llarp
}
bool
CloseExitMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
CloseExitMessage::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, k, buf))

@ -26,7 +26,7 @@ namespace llarp
}
bool
GrantExitMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
GrantExitMessage::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, k, buf))

@ -67,7 +67,7 @@ namespace llarp
}
bool
ObtainExitMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
ObtainExitMessage::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictList("B", B, read, k, buf))

@ -25,7 +25,7 @@ namespace llarp
}
bool
Policy::DecodeKey(llarp_buffer_t k, llarp_buffer_t *buf)
Policy::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("a", proto, read, k, buf))

@ -16,7 +16,7 @@ namespace llarp
uint64_t drop;
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override;
bool
BEncode(llarp_buffer_t* buf) const override;

@ -30,7 +30,7 @@ namespace llarp
}
bool
RejectExitMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
RejectExitMessage::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("B", B, read, k, buf))

@ -7,9 +7,10 @@ namespace llarp
{
namespace exit
{
BaseSession::BaseSession(const llarp::RouterID& router,
std::function< bool(llarp_buffer_t) > writepkt,
llarp::Router* r, size_t numpaths, size_t hoplen)
BaseSession::BaseSession(
const llarp::RouterID& router,
std::function< bool(const llarp_buffer_t&) > writepkt, llarp::Router* r,
size_t numpaths, size_t hoplen)
: llarp::path::Builder(r, r->dht, numpaths, hoplen)
, m_ExitRouter(router)
, m_WritePacket(writepkt)
@ -117,7 +118,7 @@ namespace llarp
}
bool
BaseSession::HandleTraffic(llarp::path::Path* p, llarp_buffer_t buf,
BaseSession::HandleTraffic(llarp::path::Path* p, const llarp_buffer_t& buf,
uint64_t counter)
{
(void)p;
@ -219,10 +220,10 @@ namespace llarp
return true;
}
SNodeSession::SNodeSession(const llarp::RouterID& snodeRouter,
std::function< bool(llarp_buffer_t) > writepkt,
llarp::Router* r, size_t numpaths, size_t hoplen,
bool useRouterSNodeKey)
SNodeSession::SNodeSession(
const llarp::RouterID& snodeRouter,
std::function< bool(const llarp_buffer_t&) > writepkt, llarp::Router* r,
size_t numpaths, size_t hoplen, bool useRouterSNodeKey)
: BaseSession(snodeRouter, writepkt, r, numpaths, hoplen)
{
if(useRouterSNodeKey)

@ -13,14 +13,14 @@ namespace llarp
{
namespace exit
{
/// a persisiting exit session with an exit router
/// a persisting exit session with an exit router
struct BaseSession : public llarp::path::Builder
{
static constexpr size_t MaxUpstreamQueueLength = 256;
static constexpr llarp_time_t LifeSpan = 60 * 10 * 1000;
BaseSession(const llarp::RouterID& exitRouter,
std::function< bool(llarp_buffer_t) > writepkt,
std::function< bool(const llarp_buffer_t&) > writepkt,
llarp::Router* r, size_t numpaths, size_t hoplen);
virtual ~BaseSession();
@ -64,7 +64,7 @@ namespace llarp
protected:
llarp::RouterID m_ExitRouter;
llarp::SecretKey m_ExitIdentity;
std::function< bool(llarp_buffer_t) > m_WritePacket;
std::function< bool(const llarp_buffer_t&) > m_WritePacket;
virtual void
PopulateRequest(llarp::routing::ObtainExitMessage& msg) const = 0;
@ -77,7 +77,8 @@ namespace llarp
HandleGotExit(llarp::path::Path* p, llarp_time_t b);
bool
HandleTraffic(llarp::path::Path* p, llarp_buffer_t buf, uint64_t seqno);
HandleTraffic(llarp::path::Path* p, const llarp_buffer_t& buf,
uint64_t seqno);
private:
using UpstreamTrafficQueue_t =
@ -108,7 +109,7 @@ namespace llarp
struct ExitSession final : public BaseSession
{
ExitSession(const llarp::RouterID& snodeRouter,
std::function< bool(llarp_buffer_t) > writepkt,
std::function< bool(const llarp_buffer_t&) > writepkt,
llarp::Router* r, size_t numpaths, size_t hoplen)
: BaseSession(snodeRouter, writepkt, r, numpaths, hoplen){};
@ -127,7 +128,7 @@ namespace llarp
struct SNodeSession final : public BaseSession
{
SNodeSession(const llarp::RouterID& snodeRouter,
std::function< bool(llarp_buffer_t) > writepkt,
std::function< bool(const llarp_buffer_t&) > writepkt,
llarp::Router* r, size_t numpaths, size_t hoplen,
bool useRouterSNodeKey = false);

@ -17,7 +17,8 @@ namespace llarp
}
bool
TransferTrafficMessage::PutBuffer(llarp_buffer_t buf, uint64_t counter)
TransferTrafficMessage::PutBuffer(const llarp_buffer_t& buf,
uint64_t counter)
{
if(buf.sz > MaxExitMTU)
return false;
@ -48,7 +49,8 @@ namespace llarp
}
bool
TransferTrafficMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
TransferTrafficMessage::DecodeKey(const llarp_buffer_t& key,
llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, key, buf))

@ -26,7 +26,7 @@ namespace llarp
}
bool
UpdateExitMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
UpdateExitMessage::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, k, buf))
@ -104,7 +104,7 @@ namespace llarp
}
bool
UpdateExitVerifyMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
UpdateExitVerifyMessage::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("S", S, read, k, buf))

@ -12,7 +12,7 @@ namespace llarp
namespace handlers
{
static void
ExitHandlerRecvPkt(llarp_tun_io *tun, llarp_buffer_t buf)
ExitHandlerRecvPkt(llarp_tun_io *tun, const llarp_buffer_t &buf)
{
static_cast< ExitEndpoint * >(tun->user)->OnInetPacket(buf);
}
@ -338,9 +338,9 @@ namespace llarp
}
bool
ExitEndpoint::QueueOutboundTraffic(llarp_buffer_t buf)
ExitEndpoint::QueueOutboundTraffic(const llarp_buffer_t &buf)
{
return llarp_ev_tun_async_write(&m_Tun, buf);
return llarp_ev_tun_async_write(&m_Tun, buf.clone());
}
void
@ -363,14 +363,15 @@ namespace llarp
}
void
ExitEndpoint::OnInetPacket(llarp_buffer_t buf)
ExitEndpoint::OnInetPacket(const llarp_buffer_t &buf)
{
m_InetToNetwork.EmplaceIf(
[buf](Pkt_t &pkt) -> bool { return pkt.Load(buf); });
m_InetToNetwork.EmplaceIf([b = CopyableBuffer(buf)](Pkt_t &pkt) -> bool {
return pkt.Load(b.underlying);
});
}
bool
ExitEndpoint::QueueSNodePacket(llarp_buffer_t buf, huint32_t from)
ExitEndpoint::QueueSNodePacket(const llarp_buffer_t &buf, huint32_t from)
{
net::IPv4Packet pkt;
if(!pkt.Load(buf))

@ -47,7 +47,7 @@ namespace llarp
/// handle ip packet from outside
void
OnInetPacket(llarp_buffer_t buf);
OnInetPacket(const llarp_buffer_t& buf);
Router*
GetRouter();
@ -80,7 +80,7 @@ namespace llarp
RemoveExit(const exit::Endpoint* ep);
bool
QueueOutboundTraffic(llarp_buffer_t buf);
QueueOutboundTraffic(const llarp_buffer_t& buf);
/// sets up networking and starts traffic
bool
@ -114,7 +114,7 @@ namespace llarp
ObtainServiceNodeIP(const RouterID& router);
bool
QueueSNodePacket(llarp_buffer_t buf, huint32_t from);
QueueSNodePacket(const llarp_buffer_t & buf, huint32_t from);
void
MarkIPActive(huint32_t ip);

@ -13,8 +13,9 @@ namespace llarp
llarp::service::Context *parent)
: llarp::service::Endpoint(name, r, parent){};
bool HandleWriteIPPacket(llarp_buffer_t,
std::function< huint32_t(void) >) override
bool
HandleWriteIPPacket(const llarp_buffer_t &,
std::function< huint32_t(void) >) override
{
return true;
}

@ -249,7 +249,7 @@ namespace llarp
llarp::service::Address addr;
// forward dns
if(msg.questions[0].qname == "localhost.loki"
|| msg.questions[0].qname == "localhost.loki.")
|| msg.questions[0].qname == "localhost.loki.")
{
size_t counter = 0;
context->ForEachService(
@ -344,7 +344,7 @@ namespace llarp
if(msg.questions[0].qname == "random.snode"
|| msg.questions[0].qname == "random.snode.")
return true;
// hook localhost.loki
// hook localhost.loki
if(msg.questions[0].qname == "localhost.loki"
|| msg.questions[0].qname == "localhost.loki.")
return true;
@ -570,17 +570,18 @@ namespace llarp
}
bool
TunEndpoint::HandleWriteIPPacket(llarp_buffer_t buf,
TunEndpoint::HandleWriteIPPacket(const llarp_buffer_t &b,
std::function< huint32_t(void) > getFromIP)
{
// llarp::LogInfo("got packet from ", msg->sender.Addr());
auto themIP = getFromIP();
// llarp::LogInfo("themIP ", themIP);
auto usIP = m_OurIP;
CopyableBuffer buf(b);
return m_NetworkToUserPktQueue.EmplaceIf(
[buf, themIP, usIP](net::IPv4Packet &pkt) -> bool {
// load
if(!pkt.Load(buf))
if(!pkt.Load(buf.underlying))
return false;
// filter out:
// - packets smaller than minimal IPv4 header
@ -719,18 +720,19 @@ namespace llarp
}
void
TunEndpoint::tunifRecvPkt(llarp_tun_io *tun, llarp_buffer_t buf)
TunEndpoint::tunifRecvPkt(llarp_tun_io *tun, const llarp_buffer_t &b)
{
// called for every packet read from user in isolated network thread
TunEndpoint *self = static_cast< TunEndpoint * >(tun->user);
CopyableBuffer buf(b);
if(!self->m_UserToNetworkPktQueue.EmplaceIf(
[buf](net::IPv4Packet &pkt) -> bool {
return pkt.Load(buf) && pkt.Header()->version == 4;
return pkt.Load(buf.underlying) && pkt.Header()->version == 4;
}))
{
#if defined(DEBUG) || !defined(RELEASE_MOTTO)
llarp::LogInfo("invalid pkt");
llarp::DumpBuffer(buf);
llarp::DumpBuffer(buf.underlying);
#endif
}
}

@ -64,7 +64,7 @@ namespace llarp
/// overrides Endpoint
/// handle inbound traffic
bool
HandleWriteIPPacket(llarp_buffer_t buf,
HandleWriteIPPacket(const llarp_buffer_t& buf,
std::function< huint32_t(void) > getFromIP) override;
/// queue outbound packet to the world
@ -99,7 +99,7 @@ namespace llarp
/// called every time we wish to read a packet from the tun interface
static void
tunifRecvPkt(llarp_tun_io* t, llarp_buffer_t buf);
tunifRecvPkt(llarp_tun_io* t, const llarp_buffer_t& buf);
/// called in the endpoint logic thread
static void
@ -176,11 +176,13 @@ namespace llarp
private:
bool
QueueInboundPacketForExit(llarp_buffer_t buf)
QueueInboundPacketForExit(const llarp_buffer_t& buf)
{
llarp_buffer_t copy = buf.clone();
return m_NetworkToUserPktQueue.EmplaceIf(
[&](llarp::net::IPv4Packet& pkt) -> bool {
if(!pkt.Load(buf))
if(!pkt.Load(copy))
return false;
pkt.UpdateIPv4PacketOnDst(pkt.src(), m_OurIP);
return true;

@ -35,7 +35,7 @@ namespace llarp
TickIO(llarp_time_t now);
bool
QueueMessageBuffer(llarp_buffer_t buf);
QueueMessageBuffer(const llarp_buffer_t& buf);
/// return true if the session is established and handshaked and all that
/// jazz
@ -184,7 +184,7 @@ namespace llarp
}
/// outbound
MessageState(llarp_buffer_t buf)
MessageState(const llarp_buffer_t &buf)
{
sz = std::min(buf.sz, MAX_LINK_MSG_SIZE);
memcpy(msg.data(), buf.base, sz);

@ -250,7 +250,7 @@ namespace llarp
}
bool
ILinkLayer::SendTo(const RouterID& remote, llarp_buffer_t buf)
ILinkLayer::SendTo(const RouterID& remote, const llarp_buffer_t& buf)
{
ILinkSession* s = nullptr;
{

@ -16,10 +16,11 @@ namespace llarp
{
/// handle a link layer message
using LinkMessageHandler =
std::function< bool(ILinkSession*, llarp_buffer_t) >;
std::function< bool(ILinkSession*, const llarp_buffer_t&) >;
/// sign a buffer with identity key
using SignBufferFunc = std::function< bool(Signature&, llarp_buffer_t) >;
using SignBufferFunc =
std::function< bool(Signature&, const llarp_buffer_t&) >;
/// handle connection timeout
using TimeoutHandler = std::function< void(ILinkSession*) >;
@ -128,7 +129,7 @@ namespace llarp
KeepAliveSessionTo(const RouterID& remote);
bool
SendTo(const RouterID& remote, llarp_buffer_t buf);
SendTo(const RouterID& remote, const llarp_buffer_t& buf);
bool
GetOurAddressInfo(AddressInfo& addr) const;

@ -24,7 +24,7 @@ namespace llarp
std::function< void(llarp_time_t) > Tick;
/// send a message buffer to the remote endpoint
std::function< bool(llarp_buffer_t) > SendMessageBuffer;
std::function< bool(const llarp_buffer_t &) > SendMessageBuffer;
/// start the connection
std::function< void(void) > Start;

@ -736,7 +736,7 @@ namespace llarp
}
bool
Session::QueueWriteBuffers(llarp_buffer_t buf)
Session::QueueWriteBuffers(const llarp_buffer_t& buf)
{
if(sendq.size() >= MaxSendQueueSize)
return false;
@ -1116,7 +1116,7 @@ namespace llarp
{
// we done with this guy, prune next tick
itr->second.lastActive = 0;
llarp_buffer_t buf = itr->second.buffer;
llarp_buffer_t buf(itr->second.buffer.clone());
// resize
buf.sz = buf.cur - buf.base;
// rewind

@ -197,7 +197,7 @@ namespace llarp
/// queue a fully formed message
bool
QueueWriteBuffers(llarp_buffer_t buf);
QueueWriteBuffers(const llarp_buffer_t& buf);
/// prune expired inbound messages
void

@ -18,7 +18,7 @@ namespace llarp
~DHTMessage();
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
bool
BEncode(llarp_buffer_t* buf) const override;

@ -15,7 +15,7 @@ namespace llarp
}
bool
DHTImmediateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
DHTImmediateMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
if(llarp_buffer_eq(key, "m"))
return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()),

@ -19,7 +19,7 @@ namespace llarp
std::vector< std::unique_ptr< llarp::dht::IMessage > > msgs;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
BEncode(llarp_buffer_t* buf) const override;

@ -32,7 +32,7 @@ namespace llarp
}
bool
DecodeKey(__attribute__((unused)) llarp_buffer_t key,
DecodeKey(__attribute__((unused)) const llarp_buffer_t& key,
__attribute__((unused)) llarp_buffer_t* buf) override
{
return false;
@ -73,7 +73,7 @@ namespace llarp
}
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf) override
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("P", P, read, k, buf))

@ -53,7 +53,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;
@ -86,7 +86,7 @@ namespace llarp
Verify(llarp::Crypto* c, const llarp::PubKey& pk) const;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;
@ -140,7 +140,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;
@ -182,7 +182,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;
@ -217,7 +217,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;
@ -254,7 +254,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;

@ -12,7 +12,7 @@ namespace llarp
}
bool
LinkIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, "a"))
{
@ -133,7 +133,7 @@ namespace llarp
bool
LinkIntroMessage::Sign(
std::function< bool(Signature&, llarp_buffer_t) > signer)
std::function< bool(Signature&, const llarp_buffer_t&) > signer)
{
Z.Zero();
byte_t tmp[MaxSize] = {0};

@ -28,7 +28,7 @@ namespace llarp
operator=(const LinkIntroMessage& msg);
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override;
bool
BEncode(llarp_buffer_t* buf) const override;
@ -37,7 +37,7 @@ namespace llarp
HandleMessage(llarp::Router* router) const override;
bool
Sign(std::function< bool(Signature&, llarp_buffer_t) > signer);
Sign(std::function< bool(Signature&, const llarp_buffer_t&) > signer);
bool
Verify(llarp::Crypto* c) const;

@ -90,7 +90,8 @@ namespace llarp
}
bool
InboundMessageParser::ProcessFrom(ILinkSession* src, llarp_buffer_t buf)
InboundMessageParser::ProcessFrom(ILinkSession* src,
const llarp_buffer_t& buf)
{
if(!src)
{
@ -101,7 +102,8 @@ namespace llarp
reader.on_key = &OnKey;
from = src;
firstkey = true;
return bencode_read_dict(&buf, &reader);
llarp_buffer_t copy(buf.clone());
return bencode_read_dict(&copy, &reader);
}
void

@ -20,7 +20,7 @@ namespace llarp
/// start processig message from a link session
bool
ProcessFrom(ILinkSession* from, llarp_buffer_t buf);
ProcessFrom(ILinkSession* from, const llarp_buffer_t& buf);
/// called when the message is fully read
/// return true when the message was accepted otherwise returns false

@ -19,7 +19,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;

@ -17,7 +17,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
void
Clear() override

@ -25,7 +25,7 @@ namespace llarp
~PathTransferMessage();
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val) override;
bool
BEncode(llarp_buffer_t* buf) const override;

@ -41,7 +41,7 @@ namespace llarp
}
bool
RelayUpstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
RelayUpstreamMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf))
@ -103,7 +103,7 @@ namespace llarp
}
bool
RelayDownstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
RelayDownstreamMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf))

@ -21,7 +21,7 @@ namespace llarp
~RelayUpstreamMessage();
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
BEncode(llarp_buffer_t* buf) const override;
@ -43,7 +43,7 @@ namespace llarp
~RelayDownstreamMessage();
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
BEncode(llarp_buffer_t* buf) const override;

@ -14,7 +14,7 @@ namespace llarp
}
bool
LR_CommitMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
LR_CommitMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, "c"))
{

@ -58,7 +58,7 @@ namespace llarp
Clear();
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf);
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf);
bool
BEncode(llarp_buffer_t *buf) const;

@ -36,13 +36,13 @@ namespace llarp
/// append buffer to X
bool
PutBuffer(llarp_buffer_t buf, uint64_t counter);
PutBuffer(const llarp_buffer_t& buf, uint64_t counter);
bool
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override;
bool
HandleMessage(IMessageHandler* h, llarp::Router* r) const override;

@ -42,7 +42,7 @@ namespace llarp
}
bool
AddressInfo::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
AddressInfo::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
uint64_t i;
char tmp[128] = {0};

@ -50,7 +50,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override;
friend std::ostream&
operator<<(std::ostream& out, const AddressInfo& a)

@ -68,7 +68,7 @@ namespace llarp
}
bool
ExitInfo::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
ExitInfo::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, k, buf))

@ -50,7 +50,7 @@ namespace llarp
BEncode(llarp_buffer_t *buf) const override;
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t *buf) override;
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf) override;
ExitInfo &
operator=(const ExitInfo &other);

@ -16,7 +16,7 @@ namespace llarp
namespace net
{
bool
IPv4Packet::Load(llarp_buffer_t pkt)
IPv4Packet::Load(const llarp_buffer_t &pkt)
{
if(pkt.sz > sizeof(buf))
return false;

@ -96,7 +96,7 @@ namespace llarp
ConstBuffer() const;
bool
Load(llarp_buffer_t buf);
Load(const llarp_buffer_t& buf);
struct GetTime
{

@ -503,7 +503,7 @@ namespace llarp
}
bool
Path::HandleUpstream(llarp_buffer_t buf, const TunnelNonce& Y,
Path::HandleUpstream(const llarp_buffer_t& buf, const TunnelNonce& Y,
llarp::Router* r)
{
TunnelNonce n = Y;
@ -542,7 +542,7 @@ namespace llarp
}
bool
Path::HandleDownstream(llarp_buffer_t buf, const TunnelNonce& Y,
Path::HandleDownstream(const llarp_buffer_t& buf, const TunnelNonce& Y,
llarp::Router* r)
{
TunnelNonce n = Y;
@ -555,7 +555,7 @@ namespace llarp
}
bool
Path::HandleRoutingMessage(llarp_buffer_t buf, llarp::Router* r)
Path::HandleRoutingMessage(const llarp_buffer_t& buf, llarp::Router* r)
{
if(!r->ParseRoutingMessageBuffer(buf, this, RXID()))
{

@ -116,12 +116,12 @@ namespace llarp
// handle data in upstream direction
virtual bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y,
HandleUpstream(const llarp_buffer_t& X, const TunnelNonce& Y,
llarp::Router* r) = 0;
// handle data in downstream direction
virtual bool
HandleDownstream(llarp_buffer_t X, const TunnelNonce& Y,
HandleDownstream(const llarp_buffer_t& X, const TunnelNonce& Y,
llarp::Router* r) = 0;
/// return timestamp last remote activity happened at
@ -256,12 +256,12 @@ namespace llarp
// handle data in upstream direction
bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y,
HandleUpstream(const llarp_buffer_t& X, const TunnelNonce& Y,
llarp::Router* r) override;
// handle data in downstream direction
bool
HandleDownstream(llarp_buffer_t X, const TunnelNonce& Y,
HandleDownstream(const llarp_buffer_t& X, const TunnelNonce& Y,
llarp::Router* r) override;
};
@ -302,7 +302,7 @@ namespace llarp
using ExitUpdatedFunc = std::function< bool(Path*) >;
using ExitClosedFunc = std::function< bool(Path*) >;
using ExitTrafficHandlerFunc =
std::function< bool(Path*, llarp_buffer_t, uint64_t) >;
std::function< bool(Path*, const llarp_buffer_t&, uint64_t) >;
/// (path, backoff) backoff is 0 on success
using ObtainedExitHandler = std::function< bool(Path*, llarp_time_t) >;
@ -475,16 +475,16 @@ namespace llarp
llarp::Router* r) override;
bool
HandleRoutingMessage(llarp_buffer_t buf, llarp::Router* r);
HandleRoutingMessage(const llarp_buffer_t& buf, llarp::Router* r);
// handle data in upstream direction
bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y,
HandleUpstream(const llarp_buffer_t& X, const TunnelNonce& Y,
llarp::Router* r) override;
// handle data in downstream direction
bool
HandleDownstream(llarp_buffer_t X, const TunnelNonce& Y,
HandleDownstream(const llarp_buffer_t& X, const TunnelNonce& Y,
llarp::Router* r) override;
bool

@ -83,7 +83,7 @@ namespace llarp
}
bool
TransitHop::HandleDownstream(llarp_buffer_t buf, const TunnelNonce& Y,
TransitHop::HandleDownstream(const llarp_buffer_t& buf, const TunnelNonce& Y,
llarp::Router* r)
{
RelayDownstreamMessage msg;
@ -97,7 +97,7 @@ namespace llarp
}
bool
TransitHop::HandleUpstream(llarp_buffer_t buf, const TunnelNonce& Y,
TransitHop::HandleUpstream(const llarp_buffer_t& buf, const TunnelNonce& Y,
llarp::Router* r)
{
r->crypto->xchacha20(buf, pathKey, Y);

@ -11,7 +11,7 @@ namespace llarp
}
bool
PoW::DecodeKey(__attribute__((unused)) llarp_buffer_t k,
PoW::DecodeKey(__attribute__((unused)) const llarp_buffer_t& k,
__attribute__((unused)) llarp_buffer_t* val)
{
// TODO: implement me

@ -21,7 +21,7 @@ namespace llarp
IsValid(shorthash_func hashfunc, llarp_time_t now) const;
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* val) override;
bool
BEncode(llarp_buffer_t* buf) const override;

@ -25,7 +25,7 @@ namespace llarp
}
bool
RouterProfile::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
RouterProfile::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("g", connectGoodCount, read, k, buf))
@ -134,7 +134,7 @@ namespace llarp
}
bool
Profiling::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
Profiling::DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf)
{
if(k.sz != 32)
return false;

@ -26,7 +26,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf) override;
bool
IsGood(uint64_t chances) const;
@ -55,7 +55,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t* buf) override;
bool
Load(const char* fname);

@ -233,7 +233,7 @@ namespace llarp
bool
Router::HandleRecvLinkMessageBuffer(llarp::ILinkSession *session,
llarp_buffer_t buf)
const llarp_buffer_t &buf)
{
if(_stopping)
return true;
@ -561,7 +561,7 @@ namespace llarp
}
bool
Router::ParseRoutingMessageBuffer(llarp_buffer_t buf,
Router::ParseRoutingMessageBuffer(const llarp_buffer_t &buf,
routing::IMessageHandler *h, PathID_t rxid)
{
return inbound_routing_msg_parser.ParseMessageBuffer(buf, h, rxid, this);
@ -816,7 +816,7 @@ namespace llarp
}
bool
Router::Sign(llarp::Signature &sig, llarp_buffer_t buf) const
Router::Sign(llarp::Signature &sig, const llarp_buffer_t &buf) const
{
return crypto->sign(sig, identity, buf);
}
@ -1006,7 +1006,7 @@ namespace llarp
}
if(whitelistRouters)
{
rpcCaller = std::make_unique<llarp::rpc::Caller>(this);
rpcCaller = std::make_unique< llarp::rpc::Caller >(this);
rpcCaller->SetBasicAuth(lokidRPCUser, lokidRPCPassword);
while(!rpcCaller->Start(lokidRPCAddr))
{
@ -1633,7 +1633,7 @@ namespace llarp
else if(StrEq(section, "connect")
|| (StrEq(section, "bootstrap") && StrEq(key, "add-node")))
{
//llarp::LogDebug("connect section has ", key, "=", val);
// llarp::LogDebug("connect section has ", key, "=", val);
self->bootstrapRCList.emplace_back();
auto &rc = self->bootstrapRCList.back();
if(!rc.Read(val))

@ -115,7 +115,7 @@ namespace llarp
llarp_dht_context *dht = nullptr;
bool
Sign(Signature &sig, llarp_buffer_t buf) const;
Sign(Signature &sig, const llarp_buffer_t &buf) const;
llarp_nodedb *nodedb;
@ -228,7 +228,8 @@ namespace llarp
OnSessionEstablished(llarp::RouterContact rc);
bool
HandleRecvLinkMessageBuffer(llarp::ILinkSession *from, llarp_buffer_t msg);
HandleRecvLinkMessageBuffer(llarp::ILinkSession *from,
const llarp_buffer_t &msg);
void
AddInboundLink(std::unique_ptr< llarp::ILinkLayer > &link);
@ -394,8 +395,8 @@ namespace llarp
/// successful parsing return true on parse and handle success otherwise
/// return false
bool
ParseRoutingMessageBuffer(llarp_buffer_t buf, routing::IMessageHandler *h,
PathID_t rxid);
ParseRoutingMessageBuffer(const llarp_buffer_t &buf,
routing::IMessageHandler *h, PathID_t rxid);
void
ConnectToRandomRouters(int N);

@ -152,7 +152,7 @@ namespace llarp
}
bool
RouterContact::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
RouterContact::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
bool read = false;
if(!BEncodeMaybeReadDictList("a", addrs, read, key, buf))

@ -127,7 +127,7 @@ namespace llarp
}
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t *buf) override;
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf) override;
RouterContact &
operator=(const RouterContact &other);

@ -11,7 +11,7 @@ namespace llarp
}
bool
DHTMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
DHTMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val)
{
llarp::dht::Key_t from;
from.Zero();

@ -87,7 +87,7 @@ namespace llarp
}
bool
InboundMessageParser::ParseMessageBuffer(llarp_buffer_t buf,
InboundMessageParser::ParseMessageBuffer(const llarp_buffer_t& buf,
IMessageHandler* h,
const PathID_t& from,
llarp::Router* r)
@ -95,7 +95,8 @@ namespace llarp
bool result = false;
msg = nullptr;
firstKey = true;
if(bencode_read_dict(&buf, &reader))
llarp_buffer_t copy(buf.clone());
if(bencode_read_dict(&copy, &reader))
{
msg->from = from;
result = msg->HandleMessage(h, r);

@ -23,7 +23,7 @@ namespace llarp
InboundMessageParser();
bool
ParseMessageBuffer(llarp_buffer_t buf, IMessageHandler* handler,
ParseMessageBuffer(const llarp_buffer_t& buf, IMessageHandler* handler,
const PathID_t& from, llarp::Router* r);
private:

@ -18,7 +18,7 @@ namespace llarp
}
bool
PathConfirmMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
PathConfirmMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("L", pathLifetime, read, key, val))

@ -10,7 +10,7 @@ namespace llarp
}
bool
PathLatencyMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
PathLatencyMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("L", L, read, key, val))

@ -16,7 +16,7 @@ namespace llarp
}
bool
PathTransferMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
PathTransferMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* val)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("P", P, read, key, val))

@ -29,7 +29,7 @@ namespace llarp
}
bool
Identity::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
Identity::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf))
@ -75,7 +75,7 @@ namespace llarp
}
bool
Identity::Sign(Crypto* c, Signature& sig, llarp_buffer_t buf) const
Identity::Sign(Crypto* c, Signature& sig, const llarp_buffer_t& buf) const
{
return c->sign(sig, signkey, buf);
}

@ -46,13 +46,13 @@ namespace llarp
const ServiceInfo& other, const KeyExchangeNonce& N) const;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override;
bool
SignIntroSet(IntroSet& i, llarp::Crypto* c, llarp_time_t now) const;
bool
Sign(llarp::Crypto*, Signature& sig, llarp_buffer_t buf) const;
Sign(llarp::Crypto*, Signature& sig, const llarp_buffer_t& buf) const;
};
} // namespace service
} // namespace llarp

@ -60,7 +60,7 @@ namespace llarp
}
bool
Verify(llarp::Crypto* crypto, llarp_buffer_t payload,
Verify(llarp::Crypto* crypto, const llarp_buffer_t& payload,
const Signature& sig) const;
const PubKey&
@ -148,7 +148,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override;
private:
Address m_CachedAddr;

@ -9,7 +9,7 @@ namespace llarp
}
bool
Introduction::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
Introduction::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("k", router, read, key, buf))

@ -56,7 +56,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
void
Clear();

@ -13,7 +13,7 @@ namespace llarp
}
bool
IntroSet::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
IntroSet::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf))

@ -149,7 +149,7 @@ namespace llarp
BEncode(llarp_buffer_t* buf) const override;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
DecodeKey(const llarp_buffer_t &key, llarp_buffer_t* buf) override;
bool
Verify(llarp::Crypto* crypto, llarp_time_t now) const;

@ -1157,7 +1157,7 @@ namespace llarp
}
bool
Endpoint::SendToSNodeOrQueue(const RouterID& addr, llarp_buffer_t buf)
Endpoint::SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& buf)
{
llarp::net::IPv4Packet pkt;
if(!pkt.Load(buf))
@ -1180,8 +1180,8 @@ namespace llarp
}
bool
Endpoint::SendToServiceOrQueue(const RouterID& addr, llarp_buffer_t data,
ProtocolType t)
Endpoint::SendToServiceOrQueue(const RouterID& addr,
const llarp_buffer_t& data, ProtocolType t)
{
service::Address remote(addr.as_array());
@ -1416,7 +1416,7 @@ namespace llarp
}
void
Endpoint::SendContext::AsyncEncryptAndSendTo(llarp_buffer_t data,
Endpoint::SendContext::AsyncEncryptAndSendTo(const llarp_buffer_t& data,
ProtocolType protocol)
{
auto now = m_Endpoint->Now();
@ -1536,7 +1536,7 @@ namespace llarp
}
void
Endpoint::OutboundContext::AsyncGenIntro(llarp_buffer_t payload,
Endpoint::OutboundContext::AsyncGenIntro(const llarp_buffer_t& payload,
__attribute__((unused))
ProtocolType t)
{
@ -1728,7 +1728,7 @@ namespace llarp
/// send on an established convo tag
void
Endpoint::SendContext::EncryptAndSendTo(llarp_buffer_t payload,
Endpoint::SendContext::EncryptAndSendTo(const llarp_buffer_t& payload,
ProtocolType t)
{
auto crypto = m_Endpoint->Router()->crypto.get();

@ -142,7 +142,7 @@ namespace llarp
HandleDataMessage(const PathID_t&, ProtocolMessage* msg) override;
virtual bool
HandleWriteIPPacket(llarp_buffer_t pkt,
HandleWriteIPPacket(const llarp_buffer_t& pkt,
std::function< huint32_t(void) > getFromIP) = 0;
bool
@ -173,11 +173,11 @@ namespace llarp
HandlePathBuilt(path::Path* path) override;
bool
SendToServiceOrQueue(const RouterID& addr, llarp_buffer_t payload,
SendToServiceOrQueue(const RouterID& addr, const llarp_buffer_t& payload,
ProtocolType t);
bool
SendToSNodeOrQueue(const RouterID& addr, llarp_buffer_t payload);
SendToSNodeOrQueue(const RouterID& addr, const llarp_buffer_t& payload);
void
FlushSNodeTraffic();
@ -187,7 +187,7 @@ namespace llarp
std::vector< byte_t > payload;
ProtocolType protocol;
PendingBuffer(llarp_buffer_t buf, ProtocolType t)
PendingBuffer(const llarp_buffer_t& buf, ProtocolType t)
: payload(buf.sz), protocol(t)
{
memcpy(payload.data(), buf.base, buf.sz);
@ -214,7 +214,7 @@ namespace llarp
PathSet* send, Endpoint* ep);
void
AsyncEncryptAndSendTo(llarp_buffer_t payload, ProtocolType t);
AsyncEncryptAndSendTo(const llarp_buffer_t& payload, ProtocolType t);
/// send a fully encrypted hidden service frame
/// via a path on our pathset with path id p
@ -249,10 +249,10 @@ namespace llarp
private:
void
EncryptAndSendTo(llarp_buffer_t payload, ProtocolType t);
EncryptAndSendTo(const llarp_buffer_t& payload, ProtocolType t);
virtual void
AsyncGenIntro(llarp_buffer_t payload, ProtocolType t) = 0;
AsyncGenIntro(const llarp_buffer_t& payload, ProtocolType t) = 0;
};
static void
@ -302,7 +302,7 @@ namespace llarp
CheckPathIsDead(path::Path* p, llarp_time_t dlt);
void
AsyncGenIntro(llarp_buffer_t payload, ProtocolType t) override;
AsyncGenIntro(const llarp_buffer_t& payload, ProtocolType t) override;
/// issues a lookup to find the current intro set of the remote service
void

@ -13,14 +13,14 @@ namespace llarp
namespace service
{
bool
ServiceInfo::Verify(llarp::Crypto* crypto, llarp_buffer_t payload,
ServiceInfo::Verify(llarp::Crypto* crypto, const llarp_buffer_t& payload,
const Signature& sig) const
{
return crypto->verify(signkey, payload, sig);
}
bool
ServiceInfo::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
ServiceInfo::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, val))

@ -23,7 +23,7 @@ namespace llarp
}
void
ProtocolMessage::PutBuffer(llarp_buffer_t buf)
ProtocolMessage::PutBuffer(const llarp_buffer_t& buf)
{
payload.resize(buf.sz);
memcpy(payload.data(), buf.base, buf.sz);
@ -39,7 +39,7 @@ namespace llarp
}
bool
ProtocolMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
ProtocolMessage::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("a", proto, read, k, buf))
@ -123,7 +123,7 @@ namespace llarp
}
bool
ProtocolFrame::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
ProtocolFrame::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
{
bool read = false;
if(llarp_buffer_eq(key, "A"))

@ -47,13 +47,13 @@ namespace llarp
ConvoTag tag;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override;
bool
BEncode(llarp_buffer_t* buf) const override;
void
PutBuffer(llarp_buffer_t payload);
PutBuffer(const llarp_buffer_t& payload);
static void
ProcessAsync(void* user);
@ -115,7 +115,7 @@ namespace llarp
ProtocolMessage& into) const;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) override;
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override;
bool
BEncode(llarp_buffer_t* buf) const override;

@ -43,7 +43,7 @@ namespace llarp
template < typename List_t >
bool
BEncodeMaybeReadDictList(const char* k, List_t& item, bool& read,
llarp_buffer_t key, llarp_buffer_t* buf)
const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
{
@ -59,7 +59,7 @@ namespace llarp
template < typename Item_t >
bool
BEncodeMaybeReadDictEntry(const char* k, Item_t& item, bool& read,
llarp_buffer_t key, llarp_buffer_t* buf)
const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
{
@ -78,7 +78,7 @@ namespace llarp
template < typename Int_t >
bool
BEncodeMaybeReadDictInt(const char* k, Int_t& i, bool& read,
llarp_buffer_t key, llarp_buffer_t* buf)
const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
{
@ -96,7 +96,8 @@ namespace llarp
template < typename Item_t >
bool
BEncodeMaybeReadVersion(const char* k, Item_t& item, uint64_t expect,
bool& read, llarp_buffer_t key, llarp_buffer_t* buf)
bool& read, const llarp_buffer_t& key,
llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
{
@ -233,7 +234,7 @@ namespace llarp
}
virtual bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* val) = 0;
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) = 0;
virtual bool
BEncode(llarp_buffer_t* buf) const = 0;

@ -5,7 +5,7 @@
#include <stdio.h>
size_t
llarp_buffer_size_left(llarp_buffer_t buff)
llarp_buffer_size_left(const llarp_buffer_t& buff)
{
size_t diff = buff.cur - buff.base;
if(diff > buff.sz)
@ -51,7 +51,8 @@ llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result,
size_t read = 0;
// do the bound check first, to avoid over running
while((buff->cur != buff->base + buff->sz) && *buff->cur != delim && resultsize)
while((buff->cur != buff->base + buff->sz) && *buff->cur != delim
&& resultsize)
{
*result = *buff->cur;
buff->cur++;
@ -67,13 +68,14 @@ llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result,
}
bool
llarp_buffer_eq(llarp_buffer_t buf, const char* str)
llarp_buffer_eq(const llarp_buffer_t& buf, const char* str)
{
while(*str && buf.cur != (buf.base + buf.sz))
llarp_buffer_t copy = buf.clone();
while(*str && copy.cur != (copy.base + copy.sz))
{
if(*buf.cur != *str)
if(*copy.cur != *str)
return false;
buf.cur++;
copy.cur++;
str++;
}
return *str == 0;

@ -72,17 +72,42 @@ struct llarp_buffer_t
{
}
llarp_buffer_t(const llarp_buffer_t &) = delete;
llarp_buffer_t(llarp_buffer_t &&) = default;
llarp_buffer_t(byte_t *b, byte_t *c, size_t s) : base(b), cur(c), sz(s)
{
assert(b != nullptr);
assert(c != nullptr);
assert(s != 0);
}
llarp_buffer_t
clone() const
{
return llarp_buffer_t(base, cur, sz);
}
};
struct CopyableBuffer
{
llarp_buffer_t underlying;
explicit CopyableBuffer(const llarp_buffer_t &b)
: underlying(b.base, b.cur, b.sz)
{
}
CopyableBuffer(CopyableBuffer &&) = default;
explicit CopyableBuffer(const CopyableBuffer &c)
: underlying(c.underlying.base, c.underlying.cur, c.underlying.sz)
{
}
};
/// how much room is left in buffer
size_t
llarp_buffer_size_left(llarp_buffer_t buff);
llarp_buffer_size_left(const llarp_buffer_t &buff);
/// write a chunk of data size "sz"
bool
@ -98,7 +123,7 @@ llarp_buffer_read_until(llarp_buffer_t *buff, char delim, byte_t *result,
size_t resultlen);
/// compare buffers, true if equal else false
bool
llarp_buffer_eq(llarp_buffer_t buff, const char *data);
llarp_buffer_eq(const llarp_buffer_t &buff, const char *data);
/// put big endian unsigned 16 bit integer
bool

@ -157,7 +157,8 @@ struct LinkLayerTest : public ::testing::Test
llarp_ev_loop_stop(netLoop);
}
bool AliceGotMessage(llarp_buffer_t)
bool
AliceGotMessage(const llarp_buffer_t&)
{
success = true;
Stop();
@ -170,7 +171,7 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
Alice.link = llarp::utp::NewServer(
&crypto, Alice.encryptionKey,
[&]() -> const llarp::RouterContact& { return Alice.GetRC(); },
[&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool {
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
if(Alice.gotLIM)
{
Alice.Regen();
@ -179,7 +180,8 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
else
{
llarp::LinkIntroMessage msg;
if(!msg.BDecode(&buf))
llarp_buffer_t copy(buf.clone());
if(!msg.BDecode(&copy))
return false;
if(!s->GotLIM(&msg))
return false;
@ -192,7 +194,7 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
llarp::LogInfo("alice established with bob");
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, llarp_buffer_t buf) -> bool {
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Alice.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
@ -216,9 +218,10 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
Bob.link = llarp::utp::NewServer(
&crypto, Bob.encryptionKey,
[&]() -> const llarp::RouterContact& { return Bob.GetRC(); },
[&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool {
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
llarp::LinkIntroMessage msg;
if(!msg.BDecode(&buf))
llarp_buffer_t copy(buf.clone());
if(!msg.BDecode(&copy))
return false;
if(!s->GotLIM(&msg))
return false;
@ -235,7 +238,7 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
success = newrc.pubkey == oldrc.pubkey;
return true;
},
[&](llarp::Signature& sig, llarp_buffer_t buf) -> bool {
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Bob.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
@ -259,7 +262,7 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
Alice.link = llarp::utp::NewServer(
&crypto, Alice.encryptionKey,
[&]() -> const llarp::RouterContact& { return Alice.GetRC(); },
[&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool {
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
if(Alice.gotLIM)
{
return AliceGotMessage(buf);
@ -267,7 +270,8 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
else
{
llarp::LinkIntroMessage msg;
if(!msg.BDecode(&buf))
llarp_buffer_t copy(buf.clone());
if(!msg.BDecode(&copy))
return false;
if(!s->GotLIM(&msg))
return false;
@ -280,7 +284,7 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
llarp::LogInfo("alice established with bob");
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, llarp_buffer_t buf) -> bool {
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Alice.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
@ -304,9 +308,10 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
Bob.link = llarp::utp::NewServer(
&crypto, Bob.encryptionKey,
[&]() -> const llarp::RouterContact& { return Bob.GetRC(); },
[&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool {
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
llarp::LinkIntroMessage msg;
if(!msg.BDecode(&buf))
llarp_buffer_t copy(buf.clone());
if(!msg.BDecode(&copy))
return false;
if(!s->GotLIM(&msg))
return false;
@ -320,7 +325,7 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
sendDiscardMessage);
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, llarp_buffer_t buf) -> bool {
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Bob.signingKey, buf);
},
[&](llarp::ILinkSession* session) {

@ -45,7 +45,7 @@ TEST_P(ReadInt, readInt)
{
auto d = GetParam();
llarp_buffer_t buffer = llarp::Buffer(d.buffer);
llarp_buffer_t buffer(llarp::Buffer(d.buffer));
uint64_t result = 0;
bool rc = bencode_read_integer(&buffer, &result);
@ -205,16 +205,21 @@ TEST_P(WriteIntValues, anyvalue)
uint64_t val = GetParam();
std::vector< byte_t > backingBuffer(100, 0);
llarp_buffer_t buffer = llarp::Buffer(backingBuffer);
bool rc = bencode_write_uint64(&buffer, val);
ASSERT_TRUE(rc);
{
llarp_buffer_t buffer = llarp::Buffer(backingBuffer);
uint64_t result = 0;
buffer = llarp::Buffer(backingBuffer);
rc = bencode_read_integer(&buffer, &result);
ASSERT_TRUE(rc);
ASSERT_EQ(result, val);
bool rc = bencode_write_uint64(&buffer, val);
ASSERT_TRUE(rc);
}
{
uint64_t result = 0;
llarp_buffer_t buffer = llarp::Buffer(backingBuffer);
bool rc = bencode_read_integer(&buffer, &result);
ASSERT_TRUE(rc);
ASSERT_EQ(result, val);
}
}
INSTANTIATE_TEST_CASE_P(
@ -224,7 +229,7 @@ INSTANTIATE_TEST_CASE_P(
std::numeric_limits< uint64_t >::max() / 2,
std::numeric_limits< uint64_t >::max() / 3));
TEST(TestBencode, version)
TEST(TestBencode, good_version)
{
std::vector< byte_t > backingBuffer(100, 0);
llarp_buffer_t buffer = llarp::Buffer(backingBuffer);
@ -232,9 +237,12 @@ TEST(TestBencode, version)
ASSERT_TRUE(bencode_write_version_entry(&buffer));
ASSERT_EQ(std::string(buffer.base, buffer.cur), "1:vi0e");
}
TEST(TestBencode, bad_version)
{
std::vector< byte_t > otherBuffer(1, 0);
buffer = llarp::Buffer(otherBuffer);
llarp_buffer_t buffer = llarp::Buffer(otherBuffer);
ASSERT_FALSE(bencode_write_version_entry(&buffer));
}

Loading…
Cancel
Save