From 50e507ad8ddb3c93a1791f571fe4a4dc11ccde94 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Thu, 3 Nov 2022 12:04:41 -0400 Subject: [PATCH] tmp, squash --- CMakeLists.txt | 5 ++ contrib/liblokinet/CMakeLists.txt | 14 ++-- contrib/liblokinet/tcp_connect.cpp | 108 +++++++++++++++++++++++++++++ contrib/liblokinet/tcp_listen.cpp | 96 +++++++++++++++++++++++++ llarp/config/config.cpp | 2 +- llarp/lokinet_shared.cpp | 2 + llarp/router/rc_lookup_handler.cpp | 7 ++ llarp/service/endpoint.cpp | 3 + 8 files changed, 231 insertions(+), 6 deletions(-) create mode 100644 contrib/liblokinet/tcp_connect.cpp create mode 100644 contrib/liblokinet/tcp_listen.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index dac2c3d36..71958b675 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ option(USE_AVX2 "enable avx2 code" OFF) option(USE_NETNS "enable networking namespace support. Linux only" OFF) option(NATIVE_BUILD "optimise for host system and FPU" ON) option(WITH_EMBEDDED_LOKINET "build liblokinet.so for embedded lokinet" OFF) +option(LIBLOKINET_TEST_UTILS "build test utils in contrib/liblokinet" OFF) option(XSAN "use sanitiser, if your system has it (requires -DCMAKE_BUILD_TYPE=Debug)" OFF) option(USE_JEMALLOC "Link to jemalloc for memory allocations, if found" ON) option(TESTNET "testnet build" OFF) @@ -313,6 +314,10 @@ if(ANDROID) add_subdirectory(jni) endif() +if(BUILD_LIBLOKINET AND LIBLOKINET_TEST_UTILS) + add_subdirectory(contrib/liblokinet) +endif() + add_subdirectory(docs) include(cmake/gui.cmake) diff --git a/contrib/liblokinet/CMakeLists.txt b/contrib/liblokinet/CMakeLists.txt index 6985f741b..236979b61 100644 --- a/contrib/liblokinet/CMakeLists.txt +++ b/contrib/liblokinet/CMakeLists.txt @@ -1,10 +1,14 @@ - -cmake_minimum_required(VERSION 3.10) - +#[[ project(udptest LANGUAGES CXX) - -set(CMAKE_CXX_STANDARD 17) add_executable(udptest udptest.cpp) include_directories(../../include) target_link_libraries(udptest PUBLIC lokinet) +]] +add_executable(tcp_listen + tcp_listen.cpp) +target_link_libraries(tcp_listen PUBLIC lokinet-shared) + +add_executable(tcp_connect + tcp_connect.cpp) +target_link_libraries(tcp_connect PUBLIC lokinet-shared) diff --git a/contrib/liblokinet/tcp_connect.cpp b/contrib/liblokinet/tcp_connect.cpp new file mode 100644 index 000000000..be5559a4d --- /dev/null +++ b/contrib/liblokinet/tcp_connect.cpp @@ -0,0 +1,108 @@ +// Test utility to bind a local tcp socket listener with liblokinet + +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +bool _run{true}; + +void +signal_handler(int) +{ + _run = false; +} + +int +main(int argc, char* argv[]) +{ + if (argc != 2) + { + std::cout << "Usage: " << argv[0] << " something.loki\n"; + return 0; + } + + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + + if (auto* loglevel = getenv("LOKINET_LOG")) + lokinet_log_level(loglevel); + else + lokinet_log_level("info"); + + // log level debug for quic + llarp::log::set_level("quic", llarp::log::Level::debug); + + std::cout << "starting up\n"; + + auto shared_ctx = std::shared_ptr(lokinet_context_new(), lokinet_context_free); + auto* ctx = shared_ctx.get(); + if (lokinet_context_start(ctx)) + throw std::runtime_error{"could not start context"}; + + int status; + for (status = lokinet_status(ctx); _run and status == -1; status = lokinet_status(ctx)) + { + std::cout << "waiting for lokinet to be ready..." << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds{500}); + } + if (not _run) + { + std::cout << "exit requested before context was ready.\n"; + return 0; + } + if (status != 0) + { + std::cout << "lokinet_status = " << status << " after waiting for ready.\n"; + return 0; + } + + auto addr_c = lokinet_address(ctx); + std::string addr{addr_c}; + free(addr_c); + std::cout << "lokinet address: " << addr << "\n"; + + // wait a bit just so log output calms down so we can see stuff + // printed from here + std::this_thread::sleep_for(std::chrono::milliseconds{3000}); + + lokinet_stream_result stream_res; + + std::string target{argv[1]}; + target += ":12345"; + lokinet_outbound_stream(&stream_res, target.c_str(), nullptr, ctx); + + if (stream_res.error) + { + std::cout << "failed to prepare outbound tcp: " << strerror(stream_res.error) << "\n"; + return 0; + } + + + size_t counter = 0; + do + { + std::this_thread::sleep_for(std::chrono::milliseconds{100}); + if (counter++ % 30 == 0) + std::cout << "outbound tcp ready on " << stream_res.local_address << ":" << stream_res.local_port << "\n"; + } while (_run); + + std::cout << "tcp_connect shutting down...\n"; + + lokinet_close_stream(stream_res.stream_id, ctx); + return 0; +} + diff --git a/contrib/liblokinet/tcp_listen.cpp b/contrib/liblokinet/tcp_listen.cpp new file mode 100644 index 000000000..5defb6a5b --- /dev/null +++ b/contrib/liblokinet/tcp_listen.cpp @@ -0,0 +1,96 @@ +// Test utility to bind a local tcp socket listener with liblokinet + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +bool _run{true}; + +void +signal_handler(int) +{ + _run = false; +} + +int +main(int argc, char* argv[]) +{ + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + + if (auto* loglevel = getenv("LOKINET_LOG")) + lokinet_log_level(loglevel); + else + lokinet_log_level("info"); + + std::cout << "starting up\n"; + + auto shared_ctx = std::shared_ptr(lokinet_context_new(), lokinet_context_free); + auto* ctx = shared_ctx.get(); + if (lokinet_context_start(ctx)) + throw std::runtime_error{"could not start context"}; + + int status; + for (status = lokinet_status(ctx); _run and status == -1; status = lokinet_status(ctx)) + { + std::cout << "waiting for lokinet to be ready..." << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds{500}); + } + if (not _run) + { + std::cout << "exit requested before context was ready.\n"; + return 0; + } + if (status != 0) + { + std::cout << "lokinet_status = " << status << " after waiting for ready.\n"; + return 0; + } + + // wait a bit just so log output calms down so we can see stuff + // printed from here + std::this_thread::sleep_for(std::chrono::milliseconds{3000}); + + const auto port = 10000; + + auto id = lokinet_inbound_stream(port, ctx); + if (id < 0) + { + std::cout << "failed to bind tcp socket\n"; + return 0; + } + + std::cout << "bound tcp on localhost port: " << port << "\n"; + + auto addr_c = lokinet_address(ctx); + std::string addr{addr_c}; + free(addr_c); + + std::cout << "lokinet address: " << addr << "\n"; + + size_t counter = 0; + do + { + std::this_thread::sleep_for(std::chrono::milliseconds{100}); + if (++counter % 30 == 0) + std::cout << "lokinet address: " << addr << "\n"; + } while (_run); + + std::cout << "tcp_listen shutting down...\n"; + + lokinet_close_stream(id, ctx); + return 0; +} + diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index eda628dda..7328442c2 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -1654,7 +1654,7 @@ namespace llarp { auto config = std::make_shared(); config->Load(); - config->logging.m_logLevel = log::Level::off; + config->logging.m_logLevel = log::Level::debug; config->api.m_enableRPCServer = false; config->network.m_endpointType = "null"; config->network.m_saveProfiles = false; diff --git a/llarp/lokinet_shared.cpp b/llarp/lokinet_shared.cpp index 7d4a0f085..bcaf52ebf 100644 --- a/llarp/lokinet_shared.cpp +++ b/llarp/lokinet_shared.cpp @@ -483,6 +483,8 @@ extern "C" int EXPORT lokinet_add_bootstrap_rc(const char* data, size_t datalen, struct lokinet_context* ctx) { + // FIXME: bootstrap loading was rewritten but this code needs updated to do + // it how Router does now. if (data == nullptr or datalen == 0) return -3; llarp_buffer_t buf{data, datalen}; diff --git a/llarp/router/rc_lookup_handler.cpp b/llarp/router/rc_lookup_handler.cpp index ca9169e65..afa892f8e 100644 --- a/llarp/router/rc_lookup_handler.cpp +++ b/llarp/router/rc_lookup_handler.cpp @@ -18,6 +18,8 @@ namespace llarp { + static auto logcat = log::Cat("rc-lookup"); + void RCLookupHandler::AddValidRouter(const RouterID& router) { @@ -91,6 +93,11 @@ namespace llarp { itr_pair.first->second.push_back(callback); } + log::trace( + logcat, + "RC Lookup for {} has {} pending callbacks.", + router, + itr_pair.first->second.size()); shouldDoLookup = itr_pair.second; } diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 89c24bc6b..10cc9fa00 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -148,7 +148,10 @@ namespace llarp if (auto* quic = GetQUICTunnel()) { if (quic->hasListeners()) + { + log::debug(logcat, "IntroSet setting QUIC as available protocol."); introSet().supportedProtocols.push_back(ProtocolType::QUIC); + } } introSet().intros.clear();