From 12a89328fe8c343aae9a55d438ac736527e9bd26 Mon Sep 17 00:00:00 2001 From: dr7ana Date: Mon, 23 Oct 2023 15:58:58 -0700 Subject: [PATCH] Cmake restructuring --- CMakeLists.txt | 21 +- cmake/CMakeGraphVizOptions.cmake | 9 +- daemon/CMakeLists.txt | 6 +- daemon/lokinet.cpp | 3 - external/CMakeLists.txt | 2 +- llarp/CMakeLists.txt | 226 ++++++-------- llarp/CMakeLists_old.txt | 421 --------------------------- llarp/config/config.hpp | 1 + llarp/config/key_manager.cpp | 4 +- llarp/crypto/crypto.cpp | 4 +- llarp/crypto/crypto.hpp | 4 +- llarp/crypto/encrypted_frame.cpp | 4 +- llarp/crypto/types.cpp | 3 - llarp/crypto/types.hpp | 7 +- llarp/handlers/exit.cpp | 6 +- llarp/link/tunnel.hpp | 18 +- llarp/messages/exit.hpp | 18 +- llarp/messages/path.hpp | 2 +- llarp/path/transit_hop.cpp | 4 +- llarp/pow.cpp | 3 +- llarp/router_contact.cpp | 3 +- llarp/service/async_key_exchange.cpp | 3 +- llarp/service/auth.cpp | 3 +- llarp/service/context.cpp | 337 +++++++++++---------- llarp/service/identity.cpp | 3 +- llarp/service/intro_set.cpp | 6 +- llarp/service/protocol.cpp | 3 +- 27 files changed, 311 insertions(+), 813 deletions(-) delete mode 100644 llarp/CMakeLists_old.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index eff2b914f..4e68a2426 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "macOS deployment target (Apple clang only)") option(BUILD_DAEMON "build lokinet daemon and associated utils" ON) - +option(GRAPH_DEPENDENCIES "Produce graphviz representation of cmake dependencies" OFF) set(LANGS C CXX) if(APPLE AND BUILD_DAEMON) @@ -257,23 +257,26 @@ pkg_check_modules(SD libsystemd IMPORTED_TARGET) # Default WITH_SYSTEMD to true if we found it option(WITH_SYSTEMD "enable systemd integration for sd_notify" ${SD_FOUND}) -# Base interface target where we set up global link libraries, definitions, includes, etc. -add_library(base_libs INTERFACE) +add_subdirectory(external) + +# interface library for setting common includes, linkage and flags +add_library(lokinet-base INTERFACE) +target_include_directories(lokinet-base INTERFACE . include) +target_link_libraries(lokinet-base INTERFACE quic) if(WITH_SYSTEMD AND (NOT ANDROID)) if(NOT SD_FOUND) message(FATAL_ERROR "libsystemd not found") endif() - target_link_libraries(base_libs INTERFACE PkgConfig::SD) - target_compile_definitions(base_libs INTERFACE WITH_SYSTEMD) + target_link_libraries(lokinet-base INTERFACE PkgConfig::SD) + target_compile_definitions(lokinet-base INTERFACE WITH_SYSTEMD) endif() -add_subdirectory(external) if(USE_JEMALLOC AND NOT STATIC_LINK) pkg_check_modules(JEMALLOC jemalloc IMPORTED_TARGET) if(JEMALLOC_FOUND) - target_link_libraries(base_libs INTERFACE PkgConfig::JEMALLOC) + target_link_libraries(lokinet-base INTERFACE PkgConfig::JEMALLOC) else() message(STATUS "jemalloc not found, not linking to jemalloc") endif() @@ -283,8 +286,8 @@ endif() if(ANDROID) - target_link_libraries(base_libs INTERFACE log) - target_compile_definitions(base_libs INTERFACE ANDROID) + target_link_libraries(lokinet-base INTERFACE log) + target_compile_definitions(lokinet-base INTERFACE ANDROID) set(ANDROID_PLATFORM_SRC android/ifaddrs.c) endif() diff --git a/cmake/CMakeGraphVizOptions.cmake b/cmake/CMakeGraphVizOptions.cmake index f0de4137e..4d9740171 100644 --- a/cmake/CMakeGraphVizOptions.cmake +++ b/cmake/CMakeGraphVizOptions.cmake @@ -1,5 +1,6 @@ -set(GRAPHVIZ_MODULE_LIBS OFF CACHE BOOL "") -set(GRAPHVIZ_EXTERNAL_LIBS OFF CACHE BOOL "") -set(GRAPHVIZ_GENERATE_PER_TARGET OFF CACHE BOOL "") -set(GRAPHVIZ_GENERATE_DEPENDERS OFF CACHE BOOL "") +set(GRAPHVIZ_GRAPH_NAME "graph.dot" CACHE STRING "") +# set(GRAPHVIZ_MODULE_LIBS OFF CACHE BOOL "") +# set(GRAPHVIZ_EXTERNAL_LIBS OFF CACHE BOOL "") +set(GRAPHVIZ_GENERATE_PER_TARGET FALSE CACHE BOOL "") +set(GRAPHVIZ_GENERATE_DEPENDERS FALSE CACHE BOOL "") set(GRAPHVIZ_OBJECT_LIBS OFF CACHE BOOL "") diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index c9ff4aec6..05685f44a 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -59,7 +59,8 @@ foreach(exe ${exetargets}) elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") target_link_directories(${exe} PRIVATE /usr/local/lib) endif() - target_link_libraries(${exe} PUBLIC lokinet-amalgum hax_and_shims_for_cmake) + # target_link_libraries(${exe} PUBLIC lokinet-amalgum hax_and_shims_for_cmake) + target_link_libraries(${exe} PUBLIC lokinet-core hax_and_shims_for_cmake) if(STRIP_SYMBOLS) add_custom_command(TARGET ${exe} POST_BUILD @@ -79,6 +80,9 @@ foreach(exe ${exetargets}) endif() endforeach() +target_link_libraries(lokinet PRIVATE CLI11) +target_link_libraries(lokinet-vpn PRIVATE CLI11) + if(SETCAP) install(CODE "execute_process(COMMAND ${SETCAP} cap_net_admin,cap_net_bind_service=+eip ${CMAKE_INSTALL_PREFIX}/bin/lokinet)") endif() diff --git a/daemon/lokinet.cpp b/daemon/lokinet.cpp index 2d87fc7bb..b57e28449 100644 --- a/daemon/lokinet.cpp +++ b/daemon/lokinet.cpp @@ -371,9 +371,6 @@ namespace int lokinet_main(int argc, char** argv) { - // if (auto result = Lokinet_INIT()) - // return result; - llarp::RuntimeOptions opts; opts.showBanner = false; diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 5fb082101..2b09d0ee5 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -147,7 +147,7 @@ option(WITH_LIBCRYPT "enable fast password hash with libcrypt" ${default_libcryp add_library(lokinet-libcrypt INTERFACE) if(WITH_LIBCRYPT) pkg_check_modules(LIBCRYPT libcrypt IMPORTED_TARGET REQUIRED) - add_definitions(-DHAVE_CRYPT) + target_compile_definitions(lokinet-libcrypt INTERFACE -DHAVE_CRYPT) target_link_libraries(lokinet-libcrypt INTERFACE PkgConfig::LIBCRYPT) message(STATUS "using libcrypt ${LIBCRYPT_VERSION}") else() diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index aec3b59f0..a3c698a2f 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -1,44 +1,49 @@ include(Version) -add_library(lokinet-cryptography - STATIC + +# Add an internal lokinet static library target, enables LTO (if enabled) on the target, +# and links it to the common lokinet-base interface. +# Invoke with the target/library name (e.g. "lokinet-foo") and list of source files, e.g. +# lokinet_add_library(lokinet-foo foo/source1.cpp foo/source2.cpp) +function(lokinet_add_library libname) + add_library(${libname} STATIC ${ARGN}) + target_link_libraries(${libname} PUBLIC lokinet-base) + enable_lto(${libname}) +endfunction() + + +lokinet_add_library(lokinet-cryptography crypto/crypto.cpp crypto/encrypted_frame.cpp crypto/types.cpp ) -# files only included by lokinet-core -add_library(lokinet-core-internal - STATIC - router/rc_gossiper.cpp - service/info.cpp - service/lns_tracker.cpp - service/session.cpp -) - # Functional objects use by lokinet-core and other libraries # needed by vpn/ router/ rpc/ handlers/ net/ link/ -add_library(lokinet-core-utils - STATIC +lokinet_add_library(lokinet-core-utils endpoint_base.cpp exit/context.cpp exit/endpoint.cpp # handlers/exit.hpp exit/policy.cpp # net/ + handlers/exit.cpp # link/ exit/ + handlers/tun.cpp + router/rc_gossiper.cpp service/auth.cpp # config/ service/context.cpp service/identity.cpp + service/info.cpp service/intro.cpp # path + service/lns_tracker.cpp service/name.cpp + service/session.cpp service/tag.cpp + vpn/egres_packet_router.cpp ) -add_library(lokinet-core - STATIC +lokinet_add_library(lokinet-core context.cpp consensus/reachability_testing.cpp exit/session.cpp - handlers/exit.cpp # link/ exit/ - handlers/tun.cpp link/link_manager.cpp router/router.cpp router/route_poker.cpp @@ -49,16 +54,9 @@ add_library(lokinet-core service/name.cpp service/outbound_context.cpp service/protocol.cpp - vpn/egres_packet_router.cpp ) - -# Files only included by lokinet-rpc -# add_library(lokinet-rpc-internal -# STATIC -# ) - - add_library(lokinet-rpc - STATIC + +lokinet_add_library(lokinet-rpc rpc/json_binary_proxy.cpp rpc/json_conversions.cpp rpc/lokid_rpc_client.cpp @@ -66,28 +64,16 @@ add_library(lokinet-core rpc/rpc_server.cpp rpc/endpoint_rpc.cpp ) - -# Files only included by lokinet-wire -add_library(lokinet-wire-internal - STATIC + +lokinet_add_library(lokinet-wire link/connection.cpp -) - -add_library(lokinet-wire - STATIC link/contacts.cpp link/link_manager.cpp # link/tunnel.cpp ) -add_library(lokinet-utils-internal - STATIC - util/thread/queue_manager.cpp -) - # config, crypto, timeplace, nodedb, bootstrap.cpp, net, router -add_library(lokinet-utils - STATIC +lokinet_add_library(lokinet-utils ${CMAKE_CURRENT_BINARY_DIR}/constants/version.cpp util/bencode.cpp util/buffer.cpp @@ -95,16 +81,16 @@ add_library(lokinet-utils util/mem.cpp util/str.cpp util/thread/threading.cpp + util/thread/queue_manager.cpp util/time.cpp util/logging/buffer.cpp ) - + add_dependencies(lokinet-utils genversion) # Addressing and event loop files used by lokinet-core and other libraries # needed by rpc/ link/ service/ config/ path/ dht/ -add_library(lokinet-time-place - STATIC +lokinet_add_library(lokinet-time-place pow.cpp # only intro_set router_contact.cpp router_id.cpp @@ -124,8 +110,7 @@ add_library(lokinet-time-place ) # lokinet-platform holds all platform specific code -add_library(lokinet-platform - STATIC +lokinet_add_library(lokinet-platform router/rc_lookup_handler.cpp net/interface_info.cpp vpn/packet_router.cpp @@ -171,7 +156,6 @@ endif() # should have no concept of dns caching, this is left as an implementation # detail of dns resolvers (LATER: make separate lib for dns resolvers) add_library(lokinet-dns-internal - STATIC dns/name.cpp # srv_data, question, rr dns/platform.cpp dns/question.cpp # message @@ -179,8 +163,7 @@ add_library(lokinet-dns-internal dns/serialize.cpp ) -add_library(lokinet-dns - STATIC +lokinet_add_library(lokinet-dns dns/message.cpp # dns/server dns/server.cpp # handlers/exit+tun and service/endpoint dns/srv_data.cpp @@ -195,8 +178,7 @@ endif() # lokinet-nodedb holds all types and logic for storing parsing and constructing # nodedb data published to the network and versions of it stored locally -add_library(lokinet-nodedb - STATIC +lokinet_add_library(lokinet-nodedb bootstrap.cpp # config, router.hpp nodedb.cpp profiling.cpp # path, router, service::endpoint @@ -223,8 +205,7 @@ configure_file("bootstrap-fallbacks.cpp.in" "${CMAKE_CURRENT_BINARY_DIR}/bootstr target_sources(lokinet-nodedb PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/bootstrap-fallbacks.cpp") # lokinet-config is for all configuration types and parsers -add_library(lokinet-config - STATIC +lokinet_add_library(lokinet-config config/config.cpp config/definition.cpp config/ini.cpp @@ -232,8 +213,7 @@ add_library(lokinet-config ) # All path objects; link directly to lokinet-core -add_library(lokinet-path - STATIC +lokinet_add_library(lokinet-path path/abstracthophandler.cpp path/path_context.cpp path/path.cpp @@ -243,23 +223,11 @@ add_library(lokinet-path messages/relay.cpp ) -add_library(lokinet-amalgum INTERFACE) - -# interface library for setting common includes, linkage and flags. -add_library(lokinet-base INTERFACE) -target_include_directories(lokinet-base - INTERFACE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include -) -target_link_libraries(lokinet-base INTERFACE quic) - # Link libraries to their internals -target_link_libraries(lokinet-core-internal PUBLIC lokinet-utils lokinet-core-utils) -target_link_libraries(lokinet-core PRIVATE lokinet-core-internal) -# target_link_libraries(lokinet-rpc PRIVATE lokinet-rpc-internal) -target_link_libraries(lokinet-wire PRIVATE lokinet-wire-internal) -# target_link_libraries(lokinet-time-place PRIVATE lokinet-time-place-internal) +target_link_libraries(lokinet-core-utils PUBLIC lokinet-dns) +target_link_libraries(lokinet-core PUBLIC lokinet-core-utils) + target_link_libraries(lokinet-dns PRIVATE lokinet-dns-internal) -target_link_libraries(lokinet-utils PRIVATE lokinet-utils-internal) # Link lokinet-dns to alternate libraries target_link_libraries(lokinet-dns PUBLIC lokinet-dns-platform) @@ -267,36 +235,9 @@ if (TARGET lokinet-dns-systemd) target_link_libraries(lokinet-dns PUBLIC lokinet-dns-systemd) endif() -# Link relevant to lokinet-base -target_link_libraries(lokinet-core PUBLIC lokinet-base) -target_link_libraries(lokinet-rpc PUBLIC lokinet-base) -target_link_libraries(lokinet-nodedb PUBLIC lokinet-base) -target_link_libraries(lokinet-wire PUBLIC lokinet-base) -target_link_libraries(lokinet-time-place PUBLIC lokinet-base) -target_link_libraries(lokinet-dns PUBLIC lokinet-base) -target_link_libraries(lokinet-platform PUBLIC lokinet-base) -target_link_libraries(lokinet-utils PUBLIC lokinet-base) -target_link_libraries(lokinet-libntrup PUBLIC lokinet-base) - -# Link relevant to amalgum -target_link_libraries(lokinet-amalgum - INTERFACE - lokinet-core - lokinet-rpc - lokinet-nodedb - lokinet-wire - lokinet-time-place - lokinet-dns - lokinet-platform - lokinet-utils - lokinet-cryptography - lokinet-path -) -#or just do this?? -# target_link_libraries(lokinet-amalgum INTERFACE lokinet-base) -target_link_libraries(lokinet-wire-internal PUBLIC lokinet-time-place) -target_link_libraries(lokinet-dns-internal PUBLIC lokinet-utils lokinet-config) -target_link_libraries(lokinet-dns PUBLIC lokinet-cryptography) +target_link_libraries(lokinet-wire PUBLIC lokinet-time-place) +target_link_libraries(lokinet-dns-internal PUBLIC lokinet-utils) +target_link_libraries(lokinet-dns PUBLIC lokinet-cryptography lokinet-config) target_link_libraries(lokinet-nodedb PUBLIC lokinet-cryptography) target_link_libraries(lokinet-platform PUBLIC lokinet-cryptography) target_link_libraries(lokinet-rpc PUBLIC lokinet-wire) @@ -304,6 +245,11 @@ target_link_libraries(lokinet-time-place PUBLIC lokinet-utils lokinet-cryptograp target_link_libraries(lokinet-wire PUBLIC lokinet-cryptography) target_link_libraries(lokinet-config PUBLIC lokinet-cryptography) +target_link_libraries(lokinet-dns + PUBLIC + lokinet-time-place +) + target_link_libraries(lokinet-path PUBLIC lokinet-time-place @@ -311,10 +257,10 @@ target_link_libraries(lokinet-path target_link_libraries(lokinet-core-utils PUBLIC - lokinet-core - lokinet-wire - lokinet-rpc + lokinet-config lokinet-platform + lokinet-rpc + lokinet-wire ) target_link_libraries(lokinet-cryptography @@ -333,50 +279,42 @@ target_link_libraries(lokinet-core PUBLIC lokinet-rpc lokinet-wire - lokinet-nodedb - lokinet-config + lokinet-nodedb + lokinet-path lokinet-cryptography ) +target_link_libraries(lokinet-base INTERFACE oxenc::oxenc oxen::logging) -# per component external deps -# target_link_libraries(oxenmq::oxenmq -# PUBLIC -# lokinet-time-place -# lokinet-core -# lokinet-config -# lokinet-platform -# lokinet-dns -# lokinet-rpc -# ) - -target_link_libraries(lokinet-rpc PUBLIC oxenmq::oxenmq oxenc::oxenc) -target_link_libraries(lokinet-core PUBLIC oxenmq::oxenmq CLI11 uvw) +target_link_libraries(lokinet-rpc PUBLIC oxenmq::oxenmq) +target_link_libraries(lokinet-core PUBLIC oxenmq::oxenmq) target_link_libraries(lokinet-config PUBLIC oxenmq::oxenmq) -target_link_libraries(lokinet-nodedb PUBLIC oxenmq::oxenmq uvw) +target_link_libraries(lokinet-nodedb PUBLIC oxenmq::oxenmq) target_link_libraries(lokinet-path PUBLIC oxenmq::oxenmq) -target_link_libraries(lokinet-time-place PUBLIC uvw oxenc::oxenc) -target_link_libraries(lokinet-platform PUBLIC oxenmq::oxenmq Threads::Threads base_libs uvw) -target_link_libraries(lokinet-cryptography PUBLIC oxenc::oxenc sodium) -target_link_libraries(lokinet-dns PUBLIC libunbound uvw) -target_link_libraries(lokinet-utils PUBLIC nlohmann_json::nlohmann_json oxenc::oxenc) -target_link_libraries(lokinet-wire PUBLIC oxenmq::oxenmq quic uvw) - -# if(WITH_EMBEDDED_LOKINET) -# include(GNUInstallDirs) -# add_library(lokinet-shared SHARED lokinet_shared.cpp) -# target_link_libraries(lokinet-shared PUBLIC lokinet-amalgum) -# if(WIN32) -# set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "") -# endif() -# set_target_properties(lokinet-shared PROPERTIES OUTPUT_NAME lokinet) -# if(WIN32) -# target_link_libraries(lokinet-shared PUBLIC ws2_32 iphlpapi -fstack-protector) -# install(TARGETS lokinet-shared DESTINATION bin COMPONENT liblokinet) -# elseif(NOT APPLE) -# install(TARGETS lokinet-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT liblokinet) -# endif() -# endif() - -# file(GLOB_RECURSE docs_SRC */*.hpp *.hpp) -# set(DOCS_SRC ${docs_SRC} PARENT_SCOPE) +target_link_libraries(lokinet-time-place PUBLIC uvw) +# target_link_libraries(lokinet-platform PUBLIC oxenmq::oxenmq Threads::Threads base_libs uvw) +target_link_libraries(lokinet-platform PUBLIC oxenmq::oxenmq Threads::Threads uvw) +target_link_libraries(lokinet-cryptography PUBLIC sodium) +target_link_libraries(lokinet-dns PUBLIC libunbound) +target_link_libraries(lokinet-utils PUBLIC nlohmann_json::nlohmann_json) +target_link_libraries(lokinet-wire PUBLIC oxenmq::oxenmq quic) + +if(WITH_EMBEDDED_LOKINET) + include(GNUInstallDirs) + add_library(lokinet-shared SHARED lokinet_shared.cpp) + # target_link_libraries(lokinet-shared PUBLIC lokinet-amalgum) + target_link_libraries(lokinet-shared PUBLIC lokinet-core) + if(WIN32) + set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "") + endif() + set_target_properties(lokinet-shared PROPERTIES OUTPUT_NAME lokinet) + if(WIN32) + target_link_libraries(lokinet-shared PUBLIC ws2_32 iphlpapi -fstack-protector) + install(TARGETS lokinet-shared DESTINATION bin COMPONENT liblokinet) + elseif(NOT APPLE) + install(TARGETS lokinet-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT liblokinet) + endif() +endif() + +file(GLOB_RECURSE docs_SRC */*.hpp *.hpp) +set(DOCS_SRC ${docs_SRC} PARENT_SCOPE) diff --git a/llarp/CMakeLists_old.txt b/llarp/CMakeLists_old.txt deleted file mode 100644 index cc2d51d5e..000000000 --- a/llarp/CMakeLists_old.txt +++ /dev/null @@ -1,421 +0,0 @@ -include(Version) - -add_library(lokinet-cryptography - STATIC - crypto/crypto.cpp - crypto/encrypted_frame.cpp - crypto/types.cpp -) - -add_library(lokinet-util - STATIC - ${CMAKE_CURRENT_BINARY_DIR}/constants/version.cpp - util/bencode.cpp - util/buffer.cpp - util/file.cpp - util/json.cpp - util/logging/buffer.cpp - util/easter_eggs.cpp - util/mem.cpp - util/str.cpp - util/thread/queue_manager.cpp - util/thread/threading.cpp - util/time.cpp) - -add_dependencies(lokinet-util genversion) - -# lokinet-platform holds all platform specific code -add_library(lokinet-platform - STATIC - # for networking - ev/ev.cpp - ev/libuv.cpp - net/interface_info.cpp - net/ip.cpp - net/ip_address.cpp - net/ip_packet.cpp - net/ip_range.cpp - net/net_int.cpp - net/sock_addr.cpp - vpn/packet_router.cpp - vpn/egres_packet_router.cpp - vpn/platform.cpp -) - -if (ANDROID) - target_sources(lokinet-platform PRIVATE android/ifaddrs.c util/nop_service_manager.cpp) -endif() - -if(CMAKE_SYSTEM_NAME MATCHES "Linux") - target_sources(lokinet-platform PRIVATE linux/dbus.cpp) - if(WITH_SYSTEMD) - target_sources(lokinet-platform PRIVATE linux/sd_service_manager.cpp) - else() - target_sources(lokinet-platform PRIVATE util/nop_service_manager.cpp) - endif() -endif() - -if (WIN32) - target_sources(lokinet-platform PRIVATE - net/win32.cpp - vpn/win32.cpp - win32/service_manager.cpp - win32/exec.cpp - win32/dll.cpp - win32/exception.cpp - win32/wintun.cpp - win32/windivert.cpp) - target_include_directories(lokinet-platform PRIVATE ${CMAKE_BINARY_DIR}/wintun/include/ ${CMAKE_BINARY_DIR}/WinDivert-${WINDIVERT_VERSION}/include/) -else() - target_sources(lokinet-platform PRIVATE - net/posix.cpp) -endif() - -if(APPLE) - add_subdirectory(apple) - target_sources(lokinet-platform PRIVATE util/nop_service_manager.cpp) -endif() - -# lokinet-dns is the dns parsing and hooking library that we use to -# parse modify and reconstitute dns wire proto, dns queries and RR -# should have no concept of dns caching, this is left as an implementation -# detail of dns resolvers (LATER: make separate lib for dns resolvers) -add_library(lokinet-dns - STATIC - dns/message.cpp - dns/name.cpp - dns/platform.cpp - dns/question.cpp - dns/rr.cpp - dns/serialize.cpp - dns/server.cpp - dns/srv_data.cpp) - -# platform specific bits and bobs for setting dns -add_library(lokinet-dns-platform INTERFACE) -if(WITH_SYSTEMD) - add_library(lokinet-dns-systemd STATIC dns/nm_platform.cpp dns/sd_platform.cpp) - target_link_libraries(lokinet-dns-platform INTERFACE lokinet-dns-systemd) -endif() - -# lokinet-nodedb holds all types and logic for storing parsing and constructing -# nodedb data published to the network and versions of it stored locally -add_library(lokinet-nodedb - STATIC - bootstrap.cpp - net/exit_info.cpp - net/traffic_policy.cpp - nodedb.cpp - pow.cpp - profiling.cpp - router_contact.cpp - router_id.cpp - router_version.cpp -) - -set(BOOTSTRAP_FALLBACKS) -foreach(bs IN ITEMS MAINNET TESTNET) - if(BOOTSTRAP_FALLBACK_${bs}) - message(STATUS "Building with ${bs} fallback boostrap path \"${BOOTSTRAP_FALLBACK_${bs}}\"") - file(READ "${BOOTSTRAP_FALLBACK_${bs}}" bs_data HEX) - if(bs STREQUAL TESTNET) - set(network "gamma") - elseif(bs STREQUAL MAINNET) - set(network "lokinet") - else() - string(TOLOWER "${bs}" network) - endif() - string(REGEX REPLACE "([0-9a-f][0-9a-f])" "\\\\x\\1" bs_data "${bs_data}") - set(BOOTSTRAP_FALLBACKS "${BOOTSTRAP_FALLBACKS}{\"${network}\"s, \"${bs_data}\"sv},\n") - endif() -endforeach() -configure_file("bootstrap-fallbacks.cpp.in" "${CMAKE_CURRENT_BINARY_DIR}/bootstrap-fallbacks.cpp" @ONLY) -target_sources(lokinet-nodedb PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/bootstrap-fallbacks.cpp") - -# lokinet-config is for all configuration types and parsers -add_library(lokinet-config - STATIC - config/config.cpp - config/definition.cpp - config/ini.cpp - config/key_manager.cpp -) - -# lokinet-consensus is for deriving and tracking network consensus state for both service nodes and clients -add_library(lokinet-consensus - STATIC - consensus/reachability_testing.cpp -) - -# lokinet-layer-onion is the "dumb" onion routing layer with builds manages and does i/o -# with onion paths. onion paths anonymize routing layer pdu. -add_library(lokinet-layer-onion - STATIC - path/abstracthophandler.cpp - path/path_context.cpp - path/path.cpp - path/pathbuilder.cpp - path/pathset.cpp - path/transit_hop.cpp - messages/relay.cpp -) - -# lokinet-layer-link is for our layer 2 analog which splits up layer 2 frames into -# a series of layer 1 symbols which are then transmitted between lokinet instances -add_library(lokinet-layer-link - STATIC - link/connection.cpp - link/contacts.cpp - link/link_manager.cpp - # link/tunnel.cpp -) - -# lokinet-context holds the contextualized god objects for a lokinet instance -# it is what any main function would link to in practice but it is hidden behind an interface library (lokinet-amalgum) -add_library(lokinet-context - STATIC - context.cpp - link/link_manager.cpp - router/rc_lookup_handler.cpp - router/rc_gossiper.cpp - router/router.cpp - router/route_poker.cpp -) - -# lokinet-rpc holds all rpc related compilation units -add_library(lokinet-rpc - STATIC - rpc/json_binary_proxy.cpp - rpc/json_conversions.cpp - rpc/lokid_rpc_client.cpp - rpc/rpc_request_parser.cpp - rpc/rpc_server.cpp - rpc/endpoint_rpc.cpp -) - -# optional peer stats library -add_library(lokinet-peerstats - STATIC - peerstats/peer_db.cpp - peerstats/types.cpp -) - -# kitchen sink to be removed after refactor -add_library(lokinet-service-deprecated-kitchensink - STATIC - endpoint_base.cpp - exit/context.cpp - exit/endpoint.cpp - exit/policy.cpp - exit/session.cpp - handlers/exit.cpp - handlers/tun.cpp - service/name.cpp - service/address.cpp - service/async_key_exchange.cpp - service/auth.cpp - service/convotag.cpp - service/context.cpp - service/endpoint_state.cpp - service/endpoint_util.cpp - service/endpoint.cpp - service/identity.cpp - service/info.cpp - service/intro_set.cpp - service/intro.cpp - service/lns_tracker.cpp - service/name.cpp - service/outbound_context.cpp - service/protocol.cpp - service/session.cpp - service/tag.cpp -) - -# interface library for setting commone includes, linkage and flags. -add_library(lokinet-base INTERFACE) -target_include_directories(lokinet-base - INTERFACE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include -) -target_link_libraries(lokinet-base INTERFACE quic) - -if(WITH_PEERSTATS) - target_compile_definitions(lokinet-base INTERFACE -DLOKINET_PEERSTATS_BACKEND) - target_link_libraries(lokinet-base INTERFACE sqlite_orm) -endif() - -# interface libraries for internal linkage -add_library(lokinet-layers INTERFACE) -add_library(lokinet-amalgum INTERFACE) - -# helper function to link a library to lokinet-base, enable lto, add to lokinet-amalgum and then link to other libs -function(lokinet_link_lib libname) - # Absolutely fuck this line, it doesn't "create" the target, as the target already exists - # message(DEBUG "created target: ${libname}") - enable_lto(${libname}) - target_link_libraries(${libname} PUBLIC lokinet-base ${ARGN}) - target_link_libraries(lokinet-amalgum INTERFACE ${libname}) -endfunction() - -# internal public linkages of components -lokinet_link_lib(lokinet-util lokinet-libntrup) -lokinet_link_lib(lokinet-cryptography lokinet-libcrypt lokinet-util) -lokinet_link_lib(lokinet-peerstats lokinet-context) -lokinet_link_lib(lokinet-consensus lokinet-context) -lokinet_link_lib(lokinet-layer-link lokinet-cryptography lokinet-peerstats) - -if(TARGET lokinet-dns-systemd) - lokinet_link_lib(lokinet-dns-systemd - lokinet-dns - lokinet-platform - ) -endif() - -lokinet_link_lib(lokinet-platform lokinet-util) - -lokinet_link_lib(lokinet-config - lokinet-util - lokinet-nodedb - lokinet-dns - lokinet-platform -) - -lokinet_link_lib(lokinet-context - lokinet-config - lokinet-platform - lokinet-peerstats - lokinet-layers - lokinet-consensus - lokinet-rpc -) - -lokinet_link_lib( - lokinet-platform - lokinet-config -) - -lokinet_link_lib(lokinet-dns - lokinet-platform - lokinet-dns-platform - lokinet-config -) - -lokinet_link_lib(lokinet-nodedb - lokinet-util - lokinet-platform -) - -lokinet_link_lib(lokinet-util - lokinet-nodedb - lokinet-platform -) - -lokinet_link_lib(lokinet-rpc - lokinet-context - lokinet-peerstats - lokinet-util -) - -# inter lokinet-layer public/private linkage. -# when linking each layer, we consider the layer directly below private linkage and the layer above public linkage. -# this lets us hide functionality of layers below us when depended on by another component. -# -# from highest to lowest layer, the above layers are stacked as follows: -# -# platform (what lokinet snapps interact with, be it l3 os interaction or embedded lokinet) -# flow (how we want to route and stripe over our onion routing) -# routing (what we are onion routing) -# onion (how the onion routing happens) -# link (what we want to send over the wire and to where) -# wire (what is actually sent over the wire) -# -function(link_lokinet_layers) - set(lib ${ARGV0}) - if(${ARGC} GREATER 1) - lokinet_link_lib(${ARGV1} ${lib}) - list(REMOVE_AT ARGV 1) - target_link_libraries(${lib} PRIVATE ${ARGV1}) - # recursion :D - link_lokinet_layers(${ARGV}) - else() - lokinet_link_lib(${lib}) - endif() -endfunction() - -link_lokinet_layers( - lokinet-layer-onion - lokinet-layer-link -) - -# set me to OFF to disable old codepath -set(use_old_impl ON) -if(use_old_impl) - # flow layer deprecated-kitchensink (remove me after refactor) - lokinet_link_lib(lokinet-service-deprecated-kitchensink - lokinet-dns - lokinet-nodedb - lokinet-context - lokinet-layer-onion - lokinet-platform - lokinet-rpc - ) - target_link_libraries(lokinet-layers INTERFACE lokinet-service-deprecated-kitchensink) -endif() - -target_link_libraries(lokinet-layers INTERFACE - lokinet-layer-onion - lokinet-layer-link -) - - -# per component external deps - -target_link_libraries(lokinet-config PUBLIC oxenmq::oxenmq) -target_link_libraries(lokinet-platform PUBLIC oxenmq::oxenmq) -target_link_libraries(lokinet-dns PUBLIC libunbound) - -target_link_libraries(lokinet-cryptography PUBLIC - oxenc::oxenc - sodium -) - -target_link_libraries(lokinet-context PUBLIC - CLI11 - oxenmq::oxenmq - uvw -) - -target_link_libraries(lokinet-platform PUBLIC - Threads::Threads - base_libs - uvw -) - -target_link_libraries(lokinet-util PUBLIC - nlohmann_json::nlohmann_json -# filesystem - oxenc::oxenc -) - -target_link_libraries(lokinet-layer-link PUBLIC - quic - uvw -) - -if(WITH_EMBEDDED_LOKINET) - include(GNUInstallDirs) - add_library(lokinet-shared SHARED lokinet_shared.cpp) - target_link_libraries(lokinet-shared PUBLIC lokinet-amalgum) - if(WIN32) - set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "") - endif() - set_target_properties(lokinet-shared PROPERTIES OUTPUT_NAME lokinet) - if(WIN32) - target_link_libraries(lokinet-shared PUBLIC ws2_32 iphlpapi -fstack-protector) - install(TARGETS lokinet-shared DESTINATION bin COMPONENT liblokinet) - elseif(NOT APPLE) - install(TARGETS lokinet-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT liblokinet) - endif() -endif() - -file(GLOB_RECURSE docs_SRC */*.hpp *.hpp) -set(DOCS_SRC ${docs_SRC} PARENT_SCOPE) diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 692f8521b..ee357a3cf 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -184,6 +184,7 @@ namespace llarp defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params); }; + // TODO: remove oxenmq from this header struct ApiConfig { bool m_enableRPCServer = false; diff --git a/llarp/config/key_manager.cpp b/llarp/config/key_manager.cpp index ae63a68f5..96b73fb79 100644 --- a/llarp/config/key_manager.cpp +++ b/llarp/config/key_manager.cpp @@ -100,9 +100,7 @@ namespace llarp } // load encryption key - auto encryptionKeygen = [](llarp::SecretKey& key) { - llarp::crypto::encryption_keygen(key); - }; + auto encryptionKeygen = [](llarp::SecretKey& key) { llarp::crypto::encryption_keygen(key); }; if (not loadOrCreateKey(m_encKeyPath, encryptionKey, encryptionKeygen)) return false; diff --git a/llarp/crypto/crypto.cpp b/llarp/crypto/crypto.cpp index 79b71ab8b..9ef4a3a42 100644 --- a/llarp/crypto/crypto.cpp +++ b/llarp/crypto/crypto.cpp @@ -109,7 +109,7 @@ namespace llarp } std::optional> - crypto::maybe_decrypt_name(std::string_view ciphertext, SymmNonce nounce, std::string_view name) + crypto::maybe_decrypt_name(std::string_view ciphertext, SymmNonce nonce, std::string_view name) { const auto payloadsize = ciphertext.size() - crypto_aead_xchacha20poly1305_ietf_ABYTES; if (payloadsize != 32) @@ -132,7 +132,7 @@ namespace llarp ciphertext.size(), nullptr, 0, - nounce.data(), + nonce.data(), derivedKey.data()) == -1) { diff --git a/llarp/crypto/crypto.hpp b/llarp/crypto/crypto.hpp index 2bb41122c..2aae6a7ec 100644 --- a/llarp/crypto/crypto.hpp +++ b/llarp/crypto/crypto.hpp @@ -19,7 +19,7 @@ namespace llarp { /// decrypt cipherText given the key generated from name std::optional> - maybe_decrypt_name(std::string_view ciphertext, SymmNonce nounce, std::string_view name); + maybe_decrypt_name(std::string_view ciphertext, SymmNonce nonce, std::string_view name); /// xchacha symmetric cipher bool @@ -117,7 +117,7 @@ namespace llarp bool check_passwd_hash(std::string pwhash, std::string challenge); - }; + }; // namespace crypto /// return random 64bit unsigned interger uint64_t diff --git a/llarp/crypto/encrypted_frame.cpp b/llarp/crypto/encrypted_frame.cpp index d8ca8c088..7171c5eb2 100644 --- a/llarp/crypto/encrypted_frame.cpp +++ b/llarp/crypto/encrypted_frame.cpp @@ -16,8 +16,8 @@ namespace llarp if (noDH) { - crypto::randbytes(nonce_ptr, TUNNONCESIZE); - crypto::randbytes(pubkey_ptr, PUBKEYSIZE); + crypto::randbytes(nonce_ptr, TUNNONCESIZE); + crypto::randbytes(pubkey_ptr, PUBKEYSIZE); } TunnelNonce nonce(nonce_ptr); diff --git a/llarp/crypto/types.cpp b/llarp/crypto/types.cpp index 9cf1c3159..bf375eda7 100644 --- a/llarp/crypto/types.cpp +++ b/llarp/crypto/types.cpp @@ -4,9 +4,6 @@ #include #include -// #include -// #include - namespace llarp { bool diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index a20c84fd2..92bdd64f9 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -220,15 +220,14 @@ namespace llarp using PQKeyPair = AlignedBuffer; /// PKE(result, publickey, secretkey, nonce) - using path_dh_func = - bool(*)(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&); + using path_dh_func = bool (*)(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&); /// TKE(result, publickey, secretkey, nonce) using transport_dh_func = - bool(*)(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&); + bool (*)(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&); /// SH(result, body) - using shorthash_func = bool(*)(ShortHash&, const llarp_buffer_t&); + using shorthash_func = bool (*)(ShortHash&, const llarp_buffer_t&); } // namespace llarp namespace std diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index a82c1fc72..20578e510 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -10,10 +10,8 @@ namespace llarp::handlers { - ExitEndpoint::ExitEndpoint(std::string name, Router* r) - : router(r) - , name(std::move(name)) - // , tunnel_manager{std::make_shared(*this)} + ExitEndpoint::ExitEndpoint(std::string name, Router* r) : router(r), name(std::move(name)) + // , tunnel_manager{std::make_shared(*this)} { should_init_tun = true; } diff --git a/llarp/link/tunnel.hpp b/llarp/link/tunnel.hpp index 4825af186..4d5abca8e 100644 --- a/llarp/link/tunnel.hpp +++ b/llarp/link/tunnel.hpp @@ -11,7 +11,7 @@ #include #include -#include +// #include namespace llarp { @@ -157,12 +157,12 @@ namespace llarp::link // Callback to invoke on quic connection established (true argument) or failed (false arg) OpenCallback open_cb; // TCP listening socket - std::shared_ptr tcp; - // Accepted TCP connections - std::unordered_set> conns; - // Queue of incoming connections that are waiting for a stream to become available (either - // because we are still handshaking, or we reached the stream limit). - std::queue> pending_incoming; + // std::shared_ptr tcp; + // // Accepted TCP connections + // std::unordered_set> conns; + // // Queue of incoming connections that are waiting for a stream to become available (either + // // because we are still handshaking, or we reached the stream limit). + // std::queue> pending_incoming; ~ClientTunnel() { @@ -225,8 +225,8 @@ namespace llarp::link std::map incoming_handlers_; int next_handler_id_ = 1; - std::shared_ptr - get_loop(); + // std::shared_ptr + // get_loop(); // Cleanup member std::shared_ptr timer_keepalive_ = std::make_shared(0); diff --git a/llarp/messages/exit.hpp b/llarp/messages/exit.hpp index 20d1084eb..de53a01d7 100644 --- a/llarp/messages/exit.hpp +++ b/llarp/messages/exit.hpp @@ -27,8 +27,7 @@ namespace llarp btdp.append("E", flag); btdp.append("T", tx_id); - if (not crypto::sign( - reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) + if (not crypto::sign(reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) throw std::runtime_error{ "Error: ObtainExitMessage failed to sign and serialize contents!"}; } @@ -51,8 +50,7 @@ namespace llarp btdp.append("T", tx_id); btdp.append("Y", nonce); - if (crypto::sign( - reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) + if (crypto::sign(reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) throw std::runtime_error{ "Error: ObtainExitMessage response failed to sign and serialize contents!"}; } @@ -80,8 +78,7 @@ namespace llarp btdp.append("P", path_id); btdp.append("T", tx_id); - if (not crypto::sign( - reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) + if (not crypto::sign(reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) throw std::runtime_error{ "Error: UpdateExitMessage failed to sign and serialize contents!"}; } @@ -104,8 +101,7 @@ namespace llarp btdp.append("T", tx_id); btdp.append("Y", nonce); - if (crypto::sign( - reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) + if (crypto::sign(reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) throw std::runtime_error{ "Error: UpdateExitMessage response failed to sign and serialize contents!"}; } @@ -134,8 +130,7 @@ namespace llarp btdp.append("T", tx_id); btdp.append("Y", nonce); - if (not crypto::sign( - reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) + if (not crypto::sign(reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) throw std::runtime_error{ "Error: CloseExitMessage failed to sign and serialize contents!"}; } @@ -158,8 +153,7 @@ namespace llarp btdp.append("T", tx_id); btdp.append("Y", nonce); - if (crypto::sign( - reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) + if (crypto::sign(reinterpret_cast(sig.data()), sk, to_usv(btdp.view()))) throw std::runtime_error{ "Error: CloseExitMessage response failed to sign and serialize contents!"}; } diff --git a/llarp/messages/path.hpp b/llarp/messages/path.hpp index 053b96702..e4885c620 100644 --- a/llarp/messages/path.hpp +++ b/llarp/messages/path.hpp @@ -18,7 +18,7 @@ namespace llarp setup_hop_keys(path::PathHopConfig& hop, const RouterID& nextHop) { // generate key - crypto::encryption_keygen(hop.commkey); + crypto::encryption_keygen(hop.commkey); hop.nonce.Randomize(); // do key exchange diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index 0e681455f..a228516c7 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -25,9 +25,7 @@ namespace llarp::path bool TransitHop::send_path_control_message( - std::string, - std::string, - std::function) + std::string, std::string, std::function) { return true; } diff --git a/llarp/pow.cpp b/llarp/pow.cpp index 5012ec394..b0ae27898 100644 --- a/llarp/pow.cpp +++ b/llarp/pow.cpp @@ -31,8 +31,7 @@ namespace llarp auto buf = bt_encode(); // hash - if (!crypto::shorthash( - digest, reinterpret_cast(buf.data()), buf.size())) + if (!crypto::shorthash(digest, reinterpret_cast(buf.data()), buf.size())) return false; // check bytes required uint32_t required = std::floor(std::log(extendedLifetime.count())); diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 3d5e439be..e5396af49 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -409,8 +409,7 @@ namespace llarp copy.signature.Zero(); auto bte = copy.bt_encode(); - return crypto::verify( - pubkey, reinterpret_cast(bte.data()), bte.size(), signature); + return crypto::verify(pubkey, reinterpret_cast(bte.data()), bte.size(), signature); } static constexpr std::array obsolete_bootstraps = { diff --git a/llarp/service/async_key_exchange.cpp b/llarp/service/async_key_exchange.cpp index 0def85fac..fbb42423c 100644 --- a/llarp/service/async_key_exchange.cpp +++ b/llarp/service/async_key_exchange.cpp @@ -50,7 +50,8 @@ namespace llarp::service // compute post handshake session key // PKE (A, B, N) SharedSecret sharedSecret; - if (!self->m_LocalIdentity.KeyExchange(crypto::dh_client, sharedSecret, self->m_remote, frame->nonce)) + if (!self->m_LocalIdentity.KeyExchange( + crypto::dh_client, sharedSecret, self->m_remote, frame->nonce)) { LogError("failed to derive x25519 shared key component"); } diff --git a/llarp/service/auth.cpp b/llarp/service/auth.cpp index f68f9e355..71e2f1509 100644 --- a/llarp/service/auth.cpp +++ b/llarp/service/auth.cpp @@ -124,8 +124,7 @@ namespace llarp::service case AuthFileType::eAuthFilePlain: return hash == challenge; case AuthFileType::eAuthFileHashes: - return crypto::check_passwd_hash( - std::move(hash), std::move(challenge)); + return crypto::check_passwd_hash(std::move(hash), std::move(challenge)); default: return false; } diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index b465e8f07..abea6ca49 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -6,214 +6,211 @@ #include "endpoint.hpp" -namespace llarp +namespace llarp::service { - namespace service + static auto logcat = log::Cat("service"); + namespace { - static auto logcat = log::Cat("service"); - namespace - { - using EndpointConstructor = std::function; - using EndpointConstructors = std::map; - - static EndpointConstructors endpointConstructors = { - {"tun", - [](Router* r, service::Context* c) { - return std::make_shared(r, c); - }}, - {"android", - [](Router* r, service::Context* c) { - return std::make_shared(r, c); - }}, - {"ios", - [](Router* r, service::Context* c) { - return std::make_shared(r, c); - }}, - {"null", [](Router* r, service::Context* c) { - return std::make_shared(r, c); - }}}; - - } // namespace - Context::Context(Router* r) : m_Router(r) - {} - - Context::~Context() = default; - - bool - Context::StopAll() + using EndpointConstructor = std::function; + using EndpointConstructors = std::map; + + static EndpointConstructors endpointConstructors = { + {"tun", + [](Router* r, service::Context* c) { + return std::make_shared(r, c); + }}, + {"android", + [](Router* r, service::Context* c) { + return std::make_shared(r, c); + }}, + {"ios", + [](Router* r, service::Context* c) { + return std::make_shared(r, c); + }}, + {"null", [](Router* r, service::Context* c) { + return std::make_shared(r, c); + }}}; + + } // namespace + Context::Context(Router* r) : m_Router(r) + {} + + Context::~Context() = default; + + bool + Context::StopAll() + { + auto itr = m_Endpoints.begin(); + while (itr != m_Endpoints.end()) { - auto itr = m_Endpoints.begin(); - while (itr != m_Endpoints.end()) - { - log::debug(logcat, "Stopping endpoint {}.", itr->first); - itr->second->Stop(); - log::debug(logcat, "Endpoint {} stopped.", itr->first); - m_Stopped.emplace_back(std::move(itr->second)); - itr = m_Endpoints.erase(itr); - } - return true; + log::debug(logcat, "Stopping endpoint {}.", itr->first); + itr->second->Stop(); + log::debug(logcat, "Endpoint {} stopped.", itr->first); + m_Stopped.emplace_back(std::move(itr->second)); + itr = m_Endpoints.erase(itr); } + return true; + } - util::StatusObject - Context::ExtractStatus() const - { - util::StatusObject obj{}; - auto itr = m_Endpoints.begin(); - while (itr != m_Endpoints.end()) - { - obj[itr->first] = itr->second->ExtractStatus(); - ++itr; - } - return obj; - } - - void - Context::ForEachService( - std::function&)> visit) const + util::StatusObject + Context::ExtractStatus() const + { + util::StatusObject obj{}; + auto itr = m_Endpoints.begin(); + while (itr != m_Endpoints.end()) { - auto itr = m_Endpoints.begin(); - while (itr != m_Endpoints.end()) - { - if (visit(itr->first, itr->second)) - ++itr; - else - return; - } + obj[itr->first] = itr->second->ExtractStatus(); + ++itr; } + return obj; + } - void - Context::Pump() + void + Context::ForEachService( + std::function&)> visit) const + { + auto itr = m_Endpoints.begin(); + while (itr != m_Endpoints.end()) { - auto now = time_now_ms(); - for (auto& [name, endpoint] : m_Endpoints) - endpoint->Pump(now); + if (visit(itr->first, itr->second)) + ++itr; + else + return; } + } - bool - Context::RemoveEndpoint(const std::string& name) - { - auto itr = m_Endpoints.find(name); - if (itr == m_Endpoints.end()) - return false; - std::shared_ptr ep = std::move(itr->second); - m_Endpoints.erase(itr); - ep->Stop(); - m_Stopped.emplace_back(std::move(ep)); - return true; - } + void + Context::Pump() + { + auto now = time_now_ms(); + for (auto& [name, endpoint] : m_Endpoints) + endpoint->Pump(now); + } - void - Context::Tick(llarp_time_t now) + bool + Context::RemoveEndpoint(const std::string& name) + { + auto itr = m_Endpoints.find(name); + if (itr == m_Endpoints.end()) + return false; + std::shared_ptr ep = std::move(itr->second); + m_Endpoints.erase(itr); + ep->Stop(); + m_Stopped.emplace_back(std::move(ep)); + return true; + } + + void + Context::Tick(llarp_time_t now) + { + // erase stopped endpoints that are done { - // erase stopped endpoints that are done + auto itr = m_Stopped.begin(); + while (itr != m_Stopped.end()) { - auto itr = m_Stopped.begin(); - while (itr != m_Stopped.end()) - { - if ((*itr)->ShouldRemove()) - itr = m_Stopped.erase(itr); - else - ++itr; - } - } - // tick active endpoints - for (const auto& item : m_Endpoints) - { - item.second->Tick(now); + if ((*itr)->ShouldRemove()) + itr = m_Stopped.erase(itr); + else + ++itr; } } - - bool - Context::hasEndpoints() + // tick active endpoints + for (const auto& item : m_Endpoints) { - return m_Endpoints.size() ? true : false; + item.second->Tick(now); } + } - static const char* - DefaultEndpointType() - { + bool + Context::hasEndpoints() + { + return m_Endpoints.size() ? true : false; + } + + static const char* + DefaultEndpointType() + { #ifdef ANDROID - return "android"; + return "android"; #else #ifdef IOS - return "ios"; + return "ios"; #else - return "tun"; + return "tun"; #endif #endif - } + } - bool - Context::StartAll() + bool + Context::StartAll() + { + auto itr = m_Endpoints.begin(); + while (itr != m_Endpoints.end()) { - auto itr = m_Endpoints.begin(); - while (itr != m_Endpoints.end()) + if (!itr->second->Start()) { - if (!itr->second->Start()) - { - LogError(itr->first, " failed to start"); - return false; - } - LogInfo(itr->first, " started"); - ++itr; + LogError(itr->first, " failed to start"); + return false; } - return true; - } - - Endpoint_ptr - Context::GetEndpointByName(const std::string& name) const - { - auto itr = m_Endpoints.find(name); - if (itr != m_Endpoints.end()) - return itr->second; - return nullptr; + LogInfo(itr->first, " started"); + ++itr; } + return true; + } - void - Context::InjectEndpoint(std::string name, std::shared_ptr ep) + Endpoint_ptr + Context::GetEndpointByName(const std::string& name) const + { + auto itr = m_Endpoints.find(name); + if (itr != m_Endpoints.end()) + return itr->second; + return nullptr; + } + + void + Context::InjectEndpoint(std::string name, std::shared_ptr ep) + { + ep->LoadKeyFile(); + if (ep->Start()) { - ep->LoadKeyFile(); - if (ep->Start()) - { - m_Endpoints.emplace(std::move(name), std::move(ep)); - } + m_Endpoints.emplace(std::move(name), std::move(ep)); } + } - void - Context::AddEndpoint(const Config& conf, bool autostart) - { - constexpr auto endpointName = "default"; - - if (m_Endpoints.find(endpointName) != m_Endpoints.end()) - throw std::invalid_argument("service::Context only supports one endpoint now"); + void + Context::AddEndpoint(const Config& conf, bool autostart) + { + constexpr auto endpointName = "default"; - const auto& endpointType = conf.network.m_endpointType; - // use factory to create endpoint - const auto itr = endpointConstructors.find(endpointType); - if (itr == endpointConstructors.end()) - throw std::invalid_argument{fmt::format("Endpoint type {} does not exist", endpointType)}; + if (m_Endpoints.find(endpointName) != m_Endpoints.end()) + throw std::invalid_argument("service::Context only supports one endpoint now"); - auto service = itr->second(m_Router, this); - if (not service) - throw std::runtime_error{ - fmt::format("Failed to construct endpoint of type {}", endpointType)}; + const auto& endpointType = conf.network.m_endpointType; + // use factory to create endpoint + const auto itr = endpointConstructors.find(endpointType); + if (itr == endpointConstructors.end()) + throw std::invalid_argument{fmt::format("Endpoint type {} does not exist", endpointType)}; - // pass conf to service - service->Configure(conf.network, conf.dns); + auto service = itr->second(m_Router, this); + if (not service) + throw std::runtime_error{ + fmt::format("Failed to construct endpoint of type {}", endpointType)}; - if (not service->LoadKeyFile()) - throw std::runtime_error("Endpoint's keyfile could not be loaded"); + // pass conf to service + service->Configure(conf.network, conf.dns); - // autostart if requested - if (autostart) - { - if (service->Start()) - LogInfo("autostarting hidden service endpoint ", service->Name()); - else - throw std::runtime_error("failed to start hidden service endpoint"); - } + if (not service->LoadKeyFile()) + throw std::runtime_error("Endpoint's keyfile could not be loaded"); - m_Endpoints.emplace(endpointName, service); + // autostart if requested + if (autostart) + { + if (service->Start()) + LogInfo("autostarting hidden service endpoint ", service->Name()); + else + throw std::runtime_error("failed to start hidden service endpoint"); } - } // namespace service -} // namespace llarp + + m_Endpoints.emplace(endpointName, service); + } +} // namespace llarp::service diff --git a/llarp/service/identity.cpp b/llarp/service/identity.cpp index 9cfdf45df..3452146b5 100644 --- a/llarp/service/identity.cpp +++ b/llarp/service/identity.cpp @@ -168,8 +168,7 @@ namespace llarp::service auto bte = i.bt_encode(); const SharedSecret k{i.address_keys.Addr()}; - crypto::xchacha20( - reinterpret_cast(bte.data()), bte.size(), k, encrypted.nounce); + crypto::xchacha20(reinterpret_cast(bte.data()), bte.size(), k, encrypted.nounce); std::memcpy(encrypted.introsetPayload.data(), bte.data(), bte.size()); diff --git a/llarp/service/intro_set.cpp b/llarp/service/intro_set.cpp index 11dd6f999..bc2db09da 100644 --- a/llarp/service/intro_set.cpp +++ b/llarp/service/intro_set.cpp @@ -123,8 +123,7 @@ namespace llarp::service std::string payload{ reinterpret_cast(introsetPayload.data()), introsetPayload.size()}; - crypto::xchacha20( - reinterpret_cast(payload.data()), payload.size(), k, nounce); + crypto::xchacha20(reinterpret_cast(payload.data()), payload.size(), k, nounce); return IntroSet{payload}; } @@ -144,8 +143,7 @@ namespace llarp::service sig.Zero(); auto bte = bt_encode(); - if (not crypto::sign( - sig, k, reinterpret_cast(bte.data()), bte.size())) + if (not crypto::sign(sig, k, reinterpret_cast(bte.data()), bte.size())) return false; LogDebug("signed encrypted introset: ", *this); return true; diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index a21a4b9b9..1ec6783a6 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -207,8 +207,7 @@ namespace llarp::service // encode message auto bte1 = msg.bt_encode(); // encrypt - crypto::xchacha20( - reinterpret_cast(bte1.data()), bte1.size(), sessionKey, nonce); + crypto::xchacha20(reinterpret_cast(bte1.data()), bte1.size(), sessionKey, nonce); // put encrypted buffer std::memcpy(enc.data(), bte1.data(), bte1.size()); // zero out signature