Merge remote-tracking branch 'origin/master'

pull/317/head
Ryan Tharp 5 years ago
commit 25d00e2974

@ -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($<$<COMPILE_LANGUAGE:C>:-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)

@ -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")

@ -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

@ -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

@ -11,96 +11,79 @@ datagram based network layer.
outer message format:
{
A: command,
B: <16 bytes flow id>,
C: <optional 32 bytes cookie>,
X: <N bytes payload>
}
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:
<outer-header>
<32 bytes random>
<8 bytes net id>
<remaining discarded>
{
A: R,
B: msg.B,
X: "<reply line>"
}
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
<outer-header>
<32 byte new flow-id, X>
<remaining discarded>
B is the B value from the get handshake cookie message
X is a 32 byte handshake cookie, calcuated via:
r = RAND(32)
a = "<ascii representation of ip>" + " " + "<port number>"
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.<ascii representation of ipv4 address>" + ":" + "<port number>"
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
<outer-header>
<N arbitrary bytes reason for rejection>
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:
<reply-line> ::= <status-code> <space> <method> <space> <message>
<status-code> ::= <integer> <digit> <digit>
<word> ::= <letter>+
<message> ::= <word> <space> <word>* | <word>
<method> ::= "COOKIE" | "HANDSHAKE"
<user-agent> ::= <net-id> <space> <protocol-version> <space> <agent-version>
resulting message of reject with reason "no you"
<net-id> ::= "lokinet" | "testnet"
"R=<32 byte flow-id>no you"
<space> ::= " "
command 'S' - session negotiation
<zero> ::= "0"
<integer> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<digit> ::= <zero> | <integer>
negotiate encrypted session
<number> ::= <zero> | <integer> <digit>*
<outer-header>
<24 bytes nounce, N>
<encrypted session negotiation data, X>
<last 32 bytes keyed hash, Z>
<agent-version> ::= <number> "." <number> "." <number>
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
<protocol-version> ::= <number>
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>
<remaining bytes encrypted payload>
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>
<remaining data payload>
<outer-header>
<24 bytes nonce, N>
<encrypted data, X>
<last 32 bytes keyed hash of entire payload, Z>
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
<header>
<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>
<remaining bytes discarded>
command: 'L' (keep alive ack)
acknolege keep alive message
<header>
<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>
<remaining bytes discarded>
command: 'C' (congestion)
tell other side to slow down
<header>
<2 bytes reduce TX rate by this many 1024 bytes per second>
<4 bytes milliseconds slowdown lifetime>
<remaining bytes discarded>
command: 'D' (anti-congestion)
tell other side to speed up
<header>
<2 bytes increase TX rate by this many 1024 bytes per second>
<4 bytes milliseconds speedup lifetime>
<remaining bytes discarded>
command: 'T' (transmit)
transit fragment
<header>
<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>
<remaining bytes discarded>
command: 'A' (ack)
acknoledge fragments
<header>
<1 byte number of acks following, N>
<8 * N bytes acks>
<remaining bytes discarded>
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>
<remaining bytes discarded>
command: 'U' (upgrade)
request protocol upgrade
<header>
<1 byte protocol min version to upgrade to>
<1 byte protocol max version to upgrade to>
<remaining bytes discarded>
command: 'V' (version upgrade)
sent in response to upgrade message
<header>
<1 byte protocol version selected>
<1 byte protocol version highest we support>
<remaining bytes discarded>

@ -1,8 +1,8 @@
#ifndef __ABYSS_CLIENT_HPP__
#define __ABYSS_CLIENT_HPP__
#include <abyss/json.hpp>
#include <ev/ev.h>
#include <util/json.hpp>
#include <util/string_view.hpp>
#include <deque>
@ -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;

@ -1,6 +1,6 @@
#ifndef __ABYSS_HTTP_HPP__
#define __ABYSS_HTTP_HPP__
#include <abyss/json.hpp>
#include <util/json.hpp>
#include <util/string_view.hpp>
#include <string>
@ -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

@ -1,59 +0,0 @@
#ifndef __ABYSS_JSON_JSON_HPP
#define __ABYSS_JSON_JSON_HPP
#include <memory>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/ostreamwrapper.h>
#include <iostream>
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

