diff --git a/llarp/dht/message.cpp b/llarp/dht/message.cpp index 7435cc3cb..dfaabfea3 100644 --- a/llarp/dht/message.cpp +++ b/llarp/dht/message.cpp @@ -35,7 +35,7 @@ namespace llarp // first key if (firstKey) { - if (!(*key == "A")) + if (!(key->startswith("A"))) return false; if (!bencode_read_string(buffer, &strbuf)) return false; diff --git a/llarp/dht/messages/findname.cpp b/llarp/dht/messages/findname.cpp index 049cc0724..975ee2a09 100644 --- a/llarp/dht/messages/findname.cpp +++ b/llarp/dht/messages/findname.cpp @@ -26,11 +26,11 @@ namespace llarp::dht bool FindNameMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) { - if (key == "H") + if (key.startswith("H")) { return NameHash.BDecode(val); } - if (key == "T") + if (key.startswith("T")) { return bencode_read_integer(val, &TxID); } diff --git a/llarp/dht/messages/findrouter.cpp b/llarp/dht/messages/findrouter.cpp index 9ce1f4d40..35978122d 100644 --- a/llarp/dht/messages/findrouter.cpp +++ b/llarp/dht/messages/findrouter.cpp @@ -105,7 +105,7 @@ namespace llarp { llarp_buffer_t strbuf; - if (key == "E") + if (key.startswith("E")) { uint64_t result; if (!bencode_read_integer(val, &result)) @@ -115,7 +115,7 @@ namespace llarp return true; } - if (key == "I") + if (key.startswith("I")) { uint64_t result; if (!bencode_read_integer(val, &result)) @@ -124,7 +124,7 @@ namespace llarp iterative = result != 0; return true; } - if (key == "K") + if (key.startswith("K")) { if (!bencode_read_string(val, &strbuf)) return false; @@ -134,11 +134,11 @@ namespace llarp std::copy(strbuf.base, strbuf.base + targetKey.SIZE, targetKey.begin()); return true; } - if (key == "T") + if (key.startswith("T")) { return bencode_read_integer(val, &txid); } - if (key == "V") + if (key.startswith("V")) { return bencode_read_integer(val, &version); } diff --git a/llarp/dht/messages/gotintro.cpp b/llarp/dht/messages/gotintro.cpp index 0480a521a..f96a9cd4b 100644 --- a/llarp/dht/messages/gotintro.cpp +++ b/llarp/dht/messages/gotintro.cpp @@ -79,11 +79,11 @@ namespace llarp bool GotIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == "I") + if (key.startswith("I")) { return BEncodeReadList(found, buf); } - if (key == "K") + if (key.startswith("K")) { if (closer) // duplicate key? return false; diff --git a/llarp/dht/messages/gotname.cpp b/llarp/dht/messages/gotname.cpp index 2f1720938..78c0f2e74 100644 --- a/llarp/dht/messages/gotname.cpp +++ b/llarp/dht/messages/gotname.cpp @@ -27,7 +27,7 @@ namespace llarp::dht bool GotNameMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) { - if (key == "D") + if (key.startswith("D")) { llarp_buffer_t str{}; if (not bencode_read_string(val, &str)) @@ -38,11 +38,11 @@ namespace llarp::dht std::copy_n(str.cur, str.sz, result.ciphertext.data()); return true; } - if (key == "N") + if (key.startswith("N")) { return result.nonce.BDecode(val); } - if (key == "T") + if (key.startswith("T")) { return bencode_read_integer(val, &TxID); } diff --git a/llarp/dht/messages/gotrouter.cpp b/llarp/dht/messages/gotrouter.cpp index 9fcb56c31..46bb89303 100644 --- a/llarp/dht/messages/gotrouter.cpp +++ b/llarp/dht/messages/gotrouter.cpp @@ -53,22 +53,22 @@ namespace llarp bool GotRouterMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) { - if (key == "K") + if (key.startswith("K")) { if (closerTarget) // duplicate key? return false; closerTarget = std::make_unique(); return closerTarget->BDecode(val); } - if (key == "N") + if (key.startswith("N")) { return BEncodeReadList(nearKeys, val); } - if (key == "R") + if (key.startswith("R")) { return BEncodeReadList(foundRCs, val); } - if (key == "T") + if (key.startswith("T")) { return bencode_read_integer(val, &txid); } diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 9a873fb55..8c8553922 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -213,7 +213,7 @@ namespace llarp if (m_WritePacket) { - llarp::net::IPPacket pkt{buf.view()}; + llarp::net::IPPacket pkt{buf.view_all()}; if (pkt.empty()) return false; m_LastUse = m_router->Now(); @@ -367,7 +367,7 @@ namespace llarp void SNodeSession::SendPacketToRemote(const llarp_buffer_t& buf, service::ProtocolType t) { - net::IPPacket pkt{buf.view()}; + net::IPPacket pkt{buf.view_all()}; if (pkt.empty()) return; pkt.ZeroAddresses(); @@ -377,7 +377,7 @@ namespace llarp void ExitSession::SendPacketToRemote(const llarp_buffer_t& buf, service::ProtocolType t) { - net::IPPacket pkt{buf.view()}; + net::IPPacket pkt{buf.view_all()}; if (pkt.empty()) return; diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index df508141c..11c9d35c2 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -662,7 +662,7 @@ namespace llarp bool ExitEndpoint::QueueSNodePacket(const llarp_buffer_t& buf, huint128_t from) { - net::IPPacket pkt{buf.view()}; + net::IPPacket pkt{buf.view_all()}; if (pkt.empty()) return false; // rewrite ip diff --git a/llarp/messages/dht_immediate.cpp b/llarp/messages/dht_immediate.cpp index 2ab9f13e3..479932979 100644 --- a/llarp/messages/dht_immediate.cpp +++ b/llarp/messages/dht_immediate.cpp @@ -14,9 +14,9 @@ namespace llarp bool DHTImmediateMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == "m") + if (key.startswith("m")) return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()), buf, msgs); - if (key == "v") + if (key.startswith("v")) { if (!bencode_read_integer(buf, &version)) return false; diff --git a/llarp/messages/discard.hpp b/llarp/messages/discard.hpp index 46d24d429..5a3458a59 100644 --- a/llarp/messages/discard.hpp +++ b/llarp/messages/discard.hpp @@ -39,7 +39,7 @@ namespace llarp bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override { - if (key == "a") + if (key.startswith("a")) { llarp_buffer_t strbuf; if (!bencode_read_string(buf, &strbuf)) diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index 88d1af2d1..2ffab6f3c 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -11,7 +11,7 @@ namespace llarp bool LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == "a") + if (key.startswith("a")) { llarp_buffer_t strbuf; if (!bencode_read_string(buf, &strbuf)) @@ -20,18 +20,18 @@ namespace llarp return false; return *strbuf.cur == 'i'; } - if (key == "n") + if (key.startswith("n")) { if (N.BDecode(buf)) return true; llarp::LogWarn("failed to decode nonce in LIM"); return false; } - if (key == "p") + if (key.startswith("p")) { return bencode_read_integer(buf, &P); } - if (key == "r") + if (key.startswith("r")) { if (rc.BDecode(buf)) return true; @@ -39,7 +39,7 @@ namespace llarp llarp::DumpBuffer(*buf); return false; } - if (key == "v") + if (key.startswith("v")) { if (!bencode_read_integer(buf, &version)) return false; @@ -52,7 +52,7 @@ namespace llarp llarp::LogDebug("LIM version ", version); return true; } - if (key == "z") + if (key.startswith("z")) { return Z.BDecode(buf); } diff --git a/llarp/messages/link_message_parser.cpp b/llarp/messages/link_message_parser.cpp index 971af9a6d..814b7d0d6 100644 --- a/llarp/messages/link_message_parser.cpp +++ b/llarp/messages/link_message_parser.cpp @@ -45,7 +45,7 @@ namespace llarp if (!key) return false; // we are expecting the first key to be 'a' - if (!(*key == "a")) + if (!key->startswith("a")) { llarp::LogWarn("message has no message type"); return false; diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index 74b5ecc35..4cffe912f 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -22,7 +22,7 @@ namespace llarp bool LR_CommitMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == "c") + if (key.startswith("c")) { /// so we dont put it into the shitty queue pathid.Fill('c'); @@ -131,7 +131,7 @@ namespace llarp return false; if (!BEncodeMaybeReadDictEntry("t", txid, read, *key, buffer)) return false; - if (*key == "u") + if (key->startswith("u")) { nextRC = std::make_unique(); return nextRC->BDecode(buffer); @@ -139,7 +139,7 @@ namespace llarp if (!BEncodeMaybeVerifyVersion( "v", version, llarp::constants::proto_version, read, *key, buffer)) return false; - if (*key == "w") + if (key->startswith("w")) { // check for duplicate if (work) diff --git a/llarp/messages/relay_status.cpp b/llarp/messages/relay_status.cpp index 8d0732059..0f5fdc607 100644 --- a/llarp/messages/relay_status.cpp +++ b/llarp/messages/relay_status.cpp @@ -60,25 +60,25 @@ namespace llarp LR_StatusMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { bool read = false; - if (key == "c") + if (key.startswith("c")) { return BEncodeReadArray(frames, buf); } - if (key == "p") + if (key.startswith("p")) { if (!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf)) { return false; } } - else if (key == "s") + else if (key.startswith("s")) { if (!BEncodeMaybeReadDictInt("s", status, read, key, buf)) { return false; } } - else if (key == "v") + else if (key.startswith("v")) { if (!BEncodeMaybeVerifyVersion("v", version, llarp::constants::proto_version, read, key, buf)) { diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index 70788b258..fd5424632 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -41,7 +41,7 @@ namespace llarp llarp_buffer_t strbuf; // rank - if (key == "c") + if (key.startswith("c")) { if (!bencode_read_integer(buf, &i)) return false; @@ -54,7 +54,7 @@ namespace llarp } // dialect - if (key == "d") + if (key.startswith("d")) { if (!bencode_read_string(buf, &strbuf)) return false; @@ -67,13 +67,13 @@ namespace llarp } // encryption public key - if (key == "e") + if (key.startswith("e")) { return pubkey.BDecode(buf); } // ip address - if (key == "i") + if (key.startswith("i")) { if (!bencode_read_string(buf, &strbuf)) return false; @@ -87,7 +87,7 @@ namespace llarp } // port - if (key == "p") + if (key.startswith("p")) { if (!bencode_read_integer(buf, &i)) return false; @@ -100,7 +100,7 @@ namespace llarp } // version - if (key == "v") + if (key.startswith("v")) { if (!bencode_read_integer(buf, &i)) return false; diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index 2af7c707f..ef91471d3 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -69,7 +69,7 @@ namespace llarp return false; if (!BEncodeMaybeReadDictInt("v", version, read, k, buf)) return false; - if (k == "a") + if (k.startswith("a")) { in6_addr tmp; if (not bdecode_ip_string(buf, tmp)) @@ -79,7 +79,7 @@ namespace llarp ipAddress = IpAddress(addr); return true; } - if (k == "b") + if (k.startswith("b")) { in6_addr tmp; if (not bdecode_ip_string(buf, tmp)) diff --git a/llarp/net/traffic_policy.cpp b/llarp/net/traffic_policy.cpp index f8d00d80d..e94cb94a7 100644 --- a/llarp/net/traffic_policy.cpp +++ b/llarp/net/traffic_policy.cpp @@ -167,11 +167,11 @@ namespace llarp::net [&](llarp_buffer_t* buffer, llarp_buffer_t* key) -> bool { if (key == nullptr) return true; - if (*key == "p") + if (key->startswith("p")) { return BEncodeReadSet(protocols, buffer); } - if (*key == "r") + if (key->startswith("r")) { return BEncodeReadSet(ranges, buffer); } diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index f565255d2..e06c8101f 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -344,7 +344,7 @@ namespace llarp if (!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf)) return false; - if (key == "r") + if (key.startswith("r")) { RouterVersion r; if (not r.BDecode(buf)) @@ -353,7 +353,7 @@ namespace llarp return true; } - if (key == "n") + if (key.startswith("n")) { llarp_buffer_t strbuf; if (!bencode_read_string(buf, &strbuf)) @@ -381,7 +381,7 @@ namespace llarp if (!BEncodeMaybeReadDictInt("v", version, read, key, buf)) return false; - if (key == "x" and serializeExit) + if (key.startswith("x") and serializeExit) { return bencode_discard(buf); } diff --git a/llarp/routing/dht_message.cpp b/llarp/routing/dht_message.cpp index 510754049..72ffe6bb4 100644 --- a/llarp/routing/dht_message.cpp +++ b/llarp/routing/dht_message.cpp @@ -10,17 +10,17 @@ namespace llarp bool DHTMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) { - if (key == "M") + if (key.startswith("M")) { llarp::dht::Key_t fromKey; fromKey.Zero(); return llarp::dht::DecodeMesssageList(fromKey, val, M, true); } - if (key == "S") + if (key.startswith("S")) { return bencode_read_integer(val, &S); } - if (key == "V") + if (key.startswith("V")) { return bencode_read_integer(val, &V); } diff --git a/llarp/routing/message_parser.cpp b/llarp/routing/message_parser.cpp index 68e015dc9..16f77ae87 100644 --- a/llarp/routing/message_parser.cpp +++ b/llarp/routing/message_parser.cpp @@ -48,7 +48,7 @@ namespace llarp if (firstKey) { llarp_buffer_t strbuf; - if (!(*key == "A")) + if (!(key->startswith("A"))) return false; if (!bencode_read_string(buffer, &strbuf)) return false; diff --git a/llarp/service/intro_set.cpp b/llarp/service/intro_set.cpp index 5141256db..07c39497f 100644 --- a/llarp/service/intro_set.cpp +++ b/llarp/service/intro_set.cpp @@ -38,7 +38,7 @@ namespace llarp::service EncryptedIntroSet::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { bool read = false; - if (key == "x") + if (key.startswith("x")) { llarp_buffer_t strbuf; if (not bencode_read_string(buf, &strbuf)) @@ -178,7 +178,7 @@ namespace llarp::service if (!BEncodeMaybeReadDictEntry("a", addressKeys, read, key, buf)) return false; - if (key == "e") + if (key.startswith("e")) { net::TrafficPolicy policy; if (not policy.BDecode(buf)) @@ -187,7 +187,7 @@ namespace llarp::service return true; } - if (key == "i") + if (key.startswith("i")) { return BEncodeReadList(intros, buf); } @@ -197,7 +197,7 @@ namespace llarp::service if (!BEncodeMaybeReadDictEntry("n", topic, read, key, buf)) return false; - if (key == "p") + if (key.startswith("p")) { return bencode_read_list( [&](llarp_buffer_t* buf, bool more) { @@ -213,12 +213,12 @@ namespace llarp::service buf); } - if (key == "r") + if (key.startswith("r")) { return BEncodeReadSet(ownedRanges, buf); } - if (key == "s") + if (key.startswith("s")) { byte_t* begin = buf->cur; if (not bencode_discard(buf)) diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index 0b9939c59..b838c44cd 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -43,7 +43,7 @@ namespace llarp bool read = false; if (!BEncodeMaybeReadDictInt("a", proto, read, k, buf)) return false; - if (k == "d") + if (k.startswith("d")) { llarp_buffer_t strbuf; if (!bencode_read_string(buf, &strbuf)) @@ -168,7 +168,7 @@ namespace llarp ProtocolFrame::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) { bool read = false; - if (key == "A") + if (key.startswith("A")) { llarp_buffer_t strbuf; if (!bencode_read_string(val, &strbuf)) diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index c02453f58..24f6a520b 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -55,7 +55,7 @@ namespace llarp BEncodeMaybeReadDictList( const char* k, List_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == k) + if (key.startswith(k)) { if (!BEncodeReadList(item, buf)) { @@ -71,7 +71,7 @@ namespace llarp BEncodeMaybeReadDictEntry( const char* k, Item_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == k) + if (key.startswith(k)) { if (!item.BDecode(buf)) { @@ -89,7 +89,7 @@ namespace llarp BEncodeMaybeReadDictInt( const char* k, Int_t& i, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == k) + if (key.startswith(k)) { uint64_t read_i; if (!bencode_read_integer(buf, &read_i)) @@ -116,7 +116,7 @@ namespace llarp const llarp_buffer_t& key, llarp_buffer_t* buf) { - if (key == k) + if (key.startswith(k)) { if (!bencode_read_integer(buf, &item)) return false; diff --git a/llarp/util/buffer.cpp b/llarp/util/buffer.cpp index 2373ceadc..bc495dacd 100644 --- a/llarp/util/buffer.cpp +++ b/llarp/util/buffer.cpp @@ -4,18 +4,6 @@ #include #include -size_t -llarp_buffer_t::size_left() const -{ - size_t diff = cur - base; - if (diff > sz) - { - return 0; - } - - return sz - diff; -} - bool llarp_buffer_t::writef(const char* fmt, ...) { @@ -127,12 +115,6 @@ llarp_buffer_t::copy() const return copy; } -llarp::byte_view_t -llarp_buffer_t::view() const -{ - return {base, sz}; - -} namespace llarp { diff --git a/llarp/util/buffer.hpp b/llarp/util/buffer.hpp index 1bfef5db4..343b2f466 100644 --- a/llarp/util/buffer.hpp +++ b/llarp/util/buffer.hpp @@ -117,7 +117,14 @@ struct [[deprecated("this type is stupid, use something else")]] llarp_buffer_t return base + sz; } - size_t size_left() const; + size_t size_left() const + { + size_t diff = cur - base; + assert(diff <= sz); + if (diff > sz) + return 0; + return sz - diff; + } template bool read_into(OutputIt begin, OutputIt end); @@ -149,12 +156,23 @@ struct [[deprecated("this type is stupid, use something else")]] llarp_buffer_t /// make a copy of this buffer std::vector copy() const; - /// get a read only view over the entire region - llarp::byte_view_t view() const; + /// get a read-only view over the entire region + llarp::byte_view_t view_all() const + { + return {base, sz}; + } + + /// get a read-only view over the remaining/unused region + llarp::byte_view_t view_remaining() const + { + return {cur, size_left()}; + } - bool operator==(std::string_view data) const + /// Part of the curse. Returns true if the remaining buffer space starts with the given string view. + bool startswith(std::string_view prefix_str) const { - return std::string_view{reinterpret_cast(base), sz} == data; + llarp::byte_view_t prefix{reinterpret_cast(prefix_str.data()), prefix_str.size()}; + return view_remaining().substr(0, prefix.size()) == prefix; } private: