From 9e6a334da331a3e8d836aa7bcbb570114610834e Mon Sep 17 00:00:00 2001 From: dr7ana Date: Wed, 10 Apr 2024 04:39:50 -0700 Subject: [PATCH] Buffer methods - Fixed final bencode.h dependencies in AlignedBuffer that was pending refactor of other types - Last traces of any non-oxenc bencode are done - Next is TCP listeners! --- llarp/address/keys.hpp | 1 + llarp/config/config.hpp | 3 +- llarp/crypto/types.cpp | 38 ++--------------- llarp/crypto/types.hpp | 3 -- llarp/dns/srv_data.cpp | 1 - llarp/handlers/endpoint.cpp | 4 +- llarp/path/path.hpp | 2 +- llarp/service/info.hpp | 1 + llarp/session/outbound_session.cpp | 6 +-- llarp/session/session.hpp | 1 - llarp/util/aligned.hpp | 31 +++----------- llarp/util/bencode.h | 37 ----------------- llarp/util/buffer.hpp | 66 ------------------------------ 13 files changed, 19 insertions(+), 175 deletions(-) delete mode 100644 llarp/util/bencode.h diff --git a/llarp/address/keys.hpp b/llarp/address/keys.hpp index 825a6d0cd..5e032313e 100644 --- a/llarp/address/keys.hpp +++ b/llarp/address/keys.hpp @@ -2,6 +2,7 @@ #include #include +#include #include /** TODO: diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 3786ee787..7d2a3b72c 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -122,14 +122,13 @@ namespace llarp std::unordered_map exit_auths; std::unordered_map ons_exit_auths; + // RPC and File auth values auth::AuthType auth_type = auth::AuthType::NONE; auth::AuthFileType auth_file_type = auth::AuthFileType::HASHES; std::optional auth_url; std::optional auth_method; - std::unordered_set auth_whitelist; std::unordered_set auth_static_tokens; - std::set auth_files; std::vector srv_records; diff --git a/llarp/crypto/types.cpp b/llarp/crypto/types.cpp index 3419a7998..c86fbdffd 100644 --- a/llarp/crypto/types.cpp +++ b/llarp/crypto/types.cpp @@ -10,37 +10,6 @@ namespace llarp { - // bool PubKey::from_hex(const std::string& str) - // { - // if (str.size() != 2 * size()) - // return false; - // oxenc::from_hex(str.begin(), str.end(), begin()); - // return true; - // } - - // PubKey PubKey::make_from_hex(const std::string& s) - // { - // PubKey p; - // oxenc::from_hex(s.begin(), s.end(), p.begin()); - // return p; - // } - - // std::string PubKey::to_string() const - // { - // return oxenc::to_hex(begin(), end()); - // } - - // PubKey& PubKey::operator=(const uint8_t* ptr) - // { - // std::copy(ptr, ptr + SIZE, begin()); - // return *this; - // } - - // bool operator==(const PubKey& lhs, const PubKey& rhs) - // { - // return lhs.as_array() == rhs.as_array(); - // } - PubKey SecretKey::to_pubkey() const { return PubKey(data() + 32); @@ -49,7 +18,9 @@ namespace llarp bool SecretKey::load_from_file(const fs::path& fname) { size_t sz; - std::array tmp; + std::string tmp; + tmp.resize(128); + try { sz = util::file_to_buffer(fname, tmp.data(), tmp.size()); @@ -66,8 +37,7 @@ namespace llarp return true; } - llarp_buffer_t buf(tmp); - return BDecode(&buf); + return bt_decode(tmp); } bool SecretKey::recalculate() diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index d3192c7b9..d84cebdfa 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -128,7 +128,4 @@ namespace llarp /// PKE(result, publickey, secretkey, nonce) using path_dh_func = bool (*)(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&); - - /// SH(result, body) - using shorthash_func = bool (*)(ShortHash&, const llarp_buffer_t&); } // namespace llarp diff --git a/llarp/dns/srv_data.cpp b/llarp/dns/srv_data.cpp index 7ecae10f6..dfc7bc54b 100644 --- a/llarp/dns/srv_data.cpp +++ b/llarp/dns/srv_data.cpp @@ -1,6 +1,5 @@ #include "srv_data.hpp" -// #include #include #include diff --git a/llarp/handlers/endpoint.cpp b/llarp/handlers/endpoint.cpp index 809a73ed1..c53bc1d37 100644 --- a/llarp/handlers/endpoint.cpp +++ b/llarp/handlers/endpoint.cpp @@ -47,7 +47,6 @@ namespace llarp::handlers void LocalEndpoint::configure() { - // auto _dns_config = _router.config()->dns; auto net_config = _router.config()->network; if (net_config.is_reachable) @@ -68,6 +67,9 @@ namespace llarp::handlers _local_introset.exit_policy = _exit_policy; } + if (not net_config.srv_records.empty()) + _local_introset.SRVs = std::move(net_config.srv_records); + _if_name = *net_config._if_name; _local_range = *net_config._local_ip_range; _local_addr = *net_config._local_addr; diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 692f51af4..0e44a8948 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -34,7 +34,7 @@ namespace llarp struct PathHopConfig; /// A path we made - struct Path final : public std::enable_shared_from_this + struct Path : public std::enable_shared_from_this { std::vector hops; diff --git a/llarp/service/info.hpp b/llarp/service/info.hpp index 196182eb1..9fd3bb70e 100644 --- a/llarp/service/info.hpp +++ b/llarp/service/info.hpp @@ -3,6 +3,7 @@ #include "vanity.hpp" #include +#include #include #include diff --git a/llarp/session/outbound_session.cpp b/llarp/session/outbound_session.cpp index 421ebdc38..17f5e5882 100644 --- a/llarp/session/outbound_session.cpp +++ b/llarp/session/outbound_session.cpp @@ -3,8 +3,8 @@ #include #include #include -#include #include +#include #include @@ -87,7 +87,7 @@ namespace llarp::session path::PathHandler::path_build_succeeded(remote, p); // TODO: add callback here - if (p->obtain_exit(_auth->session_key(), _is_snode_service ? 1 : 0, p->TXID().bt_encode())) + if (p->obtain_exit(_auth->session_key(), _is_snode_service ? 1 : 0, p->TXID().to_string())) log::info(logcat, "Asking {} for exit", _remote_router); else log::warning(logcat, "Failed to send exit request"); @@ -156,7 +156,7 @@ namespace llarp::session void OutboundSession::send_path_close(std::shared_ptr p) { - if (p->close_exit(_auth->session_key(), p->TXID().bt_encode())) + if (p->close_exit(_auth->session_key(), p->TXID().to_string())) log::info(logcat, "Sent path close on path {}", p->to_string()); else log::warning(logcat, "Failed to send path close on path {}", p->to_string()); diff --git a/llarp/session/session.hpp b/llarp/session/session.hpp index 0b6585118..1dadc3091 100644 --- a/llarp/session/session.hpp +++ b/llarp/session/session.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include diff --git a/llarp/util/aligned.hpp b/llarp/util/aligned.hpp index 36a54565b..ca9dd1ecc 100644 --- a/llarp/util/aligned.hpp +++ b/llarp/util/aligned.hpp @@ -1,9 +1,9 @@ #pragma once -#include "bencode.h" #include "formattable.hpp" #include "logging.hpp" +#include #include #include @@ -202,18 +202,6 @@ namespace llarp return _data.cend(); } - // TODO: move to .cpp file to add static logcat def - bool FromBytestring(llarp_buffer_t* buf) - { - if (buf->sz != sz) - { - // log::error(logcat, "bdecode buffer size mismatch {}!={}", buf->sz, sz); - return false; - } - memcpy(data(), buf->base, sz); - return true; - } - bool from_string(std::string_view b) { if (b.size() != sz) @@ -226,24 +214,15 @@ namespace llarp return true; } - bool bt_encode(llarp_buffer_t* buf) const - { - return bencode_write_bytestring(buf, data(), sz); - } - std::string bt_encode() const { - return {reinterpret_cast(data()), sz}; + return oxenc::bt_serialize(_data); } - bool BDecode(llarp_buffer_t* buf) + bool bt_decode(std::string buf) { - llarp_buffer_t strbuf; - if (!bencode_read_string(buf, &strbuf)) - { - return false; - } - return FromBytestring(&strbuf); + oxenc::bt_deserialize(buf, *this); + return true; } std::string_view to_view() const diff --git a/llarp/util/bencode.h b/llarp/util/bencode.h deleted file mode 100644 index e78fe6838..000000000 --- a/llarp/util/bencode.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include "buffer.hpp" -#include "common.hpp" - -#include - -#include -#include - -/** - * bencode.h - * - * helper functions for handling bencoding - * https://en.wikipedia.org/wiki/Bencode for more information on the format - * we utilize llarp_buffer which provides memory management - */ - -bool bencode_read_integer(llarp_buffer_t* buffer, uint64_t* result); - -bool 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); - -bool bencode_write_uint64(llarp_buffer_t* buff, uint64_t i); - -/// Write a dictionary entry with a uint64_t value -bool bencode_write_uint64_entry(llarp_buffer_t* buff, const void* name, size_t sz, uint64_t i); - -bool bencode_start_list(llarp_buffer_t* buff); - -bool bencode_start_dict(llarp_buffer_t* buff); - -bool bencode_end(llarp_buffer_t* buff); - -/// read next member, discard it and advance buffer -bool bencode_discard(llarp_buffer_t* buf); diff --git a/llarp/util/buffer.hpp b/llarp/util/buffer.hpp index 27dfe5192..e3a3f4516 100644 --- a/llarp/util/buffer.hpp +++ b/llarp/util/buffer.hpp @@ -48,72 +48,6 @@ namespace llarp { return {reinterpret_cast(v.data()), v.size()}; } - - struct llarp_buffer - { - private: - std::string _buf; - std::string_view _bview; - size_t _size; - - public: - llarp_buffer() = default; - llarp_buffer(size_t s) : _size{s} - { - _buf.reserve(_size); - _bview = {_buf}; - } - llarp_buffer(std::string& b) : _buf{std::move(b)}, _bview{_buf}, _size{_buf.size()} - {} - llarp_buffer(std::string_view bv) : _buf{bv}, _bview{_buf}, _size{_buf.size()} - {} - - template < - typename CharT, - std::enable_if_t || std::is_constructible_v, int> = - 0> - llarp_buffer(CharT* c) : _buf{c}, _bview{_buf}, _size{_buf.size()} - {} - - std::string_view view() const - { - return _bview; - } - - size_t size() const - { - return _size; - } - - bool is_empty() const - { - return _buf.empty(); - } - - char* data() - { - return _buf.data(); - } - char* data_at(size_t pos) - { - return _buf.data() + pos; - } - - const char* vdata() - { - return _bview.data(); - } - const char* vdata_at(size_t pos) - { - return _bview.data() + pos; - } - - char operator[](size_t pos) - { - return *(data() + pos); - } - }; - } // namespace llarp struct ManagedBuffer;