@ -1,8 +1,8 @@
#ifndef __ABYSS_SERVER_HPP__
#define __ABYSS_SERVER_HPP__
#include <abyss/json.hpp>
#include <ev/ev.h>
#include <util/json.hpp>
#include <util/logic.hpp>
#include <util/string_view.hpp>
#include <util/time.hpp>
@ -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);

@ -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));

@ -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))
{

@ -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))

@ -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)

@ -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})

@ -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 >;

@ -15,7 +15,9 @@
#include <dht/taglookup.hpp>
#include <messages/dht.hpp>
#include <messages/dht_immediate.hpp>
#include <router/router.hpp>
#include <path/path.hpp>
#include <router/abstractrouter.hpp>
#include <util/logic.hpp>
#include <vector>
@ -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;

@ -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

@ -2,13 +2,13 @@
#include <dht/dht.h>
#include <router_contact.hpp>
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);
}

@ -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

@ -2,7 +2,7 @@
#include <dht/context.hpp>
#include <dht/messages/findrouter.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
namespace llarp
{

@ -3,7 +3,8 @@
#include <dht/context.hpp>
#include <dht/messages/gotrouter.hpp>
#include <messages/dht.hpp>
#include <router/router.hpp>
#include <path/path.hpp>
#include <router/abstractrouter.hpp>
#include <util/logger.hpp>
namespace llarp

@ -2,7 +2,9 @@
#include <dht/context.hpp>
#include <dht/messages/gotintro.hpp>
#include <router/router.hpp>
#include <messages/dht.hpp>
#include <path/path.hpp>
#include <router/abstractrouter.hpp>
#include <util/logger.hpp>
namespace llarp

@ -3,7 +3,8 @@
#include <dht/context.hpp>
#include <dht/messages/gotintro.hpp>
#include <messages/dht.hpp>
#include <router/router.hpp>
#include <path/path.hpp>
#include <router/abstractrouter.hpp>
namespace llarp
{

@ -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;

@ -3,7 +3,9 @@
#include <dht/context.hpp>
#include <dht/messages/gotrouter.hpp>
#include <messages/dht.hpp>
#include <router/router.hpp>
#include <path/path.hpp>
#include <nodedb.hpp>
#include <router/abstractrouter.hpp>
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);
}

@ -2,7 +2,8 @@
#include <dht/context.hpp>
#include <messages/dht.hpp>
#include <router/router.hpp>
#include <path/path.hpp>
#include <router/abstractrouter.hpp>
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;

@ -1,7 +1,8 @@
#include <dht/context.hpp>
#include <dht/messages/gotrouter.hpp>
#include <router/router.hpp>
#include <path/path.hpp>
#include <router/abstractrouter.hpp>
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

@ -4,7 +4,7 @@
#include <dht/messages/gotintro.hpp>
#include <messages/dht.hpp>
#include <messages/dht_immediate.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
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;

@ -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

@ -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;

@ -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)
{

@ -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;

@ -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;

@ -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));

@ -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

@ -1,5 +1,6 @@
#include <messages/exit.hpp>
#include <routing/handler.hpp>
#include <crypto/crypto.hpp>
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);
}

@ -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()

@ -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;

@ -1,6 +1,6 @@
#include <exit/endpoint.hpp>
#include <handlers/exit.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
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

@ -6,6 +6,8 @@
#include <path/path.hpp>
#include <util/time.hpp>
#include <queue>
namespace llarp
{
namespace handlers

@ -1,4 +1,6 @@
#include <messages/exit.hpp>
#include <crypto/crypto.hpp>
#include <routing/handler.hpp>
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);
}

@ -1,5 +1,6 @@
#include <messages/exit.hpp>
#include <crypto/crypto.hpp>
#include <routing/handler.hpp>
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);
}

@ -1,4 +1,6 @@
#include <messages/exit.hpp>
#include <crypto/crypto.hpp>
#include <routing/handler.hpp>
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);
}

@ -1,7 +1,8 @@
#include <exit/session.hpp>
#include <nodedb.hpp>
#include <path/path.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
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

@ -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(){};

@ -64,7 +64,7 @@ namespace llarp
bool
TransferTrafficMessage::HandleMessage(IMessageHandler* h,
llarp::Router* r) const
AbstractRouter* r) const
{
return h->HandleTransferTrafficMessage(this, r);
}

