diff --git a/.clang-tidy b/.clang-tidy index 5d635f09e..234f1e1d9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,2 +1,2 @@ HeaderFilterRegex: 'llarp/.*' -Checks: 'readability-else-after-return,clang-analyzer-core-*' +Checks: 'readability-else-after-return,clang-analyzer-core-*,modernize-*' diff --git a/.travis.yml b/.travis.yml index a2a4566e4..773160b02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,16 @@ cache: ccache matrix: fast_finish: true include: + - os: linux + dist: xenial + compiler: gcc + env: MAKE_TARGET=format-verify PATH="/usr/lib/llvm-8/bin:$PATH" + addons: + apt: + sources: + - llvm-toolchain-xenial-8 + packages: + - clang-format-8 - os: linux dist: xenial compiler: gcc @@ -47,6 +57,12 @@ matrix: - os: osx osx_image: xcode10.2 env: BUILD_TYPE=Release PATH="/usr/local/opt/ccache/libexec:$PATH" + - os: osx + osx_image: xcode10.2 + env: MAKE_TARGET=windows + - os: osx + osx_image: xcode10.2 + env: MAKE_TARGET=windows-release - os: osx osx_image: xcode11 env: BUILD_TYPE=Release PATH="/usr/local/opt/ccache/libexec:$PATH" @@ -66,6 +82,12 @@ matrix: dist: xenial env: DOCKER_FILE=docker/gcc-trunk.Dockerfile services: docker + - os: osx + osx_image: xcode10.2 + env: MAKE_TARGET=windows + - os: osx + osx_image: xcode10.2 + env: MAKE_TARGET=windows-release env: global: @@ -77,6 +99,7 @@ addons: packages: - binutils-gold - build-essential + - clang-format - cmake - curl - docker-ce @@ -94,8 +117,10 @@ addons: - cmake - libuv - llvm - - ninja - make + - mingw-w64 + - ninja + update: true before_install: - if [ "$TRAVIS_OS_NAME" == "windows" ]; then diff --git a/CMakeLists.txt b/CMakeLists.txt index 428b39950..f9cadc03e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,22 +173,21 @@ if(STATIC_LINK_RUNTIME) else() add_compile_options(-static) endif() + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") link_libraries( -static -static-libstdc++ -pthread -flto ) + elseif(NOT CMAKE_CROSSCOMPILING) + # this is messing with release builds + add_compile_options(-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0) + set(CMAKE_AR "gcc-ar") + set(CMAKE_C_ARCHIVE_CREATE " qcs ") + set(CMAKE_C_ARCHIVE_FINISH "true") + set(CMAKE_CXX_ARCHIVE_CREATE " qcs ") + set(CMAKE_CXX_ARCHIVE_FINISH "true") + link_libraries( -flto -static-libstdc++ -static-libgcc -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive ) else() - if(NOT CMAKE_CROSSCOMPILING) - # this is messing with release builds - add_compile_options(-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0) - set(CMAKE_AR "gcc-ar") - set(CMAKE_C_ARCHIVE_CREATE " qcs ") - set(CMAKE_C_ARCHIVE_FINISH "true") - set(CMAKE_CXX_ARCHIVE_CREATE " qcs ") - set(CMAKE_CXX_ARCHIVE_FINISH "true") - link_libraries( -flto -static-libstdc++ -static-libgcc -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive ) - else() - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive" ) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") - endif() + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive" ) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") endif() else() link_libraries( -static-libstdc++ -static-libgcc ) @@ -286,15 +285,20 @@ if(SHADOW) else() if(NOT WIN32) add_executable(${EXE} ${EXE_SRC}) + add_executable(lokinet-rcutil daemon/rcutil.cpp) elseif(NOT MSVC_VERSION) add_executable(${EXE} ${EXE_SRC} llarp/win32/version.rc) + add_executable(lokinet-rcutil daemon/rcutil.cpp llarp/win32/version.rc) else() add_executable(${EXE} ${EXE_SRC}) + add_executable(lokinet-rcutil daemon/rcutil.cpp) endif(NOT WIN32) add_log_tag(${EXE}) + add_log_tag(lokinet-rcutil) install(TARGETS ${EXE} RUNTIME DESTINATION bin) + install(TARGETS lokinet-rcutil RUNTIME DESTINATION bin) if(WIN32) install(PROGRAMS ${CMAKE_SOURCE_DIR}/lokinet-bootstrap.exe DESTINATION bin) else() @@ -306,8 +310,10 @@ else() endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") target_link_directories(${EXE} PRIVATE /usr/local/lib) + target_link_directories(lokinet-rcutil PRIVATE /usr/local/lib) endif() target_link_libraries(${EXE} PUBLIC ${EXE_LIBS} ${LIBS}) + target_link_libraries(lokinet-rcutil PUBLIC ${EXE_LIBS} ${LIBS}) if(ANDROID) add_library(${ANDROID_LIB} SHARED jni/lokinet_android.cpp) diff --git a/Makefile b/Makefile index 6f2e3b591..274d530e0 100644 --- a/Makefile +++ b/Makefile @@ -243,6 +243,11 @@ abyss: debug format: clang-format -i $$(find jni daemon llarp include libabyss | grep -E '\.[h,c](pp)?$$') +format-verify: format + (type clang-format) + clang-format --version + git diff --quiet || (echo 'Please run make format!!' && git --no-pager diff ; exit 1) + analyze-config: clean mkdir -p '$(BUILD_ROOT)' $(ANALYZE_CONFIG_CMD) diff --git a/cmake/cross_compile.cmake b/cmake/cross_compile.cmake index 1539f3b19..7b3e74799 100644 --- a/cmake/cross_compile.cmake +++ b/cmake/cross_compile.cmake @@ -4,8 +4,10 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wno-unused-command-line-argument -Wno-c++11-narrowing) add_compile_options($<$:-Wno-bad-function-cast>) if (NO_LIBGCC) - set(CMAKE_CXX_STANDARD_LIBRARIES "-lunwind -lpsapi ${CMAKE_CXX_STANDARD_LIBRARIES}") - set(CMAKE_C_STANDARD_LIBRARIES "-lunwind -lpsapi ${CMAKE_C_STANDARD_LIBRARIES}") + find_library(UNWIND_LIB unwind) + link_libraries(${UNWIND_LIB}) + find_library(PSAPI_LIB psapi) + link_libraries(${PSAPI_LIB}) endif(NO_LIBGCC) else() # found it. this is GNU only diff --git a/cmake/win32.cmake b/cmake/win32.cmake index 31f826968..52956bd99 100644 --- a/cmake/win32.cmake +++ b/cmake/win32.cmake @@ -8,6 +8,10 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) if (MSVC OR MSVC_VERSION) add_compile_options(/EHca /arch:AVX2 /MD) add_definitions(-D_SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING) + + if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" AND "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC") + add_compile_options(-Wno-nonportable-system-include-path) + endif() endif() if(NOT MSVC_VERSION) @@ -17,7 +21,9 @@ if(NOT MSVC_VERSION) # GNU ld sees fit to merge *all* the .ident sections in object files # to .r[o]data section one after the other! add_compile_options(-fno-ident -Wa,-mbig-obj) - link_libraries( -lshlwapi -ldbghelp ) + find_library(shlwapi_lib shlwapi) + find_library(dbghelp_lib dbghelp) + link_libraries( ${shlwapi_lib} ${dbghelp_lib} ) add_definitions(-DWINVER=0x0500 -D_WIN32_WINNT=0x0500) # Wait a minute, if we're not Microsoft C++, nor a Clang paired with Microsoft C++, # then the only possible option has to be GNU or a GNU-linked Clang! @@ -29,7 +35,10 @@ endif() get_filename_component(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-windows.c ABSOLUTE) get_filename_component(EV_SRC "llarp/ev/ev_win32.cpp" ABSOLUTE) add_definitions(-DWIN32_LEAN_AND_MEAN -DWIN32 -DWINVER=0x0500) -set(EXE_LIBS ${STATIC_LIB} ${FS_LIB} ws2_32 iphlpapi) + +find_library(ws2_32_lib ws2_32) +find_library(iphlpapi_lib iphlpapi) +set(EXE_LIBS ${STATIC_LIB} ${FS_LIB} ${ws2_32_lib} ${iphlpapi_lib}) if(RELEASE_MOTTO) add_definitions(-DLLARP_RELEASE_MOTTO="${RELEASE_MOTTO}") diff --git a/contrib/cross/mingw.cmake b/contrib/cross/mingw.cmake index 9521c8009..0a591fb53 100644 --- a/contrib/cross/mingw.cmake +++ b/contrib/cross/mingw.cmake @@ -1,28 +1,16 @@ set(CMAKE_SYSTEM_NAME Windows) set(TOOLCHAIN_PREFIX x86_64-w64-mingw32) set(TOOLCHAIN_SUFFIX "") - -add_definitions("-DWINNT_CROSS_COMPILE") - -# target environment on the build host system -# second one is for non-root installs -set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /usr/local/opt/mingw-w64/toolchain-x86_64/ /opt/mingw64 /home/$ENV{USER}/mingw32 /home/$ENV{USER}/mingw64 /home/$ENV{USER}/mingw64/${TOOLCHAIN_PREFIX} /home/$ENV{USER}/mingw32/${TOOLCHAIN_PREFIX}) - -# modify default behavior of FIND_XXX() commands -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) - -# cross compilers to use -if($ENV{COMPILER} MATCHES "clang") - set(USING_CLANG ON) - set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-clang) - set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-clang++) -else() - set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc${TOOLCHAIN_SUFFIX}) - set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++${TOOLCHAIN_SUFFIX}) - add_compile_options("-Wa,-mbig-obj") -endif() - -set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) set(WIN64_CROSS_COMPILE ON) + +set(TOOLCHAIN_PATHS + /usr/${TOOLCHAIN_PREFIX} + /usr/local/opt/mingw-w64/toolchain-x86_64 + /usr/local/opt/mingw-w64/toolchain-x86_64/x86_64-w64-mingw32 + /opt/mingw64 + /home/$ENV{USER}/mingw32 + /home/$ENV{USER}/mingw64 + /home/$ENV{USER}/mingw64/${TOOLCHAIN_PREFIX} + /home/$ENV{USER}/mingw32/${TOOLCHAIN_PREFIX}) + +include("${CMAKE_CURRENT_LIST_DIR}/mingw_core.cmake") diff --git a/contrib/cross/mingw32.cmake b/contrib/cross/mingw32.cmake index 6a853511f..3e1e490ff 100644 --- a/contrib/cross/mingw32.cmake +++ b/contrib/cross/mingw32.cmake @@ -1,26 +1,15 @@ set(CMAKE_SYSTEM_NAME Windows) set(TOOLCHAIN_PREFIX i686-w64-mingw32) - -add_definitions("-DWINNT_CROSS_COMPILE") - -# target environment on the build host system -# second one is for non-root installs -set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /opt/mingw32 /home/$ENV{USER}/mingw32 /home/$ENV{USER}/mingw32/${TOOLCHAIN_PREFIX}) - -# modify default behavior of FIND_XXX() commands -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) - -# cross compilers to use -if($ENV{COMPILER} MATCHES "clang") -set(USING_CLANG ON) -set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-clang) -set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-clang++) -else() -set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) -set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) -endif() - -set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) +set(TOOLCHAIN_SUFFIX "") set(WOW64_CROSS_COMPILE ON) + +set(TOOLCHAIN_PATHS + /usr/${TOOLCHAIN_PREFIX} + /usr/local/opt/mingw-w64/toolchain-i686 + /usr/local/opt/mingw-w64/toolchain-i686/i686-w64-mingw32 + /opt/mingw32 + /home/$ENV{USER}/mingw32 + /home/$ENV{USER}/mingw32/${TOOLCHAIN_PREFIX} +) + +include("${CMAKE_CURRENT_LIST_DIR}/mingw_core.cmake") diff --git a/contrib/cross/mingw_core.cmake b/contrib/cross/mingw_core.cmake new file mode 100644 index 000000000..c636e7ba5 --- /dev/null +++ b/contrib/cross/mingw_core.cmake @@ -0,0 +1,24 @@ +# target environment on the build host system +# second one is for non-root installs +set(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_PATHS}) + +add_definitions("-DWINNT_CROSS_COMPILE") + +# modify default behavior of FIND_XXX() commands +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + +# cross compilers to use +if($ENV{COMPILER} MATCHES "clang") + set(USING_CLANG ON) + set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-clang) + set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-clang++) +else() + set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc${TOOLCHAIN_SUFFIX}) + set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++${TOOLCHAIN_SUFFIX}) + add_compile_options("-Wa,-mbig-obj") +endif() + +set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) diff --git a/contrib/py/admin/.gitignore b/contrib/py/admin/.gitignore new file mode 100644 index 000000000..6f53c74ca --- /dev/null +++ b/contrib/py/admin/.gitignore @@ -0,0 +1 @@ +v/ \ No newline at end of file diff --git a/contrib/py/admin/lokinetmon b/contrib/py/admin/lokinetmon new file mode 100755 index 000000000..1c4258101 --- /dev/null +++ b/contrib/py/admin/lokinetmon @@ -0,0 +1,266 @@ +#!/usr/bin/env python3 +import requests +import json +import time +import curses +import math +import traceback + + +class Monitor: + + _speedSamples = 8 + _globalspeed = [] + + def __init__(self, url): + self.data = dict() + self.win = curses.initscr() + self._url = url + while len(self._globalspeed) < self._speedSamples: + self._globalspeed.append((0, 0)) + + def __del__(self): + curses.endwin() + + def on_timer(self, event): + """called on timer event""" + self.update_data() + + def jsonrpc(self, meth, params): + r = requests.post( + self._url, + headers={"Content-Type": "application/json"}, + json={ + "jsonrpc": "2.0", + "id": "0", + "method": "{}".format(meth), + "params": params, + }, + ) + return r.json() + + def update_data(self): + """update data from lokinet""" + try: + j = self.jsonrpc("llarp.admin.dumpstate", {}) + self.data = j["result"] + except Exception as ex: + self.data = None + + def _render_path(self, y, path, name): + """render a path at current position""" + self.win.move(y, 1) + self.win.addstr("({}) ".format(name)) + y += 1 + self.win.move(y, 1) + y += 1 + self.win.addstr("me -> ") + for hop in path["hops"]: + self.win.addstr(" {} ->".format(hop["router"][:4])) + self.win.addstr(" [{} ms latency]".format(path["intro"]["latency"])) + self.win.addstr(" [{} until expire]".format(self.timeTo(path["expiresAt"]))) + if path["expiresSoon"]: + self.win.addstr("(expiring)") + elif path["expired"]: + self.win.addstr("(expired)") + return y + + def timeTo(self, ts): + """ return time until timestamp in seconds formatted""" + now = time.time() * 1000 + return "{} seconds".format(int((ts - now) / 1000)) + + def speedOf(self, rate): + """turn int speed into string formatted""" + units = ["B", "KB", "MB", "GB"] + idx = 0 + while rate > 1000 and idx < len(units): + rate /= 1000.0 + idx += 1 + return "{} {}ps".format("%.2f" % rate, units[idx]) + + def display_service(self, y, name, status): + """display a service at current position""" + self.win.move(y, 1) + self.win.addstr("service [{}]".format(name)) + build = status["buildStats"] + ratio = build["success"] / (build["attempts"] or 1) + y += 1 + self.win.move(y, 1) + self.win.addstr("build success: {} %".format(int(100 * ratio))) + y += 1 + self.win.move(y, 1) + paths = status["paths"] + self.win.addstr("paths: {}".format(len(paths))) + for path in paths: + y = self._render_path(y, path, "inbound") + for session in status["remoteSessions"]: + for path in session["paths"]: + y = self._render_path( + y, path, "[active] {}".format(session["currentConvoTag"]) + ) + for session in status["snodeSessions"]: + for path in session["paths"]: + y = self._render_path(y, path, "[snode]") + return y + + # for k in status: + # self.win.move(y + 1, 1) + # y += 1 + # self.win.addstr('{}: {}'.format(k, json.dumps(status[k]))) + + def display_links(self, y, data): + self.txrate = 0 + self.rxrate = 0 + for link in data["outbound"]: + y += 1 + self.win.move(y, 1) + self.win.addstr("outbound sessions:") + y = self.display_link(y, link) + for link in data["inbound"]: + y += 1 + self.win.move(y, 1) + self.win.addstr("inbound sessions:") + y = self.display_link(y, link) + y += 2 + self.win.move(y, 1) + self.win.addstr( + "global speed:\t\t[{}\ttx]\t[{}\trx]".format( + self.speedOf(self.txrate), self.speedOf(self.rxrate) + ) + ) + + self._globalspeed.append((self.txrate, self.rxrate)) + while len(self._globalspeed) > self._speedSamples: + self._globalspeed.pop(0) + return self.display_speedgraph(y + 2, self._globalspeed) + + def display_speedgraph(self, y, samps, maxsz=20): + """ display global speed graph """ + + def scale(x, n): + while n > 0: + x /= 2 + n -= 1 + return int(x) + + txmax, rxmax = 1000, 1000 + for tx, rx in samps: + if tx > txmax: + txmax = tx + if rx > rxmax: + rxmax = rx + + rxscale = 0 + while rxmax > maxsz: + rxscale += 1 + rxmax /= 2 + + txscale = 0 + while txmax > maxsz: + txscale += 1 + txmax /= 2 + + def makebar(samp, max): + bar = "#" * samp + pad = " " * (max - samp) + return pad, bar + + txlabelpad = int(txmax / 2) - 1 + rxlabelpad = int(rxmax / 2) - 1 + if txlabelpad <= 0: + txlabelpad = 1 + if rxlabelpad <= 0: + rxlabelpad = 1 + txlabelpad = " " * txlabelpad + rxlabelpad = " " * rxlabelpad + y += 1 + self.win.move(y, 1) + self.win.addstr( + "{}tx{}{}rx{}".format(txlabelpad, txlabelpad, rxlabelpad, rxlabelpad) + ) + for tx, rx in samps: + y += 1 + self.win.move(y, 1) + txpad, txbar = makebar(scale(tx, txscale), int(txmax)) + rxpad, rxbar = makebar(scale(rx, rxscale), int(rxmax)) + self.win.addstr("{}{}|{}{}".format(txpad, txbar, rxbar, rxpad)) + return y + 2 + + def display_link(self, y, link): + y += 1 + self.win.move(y, 1) + sessions = link["sessions"]["established"] + for s in sessions: + y += 1 + self.win.move(y, 1) + self.txrate += s["tx"] + self.rxrate += s["rx"] + self.win.addstr( + "{}\t[{}\ttx]\t[{}\trx]".format( + s["remoteAddr"], self.speedOf(s["tx"]), self.speedOf(s["rx"]) + ) + ) + if s["sendBacklog"] > 0: + self.win.addstr("[backlog {}]".format(s["sendBacklog"])) + return y + + def display_dht(self, y, data): + y += 2 + self.win.move(y, 1) + self.win.addstr("DHT:") + y += 1 + self.win.move(y, 1) + self.win.addstr("introset lookups") + y = self.display_bucket(y, data["pendingIntrosetLookups"]) + y += 1 + self.win.move(y, 1) + self.win.addstr("router lookups") + return self.display_bucket(y, data["pendingRouterLookups"]) + + def display_bucket(self, y, data): + txs = data["tx"] + self.win.addstr(" ({} lookups)".format(len(txs))) + for tx in txs: + y += 1 + self.win.move(y, 1) + self.win.addstr("search for {}".format(tx["tx"]["target"])) + return y + + def display_data(self): + """draw main window""" + if self.data is not None: + self.win.addstr(1, 1, "lokinet online") + # print(self.data) + services = self.data["services"] or {} + y = 3 + try: + y = self.display_links(y, self.data["links"]) + for k in services: + y = self.display_service(y, k, services[k]) + y = self.display_dht(y, self.data["dht"]) + except Exception as exc: + pass + else: + self.win.move(1, 1) + self.win.addstr("lokinet offline") + + def run(self): + while True: + self.win.clear() + self.win.box() + self.update_data() + self.display_data() + self.win.refresh() + time.sleep(1) + + +if __name__ == "__main__": + import sys + + mon = Monitor( + "http://{}/jsonrpc".format( + len(sys.argv) > 1 and sys.argv[1] or "127.0.0.1:1190" + ) + ) + mon.run() diff --git a/contrib/py/admin/requirements.txt b/contrib/py/admin/requirements.txt new file mode 100644 index 000000000..663bd1f6a --- /dev/null +++ b/contrib/py/admin/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 380ed7b62..7759032e7 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -1,598 +1,99 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include +#include +#include -#include -#include -#include +#include +#include +#include +#include -struct llarp_main *ctx = 0; - -void -handle_signal(int sig) +bool +dumpRc(const std::vector< std::string >& files, bool json) { - if(ctx) - llarp_main_signal(ctx, sig); -} - -#ifndef TESTNET -#define TESTNET 0 -#endif - -void -displayRC(const llarp::RouterContact &rc) -{ - std::cout << rc.pubkey << std::endl; - for(const auto &addr : rc.addrs) + nlohmann::json result; + for(const auto& file : files) { - std::cout << "AddressInfo: " << addr << std::endl; - } -} + llarp::RouterContact rc; + const bool ret = rc.Read(file.c_str()); -// fwd declr -struct check_online_request; - -void -HandleDHTLocate(llarp_router_lookup_job *job) -{ - llarp::LogInfo("DHT result: ", job->found ? "found" : "not found"); - if(job->found) - { - // save to nodedb? - displayRC(job->result); - } - // shutdown router - - // well because we're in the gotroutermessage, we can't sigint because we'll - // deadlock because we're session locked - // llarp_main_signal(ctx, SIGINT); - - // llarp_timer_run(logic->timer, logic->thread); - // we'll we don't want logic thread - // but we want to switch back to the main thread - // llarp_logic_stop(); - // still need to exit this logic thread... - llarp_main_abort(ctx); -} - -int -main(int argc, char *argv[]) -{ - // take -c to set location of daemon.ini - // take -o to set log level - // --generate-blank /path/to/file.signed - // --update-ifs /path/to/file.signed - // --key /path/to/long_term_identity.key - // --import - // --export - - // --generate /path/to/file.signed - // --update /path/to/file.signed - // --verify /path/to/file.signed - // printf("has [%d]options\n", argc); - if(argc < 2) - { - printf( - "please specify: \n" - "--generate with a path to a router contact file\n" - "--update with a path to a router contact file\n" - "--list path to nodedb skiplist\n" - "--import with a path to a router contact file\n" - "--export a hex formatted public key\n" - "--locate a hex formatted public key\n" - "--find a base32 formatted service address\n" - "--b32 a hex formatted public key\n" - "--hex a base32 formatted public key\n" - "--localInfo \n" - "--read with a path to a router contact file\n" - "--verify with a path to a router contact file\n" - "\n"); - return 0; - } - bool haveRequiredOptions = false; - bool genMode = false; - bool updMode = false; - bool listMode = false; - bool importMode = false; - bool exportMode = false; - bool locateMode = false; - bool findMode = false; - bool localMode = false; - bool verifyMode = false; - bool readMode = false; - bool toHexMode = false; - bool toB32Mode = false; - int c; - char *conffname; - char defaultConfName[] = "daemon.ini"; - conffname = defaultConfName; - char *rcfname = nullptr; - char *nodesdir = nullptr; - - llarp::RouterContact rc; - while(1) - { - static struct option long_options[] = { - {"file", required_argument, 0, 'f'}, - {"config", required_argument, 0, 'c'}, - {"logLevel", required_argument, 0, 'o'}, - {"generate", required_argument, 0, 'g'}, - {"update", required_argument, 0, 'u'}, - {"list", required_argument, 0, 'l'}, - {"import", required_argument, 0, 'i'}, - {"export", required_argument, 0, 'e'}, - {"locate", required_argument, 0, 'q'}, - {"find", required_argument, 0, 'F'}, - {"localInfo", no_argument, 0, 'n'}, - {"read", required_argument, 0, 'r'}, - {"b32", required_argument, 0, 'b'}, - {"hex", required_argument, 0, 'h'}, - {"verify", required_argument, 0, 'V'}, - {0, 0, 0, 0}}; - int option_index = 0; - c = getopt_long(argc, argv, "c:f:o:g:lu:i:e:q:F:nr:b:h:V:", long_options, - &option_index); -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) - if(c == -1) - break; - switch(c) + if(ret) { - case 0: - break; - case 'c': - conffname = optarg; - break; - case 'o': - if(strncmp(optarg, "debug", - MIN(strlen(optarg), static_cast< unsigned long >(5))) - == 0) - { - llarp::SetLogLevel(llarp::eLogDebug); - } - else if(strncmp(optarg, "info", - MIN(strlen(optarg), static_cast< unsigned long >(4))) - == 0) - { - llarp::SetLogLevel(llarp::eLogInfo); - } - else if(strncmp(optarg, "warn", - MIN(strlen(optarg), static_cast< unsigned long >(4))) - == 0) - { - llarp::SetLogLevel(llarp::eLogWarn); - } - else if(strncmp(optarg, "error", - MIN(strlen(optarg), static_cast< unsigned long >(5))) - == 0) - { - llarp::SetLogLevel(llarp::eLogError); - } - break; - case 'V': - rcfname = optarg; - haveRequiredOptions = true; - verifyMode = true; - break; - case 'f': - rcfname = optarg; - break; - case 'l': - nodesdir = optarg; - listMode = true; - break; - case 'i': - // printf ("option -g with value `%s'\n", optarg); - nodesdir = optarg; - importMode = true; - break; - case 'e': - // printf ("option -g with value `%s'\n", optarg); - rcfname = optarg; - exportMode = true; - break; - case 'q': - // printf ("option -g with value `%s'\n", optarg); - rcfname = optarg; - locateMode = true; - break; - case 'F': - rcfname = optarg; - haveRequiredOptions = true; - findMode = true; - break; - case 'g': - // printf ("option -g with value `%s'\n", optarg); - rcfname = optarg; - genMode = true; - break; - case 'u': - // printf ("option -u with value `%s'\n", optarg); - rcfname = optarg; - updMode = true; - break; - case 'n': - localMode = true; - break; - case 'r': - rcfname = optarg; - readMode = true; - break; - case 'b': - rcfname = optarg; - haveRequiredOptions = true; - toB32Mode = true; - break; - case 'h': - rcfname = optarg; - haveRequiredOptions = true; - toHexMode = true; - break; - default: - printf("Bad option: %c\n", c); - return -1; - } - } -#undef MIN - if(!haveRequiredOptions) - { - llarp::LogError("Parameters dont all have their required parameters.\n"); - return 0; - } - // printf("parsed options\n"); - - if(!genMode && !updMode && !listMode && !importMode && !exportMode - && !locateMode && !localMode && !readMode && !findMode && !toB32Mode - && !toHexMode && !verifyMode) - { - llarp::LogError( - "I don't know what to do, no generate or update parameter\n"); - return 1; - } - -#ifdef LOKINET_DEBUG - absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort); -#endif - - llarp::RouterContact tmp; - - if(verifyMode) - { - llarp::Crypto crypto(llarp::Crypto::sodium{}); - if(!rc.Read(rcfname)) - { - std::cout << "failed to read " << rcfname << std::endl; - return 1; - } - if(!rc.Verify(&crypto)) - { - std::cout << rcfname << " is invalid" << std::endl; - return 1; - } - if(!rc.IsPublicRouter()) - { - std::cout << rcfname << " is not a public router"; - if(rc.addrs.size() == 0) + if(json) { - std::cout << " because it has no public addresses"; + result[file] = rc.ToJson(); } - std::cout << std::endl; - return 1; - } - - std::cout << "router identity and dht routing key: " << rc.pubkey - << std::endl; - - std::cout << "router encryption key: " << rc.enckey << std::endl; - - if(rc.HasNick()) - std::cout << "router nickname: " << rc.Nick() << std::endl; - - std::cout << "advertised addresses: " << std::endl; - for(const auto &addr : rc.addrs) - { - std::cout << addr << std::endl; - } - std::cout << std::endl; - - std::cout << "advertised exits: "; - if(rc.exits.size()) - { - for(const auto &exit : rc.exits) + else { - std::cout << exit << std::endl; + std::cout << "file = " << file << "\n"; + std::cout << rc << "\n\n"; } } else { - std::cout << "none"; + std::cerr << "file = " << file << " was not a valid rc file\n"; } - std::cout << std::endl; - return 0; } - ctx = llarp_main_init(conffname, !TESTNET); - if(!ctx) + if(json) + std::cout << result << "\n"; + + return true; +} + +int +main(int argc, char* argv[]) +{ +#ifdef LOKINET_DEBUG + absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort); +#endif + + // clang-format off + cxxopts::Options options( + "lokinet-rcutil", + "LokiNET is a free, open source, private, decentralized, \"market based sybil resistant\" and IP based onion routing network" + ); + + options.add_options() + ("v,verbose", "Verbose", cxxopts::value()) + ("h,help", "help", cxxopts::value()) + ("j,json", "output in json", cxxopts::value()) + ("dump", "dump rc file", cxxopts::value >(), "FILE"); + // clang-format on + + try { - llarp::LogError("Cant set up context"); + const auto result = options.parse(argc, argv); + + const bool json = result["json"].as< bool >(); + + if(result.count("verbose") > 0) + { + SetLogLevel(llarp::eLogDebug); + llarp::LogContext::Instance().logStream = + std::make_unique< llarp::OStreamLogStream >(std::cerr); + llarp::LogDebug("debug logging activated"); + } + + if(result.count("help") > 0) + { + std::cout << options.help() << std::endl; + return 0; + } + + if(result.count("dump") > 0) + { + if(!dumpRc(result["dump"].as< std::vector< std::string > >(), json)) + { + return 1; + } + } + } + catch(const cxxopts::OptionParseException& ex) + { + std::cerr << ex.what() << std::endl; + std::cout << options.help() << std::endl; return 1; } - signal(SIGINT, handle_signal); - // is this Neuro or Jeff's? - // this is the only one... - if(listMode) - { - llarp::Crypto crypto(llarp::Crypto::sodium{}); - auto nodedb = llarp_nodedb_new(&crypto); - llarp_nodedb_iter itr; - itr.visit = [](llarp_nodedb_iter *i) -> bool { - std::cout << i->rc->pubkey << std::endl; - return true; - }; - if(llarp_nodedb_load_dir(nodedb, nodesdir) > 0) - llarp_nodedb_iterate_all(nodedb, itr); - llarp_nodedb_free(&nodedb); - return 0; - } - - if(importMode) - { - if(rcfname == nullptr) - { - std::cout << "no file to import" << std::endl; - return 1; - } - llarp::Crypto crypto(llarp::Crypto::sodium{}); - auto nodedb = llarp_nodedb_new(&crypto); - if(!llarp_nodedb_ensure_dir(nodesdir)) - { - std::cout << "failed to ensure " << nodesdir << strerror(errno) - << std::endl; - return 1; - } - llarp_nodedb_set_dir(nodedb, nodesdir); - if(!rc.Read(rcfname)) - { - std::cout << "failed to read " << rcfname << " " << strerror(errno) - << std::endl; - return 1; - } - - if(!rc.Verify(&crypto)) - { - std::cout << rcfname << " is invalid" << std::endl; - return 1; - } - - if(!llarp_nodedb_put_rc(nodedb, rc)) - { - std::cout << "failed to store " << strerror(errno) << std::endl; - return 1; - } - - std::cout << "imported " << rc.pubkey << std::endl; - - return 0; - } - - if(genMode) - { - printf("Creating [%s]\n", rcfname); - // if we zero it out then - // set updated timestamp - rc.last_updated = llarp::time_now_ms(); - // load longterm identity - llarp::Crypto crypt(llarp::Crypto::sodium{}); - - // which is in daemon.ini config: router.encryption-privkey (defaults - // "encryption.key") - fs::path encryption_keyfile = "encryption.key"; - llarp::SecretKey encryption; - - llarp_findOrCreateEncryption(&crypt, encryption_keyfile, encryption); - - rc.enckey = llarp::seckey_topublic(encryption); - - // get identity public sig key - fs::path ident_keyfile = "identity.key"; - llarp::SecretKey identity; - llarp_findOrCreateIdentity(&crypt, ident_keyfile, identity); - - rc.pubkey = llarp::seckey_topublic(identity); - - // this causes a segfault - if(!rc.Sign(&crypt, identity)) - { - std::cout << "failed to sign" << std::endl; - return 1; - } - // set filename - fs::path our_rc_file = rcfname; - // write file - rc.Write(our_rc_file.string().c_str()); - - // llarp_rc_write(&tmp, our_rc_file.string().c_str()); - - // release memory for tmp lists - // llarp_rc_free(&tmp); - } - if(updMode) - { - printf("rcutil.cpp - Loading [%s]\n", rcfname); - llarp::RouterContact tmp; - // llarp_rc_clear(&rc); - rc.Clear(); - // FIXME: new rc api - // llarp_rc_read(rcfname, &rc); - - // set updated timestamp - rc.last_updated = llarp::time_now_ms(); - // load longterm identity - llarp::Crypto crypt(llarp::Crypto::sodium{}); - - // no longer used? - // llarp_crypto_libsodium_init(&crypt); - llarp::SecretKey identityKey; // FIXME: Jeff requests we use this - fs::path ident_keyfile = "identity.key"; - llarp::SecretKey identity; - llarp_findOrCreateIdentity(&crypt, ident_keyfile, identity); - - // FIXME: update RC API - // get identity public key - // const uint8_t *pubkey = llarp::seckey_topublic(identity); - - // FIXME: update RC API - // llarp_rc_set_pubsigkey(&rc, pubkey); - // // FIXME: update RC API - // llarp_rc_sign(&crypt, identity, &rc); - - // set filename - fs::path our_rc_file_out = "update_debug.rc"; - // write file - // FIXME: update RC API - // rc.Write(our_rc_file.string().c_str()); - // llarp_rc_write(&tmp, our_rc_file_out.string().c_str()); - } - - if(listMode) - { - llarp::Crypto crypto(llarp::Crypto::sodium{}); - auto nodedb = llarp_nodedb_new(&crypto); - llarp_nodedb_iter itr; - itr.visit = [](llarp_nodedb_iter *i) -> bool { - std::cout << llarp::PubKey(i->rc->pubkey) << std::endl; - return true; - }; - if(llarp_nodedb_load_dir(nodedb, nodesdir) > 0) - llarp_nodedb_iterate_all(nodedb, itr); - llarp_nodedb_free(&nodedb); - return 0; - } - if(exportMode) - { - llarp_main_loadDatabase(ctx); - // llarp::LogInfo("Looking for string: ", rcfname); - - llarp::PubKey binaryPK; - llarp::HexDecode(rcfname, binaryPK.data(), binaryPK.size()); - - llarp::LogInfo("Looking for binary: ", binaryPK); - llarp::RouterContact *rc = llarp_main_getDatabase(ctx, binaryPK.data()); - if(!rc) - { - llarp::LogError("Can't load RC from database"); - } - std::string filename(rcfname); - filename.append(".signed"); - llarp::LogInfo("Writing out: ", filename); - // FIXME: update RC API - // rc.Write(our_rc_file.string().c_str()); - // llarp_rc_write(rc, filename.c_str()); - } - if(locateMode) - { - llarp::LogInfo("Going online"); - llarp_main_setup(ctx); - - llarp::PubKey binaryPK; - llarp::HexDecode(rcfname, binaryPK.data(), binaryPK.size()); - - llarp::LogInfo("Queueing job"); - llarp_router_lookup_job *job = new llarp_router_lookup_job; - job->iterative = true; - job->found = false; - job->hook = &HandleDHTLocate; - // llarp_rc_new(&job->result); - job->target = binaryPK; // set job's target - - // create query DHT request - check_online_request *request = new check_online_request; - request->ptr = ctx; - request->job = job; - request->online = false; - request->nodes = 0; - request->first = false; - llarp_main_queryDHT(request); - - llarp::LogInfo("Processing"); - // run system and wait - llarp_main_run(ctx); - } - if(findMode) - { - llarp::LogInfo("Going online"); - llarp_main_setup(ctx); - - llarp::LogInfo("Please find ", rcfname); - std::string str(rcfname); - - llarp::service::Tag tag(rcfname); - llarp::LogInfo("Tag ", tag); - - llarp::service::Address addr; - str = str.append(".loki"); - llarp::LogInfo("Prestring ", str); - bool res = addr.FromString(str.c_str()); - llarp::LogInfo(res ? "Success" : "not a base32 string"); - - // Base32Decode(rcfname, addr); - llarp::LogInfo("Addr ", addr); - - // uint64_t txid, const llarp::service::Address& addr - // FIXME: new API? - // msg->M.push_back(new llarp::dht::FindIntroMessage(tag, 1)); - - // I guess we may need a router to get any replies - llarp::LogInfo("Processing"); - // run system and wait - llarp_main_run(ctx); - } - if(localMode) - { - // FIXME: update llarp_main_getLocalRC - // llarp::RouterContact *rc = llarp_main_getLocalRC(ctx); - // displayRC(rc); - // delete it - } - { - if(rc.Read(rcfname)) - displayRC(rc); - } - - if(toB32Mode) - { - llarp::LogInfo("Converting hex string ", rcfname); - std::string str(rcfname); - - llarp::PubKey binaryPK; - // llarp::service::Address::FromString - llarp::HexDecode(rcfname, binaryPK.data(), binaryPK.size()); - char tmp[(1 + 32) * 2] = {0}; - std::string b32 = llarp::Base32Encode(binaryPK, tmp); - llarp::LogInfo("to base32 ", b32); - } - if(toHexMode) - { - llarp::service::Address addr; - llarp::Base32Decode(rcfname, addr); - llarp::LogInfo("Converting base32 string ", addr); - - // llarp::service::Address::ToString - char ftmp[68] = {0}; - const char *hexname = - llarp::HexEncode< llarp::service::Address, decltype(ftmp) >(addr, ftmp); - - llarp::LogInfo("to hex ", hexname); - } - // it's a unique_ptr, should clean up itself - // llarp_main_free(ctx); - return 0; // success + return 0; } diff --git a/docker/compose/bootstrap.Dockerfile b/docker/compose/bootstrap.Dockerfile new file mode 100644 index 000000000..b95851c18 --- /dev/null +++ b/docker/compose/bootstrap.Dockerfile @@ -0,0 +1,19 @@ +ARG LOKINET_NETID=docker + +FROM alpine:edge as builder + +RUN apk update && \ + apk add build-base cmake git libcap-dev libcap-static libuv-dev libuv-static curl ninja bash binutils-gold + +WORKDIR /src/ +COPY . /src/ + +RUN make NINJA=ninja STATIC_LINK=ON BUILD_TYPE=Release + +FROM alpine:latest + +COPY ./docker/compose/bootstrap.ini /root/.lokinet/lokinet.ini +COPY --from=builder /src/build/lokinet . + +CMD ["./lokinet"] +EXPOSE 1090/udp 1190/tcp diff --git a/docker/compose/bootstrap.ini b/docker/compose/bootstrap.ini new file mode 100644 index 000000000..6b01cb265 --- /dev/null +++ b/docker/compose/bootstrap.ini @@ -0,0 +1,76 @@ +# this configuration was auto generated with 'sane' defaults +# change these values as desired + + +[router] +# number of crypto worker threads +threads=4 +# path to store signed RC +contact-file=/root/.lokinet/self.signed +# path to store transport private key +transport-privkey=/root/.lokinet/transport.private +# path to store identity signing key +ident-privkey=/root/.lokinet/identity.private +# encryption key for onion routing +encryption-privkey=/root/.lokinet/encryption.private + +# uncomment following line to set router nickname to 'lokinet' +#nickname=lokinet + + +[logging] +level=info +# uncomment for logging to file +#type=file +#file=/path/to/logfile +# uncomment for syslog logging +#type=syslog + +[metrics] +json-metrics-path=/root/.lokinet/metrics.json + +# admin api (disabled by default) +[api] +enabled=true +#authkey=insertpubkey1here +#authkey=insertpubkey2here +#authkey=insertpubkey3here +bind=127.0.0.1:1190 + +# system settings for privileges and such +[system] +user=lokinet +group=lokinet +pidfile=/root/.lokinet/lokinet.pid + +# dns provider configuration section +[dns] +# resolver +upstream=1.1.1.1 +bind=127.3.2.1:53 + +# network database settings block +[netdb] +# directory for network database skiplist storage +dir=/netdb + +# lokid settings (disabled by default) +[lokid] +enabled=false +jsonrpc=127.0.0.1:22023 +#service-node-seed=/path/to/servicenode/seed + +# network settings +[network] +profiles=/root/.lokinet/profiles.dat +enabled=true +exit=false +#exit-blacklist=tcp:25 +#exit-whitelist=tcp:* +#exit-whitelist=udp:* +ifaddr=10.200.0.1/8 +ifname=loki-docker0 + +# ROUTERS ONLY: publish network interfaces for handling inbound traffic +[bind] +eth0=1090 diff --git a/docker/compose/docker-compose.yml b/docker/compose/docker-compose.yml new file mode 100644 index 000000000..a88028e83 --- /dev/null +++ b/docker/compose/docker-compose.yml @@ -0,0 +1,59 @@ +version: '3.2' +services: + bootstrap-router: + build: + context: . + dockerfile: docker/compose/bootstrap.Dockerfile + image: bootstrap + cap_add: + - NET_ADMIN + devices: + - "/dev/net/tun:/dev/net/tun" + ports: + - target: 1090 + protocol: udp + mode: host + - target: 1190 + protocol: tcp + mode: host + volumes: + - bootstrap-dir:/root/.lokinet/ + environment: + - LOKINET_NETID=docker + networks: + testing_net: + + router: + depends_on: + - bootstrap-router + build: + context: . + dockerfile: docker/compose/router.Dockerfile + image: router + devices: + - "/dev/net/tun:/dev/net/tun" + ports: + - target: 1090 + protocol: udp + mode: host + - target: 1190 + protocol: tcp + mode: host + cap_add: + - NET_ADMIN + volumes: + - bootstrap-dir:/bootstrap/ + environment: + - LOKINET_NETID=docker + networks: + testing_net: + +volumes: + bootstrap-dir: + +networks: + testing_net: + ipam: + driver: default + config: + - subnet: 172.28.0.0/16 diff --git a/docker/compose/router.Dockerfile b/docker/compose/router.Dockerfile new file mode 100644 index 000000000..a934c2a7d --- /dev/null +++ b/docker/compose/router.Dockerfile @@ -0,0 +1,19 @@ +ARG LOKINET_NETID=docker + +FROM alpine:edge as builder + +RUN apk update && \ + apk add build-base cmake git libcap-dev libcap-static libuv-dev libuv-static curl ninja bash binutils-gold + +WORKDIR /src/ +COPY . /src/ + +RUN make NINJA=ninja STATIC_LINK=ON BUILD_TYPE=Release + +FROM alpine:latest + +COPY ./docker/compose/router.ini /root/.lokinet/lokinet.ini +COPY --from=builder /src/build/lokinet . + +CMD ["./lokinet"] +EXPOSE 1090/udp 1190/tcp diff --git a/docker/compose/router.ini b/docker/compose/router.ini new file mode 100644 index 000000000..873bb617c --- /dev/null +++ b/docker/compose/router.ini @@ -0,0 +1,82 @@ +# this configuration was auto generated with 'sane' defaults +# change these values as desired + + +[router] +# number of crypto worker threads +threads=4 +# path to store signed RC +contact-file=/root/.lokinet/self.signed +# path to store transport private key +transport-privkey=/root/.lokinet/transport.private +# path to store identity signing key +ident-privkey=/root/.lokinet/identity.private +# encryption key for onion routing +encryption-privkey=/root/.lokinet/encryption.private + +# uncomment following line to set router nickname to 'lokinet' +#nickname=lokinet + + +[logging] +level=info +# uncomment for logging to file +#type=file +#file=/path/to/logfile +# uncomment for syslog logging +#type=syslog + +[metrics] +json-metrics-path=/root/.lokinet/metrics.json + +# admin api (disabled by default) +[api] +enabled=true +#authkey=insertpubkey1here +#authkey=insertpubkey2here +#authkey=insertpubkey3here +bind=127.0.0.1:1190 + +# system settings for privileges and such +[system] +user=lokinet +group=lokinet +pidfile=/root/.lokinet/lokinet.pid + +# dns provider configuration section +[dns] +# resolver +upstream=1.1.1.1 +bind=127.3.2.1:53 + +# network database settings block +[netdb] +# directory for network database skiplist storage +dir=/netdb + +# bootstrap settings +[bootstrap] +# add a bootstrap node's signed identity to the list of nodes we want to bootstrap from +# if we don't have any peers we connect to this router +add-node=/bootstrap/self.signed + +# lokid settings (disabled by default) +[lokid] +enabled=false +jsonrpc=127.0.0.1:22023 +#service-node-seed=/path/to/servicenode/seed + +# network settings +[network] +profiles=/root/.lokinet/profiles.dat +enabled=true +exit=false +#exit-blacklist=tcp:25 +#exit-whitelist=tcp:* +#exit-whitelist=udp:* +ifaddr=10.200.0.1/8 +ifname=loki-docker0 + +# ROUTERS ONLY: publish network interfaces for handling inbound traffic +[bind] +eth0=1090 diff --git a/docker/router.Dockerfile b/docker/router.Dockerfile index 7e0c00549..33c913bdf 100644 --- a/docker/router.Dockerfile +++ b/docker/router.Dockerfile @@ -1,3 +1,4 @@ +ARG bootstrap="https://i2p.rocks/i2procks.signed" FROM alpine:edge as builder RUN apk update && \ @@ -7,7 +8,7 @@ WORKDIR /src/ COPY . /src/ RUN make NINJA=ninja STATIC_LINK=ON BUILD_TYPE=Release -RUN ./lokinet-bootstrap +RUN ./lokinet-bootstrap ${bootstrap} FROM alpine:latest diff --git a/include/tuntap.h b/include/tuntap.h index 8a56eb28e..6cc444a70 100644 --- a/include/tuntap.h +++ b/include/tuntap.h @@ -147,7 +147,7 @@ extern "C" int flags; /* ifr.ifr_flags on Unix */ char if_name[IF_NAMESIZE]; #if defined(Windows) - int idx; /* needed to set ipv6 address */ + int idx; /* needed to set ipv6 address */ #endif #if defined(FreeBSD) int mode; diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 36510c0e1..54fef01c3 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -2,6 +2,7 @@ set(LIB_UTIL_SRC config/config.cpp config/ini.cpp constants/defaults.cpp + constants/limits.cpp constants/link_layer.cpp constants/path.cpp constants/proto.cpp diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index bf10ff91d..a26bf3a30 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -501,7 +502,7 @@ llarp_ensure_config(const char *fname, const char *basedir, bool overwrite, return false; } auto &f = optional_f.value(); - llarp_generic_ensure_config(f, basepath); + llarp_generic_ensure_config(f, basepath, asRouter); if(asRouter) { llarp_ensure_router_config(f, basepath); @@ -515,7 +516,8 @@ llarp_ensure_config(const char *fname, const char *basedir, bool overwrite, } void -llarp_generic_ensure_config(std::ofstream &f, std::string basepath) +llarp_generic_ensure_config(std::ofstream &f, std::string basepath, + bool isRouter) { f << "# this configuration was auto generated with 'sane' defaults" << std::endl; @@ -536,6 +538,13 @@ llarp_generic_ensure_config(std::ofstream &f, std::string basepath) f << "# uncomment following line to set router nickname to 'lokinet'" << std::endl; f << "#nickname=lokinet" << std::endl; + const auto limits = isRouter ? llarp::limits::snode : llarp::limits::client; + + f << "# maintain min connections to other routers" << std::endl; + f << "min-routers=" << std::to_string(limits.DefaultMinRouters) << std::endl; + f << "# hard limit of routers globally we are connected to at any given time" + << std::endl; + f << "max-routers=" << std::to_string(limits.DefaultMaxRouters) << std::endl; f << std::endl << std::endl; // logging @@ -693,7 +702,7 @@ llarp_ensure_client_config(std::ofstream &f, std::string basepath) // now do up fname f << std::endl << std::endl; f << "# snapps configuration section" << std::endl; - f << "[services]"; + f << "[services]" << std::endl; f << "# uncomment next line to enable a snapp" << std::endl; f << "#example-snapp=" << snappExample_fpath << std::endl; f << std::endl << std::endl; diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 6ed3ee297..58e5cc4a5 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -27,10 +27,8 @@ namespace llarp { return ptr; } - else - { - return val; - } + + return val; } template <> @@ -43,10 +41,8 @@ namespace llarp { return std::atoi(ptr); } - else - { - return val; - } + + return val; } template <> @@ -59,10 +55,8 @@ namespace llarp { return std::atoi(ptr); } - else - { - return val; - } + + return val; } template <> @@ -75,10 +69,8 @@ namespace llarp { return std::atoll(ptr); } - else - { - return val; - } + + return val; } template <> @@ -92,10 +84,8 @@ namespace llarp { return IsTrueValue(ptr); } - else - { - return val; - } + + return val; } class RouterConfig @@ -105,7 +95,7 @@ namespace llarp size_t m_minConnectedRouters = 2; /// hard upperbound limit on the number of router to router connections - size_t m_maxConnectedRouters = 2000; + size_t m_maxConnectedRouters = 5; std::string m_netId; std::string m_nickname; @@ -325,7 +315,8 @@ namespace llarp } // namespace llarp void -llarp_generic_ensure_config(std::ofstream& f, std::string basepath); +llarp_generic_ensure_config(std::ofstream& f, std::string basepath, + bool isRouter); void llarp_ensure_router_config(std::ofstream& f, std::string basepath); diff --git a/llarp/constants/limits.cpp b/llarp/constants/limits.cpp new file mode 100644 index 000000000..e7358b876 --- /dev/null +++ b/llarp/constants/limits.cpp @@ -0,0 +1,13 @@ +#include + +namespace llarp +{ + namespace limits + { + /// snode limit parameters + const LimitParameters snode = {6, 60}; + + /// client limit parameters + const LimitParameters client = {4, 6}; + } // namespace limits +} // namespace llarp \ No newline at end of file diff --git a/llarp/constants/limits.hpp b/llarp/constants/limits.hpp new file mode 100644 index 000000000..626170139 --- /dev/null +++ b/llarp/constants/limits.hpp @@ -0,0 +1,26 @@ +#ifndef LLARP_CONSTANTS_LIMITS_HPP +#define LLARP_CONSTANTS_LIMITS_HPP +#include +namespace llarp +{ + namespace limits + { + /// Limits are a struct that contains all hard and soft limit + /// parameters for a given mode of operation + struct LimitParameters + { + /// minimum routers needed to run + std::size_t DefaultMinRouters; + /// hard limit on router sessions (by pubkey) + std::size_t DefaultMaxRouters; + }; + + /// snode limit parameters + const extern LimitParameters snode; + + /// client limit parameters + const extern LimitParameters client; + } // namespace limits +} // namespace llarp + +#endif \ No newline at end of file diff --git a/llarp/constants/link_layer.hpp b/llarp/constants/link_layer.hpp index 7fa0f6082..b09ee5e4c 100644 --- a/llarp/constants/link_layer.hpp +++ b/llarp/constants/link_layer.hpp @@ -2,7 +2,7 @@ #define LLARP_LINK_LAYER_HPP #include -#include +#include constexpr size_t MAX_LINK_MSG_SIZE = 8192; constexpr llarp_time_t DefaultLinkSessionLifetime = 60 * 1000; diff --git a/llarp/context.cpp b/llarp/context.cpp index 9817a3956..ab989ec53 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__) #include @@ -27,9 +27,7 @@ namespace llarp { - Context::Context() - { - } + Context::Context() = default; Context::~Context() { @@ -421,8 +419,8 @@ extern "C" { cSetLogLevel(eLogDebug); } - llarp_main *m = new llarp_main; - m->ctx = std::make_unique< llarp::Context >(); + auto *m = new llarp_main; + m->ctx = std::make_unique< llarp::Context >(); if(!m->ctx->LoadConfig(fname)) { m->ctx->Close(); diff --git a/llarp/crypto/constants.hpp b/llarp/crypto/constants.hpp index 81e45f9d1..d0331f279 100644 --- a/llarp/crypto/constants.hpp +++ b/llarp/crypto/constants.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_CRYPTO_CONSTANTS_HPP #define LLARP_CRYPTO_CONSTANTS_HPP -#include +#include #include diff --git a/llarp/crypto/crypto.hpp b/llarp/crypto/crypto.hpp index d248062d5..f97bdbfc7 100644 --- a/llarp/crypto/crypto.hpp +++ b/llarp/crypto/crypto.hpp @@ -8,8 +8,8 @@ #include #include -#include -#include + +#include /** * crypto.hpp @@ -89,9 +89,7 @@ namespace llarp pqe_encrypt(PQCipherBlock &, SharedSecret &, const PQPubKey &) = 0; }; - inline Crypto::~Crypto() - { - } + inline Crypto::~Crypto() = default; /// return random 64bit unsigned interger uint64_t diff --git a/llarp/crypto/crypto_libsodium.cpp b/llarp/crypto/crypto_libsodium.cpp index 9402af1ed..aab38665e 100644 --- a/llarp/crypto/crypto_libsodium.cpp +++ b/llarp/crypto/crypto_libsodium.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include extern "C" { diff --git a/llarp/crypto/crypto_libsodium.hpp b/llarp/crypto/crypto_libsodium.hpp index 4a06a3054..a2eebe0e4 100644 --- a/llarp/crypto/crypto_libsodium.hpp +++ b/llarp/crypto/crypto_libsodium.hpp @@ -11,9 +11,7 @@ namespace llarp { CryptoLibSodium(); - ~CryptoLibSodium() - { - } + ~CryptoLibSodium() override = default; /// xchacha symmetric cipher bool diff --git a/llarp/crypto/crypto_noop.hpp b/llarp/crypto/crypto_noop.hpp index 28373f2dd..177515258 100644 --- a/llarp/crypto/crypto_noop.hpp +++ b/llarp/crypto/crypto_noop.hpp @@ -20,7 +20,7 @@ namespace llarp { } - ~NoOpCrypto() = default; + ~NoOpCrypto() override = default; bool xchacha20(const llarp_buffer_t &, const SharedSecret &, diff --git a/llarp/crypto/encrypted_frame.hpp b/llarp/crypto/encrypted_frame.hpp index f8d1ae644..bbd7537da 100644 --- a/llarp/crypto/encrypted_frame.hpp +++ b/llarp/crypto/encrypted_frame.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -60,8 +61,7 @@ namespace llarp static void Decrypt(void* user) { - AsyncFrameDecrypter< User >* ctx = - static_cast< AsyncFrameDecrypter< User >* >(user); + auto* ctx = static_cast< AsyncFrameDecrypter< User >* >(user); if(ctx->target.DecryptInPlace(ctx->seckey)) { @@ -75,7 +75,7 @@ namespace llarp } AsyncFrameDecrypter(const SecretKey& secretkey, DecryptHandler h) - : result(h), seckey(secretkey) + : result(std::move(h)), seckey(secretkey) { } diff --git a/llarp/dht/bucket.hpp b/llarp/dht/bucket.hpp index d796bc480..ab2e069e9 100644 --- a/llarp/dht/bucket.hpp +++ b/llarp/dht/bucket.hpp @@ -19,7 +19,8 @@ namespace llarp using BucketStorage_t = std::map< Key_t, Val_t, XorMetric >; using Random_t = std::function< uint64_t() >; - Bucket(const Key_t& us, Random_t r) : nodes(XorMetric(us)), random(r) + Bucket(const Key_t& us, Random_t r) + : nodes(XorMetric(us)), random(std::move(r)) { } diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 56b8683e4..0a77440e8 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -26,17 +26,13 @@ namespace llarp { namespace dht { - AbstractContext::~AbstractContext() - { - } + AbstractContext::~AbstractContext() = default; struct Context final : public AbstractContext { Context(); - ~Context() - { - } + ~Context() override = default; util::StatusObject ExtractStatus() const override; @@ -167,7 +163,7 @@ namespace llarp void Explore(size_t N = 3); - llarp::AbstractRouter* router; + llarp::AbstractRouter* router{nullptr}; // for router contacts std::unique_ptr< Bucket< RCNode > > _nodes; @@ -180,7 +176,7 @@ namespace llarp return _services.get(); } - bool allowTransit; + bool allowTransit{false}; bool& AllowTransit() override @@ -308,7 +304,7 @@ namespace llarp Key_t ourKey; }; - Context::Context() : router(nullptr), allowTransit(false) + Context::Context() { randombytes((byte_t*)&ids, sizeof(uint64_t)); } @@ -346,7 +342,7 @@ namespace llarp { if(left) return; - Context* ctx = static_cast< Context* >(u); + auto* ctx = static_cast< Context* >(u); const auto num = std::min(ctx->router->NumberOfConnectedRouters(), size_t(4)); if(num) @@ -361,7 +357,7 @@ namespace llarp { if(left) return; - Context* ctx = static_cast< Context* >(u); + auto* ctx = static_cast< Context* >(u); // clean up transactions ctx->CleanupTX(); diff --git a/llarp/dht/dht.h b/llarp/dht/dht.h index 24b84468c..c7854085c 100644 --- a/llarp/dht/dht.h +++ b/llarp/dht/dht.h @@ -33,7 +33,7 @@ llarp_dht_context_start(struct llarp_dht_context* ctx, const byte_t* key); // remove this? dns needs it atm struct llarp_router_lookup_job; -typedef void (*llarp_router_lookup_handler)(struct llarp_router_lookup_job*); +using llarp_router_lookup_handler = void (*)(struct llarp_router_lookup_job*); struct llarp_router_lookup_job { diff --git a/llarp/dht/message.cpp b/llarp/dht/message.cpp index 98786fd66..04889f1b2 100644 --- a/llarp/dht/message.cpp +++ b/llarp/dht/message.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -46,29 +47,29 @@ namespace llarp switch(*strbuf.base) { case 'F': - msg.reset(new FindIntroMessage(From, relayed)); + msg = std::make_unique< FindIntroMessage >(From, relayed); break; case 'R': if(relayed) - msg.reset(new RelayedFindRouterMessage(From)); + msg = std::make_unique< RelayedFindRouterMessage >(From); else - msg.reset(new FindRouterMessage(From)); + msg = std::make_unique< FindRouterMessage >(From); break; case 'S': - msg.reset(new GotRouterMessage(From, relayed)); + msg = std::make_unique< GotRouterMessage >(From, relayed); break; case 'I': - msg.reset(new PublishIntroMessage()); + msg = std::make_unique< PublishIntroMessage >(); break; case 'G': if(relayed) { - msg.reset(new RelayedGotIntroMessage()); + msg = std::make_unique< RelayedGotIntroMessage >(); break; } else { - msg.reset(new GotIntroMessage(From)); + msg = std::make_unique< GotIntroMessage >(From); break; } default: diff --git a/llarp/dht/message.hpp b/llarp/dht/message.hpp index e5aa63ff5..cf5adf199 100644 --- a/llarp/dht/message.hpp +++ b/llarp/dht/message.hpp @@ -16,9 +16,7 @@ namespace llarp struct IMessage { - virtual ~IMessage() - { - } + virtual ~IMessage() = default; /// construct IMessage(const Key_t& from) : From(from) diff --git a/llarp/dht/messages/findintro.cpp b/llarp/dht/messages/findintro.cpp index 8240385d6..ad654f33c 100644 --- a/llarp/dht/messages/findintro.cpp +++ b/llarp/dht/messages/findintro.cpp @@ -7,9 +7,7 @@ namespace llarp { namespace dht { - FindIntroMessage::~FindIntroMessage() - { - } + FindIntroMessage::~FindIntroMessage() = default; bool FindIntroMessage::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) diff --git a/llarp/dht/messages/findintro.hpp b/llarp/dht/messages/findintro.hpp index e16946bda..bf510aadc 100644 --- a/llarp/dht/messages/findintro.hpp +++ b/llarp/dht/messages/findintro.hpp @@ -45,7 +45,7 @@ namespace llarp R = 1; } - ~FindIntroMessage(); + ~FindIntroMessage() override; bool BEncode(llarp_buffer_t* buf) const override; diff --git a/llarp/dht/messages/findrouter.cpp b/llarp/dht/messages/findrouter.cpp index 8828a4e91..d2cef6c0d 100644 --- a/llarp/dht/messages/findrouter.cpp +++ b/llarp/dht/messages/findrouter.cpp @@ -57,9 +57,7 @@ namespace llarp return true; } - FindRouterMessage::~FindRouterMessage() - { - } + FindRouterMessage::~FindRouterMessage() = default; bool FindRouterMessage::BEncode(llarp_buffer_t *buf) const @@ -183,8 +181,8 @@ namespace llarp replies.emplace_back(new GotRouterMessage(k, txid, {found}, false)); return true; } - else - dht.LookupRouterRelayed(From, txid, k, !iterative, replies); + + dht.LookupRouterRelayed(From, txid, k, !iterative, replies); return true; } } // namespace dht diff --git a/llarp/dht/messages/findrouter.hpp b/llarp/dht/messages/findrouter.hpp index 903925dac..cb4f0a824 100644 --- a/llarp/dht/messages/findrouter.hpp +++ b/llarp/dht/messages/findrouter.hpp @@ -25,7 +25,7 @@ namespace llarp K.Randomize(); } - ~FindRouterMessage(); + ~FindRouterMessage() override; bool BEncode(llarp_buffer_t* buf) const override; @@ -33,7 +33,7 @@ namespace llarp bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; - virtual bool + bool HandleMessage( llarp_dht_context* ctx, std::vector< std::unique_ptr< IMessage > >& replies) const override; @@ -55,7 +55,7 @@ namespace llarp /// handle a relayed FindRouterMessage, do a lookup on the dht and inform /// the path of the result /// TODO: smart path expiration logic needs to be implemented - virtual bool + bool HandleMessage(llarp_dht_context* ctx, std::vector< IMessage::Ptr_t >& replies) const override; }; diff --git a/llarp/dht/messages/gotintro.cpp b/llarp/dht/messages/gotintro.cpp index 49db69753..8556d398f 100644 --- a/llarp/dht/messages/gotintro.cpp +++ b/llarp/dht/messages/gotintro.cpp @@ -1,29 +1,27 @@ #include #include +#include #include #include #include +#include namespace llarp { namespace dht { - GotIntroMessage::GotIntroMessage( - const std::vector< llarp::service::IntroSet > &results, uint64_t tx) - : IMessage({}), I(results), T(tx) - { - } - - GotIntroMessage::~GotIntroMessage() + GotIntroMessage::GotIntroMessage(std::vector< service::IntroSet > results, + uint64_t tx) + : IMessage({}), I(std::move(results)), T(tx) { } bool GotIntroMessage::HandleMessage( llarp_dht_context *ctx, - __attribute__((unused)) - std::vector< std::unique_ptr< IMessage > > &replies) const + ABSL_ATTRIBUTE_UNUSED std::vector< std::unique_ptr< IMessage > > + &replies) const { auto &dht = *ctx->impl; @@ -31,7 +29,7 @@ namespace llarp { if(!introset.Verify(dht.Now())) { - llarp::LogWarn( + LogWarn( "Invalid introset while handling direct GotIntro " "from ", From); @@ -59,7 +57,7 @@ namespace llarp } return true; } - llarp::LogError("no pending TX for GIM from ", From, " txid=", T); + LogError("no pending TX for GIM from ", From, " txid=", T); return false; } @@ -77,7 +75,7 @@ namespace llarp auto copy = std::make_shared< const RelayedGotIntroMessage >(*this); return pathset->HandleGotIntroMessage(copy); } - llarp::LogWarn("No path for got intro message pathid=", pathID); + LogWarn("No path for got intro message pathid=", pathID); return false; } @@ -92,7 +90,7 @@ namespace llarp { if(K) // duplicate key? return false; - K.reset(new dht::Key_t()); + K = std::make_unique< dht::Key_t >(); return K->BDecode(buf); } bool read = false; diff --git a/llarp/dht/messages/gotintro.hpp b/llarp/dht/messages/gotintro.hpp index bee067055..42a1e5ffb 100644 --- a/llarp/dht/messages/gotintro.hpp +++ b/llarp/dht/messages/gotintro.hpp @@ -15,7 +15,7 @@ namespace llarp struct GotIntroMessage : public IMessage { /// the found introsets - std::vector< llarp::service::IntroSet > I; + std::vector< service::IntroSet > I; /// txid uint64_t T = 0; /// the key of a router closer in keyspace if iterative lookup @@ -41,10 +41,9 @@ namespace llarp } /// for recursive reply - GotIntroMessage(const std::vector< llarp::service::IntroSet >& results, - uint64_t txid); + GotIntroMessage(std::vector< service::IntroSet > results, uint64_t txid); - ~GotIntroMessage(); + ~GotIntroMessage() override = default; bool BEncode(llarp_buffer_t* buf) const override; @@ -52,7 +51,7 @@ namespace llarp bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; - virtual bool + bool HandleMessage(llarp_dht_context* ctx, std::vector< IMessage::Ptr_t >& replies) const override; }; diff --git a/llarp/dht/messages/gotrouter.cpp b/llarp/dht/messages/gotrouter.cpp index 9d73f4639..bcc2a9b1e 100644 --- a/llarp/dht/messages/gotrouter.cpp +++ b/llarp/dht/messages/gotrouter.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -8,9 +9,7 @@ namespace llarp { namespace dht { - GotRouterMessage::~GotRouterMessage() - { - } + GotRouterMessage::~GotRouterMessage() = default; bool GotRouterMessage::BEncode(llarp_buffer_t *buf) const @@ -56,7 +55,7 @@ namespace llarp { if(K) // duplicate key? return false; - K.reset(new dht::Key_t()); + K = std::make_unique< dht::Key_t >(); return K->BDecode(val); } if(key == "N") diff --git a/llarp/dht/messages/gotrouter.hpp b/llarp/dht/messages/gotrouter.hpp index 173b91936..4bb174410 100644 --- a/llarp/dht/messages/gotrouter.hpp +++ b/llarp/dht/messages/gotrouter.hpp @@ -1,8 +1,11 @@ #ifndef LLARP_DHT_MESSAGES_GOT_ROUTER_HPP #define LLARP_DHT_MESSAGES_GOT_ROUTER_HPP + #include #include #include +#include +#include namespace llarp { @@ -15,9 +18,8 @@ namespace llarp { } GotRouterMessage(const Key_t& from, uint64_t id, - const std::vector< RouterContact >& results, - bool tunneled) - : IMessage(from), R(results), txid(id), relayed(tunneled) + std::vector< RouterContact > results, bool tunneled) + : IMessage(from), R(std::move(results)), txid(id), relayed(tunneled) { } @@ -27,9 +29,9 @@ namespace llarp { } - GotRouterMessage(uint64_t id, const std::vector< RouterID >& near, + GotRouterMessage(uint64_t id, std::vector< RouterID > _near, bool tunneled) - : IMessage({}), N(near), txid(id), relayed(tunneled) + : IMessage({}), N(std::move(_near)), txid(id), relayed(tunneled) { } @@ -44,7 +46,7 @@ namespace llarp version = other.version; } - ~GotRouterMessage(); + ~GotRouterMessage() override; bool BEncode(llarp_buffer_t* buf) const override; @@ -52,7 +54,7 @@ namespace llarp bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; - virtual bool + bool HandleMessage( llarp_dht_context* ctx, std::vector< std::unique_ptr< IMessage > >& replies) const override; diff --git a/llarp/dht/messages/pubintro.cpp b/llarp/dht/messages/pubintro.cpp index 5c0e231b9..ce7a10768 100644 --- a/llarp/dht/messages/pubintro.cpp +++ b/llarp/dht/messages/pubintro.cpp @@ -10,9 +10,7 @@ namespace llarp { namespace dht { - PublishIntroMessage::~PublishIntroMessage() - { - } + PublishIntroMessage::~PublishIntroMessage() = default; bool PublishIntroMessage::DecodeKey(const llarp_buffer_t &key, diff --git a/llarp/dht/messages/pubintro.hpp b/llarp/dht/messages/pubintro.hpp index 6854965d1..9cf34f9d1 100644 --- a/llarp/dht/messages/pubintro.hpp +++ b/llarp/dht/messages/pubintro.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace llarp @@ -22,14 +23,14 @@ namespace llarp } PublishIntroMessage(const llarp::service::IntroSet& i, uint64_t tx, - uint64_t s, const std::vector< Key_t >& exclude = {}) - : IMessage({}), E(exclude), txID(tx) + uint64_t s, std::vector< Key_t > exclude = {}) + : IMessage({}), E(std::move(exclude)), txID(tx) { I = i; S = s; } - ~PublishIntroMessage(); + ~PublishIntroMessage() override; bool BEncode(llarp_buffer_t* buf) const override; @@ -37,7 +38,7 @@ namespace llarp bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; - virtual bool + bool HandleMessage( llarp_dht_context* ctx, std::vector< std::unique_ptr< IMessage > >& replies) const override; diff --git a/llarp/dht/node.hpp b/llarp/dht/node.hpp index 59cb96372..1c8dac67d 100644 --- a/llarp/dht/node.hpp +++ b/llarp/dht/node.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace llarp { @@ -47,7 +48,7 @@ namespace llarp ID.Zero(); } - ISNode(const service::IntroSet& other) : introset(other) + ISNode(service::IntroSet other) : introset(std::move(other)) { introset.A.CalculateAddress(ID.as_array()); } diff --git a/llarp/dht/publishservicejob.cpp b/llarp/dht/publishservicejob.cpp index 12a9a7752..22e130d31 100644 --- a/llarp/dht/publishservicejob.cpp +++ b/llarp/dht/publishservicejob.cpp @@ -2,6 +2,7 @@ #include #include +#include namespace llarp { @@ -10,11 +11,11 @@ namespace llarp PublishServiceJob::PublishServiceJob(const TXOwner &asker, const service::IntroSet &introset, AbstractContext *ctx, uint64_t s, - const std::set< Key_t > &exclude) + std::set< Key_t > exclude) : TX< service::Address, service::IntroSet >(asker, introset.A.Addr(), ctx) , S(s) - , dontTell(exclude) + , dontTell(std::move(exclude)) , I(introset) { } diff --git a/llarp/dht/publishservicejob.hpp b/llarp/dht/publishservicejob.hpp index 52da0993e..b86bdf912 100644 --- a/llarp/dht/publishservicejob.hpp +++ b/llarp/dht/publishservicejob.hpp @@ -20,7 +20,7 @@ namespace llarp PublishServiceJob(const TXOwner &asker, const service::IntroSet &introset, AbstractContext *ctx, uint64_t s, - const std::set< Key_t > &exclude); + std::set< Key_t > exclude); bool Validate(const service::IntroSet &introset) const override; diff --git a/llarp/dht/recursiverouterlookup.cpp b/llarp/dht/recursiverouterlookup.cpp index 11468d659..ae7b44ede 100644 --- a/llarp/dht/recursiverouterlookup.cpp +++ b/llarp/dht/recursiverouterlookup.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace llarp { @@ -13,7 +14,7 @@ namespace llarp AbstractContext *ctx, RouterLookupHandler result) : TX< RouterID, RouterContact >(_whoasked, _target, ctx) - , resultHandler(result) + , resultHandler(std::move(result)) { peersAsked.insert(ctx->OurKey()); diff --git a/llarp/dht/recursiverouterlookup.hpp b/llarp/dht/recursiverouterlookup.hpp index 653f94a6a..b7f56e063 100644 --- a/llarp/dht/recursiverouterlookup.hpp +++ b/llarp/dht/recursiverouterlookup.hpp @@ -28,7 +28,7 @@ namespace llarp void Start(const TXOwner &peer) override; - virtual void + void SendReply() override; }; } // namespace dht diff --git a/llarp/dht/serviceaddresslookup.cpp b/llarp/dht/serviceaddresslookup.cpp index 99f358a1d..958deb972 100644 --- a/llarp/dht/serviceaddresslookup.cpp +++ b/llarp/dht/serviceaddresslookup.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace llarp { @@ -13,7 +14,7 @@ namespace llarp AbstractContext *ctx, uint64_t r, service::IntroSetLookupHandler handler) : TX< service::Address, service::IntroSet >(asker, addr, ctx) - , handleResult(handler) + , handleResult(std::move(handler)) , R(r) { peersAsked.insert(ctx->OurKey()); diff --git a/llarp/dht/serviceaddresslookup.hpp b/llarp/dht/serviceaddresslookup.hpp index 122ea31a0..7e3626585 100644 --- a/llarp/dht/serviceaddresslookup.hpp +++ b/llarp/dht/serviceaddresslookup.hpp @@ -34,7 +34,7 @@ namespace llarp void DoNextRequest(const Key_t &ask) override; - virtual void + void SendReply() override; }; } // namespace dht diff --git a/llarp/dht/tx.hpp b/llarp/dht/tx.hpp index 1edc36200..e8b4239fa 100644 --- a/llarp/dht/tx.hpp +++ b/llarp/dht/tx.hpp @@ -29,9 +29,7 @@ namespace llarp { } - virtual ~TX() - { - } + virtual ~TX() = default; void OnFound(const Key_t& askedPeer, const V& value); diff --git a/llarp/dns.cpp b/llarp/dns.cpp index e3e4c36ab..0fb3abd4f 100644 --- a/llarp/dns.cpp +++ b/llarp/dns.cpp @@ -96,7 +96,7 @@ getDNSstring(const char *const buffer, uint32_t *pos) } void -code_domain(char *&buffer, const std::string &domain) throw() +code_domain(char *&buffer, const std::string &domain) noexcept { std::string::size_type start(0); std::string::size_type end; // indexes @@ -124,7 +124,7 @@ code_domain(char *&buffer, const std::string &domain) throw() } void -vcode_domain(std::vector< byte_t > &bytes, const std::string &domain) throw() +vcode_domain(std::vector< byte_t > &bytes, const std::string &domain) noexcept { std::string::size_type start(0); std::string::size_type end; // indexes @@ -153,7 +153,7 @@ vcode_domain(std::vector< byte_t > &bytes, const std::string &domain) throw() // expects host order void -vput16bits(std::vector< byte_t > &bytes, uint16_t value) throw() +vput16bits(std::vector< byte_t > &bytes, uint16_t value) noexcept { char buf[2] = {0}; char *write_buffer = buf; @@ -164,7 +164,7 @@ vput16bits(std::vector< byte_t > &bytes, uint16_t value) throw() // expects host order void -vput32bits(std::vector< byte_t > &bytes, uint32_t value) throw() +vput32bits(std::vector< byte_t > &bytes, uint32_t value) noexcept { char buf[4] = {0}; char *write_buffer = buf; @@ -178,7 +178,7 @@ vput32bits(std::vector< byte_t > &bytes, uint32_t value) throw() void dns_writeType(std::vector< byte_t > &bytes, llarp::dns::record *record) { - llarp::dns::type_1a *type1a = dynamic_cast< llarp::dns::type_1a * >(record); + auto *type1a = dynamic_cast< llarp::dns::type_1a * >(record); if(type1a) { std::vector< byte_t > more_bytes = type1a->to_bytes(); @@ -186,8 +186,7 @@ dns_writeType(std::vector< byte_t > &bytes, llarp::dns::record *record) bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end()); } - llarp::dns::type_2ns *type2ns = - dynamic_cast< llarp::dns::type_2ns * >(record); + auto *type2ns = dynamic_cast< llarp::dns::type_2ns * >(record); if(type2ns) { std::vector< byte_t > more_bytes = type2ns->to_bytes(); @@ -195,8 +194,7 @@ dns_writeType(std::vector< byte_t > &bytes, llarp::dns::record *record) bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end()); } - llarp::dns::type_5cname *type5cname = - dynamic_cast< llarp::dns::type_5cname * >(record); + auto *type5cname = dynamic_cast< llarp::dns::type_5cname * >(record); if(type5cname) { std::vector< byte_t > more_bytes = type5cname->to_bytes(); @@ -204,24 +202,21 @@ dns_writeType(std::vector< byte_t > &bytes, llarp::dns::record *record) bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end()); } - llarp::dns::type_12ptr *type12ptr = - dynamic_cast< llarp::dns::type_12ptr * >(record); + auto *type12ptr = dynamic_cast< llarp::dns::type_12ptr * >(record); if(type12ptr) { std::vector< byte_t > more_bytes = type12ptr->to_bytes(); llarp::LogDebug("[12]Adding ", more_bytes.size()); bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end()); } - llarp::dns::type_15mx *type15mx = - dynamic_cast< llarp::dns::type_15mx * >(record); + auto *type15mx = dynamic_cast< llarp::dns::type_15mx * >(record); if(type15mx) { std::vector< byte_t > more_bytes = type15mx->to_bytes(); llarp::LogDebug("[15]Adding ", more_bytes.size()); bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end()); } - llarp::dns::type_16txt *type16txt = - dynamic_cast< llarp::dns::type_16txt * >(record); + auto *type16txt = dynamic_cast< llarp::dns::type_16txt * >(record); if(type16txt) { std::vector< byte_t > more_bytes = type16txt->to_bytes(); @@ -292,7 +287,7 @@ packet2bytes(dns_packet &in) extern "C" { uint16_t - get16bits(const char *&buffer) throw() + get16bits(const char *&buffer) noexcept { uint16_t value = bufbe16toh(buffer); buffer += 2; @@ -300,7 +295,7 @@ extern "C" } uint32_t - get32bits(const char *&buffer) throw() + get32bits(const char *&buffer) noexcept { uint32_t value = bufbe32toh(buffer); buffer += 4; @@ -350,7 +345,7 @@ extern "C" dns_msg_question * decode_question(const char *buffer, uint32_t *pos) { - dns_msg_question *question = new dns_msg_question; + auto *question = new dns_msg_question; std::string m_qName = getDNSstring(buffer, pos); llarp::LogDebug("Got question name: ", m_qName); @@ -369,7 +364,7 @@ extern "C" dns_msg_answer * decode_answer(const char *const buffer, uint32_t *pos) { - dns_msg_answer *answer = new dns_msg_answer; + auto *answer = new dns_msg_answer; /* llarp_buffer_t bob; bob.base = (unsigned char *)buffer; @@ -589,14 +584,14 @@ extern "C" } void - put16bits(char *&buffer, uint16_t value) throw() + put16bits(char *&buffer, uint16_t value) noexcept { htobe16buf(buffer, value); buffer += 2; } void - put32bits(char *&buffer, uint32_t value) throw() + put32bits(char *&buffer, uint32_t value) noexcept { htobe32buf(buffer, value); buffer += 4; diff --git a/llarp/dns.h b/llarp/dns.h index 728009a37..a2eee4a42 100644 --- a/llarp/dns.h +++ b/llarp/dns.h @@ -14,7 +14,7 @@ extern "C" // fwd declr struct dnsc_answer_request; - typedef void (*dnsc_answer_hook_func)(dnsc_answer_request *request); + using dnsc_answer_hook_func = void (*)(dnsc_answer_request *); #ifdef __cplusplus } diff --git a/llarp/dns.hpp b/llarp/dns.hpp index a0edeeaf5..b290994b5 100644 --- a/llarp/dns.hpp +++ b/llarp/dns.hpp @@ -111,24 +111,24 @@ std::string getDNSstring(const char *const buffer, uint32_t *pos); void -code_domain(char *&buffer, const std::string &domain) throw(); +code_domain(char *&buffer, const std::string &domain) noexcept; void -vcode_domain(std::vector< byte_t > &bytes, const std::string &domain) throw(); +vcode_domain(std::vector< byte_t > &bytes, const std::string &domain) noexcept; void -vput16bits(std::vector< byte_t > &bytes, uint16_t value) throw(); +vput16bits(std::vector< byte_t > &bytes, uint16_t value) noexcept; void -vput32bits(std::vector< byte_t > &bytes, uint32_t value) throw(); +vput32bits(std::vector< byte_t > &bytes, uint32_t value) noexcept; extern "C" { uint16_t - get16bits(const char *&buffer) throw(); + get16bits(const char *&buffer) noexcept; uint32_t - get32bits(const char *&buffer) throw(); + get32bits(const char *&buffer) noexcept; bool decode_hdr(llarp_buffer_t *buffer, dns_msg_header *hdr); @@ -140,10 +140,10 @@ extern "C" decode_answer(const char *const buffer, uint32_t *pos); void - put16bits(char *&buffer, uint16_t value) throw(); + put16bits(char *&buffer, uint16_t value) noexcept; void - put32bits(char *&buffer, uint32_t value) throw(); + put32bits(char *&buffer, uint32_t value) noexcept; void llarp_handle_dns_recvfrom(struct llarp_udp_io *udp, diff --git a/llarp/dns/dns.hpp b/llarp/dns/dns.hpp index 921b16f38..cfcd5ee9c 100644 --- a/llarp/dns/dns.hpp +++ b/llarp/dns/dns.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_DNS_DNS_HPP #define LLARP_DNS_DNS_HPP -#include +#include namespace llarp { diff --git a/llarp/dns/name.cpp b/llarp/dns/name.cpp index f23e2c1c9..ad51ad6c3 100644 --- a/llarp/dns/name.cpp +++ b/llarp/dns/name.cpp @@ -107,11 +107,11 @@ namespace llarp ip = net::IPPacket::ExpandV4(llarp::ipaddr_ipv4_bits(a, b, c, d)); return true; } - else if(numdots == 32 && isV6) + if(numdots == 32 && isV6) { size_t idx = 0; uint8_t lo, hi; - uint8_t* ptr = (uint8_t*)&ip.h; + auto* ptr = (uint8_t*)&ip.h; while(idx < 16) { pos = sub.find('.'); diff --git a/llarp/dns/rectypes.cpp b/llarp/dns/rectypes.cpp index a552b7509..d2771cb3c 100644 --- a/llarp/dns/rectypes.cpp +++ b/llarp/dns/rectypes.cpp @@ -5,9 +5,7 @@ namespace llarp { namespace dns { - record::~record() - { - } + record::~record() = default; bool record::parse(std::vector< byte_t > bytes) diff --git a/llarp/dns/rectypes.hpp b/llarp/dns/rectypes.hpp index a56b4f89d..4d651f4a7 100644 --- a/llarp/dns/rectypes.hpp +++ b/llarp/dns/rectypes.hpp @@ -13,9 +13,7 @@ namespace llarp struct record { virtual ~record() = 0; - record() - { - } + record() = default; virtual bool parse(std::vector< byte_t > bytes) = 0; @@ -28,9 +26,7 @@ namespace llarp { huint32_t ipaddr; - virtual ~type_1a() - { - } + ~type_1a() override = default; type_1a(); bool @@ -44,9 +40,7 @@ namespace llarp { std::string ns; - virtual ~type_2ns() - { - } + ~type_2ns() override = default; type_2ns(); bool @@ -66,9 +60,7 @@ namespace llarp uint32_t expire; uint32_t minimum; - virtual ~type_6soa() - { - } + ~type_6soa() override = default; type_6soa(); bool @@ -82,9 +74,7 @@ namespace llarp { std::string cname; - virtual ~type_5cname() - { - } + ~type_5cname() override = default; type_5cname(); bool @@ -98,9 +88,7 @@ namespace llarp { std::string revname; - virtual ~type_12ptr() - { - } + ~type_12ptr() override = default; type_12ptr(); bool @@ -115,9 +103,7 @@ namespace llarp std::string mx; uint16_t priority; - virtual ~type_15mx() - { - } + ~type_15mx() override = default; type_15mx(); bool @@ -131,9 +117,7 @@ namespace llarp { std::string txt; - virtual ~type_16txt() - { - } + ~type_16txt() override = default; type_16txt(); bool diff --git a/llarp/dns/serialize.cpp b/llarp/dns/serialize.cpp index f6c32239a..70a29c356 100644 --- a/llarp/dns/serialize.cpp +++ b/llarp/dns/serialize.cpp @@ -5,9 +5,7 @@ namespace llarp { namespace dns { - Serialize::~Serialize() - { - } + Serialize::~Serialize() = default; bool EncodeRData(llarp_buffer_t* buf, const std::vector< byte_t >& v) diff --git a/llarp/dns/server.cpp b/llarp/dns/server.cpp index 6ea3394e1..eae005b8b 100644 --- a/llarp/dns/server.cpp +++ b/llarp/dns/server.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace llarp { @@ -11,10 +12,10 @@ namespace llarp Proxy::Proxy(llarp_ev_loop_ptr serverLoop, Logic_ptr serverLogic, llarp_ev_loop_ptr clientLoop, Logic_ptr clientLogic, IQueryHandler* h) - : m_ServerLoop(serverLoop) - , m_ClientLoop(clientLoop) - , m_ServerLogic(serverLogic) - , m_ClientLogic(clientLogic) + : m_ServerLoop(std::move(serverLoop)) + , m_ClientLoop(std::move(clientLoop)) + , m_ServerLogic(std::move(serverLogic)) + , m_ClientLogic(std::move(clientLogic)) , m_QueryHandler(h) { m_Client.user = this; diff --git a/llarp/dns/server.hpp b/llarp/dns/server.hpp index b7d9aa195..f2f66ab5a 100644 --- a/llarp/dns/server.hpp +++ b/llarp/dns/server.hpp @@ -16,9 +16,7 @@ namespace llarp /// handler of dns query hooking struct IQueryHandler { - virtual ~IQueryHandler() - { - } + virtual ~IQueryHandler() = default; /// return true if we should hook this message virtual bool diff --git a/llarp/dnsc.cpp b/llarp/dnsc.cpp index 3e1034849..37cf990eb 100644 --- a/llarp/dnsc.cpp +++ b/llarp/dnsc.cpp @@ -10,12 +10,12 @@ #include /* close */ #endif -#include /* exit */ -#include /* memset */ +#include /* exit */ +#include /* memset */ #include #include // for std::find_if -#include // sprintf +#include // sprintf dns_tracker dns_udp_tracker; @@ -36,8 +36,8 @@ struct dns_query struct dns_query * build_dns_packet(char *url, uint16_t id, uint16_t reqType) { - dns_query *dnsQuery = new dns_query; - dnsQuery->length = 12; + auto *dnsQuery = new dns_query; + dnsQuery->length = 12; // ID // buffer[0] = (value & 0xFF00) >> 8; // buffer[1] = value & 0xFF; @@ -505,7 +505,7 @@ generic_handle_dnsc_recvfrom(dnsc_answer_request *request, } else if(answer->type == 15) { - llarp::dns::type_15mx *record = + auto *record = dynamic_cast< llarp::dns::type_15mx * >(answer->record.get()); llarp::LogDebug("Resolving MX ", record->mx, "@", record->priority); request->found = true; @@ -639,7 +639,7 @@ raw_resolve_host(struct dnsc_context *const dnsc, const char *url, llarp::LogInfo("response header says it belongs to id #", hdr.id); // if we sent this out, then there's an id - struct dns_tracker *tracker = (struct dns_tracker *)dnsc->tracker; + auto *tracker = (struct dns_tracker *)dnsc->tracker; struct dnsc_answer_request *request = tracker->client_request[hdr.id].get(); if(request) @@ -675,7 +675,7 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *const udp, llarp::LogDebug("Header got client responses for id: ", hdr.id); // if we sent this out, then there's an id - struct dns_tracker *tracker = (struct dns_tracker *)udp->user; + auto *tracker = (struct dns_tracker *)udp->user; struct dnsc_answer_request *request = tracker->client_request[hdr.id].get(); // sometimes we'll get double responses @@ -766,8 +766,8 @@ llarp_resolve_host(struct dnsc_context *const dnsc, const char *url, void llarp_host_resolved(dnsc_answer_request *const request) { - dns_tracker *tracker = (dns_tracker *)request->context->tracker; - auto val = std::find_if( + auto *tracker = (dns_tracker *)request->context->tracker; + auto val = std::find_if( tracker->client_request.begin(), tracker->client_request.end(), [request]( std::pair< const uint32_t, std::unique_ptr< dnsc_answer_request > > diff --git a/llarp/dnsc.hpp b/llarp/dnsc.hpp index f67ddd54a..7ae53da5b 100644 --- a/llarp/dnsc.hpp +++ b/llarp/dnsc.hpp @@ -26,7 +26,7 @@ build_dns_packet(char *url, uint16_t id, uint16_t reqType); /// hook function to handle an dns client request // should we pass by llarp::Addr // not as long as we're supporting raw -typedef void (*dnsc_answer_hook_func)(dnsc_answer_request *request); +using dnsc_answer_hook_func = void (*)(dnsc_answer_request *); /// struct for dns client requests struct dnsc_answer_request diff --git a/llarp/dnsd.cpp b/llarp/dnsd.cpp index 813c98cde..6f8eb7a42 100644 --- a/llarp/dnsd.cpp +++ b/llarp/dnsd.cpp @@ -27,7 +27,7 @@ ssize_t llarp_sendto_dns_hook_func(void *sock, const struct sockaddr *from, ManagedBuffer buf) { - struct llarp_udp_io *udp = (struct llarp_udp_io *)sock; + auto *udp = (struct llarp_udp_io *)sock; if(!udp) { llarp::LogWarn("couldnt cast to udp"); @@ -333,8 +333,7 @@ writesend_dnss_txtresponse(std::string txt, const struct sockaddr *from, void handle_dnsc_result(dnsc_answer_request *client_request) { - dnsd_question_request *server_request = - (dnsd_question_request *)client_request->user; + auto *server_request = (dnsd_question_request *)client_request->user; if(!server_request) { llarp::LogError("Couldn't map client requser user to a server request"); @@ -522,7 +521,7 @@ llarp_handle_dnsd_recvfrom(struct llarp_udp_io *udp, return; } // create new request - dnsd_question_request *llarp_dns_request = new dnsd_question_request; + auto *llarp_dns_request = new dnsd_question_request; llarp_dns_request->context = dns_udp_tracker.dnsd; // set context llarp_dns_request->from = new sockaddr(*saddr); // make a copy of the sockaddr @@ -543,7 +542,7 @@ raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr, llarp::LogError("No tracker set in dnsd context"); return; } - dnsd_question_request *llarp_dns_request = new dnsd_question_request; + auto *llarp_dns_request = new dnsd_question_request; llarp_dns_request->context = dns_udp_tracker.dnsd; // set context llarp_dns_request->from = new sockaddr(*saddr); // make a copy of the sockaddr diff --git a/llarp/ev/ev.h b/llarp/ev/ev.h index 71e772071..961dcbcc9 100644 --- a/llarp/ev/ev.h +++ b/llarp/ev/ev.h @@ -23,9 +23,9 @@ typedef SSIZE_T ssize_t; #endif #include -#include -#include -#include + +#include +#include #if !defined(WIN32) #include diff --git a/llarp/ev/ev.hpp b/llarp/ev/ev.hpp index e53e125b3..0a4de55fa 100644 --- a/llarp/ev/ev.hpp +++ b/llarp/ev/ev.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -305,7 +306,7 @@ namespace llarp struct GetNow { llarp_ev_loop_ptr loop; - GetNow(llarp_ev_loop_ptr l) : loop(l) + GetNow(llarp_ev_loop_ptr l) : loop(std::move(l)) { } @@ -319,7 +320,7 @@ namespace llarp struct PutTime { llarp_ev_loop_ptr loop; - PutTime(llarp_ev_loop_ptr l) : loop(l) + PutTime(llarp_ev_loop_ptr l) : loop(std::move(l)) { } void @@ -570,9 +571,7 @@ namespace llarp tcp.close = &DoClose; } - virtual ~tcp_conn() - { - } + ~tcp_conn() override = default; /// start connecting void @@ -631,10 +630,10 @@ namespace llarp errno = 0; } - virtual ssize_t + ssize_t do_write(void* buf, size_t sz) override; - virtual int + int read(byte_t* buf, size_t sz) override; bool @@ -652,7 +651,7 @@ namespace llarp } bool - tick() + tick() override { if(tcp->tick) tcp->tick(tcp); @@ -660,8 +659,8 @@ namespace llarp } /// actually does accept() :^) - virtual int - read(byte_t*, size_t); + int + read(byte_t*, size_t) override; }; } // namespace llarp @@ -784,9 +783,7 @@ struct llarp_ev_loop return conn && add_ev(conn, true); } - virtual ~llarp_ev_loop() - { - } + virtual ~llarp_ev_loop() = default; std::list< std::unique_ptr< llarp::ev_io > > handlers; diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index feb6221c6..3b9ac4307 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include namespace libuv { @@ -65,7 +65,7 @@ namespace libuv static void OnOutboundConnect(uv_connect_t* c, int status) { - conn_glue* self = static_cast< conn_glue* >(c->data); + auto* self = static_cast< conn_glue* >(c->data); self->HandleConnectResult(status); c->data = nullptr; } @@ -181,9 +181,9 @@ namespace libuv { m_WriteQueue.emplace_back(sz); std::copy_n(data, sz, m_WriteQueue.back().begin()); - auto buf = uv_buf_init(m_WriteQueue.back().data(), sz); - uv_write_t* req = new uv_write_t(); - req->data = this; + auto buf = uv_buf_init(m_WriteQueue.back().data(), sz); + auto* req = new uv_write_t(); + req->data = this; return uv_write(req, Stream(), &buf, 1, &OnWritten) == 0 ? sz : 0; } @@ -196,10 +196,10 @@ namespace libuv static void FullClose(uv_handle_t* h) { - conn_glue* self = static_cast< conn_glue* >(h->data); - h->data = nullptr; + auto* self = static_cast< conn_glue* >(h->data); + h->data = nullptr; delete self; - llarp::LogInfo("deleted"); + llarp::LogDebug("deleted"); } void @@ -217,7 +217,7 @@ namespace libuv m_Conn.closed(&m_Conn); } m_Conn.impl = nullptr; - llarp::LogInfo("closed"); + llarp::LogDebug("closed"); uv_close((uv_handle_t*)&m_Ticker, &FullClose); } @@ -225,7 +225,7 @@ namespace libuv OnShutdown(uv_shutdown_t* shut, int code) { llarp::LogDebug("shut down ", code); - conn_glue* self = static_cast< conn_glue* >(shut->data); + auto* self = static_cast< conn_glue* >(shut->data); uv_close((uv_handle_t*)&self->m_Handle, &OnClosed); delete shut; } @@ -236,8 +236,8 @@ namespace libuv llarp::LogDebug("close tcp connection"); uv_check_stop(&m_Ticker); uv_read_stop(Stream()); - uv_shutdown_t* shut = new uv_shutdown_t(); - shut->data = this; + auto* shut = new uv_shutdown_t(); + shut->data = this; uv_shutdown(shut, Stream(), &OnShutdown); } @@ -285,7 +285,7 @@ namespace libuv { if(m_Accept && m_Accept->accepted) { - conn_glue* child = new conn_glue(this); + auto* child = new conn_glue(this); llarp::LogDebug("accepted new connection"); child->m_Conn.impl = child; child->m_Conn.loop = m_Accept->loop; @@ -375,7 +375,7 @@ namespace libuv static int SendTo(llarp_udp_io* udp, const sockaddr* to, const byte_t* ptr, size_t sz) { - udp_glue* self = static_cast< udp_glue* >(udp->impl); + auto* self = static_cast< udp_glue* >(udp->impl); if(self == nullptr) return -1; uv_buf_t buf = uv_buf_init((char*)ptr, sz); @@ -410,7 +410,7 @@ namespace libuv static void OnClosed(uv_handle_t* h) { - udp_glue* glue = static_cast< udp_glue* >(h->data); + auto* glue = static_cast< udp_glue* >(h->data); if(glue) { h->data = nullptr; @@ -443,7 +443,7 @@ namespace libuv readpkt = false; } - ~tun_glue() + ~tun_glue() override { tuntap_destroy(m_Device); } @@ -488,7 +488,7 @@ namespace libuv static void OnClosed(uv_handle_t* h) { - tun_glue* self = static_cast< tun_glue* >(h->data); + auto* self = static_cast< tun_glue* >(h->data); if(self) { self->m_Tun->impl = nullptr; @@ -597,8 +597,8 @@ namespace libuv bool Loop::tcp_connect(llarp_tcp_connecter* tcp, const sockaddr* addr) { - conn_glue* impl = new conn_glue(m_Impl.get(), tcp, addr); - tcp->impl = impl; + auto* impl = new conn_glue(m_Impl.get(), tcp, addr); + tcp->impl = impl; if(impl->ConnectAsync()) return true; delete impl; @@ -633,16 +633,17 @@ namespace libuv Loop::CloseAll() { llarp::LogInfo("Closing all handles"); - uv_walk(m_Impl.get(), - [](uv_handle_t* h, void*) { - if(uv_is_closing(h)) - return; - if(h->data && uv_is_active(h)) - { - static_cast< glue* >(h->data)->Close(); - } - }, - nullptr); + uv_walk( + m_Impl.get(), + [](uv_handle_t* h, void*) { + if(uv_is_closing(h)) + return; + if(h->data && uv_is_active(h)) + { + static_cast< glue* >(h->data)->Close(); + } + }, + nullptr); } void @@ -655,8 +656,8 @@ namespace libuv bool Loop::udp_listen(llarp_udp_io* udp, const sockaddr* src) { - udp_glue* impl = new udp_glue(m_Impl.get(), udp, src); - udp->impl = impl; + auto* impl = new udp_glue(m_Impl.get(), udp, src); + udp->impl = impl; if(impl->Bind()) { return true; @@ -670,7 +671,7 @@ namespace libuv { if(udp == nullptr) return false; - udp_glue* glue = static_cast< udp_glue* >(udp->impl); + auto* glue = static_cast< udp_glue* >(udp->impl); if(glue == nullptr) return false; glue->Close(); @@ -680,8 +681,8 @@ namespace libuv bool Loop::tun_listen(llarp_tun_io* tun) { - tun_glue* glue = new tun_glue(tun); - tun->impl = glue; + auto* glue = new tun_glue(tun); + tun->impl = glue; if(glue->Init(m_Impl.get())) { return true; @@ -693,8 +694,8 @@ namespace libuv bool Loop::tcp_listen(llarp_tcp_acceptor* tcp, const sockaddr* addr) { - conn_glue* glue = new conn_glue(m_Impl.get(), tcp, addr); - tcp->impl = glue; + auto* glue = new conn_glue(m_Impl.get(), tcp, addr); + tcp->impl = glue; if(glue->Server()) return true; tcp->impl = nullptr; diff --git a/llarp/ev/pipe.cpp b/llarp/ev/pipe.cpp index b92f5ba0b..76d141a9e 100644 --- a/llarp/ev/pipe.cpp +++ b/llarp/ev/pipe.cpp @@ -1,4 +1,5 @@ #include +#include #ifndef _MSC_VER #include @@ -6,7 +7,7 @@ #include llarp_ev_pkt_pipe::llarp_ev_pkt_pipe(llarp_ev_loop_ptr loop) - : llarp::ev_io(-1, new LosslessWriteQueue_t()), m_Loop(loop) + : llarp::ev_io(-1, new LosslessWriteQueue_t()), m_Loop(std::move(loop)) { } diff --git a/llarp/ev/pipe.hpp b/llarp/ev/pipe.hpp index 6800b1b8b..0bf1953fc 100644 --- a/llarp/ev/pipe.hpp +++ b/llarp/ev/pipe.hpp @@ -25,7 +25,7 @@ struct llarp_ev_pkt_pipe : public llarp::ev_io ssize_t do_write(void* buf, size_t sz) override; - virtual bool + bool tick() override; int diff --git a/llarp/exit/context.cpp b/llarp/exit/context.cpp index f8e995c92..b8560ba2c 100644 --- a/llarp/exit/context.cpp +++ b/llarp/exit/context.cpp @@ -1,4 +1,5 @@ #include +#include namespace llarp { @@ -7,9 +8,7 @@ namespace llarp Context::Context(AbstractRouter* r) : m_Router(r) { } - Context::~Context() - { - } + Context::~Context() = default; void Context::Tick(llarp_time_t now) @@ -112,7 +111,7 @@ namespace llarp } std::unique_ptr< handlers::ExitEndpoint > endpoint; // make new endpoint - endpoint.reset(new handlers::ExitEndpoint(name, m_Router)); + endpoint = std::make_unique< handlers::ExitEndpoint >(name, m_Router); // configure { auto itr = conf.begin(); diff --git a/llarp/exit/exit_messages.hpp b/llarp/exit/exit_messages.hpp index 168c3833e..6f7ffffbb 100644 --- a/llarp/exit/exit_messages.hpp +++ b/llarp/exit/exit_messages.hpp @@ -14,20 +14,18 @@ namespace llarp struct ObtainExitMessage final : public IMessage { std::vector< llarp::exit::Policy > B; - uint64_t E; + uint64_t E{0}; llarp::PubKey I; - uint64_t T; + uint64_t T{0}; std::vector< llarp::exit::Policy > W; - llarp_time_t X; + llarp_time_t X{0}; llarp::Signature Z; - ObtainExitMessage() : IMessage(), E(0), T(0), X(0) + ObtainExitMessage() : IMessage() { } - ~ObtainExitMessage() - { - } + ~ObtainExitMessage() override = default; void Clear() override @@ -132,7 +130,7 @@ namespace llarp Nonce_t Y; llarp::Signature Z; - ~UpdateExitVerifyMessage() = default; + ~UpdateExitVerifyMessage() override = default; void Clear() override diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 9ae0b5d01..4d4296b90 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace llarp { @@ -17,7 +18,7 @@ namespace llarp AbstractRouter* r, size_t numpaths, size_t hoplen, bool bundleRC) : llarp::path::Builder(r, numpaths, hoplen) , m_ExitRouter(routerId) - , m_WritePacket(writepkt) + , m_WritePacket(std::move(writepkt)) , m_Counter(0) , m_LastUse(0) , m_BundleRC(bundleRC) @@ -25,9 +26,7 @@ namespace llarp CryptoManager::instance()->identity_keygen(m_ExitIdentity); } - BaseSession::~BaseSession() - { - } + BaseSession::~BaseSession() = default; void BaseSession::HandlePathDied(path::Path_ptr p) diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index 449c2fb77..550173ce6 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -31,7 +31,7 @@ namespace llarp AbstractRouter* r, size_t numpaths, size_t hoplen, bool bundleRC); - virtual ~BaseSession(); + ~BaseSession() override; std::shared_ptr< path::PathSet > GetSelf() override @@ -51,7 +51,7 @@ namespace llarp return m_BundleRC; } - virtual void + void ResetInternalState() override; bool UrgentBuild(llarp_time_t) const override; @@ -175,13 +175,13 @@ namespace llarp { } - ~ExitSession() = default; + ~ExitSession() override = default; std::string Name() const override; protected: - virtual void + void PopulateRequest(llarp::routing::ObtainExitMessage& msg) const override { // TODO: set expiration time @@ -197,7 +197,7 @@ namespace llarp AbstractRouter* r, size_t numpaths, size_t hoplen, bool useRouterSNodeKey, bool bundleRC); - ~SNodeSession() = default; + ~SNodeSession() override = default; std::string Name() const override; diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 18524ceec..4ec317e00 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -29,7 +29,8 @@ namespace llarp , m_Resolver(std::make_shared< dns::Proxy >( r->netloop(), r->logic(), r->netloop(), r->logic(), this)) , m_Name(name) - , m_Tun{{0}, 0, {0}, 0, 0, 0, 0, 0, 0, 0, 0} + , m_Tun{{0}, 0, {0}, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr} , m_LocalResolverAddr("127.0.0.1", 53) , m_InetToNetwork(name + "_exit_rx", r->netloop(), r->netloop()) @@ -40,9 +41,7 @@ namespace llarp m_ShouldInitTun = true; } - ExitEndpoint::~ExitEndpoint() - { - } + ExitEndpoint::~ExitEndpoint() = default; util::StatusObject ExitEndpoint::ExtractStatus() const diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index d8d6abbab..21a499160 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -14,7 +14,7 @@ namespace llarp struct ExitEndpoint : public dns::IQueryHandler { ExitEndpoint(const std::string& name, AbstractRouter* r); - ~ExitEndpoint(); + ~ExitEndpoint() override; void Tick(llarp_time_t now); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index d796fd033..2292ff1ee 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -31,7 +31,7 @@ namespace llarp static void tunifTick(llarp_tun_io *tun) { - TunEndpoint *self = static_cast< TunEndpoint * >(tun->user); + auto *self = static_cast< TunEndpoint * >(tun->user); self->Flush(); } @@ -412,7 +412,7 @@ namespace llarp } else { - dns::Message *replyMsg = new dns::Message(std::move(msg)); + auto *replyMsg = new dns::Message(std::move(msg)); using service::Address; using service::OutboundContext; return EnsurePathToService( @@ -432,7 +432,7 @@ namespace llarp } else { - dns::Message *replyMsg = new dns::Message(std::move(msg)); + auto *replyMsg = new dns::Message(std::move(msg)); EnsurePathToSNode(addr.as_array(), [=](const RouterID &, exit::BaseSession_ptr s) { SendDNSReply(addr, s, replyMsg, reply, true, @@ -577,7 +577,7 @@ namespace llarp return false; } - struct addrinfo hint, *res = NULL; + struct addrinfo hint, *res = nullptr; int ret; memset(&hint, 0, sizeof hint); @@ -585,7 +585,7 @@ namespace llarp hint.ai_family = PF_UNSPEC; hint.ai_flags = AI_NUMERICHOST; - ret = getaddrinfo(tunif.ifaddr, NULL, &hint, &res); + ret = getaddrinfo(tunif.ifaddr, nullptr, &hint, &res); if(ret) { llarp::LogError(Name(), @@ -909,7 +909,7 @@ namespace llarp TunEndpoint::tunifBeforeWrite(llarp_tun_io *tun) { // called in the isolated network thread - TunEndpoint *self = static_cast< TunEndpoint * >(tun->user); + auto *self = static_cast< TunEndpoint * >(tun->user); // flush user to network self->FlushSend(); // flush exit traffic queues if it's there @@ -928,15 +928,13 @@ namespace llarp TunEndpoint::tunifRecvPkt(llarp_tun_io *tun, const llarp_buffer_t &b) { // called for every packet read from user in isolated network thread - TunEndpoint *self = static_cast< TunEndpoint * >(tun->user); + auto *self = static_cast< TunEndpoint * >(tun->user); const ManagedBuffer buf(b); self->m_UserToNetworkPktQueue.EmplaceIf( [&buf](net::IPPacket &pkt) -> bool { return pkt.Load(buf); }); } - TunEndpoint::~TunEndpoint() - { - } + TunEndpoint::~TunEndpoint() = default; } // namespace handlers } // namespace llarp diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 98d47daf7..8af830e07 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -21,7 +21,7 @@ namespace llarp { TunEndpoint(const std::string& nickname, AbstractRouter* r, llarp::service::Context* parent); - ~TunEndpoint(); + ~TunEndpoint() override; path::PathSet_ptr GetSelf() override @@ -29,10 +29,10 @@ namespace llarp return shared_from_this(); } - virtual bool + bool SetOption(const std::string& k, const std::string& v) override; - virtual void + void Tick(llarp_time_t now) override; util::StatusObject @@ -165,7 +165,7 @@ namespace llarp void Flush(); - virtual void + void ResetInternalState() override; protected: diff --git a/llarp/hook/ihook.hpp b/llarp/hook/ihook.hpp index 83d57d0fa..553de68e0 100644 --- a/llarp/hook/ihook.hpp +++ b/llarp/hook/ihook.hpp @@ -26,9 +26,7 @@ namespace llarp using Backend_ptr = std::shared_ptr< IBackend >; - inline IBackend::~IBackend() - { - } + inline IBackend::~IBackend() = default; } // namespace hooks } // namespace llarp diff --git a/llarp/iwp/linklayer.cpp b/llarp/iwp/linklayer.cpp index 2f0af3226..8a984d7c4 100644 --- a/llarp/iwp/linklayer.cpp +++ b/llarp/iwp/linklayer.cpp @@ -13,9 +13,7 @@ namespace llarp m_FlowCookie.Randomize(); } - LinkLayer::~LinkLayer() - { - } + LinkLayer::~LinkLayer() = default; void LinkLayer::Pump() diff --git a/llarp/iwp/linklayer.hpp b/llarp/iwp/linklayer.hpp index 50f9f3557..e7265188e 100644 --- a/llarp/iwp/linklayer.hpp +++ b/llarp/iwp/linklayer.hpp @@ -19,7 +19,7 @@ namespace llarp SessionRenegotiateHandler reneg, SignBufferFunc sign, TimeoutHandler timeout, SessionClosedHandler closed); - ~LinkLayer(); + ~LinkLayer() override; bool Start(std::shared_ptr< Logic > l) override; diff --git a/llarp/iwp/outermessage.cpp b/llarp/iwp/outermessage.cpp index bb3b9b724..3d7ad2af7 100644 --- a/llarp/iwp/outermessage.cpp +++ b/llarp/iwp/outermessage.cpp @@ -1,4 +1,5 @@ #include +#include namespace llarp { @@ -15,9 +16,7 @@ namespace llarp Clear(); } - OuterMessage::~OuterMessage() - { - } + OuterMessage::~OuterMessage() = default; void OuterMessage::Clear() @@ -131,7 +130,7 @@ namespace llarp return false; if(buf->size_left() == Zsig.size() + 32) { - A.reset(new AlignedBuffer< 32 >()); + A = std::make_unique< AlignedBuffer< 32 > >(); if(!buf->read_into(A->begin(), A->end())) return false; } diff --git a/llarp/link/link_manager.hpp b/llarp/link/link_manager.hpp index 4e9296781..17f29f8c3 100644 --- a/llarp/link/link_manager.hpp +++ b/llarp/link/link_manager.hpp @@ -18,7 +18,7 @@ namespace llarp struct LinkManager final : public ILinkManager { public: - ~LinkManager() = default; + ~LinkManager() override = default; LinkLayer_ptr GetCompatibleLink(const RouterContact &rc) const override; @@ -71,7 +71,7 @@ namespace llarp void CheckPersistingSessions(llarp_time_t now) override; - virtual util::StatusObject + util::StatusObject ExtractStatus() const override; void diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index ec6da7c3d..73da6dcb3 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -2,6 +2,7 @@ #include #include +#include namespace llarp { @@ -12,20 +13,18 @@ namespace llarp SessionEstablishedHandler establishedSession, SessionRenegotiateHandler reneg, TimeoutHandler timeout, SessionClosedHandler closed) - : HandleMessage(handler) - , HandleTimeout(timeout) - , Sign(signbuf) - , GetOurRC(getrc) - , SessionEstablished(establishedSession) - , SessionClosed(closed) - , SessionRenegotiate(reneg) + : HandleMessage(std::move(handler)) + , HandleTimeout(std::move(timeout)) + , Sign(std::move(signbuf)) + , GetOurRC(std::move(getrc)) + , SessionEstablished(std::move(establishedSession)) + , SessionClosed(std::move(closed)) + , SessionRenegotiate(std::move(reneg)) , m_RouterEncSecret(routerEncSecret) { } - ILinkLayer::~ILinkLayer() - { - } + ILinkLayer::~ILinkLayer() = default; bool ILinkLayer::HasSessionTo(const RouterID& id) diff --git a/llarp/link/session.hpp b/llarp/link/session.hpp index 5b904f633..39dc1ae96 100644 --- a/llarp/link/session.hpp +++ b/llarp/link/session.hpp @@ -16,9 +16,7 @@ namespace llarp struct ILinkSession { - virtual ~ILinkSession() - { - } + virtual ~ILinkSession() = default; /// delivery status of a message enum class DeliveryStatus diff --git a/llarp/messages/dht_immediate.hpp b/llarp/messages/dht_immediate.hpp index 88f4e765d..31afbba9e 100644 --- a/llarp/messages/dht_immediate.hpp +++ b/llarp/messages/dht_immediate.hpp @@ -10,8 +10,8 @@ namespace llarp { struct DHTImmediateMessage final : public ILinkMessage { - DHTImmediateMessage() = default; - ~DHTImmediateMessage() = default; + DHTImmediateMessage() = default; + ~DHTImmediateMessage() override = default; std::vector< std::unique_ptr< dht::IMessage > > msgs; diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index b414d95d4..a4f3c8d1c 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -31,7 +31,7 @@ namespace llarp { return bencode_read_integer(buf, &P); } - else if(key == "r") + if(key == "r") { if(rc.BDecode(buf)) return true; @@ -39,7 +39,7 @@ namespace llarp llarp::DumpBuffer(*buf); return false; } - else if(key == "v") + if(key == "v") { if(!bencode_read_integer(buf, &version)) return false; @@ -52,15 +52,13 @@ namespace llarp llarp::LogDebug("LIM version ", version); return true; } - else if(key == "z") + if(key == "z") { return Z.BDecode(buf); } - else - { - llarp::LogWarn("invalid LIM key: ", *key.cur); - return false; - } + + llarp::LogWarn("invalid LIM key: ", *key.cur); + return false; } bool diff --git a/llarp/messages/link_message_parser.cpp b/llarp/messages/link_message_parser.cpp index 3e8859ad5..e7fd1b16b 100644 --- a/llarp/messages/link_message_parser.cpp +++ b/llarp/messages/link_message_parser.cpp @@ -37,9 +37,7 @@ namespace llarp { } - LinkMessageParser::~LinkMessageParser() - { - } + LinkMessageParser::~LinkMessageParser() = default; bool LinkMessageParser::operator()(llarp_buffer_t* buffer, llarp_buffer_t* key) diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index d0a19bd55..a269590be 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -171,9 +171,9 @@ namespace llarp struct LRCMFrameDecrypt { - typedef llarp::path::PathContext Context; - typedef llarp::path::TransitHop Hop; - typedef AsyncFrameDecrypter< LRCMFrameDecrypt > Decrypter; + using Context = llarp::path::PathContext; + using Hop = llarp::path::TransitHop; + using Decrypter = AsyncFrameDecrypter< LRCMFrameDecrypt >; using Decrypter_ptr = std::unique_ptr< Decrypter >; Decrypter_ptr decrypter; std::array< EncryptedFrame, 8 > frames; @@ -193,9 +193,7 @@ namespace llarp hop->info.downstream = commit->session->GetPubKey(); } - ~LRCMFrameDecrypt() - { - } + ~LRCMFrameDecrypt() = default; static void OnForwardLRCMResult(AbstractRouter* router, const PathID_t pathid, diff --git a/llarp/messages/relay_commit.hpp b/llarp/messages/relay_commit.hpp index 86abf5d95..7c54349fc 100644 --- a/llarp/messages/relay_commit.hpp +++ b/llarp/messages/relay_commit.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace llarp { @@ -49,14 +50,14 @@ namespace llarp { std::array< EncryptedFrame, 8 > frames; - LR_CommitMessage(const std::array< EncryptedFrame, 8 > &_frames) - : ILinkMessage(), frames(_frames) + LR_CommitMessage(std::array< EncryptedFrame, 8 > _frames) + : ILinkMessage(), frames(std::move(_frames)) { } LR_CommitMessage() = default; - ~LR_CommitMessage() = default; + ~LR_CommitMessage() override = default; void Clear() override; diff --git a/llarp/messages/relay_status.cpp b/llarp/messages/relay_status.cpp index 5ca6b07d1..d72e41d87 100644 --- a/llarp/messages/relay_status.cpp +++ b/llarp/messages/relay_status.cpp @@ -12,6 +12,7 @@ #include #include +#include namespace llarp { @@ -25,10 +26,12 @@ namespace llarp HopHandler_ptr path; AbstractRouter* router; - LRSM_AsyncHandler(const std::array< EncryptedFrame, 8 >& _frames, - uint64_t _status, HopHandler_ptr _path, - AbstractRouter* _router) - : frames(_frames), status(_status), path(_path), router(_router) + LRSM_AsyncHandler(std::array< EncryptedFrame, 8 > _frames, uint64_t _status, + HopHandler_ptr _path, AbstractRouter* _router) + : frames(std::move(_frames)) + , status(_status) + , path(std::move(_path)) + , router(_router) { } @@ -57,7 +60,7 @@ namespace llarp { return BEncodeReadArray(frames, buf); } - else if(key == "p") + if(key == "p") { if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf)) { diff --git a/llarp/messages/relay_status.hpp b/llarp/messages/relay_status.hpp index fd8684123..2e744f8dd 100644 --- a/llarp/messages/relay_status.hpp +++ b/llarp/messages/relay_status.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace llarp { @@ -56,14 +57,14 @@ namespace llarp uint64_t status = 0; - LR_StatusMessage(const std::array< EncryptedFrame, 8 > &_frames) - : ILinkMessage(), frames(_frames) + LR_StatusMessage(std::array< EncryptedFrame, 8 > _frames) + : ILinkMessage(), frames(std::move(_frames)) { } LR_StatusMessage() = default; - ~LR_StatusMessage() = default; + ~LR_StatusMessage() override = default; void Clear() override; diff --git a/llarp/metrics/json_publisher.hpp b/llarp/metrics/json_publisher.hpp index d0cc2b06c..b684a2495 100644 --- a/llarp/metrics/json_publisher.hpp +++ b/llarp/metrics/json_publisher.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace llarp { @@ -22,13 +23,11 @@ namespace llarp PublishFunction m_publish; public: - JsonPublisher(const PublishFunction& publish) : m_publish(publish) + JsonPublisher(PublishFunction publish) : m_publish(std::move(publish)) { } - ~JsonPublisher() - { - } + ~JsonPublisher() override = default; void publish(const Sample& values) override; diff --git a/llarp/metrics/metrictank_publisher.hpp b/llarp/metrics/metrictank_publisher.hpp index 23fbb699f..044b4ec87 100644 --- a/llarp/metrics/metrictank_publisher.hpp +++ b/llarp/metrics/metrictank_publisher.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace llarp @@ -32,9 +33,7 @@ namespace llarp { } - ~MetricTankPublisherInterface() - { - } + ~MetricTankPublisherInterface() override = default; static std::string makeSuffix(const Tags& tags); @@ -66,16 +65,16 @@ namespace llarp work(); public: - MetricTankPublisher(const Tags& tags, const std::string& host, short port) + MetricTankPublisher(const Tags& tags, std::string host, short port) : MetricTankPublisherInterface(tags) - , m_host(host) + , m_host(std::move(host)) , m_port(port) , m_queue(100) , m_worker(&MetricTankPublisher::work, this) { } - ~MetricTankPublisher() + ~MetricTankPublisher() override { // Push back a signal value onto the queue m_queue.pushBack(StopStruct()); diff --git a/llarp/metrics/stream_publisher.hpp b/llarp/metrics/stream_publisher.hpp index a50ebe686..4fbb9ff7a 100644 --- a/llarp/metrics/stream_publisher.hpp +++ b/llarp/metrics/stream_publisher.hpp @@ -18,7 +18,7 @@ namespace llarp { } - ~StreamPublisher() = default; + ~StreamPublisher() override = default; void publish(const Sample& values) override; diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index 6a81f7490..1ae3a7c1c 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index 48ec5f6d1..401571fe6 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -8,7 +8,6 @@ #include #include -#include /** * address_info.hpp diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index 9c4435356..be1e6efd3 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/net/exit_info.hpp b/llarp/net/exit_info.hpp index b7355abee..c1fe6f678 100644 --- a/llarp/net/exit_info.hpp +++ b/llarp/net/exit_info.hpp @@ -23,9 +23,7 @@ namespace llarp PubKey pubkey; uint64_t version = LLARP_PROTO_VERSION; - ExitInfo() - { - } + ExitInfo() = default; ExitInfo(const PubKey &pk, const nuint32_t &ipv4_exit) : pubkey(pk) { diff --git a/llarp/net/ip.cpp b/llarp/net/ip.cpp index 8723d7636..80df045e3 100644 --- a/llarp/net/ip.cpp +++ b/llarp/net/ip.cpp @@ -71,8 +71,8 @@ namespace llarp { if(IsV6()) return In6ToHUInt(HeaderV6()->srcaddr); - else - return ExpandV4(srcv4()); + + return ExpandV4(srcv4()); } huint128_t @@ -80,8 +80,8 @@ namespace llarp { if(IsV6()) return In6ToHUInt(HeaderV6()->dstaddr); - else - return ExpandV4(dstv4()); + + return ExpandV4(dstv4()); } bool diff --git a/llarp/net/ip.hpp b/llarp/net/ip.hpp index 49f062e2b..83ddf3714 100644 --- a/llarp/net/ip.hpp +++ b/llarp/net/ip.hpp @@ -88,6 +88,7 @@ struct ipv6_header #include #include +#include struct llarp_ev_loop; @@ -136,7 +137,7 @@ namespace llarp struct PutTime { llarp_ev_loop_ptr loop; - PutTime(llarp_ev_loop_ptr evloop) : loop(evloop) + PutTime(llarp_ev_loop_ptr evloop) : loop(std::move(evloop)) { } void @@ -149,7 +150,7 @@ namespace llarp struct GetNow { llarp_ev_loop_ptr loop; - GetNow(llarp_ev_loop_ptr evloop) : loop(evloop) + GetNow(llarp_ev_loop_ptr evloop) : loop(std::move(evloop)) { } llarp_time_t @@ -224,10 +225,10 @@ namespace llarp { if(IsV4()) return service::eProtocolTrafficV4; - else if(IsV6()) + if(IsV6()) return service::eProtocolTrafficV6; - else - return service::eProtocolControl; + + return service::eProtocolControl; } huint128_t diff --git a/llarp/net/net.cpp b/llarp/net/net.cpp index 010797d3c..f55916912 100644 --- a/llarp/net/net.cpp +++ b/llarp/net/net.cpp @@ -786,7 +786,7 @@ llarp_getifaddr(const char* ifname, int af, struct sockaddr* addr) if(af == AF_INET6) { // set scope id - sockaddr_in6* ip6addr = (sockaddr_in6*)addr; + auto* ip6addr = (sockaddr_in6*)addr; ip6addr->sin6_scope_id = if_nametoindex(ifname); ip6addr->sin6_flowinfo = 0; } @@ -861,8 +861,8 @@ namespace llarp const auto fam = i->ifa_addr->sa_family; if(fam != AF_INET) return; - sockaddr_in* addr = (sockaddr_in*)i->ifa_addr; - sockaddr_in* mask = (sockaddr_in*)i->ifa_netmask; + auto* addr = (sockaddr_in*)i->ifa_addr; + auto* mask = (sockaddr_in*)i->ifa_netmask; nuint32_t ifaddr{addr->sin_addr.s_addr}; nuint32_t ifmask{mask->sin_addr.s_addr}; currentRanges.emplace_back( @@ -926,7 +926,7 @@ namespace llarp GetIFAddr(const std::string& ifname, Addr& addr, int af) { sockaddr_storage s; - sockaddr* sptr = (sockaddr*)&s; + auto* sptr = (sockaddr*)&s; if(!llarp_getifaddr(ifname.c_str(), af, sptr)) return false; addr = *sptr; diff --git a/llarp/net/net.h b/llarp/net/net.h index 55b349cad..fbcb02411 100644 --- a/llarp/net/net.h +++ b/llarp/net/net.h @@ -25,7 +25,7 @@ typedef unsigned int in_addr_t; #include #include #endif -#include + #include bool diff --git a/llarp/net/net.hpp b/llarp/net/net.hpp index f9848c948..044e512f6 100644 --- a/llarp/net/net.hpp +++ b/llarp/net/net.hpp @@ -11,7 +11,7 @@ #include #include -#include // for itoa +#include // for itoa #include // for addrinfo diff --git a/llarp/net/net_addr.cpp b/llarp/net/net_addr.cpp index de2387e51..bed44a9c6 100644 --- a/llarp/net/net_addr.cpp +++ b/llarp/net/net_addr.cpp @@ -16,12 +16,8 @@ namespace llarp { - Addr::Addr() - { - } - Addr::~Addr() - { - } + Addr::Addr() = default; + Addr::~Addr() = default; void Addr::port(uint16_t port) @@ -87,7 +83,7 @@ namespace llarp this->port(port); } Zero(&_addr, sizeof(sockaddr_in6)); - struct addrinfo hint, *res = NULL; + struct addrinfo hint, *res = nullptr; int ret; memset(&hint, '\0', sizeof hint); @@ -98,11 +94,11 @@ namespace llarp if(pPosition != string_view::npos) { ret = getaddrinfo(std::string(in.begin(), in.begin() + pPosition).c_str(), - NULL, &hint, &res); + nullptr, &hint, &res); } else { - ret = getaddrinfo(std::string(in).c_str(), NULL, &hint, &res); + ret = getaddrinfo(std::string(in).c_str(), nullptr, &hint, &res); } if(ret) @@ -152,7 +148,7 @@ namespace llarp { Zero(&_addr, sizeof(sockaddr_in6)); struct in_addr* addr = &_addr4.sin_addr; - unsigned char* ip = (unsigned char*)&(addr->s_addr); + auto* ip = (unsigned char*)&(addr->s_addr); _addr.sin6_family = AF_INET; // set ipv4 mode _addr4.sin_family = AF_INET; @@ -311,11 +307,11 @@ namespace llarp { case AF_INET: { - sockaddr_in* ipv4_dst = (sockaddr_in*)other; - dst = (void*)&ipv4_dst->sin_addr.s_addr; - src = (void*)&_addr4.sin_addr.s_addr; - ptr = &((sockaddr_in*)other)->sin_port; - slen = sizeof(in_addr); + auto* ipv4_dst = (sockaddr_in*)other; + dst = (void*)&ipv4_dst->sin_addr.s_addr; + src = (void*)&_addr4.sin_addr.s_addr; + ptr = &((sockaddr_in*)other)->sin_port; + slen = sizeof(in_addr); break; } case AF_INET6: diff --git a/llarp/net/net_inaddr.cpp b/llarp/net/net_inaddr.cpp index 45af70628..1617bf3a3 100644 --- a/llarp/net/net_inaddr.cpp +++ b/llarp/net/net_inaddr.cpp @@ -32,7 +32,7 @@ namespace llarp this->reset(); // maybe refactor the family detection out - struct addrinfo hint, *res = NULL; + struct addrinfo hint, *res = nullptr; int ret; memset(&hint, '\0', sizeof hint); @@ -40,7 +40,7 @@ namespace llarp hint.ai_family = PF_UNSPEC; hint.ai_flags = AI_NUMERICHOST; - ret = getaddrinfo(str, NULL, &hint, &res); + ret = getaddrinfo(str, nullptr, &hint, &res); if(ret) { llarp::LogError("failed to determine address family: ", str); diff --git a/llarp/net/net_int.hpp b/llarp/net/net_int.hpp index 5a64093ce..5421abc4d 100644 --- a/llarp/net/net_int.hpp +++ b/llarp/net/net_int.hpp @@ -14,7 +14,7 @@ #include -#include // for itoa +#include // for itoa #include #include #include diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 9d7134258..9490f37e7 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -12,12 +12,13 @@ #include #include +#include static const char skiplist_subdirs[] = "0123456789abcdef"; static const std::string RC_FILE_EXT = ".signed"; -llarp_nodedb::NetDBEntry::NetDBEntry(const llarp::RouterContact &value) - : rc(value), inserted(llarp::time_now_ms()) +llarp_nodedb::NetDBEntry::NetDBEntry(llarp::RouterContact value) + : rc(std::move(value)), inserted(llarp::time_now_ms()) { } @@ -151,7 +152,7 @@ llarp_nodedb::UpdateAsyncIfNewer(llarp::RouterContact rc, InsertAsync(rc, logic, completionHandler); return true; } - else if(itr != entries.end()) + if(itr != entries.end()) { // insertion time is set on...insertion. But it should be updated here // even if there is no insertion of a new RC, to show that the existing one @@ -362,8 +363,7 @@ llarp_nodedb::Save() void logic_threadworker_callback(void *user) { - llarp_async_verify_rc *verify_request = - static_cast< llarp_async_verify_rc * >(user); + auto *verify_request = static_cast< llarp_async_verify_rc * >(user); if(verify_request->hook) verify_request->hook(verify_request); } @@ -382,8 +382,7 @@ disk_threadworker_setRC(llarp_async_verify_rc *verify_request) void crypto_threadworker_verifyrc(void *user) { - llarp_async_verify_rc *verify_request = - static_cast< llarp_async_verify_rc * >(user); + auto *verify_request = static_cast< llarp_async_verify_rc * >(user); llarp::RouterContact rc = verify_request->rc; verify_request->valid = rc.Verify(llarp::time_now_ms()); // if it's valid we need to set it @@ -404,14 +403,14 @@ crypto_threadworker_verifyrc(void *user) void nodedb_inform_load_rc(void *user) { - llarp_async_load_rc *job = static_cast< llarp_async_load_rc * >(user); + auto *job = static_cast< llarp_async_load_rc * >(user); job->hook(job); } void nodedb_async_load_rc(void *user) { - llarp_async_load_rc *job = static_cast< llarp_async_load_rc * >(user); + auto *job = static_cast< llarp_async_load_rc * >(user); auto fpath = job->nodedb->getRCFilePath(job->pubkey); job->loaded = job->nodedb->loadfile(fpath); diff --git a/llarp/nodedb.hpp b/llarp/nodedb.hpp index 29d29faa7..d88eda827 100644 --- a/llarp/nodedb.hpp +++ b/llarp/nodedb.hpp @@ -10,6 +10,7 @@ #include #include +#include #ifdef _MSC_VER #include @@ -45,7 +46,7 @@ struct llarp_nodedb_iter struct llarp_nodedb { explicit llarp_nodedb(std::shared_ptr< llarp::thread::ThreadPool > diskworker) - : disk(diskworker) + : disk(std::move(diskworker)) { } @@ -62,7 +63,7 @@ struct llarp_nodedb const llarp::RouterContact rc; llarp_time_t inserted; - NetDBEntry(const llarp::RouterContact &data); + NetDBEntry(llarp::RouterContact data); }; using NetDBMap_t = diff --git a/llarp/path/ihophandler.hpp b/llarp/path/ihophandler.hpp index f3a4e5685..ea672d74b 100644 --- a/llarp/path/ihophandler.hpp +++ b/llarp/path/ihophandler.hpp @@ -22,9 +22,7 @@ namespace llarp { struct IHopHandler { - virtual ~IHopHandler() - { - } + virtual ~IHopHandler() = default; virtual bool Expired(llarp_time_t now) const = 0; diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index 97f69d3fe..7b56876c2 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -103,8 +103,8 @@ namespace llarp } bool - Path::HandleLRSM(uint64_t status, - std::array< EncryptedFrame, 8 >& frames, AbstractRouter* r) + Path::HandleLRSM(uint64_t status, std::array< EncryptedFrame, 8 >& frames, + AbstractRouter* r) { uint64_t currentStatus = status; @@ -215,7 +215,7 @@ namespace llarp m_PathSet->HandlePathBuildFailed(shared_from_this()); return; } - else if(st == ePathExpired && _status == ePathBuilding) + if(st == ePathExpired && _status == ePathBuilding) { _status = st; m_PathSet->HandlePathBuildTimeout(shared_from_this()); diff --git a/llarp/path/path_context.cpp b/llarp/path/path_context.cpp index b2460f84b..84a19f575 100644 --- a/llarp/path/path_context.cpp +++ b/llarp/path/path_context.cpp @@ -164,14 +164,15 @@ namespace llarp HopHandler_ptr PathContext::GetByUpstream(const RouterID& remote, const PathID_t& id) { - auto own = MapGet(m_OurPaths, id, - [](const PathSet_ptr) -> bool { - // TODO: is this right? - return true; - }, - [remote, id](PathSet_ptr p) -> HopHandler_ptr { - return p->GetByUpstream(remote, id); - }); + auto own = MapGet( + m_OurPaths, id, + [](const PathSet_ptr) -> bool { + // TODO: is this right? + return true; + }, + [remote, id](PathSet_ptr p) -> HopHandler_ptr { + return p->GetByUpstream(remote, id); + }); if(own) return own; diff --git a/llarp/path/path_context.hpp b/llarp/path/path_context.hpp index f6b9add0d..fce0f1454 100644 --- a/llarp/path/path_context.hpp +++ b/llarp/path/path_context.hpp @@ -107,6 +107,7 @@ namespace llarp void ForEach(std::function< void(const TransitHop_ptr&) > visit) + LOCKS_EXCLUDED(first) { util::Lock lock(&first); for(const auto& item : second) diff --git a/llarp/path/pathbuilder.hpp b/llarp/path/pathbuilder.hpp index fe49954f5..8e35f89d8 100644 --- a/llarp/path/pathbuilder.hpp +++ b/llarp/path/pathbuilder.hpp @@ -53,18 +53,18 @@ namespace llarp util::StatusObject ExtractStatus() const; - virtual bool + bool SelectHop(llarp_nodedb* db, const std::set< RouterID >& prev, RouterContact& cur, size_t hop, PathRole roles) override; - virtual bool + bool ShouldBuildMore(llarp_time_t now) const override; /// should we bundle RCs in builds? virtual bool ShouldBundleRC() const = 0; - virtual void + void ResetInternalState() override; /// return true if we hit our soft limit for building paths too fast @@ -78,7 +78,7 @@ namespace llarp return ePathRoleAny; } - virtual bool + bool Stop() override; bool @@ -90,7 +90,7 @@ namespace llarp llarp_time_t Now() const override; - virtual void + void Tick(llarp_time_t now) override; void @@ -113,13 +113,13 @@ namespace llarp virtual const SecretKey& GetTunnelEncryptionSecretKey() const; - virtual void + void HandlePathBuilt(Path_ptr p) override; - virtual void + void HandlePathBuildTimeout(Path_ptr p) override; - virtual void + void HandlePathBuildFailed(Path_ptr p) override; }; diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index f2a1d3bc3..b9a46c24c 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -31,9 +31,7 @@ namespace llarp return stream; } - TransitHop::TransitHop() - { - } + TransitHop::TransitHop() = default; bool TransitHop::Expired(llarp_time_t now) const diff --git a/llarp/pow.cpp b/llarp/pow.cpp index d8f20cd35..11673ff43 100644 --- a/llarp/pow.cpp +++ b/llarp/pow.cpp @@ -7,9 +7,7 @@ namespace llarp { - PoW::~PoW() - { - } + PoW::~PoW() = default; bool PoW::DecodeKey(ABSL_ATTRIBUTE_UNUSED const llarp_buffer_t& k, diff --git a/llarp/router/i_outbound_session_maker.hpp b/llarp/router/i_outbound_session_maker.hpp index fdebf0839..e5bb1ce98 100644 --- a/llarp/router/i_outbound_session_maker.hpp +++ b/llarp/router/i_outbound_session_maker.hpp @@ -52,6 +52,9 @@ namespace llarp virtual util::StatusObject ExtractStatus() const = 0; + + virtual bool + ShouldConnectTo(const RouterID &router) const = 0; }; } // namespace llarp diff --git a/llarp/router/outbound_message_handler.hpp b/llarp/router/outbound_message_handler.hpp index 54efeea63..d64a7c905 100644 --- a/llarp/router/outbound_message_handler.hpp +++ b/llarp/router/outbound_message_handler.hpp @@ -22,11 +22,11 @@ namespace llarp struct OutboundMessageHandler final : public IOutboundMessageHandler { public: - ~OutboundMessageHandler() = default; + ~OutboundMessageHandler() override = default; bool QueueMessage(const RouterID &remote, const ILinkMessage *msg, - SendStatusHandler callback) override; + SendStatusHandler callback) override LOCKS_EXCLUDED(_mutex); util::StatusObject ExtractStatus() const override; @@ -72,7 +72,8 @@ namespace llarp SendIfSession(const RouterID &remote, const Message &msg); void - FinalizeRequest(const RouterID &router, SendStatus status); + FinalizeRequest(const RouterID &router, SendStatus status) + LOCKS_EXCLUDED(_mutex); mutable util::Mutex _mutex; // protects outboundMessageQueue diff --git a/llarp/router/outbound_session_maker.cpp b/llarp/router/outbound_session_maker.cpp index 5a52a0aba..efdc73b1d 100644 --- a/llarp/router/outbound_session_maker.cpp +++ b/llarp/router/outbound_session_maker.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace llarp { @@ -22,8 +23,8 @@ namespace llarp size_t attemptCount = 0; - PendingSession(const RouterContact &_rc, LinkLayer_ptr _link) - : rc(_rc), link(_link) + PendingSession(RouterContact _rc, LinkLayer_ptr _link) + : rc(std::move(_rc)), link(std::move(_link)) { } }; @@ -192,7 +193,7 @@ namespace llarp const RouterContact &rc) { { - util::Lock l(&_mutex); + util::ReleasableLock l(&_mutex); // in case other request found RC for this router after this request was // made @@ -206,6 +207,7 @@ namespace llarp if(!link) { + l.Release(); FinalizeRequest(router, SessionResult::NoLink); return; } @@ -214,9 +216,26 @@ namespace llarp itr->second = session; } + if(ShouldConnectTo(router)) + { + auto fn = std::bind(&OutboundSessionMaker::DoEstablish, this, router); + _logic->queue_func(fn); + } + } - auto fn = std::bind(&OutboundSessionMaker::DoEstablish, this, router); - _logic->queue_func(fn); + bool + OutboundSessionMaker::ShouldConnectTo(const RouterID &router) const + { + size_t numPending = 0; + { + util::Lock lock(&_mutex); + if(pendingSessions.find(router) == pendingSessions.end()) + numPending += pendingSessions.size(); + } + if(_linkManager->HasSessionTo(router)) + return false; + return _linkManager->NumberOfConnectedRouters() + numPending + < maxConnectedRouters; } void diff --git a/llarp/router/outbound_session_maker.hpp b/llarp/router/outbound_session_maker.hpp index 4b5e291a4..f14352651 100644 --- a/llarp/router/outbound_session_maker.hpp +++ b/llarp/router/outbound_session_maker.hpp @@ -26,7 +26,7 @@ namespace llarp using CallbacksQueue = std::list< RouterCallback >; public: - ~OutboundSessionMaker() = default; + ~OutboundSessionMaker() override = default; bool OnSessionEstablished(ILinkSession *session) override; @@ -35,13 +35,16 @@ namespace llarp OnConnectTimeout(ILinkSession *session) override; void - CreateSessionTo(const RouterID &router, RouterCallback on_result) override; + CreateSessionTo(const RouterID &router, RouterCallback on_result) override + LOCKS_EXCLUDED(_mutex); void - CreateSessionTo(const RouterContact &rc, RouterCallback on_result) override; + CreateSessionTo(const RouterContact &rc, RouterCallback on_result) override + LOCKS_EXCLUDED(_mutex); bool - HavePendingSessionTo(const RouterID &router) const override; + HavePendingSessionTo(const RouterID &router) const override + LOCKS_EXCLUDED(_mutex); void ConnectToRandomRouters(int numDesired, llarp_time_t now) override; @@ -49,17 +52,27 @@ namespace llarp util::StatusObject ExtractStatus() const override; + bool + ShouldConnectTo(const RouterID &router) const override + LOCKS_EXCLUDED(_mutex); + void Init(ILinkManager *linkManager, I_RCLookupHandler *rcLookup, std::shared_ptr< Logic > logic, llarp_nodedb *nodedb, std::shared_ptr< llarp::thread::ThreadPool > threadpool); + /// always maintain this many connections to other routers + size_t minConnectedRouters = 4; + /// hard upperbound limit on the number of router to router connections + size_t maxConnectedRouters = 6; + private: void - DoEstablish(const RouterID &router); + DoEstablish(const RouterID &router) LOCKS_EXCLUDED(_mutex); void - GotRouterContact(const RouterID &router, const RouterContact &rc); + GotRouterContact(const RouterID &router, const RouterContact &rc) + LOCKS_EXCLUDED(_mutex); void InvalidRouter(const RouterID &router); @@ -75,10 +88,11 @@ namespace llarp VerifyRC(const RouterContact rc); void - CreatePendingSession(const RouterID &router); + CreatePendingSession(const RouterID &router) LOCKS_EXCLUDED(_mutex); void - FinalizeRequest(const RouterID &router, const SessionResult type); + FinalizeRequest(const RouterID &router, const SessionResult type) + LOCKS_EXCLUDED(_mutex); mutable util::Mutex _mutex; // protects pendingSessions, pendingCallbacks diff --git a/llarp/router/rc_lookup_handler.hpp b/llarp/router/rc_lookup_handler.hpp index 1638da6c6..a94a60a1c 100644 --- a/llarp/router/rc_lookup_handler.hpp +++ b/llarp/router/rc_lookup_handler.hpp @@ -28,28 +28,32 @@ namespace llarp public: using CallbacksQueue = std::list< RCRequestCallback >; - ~RCLookupHandler() = default; + ~RCLookupHandler() override = default; void - AddValidRouter(const RouterID &router) override; + AddValidRouter(const RouterID &router) override LOCKS_EXCLUDED(_mutex); void - RemoveValidRouter(const RouterID &router) override; + RemoveValidRouter(const RouterID &router) override LOCKS_EXCLUDED(_mutex); void - SetRouterWhitelist(const std::vector< RouterID > &routers) override; + SetRouterWhitelist(const std::vector< RouterID > &routers) override + LOCKS_EXCLUDED(_mutex); void - GetRC(const RouterID &router, RCRequestCallback callback) override; + GetRC(const RouterID &router, RCRequestCallback callback) override + LOCKS_EXCLUDED(_mutex); bool - RemoteIsAllowed(const RouterID &remote) const override; + RemoteIsAllowed(const RouterID &remote) const override + LOCKS_EXCLUDED(_mutex); bool CheckRC(const RouterContact &rc) const override; bool - GetRandomWhitelistRouter(RouterID &router) const override; + GetRandomWhitelistRouter(RouterID &router) const override + LOCKS_EXCLUDED(_mutex); bool CheckRenegotiateValid(RouterContact newrc, RouterContact oldrc) override; @@ -74,14 +78,14 @@ namespace llarp const std::vector< RouterContact > &results); bool - HavePendingLookup(RouterID remote) const; + HavePendingLookup(RouterID remote) const LOCKS_EXCLUDED(_mutex); bool RemoteInBootstrap(const RouterID &remote) const; void FinalizeRequest(const RouterID &router, const RouterContact *const rc, - RCRequestResult result); + RCRequestResult result) LOCKS_EXCLUDED(_mutex); mutable util::Mutex _mutex; // protects pendingCallbacks, whitelistRouters diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 686e44d1a..5fae4bc3c 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include #include +#include #include #include @@ -81,9 +83,9 @@ namespace llarp Router::Router(std::shared_ptr< llarp::thread::ThreadPool > _tp, llarp_ev_loop_ptr __netloop, std::shared_ptr< Logic > l) : ready(false) - , _netloop(__netloop) - , cryptoworker(_tp) - , _logic(l) + , _netloop(std::move(__netloop)) + , cryptoworker(std::move(_tp)) + , _logic(std::move(l)) , paths(this) , _exitContext(this) , disk(std::make_shared< llarp::thread::ThreadPool >(1, 1000, @@ -288,7 +290,7 @@ namespace llarp { if(left) return; - Router *self = static_cast< Router * >(user); + auto *self = static_cast< Router * >(user); self->ticker_job_id = 0; self->Tick(); self->ScheduleTicker(orig); @@ -373,14 +375,16 @@ namespace llarp m_OutboundPort = conf->iwp_links.outboundPort(); // Router config _rc.SetNick(conf->router.nickname()); - maxConnectedRouters = conf->router.maxConnectedRouters(); - minConnectedRouters = conf->router.minConnectedRouters(); - encryption_keyfile = conf->router.encryptionKeyfile(); - our_rc_file = conf->router.ourRcFile(); - transport_keyfile = conf->router.transportKeyfile(); - addrInfo = conf->router.addrInfo(); - publicOverride = conf->router.publicOverride(); - ip4addr = conf->router.ip4addr(); + _outboundSessionMaker.maxConnectedRouters = + conf->router.maxConnectedRouters(); + _outboundSessionMaker.minConnectedRouters = + conf->router.minConnectedRouters(); + encryption_keyfile = conf->router.encryptionKeyfile(); + our_rc_file = conf->router.ourRcFile(); + transport_keyfile = conf->router.transportKeyfile(); + addrInfo = conf->router.addrInfo(); + publicOverride = conf->router.publicOverride(); + ip4addr = conf->router.ip4addr(); // Lokid Config usingSNSeed = conf->lokid.usingSNSeed; @@ -655,16 +659,16 @@ namespace llarp const size_t connected = NumberOfConnectedRouters(); const size_t N = nodedb()->num_loaded(); - if(N < minRequiredRouters) + if(N < llarp::path::default_len) { - LogInfo("We need at least ", minRequiredRouters, + LogInfo("We need at least ", llarp::path::default_len, " service nodes to build paths but we have ", N, " in nodedb"); _rcLookupHandler.ExploreNetwork(); } - if(connected < minConnectedRouters) + if(connected < _outboundSessionMaker.minConnectedRouters) { - size_t dlt = minConnectedRouters - connected; + size_t dlt = _outboundSessionMaker.minConnectedRouters - connected; LogInfo("connecting to ", dlt, " random routers to keep alive"); _outboundSessionMaker.ConnectToRandomRouters(dlt, now); } @@ -886,6 +890,14 @@ namespace llarp EnsureNetConfigDefaultsSane(netConfig); + const auto limits = + IsServiceNode() ? llarp::limits::snode : llarp::limits::client; + + _outboundSessionMaker.minConnectedRouters = std::max( + _outboundSessionMaker.minConnectedRouters, limits.DefaultMinRouters); + _outboundSessionMaker.maxConnectedRouters = std::max( + _outboundSessionMaker.maxConnectedRouters, limits.DefaultMaxRouters); + if(IsServiceNode()) { // initialize as service node @@ -896,14 +908,12 @@ namespace llarp } RouterID us = pubkey(); LogInfo("initalized service node: ", us); - if(minConnectedRouters < 6) - minConnectedRouters = 6; + // relays do not use profiling routerProfiling().Disable(); } else { - maxConnectedRouters = minConnectedRouters + 1; // we are a client // regenerate keys and resign rc before everything else CryptoManager::instance()->identity_keygen(_identity); @@ -965,14 +975,14 @@ namespace llarp static void RouterAfterStopLinks(void *u, uint64_t, uint64_t) { - Router *self = static_cast< Router * >(u); + auto *self = static_cast< Router * >(u); self->Close(); } static void RouterAfterStopIssued(void *u, uint64_t, uint64_t) { - Router *self = static_cast< Router * >(u); + auto *self = static_cast< Router * >(u); self->StopLinks(); self->nodedb()->AsyncFlushToDisk(); self->_logic->call_later({200, self, &RouterAfterStopLinks}); diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 2b6b2df86..27994a573 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -194,13 +194,6 @@ namespace llarp Sign(Signature &sig, const llarp_buffer_t &buf) const override; uint16_t m_OutboundPort = 0; - - /// always maintain this many connections to other routers - size_t minConnectedRouters = 2; - /// hard upperbound limit on the number of router to router connections - size_t maxConnectedRouters = 2000; - - size_t minRequiredRouters = 4; /// how often do we resign our RC? milliseconds. // TODO: make configurable llarp_time_t rcRegenInterval = 60 * 60 * 1000; @@ -297,7 +290,7 @@ namespace llarp Router(std::shared_ptr< llarp::thread::ThreadPool > worker, llarp_ev_loop_ptr __netloop, std::shared_ptr< Logic > logic); - ~Router(); + ~Router() override; bool HandleRecvLinkMessageBuffer(ILinkSession *from, diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index a65f7bcea..e7eb10bc7 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #define MAX_RC_SIZE (1024) @@ -107,6 +108,12 @@ namespace llarp util::StatusObject ExtractStatus() const; + nlohmann::json + ToJson() const + { + return ExtractStatus().get(); + } + bool BEncode(llarp_buffer_t *buf) const; diff --git a/llarp/routing/dht_message.hpp b/llarp/routing/dht_message.hpp index ae249401d..24534087b 100644 --- a/llarp/routing/dht_message.hpp +++ b/llarp/routing/dht_message.hpp @@ -15,7 +15,7 @@ namespace llarp std::vector< llarp::dht::IMessage::Ptr_t > M; uint64_t V = 0; - ~DHTMessage() = default; + ~DHTMessage() override = default; bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; diff --git a/llarp/routing/message.hpp b/llarp/routing/message.hpp index e42909b5f..89cda76c7 100644 --- a/llarp/routing/message.hpp +++ b/llarp/routing/message.hpp @@ -15,16 +15,12 @@ namespace llarp struct IMessage { PathID_t from; - uint64_t S; + uint64_t S{0}; uint64_t version = LLARP_PROTO_VERSION; - IMessage() : S(0) - { - } + IMessage() = default; - virtual ~IMessage() - { - } + virtual ~IMessage() = default; virtual bool BEncode(llarp_buffer_t* buf) const = 0; diff --git a/llarp/routing/message_parser.cpp b/llarp/routing/message_parser.cpp index af90db319..b1173b29b 100644 --- a/llarp/routing/message_parser.cpp +++ b/llarp/routing/message_parser.cpp @@ -31,16 +31,11 @@ namespace llarp }; InboundMessageParser::InboundMessageParser() - : firstKey(false) - , ourKey('\0') - , msg(nullptr) - , m_Holder(std::make_unique< MessageHolder >()) + : m_Holder(std::make_unique< MessageHolder >()) { } - InboundMessageParser::~InboundMessageParser() - { - } + InboundMessageParser::~InboundMessageParser() = default; bool InboundMessageParser::operator()(llarp_buffer_t* buffer, diff --git a/llarp/routing/message_parser.hpp b/llarp/routing/message_parser.hpp index 5c0d7f78d..7353ce470 100644 --- a/llarp/routing/message_parser.hpp +++ b/llarp/routing/message_parser.hpp @@ -29,12 +29,12 @@ namespace llarp operator()(llarp_buffer_t* buffer, llarp_buffer_t* key); private: - bool firstKey; - char ourKey; + bool firstKey{false}; + char ourKey{'\0'}; struct MessageHolder; - IMessage* msg; + IMessage* msg{nullptr}; std::unique_ptr< MessageHolder > m_Holder; }; } // namespace routing diff --git a/llarp/routing/path_confirm_message.hpp b/llarp/routing/path_confirm_message.hpp index 7cfc008f6..58b3e5b80 100644 --- a/llarp/routing/path_confirm_message.hpp +++ b/llarp/routing/path_confirm_message.hpp @@ -14,7 +14,7 @@ namespace llarp PathConfirmMessage() = default; PathConfirmMessage(uint64_t lifetime); - ~PathConfirmMessage() = default; + ~PathConfirmMessage() override = default; bool BEncode(llarp_buffer_t* buf) const override; diff --git a/llarp/routing/path_latency_message.cpp b/llarp/routing/path_latency_message.cpp index 5d8d51a08..906f36497 100644 --- a/llarp/routing/path_latency_message.cpp +++ b/llarp/routing/path_latency_message.cpp @@ -7,9 +7,7 @@ namespace llarp { namespace routing { - PathLatencyMessage::PathLatencyMessage() - { - } + PathLatencyMessage::PathLatencyMessage() = default; bool PathLatencyMessage::DecodeKey(const llarp_buffer_t& key, diff --git a/llarp/routing/path_transfer_message.hpp b/llarp/routing/path_transfer_message.hpp index f0d9332f4..4dbcd56ef 100644 --- a/llarp/routing/path_transfer_message.hpp +++ b/llarp/routing/path_transfer_message.hpp @@ -22,7 +22,7 @@ namespace llarp { Y.Randomize(); } - ~PathTransferMessage() = default; + ~PathTransferMessage() override = default; bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; diff --git a/llarp/rpc/rpc.cpp b/llarp/rpc/rpc.cpp index 8ae892ea1..8760aeece 100644 --- a/llarp/rpc/rpc.cpp +++ b/llarp/rpc/rpc.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace llarp { @@ -22,15 +23,13 @@ namespace llarp { } - ~CallerHandler() - { - } + ~CallerHandler() override = default; virtual bool HandleJSONResult(const nlohmann::json& val) = 0; bool - HandleResponse(::abyss::http::RPC_Response response) + HandleResponse(::abyss::http::RPC_Response response) override { if(!response.is_object()) { @@ -50,7 +49,7 @@ namespace llarp } void - PopulateReqHeaders(abyss::http::Headers_t& hdr); + PopulateReqHeaders(abyss::http::Headers_t& hdr) override; }; struct GetServiceNodeListHandler final : public CallerHandler @@ -58,14 +57,12 @@ namespace llarp using PubkeyList_t = std::vector< RouterID >; using Callback_t = std::function< void(const PubkeyList_t&, bool) >; - ~GetServiceNodeListHandler() - { - } + ~GetServiceNodeListHandler() override = default; Callback_t handler; GetServiceNodeListHandler(::abyss::http::ConnImpl* impl, CallerImpl* parent, Callback_t h) - : CallerHandler(impl, parent), handler(h) + : CallerHandler(impl, parent), handler(std::move(h)) { } @@ -178,9 +175,7 @@ namespace llarp LogError("service node list not updated"); } - ~CallerImpl() - { - } + ~CallerImpl() = default; }; void @@ -197,9 +192,7 @@ namespace llarp { } - ~Handler() - { - } + ~Handler() override = default; Response DumpState() const @@ -269,7 +262,7 @@ namespace llarp absl::optional< Response > HandleJSONRPC(Method_t method, - __attribute__((unused)) const Params& params) + ABSL_ATTRIBUTE_UNUSED const Params& params) override { if(method == "llarp.admin.link.neighboors") { @@ -283,7 +276,7 @@ namespace llarp { return DumpState(); } - else if(method == "llarp.admin.status") + if(method == "llarp.admin.status") { return DumpStatus(); } @@ -299,7 +292,7 @@ namespace llarp } AbstractRouter* router; ::abyss::httpd::IRPCHandler* - CreateHandler(::abyss::httpd::ConnImpl* conn) + CreateHandler(::abyss::httpd::ConnImpl* conn) override { return new Handler(conn, router); } @@ -314,9 +307,7 @@ namespace llarp { } - ~ServerImpl() - { - } + ~ServerImpl() = default; void Stop() @@ -349,9 +340,7 @@ namespace llarp { } - Caller::~Caller() - { - } + Caller::~Caller() = default; void Caller::Stop() @@ -382,9 +371,7 @@ namespace llarp { } - Server::~Server() - { - } + Server::~Server() = default; void Server::Stop() diff --git a/llarp/service/async_key_exchange.cpp b/llarp/service/async_key_exchange.cpp index a788e24e3..660277c95 100644 --- a/llarp/service/async_key_exchange.cpp +++ b/llarp/service/async_key_exchange.cpp @@ -4,20 +4,18 @@ #include #include #include +#include namespace llarp { namespace service { - AsyncKeyExchange::AsyncKeyExchange(std::shared_ptr< Logic > l, - const ServiceInfo& r, - const Identity& localident, - const PQPubKey& introsetPubKey, - const Introduction& remote, - IDataHandler* h, const ConvoTag& t, - ProtocolType proto) - : logic(l) - , m_remote(r) + AsyncKeyExchange::AsyncKeyExchange( + std::shared_ptr< Logic > l, ServiceInfo r, const Identity& localident, + const PQPubKey& introsetPubKey, const Introduction& remote, + IDataHandler* h, const ConvoTag& t, ProtocolType proto) + : logic(std::move(l)) + , m_remote(std::move(r)) , m_LocalIdentity(localident) , introPubKey(introsetPubKey) , remoteIntro(remote) @@ -30,7 +28,7 @@ namespace llarp void AsyncKeyExchange::Result(void* user) { - AsyncKeyExchange* self = static_cast< AsyncKeyExchange* >(user); + auto* self = static_cast< AsyncKeyExchange* >(user); // put values self->handler->PutSenderFor(self->msg.tag, self->m_remote, false); self->handler->PutCachedSessionKeyFor(self->msg.tag, self->sharedKey); @@ -43,7 +41,7 @@ namespace llarp void AsyncKeyExchange::Encrypt(void* user) { - AsyncKeyExchange* self = static_cast< AsyncKeyExchange* >(user); + auto* self = static_cast< AsyncKeyExchange* >(user); // derive ntru session key component SharedSecret K; auto crypto = CryptoManager::instance(); diff --git a/llarp/service/async_key_exchange.hpp b/llarp/service/async_key_exchange.hpp index a555632f4..358481b23 100644 --- a/llarp/service/async_key_exchange.hpp +++ b/llarp/service/async_key_exchange.hpp @@ -26,7 +26,7 @@ namespace llarp IDataHandler* handler; ConvoTag tag; - AsyncKeyExchange(std::shared_ptr< Logic > l, const ServiceInfo& r, + AsyncKeyExchange(std::shared_ptr< Logic > l, ServiceInfo r, const Identity& localident, const PQPubKey& introsetPubKey, const Introduction& remote, IDataHandler* h, diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index ca1e3e420..6afaa75a5 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -49,9 +49,7 @@ namespace llarp { } - Context::~Context() - { - } + Context::~Context() = default; bool Context::StopAll() diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index f00fb9e9d..c8ff71eef 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace llarp { @@ -138,18 +139,17 @@ namespace llarp addr = itr->second.remote.Addr(); return true; } - else + + for(const auto& item : m_state->m_SNodeSessions) { - for(const auto& item : m_state->m_SNodeSessions) + if(item.second.second == tag) { - if(item.second.second == tag) - { - snode = true; - addr = item.first; - return true; - } + snode = true; + addr = item.first; + return true; } } + return false; } @@ -497,16 +497,15 @@ namespace llarp { IntroSet m_IntroSet; Endpoint* m_Endpoint; - PublishIntroSetJob(Endpoint* parent, uint64_t id, - const IntroSet& introset) + PublishIntroSetJob(Endpoint* parent, uint64_t id, IntroSet introset) : IServiceLookup(parent, id, "PublishIntroSet") - , m_IntroSet(introset) + , m_IntroSet(std::move(introset)) , m_Endpoint(parent) { } std::shared_ptr< routing::IMessage > - BuildRequestMessage() + BuildRequestMessage() override { auto msg = std::make_shared< routing::DHTMessage >(); msg->M.emplace_back( @@ -515,7 +514,7 @@ namespace llarp } bool - HandleResponse(const std::set< IntroSet >& response) + HandleResponse(const std::set< IntroSet >& response) override { if(response.size()) m_Endpoint->IntroSetPublished(); @@ -698,11 +697,11 @@ namespace llarp { if(msg->R.size()) { - llarp_async_verify_rc* job = new llarp_async_verify_rc; - job->nodedb = Router()->nodedb(); - job->cryptoworker = Router()->threadpool(); - job->diskworker = Router()->diskworker(); - job->logic = Router()->logic(); + auto* job = new llarp_async_verify_rc; + job->nodedb = Router()->nodedb(); + job->cryptoworker = Router()->threadpool(); + job->diskworker = Router()->diskworker(); + job->logic = Router()->logic(); job->hook = std::bind(&Endpoint::HandleVerifyGotRouter, this, msg, std::placeholders::_1); job->rc = msg->R[0]; diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index d895e900a..b40dfc345 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -71,7 +71,7 @@ namespace llarp static const size_t MAX_OUTBOUND_CONTEXT_COUNT = 4; Endpoint(const std::string& nickname, AbstractRouter* r, Context* parent); - ~Endpoint(); + ~Endpoint() override; /// return true if we are ready to recv packets from the void bool @@ -94,7 +94,7 @@ namespace llarp virtual bool SetOption(const std::string& k, const std::string& v); - virtual void + void Tick(llarp_time_t now) override; /// return true if we have a resolvable ip address @@ -111,7 +111,7 @@ namespace llarp return {0}; } - virtual void + void ResetInternalState() override; /// router's logic @@ -142,7 +142,7 @@ namespace llarp virtual bool Start(); - virtual std::string + std::string Name() const override; bool @@ -335,7 +335,7 @@ namespace llarp uint64_t GetSeqNoForConvo(const ConvoTag& tag); - virtual bool + bool SelectHop(llarp_nodedb* db, const std::set< RouterID >& prev, RouterContact& cur, size_t hop, path::PathRole roles) override; diff --git a/llarp/service/endpoint_state.hpp b/llarp/service/endpoint_state.hpp index 0164c3e2e..d01b274ac 100644 --- a/llarp/service/endpoint_state.hpp +++ b/llarp/service/endpoint_state.hpp @@ -40,7 +40,7 @@ namespace llarp hooks::Backend_ptr m_OnDown; hooks::Backend_ptr m_OnReady; - util::Mutex m_InboundTrafficQueueMutex; + util::Mutex m_InboundTrafficQueueMutex; // protects m_InboundTrafficQueue /// ordered queue for inbound hidden service traffic RecvPacketQueue_t m_InboundTrafficQueue GUARDED_BY(m_InboundTrafficQueueMutex); @@ -55,7 +55,7 @@ namespace llarp std::string m_NetNS; bool m_BundleRC = false; - util::Mutex m_SendQueueMutex; + util::Mutex m_SendQueueMutex; // protects m_SendQueue std::deque< SendEvent_t > m_SendQueue GUARDED_BY(m_SendQueueMutex); PendingTraffic m_PendingTraffic; diff --git a/llarp/service/hidden_service_address_lookup.cpp b/llarp/service/hidden_service_address_lookup.cpp index dd9340f7a..fda8cba45 100644 --- a/llarp/service/hidden_service_address_lookup.cpp +++ b/llarp/service/hidden_service_address_lookup.cpp @@ -2,6 +2,7 @@ #include #include +#include namespace llarp { @@ -11,7 +12,7 @@ namespace llarp HandlerFunc h, const Address& addr, uint64_t tx) - : IServiceLookup(p, tx, "HSLookup"), remote(addr), handle(h) + : IServiceLookup(p, tx, "HSLookup"), remote(addr), handle(std::move(h)) { } diff --git a/llarp/service/hidden_service_address_lookup.hpp b/llarp/service/hidden_service_address_lookup.hpp index 9fdccd472..86aaeee64 100644 --- a/llarp/service/hidden_service_address_lookup.hpp +++ b/llarp/service/hidden_service_address_lookup.hpp @@ -20,15 +20,13 @@ namespace llarp HiddenServiceAddressLookup(Endpoint* p, HandlerFunc h, const Address& addr, uint64_t tx); - ~HiddenServiceAddressLookup() - { - } + ~HiddenServiceAddressLookup() override = default; bool - HandleResponse(const std::set< IntroSet >& results); + HandleResponse(const std::set< IntroSet >& results) override; std::shared_ptr< routing::IMessage > - BuildRequestMessage(); + BuildRequestMessage() override; }; } // namespace service } // namespace llarp diff --git a/llarp/service/lookup.cpp b/llarp/service/lookup.cpp index 51f9cfbbf..e231615f5 100644 --- a/llarp/service/lookup.cpp +++ b/llarp/service/lookup.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace llarp { @@ -11,9 +12,8 @@ namespace llarp namespace service { - IServiceLookup::IServiceLookup(ILookupHolder *p, uint64_t tx, - const std::string &n) - : m_parent(p), txid(tx), name(n) + IServiceLookup::IServiceLookup(ILookupHolder *p, uint64_t tx, std::string n) + : m_parent(p), txid(tx), name(std::move(n)) { m_created = time_now_ms(); p->PutLookup(this, tx); diff --git a/llarp/service/lookup.hpp b/llarp/service/lookup.hpp index 25c2dba68..03ffb09e4 100644 --- a/llarp/service/lookup.hpp +++ b/llarp/service/lookup.hpp @@ -23,10 +23,8 @@ namespace llarp struct IServiceLookup { - IServiceLookup() = delete; - virtual ~IServiceLookup() - { - } + IServiceLookup() = delete; + virtual ~IServiceLookup() = default; /// handle lookup result virtual bool @@ -71,8 +69,7 @@ namespace llarp } protected: - IServiceLookup(ILookupHolder* parent, uint64_t tx, - const std::string& name); + IServiceLookup(ILookupHolder* parent, uint64_t tx, std::string name); llarp_time_t m_created; }; diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index 53f630a21..f975c486c 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -66,9 +66,7 @@ namespace llarp } } - OutboundContext::~OutboundContext() - { - } + OutboundContext::~OutboundContext() = default; /// actually swap intros void diff --git a/llarp/service/outbound_context.hpp b/llarp/service/outbound_context.hpp index 6cffa1296..1f591540b 100644 --- a/llarp/service/outbound_context.hpp +++ b/llarp/service/outbound_context.hpp @@ -21,7 +21,7 @@ namespace llarp public std::enable_shared_from_this< OutboundContext > { OutboundContext(const IntroSet& introSet, Endpoint* parent); - ~OutboundContext(); + ~OutboundContext() override; util::StatusObject ExtractStatus() const; diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index c3d0b245e..09fb02d82 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace llarp { @@ -19,9 +20,7 @@ namespace llarp { } - ProtocolMessage::~ProtocolMessage() - { - } + ProtocolMessage::~ProtocolMessage() = default; void ProtocolMessage::PutBuffer(const llarp_buffer_t& buf) @@ -92,9 +91,7 @@ namespace llarp return bencode_end(buf); } - ProtocolFrame::~ProtocolFrame() - { - } + ProtocolFrame::~ProtocolFrame() = default; bool ProtocolFrame::BEncode(llarp_buffer_t* buf) const @@ -255,11 +252,10 @@ namespace llarp const Introduction fromIntro; AsyncFrameDecrypt(std::shared_ptr< Logic > l, const Identity& localIdent, - IDataHandler* h, - const std::shared_ptr< ProtocolMessage >& m, + IDataHandler* h, std::shared_ptr< ProtocolMessage > m, const ProtocolFrame& f, const Introduction& recvIntro) - : logic(l) - , msg(m) + : logic(std::move(l)) + , msg(std::move(m)) , m_LocalIdentity(localIdent) , handler(h) , frame(f) @@ -270,8 +266,8 @@ namespace llarp static void Work(void* user) { - AsyncFrameDecrypt* self = static_cast< AsyncFrameDecrypt* >(user); - auto crypto = CryptoManager::instance(); + auto* self = static_cast< AsyncFrameDecrypt* >(user); + auto crypto = CryptoManager::instance(); SharedSecret K; SharedSecret sharedKey; // copy @@ -405,6 +401,11 @@ namespace llarp LogError("failed to decrypt message"); return false; } + if(T != msg->tag && !msg->tag.IsZero()) + { + LogError("convotag missmatch: ", T, " != ", msg->tag); + return false; + } msg->handler = handler; const PathID_t fromPath = F; logic->queue_func( diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index 80e5ed8e2..c26963986 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -104,7 +104,7 @@ namespace llarp Clear(); } - ~ProtocolFrame(); + ~ProtocolFrame() override; bool operator==(const ProtocolFrame& other) const; diff --git a/llarp/service/router_lookup_job.cpp b/llarp/service/router_lookup_job.cpp index 1b60d2cd4..395a6c1e3 100644 --- a/llarp/service/router_lookup_job.cpp +++ b/llarp/service/router_lookup_job.cpp @@ -1,13 +1,14 @@ #include #include +#include namespace llarp { namespace service { RouterLookupJob::RouterLookupJob(Endpoint* p, RouterLookupHandler h) - : handler(h), txid(p->GenTXID()), started(p->Now()) + : handler(std::move(h)), txid(p->GenTXID()), started(p->Now()) { } diff --git a/llarp/service/sendcontext.cpp b/llarp/service/sendcontext.cpp index a7ba5a088..e25647978 100644 --- a/llarp/service/sendcontext.cpp +++ b/llarp/service/sendcontext.cpp @@ -4,15 +4,15 @@ #include #include #include +#include namespace llarp { namespace service { - SendContext::SendContext(const ServiceInfo& ident, - const Introduction& intro, path::PathSet* send, - Endpoint* ep) - : remoteIdent(ident) + SendContext::SendContext(ServiceInfo ident, const Introduction& intro, + path::PathSet* send, Endpoint* ep) + : remoteIdent(std::move(ident)) , remoteIntro(intro) , m_PathSet(send) , m_DataHandler(ep) diff --git a/llarp/service/sendcontext.hpp b/llarp/service/sendcontext.hpp index d289c5640..9ca0cb8c0 100644 --- a/llarp/service/sendcontext.hpp +++ b/llarp/service/sendcontext.hpp @@ -20,7 +20,7 @@ namespace llarp struct SendContext { - SendContext(const ServiceInfo& ident, const Introduction& intro, + SendContext(ServiceInfo ident, const Introduction& intro, path::PathSet* send, Endpoint* ep); void diff --git a/llarp/service/tag_lookup_job.hpp b/llarp/service/tag_lookup_job.hpp index f061df504..0221e4e6b 100644 --- a/llarp/service/tag_lookup_job.hpp +++ b/llarp/service/tag_lookup_job.hpp @@ -28,9 +28,7 @@ namespace llarp { } - ~CachedTagResult() - { - } + ~CachedTagResult() = default; void Expire(llarp_time_t now); @@ -54,9 +52,7 @@ namespace llarp { TagLookupJob(Endpoint* parent, CachedTagResult* result); - ~TagLookupJob() - { - } + ~TagLookupJob() override = default; std::shared_ptr< routing::IMessage > BuildRequestMessage() override diff --git a/llarp/util/bencode.cpp b/llarp/util/bencode.cpp index b8b32b3ee..25e46238f 100644 --- a/llarp/util/bencode.cpp +++ b/llarp/util/bencode.cpp @@ -1,8 +1,8 @@ #include #include #include -#include -#include +#include +#include bool bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result) diff --git a/llarp/util/bencode.h b/llarp/util/bencode.h index 04fe12c8a..46284126d 100644 --- a/llarp/util/bencode.h +++ b/llarp/util/bencode.h @@ -6,8 +6,8 @@ #include #include -#include -#include + +#include /** * bencode.h diff --git a/llarp/util/buffer.cpp b/llarp/util/buffer.cpp index 8a0f0d9bc..aca4fa1ae 100644 --- a/llarp/util/buffer.cpp +++ b/llarp/util/buffer.cpp @@ -1,8 +1,8 @@ #include #include -#include -#include +#include +#include size_t llarp_buffer_t::size_left() const diff --git a/llarp/util/buffer.hpp b/llarp/util/buffer.hpp index e95e66d93..e9a635378 100644 --- a/llarp/util/buffer.hpp +++ b/llarp/util/buffer.hpp @@ -7,10 +7,10 @@ #include #include -#include -#include -#include -#include + +#include +#include +#include #include #include @@ -64,20 +64,18 @@ struct ManagedBuffer; struct llarp_buffer_t { /// starting memory address - byte_t *base; + byte_t *base{nullptr}; /// memory address of stream position - byte_t *cur; + byte_t *cur{nullptr}; /// max size of buffer - size_t sz; + size_t sz{0}; byte_t operator[](size_t x) { return *(this->base + x); } - llarp_buffer_t() : base(nullptr), cur(nullptr), sz(0) - { - } + llarp_buffer_t() = default; llarp_buffer_t(byte_t *b, byte_t *c, size_t s) : base(b), cur(c), sz(s) { diff --git a/llarp/util/codel.hpp b/llarp/util/codel.hpp index 02486ea50..2d9e5d0bd 100644 --- a/llarp/util/codel.hpp +++ b/llarp/util/codel.hpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace llarp { @@ -31,8 +32,11 @@ namespace llarp llarp_time_t initialIntervalMs = 100, size_t MaxSize = 1024 > struct CoDelQueue { - CoDelQueue(const std::string& name, const PutTime& put, const GetNow& now) - : m_QueueIdx(0), m_name(name), _putTime(put), _getNow(now) + CoDelQueue(std::string name, PutTime put, GetNow now) + : m_QueueIdx(0) + , m_name(std::move(name)) + , _putTime(std::move(put)) + , _getNow(std::move(now)) { } diff --git a/llarp/util/common.hpp b/llarp/util/common.hpp index 3ef490f75..4223ebf52 100644 --- a/llarp/util/common.hpp +++ b/llarp/util/common.hpp @@ -5,7 +5,7 @@ #else #define INLINE inline #endif -#include -#include -#include + +#include +#include #endif diff --git a/llarp/util/encode.hpp b/llarp/util/encode.hpp index dee452a89..472007f61 100644 --- a/llarp/util/encode.hpp +++ b/llarp/util/encode.hpp @@ -1,6 +1,6 @@ #ifndef LLARP_ENCODE_HPP #define LLARP_ENCODE_HPP -#include +#include #include #include #include diff --git a/llarp/util/endian.hpp b/llarp/util/endian.hpp index 087517348..60d0a4659 100644 --- a/llarp/util/endian.hpp +++ b/llarp/util/endian.hpp @@ -3,8 +3,8 @@ // adapted from libi2pd -#include -#include +#include +#include #include #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) diff --git a/llarp/util/file_logger.cpp b/llarp/util/file_logger.cpp index 4c335e854..0b0bcbf4e 100644 --- a/llarp/util/file_logger.cpp +++ b/llarp/util/file_logger.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace llarp { @@ -16,7 +17,7 @@ namespace llarp FileLogStream::FileLogStream(std::shared_ptr< thread::ThreadPool > disk, FILE *f, llarp_time_t flushInterval, bool closeFile) - : m_Disk(disk) + : m_Disk(std::move(disk)) , m_File(f) , m_FlushInterval(flushInterval) , m_Close(closeFile) diff --git a/llarp/util/file_logger.hpp b/llarp/util/file_logger.hpp index 6143e775b..463b987bf 100644 --- a/llarp/util/file_logger.hpp +++ b/llarp/util/file_logger.hpp @@ -15,7 +15,7 @@ namespace llarp FileLogStream(std::shared_ptr< thread::ThreadPool > disk, FILE* f, llarp_time_t flushInterval, bool closefile = true); - ~FileLogStream(); + ~FileLogStream() override; void PreLog(std::stringstream& out, LogLevel lvl, const char* fname, int lineno, diff --git a/llarp/util/json.cpp b/llarp/util/json.cpp index dadf9d47f..9c9358ca2 100644 --- a/llarp/util/json.cpp +++ b/llarp/util/json.cpp @@ -21,7 +21,7 @@ namespace llarp size_t m_Offset; bool - FeedData(const char* buf, size_t sz) + FeedData(const char* buf, size_t sz) override { if(m_Offset + sz > m_Buf.size() - 1) return false; @@ -32,7 +32,7 @@ namespace llarp } Result - Parse(nlohmann::json& obj) const + Parse(nlohmann::json& obj) const override { if(m_Offset < m_Buf.size() - 1) return eNeedData; diff --git a/llarp/util/json.hpp b/llarp/util/json.hpp index 0fdd4a3dc..a9b8b8571 100644 --- a/llarp/util/json.hpp +++ b/llarp/util/json.hpp @@ -16,9 +16,7 @@ namespace llarp struct IParser { - virtual ~IParser() - { - } + virtual ~IParser() = default; /// result from feeding data to parser enum Result diff --git a/llarp/util/mem.cpp b/llarp/util/mem.cpp index cb082353a..210c9a8c8 100644 --- a/llarp/util/mem.cpp +++ b/llarp/util/mem.cpp @@ -9,7 +9,7 @@ namespace llarp void Zero(void *ptr, size_t sz) { - uint8_t *p = (uint8_t *)ptr; + auto *p = (uint8_t *)ptr; while(sz--) { *p = 0; @@ -30,9 +30,9 @@ llarp_mem_slab(ABSL_ATTRIBUTE_UNUSED struct llarp_alloc *mem, bool llarp_eq(const void *a, const void *b, size_t sz) { - bool result = true; - const uint8_t *a_ptr = (const uint8_t *)a; - const uint8_t *b_ptr = (const uint8_t *)b; + bool result = true; + const auto *a_ptr = (const uint8_t *)a; + const auto *b_ptr = (const uint8_t *)b; while(sz--) { result &= a_ptr[sz] == b_ptr[sz]; diff --git a/llarp/util/mem.h b/llarp/util/mem.h index acc98855c..313837038 100644 --- a/llarp/util/mem.h +++ b/llarp/util/mem.h @@ -1,8 +1,8 @@ #ifndef LLARP_MEM_H_ #define LLARP_MEM_H_ -#include -#include -#include + +#include +#include /** constant time memcmp */ bool diff --git a/llarp/util/metrics_core.cpp b/llarp/util/metrics_core.cpp index 932d5215a..e58b63dbc 100644 --- a/llarp/util/metrics_core.cpp +++ b/llarp/util/metrics_core.cpp @@ -135,7 +135,7 @@ namespace llarp void Registry::setFormat(const Id &id, const Format &format) { - Description *description = const_cast< Description * >(id.description()); + auto *description = const_cast< Description * >(id.description()); absl::WriterMutexLock l(&m_mutex); @@ -197,7 +197,7 @@ namespace llarp const SampleGroup< int > &intGroup, const absl::Time &timeStamp) { - SampleCache::iterator it = cache.find(publisher); + auto it = cache.find(publisher); if(it == cache.end()) { Sample sample; @@ -227,7 +227,7 @@ namespace llarp manager.m_intRepo.collect(category)); // Get the time since last reset, and clear if needed. - Manager::ResetTimes::iterator it = manager.m_resetTimes.find(category); + auto it = manager.m_resetTimes.find(category); if(it == manager.m_resetTimes.end()) { if(clear) @@ -432,14 +432,13 @@ namespace llarp struct PublisherSchedulerData { util::Mutex m_mutex; - thread::Scheduler::Handle m_handle; - std::set< const Category * > m_categories; + thread::Scheduler::Handle m_handle GUARDED_BY(m_mutex); + std::set< const Category * > m_categories GUARDED_BY(m_mutex); - bool m_default; - std::set< const Category * > m_nonDefaultCategories; + bool m_default GUARDED_BY(m_mutex){false}; + std::set< const Category * > m_nonDefaultCategories GUARDED_BY(m_mutex); - PublisherSchedulerData() - : m_handle(thread::Scheduler::INVALID_HANDLE), m_default(false) + PublisherSchedulerData() : m_handle(thread::Scheduler::INVALID_HANDLE) { } }; @@ -506,11 +505,9 @@ namespace llarp m_categories.erase(it); auto data = repeatIt->second; - { - util::Lock l(&data->m_mutex); - assert(data->m_categories.find(category) != data->m_categories.end()); - data->m_categories.erase(category); - } + util::Lock l(&data->m_mutex); + assert(data->m_categories.find(category) != data->m_categories.end()); + data->m_categories.erase(category); if(!data->m_default) { @@ -526,7 +523,7 @@ namespace llarp assert(defaultIntervalIt != m_repeaters.end()); auto &defaultRepeater = defaultIntervalIt->second; - util::Lock l(&defaultRepeater->m_mutex); + util::Lock lock(&defaultRepeater->m_mutex); defaultRepeater->m_nonDefaultCategories.erase(category); } } @@ -547,6 +544,8 @@ namespace llarp assert(repeatIt != m_repeaters.end()); auto data = repeatIt->second; + util::Lock l(&data->m_mutex); + if(data->m_categories.empty()) { assert(data->m_handle != thread::Scheduler::INVALID_HANDLE); @@ -555,7 +554,6 @@ namespace llarp } else { - util::Lock l(&data->m_mutex); data->m_default = false; data->m_nonDefaultCategories.clear(); } @@ -593,9 +591,9 @@ namespace llarp if(repeatIt == m_repeaters.end()) { data = std::make_shared< PublisherSchedulerData >(); + util::Lock lock(&data->m_mutex); data->m_categories.insert(category); m_repeaters.emplace(interval, data); - util::Lock lock(&data->m_mutex); data->m_handle = m_scheduler.scheduleRepeat( interval, std::bind(&PublisherScheduler::publish, this, data)); } @@ -609,6 +607,7 @@ namespace llarp // If this isn't being added to the default schedule, then add to the set // of non-default categories in the default schedule. + util::Lock dataLock(&data->m_mutex); if(!data->m_default && m_defaultInterval != absl::Duration()) { auto defaultIntervalIt = m_repeaters.find(m_defaultInterval); @@ -656,7 +655,7 @@ namespace llarp util::Lock lock(&data->m_mutex); data->m_default = true; - Categories::iterator cIt = m_categories.begin(); + auto cIt = m_categories.begin(); for(; cIt != m_categories.end(); ++cIt) { if(cIt->second != interval) @@ -679,7 +678,7 @@ namespace llarp { util::Lock l(&m_mutex); - Categories::iterator it = m_categories.find(category); + auto it = m_categories.find(category); if(it == m_categories.end()) { // This category has no specific schedule. @@ -703,6 +702,7 @@ namespace llarp util::Lock l(&m_mutex); for(auto &repeat : m_repeaters) { + util::Lock dataLock(&repeat.second->m_mutex); m_scheduler.cancelRepeat(repeat.second->m_handle, true); } diff --git a/llarp/util/metrics_core.hpp b/llarp/util/metrics_core.hpp index fecfbee27..512fa18ac 100644 --- a/llarp/util/metrics_core.hpp +++ b/llarp/util/metrics_core.hpp @@ -8,6 +8,7 @@ #include #include +#include #include namespace llarp @@ -56,7 +57,7 @@ namespace llarp private: TaggedRecordsType m_records GUARDED_BY(m_mutex); - mutable util::Mutex m_mutex; + mutable util::Mutex m_mutex; // protects m_records Collector(const Collector &) = delete; Collector & @@ -279,8 +280,9 @@ namespace llarp std::set< std::string > m_stringmem GUARDED_BY(m_mutex); CategoryMap m_categories GUARDED_BY(m_mutex); MetricMap m_metrics GUARDED_BY(m_mutex); - bool m_defaultEnabled GUARDED_BY(m_mutex); - mutable util::Mutex m_mutex; + bool m_defaultEnabled GUARDED_BY(m_mutex){true}; + mutable util::Mutex m_mutex; // protects m_stringmem, m_categories, + // m_metrics, m_defaultEnabled Registry(const Registry &) = delete; Registry & @@ -291,9 +293,7 @@ namespace llarp EXCLUSIVE_LOCKS_REQUIRED(m_mutex); public: - Registry() : m_defaultEnabled(true) - { - } + Registry() = default; Id add(string_view category, string_view name) LOCKS_EXCLUDED(m_mutex); @@ -347,12 +347,10 @@ namespace llarp DoubleRecords doubleRecords; IntRecords intRecords; - Records() - { - } + Records() = default; - Records(const DoubleRecords &d, const IntRecords &i) - : doubleRecords(d), intRecords(i) + Records(DoubleRecords d, IntRecords i) + : doubleRecords(std::move(d)), intRecords(std::move(i)) { } }; @@ -374,16 +372,16 @@ namespace llarp Registry *m_registry; IdCollectors m_collectors; - CategoryCollectors m_categories; + CategoryCollectors m_categories GUARDED_BY(m_mutex); - mutable util::Mutex m_mutex; + mutable util::Mutex m_mutex; // protects m_categories CollectorRepo(const CollectorRepo &) = delete; CollectorRepo & operator=(const CollectorRepo &) = delete; Collectors< Type > & - getCollectors(const Id &id) + getCollectors(const Id &id) SHARED_LOCKS_REQUIRED(m_mutex) { auto it = m_collectors.find(id); @@ -406,7 +404,7 @@ namespace llarp template < TaggedRecords< Type > (Collectors< Type >::*func)() > std::vector< TaggedRecords< Type > > - collectOp(const Category *category) + collectOp(const Category *category) LOCKS_EXCLUDED(m_mutex) { absl::WriterMutexLock l(&m_mutex); @@ -452,7 +450,7 @@ namespace llarp } Collector< Type > * - defaultCollector(const Id &id) + defaultCollector(const Id &id) LOCKS_EXCLUDED(m_mutex) { { absl::ReaderMutexLock l(&m_mutex); @@ -476,14 +474,14 @@ namespace llarp } std::shared_ptr< Collector< Type > > - addCollector(const Id &id) + addCollector(const Id &id) LOCKS_EXCLUDED(m_mutex) { absl::WriterMutexLock l(&m_mutex); return getCollectors(id).add(); } std::vector< std::shared_ptr< Collector< Type > > > - allCollectors(const Id &id) + allCollectors(const Id &id) LOCKS_EXCLUDED(m_mutex) { absl::ReaderMutexLock l(&m_mutex); @@ -916,7 +914,7 @@ namespace llarp { manager = DefaultManager::manager(manager); return manager ? (manager->*repoFunc)().defaultCollector(category, name) - : 0; + : nullptr; } static Collector * @@ -1134,7 +1132,8 @@ namespace llarp Repeaters m_repeaters GUARDED_BY(m_mutex); absl::Duration m_defaultInterval GUARDED_BY(m_mutex); - mutable util::Mutex m_mutex; + mutable util::Mutex + m_mutex; // protected m_categories, m_repeaters, m_defaultInterval void publish(const std::shared_ptr< PublisherSchedulerData > &data) const; diff --git a/llarp/util/metrics_types.hpp b/llarp/util/metrics_types.hpp index 78836e18d..6a9d9dfe6 100644 --- a/llarp/util/metrics_types.hpp +++ b/llarp/util/metrics_types.hpp @@ -51,12 +51,12 @@ namespace llarp struct FormatSpec { - float m_scale; + float m_scale{1.0}; string_view m_format; static constexpr char DEFAULT_FORMAT[] = "%f"; - constexpr FormatSpec() : m_scale(1.0), m_format(DEFAULT_FORMAT) + constexpr FormatSpec() : m_format(DEFAULT_FORMAT) { } @@ -200,56 +200,56 @@ namespace llarp } void - category(const Category *c) + category(const Category *c) LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); m_category = c; } const Category * - category() const + category() const LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); return m_category; } void - name(string_view n) + name(string_view n) LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); m_name = n; } string_view - name() const + name() const LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); return m_name; } void - type(Publication::Type t) + type(Publication::Type t) LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); m_type = t; } Publication::Type - type() const + type() const LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); return m_type; } void - format(const std::shared_ptr< Format > &f) + format(const std::shared_ptr< Format > &f) LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); m_format = f; } std::shared_ptr< Format > - format() const + format() const LOCKS_EXCLUDED(m_mutex) { util::Lock l(&m_mutex); return m_format; @@ -272,12 +272,10 @@ namespace llarp /// order to make things like static initialisation cleaner. class Id { - const Description *m_description; + const Description *m_description{nullptr}; public: - constexpr Id() : m_description(nullptr) - { - } + constexpr Id() = default; constexpr Id(const Description *description) : m_description(description) { @@ -395,7 +393,7 @@ namespace llarp template < typename Type > class Record { - size_t m_count; + size_t m_count{0}; Type m_total; Type m_min; Type m_max; @@ -406,8 +404,7 @@ namespace llarp static constexpr Type DEFAULT_MAX() { return RecordMax::max(); }; // clang-format on - Record() - : m_count(0), m_total(0.0), m_min(DEFAULT_MIN()), m_max(DEFAULT_MAX()) + Record() : m_total(0.0), m_min(DEFAULT_MIN()), m_max(DEFAULT_MAX()) { } @@ -603,12 +600,12 @@ namespace llarp absl::Time m_sampleTime; std::vector< absl::variant< SampleGroup< double >, SampleGroup< int > > > m_samples; - size_t m_recordCount; + size_t m_recordCount{0}; public: using const_iterator = typename decltype(m_samples)::const_iterator; - Sample() : m_sampleTime(), m_recordCount(0) + Sample() : m_sampleTime() { } diff --git a/llarp/util/ostream_logger.hpp b/llarp/util/ostream_logger.hpp index cbcb9d9ac..f423d8cc4 100644 --- a/llarp/util/ostream_logger.hpp +++ b/llarp/util/ostream_logger.hpp @@ -10,18 +10,16 @@ namespace llarp { OStreamLogStream(std::ostream& out); - ~OStreamLogStream() - { - } + ~OStreamLogStream() override = default; - virtual void + void PreLog(std::stringstream& s, LogLevel lvl, const char* fname, int lineno, const std::string& nodename) const override; void Print(LogLevel lvl, const char* tag, const std::string& msg) override; - virtual void + void PostLog(std::stringstream& ss) const override; void Tick(llarp_time_t) override diff --git a/llarp/util/queue_manager.cpp b/llarp/util/queue_manager.cpp index e799ba668..bdec4b55e 100644 --- a/llarp/util/queue_manager.cpp +++ b/llarp/util/queue_manager.cpp @@ -226,7 +226,7 @@ namespace llarp uint32_t elemGen = decodeGenerationFromElementState(compare); - int32_t difference = static_cast< int32_t >(currGen - elemGen); + auto difference = static_cast< int32_t >(currGen - elemGen); if(difference == 1 || (difference == -static_cast< int32_t >(m_maxGeneration))) @@ -326,7 +326,7 @@ namespace llarp uint32_t elemGen = decodeGenerationFromElementState(compare); ElementState state = decodeStateFromElementState(compare); - int32_t difference = static_cast< int32_t >(currGen - elemGen); + auto difference = static_cast< int32_t >(currGen - elemGen); if(difference == 1 || (difference == -static_cast< int32_t >(m_maxGeneration))) @@ -455,9 +455,9 @@ namespace llarp assert(0 < circularDifference(endCombinedIndex, loadedCombinedIndex, m_maxCombinedIndex + 1)); - uint32_t currIdx = + auto currIdx = static_cast< uint32_t >(loadedCombinedIndex % m_capacity); - uint32_t currGen = + auto currGen = static_cast< uint32_t >(loadedCombinedIndex / m_capacity); // Try to swap this cell from Full to Reading. diff --git a/llarp/util/scheduler.cpp b/llarp/util/scheduler.cpp index 3c45c1878..0b56f4efc 100644 --- a/llarp/util/scheduler.cpp +++ b/llarp/util/scheduler.cpp @@ -1,4 +1,5 @@ #include +#include namespace llarp { @@ -152,9 +153,9 @@ namespace llarp } } - Scheduler::Scheduler(const EventDispatcher& dispatcher, const Clock& clock) - : m_clock(clock) - , m_dispatcher(dispatcher) + Scheduler::Scheduler(EventDispatcher dispatcher, Clock clock) + : m_clock(std::move(clock)) + , m_dispatcher(std::move(dispatcher)) , m_running(false) , m_iterationCount(0) , m_eventIt() diff --git a/llarp/util/scheduler.hpp b/llarp/util/scheduler.hpp index 7bcb9854f..970ecfda1 100644 --- a/llarp/util/scheduler.hpp +++ b/llarp/util/scheduler.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace llarp @@ -40,8 +41,8 @@ namespace llarp std::atomic_bool m_isCancelled; Handle m_handle; - RepeatData(const Callback& callback, absl::Duration period) - : m_callback(callback) + RepeatData(Callback callback, absl::Duration period) + : m_callback(std::move(callback)) , m_period(period) , m_isCancelled(false) , m_handle(0) @@ -133,7 +134,7 @@ namespace llarp { } - Scheduler(const EventDispatcher& dispatcher, const Clock& clock); + Scheduler(EventDispatcher dispatcher, Clock clock); ~Scheduler(); diff --git a/llarp/util/stopwatch.hpp b/llarp/util/stopwatch.hpp index cfc992793..25a772c22 100644 --- a/llarp/util/stopwatch.hpp +++ b/llarp/util/stopwatch.hpp @@ -14,9 +14,7 @@ namespace llarp absl::optional< absl::Time > m_stop; public: - Stopwatch() - { - } + Stopwatch() = default; void start() diff --git a/llarp/util/threading.hpp b/llarp/util/threading.hpp index fb3608171..05113a2b3 100644 --- a/llarp/util/threading.hpp +++ b/llarp/util/threading.hpp @@ -32,6 +32,7 @@ namespace llarp ~NullLock() UNLOCK_FUNCTION() { + (void)this; // trick clang-tidy } }; diff --git a/llarp/util/threadpool.h b/llarp/util/threadpool.h index 3a513d44e..bd1103ea4 100644 --- a/llarp/util/threadpool.h +++ b/llarp/util/threadpool.h @@ -25,7 +25,7 @@ struct llarp_threadpool } llarp_threadpool() - : jobs(new llarp::thread::Queue< std::function< void(void) > >(128)) + : jobs(new llarp::thread::Queue< std::function< void() > >(128)) , callingPID(llarp::util::GetPid()) { jobs->enable(); @@ -59,23 +59,21 @@ llarp_init_same_process_threadpool(); void llarp_free_threadpool(struct llarp_threadpool **tp); -typedef void (*llarp_thread_work_func)(void *); +using llarp_thread_work_func = void (*)(void *); /** job to be done in worker thread */ struct llarp_thread_job { /** user data to pass to work function */ - void *user; + void *user{nullptr}; /** called in threadpool worker thread */ - llarp_thread_work_func work; + llarp_thread_work_func work{nullptr}; #ifdef __cplusplus llarp_thread_job(void *u, llarp_thread_work_func w) : user(u), work(w) { } - llarp_thread_job() : user(nullptr), work(nullptr) - { - } + llarp_thread_job() = default; #endif }; diff --git a/llarp/util/timer.cpp b/llarp/util/timer.cpp index 08d8ae522..c9278b5af 100644 --- a/llarp/util/timer.cpp +++ b/llarp/util/timer.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace llarp { @@ -28,15 +29,13 @@ namespace llarp , called_at(0) , started(now) , timeout(ms) - , func(_func) + , func(std::move(_func)) , done(false) , canceled(false) { } - ~timer() - { - } + ~timer() = default; void exec(); @@ -75,9 +74,7 @@ struct llarp_timer_context uint32_t currentId = 0; bool _run = true; - ~llarp_timer_context() - { - } + ~llarp_timer_context() = default; bool run() diff --git a/llarp/util/timerqueue.hpp b/llarp/util/timerqueue.hpp index eb2e0fd9c..5dd283748 100644 --- a/llarp/util/timerqueue.hpp +++ b/llarp/util/timerqueue.hpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace llarp { @@ -55,7 +56,7 @@ namespace llarp private: struct Node { - int m_index; + int m_index{0}; absl::Time m_time; Key m_key; Node* m_prev; @@ -63,8 +64,7 @@ namespace llarp object::Buffer< Value > m_value; Node() - : m_index(0) - , m_time() + : m_time() , m_key(nullptr) , m_prev(nullptr) , m_next(nullptr) @@ -73,8 +73,7 @@ namespace llarp } explicit Node(const absl::Time& time) - : m_index(0) - , m_time(time) + : m_time(time) , m_key(nullptr) , m_prev(nullptr) , m_next(nullptr) @@ -311,9 +310,12 @@ namespace llarp { } - TimerQueueItem(absl::Time time, const Value& value, Handle handle, + TimerQueueItem(absl::Time time, Value value, Handle handle, const Key& key) - : m_time(time), m_value(value), m_handle(handle), m_key(key) + : m_time(time) + , m_value(std::move(value)) + , m_handle(handle) + , m_key(key) { } diff --git a/llarp/utp/inbound_message.hpp b/llarp/utp/inbound_message.hpp index e8d492425..0bcf69757 100644 --- a/llarp/utp/inbound_message.hpp +++ b/llarp/utp/inbound_message.hpp @@ -45,7 +45,7 @@ namespace llarp struct InboundMessage { /// timestamp of last activity - llarp_time_t lastActive; + llarp_time_t lastActive{0}; /// the underlying message buffer MessageBuffer _msg; @@ -63,7 +63,7 @@ namespace llarp bool AppendData(const byte_t* ptr, uint16_t sz); - InboundMessage() : lastActive(0), _msg(), buffer(_msg) + InboundMessage() : _msg(), buffer(_msg) { } diff --git a/llarp/utp/linklayer.cpp b/llarp/utp/linklayer.cpp index 3078e9884..b0b589fc5 100644 --- a/llarp/utp/linklayer.cpp +++ b/llarp/utp/linklayer.cpp @@ -18,7 +18,7 @@ #endif #include -#include +#include namespace llarp { @@ -27,9 +27,9 @@ namespace llarp uint64 LinkLayer::OnConnect(utp_callback_arguments* arg) { - LinkLayer* l = + auto* l = static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); - Session* session = static_cast< Session* >(utp_get_userdata(arg->socket)); + auto* session = static_cast< Session* >(utp_get_userdata(arg->socket)); if(session && l) session->OutboundLinkEstablished(l); return 0; @@ -38,7 +38,7 @@ namespace llarp uint64 LinkLayer::SendTo(utp_callback_arguments* arg) { - LinkLayer* l = + auto* l = static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); if(l == nullptr) return 0; @@ -49,9 +49,9 @@ namespace llarp uint64 LinkLayer::OnError(utp_callback_arguments* arg) { - Session* session = static_cast< Session* >(utp_get_userdata(arg->socket)); + auto* session = static_cast< Session* >(utp_get_userdata(arg->socket)); - LinkLayer* link = + auto* link = static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); if(session && link) @@ -270,7 +270,7 @@ namespace llarp uint64 LinkLayer::OnRead(utp_callback_arguments* arg) { - Session* self = static_cast< Session* >(utp_get_userdata(arg->socket)); + auto* self = static_cast< Session* >(utp_get_userdata(arg->socket)); if(self) { @@ -297,12 +297,12 @@ namespace llarp uint64 LinkLayer::OnStateChange(utp_callback_arguments* arg) { - Session* session = static_cast< Session* >(utp_get_userdata(arg->socket)); + auto* session = static_cast< Session* >(utp_get_userdata(arg->socket)); if(session) { if(arg->state == UTP_STATE_WRITABLE) { - session->PumpWrite(); + session->Pump(); } else if(arg->state == UTP_STATE_EOF) { @@ -316,7 +316,7 @@ namespace llarp uint64 LinkLayer::OnAccept(utp_callback_arguments* arg) { - LinkLayer* self = + auto* self = static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); Addr remote(*arg->address); diff --git a/llarp/utp/linklayer.hpp b/llarp/utp/linklayer.hpp index 44c1d0f29..cb7f6929f 100644 --- a/llarp/utp/linklayer.hpp +++ b/llarp/utp/linklayer.hpp @@ -53,7 +53,7 @@ namespace llarp SessionClosedHandler closed, bool acceptInbound); /// destruct - ~LinkLayer(); + ~LinkLayer() override; /// get AI rank uint16_t diff --git a/llarp/utp/session.cpp b/llarp/utp/session.cpp index 99444b352..1be1c16ff 100644 --- a/llarp/utp/session.cpp +++ b/llarp/utp/session.cpp @@ -20,7 +20,7 @@ namespace llarp /// pump tx queue void - Session::PumpWrite() + Session::PumpWrite(size_t numSend) { if(!sock) return; @@ -31,10 +31,11 @@ namespace llarp { for(const auto& vec : msg.vecs) { - if(vec.iov_len) + if(vec.iov_len > 0) { expect += vec.iov_len; send.emplace_back(vec); + numSend--; } } } @@ -147,10 +148,20 @@ namespace llarp Session::Tick(llarp_time_t now) { PruneInboundMessages(now); - m_TXRate = 0; - m_RXRate = 0; - metrics::integerTick("utp.session.sendq", "size", sendq.size(), "id", - RouterID(remoteRC.pubkey).ToString()); + // ensure that this section is called every 1s or so + if(now > m_LastTick && now - m_LastTick >= 1000) + { + m_TXRate = 0; + m_RXRate = 0; + metrics::integerTick("utp.session.sendq", "size", sendq.size(), "id", + RouterID(remoteRC.pubkey).ToString()); + m_LastTick = now; + } + else + { + // try sending 1 segment + PumpWrite(1); + } } /// low level read @@ -209,13 +220,29 @@ namespace llarp return now - lastActive > 5000; if(state == eSessionReady) { - // don't time out the connection if backlogged in downstream direction - // for clients dangling off the side of the network - const auto timestamp = - remoteRC.IsPublicRouter() ? lastSend : lastActive; - if(now <= timestamp) - return false; - return now - timestamp > 30000; + const bool remoteIsSNode = remoteRC.IsPublicRouter(); + const bool weAreSnode = parent->GetOurRC().IsPublicRouter(); + const bool recvTimeout = + (now > lastActive) && now - lastActive > sessionTimeout; + const bool sendTimeout = + (now > lastSend) && now - lastSend > sessionTimeout; + if(remoteIsSNode && weAreSnode) + { + /// for s2s connections only check write direction + return sendTimeout; + } + else if(weAreSnode) + { + // for edge connection as service node check either direction for + // timeout + return recvTimeout || sendTimeout; + } + else + { + /// for edge connections as client we check if both directions have + /// been silent for 60s + return recvTimeout && sendTimeout; + } } if(state == eLinkEstablished) return now - lastActive @@ -268,8 +295,10 @@ namespace llarp void Session::Pump() { - // pump write queue - PumpWrite(); + if(sendq.size() > (MaxSendQueueSize / 4)) + PumpWrite(sendq.size() / 2); + else + PumpWrite(sendq.size()); // prune inbound messages PruneInboundMessages(parent->Now()); } @@ -282,7 +311,7 @@ namespace llarp if(SendQueueBacklog() >= MaxSendQueueSize) { // pump write queue if we seem to be full - PumpWrite(); + PumpWrite(MaxSendQueueSize / 2); } if(SendQueueBacklog() >= MaxSendQueueSize) { @@ -308,7 +337,7 @@ namespace llarp sz -= s; } if(state != eSessionReady) - PumpWrite(); + PumpWrite(sendq.size()); return true; } diff --git a/llarp/utp/session.hpp b/llarp/utp/session.hpp index b605850f3..20a07766d 100644 --- a/llarp/utp/session.hpp +++ b/llarp/utp/session.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -42,7 +43,7 @@ namespace llarp struct OutboundMessage { OutboundMessage(uint32_t id, CompletionHandler func) - : msgid{id}, completed{func} + : msgid{id}, completed{std::move(func)} { } @@ -101,6 +102,8 @@ namespace llarp uint64_t m_RXRate = 0; uint64_t m_TXRate = 0; + llarp_time_t m_LastTick = 0; + /// mark session as alive void Alive(); @@ -108,7 +111,7 @@ namespace llarp util::StatusObject ExtractStatus() const override; - virtual ~Session() = 0; + ~Session() override = 0; /// base explicit Session(LinkLayer* p); @@ -147,7 +150,7 @@ namespace llarp /// pump tx queue void - PumpWrite(); + PumpWrite(size_t numMessages); void Pump() override; diff --git a/vendor/cxxopts/CHANGELOG.md b/vendor/cxxopts/CHANGELOG.md index 7bcbf72ab..bd0eed198 100644 --- a/vendor/cxxopts/CHANGELOG.md +++ b/vendor/cxxopts/CHANGELOG.md @@ -3,16 +3,29 @@ This is the changelog for `cxxopts`, a C++11 library for parsing command line options. The project adheres to semantic versioning. +## Next version + +### Changed + +* Only search for a C++ compiler in CMakeLists.txt. +* Allow for exceptions to be disabled. +* Fix duplicate default options when there is a short and long option. +* Add `CXXOPTS_NO_EXCEPTIONS` to disable exceptions. + ## 2.2 ### Changed * Allow integers to have leading zeroes. * Build the tests by default. +* Don't check for container when showing positional help. ### Added * Iterator inputs to `parse_positional`. +* Throw an exception if the option in `parse_positional` doesn't exist. +* Parse a delimited list in a single argument for vector options. +* Add an option to disable implicit value on booleans. ### Bug Fixes @@ -22,6 +35,7 @@ options. The project adheres to semantic versioning. * Throw on invalid option syntax when beginning with a `-`. * Throw in `as` when option wasn't present. * Fix catching exceptions by reference. +* Fix out of bounds errors parsing integers. ## 2.1.1 diff --git a/vendor/cxxopts/CMakeLists.txt b/vendor/cxxopts/CMakeLists.txt index e866cb128..1b524f723 100644 --- a/vendor/cxxopts/CMakeLists.txt +++ b/vendor/cxxopts/CMakeLists.txt @@ -30,12 +30,13 @@ endforeach() set(VERSION ${CXXOPTS__VERSION_MAJOR}.${CXXOPTS__VERSION_MINOR}.${CXXOPTS__VERSION_PATCH}) message(STATUS "cxxopts version ${VERSION}") -project(cxxopts VERSION "${VERSION}") +project(cxxopts VERSION "${VERSION}" LANGUAGES CXX) enable_testing() option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON) option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" ON) +option(CXXOPTS_ENABLE_INSTALL "Generate the install target" ON) # request c++11 without gnu extension for the whole project and enable more warnings if (CXXOPTS_CXX_STANDARD) @@ -53,6 +54,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES endif() add_library(cxxopts INTERFACE) +add_library(cxxopts::cxxopts ALIAS cxxopts) # optionally, enable unicode support using the ICU library set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library") @@ -70,35 +72,37 @@ target_include_directories(cxxopts INTERFACE $ ) -include(CMakePackageConfigHelpers) -set(CXXOPTS_CMAKE_DIR "lib/cmake/cxxopts" CACHE STRING - "Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.") -set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake") -set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake") -set(targets_export_name cxxopts-targets) +if(CXXOPTS_ENABLE_INSTALL) + include(CMakePackageConfigHelpers) + set(CXXOPTS_CMAKE_DIR "lib/cmake/cxxopts" CACHE STRING + "Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.") + set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake") + set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake") + set(targets_export_name cxxopts-targets) -# Generate the version, config and target files into the build directory. -write_basic_package_version_file( - ${version_config} - VERSION ${VERSION} - COMPATIBILITY AnyNewerVersion) -configure_package_config_file( - ${PROJECT_SOURCE_DIR}/cxxopts-config.cmake.in - ${project_config} - INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR}) -export(TARGETS cxxopts NAMESPACE cxxopts:: - FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake) + # Generate the version, config and target files into the build directory. + write_basic_package_version_file( + ${version_config} + VERSION ${VERSION} + COMPATIBILITY AnyNewerVersion) + configure_package_config_file( + ${PROJECT_SOURCE_DIR}/cxxopts-config.cmake.in + ${project_config} + INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR}) + export(TARGETS cxxopts NAMESPACE cxxopts:: + FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake) -# Install version, config and target files. -#install( -# FILES ${project_config} ${version_config} -# DESTINATION ${CXXOPTS_CMAKE_DIR}) -#install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR} -# NAMESPACE cxxopts::) + # Install version, config and target files. + install( + FILES ${project_config} ${version_config} + DESTINATION ${CXXOPTS_CMAKE_DIR}) + install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR} + NAMESPACE cxxopts::) -# Install the header file and export the target -#install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION lib) -#install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include) + # Install the header file and export the target + install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION lib) + install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include) +endif() add_subdirectory(src) add_subdirectory(test) diff --git a/vendor/cxxopts/README.md b/vendor/cxxopts/README.md index fa2bce56c..93dcf3b10 100644 --- a/vendor/cxxopts/README.md +++ b/vendor/cxxopts/README.md @@ -116,6 +116,18 @@ There is no way to disambiguate positional arguments from the value following a boolean, so we have chosen that they will be positional arguments, and therefore, `-o false` does not work. +## `std::vector` values + +Parsing of list of values in form of an `std::vector` is also supported, as long as `T` +can be parsed. To separate single values in a list the definition `CXXOPTS_VECTOR_DELIMITER` +is used, which is ',' by default. Ensure that you use no whitespaces between values because +those would be interpreted as the next command line option. Example for a command line option +that can be parsed as a `std::vector`: + +~~~ +--my_list=1,-2.1,3,4.5 +~~~ + ## Custom help The string after the program name on the first line of the help can be diff --git a/vendor/cxxopts/include/cxxopts.hpp b/vendor/cxxopts/include/cxxopts.hpp index 94b81ef0b..9cde72d6b 100644 --- a/vendor/cxxopts/include/cxxopts.hpp +++ b/vendor/cxxopts/include/cxxopts.hpp @@ -29,6 +29,7 @@ THE SOFTWARE. #include #include #include +#include #include #include #include @@ -43,6 +44,10 @@ THE SOFTWARE. #define CXXOPTS_HAS_OPTIONAL #endif +#ifndef CXXOPTS_VECTOR_DELIMITER +#define CXXOPTS_VECTOR_DELIMITER ',' +#endif + #define CXXOPTS__VERSION_MAJOR 2 #define CXXOPTS__VERSION_MINOR 2 #define CXXOPTS__VERSION_PATCH 0 @@ -309,6 +314,9 @@ namespace cxxopts virtual std::shared_ptr implicit_value(const std::string& value) = 0; + virtual std::shared_ptr + no_implicit_value() = 0; + virtual bool is_boolean() const = 0; }; @@ -354,7 +362,7 @@ namespace cxxopts { public: option_exists_error(const std::string& option) - : OptionSpecException(u8"Option " + LQUOTE + option + RQUOTE + u8" already exists") + : OptionSpecException("Option " + LQUOTE + option + RQUOTE + " already exists") { } }; @@ -363,7 +371,7 @@ namespace cxxopts { public: invalid_option_format_error(const std::string& format) - : OptionSpecException(u8"Invalid option format " + LQUOTE + format + RQUOTE) + : OptionSpecException("Invalid option format " + LQUOTE + format + RQUOTE) { } }; @@ -371,8 +379,8 @@ namespace cxxopts class option_syntax_exception : public OptionParseException { public: option_syntax_exception(const std::string& text) - : OptionParseException(u8"Argument " + LQUOTE + text + RQUOTE + - u8" starts with a - but has incorrect syntax") + : OptionParseException("Argument " + LQUOTE + text + RQUOTE + + " starts with a - but has incorrect syntax") { } }; @@ -381,7 +389,7 @@ namespace cxxopts { public: option_not_exists_exception(const std::string& option) - : OptionParseException(u8"Option " + LQUOTE + option + RQUOTE + u8" does not exist") + : OptionParseException("Option " + LQUOTE + option + RQUOTE + " does not exist") { } }; @@ -391,7 +399,7 @@ namespace cxxopts public: missing_argument_exception(const std::string& option) : OptionParseException( - u8"Option " + LQUOTE + option + RQUOTE + u8" is missing an argument" + "Option " + LQUOTE + option + RQUOTE + " is missing an argument" ) { } @@ -402,7 +410,7 @@ namespace cxxopts public: option_requires_argument_exception(const std::string& option) : OptionParseException( - u8"Option " + LQUOTE + option + RQUOTE + u8" requires an argument" + "Option " + LQUOTE + option + RQUOTE + " requires an argument" ) { } @@ -417,8 +425,8 @@ namespace cxxopts const std::string& arg ) : OptionParseException( - u8"Option " + LQUOTE + option + RQUOTE + - u8" does not take an argument, but argument " + + "Option " + LQUOTE + option + RQUOTE + + " does not take an argument, but argument " + LQUOTE + arg + RQUOTE + " given" ) { @@ -429,7 +437,7 @@ namespace cxxopts { public: option_not_present_exception(const std::string& option) - : OptionParseException(u8"Option " + LQUOTE + option + RQUOTE + u8" not present") + : OptionParseException("Option " + LQUOTE + option + RQUOTE + " not present") { } }; @@ -442,7 +450,7 @@ namespace cxxopts const std::string& arg ) : OptionParseException( - u8"Argument " + LQUOTE + arg + RQUOTE + u8" failed to parse" + "Argument " + LQUOTE + arg + RQUOTE + " failed to parse" ) { } @@ -453,12 +461,32 @@ namespace cxxopts public: option_required_exception(const std::string& option) : OptionParseException( - u8"Option " + LQUOTE + option + RQUOTE + u8" is required but not present" + "Option " + LQUOTE + option + RQUOTE + " is required but not present" ) { } }; + template + void throw_or_mimic(const std::string& text) + { + static_assert(std::is_base_of::value, + "throw_or_mimic only works on std::exception and " + "deriving classes"); + +#ifndef CXXOPTS_NO_EXCEPTIONS + // If CXXOPTS_NO_EXCEPTIONS is not defined, just throw + throw T{text}; +#else + // Otherwise manually instantiate the exception, print what() to stderr, + // and abort + T exception{text}; + std::cerr << exception.what() << std::endl; + std::cerr << "Aborting (exceptions disabled)..." << std::endl; + std::abort(); +#endif + } + namespace values { namespace @@ -466,9 +494,9 @@ namespace cxxopts std::basic_regex integer_pattern ("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)"); std::basic_regex truthy_pattern - ("(t|T)(rue)?"); + ("(t|T)(rue)?|1"); std::basic_regex falsy_pattern - ("((f|F)(alse)?)?"); + ("(f|F)(alse)?|0"); } namespace detail @@ -485,16 +513,16 @@ namespace cxxopts { if (negative) { - if (u > static_cast(-(std::numeric_limits::min)())) + if (u > static_cast((std::numeric_limits::min)())) { - throw argument_incorrect_type(text); + throw_or_mimic(text); } } else { if (u > static_cast((std::numeric_limits::max)())) { - throw argument_incorrect_type(text); + throw_or_mimic(text); } } } @@ -523,14 +551,15 @@ namespace cxxopts // if we got to here, then `t` is a positive number that fits into // `R`. So to avoid MSVC C4146, we first cast it to `R`. // See https://github.com/jarro2783/cxxopts/issues/62 for more details. - return -static_cast(t); + return -static_cast(t-1)-1; } template T - checked_negate(T&&, const std::string& text, std::false_type) + checked_negate(T&& t, const std::string& text, std::false_type) { - throw argument_incorrect_type(text); + throw_or_mimic(text); + return t; } template @@ -542,7 +571,7 @@ namespace cxxopts if (match.length() == 0) { - throw argument_incorrect_type(text); + throw_or_mimic(text); } if (match.length(4) > 0) @@ -553,7 +582,6 @@ namespace cxxopts using US = typename std::make_unsigned::type; - constexpr auto umax = (std::numeric_limits::max)(); constexpr bool is_signed = std::numeric_limits::is_signed; const bool negative = match.length(1) > 0; const uint8_t base = match.length(2) > 0 ? 16 : 10; @@ -568,27 +596,28 @@ namespace cxxopts if (*iter >= '0' && *iter <= '9') { - digit = *iter - '0'; + digit = static_cast(*iter - '0'); } else if (base == 16 && *iter >= 'a' && *iter <= 'f') { - digit = *iter - 'a' + 10; + digit = static_cast(*iter - 'a' + 10); } else if (base == 16 && *iter >= 'A' && *iter <= 'F') { - digit = *iter - 'A' + 10; + digit = static_cast(*iter - 'A' + 10); } else { - throw argument_incorrect_type(text); + throw_or_mimic(text); } - if (umax - digit < result * base) + US next = result * base + digit; + if (result > next) { - throw argument_incorrect_type(text); + throw_or_mimic(text); } - result = result * base + digit; + result = next; } detail::check_signed_range(negative, result, text); @@ -601,7 +630,7 @@ namespace cxxopts } else { - value = result; + value = static_cast(result); } } @@ -611,7 +640,7 @@ namespace cxxopts std::stringstream in(text); in >> value; if (!in) { - throw argument_incorrect_type(text); + throw_or_mimic(text); } } @@ -691,7 +720,7 @@ namespace cxxopts return; } - throw argument_incorrect_type(text); + throw_or_mimic(text); } inline @@ -714,9 +743,13 @@ namespace cxxopts void parse_value(const std::string& text, std::vector& value) { - T v; - parse_value(text, v); - value.push_back(v); + std::stringstream in(text); + std::string token; + while(in.eof() == false && std::getline(in, token, CXXOPTS_VECTOR_DELIMITER)) { + T v; + parse_value(token, v); + value.emplace_back(std::move(v)); + } } #ifdef CXXOPTS_HAS_OPTIONAL @@ -825,6 +858,13 @@ namespace cxxopts return shared_from_this(); } + std::shared_ptr + no_implicit_value() + { + m_implicit = false; + return shared_from_this(); + } + std::string get_default_value() const { @@ -1035,21 +1075,29 @@ namespace cxxopts parse_default(std::shared_ptr details) { ensure_value(details); + m_default = true; m_value->parse(); } size_t - count() const + count() const noexcept { return m_count; } + // TODO: maybe default options should count towards the number of arguments + bool + has_default() const noexcept + { + return m_default; + } + template const T& as() const { if (m_value == nullptr) { - throw std::domain_error("No value"); + throw_or_mimic("No value"); } #ifdef CXXOPTS_NO_RTTI @@ -1071,6 +1119,7 @@ namespace cxxopts std::shared_ptr m_value; size_t m_count = 0; + bool m_default = false; }; class KeyValue @@ -1143,7 +1192,7 @@ namespace cxxopts if (iter == m_options->end()) { - throw option_not_present_exception(option); + throw_or_mimic(option); } auto riter = m_results.find(iter->second); @@ -1202,6 +1251,28 @@ namespace cxxopts std::vector m_sequential; }; + struct Option + { + Option + ( + const std::string& opts, + const std::string& desc, + const std::shared_ptr& value = ::cxxopts::value(), + const std::string& arg_help = "" + ) + : opts_(opts) + , desc_(desc) + , value_(value) + , arg_help_(arg_help) + { + } + + std::string opts_; + std::string desc_; + std::shared_ptr value_; + std::string arg_help_; + }; + class Options { typedef std::unordered_map> @@ -1254,6 +1325,20 @@ namespace cxxopts OptionAdder add_options(std::string group = ""); + void + add_options + ( + const std::string& group, + std::initializer_list