diff --git a/CMakeLists.txt b/CMakeLists.txt index f21aaea67..f5d8eb0b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -491,12 +491,12 @@ set(LIB_SRC ${DNSLIB_SRC} ${EV_SRC} ${UTP_SRC} - llarp/address_info.cpp llarp/config.cpp llarp/context.cpp llarp/crypto/constants.cpp - llarp/crypto/crypto.cpp llarp/crypto/crypto_libsodium.cpp + llarp/crypto/crypto.cpp + llarp/crypto/encrypted_frame.cpp llarp/crypto/encrypted.cpp llarp/crypto/types.cpp llarp/dht/bucket.cpp @@ -522,8 +522,6 @@ set(LIB_SRC llarp/dns.cpp llarp/dnsc.cpp llarp/dnsd.cpp - llarp/encrypted_frame.cpp - llarp/establish_job.cpp llarp/exit/close_exit.cpp llarp/exit/context.cpp llarp/exit/endpoint.cpp @@ -534,53 +532,50 @@ set(LIB_SRC llarp/exit/session.cpp llarp/exit/transfer_traffic.cpp llarp/exit/update_exit.cpp - llarp/exit_info.cpp llarp/handlers/exit.cpp llarp/handlers/null.cpp llarp/handlers/tun.cpp - llarp/ip.cpp llarp/link/curvecp.cpp llarp/link/encoder.cpp llarp/link/iwp.cpp llarp/link/server.cpp llarp/link/session.cpp llarp/link/utp.cpp - llarp/link_intro.cpp - llarp/link_message.cpp llarp/messages/dht.cpp llarp/messages/dht_immediate.cpp llarp/messages/discard.cpp llarp/messages/exit.cpp llarp/messages/link_intro.cpp + llarp/messages/link_message.cpp llarp/messages/path_confirm.cpp llarp/messages/path_latency.cpp llarp/messages/path_transfer.cpp llarp/messages/relay.cpp llarp/messages/relay_commit.cpp llarp/messages/transfer_traffic.cpp + llarp/net/address_info.cpp + llarp/net/exit_info.cpp + llarp/net/ip.cpp llarp/net/net_int.cpp llarp/nodedb.cpp llarp/path/path.cpp llarp/path/path_types.cpp llarp/path/pathbuilder.cpp llarp/path/pathset.cpp + llarp/path/transit_hop.cpp llarp/pow.cpp llarp/profiling.cpp - llarp/proofofwork.cpp - llarp/relay_commit.cpp - llarp/relay_up_down.cpp - llarp/router.cpp + llarp/router/router.cpp llarp/router_contact.cpp llarp/router_id.cpp llarp/routing/dht_message.cpp llarp/routing/handler.cpp - llarp/routing/message.cpp llarp/routing/message_parser.cpp + llarp/routing/message.cpp llarp/routing/path_confirm.cpp llarp/routing/path_latency.cpp llarp/routing/path_transfer.cpp - llarp/routing_endpoint.cpp - llarp/rpc.cpp + llarp/rpc/rpc.cpp llarp/service/Identity.cpp llarp/service/Intro.cpp llarp/service/IntroSet.cpp @@ -596,7 +591,6 @@ set(LIB_SRC llarp/service/types.cpp llarp/service/vanity.cpp llarp/testnet.c - llarp/transit_hop.cpp ) set(RC_SRC diff --git a/docs/llarp.png b/docs/llarp.png index 96e36e178..781db1124 100644 Binary files a/docs/llarp.png and b/docs/llarp.png differ diff --git a/docs/llarp_structure.dot b/docs/llarp_structure.dot index 15759a879..1052f0998 100644 --- a/docs/llarp_structure.dot +++ b/docs/llarp_structure.dot @@ -1,11 +1,6 @@ digraph { constants -> util; - util -> constants; - util -> win32; - - android -> linux; - crypto -> constants; crypto -> llarp; crypto -> util; @@ -28,12 +23,11 @@ digraph { ev -> net; ev -> util; - ev -> win32; exit -> crypto; exit -> handlers; - exit -> llarp; exit -> messages; + exit -> net; exit -> path; exit -> routing; exit -> util; @@ -41,7 +35,6 @@ digraph { handlers -> dns; handlers -> ev; handlers -> exit; - handlers -> llarp; handlers -> net; handlers -> service; handlers -> util; @@ -49,24 +42,21 @@ digraph { link -> constants; link -> crypto; link -> ev; - link -> linux; link -> messages; link -> net; link -> util; - linux -> util; - messages -> crypto; messages -> dht; messages -> exit; + messages -> link; messages -> llarp; messages -> path; messages -> routing; messages -> service; messages -> util; - net -> android; - net -> llarp; + net -> crypto; net -> util; path -> crypto; @@ -93,6 +83,8 @@ digraph { service -> routing; service -> util; + util -> constants; + llarp -> constants; llarp -> crypto; llarp -> dht; @@ -105,5 +97,6 @@ digraph { llarp -> net; llarp -> path; llarp -> routing; + llarp -> service; llarp -> util; } diff --git a/llarp/context.cpp b/llarp/context.cpp index b552a25e7..7c8891342 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include diff --git a/llarp/encrypted_frame.cpp b/llarp/crypto/encrypted_frame.cpp similarity index 98% rename from llarp/encrypted_frame.cpp rename to llarp/crypto/encrypted_frame.cpp index 803bd5db8..666ec7b26 100644 --- a/llarp/encrypted_frame.cpp +++ b/llarp/crypto/encrypted_frame.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/llarp/encrypted_frame.hpp b/llarp/crypto/encrypted_frame.hpp similarity index 100% rename from llarp/encrypted_frame.hpp rename to llarp/crypto/encrypted_frame.hpp diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 96e36e73d..62a1bfa77 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -1,8 +1,9 @@ #include + #include #include #include -#include +#include #include @@ -731,7 +732,8 @@ namespace llarp void Start(const TXOwner &peer) override { - parent->DHTSendTo(peer.node.as_array(), new FindRouterMessage(peer.txid, target)); + parent->DHTSendTo(peer.node.as_array(), + new FindRouterMessage(peer.txid, target)); } virtual void diff --git a/llarp/dht/dht_immediate.cpp b/llarp/dht/dht_immediate.cpp index 42149dba0..74b648db3 100644 --- a/llarp/dht/dht_immediate.cpp +++ b/llarp/dht/dht_immediate.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace llarp { diff --git a/llarp/dht/find_router.cpp b/llarp/dht/find_router.cpp index 7e6eb1af3..88643d664 100644 --- a/llarp/dht/find_router.cpp +++ b/llarp/dht/find_router.cpp @@ -1,8 +1,9 @@ -#include #include + +#include #include #include -#include +#include namespace llarp { diff --git a/llarp/dht/got_intro.cpp b/llarp/dht/got_intro.cpp index dccd96e7b..c60a4d28c 100644 --- a/llarp/dht/got_intro.cpp +++ b/llarp/dht/got_intro.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include namespace llarp { diff --git a/llarp/dht/got_router.cpp b/llarp/dht/got_router.cpp index c65877419..60fa5d1f7 100644 --- a/llarp/dht/got_router.cpp +++ b/llarp/dht/got_router.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/dht/publish_intro.cpp b/llarp/dht/publish_intro.cpp index 9f1cc3c90..e49a12e60 100644 --- a/llarp/dht/publish_intro.cpp +++ b/llarp/dht/publish_intro.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include namespace llarp { diff --git a/llarp/establish_job.cpp b/llarp/establish_job.cpp deleted file mode 100644 index 66b23cd45..000000000 --- a/llarp/establish_job.cpp +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/llarp/establish_job.hpp b/llarp/establish_job.hpp deleted file mode 100644 index 634fadc1b..000000000 --- a/llarp/establish_job.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef LLARP_ESTABLISH_JOB_HPP -#define LLARP_ESTABLISH_JOB_HPP - -#include - -namespace llarp -{ - struct OutboundLinkEstablishJob - { - RouterContact rc; - - OutboundLinkEstablishJob(const RouterContact& remote) : rc(remote) - { - } - - virtual ~OutboundLinkEstablishJob(){}; - - virtual void - Success() = 0; - - virtual void - Failed() = 0; - - virtual void - AttemptTimedout() = 0; - - virtual void - Attempt() = 0; - - virtual bool - ShouldRetry() const = 0; - }; -} // namespace llarp - -#endif diff --git a/llarp/exit/endpoint.cpp b/llarp/exit/endpoint.cpp index 774615e84..2ea1ae5b0 100644 --- a/llarp/exit/endpoint.cpp +++ b/llarp/exit/endpoint.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/exit/endpoint.hpp b/llarp/exit/endpoint.hpp index 7f67c41ba..0def297d1 100644 --- a/llarp/exit/endpoint.hpp +++ b/llarp/exit/endpoint.hpp @@ -2,7 +2,7 @@ #define LLARP_EXIT_ENDPOINT_HPP #include -#include +#include #include #include diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 84f2afa63..1dc30ead0 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/exit/session.hpp b/llarp/exit/session.hpp index 0f1996813..5d68627b2 100644 --- a/llarp/exit/session.hpp +++ b/llarp/exit/session.hpp @@ -1,9 +1,9 @@ #ifndef LLARP_EXIT_SESSION_HPP #define LLARP_EXIT_SESSION_HPP -#include #include #include +#include #include #include diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index c0dc224a6..04c0d9122 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index e320ad756..0e521ff16 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include namespace llarp { diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index f2e7a17cc..d7d36c65b 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include diff --git a/llarp/link/iwp.cpp b/llarp/link/iwp.cpp index 90b9f45b4..98c2fd6e9 100644 --- a/llarp/link/iwp.cpp +++ b/llarp/link/iwp.cpp @@ -1,5 +1,5 @@ #include -#include +#include namespace llarp { diff --git a/llarp/link/utp.cpp b/llarp/link/utp.cpp index 15f28520d..0f82e0d36 100644 --- a/llarp/link/utp.cpp +++ b/llarp/link/utp.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include diff --git a/llarp/link_intro.cpp b/llarp/link_intro.cpp deleted file mode 100644 index 7c75435f9..000000000 --- a/llarp/link_intro.cpp +++ /dev/null @@ -1,175 +0,0 @@ -#include - -#include -#include -#include -#include - -namespace llarp -{ - LinkIntroMessage::~LinkIntroMessage() - { - } - - bool - LinkIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) - { - if(llarp_buffer_eq(key, "a")) - { - llarp_buffer_t strbuf; - if(!bencode_read_string(buf, &strbuf)) - return false; - if(strbuf.sz != 1) - return false; - return *strbuf.cur == 'i'; - } - if(llarp_buffer_eq(key, "n")) - { - if(N.BDecode(buf)) - return true; - llarp::LogWarn("failed to decode nonce in LIM"); - return false; - } - if(llarp_buffer_eq(key, "p")) - { - return bencode_read_integer(buf, &P); - } - if(llarp_buffer_eq(key, "r")) - { - if(rc.BDecode(buf)) - return true; - llarp::LogWarn("failed to decode RC in LIM"); - llarp::DumpBuffer(*buf); - return false; - } - else if(llarp_buffer_eq(key, "v")) - { - if(!bencode_read_integer(buf, &version)) - return false; - if(version != LLARP_PROTO_VERSION) - { - llarp::LogWarn("llarp protocol version missmatch ", version, - " != ", LLARP_PROTO_VERSION); - return false; - } - llarp::LogDebug("LIM version ", version); - return true; - } - else if(llarp_buffer_eq(key, "z")) - { - return Z.BDecode(buf); - } - else - { - llarp::LogWarn("invalid LIM key: ", *key.cur); - return false; - } - } - - bool - LinkIntroMessage::BEncode(llarp_buffer_t* buf) const - { - if(!bencode_start_dict(buf)) - return false; - - if(!bencode_write_bytestring(buf, "a", 1)) - return false; - if(!bencode_write_bytestring(buf, "i", 1)) - return false; - - if(!bencode_write_bytestring(buf, "n", 1)) - return false; - if(!N.BEncode(buf)) - return false; - - if(!bencode_write_bytestring(buf, "p", 1)) - return false; - if(!bencode_write_uint64(buf, P)) - return false; - - if(!bencode_write_bytestring(buf, "r", 1)) - return false; - if(!rc.BEncode(buf)) - return false; - - if(!bencode_write_version_entry(buf)) - return false; - - if(!bencode_write_bytestring(buf, "z", 1)) - return false; - if(!Z.BEncode(buf)) - return false; - - return bencode_end(buf); - } - - LinkIntroMessage& - LinkIntroMessage::operator=(const LinkIntroMessage& msg) - { - version = msg.version; - Z = msg.Z; - rc = msg.rc; - N = msg.N; - P = msg.P; - return *this; - } - - bool - LinkIntroMessage::HandleMessage(llarp::Router* router) const - { - if(!Verify(&router->crypto)) - return false; - return session->GotLIM(this); - } - - void - LinkIntroMessage::Clear() - { - P = 0; - N.Zero(); - rc.Clear(); - Z.Zero(); - } - - bool - LinkIntroMessage::Sign( - std::function< bool(Signature&, llarp_buffer_t) > signer) - { - Z.Zero(); - byte_t tmp[MaxSize] = {0}; - auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); - if(!BEncode(&buf)) - return false; - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - return signer(Z, buf); - } - - bool - LinkIntroMessage::Verify(llarp::Crypto* c) const - { - LinkIntroMessage copy; - copy = *this; - copy.Z.Zero(); - byte_t tmp[MaxSize] = {0}; - auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); - if(!copy.BEncode(&buf)) - return false; - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - // outer signature - if(!c->verify(rc.pubkey, buf, Z)) - { - llarp::LogError("outer signature failure"); - return false; - } - // verify RC - if(!rc.Verify(c, llarp::time_now_ms())) - { - llarp::LogError("invalid RC in link intro"); - return false; - } - return true; - } - -} // namespace llarp diff --git a/llarp/messages/dht_immediate.hpp b/llarp/messages/dht_immediate.hpp index 3cb129b2a..ccbf07cd8 100644 --- a/llarp/messages/dht_immediate.hpp +++ b/llarp/messages/dht_immediate.hpp @@ -2,7 +2,7 @@ #define LLARP_MESSAGES_DHT_IMMEDIATE_HPP #include -#include +#include #include diff --git a/llarp/messages/discard.hpp b/llarp/messages/discard.hpp index 3315b965f..61a8ff56f 100644 --- a/llarp/messages/discard.hpp +++ b/llarp/messages/discard.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_MESSAGES_DISCARD_HPP #define LLARP_MESSAGES_DISCARD_HPP -#include +#include #include #include #include @@ -61,7 +61,10 @@ namespace llarp version = LLARP_PROTO_VERSION; } - void Clear() override {} + void + Clear() override + { + } bool HandleMessage(IMessageHandler* h, llarp::Router* r) const override diff --git a/llarp/messages/link_intro.cpp b/llarp/messages/link_intro.cpp index 9dbda4e55..c314e2b6a 100644 --- a/llarp/messages/link_intro.cpp +++ b/llarp/messages/link_intro.cpp @@ -1 +1,175 @@ #include + +#include +#include +#include +#include + +namespace llarp +{ + LinkIntroMessage::~LinkIntroMessage() + { + } + + bool + LinkIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) + { + if(llarp_buffer_eq(key, "a")) + { + llarp_buffer_t strbuf; + if(!bencode_read_string(buf, &strbuf)) + return false; + if(strbuf.sz != 1) + return false; + return *strbuf.cur == 'i'; + } + if(llarp_buffer_eq(key, "n")) + { + if(N.BDecode(buf)) + return true; + llarp::LogWarn("failed to decode nonce in LIM"); + return false; + } + if(llarp_buffer_eq(key, "p")) + { + return bencode_read_integer(buf, &P); + } + if(llarp_buffer_eq(key, "r")) + { + if(rc.BDecode(buf)) + return true; + llarp::LogWarn("failed to decode RC in LIM"); + llarp::DumpBuffer(*buf); + return false; + } + else if(llarp_buffer_eq(key, "v")) + { + if(!bencode_read_integer(buf, &version)) + return false; + if(version != LLARP_PROTO_VERSION) + { + llarp::LogWarn("llarp protocol version missmatch ", version, + " != ", LLARP_PROTO_VERSION); + return false; + } + llarp::LogDebug("LIM version ", version); + return true; + } + else if(llarp_buffer_eq(key, "z")) + { + return Z.BDecode(buf); + } + else + { + llarp::LogWarn("invalid LIM key: ", *key.cur); + return false; + } + } + + bool + LinkIntroMessage::BEncode(llarp_buffer_t* buf) const + { + if(!bencode_start_dict(buf)) + return false; + + if(!bencode_write_bytestring(buf, "a", 1)) + return false; + if(!bencode_write_bytestring(buf, "i", 1)) + return false; + + if(!bencode_write_bytestring(buf, "n", 1)) + return false; + if(!N.BEncode(buf)) + return false; + + if(!bencode_write_bytestring(buf, "p", 1)) + return false; + if(!bencode_write_uint64(buf, P)) + return false; + + if(!bencode_write_bytestring(buf, "r", 1)) + return false; + if(!rc.BEncode(buf)) + return false; + + if(!bencode_write_version_entry(buf)) + return false; + + if(!bencode_write_bytestring(buf, "z", 1)) + return false; + if(!Z.BEncode(buf)) + return false; + + return bencode_end(buf); + } + + LinkIntroMessage& + LinkIntroMessage::operator=(const LinkIntroMessage& msg) + { + version = msg.version; + Z = msg.Z; + rc = msg.rc; + N = msg.N; + P = msg.P; + return *this; + } + + bool + LinkIntroMessage::HandleMessage(llarp::Router* router) const + { + if(!Verify(&router->crypto)) + return false; + return session->GotLIM(this); + } + + void + LinkIntroMessage::Clear() + { + P = 0; + N.Zero(); + rc.Clear(); + Z.Zero(); + } + + bool + LinkIntroMessage::Sign( + std::function< bool(Signature&, llarp_buffer_t) > signer) + { + Z.Zero(); + byte_t tmp[MaxSize] = {0}; + auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); + if(!BEncode(&buf)) + return false; + buf.sz = buf.cur - buf.base; + buf.cur = buf.base; + return signer(Z, buf); + } + + bool + LinkIntroMessage::Verify(llarp::Crypto* c) const + { + LinkIntroMessage copy; + copy = *this; + copy.Z.Zero(); + byte_t tmp[MaxSize] = {0}; + auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); + if(!copy.BEncode(&buf)) + return false; + buf.sz = buf.cur - buf.base; + buf.cur = buf.base; + // outer signature + if(!c->verify(rc.pubkey, buf, Z)) + { + llarp::LogError("outer signature failure"); + return false; + } + // verify RC + if(!rc.Verify(c, llarp::time_now_ms())) + { + llarp::LogError("invalid RC in link intro"); + return false; + } + return true; + } + +} // namespace llarp diff --git a/llarp/messages/link_intro.hpp b/llarp/messages/link_intro.hpp index 345fb55ba..56b673894 100644 --- a/llarp/messages/link_intro.hpp +++ b/llarp/messages/link_intro.hpp @@ -2,7 +2,7 @@ #define LLARP_MESSAGES_LINK_INTRO_HPP #include -#include +#include #include namespace llarp diff --git a/llarp/link_message.cpp b/llarp/messages/link_message.cpp similarity index 98% rename from llarp/link_message.cpp rename to llarp/messages/link_message.cpp index 037c86f21..9f8c61e70 100644 --- a/llarp/link_message.cpp +++ b/llarp/messages/link_message.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/llarp/link_message.hpp b/llarp/messages/link_message.hpp similarity index 100% rename from llarp/link_message.hpp rename to llarp/messages/link_message.hpp diff --git a/llarp/link_message_parser.hpp b/llarp/messages/link_message_parser.hpp similarity index 96% rename from llarp/link_message_parser.hpp rename to llarp/messages/link_message_parser.hpp index 056a8c198..8c7c199fe 100644 --- a/llarp/link_message_parser.hpp +++ b/llarp/messages/link_message_parser.hpp @@ -1,10 +1,10 @@ #ifndef LLARP_LINK_MESSAGE_PARSER_HPP #define LLARP_LINK_MESSAGE_PARSER_HPP -#include #include #include #include +#include #include #include diff --git a/llarp/messages/relay.cpp b/llarp/messages/relay.cpp index 53f5e42a4..8703c2ad5 100644 --- a/llarp/messages/relay.cpp +++ b/llarp/messages/relay.cpp @@ -1 +1,132 @@ #include + +#include +#include + +namespace llarp +{ + RelayUpstreamMessage::RelayUpstreamMessage() : ILinkMessage() + { + } + + RelayUpstreamMessage::~RelayUpstreamMessage() + { + } + + void + RelayUpstreamMessage::Clear() + { + pathid.Zero(); + X.Clear(); + Y.Zero(); + } + + bool + RelayUpstreamMessage::BEncode(llarp_buffer_t *buf) const + { + if(!bencode_start_dict(buf)) + return false; + if(!BEncodeWriteDictMsgType(buf, "a", "u")) + return false; + + if(!BEncodeWriteDictEntry("p", pathid, buf)) + return false; + if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf)) + return false; + if(!BEncodeWriteDictEntry("x", X, buf)) + return false; + if(!BEncodeWriteDictEntry("y", Y, buf)) + return false; + return bencode_end(buf); + } + + bool + RelayUpstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf) + { + bool read = false; + if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf)) + return false; + if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key, + buf)) + return false; + if(!BEncodeMaybeReadDictEntry("x", X, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictEntry("y", Y, read, key, buf)) + return false; + return read; + } + + bool + RelayUpstreamMessage::HandleMessage(llarp::Router *r) const + { + auto path = r->paths.GetByDownstream(session->GetPubKey(), pathid); + if(path) + { + return path->HandleUpstream(X.Buffer(), Y, r); + } + return false; + } + + RelayDownstreamMessage::RelayDownstreamMessage() : ILinkMessage() + { + } + + RelayDownstreamMessage::~RelayDownstreamMessage() + { + } + + void + RelayDownstreamMessage::Clear() + { + pathid.Zero(); + X.Clear(); + Y.Zero(); + } + + bool + RelayDownstreamMessage::BEncode(llarp_buffer_t *buf) const + { + if(!bencode_start_dict(buf)) + return false; + if(!BEncodeWriteDictMsgType(buf, "a", "d")) + return false; + + if(!BEncodeWriteDictEntry("p", pathid, buf)) + return false; + if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf)) + return false; + if(!BEncodeWriteDictEntry("x", X, buf)) + return false; + if(!BEncodeWriteDictEntry("y", Y, buf)) + return false; + return bencode_end(buf); + } + + bool + RelayDownstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf) + { + bool read = false; + if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf)) + return false; + if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key, + buf)) + return false; + if(!BEncodeMaybeReadDictEntry("x", X, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictEntry("y", Y, read, key, buf)) + return false; + return read; + } + + bool + RelayDownstreamMessage::HandleMessage(llarp::Router *r) const + { + auto path = r->paths.GetByUpstream(session->GetPubKey(), pathid); + if(path) + { + return path->HandleDownstream(X.Buffer(), Y, r); + } + llarp::LogWarn("unhandled downstream message"); + return false; + } +} // namespace llarp diff --git a/llarp/messages/relay.hpp b/llarp/messages/relay.hpp index ca2b09e1c..89e6599e8 100644 --- a/llarp/messages/relay.hpp +++ b/llarp/messages/relay.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include diff --git a/llarp/messages/relay_commit.cpp b/llarp/messages/relay_commit.cpp index c0cc728b6..4a855225e 100644 --- a/llarp/messages/relay_commit.cpp +++ b/llarp/messages/relay_commit.cpp @@ -1 +1,345 @@ #include + +#include +#include +#include +#include +#include +#include + +namespace llarp +{ + LR_CommitMessage::~LR_CommitMessage() + { + } + + bool + LR_CommitMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) + { + if(llarp_buffer_eq(key, "c")) + { + return BEncodeReadArray(frames, buf); + } + bool read = false; + if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key, + buf)) + return false; + + return read; + } + + void + LR_CommitMessage::Clear() + { + frames[0].Clear(); + frames[1].Clear(); + frames[2].Clear(); + frames[3].Clear(); + frames[4].Clear(); + frames[5].Clear(); + frames[6].Clear(); + frames[7].Clear(); + } + + bool + LR_CommitMessage::BEncode(llarp_buffer_t* buf) const + { + if(!bencode_start_dict(buf)) + return false; + // msg type + if(!BEncodeWriteDictMsgType(buf, "a", "c")) + return false; + // frames + if(!BEncodeWriteDictArray("c", frames, buf)) + return false; + // version + if(!bencode_write_version_entry(buf)) + return false; + + return bencode_end(buf); + } + + bool + LR_CommitMessage::HandleMessage(llarp::Router* router) const + { + if(frames.size() != MAXHOPS) + { + llarp::LogError("LRCM invalid number of records, ", frames.size(), + "!=", MAXHOPS); + return false; + } + if(!router->paths.AllowingTransit()) + { + llarp::LogError("got LRCM when not permitting transit"); + return false; + } + return AsyncDecrypt(&router->paths); + } + + bool + LR_CommitRecord::BEncode(llarp_buffer_t* buf) const + { + if(!bencode_start_dict(buf)) + return false; + + if(!BEncodeWriteDictEntry("c", commkey, buf)) + return false; + if(!BEncodeWriteDictEntry("i", nextHop, buf)) + return false; + if(lifetime > 10 && lifetime < 600) + { + if(!BEncodeWriteDictInt("i", lifetime, buf)) + return false; + } + if(!BEncodeWriteDictEntry("n", tunnelNonce, buf)) + return false; + if(!BEncodeWriteDictEntry("r", rxid, buf)) + return false; + if(!BEncodeWriteDictEntry("t", txid, buf)) + return false; + if(!bencode_write_version_entry(buf)) + return false; + if(work && !BEncodeWriteDictEntry("w", *work, buf)) + return false; + + return bencode_end(buf); + } + + LR_CommitRecord::~LR_CommitRecord() + { + if(work) + delete work; + } + + bool + LR_CommitRecord::OnKey(dict_reader* r, llarp_buffer_t* key) + { + if(!key) + return true; + + LR_CommitRecord* self = static_cast< LR_CommitRecord* >(r->user); + + bool read = false; + + if(!BEncodeMaybeReadDictEntry("c", self->commkey, read, *key, r->buffer)) + return false; + if(!BEncodeMaybeReadDictEntry("i", self->nextHop, read, *key, r->buffer)) + return false; + if(!BEncodeMaybeReadDictInt("l", self->lifetime, read, *key, r->buffer)) + return false; + if(!BEncodeMaybeReadDictEntry("n", self->tunnelNonce, read, *key, + r->buffer)) + return false; + if(!BEncodeMaybeReadDictEntry("r", self->rxid, read, *key, r->buffer)) + return false; + if(!BEncodeMaybeReadDictEntry("t", self->txid, read, *key, r->buffer)) + return false; + if(!BEncodeMaybeReadVersion("v", self->version, LLARP_PROTO_VERSION, read, + *key, r->buffer)) + return false; + if(llarp_buffer_eq(*key, "w")) + { + // check for duplicate + if(self->work) + { + llarp::LogWarn("duplicate POW in LRCR"); + return false; + } + + self->work = new PoW(); + return self->work->BDecode(r->buffer); + } + return read; + } + + bool + LR_CommitRecord::BDecode(llarp_buffer_t* buf) + { + dict_reader r; + r.user = this; + r.on_key = &OnKey; + return bencode_read_dict(buf, &r); + } + + bool + LR_CommitRecord::operator==(const LR_CommitRecord& other) const + { + if(work && other.work) + { + if(*work != *other.work) + return false; + } + return nextHop == other.nextHop && commkey == other.commkey + && txid == other.txid && rxid == other.rxid; + } + + struct LRCMFrameDecrypt + { + typedef llarp::path::PathContext Context; + typedef llarp::path::TransitHop Hop; + typedef AsyncFrameDecrypter< LRCMFrameDecrypt > Decrypter; + Decrypter* decrypter; + std::array< EncryptedFrame, 8 > frames; + Context* context; + // decrypted record + LR_CommitRecord record; + // the actual hop + std::shared_ptr< Hop > hop; + + LRCMFrameDecrypt(Context* ctx, Decrypter* dec, + const LR_CommitMessage* commit) + : decrypter(dec), frames(commit->frames), context(ctx), hop(new Hop()) + { + hop->info.downstream = commit->session->GetPubKey(); + } + + ~LRCMFrameDecrypt() + { + delete decrypter; + } + + /// this is done from logic thread + static void + SendLRCM(void* user) + { + LRCMFrameDecrypt* self = static_cast< LRCMFrameDecrypt* >(user); + // persist sessions to upstream and downstream routers until the commit + // ends + self->context->Router()->PersistSessionUntil(self->hop->info.downstream, + self->hop->ExpireTime()); + self->context->Router()->PersistSessionUntil(self->hop->info.upstream, + self->hop->ExpireTime()); + // put hop + self->context->PutTransitHop(self->hop); + // forward to next hop + self->context->ForwardLRCM(self->hop->info.upstream, self->frames); + self->hop = nullptr; + delete self; + } + + // this is called from the logic thread + static void + SendPathConfirm(void* user) + { + LRCMFrameDecrypt* self = static_cast< LRCMFrameDecrypt* >(user); + // persist session to downstream until path expiration + self->context->Router()->PersistSessionUntil(self->hop->info.downstream, + self->hop->ExpireTime()); + // put hop + self->context->PutTransitHop(self->hop); + // send path confirmation + llarp::routing::PathConfirmMessage confirm(self->hop->lifetime); + if(!self->hop->SendRoutingMessage(&confirm, self->context->Router())) + { + llarp::LogError("failed to send path confirmation for ", + self->hop->info); + } + self->hop = nullptr; + delete self; + } + + static void + HandleDecrypted(llarp_buffer_t* buf, LRCMFrameDecrypt* self) + { + auto now = self->context->Router()->Now(); + auto& info = self->hop->info; + if(!buf) + { + llarp::LogError("LRCM decrypt failed from ", info.downstream); + delete self; + return; + } + buf->cur = buf->base + EncryptedFrameOverheadSize; + llarp::LogDebug("decrypted LRCM from ", info.downstream); + // successful decrypt + if(!self->record.BDecode(buf)) + { + llarp::LogError("malformed frame inside LRCM from ", info.downstream); + delete self; + return; + } + + info.txID = self->record.txid; + info.rxID = self->record.rxid; + info.upstream = self->record.nextHop; + if(self->context->HasTransitHop(info)) + { + llarp::LogError("duplicate transit hop ", info); + delete self; + return; + } + // generate path key as we are in a worker thread + auto DH = self->context->Crypto()->dh_server; + if(!DH(self->hop->pathKey, self->record.commkey, + self->context->EncryptionSecretKey(), self->record.tunnelNonce)) + { + llarp::LogError("LRCM DH Failed ", info); + delete self; + return; + } + // generate hash of hop key for nonce mutation + self->context->Crypto()->shorthash(self->hop->nonceXOR, + self->hop->pathKey.as_buffer()); + if(self->record.work + && self->record.work->IsValid(self->context->Crypto()->shorthash, now)) + { + llarp::LogDebug("LRCM extended lifetime by ", + self->record.work->extendedLifetime, " seconds for ", + info); + self->hop->lifetime += 1000 * self->record.work->extendedLifetime; + } + else if(self->record.lifetime < 600 && self->record.lifetime > 10) + { + self->hop->lifetime = self->record.lifetime; + llarp::LogDebug("LRCM short lifespan set to ", self->hop->lifetime, + " seconds for ", info); + } + + // TODO: check if we really want to accept it + self->hop->started = now; + + size_t sz = self->frames[0].size(); + // shift + std::array< EncryptedFrame, 8 > frames; + frames[0] = self->frames[1]; + frames[1] = self->frames[2]; + frames[2] = self->frames[3]; + frames[3] = self->frames[4]; + frames[4] = self->frames[5]; + frames[5] = self->frames[6]; + frames[6] = self->frames[7]; + // put our response on the end + frames[7] = EncryptedFrame(sz - EncryptedFrameOverheadSize); + // random junk for now + frames[7].Randomize(); + self->frames = std::move(frames); + if(self->context->HopIsUs(info.upstream)) + { + // we are the farthest hop + llarp::LogDebug("We are the farthest hop for ", info); + // send a LRAM down the path + self->context->Logic()->queue_job({self, &SendPathConfirm}); + } + else + { + // forward upstream + // we are still in the worker thread so post job to logic + self->context->Logic()->queue_job({self, &SendLRCM}); + } + } + }; + + bool + LR_CommitMessage::AsyncDecrypt(llarp::path::PathContext* context) const + { + LRCMFrameDecrypt::Decrypter* decrypter = new LRCMFrameDecrypt::Decrypter( + context->Crypto(), context->EncryptionSecretKey(), + &LRCMFrameDecrypt::HandleDecrypted); + // copy frames so we own them + LRCMFrameDecrypt* frames = new LRCMFrameDecrypt(context, decrypter, this); + + // decrypt frames async + decrypter->AsyncDecrypt(context->Worker(), frames->frames[0], frames); + return true; + } +} // namespace llarp diff --git a/llarp/messages/relay_commit.hpp b/llarp/messages/relay_commit.hpp index 2a3307708..dfc91941a 100644 --- a/llarp/messages/relay_commit.hpp +++ b/llarp/messages/relay_commit.hpp @@ -1,9 +1,9 @@ #ifndef LLARP_RELAY_COMMIT_HPP #define LLARP_RELAY_COMMIT_HPP +#include #include -#include -#include +#include #include #include diff --git a/llarp/address_info.cpp b/llarp/net/address_info.cpp similarity index 99% rename from llarp/address_info.cpp rename to llarp/net/address_info.cpp index 6816821ff..ef1cacc0f 100644 --- a/llarp/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -1,4 +1,5 @@ -#include +#include + #ifndef _WIN32 #include #endif diff --git a/llarp/address_info.hpp b/llarp/net/address_info.hpp similarity index 100% rename from llarp/address_info.hpp rename to llarp/net/address_info.hpp diff --git a/llarp/exit_info.cpp b/llarp/net/exit_info.cpp similarity index 97% rename from llarp/exit_info.cpp rename to llarp/net/exit_info.cpp index f0e725c07..593c84797 100644 --- a/llarp/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -2,7 +2,7 @@ #include #endif -#include +#include #include #include diff --git a/llarp/exit_info.hpp b/llarp/net/exit_info.hpp similarity index 100% rename from llarp/exit_info.hpp rename to llarp/net/exit_info.hpp diff --git a/llarp/ip.cpp b/llarp/net/ip.cpp similarity index 99% rename from llarp/ip.cpp rename to llarp/net/ip.cpp index b0204d27e..c2c01b3fd 100644 --- a/llarp/ip.cpp +++ b/llarp/net/ip.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/llarp/ip.hpp b/llarp/net/ip.hpp similarity index 100% rename from llarp/ip.hpp rename to llarp/net/ip.hpp diff --git a/llarp/net/net.hpp b/llarp/net/net.hpp index a28cae3a3..0d8c6e2f0 100644 --- a/llarp/net/net.hpp +++ b/llarp/net/net.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_NET_HPP #define LLARP_NET_HPP -#include +#include #include #include #include diff --git a/llarp/net/net_addr.hpp b/llarp/net/net_addr.hpp index 07c525ccf..d891d340b 100644 --- a/llarp/net/net_addr.hpp +++ b/llarp/net/net_addr.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_NET_ADDR_HPP #define LLARP_NET_ADDR_HPP -#include +#include #include #include #include diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index de80c625d..8e3d6f6b5 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -1,10 +1,9 @@ #include -#include #include #include #include -#include +#include #include #include diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index e52acf7de..620dc110e 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -1,8 +1,8 @@ #ifndef LLARP_PATH_HPP #define LLARP_PATH_HPP +#include #include -#include #include #include #include @@ -33,6 +33,8 @@ namespace llarp { struct Crypto; + struct LR_CommitMessage; + struct LR_CommitRecord; namespace path { struct TransitHopInfo diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index f0926c4c5..f8be5cf27 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include diff --git a/llarp/transit_hop.cpp b/llarp/path/transit_hop.cpp similarity index 99% rename from llarp/transit_hop.cpp rename to llarp/path/transit_hop.cpp index 8085a0ed3..2a4e05983 100644 --- a/llarp/transit_hop.cpp +++ b/llarp/path/transit_hop.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/llarp/pow.cpp b/llarp/pow.cpp index 6f96abffd..298744206 100644 --- a/llarp/pow.cpp +++ b/llarp/pow.cpp @@ -1 +1,58 @@ #include + +#include + +#include + +namespace llarp +{ + PoW::~PoW() + { + } + + bool + PoW::DecodeKey(__attribute__((unused)) llarp_buffer_t k, + __attribute__((unused)) llarp_buffer_t* val) + { + // TODO: implement me + return false; + } + + bool + PoW::BEncode(llarp_buffer_t* buf) const + { + // TODO: implement me + if(!bencode_start_dict(buf)) + return false; + return bencode_end(buf); + } + + bool + PoW::IsValid(shorthash_func hashfunc, llarp_time_t now) const + { + if(now - timestamp > (uint64_t(extendedLifetime) * 1000)) + return false; + + ShortHash digest; + byte_t tmp[MaxSize]; + auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); + // encode + if(!BEncode(&buf)) + return false; + // rewind + buf.sz = buf.cur - buf.base; + buf.cur = buf.base; + // hash + if(!hashfunc(digest, buf)) + return false; + // check bytes required + uint32_t required = std::floor(std::log(extendedLifetime)); + for(uint32_t idx = 0; idx < required; ++idx) + { + if(digest[idx]) + return false; + } + return true; + } + +} // namespace llarp diff --git a/llarp/proofofwork.cpp b/llarp/proofofwork.cpp deleted file mode 100644 index 298744206..000000000 --- a/llarp/proofofwork.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include - -#include - -#include - -namespace llarp -{ - PoW::~PoW() - { - } - - bool - PoW::DecodeKey(__attribute__((unused)) llarp_buffer_t k, - __attribute__((unused)) llarp_buffer_t* val) - { - // TODO: implement me - return false; - } - - bool - PoW::BEncode(llarp_buffer_t* buf) const - { - // TODO: implement me - if(!bencode_start_dict(buf)) - return false; - return bencode_end(buf); - } - - bool - PoW::IsValid(shorthash_func hashfunc, llarp_time_t now) const - { - if(now - timestamp > (uint64_t(extendedLifetime) * 1000)) - return false; - - ShortHash digest; - byte_t tmp[MaxSize]; - auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); - // encode - if(!BEncode(&buf)) - return false; - // rewind - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - // hash - if(!hashfunc(digest, buf)) - return false; - // check bytes required - uint32_t required = std::floor(std::log(extendedLifetime)); - for(uint32_t idx = 0; idx < required; ++idx) - { - if(digest[idx]) - return false; - } - return true; - } - -} // namespace llarp diff --git a/llarp/relay_commit.cpp b/llarp/relay_commit.cpp deleted file mode 100644 index 1513f1416..000000000 --- a/llarp/relay_commit.cpp +++ /dev/null @@ -1,344 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -namespace llarp -{ - LR_CommitMessage::~LR_CommitMessage() - { - } - - bool - LR_CommitMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) - { - if(llarp_buffer_eq(key, "c")) - { - return BEncodeReadArray(frames, buf); - } - bool read = false; - if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key, - buf)) - return false; - - return read; - } - - void - LR_CommitMessage::Clear() - { - frames[0].Clear(); - frames[1].Clear(); - frames[2].Clear(); - frames[3].Clear(); - frames[4].Clear(); - frames[5].Clear(); - frames[6].Clear(); - frames[7].Clear(); - } - - bool - LR_CommitMessage::BEncode(llarp_buffer_t* buf) const - { - if(!bencode_start_dict(buf)) - return false; - // msg type - if(!BEncodeWriteDictMsgType(buf, "a", "c")) - return false; - // frames - if(!BEncodeWriteDictArray("c", frames, buf)) - return false; - // version - if(!bencode_write_version_entry(buf)) - return false; - - return bencode_end(buf); - } - - bool - LR_CommitMessage::HandleMessage(llarp::Router* router) const - { - if(frames.size() != MAXHOPS) - { - llarp::LogError("LRCM invalid number of records, ", frames.size(), - "!=", MAXHOPS); - return false; - } - if(!router->paths.AllowingTransit()) - { - llarp::LogError("got LRCM when not permitting transit"); - return false; - } - return AsyncDecrypt(&router->paths); - } - - bool - LR_CommitRecord::BEncode(llarp_buffer_t* buf) const - { - if(!bencode_start_dict(buf)) - return false; - - if(!BEncodeWriteDictEntry("c", commkey, buf)) - return false; - if(!BEncodeWriteDictEntry("i", nextHop, buf)) - return false; - if(lifetime > 10 && lifetime < 600) - { - if(!BEncodeWriteDictInt("i", lifetime, buf)) - return false; - } - if(!BEncodeWriteDictEntry("n", tunnelNonce, buf)) - return false; - if(!BEncodeWriteDictEntry("r", rxid, buf)) - return false; - if(!BEncodeWriteDictEntry("t", txid, buf)) - return false; - if(!bencode_write_version_entry(buf)) - return false; - if(work && !BEncodeWriteDictEntry("w", *work, buf)) - return false; - - return bencode_end(buf); - } - - LR_CommitRecord::~LR_CommitRecord() - { - if(work) - delete work; - } - - bool - LR_CommitRecord::OnKey(dict_reader* r, llarp_buffer_t* key) - { - if(!key) - return true; - - LR_CommitRecord* self = static_cast< LR_CommitRecord* >(r->user); - - bool read = false; - - if(!BEncodeMaybeReadDictEntry("c", self->commkey, read, *key, r->buffer)) - return false; - if(!BEncodeMaybeReadDictEntry("i", self->nextHop, read, *key, r->buffer)) - return false; - if(!BEncodeMaybeReadDictInt("l", self->lifetime, read, *key, r->buffer)) - return false; - if(!BEncodeMaybeReadDictEntry("n", self->tunnelNonce, read, *key, - r->buffer)) - return false; - if(!BEncodeMaybeReadDictEntry("r", self->rxid, read, *key, r->buffer)) - return false; - if(!BEncodeMaybeReadDictEntry("t", self->txid, read, *key, r->buffer)) - return false; - if(!BEncodeMaybeReadVersion("v", self->version, LLARP_PROTO_VERSION, read, - *key, r->buffer)) - return false; - if(llarp_buffer_eq(*key, "w")) - { - // check for duplicate - if(self->work) - { - llarp::LogWarn("duplicate POW in LRCR"); - return false; - } - - self->work = new PoW(); - return self->work->BDecode(r->buffer); - } - return read; - } - - bool - LR_CommitRecord::BDecode(llarp_buffer_t* buf) - { - dict_reader r; - r.user = this; - r.on_key = &OnKey; - return bencode_read_dict(buf, &r); - } - - bool - LR_CommitRecord::operator==(const LR_CommitRecord& other) const - { - if(work && other.work) - { - if(*work != *other.work) - return false; - } - return nextHop == other.nextHop && commkey == other.commkey - && txid == other.txid && rxid == other.rxid; - } - - struct LRCMFrameDecrypt - { - typedef llarp::path::PathContext Context; - typedef llarp::path::TransitHop Hop; - typedef AsyncFrameDecrypter< LRCMFrameDecrypt > Decrypter; - Decrypter* decrypter; - std::array< EncryptedFrame, 8 > frames; - Context* context; - // decrypted record - LR_CommitRecord record; - // the actual hop - std::shared_ptr< Hop > hop; - - LRCMFrameDecrypt(Context* ctx, Decrypter* dec, - const LR_CommitMessage* commit) - : decrypter(dec), frames(commit->frames), context(ctx), hop(new Hop()) - { - hop->info.downstream = commit->session->GetPubKey(); - } - - ~LRCMFrameDecrypt() - { - delete decrypter; - } - - /// this is done from logic thread - static void - SendLRCM(void* user) - { - LRCMFrameDecrypt* self = static_cast< LRCMFrameDecrypt* >(user); - // persist sessions to upstream and downstream routers until the commit - // ends - self->context->Router()->PersistSessionUntil(self->hop->info.downstream, - self->hop->ExpireTime()); - self->context->Router()->PersistSessionUntil(self->hop->info.upstream, - self->hop->ExpireTime()); - // put hop - self->context->PutTransitHop(self->hop); - // forward to next hop - self->context->ForwardLRCM(self->hop->info.upstream, self->frames); - self->hop = nullptr; - delete self; - } - - // this is called from the logic thread - static void - SendPathConfirm(void* user) - { - LRCMFrameDecrypt* self = static_cast< LRCMFrameDecrypt* >(user); - // persist session to downstream until path expiration - self->context->Router()->PersistSessionUntil(self->hop->info.downstream, - self->hop->ExpireTime()); - // put hop - self->context->PutTransitHop(self->hop); - // send path confirmation - llarp::routing::PathConfirmMessage confirm(self->hop->lifetime); - if(!self->hop->SendRoutingMessage(&confirm, self->context->Router())) - { - llarp::LogError("failed to send path confirmation for ", - self->hop->info); - } - self->hop = nullptr; - delete self; - } - - static void - HandleDecrypted(llarp_buffer_t* buf, LRCMFrameDecrypt* self) - { - auto now = self->context->Router()->Now(); - auto& info = self->hop->info; - if(!buf) - { - llarp::LogError("LRCM decrypt failed from ", info.downstream); - delete self; - return; - } - buf->cur = buf->base + EncryptedFrameOverheadSize; - llarp::LogDebug("decrypted LRCM from ", info.downstream); - // successful decrypt - if(!self->record.BDecode(buf)) - { - llarp::LogError("malformed frame inside LRCM from ", info.downstream); - delete self; - return; - } - - info.txID = self->record.txid; - info.rxID = self->record.rxid; - info.upstream = self->record.nextHop; - if(self->context->HasTransitHop(info)) - { - llarp::LogError("duplicate transit hop ", info); - delete self; - return; - } - // generate path key as we are in a worker thread - auto DH = self->context->Crypto()->dh_server; - if(!DH(self->hop->pathKey, self->record.commkey, - self->context->EncryptionSecretKey(), self->record.tunnelNonce)) - { - llarp::LogError("LRCM DH Failed ", info); - delete self; - return; - } - // generate hash of hop key for nonce mutation - self->context->Crypto()->shorthash(self->hop->nonceXOR, - self->hop->pathKey.as_buffer()); - if(self->record.work - && self->record.work->IsValid(self->context->Crypto()->shorthash, now)) - { - llarp::LogDebug("LRCM extended lifetime by ", - self->record.work->extendedLifetime, " seconds for ", - info); - self->hop->lifetime += 1000 * self->record.work->extendedLifetime; - } - else if(self->record.lifetime < 600 && self->record.lifetime > 10) - { - self->hop->lifetime = self->record.lifetime; - llarp::LogDebug("LRCM short lifespan set to ", self->hop->lifetime, - " seconds for ", info); - } - - // TODO: check if we really want to accept it - self->hop->started = now; - - size_t sz = self->frames[0].size(); - // shift - std::array< EncryptedFrame, 8 > frames; - frames[0] = self->frames[1]; - frames[1] = self->frames[2]; - frames[2] = self->frames[3]; - frames[3] = self->frames[4]; - frames[4] = self->frames[5]; - frames[5] = self->frames[6]; - frames[6] = self->frames[7]; - // put our response on the end - frames[7] = EncryptedFrame(sz - EncryptedFrameOverheadSize); - // random junk for now - frames[7].Randomize(); - self->frames = std::move(frames); - if(self->context->HopIsUs(info.upstream)) - { - // we are the farthest hop - llarp::LogDebug("We are the farthest hop for ", info); - // send a LRAM down the path - self->context->Logic()->queue_job({self, &SendPathConfirm}); - } - else - { - // forward upstream - // we are still in the worker thread so post job to logic - self->context->Logic()->queue_job({self, &SendLRCM}); - } - } - }; - - bool - LR_CommitMessage::AsyncDecrypt(llarp::path::PathContext* context) const - { - LRCMFrameDecrypt::Decrypter* decrypter = new LRCMFrameDecrypt::Decrypter( - context->Crypto(), context->EncryptionSecretKey(), - &LRCMFrameDecrypt::HandleDecrypted); - // copy frames so we own them - LRCMFrameDecrypt* frames = new LRCMFrameDecrypt(context, decrypter, this); - - // decrypt frames async - decrypter->AsyncDecrypt(context->Worker(), frames->frames[0], frames); - return true; - } -} // namespace llarp diff --git a/llarp/relay_up_down.cpp b/llarp/relay_up_down.cpp deleted file mode 100644 index 696e2ba21..000000000 --- a/llarp/relay_up_down.cpp +++ /dev/null @@ -1,131 +0,0 @@ -#include -#include -#include - -namespace llarp -{ - RelayUpstreamMessage::RelayUpstreamMessage() : ILinkMessage() - { - } - - RelayUpstreamMessage::~RelayUpstreamMessage() - { - } - - void - RelayUpstreamMessage::Clear() - { - pathid.Zero(); - X.Clear(); - Y.Zero(); - } - - bool - RelayUpstreamMessage::BEncode(llarp_buffer_t *buf) const - { - if(!bencode_start_dict(buf)) - return false; - if(!BEncodeWriteDictMsgType(buf, "a", "u")) - return false; - - if(!BEncodeWriteDictEntry("p", pathid, buf)) - return false; - if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf)) - return false; - if(!BEncodeWriteDictEntry("x", X, buf)) - return false; - if(!BEncodeWriteDictEntry("y", Y, buf)) - return false; - return bencode_end(buf); - } - - bool - RelayUpstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf) - { - bool read = false; - if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf)) - return false; - if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key, - buf)) - return false; - if(!BEncodeMaybeReadDictEntry("x", X, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictEntry("y", Y, read, key, buf)) - return false; - return read; - } - - bool - RelayUpstreamMessage::HandleMessage(llarp::Router *r) const - { - auto path = r->paths.GetByDownstream(session->GetPubKey(), pathid); - if(path) - { - return path->HandleUpstream(X.Buffer(), Y, r); - } - return false; - } - - RelayDownstreamMessage::RelayDownstreamMessage() : ILinkMessage() - { - } - - RelayDownstreamMessage::~RelayDownstreamMessage() - { - } - - void - RelayDownstreamMessage::Clear() - { - pathid.Zero(); - X.Clear(); - Y.Zero(); - } - - bool - RelayDownstreamMessage::BEncode(llarp_buffer_t *buf) const - { - if(!bencode_start_dict(buf)) - return false; - if(!BEncodeWriteDictMsgType(buf, "a", "d")) - return false; - - if(!BEncodeWriteDictEntry("p", pathid, buf)) - return false; - if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf)) - return false; - if(!BEncodeWriteDictEntry("x", X, buf)) - return false; - if(!BEncodeWriteDictEntry("y", Y, buf)) - return false; - return bencode_end(buf); - } - - bool - RelayDownstreamMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf) - { - bool read = false; - if(!BEncodeMaybeReadDictEntry("p", pathid, read, key, buf)) - return false; - if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key, - buf)) - return false; - if(!BEncodeMaybeReadDictEntry("x", X, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictEntry("y", Y, read, key, buf)) - return false; - return read; - } - - bool - RelayDownstreamMessage::HandleMessage(llarp::Router *r) const - { - auto path = r->paths.GetByUpstream(session->GetPubKey(), pathid); - if(path) - { - return path->HandleDownstream(X.Buffer(), Y, r); - } - llarp::LogWarn("unhandled downstream message"); - return false; - } -} // namespace llarp diff --git a/llarp/router.cpp b/llarp/router/router.cpp similarity index 99% rename from llarp/router.cpp rename to llarp/router/router.cpp index c14563f77..91d2edcc3 100644 --- a/llarp/router.cpp +++ b/llarp/router/router.cpp @@ -1,14 +1,14 @@ -#include +#include #include #include #include -#include #include #include #include +#include #include -#include +#include #include #include #include diff --git a/llarp/router.hpp b/llarp/router/router.hpp similarity index 99% rename from llarp/router.hpp rename to llarp/router/router.hpp index 0607c0ef4..c38075fdf 100644 --- a/llarp/router.hpp +++ b/llarp/router/router.hpp @@ -4,18 +4,17 @@ #include #include #include -#include #include #include #include -#include +#include #include #include #include #include #include #include -#include +#include #include #include #include diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 7bcde3965..a110419df 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -1,10 +1,10 @@ #ifndef LLARP_RC_HPP #define LLARP_RC_HPP -#include #include #include -#include +#include +#include #include #include diff --git a/llarp/routing/dht_message.cpp b/llarp/routing/dht_message.cpp index 772158a95..a854223a1 100644 --- a/llarp/routing/dht_message.cpp +++ b/llarp/routing/dht_message.cpp @@ -1,5 +1,6 @@ #include -#include + +#include namespace llarp { diff --git a/llarp/routing/path_transfer.cpp b/llarp/routing/path_transfer.cpp index e7555f7d1..625a95eb6 100644 --- a/llarp/routing/path_transfer.cpp +++ b/llarp/routing/path_transfer.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include namespace llarp diff --git a/llarp/routing_endpoint.cpp b/llarp/routing_endpoint.cpp deleted file mode 100644 index 2c8721887..000000000 --- a/llarp/routing_endpoint.cpp +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/llarp/routing_endpoint.hpp b/llarp/routing_endpoint.hpp deleted file mode 100644 index f23665f67..000000000 --- a/llarp/routing_endpoint.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef LLARP_ROUTING_ENDPOINT_HPP -#define LLARP_ROUTING_ENDPOINT_HPP - -#include -#include - -namespace llarp -{ - using RoutingEndpoint_t = AlignedBuffer< 32 >; - - /// Interface for end to end crypto between endpoints - struct IRoutingEndpoint - { - virtual ~IRoutingEndpoint(){}; - }; -} // namespace llarp - -#endif diff --git a/llarp/rpc.cpp b/llarp/rpc/rpc.cpp similarity index 80% rename from llarp/rpc.cpp rename to llarp/rpc/rpc.cpp index b887f8aad..b94a9b82b 100644 --- a/llarp/rpc.cpp +++ b/llarp/rpc/rpc.cpp @@ -1,9 +1,11 @@ -#include -#include +#include + +#include #ifdef USE_ABYSS #include #endif + namespace llarp { namespace rpc @@ -51,7 +53,7 @@ namespace llarp struct GetServiceNodeListHandler final : public CallerHandler { - using PubkeyList_t = std::vector< llarp::PubKey >; + using PubkeyList_t = std::vector< PubKey >; using Callback_t = std::function< void(const PubkeyList_t&, bool) >; ~GetServiceNodeListHandler() @@ -90,7 +92,8 @@ namespace llarp if(key_itr->IsString()) { keys.emplace_back(); - if(!HexDecode(key_itr->GetString(), keys.back().begin(), decltype(keys)::value_type::SIZE)) + if(!HexDecode(key_itr->GetString(), keys.back().begin(), + decltype(keys)::value_type::SIZE)) { keys.pop_back(); } @@ -110,12 +113,12 @@ namespace llarp struct CallerImpl : public ::abyss::http::JSONRPC { - llarp::Router* router; + Router* router; llarp_time_t m_NextKeyUpdate; const llarp_time_t KeyUpdateInterval = 1000 * 60 * 2; using PubkeyList_t = GetServiceNodeListHandler::PubkeyList_t; - CallerImpl(llarp::Router* r) : ::abyss::http::JSONRPC(), router(r) + CallerImpl(Router* r) : ::abyss::http::JSONRPC(), router(r) { } @@ -133,7 +136,7 @@ namespace llarp void AsyncUpdatePubkeyList() { - llarp::LogInfo("Updating service node list"); + LogInfo("Updating service node list"); ::abyss::json::Value params; params.SetObject(); QueueRPC("/get_all_service_node_keys", std::move(params), @@ -165,11 +168,11 @@ namespace llarp for(const auto& pk : list) router->lokinetRouters.insert(std::make_pair( pk.data(), std::numeric_limits< llarp_time_t >::max())); - llarp::LogInfo("updated service node list, we have ", - router->lokinetRouters.size(), " authorized routers"); + LogInfo("updated service node list, we have ", + router->lokinetRouters.size(), " authorized routers"); } else - llarp::LogError("service node list not updated"); + LogError("service node list not updated"); } ~CallerImpl() @@ -179,8 +182,8 @@ namespace llarp struct Handler : public ::abyss::httpd::IRPCHandler { - llarp::Router* router; - Handler(::abyss::httpd::ConnImpl* conn, llarp::Router* r) + Router* router; + Handler(::abyss::httpd::ConnImpl* conn, Router* r) : ::abyss::httpd::IRPCHandler(conn), router(r) { } @@ -192,7 +195,7 @@ namespace llarp bool ListExitLevels(Response& resp) const { - llarp::exit::Context::TrafficStats stats; + exit::Context::TrafficStats stats; router->exitContext.CalculateExitTraffic(stats); auto& alloc = resp.GetAllocator(); abyss::json::Value exits; @@ -219,23 +222,22 @@ namespace llarp auto& alloc = resp.GetAllocator(); abyss::json::Value peers; peers.SetArray(); - router->ForEachPeer( - [&](const llarp::ILinkSession* session, bool outbound) { - abyss::json::Value peer; - peer.SetObject(); - abyss::json::Value ident_val, addr_val; + router->ForEachPeer([&](const ILinkSession* session, bool outbound) { + abyss::json::Value peer; + peer.SetObject(); + abyss::json::Value ident_val, addr_val; - auto ident = RouterID(session->GetPubKey()).ToString(); - ident_val.SetString(ident.c_str(), alloc); + auto ident = RouterID(session->GetPubKey()).ToString(); + ident_val.SetString(ident.c_str(), alloc); - auto addr = session->GetRemoteEndpoint().ToString(); - addr_val.SetString(addr.c_str(), alloc); + auto addr = session->GetRemoteEndpoint().ToString(); + addr_val.SetString(addr.c_str(), alloc); - peer.AddMember("addr", addr_val, alloc); - peer.AddMember("ident", ident_val, alloc); - peer.AddMember("outbound", abyss::json::Value(outbound), alloc); - peers.PushBack(peer, alloc); - }); + peer.AddMember("addr", addr_val, alloc); + peer.AddMember("ident", ident_val, alloc); + peer.AddMember("outbound", abyss::json::Value(outbound), alloc); + peers.PushBack(peer, alloc); + }); resp.AddMember("result", peers, alloc); return true; } @@ -259,11 +261,11 @@ namespace llarp struct ReqHandlerImpl : public ::abyss::httpd::BaseReqHandler { - ReqHandlerImpl(llarp::Router* r, llarp_time_t reqtimeout) + ReqHandlerImpl(Router* r, llarp_time_t reqtimeout) : ::abyss::httpd::BaseReqHandler(reqtimeout), router(r) { } - llarp::Router* router; + Router* router; ::abyss::httpd::IRPCHandler* CreateHandler(::abyss::httpd::ConnImpl* conn) { @@ -273,10 +275,10 @@ namespace llarp struct ServerImpl { - llarp::Router* router; + Router* router; ReqHandlerImpl _handler; - ServerImpl(llarp::Router* r) : router(r), _handler(r, 2000) + ServerImpl(Router* r) : router(r), _handler(r, 2000) { } @@ -295,11 +297,11 @@ namespace llarp { uint16_t port = 0; auto idx = addr.find_first_of(':'); - llarp::Addr netaddr; + Addr netaddr; if(idx != std::string::npos) { port = std::stoi(addr.substr(1 + idx)); - netaddr = llarp::Addr(addr.substr(0, idx)); + netaddr = Addr(addr.substr(0, idx)); } sockaddr_in saddr; saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); @@ -312,7 +314,7 @@ namespace llarp #else struct ServerImpl { - ServerImpl(__attribute__((unused)) llarp::Router* r){}; + ServerImpl(__attribute__((unused)) Router* r){}; bool Start(__attribute__((unused)) const std::string& addr) @@ -328,7 +330,7 @@ namespace llarp struct CallerImpl { - CallerImpl(__attribute__((unused)) llarp::Router* r) + CallerImpl(__attribute__((unused)) Router* r) { } @@ -356,7 +358,7 @@ namespace llarp #endif - Caller::Caller(llarp::Router* r) : m_Impl(new CallerImpl(r)) + Caller::Caller(Router* r) : m_Impl(new CallerImpl(r)) { } @@ -383,7 +385,7 @@ namespace llarp m_Impl->Tick(now); } - Server::Server(llarp::Router* r) : m_Impl(new ServerImpl(r)) + Server::Server(Router* r) : m_Impl(new ServerImpl(r)) { } diff --git a/llarp/rpc.hpp b/llarp/rpc/rpc.hpp similarity index 87% rename from llarp/rpc.hpp rename to llarp/rpc/rpc.hpp index dd024481e..15916d4a4 100644 --- a/llarp/rpc.hpp +++ b/llarp/rpc/rpc.hpp @@ -1,7 +1,6 @@ #ifndef LLARP_RPC_HPP #define LLARP_RPC_HPP -#include #include #include @@ -9,6 +8,7 @@ namespace llarp { + struct PubKey; struct Router; namespace rpc @@ -18,7 +18,7 @@ namespace llarp /// jsonrpc server struct Server { - Server(llarp::Router* r); + Server(Router* r); ~Server(); bool @@ -37,7 +37,7 @@ namespace llarp /// jsonrpc caller struct Caller { - Caller(llarp::Router* r); + Caller(Router* r); ~Caller(); /// start with jsonrpc endpoint address @@ -50,7 +50,7 @@ namespace llarp /// test if a router is valid bool - VerifyRouter(const llarp::PubKey& pk); + VerifyRouter(const PubKey& pk); /// do per second tick void diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index e3f4cbcee..7c58c9528 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -1,8 +1,9 @@ +#include + #include #include -#include +#include #include -#include namespace llarp { diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 7ba7704fa..2eb0dbdbd 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include diff --git a/test/exit/test_llarp_exit_context.cpp b/test/exit/test_llarp_exit_context.cpp index 4d73ea86a..7f3e9fd6f 100644 --- a/test/exit/test_llarp_exit_context.cpp +++ b/test/exit/test_llarp_exit_context.cpp @@ -1,7 +1,8 @@ -#include - #include -#include + +#include + +#include struct ExitTest : public ::testing::Test { diff --git a/test/test_llarp_encrypted_frame.cpp b/test/test_llarp_encrypted_frame.cpp index 450a117bf..478f7fd64 100644 --- a/test/test_llarp_encrypted_frame.cpp +++ b/test/test_llarp_encrypted_frame.cpp @@ -1,6 +1,6 @@ -#include +#include -#include +#include #include #include diff --git a/test/test_llarp_router.cpp b/test/test_llarp_router.cpp index 5ebdfad12..c327abc05 100644 --- a/test/test_llarp_router.cpp +++ b/test/test_llarp_router.cpp @@ -1,4 +1,5 @@ -#include +#include + #include #include