@ -1,4 +1,6 @@
#include <messages/exit.hpp>
#include <crypto/crypto.hpp>
#include <routing/handler.hpp>
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);
}

@ -2,7 +2,7 @@
#include <dns/dns.hpp>
#include <net/net.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
#include <util/str.hpp>
#include <cassert>
@ -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

@ -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;

@ -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){};

@ -10,7 +10,8 @@
#include <dns/dns.hpp>
#include <ev/ev.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
#include <service/context.hpp>
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;

@ -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();

@ -1,16 +1,17 @@
#include <link/iwp.hpp>
#include <link/iwp_internal.hpp>
#include <router/router.hpp>
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,

@ -1,11 +1,14 @@
#ifndef LLARP_LINK_IWP_HPP
#define LLARP_LINK_IWP_HPP
#include <memory>
#include <link/server.hpp>
#include <memory>
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

@ -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;

@ -170,6 +170,29 @@ namespace llarp
m_Pending.erase(remote);
}
util::StatusObject
ILinkLayer::ExtractStatus() const
{
std::vector<util::StatusObject> 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()
{

@ -8,6 +8,7 @@
#include <router_contact.hpp>
#include <util/logic.hpp>
#include <util/threading.hpp>
#include <util/status.hpp>
#include <list>
#include <unordered_map>
@ -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;

@ -13,7 +13,7 @@ namespace llarp
struct LinkIntroMessage;
struct ILinkMessage;
struct ILinkLayer;
struct ILinkSession
struct ILinkSession : public util::IStateful
{
virtual ~ILinkSession(){};

@ -1,9 +1,10 @@
#include <link/utp.hpp>
#include <crypto/crypto.hpp>
#include <link/server.hpp>
#include <messages/discard.hpp>
#include <messages/link_intro.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
#include <util/buffer.hpp>
#include <util/endian.hpp>
@ -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);
}
}

@ -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

@ -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);

@ -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

@ -1,6 +1,6 @@
#include <messages/dht_immediate.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
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;

@ -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;

@ -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);
}

@ -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);

@ -1,7 +1,8 @@
#include <messages/link_intro.hpp>
#include <crypto/crypto.hpp>
#include <router_contact.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
#include <util/bencode.h>
#include <util/logger.hpp>
@ -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;

@ -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);

@ -1,12 +1,36 @@
#include <messages/link_message_parser.hpp>
#include <messages/dht_immediate.hpp>
#include <messages/discard.hpp>
#include <messages/link_intro.hpp>
#include <messages/link_message.hpp>
#include <messages/relay_commit.hpp>
#include <messages/relay.hpp>
#include <router_contact.hpp>
#include <util/buffer.hpp>
#include <util/logger.hpp>
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;

@ -5,15 +5,12 @@
#include <router_id.hpp>
#include <util/bencode.hpp>
#include <queue>
#include <vector>
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;

@ -1,18 +1,21 @@
#ifndef LLARP_LINK_MESSAGE_PARSER_HPP
#define LLARP_LINK_MESSAGE_PARSER_HPP
#include <messages/discard.hpp>
#include <messages/dht_immediate.hpp>
#include <messages/link_intro.hpp>
#include <messages/link_message.hpp>
#include <messages/relay.hpp>
#include <messages/relay_commit.hpp>
#include <router_id.hpp>
#include <util/bencode.h>
#include <memory>
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

@ -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

@ -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

@ -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

@ -1,6 +1,7 @@
#include <messages/relay.hpp>
#include <router/router.hpp>
#include <path/path.hpp>
#include <router/abstractrouter.hpp>
#include <util/bencode.hpp>
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);

@ -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;

@ -2,10 +2,11 @@
#include <messages/path_confirm.hpp>
#include <path/path.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
#include <util/bencode.hpp>
#include <util/buffer.hpp>
#include <util/logger.hpp>
#include <util/logic.hpp>
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)

@ -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;

@ -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

@ -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;

@ -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;
}

@ -2,8 +2,13 @@
#include <messages/dht.hpp>
#include <messages/discard.hpp>
#include <messages/exit.hpp>
#include <messages/path_latency.hpp>
#include <messages/relay_commit.hpp>
#include <messages/transfer_traffic.hpp>
#include <path/pathbuilder.hpp>
#include <router/router.hpp>
#include <profiling.hpp>
#include <router/abstractrouter.hpp>
#include <util/buffer.hpp>
#include <util/endian.hpp>
@ -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))

