From a0125ffe86e9a1a1a25d7566a2b62b5f7968ed7d Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 15 Feb 2019 23:04:04 +0000 Subject: [PATCH 1/2] Move abyss/json to llarp/util/json --- CMakeLists.txt | 6 +----- libabyss/include/abyss/client.hpp | 6 +++--- libabyss/include/abyss/http.hpp | 8 +++++--- libabyss/include/abyss/server.hpp | 6 +++--- libabyss/main.cpp | 4 ++-- libabyss/src/client.cpp | 7 ++++--- libabyss/src/server.cpp | 9 +++++---- llarp/CMakeLists.txt | 4 +--- llarp/link/iwp_internal.hpp | 7 +++++++ llarp/rpc/rpc.cpp | 6 +++--- {libabyss/src => llarp/util}/json.cpp | 6 +++--- {libabyss/include/abyss => llarp/util}/json.hpp | 11 ++++++----- llarp/util/status.hpp | 8 ++++---- test/test_libabyss.cpp | 2 +- 14 files changed, 48 insertions(+), 42 deletions(-) rename {libabyss/src => llarp/util}/json.cpp (94%) rename {libabyss/include/abyss => llarp/util}/json.hpp (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0be0b1a9d..34ba8046b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -316,11 +316,8 @@ if(USE_LIBABYSS) set(ABYSS_SRC ${ABYSS}/src/http.cpp ${ABYSS}/src/client.cpp - ${ABYSS}/src/server.cpp - ${ABYSS}/src/json.cpp) + ${ABYSS}/src/server.cpp) add_library(${ABYSS_LIB} STATIC ${ABYSS_SRC}) - - endif(USE_LIBABYSS) add_subdirectory(crypto) @@ -337,7 +334,6 @@ if(USE_LIBABYSS) add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp llarp/win32/abyss.rc) target_link_libraries(${ABYSS_EXE} PUBLIC ${ABYSS_LIB} ${STATIC_LIB} ws2_32) endif(NOT WIN32) - target_include_directories(${UTIL_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include") target_include_directories(${ABYSS_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include") target_include_directories(${ABYSS_EXE} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include") # for freebsd diff --git a/libabyss/include/abyss/client.hpp b/libabyss/include/abyss/client.hpp index 8a6bd3aea..302418fd2 100644 --- a/libabyss/include/abyss/client.hpp +++ b/libabyss/include/abyss/client.hpp @@ -1,8 +1,8 @@ #ifndef __ABYSS_CLIENT_HPP__ #define __ABYSS_CLIENT_HPP__ -#include #include +#include #include #include @@ -18,8 +18,8 @@ namespace abyss namespace http { using RPC_Method_t = std::string; - using RPC_Params = json::Value; - using RPC_Response = json::Document; + using RPC_Params = llarp::json::Value; + using RPC_Response = llarp::json::Document; using Headers_t = std::unordered_multimap< std::string, std::string >; struct ConnImpl; diff --git a/libabyss/include/abyss/http.hpp b/libabyss/include/abyss/http.hpp index 2298ba782..0ac3abfb5 100644 --- a/libabyss/include/abyss/http.hpp +++ b/libabyss/include/abyss/http.hpp @@ -1,6 +1,6 @@ #ifndef __ABYSS_HTTP_HPP__ #define __ABYSS_HTTP_HPP__ -#include +#include #include #include @@ -20,16 +20,18 @@ namespace abyss struct HeaderReader { + using string_view = llarp::string_view; + RequestHeader Header; virtual ~HeaderReader() { } bool - ProcessHeaderLine(abyss::string_view line, bool& done); + ProcessHeaderLine(string_view line, bool& done); virtual bool - ShouldProcessHeader(const abyss::string_view& line) const = 0; + ShouldProcessHeader(const string_view& line) const = 0; }; } // namespace http diff --git a/libabyss/include/abyss/server.hpp b/libabyss/include/abyss/server.hpp index f6891bb2a..5a3046afa 100644 --- a/libabyss/include/abyss/server.hpp +++ b/libabyss/include/abyss/server.hpp @@ -1,8 +1,8 @@ #ifndef __ABYSS_SERVER_HPP__ #define __ABYSS_SERVER_HPP__ -#include #include +#include #include #include #include @@ -21,8 +21,8 @@ namespace abyss struct IRPCHandler { using Method_t = std::string; - using Params = json::Value; - using Response = json::Writer; + using Params = llarp::json::Value; + using Response = llarp::json::Writer; IRPCHandler(ConnImpl* impl); diff --git a/libabyss/main.cpp b/libabyss/main.cpp index 3ef0faad3..5d4d174d1 100644 --- a/libabyss/main.cpp +++ b/libabyss/main.cpp @@ -32,7 +32,7 @@ struct DemoCall : public abyss::http::IRPCClientHandler bool HandleResponse(abyss::http::RPC_Response resp) override { - abyss::json::ToString(resp, std::cout); + llarp::json::ToString(resp, std::cout); return true; } @@ -60,7 +60,7 @@ struct DemoClient : public abyss::http::JSONRPC void DoDemoRequest() { - abyss::json::Value params; + llarp::json::Value params; params.SetObject(); QueueRPC("test", std::move(params), std::bind(&DemoClient::NewConn, this, std::placeholders::_1)); diff --git a/libabyss/src/client.cpp b/libabyss/src/client.cpp index f291ee621..a8bb99d2d 100644 --- a/libabyss/src/client.cpp +++ b/libabyss/src/client.cpp @@ -8,6 +8,7 @@ namespace abyss { namespace http { + namespace json = llarp::json; struct ConnImpl : HeaderReader { // big @@ -16,7 +17,7 @@ namespace abyss json::Document m_RequestBody; Headers_t m_SendHeaders; IRPCClientHandler* handler; - std::unique_ptr< abyss::json::IParser > m_BodyParser; + std::unique_ptr< json::IParser > m_BodyParser; json::Document m_Response; enum State @@ -94,7 +95,7 @@ namespace abyss } bool - ShouldProcessHeader(const abyss::string_view& name) const + ShouldProcessHeader(const llarp::string_view& name) const { return name == "content-length" || name == "content-type"; } @@ -123,7 +124,7 @@ namespace abyss if(contentSize > MAX_BODY_SIZE) return false; - m_BodyParser.reset(abyss::json::MakeParser(contentSize)); + m_BodyParser.reset(json::MakeParser(contentSize)); } if(m_BodyParser && m_BodyParser->FeedData(buf, sz)) { diff --git a/libabyss/src/server.cpp b/libabyss/src/server.cpp index cdfa62cd4..75a7c757b 100644 --- a/libabyss/src/server.cpp +++ b/libabyss/src/server.cpp @@ -14,6 +14,7 @@ namespace abyss { namespace httpd { + namespace json = llarp::json; struct ConnImpl : abyss::http::HeaderReader { llarp_tcp_conn* _conn; @@ -22,11 +23,11 @@ namespace abyss llarp_time_t m_LastActive; llarp_time_t m_ReadTimeout; bool m_Bad; - std::unique_ptr< abyss::json::IParser > m_BodyParser; + std::unique_ptr< json::IParser > m_BodyParser; json::Document m_Request; std::stringstream m_ResponseBuffer; - abyss::json::Stream m_ResponseStream; - abyss::json::Writer m_Response; + json::Stream m_ResponseStream; + json::Writer m_Response; enum HTTPState { @@ -167,7 +168,7 @@ namespace abyss } else { - m_BodyParser.reset(abyss::json::MakeParser(contentLength)); + m_BodyParser.reset(json::MakeParser(contentLength)); } } if(!m_BodyParser->FeedData(buf, sz)) diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 043bf1882..c2122a09c 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -13,6 +13,7 @@ set(LIB_UTIL_SRC util/endian.cpp util/fs.cpp util/ini.cpp + util/json.cpp util/logger.cpp util/logic.cpp util/mem.cpp @@ -78,7 +79,6 @@ if(WIN32) target_link_libraries(${PLATFORM_LIB} PUBLIC iphlpapi) endif() - set(DNSLIB_SRC dns/dotlokilookup.cpp dns/dns.cpp @@ -206,8 +206,6 @@ target_link_libraries(${STATIC_LIB} PUBLIC ${PLATFORM_LIB} ${UTIL_LIB} ${CRYPTOG if(USE_LIBABYSS) add_definitions(-DUSE_ABYSS=1) - target_link_libraries(${UTIL_LIB} PUBLIC ${ABYSS_LIB}) - target_link_libraries(${PLATFORM_LIB} PUBLIC ${ABYSS_LIB}) target_link_libraries(${STATIC_LIB} PUBLIC ${ABYSS_LIB}) endif() diff --git a/llarp/link/iwp_internal.hpp b/llarp/link/iwp_internal.hpp index 96dfb7a68..579f8ed99 100644 --- a/llarp/link/iwp_internal.hpp +++ b/llarp/link/iwp_internal.hpp @@ -28,6 +28,13 @@ namespace llarp const AddressInfo &ai); ~Session(); + util::StatusObject + ExtractStatus() const override + { + // TODO: fill me in. + return {}; + } + void PumpIO(); diff --git a/llarp/rpc/rpc.cpp b/llarp/rpc/rpc.cpp index 134f6cca3..19f29702e 100644 --- a/llarp/rpc/rpc.cpp +++ b/llarp/rpc/rpc.cpp @@ -29,7 +29,7 @@ namespace llarp } virtual bool - HandleJSONResult(const ::abyss::json::Value& val) = 0; + HandleJSONResult(const json::Value& val) = 0; bool HandleResponse(::abyss::http::RPC_Response response) @@ -69,7 +69,7 @@ namespace llarp } bool - HandleJSONResult(const ::abyss::json::Value& result) override + HandleJSONResult(const json::Value& result) override { PubkeyList_t keys; if(!result.IsObject()) @@ -153,7 +153,7 @@ namespace llarp AsyncUpdatePubkeyList() { LogInfo("Updating service node list"); - ::abyss::json::Value params; + json::Value params; params.SetObject(); QueueRPC("get_all_service_nodes_keys", std::move(params), std::bind(&CallerImpl::NewAsyncUpdatePubkeyListConn, this, diff --git a/libabyss/src/json.cpp b/llarp/util/json.cpp similarity index 94% rename from libabyss/src/json.cpp rename to llarp/util/json.cpp index 8bfc8d070..36882971d 100644 --- a/libabyss/src/json.cpp +++ b/llarp/util/json.cpp @@ -1,11 +1,11 @@ -#include +#include #include #include #include #include -namespace abyss +namespace llarp { namespace json { @@ -56,4 +56,4 @@ namespace abyss } } // namespace json -} // namespace abyss +} // namespace llarp diff --git a/libabyss/include/abyss/json.hpp b/llarp/util/json.hpp similarity index 95% rename from libabyss/include/abyss/json.hpp rename to llarp/util/json.hpp index 022e6e7f3..0bfa1b2e9 100644 --- a/libabyss/include/abyss/json.hpp +++ b/llarp/util/json.hpp @@ -1,12 +1,13 @@ -#ifndef __ABYSS_JSON_JSON_HPP -#define __ABYSS_JSON_JSON_HPP +#ifndef LLARP_UTIL_JSON_HPP +#define LLARP_UTIL_JSON_HPP -#include #include #include + +#include #include -namespace abyss +namespace llarp { namespace json { @@ -116,6 +117,6 @@ namespace abyss ToString(const json::Document& obj, std::ostream& out); } // namespace json -} // namespace abyss +} // namespace llarp #endif diff --git a/llarp/util/status.hpp b/llarp/util/status.hpp index 12ed47671..02e06ea42 100644 --- a/llarp/util/status.hpp +++ b/llarp/util/status.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_UTIL_STATUS_HPP #define LLARP_UTIL_STATUS_HPP #ifdef USE_ABYSS -#include +#include #endif #include #include @@ -14,8 +14,8 @@ namespace llarp namespace util { #ifdef USE_ABYSS - using StatusObject_Impl = ::abyss::json::Document; - using Value_t = ::abyss::json::Value; + using StatusObject_Impl = json::Document; + using Value_t = json::Value; #else struct StatusObject_Impl { @@ -27,7 +27,7 @@ namespace llarp struct StatusObject { - using String_t = llarp::string_view; + using String_t = string_view; using Variant = absl::variant< uint64_t, std::string, bool, StatusObject, std::vector< std::string >, std::vector< StatusObject > >; diff --git a/test/test_libabyss.cpp b/test/test_libabyss.cpp index ae36de93f..8eff239f3 100644 --- a/test/test_libabyss.cpp +++ b/test/test_libabyss.cpp @@ -200,7 +200,7 @@ struct AbyssTest : public AbyssTestBase, TEST_F(AbyssTest, TestClientAndServer) { Start(); - abyss::json::Value params; + llarp::json::Value params; params.SetObject(); QueueRPC(method, std::move(params), std::bind(&AbyssTest::NewConn, this, std::placeholders::_1)); From a00d6afc5e23e9b66a764530fee19d6abacbd83b Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Feb 2019 12:13:34 +0000 Subject: [PATCH 2/2] Convert llarp_buffer_t to be a class with methods --- llarp/dht/message.cpp | 2 +- llarp/dht/messages/findrouter.cpp | 10 +-- llarp/dht/messages/gotintro.cpp | 4 +- llarp/dht/messages/gotrouter.cpp | 8 +- llarp/dht/messages/pubintro.cpp | 4 +- llarp/dns.cpp | 12 +-- llarp/dns/message.cpp | 26 +++---- llarp/dns/name.cpp | 6 +- llarp/dns/question.cpp | 8 +- llarp/dns/rr.cpp | 12 +-- llarp/dns/serialize.cpp | 8 +- llarp/link/iwp_internal.hpp | 19 +++-- llarp/link/utp.cpp | 22 +++--- llarp/messages/dht_immediate.cpp | 4 +- llarp/messages/link_intro.cpp | 12 +-- llarp/messages/link_message.cpp | 2 +- llarp/messages/relay_commit.cpp | 4 +- llarp/net/address_info.cpp | 12 +-- llarp/net/exit_info.cpp | 4 +- llarp/router/router.cpp | 37 +++++----- llarp/router_contact.cpp | 2 +- llarp/routing/dht_message.cpp | 6 +- llarp/routing/message_parser.cpp | 2 +- llarp/service/Identity.cpp | 2 +- llarp/service/IntroSet.cpp | 4 +- llarp/service/protocol.cpp | 4 +- llarp/util/bencode.cpp | 41 ++++++---- llarp/util/bencode.hpp | 14 ++-- llarp/util/buffer.cpp | 119 +++++++++++++----------------- llarp/util/buffer.hpp | 77 ++++++++++--------- 30 files changed, 248 insertions(+), 239 deletions(-) diff --git a/llarp/dht/message.cpp b/llarp/dht/message.cpp index dd822cd76..90711b797 100644 --- a/llarp/dht/message.cpp +++ b/llarp/dht/message.cpp @@ -33,7 +33,7 @@ namespace llarp // first key if(dec->firstKey) { - if(!llarp_buffer_eq(*key, "A")) + if(!(*key == "A")) return false; if(!bencode_read_string(r->buffer, &strbuf)) return false; diff --git a/llarp/dht/messages/findrouter.cpp b/llarp/dht/messages/findrouter.cpp index 4cc5efd71..e2db9c48e 100644 --- a/llarp/dht/messages/findrouter.cpp +++ b/llarp/dht/messages/findrouter.cpp @@ -100,7 +100,7 @@ namespace llarp { llarp_buffer_t strbuf; - if(llarp_buffer_eq(key, "E")) + if(key == "E") { uint64_t result; if(!bencode_read_integer(val, &result)) @@ -110,7 +110,7 @@ namespace llarp return true; } - if(llarp_buffer_eq(key, "I")) + if(key == "I") { uint64_t result; if(!bencode_read_integer(val, &result)) @@ -119,7 +119,7 @@ namespace llarp iterative = result != 0; return true; } - if(llarp_buffer_eq(key, "K")) + if(key == "K") { if(!bencode_read_string(val, &strbuf)) return false; @@ -129,11 +129,11 @@ namespace llarp std::copy(strbuf.base, strbuf.base + K.SIZE, K.begin()); return true; } - if(llarp_buffer_eq(key, "T")) + if(key == "T") { return bencode_read_integer(val, &txid); } - if(llarp_buffer_eq(key, "V")) + if(key == "V") { return bencode_read_integer(val, &version); } diff --git a/llarp/dht/messages/gotintro.cpp b/llarp/dht/messages/gotintro.cpp index ed12fe685..45b5a60fc 100644 --- a/llarp/dht/messages/gotintro.cpp +++ b/llarp/dht/messages/gotintro.cpp @@ -83,11 +83,11 @@ namespace llarp bool GotIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf) { - if(llarp_buffer_eq(key, "I")) + if(key == "I") { return BEncodeReadList(I, buf); } - if(llarp_buffer_eq(key, "K")) + if(key == "K") { if(K) // duplicate key? return false; diff --git a/llarp/dht/messages/gotrouter.cpp b/llarp/dht/messages/gotrouter.cpp index 05837ec21..9887fb32c 100644 --- a/llarp/dht/messages/gotrouter.cpp +++ b/llarp/dht/messages/gotrouter.cpp @@ -52,22 +52,22 @@ namespace llarp bool GotRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val) { - if(llarp_buffer_eq(key, "K")) + if(key == "K") { if(K) // duplicate key? return false; K.reset(new dht::Key_t()); return K->BDecode(val); } - if(llarp_buffer_eq(key, "N")) + if(key == "N") { return BEncodeReadList(N, val); } - if(llarp_buffer_eq(key, "R")) + if(key == "R") { return BEncodeReadList(R, val); } - if(llarp_buffer_eq(key, "T")) + if(key == "T") { return bencode_read_integer(val, &txid); } diff --git a/llarp/dht/messages/pubintro.cpp b/llarp/dht/messages/pubintro.cpp index 6578e68aa..d9c0ea153 100644 --- a/llarp/dht/messages/pubintro.cpp +++ b/llarp/dht/messages/pubintro.cpp @@ -19,7 +19,7 @@ namespace llarp llarp_buffer_t *val) { bool read = false; - if(llarp_buffer_eq(key, "E")) + if(key == "E") { return BEncodeReadList(E, val); } @@ -27,7 +27,7 @@ namespace llarp return false; if(!BEncodeMaybeReadDictInt("R", R, read, key, val)) return false; - if(llarp_buffer_eq(key, "S")) + if(key == "S") { read = true; hasS = true; diff --git a/llarp/dns.cpp b/llarp/dns.cpp index 7564c2205..e3e4c36ab 100644 --- a/llarp/dns.cpp +++ b/llarp/dns.cpp @@ -313,17 +313,17 @@ extern "C" uint16_t fields; // reads as HOST byte order - if(!llarp_buffer_read_uint16(buffer, &hdr->id)) + if(!buffer->read_uint16(hdr->id)) return false; - if(!llarp_buffer_read_uint16(buffer, &fields)) + if(!buffer->read_uint16(fields)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->qdCount)) + if(!buffer->read_uint16(hdr->qdCount)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->anCount)) + if(!buffer->read_uint16(hdr->anCount)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->nsCount)) + if(!buffer->read_uint16(hdr->nsCount)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->arCount)) + if(!buffer->read_uint16(hdr->arCount)) return false; // decode fields into hdr diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index 8536df98e..1cfcd7836 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -14,33 +14,33 @@ namespace llarp bool MessageHeader::Encode(llarp_buffer_t* buf) const { - if(!llarp_buffer_put_uint16(buf, id)) + if(!buf->put_uint16(id)) return false; - if(!llarp_buffer_put_uint16(buf, fields)) + if(!buf->put_uint16(fields)) return false; - if(!llarp_buffer_put_uint16(buf, qd_count)) + if(!buf->put_uint16(qd_count)) return false; - if(!llarp_buffer_put_uint16(buf, an_count)) + if(!buf->put_uint16(an_count)) return false; - if(!llarp_buffer_put_uint16(buf, ns_count)) + if(!buf->put_uint16(ns_count)) return false; - return llarp_buffer_put_uint16(buf, ar_count); + return buf->put_uint16(ar_count); } bool MessageHeader::Decode(llarp_buffer_t* buf) { - if(!llarp_buffer_read_uint16(buf, &id)) + if(!buf->read_uint16(id)) return false; - if(!llarp_buffer_read_uint16(buf, &fields)) + if(!buf->read_uint16(fields)) return false; - if(!llarp_buffer_read_uint16(buf, &qd_count)) + if(!buf->read_uint16(qd_count)) return false; - if(!llarp_buffer_read_uint16(buf, &an_count)) + if(!buf->read_uint16(an_count)) return false; - if(!llarp_buffer_read_uint16(buf, &ns_count)) + if(!buf->read_uint16(ns_count)) return false; - return llarp_buffer_read_uint16(buf, &ar_count); + return buf->read_uint16(ar_count); } Message::Message(Message&& other) @@ -232,7 +232,7 @@ namespace llarp rec.ttl = ttl; std::array< byte_t, 512 > tmp = {{0}}; llarp_buffer_t buf(tmp); - llarp_buffer_put_uint16(&buf, priority); + buf.put_uint16(priority); if(EncodeName(&buf, name)) { buf.sz = buf.cur - buf.base; diff --git a/llarp/dns/name.cpp b/llarp/dns/name.cpp index 46f466b1f..b0936d7bc 100644 --- a/llarp/dns/name.cpp +++ b/llarp/dns/name.cpp @@ -11,7 +11,7 @@ namespace llarp bool DecodeName(llarp_buffer_t* buf, Name_t& name) { - if(llarp_buffer_size_left(*buf) < 1) + if(buf->size_left() < 1) return false; std::stringstream ss; size_t l; @@ -27,7 +27,7 @@ namespace llarp llarp::DumpBuffer(*buf); return false; } - if(llarp_buffer_size_left(*buf) < l) + if(buf->size_left() < l) return false; ss << Name_t((const char*)buf->cur, l); @@ -56,7 +56,7 @@ namespace llarp return false; *(buf->cur) = l; buf->cur++; - if(llarp_buffer_size_left(*buf) < l) + if(buf->size_left() < l) return false; if(l) { diff --git a/llarp/dns/question.cpp b/llarp/dns/question.cpp index 27c65ca46..b1ea92f62 100644 --- a/llarp/dns/question.cpp +++ b/llarp/dns/question.cpp @@ -22,9 +22,9 @@ namespace llarp { if(!EncodeName(buf, qname)) return false; - if(!llarp_buffer_put_uint16(buf, qtype)) + if(!buf->put_uint16(qtype)) return false; - return llarp_buffer_put_uint16(buf, qclass); + return buf->put_uint16(qclass); } bool @@ -35,12 +35,12 @@ namespace llarp llarp::LogError("failed to decode name"); return false; } - if(!llarp_buffer_read_uint16(buf, &qtype)) + if(!buf->read_uint16(qtype)) { llarp::LogError("failed to decode type"); return false; } - if(!llarp_buffer_read_uint16(buf, &qclass)) + if(!buf->read_uint16(qclass)) { llarp::LogError("failed to decode class"); return false; diff --git a/llarp/dns/rr.cpp b/llarp/dns/rr.cpp index 5d978c3b2..4beae892d 100644 --- a/llarp/dns/rr.cpp +++ b/llarp/dns/rr.cpp @@ -31,15 +31,15 @@ namespace llarp { return false; } - if(!llarp_buffer_put_uint16(buf, rr_type)) + if(!buf->put_uint16(rr_type)) { return false; } - if(!llarp_buffer_put_uint16(buf, rr_class)) + if(!buf->put_uint16(rr_class)) { return false; } - if(!llarp_buffer_put_uint32(buf, ttl)) + if(!buf->put_uint32(ttl)) { return false; } @@ -58,17 +58,17 @@ namespace llarp llarp::LogError("failed to decode rr name"); return false; } - if(!llarp_buffer_read_uint16(buf, &rr_type)) + if(!buf->read_uint16(rr_type)) { llarp::LogError("failed to decode rr type"); return false; } - if(!llarp_buffer_read_uint16(buf, &rr_class)) + if(!buf->read_uint16(rr_class)) { llarp::LogError("failed to decode rr class"); return false; } - if(!llarp_buffer_read_uint32(buf, &ttl)) + if(!buf->read_uint32(ttl)) { llarp::LogError("failed to decode ttl"); return false; diff --git a/llarp/dns/serialize.cpp b/llarp/dns/serialize.cpp index 8e69cd807..7f13b394c 100644 --- a/llarp/dns/serialize.cpp +++ b/llarp/dns/serialize.cpp @@ -11,9 +11,9 @@ namespace llarp if(v.size() > 65536) return false; uint16_t len = v.size(); - if(!llarp_buffer_put_uint16(buf, len)) + if(!buf->put_uint16(len)) return false; - if(llarp_buffer_size_left(*buf) < len) + if(buf->size_left() < len) return false; memcpy(buf->cur, v.data(), len); buf->cur += len; @@ -24,9 +24,9 @@ namespace llarp DecodeRData(llarp_buffer_t* buf, std::vector< byte_t >& v) { uint16_t len; - if(!llarp_buffer_read_uint16(buf, &len)) + if(!buf->read_uint16(len)) return false; - size_t left = llarp_buffer_size_left(*buf); + size_t left = buf->size_left(); if(left < len) return false; v.resize(size_t(len)); diff --git a/llarp/link/iwp_internal.hpp b/llarp/link/iwp_internal.hpp index 96dfb7a68..3c1977d49 100644 --- a/llarp/link/iwp_internal.hpp +++ b/llarp/link/iwp_internal.hpp @@ -28,6 +28,13 @@ namespace llarp const AddressInfo &ai); ~Session(); + util::StatusObject + ExtractStatus() const override + { + // TODO: fill me in. + return {}; + } + void PumpIO(); @@ -124,7 +131,7 @@ namespace llarp bool Decode(llarp_buffer_t *buf) { - if(llarp_buffer_size_left(*buf) < fragoverhead) + if(buf->size_left() < fragoverhead) return false; version = *buf->cur; if(version != LLARP_PROTO_VERSION) @@ -136,8 +143,8 @@ namespace llarp buf->cur++; fragno = *buf->cur; buf->cur++; - llarp_buffer_read_uint16(buf, &fraglen); - llarp_buffer_read_uint32(buf, &seqno); + buf->read_uint16(fraglen); + buf->read_uint32(seqno); return fraglen <= fragsize; } @@ -147,7 +154,7 @@ namespace llarp if(body.sz > fragsize) return false; fraglen = body.sz; - if(llarp_buffer_size_left(*buf) < (fragoverhead + fraglen)) + if(buf->size_left() < (fragoverhead + fraglen)) return false; *buf->cur = LLARP_PROTO_VERSION; buf->cur++; @@ -157,8 +164,8 @@ namespace llarp buf->cur++; *buf->cur = fragno; buf->cur++; - llarp_buffer_put_uint16(buf, fraglen); - llarp_buffer_put_uint32(buf, seqno); + buf->put_uint16(fraglen); + buf->put_uint32(seqno); if(fraglen) memcpy(buf->cur, body.base, fraglen); buf->cur += fraglen; diff --git a/llarp/link/utp.cpp b/llarp/link/utp.cpp index a4520cc2c..3da91cf5b 100644 --- a/llarp/link/utp.cpp +++ b/llarp/link/utp.cpp @@ -40,7 +40,7 @@ namespace llarp bool InboundMessage::AppendData(const byte_t* ptr, uint16_t sz) { - if(llarp_buffer_size_left(buffer) < sz) + if(buffer.size_left() < sz) return false; memcpy(buffer.cur, ptr, sz); buffer.cur += sz; @@ -867,7 +867,6 @@ namespace llarp } else { - LogWarn("utp_socket got data with no underlying session"); utp_shutdown(arg->socket, SHUT_RDWR); utp_close(arg->socket); @@ -986,14 +985,12 @@ namespace llarp util::StatusObject Session::ExtractStatus() const { - return { - {"client", !remoteRC.IsPublicRouter()}, - {"sendBacklog", uint64_t(SendQueueBacklog())}, - {"tx", m_TXRate}, - {"rx", m_RXRate}, - {"remoteAddr", remoteAddr.ToString()}, - {"pubkey", remoteRC.pubkey.ToHex()} - }; + return {{"client", !remoteRC.IsPublicRouter()}, + {"sendBacklog", uint64_t(SendQueueBacklog())}, + {"tx", m_TXRate}, + {"rx", m_RXRate}, + {"remoteAddr", remoteAddr.ToString()}, + {"pubkey", remoteRC.pubkey.ToHex()}}; } bool @@ -1079,15 +1076,14 @@ namespace llarp out.cur += A.size(); // read msgid uint32_t msgid; - if(!llarp_buffer_read_uint32(&out, &msgid)) + if(!out.read_uint32(msgid)) { LogError("failed to read msgid"); return false; } // read length and remaining uint16_t length, remaining; - if(!(llarp_buffer_read_uint16(&out, &length) - && llarp_buffer_read_uint16(&out, &remaining))) + if(!(out.read_uint16(length) && out.read_uint16(remaining))) { LogError("failed to read the rest of the header"); return false; diff --git a/llarp/messages/dht_immediate.cpp b/llarp/messages/dht_immediate.cpp index 295d55b24..451e7acbe 100644 --- a/llarp/messages/dht_immediate.cpp +++ b/llarp/messages/dht_immediate.cpp @@ -17,10 +17,10 @@ namespace llarp bool DHTImmediateMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf) { - if(llarp_buffer_eq(key, "m")) + if(key == "m") return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()), buf, msgs); - if(llarp_buffer_eq(key, "v")) + if(key == "v") { if(!bencode_read_integer(buf, &version)) return false; diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index 6254bff70..d4879138d 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -15,7 +15,7 @@ namespace llarp bool LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, "a")) + if(key == "a") { llarp_buffer_t strbuf; if(!bencode_read_string(buf, &strbuf)) @@ -24,18 +24,18 @@ namespace llarp return false; return *strbuf.cur == 'i'; } - if(llarp_buffer_eq(key, "n")) + else if(key == "n") { if(N.BDecode(buf)) return true; llarp::LogWarn("failed to decode nonce in LIM"); return false; } - if(llarp_buffer_eq(key, "p")) + else if(key == "p") { return bencode_read_integer(buf, &P); } - if(llarp_buffer_eq(key, "r")) + else if(key == "r") { if(rc.BDecode(buf)) return true; @@ -43,7 +43,7 @@ namespace llarp llarp::DumpBuffer(*buf); return false; } - else if(llarp_buffer_eq(key, "v")) + else if(key == "v") { if(!bencode_read_integer(buf, &version)) return false; @@ -56,7 +56,7 @@ namespace llarp llarp::LogDebug("LIM version ", version); return true; } - else if(llarp_buffer_eq(key, "z")) + else if(key == "z") { return Z.BDecode(buf); } diff --git a/llarp/messages/link_message.cpp b/llarp/messages/link_message.cpp index 00371da4c..d9a49d7b8 100644 --- a/llarp/messages/link_message.cpp +++ b/llarp/messages/link_message.cpp @@ -48,7 +48,7 @@ namespace llarp if(!key) return false; // we are expecting the first key to be 'a' - if(!llarp_buffer_eq(*key, "a")) + if(!(*key == "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 1a7bb7e69..5931a452a 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -17,7 +17,7 @@ namespace llarp bool LR_CommitMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, "c")) + if(key == "c") { return BEncodeReadArray(frames, buf); } @@ -132,7 +132,7 @@ namespace llarp if(!BEncodeMaybeReadVersion("v", self->version, LLARP_PROTO_VERSION, read, *key, r->buffer)) return false; - if(llarp_buffer_eq(*key, "w")) + if(*key == "w") { // check for duplicate if(self->work) diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index 2dfd89e2e..f56897c6c 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -50,7 +50,7 @@ namespace llarp llarp_buffer_t strbuf; // rank - if(llarp_buffer_eq(key, "c")) + if(key == "c") { if(!bencode_read_integer(buf, &i)) return false; @@ -63,7 +63,7 @@ namespace llarp } // dialect - if(llarp_buffer_eq(key, "d")) + if(key == "d") { if(!bencode_read_string(buf, &strbuf)) return false; @@ -76,13 +76,13 @@ namespace llarp } // encryption public key - if(llarp_buffer_eq(key, "e")) + if(key == "e") { return pubkey.BDecode(buf); } // ip address - if(llarp_buffer_eq(key, "i")) + if(key == "i") { if(!bencode_read_string(buf, &strbuf)) return false; @@ -96,7 +96,7 @@ namespace llarp } // port - if(llarp_buffer_eq(key, "p")) + if(key == "p") { if(!bencode_read_integer(buf, &i)) return false; @@ -109,7 +109,7 @@ namespace llarp } // version - if(llarp_buffer_eq(key, "v")) + if(key == "v") { if(!bencode_read_integer(buf, &i)) return false; diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index 967829cf3..40a074313 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -76,9 +76,9 @@ namespace llarp return false; if(!BEncodeMaybeReadDictInt("v", version, read, k, buf)) return false; - if(llarp_buffer_eq(k, "a")) + if(k == "a") return bdecode_ip_string(buf, address); - if(llarp_buffer_eq(k, "b")) + if(k == "b") return bdecode_ip_string(buf, netmask); return read; } diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index e90047ba6..c18aeec65 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -236,17 +236,19 @@ namespace llarp util::StatusObject obj{{"dht", _dht->impl.ExtractStatus()}, {"services", hiddenServiceContext.ExtractStatus()}, {"exit", _exitContext.ExtractStatus()}}; - std::vector ob_links, ib_links; - std::transform(inboundLinks.begin(), inboundLinks.end(), std::back_inserter(ib_links), [](const auto & link) -> util::StatusObject { - return link->ExtractStatus(); - }); - std::transform(outboundLinks.begin(), outboundLinks.end(), std::back_inserter(ob_links), [](const auto & link) -> util::StatusObject { - return link->ExtractStatus(); - }); - obj.Put("links", util::StatusObject{ - {"outbound", ob_links}, - {"inbound", ib_links} - }); + std::vector< util::StatusObject > ob_links, ib_links; + std::transform(inboundLinks.begin(), inboundLinks.end(), + std::back_inserter(ib_links), + [](const auto &link) -> util::StatusObject { + return link->ExtractStatus(); + }); + std::transform(outboundLinks.begin(), outboundLinks.end(), + std::back_inserter(ob_links), + [](const auto &link) -> util::StatusObject { + return link->ExtractStatus(); + }); + obj.Put("links", + util::StatusObject{{"outbound", ob_links}, {"inbound", ib_links}}); return obj; } @@ -1108,7 +1110,7 @@ namespace llarp if(!msg->BEncode(&buf)) { LogWarn("failed to encode outbound message, buffer size left: ", - llarp_buffer_size_left(buf)); + buf.size_left()); return; } // set size of message @@ -1255,13 +1257,14 @@ namespace llarp } void - Router::SetRouterWhitelist(const std::vector & routers) + Router::SetRouterWhitelist(const std::vector< RouterID > &routers) { lokinetRouters.clear(); - for(const auto & router : routers) - lokinetRouters.emplace(router, std::numeric_limits::max()); - LogInfo("lokinet service node list now has ", lokinetRouters.size(), - " routers"); + for(const auto &router : routers) + lokinetRouters.emplace(router, + std::numeric_limits< llarp_time_t >::max()); + LogInfo("lokinet service node list now has ", lokinetRouters.size(), + " routers"); } bool diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 592f404b6..60bc626ed 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -176,7 +176,7 @@ namespace llarp if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf)) return false; - if(llarp_buffer_eq(key, "n")) + if(key == "n") { llarp_buffer_t strbuf; if(!bencode_read_string(buf, &strbuf)) diff --git a/llarp/routing/dht_message.cpp b/llarp/routing/dht_message.cpp index 95ef96a3b..7e0cc5eb3 100644 --- a/llarp/routing/dht_message.cpp +++ b/llarp/routing/dht_message.cpp @@ -16,15 +16,15 @@ namespace llarp { llarp::dht::Key_t from; from.Zero(); - if(llarp_buffer_eq(key, "M")) + if(key == "M") { return llarp::dht::DecodeMesssageList(from, val, M, true); } - else if(llarp_buffer_eq(key, "S")) + else if(key == "S") { return bencode_read_integer(val, &S); } - else if(llarp_buffer_eq(key, "V")) + else if(key == "V") { return bencode_read_integer(val, &V); } diff --git a/llarp/routing/message_parser.cpp b/llarp/routing/message_parser.cpp index be4c422b5..ca56192ff 100644 --- a/llarp/routing/message_parser.cpp +++ b/llarp/routing/message_parser.cpp @@ -60,7 +60,7 @@ namespace llarp if(self->firstKey) { llarp_buffer_t strbuf; - if(!llarp_buffer_eq(*key, "A")) + if(!(*key == "A")) return false; if(!bencode_read_string(r->buffer, &strbuf)) return false; diff --git a/llarp/service/Identity.cpp b/llarp/service/Identity.cpp index 90bf8a847..b523e078c 100644 --- a/llarp/service/Identity.cpp +++ b/llarp/service/Identity.cpp @@ -34,7 +34,7 @@ namespace llarp bool read = false; if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf)) return false; - if(llarp_buffer_eq(key, "q")) + if(key == "q") { llarp_buffer_t str; if(!bencode_read_string(buf, &str)) diff --git a/llarp/service/IntroSet.cpp b/llarp/service/IntroSet.cpp index 4f4a95f39..7ed83b9f3 100644 --- a/llarp/service/IntroSet.cpp +++ b/llarp/service/IntroSet.cpp @@ -34,7 +34,7 @@ namespace llarp if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf)) return false; - if(llarp_buffer_eq(key, "i")) + if(key == "i") { return BEncodeReadList(I, buf); } @@ -47,7 +47,7 @@ namespace llarp if(!BEncodeMaybeReadDictInt("t", T, read, key, buf)) return false; - if(llarp_buffer_eq(key, "w")) + if(key == "w") { if(W) delete W; diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index 90ef54ae9..2eb678520 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -44,7 +44,7 @@ namespace llarp bool read = false; if(!BEncodeMaybeReadDictInt("a", proto, read, k, buf)) return false; - if(llarp_buffer_eq(k, "d")) + if(k == "d") { llarp_buffer_t strbuf; if(!bencode_read_string(buf, &strbuf)) @@ -126,7 +126,7 @@ namespace llarp ProtocolFrame::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) { bool read = false; - if(llarp_buffer_eq(key, "A")) + if(key == "A") { llarp_buffer_t strbuf; if(!bencode_read_string(val, &strbuf)) diff --git a/llarp/util/bencode.cpp b/llarp/util/bencode.cpp index 5d157ca5b..16e52e00c 100644 --- a/llarp/util/bencode.cpp +++ b/llarp/util/bencode.cpp @@ -15,8 +15,7 @@ bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result) buffer->cur++; - len = - llarp_buffer_read_until(buffer, 'e', (byte_t*)numbuf, sizeof(numbuf) - 1); + len = buffer->read_until('e', (byte_t*)numbuf, sizeof(numbuf) - 1); if(!len) { return false; @@ -34,8 +33,7 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result) { char numbuf[10]; - size_t len = - llarp_buffer_read_until(buffer, ':', (byte_t*)numbuf, sizeof(numbuf) - 1); + size_t len = buffer->read_until(':', (byte_t*)numbuf, sizeof(numbuf) - 1); if(!len) return false; @@ -50,7 +48,7 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result) buffer->cur++; - len = llarp_buffer_size_left(*buffer); + len = buffer->size_left(); if(len < slen) { return false; @@ -66,42 +64,55 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result) bool bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz) { - if(!llarp_buffer_writef(buff, "%zu:", sz)) + if(!buff->writef("%zu:", sz)) { return false; } - return llarp_buffer_write(buff, data, sz); + return buff->write(reinterpret_cast< const char* >(data), + reinterpret_cast< const char* >(data) + sz); } bool bencode_write_uint64(llarp_buffer_t* buff, uint64_t i) { - return llarp_buffer_writef(buff, "i%llu", i) - && llarp_buffer_write(buff, "e", 1); + if(!buff->writef("i%llu", i)) + { + return false; + } + + static const char letter[1] = {'e'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool bencode_write_version_entry(llarp_buffer_t* buff) { - return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION); + return buff->writef("1:vi%de", LLARP_PROTO_VERSION); } bool bencode_start_list(llarp_buffer_t* buff) { - return llarp_buffer_write(buff, "l", 1); + static const char letter[1] = {'l'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool bencode_start_dict(llarp_buffer_t* buff) { - return llarp_buffer_write(buff, "d", 1); + static const char letter[1] = {'d'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool bencode_end(llarp_buffer_t* buff) { - return llarp_buffer_write(buff, "e", 1); + static const char letter[1] = {'e'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool @@ -112,7 +123,7 @@ bencode_read_dict(llarp_buffer_t* buff, struct dict_reader* r) if(*r->buffer->cur != 'd') // ensure is a dictionary return false; r->buffer->cur++; - while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e') + while(r->buffer->size_left() && *r->buffer->cur != 'e') { if(bencode_read_string(r->buffer, &strbuf)) { @@ -145,7 +156,7 @@ bencode_read_list(llarp_buffer_t* buff, struct list_reader* r) } r->buffer->cur++; - while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e') + while(r->buffer->size_left() && *r->buffer->cur != 'e') { if(!r->on_item(r, true)) // check for early abort return false; diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index 893eca180..abe74895d 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -46,7 +46,7 @@ namespace llarp BEncodeMaybeReadDictList(const char* k, List_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!BEncodeReadList(item, buf)) { @@ -62,7 +62,7 @@ namespace llarp BEncodeMaybeReadDictEntry(const char* k, Item_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!item.BDecode(buf)) { @@ -81,7 +81,7 @@ namespace llarp BEncodeMaybeReadDictInt(const char* k, Int_t& i, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!bencode_read_integer(buf, &i)) { @@ -100,7 +100,7 @@ namespace llarp bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!bencode_read_integer(buf, &item)) return false; @@ -149,7 +149,7 @@ namespace llarp buf->cur++; size_t idx = 0; - while(llarp_buffer_size_left(*buf) && *buf->cur != 'e') + while(buf->size_left() && *buf->cur != 'e') { if(idx >= array.size()) return false; @@ -184,7 +184,7 @@ namespace llarp return false; buf->cur++; - while(llarp_buffer_size_left(*buf) && *buf->cur != 'e') + while(buf->size_left() && *buf->cur != 'e') { if(!result.emplace(result.end())->BDecode(buf)) return false; @@ -203,7 +203,7 @@ namespace llarp return false; buf->cur++; - while(llarp_buffer_size_left(*buf) && *buf->cur != 'e') + while(buf->size_left() && *buf->cur != 'e') { T item; if(!item.BDecode(buf)) diff --git a/llarp/util/buffer.cpp b/llarp/util/buffer.cpp index 30fe602b6..4518d9783 100644 --- a/llarp/util/buffer.cpp +++ b/llarp/util/buffer.cpp @@ -5,72 +5,99 @@ #include size_t -llarp_buffer_size_left(const llarp_buffer_t& buff) +llarp_buffer_t::size_left() const { - size_t diff = buff.cur - buff.base; - if(diff > buff.sz) + size_t diff = cur - base; + if(diff > sz) + { return 0; + } else - return buff.sz - diff; + return sz - diff; } bool -llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) +llarp_buffer_t::writef(const char* fmt, ...) { int written; - ssize_t sz = llarp_buffer_size_left(*buff); + ssize_t sz = size_left(); va_list args; va_start(args, fmt); - written = vsnprintf((char*)buff->cur, sz, fmt, args); + written = vsnprintf(reinterpret_cast< char* >(cur), sz, fmt, args); va_end(args); if(written <= 0) return false; if(sz < written) return false; - buff->cur += written; + cur += written; return true; } bool -llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz) +llarp_buffer_t::put_uint16(uint16_t i) { - size_t left = llarp_buffer_size_left(*buff); - if(left >= sz) - { - memcpy(buff->cur, data, sz); - buff->cur += sz; - return true; - } - return false; + if(size_left() < sizeof(uint16_t)) + return false; + htobe16buf(cur, i); + cur += sizeof(uint16_t); + return true; +} + +bool +llarp_buffer_t::put_uint32(uint32_t i) +{ + if(size_left() < sizeof(uint32_t)) + return false; + htobe32buf(cur, i); + cur += sizeof(uint32_t); + return true; +} + +bool +llarp_buffer_t::read_uint16(uint16_t& i) +{ + if(size_left() < sizeof(uint16_t)) + return false; + i = bufbe16toh(cur); + cur += sizeof(uint16_t); + return true; +} + +bool +llarp_buffer_t::read_uint32(uint32_t& i) +{ + if(size_left() < sizeof(uint32_t)) + return false; + i = bufbe32toh(cur); + cur += sizeof(uint32_t); + return true; } size_t -llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result, - size_t resultsize) +llarp_buffer_t::read_until(char delim, byte_t* result, size_t resultsize) { 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((cur != base + sz) && *cur != delim && resultsize) { - *result = *buff->cur; - buff->cur++; + *result = *cur; + cur++; result++; resultsize--; read++; } - if(llarp_buffer_size_left(*buff)) + if(size_left()) return read; else return 0; } bool -llarp_buffer_eq(const llarp_buffer_t& buf, const char* str) +operator==(const llarp_buffer_t& buff, const char* str) { - ManagedBuffer copy{buf}; + ManagedBuffer copy{buff}; while(*str && copy.underlying.cur != (copy.underlying.base + copy.underlying.sz)) { @@ -81,43 +108,3 @@ llarp_buffer_eq(const llarp_buffer_t& buf, const char* str) } return *str == 0; } - -bool -llarp_buffer_put_uint16(llarp_buffer_t* buf, uint16_t i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint16_t)) - return false; - htobe16buf(buf->cur, i); - buf->cur += sizeof(uint16_t); - return true; -} - -bool -llarp_buffer_put_uint32(llarp_buffer_t* buf, uint32_t i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint32_t)) - return false; - htobe32buf(buf->cur, i); - buf->cur += sizeof(uint32_t); - return true; -} - -bool -llarp_buffer_read_uint16(llarp_buffer_t* buf, uint16_t* i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint16_t)) - return false; - *i = bufbe16toh(buf->cur); - buf->cur += sizeof(uint16_t); - return true; -} - -bool -llarp_buffer_read_uint32(llarp_buffer_t* buf, uint32_t* i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint32_t)) - return false; - *i = bufbe32toh(buf->cur); - buf->cur += sizeof(uint32_t); - return true; -} diff --git a/llarp/util/buffer.hpp b/llarp/util/buffer.hpp index c500335ed..e7b4cd963 100644 --- a/llarp/util/buffer.hpp +++ b/llarp/util/buffer.hpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -112,12 +113,52 @@ struct llarp_buffer_t { } + size_t + size_left() const; + + template < typename InputIt > + bool + write(InputIt begin, InputIt end); + + bool + writef(const char *fmt, ...) __attribute__((format(printf, 2, 3))); + ; + + bool + put_uint16(uint16_t i); + bool + put_uint32(uint32_t i); + + bool + read_uint16(uint16_t &i); + bool + read_uint32(uint32_t &i); + + size_t + read_until(char delim, byte_t *result, size_t resultlen); + private: friend struct ManagedBuffer; llarp_buffer_t(const llarp_buffer_t &) = default; llarp_buffer_t(llarp_buffer_t &&) = default; }; +bool +operator==(const llarp_buffer_t &buff, const char *data); + +template < typename InputIt > +bool +llarp_buffer_t::write(InputIt begin, InputIt end) +{ + auto dist = std::distance(begin, end); + if(static_cast< decltype(dist) >(size_left()) >= dist) + { + cur = std::copy(begin, end, cur); + return true; + } + return false; +} + /** Provide a copyable/moveable wrapper around `llarp_buffer_t`. */ @@ -140,40 +181,4 @@ struct ManagedBuffer } }; -/// how much room is left in buffer -size_t -llarp_buffer_size_left(const llarp_buffer_t &buff); - -/// write a chunk of data size "sz" -bool -llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz); - -/// write multiple strings -bool -llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...); - -/// read buffer upto character delimiter -size_t -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(const llarp_buffer_t &buff, const char *data); - -/// put big endian unsigned 16 bit integer -bool -llarp_buffer_put_uint16(llarp_buffer_t *buf, uint16_t i); - -/// put big endian unsigned 32 bit integer -bool -llarp_buffer_put_uint32(llarp_buffer_t *buf, uint32_t i); - -/// read big endian unsigned 16 bit integer -bool -llarp_buffer_read_uint16(llarp_buffer_t *buf, uint16_t *i); - -/// read big endian unsigned 32 bit integer -bool -llarp_buffer_read_uint32(llarp_buffer_t *buf, uint32_t *i); - #endif