diff --git a/llarp/crypto/crypto.hpp b/llarp/crypto/crypto.hpp index a4c3b659a..f9d606df3 100644 --- a/llarp/crypto/crypto.hpp +++ b/llarp/crypto/crypto.hpp @@ -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; diff --git a/llarp/crypto/crypto_libsodium.cpp b/llarp/crypto/crypto_libsodium.cpp index 6d5ddc683..a55dc40ba 100644 --- a/llarp/crypto/crypto_libsodium.cpp +++ b/llarp/crypto/crypto_libsodium.cpp @@ -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); } diff --git a/llarp/crypto/crypto_libsodium.hpp b/llarp/crypto/crypto_libsodium.hpp index 57e79c737..2f4bbbd0a 100644 --- a/llarp/crypto/crypto_libsodium.hpp +++ b/llarp/crypto/crypto_libsodium.hpp @@ -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; diff --git a/llarp/crypto/encrypted.hpp b/llarp/crypto/encrypted.hpp index 75c856727..d87bbd708 100644 --- a/llarp/crypto/encrypted.hpp +++ b/llarp/crypto/encrypted.hpp @@ -138,7 +138,7 @@ namespace llarp llarp_buffer_t Buffer() const { - return m_Buffer; + return m_Buffer.clone(); } size_t diff --git a/llarp/dht/messages/findintro.cpp b/llarp/dht/messages/findintro.cpp index fc0e03a93..b793665b4 100644 --- a/llarp/dht/messages/findintro.cpp +++ b/llarp/dht/messages/findintro.cpp @@ -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; diff --git a/llarp/dht/messages/findintro.hpp b/llarp/dht/messages/findintro.hpp index 1dd193934..774a9d561 100644 --- a/llarp/dht/messages/findintro.hpp +++ b/llarp/dht/messages/findintro.hpp @@ -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( diff --git a/llarp/dht/messages/findrouter.cpp b/llarp/dht/messages/findrouter.cpp index 88643d664..834834088 100644 --- a/llarp/dht/messages/findrouter.cpp +++ b/llarp/dht/messages/findrouter.cpp @@ -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; diff --git a/llarp/dht/messages/findrouter.hpp b/llarp/dht/messages/findrouter.hpp index 7ed96a3c0..1a917ae53 100644 --- a/llarp/dht/messages/findrouter.hpp +++ b/llarp/dht/messages/findrouter.hpp @@ -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( diff --git a/llarp/dht/messages/gotintro.cpp b/llarp/dht/messages/gotintro.cpp index 08073bc54..1b04c76fe 100644 --- a/llarp/dht/messages/gotintro.cpp +++ b/llarp/dht/messages/gotintro.cpp @@ -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")) { diff --git a/llarp/dht/messages/gotintro.hpp b/llarp/dht/messages/gotintro.hpp index c398f3969..50c8fb88f 100644 --- a/llarp/dht/messages/gotintro.hpp +++ b/llarp/dht/messages/gotintro.hpp @@ -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( diff --git a/llarp/dht/messages/gotrouter.cpp b/llarp/dht/messages/gotrouter.cpp index 60fa5d1f7..75839dd75 100644 --- a/llarp/dht/messages/gotrouter.cpp +++ b/llarp/dht/messages/gotrouter.cpp @@ -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")) { diff --git a/llarp/dht/messages/gotrouter.hpp b/llarp/dht/messages/gotrouter.hpp index 7c8ff4ead..de00b4d53 100644 --- a/llarp/dht/messages/gotrouter.hpp +++ b/llarp/dht/messages/gotrouter.hpp @@ -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( diff --git a/llarp/dht/messages/pubintro.cpp b/llarp/dht/messages/pubintro.cpp index fe452a004..173925f80 100644 --- a/llarp/dht/messages/pubintro.cpp +++ b/llarp/dht/messages/pubintro.cpp @@ -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")) diff --git a/llarp/dht/messages/pubintro.hpp b/llarp/dht/messages/pubintro.hpp index d5447f63a..d2b794bfa 100644 --- a/llarp/dht/messages/pubintro.hpp +++ b/llarp/dht/messages/pubintro.hpp @@ -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( diff --git a/llarp/dns.cpp b/llarp/dns.cpp index 2c49c7953..9b4fa7e03 100644 --- a/llarp/dns.cpp +++ b/llarp/dns.cpp @@ -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()); } } } diff --git a/llarp/dnsc.cpp b/llarp/dnsc.cpp index 2892ec5c2..a50975cd6 100644 --- a/llarp/dnsc.cpp +++ b/llarp/dnsc.cpp @@ -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) { diff --git a/llarp/dnsc.hpp b/llarp/dnsc.hpp index 742070fc0..a68b76514 100644 --- a/llarp/dnsc.hpp +++ b/llarp/dnsc.hpp @@ -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 diff --git a/llarp/ev/ev.cpp b/llarp/ev/ev.cpp index 0180253d8..ffca3401d 100644 --- a/llarp/ev/ev.cpp +++ b/llarp/ev/ev.cpp @@ -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); diff --git a/llarp/ev/ev.h b/llarp/ev/ev.h index b26a7b3c2..35094f9fb 100644 --- a/llarp/ev/ev.h +++ b/llarp/ev/ev.h @@ -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 diff --git a/llarp/exit/close_exit.cpp b/llarp/exit/close_exit.cpp index 3fe7d3993..175e9631e 100644 --- a/llarp/exit/close_exit.cpp +++ b/llarp/exit/close_exit.cpp @@ -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)) diff --git a/llarp/exit/grant_exit.cpp b/llarp/exit/grant_exit.cpp index 2b8395e59..4afad2486 100644 --- a/llarp/exit/grant_exit.cpp +++ b/llarp/exit/grant_exit.cpp @@ -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)) diff --git a/llarp/exit/obtain_exit.cpp b/llarp/exit/obtain_exit.cpp index e39f3f0cf..5894dab62 100644 --- a/llarp/exit/obtain_exit.cpp +++ b/llarp/exit/obtain_exit.cpp @@ -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)) diff --git a/llarp/exit/policy.cpp b/llarp/exit/policy.cpp index ffefb7fe7..31cd51ef4 100644 --- a/llarp/exit/policy.cpp +++ b/llarp/exit/policy.cpp @@ -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)) diff --git a/llarp/exit/policy.hpp b/llarp/exit/policy.hpp index 1905fca76..e8e0b1207 100644 --- a/llarp/exit/policy.hpp +++ b/llarp/exit/policy.hpp @@ -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; diff --git a/llarp/exit/reject_exit.cpp b/llarp/exit/reject_exit.cpp index 2edd8280e..e739a6c42 100644 --- a/llarp/exit/reject_exit.cpp +++ b/llarp/exit/reject_exit.cpp @@ -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)) diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index d99a589c7..a7fa526d6 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -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) diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index 5d68627b2..d72e78dd4 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -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); diff --git a/llarp/exit/transfer_traffic.cpp b/llarp/exit/transfer_traffic.cpp index f1035f878..ab67de1a3 100644 --- a/llarp/exit/transfer_traffic.cpp +++ b/llarp/exit/transfer_traffic.cpp @@ -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)) diff --git a/llarp/exit/update_exit.cpp b/llarp/exit/update_exit.cpp index 06723066d..604ea0430 100644 --- a/llarp/exit/update_exit.cpp +++ b/llarp/exit/update_exit.cpp @@ -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)) diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 902e0ce46..21e61115c 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -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)) diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index b7018cab7..ad2ea3c75 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -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); diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index fb61d4d1c..f52a7ed42 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -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; } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index a8568627e..bd09f3b72 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -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 } } diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 22bbcf7fe..8bb544b6e 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -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; diff --git a/llarp/link/iwp_internal.hpp b/llarp/link/iwp_internal.hpp index e06226ded..e89653487 100644 --- a/llarp/link/iwp_internal.hpp +++ b/llarp/link/iwp_internal.hpp @@ -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); diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index 25375a29a..cd7bb7f93 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -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; { diff --git a/llarp/link/server.hpp b/llarp/link/server.hpp index 353d78173..e3a70471d 100644 --- a/llarp/link/server.hpp +++ b/llarp/link/server.hpp @@ -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; diff --git a/llarp/link/session.hpp b/llarp/link/session.hpp index 8d4d0080f..23186482f 100644 --- a/llarp/link/session.hpp +++ b/llarp/link/session.hpp @@ -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; diff --git a/llarp/link/utp.cpp b/llarp/link/utp.cpp index 6b5bb239d..8c5794b5f 100644 --- a/llarp/link/utp.cpp +++ b/llarp/link/utp.cpp @@ -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 diff --git a/llarp/link/utp_internal.hpp b/llarp/link/utp_internal.hpp index c47a6949d..cbdc35d89 100644 --- a/llarp/link/utp_internal.hpp +++ b/llarp/link/utp_internal.hpp @@ -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 diff --git a/llarp/messages/dht.hpp b/llarp/messages/dht.hpp index c1383ac52..90c6c3648 100644 --- a/llarp/messages/dht.hpp +++ b/llarp/messages/dht.hpp @@ -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; diff --git a/llarp/messages/dht_immediate.cpp b/llarp/messages/dht_immediate.cpp index e9da3b54e..a025e5e73 100644 --- a/llarp/messages/dht_immediate.cpp +++ b/llarp/messages/dht_immediate.cpp @@ -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()), diff --git a/llarp/messages/dht_immediate.hpp b/llarp/messages/dht_immediate.hpp index 702fc17d7..7123a86f1 100644 --- a/llarp/messages/dht_immediate.hpp +++ b/llarp/messages/dht_immediate.hpp @@ -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; diff --git a/llarp/messages/discard.hpp b/llarp/messages/discard.hpp index 61a8ff56f..098309dd9 100644 --- a/llarp/messages/discard.hpp +++ b/llarp/messages/discard.hpp @@ -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)) diff --git a/llarp/messages/exit.hpp b/llarp/messages/exit.hpp index 8a09bc819..44e187c4e 100644 --- a/llarp/messages/exit.hpp +++ b/llarp/messages/exit.hpp @@ -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; diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index 7a1c6ce52..a2ba3cbda 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -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}; diff --git a/llarp/messages/link_intro.hpp b/llarp/messages/link_intro.hpp index 56b673894..3d3b0310f 100644 --- a/llarp/messages/link_intro.hpp +++ b/llarp/messages/link_intro.hpp @@ -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; diff --git a/llarp/messages/link_message.cpp b/llarp/messages/link_message.cpp index 9f8c61e70..628a2f78c 100644 --- a/llarp/messages/link_message.cpp +++ b/llarp/messages/link_message.cpp @@ -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(©, &reader); } void diff --git a/llarp/messages/link_message_parser.hpp b/llarp/messages/link_message_parser.hpp index 7340c8504..f1bd42520 100644 --- a/llarp/messages/link_message_parser.hpp +++ b/llarp/messages/link_message_parser.hpp @@ -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 diff --git a/llarp/messages/path_confirm.hpp b/llarp/messages/path_confirm.hpp index 773f3f08e..89cf0bf87 100644 --- a/llarp/messages/path_confirm.hpp +++ b/llarp/messages/path_confirm.hpp @@ -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; diff --git a/llarp/messages/path_latency.hpp b/llarp/messages/path_latency.hpp index ff78f1bf5..e8f4b9028 100644 --- a/llarp/messages/path_latency.hpp +++ b/llarp/messages/path_latency.hpp @@ -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 diff --git a/llarp/messages/path_transfer.hpp b/llarp/messages/path_transfer.hpp index 802c30a5b..c4dcba9c3 100644 --- a/llarp/messages/path_transfer.hpp +++ b/llarp/messages/path_transfer.hpp @@ -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; diff --git a/llarp/messages/relay.cpp b/llarp/messages/relay.cpp index 8703c2ad5..1c826d1b1 100644 --- a/llarp/messages/relay.cpp +++ b/llarp/messages/relay.cpp @@ -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)) diff --git a/llarp/messages/relay.hpp b/llarp/messages/relay.hpp index 89e6599e8..26aa7c795 100644 --- a/llarp/messages/relay.hpp +++ b/llarp/messages/relay.hpp @@ -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; diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index df74fd9fc..e6c8a8c8e 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -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")) { diff --git a/llarp/messages/relay_commit.hpp b/llarp/messages/relay_commit.hpp index dfc91941a..0da56e84e 100644 --- a/llarp/messages/relay_commit.hpp +++ b/llarp/messages/relay_commit.hpp @@ -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; diff --git a/llarp/messages/transfer_traffic.hpp b/llarp/messages/transfer_traffic.hpp index ada6946f5..369d599d0 100644 --- a/llarp/messages/transfer_traffic.hpp +++ b/llarp/messages/transfer_traffic.hpp @@ -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; diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index 7852b38d6..2dfd89e2e 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -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}; diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index 170258469..1776ed347 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -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) diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index ae31f127d..e9f7acf57 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -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)) diff --git a/llarp/net/exit_info.hpp b/llarp/net/exit_info.hpp index d3a3c8610..356c62c01 100644 --- a/llarp/net/exit_info.hpp +++ b/llarp/net/exit_info.hpp @@ -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); diff --git a/llarp/net/ip.cpp b/llarp/net/ip.cpp index c2c01b3fd..506a7fab3 100644 --- a/llarp/net/ip.cpp +++ b/llarp/net/ip.cpp @@ -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; diff --git a/llarp/net/ip.hpp b/llarp/net/ip.hpp index db77b5ef4..0981f38f6 100644 --- a/llarp/net/ip.hpp +++ b/llarp/net/ip.hpp @@ -96,7 +96,7 @@ namespace llarp ConstBuffer() const; bool - Load(llarp_buffer_t buf); + Load(const llarp_buffer_t& buf); struct GetTime { diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index 6b41569bb..f031bcfb6 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -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())) { diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 620dc110e..5abb57135 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -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 diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index 22bd7adfa..2c22ee5f9 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -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); diff --git a/llarp/pow.cpp b/llarp/pow.cpp index 298744206..2ccb0fcf1 100644 --- a/llarp/pow.cpp +++ b/llarp/pow.cpp @@ -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 diff --git a/llarp/pow.hpp b/llarp/pow.hpp index 73bc10090..9138fbf1c 100644 --- a/llarp/pow.hpp +++ b/llarp/pow.hpp @@ -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; diff --git a/llarp/profiling.cpp b/llarp/profiling.cpp index 1b59b43c5..9e2a84422 100644 --- a/llarp/profiling.cpp +++ b/llarp/profiling.cpp @@ -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; diff --git a/llarp/profiling.hpp b/llarp/profiling.hpp index bc449b416..e92ba07d5 100644 --- a/llarp/profiling.hpp +++ b/llarp/profiling.hpp @@ -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); diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index bd32ec5d6..4a079b629 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -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(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)) diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 8a1edefac..2f3573f45 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -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); diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index a196065db..2f7591d77 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -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)) diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 3d3b1c077..2f491632e 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -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); diff --git a/llarp/routing/dht_message.cpp b/llarp/routing/dht_message.cpp index a854223a1..d4d3821ad 100644 --- a/llarp/routing/dht_message.cpp +++ b/llarp/routing/dht_message.cpp @@ -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(); diff --git a/llarp/routing/message_parser.cpp b/llarp/routing/message_parser.cpp index b3648fb9d..1813fd2bb 100644 --- a/llarp/routing/message_parser.cpp +++ b/llarp/routing/message_parser.cpp @@ -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(©, &reader)) { msg->from = from; result = msg->HandleMessage(h, r); diff --git a/llarp/routing/message_parser.hpp b/llarp/routing/message_parser.hpp index 02fcd0985..8a76d3b6a 100644 --- a/llarp/routing/message_parser.hpp +++ b/llarp/routing/message_parser.hpp @@ -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: diff --git a/llarp/routing/path_confirm.cpp b/llarp/routing/path_confirm.cpp index e9a72ef0a..20c5c7271 100644 --- a/llarp/routing/path_confirm.cpp +++ b/llarp/routing/path_confirm.cpp @@ -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)) diff --git a/llarp/routing/path_latency.cpp b/llarp/routing/path_latency.cpp index 2f4dafde3..a8055d02f 100644 --- a/llarp/routing/path_latency.cpp +++ b/llarp/routing/path_latency.cpp @@ -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)) diff --git a/llarp/routing/path_transfer.cpp b/llarp/routing/path_transfer.cpp index 625a95eb6..14202d82f 100644 --- a/llarp/routing/path_transfer.cpp +++ b/llarp/routing/path_transfer.cpp @@ -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)) diff --git a/llarp/service/Identity.cpp b/llarp/service/Identity.cpp index a20b72e6d..40cde7b38 100644 --- a/llarp/service/Identity.cpp +++ b/llarp/service/Identity.cpp @@ -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); } diff --git a/llarp/service/Identity.hpp b/llarp/service/Identity.hpp index f6450d6ce..bc1cf7830 100644 --- a/llarp/service/Identity.hpp +++ b/llarp/service/Identity.hpp @@ -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 diff --git a/llarp/service/Info.hpp b/llarp/service/Info.hpp index 5f6aa1476..85af70fb6 100644 --- a/llarp/service/Info.hpp +++ b/llarp/service/Info.hpp @@ -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; diff --git a/llarp/service/Intro.cpp b/llarp/service/Intro.cpp index e4f57b7e2..220c8da89 100644 --- a/llarp/service/Intro.cpp +++ b/llarp/service/Intro.cpp @@ -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)) diff --git a/llarp/service/Intro.hpp b/llarp/service/Intro.hpp index 278ef4388..cfbc039ac 100644 --- a/llarp/service/Intro.hpp +++ b/llarp/service/Intro.hpp @@ -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(); diff --git a/llarp/service/IntroSet.cpp b/llarp/service/IntroSet.cpp index 3581f77b8..5f879b362 100644 --- a/llarp/service/IntroSet.cpp +++ b/llarp/service/IntroSet.cpp @@ -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)) diff --git a/llarp/service/IntroSet.hpp b/llarp/service/IntroSet.hpp index 46577f085..541537f48 100644 --- a/llarp/service/IntroSet.hpp +++ b/llarp/service/IntroSet.hpp @@ -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; diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index f91ea8d05..425a47d06 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -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(); diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 1e2445723..f7a5f444d 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -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 diff --git a/llarp/service/info.cpp b/llarp/service/info.cpp index 458ef30a2..5b0eb5e3f 100644 --- a/llarp/service/info.cpp +++ b/llarp/service/info.cpp @@ -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)) diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index fc564d7f7..8c61db9a5 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -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")) diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index 6ad18a990..72ec84542 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -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; diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index 163150a32..8655d86a5 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -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; diff --git a/llarp/util/buffer.cpp b/llarp/util/buffer.cpp index d2bd9ad28..7b39e1f6e 100644 --- a/llarp/util/buffer.cpp +++ b/llarp/util/buffer.cpp @@ -5,7 +5,7 @@ #include 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; diff --git a/llarp/util/buffer.h b/llarp/util/buffer.h index 7f2022725..f04b0e6d4 100644 --- a/llarp/util/buffer.h +++ b/llarp/util/buffer.h @@ -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 diff --git a/test/link/test_llarp_link.cpp b/test/link/test_llarp_link.cpp index de3b4c483..e2c216967 100644 --- a/test/link/test_llarp_link.cpp +++ b/test/link/test_llarp_link.cpp @@ -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(©)) 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(©)) 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(©)) 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(©)) 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) { diff --git a/test/util/test_llarp_util_bencode.cpp b/test/util/test_llarp_util_bencode.cpp index 91adedc9d..26c092def 100644 --- a/test/util/test_llarp_util_bencode.cpp +++ b/test/util/test_llarp_util_bencode.cpp @@ -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)); }