@ -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;

@ -6,7 +6,10 @@
namespace llarp
{
using PathID_t = AlignedBuffer< PATHIDSIZE >;
}
struct PathID_t final : public AlignedBuffer< PATHIDSIZE >
{
};
} // namespace llarp
#endif

@ -1,9 +1,12 @@
#include <path/pathbuilder.hpp>
#include <messages/relay_commit.hpp>
#include <nodedb.hpp>
#include <path/path.hpp>
#include <router/router.hpp>
#include <profiling.hpp>
#include <router/abstractrouter.hpp>
#include <util/buffer.hpp>
#include <util/logic.hpp>
#include <functional>
@ -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);
}

@ -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();

@ -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)

@ -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;
}

@ -1,8 +1,13 @@
#include <path/path.hpp>
#include <dht/context.hpp>
#include <exit/context.hpp>
#include <messages/discard.hpp>
#include <router/router.hpp>
#include <messages/exit.hpp>
#include <messages/path_latency.hpp>
#include <messages/path_transfer.hpp>
#include <messages/relay_commit.hpp>
#include <router/abstractrouter.hpp>
#include <routing/handler.hpp>
#include <util/buffer.hpp>
#include <util/endian.hpp>
@ -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)
{

@ -0,0 +1,6 @@
#include <router/abstractrouter.hpp>
namespace llarp
{
} // namespace llarp

@ -0,0 +1,151 @@
#ifndef LLARP_ABSTRACT_ROUTER_HPP
#define LLARP_ABSTRACT_ROUTER_HPP
#include <util/types.hpp>
#include <util/status.hpp>
#include <vector>
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<RouterID> & routers) =0 ;
/// visit each connected link session
virtual void
ForEachPeer(std::function<void(const ILinkSession*, bool)> visit) const = 0;
};
} // namespace llarp
#endif

File diff suppressed because it is too large Load Diff

@ -1,6 +1,8 @@
#ifndef LLARP_ROUTER_HPP
#define LLARP_ROUTER_HPP
#include <router/abstractrouter.hpp>
#include <constants/link_layer.hpp>
#include <crypto/types.hpp>
#include <ev/ev.h>
@ -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<RouterID> & 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);

@ -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))

@ -1,6 +1,7 @@
#include <messages/dht.hpp>
#include <router/router.hpp>
#include <router/abstractrouter.hpp>
#include <routing/handler.hpp>
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()};

@ -1,68 +1,85 @@
#ifndef LLARP_ROUTING_HANDLER_HPP
#define LLARP_ROUTING_HANDLER_HPP
#include <messages/exit.hpp>
#include <messages/path_confirm.hpp>
#include <messages/path_latency.hpp>
#include <messages/path_transfer.hpp>
#include <messages/transfer_traffic.hpp>
#include <util/buffer.hpp>
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

@ -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;

@ -1,16 +1,47 @@
#include <routing/message_parser.hpp>
#include <messages/dht.hpp>
#include <messages/discard.hpp>
#include <messages/exit.hpp>
#include <messages/path_confirm.hpp>
#include <messages/path_latency.hpp>
#include <messages/path_transfer.hpp>
#include <messages/transfer_traffic.hpp>
#include <path/path_types.hpp>
#include <util/mem.hpp>
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;

@ -1,56 +1,42 @@
#ifndef LLARP_ROUTING_MESSAGE_PARSER_HPP
#define LLARP_ROUTING_MESSAGE_PARSER_HPP
#include <messages/dht.hpp>
#include <messages/discard.hpp>
#include <messages/path_confirm.hpp>
#include <messages/path_latency.hpp>
#include <messages/path_transfer.hpp>
#include <path/path_types.hpp>
#include <util/bencode.hpp>
#include <util/bencode.h>
#include <util/buffer.hpp>
#include <memory>
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

@ -53,7 +53,7 @@ namespace llarp
bool
PathConfirmMessage::HandleMessage(IMessageHandler* h,
llarp::Router* r) const
AbstractRouter* r) const
{
return h && h->HandlePathConfirmMessage(this, r);
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save