diff --git a/CMakeLists.txt b/CMakeLists.txt index b03e9a3f4..34ba8046b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,7 @@ if (WOW64_CROSS_COMPILE OR WIN64_CROSS_COMPILE) add_compile_options(-Wno-unused-command-line-argument -Wno-c++11-narrowing) add_compile_options($<$:-Wno-bad-function-cast>) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--allow-multiple-definition") if (NO_LIBGCC) set(CMAKE_CXX_STANDARD_LIBRARIES "-lgcc_eh ${CMAKE_CXX_STANDARD_LIBRARIES}") set(CMAKE_C_STANDARD_LIBRARIES "-lgcc_eh ${CMAKE_C_STANDARD_LIBRARIES}") @@ -238,6 +239,9 @@ set(CRYPTOGRAPHY_LIB ${LIB}-cryptography) set(UTIL_LIB ${LIB}-util) set(PLATFORM_LIB ${LIB}-platform) set(ANDROID_LIB ${LIB}android) +set(ABYSS libabyss) +set(ABYSS_LIB abyss) +set(ABYSS_EXE ${ABYSS_LIB}-main) get_filename_component(TT_ROOT "vendor/libtuntap-master" ABSOLUTE) add_definitions(-D${CMAKE_SYSTEM_NAME}) @@ -277,7 +281,7 @@ if(UNIX) message(FATAL_ERROR "Your operating system is not supported yet") endif() elseif(WIN32) - get_filename_component(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-windows.c ABSOLUTE) + 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=0x500 -D_WIN32_WINNT=0x500) else() @@ -299,19 +303,46 @@ if(NOT WIN32) add_subdirectory(vendor) endif(NOT WIN32) +function(add_log_tag target) + get_target_property(TARGET_SRCS ${target} SOURCES) + foreach(F ${TARGET_SRCS}) + set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS -DLOG_TAG=\\\"${F}\\\") + endforeach(F) +endfunction() + +if(USE_LIBABYSS) + add_definitions(-DUSE_ABYSS=1) + + set(ABYSS_SRC + ${ABYSS}/src/http.cpp + ${ABYSS}/src/client.cpp + ${ABYSS}/src/server.cpp) + add_library(${ABYSS_LIB} STATIC ${ABYSS_SRC}) +endif(USE_LIBABYSS) + add_subdirectory(crypto) add_subdirectory(libutp) add_subdirectory(llarp) -set(RC_SRC - daemon/rcutil.cpp -) - -set(ALL_SRC ${RC_SRC} ${EXE_SRC} ${LIB_PLATFORM_SRC} ${LIB_SRC}) +if(USE_LIBABYSS) + target_link_libraries(${ABYSS_LIB} PUBLIC ${PLATFORM_LIB}) -foreach(F ${ALL_SRC}) - set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS -DLOG_TAG=\\\"${F}\\\") -endforeach(F) + if (NOT WIN32) + add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp) + target_link_libraries(${ABYSS_EXE} PUBLIC ${ABYSS_LIB} ${STATIC_LIB} Threads::Threads) + else() + add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp llarp/win32/abyss.rc) + target_link_libraries(${ABYSS_EXE} PUBLIC ${ABYSS_LIB} ${STATIC_LIB} ws2_32) + endif(NOT WIN32) + target_include_directories(${ABYSS_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include") + target_include_directories(${ABYSS_EXE} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include") + # for freebsd + if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") + target_include_directories(${ABYSS_LIB} /usr/local/include) + endif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") + add_log_tag(${ABYSS_EXE}) + add_log_tag(${ABYSS_LIB}) +endif(USE_LIBABYSS) if(SHADOW) add_shadow_plugin(shadow-plugin-${SHARED_LIB} ${EXE_SRC} ${LIB_SRC} ${UTP_SRC} ${LIB_PLATFORM_SRC} ${CPP_BACKPORT_SRC} ${ABYSS_SRC} ${CRYPTOGRAPHY_SRC}) @@ -324,6 +355,8 @@ else() add_executable(${EXE} ${EXE_SRC} llarp/win32/version.rc) endif(NOT WIN32) + add_log_tag(${EXE}) + install(TARGETS ${EXE} RUNTIME DESTINATION bin) if(WIN32) install(PROGRAMS ${CMAKE_SOURCE_DIR}/lokinet-bootstrap.exe DESTINATION bin) @@ -336,49 +369,19 @@ else() endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") if (WIN32) - target_link_libraries(${EXE} ${STATIC_LIB} ws2_32 iphlpapi) + target_link_libraries(${EXE} PUBLIC ${STATIC_LIB} ws2_32 iphlpapi) else() - target_link_libraries(${EXE} ${STATIC_LIB} cppbackport libutp) + target_link_libraries(${EXE} PUBLIC ${STATIC_LIB} cppbackport libutp) endif(WIN32) if(ANDROID) add_library(${ANDROID_LIB} SHARED jni/lokinet_android.cpp) + add_log_tag(${ANDROID_LIB}) target_link_libraries(${ANDROID_LIB} ${STATIC_LIB} ${LIBS}) endif(ANDROID) endif(SHADOW) -if(USE_LIBABYSS) - add_definitions(-DUSE_ABYSS=1) - set(ABYSS libabyss) - set(ABYSS_LIB abyss) - set(ABYSS_EXE ${ABYSS_LIB}-main) - set(ABYSS_SRC - ${ABYSS}/src/http.cpp - ${ABYSS}/src/client.cpp - ${ABYSS}/src/server.cpp - ${ABYSS}/src/json.cpp) - add_library(${ABYSS_LIB} STATIC ${ABYSS_SRC}) - target_link_libraries(${ABYSS_LIB} PUBLIC ${PLATFORM_LIB}) - list(APPEND ALL_SRC ${ABYSS_SRC} ${ABYSS}/main.cpp) - - if (NOT WIN32) - add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp) - else() - add_executable(${ABYSS_EXE} ${ABYSS}/main.cpp llarp/win32/abyss.rc) - target_link_libraries(${ABYSS_EXE} ws2_32 iphlpapi) - endif(NOT WIN32) - - target_link_libraries(${ABYSS_EXE} PUBLIC ${ABYSS_LIB} ${STATIC_LIB} Threads::Threads) - target_include_directories(${ABYSS_EXE} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include") - target_include_directories(${ABYSS_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${ABYSS}/include") - - # for freebsd - if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") - target_include_directories(${ABYSS_LIB} /usr/local/include) - endif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") -endif(USE_LIBABYSS) - enable_testing() if (NOT SHADOW) diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt index ce36e2651..6eadc39d4 100644 --- a/crypto/CMakeLists.txt +++ b/crypto/CMakeLists.txt @@ -112,4 +112,5 @@ set(CRYPTOGRAPHY_SRC ${NTRU_SRC}) add_library(${CRYPTOGRAPHY_LIB} STATIC ${CRYPTOGRAPHY_SRC}) +add_log_tag(${CRYPTOGRAPHY_LIB}) target_include_directories (${CRYPTOGRAPHY_LIB} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") diff --git a/docker/debian.Dockerfile b/docker/debian.Dockerfile index 7c93a0424..d66204fd1 100644 --- a/docker/debian.Dockerfile +++ b/docker/debian.Dockerfile @@ -1,10 +1,10 @@ FROM debian:stable RUN apt update && \ - apt install -y build-essential cmake git libcap-dev curl rapidjson-dev + apt install -y build-essential cmake git libcap-dev curl rapidjson-dev ninja-build WORKDIR /src/ COPY . /src/ -RUN make -j 8 JSONRPC=ON && make install +RUN make NINJA=ninja JSONRPC=ON && make install NINJA=ninja diff --git a/docker/ubuntu.Dockerfile b/docker/ubuntu.Dockerfile index 90e051d4f..031515e2b 100644 --- a/docker/ubuntu.Dockerfile +++ b/docker/ubuntu.Dockerfile @@ -1,10 +1,10 @@ FROM ubuntu:latest RUN apt update && \ - apt install -y build-essential cmake git libcap-dev curl rapidjson-dev + apt install -y build-essential cmake git libcap-dev curl rapidjson-dev ninja-build WORKDIR /src/ COPY . /src/ -RUN make -j 8 JSONRPC=ON +RUN make NINJA=ninja JSONRPC=ON diff --git a/docs/wire-protocol.txt b/docs/wire-protocol.txt index 6b3154e45..6070cad2e 100644 --- a/docs/wire-protocol.txt +++ b/docs/wire-protocol.txt @@ -11,96 +11,79 @@ datagram based network layer. outer message format: -{ - A: command, - B: <16 bytes flow id>, - C: , - X: -} +outer-header: -comamnds: +<1 byte command> +<1 byte reserved, R, set to '=' (0x3d)> +<32 bytes flow id, B> -A - get handshake cookie -obtain a handshake cookie +command 'O' - obtain flow id -B is randomized -X MUST contain the user agent string of the requester. +obtain a handshake cookie -the if the network id differs from the current network's id a reject message is -sent: + +<32 bytes random> +<8 bytes net id> + -{ - A: R, - B: msg.B, - X: "" -} +the if the network id differs from the current network's id a reject message +MUST be sent MUST be replied to with a message rejected or a give handshake cookie -C - give handshake cookie +command 'G' - give flow id -give a handshake cookie to a remote endpoint that asks for one + +<32 byte new flow-id, X> + -B is the B value from the get handshake cookie message -X is a 32 byte handshake cookie, calcuated via: - -r = RAND(32) -a = "" + " " + "" -X = HS(a + B + r) +give a flow id to a remote endpoint that asks for one -R - message rejected +B is the B value from the request flow id message +X is a 32 byte the flow id, calcuated via: -B is the flow id from the recipiant -X is a reply line +r = RAND(32) +a = "::ffff." + ":" + "" +X = HS(a + B + r + net id) -reject a message with flow id B +resulting message: -S - session negotiation + "G=<32 byte flowid><32 bytes new flowid>" -negotiate encrypted session +after recieving a give flow id message a session negotiation can happen -B is the flow id from the recipiant -C is the handshake cookie -X is encrypted session negotiation data +command 'R' - message rejected -D - encrypted data transmission + + -transmit encrypted data on session +reject a message on a flow id B is the flow id from the recipiant -X is authenticated and encrypted data - -BNF: - - ::= - - ::= - - ::= + - - ::= * | - - ::= "COOKIE" | "HANDSHAKE" - ::= +resulting message of reject with reason "no you" - ::= "lokinet" | "testnet" + "R=<32 byte flow-id>no you" - ::= " " +command 'S' - session negotiation - ::= "0" - - ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" - - ::= | +negotiate encrypted session - ::= | * + +<24 bytes nounce, N> + + - ::= "." "." +B is the flow id from the recipiant generated by the give flow id message (from outer header) +N is a random nounce +X is encrypted session negotiation data +Z is a keyed hash - ::= +Z is generated via: +msg.Z = '0x00' * 32 +msg.Z = MDS(msg, tx_K) session negotiation: @@ -119,15 +102,9 @@ a.tx_K = k_b b.tx_K = k_a -inner message format: - -<32 bytes blake2s keyed hash of following data> -<24 bytes nounce> - - decryption is done via: -SD(remaining, rx_K, nounce) +SD(msg.X, rx_K, msg.N) encrypted payload is bencoded LIM (see proto_v0.txt) @@ -147,20 +124,144 @@ b.rx_K = k_b a.tx_K = k_b b.tx_K = k_a -afterwards data transmission may happen +afterwards data transmission may happen. +the intiator's remote address is permitted to change during data transmission. +remote address of the last successfully -data tranmission: +D - encrypted data transmission -message format: +transmit encrypted data on session -<10 byte header> - + +<24 bytes nonce, N> + + -header format: -<1 byte proto version> +B is the flow id from the recipiant (from outer header) +N is a random nounce +X is encrypted data +Z is keyed hash of entire message + +Z is generated via: + +msg.Z = '0x00' * 32 +msg.Z = MDS(msg, tx_K) + +data tranmission: + +inner message format of X (after decryption): + +header: + +<1 byte protocol version> <1 byte command> -<1 byte flags> -<1 byte fragno> -<2 bytes fraglen> -<4 bytes seqno> + + +command: 'K' (keep alive) + +tell other side to acknoledge they are alive + +
+<2 bytes resevered, set to 0> +<2 bytes attempt counter, set to 0 and incremented every retransmit, reset when we get a keepalive ack> +<2 bytes milliseconds ping timeout> +<8 bytes current session TX limit in bytes per second> +<8 bytes current session RX use in bytes per second> +<8 bytes milliseconds since epoch our current time> + + +command: 'L' (keep alive ack) + +acknolege keep alive message + +
+<6 bytes reserved, set to 0> +<8 bytes current session RX limit in bytes per second> +<8 bytes current session TX use in bytes per second> +<8 bytes milliseconds since epoch our current time> + + + +command: 'C' (congestion) + +tell other side to slow down + +
+<2 bytes reduce TX rate by this many 1024 bytes per second> +<4 bytes milliseconds slowdown lifetime> + + +command: 'D' (anti-congestion) + +tell other side to speed up + +
+<2 bytes increase TX rate by this many 1024 bytes per second> +<4 bytes milliseconds speedup lifetime> + + +command: 'T' (transmit) + +transit fragment + +
+<1 byte number of 16 byte blocks offset from beginning of message, O> +<1 byte number of 16 byte blocks size of fragment data, N> +<4 bytes sequence number> +<32 bytes expected digest of message, present if O is 0, otherwise omitted> +<16 * N bytes of data> + + +command: 'A' (ack) + +acknoledge fragments + +
+<1 byte number of acks following, N> +<8 * N bytes acks> + + +ack format: + +<4 byte message sequence number> +<1 byte reserved current set to 0> +<1 byte ack counter (number of acks sent for the corrisponding message)> +<1 byte bitmask fragments selective ack (msb is fragment 0, lsb is fragment 7)> +<1 byte bitmask fragments posative ack (msb is fragment 0, lsb is fragment 7)> + + +command: 'R' (rotate keys) + +inform remote that their RX key should be rotated + +given alice(A) sends this message to bob(B) the new keys are computed as such: + +n_K = TKE(K, B_e, K_seed, N) + +A.tx_K = n_K +B.rx_K = n_K + +<2 bytes milliseconds lifetime of old keys, retain them for this long and then discard> +<4 bytes reserved, set to 0> +<32 bytes key exchange nounce, N> +<32 bytes next public encryption key, K> + + +command: 'U' (upgrade) + +request protocol upgrade + +
+<1 byte protocol min version to upgrade to> +<1 byte protocol max version to upgrade to> + + +command: 'V' (version upgrade) + +sent in response to upgrade message + +
+<1 byte protocol version selected> +<1 byte protocol version highest we support> + diff --git a/libabyss/include/abyss/client.hpp b/libabyss/include/abyss/client.hpp index 8a6bd3aea..302418fd2 100644 --- a/libabyss/include/abyss/client.hpp +++ b/libabyss/include/abyss/client.hpp @@ -1,8 +1,8 @@ #ifndef __ABYSS_CLIENT_HPP__ #define __ABYSS_CLIENT_HPP__ -#include #include +#include #include #include @@ -18,8 +18,8 @@ namespace abyss namespace http { using RPC_Method_t = std::string; - using RPC_Params = json::Value; - using RPC_Response = json::Document; + using RPC_Params = llarp::json::Value; + using RPC_Response = llarp::json::Document; using Headers_t = std::unordered_multimap< std::string, std::string >; struct ConnImpl; diff --git a/libabyss/include/abyss/http.hpp b/libabyss/include/abyss/http.hpp index 2298ba782..0ac3abfb5 100644 --- a/libabyss/include/abyss/http.hpp +++ b/libabyss/include/abyss/http.hpp @@ -1,6 +1,6 @@ #ifndef __ABYSS_HTTP_HPP__ #define __ABYSS_HTTP_HPP__ -#include +#include #include #include @@ -20,16 +20,18 @@ namespace abyss struct HeaderReader { + using string_view = llarp::string_view; + RequestHeader Header; virtual ~HeaderReader() { } bool - ProcessHeaderLine(abyss::string_view line, bool& done); + ProcessHeaderLine(string_view line, bool& done); virtual bool - ShouldProcessHeader(const abyss::string_view& line) const = 0; + ShouldProcessHeader(const string_view& line) const = 0; }; } // namespace http diff --git a/libabyss/include/abyss/json.hpp b/libabyss/include/abyss/json.hpp deleted file mode 100644 index 64e9d50df..000000000 --- a/libabyss/include/abyss/json.hpp +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __ABYSS_JSON_JSON_HPP -#define __ABYSS_JSON_JSON_HPP - -#include -#include -#include -#include -#include - -namespace abyss -{ - namespace json - { - using Document = rapidjson::Document; - using Value = rapidjson::Value; - using Writer = rapidjson::Writer< rapidjson::OStreamWrapper >; - } // namespace json - -#if __cplusplus >= 201703L - using string_view = std::string_view; -#else - using string_view = std::string; -#endif - namespace json - { - struct IParser - { - virtual ~IParser(){}; - - /// result from feeding data to parser - enum Result - { - /// we need more data to finish parsing - eNeedData, - /// we have parsed the object fully - eDone, - /// we have a parsing syntax error - eParseError - }; - - /// feed data to parser return true if successful - virtual bool - FeedData(const char *buf, size_t sz) = 0; - /// parse internal buffer - virtual Result - Parse(Document &obj) const = 0; - }; - - /// create new parser - IParser * - MakeParser(size_t contentSize); - - void - ToString(const json::Document &obj, std::ostream &out); - - } // namespace json -} // namespace abyss - -#endif diff --git a/libabyss/include/abyss/server.hpp b/libabyss/include/abyss/server.hpp index f6891bb2a..5a3046afa 100644 --- a/libabyss/include/abyss/server.hpp +++ b/libabyss/include/abyss/server.hpp @@ -1,8 +1,8 @@ #ifndef __ABYSS_SERVER_HPP__ #define __ABYSS_SERVER_HPP__ -#include #include +#include #include #include #include @@ -21,8 +21,8 @@ namespace abyss struct IRPCHandler { using Method_t = std::string; - using Params = json::Value; - using Response = json::Writer; + using Params = llarp::json::Value; + using Response = llarp::json::Writer; IRPCHandler(ConnImpl* impl); diff --git a/libabyss/main.cpp b/libabyss/main.cpp index 3ef0faad3..5d4d174d1 100644 --- a/libabyss/main.cpp +++ b/libabyss/main.cpp @@ -32,7 +32,7 @@ struct DemoCall : public abyss::http::IRPCClientHandler bool HandleResponse(abyss::http::RPC_Response resp) override { - abyss::json::ToString(resp, std::cout); + llarp::json::ToString(resp, std::cout); return true; } @@ -60,7 +60,7 @@ struct DemoClient : public abyss::http::JSONRPC void DoDemoRequest() { - abyss::json::Value params; + llarp::json::Value params; params.SetObject(); QueueRPC("test", std::move(params), std::bind(&DemoClient::NewConn, this, std::placeholders::_1)); diff --git a/libabyss/src/client.cpp b/libabyss/src/client.cpp index f291ee621..a8bb99d2d 100644 --- a/libabyss/src/client.cpp +++ b/libabyss/src/client.cpp @@ -8,6 +8,7 @@ namespace abyss { namespace http { + namespace json = llarp::json; struct ConnImpl : HeaderReader { // big @@ -16,7 +17,7 @@ namespace abyss json::Document m_RequestBody; Headers_t m_SendHeaders; IRPCClientHandler* handler; - std::unique_ptr< abyss::json::IParser > m_BodyParser; + std::unique_ptr< json::IParser > m_BodyParser; json::Document m_Response; enum State @@ -94,7 +95,7 @@ namespace abyss } bool - ShouldProcessHeader(const abyss::string_view& name) const + ShouldProcessHeader(const llarp::string_view& name) const { return name == "content-length" || name == "content-type"; } @@ -123,7 +124,7 @@ namespace abyss if(contentSize > MAX_BODY_SIZE) return false; - m_BodyParser.reset(abyss::json::MakeParser(contentSize)); + m_BodyParser.reset(json::MakeParser(contentSize)); } if(m_BodyParser && m_BodyParser->FeedData(buf, sz)) { diff --git a/libabyss/src/server.cpp b/libabyss/src/server.cpp index 0f133b6ad..75a7c757b 100644 --- a/libabyss/src/server.cpp +++ b/libabyss/src/server.cpp @@ -14,6 +14,7 @@ namespace abyss { namespace httpd { + namespace json = llarp::json; struct ConnImpl : abyss::http::HeaderReader { llarp_tcp_conn* _conn; @@ -22,11 +23,11 @@ namespace abyss llarp_time_t m_LastActive; llarp_time_t m_ReadTimeout; bool m_Bad; - std::unique_ptr< abyss::json::IParser > m_BodyParser; + std::unique_ptr< json::IParser > m_BodyParser; json::Document m_Request; std::stringstream m_ResponseBuffer; - rapidjson::OStreamWrapper m_ResponseStream; - abyss::json::Writer m_Response; + json::Stream m_ResponseStream; + json::Writer m_Response; enum HTTPState { @@ -167,7 +168,7 @@ namespace abyss } else { - m_BodyParser.reset(abyss::json::MakeParser(contentLength)); + m_BodyParser.reset(json::MakeParser(contentLength)); } } if(!m_BodyParser->FeedData(buf, sz)) diff --git a/libutp/CMakeLists.txt b/libutp/CMakeLists.txt index 5896a88cd..e089e94ec 100644 --- a/libutp/CMakeLists.txt +++ b/libutp/CMakeLists.txt @@ -12,4 +12,8 @@ if(WIN32) endif(WIN32) add_library(libutp STATIC ${UTP_SRC}) +if(WIN32) + target_link_libraries(libutp ws2_32) +endif(WIN32) target_include_directories(libutp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CORE_INCLUDE}) +add_log_tag(libutp) diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 93c45c600..c2122a09c 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -13,13 +13,14 @@ set(LIB_UTIL_SRC util/endian.cpp util/fs.cpp util/ini.cpp + util/json.cpp util/logger.cpp util/logic.cpp util/mem.cpp util/queue_manager.cpp util/queue.cpp - util/str.cpp util/status.cpp + util/str.cpp util/string_view.cpp util/thread_pool.cpp util/threading.cpp @@ -31,11 +32,10 @@ set(LIB_UTIL_SRC add_library(${UTIL_LIB} STATIC ${LIB_UTIL_SRC}) target_include_directories(${UTIL_LIB} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(${UTIL_LIB} PUBLIC cppbackport absl::variant) # cut back on fluff if (NOT WIN32) - target_link_libraries(${UTIL_LIB} PUBLIC absl::optional) + target_link_libraries(${UTIL_LIB} PUBLIC absl::optional absl::variant cppbackport) endif(NOT WIN32) if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") @@ -58,7 +58,7 @@ set(LIB_PLATFORM_SRC ) add_library(${PLATFORM_LIB} STATIC ${LIB_PLATFORM_SRC}) -target_link_libraries(${PLATFORM_LIB} PUBLIC ${CRYPTOGRAPHY_LIB} ${UTIL_LIB} libutp cppbackport Threads::Threads) +target_link_libraries(${PLATFORM_LIB} PUBLIC ${CRYPTOGRAPHY_LIB} ${UTIL_LIB} libutp Threads::Threads) if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") if(NON_PC_TARGET) @@ -72,11 +72,11 @@ endif() if(WIN32) # win32 inline code - target_sources(${PLATFORM_LIB} PUBLIC win32/win32_inet.c win32/win32_intrnl.c win32/win32_upoll.c) -endif() - -if(USE_LIBABYSS) - target_link_libraries(${PLATFORM_LIB} ${ABYSS_LIB}) + get_filename_component(win32_inet win32/win32_inet.c ABSOLUTE) + get_filename_component(win32_intrnl win32/win32_intrnl.c ABSOLUTE) + get_filename_component(win32_upoll win32/win32_upoll.c ABSOLUTE) + target_sources(${PLATFORM_LIB} PUBLIC ${win32_inet} ${win32_intrnl} ${win32_upoll}) + target_link_libraries(${PLATFORM_LIB} PUBLIC iphlpapi) endif() set(DNSLIB_SRC @@ -173,6 +173,7 @@ set(LIB_SRC path/transit_hop.cpp pow.cpp profiling.cpp + router/abstractrouter.cpp router/router.cpp router_contact.cpp router_id.cpp @@ -201,7 +202,12 @@ set(LIB_SRC ) add_library(${STATIC_LIB} STATIC ${LIB_SRC}) -target_link_libraries(${STATIC_LIB} PUBLIC ${PLATFORM_LIB} ${UTIL_LIB} ${CRYPTOGRAPHY_LIB} cppbackport libutp ${LIBS}) +target_link_libraries(${STATIC_LIB} PUBLIC ${PLATFORM_LIB} ${UTIL_LIB} ${CRYPTOGRAPHY_LIB} libutp ${LIBS}) + +if(USE_LIBABYSS) + add_definitions(-DUSE_ABYSS=1) + target_link_libraries(${STATIC_LIB} PUBLIC ${ABYSS_LIB}) +endif() if(TESTNET) target_sources(${STATIC_LIB} PUBLIC testnet.c) @@ -215,4 +221,9 @@ if(WITH_SHARED) target_link_libraries(${SHARED_LIB} ${CRYPTOGRAPHY_LIB} ${LIBS} ${UTIL_LIB} libutp ${PLATFORM_LIB} Threads::Threads) install(TARGETS ${SHARED_LIB} LIBRARY DESTINATION lib) endif() + add_log_tag(${SHARED_LIB}) endif() + +add_log_tag(${UTIL_LIB}) +add_log_tag(${PLATFORM_LIB}) +add_log_tag(${STATIC_LIB}) diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index 2a47847d3..b8d724ecf 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -130,8 +130,10 @@ namespace llarp LoadFromFile(const char *fname); }; - using ShortHash = AlignedBuffer< SHORTHASHSIZE >; - using Signature = AlignedBuffer< SIGSIZE >; + using ShortHash = AlignedBuffer< SHORTHASHSIZE >; + struct Signature final : public AlignedBuffer< SIGSIZE > + { + }; using TunnelNonce = AlignedBuffer< TUNNONCESIZE >; using SymmNonce = AlignedBuffer< NONCESIZE >; using SymmKey = AlignedBuffer< 32 >; diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 68a5db206..b33ce7431 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -15,7 +15,9 @@ #include #include #include -#include +#include +#include +#include #include @@ -237,7 +239,7 @@ namespace llarp } void - Context::Init(const Key_t &us, llarp::Router *r, + Context::Init(const Key_t &us, AbstractRouter *r, llarp_time_t exploreInterval) { router = r; diff --git a/llarp/dht/context.hpp b/llarp/dht/context.hpp index 2bd0381d9..11e17d3db 100644 --- a/llarp/dht/context.hpp +++ b/llarp/dht/context.hpp @@ -180,7 +180,7 @@ namespace llarp /// initialize dht context and explore every exploreInterval milliseconds void - Init(const Key_t& us, llarp::Router* router, + Init(const Key_t& us, AbstractRouter* router, llarp_time_t exploreInterval); /// get localally stored introset by service address @@ -262,8 +262,8 @@ namespace llarp struct llarp_dht_context { llarp::dht::Context impl; - llarp::Router* parent; - llarp_dht_context(llarp::Router* router); + llarp::AbstractRouter* parent; + llarp_dht_context(llarp::AbstractRouter* router); }; #endif diff --git a/llarp/dht/dht.cpp b/llarp/dht/dht.cpp index 780c0d5a1..a3b71fc54 100644 --- a/llarp/dht/dht.cpp +++ b/llarp/dht/dht.cpp @@ -2,13 +2,13 @@ #include #include -llarp_dht_context::llarp_dht_context(llarp::Router *router) +llarp_dht_context::llarp_dht_context(llarp::AbstractRouter *router) { parent = router; } struct llarp_dht_context * -llarp_dht_context_new(llarp::Router *router) +llarp_dht_context_new(llarp::AbstractRouter *router) { return new llarp_dht_context(router); } diff --git a/llarp/dht/dht.h b/llarp/dht/dht.h index cd3f163ea..24b84468c 100644 --- a/llarp/dht/dht.h +++ b/llarp/dht/dht.h @@ -15,12 +15,12 @@ struct llarp_dht_context; namespace llarp { - struct Router; + struct AbstractRouter; } /// allocator struct llarp_dht_context* -llarp_dht_context_new(llarp::Router* parent); +llarp_dht_context_new(llarp::AbstractRouter* parent); /// deallocator void diff --git a/llarp/dht/explorenetworkjob.cpp b/llarp/dht/explorenetworkjob.cpp index 5683d75d0..082dd9ccb 100644 --- a/llarp/dht/explorenetworkjob.cpp +++ b/llarp/dht/explorenetworkjob.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/dht/localrouterlookup.cpp b/llarp/dht/localrouterlookup.cpp index fe5bcd2dc..6f7c33fa7 100644 --- a/llarp/dht/localrouterlookup.cpp +++ b/llarp/dht/localrouterlookup.cpp @@ -3,7 +3,8 @@ #include #include #include -#include +#include +#include #include namespace llarp diff --git a/llarp/dht/localserviceaddresslookup.cpp b/llarp/dht/localserviceaddresslookup.cpp index def38fc67..187b5331a 100644 --- a/llarp/dht/localserviceaddresslookup.cpp +++ b/llarp/dht/localserviceaddresslookup.cpp @@ -2,7 +2,9 @@ #include #include -#include +#include +#include +#include #include namespace llarp diff --git a/llarp/dht/localtaglookup.cpp b/llarp/dht/localtaglookup.cpp index a55849590..a0cfb6d20 100644 --- a/llarp/dht/localtaglookup.cpp +++ b/llarp/dht/localtaglookup.cpp @@ -3,7 +3,8 @@ #include #include #include -#include +#include +#include namespace llarp { diff --git a/llarp/dht/message.cpp b/llarp/dht/message.cpp index dd822cd76..90711b797 100644 --- a/llarp/dht/message.cpp +++ b/llarp/dht/message.cpp @@ -33,7 +33,7 @@ namespace llarp // first key if(dec->firstKey) { - if(!llarp_buffer_eq(*key, "A")) + if(!(*key == "A")) return false; if(!bencode_read_string(r->buffer, &strbuf)) return false; diff --git a/llarp/dht/messages/findrouter.cpp b/llarp/dht/messages/findrouter.cpp index e7f490d25..e2db9c48e 100644 --- a/llarp/dht/messages/findrouter.cpp +++ b/llarp/dht/messages/findrouter.cpp @@ -3,7 +3,9 @@ #include #include #include -#include +#include +#include +#include namespace llarp { @@ -98,7 +100,7 @@ namespace llarp { llarp_buffer_t strbuf; - if(llarp_buffer_eq(key, "E")) + if(key == "E") { uint64_t result; if(!bencode_read_integer(val, &result)) @@ -108,7 +110,7 @@ namespace llarp return true; } - if(llarp_buffer_eq(key, "I")) + if(key == "I") { uint64_t result; if(!bencode_read_integer(val, &result)) @@ -117,7 +119,7 @@ namespace llarp iterative = result != 0; return true; } - if(llarp_buffer_eq(key, "K")) + if(key == "K") { if(!bencode_read_string(val, &strbuf)) return false; @@ -127,11 +129,11 @@ namespace llarp std::copy(strbuf.base, strbuf.base + K.SIZE, K.begin()); return true; } - if(llarp_buffer_eq(key, "T")) + if(key == "T") { return bencode_read_integer(val, &txid); } - if(llarp_buffer_eq(key, "V")) + if(key == "V") { return bencode_read_integer(val, &version); } diff --git a/llarp/dht/messages/gotintro.cpp b/llarp/dht/messages/gotintro.cpp index 98f405d22..45b5a60fc 100644 --- a/llarp/dht/messages/gotintro.cpp +++ b/llarp/dht/messages/gotintro.cpp @@ -2,7 +2,8 @@ #include #include -#include +#include +#include namespace llarp { @@ -82,11 +83,11 @@ namespace llarp bool GotIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf) { - if(llarp_buffer_eq(key, "I")) + if(key == "I") { return BEncodeReadList(I, buf); } - if(llarp_buffer_eq(key, "K")) + if(key == "K") { if(K) // duplicate key? return false; diff --git a/llarp/dht/messages/gotrouter.cpp b/llarp/dht/messages/gotrouter.cpp index 0c856516a..9887fb32c 100644 --- a/llarp/dht/messages/gotrouter.cpp +++ b/llarp/dht/messages/gotrouter.cpp @@ -1,7 +1,8 @@ #include #include -#include +#include +#include namespace llarp { @@ -51,22 +52,22 @@ namespace llarp bool GotRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val) { - if(llarp_buffer_eq(key, "K")) + if(key == "K") { if(K) // duplicate key? return false; K.reset(new dht::Key_t()); return K->BDecode(val); } - if(llarp_buffer_eq(key, "N")) + if(key == "N") { return BEncodeReadList(N, val); } - if(llarp_buffer_eq(key, "R")) + if(key == "R") { return BEncodeReadList(R, val); } - if(llarp_buffer_eq(key, "T")) + if(key == "T") { return bencode_read_integer(val, &txid); } @@ -107,7 +108,7 @@ namespace llarp if(!dht.pendingRouterLookups.HasPendingLookupFrom(owner)) { - llarp::LogWarn("Unwarrented GRM from ", From, " txid=", txid); + llarp::LogWarn("Unwarranted GRM from ", From, " txid=", txid); return false; } // no pending lookup diff --git a/llarp/dht/messages/pubintro.cpp b/llarp/dht/messages/pubintro.cpp index e3fd876c8..d9c0ea153 100644 --- a/llarp/dht/messages/pubintro.cpp +++ b/llarp/dht/messages/pubintro.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include namespace llarp { @@ -19,7 +19,7 @@ namespace llarp llarp_buffer_t *val) { bool read = false; - if(llarp_buffer_eq(key, "E")) + if(key == "E") { return BEncodeReadList(E, val); } @@ -27,7 +27,7 @@ namespace llarp return false; if(!BEncodeMaybeReadDictInt("R", R, read, key, val)) return false; - if(llarp_buffer_eq(key, "S")) + if(key == "S") { read = true; hasS = true; diff --git a/llarp/dns.cpp b/llarp/dns.cpp index 7564c2205..e3e4c36ab 100644 --- a/llarp/dns.cpp +++ b/llarp/dns.cpp @@ -313,17 +313,17 @@ extern "C" uint16_t fields; // reads as HOST byte order - if(!llarp_buffer_read_uint16(buffer, &hdr->id)) + if(!buffer->read_uint16(hdr->id)) return false; - if(!llarp_buffer_read_uint16(buffer, &fields)) + if(!buffer->read_uint16(fields)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->qdCount)) + if(!buffer->read_uint16(hdr->qdCount)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->anCount)) + if(!buffer->read_uint16(hdr->anCount)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->nsCount)) + if(!buffer->read_uint16(hdr->nsCount)) return false; - if(!llarp_buffer_read_uint16(buffer, &hdr->arCount)) + if(!buffer->read_uint16(hdr->arCount)) return false; // decode fields into hdr diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index 8536df98e..1cfcd7836 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -14,33 +14,33 @@ namespace llarp bool MessageHeader::Encode(llarp_buffer_t* buf) const { - if(!llarp_buffer_put_uint16(buf, id)) + if(!buf->put_uint16(id)) return false; - if(!llarp_buffer_put_uint16(buf, fields)) + if(!buf->put_uint16(fields)) return false; - if(!llarp_buffer_put_uint16(buf, qd_count)) + if(!buf->put_uint16(qd_count)) return false; - if(!llarp_buffer_put_uint16(buf, an_count)) + if(!buf->put_uint16(an_count)) return false; - if(!llarp_buffer_put_uint16(buf, ns_count)) + if(!buf->put_uint16(ns_count)) return false; - return llarp_buffer_put_uint16(buf, ar_count); + return buf->put_uint16(ar_count); } bool MessageHeader::Decode(llarp_buffer_t* buf) { - if(!llarp_buffer_read_uint16(buf, &id)) + if(!buf->read_uint16(id)) return false; - if(!llarp_buffer_read_uint16(buf, &fields)) + if(!buf->read_uint16(fields)) return false; - if(!llarp_buffer_read_uint16(buf, &qd_count)) + if(!buf->read_uint16(qd_count)) return false; - if(!llarp_buffer_read_uint16(buf, &an_count)) + if(!buf->read_uint16(an_count)) return false; - if(!llarp_buffer_read_uint16(buf, &ns_count)) + if(!buf->read_uint16(ns_count)) return false; - return llarp_buffer_read_uint16(buf, &ar_count); + return buf->read_uint16(ar_count); } Message::Message(Message&& other) @@ -232,7 +232,7 @@ namespace llarp rec.ttl = ttl; std::array< byte_t, 512 > tmp = {{0}}; llarp_buffer_t buf(tmp); - llarp_buffer_put_uint16(&buf, priority); + buf.put_uint16(priority); if(EncodeName(&buf, name)) { buf.sz = buf.cur - buf.base; diff --git a/llarp/dns/name.cpp b/llarp/dns/name.cpp index 46f466b1f..b0936d7bc 100644 --- a/llarp/dns/name.cpp +++ b/llarp/dns/name.cpp @@ -11,7 +11,7 @@ namespace llarp bool DecodeName(llarp_buffer_t* buf, Name_t& name) { - if(llarp_buffer_size_left(*buf) < 1) + if(buf->size_left() < 1) return false; std::stringstream ss; size_t l; @@ -27,7 +27,7 @@ namespace llarp llarp::DumpBuffer(*buf); return false; } - if(llarp_buffer_size_left(*buf) < l) + if(buf->size_left() < l) return false; ss << Name_t((const char*)buf->cur, l); @@ -56,7 +56,7 @@ namespace llarp return false; *(buf->cur) = l; buf->cur++; - if(llarp_buffer_size_left(*buf) < l) + if(buf->size_left() < l) return false; if(l) { diff --git a/llarp/dns/question.cpp b/llarp/dns/question.cpp index 27c65ca46..b1ea92f62 100644 --- a/llarp/dns/question.cpp +++ b/llarp/dns/question.cpp @@ -22,9 +22,9 @@ namespace llarp { if(!EncodeName(buf, qname)) return false; - if(!llarp_buffer_put_uint16(buf, qtype)) + if(!buf->put_uint16(qtype)) return false; - return llarp_buffer_put_uint16(buf, qclass); + return buf->put_uint16(qclass); } bool @@ -35,12 +35,12 @@ namespace llarp llarp::LogError("failed to decode name"); return false; } - if(!llarp_buffer_read_uint16(buf, &qtype)) + if(!buf->read_uint16(qtype)) { llarp::LogError("failed to decode type"); return false; } - if(!llarp_buffer_read_uint16(buf, &qclass)) + if(!buf->read_uint16(qclass)) { llarp::LogError("failed to decode class"); return false; diff --git a/llarp/dns/rr.cpp b/llarp/dns/rr.cpp index 5d978c3b2..4beae892d 100644 --- a/llarp/dns/rr.cpp +++ b/llarp/dns/rr.cpp @@ -31,15 +31,15 @@ namespace llarp { return false; } - if(!llarp_buffer_put_uint16(buf, rr_type)) + if(!buf->put_uint16(rr_type)) { return false; } - if(!llarp_buffer_put_uint16(buf, rr_class)) + if(!buf->put_uint16(rr_class)) { return false; } - if(!llarp_buffer_put_uint32(buf, ttl)) + if(!buf->put_uint32(ttl)) { return false; } @@ -58,17 +58,17 @@ namespace llarp llarp::LogError("failed to decode rr name"); return false; } - if(!llarp_buffer_read_uint16(buf, &rr_type)) + if(!buf->read_uint16(rr_type)) { llarp::LogError("failed to decode rr type"); return false; } - if(!llarp_buffer_read_uint16(buf, &rr_class)) + if(!buf->read_uint16(rr_class)) { llarp::LogError("failed to decode rr class"); return false; } - if(!llarp_buffer_read_uint32(buf, &ttl)) + if(!buf->read_uint32(ttl)) { llarp::LogError("failed to decode ttl"); return false; diff --git a/llarp/dns/serialize.cpp b/llarp/dns/serialize.cpp index 8e69cd807..7f13b394c 100644 --- a/llarp/dns/serialize.cpp +++ b/llarp/dns/serialize.cpp @@ -11,9 +11,9 @@ namespace llarp if(v.size() > 65536) return false; uint16_t len = v.size(); - if(!llarp_buffer_put_uint16(buf, len)) + if(!buf->put_uint16(len)) return false; - if(llarp_buffer_size_left(*buf) < len) + if(buf->size_left() < len) return false; memcpy(buf->cur, v.data(), len); buf->cur += len; @@ -24,9 +24,9 @@ namespace llarp DecodeRData(llarp_buffer_t* buf, std::vector< byte_t >& v) { uint16_t len; - if(!llarp_buffer_read_uint16(buf, &len)) + if(!buf->read_uint16(len)) return false; - size_t left = llarp_buffer_size_left(*buf); + size_t left = buf->size_left(); if(left < len) return false; v.resize(size_t(len)); diff --git a/llarp/ev/ev.cpp b/llarp/ev/ev.cpp index 34aa4d026..9f9bf7e8a 100644 --- a/llarp/ev/ev.cpp +++ b/llarp/ev/ev.cpp @@ -44,8 +44,7 @@ llarp_make_ev_loop() || (__APPLE__ && __MACH__) std::unique_ptr< llarp_ev_loop > r = std::make_unique< llarp_kqueue_loop >(); #elif defined(_WIN32) || defined(_WIN64) || defined(__NT__) - std::unique_ptr< llarp_win32_loop > r = - std::make_unique< llarp_kqueue_loop >(); + std::unique_ptr< llarp_ev_loop > r = std::make_unique< llarp_win32_loop >(); #else // TODO: fall back to a generic select-based event loop #error no event loop subclass diff --git a/llarp/exit/close_exit.cpp b/llarp/exit/close_exit.cpp index 71dd03268..3af088a24 100644 --- a/llarp/exit/close_exit.cpp +++ b/llarp/exit/close_exit.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace llarp { @@ -76,7 +77,7 @@ namespace llarp } bool - CloseExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + CloseExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { return h->HandleCloseExitMessage(this, r); } diff --git a/llarp/exit/context.cpp b/llarp/exit/context.cpp index 3e1e7b38c..8f798aa40 100644 --- a/llarp/exit/context.cpp +++ b/llarp/exit/context.cpp @@ -4,7 +4,7 @@ namespace llarp { namespace exit { - Context::Context(llarp::Router* r) : m_Router(r) + Context::Context(AbstractRouter* r) : m_Router(r) { } Context::~Context() diff --git a/llarp/exit/context.hpp b/llarp/exit/context.hpp index a199e30d2..7cf036193 100644 --- a/llarp/exit/context.hpp +++ b/llarp/exit/context.hpp @@ -15,7 +15,7 @@ namespace llarp { using Config_t = std::unordered_multimap< std::string, std::string >; - Context(llarp::Router *r); + Context(AbstractRouter *r); ~Context(); void @@ -50,7 +50,7 @@ namespace llarp CalculateExitTraffic(TrafficStats &stats); private: - llarp::Router *m_Router; + AbstractRouter *m_Router; std::unordered_map< std::string, std::unique_ptr< llarp::handlers::ExitEndpoint > > m_Exits; diff --git a/llarp/exit/endpoint.cpp b/llarp/exit/endpoint.cpp index dd630c847..69932ae89 100644 --- a/llarp/exit/endpoint.cpp +++ b/llarp/exit/endpoint.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace llarp { @@ -194,7 +194,8 @@ namespace llarp Endpoint::GetCurrentPath() const { auto router = m_Parent->GetRouter(); - return router->paths.GetByUpstream(router->pubkey(), m_CurrentPath); + return router->pathContext().GetByUpstream(router->pubkey(), + m_CurrentPath); } } // namespace exit } // namespace llarp diff --git a/llarp/exit/endpoint.hpp b/llarp/exit/endpoint.hpp index 02ce4826d..c9e7facb1 100644 --- a/llarp/exit/endpoint.hpp +++ b/llarp/exit/endpoint.hpp @@ -6,6 +6,8 @@ #include #include +#include + namespace llarp { namespace handlers diff --git a/llarp/exit/grant_exit.cpp b/llarp/exit/grant_exit.cpp index ad98beafa..07df83e03 100644 --- a/llarp/exit/grant_exit.cpp +++ b/llarp/exit/grant_exit.cpp @@ -1,4 +1,6 @@ #include + +#include #include namespace llarp @@ -81,7 +83,7 @@ namespace llarp } bool - GrantExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + GrantExitMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { return h->HandleGrantExitMessage(this, r); } diff --git a/llarp/exit/obtain_exit.cpp b/llarp/exit/obtain_exit.cpp index 59e310bd3..bd720dbe1 100644 --- a/llarp/exit/obtain_exit.cpp +++ b/llarp/exit/obtain_exit.cpp @@ -1,5 +1,6 @@ #include +#include #include namespace llarp @@ -11,7 +12,7 @@ namespace llarp { std::array< byte_t, 1024 > tmp; llarp_buffer_t buf(tmp); - I = llarp::seckey_topublic(sk); + I = seckey_topublic(sk); Z.Zero(); if(!BEncode(&buf)) { @@ -92,7 +93,8 @@ namespace llarp } bool - ObtainExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + ObtainExitMessage::HandleMessage(IMessageHandler* h, + AbstractRouter* r) const { return h->HandleObtainExitMessage(this, r); } diff --git a/llarp/exit/reject_exit.cpp b/llarp/exit/reject_exit.cpp index 3c17ac074..4d3038ba1 100644 --- a/llarp/exit/reject_exit.cpp +++ b/llarp/exit/reject_exit.cpp @@ -1,4 +1,6 @@ #include + +#include #include namespace llarp @@ -91,7 +93,8 @@ namespace llarp } bool - RejectExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + RejectExitMessage::HandleMessage(IMessageHandler* h, + AbstractRouter* r) const { return h->HandleRejectExitMessage(this, r); } diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 130439100..17d7d2fc1 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -1,7 +1,8 @@ #include +#include #include -#include +#include namespace llarp { @@ -9,8 +10,8 @@ namespace llarp { BaseSession::BaseSession( const llarp::RouterID& router, - std::function< bool(const llarp_buffer_t&) > writepkt, llarp::Router* r, - size_t numpaths, size_t hoplen) + std::function< bool(const llarp_buffer_t&) > writepkt, + AbstractRouter* r, size_t numpaths, size_t hoplen) : llarp::path::Builder(r, r->dht(), numpaths, hoplen) , m_ExitRouter(router) , m_WritePacket(writepkt) @@ -232,13 +233,14 @@ namespace llarp SNodeSession::SNodeSession( const llarp::RouterID& snodeRouter, - std::function< bool(const llarp_buffer_t&) > writepkt, llarp::Router* r, - size_t numpaths, size_t hoplen, bool useRouterSNodeKey) + std::function< bool(const llarp_buffer_t&) > writepkt, + AbstractRouter* r, size_t numpaths, size_t hoplen, + bool useRouterSNodeKey) : BaseSession(snodeRouter, writepkt, r, numpaths, hoplen) { if(useRouterSNodeKey) { - m_ExitIdentity = r->identity; + m_ExitIdentity = r->identity(); } } } // namespace exit diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index b1212cb57..b669e67ee 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -21,7 +21,7 @@ namespace llarp BaseSession(const llarp::RouterID& exitRouter, std::function< bool(const llarp_buffer_t&) > writepkt, - llarp::Router* r, size_t numpaths, size_t hoplen); + AbstractRouter* r, size_t numpaths, size_t hoplen); virtual ~BaseSession(); @@ -113,7 +113,7 @@ namespace llarp { ExitSession(const llarp::RouterID& snodeRouter, std::function< bool(const llarp_buffer_t&) > writepkt, - llarp::Router* r, size_t numpaths, size_t hoplen) + AbstractRouter* r, size_t numpaths, size_t hoplen) : BaseSession(snodeRouter, writepkt, r, numpaths, hoplen){}; ~ExitSession(){}; @@ -132,7 +132,7 @@ namespace llarp { SNodeSession(const llarp::RouterID& snodeRouter, std::function< bool(const llarp_buffer_t&) > writepkt, - llarp::Router* r, size_t numpaths, size_t hoplen, + AbstractRouter* r, size_t numpaths, size_t hoplen, bool useRouterSNodeKey = false); ~SNodeSession(){}; diff --git a/llarp/exit/transfer_traffic.cpp b/llarp/exit/transfer_traffic.cpp index ab67de1a3..2058781a1 100644 --- a/llarp/exit/transfer_traffic.cpp +++ b/llarp/exit/transfer_traffic.cpp @@ -64,7 +64,7 @@ namespace llarp bool TransferTrafficMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h->HandleTransferTrafficMessage(this, r); } diff --git a/llarp/exit/update_exit.cpp b/llarp/exit/update_exit.cpp index 2918b0b79..9b08db6a1 100644 --- a/llarp/exit/update_exit.cpp +++ b/llarp/exit/update_exit.cpp @@ -1,4 +1,6 @@ #include + +#include #include namespace llarp @@ -82,7 +84,8 @@ namespace llarp } bool - UpdateExitMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + UpdateExitMessage::HandleMessage(IMessageHandler* h, + AbstractRouter* r) const { return h->HandleUpdateExitMessage(this, r); } @@ -119,7 +122,7 @@ namespace llarp bool UpdateExitVerifyMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h->HandleUpdateExitVerifyMessage(this, r); } diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index e1a077586..4d6fde60d 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -23,13 +23,13 @@ namespace llarp static_cast< ExitEndpoint * >(tun->user)->Flush(); } - ExitEndpoint::ExitEndpoint(const std::string &name, Router *r) + ExitEndpoint::ExitEndpoint(const std::string &name, AbstractRouter *r) : m_Router(r) - , m_Resolver(r->netloop, this) + , m_Resolver(r->netloop(), this) , m_Name(name) , m_Tun{{0}, 0, {0}, 0, 0, 0, 0, 0, 0, 0} , m_LocalResolverAddr("127.0.0.1", 53) - , m_InetToNetwork(name + "_exit_rx", r->netloop, r->netloop) + , m_InetToNetwork(name + "_exit_rx", r->netloop(), r->netloop()) { m_Tun.user = this; @@ -237,7 +237,7 @@ namespace llarp { if(m_ShouldInitTun) { - if(!llarp_ev_add_tun(GetRouter()->netloop, &m_Tun)) + if(!llarp_ev_add_tun(GetRouter()->netloop(), &m_Tun)) { llarp::LogWarn("Could not create tunnel for exit endpoint"); return false; @@ -251,7 +251,7 @@ namespace llarp return true; } - Router * + AbstractRouter * ExitEndpoint::GetRouter() { return m_Router; @@ -542,7 +542,8 @@ namespace llarp if(wantInternet && !m_PermitExit) return false; huint32_t ip = GetIPForIdent(pk); - if(GetRouter()->paths.TransitHopPreviousIsRouter(path, pk.as_array())) + if(GetRouter()->pathContext().TransitHopPreviousIsRouter(path, + pk.as_array())) { // we think this path belongs to a service node // mark it as such so we don't make an outbound session to them diff --git a/llarp/handlers/exit.hpp b/llarp/handlers/exit.hpp index e5d4697cc..88780eef4 100644 --- a/llarp/handlers/exit.hpp +++ b/llarp/handlers/exit.hpp @@ -8,12 +8,12 @@ namespace llarp { - struct Router; + struct AbstractRouter; namespace handlers { struct ExitEndpoint : public dns::IQueryHandler, public util::IStateful { - ExitEndpoint(const std::string& name, Router* r); + ExitEndpoint(const std::string& name, AbstractRouter* r); ~ExitEndpoint(); void @@ -52,7 +52,7 @@ namespace llarp void OnInetPacket(const llarp_buffer_t& buf); - Router* + AbstractRouter* GetRouter(); llarp_time_t @@ -125,7 +125,7 @@ namespace llarp void KickIdentOffExit(const PubKey& pk); - Router* m_Router; + AbstractRouter* m_Router; dns::Proxy m_Resolver; bool m_ShouldInitTun; std::string m_Name; diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index f52a7ed42..009847614 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -9,7 +9,7 @@ namespace llarp { struct NullEndpoint final : public llarp::service::Endpoint { - NullEndpoint(const std::string &name, llarp::Router *r, + NullEndpoint(const std::string &name, AbstractRouter *r, llarp::service::Context *parent) : llarp::service::Endpoint(name, r, parent){}; diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 9c10c03e5..fedbc7c92 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -10,7 +10,8 @@ #include #include -#include +#include +#include namespace llarp { @@ -29,12 +30,14 @@ namespace llarp self->Flush(); } - TunEndpoint::TunEndpoint(const std::string &nickname, llarp::Router *r, + TunEndpoint::TunEndpoint(const std::string &nickname, AbstractRouter *r, service::Context *parent) : service::Endpoint(nickname, r, parent) - , m_UserToNetworkPktQueue(nickname + "_sendq", r->netloop, r->netloop) - , m_NetworkToUserPktQueue(nickname + "_recvq", r->netloop, r->netloop) - , m_Resolver(r->netloop, this) + , m_UserToNetworkPktQueue(nickname + "_sendq", r->netloop(), + r->netloop()) + , m_NetworkToUserPktQueue(nickname + "_recvq", r->netloop(), + r->netloop()) + , m_Resolver(r->netloop(), this) { #ifdef ANDROID tunif.get_fd_promise = &get_tun_fd_promise; diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index b930cb0cf..573ec2360 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -20,7 +20,7 @@ namespace llarp struct TunEndpoint : public service::Endpoint, public dns::IQueryHandler { - TunEndpoint(const std::string& nickname, llarp::Router* r, + TunEndpoint(const std::string& nickname, AbstractRouter* r, llarp::service::Context* parent); ~TunEndpoint(); diff --git a/llarp/link/iwp.cpp b/llarp/link/iwp.cpp index 98c2fd6e9..91e7dccab 100644 --- a/llarp/link/iwp.cpp +++ b/llarp/link/iwp.cpp @@ -1,16 +1,17 @@ +#include #include -#include namespace llarp { namespace iwp { std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router*) + NewServerFromRouter(AbstractRouter*) { // TODO: implement me return nullptr; } + std::unique_ptr< ILinkLayer > NewServer(llarp::Crypto*, const SecretKey&, llarp::GetRCFunc, llarp::LinkMessageHandler, llarp::SessionEstablishedHandler, diff --git a/llarp/link/iwp.hpp b/llarp/link/iwp.hpp index a7572f453..c64494126 100644 --- a/llarp/link/iwp.hpp +++ b/llarp/link/iwp.hpp @@ -1,11 +1,14 @@ #ifndef LLARP_LINK_IWP_HPP #define LLARP_LINK_IWP_HPP -#include #include +#include + namespace llarp { + struct AbstractRouter; + namespace iwp { std::unique_ptr< ILinkLayer > @@ -17,7 +20,7 @@ namespace llarp llarp::SessionClosedHandler closed); std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router* r); + NewServerFromRouter(AbstractRouter* r); } // namespace iwp } // namespace llarp diff --git a/llarp/link/iwp_internal.hpp b/llarp/link/iwp_internal.hpp index 96dfb7a68..3c1977d49 100644 --- a/llarp/link/iwp_internal.hpp +++ b/llarp/link/iwp_internal.hpp @@ -28,6 +28,13 @@ namespace llarp const AddressInfo &ai); ~Session(); + util::StatusObject + ExtractStatus() const override + { + // TODO: fill me in. + return {}; + } + void PumpIO(); @@ -124,7 +131,7 @@ namespace llarp bool Decode(llarp_buffer_t *buf) { - if(llarp_buffer_size_left(*buf) < fragoverhead) + if(buf->size_left() < fragoverhead) return false; version = *buf->cur; if(version != LLARP_PROTO_VERSION) @@ -136,8 +143,8 @@ namespace llarp buf->cur++; fragno = *buf->cur; buf->cur++; - llarp_buffer_read_uint16(buf, &fraglen); - llarp_buffer_read_uint32(buf, &seqno); + buf->read_uint16(fraglen); + buf->read_uint32(seqno); return fraglen <= fragsize; } @@ -147,7 +154,7 @@ namespace llarp if(body.sz > fragsize) return false; fraglen = body.sz; - if(llarp_buffer_size_left(*buf) < (fragoverhead + fraglen)) + if(buf->size_left() < (fragoverhead + fraglen)) return false; *buf->cur = LLARP_PROTO_VERSION; buf->cur++; @@ -157,8 +164,8 @@ namespace llarp buf->cur++; *buf->cur = fragno; buf->cur++; - llarp_buffer_put_uint16(buf, fraglen); - llarp_buffer_put_uint32(buf, seqno); + buf->put_uint16(fraglen); + buf->put_uint32(seqno); if(fraglen) memcpy(buf->cur, body.base, fraglen); buf->cur += fraglen; diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index 853e38bab..c9ebfbef5 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -170,6 +170,29 @@ namespace llarp m_Pending.erase(remote); } + util::StatusObject + ILinkLayer::ExtractStatus() const + { + std::vector pending, established; + + std::transform(m_Pending.begin(), m_Pending.end(), std::back_inserter(pending), [](const auto & item) -> util::StatusObject { + return item.second->ExtractStatus(); + }); + std::transform(m_AuthedLinks.begin(), m_AuthedLinks.end(), std::back_inserter(established), [](const auto & item) -> util::StatusObject { + return item.second->ExtractStatus(); + }); + + return { + {"name", Name()}, + {"rank", uint64_t(Rank())}, + {"addr", m_ourAddr.ToString()}, + {"sessions", util::StatusObject{ + {"pending", pending}, + {"established", established} + }} + }; + } + bool ILinkLayer::TryEstablishTo(RouterContact rc) { @@ -196,6 +219,18 @@ namespace llarp return true; } + void + ILinkLayer::Tick(llarp_time_t now) + { + Lock l(m_AuthedLinksMutex); + auto itr = m_AuthedLinks.begin(); + while(itr != m_AuthedLinks.end()) + { + itr->second->Tick(now); + ++itr; + } + } + void ILinkLayer::Stop() { diff --git a/llarp/link/server.hpp b/llarp/link/server.hpp index d657ffcf7..7349d571f 100644 --- a/llarp/link/server.hpp +++ b/llarp/link/server.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -41,7 +42,7 @@ namespace llarp /// handles close of all sessions with pubkey using SessionClosedHandler = std::function< void(llarp::RouterID) >; - struct ILinkLayer + struct ILinkLayer : public util::IStateful { ILinkLayer(const SecretKey& routerEncSecret, GetRCFunc getrc, LinkMessageHandler handler, SignBufferFunc signFunc, @@ -123,6 +124,9 @@ namespace llarp virtual const char* Name() const = 0; + util::StatusObject + ExtractStatus() const override; + void CloseSessionTo(const RouterID& remote); @@ -176,9 +180,7 @@ namespace llarp bool MapAddr(const RouterID& pk, ILinkSession* s); - virtual void Tick(llarp_time_t) - { - } + void Tick(llarp_time_t now); LinkMessageHandler HandleMessage; TimeoutHandler HandleTimeout; diff --git a/llarp/link/session.hpp b/llarp/link/session.hpp index 23186482f..685abaf09 100644 --- a/llarp/link/session.hpp +++ b/llarp/link/session.hpp @@ -13,7 +13,7 @@ namespace llarp struct LinkIntroMessage; struct ILinkMessage; struct ILinkLayer; - struct ILinkSession + struct ILinkSession : public util::IStateful { virtual ~ILinkSession(){}; diff --git a/llarp/link/utp.cpp b/llarp/link/utp.cpp index 052d7eb17..3da91cf5b 100644 --- a/llarp/link/utp.cpp +++ b/llarp/link/utp.cpp @@ -1,9 +1,10 @@ #include +#include #include #include #include -#include +#include #include #include @@ -39,7 +40,7 @@ namespace llarp bool InboundMessage::AppendData(const byte_t* ptr, uint16_t sz) { - if(llarp_buffer_size_left(buffer) < sz) + if(buffer.size_left() < sz) return false; memcpy(buffer.cur, ptr, sz); buffer.cur += sz; @@ -51,17 +52,17 @@ namespace llarp { parent = p; EnterState(eLinkEstablished); - llarp::LogDebug("link established with ", remoteAddr); + LogDebug("link established with ", remoteAddr); } - llarp::Crypto* - Session::Crypto() + Crypto* + Session::OurCrypto() { - return parent->Crypto(); + return parent->OurCrypto(); } - llarp::Crypto* - LinkLayer::Crypto() + Crypto* + LinkLayer::OurCrypto() { return _crypto; } @@ -82,8 +83,8 @@ namespace llarp if(expect) { ssize_t s = utp_writev(sock, vecs.data(), vecs.size()); - - while(s > static_cast< ssize_t >(vecq.front().iov_len)) + m_TXRate += s; + while(s > 0 && s >= static_cast< ssize_t >(vecq.front().iov_len)) { s -= vecq.front().iov_len; vecq.pop_front(); @@ -140,19 +141,19 @@ namespace llarp std::copy(K.begin(), K.end(), tmp.begin()); std::copy(n.begin(), n.end(), tmp.begin() + K.size()); // t_h = HS(K + L.n) - if(!Crypto()->shorthash(t_h, llarp_buffer_t(tmp))) + if(!OurCrypto()->shorthash(t_h, llarp_buffer_t(tmp))) { - llarp::LogError("failed to mix key to ", remoteAddr); + LogError("failed to mix key to ", remoteAddr); return false; } // K = TKE(a.p, B_a.e, sk, t_h) if(!dh(K, other, secret, t_h)) { - llarp::LogError("key exchange with ", other, " failed"); + LogError("key exchange with ", other, " failed"); return false; } - llarp::LogDebug("keys mixed with session to ", remoteAddr); + LogDebug("keys mixed with session to ", remoteAddr); return true; } @@ -165,13 +166,15 @@ namespace llarp buf.cur += K.size(); std::copy(A.begin(), A.end(), buf.cur); buf.cur = buf.base; - return Crypto()->shorthash(K, buf); + return OurCrypto()->shorthash(K, buf); } void Session::TickImpl(llarp_time_t now) { PruneInboundMessages(now); + m_TXRate = 0; + m_RXRate = 0; } /// low level read @@ -180,6 +183,7 @@ namespace llarp { // mark we are alive Alive(); + m_RXRate += sz; size_t s = sz; // process leftovers if(recvBufOffset) @@ -188,8 +192,8 @@ namespace llarp if(s >= left) { // yes it fills it - llarp::LogDebug("process leftovers, offset=", recvBufOffset, - " sz=", s, " left=", left); + LogDebug("process leftovers, offset=", recvBufOffset, " sz=", s, + " left=", left); std::copy(buf, buf + left, recvBuf.begin() + recvBufOffset); s -= left; recvBufOffset = 0; @@ -202,7 +206,7 @@ namespace llarp while(s >= FragmentBufferSize) { recvBufOffset = 0; - llarp::LogDebug("process full sz=", s); + LogDebug("process full sz=", s); if(!VerifyThenDecrypt(buf)) return false; buf += FragmentBufferSize; @@ -211,7 +215,7 @@ namespace llarp if(s) { // hold onto leftovers - llarp::LogDebug("leftovers sz=", s); + LogDebug("leftovers sz=", s); std::copy(buf, buf + s, recvBuf.begin() + recvBufOffset); recvBufOffset += s; } @@ -228,7 +232,7 @@ namespace llarp auto dlt = now - lastActive; if(dlt >= sessionTimeout) { - llarp::LogDebug("session timeout reached for ", remoteAddr); + LogDebug("session timeout reached for ", remoteAddr); return true; } return false; @@ -253,8 +257,7 @@ namespace llarp static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); if(l == nullptr) return 0; - llarp::LogDebug("utp_sendto ", Addr(*arg->address), " ", arg->len, - " bytes"); + LogDebug("utp_sendto ", Addr(*arg->address), " ", arg->len, " bytes"); // For whatever reason, the UTP_UDP_DONTFRAG flag is set // on the socket itself....which isn't correct and causes // winsock (at minimum) to reeee @@ -316,9 +319,9 @@ namespace llarp int err = WSAGetLastError(); FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, buf, 1024, nullptr); - llarp::LogError("sendto failed: ", buf); + LogError("sendto failed: ", buf); #else - llarp::LogError("sendto failed: ", strerror(errno)); + LogError("sendto failed: ", strerror(errno)); #endif } return 0; @@ -337,7 +340,9 @@ namespace llarp link->HandleTimeout(session); llarp::LogError(utp_error_code_names[arg->error_code], " via ", session->remoteAddr); - if(arg->error_code != UTP_ETIMEDOUT) + if(arg->error_code == UTP_ETIMEDOUT) + utp_close(arg->socket); + else session->Close(); link->RemovePending(session); } @@ -347,18 +352,16 @@ namespace llarp uint64 LinkLayer::OnLog(utp_callback_arguments* arg) { - llarp::LogDebug(arg->buf); + LogDebug(arg->buf); return 0; } - LinkLayer::LinkLayer(llarp::Crypto* crypto, - const SecretKey& routerEncSecret, - llarp::GetRCFunc getrc, llarp::LinkMessageHandler h, - llarp::SignBufferFunc sign, - llarp::SessionEstablishedHandler established, - llarp::SessionRenegotiateHandler reneg, - llarp::TimeoutHandler timeout, - llarp::SessionClosedHandler closed) + LinkLayer::LinkLayer(Crypto* crypto, const SecretKey& routerEncSecret, + GetRCFunc getrc, LinkMessageHandler h, + SignBufferFunc sign, + SessionEstablishedHandler established, + SessionRenegotiateHandler reneg, + TimeoutHandler timeout, SessionClosedHandler closed) : ILinkLayer(routerEncSecret, getrc, h, sign, established, reneg, timeout, closed) { @@ -428,7 +431,7 @@ namespace llarp if(errno == EAGAIN || errno == EWOULDBLOCK) errno = 0; else - llarp::LogError("failed to read icmp for utp ", strerror(errno)); + LogError("failed to read icmp for utp ", strerror(errno)); return; } @@ -511,7 +514,7 @@ namespace llarp bool LinkLayer::KeyGen(SecretKey& k) { - Crypto()->encryption_keygen(k); + OurCrypto()->encryption_keygen(k); return true; } @@ -535,12 +538,10 @@ namespace llarp } std::unique_ptr< ILinkLayer > - NewServer(llarp::Crypto* crypto, const SecretKey& routerEncSecret, - llarp::GetRCFunc getrc, llarp::LinkMessageHandler h, - llarp::SessionEstablishedHandler est, - llarp::SessionRenegotiateHandler reneg, - llarp::SignBufferFunc sign, llarp::TimeoutHandler timeout, - llarp::SessionClosedHandler closed) + NewServer(Crypto* crypto, const SecretKey& routerEncSecret, GetRCFunc getrc, + LinkMessageHandler h, SessionEstablishedHandler est, + SessionRenegotiateHandler reneg, SignBufferFunc sign, + TimeoutHandler timeout, SessionClosedHandler closed) { return std::unique_ptr< ILinkLayer >( new LinkLayer(crypto, routerEncSecret, getrc, h, sign, est, reneg, @@ -548,20 +549,17 @@ namespace llarp } std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router* r) + NewServerFromRouter(AbstractRouter* r) { + using namespace std::placeholders; return NewServer( - r->crypto(), r->encryption, std::bind(&llarp::Router::rc, r), - std::bind(&llarp::Router::HandleRecvLinkMessageBuffer, r, - std::placeholders::_1, std::placeholders::_2), - std::bind(&llarp::Router::OnSessionEstablished, r, - std::placeholders::_1), - std::bind(&llarp::Router::CheckRenegotiateValid, r, - std::placeholders::_1, std::placeholders::_2), - std::bind(&llarp::Router::Sign, r, std::placeholders::_1, - std::placeholders::_2), - std::bind(&llarp::Router::OnConnectTimeout, r, std::placeholders::_1), - std::bind(&llarp::Router::SessionClosed, r, std::placeholders::_1)); + r->crypto(), r->encryption(), std::bind(&AbstractRouter::rc, r), + std::bind(&AbstractRouter::HandleRecvLinkMessageBuffer, r, _1, _2), + std::bind(&AbstractRouter::OnSessionEstablished, r, _1), + std::bind(&AbstractRouter::CheckRenegotiateValid, r, _1, _2), + std::bind(&AbstractRouter::Sign, r, _1, _2), + std::bind(&AbstractRouter::OnConnectTimeout, r, _1), + std::bind(&AbstractRouter::SessionClosed, r, _1)); } /// base constructor @@ -595,7 +593,7 @@ namespace llarp recvBufOffset = 0; TimedOut = std::bind(&Session::IsTimedOut, this, std::placeholders::_1); GetPubKey = std::bind(&Session::RemotePubKey, this); - GetRemoteRC = [&]() -> llarp::RouterContact { return this->remoteRC; }; + GetRemoteRC = [&]() -> RouterContact { return this->remoteRC; }; GetLinkLayer = std::bind(&Session::GetParent, this); lastActive = parent->Now(); @@ -622,9 +620,9 @@ namespace llarp remoteTransportPubKey = addr.pubkey; remoteRC = rc; RouterID rid = remoteRC.pubkey; - Crypto()->shorthash(txKey, llarp_buffer_t(rid)); + OurCrypto()->shorthash(txKey, llarp_buffer_t(rid)); rid = p->GetOurRC().pubkey; - Crypto()->shorthash(rxKey, llarp_buffer_t(rid)); + OurCrypto()->shorthash(rxKey, llarp_buffer_t(rid)); sock = s; assert(utp_set_userdata(sock, this) == this); @@ -638,7 +636,7 @@ namespace llarp Session::Session(LinkLayer* p, utp_socket* s, const Addr& addr) : Session(p) { RouterID rid = p->GetOurRC().pubkey; - Crypto()->shorthash(rxKey, llarp_buffer_t(rid)); + OurCrypto()->shorthash(rxKey, llarp_buffer_t(rid)); remoteRC.Clear(); sock = s; assert(s == sock); @@ -665,10 +663,10 @@ namespace llarp if(!gotLIM) { remoteRC = msg->rc; - Crypto()->shorthash(txKey, llarp_buffer_t(remoteRC.pubkey)); + OurCrypto()->shorthash(txKey, llarp_buffer_t(remoteRC.pubkey)); - if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, Crypto(), _1, - _2, _3, _4), + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, OurCrypto(), + _1, _2, _3, _4), rxKey, msg->N, remoteRC.enckey, parent->TransportSecretKey())) return false; @@ -677,9 +675,9 @@ namespace llarp llarp_buffer_t buf(tmp); LinkIntroMessage replymsg; replymsg.rc = parent->GetOurRC(); - if(!replymsg.rc.Verify(Crypto(), parent->Now())) + if(!replymsg.rc.Verify(OurCrypto(), parent->Now())) { - llarp::LogError("our RC is invalid? closing session to", remoteAddr); + LogError("our RC is invalid? closing session to", remoteAddr); Close(); return false; } @@ -687,16 +685,15 @@ namespace llarp replymsg.P = DefaultLinkSessionLifetime; if(!replymsg.Sign(parent->Sign)) { - llarp::LogError("failed to sign LIM for inbound handshake from ", - remoteAddr); + LogError("failed to sign LIM for inbound handshake from ", + remoteAddr); Close(); return false; } // encode if(!replymsg.BEncode(&buf)) { - llarp::LogError("failed to encode LIM for handshake from ", - remoteAddr); + LogError("failed to encode LIM for handshake from ", remoteAddr); Close(); return false; } @@ -706,17 +703,17 @@ namespace llarp // send if(!SendMessageBuffer(buf)) { - llarp::LogError("failed to repl to handshake from ", remoteAddr); + LogError("failed to repl to handshake from ", remoteAddr); Close(); return false; } - if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, Crypto(), _1, - _2, _3, _4), + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, OurCrypto(), + _1, _2, _3, _4), txKey, replymsg.N, remoteRC.enckey, parent->RouterEncryptionSecret())) return false; - llarp::LogDebug("Sent reply LIM"); + LogDebug("Sent reply LIM"); gotLIM = true; EnterState(eSessionReady); /// future LIM are used for session renegotiation @@ -748,10 +745,10 @@ namespace llarp uint32_t s = std::min(FragmentBodyPayloadSize, sz); if(!EncryptThenHash(ptr, msgid, s, sz - s)) { - llarp::LogError("EncryptThenHash failed?!"); + LogError("EncryptThenHash failed?!"); return false; } - llarp::LogDebug("encrypted ", s, " bytes"); + LogDebug("encrypted ", s, " bytes"); ptr += s; sz -= s; } @@ -768,9 +765,10 @@ namespace llarp remoteRC = msg->rc; gotLIM = true; - if(!DoKeyExchange( - std::bind(&Crypto::transport_dh_server, Crypto(), _1, _2, _3, _4), - rxKey, msg->N, remoteRC.enckey, parent->RouterEncryptionSecret())) + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, OurCrypto(), _1, + _2, _3, _4), + rxKey, msg->N, remoteRC.enckey, + parent->RouterEncryptionSecret())) { Close(); return false; @@ -790,9 +788,9 @@ namespace llarp // build our RC LinkIntroMessage msg; msg.rc = parent->GetOurRC(); - if(!msg.rc.Verify(Crypto(), parent->Now())) + if(!msg.rc.Verify(OurCrypto(), parent->Now())) { - llarp::LogError("our RC is invalid? closing session to", remoteAddr); + LogError("our RC is invalid? closing session to", remoteAddr); Close(); return; } @@ -800,15 +798,14 @@ namespace llarp msg.P = DefaultLinkSessionLifetime; if(!msg.Sign(parent->Sign)) { - llarp::LogError("failed to sign LIM for outbound handshake to ", - remoteAddr); + LogError("failed to sign LIM for outbound handshake to ", remoteAddr); Close(); return; } // encode if(!msg.BEncode(&buf)) { - llarp::LogError("failed to encode LIM for handshake to ", remoteAddr); + LogError("failed to encode LIM for handshake to ", remoteAddr); Close(); return; } @@ -818,18 +815,17 @@ namespace llarp // send if(!SendMessageBuffer(buf)) { - llarp::LogError("failed to send handshake to ", remoteAddr); + LogError("failed to send handshake to ", remoteAddr); Close(); return; } - if(!DoKeyExchange( - std::bind(&Crypto::transport_dh_client, Crypto(), _1, _2, _3, _4), - txKey, msg.N, remoteTransportPubKey, - parent->RouterEncryptionSecret())) + if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, OurCrypto(), _1, + _2, _3, _4), + txKey, msg.N, remoteTransportPubKey, + parent->RouterEncryptionSecret())) { - llarp::LogError("failed to mix keys for outbound session to ", - remoteAddr); + LogError("failed to mix keys for outbound session to ", remoteAddr); Close(); return; } @@ -863,7 +859,7 @@ namespace llarp } if(!self->Recv(arg->buf, arg->len)) { - llarp::LogDebug("recv fail for ", self->remoteAddr); + LogDebug("recv fail for ", self->remoteAddr); self->Close(); return 0; } @@ -871,7 +867,8 @@ namespace llarp } else { - llarp::LogWarn("utp_socket got data with no underlying session"); + LogWarn("utp_socket got data with no underlying session"); + utp_shutdown(arg->socket, SHUT_RDWR); utp_close(arg->socket); } return 0; @@ -899,7 +896,7 @@ namespace llarp } else if(arg->state == UTP_STATE_EOF) { - llarp::LogDebug("got eof from ", session->remoteAddr); + LogDebug("got eof from ", session->remoteAddr); session->Close(); } } @@ -912,7 +909,7 @@ namespace llarp LinkLayer* self = static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); Addr remote(*arg->address); - llarp::LogDebug("utp accepted from ", remote); + LogDebug("utp accepted from ", remote); Session* session = new Session(self, arg->socket, remote); if(!self->PutSession(session)) { @@ -961,14 +958,14 @@ namespace llarp TunnelNonce nonce(noncePtr); // encrypt - if(!Crypto()->xchacha20(payload, txKey, nonce)) + if(!OurCrypto()->xchacha20(payload, txKey, nonce)) return false; payload.base = noncePtr; payload.cur = payload.base; payload.sz = FragmentBufferSize - FragmentHashSize; // key'd hash - if(!Crypto()->hmac(buf.data(), payload, txKey)) + if(!OurCrypto()->hmac(buf.data(), payload, txKey)) return false; return MutateKey(txKey, A); } @@ -985,6 +982,17 @@ namespace llarp } } + util::StatusObject + Session::ExtractStatus() const + { + return {{"client", !remoteRC.IsPublicRouter()}, + {"sendBacklog", uint64_t(SendQueueBacklog())}, + {"tx", m_TXRate}, + {"rx", m_RXRate}, + {"remoteAddr", remoteAddr.ToString()}, + {"pubkey", remoteRC.pubkey.ToHex()}}; + } + bool Session::GotSessionRenegotiate(const LinkIntroMessage* msg) { @@ -999,7 +1007,7 @@ namespace llarp remoteRC = msg->rc; // recalculate rx key return DoKeyExchange( - std::bind(&Crypto::transport_dh_server, Crypto(), _1, _2, _3, _4), + std::bind(&Crypto::transport_dh_server, OurCrypto(), _1, _2, _3, _4), rxKey, msg->N, remoteRC.enckey, parent->RouterEncryptionSecret()); } @@ -1025,28 +1033,28 @@ namespace llarp return false; // regen our tx Key return DoKeyExchange( - std::bind(&Crypto::transport_dh_client, Crypto(), _1, _2, _3, _4), + std::bind(&Crypto::transport_dh_client, OurCrypto(), _1, _2, _3, _4), txKey, lim.N, remoteRC.enckey, parent->RouterEncryptionSecret()); } bool Session::VerifyThenDecrypt(const byte_t* ptr) { - llarp::LogDebug("verify then decrypt ", remoteAddr); + LogDebug("verify then decrypt ", remoteAddr); ShortHash digest; llarp_buffer_t hbuf(ptr + FragmentHashSize, FragmentBufferSize - FragmentHashSize); - if(!Crypto()->hmac(digest.data(), hbuf, rxKey)) + if(!OurCrypto()->hmac(digest.data(), hbuf, rxKey)) { - llarp::LogError("keyed hash failed"); + LogError("keyed hash failed"); return false; } ShortHash expected(ptr); if(expected != digest) { - llarp::LogError("Message Integrity Failed: got ", digest, " from ", - remoteAddr, " instead of ", expected); + LogError("Message Integrity Failed: got ", digest, " from ", remoteAddr, + " instead of ", expected); Close(); return false; } @@ -1057,9 +1065,9 @@ namespace llarp llarp_buffer_t out(rxFragBody); // decrypt - if(!Crypto()->xchacha20_alt(out, in, rxKey, ptr + FragmentHashSize)) + if(!OurCrypto()->xchacha20_alt(out, in, rxKey, ptr + FragmentHashSize)) { - llarp::LogError("failed to decrypt message from ", remoteAddr); + LogError("failed to decrypt message from ", remoteAddr); return false; } // get inner nonce @@ -1068,23 +1076,22 @@ namespace llarp out.cur += A.size(); // read msgid uint32_t msgid; - if(!llarp_buffer_read_uint32(&out, &msgid)) + if(!out.read_uint32(msgid)) { - llarp::LogError("failed to read msgid"); + LogError("failed to read msgid"); return false; } // read length and remaining uint16_t length, remaining; - if(!(llarp_buffer_read_uint16(&out, &length) - && llarp_buffer_read_uint16(&out, &remaining))) + if(!(out.read_uint16(length) && out.read_uint16(remaining))) { - llarp::LogError("failed to read the rest of the header"); + LogError("failed to read the rest of the header"); return false; } if(length > (out.sz - (out.cur - out.base))) { // too big length - llarp::LogError("fragment body too big"); + LogError("fragment body too big"); return false; } if(msgid < m_NextRXMsgID) @@ -1103,13 +1110,13 @@ namespace llarp // append data if(!itr->second.AppendData(out.cur, length)) { - llarp::LogError("inbound buffer is full"); + LogError("inbound buffer is full"); return false; // not enough room } // mutate key if(!MutateKey(rxKey, A)) { - llarp::LogError("failed to mutate rx key"); + LogError("failed to mutate rx key"); return false; } @@ -1123,7 +1130,7 @@ namespace llarp // rewind buf.underlying.cur = buf.underlying.base; // process buffer - llarp::LogDebug("got message ", msgid, " from ", remoteAddr); + LogDebug("got message ", msgid, " from ", remoteAddr); return parent->HandleMessage(this, buf.underlying); } return true; @@ -1142,7 +1149,7 @@ namespace llarp utp_shutdown(sock, SHUT_RDWR); utp_close(sock); } - llarp::LogDebug("utp_close ", remoteAddr); + LogDebug("utp_close ", remoteAddr); utp_set_userdata(sock, nullptr); } } diff --git a/llarp/link/utp.hpp b/llarp/link/utp.hpp index 5f482b28d..7032fef95 100644 --- a/llarp/link/utp.hpp +++ b/llarp/link/utp.hpp @@ -6,7 +6,7 @@ namespace llarp { - struct Router; + struct AbstractRouter; namespace utp { @@ -19,7 +19,7 @@ namespace llarp llarp::SessionClosedHandler closed); std::unique_ptr< ILinkLayer > - NewServerFromRouter(llarp::Router* r); + NewServerFromRouter(AbstractRouter* r); } // namespace utp } // namespace llarp diff --git a/llarp/link/utp_internal.hpp b/llarp/link/utp_internal.hpp index 90eaa72e5..306e48d0b 100644 --- a/llarp/link/utp_internal.hpp +++ b/llarp/link/utp_internal.hpp @@ -36,8 +36,7 @@ namespace llarp constexpr size_t FragmentBufferSize = FragmentOverheadSize + FragmentBodySize; - static_assert(FragmentBufferSize == 608, - "Fragement Buffer Size is not 608"); + static_assert(FragmentBufferSize == 608, "Fragment Buffer Size is not 608"); /// buffer for a single utp fragment using FragmentBuffer = llarp::AlignedBuffer< FragmentBufferSize >; @@ -132,10 +131,17 @@ namespace llarp std::unordered_map< uint32_t, InboundMessage > m_RecvMsgs; /// are we stalled or nah? bool stalled = false; + + uint64_t m_RXRate = 0; + uint64_t m_TXRate = 0; + /// mark session as alive void Alive(); + util::StatusObject + ExtractStatus() const override; + /// base Session(LinkLayer* p); @@ -161,7 +167,7 @@ namespace llarp // Router(); llarp::Crypto* - Crypto(); + OurCrypto(); /// session state, call EnterState(State) to set State state; @@ -327,7 +333,7 @@ namespace llarp #endif llarp::Crypto* - Crypto(); + OurCrypto(); /// pump sessions void @@ -337,7 +343,7 @@ namespace llarp void Stop(); - /// rengenerate transport keypair + /// regenerate transport keypair bool KeyGen(SecretKey& k); diff --git a/llarp/messages/dht.hpp b/llarp/messages/dht.hpp index be61d9ffc..d161495d6 100644 --- a/llarp/messages/dht.hpp +++ b/llarp/messages/dht.hpp @@ -24,7 +24,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override diff --git a/llarp/messages/dht_immediate.cpp b/llarp/messages/dht_immediate.cpp index 45cbec397..451e7acbe 100644 --- a/llarp/messages/dht_immediate.cpp +++ b/llarp/messages/dht_immediate.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace llarp { @@ -17,10 +17,10 @@ namespace llarp bool DHTImmediateMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf) { - if(llarp_buffer_eq(key, "m")) + if(key == "m") return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()), buf, msgs); - if(llarp_buffer_eq(key, "v")) + if(key == "v") { if(!bencode_read_integer(buf, &version)) return false; @@ -65,7 +65,7 @@ namespace llarp } bool - DHTImmediateMessage::HandleMessage(llarp::Router *router) const + DHTImmediateMessage::HandleMessage(AbstractRouter *router) const { DHTImmediateMessage reply; reply.session = session; diff --git a/llarp/messages/dht_immediate.hpp b/llarp/messages/dht_immediate.hpp index 00c0cb4e6..9802eecb2 100644 --- a/llarp/messages/dht_immediate.hpp +++ b/llarp/messages/dht_immediate.hpp @@ -16,7 +16,7 @@ namespace llarp ~DHTImmediateMessage(); - std::vector< std::unique_ptr< llarp::dht::IMessage > > msgs; + std::vector< std::unique_ptr< dht::IMessage > > msgs; bool DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; @@ -25,7 +25,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; void Clear() override; diff --git a/llarp/messages/discard.hpp b/llarp/messages/discard.hpp index 098309dd9..eeac2e862 100644 --- a/llarp/messages/discard.hpp +++ b/llarp/messages/discard.hpp @@ -39,7 +39,7 @@ namespace llarp } bool - HandleMessage(__attribute__((unused)) llarp::Router* router) const override + HandleMessage(__attribute__((unused)) AbstractRouter* router) const override { return true; } @@ -67,7 +67,7 @@ namespace llarp } bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override { return h->HandleDataDiscardMessage(this, r); } diff --git a/llarp/messages/exit.hpp b/llarp/messages/exit.hpp index f9b223e5b..2deff8187 100644 --- a/llarp/messages/exit.hpp +++ b/llarp/messages/exit.hpp @@ -56,7 +56,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; struct GrantExitMessage final : public IMessage @@ -89,7 +89,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override @@ -143,7 +143,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; struct UpdateExitVerifyMessage final : public IMessage @@ -185,7 +185,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; struct UpdateExitMessage final : public IMessage @@ -220,7 +220,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override @@ -257,7 +257,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; bool Sign(llarp::Crypto* c, const llarp::SecretKey& sk); diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index c4ede56eb..d4879138d 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -1,7 +1,8 @@ #include +#include #include -#include +#include #include #include @@ -14,7 +15,7 @@ namespace llarp bool LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, "a")) + if(key == "a") { llarp_buffer_t strbuf; if(!bencode_read_string(buf, &strbuf)) @@ -23,18 +24,18 @@ namespace llarp return false; return *strbuf.cur == 'i'; } - if(llarp_buffer_eq(key, "n")) + else if(key == "n") { if(N.BDecode(buf)) return true; llarp::LogWarn("failed to decode nonce in LIM"); return false; } - if(llarp_buffer_eq(key, "p")) + else if(key == "p") { return bencode_read_integer(buf, &P); } - if(llarp_buffer_eq(key, "r")) + else if(key == "r") { if(rc.BDecode(buf)) return true; @@ -42,7 +43,7 @@ namespace llarp llarp::DumpBuffer(*buf); return false; } - else if(llarp_buffer_eq(key, "v")) + else if(key == "v") { if(!bencode_read_integer(buf, &version)) return false; @@ -55,7 +56,7 @@ namespace llarp llarp::LogDebug("LIM version ", version); return true; } - else if(llarp_buffer_eq(key, "z")) + else if(key == "z") { return Z.BDecode(buf); } @@ -115,7 +116,7 @@ namespace llarp } bool - LinkIntroMessage::HandleMessage(llarp::Router* router) const + LinkIntroMessage::HandleMessage(AbstractRouter* router) const { if(!Verify(router->crypto())) return false; diff --git a/llarp/messages/link_intro.hpp b/llarp/messages/link_intro.hpp index 3d3b0310f..75c63c43e 100644 --- a/llarp/messages/link_intro.hpp +++ b/llarp/messages/link_intro.hpp @@ -34,7 +34,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; bool Sign(std::function< bool(Signature&, const llarp_buffer_t&) > signer); diff --git a/llarp/messages/link_message.cpp b/llarp/messages/link_message.cpp index 3bfb84743..d9a49d7b8 100644 --- a/llarp/messages/link_message.cpp +++ b/llarp/messages/link_message.cpp @@ -1,12 +1,36 @@ #include +#include +#include +#include +#include +#include +#include #include #include #include namespace llarp { - InboundMessageParser::InboundMessageParser(Router* _router) : router(_router) + struct InboundMessageParser::msg_holder_t + { + LinkIntroMessage i; + RelayDownstreamMessage d; + RelayUpstreamMessage u; + DHTImmediateMessage m; + LR_CommitMessage c; + DiscardMessage x; + }; + + InboundMessageParser::InboundMessageParser(AbstractRouter* _router) + : router(_router) + , from(nullptr) + , msg(nullptr) + , holder(std::make_unique< msg_holder_t >()) + { + } + + InboundMessageParser::~InboundMessageParser() { } @@ -24,7 +48,7 @@ namespace llarp if(!key) return false; // we are expecting the first key to be 'a' - if(!llarp_buffer_eq(*key, "a")) + if(!(*key == "a")) { llarp::LogWarn("message has no message type"); return false; @@ -46,22 +70,22 @@ namespace llarp switch(*strbuf.cur) { case 'i': - handler->msg = &handler->holder.i; + handler->msg = &handler->holder->i; break; case 'd': - handler->msg = &handler->holder.d; + handler->msg = &handler->holder->d; break; case 'u': - handler->msg = &handler->holder.u; + handler->msg = &handler->holder->u; break; case 'm': - handler->msg = &handler->holder.m; + handler->msg = &handler->holder->m; break; case 'c': - handler->msg = &handler->holder.c; + handler->msg = &handler->holder->c; break; case 'x': - handler->msg = &handler->holder.x; + handler->msg = &handler->holder->x; break; default: return false; diff --git a/llarp/messages/link_message.hpp b/llarp/messages/link_message.hpp index 84dcffc05..6edd91b6c 100644 --- a/llarp/messages/link_message.hpp +++ b/llarp/messages/link_message.hpp @@ -5,15 +5,12 @@ #include #include -#include #include namespace llarp { struct ILinkSession; - struct Router; - - using SendQueue = std::queue< ILinkMessage* >; + struct AbstractRouter; /// parsed link layer message struct ILinkMessage : public IBEncodeMessage @@ -29,7 +26,7 @@ namespace llarp } virtual bool - HandleMessage(Router* router) const = 0; + HandleMessage(AbstractRouter* router) const = 0; virtual void Clear() = 0; diff --git a/llarp/messages/link_message_parser.hpp b/llarp/messages/link_message_parser.hpp index f1bd42520..0bc39a899 100644 --- a/llarp/messages/link_message_parser.hpp +++ b/llarp/messages/link_message_parser.hpp @@ -1,18 +1,21 @@ #ifndef LLARP_LINK_MESSAGE_PARSER_HPP #define LLARP_LINK_MESSAGE_PARSER_HPP -#include -#include -#include -#include -#include -#include +#include +#include + +#include namespace llarp { + struct AbstractRouter; + struct ILinkMessage; + struct ILinkSession; + struct InboundMessageParser { - InboundMessageParser(Router* router); + InboundMessageParser(AbstractRouter* router); + ~InboundMessageParser(); dict_reader reader; static bool @@ -37,21 +40,12 @@ namespace llarp private: bool firstkey; - Router* router; - ILinkSession* from = nullptr; - ILinkMessage* msg = nullptr; - - struct msg_holder_t - { - LinkIntroMessage i; - RelayDownstreamMessage d; - RelayUpstreamMessage u; - DHTImmediateMessage m; - LR_CommitMessage c; - DiscardMessage x; - }; - - msg_holder_t holder; + AbstractRouter* router; + ILinkSession* from; + ILinkMessage* msg; + + struct msg_holder_t; + std::unique_ptr< msg_holder_t > holder; }; } // namespace llarp #endif diff --git a/llarp/messages/path_confirm.hpp b/llarp/messages/path_confirm.hpp index 021cc31e6..3c80873f4 100644 --- a/llarp/messages/path_confirm.hpp +++ b/llarp/messages/path_confirm.hpp @@ -22,7 +22,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; void Clear() override diff --git a/llarp/messages/path_latency.hpp b/llarp/messages/path_latency.hpp index aae47e955..21cb3d48a 100644 --- a/llarp/messages/path_latency.hpp +++ b/llarp/messages/path_latency.hpp @@ -27,7 +27,7 @@ namespace llarp }; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; } // namespace routing } // namespace llarp diff --git a/llarp/messages/path_transfer.hpp b/llarp/messages/path_transfer.hpp index 97b333026..ee6814430 100644 --- a/llarp/messages/path_transfer.hpp +++ b/llarp/messages/path_transfer.hpp @@ -31,7 +31,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(IMessageHandler*, llarp::Router* r) const override; + HandleMessage(IMessageHandler*, AbstractRouter* r) const override; void Clear() override diff --git a/llarp/messages/relay.cpp b/llarp/messages/relay.cpp index 4aff164b4..d6694471a 100644 --- a/llarp/messages/relay.cpp +++ b/llarp/messages/relay.cpp @@ -1,6 +1,7 @@ #include -#include +#include +#include #include namespace llarp @@ -58,9 +59,9 @@ namespace llarp } bool - RelayUpstreamMessage::HandleMessage(llarp::Router *r) const + RelayUpstreamMessage::HandleMessage(AbstractRouter *r) const { - auto path = r->paths.GetByDownstream(session->GetPubKey(), pathid); + auto path = r->pathContext().GetByDownstream(session->GetPubKey(), pathid); if(path) { return path->HandleUpstream(llarp_buffer_t(X), Y, r); @@ -121,9 +122,9 @@ namespace llarp } bool - RelayDownstreamMessage::HandleMessage(llarp::Router *r) const + RelayDownstreamMessage::HandleMessage(AbstractRouter *r) const { - auto path = r->paths.GetByUpstream(session->GetPubKey(), pathid); + auto path = r->pathContext().GetByUpstream(session->GetPubKey(), pathid); if(path) { return path->HandleDownstream(llarp_buffer_t(X), Y, r); diff --git a/llarp/messages/relay.hpp b/llarp/messages/relay.hpp index 8d236e489..7b4b10be3 100644 --- a/llarp/messages/relay.hpp +++ b/llarp/messages/relay.hpp @@ -27,7 +27,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; void Clear() override; @@ -49,7 +49,7 @@ namespace llarp BEncode(llarp_buffer_t* buf) const override; bool - HandleMessage(llarp::Router* router) const override; + HandleMessage(AbstractRouter* router) const override; void Clear() override; diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index d63f0cd5a..5931a452a 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -2,10 +2,11 @@ #include #include -#include +#include #include #include #include +#include namespace llarp { @@ -16,7 +17,7 @@ namespace llarp bool LR_CommitMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, "c")) + if(key == "c") { return BEncodeReadArray(frames, buf); } @@ -60,7 +61,7 @@ namespace llarp } bool - LR_CommitMessage::HandleMessage(llarp::Router* router) const + LR_CommitMessage::HandleMessage(AbstractRouter* router) const { if(frames.size() != MAXHOPS) { @@ -68,12 +69,12 @@ namespace llarp "!=", MAXHOPS); return false; } - if(!router->paths.AllowingTransit()) + if(!router->pathContext().AllowingTransit()) { llarp::LogError("got LRCM when not permitting transit"); return false; } - return AsyncDecrypt(&router->paths); + return AsyncDecrypt(&router->pathContext()); } bool @@ -131,7 +132,7 @@ namespace llarp if(!BEncodeMaybeReadVersion("v", self->version, LLARP_PROTO_VERSION, read, *key, r->buffer)) return false; - if(llarp_buffer_eq(*key, "w")) + if(*key == "w") { // check for duplicate if(self->work) diff --git a/llarp/messages/relay_commit.hpp b/llarp/messages/relay_commit.hpp index a37aea74d..d68fce51e 100644 --- a/llarp/messages/relay_commit.hpp +++ b/llarp/messages/relay_commit.hpp @@ -13,6 +13,7 @@ namespace llarp { // forward declare + struct AbstractRouter; namespace path { struct PathContext; @@ -63,7 +64,7 @@ namespace llarp BEncode(llarp_buffer_t *buf) const; bool - HandleMessage(llarp::Router *router) const; + HandleMessage(AbstractRouter *router) const; bool AsyncDecrypt(llarp::path::PathContext *context) const; diff --git a/llarp/messages/transfer_traffic.hpp b/llarp/messages/transfer_traffic.hpp index 369d599d0..c56e05617 100644 --- a/llarp/messages/transfer_traffic.hpp +++ b/llarp/messages/transfer_traffic.hpp @@ -45,7 +45,7 @@ namespace llarp DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* val) override; bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const override; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const override; }; } // namespace routing } // namespace llarp diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index 2dfd89e2e..f56897c6c 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -50,7 +50,7 @@ namespace llarp llarp_buffer_t strbuf; // rank - if(llarp_buffer_eq(key, "c")) + if(key == "c") { if(!bencode_read_integer(buf, &i)) return false; @@ -63,7 +63,7 @@ namespace llarp } // dialect - if(llarp_buffer_eq(key, "d")) + if(key == "d") { if(!bencode_read_string(buf, &strbuf)) return false; @@ -76,13 +76,13 @@ namespace llarp } // encryption public key - if(llarp_buffer_eq(key, "e")) + if(key == "e") { return pubkey.BDecode(buf); } // ip address - if(llarp_buffer_eq(key, "i")) + if(key == "i") { if(!bencode_read_string(buf, &strbuf)) return false; @@ -96,7 +96,7 @@ namespace llarp } // port - if(llarp_buffer_eq(key, "p")) + if(key == "p") { if(!bencode_read_integer(buf, &i)) return false; @@ -109,7 +109,7 @@ namespace llarp } // version - if(llarp_buffer_eq(key, "v")) + if(key == "v") { if(!bencode_read_integer(buf, &i)) return false; diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index 967829cf3..40a074313 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -76,9 +76,9 @@ namespace llarp return false; if(!BEncodeMaybeReadDictInt("v", version, read, k, buf)) return false; - if(llarp_buffer_eq(k, "a")) + if(k == "a") return bdecode_ip_string(buf, address); - if(llarp_buffer_eq(k, "b")) + if(k == "b") return bdecode_ip_string(buf, netmask); return read; } diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index 628fd2a4f..fea49d161 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -2,8 +2,13 @@ #include #include +#include +#include +#include +#include #include -#include +#include +#include #include #include @@ -13,7 +18,7 @@ namespace llarp { namespace path { - PathContext::PathContext(llarp::Router* router) + PathContext::PathContext(AbstractRouter* router) : m_Router(router), m_AllowTransit(false) { } @@ -37,25 +42,25 @@ namespace llarp llarp_threadpool* PathContext::Worker() { - return m_Router->tp; + return m_Router->threadpool(); } - llarp::Crypto* + Crypto* PathContext::Crypto() { return m_Router->crypto(); } - llarp::Logic* + Logic* PathContext::Logic() { return m_Router->logic(); } - llarp::SecretKey& + const SecretKey& PathContext::EncryptionSecretKey() { - return m_Router->encryption; + return m_Router->encryption(); } bool @@ -69,7 +74,7 @@ namespace llarp PathContext::ForwardLRCM(const RouterID& nextHop, const std::array< EncryptedFrame, 8 >& frames) { - llarp::LogDebug("forwarding LRCM to ", nextHop); + LogDebug("forwarding LRCM to ", nextHop); LR_CommitMessage msg; msg.frames = frames; return m_Router->SendToOrQueue(nextHop, &msg); @@ -217,7 +222,7 @@ namespace llarp return m_Router->pubkey(); } - llarp::Router* + AbstractRouter* PathContext::Router() { return m_Router; @@ -424,13 +429,12 @@ namespace llarp } else if(st == ePathBuilding) { - llarp::LogInfo("path ", Name(), " is building"); + LogInfo("path ", Name(), " is building"); buildStarted = now; } else if(st == ePathEstablished && _status == ePathBuilding) { - llarp::LogInfo("path ", Name(), " is built, took ", now - buildStarted, - " ms"); + LogInfo("path ", Name(), " is built, took ", now - buildStarted, " ms"); } _status = st; } @@ -489,7 +493,7 @@ namespace llarp } void - Path::Tick(llarp_time_t now, llarp::Router* r) + Path::Tick(llarp_time_t now, AbstractRouter* r) { if(Expired(now)) return; @@ -501,7 +505,7 @@ namespace llarp auto dlt = now - buildStarted; if(dlt >= PATH_BUILD_TIMEOUT) { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); return; } @@ -514,8 +518,8 @@ namespace llarp auto dlt = now - m_LastLatencyTestTime; if(dlt > 5000 && m_LastLatencyTestID == 0) { - llarp::routing::PathLatencyMessage latency; - latency.T = llarp::randint(); + routing::PathLatencyMessage latency; + latency.T = randint(); m_LastLatencyTestID = latency.T; m_LastLatencyTestTime = now; SendRoutingMessage(&latency, r); @@ -526,7 +530,7 @@ namespace llarp && now - m_LastRecvMessage > PATH_ALIVE_TIMEOUT) { // TODO: send close exit message - // r->routerProfiling.MarkPathFail(this); + // r->routerProfiling().MarkPathFail(this); // EnterState(ePathTimeout, now); return; } @@ -538,19 +542,19 @@ namespace llarp { if(m_CheckForDead(this, dlt)) { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); } } else { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); } } else if(dlt >= 10000 && m_LastRecvMessage == 0) { - r->routerProfiling.MarkPathFail(this); + r->routerProfiling().MarkPathFail(this); EnterState(ePathTimeout, now); } } @@ -572,7 +576,7 @@ namespace llarp msg.pathid = TXID(); if(r->SendToOrQueue(Upstream(), &msg)) return true; - llarp::LogError("send to ", Upstream(), " failed"); + LogError("send to ", Upstream(), " failed"); return false; } @@ -613,7 +617,7 @@ namespace llarp { if(!r->ParseRoutingMessageBuffer(buf, this, RXID())) { - llarp::LogWarn("Failed to parse inbound routing message"); + LogWarn("Failed to parse inbound routing message"); return false; } m_LastRecvMessage = r->Now(); @@ -622,7 +626,7 @@ namespace llarp bool Path::HandleUpdateExitVerifyMessage( - const llarp::routing::UpdateExitVerifyMessage* msg, llarp::Router* r) + const routing::UpdateExitVerifyMessage* msg, AbstractRouter* r) { (void)r; if(m_UpdateExitTX && msg->T == m_UpdateExitTX) @@ -639,19 +643,18 @@ namespace llarp } bool - Path::SendRoutingMessage(const llarp::routing::IMessage* msg, - llarp::AbstractRouter* r) + Path::SendRoutingMessage(const routing::IMessage* msg, AbstractRouter* r) { std::array< byte_t, MAX_LINK_MSG_SIZE / 2 > tmp; llarp_buffer_t buf(tmp); - // should help prevent bad paths with uninitialised members - // FIXME: Why would we get uninitialised IMessages? + // should help prevent bad paths with uninitialized members + // FIXME: Why would we get uninitialized IMessages? if(msg->version != LLARP_PROTO_VERSION) return false; if(!msg->BEncode(&buf)) { - llarp::LogError("Bencode failed"); - llarp::DumpBuffer(buf); + LogError("Bencode failed"); + DumpBuffer(buf); return false; } // make nonce @@ -670,18 +673,18 @@ namespace llarp } bool - Path::HandlePathTransferMessage( - __attribute__((unused)) const llarp::routing::PathTransferMessage* msg, - __attribute__((unused)) llarp::Router* r) + Path::HandlePathTransferMessage(__attribute__((unused)) + const routing::PathTransferMessage* msg, + __attribute__((unused)) AbstractRouter* r) { - llarp::LogWarn("unwarranted path transfer message on tx=", TXID(), - " rx=", RXID()); + LogWarn("unwarranted path transfer message on tx=", TXID(), + " rx=", RXID()); return false; } bool - Path::HandleDataDiscardMessage( - const llarp::routing::DataDiscardMessage* msg, llarp::Router* r) + Path::HandleDataDiscardMessage(const routing::DataDiscardMessage* msg, + AbstractRouter* r) { MarkActive(r->Now()); if(m_DropHandler) @@ -690,9 +693,9 @@ namespace llarp } bool - Path::HandlePathConfirmMessage( - __attribute__((unused)) const llarp::routing::PathConfirmMessage* msg, - llarp::Router* r) + Path::HandlePathConfirmMessage(__attribute__((unused)) + const routing::PathConfirmMessage* msg, + AbstractRouter* r) { auto now = r->Now(); if(_status == ePathBuilding) @@ -700,33 +703,33 @@ namespace llarp // finish initializing introduction intro.expiresAt = buildStarted + hops[0].lifetime; - r->routerProfiling.MarkPathSuccess(this); + r->routerProfiling().MarkPathSuccess(this); // persist session with upstream router until the path is done r->PersistSessionUntil(Upstream(), intro.expiresAt); MarkActive(now); // send path latency test - llarp::routing::PathLatencyMessage latency; - latency.T = llarp::randint(); + routing::PathLatencyMessage latency; + latency.T = randint(); m_LastLatencyTestID = latency.T; m_LastLatencyTestTime = now; return SendRoutingMessage(&latency, r); } - llarp::LogWarn("got unwarrented path confirm message on tx=", RXID(), - " rx=", RXID()); + LogWarn("got unwarranted path confirm message on tx=", RXID(), + " rx=", RXID()); return false; } bool - Path::HandleHiddenServiceFrame(const llarp::service::ProtocolFrame* frame) + Path::HandleHiddenServiceFrame(const service::ProtocolFrame* frame) { MarkActive(m_PathSet->Now()); return m_DataHandler && m_DataHandler(this, frame); } bool - Path::HandlePathLatencyMessage( - const llarp::routing::PathLatencyMessage* msg, llarp::Router* r) + Path::HandlePathLatencyMessage(const routing::PathLatencyMessage* msg, + AbstractRouter* r) { auto now = r->Now(); MarkActive(now); @@ -743,15 +746,15 @@ namespace llarp } else { - llarp::LogWarn("unwarrented path latency message via ", Upstream()); + LogWarn("unwarranted path latency message via ", Upstream()); return false; } } bool - Path::HandleDHTMessage(const llarp::dht::IMessage* msg, llarp::Router* r) + Path::HandleDHTMessage(const dht::IMessage* msg, AbstractRouter* r) { - llarp::routing::DHTMessage reply; + routing::DHTMessage reply; if(!msg->HandleMessage(r->dht(), reply.M)) return false; MarkActive(r->Now()); @@ -761,102 +764,101 @@ namespace llarp } bool - Path::HandleCloseExitMessage(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) + Path::HandleCloseExitMessage(const routing::CloseExitMessage* msg, + AbstractRouter* r) { /// allows exits to close from their end if(SupportsAnyRoles(ePathRoleExit | ePathRoleSVC)) { if(msg->Verify(r->crypto(), EndpointPubKey())) { - llarp::LogInfo(Name(), " had its exit closed"); + LogInfo(Name(), " had its exit closed"); _role &= ~ePathRoleExit; return true; } else - llarp::LogError(Name(), " CXM from exit with bad signature"); + LogError(Name(), " CXM from exit with bad signature"); } else - llarp::LogError(Name(), " unwarrented CXM"); + LogError(Name(), " unwarranted CXM"); return false; } bool - Path::SendExitRequest(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) + Path::SendExitRequest(const routing::ObtainExitMessage* msg, + AbstractRouter* r) { - llarp::LogInfo(Name(), " sending exit request to ", Endpoint()); + LogInfo(Name(), " sending exit request to ", Endpoint()); m_ExitObtainTX = msg->T; return SendRoutingMessage(msg, r); } bool - Path::SendExitClose(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) + Path::SendExitClose(const routing::CloseExitMessage* msg, AbstractRouter* r) { - llarp::LogInfo(Name(), " closing exit to ", Endpoint()); + LogInfo(Name(), " closing exit to ", Endpoint()); // mark as not exit anymore _role &= ~ePathRoleExit; return SendRoutingMessage(msg, r); } bool - Path::HandleObtainExitMessage(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) + Path::HandleObtainExitMessage(const routing::ObtainExitMessage* msg, + AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(Name(), " got unwarrented OXM"); + LogError(Name(), " got unwarranted OXM"); return false; } bool - Path::HandleUpdateExitMessage(const llarp::routing::UpdateExitMessage* msg, - llarp::Router* r) + Path::HandleUpdateExitMessage(const routing::UpdateExitMessage* msg, + AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(Name(), " got unwarrented UXM"); + LogError(Name(), " got unwarranted UXM"); return false; } bool - Path::HandleRejectExitMessage(const llarp::routing::RejectExitMessage* msg, - llarp::Router* r) + Path::HandleRejectExitMessage(const routing::RejectExitMessage* msg, + AbstractRouter* r) { if(m_ExitObtainTX && msg->T == m_ExitObtainTX) { if(!msg->Verify(r->crypto(), EndpointPubKey())) { - llarp::LogError(Name(), "RXM invalid signature"); + LogError(Name(), "RXM invalid signature"); return false; } - llarp::LogInfo(Name(), " ", Endpoint(), " Rejected exit"); + LogInfo(Name(), " ", Endpoint(), " Rejected exit"); MarkActive(r->Now()); return InformExitResult(msg->B); } - llarp::LogError(Name(), " got unwarrented RXM"); + LogError(Name(), " got unwarranted RXM"); return false; } bool - Path::HandleGrantExitMessage(const llarp::routing::GrantExitMessage* msg, - llarp::Router* r) + Path::HandleGrantExitMessage(const routing::GrantExitMessage* msg, + AbstractRouter* r) { if(m_ExitObtainTX && msg->T == m_ExitObtainTX) { if(!msg->Verify(r->crypto(), EndpointPubKey())) { - llarp::LogError(Name(), " GXM signature failed"); + LogError(Name(), " GXM signature failed"); return false; } // we now can send exit traffic _role |= ePathRoleExit; - llarp::LogInfo(Name(), " ", Endpoint(), " Granted exit"); + LogInfo(Name(), " ", Endpoint(), " Granted exit"); MarkActive(r->Now()); return InformExitResult(0); } - llarp::LogError(Name(), " got unwarrented GXM"); + LogError(Name(), " got unwarranted GXM"); return false; } @@ -872,7 +874,7 @@ namespace llarp bool Path::HandleTransferTrafficMessage( - const llarp::routing::TransferTrafficMessage* msg, llarp::Router* r) + const routing::TransferTrafficMessage* msg, AbstractRouter* r) { // check if we can handle exit data if(!SupportsAnyRoles(ePathRoleExit | ePathRoleSVC)) diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index 91c637f0a..ec3f81f71 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -32,10 +32,12 @@ namespace llarp { + class Logic; struct AbstractRouter; struct Crypto; struct LR_CommitMessage; struct LR_CommitRecord; + namespace path { struct TransitHopInfo @@ -198,46 +200,46 @@ namespace llarp bool HandleDataDiscardMessage(const llarp::routing::DataDiscardMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathConfirmMessage(const llarp::routing::PathConfirmMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathTransferMessage(const llarp::routing::PathTransferMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathLatencyMessage(const llarp::routing::PathLatencyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleObtainExitMessage(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitVerifyMessage( const llarp::routing::UpdateExitVerifyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleTransferTrafficMessage( const llarp::routing::TransferTrafficMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitMessage(const llarp::routing::UpdateExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleGrantExitMessage(const llarp::routing::GrantExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleRejectExitMessage(const llarp::routing::RejectExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleCloseExitMessage(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleHiddenServiceFrame(__attribute__(( @@ -253,7 +255,7 @@ namespace llarp bool HandleDHTMessage(const llarp::dht::IMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; // handle data in upstream direction bool @@ -422,7 +424,7 @@ namespace llarp Expired(llarp_time_t now) const override; void - Tick(llarp_time_t now, llarp::Router* r); + Tick(llarp_time_t now, AbstractRouter* r); bool SendRoutingMessage(const llarp::routing::IMessage* msg, @@ -430,47 +432,47 @@ namespace llarp bool HandleObtainExitMessage(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitVerifyMessage( const llarp::routing::UpdateExitVerifyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleTransferTrafficMessage( const llarp::routing::TransferTrafficMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleUpdateExitMessage(const llarp::routing::UpdateExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleCloseExitMessage(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleGrantExitMessage(const llarp::routing::GrantExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleRejectExitMessage(const llarp::routing::RejectExitMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleDataDiscardMessage(const llarp::routing::DataDiscardMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathConfirmMessage(const llarp::routing::PathConfirmMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathLatencyMessage(const llarp::routing::PathLatencyMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandlePathTransferMessage(const llarp::routing::PathTransferMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleHiddenServiceFrame( @@ -481,7 +483,7 @@ namespace llarp bool HandleDHTMessage(const llarp::dht::IMessage* msg, - llarp::Router* r) override; + AbstractRouter* r) override; bool HandleRoutingMessage(const llarp_buffer_t& buf, AbstractRouter* r); @@ -530,11 +532,11 @@ namespace llarp bool SendExitRequest(const llarp::routing::ObtainExitMessage* msg, - llarp::Router* r); + AbstractRouter* r); bool SendExitClose(const llarp::routing::CloseExitMessage* msg, - llarp::Router* r); + AbstractRouter* r); private: /// call obtained exit hooks @@ -568,7 +570,7 @@ namespace llarp struct PathContext { - PathContext(llarp::Router* router); + PathContext(AbstractRouter* router); ~PathContext(); /// called from router tick function @@ -665,17 +667,17 @@ namespace llarp llarp::Logic* Logic(); - llarp::Router* + AbstractRouter* Router(); - llarp::SecretKey& + const llarp::SecretKey& EncryptionSecretKey(); const byte_t* OurRouterID() const; private: - llarp::Router* m_Router; + AbstractRouter* m_Router; SyncTransitMap_t m_TransitPaths; SyncTransitMap_t m_Paths; SyncOwnedPathsMap_t m_OurPaths; diff --git a/llarp/path/path_types.hpp b/llarp/path/path_types.hpp index 7f82dc386..8256c383c 100644 --- a/llarp/path/path_types.hpp +++ b/llarp/path/path_types.hpp @@ -6,7 +6,10 @@ namespace llarp { - using PathID_t = AlignedBuffer< PATHIDSIZE >; -} + struct PathID_t final : public AlignedBuffer< PATHIDSIZE > + { + }; + +} // namespace llarp #endif diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 9cdaf225f..35f902aaa 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -1,9 +1,12 @@ #include +#include #include #include -#include +#include +#include #include +#include #include @@ -21,7 +24,7 @@ namespace llarp Handler result; size_t idx = 0; - llarp::Router* router = nullptr; + AbstractRouter* router = nullptr; llarp_threadpool* worker = nullptr; llarp::Logic* logic = nullptr; llarp::Crypto* crypto = nullptr; @@ -123,7 +126,7 @@ namespace llarp { } - /// Generate all keys asynchronously and call hadler when done + /// Generate all keys asynchronously and call handler when done void AsyncGenerateKeys(Path_t* p, llarp::Logic* l, llarp_threadpool* pool, User* u, Handler func) @@ -154,7 +157,7 @@ namespace llarp // persist session with router until this path is done ctx->router->PersistSessionUntil(remote, ctx->path->ExpireTime()); // add own path - ctx->router->paths.AddOwnPath(ctx->pathset, ctx->path); + ctx->router->pathContext().AddOwnPath(ctx->pathset, ctx->path); ctx->path = nullptr; } else @@ -166,7 +169,7 @@ namespace llarp namespace path { - Builder::Builder(llarp::Router* p_router, struct llarp_dht_context* p_dht, + Builder::Builder(AbstractRouter* p_router, struct llarp_dht_context* p_dht, size_t pathNum, size_t hops) : llarp::path::PathSet(pathNum) , router(p_router) @@ -181,7 +184,7 @@ namespace llarp Builder::~Builder() { - router->paths.RemovePathBuilder(this); + router->pathContext().RemovePathBuilder(this); } util::StatusObject @@ -215,7 +218,7 @@ namespace llarp --tries; if(db->select_random_hop(prev, cur, hop)) return true; - } while(router->routerProfiling.IsBad(cur.pubkey) && tries > 0); + } while(router->routerProfiling().IsBad(cur.pubkey) && tries > 0); return false; } @@ -317,7 +320,7 @@ namespace llarp path->SetBuildResultHook(std::bind(&llarp::path::Builder::HandlePathBuilt, this, std::placeholders::_1)); ++keygens; - ctx->AsyncGenerateKeys(path, router->logic(), router->tp, this, + ctx->AsyncGenerateKeys(path, router->logic(), router->threadpool(), this, &PathBuilderKeysGenerated); } diff --git a/llarp/path/pathbuilder.hpp b/llarp/path/pathbuilder.hpp index c8c09d0e2..160145ee9 100644 --- a/llarp/path/pathbuilder.hpp +++ b/llarp/path/pathbuilder.hpp @@ -22,7 +22,7 @@ namespace llarp std::atomic< bool > _run; public: - llarp::Router* router; + AbstractRouter* router; llarp_dht_context* dht; llarp::SecretKey enckey; size_t numHops; @@ -33,7 +33,7 @@ namespace llarp std::atomic< uint8_t > keygens; /// construct - Builder(llarp::Router* p_router, llarp_dht_context* p_dht, + Builder(AbstractRouter* p_router, llarp_dht_context* p_dht, size_t numPaths, size_t numHops); virtual ~Builder(); diff --git a/llarp/path/pathset.cpp b/llarp/path/pathset.cpp index 1aa8558bd..48835d69f 100644 --- a/llarp/path/pathset.cpp +++ b/llarp/path/pathset.cpp @@ -58,7 +58,7 @@ namespace llarp } void - PathSet::Tick(llarp_time_t now, llarp::Router* r) + PathSet::Tick(llarp_time_t now, AbstractRouter* r) { Lock_t l(m_PathsMutex); for(auto& item : m_Paths) diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index 894a3e6a2..dcaec43cd 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -65,7 +65,7 @@ namespace llarp /// tick owned paths void - Tick(llarp_time_t now, llarp::Router* r); + Tick(llarp_time_t now, AbstractRouter* r); /// count the number of paths that will exist at this timestamp in future size_t @@ -186,7 +186,7 @@ namespace llarp std::set< llarp::service::Introduction >& intros) const; virtual bool - PublishIntroSet(__attribute__((unused)) llarp::Router* r) + PublishIntroSet(__attribute__((unused)) AbstractRouter* r) { return false; } diff --git a/llarp/path/transit_hop.cpp b/llarp/path/transit_hop.cpp index f191e23da..ca2a524b1 100644 --- a/llarp/path/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -1,8 +1,13 @@ #include #include +#include #include -#include +#include +#include +#include +#include +#include #include #include #include @@ -122,14 +127,14 @@ namespace llarp bool TransitHop::HandleDHTMessage(const llarp::dht::IMessage* msg, - llarp::Router* r) + AbstractRouter* r) { return r->dht()->impl.RelayRequestForPath(info.rxID, msg); } bool TransitHop::HandlePathLatencyMessage( - const llarp::routing::PathLatencyMessage* msg, llarp::Router* r) + const llarp::routing::PathLatencyMessage* msg, AbstractRouter* r) { llarp::routing::PathLatencyMessage reply; reply.L = msg->T; @@ -139,7 +144,7 @@ namespace llarp bool TransitHop::HandlePathConfirmMessage( __attribute__((unused)) const llarp::routing::PathConfirmMessage* msg, - __attribute__((unused)) llarp::Router* r) + __attribute__((unused)) AbstractRouter* r) { llarp::LogWarn("unwarranted path confirm message on ", info); return false; @@ -148,7 +153,7 @@ namespace llarp bool TransitHop::HandleDataDiscardMessage( __attribute__((unused)) const llarp::routing::DataDiscardMessage* msg, - __attribute__((unused)) llarp::Router* r) + __attribute__((unused)) AbstractRouter* r) { llarp::LogWarn("unwarranted path data discard message on ", info); return false; @@ -156,15 +161,15 @@ namespace llarp bool TransitHop::HandleObtainExitMessage( - const llarp::routing::ObtainExitMessage* msg, llarp::Router* r) + const llarp::routing::ObtainExitMessage* msg, AbstractRouter* r) { if(msg->Verify(r->crypto()) - && r->exitContext.ObtainNewExit(msg->I, info.rxID, msg->E != 0)) + && r->exitContext().ObtainNewExit(msg->I, info.rxID, msg->E != 0)) { llarp::routing::GrantExitMessage grant; grant.S = NextSeqNo(); grant.T = msg->T; - if(!grant.Sign(r->crypto(), r->identity)) + if(!grant.Sign(r->crypto(), r->identity())) { llarp::LogError("Failed to sign grant exit message"); return false; @@ -176,7 +181,7 @@ namespace llarp llarp::routing::RejectExitMessage reject; reject.S = NextSeqNo(); reject.T = msg->T; - if(!reject.Sign(r->crypto(), r->identity)) + if(!reject.Sign(r->crypto(), r->identity())) { llarp::LogError("Failed to sign reject exit message"); return false; @@ -186,17 +191,17 @@ namespace llarp bool TransitHop::HandleCloseExitMessage( - const llarp::routing::CloseExitMessage* msg, llarp::Router* r) + const llarp::routing::CloseExitMessage* msg, AbstractRouter* r) { llarp::routing::DataDiscardMessage discard(info.rxID, msg->S); - auto ep = r->exitContext.FindEndpointForPath(info.rxID); + auto ep = r->exitContext().FindEndpointForPath(info.rxID); if(ep && msg->Verify(r->crypto(), ep->PubKey())) { ep->Close(); // ep is now gone af llarp::routing::CloseExitMessage reply; reply.S = NextSeqNo(); - if(reply.Sign(r->crypto(), r->identity)) + if(reply.Sign(r->crypto(), r->identity())) return SendRoutingMessage(&reply, r); } return SendRoutingMessage(&discard, r); @@ -204,7 +209,7 @@ namespace llarp bool TransitHop::HandleUpdateExitVerifyMessage( - const llarp::routing::UpdateExitVerifyMessage* msg, llarp::Router* r) + const llarp::routing::UpdateExitVerifyMessage* msg, AbstractRouter* r) { (void)msg; (void)r; @@ -214,9 +219,9 @@ namespace llarp bool TransitHop::HandleUpdateExitMessage( - const llarp::routing::UpdateExitMessage* msg, llarp::Router* r) + const llarp::routing::UpdateExitMessage* msg, AbstractRouter* r) { - auto ep = r->exitContext.FindEndpointForPath(msg->P); + auto ep = r->exitContext().FindEndpointForPath(msg->P); if(ep) { if(!msg->Verify(r->crypto(), ep->PubKey())) @@ -237,29 +242,29 @@ namespace llarp bool TransitHop::HandleRejectExitMessage( - const llarp::routing::RejectExitMessage* msg, llarp::Router* r) + const llarp::routing::RejectExitMessage* msg, AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(info, " got unwarrented RXM"); + llarp::LogError(info, " got unwarranted RXM"); return false; } bool TransitHop::HandleGrantExitMessage( - const llarp::routing::GrantExitMessage* msg, llarp::Router* r) + const llarp::routing::GrantExitMessage* msg, AbstractRouter* r) { (void)msg; (void)r; - llarp::LogError(info, " got unwarrented GXM"); + llarp::LogError(info, " got unwarranted GXM"); return false; } bool TransitHop::HandleTransferTrafficMessage( - const llarp::routing::TransferTrafficMessage* msg, llarp::Router* r) + const llarp::routing::TransferTrafficMessage* msg, AbstractRouter* r) { - auto endpoint = r->exitContext.FindEndpointForPath(info.rxID); + auto endpoint = r->exitContext().FindEndpointForPath(info.rxID); if(endpoint) { bool sent = true; @@ -284,9 +289,9 @@ namespace llarp bool TransitHop::HandlePathTransferMessage( - const llarp::routing::PathTransferMessage* msg, llarp::Router* r) + const llarp::routing::PathTransferMessage* msg, AbstractRouter* r) { - auto path = r->paths.GetPathForTransfer(msg->P); + auto path = r->pathContext().GetPathForTransfer(msg->P); llarp::routing::DataDiscardMessage discarded(msg->P, msg->S); if(!path) { diff --git a/llarp/router/abstractrouter.cpp b/llarp/router/abstractrouter.cpp new file mode 100644 index 000000000..b56ffcd57 --- /dev/null +++ b/llarp/router/abstractrouter.cpp @@ -0,0 +1,6 @@ +#include + +namespace llarp +{ + +} // namespace llarp diff --git a/llarp/router/abstractrouter.hpp b/llarp/router/abstractrouter.hpp new file mode 100644 index 000000000..07fcea604 --- /dev/null +++ b/llarp/router/abstractrouter.hpp @@ -0,0 +1,151 @@ +#ifndef LLARP_ABSTRACT_ROUTER_HPP +#define LLARP_ABSTRACT_ROUTER_HPP + +#include +#include +#include + +struct llarp_buffer_t; +struct llarp_dht_context; +struct llarp_ev_loop; +struct llarp_nodedb; +struct llarp_threadpool; + +namespace llarp +{ + struct Crypto; + class Logic; + struct RouterContact; + struct RouterID; + struct ILinkMessage; + struct ILinkSession; + struct PathID_t; + struct Profiling; + struct SecretKey; + struct Signature; + + namespace exit + { + struct Context; + } + + namespace path + { + struct PathContext; + } + + namespace routing + { + struct IMessageHandler; + } + + struct AbstractRouter : public util::IStateful + { + + virtual void + OnSessionEstablished(RouterContact rc) = 0; + + virtual bool + HandleRecvLinkMessageBuffer(ILinkSession *from, + const llarp_buffer_t &msg) = 0; + + virtual Logic * + logic() const = 0; + + virtual llarp_dht_context * + dht() const = 0; + + virtual Crypto * + crypto() const = 0; + + virtual llarp_nodedb * + nodedb() const = 0; + + virtual const path::PathContext & + pathContext() const = 0; + + virtual path::PathContext & + pathContext() = 0; + + virtual const RouterContact & + rc() const = 0; + + virtual exit::Context & + exitContext() = 0; + + virtual const SecretKey & + identity() const = 0; + + virtual const SecretKey & + encryption() const = 0; + + virtual Profiling & + routerProfiling() = 0; + + virtual llarp_ev_loop * + netloop() const = 0; + + virtual llarp_threadpool * + threadpool() = 0; + + virtual llarp_threadpool * + diskworker() = 0; + + virtual bool + Sign(Signature &sig, const llarp_buffer_t &buf) const = 0; + + virtual const byte_t * + pubkey() const = 0; + + virtual void + OnConnectTimeout(ILinkSession *session) = 0; + + /// called by link when a remote session has no more sessions open + virtual void + SessionClosed(RouterID remote) = 0; + + virtual llarp_time_t + Now() const = 0; + + virtual bool + GetRandomGoodRouter(RouterID &r) = 0; + + virtual bool + SendToOrQueue(const RouterID &remote, const ILinkMessage *msg) = 0; + + virtual void + PersistSessionUntil(const RouterID &remote, llarp_time_t until) = 0; + + virtual bool + ParseRoutingMessageBuffer(const llarp_buffer_t &buf, + routing::IMessageHandler *h, + const PathID_t &rxid) = 0; + + virtual size_t + NumberOfConnectedRouters() const = 0; + + virtual bool + GetRandomConnectedRouter(RouterContact &result) const = 0; + + virtual void + HandleDHTLookupForExplore(RouterID remote, + const std::vector< RouterContact > &results) = 0; + + /// check if newRc matches oldRC and update local rc for this remote contact + /// if valid + /// returns true on valid and updated + /// returns false otherwise + virtual bool + CheckRenegotiateValid(RouterContact newRc, RouterContact oldRC) = 0; + + /// set router's service node whitelist + virtual void + SetRouterWhitelist(const std::vector & routers) =0 ; + + /// visit each connected link session + virtual void + ForEachPeer(std::function visit) const = 0; + }; +} // namespace llarp + +#endif diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index c9e4d7a3c..c18aeec65 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -68,7 +68,7 @@ struct TryConnectJob void AttemptTimedout() { - router->routerProfiling.MarkTimeout(rc.pubkey); + router->routerProfiling().MarkTimeout(rc.pubkey); if(ShouldRetry()) { Attempt(); @@ -76,7 +76,7 @@ struct TryConnectJob } if(!router->IsServiceNode()) { - if(router->routerProfiling.IsBad(rc.pubkey)) + if(router->routerProfiling().IsBad(rc.pubkey)) { router->nodedb()->Remove(rc.pubkey); } @@ -163,17 +163,13 @@ llarp_findOrCreateEncryption(llarp::Crypto *crypto, const fs::path &path, namespace llarp { - AbstractRouter::~AbstractRouter() - { - } - bool - Router::TryConnectAsync(llarp::RouterContact remote, uint16_t numretries) + Router::TryConnectAsync(RouterContact remote, uint16_t numretries) { // do we already have a pending job for this remote? if(HasPendingConnectJob(remote.pubkey)) { - llarp::LogDebug("We have pending connect jobs to ", remote.pubkey); + LogDebug("We have pending connect jobs to ", remote.pubkey); return false; } @@ -197,21 +193,21 @@ namespace llarp } void - Router::OnSessionEstablished(llarp::RouterContact rc) + Router::OnSessionEstablished(RouterContact rc) { async_verify_RC(rc, nullptr); - llarp::LogInfo("session with ", rc.pubkey, " established"); + LogInfo("session with ", rc.pubkey, " established"); } - Router::Router(struct llarp_threadpool *_tp, struct llarp_ev_loop *_netloop, - llarp::Logic *l) + Router::Router(struct llarp_threadpool *_tp, struct llarp_ev_loop *__netloop, + Logic *l) : ready(false) - , netloop(_netloop) + , _netloop(__netloop) , tp(_tp) , _logic(l) , _crypto(std::make_unique< sodium::CryptoLibSodium >()) , paths(this) - , exitContext(this) + , _exitContext(this) , _dht(llarp_dht_context_new(this)) , inbound_link_msg_parser(this) , hiddenServiceContext(this) @@ -239,12 +235,25 @@ namespace llarp { util::StatusObject obj{{"dht", _dht->impl.ExtractStatus()}, {"services", hiddenServiceContext.ExtractStatus()}, - {"exit", exitContext.ExtractStatus()}}; + {"exit", _exitContext.ExtractStatus()}}; + std::vector< util::StatusObject > ob_links, ib_links; + std::transform(inboundLinks.begin(), inboundLinks.end(), + std::back_inserter(ib_links), + [](const auto &link) -> util::StatusObject { + return link->ExtractStatus(); + }); + std::transform(outboundLinks.begin(), outboundLinks.end(), + std::back_inserter(ob_links), + [](const auto &link) -> util::StatusObject { + return link->ExtractStatus(); + }); + obj.Put("links", + util::StatusObject{{"outbound", ob_links}, {"inbound", ib_links}}); return obj; } bool - Router::HandleRecvLinkMessageBuffer(llarp::ILinkSession *session, + Router::HandleRecvLinkMessageBuffer(ILinkSession *session, const llarp_buffer_t &buf) { if(_stopping) @@ -252,16 +261,16 @@ namespace llarp if(!session) { - llarp::LogWarn("no link session"); + LogWarn("no link session"); return false; } return inbound_link_msg_parser.ProcessFrom(session, buf); } void - Router::PersistSessionUntil(const llarp::RouterID &remote, llarp_time_t until) + Router::PersistSessionUntil(const RouterID &remote, llarp_time_t until) { - llarp::LogDebug("persist session to ", remote, " until ", until); + LogDebug("persist session to ", remote, " until ", until); m_PersistingSessions[remote] = std::max(until, m_PersistingSessions[remote]); } @@ -282,8 +291,7 @@ namespace llarp constexpr size_t MaxPendingSendQueueSize = 8; bool - Router::SendToOrQueue(const llarp::RouterID &remote, - const llarp::ILinkMessage *msg) + Router::SendToOrQueue(const RouterID &remote, const ILinkMessage *msg) { for(const auto &link : inboundLinks) { @@ -303,7 +311,7 @@ namespace llarp } // no link available - // this will create an entry in the obmq if it's not already there + // this will create an entry in the outbound mq if it's not already there auto itr = outboundMessageQueue.find(remote); if(itr == outboundMessageQueue.end()) { @@ -324,10 +332,10 @@ namespace llarp } else { - llarp::LogWarn("tried to queue a message to ", remote, - " but the queue is full so we drop it like it's hawt"); + LogWarn("tried to queue a message to ", remote, + " but the queue is full so we drop it like it's hawt"); } - llarp::RouterContact remoteRC; + RouterContact remoteRC; // we don't have an open session to that router right now if(nodedb()->Get(remote, remoteRC)) { @@ -344,9 +352,8 @@ namespace llarp } void - Router::HandleDHTLookupForSendTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) + Router::HandleDHTLookupForSendTo(RouterID remote, + const std::vector< RouterContact > &results) { if(results.size()) { @@ -367,58 +374,58 @@ namespace llarp void Router::ForEachPeer( - std::function< void(const llarp::ILinkSession *, bool) > visit) const + std::function< void(const ILinkSession *, bool) > visit) const { for(const auto &link : outboundLinks) { link->ForEachSession( - [visit](const llarp::ILinkSession *peer) { visit(peer, true); }); + [visit](const ILinkSession *peer) { visit(peer, true); }); } for(const auto &link : inboundLinks) { link->ForEachSession( - [visit](const llarp::ILinkSession *peer) { visit(peer, false); }); + [visit](const ILinkSession *peer) { visit(peer, false); }); } } void - Router::ForEachPeer(std::function< void(llarp::ILinkSession *) > visit) + Router::ForEachPeer(std::function< void(ILinkSession *) > visit) { for(const auto &link : inboundLinks) { - link->ForEachSession([visit](llarp::ILinkSession *peer) { visit(peer); }); + link->ForEachSession([visit](ILinkSession *peer) { visit(peer); }); } for(const auto &link : inboundLinks) { - link->ForEachSession([visit](llarp::ILinkSession *peer) { visit(peer); }); + link->ForEachSession([visit](ILinkSession *peer) { visit(peer); }); } } void Router::try_connect(fs::path rcfile) { - llarp::RouterContact remote; + RouterContact remote; if(!remote.Read(rcfile.string().c_str())) { - llarp::LogError("failure to decode or verify of remote RC"); + LogError("failure to decode or verify of remote RC"); return; } if(remote.Verify(crypto(), Now())) { - llarp::LogDebug("verified signature"); + LogDebug("verified signature"); // store into filesystem if(!nodedb()->Insert(remote)) { - llarp::LogWarn("failed to store"); + LogWarn("failed to store"); } if(!TryConnectAsync(remote, 10)) { // or error? - llarp::LogWarn("session already made"); + LogWarn("session already made"); } } else - llarp::LogError(rcfile, " contains invalid RC"); + LogError(rcfile, " contains invalid RC"); } bool @@ -428,20 +435,20 @@ namespace llarp return false; if(usingSNSeed) return llarp_loadServiceNodeIdentityKey(crypto(), ident_keyfile, - identity); + _identity); else - return llarp_findOrCreateIdentity(crypto(), ident_keyfile, identity); + return llarp_findOrCreateIdentity(crypto(), ident_keyfile, _identity); } bool Router::EnsureEncryptionKey() { return llarp_findOrCreateEncryption(crypto(), encryption_keyfile, - this->encryption); + _encryption); } void - Router::AddInboundLink(std::unique_ptr< llarp::ILinkLayer > &link) + Router::AddInboundLink(std::unique_ptr< ILinkLayer > &link) { inboundLinks.insert(std::move(link)); } @@ -469,11 +476,11 @@ namespace llarp bool Router::SaveRC() { - llarp::LogDebug("verify RC signature"); + LogDebug("verify RC signature"); if(!_rc.Verify(crypto(), Now())) { rc().Dump< MAX_RC_SIZE >(); - llarp::LogError("RC is invalid, not saving"); + LogError("RC is invalid, not saving"); return false; } std::string fname = our_rc_file.string(); @@ -489,8 +496,8 @@ namespace llarp void Router::Close() { - llarp::LogInfo("closing router"); - llarp_ev_loop_stop(netloop); + LogInfo("closing router"); + llarp_ev_loop_stop(netloop()); inboundLinks.clear(); outboundLinks.clear(); } @@ -498,10 +505,10 @@ namespace llarp void Router::on_verify_client_rc(llarp_async_verify_rc *job) { - llarp::async_verify_context *ctx = - static_cast< llarp::async_verify_context * >(job->user); + async_verify_context *ctx = + static_cast< async_verify_context * >(job->user); auto router = ctx->router; - llarp::PubKey pk(job->rc.pubkey); + PubKey pk(job->rc.pubkey); router->FlushOutboundFor(pk, router->GetLinkWithSessionByPubkey(pk)); delete ctx; router->pendingVerifyRC.erase(pk); @@ -511,10 +518,10 @@ namespace llarp void Router::on_verify_server_rc(llarp_async_verify_rc *job) { - llarp::async_verify_context *ctx = - static_cast< llarp::async_verify_context * >(job->user); + async_verify_context *ctx = + static_cast< async_verify_context * >(job->user); auto router = ctx->router; - llarp::PubKey pk(job->rc.pubkey); + PubKey pk(job->rc.pubkey); if(!job->valid) { if(ctx->establish_job) @@ -530,14 +537,14 @@ namespace llarp } // we're valid, which means it's already been committed to the nodedb - llarp::LogDebug("rc verified and saved to nodedb"); + LogDebug("rc verified and saved to nodedb"); if(router->validRouters.count(pk)) { router->validRouters.erase(pk); } - llarp::RouterContact rc = job->rc; + RouterContact rc = job->rc; router->validRouters.emplace(pk, rc); @@ -545,7 +552,7 @@ namespace llarp router->dht()->impl.nodes->PutNode(rc); // mark success in profile - router->routerProfiling.MarkSuccess(pk); + router->routerProfiling().MarkSuccess(pk); // this was an outbound establish job if(ctx->establish_job) @@ -571,13 +578,14 @@ namespace llarp bool Router::ParseRoutingMessageBuffer(const llarp_buffer_t &buf, - routing::IMessageHandler *h, PathID_t rxid) + routing::IMessageHandler *h, + const PathID_t &rxid) { return inbound_routing_msg_parser.ParseMessageBuffer(buf, h, rxid, this); } bool - Router::ConnectionToRouterAllowed(const llarp::RouterID &router) const + Router::ConnectionToRouterAllowed(const RouterID &router) const { if(strictConnectPubkeys.size() && strictConnectPubkeys.count(router) == 0) return false; @@ -588,9 +596,8 @@ namespace llarp } void - Router::HandleDHTLookupForExplore( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) + Router::HandleDHTLookupForExplore(RouterID remote, + const std::vector< RouterContact > &results) { if(results.size() == 0) return; @@ -608,26 +615,25 @@ namespace llarp } void - Router::TryEstablishTo(const llarp::RouterID &remote) + Router::TryEstablishTo(const RouterID &remote) { if(!ConnectionToRouterAllowed(remote)) { - llarp::LogWarn("not connecting to ", remote, - " as it's not permitted by config"); + LogWarn("not connecting to ", remote, " as it's not permitted by config"); return; } - llarp::RouterContact rc; + RouterContact rc; if(nodedb()->Get(remote, rc)) { // try connecting async TryConnectAsync(rc, 5); } - else if(IsServiceNode() || !routerProfiling.IsBad(remote)) + else if(IsServiceNode() || !routerProfiling().IsBad(remote)) { if(dht()->impl.HasRouterLookup(remote)) return; - llarp::LogInfo("looking up router ", remote); + LogInfo("looking up router ", remote); // dht lookup as we don't know it dht()->impl.LookupRouter( remote, @@ -636,7 +642,7 @@ namespace llarp } else { - llarp::LogWarn("not connecting to ", remote, " as it's unreliable"); + LogWarn("not connecting to ", remote, " as it's unreliable"); } } @@ -652,13 +658,12 @@ namespace llarp void Router::HandleDHTLookupForTryEstablishTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) + RouterID remote, const std::vector< RouterContact > &results) { if(results.size() == 0) { if(!IsServiceNode()) - routerProfiling.MarkTimeout(remote); + routerProfiling().MarkTimeout(remote); } for(const auto &result : results) { @@ -679,28 +684,28 @@ namespace llarp bool Router::UpdateOurRC(bool rotateKeys) { - llarp::SecretKey nextOnionKey; - llarp::RouterContact nextRC = _rc; + SecretKey nextOnionKey; + RouterContact nextRC = _rc; if(rotateKeys) { crypto()->encryption_keygen(nextOnionKey); std::string f = encryption_keyfile.string(); if(nextOnionKey.SaveToFile(f.c_str())) { - nextRC.enckey = llarp::seckey_topublic(nextOnionKey); - encryption = nextOnionKey; + nextRC.enckey = seckey_topublic(nextOnionKey); + _encryption = nextOnionKey; } } nextRC.last_updated = Now(); - if(!nextRC.Sign(crypto(), identity)) + if(!nextRC.Sign(crypto(), identity())) return false; _rc = nextRC; // propagate RC by renegotiating sessions - ForEachPeer([](llarp::ILinkSession *s) { + ForEachPeer([](ILinkSession *s) { if(s->RenegotiateSession()) - llarp::LogInfo("renegotiated session"); + LogInfo("renegotiated session"); else - llarp::LogWarn("failed to renegotiate session"); + LogWarn("failed to renegotiate session"); }); // TODO: do this async @@ -746,7 +751,7 @@ namespace llarp llarp::LogError("failed to ensure keyfile ", transport_keyfile); return; } - if(server->Configure(netloop, key, af, proto)) + if(server->Configure(netloop(), key, af, proto)) { AddInboundLink(server); return; @@ -758,7 +763,7 @@ namespace llarp llarp::LogInfo("link ", key, " failed to configure IPv6, trying IPv4"); af = AF_INET; - if(server->Configure(netloop, key, af, proto)) + if(server->Configure(netloop(), key, af, proto)) { AddInboundLink(server); return; @@ -772,7 +777,7 @@ namespace llarp if(StrEq(key, "profiles")) { routerProfilesFile = val; - routerProfiling.Load(val); + routerProfiling().Load(val); llarp::LogInfo("setting profiles to ", routerProfilesFile); } else if(StrEq(key, "strict-connect")) @@ -981,7 +986,7 @@ namespace llarp return false; // store it in nodedb async - nodedb()->InsertAsync(newrc); + async_verify_RC(newrc, nullptr); // update dht if required if(dht()->impl.nodes->HasNode(dht::Key_t{newrc.pubkey})) { @@ -1010,14 +1015,14 @@ namespace llarp void Router::Tick() { - // llarp::LogDebug("tick router"); - auto now = llarp_ev_loop_time_now_ms(netloop); + // LogDebug("tick router"); + auto now = Now(); - if(_rc.ExpiresSoon(now, llarp::randint() % 10000)) + if(_rc.ExpiresSoon(now, randint() % 10000)) { - llarp::LogInfo("regenerating RC"); + LogInfo("regenerating RC"); if(!UpdateOurRC(false)) - llarp::LogError("Failed to update our RC"); + LogError("Failed to update our RC"); } if(IsServiceNode()) @@ -1025,7 +1030,7 @@ namespace llarp // only do this as service node // client endpoints do this on their own nodedb()->visit([&](const RouterContact &rc) -> bool { - if(rc.ExpiresSoon(now, llarp::randint() % 10000)) + if(rc.ExpiresSoon(now, randint() % 10000)) ServiceNodeLookupRouterWhenExpired(rc.pubkey); return true; }); @@ -1041,19 +1046,19 @@ namespace llarp { if(link) { - llarp::LogDebug("keepalive to ", itr->first); + LogDebug("keepalive to ", itr->first); link->KeepAliveSessionTo(itr->first); } else { - llarp::LogDebug("establish to ", itr->first); + LogDebug("establish to ", itr->first); TryEstablishTo(itr->first); } ++itr; } else { - llarp::LogInfo("commit to ", itr->first, " expired"); + LogInfo("commit to ", itr->first, " expired"); itr = m_PersistingSessions.erase(itr); } } @@ -1062,9 +1067,8 @@ namespace llarp size_t N = nodedb()->num_loaded(); if(N < minRequiredRouters) { - llarp::LogInfo("We need at least ", minRequiredRouters, - " service nodes to build paths but we have ", N, - " in nodedb"); + LogInfo("We need at least ", minRequiredRouters, + " service nodes to build paths but we have ", N, " in nodedb"); // TODO: only connect to random subset if(bootstrapRCList.size()) { @@ -1075,7 +1079,7 @@ namespace llarp } } else - llarp::LogError("we have no bootstrap nodes specified"); + LogError("we have no bootstrap nodes specified"); } if(inboundLinks.size() == 0) @@ -1087,33 +1091,32 @@ namespace llarp { ConnectToRandomRouters(minConnectedRouters); } - exitContext.Tick(now); + _exitContext.Tick(now); if(rpcCaller) rpcCaller->Tick(now); } bool - Router::Sign(llarp::Signature &sig, const llarp_buffer_t &buf) const + Router::Sign(Signature &sig, const llarp_buffer_t &buf) const { - return crypto()->sign(sig, identity, buf); + return crypto()->sign(sig, identity(), buf); } void - Router::SendTo(llarp::RouterID remote, const llarp::ILinkMessage *msg, - llarp::ILinkLayer *selected) + Router::SendTo(RouterID remote, const ILinkMessage *msg, ILinkLayer *selected) { llarp_buffer_t buf(linkmsg_buffer); if(!msg->BEncode(&buf)) { - llarp::LogWarn("failed to encode outbound message, buffer size left: ", - llarp_buffer_size_left(buf)); + LogWarn("failed to encode outbound message, buffer size left: ", + buf.size_left()); return; } // set size of message buf.sz = buf.cur - buf.base; buf.cur = buf.base; - llarp::LogDebug("send ", buf.sz, " bytes to ", remote); + LogDebug("send ", buf.sz, " bytes to ", remote); if(selected) { if(selected->SendTo(remote, buf)) @@ -1129,7 +1132,7 @@ namespace llarp if(link->SendTo(remote, buf)) return; } - llarp::LogWarn("message to ", remote, " was dropped"); + LogWarn("message to ", remote, " was dropped"); } void @@ -1139,16 +1142,16 @@ namespace llarp } void - Router::SessionClosed(llarp::RouterID remote) + Router::SessionClosed(RouterID remote) { __llarp_dht_remove_peer(dht(), remote.data()); // remove from valid routers if it's a valid router validRouters.erase(remote); - llarp::LogInfo("Session to ", remote, " fully closed"); + LogInfo("Session to ", remote, " fully closed"); } - llarp::ILinkLayer * - Router::GetLinkWithSessionByPubkey(const llarp::RouterID &pubkey) + ILinkLayer * + Router::GetLinkWithSessionByPubkey(const RouterID &pubkey) { for(const auto &link : outboundLinks) { @@ -1164,9 +1167,9 @@ namespace llarp } void - Router::FlushOutboundFor(llarp::RouterID remote, llarp::ILinkLayer *chosen) + Router::FlushOutboundFor(RouterID remote, ILinkLayer *chosen) { - llarp::LogDebug("Flush outbound for ", remote); + LogDebug("Flush outbound for ", remote); auto itr = outboundMessageQueue.find(remote); if(itr == outboundMessageQueue.end()) @@ -1184,8 +1187,8 @@ namespace llarp { llarp_buffer_t buf(itr->second.front()); if(!chosen->SendTo(remote, buf)) - llarp::LogWarn("failed to send outbound message to ", remote, " via ", - chosen->Name()); + LogWarn("failed to send outbound message to ", remote, " via ", + chosen->Name()); itr->second.pop(); } @@ -1193,20 +1196,20 @@ namespace llarp } void - Router::DiscardOutboundFor(const llarp::RouterID &remote) + Router::DiscardOutboundFor(const RouterID &remote) { outboundMessageQueue.erase(remote); } bool - Router::GetRandomConnectedRouter(llarp::RouterContact &result) const + Router::GetRandomConnectedRouter(RouterContact &result) const { auto sz = validRouters.size(); if(sz) { auto itr = validRouters.begin(); if(sz > 1) - std::advance(itr, llarp::randint() % sz); + std::advance(itr, randint() % sz); result = itr->second; return true; } @@ -1214,8 +1217,7 @@ namespace llarp } void - Router::async_verify_RC(const llarp::RouterContact &rc, - llarp::ILinkLayer *link) + Router::async_verify_RC(const RouterContact &rc, ILinkLayer *link) { if(pendingVerifyRC.count(rc.pubkey)) return; @@ -1223,15 +1225,15 @@ namespace llarp { if(lokinetRouters.find(rc.pubkey) == lokinetRouters.end()) { - llarp::LogInfo(rc.pubkey, " is NOT a valid service node, rejecting"); + LogInfo(rc.pubkey, " is NOT a valid service node, rejecting"); link->CloseSessionTo(rc.pubkey); return; } } - llarp_async_verify_rc *job = &pendingVerifyRC[rc.pubkey]; - llarp::async_verify_context *ctx = new llarp::async_verify_context(); - ctx->router = this; - ctx->establish_job = nullptr; + llarp_async_verify_rc *job = &pendingVerifyRC[rc.pubkey]; + async_verify_context *ctx = new async_verify_context(); + ctx->router = this; + ctx->establish_job = nullptr; auto itr = pendingEstablishJobs.find(rc.pubkey); if(itr != pendingEstablishJobs.end()) @@ -1254,6 +1256,17 @@ namespace llarp llarp_nodedb_async_verify(job); } + void + Router::SetRouterWhitelist(const std::vector< RouterID > &routers) + { + lokinetRouters.clear(); + for(const auto &router : routers) + lokinetRouters.emplace(router, + std::numeric_limits< llarp_time_t >::max()); + LogInfo("lokinet service node list now has ", lokinetRouters.size(), + " routers"); + } + bool Router::Run(struct llarp_nodedb *nodedb) { @@ -1267,49 +1280,49 @@ namespace llarp { rpcBindAddr = DefaultRPCBindAddr; } - rpcServer = std::make_unique< llarp::rpc::Server >(this); + rpcServer = std::make_unique< rpc::Server >(this); while(!rpcServer->Start(rpcBindAddr)) { - llarp::LogError("failed to bind jsonrpc to ", rpcBindAddr); + LogError("failed to bind jsonrpc to ", rpcBindAddr); #if defined(ANDROID) || defined(RPI) sleep(1); #else std::this_thread::sleep_for(std::chrono::seconds(1)); #endif } - llarp::LogInfo("Bound RPC server to ", rpcBindAddr); + LogInfo("Bound RPC server to ", rpcBindAddr); } if(whitelistRouters) { - rpcCaller = std::make_unique< llarp::rpc::Caller >(this); + rpcCaller = std::make_unique< rpc::Caller >(this); rpcCaller->SetBasicAuth(lokidRPCUser, lokidRPCPassword); while(!rpcCaller->Start(lokidRPCAddr)) { - llarp::LogError("failed to start jsonrpc caller to ", lokidRPCAddr); + LogError("failed to start jsonrpc caller to ", lokidRPCAddr); #if defined(ANDROID) || defined(RPI) sleep(1); #else std::this_thread::sleep_for(std::chrono::seconds(1)); #endif } - llarp::LogInfo("RPC Caller to ", lokidRPCAddr, " started"); + LogInfo("RPC Caller to ", lokidRPCAddr, " started"); } llarp_threadpool_start(tp); llarp_threadpool_start(disk); - routerProfiling.Load(routerProfilesFile.c_str()); + routerProfiling().Load(routerProfilesFile.c_str()); - llarp::Addr publicAddr(this->addrInfo); + Addr publicAddr(this->addrInfo); if(this->publicOverride) { - llarp::LogDebug("public address:port ", publicAddr); + LogDebug("public address:port ", publicAddr); } - llarp::LogInfo("You have ", inboundLinks.size(), " inbound links"); + LogInfo("You have ", inboundLinks.size(), " inbound links"); - llarp::AddressInfo ai; + AddressInfo ai; for(const auto &link : inboundLinks) { if(link->GetOurAddressInfo(ai)) @@ -1320,47 +1333,47 @@ namespace llarp ai.ip = *publicAddr.addr6(); ai.port = publicAddr.port(); } - if(llarp::IsBogon(ai.ip)) + if(IsBogon(ai.ip)) continue; _rc.addrs.push_back(ai); } } // set public encryption key - _rc.enckey = llarp::seckey_topublic(encryption); + _rc.enckey = seckey_topublic(encryption()); // set public signing key - _rc.pubkey = llarp::seckey_topublic(identity); + _rc.pubkey = seckey_topublic(identity()); if(ExitEnabled()) { - llarp::nuint32_t a = publicAddr.xtonl(); + nuint32_t a = publicAddr.xtonl(); _rc.exits.emplace_back(_rc.pubkey, a); - llarp::LogInfo( - "Neato tehl33toh, You are a freaking exit relay. w00t!!!!! your " + LogInfo( + "Neato teh l33toh, You are a freaking exit relay. w00t!!!!! your " "exit " "is advertised as exiting at ", a); } - llarp::LogInfo("Signing rc..."); - if(!_rc.Sign(crypto(), identity)) + LogInfo("Signing rc..."); + if(!_rc.Sign(crypto(), identity())) { - llarp::LogError("failed to sign rc"); + LogError("failed to sign rc"); return false; } if(!SaveRC()) { - llarp::LogError("failed to save RC"); + LogError("failed to save RC"); return false; } - llarp::LogInfo("have ", nodedb->num_loaded(), " routers"); + LogInfo("have ", nodedb->num_loaded(), " routers"); - llarp::LogInfo("starting outbound ", outboundLinks.size(), " links"); + LogInfo("starting outbound ", outboundLinks.size(), " links"); for(const auto &link : outboundLinks) { if(!link->Start(_logic)) { - llarp::LogWarn("outbound link '", link->Name(), "' failed to start"); + LogWarn("outbound link '", link->Name(), "' failed to start"); return false; } } @@ -1372,11 +1385,11 @@ namespace llarp { if(link->Start(_logic)) { - llarp::LogDebug("Link ", link->Name(), " started"); + LogDebug("Link ", link->Name(), " started"); IBLinksStarted++; } else - llarp::LogWarn("Link ", link->Name(), " failed to start"); + LogWarn("Link ", link->Name(), " failed to start"); } if(IBLinksStarted > 0) @@ -1384,23 +1397,23 @@ namespace llarp // initialize as service node if(!InitServiceNode()) { - llarp::LogError("Failed to initialize service node"); + LogError("Failed to initialize service node"); return false; } - llarp::RouterID us = pubkey(); - llarp::LogInfo("initalized service node: ", us); + RouterID us = pubkey(); + LogInfo("initalized service node: ", us); } else { // we are a client // regenerate keys and resign rc before everything else - crypto()->identity_keygen(identity); - crypto()->encryption_keygen(encryption); - _rc.pubkey = llarp::seckey_topublic(identity); - _rc.enckey = llarp::seckey_topublic(encryption); - if(!_rc.Sign(crypto(), identity)) + crypto()->identity_keygen(_identity); + crypto()->encryption_keygen(_encryption); + _rc.pubkey = seckey_topublic(identity()); + _rc.enckey = seckey_topublic(encryption()); + if(!_rc.Sign(crypto(), identity())) { - llarp::LogError("failed to regenerate keys and sign RC"); + LogError("failed to regenerate keys and sign RC"); return false; } @@ -1408,19 +1421,19 @@ namespace llarp if(this->ShouldCreateDefaultHiddenService()) { // generate default hidden service - llarp::LogInfo("setting up default network endpoint"); + LogInfo("setting up default network endpoint"); if(!CreateDefaultHiddenService()) { - llarp::LogError("failed to set up default network endpoint"); + LogError("failed to set up default network endpoint"); return false; } } } - llarp::LogInfo("starting hidden service context..."); + LogInfo("starting hidden service context..."); if(!hiddenServiceContext.StartAll()) { - llarp::LogError("Failed to start hidden service context"); + LogError("Failed to start hidden service context"); return false; } llarp_dht_context_start(dht(), pubkey()); @@ -1447,7 +1460,7 @@ namespace llarp void Router::StopLinks() { - llarp::LogInfo("stopping links"); + LogInfo("stopping links"); for(const auto &link : outboundLinks) link->Stop(); for(const auto &link : inboundLinks) @@ -1475,13 +1488,13 @@ namespace llarp { enabledOption = itr->second; } - llarp::LogDebug("IfName: ", defaultIfName, " IfAddr: ", defaultIfAddr, - " Enabled: ", enabledOption); - // llarp::LogInfo("IfAddr: ", itr->second); - // llarp::LogInfo("IfName: ", itr->second); + LogDebug("IfName: ", defaultIfName, " IfAddr: ", defaultIfAddr, + " Enabled: ", enabledOption); + // LogInfo("IfAddr: ", itr->second); + // LogInfo("IfName: ", itr->second); if(enabledOption == "false") { - llarp::LogInfo("Disabling default hidden service"); + LogInfo("Disabling default hidden service"); return false; } else if(enabledOption == "auto") @@ -1490,7 +1503,7 @@ namespace llarp // no if we have a endpoints if(hiddenServiceContext.hasEndpoints()) { - llarp::LogInfo("Auto mode detected and we have endpoints"); + LogInfo("Auto mode detected and we have endpoints"); netConfig.emplace("enabled", "false"); return false; } @@ -1503,13 +1516,13 @@ namespace llarp { // we don't have any endpoints, auto configure settings // set a default IP range - defaultIfAddr = llarp::findFreePrivateRange(); + defaultIfAddr = findFreePrivateRange(); if(defaultIfAddr == "") { - llarp::LogError( - "Could not find any free lokitun interface names, - can't auto set up " "default HS context for client"); defaultIfAddr = - "no"; netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); + LogError( + "Could not find any free lokitun interface names, can't + auto set up " "default HS context for client"); defaultIfAddr = "no"; + netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); return false; } netConfig.emplace(std::make_pair("defaultIfAddr", defaultIfAddr)); @@ -1517,10 +1530,10 @@ namespace llarp if(defaultIfName == "auto") { // pick an ifName - defaultIfName = llarp::findFreeLokiTunIfName(); + defaultIfName = findFreeLokiTunIfName(); if(defaultIfName == "") { - llarp::LogError( + LogError( "Could not find any free private ip ranges, can't auto set up " "default HS context for client"); defaultIfName = "no"; netConfig.emplace(std::make_pair("defaultIfName", defaultIfName)); @@ -1541,16 +1554,16 @@ namespace llarp return; _stopping.store(true); - llarp::LogInfo("stopping router"); + LogInfo("stopping router"); hiddenServiceContext.StopAll(); - exitContext.Stop(); + _exitContext.Stop(); if(rpcServer) rpcServer->Stop(); _logic->call_later({200, this, &RouterAfterStopIssued}); } bool - Router::HasSessionTo(const llarp::RouterID &remote) const + Router::HasSessionTo(const RouterID &remote) const { return validRouters.find(remote) != validRouters.end(); } @@ -1561,34 +1574,33 @@ namespace llarp int wanted = want; Router *self = this; - self->nodedb()->visit( - [self, &want](const llarp::RouterContact &other) -> bool { - // check if we really want to - if(other.ExpiresSoon(self->Now(), 30000)) - return want > 0; - if(!self->ConnectionToRouterAllowed(other.pubkey)) - return want > 0; - if(llarp::randint() % 2 == 0 - && !(self->HasSessionTo(other.pubkey) - || self->HasPendingConnectJob(other.pubkey))) - { - self->TryConnectAsync(other, 5); - --want; - } - return want > 0; - }); + self->nodedb()->visit([self, &want](const RouterContact &other) -> bool { + // check if we really want to + if(other.ExpiresSoon(self->Now(), 30000)) + return want > 0; + if(!self->ConnectionToRouterAllowed(other.pubkey)) + return want > 0; + if(randint() % 2 == 0 + && !(self->HasSessionTo(other.pubkey) + || self->HasPendingConnectJob(other.pubkey))) + { + self->TryConnectAsync(other, 5); + --want; + } + return want > 0; + }); if(wanted != want) - llarp::LogInfo("connecting to ", abs(want - wanted), " out of ", wanted, - " random routers"); + LogInfo("connecting to ", abs(want - wanted), " out of ", wanted, + " random routers"); } bool Router::InitServiceNode() { - llarp::LogInfo("accepting transit traffic"); + LogInfo("accepting transit traffic"); paths.AllowTransit(); llarp_dht_allow_transit(dht()); - return exitContext.AddExitEndpoint("default-connectivity", netConfig); + return _exitContext.AddExitEndpoint("default-connectivity", netConfig); } /// validate a new configuration against an already made and running @@ -1602,8 +1614,8 @@ namespace llarp { if(!OnEntry(section, key, val)) { - llarp::LogError("invalid entry in section [", section, "]: '", key, - "'='", val, "'"); + LogError("invalid entry in section [", section, "]: '", key, "'='", + val, "'"); valid = false; } } @@ -1660,10 +1672,8 @@ namespace llarp if(outboundLinks.size() > 0) return true; - static std::list< - std::function< std::unique_ptr< ILinkLayer >(llarp::Router *) > > - linkFactories = {llarp::utp::NewServerFromRouter, - llarp::iwp::NewServerFromRouter}; + static std::list< std::function< std::unique_ptr< ILinkLayer >(Router *) > > + linkFactories = {utp::NewServerFromRouter, iwp::NewServerFromRouter}; for(const auto &factory : linkFactories) { @@ -1672,7 +1682,7 @@ namespace llarp continue; if(!link->EnsureKeys(transport_keyfile.string().c_str())) { - llarp::LogError("failed to load ", transport_keyfile); + LogError("failed to load ", transport_keyfile); continue; } @@ -1680,7 +1690,7 @@ namespace llarp for(auto af : afs) { - if(!link->Configure(netloop, "*", af, m_OutboundPort)) + if(!link->Configure(netloop(), "*", af, m_OutboundPort)) continue; outboundLinks.insert(std::move(link)); break; @@ -1719,7 +1729,7 @@ namespace llarp } bool - Router::HasPendingConnectJob(const llarp::RouterID &remote) + Router::HasPendingConnectJob(const RouterID &remote) { return pendingEstablishJobs.find(remote) != pendingEstablishJobs.end(); } @@ -1727,13 +1737,13 @@ namespace llarp bool Router::LoadHiddenServiceConfig(const char *fname) { - llarp::LogDebug("opening hidden service config ", fname); - llarp::service::Config conf; + LogDebug("opening hidden service config ", fname); + service::Config conf; if(!conf.Load(fname)) return false; for(const auto &config : conf.services) { - llarp::service::Config::section_t filteredConfig; + service::Config::section_t filteredConfig; mergeHiddenServiceConfig(config.second, filteredConfig.second); filteredConfig.first = config.first; if(!hiddenServiceContext.AddEndpoint(filteredConfig)) diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 7b3e3b068..f84072ce2 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -1,6 +1,8 @@ #ifndef LLARP_ROUTER_HPP #define LLARP_ROUTER_HPP +#include + #include #include #include @@ -66,55 +68,7 @@ namespace llarp } }; - struct AbstractRouter - { - virtual ~AbstractRouter() = 0; - - virtual Logic * - logic() const = 0; - - virtual llarp_dht_context * - dht() const = 0; - - virtual Crypto * - crypto() const = 0; - - virtual llarp_nodedb * - nodedb() const = 0; - - virtual const path::PathContext & - pathContext() const = 0; - - virtual path::PathContext & - pathContext() = 0; - - virtual const llarp::RouterContact & - rc() const = 0; - - virtual const byte_t * - pubkey() const = 0; - - virtual llarp_time_t - Now() const = 0; - - virtual bool - SendToOrQueue(const llarp::RouterID &remote, - const llarp::ILinkMessage *msg) = 0; - - virtual void - PersistSessionUntil(const llarp::RouterID &remote, llarp_time_t until) = 0; - - virtual bool - ParseRoutingMessageBuffer(const llarp_buffer_t &buf, - routing::IMessageHandler *h, PathID_t rxid) = 0; - - virtual void - HandleDHTLookupForExplore( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) = 0; - }; - - struct Router final : public AbstractRouter, public util::IStateful + struct Router final : public AbstractRouter { bool ready; // transient iwp encryption key @@ -133,7 +87,7 @@ namespace llarp fs::path our_rc_file = "rc.signed"; // our router contact - llarp::RouterContact _rc; + RouterContact _rc; /// are we using the lokid service node seed ? bool usingSNSeed = false; @@ -180,31 +134,76 @@ namespace llarp return paths; } - const llarp::RouterContact & + const RouterContact & rc() const override { return _rc; } + void + SetRouterWhitelist(const std::vector & routers) override; + + exit::Context & + exitContext() override + { + return _exitContext; + } + + const SecretKey & + identity() const override + { + return _identity; + } + + const SecretKey & + encryption() const override + { + return _encryption; + } + + Profiling & + routerProfiling() override + { + return _routerProfiling; + } + + llarp_ev_loop * + netloop() const override + { + return _netloop; + } + + llarp_threadpool * + threadpool() override + { + return tp; + } + + llarp_threadpool * + diskworker() override + { + return disk; + } + // our ipv4 public setting bool publicOverride = false; struct sockaddr_in ip4addr; - llarp::AddressInfo addrInfo; + AddressInfo addrInfo; - llarp_ev_loop *netloop; + llarp_ev_loop *_netloop; llarp_threadpool *tp; Logic *_logic; std::unique_ptr< Crypto > _crypto; path::PathContext paths; - exit::Context exitContext; - SecretKey identity; - SecretKey encryption; + exit::Context _exitContext; + SecretKey _identity; + SecretKey _encryption; llarp_threadpool *disk; llarp_dht_context *_dht = nullptr; llarp_nodedb *_nodedb; bool - Sign(Signature &sig, const llarp_buffer_t &buf) const; + Sign(Signature &sig, const llarp_buffer_t &buf) const override; // buffer for serializing link messages std::array< byte_t, MAX_LINK_MSG_SIZE > linkmsg_buffer; @@ -223,10 +222,10 @@ namespace llarp uint32_t ticker_job_id = 0; - llarp::InboundMessageParser inbound_link_msg_parser; - llarp::routing::InboundMessageParser inbound_routing_msg_parser; + InboundMessageParser inbound_link_msg_parser; + routing::InboundMessageParser inbound_routing_msg_parser; - llarp::service::Context hiddenServiceContext; + service::Context hiddenServiceContext; using NetConfig_t = std::unordered_multimap< std::string, std::string >; @@ -235,10 +234,10 @@ namespace llarp /// identity keys whitelist of routers we will connect to directly (not for /// service nodes) - std::set< llarp::RouterID > strictConnectPubkeys; + std::set< RouterID > strictConnectPubkeys; /// bootstrap RCs - std::list< llarp::RouterContact > bootstrapRCList; + std::list< RouterContact > bootstrapRCList; bool ExitEnabled() const @@ -247,7 +246,7 @@ namespace llarp auto itr = netConfig.find("exit"); if(itr == netConfig.end()) return false; - return llarp::IsTrueValue(itr->second.c_str()); + return IsTrueValue(itr->second.c_str()); } bool @@ -258,76 +257,70 @@ namespace llarp const std::string DefaultRPCBindAddr = "127.0.0.1:1190"; bool enableRPCServer = true; - std::unique_ptr< llarp::rpc::Server > rpcServer; + std::unique_ptr< rpc::Server > rpcServer; std::string rpcBindAddr = DefaultRPCBindAddr; /// lokid caller const std::string DefaultLokidRPCAddr = "127.0.0.1:22023"; - std::unique_ptr< llarp::rpc::Caller > rpcCaller; + std::unique_ptr< rpc::Caller > rpcCaller; std::string lokidRPCAddr = DefaultLokidRPCAddr; std::string lokidRPCUser = ""; std::string lokidRPCPassword = ""; - std::set< std::unique_ptr< llarp::ILinkLayer >, - CompareLinks< llarp::ILinkLayer > > + std::set< std::unique_ptr< ILinkLayer >, CompareLinks< ILinkLayer > > outboundLinks; - std::set< std::unique_ptr< llarp::ILinkLayer >, - CompareLinks< llarp::ILinkLayer > > + std::set< std::unique_ptr< ILinkLayer >, CompareLinks< ILinkLayer > > inboundLinks; - llarp::Profiling routerProfiling; + Profiling _routerProfiling; std::string routerProfilesFile = "profiles.dat"; using MessageQueue = std::queue< std::vector< byte_t > >; /// outbound message queue - std::unordered_map< llarp::RouterID, MessageQueue, llarp::RouterID::Hash > + std::unordered_map< RouterID, MessageQueue, RouterID::Hash > outboundMessageQueue; /// loki verified routers - std::unordered_map< llarp::RouterID, llarp::RouterContact, - llarp::RouterID::Hash > - validRouters; + std::unordered_map< RouterID, RouterContact, RouterID::Hash > validRouters; // pending establishing session with routers - std::unordered_map< llarp::RouterID, std::unique_ptr< TryConnectJob >, - llarp::RouterID::Hash > + std::unordered_map< RouterID, std::unique_ptr< TryConnectJob >, + RouterID::Hash > pendingEstablishJobs; // pending RCs to be verified by pubkey - std::unordered_map< llarp::RouterID, llarp_async_verify_rc, - llarp::RouterID::Hash > + std::unordered_map< RouterID, llarp_async_verify_rc, RouterID::Hash > pendingVerifyRC; // sessions to persist -> timestamp to end persist at - std::unordered_map< llarp::RouterID, llarp_time_t, llarp::RouterID::Hash > + std::unordered_map< RouterID, llarp_time_t, RouterID::Hash > m_PersistingSessions; // lokinet routers from lokid, maps pubkey to when we think it will expire, // set to max value right now - std::unordered_map< llarp::RouterID, llarp_time_t, llarp::PubKey::Hash > - lokinetRouters; + std::unordered_map< RouterID, llarp_time_t, PubKey::Hash > lokinetRouters; - Router(struct llarp_threadpool *tp, struct llarp_ev_loop *netloop, - llarp::Logic *logic); + Router(struct llarp_threadpool *tp, struct llarp_ev_loop *__netloop, + Logic *logic); ~Router(); void - OnSessionEstablished(llarp::RouterContact rc); + OnSessionEstablished(RouterContact rc) override; bool - HandleRecvLinkMessageBuffer(llarp::ILinkSession *from, - const llarp_buffer_t &msg); + HandleRecvLinkMessageBuffer(ILinkSession *from, + const llarp_buffer_t &msg) override; void - AddInboundLink(std::unique_ptr< llarp::ILinkLayer > &link); + AddInboundLink(std::unique_ptr< ILinkLayer > &link); bool InitOutboundLinks(); bool - GetRandomGoodRouter(RouterID &r); + GetRandomGoodRouter(RouterID &r) override; /// initialize us as a service node /// return true on success @@ -345,7 +338,7 @@ namespace llarp LoadHiddenServiceConfig(const char *fname); bool - AddHiddenService(const llarp::service::Config::section_t &config); + AddHiddenService(const service::Config::section_t &config); bool Configure(Config *conf); @@ -365,8 +358,7 @@ namespace llarp StopLinks(); void - PersistSessionUntil(const llarp::RouterID &remote, - llarp_time_t until) override; + PersistSessionUntil(const RouterID &remote, llarp_time_t until) override; bool EnsureIdentity(); @@ -375,7 +367,7 @@ namespace llarp EnsureEncryptionKey(); bool - ConnectionToRouterAllowed(const llarp::RouterID &router) const; + ConnectionToRouterAllowed(const RouterID &router) const; bool SaveRC(); @@ -383,14 +375,14 @@ namespace llarp const byte_t * pubkey() const override { - return llarp::seckey_topublic(identity); + return seckey_topublic(_identity); } void - OnConnectTimeout(ILinkSession *session); + OnConnectTimeout(ILinkSession *session) override; bool - HasPendingConnectJob(const llarp::RouterID &remote); + HasPendingConnectJob(const RouterID &remote); void try_connect(fs::path rcfile); @@ -411,49 +403,44 @@ namespace llarp /// NOT threadsafe /// MUST be called in the logic thread bool - SendToOrQueue(const llarp::RouterID &remote, - const llarp::ILinkMessage *msg) override; + SendToOrQueue(const RouterID &remote, const ILinkMessage *msg) override; /// sendto or drop void - SendTo(llarp::RouterID remote, const llarp::ILinkMessage *msg, - llarp::ILinkLayer *chosen); + SendTo(RouterID remote, const ILinkMessage *msg, ILinkLayer *chosen); /// manually flush outbound message queue for just 1 router void - FlushOutboundFor(llarp::RouterID remote, - llarp::ILinkLayer *chosen = nullptr); + FlushOutboundFor(RouterID remote, ILinkLayer *chosen = nullptr); /// manually discard all pending messages to remote router void - DiscardOutboundFor(const llarp::RouterID &remote); + DiscardOutboundFor(const RouterID &remote); /// try establishing a session to a remote router void - TryEstablishTo(const llarp::RouterID &remote); + TryEstablishTo(const RouterID &remote); /// lookup a router by pubkey when it expires when we are a service node void - ServiceNodeLookupRouterWhenExpired(llarp::RouterID remote); + ServiceNodeLookupRouterWhenExpired(RouterID remote); void HandleDHTLookupForExplore( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results) override; + RouterID remote, const std::vector< RouterContact > &results) override; void - ForEachPeer( - std::function< void(const llarp::ILinkSession *, bool) > visit) const; + ForEachPeer(std::function< void(const ILinkSession *, bool) > visit) const override; void - ForEachPeer(std::function< void(llarp::ILinkSession *) > visit); + ForEachPeer(std::function< void(ILinkSession *) > visit); /// check if newRc matches oldRC and update local rc for this remote contact /// if valid /// returns true on valid and updated /// returns false otherwise bool - CheckRenegotiateValid(RouterContact newRc, RouterContact oldRC); + CheckRenegotiateValid(RouterContact newRc, RouterContact oldRC) override; /// flush outbound message queue void @@ -461,7 +448,7 @@ namespace llarp /// called by link when a remote session has no more sessions open void - SessionClosed(RouterID remote); + SessionClosed(RouterID remote) override; /// call internal router ticker void @@ -471,15 +458,15 @@ namespace llarp llarp_time_t Now() const override { - return llarp_ev_loop_time_now_ms(netloop); + return llarp_ev_loop_time_now_ms(_netloop); } /// schedule ticker to call i ms from now void ScheduleTicker(uint64_t i = 1000); - llarp::ILinkLayer * - GetLinkWithSessionByPubkey(const llarp::RouterID &remote); + ILinkLayer * + GetLinkWithSessionByPubkey(const RouterID &remote); /// parse a routing message in a buffer and handle it with a handler if /// successful parsing return true on parse and handle success otherwise @@ -487,35 +474,33 @@ namespace llarp bool ParseRoutingMessageBuffer(const llarp_buffer_t &buf, routing::IMessageHandler *h, - PathID_t rxid) override; + const PathID_t &rxid) override; void ConnectToRandomRouters(int N); size_t - NumberOfConnectedRouters() const; + NumberOfConnectedRouters() const override; bool - TryConnectAsync(llarp::RouterContact rc, uint16_t tries); + TryConnectAsync(RouterContact rc, uint16_t tries); bool - GetRandomConnectedRouter(llarp::RouterContact &result) const; + GetRandomConnectedRouter(RouterContact &result) const override; void - async_verify_RC(const llarp::RouterContact &rc, llarp::ILinkLayer *link); + async_verify_RC(const RouterContact &rc, ILinkLayer *link); void - HandleDHTLookupForSendTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results); + HandleDHTLookupForSendTo(RouterID remote, + const std::vector< RouterContact > &results); bool - HasSessionTo(const llarp::RouterID &remote) const; + HasSessionTo(const RouterID &remote) const; void HandleDHTLookupForTryEstablishTo( - llarp::RouterID remote, - const std::vector< llarp::RouterContact > &results); + RouterID remote, const std::vector< RouterContact > &results); static void on_verify_client_rc(llarp_async_verify_rc *context); diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 592f404b6..60bc626ed 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -176,7 +176,7 @@ namespace llarp if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf)) return false; - if(llarp_buffer_eq(key, "n")) + if(key == "n") { llarp_buffer_t strbuf; if(!bencode_read_string(buf, &strbuf)) diff --git a/llarp/routing/dht_message.cpp b/llarp/routing/dht_message.cpp index dbaa9b8e4..7e0cc5eb3 100644 --- a/llarp/routing/dht_message.cpp +++ b/llarp/routing/dht_message.cpp @@ -1,6 +1,7 @@ #include -#include +#include +#include namespace llarp { @@ -15,15 +16,15 @@ namespace llarp { llarp::dht::Key_t from; from.Zero(); - if(llarp_buffer_eq(key, "M")) + if(key == "M") { return llarp::dht::DecodeMesssageList(from, val, M, true); } - else if(llarp_buffer_eq(key, "S")) + else if(key == "S") { return bencode_read_integer(val, &S); } - else if(llarp_buffer_eq(key, "V")) + else if(key == "V") { return bencode_read_integer(val, &V); } @@ -49,7 +50,7 @@ namespace llarp } bool - DHTMessage::HandleMessage(IMessageHandler* h, llarp::Router* r) const + DHTMessage::HandleMessage(IMessageHandler* h, AbstractRouter* r) const { // set source as us llarp::dht::Key_t us{r->pubkey()}; diff --git a/llarp/routing/handler.hpp b/llarp/routing/handler.hpp index 8133b0380..354b10831 100644 --- a/llarp/routing/handler.hpp +++ b/llarp/routing/handler.hpp @@ -1,68 +1,85 @@ #ifndef LLARP_ROUTING_HANDLER_HPP #define LLARP_ROUTING_HANDLER_HPP -#include -#include -#include -#include -#include -#include - namespace llarp { + struct AbstractRouter; + + namespace dht + { + struct IMessage; + } + + namespace service + { + struct ProtocolFrame; + } + namespace routing { struct DataDiscardMessage; + struct GrantExitMessage; + struct ObtainExitMessage; + struct RejectExitMessage; + struct TransferTrafficMessage; + struct UpdateExitMessage; + struct UpdateExitVerifyMessage; + struct CloseExitMessage; + struct PathTransferMessage; + struct PathConfirmMessage; + struct PathLatencyMessage; // handles messages on the routing level struct IMessageHandler { virtual bool HandleObtainExitMessage(const ObtainExitMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool - HandleGrantExitMessage(const GrantExitMessage *msg, llarp::Router *r) = 0; + HandleGrantExitMessage(const GrantExitMessage *msg, + AbstractRouter *r) = 0; virtual bool HandleRejectExitMessage(const RejectExitMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleTransferTrafficMessage(const TransferTrafficMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleUpdateExitMessage(const UpdateExitMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleUpdateExitVerifyMessage(const UpdateExitVerifyMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool - HandleCloseExitMessage(const CloseExitMessage *msg, llarp::Router *r) = 0; + HandleCloseExitMessage(const CloseExitMessage *msg, + AbstractRouter *r) = 0; virtual bool HandleDataDiscardMessage(const DataDiscardMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandlePathTransferMessage(const PathTransferMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandleHiddenServiceFrame(const service::ProtocolFrame *msg) = 0; virtual bool HandlePathConfirmMessage(const PathConfirmMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool HandlePathLatencyMessage(const PathLatencyMessage *msg, - llarp::Router *r) = 0; + AbstractRouter *r) = 0; virtual bool - HandleDHTMessage(const llarp::dht::IMessage *msg, llarp::Router *r) = 0; + HandleDHTMessage(const dht::IMessage *msg, AbstractRouter *r) = 0; }; } // namespace routing } // namespace llarp diff --git a/llarp/routing/message.hpp b/llarp/routing/message.hpp index 9264a0fa6..c5fe40aa2 100644 --- a/llarp/routing/message.hpp +++ b/llarp/routing/message.hpp @@ -7,14 +7,14 @@ namespace llarp { - struct Router; + struct AbstractRouter; namespace routing { struct IMessageHandler; struct IMessage : public llarp::IBEncodeMessage { - llarp::PathID_t from; + PathID_t from; uint64_t S; IMessage() : llarp::IBEncodeMessage(), S(0) @@ -24,7 +24,7 @@ namespace llarp virtual ~IMessage(){}; virtual bool - HandleMessage(IMessageHandler* h, llarp::Router* r) const = 0; + HandleMessage(IMessageHandler* h, AbstractRouter* r) const = 0; virtual void Clear() = 0; diff --git a/llarp/routing/message_parser.cpp b/llarp/routing/message_parser.cpp index 31e9e18fa..ca56192ff 100644 --- a/llarp/routing/message_parser.cpp +++ b/llarp/routing/message_parser.cpp @@ -1,16 +1,47 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include #include namespace llarp { namespace routing { + struct InboundMessageParser::MessageHolder + { + DataDiscardMessage D; + PathLatencyMessage L; + DHTMessage M; + PathConfirmMessage P; + PathTransferMessage T; + service::ProtocolFrame H; + TransferTrafficMessage I; + GrantExitMessage G; + RejectExitMessage J; + ObtainExitMessage O; + UpdateExitMessage U; + CloseExitMessage C; + }; + InboundMessageParser::InboundMessageParser() + : firstKey(false) + , key('\0') + , msg(nullptr) + , m_Holder(std::make_unique< MessageHolder >()) { reader.user = this; reader.on_key = &OnKey; - firstKey = false; + } + + InboundMessageParser::~InboundMessageParser() + { } bool @@ -29,7 +60,7 @@ namespace llarp if(self->firstKey) { llarp_buffer_t strbuf; - if(!llarp_buffer_eq(*key, "A")) + if(!(*key == "A")) return false; if(!bencode_read_string(r->buffer, &strbuf)) return false; @@ -39,40 +70,40 @@ namespace llarp switch(self->key) { case 'D': - self->msg = &self->m_Holder.D; + self->msg = &self->m_Holder->D; break; case 'L': - self->msg = &self->m_Holder.L; + self->msg = &self->m_Holder->L; break; case 'M': - self->msg = &self->m_Holder.M; + self->msg = &self->m_Holder->M; break; case 'P': - self->msg = &self->m_Holder.P; + self->msg = &self->m_Holder->P; break; case 'T': - self->msg = &self->m_Holder.T; + self->msg = &self->m_Holder->T; break; case 'H': - self->msg = &self->m_Holder.H; + self->msg = &self->m_Holder->H; break; case 'I': - self->msg = &self->m_Holder.I; + self->msg = &self->m_Holder->I; break; case 'G': - self->msg = &self->m_Holder.G; + self->msg = &self->m_Holder->G; break; case 'J': - self->msg = &self->m_Holder.J; + self->msg = &self->m_Holder->J; break; case 'O': - self->msg = &self->m_Holder.O; + self->msg = &self->m_Holder->O; break; case 'U': - self->msg = &self->m_Holder.U; + self->msg = &self->m_Holder->U; break; case 'C': - self->msg = &self->m_Holder.C; + self->msg = &self->m_Holder->C; break; default: llarp::LogError("invalid routing message id: ", *strbuf.cur); @@ -90,7 +121,7 @@ namespace llarp InboundMessageParser::ParseMessageBuffer(const llarp_buffer_t& buf, IMessageHandler* h, const PathID_t& from, - llarp::Router* r) + AbstractRouter* r) { bool result = false; msg = nullptr; diff --git a/llarp/routing/message_parser.hpp b/llarp/routing/message_parser.hpp index d59391f78..3628ed01e 100644 --- a/llarp/routing/message_parser.hpp +++ b/llarp/routing/message_parser.hpp @@ -1,56 +1,42 @@ #ifndef LLARP_ROUTING_MESSAGE_PARSER_HPP #define LLARP_ROUTING_MESSAGE_PARSER_HPP -#include -#include -#include -#include -#include -#include -#include +#include #include +#include + namespace llarp { - struct Router; + struct AbstractRouter; + struct PathID_t; namespace routing { + struct IMessage; struct IMessageHandler; struct InboundMessageParser { InboundMessageParser(); + ~InboundMessageParser(); bool ParseMessageBuffer(const llarp_buffer_t& buf, IMessageHandler* handler, - const PathID_t& from, llarp::Router* r); + const PathID_t& from, AbstractRouter* r); private: static bool OnKey(dict_reader* r, llarp_buffer_t* key); + bool firstKey; char key; dict_reader reader; - struct MessageHolder - { - DataDiscardMessage D; - PathLatencyMessage L; - DHTMessage M; - PathConfirmMessage P; - PathTransferMessage T; - service::ProtocolFrame H; - TransferTrafficMessage I; - GrantExitMessage G; - RejectExitMessage J; - ObtainExitMessage O; - UpdateExitMessage U; - CloseExitMessage C; - }; - - IMessage* msg = nullptr; - MessageHolder m_Holder; + struct MessageHolder; + + IMessage* msg; + std::unique_ptr< MessageHolder > m_Holder; }; } // namespace routing } // namespace llarp diff --git a/llarp/routing/path_confirm.cpp b/llarp/routing/path_confirm.cpp index 388a13646..dec6ff447 100644 --- a/llarp/routing/path_confirm.cpp +++ b/llarp/routing/path_confirm.cpp @@ -53,7 +53,7 @@ namespace llarp bool PathConfirmMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h && h->HandlePathConfirmMessage(this, r); } diff --git a/llarp/routing/path_latency.cpp b/llarp/routing/path_latency.cpp index 845690369..e3ecbeabd 100644 --- a/llarp/routing/path_latency.cpp +++ b/llarp/routing/path_latency.cpp @@ -47,7 +47,7 @@ namespace llarp bool PathLatencyMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h && h->HandlePathLatencyMessage(this, r); } diff --git a/llarp/routing/path_transfer.cpp b/llarp/routing/path_transfer.cpp index b9c265e5e..6e360f533 100644 --- a/llarp/routing/path_transfer.cpp +++ b/llarp/routing/path_transfer.cpp @@ -59,7 +59,7 @@ namespace llarp bool PathTransferMessage::HandleMessage(IMessageHandler* h, - llarp::Router* r) const + AbstractRouter* r) const { return h->HandlePathTransferMessage(this, r); } diff --git a/llarp/rpc/rpc.cpp b/llarp/rpc/rpc.cpp index 04accd238..19f29702e 100644 --- a/llarp/rpc/rpc.cpp +++ b/llarp/rpc/rpc.cpp @@ -1,6 +1,9 @@ #include -#include +#include +#include +#include +#include #ifdef USE_ABYSS #include @@ -26,7 +29,7 @@ namespace llarp } virtual bool - HandleJSONResult(const ::abyss::json::Value& val) = 0; + HandleJSONResult(const json::Value& val) = 0; bool HandleResponse(::abyss::http::RPC_Response response) @@ -66,7 +69,7 @@ namespace llarp } bool - HandleJSONResult(const ::abyss::json::Value& result) override + HandleJSONResult(const json::Value& result) override { PubkeyList_t keys; if(!result.IsObject()) @@ -116,7 +119,7 @@ namespace llarp struct CallerImpl : public ::abyss::http::JSONRPC { - Router* router; + AbstractRouter* router; llarp_time_t m_NextKeyUpdate = 0; const llarp_time_t KeyUpdateInterval = 1000 * 60 * 2; using PubkeyList_t = GetServiceNodeListHandler::PubkeyList_t; @@ -124,7 +127,7 @@ namespace llarp std::string username; std::string password; - CallerImpl(Router* r) : ::abyss::http::JSONRPC(), router(r) + CallerImpl(AbstractRouter* r) : ::abyss::http::JSONRPC(), router(r) { } @@ -150,7 +153,7 @@ namespace llarp AsyncUpdatePubkeyList() { LogInfo("Updating service node list"); - ::abyss::json::Value params; + json::Value params; params.SetObject(); QueueRPC("get_all_service_nodes_keys", std::move(params), std::bind(&CallerImpl::NewAsyncUpdatePubkeyListConn, this, @@ -160,7 +163,7 @@ namespace llarp bool Start(const std::string& remote) { - return RunAsync(router->netloop, remote); + return RunAsync(router->netloop(), remote); } abyss::http::IRPCClientHandler* @@ -177,12 +180,7 @@ namespace llarp { if(updated) { - router->lokinetRouters.clear(); - for(const auto& pk : list) - router->lokinetRouters.insert(std::make_pair( - pk.data(), std::numeric_limits< llarp_time_t >::max())); - LogInfo("updated service node list, we have ", - router->lokinetRouters.size(), " authorized routers"); + router->SetRouterWhitelist(list); } else LogError("service node list not updated"); @@ -207,8 +205,8 @@ namespace llarp struct Handler : public ::abyss::httpd::IRPCHandler { - Router* router; - Handler(::abyss::httpd::ConnImpl* conn, Router* r) + AbstractRouter* router; + Handler(::abyss::httpd::ConnImpl* conn, AbstractRouter* r) : ::abyss::httpd::IRPCHandler(conn), router(r) { } @@ -229,7 +227,7 @@ namespace llarp ListExitLevels(Response& resp) const { exit::Context::TrafficStats stats; - router->exitContext.CalculateExitTraffic(stats); + router->exitContext().CalculateExitTraffic(stats); resp.StartArray(); auto itr = stats.begin(); while(itr != stats.end()) @@ -293,11 +291,11 @@ namespace llarp struct ReqHandlerImpl : public ::abyss::httpd::BaseReqHandler { - ReqHandlerImpl(Router* r, llarp_time_t reqtimeout) + ReqHandlerImpl(AbstractRouter* r, llarp_time_t reqtimeout) : ::abyss::httpd::BaseReqHandler(reqtimeout), router(r) { } - Router* router; + AbstractRouter* router; ::abyss::httpd::IRPCHandler* CreateHandler(::abyss::httpd::ConnImpl* conn) { @@ -307,10 +305,10 @@ namespace llarp struct ServerImpl { - Router* router; + AbstractRouter* router; ReqHandlerImpl _handler; - ServerImpl(Router* r) : router(r), _handler(r, 2000) + ServerImpl(AbstractRouter* r) : router(r), _handler(r, 2000) { } @@ -339,14 +337,14 @@ namespace llarp saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); saddr.sin_family = AF_INET; saddr.sin_port = htons(port); - return _handler.ServeAsync(router->netloop, router->logic(), + return _handler.ServeAsync(router->netloop(), router->logic(), (const sockaddr*)&saddr); } }; #else struct ServerImpl { - ServerImpl(__attribute__((unused)) Router* r){}; + ServerImpl(__attribute__((unused)) AbstractRouter* r){}; bool Start(__attribute__((unused)) const std::string& addr) @@ -362,7 +360,7 @@ namespace llarp struct CallerImpl { - CallerImpl(__attribute__((unused)) Router* r) + CallerImpl(__attribute__((unused)) AbstractRouter* r) { } @@ -395,7 +393,8 @@ namespace llarp #endif - Caller::Caller(Router* r) : m_Impl(std::make_unique< CallerImpl >(r)) + Caller::Caller(AbstractRouter* r) + : m_Impl(std::make_unique< CallerImpl >(r)) { } @@ -427,7 +426,8 @@ namespace llarp m_Impl->SetBasicAuth(user, passwd); } - Server::Server(Router* r) : m_Impl(std::make_unique< ServerImpl >(r)) + Server::Server(AbstractRouter* r) + : m_Impl(std::make_unique< ServerImpl >(r)) { } diff --git a/llarp/rpc/rpc.hpp b/llarp/rpc/rpc.hpp index 2fe990a03..3f9a48fa7 100644 --- a/llarp/rpc/rpc.hpp +++ b/llarp/rpc/rpc.hpp @@ -10,7 +10,7 @@ namespace llarp { struct PubKey; - struct Router; + struct AbstractRouter; namespace rpc { @@ -19,7 +19,7 @@ namespace llarp /// jsonrpc server struct Server { - Server(Router* r); + Server(AbstractRouter* r); ~Server(); bool @@ -38,7 +38,7 @@ namespace llarp /// jsonrpc caller struct Caller { - Caller(Router* r); + Caller(AbstractRouter* r); ~Caller(); /// set http basic auth for use with remote rpc endpoint diff --git a/llarp/service/Identity.cpp b/llarp/service/Identity.cpp index 90bf8a847..b523e078c 100644 --- a/llarp/service/Identity.cpp +++ b/llarp/service/Identity.cpp @@ -34,7 +34,7 @@ namespace llarp bool read = false; if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf)) return false; - if(llarp_buffer_eq(key, "q")) + if(key == "q") { llarp_buffer_t str; if(!bencode_read_string(buf, &str)) diff --git a/llarp/service/IntroSet.cpp b/llarp/service/IntroSet.cpp index 4f4a95f39..7ed83b9f3 100644 --- a/llarp/service/IntroSet.cpp +++ b/llarp/service/IntroSet.cpp @@ -34,7 +34,7 @@ namespace llarp if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf)) return false; - if(llarp_buffer_eq(key, "i")) + if(key == "i") { return BEncodeReadList(I, buf); } @@ -47,7 +47,7 @@ namespace llarp if(!BEncodeMaybeReadDictInt("t", T, read, key, buf)) return false; - if(llarp_buffer_eq(key, "w")) + if(key == "w") { if(W) delete W; diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index ab7a8d658..64d25baa7 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -2,14 +2,15 @@ #include #include -#include +#include +#include #include namespace llarp { namespace service { - Context::Context(llarp::Router *r) : m_Router(r) + Context::Context(AbstractRouter *r) : m_Router(r) { } @@ -312,16 +313,16 @@ namespace llarp static std::map< std::string, std::function< llarp::service::Endpoint *( - const std::string &, llarp::Router *, + const std::string &, AbstractRouter *, llarp::service::Context *) > > endpointConstructors = { {"tun", - [](const std::string &nick, llarp::Router *r, + [](const std::string &nick, AbstractRouter *r, llarp::service::Context *c) -> llarp::service::Endpoint * { return new llarp::handlers::TunEndpoint(nick, r, c); }}, {"null", - [](const std::string &nick, llarp::Router *r, + [](const std::string &nick, AbstractRouter *r, llarp::service::Context *c) -> llarp::service::Endpoint * { return new llarp::handlers::NullEndpoint(nick, r, c); }}}; diff --git a/llarp/service/context.hpp b/llarp/service/context.hpp index 80bcedfe1..80e14ad02 100644 --- a/llarp/service/context.hpp +++ b/llarp/service/context.hpp @@ -15,7 +15,7 @@ namespace llarp /// holds all the hidden service endpoints we own struct Context : public util::IStateful { - Context(llarp::Router *r); + Context(AbstractRouter *r); ~Context(); void @@ -96,7 +96,7 @@ namespace llarp StartAll(); private: - llarp::Router *m_Router; + AbstractRouter *m_Router; std::unordered_map< std::string, std::unique_ptr< Endpoint > > m_Endpoints; std::list< std::unique_ptr< Endpoint > > m_Stopped; diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 8fed2f593..28dbecdb4 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -6,7 +6,10 @@ #include #include #include -#include +#include +#include +#include +#include #include #include @@ -16,7 +19,7 @@ namespace llarp { namespace service { - Endpoint::Endpoint(const std::string& name, llarp::Router* r, + Endpoint::Endpoint(const std::string& name, AbstractRouter* r, Context* parent) : path::Builder(r, r->dht(), 6, DEFAULT_HOP_LENGTH) , context(parent) @@ -69,7 +72,7 @@ namespace llarp if(m_IsolatedNetLoop) return m_IsolatedNetLoop; else - return m_Router->netloop; + return m_Router->netloop(); } bool @@ -623,7 +626,7 @@ namespace llarp } bool - Endpoint::PublishIntroSet(llarp::Router* r) + Endpoint::PublishIntroSet(AbstractRouter* r) { // publish via near router RouterID location = m_Identity.pub.Addr().as_array(); @@ -671,7 +674,7 @@ namespace llarp } bool - Endpoint::PublishIntroSetVia(llarp::Router* r, path::Path* path) + Endpoint::PublishIntroSetVia(AbstractRouter* r, path::Path* path) { auto job = new PublishIntroSetJob(this, GenTXID(), m_IntroSet); if(job->SendRequestViaPath(path, r)) @@ -809,8 +812,8 @@ namespace llarp return false; llarp_async_verify_rc* job = new llarp_async_verify_rc; job->nodedb = m_Router->nodedb(); - job->cryptoworker = m_Router->tp; - job->diskworker = m_Router->disk; + job->cryptoworker = m_Router->threadpool(); + job->diskworker = m_Router->diskworker(); job->logic = m_Router->logic(); job->hook = nullptr; job->rc = msg->R[0]; @@ -1312,12 +1315,13 @@ namespace llarp } } // no converstation - return EnsurePathToService(remote, - [](Address, OutboundContext* c) { - if(c) - c->UpdateIntroSet(true); - }, - 5000, false); + return EnsurePathToService( + remote, + [](Address, OutboundContext* c) { + if(c) + c->UpdateIntroSet(true); + }, + 5000, false); } bool @@ -1354,8 +1358,9 @@ namespace llarp { nodedb->select_random_hop(hops[hop - 1], hops[hop], hop); --tries; - } while(m_Endpoint->Router()->routerProfiling.IsBad(hops[hop].pubkey) - && tries > 0); + } while( + m_Endpoint->Router()->routerProfiling().IsBad(hops[hop].pubkey) + && tries > 0); return tries > 0; } return false; @@ -1737,7 +1742,7 @@ namespace llarp ++itr; } // send control message if we look too quiet - if(now - lastGoodSend > 60000) + if(now - lastGoodSend > (sendTimeout / 2)) { Encrypted< 64 > tmp; tmp.Randomize(); @@ -1898,7 +1903,7 @@ namespace llarp llarp_threadpool* Endpoint::Worker() { - return m_Router->tp; + return m_Router->threadpool(); } } // namespace service diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index 53efacf4d..58b876fef 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -37,7 +37,7 @@ namespace llarp static const size_t MAX_OUTBOUND_CONTEXT_COUNT = 4; - Endpoint(const std::string& nickname, llarp::Router* r, Context* parent); + Endpoint(const std::string& nickname, AbstractRouter* r, Context* parent); ~Endpoint(); virtual util::StatusObject @@ -84,7 +84,7 @@ namespace llarp llarp_threadpool* Worker(); - llarp::Router* + AbstractRouter* Router() { return m_Router; @@ -106,10 +106,10 @@ namespace llarp EnsureReplyPath(const ServiceInfo& addr); bool - PublishIntroSet(llarp::Router* r) override; + PublishIntroSet(AbstractRouter* r) override; bool - PublishIntroSetVia(llarp::Router* r, path::Path* p); + PublishIntroSetVia(AbstractRouter* r, path::Path* p); bool HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg) override; @@ -461,7 +461,7 @@ namespace llarp std::unique_ptr< llarp::exit::BaseSession > m_Exit; private: - llarp::Router* m_Router; + AbstractRouter* m_Router; llarp_threadpool* m_IsolatedWorker = nullptr; llarp::Logic* m_IsolatedLogic = nullptr; llarp_ev_loop* m_IsolatedNetLoop = nullptr; diff --git a/llarp/service/lookup.cpp b/llarp/service/lookup.cpp index 0765b8ca9..b1f861fca 100644 --- a/llarp/service/lookup.cpp +++ b/llarp/service/lookup.cpp @@ -1,12 +1,13 @@ #include #include -#include #include #include namespace llarp { + struct AbstractRouter; + namespace service { IServiceLookup::IServiceLookup(ILookupHolder *p, uint64_t tx, @@ -18,7 +19,7 @@ namespace llarp } bool - IServiceLookup::SendRequestViaPath(path::Path *path, Router *r) + IServiceLookup::SendRequestViaPath(path::Path *path, AbstractRouter *r) { auto msg = BuildRequestMessage(); if(!msg) diff --git a/llarp/service/lookup.hpp b/llarp/service/lookup.hpp index d24788176..df9b4811b 100644 --- a/llarp/service/lookup.hpp +++ b/llarp/service/lookup.hpp @@ -48,7 +48,7 @@ namespace llarp /// build a new requset message and send it via a path bool - SendRequestViaPath(llarp::path::Path* p, llarp::Router* r); + SendRequestViaPath(llarp::path::Path* p, AbstractRouter* r); ILookupHolder* parent; uint64_t txid; diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index 127e027f1..2eb678520 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -44,7 +44,7 @@ namespace llarp bool read = false; if(!BEncodeMaybeReadDictInt("a", proto, read, k, buf)) return false; - if(llarp_buffer_eq(k, "d")) + if(k == "d") { llarp_buffer_t strbuf; if(!bencode_read_string(buf, &strbuf)) @@ -126,7 +126,7 @@ namespace llarp ProtocolFrame::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) { bool read = false; - if(llarp_buffer_eq(key, "A")) + if(key == "A") { llarp_buffer_t strbuf; if(!bencode_read_string(val, &strbuf)) @@ -392,7 +392,8 @@ namespace llarp bool ProtocolFrame::HandleMessage(llarp::routing::IMessageHandler* h, - __attribute__((unused)) llarp::Router* r) const + __attribute__((unused)) + AbstractRouter* r) const { return h->HandleHiddenServiceFrame(this); } diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index 6a56f6d8c..191c2a0b6 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -134,7 +134,7 @@ namespace llarp Verify(Crypto* c, const ServiceInfo& from) const; bool - HandleMessage(routing::IMessageHandler* h, Router* r) const override; + HandleMessage(routing::IMessageHandler* h, AbstractRouter* r) const override; }; } // namespace service } // namespace llarp diff --git a/llarp/util/bencode.cpp b/llarp/util/bencode.cpp index 5d157ca5b..1200834db 100644 --- a/llarp/util/bencode.cpp +++ b/llarp/util/bencode.cpp @@ -15,8 +15,7 @@ bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result) buffer->cur++; - len = - llarp_buffer_read_until(buffer, 'e', (byte_t*)numbuf, sizeof(numbuf) - 1); + len = buffer->read_until('e', (byte_t*)numbuf, sizeof(numbuf) - 1); if(!len) { return false; @@ -34,8 +33,7 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result) { char numbuf[10]; - size_t len = - llarp_buffer_read_until(buffer, ':', (byte_t*)numbuf, sizeof(numbuf) - 1); + size_t len = buffer->read_until(':', (byte_t*)numbuf, sizeof(numbuf) - 1); if(!len) return false; @@ -50,7 +48,7 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result) buffer->cur++; - len = llarp_buffer_size_left(*buffer); + len = buffer->size_left(); if(len < slen) { return false; @@ -66,42 +64,55 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result) bool bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz) { - if(!llarp_buffer_writef(buff, "%zu:", sz)) + if(!buff->writef("%zu:", sz)) { return false; } - return llarp_buffer_write(buff, data, sz); + return buff->write(reinterpret_cast< const char* >(data), + reinterpret_cast< const char* >(data) + sz); } bool bencode_write_uint64(llarp_buffer_t* buff, uint64_t i) { - return llarp_buffer_writef(buff, "i%llu", i) - && llarp_buffer_write(buff, "e", 1); + if(!buff->writef("i%lu", i)) + { + return false; + } + + static const char letter[1] = {'e'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool bencode_write_version_entry(llarp_buffer_t* buff) { - return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION); + return buff->writef("1:vi%de", LLARP_PROTO_VERSION); } bool bencode_start_list(llarp_buffer_t* buff) { - return llarp_buffer_write(buff, "l", 1); + static const char letter[1] = {'l'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool bencode_start_dict(llarp_buffer_t* buff) { - return llarp_buffer_write(buff, "d", 1); + static const char letter[1] = {'d'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool bencode_end(llarp_buffer_t* buff) { - return llarp_buffer_write(buff, "e", 1); + static const char letter[1] = {'e'}; + assert(std::distance(std::begin(letter), std::end(letter)) == 1); + return buff->write(std::begin(letter), std::end(letter)); } bool @@ -112,7 +123,7 @@ bencode_read_dict(llarp_buffer_t* buff, struct dict_reader* r) if(*r->buffer->cur != 'd') // ensure is a dictionary return false; r->buffer->cur++; - while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e') + while(r->buffer->size_left() && *r->buffer->cur != 'e') { if(bencode_read_string(r->buffer, &strbuf)) { @@ -145,7 +156,7 @@ bencode_read_list(llarp_buffer_t* buff, struct list_reader* r) } r->buffer->cur++; - while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e') + while(r->buffer->size_left() && *r->buffer->cur != 'e') { if(!r->on_item(r, true)) // check for early abort return false; diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index 893eca180..abe74895d 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -46,7 +46,7 @@ namespace llarp BEncodeMaybeReadDictList(const char* k, List_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!BEncodeReadList(item, buf)) { @@ -62,7 +62,7 @@ namespace llarp BEncodeMaybeReadDictEntry(const char* k, Item_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!item.BDecode(buf)) { @@ -81,7 +81,7 @@ namespace llarp BEncodeMaybeReadDictInt(const char* k, Int_t& i, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!bencode_read_integer(buf, &i)) { @@ -100,7 +100,7 @@ namespace llarp bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf) { - if(llarp_buffer_eq(key, k)) + if(key == k) { if(!bencode_read_integer(buf, &item)) return false; @@ -149,7 +149,7 @@ namespace llarp buf->cur++; size_t idx = 0; - while(llarp_buffer_size_left(*buf) && *buf->cur != 'e') + while(buf->size_left() && *buf->cur != 'e') { if(idx >= array.size()) return false; @@ -184,7 +184,7 @@ namespace llarp return false; buf->cur++; - while(llarp_buffer_size_left(*buf) && *buf->cur != 'e') + while(buf->size_left() && *buf->cur != 'e') { if(!result.emplace(result.end())->BDecode(buf)) return false; @@ -203,7 +203,7 @@ namespace llarp return false; buf->cur++; - while(llarp_buffer_size_left(*buf) && *buf->cur != 'e') + while(buf->size_left() && *buf->cur != 'e') { T item; if(!item.BDecode(buf)) diff --git a/llarp/util/buffer.cpp b/llarp/util/buffer.cpp index 30fe602b6..4518d9783 100644 --- a/llarp/util/buffer.cpp +++ b/llarp/util/buffer.cpp @@ -5,72 +5,99 @@ #include size_t -llarp_buffer_size_left(const llarp_buffer_t& buff) +llarp_buffer_t::size_left() const { - size_t diff = buff.cur - buff.base; - if(diff > buff.sz) + size_t diff = cur - base; + if(diff > sz) + { return 0; + } else - return buff.sz - diff; + return sz - diff; } bool -llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) +llarp_buffer_t::writef(const char* fmt, ...) { int written; - ssize_t sz = llarp_buffer_size_left(*buff); + ssize_t sz = size_left(); va_list args; va_start(args, fmt); - written = vsnprintf((char*)buff->cur, sz, fmt, args); + written = vsnprintf(reinterpret_cast< char* >(cur), sz, fmt, args); va_end(args); if(written <= 0) return false; if(sz < written) return false; - buff->cur += written; + cur += written; return true; } bool -llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz) +llarp_buffer_t::put_uint16(uint16_t i) { - size_t left = llarp_buffer_size_left(*buff); - if(left >= sz) - { - memcpy(buff->cur, data, sz); - buff->cur += sz; - return true; - } - return false; + if(size_left() < sizeof(uint16_t)) + return false; + htobe16buf(cur, i); + cur += sizeof(uint16_t); + return true; +} + +bool +llarp_buffer_t::put_uint32(uint32_t i) +{ + if(size_left() < sizeof(uint32_t)) + return false; + htobe32buf(cur, i); + cur += sizeof(uint32_t); + return true; +} + +bool +llarp_buffer_t::read_uint16(uint16_t& i) +{ + if(size_left() < sizeof(uint16_t)) + return false; + i = bufbe16toh(cur); + cur += sizeof(uint16_t); + return true; +} + +bool +llarp_buffer_t::read_uint32(uint32_t& i) +{ + if(size_left() < sizeof(uint32_t)) + return false; + i = bufbe32toh(cur); + cur += sizeof(uint32_t); + return true; } size_t -llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result, - size_t resultsize) +llarp_buffer_t::read_until(char delim, byte_t* result, size_t resultsize) { size_t read = 0; // do the bound check first, to avoid over running - while((buff->cur != buff->base + buff->sz) && *buff->cur != delim - && resultsize) + while((cur != base + sz) && *cur != delim && resultsize) { - *result = *buff->cur; - buff->cur++; + *result = *cur; + cur++; result++; resultsize--; read++; } - if(llarp_buffer_size_left(*buff)) + if(size_left()) return read; else return 0; } bool -llarp_buffer_eq(const llarp_buffer_t& buf, const char* str) +operator==(const llarp_buffer_t& buff, const char* str) { - ManagedBuffer copy{buf}; + ManagedBuffer copy{buff}; while(*str && copy.underlying.cur != (copy.underlying.base + copy.underlying.sz)) { @@ -81,43 +108,3 @@ llarp_buffer_eq(const llarp_buffer_t& buf, const char* str) } return *str == 0; } - -bool -llarp_buffer_put_uint16(llarp_buffer_t* buf, uint16_t i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint16_t)) - return false; - htobe16buf(buf->cur, i); - buf->cur += sizeof(uint16_t); - return true; -} - -bool -llarp_buffer_put_uint32(llarp_buffer_t* buf, uint32_t i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint32_t)) - return false; - htobe32buf(buf->cur, i); - buf->cur += sizeof(uint32_t); - return true; -} - -bool -llarp_buffer_read_uint16(llarp_buffer_t* buf, uint16_t* i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint16_t)) - return false; - *i = bufbe16toh(buf->cur); - buf->cur += sizeof(uint16_t); - return true; -} - -bool -llarp_buffer_read_uint32(llarp_buffer_t* buf, uint32_t* i) -{ - if(llarp_buffer_size_left(*buf) < sizeof(uint32_t)) - return false; - *i = bufbe32toh(buf->cur); - buf->cur += sizeof(uint32_t); - return true; -} diff --git a/llarp/util/buffer.hpp b/llarp/util/buffer.hpp index c500335ed..e7b4cd963 100644 --- a/llarp/util/buffer.hpp +++ b/llarp/util/buffer.hpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -112,12 +113,52 @@ struct llarp_buffer_t { } + size_t + size_left() const; + + template < typename InputIt > + bool + write(InputIt begin, InputIt end); + + bool + writef(const char *fmt, ...) __attribute__((format(printf, 2, 3))); + ; + + bool + put_uint16(uint16_t i); + bool + put_uint32(uint32_t i); + + bool + read_uint16(uint16_t &i); + bool + read_uint32(uint32_t &i); + + size_t + read_until(char delim, byte_t *result, size_t resultlen); + private: friend struct ManagedBuffer; llarp_buffer_t(const llarp_buffer_t &) = default; llarp_buffer_t(llarp_buffer_t &&) = default; }; +bool +operator==(const llarp_buffer_t &buff, const char *data); + +template < typename InputIt > +bool +llarp_buffer_t::write(InputIt begin, InputIt end) +{ + auto dist = std::distance(begin, end); + if(static_cast< decltype(dist) >(size_left()) >= dist) + { + cur = std::copy(begin, end, cur); + return true; + } + return false; +} + /** Provide a copyable/moveable wrapper around `llarp_buffer_t`. */ @@ -140,40 +181,4 @@ struct ManagedBuffer } }; -/// how much room is left in buffer -size_t -llarp_buffer_size_left(const llarp_buffer_t &buff); - -/// write a chunk of data size "sz" -bool -llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz); - -/// write multiple strings -bool -llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...); - -/// read buffer upto character delimiter -size_t -llarp_buffer_read_until(llarp_buffer_t *buff, char delim, byte_t *result, - size_t resultlen); -/// compare buffers, true if equal else false -bool -llarp_buffer_eq(const llarp_buffer_t &buff, const char *data); - -/// put big endian unsigned 16 bit integer -bool -llarp_buffer_put_uint16(llarp_buffer_t *buf, uint16_t i); - -/// put big endian unsigned 32 bit integer -bool -llarp_buffer_put_uint32(llarp_buffer_t *buf, uint32_t i); - -/// read big endian unsigned 16 bit integer -bool -llarp_buffer_read_uint16(llarp_buffer_t *buf, uint16_t *i); - -/// read big endian unsigned 32 bit integer -bool -llarp_buffer_read_uint32(llarp_buffer_t *buf, uint32_t *i); - #endif diff --git a/libabyss/src/json.cpp b/llarp/util/json.cpp similarity index 81% rename from libabyss/src/json.cpp rename to llarp/util/json.cpp index 00c5fdc53..36882971d 100644 --- a/libabyss/src/json.cpp +++ b/llarp/util/json.cpp @@ -1,13 +1,11 @@ -#include -#include -#include +#include #include #include #include #include -namespace abyss +namespace llarp { namespace json { @@ -52,10 +50,10 @@ namespace abyss void ToString(const json::Document& val, std::ostream& out) { - rapidjson::OStreamWrapper s(out); - rapidjson::Writer< rapidjson::OStreamWrapper > writer(s); + Stream s(out); + rapidjson::Writer< Stream > writer(s); val.Accept(writer); } } // namespace json -} // namespace abyss +} // namespace llarp diff --git a/llarp/util/json.hpp b/llarp/util/json.hpp new file mode 100644 index 000000000..0bfa1b2e9 --- /dev/null +++ b/llarp/util/json.hpp @@ -0,0 +1,122 @@ +#ifndef LLARP_UTIL_JSON_HPP +#define LLARP_UTIL_JSON_HPP + +#include +#include + +#include +#include + +namespace llarp +{ + namespace json + { + /// add this because debian stable doesn't have it + template < typename StreamType > + class BasicOStreamWrapper + { + public: + typedef typename StreamType::char_type Ch; + BasicOStreamWrapper(StreamType& stream) : stream_(stream) + { + } + + void + Put(Ch c) + { + stream_.put(c); + } + + void + Flush() + { + stream_.flush(); + } + + // Not implemented + char + Peek() const + { + RAPIDJSON_ASSERT(false); + return 0; + } + char + Take() + { + RAPIDJSON_ASSERT(false); + return 0; + } + size_t + Tell() const + { + RAPIDJSON_ASSERT(false); + return 0; + } + char* + PutBegin() + { + RAPIDJSON_ASSERT(false); + return 0; + } + size_t + PutEnd(char*) + { + RAPIDJSON_ASSERT(false); + return 0; + } + + private: + BasicOStreamWrapper(const BasicOStreamWrapper&); + BasicOStreamWrapper& + operator=(const BasicOStreamWrapper&); + + StreamType& stream_; + }; + + using Document = rapidjson::Document; + using Value = rapidjson::Value; + using Stream = BasicOStreamWrapper< std::ostream >; + using Writer = rapidjson::Writer< Stream >; + } // namespace json + +#if __cplusplus >= 201703L + using string_view = std::string_view; +#else + using string_view = std::string; +#endif + namespace json + { + struct IParser + { + virtual ~IParser(){}; + + /// result from feeding data to parser + enum Result + { + /// we need more data to finish parsing + eNeedData, + /// we have parsed the object fully + eDone, + /// we have a parsing syntax error + eParseError + }; + + /// feed data to parser return true if successful + virtual bool + FeedData(const char* buf, size_t sz) = 0; + /// parse internal buffer + virtual Result + Parse(Document& obj) const = 0; + }; + + /// create new parser + IParser* + MakeParser(size_t contentSize); + + void + ToString(const json::Document& obj, std::ostream& out); + + } // namespace json +} // namespace llarp + +#endif diff --git a/llarp/util/status.hpp b/llarp/util/status.hpp index 80f2327a8..02e06ea42 100644 --- a/llarp/util/status.hpp +++ b/llarp/util/status.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_UTIL_STATUS_HPP #define LLARP_UTIL_STATUS_HPP #ifdef USE_ABYSS -#include +#include #endif #include #include @@ -14,8 +14,8 @@ namespace llarp namespace util { #ifdef USE_ABYSS - using StatusObject_Impl = ::abyss::json::Document; - using Value_t = ::abyss::json::Value; + using StatusObject_Impl = json::Document; + using Value_t = json::Value; #else struct StatusObject_Impl { @@ -27,7 +27,7 @@ namespace llarp struct StatusObject { - using String_t = llarp::string_view; + using String_t = string_view; using Variant = absl::variant< uint64_t, std::string, bool, StatusObject, std::vector< std::string >, std::vector< StatusObject > >; @@ -71,7 +71,7 @@ namespace llarp /// an entity that has a status that can be extracted struct IStateful { - virtual ~IStateful(){}; + virtual ~IStateful() {}; virtual StatusObject ExtractStatus() const = 0; diff --git a/test/exit/test_llarp_exit_context.cpp b/test/exit/test_llarp_exit_context.cpp index 7f3e9fd6f..85380f1dd 100644 --- a/test/exit/test_llarp_exit_context.cpp +++ b/test/exit/test_llarp_exit_context.cpp @@ -23,9 +23,9 @@ TEST_F(ExitTest, AddMultipleIP) conf.emplace("exit", "true"); conf.emplace("type", "null"); conf.emplace("ifaddr", "10.0.0.1/24"); - ASSERT_TRUE(r.exitContext.AddExitEndpoint("test-exit", conf)); - ASSERT_TRUE(r.exitContext.ObtainNewExit(pk, firstPath, true)); - ASSERT_TRUE(r.exitContext.ObtainNewExit(pk, secondPath, true)); - ASSERT_TRUE(r.exitContext.FindEndpointForPath(firstPath)->LocalIP() - == r.exitContext.FindEndpointForPath(secondPath)->LocalIP()); + ASSERT_TRUE(r.exitContext().AddExitEndpoint("test-exit", conf)); + ASSERT_TRUE(r.exitContext().ObtainNewExit(pk, firstPath, true)); + ASSERT_TRUE(r.exitContext().ObtainNewExit(pk, secondPath, true)); + ASSERT_TRUE(r.exitContext().FindEndpointForPath(firstPath)->LocalIP() + == r.exitContext().FindEndpointForPath(secondPath)->LocalIP()); }; diff --git a/test/test_libabyss.cpp b/test/test_libabyss.cpp index ae36de93f..8eff239f3 100644 --- a/test/test_libabyss.cpp +++ b/test/test_libabyss.cpp @@ -200,7 +200,7 @@ struct AbyssTest : public AbyssTestBase, TEST_F(AbyssTest, TestClientAndServer) { Start(); - abyss::json::Value params; + llarp::json::Value params; params.SetObject(); QueueRPC(method, std::move(params), std::bind(&AbyssTest::NewConn, this, std::placeholders::_1)); diff --git a/test/util/test_llarp_util_bits.cpp b/test/util/test_llarp_util_bits.cpp index 5dba48927..7237e78f0 100644 --- a/test/util/test_llarp_util_bits.cpp +++ b/test/util/test_llarp_util_bits.cpp @@ -32,34 +32,34 @@ TEST_P(TestBits, bitcount) // clang-format off static const InputData inputData[] = { - {ArrayUC1{0b00000000}, 0}, - {ArrayUC1{0b00000001}, 1}, - {ArrayUC1{0b00000010}, 1}, - {ArrayUC1{0b00000100}, 1}, - {ArrayUC1{0b00001000}, 1}, - {ArrayUC1{0b00010000}, 1}, - {ArrayUC1{0b00100000}, 1}, - {ArrayUC1{0b01000000}, 1}, - {ArrayUC1{0b10000000}, 1}, - {ArrayUC1{0b11111111}, 8}, - {ArrayUC20{0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, + {ArrayUC1{{0b00000000}}, 0}, + {ArrayUC1{{0b00000001}}, 1}, + {ArrayUC1{{0b00000010}}, 1}, + {ArrayUC1{{0b00000100}}, 1}, + {ArrayUC1{{0b00001000}}, 1}, + {ArrayUC1{{0b00010000}}, 1}, + {ArrayUC1{{0b00100000}}, 1}, + {ArrayUC1{{0b01000000}}, 1}, + {ArrayUC1{{0b10000000}}, 1}, + {ArrayUC1{{0b11111111}}, 8}, + {ArrayUC20{{0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, - 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000}, 0}, - {ArrayUC20{0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100, + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000}}, 0}, + {ArrayUC20{{0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100, 0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100, 0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100, - 0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100}, 48}, - {ArrayU1{0b00000000000000000000000000000000}, 0}, - {ArrayU1{0b00101010101010101010101010101010}, 15}, - {ArrayU1{0b10101010101010101010101010101010}, 16}, - {ArrayU1{0b01010101010101010101010101010101}, 16}, - {ArrayU1{0b11111111111111111111111111111111}, 32}, - {ArrayULL1{0b0000000000000000000000000000000000000000000000000000000000000000}, 0}, - {ArrayULL1{0b0010101010101010101010101010101000101010101010101010101010101010}, 30}, - {ArrayULL1{0b1010101010101010101010101010101010101010101010101010101010101010}, 32}, - {ArrayULL1{0b0101010101010101010101010101010101010101010101010101010101010101}, 32}, - {ArrayULL1{0b1111111111111111111111111111111111111111111111111111111111111111}, 64}, + 0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100}}, 48}, + {ArrayU1{{0b00000000000000000000000000000000}}, 0}, + {ArrayU1{{0b00101010101010101010101010101010}}, 15}, + {ArrayU1{{0b10101010101010101010101010101010}}, 16}, + {ArrayU1{{0b01010101010101010101010101010101}}, 16}, + {ArrayU1{{0b11111111111111111111111111111111}}, 32}, + {ArrayULL1{{0b0000000000000000000000000000000000000000000000000000000000000000}}, 0}, + {ArrayULL1{{0b0010101010101010101010101010101000101010101010101010101010101010}}, 30}, + {ArrayULL1{{0b1010101010101010101010101010101010101010101010101010101010101010}}, 32}, + {ArrayULL1{{0b0101010101010101010101010101010101010101010101010101010101010101}}, 32}, + {ArrayULL1{{0b1111111111111111111111111111111111111111111111111111111111111111}}, 64}, }; // clang-format on diff --git a/test/win32/test.rc b/test/win32/test.rc index 04f0df02b..f775d662f 100644 --- a/test/win32/test.rc +++ b/test/win32/test.rc @@ -6,7 +6,7 @@ // // Microsoft Visual C++ generated resource script. // -#include "resource.h" +#include #include ///////////////////////////////////////////////////////////////////////////// // English (United States) resources