From 19578fa8fc22e61ecb52bd89e580c907a664a359 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 18 May 2019 16:34:03 +0100 Subject: [PATCH 1/6] Fix ASAN build --- CMakeLists.txt | 53 +++++++++++++++++----------------- llarp/router/router.cpp | 2 -- llarp/util/alloc.hpp | 4 ++- llarp/util/logger_internal.hpp | 22 ++++++++------ 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c633b9c8f..badf03bc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,33 @@ if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW) message( FATAL_ERROR "shadow-framework is Linux only" ) endif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND SHADOW) +if(NOT DEBIAN AND NOT MSVC_VERSION) + set(OPTIMIZE_FLAGS -O3) + set(DEBUG_FLAGS -O0 -g3) +endif() + +if(ASAN) + set(DEBUG_FLAGS ${DEBUG_FLAGS} -fsanitize=address -fno-omit-frame-pointer) + set(OPTIMIZE_FLAGS "-O0") +endif(ASAN) + +if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]") + set(OPTIMIZE_FLAGS "") + add_definitions(-DLOKINET_DEBUG=1) + set(CRYPTO_FLAGS "") + add_compile_options( ${DEBUG_FLAGS} ) + link_libraries( ${DEBUG_FLAGS} ) +endif(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]") + +# Add non-386 target-specific options here +if(NON_PC_TARGET) + add_definitions(-DRPI) + set(WITH_STATIC ON) +endif(NON_PC_TARGET) + +add_compile_options(${OPTIMIZE_FLAGS} ${CRYPTO_FLAGS}) + + set(ABSEIL_DIR vendor/abseil-cpp) add_subdirectory(${ABSEIL_DIR}) include_directories(SYSTEM ${ABSEIL_DIR}) @@ -156,36 +183,10 @@ if(TESTNET) add_definitions(-DTESTNET=1) endif(TESTNET) -if(NOT DEBIAN AND NOT MSVC_VERSION) - set(OPTIMIZE_FLAGS -O3) - set(DEBUG_FLAGS -O0 -g3) -endif() - -if(ASAN) - set(DEBUG_FLAGS ${DEBUG_FLAGS} -fsanitize=address -fno-omit-frame-pointer) - set(OPTIMIZE_FLAGS "-O0") -endif(ASAN) - if(SHADOW) include(cmake/shadow.cmake) endif(SHADOW) -if(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]") - set(OPTIMIZE_FLAGS "") - add_definitions(-DLOKINET_DEBUG=1) - set(CRYPTO_FLAGS "") - add_compile_options( ${DEBUG_FLAGS} ) - link_libraries( ${DEBUG_FLAGS} ) -endif(CMAKE_BUILD_TYPE MATCHES "[Dd][Ee][Bb][Uu][Gg]") - -# Add non-386 target-specific options here -if(NON_PC_TARGET) - add_definitions(-DRPI) - set(WITH_STATIC ON) -endif(NON_PC_TARGET) - -add_compile_options(${OPTIMIZE_FLAGS} ${CRYPTO_FLAGS}) - if(NOT GIT_VERSION) exec_program("git" ${CMAKE_CURRENT_SOURCE_DIR} ARGS "rev-parse --short HEAD" OUTPUT_VARIABLE GIT_VERSION_UNSTRIP) string(STRIP "${GIT_VERSION_UNSTRIP}" GIT_VERSION) diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index bb8219381..6991a2690 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -325,8 +325,6 @@ namespace llarp } } - constexpr size_t MaxPendingSendQueueSize = 8; - bool Router::SendToOrQueue(const RouterID &remote, const ILinkMessage *msg) { diff --git a/llarp/util/alloc.hpp b/llarp/util/alloc.hpp index ee6797a53..45e1a795a 100644 --- a/llarp/util/alloc.hpp +++ b/llarp/util/alloc.hpp @@ -3,6 +3,8 @@ #include #include +#include + namespace llarp { namespace util @@ -54,7 +56,7 @@ namespace llarp } bool - HasRoomFor(size_t numItems) + HasRoomFor(ABSL_ATTRIBUTE_UNUSED size_t numItems) { return true; /* return mem->hasRoomFor(numItems); */ diff --git a/llarp/util/logger_internal.hpp b/llarp/util/logger_internal.hpp index 8e91d367d..69a0c5487 100644 --- a/llarp/util/logger_internal.hpp +++ b/llarp/util/logger_internal.hpp @@ -46,22 +46,26 @@ namespace llarp { const char* format; - log_timestamp(const char* fmt = "%c %Z") : format(fmt) + log_timestamp() : format("%c %Z") { } - friend std::ostream& - operator<<(std::ostream& out, const log_timestamp& ts) + explicit log_timestamp(const char* fmt) : format(fmt) { + } + }; + + inline std::ostream& + operator<<(std::ostream& out, const log_timestamp& ts) + { #if defined(ANDROID) || defined(RPI) - (void)ts; - return out << time_now_ms(); + (void)ts; + return out << time_now_ms(); #else - return out << absl::FormatTime(ts.format, absl::Now(), - absl::LocalTimeZone()); + absl::TimeZone tz = absl::LocalTimeZone(); + return out << absl::FormatTime(ts.format, absl::Now(), tz); #endif - } - }; + } } // namespace llarp From a83be769e26d450338ba6f0ec6e8518c13390953 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 18 May 2019 18:34:07 +0100 Subject: [PATCH 2/6] More explicit error when keyfile is not a valid file --- llarp/service/context.cpp | 73 ++++++++-------- llarp/service/endpoint.cpp | 9 +- llarp/service/identity.cpp | 11 ++- llarp/service/identity.hpp | 15 ++-- test/service/test_llarp_service_identity.cpp | 92 ++++++++++++++++++-- test/test_util.hpp | 2 +- 6 files changed, 143 insertions(+), 59 deletions(-) diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index f0c238165..ec14a5449 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -10,6 +10,41 @@ namespace llarp { namespace service { + namespace + { + using EndpointConstructor = std::function< service::Endpoint_ptr( + const std::string &, AbstractRouter *, service::Context *) >; + using EndpointConstructors = std::map< std::string, EndpointConstructor >; + + static EndpointConstructors endpointConstructors = { + {"tun", + [](const std::string &nick, AbstractRouter *r, + service::Context *c) -> service::Endpoint_ptr { + return std::make_shared< handlers::TunEndpoint >(nick, r, c); + }}, + {"android-tun", + [](const std::string &, AbstractRouter *, + service::Context *) -> service::Endpoint_ptr { + return nullptr; + /// SOOOOOOON (tm) + // return std::make_shared(nick, + // r, c); + }}, + {"ios-tun", + [](const std::string &, AbstractRouter *, + service::Context *) -> service::Endpoint_ptr { + return nullptr; + /// SOOOOOOON (tm) + // return std::make_shared(nick, r, + // c); + }}, + {"null", + [](const std::string &nick, AbstractRouter *r, + service::Context *c) -> service::Endpoint_ptr { + return std::make_shared< handlers::NullEndpoint >(nick, r, c); + }}}; + + } // namespace Context::Context(AbstractRouter *r) : m_Router(r) { } @@ -190,7 +225,7 @@ namespace llarp } // extract type std::string endpointType = "tun"; - std::string keyfile = ""; + std::string keyfile; for(const auto &option : conf.second) { if(option.first == "type") @@ -201,41 +236,9 @@ namespace llarp service::Endpoint_ptr service; - static std::map< - std::string, - std::function< service::Endpoint_ptr( - const std::string &, AbstractRouter *, service::Context *) > > - endpointConstructors = { - {"tun", - [](const std::string &nick, AbstractRouter *r, - service::Context *c) -> service::Endpoint_ptr { - return std::make_shared< handlers::TunEndpoint >(nick, r, c); - }}, - {"android-tun", - [](const std::string &, AbstractRouter *, - service::Context *) -> service::Endpoint_ptr { - return nullptr; - /// SOOOOOOON (tm) - // return std::make_shared(nick, - // r, c); - }}, - {"ios-tun", - [](const std::string &, AbstractRouter *, - service::Context *) -> service::Endpoint_ptr { - return nullptr; - /// SOOOOOOON (tm) - // return std::make_shared(nick, r, - // c); - }}, - {"null", - [](const std::string &nick, AbstractRouter *r, - service::Context *c) -> service::Endpoint_ptr { - return std::make_shared< handlers::NullEndpoint >(nick, r, c); - }}}; - { // detect type - auto itr = endpointConstructors.find(endpointType); + const auto itr = endpointConstructors.find(endpointType); if(itr == endpointConstructors.end()) { LogError("no such endpoint type: ", endpointType); @@ -248,7 +251,7 @@ namespace llarp { // if ephemeral, then we need to regen key // if privkey file, then set it and load it - if(keyfile != "") + if(!keyfile.empty()) { service->SetOption("keyfile", keyfile); // load keyfile, so we have the correct name for logging diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index b9d7ba32b..722cc28ca 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -495,11 +495,11 @@ namespace llarp Endpoint::LoadKeyFile() { auto crypto = m_Router->crypto(); - if(m_Keyfile.size()) + if(!m_Keyfile.empty()) { if(!m_Identity.EnsureKeys(m_Keyfile, crypto)) { - LogWarn("Can't ensure keyfile [", m_Keyfile, "]"); + LogError("Can't ensure keyfile [", m_Keyfile, "]"); return false; } } @@ -514,7 +514,6 @@ namespace llarp Endpoint::Start() { // how can I tell if a m_Identity isn't loaded? - // this->LoadKeyFile(); if(!m_DataHandler) { m_DataHandler = this; @@ -1211,8 +1210,8 @@ namespace llarp const auto dlt = intro.expiresAt - now; return should || ( // try spacing tunnel builds out evenly in time - (dlt <= (path::default_lifetime / 4)) && - (NumInStatus(path::ePathBuilding) < m_NumPaths)); + (dlt <= (path::default_lifetime / 4)) + && (NumInStatus(path::ePathBuilding) < m_NumPaths)); } Logic* diff --git a/llarp/service/identity.cpp b/llarp/service/identity.cpp index 878134ff3..b29df81a7 100644 --- a/llarp/service/identity.cpp +++ b/llarp/service/identity.cpp @@ -6,10 +6,6 @@ namespace llarp { namespace service { - Identity::~Identity() - { - } - bool Identity::BEncode(llarp_buffer_t* buf) const { @@ -107,6 +103,13 @@ namespace llarp return false; f.write((char*)buf.cur, buf.sz); } + + if(!fs::is_regular_file(fname)) + { + LogError("keyfile ", fname, " is not a regular file"); + return false; + } + // read file std::ifstream inf(fname, std::ios::binary); inf.seekg(0, std::ios::end); diff --git a/llarp/service/identity.hpp b/llarp/service/identity.hpp index 481f2c6e2..7d63f9c23 100644 --- a/llarp/service/identity.hpp +++ b/llarp/service/identity.hpp @@ -7,6 +7,8 @@ #include #include +#include + namespace llarp { struct Crypto; @@ -25,16 +27,10 @@ namespace llarp // public service info ServiceInfo pub; - ~Identity(); - // regenerate secret keys void RegenerateKeys(Crypto* c); - // load from file - bool - LoadFromFile(const std::string& fpath); - bool BEncode(llarp_buffer_t* buf) const override; @@ -54,6 +50,13 @@ namespace llarp bool Sign(Crypto*, Signature& sig, const llarp_buffer_t& buf) const; }; + + inline bool + operator==(const Identity& lhs, const Identity& rhs) + { + return std::tie(lhs.enckey, lhs.signkey, lhs.pq, lhs.version, lhs.vanity) + == std::tie(rhs.enckey, rhs.signkey, rhs.pq, rhs.version, rhs.vanity); + } } // namespace service } // namespace llarp diff --git a/test/service/test_llarp_service_identity.cpp b/test/service/test_llarp_service_identity.cpp index ff749d84d..e66c98da9 100644 --- a/test/service/test_llarp_service_identity.cpp +++ b/test/service/test_llarp_service_identity.cpp @@ -6,12 +6,19 @@ #include #include +#include +#include + #include +#include + +using namespace llarp; +using namespace testing; struct HiddenServiceTest : public ::testing::Test { - llarp::sodium::CryptoLibSodium crypto; - llarp::service::Identity ident; + sodium::CryptoLibSodium crypto; + service::Identity ident; HiddenServiceTest() { @@ -34,15 +41,15 @@ struct HiddenServiceTest : public ::testing::Test TEST_F(HiddenServiceTest, TestGenerateIntroSet) { - llarp::service::Address addr; + service::Address addr; ASSERT_TRUE(ident.pub.CalculateAddress(addr.as_array())); - llarp::service::IntroSet I; - auto now = llarp::time_now_ms(); + service::IntroSet I; + auto now = time_now_ms(); I.T = now; while(I.I.size() < 10) { - llarp::service::Introduction intro; - intro.expiresAt = now + (llarp::path::default_lifetime / 2); + service::Introduction intro; + intro.expiresAt = now + (path::default_lifetime / 2); intro.router.Randomize(); intro.pathID.Randomize(); I.I.emplace_back(std::move(intro)); @@ -54,7 +61,76 @@ TEST_F(HiddenServiceTest, TestGenerateIntroSet) TEST_F(HiddenServiceTest, TestAddressToFromString) { auto str = ident.pub.Addr().ToString(); - llarp::service::Address addr; + service::Address addr; ASSERT_TRUE(addr.FromString(str)); ASSERT_TRUE(addr == ident.pub.Addr()); } + +struct ServiceIdentityTest : public ::testing::Test +{ + test::MockCrypto crypto; +}; + +template < typename Arg > +std::function< void(Arg&) > +FillArg(byte_t val) +{ + return [=](Arg& arg) { arg.Fill(val); }; +} + +TEST_F(ServiceIdentityTest, EnsureKeys) +{ + fs::path p = test::randFilename(); + ASSERT_FALSE(fs::exists(fs::status(p))); + + test::FileGuard guard(p); + + EXPECT_CALL(crypto, encryption_keygen(_)) + .WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x01))); + + EXPECT_CALL(crypto, identity_keygen(_)) + .WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x02))); + + EXPECT_CALL(crypto, pqe_keygen(_)) + .WillOnce(WithArg< 0 >(FillArg< PQKeyPair >(0x03))); + + service::Identity identity; + ASSERT_TRUE(identity.EnsureKeys(p, &crypto)); + ASSERT_TRUE(fs::exists(fs::status(p))); + + // Verify what is on disk is what is what was generated + service::Identity other; + // No need to set more mocks, as we shouldn't need to re-keygen + ASSERT_TRUE(other.EnsureKeys(p, &crypto)); + ASSERT_EQ(identity, other); +} + +TEST_F(ServiceIdentityTest, EnsureKeysDir) +{ + fs::path p = test::randFilename(); + ASSERT_FALSE(fs::exists(fs::status(p))); + + test::FileGuard guard(p); + std::error_code code; + ASSERT_TRUE(fs::create_directory(p, code)) << code; + + service::Identity identity; + ASSERT_FALSE(identity.EnsureKeys(p, &crypto)); +} + +TEST_F(ServiceIdentityTest, EnsureKeysBrokenFile) +{ + fs::path p = test::randFilename(); + ASSERT_FALSE(fs::exists(fs::status(p))); + + test::FileGuard guard(p); + std::error_code code; + + std::fstream file; + file.open(p.string(), std::ios::out); + ASSERT_TRUE(file.is_open()) << p; + file.close(); + + service::Identity identity; + ASSERT_FALSE(identity.EnsureKeys(p, &crypto)); +} diff --git a/test/test_util.hpp b/test/test_util.hpp index aeb0e7494..63f25699c 100644 --- a/test/test_util.hpp +++ b/test/test_util.hpp @@ -35,7 +35,7 @@ namespace llarp { if(fs::exists(fs::status(p))) { - fs::remove(p); + fs::remove_all(p); } } }; From 636bb2a17d4527a7bb7a533955115eeabea424a2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 18 May 2019 19:46:49 +0100 Subject: [PATCH 3/6] Convert router diskworker to use a modern ThreadPool --- llarp/nodedb.cpp | 69 ++++++++++----------------------- llarp/nodedb.hpp | 21 +++++----- llarp/router/abstractrouter.hpp | 7 +++- llarp/router/router.cpp | 41 ++++++++------------ llarp/router/router.hpp | 9 +++-- llarp/util/file_logger.cpp | 33 +++++++--------- llarp/util/file_logger.hpp | 25 +++--------- 7 files changed, 80 insertions(+), 125 deletions(-) diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index c32523a88..b382a62b8 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -48,31 +49,18 @@ llarp_nodedb::Get(const llarp::RouterID &pk, llarp::RouterContact &result) return true; } -// kill rcs from disk async -struct AsyncKillRCJobs +void +KillRCJobs(const std::set< std::string > &files) { - std::set< std::string > files; - - static void - Work(void *u) - { - static_cast< AsyncKillRCJobs * >(u)->Kill(); - } - - void - Kill() - { - for(const auto &file : files) - fs::remove(file); - delete this; - } -}; + for(const auto &file : files) + fs::remove(file); +} void llarp_nodedb::RemoveIf( std::function< bool(const llarp::RouterContact &rc) > filter) { - AsyncKillRCJobs *job = new AsyncKillRCJobs(); + std::set< std::string > files; { llarp::util::Lock l(&access); auto itr = entries.begin(); @@ -80,14 +68,15 @@ llarp_nodedb::RemoveIf( { if(filter(itr->second)) { - job->files.insert(getRCFilePath(itr->second.pubkey)); + files.insert(getRCFilePath(itr->second.pubkey)); itr = entries.erase(itr); } else ++itr; } } - llarp_threadpool_queue_job(disk, {job, &AsyncKillRCJobs::Work}); + + disk->addJob(std::bind(&KillRCJobs, files)); } bool @@ -118,38 +107,24 @@ llarp_nodedb::getRCFilePath(const llarp::RouterID &pubkey) const return filepath.string(); } -struct async_insert_rc -{ - llarp_nodedb *nodedb; - llarp::RouterContact rc; - llarp::Logic *logic; - std::function< void(void) > completedHook; - async_insert_rc(llarp_nodedb *n, const llarp::RouterContact &r) - : nodedb(n), rc(r) - { - } -}; - static void -handle_async_insert_rc(void *u) +handle_async_insert_rc(llarp_nodedb *nodedb, const llarp::RouterContact &rc, + llarp::Logic *logic, + const std::function< void(void) > &completedHook) { - async_insert_rc *job = static_cast< async_insert_rc * >(u); - job->nodedb->Insert(job->rc); - if(job->logic && job->completedHook) + nodedb->Insert(rc); + if(logic && completedHook) { - job->logic->queue_func(job->completedHook); + logic->queue_func(completedHook); } - delete job; } void llarp_nodedb::InsertAsync(llarp::RouterContact rc, llarp::Logic *logic, std::function< void(void) > completionHandler) { - async_insert_rc *ctx = new async_insert_rc(this, rc); - ctx->completedHook = completionHandler; - ctx->logic = logic; - llarp_threadpool_queue_job(disk, {ctx, &handle_async_insert_rc}); + disk->addJob( + std::bind(&handle_async_insert_rc, this, rc, logic, completionHandler)); } /// insert and write to disk @@ -304,10 +279,8 @@ logic_threadworker_callback(void *user) // write it to disk void -disk_threadworker_setRC(void *user) +disk_threadworker_setRC(llarp_async_verify_rc *verify_request) { - llarp_async_verify_rc *verify_request = - static_cast< llarp_async_verify_rc * >(user); verify_request->valid = verify_request->nodedb->Insert(verify_request->rc); if(verify_request->logic) verify_request->logic->queue_job( @@ -327,8 +300,8 @@ crypto_threadworker_verifyrc(void *user) if(verify_request->valid && rc.IsPublicRouter()) { llarp::LogDebug("RC is valid, saving to disk"); - llarp_threadpool_queue_job(verify_request->diskworker, - {verify_request, &disk_threadworker_setRC}); + verify_request->diskworker->addJob( + std::bind(&disk_threadworker_setRC, verify_request)); } else { diff --git a/llarp/nodedb.hpp b/llarp/nodedb.hpp index f6aba9597..2850b0e75 100644 --- a/llarp/nodedb.hpp +++ b/llarp/nodedb.hpp @@ -28,6 +28,11 @@ namespace llarp { struct Crypto; class Logic; + + namespace thread + { + class ThreadPool; + } } // namespace llarp struct llarp_nodedb_iter @@ -40,7 +45,7 @@ struct llarp_nodedb_iter struct llarp_nodedb { - llarp_nodedb(llarp::Crypto *c, llarp_threadpool *diskworker) + llarp_nodedb(llarp::Crypto *c, llarp::thread::ThreadPool *diskworker) : crypto(c), disk(diskworker) { } @@ -51,7 +56,7 @@ struct llarp_nodedb } llarp::Crypto *crypto; - llarp_threadpool *disk; + llarp::thread::ThreadPool *disk; mutable llarp::util::Mutex access; // protects entries std::unordered_map< llarp::RouterID, llarp::RouterContact, llarp::RouterID::Hash > @@ -146,13 +151,11 @@ struct llarp_async_verify_rc /// async_verify_context void *user; /// nodedb storage - struct llarp_nodedb *nodedb; + llarp_nodedb *nodedb; // llarp::Logic for queue_job llarp::Logic *logic; // includes a llarp_threadpool - // struct llarp::Crypto *crypto; // probably don't need this because we have - // it in the nodedb - struct llarp_threadpool *cryptoworker; - struct llarp_threadpool *diskworker; + llarp_threadpool *cryptoworker; + llarp::thread::ThreadPool *diskworker; /// router contact llarp::RouterContact rc; @@ -181,11 +184,11 @@ struct llarp_async_load_rc /// async_verify_context void *user; /// nodedb storage - struct llarp_nodedb *nodedb; + llarp_nodedb *nodedb; /// llarp::Logic for calling hook llarp::Logic *logic; /// disk worker threadpool - struct llarp_threadpool *diskworker; + llarp::thread::ThreadPool *diskworker; /// target pubkey llarp::PubKey pubkey; /// router contact result diff --git a/llarp/router/abstractrouter.hpp b/llarp/router/abstractrouter.hpp index c7d94d0a7..59fcd8b22 100644 --- a/llarp/router/abstractrouter.hpp +++ b/llarp/router/abstractrouter.hpp @@ -46,6 +46,11 @@ namespace llarp struct Context; } + namespace thread + { + class ThreadPool; + } + struct AbstractRouter { virtual ~AbstractRouter() = 0; @@ -96,7 +101,7 @@ namespace llarp virtual llarp_threadpool * threadpool() = 0; - virtual llarp_threadpool * + virtual thread::ThreadPool * diskworker() = 0; virtual service::Context & diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index bb8219381..d6c67f73f 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -116,7 +116,7 @@ struct TryConnectJob }; static void -on_try_connecting(std::shared_ptr j) +on_try_connecting(std::shared_ptr< TryConnectJob > j) { if(j->Attempt()) j->router->pendingEstablishJobs.erase(j->rc.pubkey); @@ -191,8 +191,8 @@ namespace llarp { if(!link->IsCompatable(remote)) continue; - std::shared_ptr< TryConnectJob > job = std::make_shared< TryConnectJob >( - remote, link, numretries, this); + std::shared_ptr< TryConnectJob > job = + std::make_shared< TryConnectJob >(remote, link, numretries, this); auto itr = pendingEstablishJobs.emplace(remote.pubkey, job); if(itr.second) { @@ -223,6 +223,7 @@ namespace llarp , _crypto(std::make_unique< sodium::CryptoLibSodium >()) , paths(this) , _exitContext(this) + , disk(1, 1000) , _dht(llarp_dht_context_new(this)) , inbound_link_msg_parser(this) , _hiddenServiceContext(this) @@ -231,11 +232,6 @@ namespace llarp this->ip4addr.sin_family = AF_INET; this->ip4addr.sin_port = htons(1090); -#ifdef TESTNET - disk = tp; -#else - disk = llarp_init_threadpool(1, "llarp-diskio"); -#endif _stopping.store(false); _running.store(false); } @@ -499,12 +495,11 @@ namespace llarp } /// called in disk worker thread - static void - HandleSaveRC(void *u) + void + Router::HandleSaveRC() const { - Router *self = static_cast< Router * >(u); - std::string fname = self->our_rc_file.string(); - self->_rc.Write(fname.c_str()); + std::string fname = our_rc_file.string(); + _rc.Write(fname.c_str()); } bool @@ -517,7 +512,7 @@ namespace llarp LogError("RC is invalid, not saving"); return false; } - llarp_threadpool_queue_job(diskworker(), {this, &HandleSaveRC}); + diskworker()->addJob(std::bind(&Router::HandleSaveRC, this)); return true; } @@ -1335,14 +1330,10 @@ namespace llarp // save profiles async if(routerProfiling().ShouldSave(now)) { - llarp_threadpool_queue_job( - diskworker(), - {this, [](void *u) { - Router *self = static_cast< Router * >(u); - self->routerProfiling().Save(self->routerProfilesFile.c_str()); - }}); + diskworker()->addJob( + [&]() { routerProfiling().Save(routerProfilesFile.c_str()); }); } - } + } // namespace llarp bool Router::Sign(Signature &sig, const llarp_buffer_t &buf) const @@ -1506,7 +1497,7 @@ namespace llarp job->nodedb = _nodedb; job->logic = _logic; job->cryptoworker = tp; - job->diskworker = disk; + job->diskworker = &disk; if(rc.IsPublicRouter()) job->hook = &Router::on_verify_server_rc; else @@ -1569,7 +1560,7 @@ namespace llarp } llarp_threadpool_start(tp); - llarp_threadpool_start(disk); + disk.start(); for(const auto &rc : bootstrapRCList) { @@ -1961,8 +1952,8 @@ namespace llarp if(outboundLinks.size() > 0) return true; - static std::list< std::function< LinkLayer_ptr(Router *) > > - linkFactories = {utp::NewServerFromRouter, iwp::NewServerFromRouter}; + static std::list< std::function< LinkLayer_ptr(Router *) > > linkFactories = + {utp::NewServerFromRouter, iwp::NewServerFromRouter}; for(const auto &factory : linkFactories) { diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 417e6792f..96bad2cf5 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -170,10 +170,10 @@ namespace llarp return tp; } - llarp_threadpool * + thread::ThreadPool * diskworker() override { - return disk; + return &disk; } // our ipv4 public setting @@ -189,7 +189,7 @@ namespace llarp exit::Context _exitContext; SecretKey _identity; SecretKey _encryption; - llarp_threadpool *disk; + thread::ThreadPool disk; llarp_dht_context *_dht = nullptr; llarp_nodedb *_nodedb; llarp_time_t _startedAt; @@ -385,6 +385,9 @@ namespace llarp bool ConnectionToRouterAllowed(const RouterID &router) const override; + void + HandleSaveRC() const; + bool SaveRC(); diff --git a/llarp/util/file_logger.cpp b/llarp/util/file_logger.cpp index d73bd8bb4..3561685b3 100644 --- a/llarp/util/file_logger.cpp +++ b/llarp/util/file_logger.cpp @@ -3,7 +3,17 @@ namespace llarp { - FileLogStream::FileLogStream(llarp_threadpool *disk, FILE *f, + namespace + { + static void + Flush(const std::deque< std::string > &lines, FILE *const f) + { + for(const auto &line : lines) + fprintf(f, "%s\n", line.c_str()); + fflush(f); + } + } // namespace + FileLogStream::FileLogStream(thread::ThreadPool *disk, FILE *f, llarp_time_t flushInterval) : m_Disk(disk), m_File(f), m_FlushInterval(flushInterval) { @@ -66,24 +76,7 @@ namespace llarp void FileLogStream::FlushLinesToDisk(llarp_time_t now) { - FlushEvent *ev = new FlushEvent(std::move(m_Lines), m_File); - llarp_threadpool_queue_job(m_Disk, {ev, &FlushEvent::HandleFlush}); + m_Disk->addJob(std::bind(&Flush, std::move(m_Lines), m_File)); m_LastFlush = now; } - - void - FileLogStream::FlushEvent::HandleFlush(void *user) - { - static_cast< FileLogStream::FlushEvent * >(user)->Flush(); - } - - void - FileLogStream::FlushEvent::Flush() - { - for(const auto &line : lines) - fprintf(f, "%s\n", line.c_str()); - fflush(f); - delete this; - } - -} // namespace llarp \ No newline at end of file +} // namespace llarp diff --git a/llarp/util/file_logger.hpp b/llarp/util/file_logger.hpp index 743193e17..0233b260b 100644 --- a/llarp/util/file_logger.hpp +++ b/llarp/util/file_logger.hpp @@ -2,15 +2,18 @@ #define LLARP_UTIL_FILE_LOGGER_HPP #include -#include +#include #include +#include + namespace llarp { /// flushable file based log stream struct FileLogStream : public ILogStream { - FileLogStream(llarp_threadpool* disk, FILE* f, llarp_time_t flushInterval); + FileLogStream(thread::ThreadPool* disk, FILE* f, + llarp_time_t flushInterval); ~FileLogStream(); @@ -30,29 +33,13 @@ namespace llarp } private: - struct FlushEvent - { - FlushEvent(std::deque< std::string > l, FILE* file) - : lines(std::move(l)), f(file) - { - } - - const std::deque< std::string > lines; - FILE* const f; - - void - Flush(); - static void - HandleFlush(void*); - }; - bool ShouldFlush(llarp_time_t now) const; void FlushLinesToDisk(llarp_time_t now); - llarp_threadpool* m_Disk; + thread::ThreadPool* m_Disk; FILE* m_File; const llarp_time_t m_FlushInterval; llarp_time_t m_LastFlush = 0; From b11bd016371e1ceadb84b45897209aa0980dbc90 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 18 May 2019 20:05:48 +0100 Subject: [PATCH 4/6] Drain diskworker pool on close --- llarp/router/router.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index d6c67f73f..4e70e46f5 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -529,6 +529,8 @@ namespace llarp llarp_ev_loop_stop(_netloop.get()); inboundLinks.clear(); outboundLinks.clear(); + disk.stop(); + disk.shutdown(); } void From 06882338bb1a23493d0d016b7b069d05aadaa91b Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 18 May 2019 20:28:14 +0100 Subject: [PATCH 5/6] Change ExecShellHookBackend to use a modern thread pool --- llarp/hook/shell.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/llarp/hook/shell.cpp b/llarp/hook/shell.cpp index 9fa4ce587..c232f8726 100644 --- a/llarp/hook/shell.cpp +++ b/llarp/hook/shell.cpp @@ -2,7 +2,7 @@ #if defined(_WIN32) /** put win32 stuff here */ #else -#include +#include #include #include #include @@ -29,13 +29,12 @@ namespace llarp : public IBackend, public std::enable_shared_from_this< ExecShellHookBackend > { - llarp_threadpool *m_ThreadPool; + thread::ThreadPool m_ThreadPool; std::vector< std::string > _args; std::vector< char * > args; - ExecShellHookBackend(std::string script) - : m_ThreadPool(llarp_init_threadpool(1, script.c_str())) + ExecShellHookBackend(std::string script) : m_ThreadPool(1, 1000) { do { @@ -55,21 +54,20 @@ namespace llarp ~ExecShellHookBackend() { - llarp_threadpool_stop(m_ThreadPool); - llarp_free_threadpool(&m_ThreadPool); + m_ThreadPool.shutdown(); } bool Start() override { - llarp_threadpool_start(m_ThreadPool); + m_ThreadPool.start(); return true; } bool Stop() override { - llarp_threadpool_stop(m_ThreadPool); + m_ThreadPool.stop(); return true; } @@ -123,8 +121,8 @@ namespace llarp return _m_env.data(); } - static void - Exec(std::shared_ptr< ExecShellHookJob > self) + void + Exec() { std::thread t([&]() { int result = 0; @@ -134,8 +132,7 @@ namespace llarp if(child) ::waitpid(child, &result, 0); else - ::execve(self->m_Parent->Exe(), self->m_Parent->Args(), - self->Env()); + ::execve(m_Parent->Exe(), m_Parent->Args(), Env()); }); t.join(); } @@ -147,7 +144,8 @@ namespace llarp { auto job = std::make_shared< ExecShellHookJob >(shared_from_this(), std::move(params)); - m_ThreadPool->QueueFunc([=]() { ExecShellHookJob::Exec(job); }); + + m_ThreadPool.addJob(std::bind(&ExecShellHookJob::Exec, job)); } Backend_ptr From dd8a93a400d5a82114bde793a38a6dce6478ea9f Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 19 May 2019 23:11:07 +0100 Subject: [PATCH 6/6] Apply rule of zero to code base --- libabyss/include/abyss/server.hpp | 2 +- libabyss/src/server.cpp | 4 +- libutp/utp_internal.cpp | 3 +- llarp/crypto/encrypted.hpp | 4 -- llarp/crypto/encrypted_frame.hpp | 10 --- llarp/crypto/types.hpp | 54 ++++++-------- llarp/exit/close_exit.cpp | 10 --- llarp/exit/grant_exit.cpp | 11 --- llarp/exit/reject_exit.cpp | 13 ---- llarp/exit/transfer_traffic.cpp | 9 --- llarp/exit/update_exit.cpp | 12 ---- llarp/messages/exit.hpp | 44 +----------- llarp/messages/link_intro.cpp | 15 ---- llarp/messages/link_intro.hpp | 5 -- llarp/messages/transfer_traffic.hpp | 3 - llarp/metrics/metrictank_publisher.cpp | 1 + llarp/net/net_addr.cpp | 99 +++++++++++++------------- llarp/path/path.hpp | 3 - llarp/path/pathbuilder.cpp | 2 +- llarp/path/transit_hop.cpp | 17 ----- llarp/service/intro.hpp | 9 --- llarp/service/tag.hpp | 7 -- llarp/util/queue_manager.cpp | 2 +- 23 files changed, 78 insertions(+), 261 deletions(-) diff --git a/libabyss/include/abyss/server.hpp b/libabyss/include/abyss/server.hpp index af83ee829..4a5c758eb 100644 --- a/libabyss/include/abyss/server.hpp +++ b/libabyss/include/abyss/server.hpp @@ -42,7 +42,7 @@ namespace abyss struct BaseReqHandler { BaseReqHandler(llarp_time_t req_timeout); - ~BaseReqHandler(); + virtual ~BaseReqHandler(); bool ServeAsync(llarp_ev_loop_ptr loop, llarp::Logic* logic, diff --git a/libabyss/src/server.cpp b/libabyss/src/server.cpp index 2affbf7b3..cd190130d 100644 --- a/libabyss/src/server.cpp +++ b/libabyss/src/server.cpp @@ -54,9 +54,7 @@ namespace abyss m_State = eReadHTTPMethodLine; } - ~ConnImpl() - { - } + ~ConnImpl() = default; bool FeedLine(std::string& line) diff --git a/libutp/utp_internal.cpp b/libutp/utp_internal.cpp index be834c686..9d6dee2aa 100644 --- a/libutp/utp_internal.cpp +++ b/libutp/utp_internal.cpp @@ -3167,7 +3167,8 @@ utp_connect(utp_socket *conn, const struct sockaddr *to, socklen_t tolen) "UTP_Connect conn_seed:%u packet_size:%u (B) " "target_delay:%u (ms) delay_history:%u " "delay_base_history:%u (minutes)", - conn->conn_seed, PACKET_SIZE, conn->target_delay / 1000, + conn->conn_seed, PACKET_SIZE, + static_cast< unsigned int >(conn->target_delay / 1000), CUR_DELAY_SIZE, DELAY_BASE_HISTORY); // Setup initial timeout timer. diff --git a/llarp/crypto/encrypted.hpp b/llarp/crypto/encrypted.hpp index f10b0f92d..285c2eb96 100644 --- a/llarp/crypto/encrypted.hpp +++ b/llarp/crypto/encrypted.hpp @@ -59,10 +59,6 @@ namespace llarp { } - ~Encrypted() - { - } - bool BEncode(llarp_buffer_t* buf) const { diff --git a/llarp/crypto/encrypted_frame.hpp b/llarp/crypto/encrypted_frame.hpp index 763a29795..ffd9a36a0 100644 --- a/llarp/crypto/encrypted_frame.hpp +++ b/llarp/crypto/encrypted_frame.hpp @@ -27,16 +27,6 @@ namespace llarp : Encrypted< EncryptedFrameSize >(std::min(sz, EncryptedFrameBodySize) + EncryptedFrameOverheadSize) { - UpdateBuffer(); - } - - EncryptedFrame& - operator=(const EncryptedFrame& other) - { - _sz = other._sz; - memcpy(data(), other.data(), size()); - UpdateBuffer(); - return *this; } void diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index d9045bdbe..4ce73af92 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -39,12 +39,6 @@ namespace llarp bool FromString(const std::string &str); - friend std::ostream & - operator<<(std::ostream &out, const PubKey &k) - { - return out << k.ToString(); - } - operator RouterID() const { return RouterID(as_array()); @@ -58,6 +52,12 @@ namespace llarp } }; + inline std::ostream & + operator<<(std::ostream &out, const PubKey &k) + { + return out << k.ToString(); + } + inline bool operator==(const PubKey &lhs, const PubKey &rhs) { @@ -82,22 +82,10 @@ namespace llarp { } - explicit SecretKey(const SecretKey &k) : AlignedBuffer< SECKEYSIZE >(k) - { - } - explicit SecretKey(const byte_t *ptr) : AlignedBuffer< SECKEYSIZE >(ptr) { } - friend std::ostream & - operator<<(std::ostream &out, const SecretKey &) - { - // return out << k.ToHex(); - // make sure we never print out secret keys - return out << "[secretkey]"; - } - std::ostream & print(std::ostream &stream, int level, int spaces) const { @@ -117,15 +105,16 @@ namespace llarp bool SaveToFile(const char *fname) const; - - SecretKey & - operator=(const byte_t *ptr) - { - std::copy(ptr, ptr + SIZE, begin()); - return *this; - } }; + inline std::ostream & + operator<<(std::ostream &out, const SecretKey &) + { + // return out << k.ToHex(); + // make sure we never print out secret keys + return out << "[secretkey]"; + } + /// IdentitySecret is a secret key from a service node secret seed struct IdentitySecret final : public AlignedBuffer< 32 > { @@ -133,14 +122,6 @@ namespace llarp { } - friend std::ostream & - operator<<(std::ostream &out, const IdentitySecret &) - { - // return out << k.ToHex(); - // make sure we never print out secret keys - return out << "[secretkey]"; - } - /// no copy constructor explicit IdentitySecret(const IdentitySecret &) = delete; // no byte data constructor @@ -151,6 +132,13 @@ namespace llarp LoadFromFile(const char *fname); }; + inline std::ostream & + operator<<(std::ostream &out, const IdentitySecret &) + { + // make sure we never print out secret keys + return out << "[IdentitySecret]"; + } + using ShortHash = AlignedBuffer< SHORTHASHSIZE >; using LongHash = AlignedBuffer< HASHSIZE >; diff --git a/llarp/exit/close_exit.cpp b/llarp/exit/close_exit.cpp index 4f8d5809c..a74a9188d 100644 --- a/llarp/exit/close_exit.cpp +++ b/llarp/exit/close_exit.cpp @@ -66,16 +66,6 @@ namespace llarp return c->sign(Z, sk, buf); } - CloseExitMessage& - CloseExitMessage::operator=(const CloseExitMessage& other) - { - S = other.S; - version = other.version; - Y = other.Y; - Z = other.Z; - return *this; - } - bool CloseExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { diff --git a/llarp/exit/grant_exit.cpp b/llarp/exit/grant_exit.cpp index 50c533092..9471ea2dc 100644 --- a/llarp/exit/grant_exit.cpp +++ b/llarp/exit/grant_exit.cpp @@ -71,17 +71,6 @@ namespace llarp return c->sign(Z, sk, buf); } - GrantExitMessage& - GrantExitMessage::operator=(const GrantExitMessage& other) - { - S = other.S; - T = other.T; - version = other.version; - Y = other.Y; - Z = other.Z; - return *this; - } - bool GrantExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { diff --git a/llarp/exit/reject_exit.cpp b/llarp/exit/reject_exit.cpp index 812feeba8..4f468fb05 100644 --- a/llarp/exit/reject_exit.cpp +++ b/llarp/exit/reject_exit.cpp @@ -52,19 +52,6 @@ namespace llarp return read; } - RejectExitMessage& - RejectExitMessage::operator=(const RejectExitMessage& other) - { - B = other.B; - R = other.R; - S = other.S; - T = other.T; - version = other.version; - Y = other.Y; - Z = other.Z; - return *this; - } - bool RejectExitMessage::Sign(llarp::Crypto* c, const llarp::SecretKey& sk) { diff --git a/llarp/exit/transfer_traffic.cpp b/llarp/exit/transfer_traffic.cpp index a3d1142e7..b6f3caafc 100644 --- a/llarp/exit/transfer_traffic.cpp +++ b/llarp/exit/transfer_traffic.cpp @@ -7,15 +7,6 @@ namespace llarp { namespace routing { - TransferTrafficMessage& - TransferTrafficMessage::operator=(const TransferTrafficMessage& other) - { - S = other.S; - version = other.version; - X = other.X; - return *this; - } - bool TransferTrafficMessage::PutBuffer(const llarp_buffer_t& buf, uint64_t counter) diff --git a/llarp/exit/update_exit.cpp b/llarp/exit/update_exit.cpp index a6a782bb2..0e3ad230a 100644 --- a/llarp/exit/update_exit.cpp +++ b/llarp/exit/update_exit.cpp @@ -59,18 +59,6 @@ namespace llarp return c->verify(pk, buf, Z); } - UpdateExitMessage& - UpdateExitMessage::operator=(const UpdateExitMessage& other) - { - P = other.P; - S = other.S; - T = other.T; - version = other.version; - Y = other.Y; - Z = other.Z; - return *this; - } - bool UpdateExitMessage::Sign(llarp::Crypto* c, const llarp::SecretKey& sk) { diff --git a/llarp/messages/exit.hpp b/llarp/messages/exit.hpp index 2deff8187..9e3af6ecb 100644 --- a/llarp/messages/exit.hpp +++ b/llarp/messages/exit.hpp @@ -62,19 +62,10 @@ namespace llarp struct GrantExitMessage final : public IMessage { using Nonce_t = llarp::AlignedBuffer< 16 >; + uint64_t T; Nonce_t Y; llarp::Signature Z; - GrantExitMessage() : IMessage() - { - } - - ~GrantExitMessage() - { - } - - GrantExitMessage& - operator=(const GrantExitMessage& other); bool BEncode(llarp_buffer_t* buf) const override; @@ -109,14 +100,6 @@ namespace llarp Nonce_t Y; llarp::Signature Z; - RejectExitMessage() : IMessage() - { - } - - ~RejectExitMessage() - { - } - void Clear() override { @@ -127,9 +110,6 @@ namespace llarp Z.Zero(); } - RejectExitMessage& - operator=(const RejectExitMessage& other); - bool Sign(llarp::Crypto* c, const llarp::SecretKey& sk); @@ -196,17 +176,6 @@ namespace llarp Nonce_t Y; llarp::Signature Z; - UpdateExitMessage() : IMessage() - { - } - - ~UpdateExitMessage() - { - } - - UpdateExitMessage& - operator=(const UpdateExitMessage& other); - bool Sign(llarp::Crypto* c, const llarp::SecretKey& sk); @@ -239,17 +208,6 @@ namespace llarp Nonce_t Y; llarp::Signature Z; - CloseExitMessage() : IMessage() - { - } - - ~CloseExitMessage() - { - } - - CloseExitMessage& - operator=(const CloseExitMessage& other); - bool BEncode(llarp_buffer_t* buf) const override; diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index d4879138d..5834b56fa 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -8,10 +8,6 @@ namespace llarp { - LinkIntroMessage::~LinkIntroMessage() - { - } - bool LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { @@ -104,17 +100,6 @@ namespace llarp return bencode_end(buf); } - LinkIntroMessage& - LinkIntroMessage::operator=(const LinkIntroMessage& msg) - { - version = msg.version; - Z = msg.Z; - rc = msg.rc; - N = msg.N; - P = msg.P; - return *this; - } - bool LinkIntroMessage::HandleMessage(AbstractRouter* router) const { diff --git a/llarp/messages/link_intro.hpp b/llarp/messages/link_intro.hpp index 83538761f..0b2cc5523 100644 --- a/llarp/messages/link_intro.hpp +++ b/llarp/messages/link_intro.hpp @@ -17,16 +17,11 @@ namespace llarp { } - ~LinkIntroMessage(); - RouterContact rc; KeyExchangeNonce N; Signature Z; uint64_t P; - LinkIntroMessage& - operator=(const LinkIntroMessage& msg); - bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; diff --git a/llarp/messages/transfer_traffic.hpp b/llarp/messages/transfer_traffic.hpp index c56e05617..aa8980319 100644 --- a/llarp/messages/transfer_traffic.hpp +++ b/llarp/messages/transfer_traffic.hpp @@ -31,9 +31,6 @@ namespace llarp return _size; } - TransferTrafficMessage& - operator=(const TransferTrafficMessage& other); - /// append buffer to X bool PutBuffer(const llarp_buffer_t& buf, uint64_t counter); diff --git a/llarp/metrics/metrictank_publisher.cpp b/llarp/metrics/metrictank_publisher.cpp index adbdc7645..af6ea5930 100644 --- a/llarp/metrics/metrictank_publisher.cpp +++ b/llarp/metrics/metrictank_publisher.cpp @@ -91,6 +91,7 @@ namespace llarp break; } assert(false && "Invalid publication type"); + return {}; } std::string diff --git a/llarp/net/net_addr.cpp b/llarp/net/net_addr.cpp index e5d79f618..383c73ce8 100644 --- a/llarp/net/net_addr.cpp +++ b/llarp/net/net_addr.cpp @@ -14,42 +14,6 @@ #define inet_aton(x, y) inet_pton(AF_INET, x, y) #endif -llarp::Addr::operator const sockaddr*() const -{ - if(af() == AF_INET) - return (const sockaddr*)&_addr4; - else - return (const sockaddr*)&_addr; -} - -llarp::Addr::operator sockaddr*() const -{ - if(af() == AF_INET) - return (sockaddr*)&_addr4; - else - return (sockaddr*)&_addr; -} - -bool -llarp::Addr::operator<(const Addr& other) const -{ - if(af() == AF_INET && other.af() == AF_INET) - return port() < other.port() || addr4()->s_addr < other.addr4()->s_addr; - else - return port() < other.port() || *addr6() < *other.addr6() - || af() < other.af(); -} - -bool -llarp::Addr::operator==(const Addr& other) const -{ - if(af() == AF_INET && other.af() == AF_INET) - return port() == other.port() && addr4()->s_addr == other.addr4()->s_addr; - else - return af() == other.af() && memcmp(addr6(), other.addr6(), 16) == 0 - && port() == other.port(); -} - namespace llarp { Addr::Addr() @@ -112,9 +76,8 @@ namespace llarp Addr::Addr(string_view addr_str, string_view port_str) { - this->from_char_array(llarp::string_view_string(addr_str).c_str()); - this->port( - std::strtoul(llarp::string_view_string(port_str).c_str(), nullptr, 10)); + this->from_char_array(string_view_string(addr_str).c_str()); + this->port(std::strtoul(string_view_string(port_str).c_str(), nullptr, 10)); } bool @@ -129,16 +92,16 @@ namespace llarp char buf[6]; snprintf(buf, 6, "%s", pPosition + 1); uint16_t port = std::atoi(buf); - llarp::LogDebug("Setting port ", std::to_string(port)); + LogDebug("Setting port ", std::to_string(port)); this->port(port); // trim str // can't VLA str = strdup(in); // copy it str[pPosition - in] = '\0'; // nul terminate it early - llarp::LogDebug("Truncating to ", str); + LogDebug("Truncating to ", str); freeStr = true; } - llarp::Zero(&_addr, sizeof(sockaddr_in6)); + Zero(&_addr, sizeof(sockaddr_in6)); struct addrinfo hint, *res = NULL; int ret; @@ -150,7 +113,7 @@ namespace llarp ret = getaddrinfo(str, NULL, &hint, &res); if(ret) { - llarp::LogError("failed to determine address family: ", str); + LogError("failed to determine address family: ", str); if(freeStr) free(str); return false; @@ -158,14 +121,14 @@ namespace llarp if(res->ai_family == AF_INET6) { - llarp::LogError("IPv6 address not supported yet", str); + LogError("IPv6 address not supported yet", str); if(freeStr) free(str); return false; } else if(res->ai_family != AF_INET) { - llarp::LogError("Address family not supported yet", str); + LogError("Address family not supported yet", str); if(freeStr) free(str); return false; @@ -175,7 +138,7 @@ namespace llarp struct in_addr* addr = &_addr4.sin_addr; if(inet_aton(str, addr) == 0) { - llarp::LogError("failed to parse ", str); + LogError("failed to parse ", str); if(freeStr) free(str); return false; @@ -208,7 +171,7 @@ namespace llarp Addr::from_4int(const uint8_t one, const uint8_t two, const uint8_t three, const uint8_t four) { - llarp::Zero(&_addr, sizeof(sockaddr_in6)); + Zero(&_addr, sizeof(sockaddr_in6)); struct in_addr* addr = &_addr4.sin_addr; unsigned char* ip = (unsigned char*)&(addr->s_addr); @@ -264,7 +227,7 @@ namespace llarp Addr::Addr(const sockaddr_in& other) { - llarp::Zero(&_addr, sizeof(sockaddr_in6)); + Zero(&_addr, sizeof(sockaddr_in6)); _addr.sin6_family = AF_INET; uint8_t* addrptr = _addr.sin6_addr.s6_addr; uint16_t* port = &_addr.sin6_port; @@ -300,7 +263,7 @@ namespace llarp Addr::Addr(const sockaddr& other) { - llarp::Zero(&_addr, sizeof(sockaddr_in6)); + Zero(&_addr, sizeof(sockaddr_in6)); _addr.sin6_family = other.sa_family; uint8_t* addrptr = _addr.sin6_addr.s6_addr; uint16_t* port = &_addr.sin6_port; @@ -406,10 +369,46 @@ namespace llarp return ntohs(_addr.sin6_port); } + Addr::operator const sockaddr*() const + { + if(af() == AF_INET) + return (const sockaddr*)&_addr4; + else + return (const sockaddr*)&_addr; + } + + Addr::operator sockaddr*() const + { + if(af() == AF_INET) + return (sockaddr*)&_addr4; + else + return (sockaddr*)&_addr; + } + + bool + Addr::operator<(const Addr& other) const + { + if(af() == AF_INET && other.af() == AF_INET) + return port() < other.port() || addr4()->s_addr < other.addr4()->s_addr; + else + return port() < other.port() || *addr6() < *other.addr6() + || af() < other.af(); + } + + bool + Addr::operator==(const Addr& other) const + { + if(af() == AF_INET && other.af() == AF_INET) + return port() == other.port() && addr4()->s_addr == other.addr4()->s_addr; + else + return af() == other.af() && memcmp(addr6(), other.addr6(), 16) == 0 + && port() == other.port(); + } + Addr& Addr::operator=(const sockaddr& other) { - llarp::Zero(&_addr, sizeof(sockaddr_in6)); + Zero(&_addr, sizeof(sockaddr_in6)); _addr.sin6_family = other.sa_family; uint8_t* addrptr = _addr.sin6_addr.s6_addr; uint16_t* port = &_addr.sin6_port; diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index bc37173ab..0e231d4a4 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -53,7 +53,6 @@ namespace llarp struct TransitHopInfo { TransitHopInfo() = default; - TransitHopInfo(const TransitHopInfo& other); TransitHopInfo(const RouterID& down, const LR_CommitRecord& record); PathID_t txID, rxID; @@ -158,8 +157,6 @@ namespace llarp { TransitHop(); - TransitHop(const TransitHop& other); - TransitHopInfo info; SharedSecret pathKey; ShortHash nonceXOR; diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 0f16c676d..21082c013 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -253,7 +253,7 @@ namespace llarp return true; } } while(tries > 0); - return tries > 0; + return false; } bool diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index c224525c8..4c97be371 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -32,14 +32,6 @@ namespace llarp return started + lifetime; } - TransitHopInfo::TransitHopInfo(const TransitHopInfo& other) - : txID(other.txID) - , rxID(other.rxID) - , upstream(other.upstream) - , downstream(other.downstream) - { - } - TransitHopInfo::TransitHopInfo(const RouterID& down, const LR_CommitRecord& record) : txID(record.txid) @@ -49,15 +41,6 @@ namespace llarp { } - TransitHop::TransitHop(const TransitHop& other) - : info(other.info) - , pathKey(other.pathKey) - , started(other.started) - , lifetime(other.lifetime) - , version(other.version) - { - } - bool TransitHop::SendRoutingMessage(const routing::IMessage& msg, AbstractRouter* r) diff --git a/llarp/service/intro.hpp b/llarp/service/intro.hpp index e62444859..8eda15e73 100644 --- a/llarp/service/intro.hpp +++ b/llarp/service/intro.hpp @@ -17,18 +17,9 @@ namespace llarp PubKey router; PathID_t pathID; uint64_t latency = 0; - uint64_t version = 0; uint64_t expiresAt = 0; Introduction() = default; - Introduction(const Introduction& other) : IBEncodeMessage(other.version) - { - router = other.router; - pathID = other.pathID; - latency = other.latency; - version = other.version; - expiresAt = other.expiresAt; - } util::StatusObject ExtractStatus() const; diff --git a/llarp/service/tag.hpp b/llarp/service/tag.hpp index c6061648b..a1b035206 100644 --- a/llarp/service/tag.hpp +++ b/llarp/service/tag.hpp @@ -36,13 +36,6 @@ namespace llarp begin()); } - Tag& - operator=(const Tag& other) - { - as_array() = other.as_array(); - return *this; - } - Tag& operator=(const std::string& str) { diff --git a/llarp/util/queue_manager.cpp b/llarp/util/queue_manager.cpp index 71f8b0e83..cd2104e2b 100644 --- a/llarp/util/queue_manager.cpp +++ b/llarp/util/queue_manager.cpp @@ -178,7 +178,7 @@ namespace llarp QueueManager::~QueueManager() { - delete m_states; + delete[] m_states; } QueueReturn