Merge pull request #5 from neuroscr/master

DHT changes, command line options
pull/6/head^2
Ryan Tharp 6 years ago committed by GitHub
commit b6e5f4bd99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -175,6 +175,7 @@ set(LIB_SRC
llarp/dht/context.cpp llarp/dht/context.cpp
llarp/dht/decode.cpp llarp/dht/decode.cpp
llarp/dht/dht_immediate.cpp llarp/dht/dht_immediate.cpp
llarp/dht/find_intro.cpp
llarp/dht/find_router.cpp llarp/dht/find_router.cpp
llarp/dht/got_intro.cpp llarp/dht/got_intro.cpp
llarp/dht/got_router.cpp llarp/dht/got_router.cpp

@ -1,5 +1,8 @@
#include <llarp.h> #include <llarp.h>
#include <signal.h> #include <signal.h>
#include <llarp/logger.h>
#include <getopt.h>
#include <sys/param.h> // for MIN
struct llarp_main *ctx = 0; struct llarp_main *ctx = 0;
@ -18,15 +21,57 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
const char *conffname = "daemon.ini"; const char *conffname = "daemon.ini";
if(argc > 1) int c;
conffname = argv[1]; while(1)
{
static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "c:o:", long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 0:
break;
case 'c':
conffname = optarg;
break;
case 'o':
if (strncmp(optarg, "debug", MIN(strlen(optarg), (unsigned long)5))==0)
{
cSetLogLevel(eLogDebug);
}
else
if (strncmp(optarg, "info", MIN(strlen(optarg), (unsigned long)4))==0)
{
cSetLogLevel(eLogInfo);
}
else
if (strncmp(optarg, "warn", MIN(strlen(optarg), (unsigned long)4))==0)
{
cSetLogLevel(eLogWarn);
}
else
if (strncmp(optarg, "error", MIN(strlen(optarg), (unsigned long)5))==0)
{
cSetLogLevel(eLogError);
}
break;
default:
abort();
}
}
ctx = llarp_main_init(conffname, !TESTNET); ctx = llarp_main_init(conffname, !TESTNET);
int code = 1; int code = 1;
if(ctx) if(ctx)
{ {
signal(SIGINT, handle_signal); signal(SIGINT, handle_signal);
code = llarp_main_run(ctx); code = llarp_main_run(ctx);
llarp_main_free(ctx); //llarp_main_free(ctx);
} }
return code; return code;
} }

@ -2,6 +2,16 @@
#include <signal.h> #include <signal.h>
#include "logger.hpp" #include "logger.hpp"
#include <getopt.h>
#include <llarp/router_contact.h>
#include <llarp/time.h>
#include <fstream>
#include "buffer.hpp"
#include "crypto.hpp"
#include "fs.hpp"
#include "net.hpp"
#include "router.hpp"
struct llarp_main *ctx = 0; struct llarp_main *ctx = 0;
llarp_main *sllarp = nullptr; llarp_main *sllarp = nullptr;
@ -17,16 +27,6 @@ handle_signal(int sig)
#define TESTNET 0 #define TESTNET 0
#endif #endif
#include <getopt.h>
#include <llarp/router_contact.h>
#include <llarp/time.h>
#include <fstream>
#include "buffer.hpp"
#include "crypto.hpp"
#include "fs.hpp"
#include "net.hpp"
#include "router.hpp"
bool bool
printNode(struct llarp_nodedb_iter *iter) printNode(struct llarp_nodedb_iter *iter)
{ {
@ -38,16 +38,6 @@ printNode(struct llarp_nodedb_iter *iter)
return false; return false;
} }
// fwd declr
struct check_online_request;
void
HandleDHTLocate(llarp_router_lookup_job *job)
{
llarp::LogInfo("DHT result: ", job->found ? "found" : "not found");
// save to nodedb?
}
bool bool
aiLister(struct llarp_ai_list_iter *request, struct llarp_ai *addr) aiLister(struct llarp_ai_list_iter *request, struct llarp_ai *addr)
{ {
@ -58,10 +48,49 @@ aiLister(struct llarp_ai_list_iter *request, struct llarp_ai *addr)
return true; return true;
} }
void displayRC(llarp_rc *rc)
{
char ftmp[68] = {0};
const char *hexPubSigKey =
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(rc->pubkey, ftmp);
printf("PubSigKey [%s]\n", hexPubSigKey);
struct llarp_ai_list_iter iter;
// iter.user
iter.visit = &aiLister;
llarp_ai_list_iterate(rc->addrs, &iter);
}
// fwd declr
struct check_online_request;
void
HandleDHTLocate(llarp_router_lookup_job *job)
{
llarp::LogInfo("DHT result: ", job->found ? "found" : "not found");
if (job->found)
{
// save to nodedb?
displayRC(&job->result);
}
// shutdown router
// well because we're in the gotroutermessage, we can't sigint because we'll deadlock because we're session locked
//llarp_main_signal(ctx, SIGINT);
// llarp_timer_run(logic->timer, logic->thread);
// we'll we don't want logic thread
// but we want to switch back to the main thread
//llarp_logic_stop();
// still need to exit this logic thread...
llarp_main_abort(ctx);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
// take -c to set location of daemon.ini // take -c to set location of daemon.ini
// take -o to set log level
// --generate-blank /path/to/file.signed // --generate-blank /path/to/file.signed
// --update-ifs /path/to/file.signed // --update-ifs /path/to/file.signed
// --key /path/to/long_term_identity.key // --key /path/to/long_term_identity.key
@ -75,12 +104,14 @@ main(int argc, char *argv[])
{ {
printf( printf(
"please specify: \n" "please specify: \n"
"--generate with a path to a router contact file\n" "--generate with a path to a router contact file\n"
"--update with a path to a router contact file\n" "--update with a path to a router contact file\n"
"--list \n" "--list \n"
"--import with a path to a router contact file\n" "--import with a path to a router contact file\n"
"--export a hex formatted public key\n" "--export a hex formatted public key\n"
"--locate a hex formatted public key" "--locate a hex formatted public key"
"--localInfo \n"
"--read with a path to a router contact file\n"
"\n"); "\n");
return 0; return 0;
} }
@ -91,6 +122,7 @@ main(int argc, char *argv[])
bool exportMode = false; bool exportMode = false;
bool locateMode = false; bool locateMode = false;
bool localMode = false; bool localMode = false;
bool readMode = false;
int c; int c;
char *conffname; char *conffname;
char defaultConfName[] = "daemon.ini"; char defaultConfName[] = "daemon.ini";
@ -103,6 +135,7 @@ main(int argc, char *argv[])
{ {
static struct option long_options[] = { static struct option long_options[] = {
{"config", required_argument, 0, 'c'}, {"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{"generate", required_argument, 0, 'g'}, {"generate", required_argument, 0, 'g'},
{"update", required_argument, 0, 'u'}, {"update", required_argument, 0, 'u'},
{"list", no_argument, 0, 'l'}, {"list", no_argument, 0, 'l'},
@ -110,9 +143,10 @@ main(int argc, char *argv[])
{"export", required_argument, 0, 'e'}, {"export", required_argument, 0, 'e'},
{"locate", required_argument, 0, 'q'}, {"locate", required_argument, 0, 'q'},
{"localInfo", no_argument, 0, 'n'}, {"localInfo", no_argument, 0, 'n'},
{"read", required_argument, 0, 'r'},
{0, 0, 0, 0}}; {0, 0, 0, 0}};
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, "cgluieqn", long_options, &option_index); c = getopt_long(argc, argv, "c:o:g:lu:i:e:q:nr:", long_options, &option_index);
if(c == -1) if(c == -1)
break; break;
switch(c) switch(c)
@ -122,6 +156,27 @@ main(int argc, char *argv[])
case 'c': case 'c':
conffname = optarg; conffname = optarg;
break; break;
case 'o':
if (strncmp(optarg, "debug", std::min(strlen(optarg), static_cast<unsigned long>(5)))==0)
{
llarp::SetLogLevel(llarp::eLogDebug);
}
else
if (strncmp(optarg, "info", std::min(strlen(optarg), static_cast<unsigned long>(4)))==0)
{
llarp::SetLogLevel(llarp::eLogInfo);
}
else
if (strncmp(optarg, "warn", std::min(strlen(optarg), static_cast<unsigned long>(4)))==0)
{
llarp::SetLogLevel(llarp::eLogWarn);
}
else
if (strncmp(optarg, "error", std::min(strlen(optarg), static_cast<unsigned long>(5)))==0)
{
llarp::SetLogLevel(llarp::eLogError);
}
break;
case 'l': case 'l':
haveRequiredOptions = true; haveRequiredOptions = true;
listMode = true; listMode = true;
@ -160,6 +215,11 @@ main(int argc, char *argv[])
haveRequiredOptions = true; haveRequiredOptions = true;
localMode = true; localMode = true;
break; break;
case 'r':
rcfname = optarg;
haveRequiredOptions = true;
readMode = true;
break;
default: default:
abort(); abort();
} }
@ -171,7 +231,7 @@ main(int argc, char *argv[])
} }
printf("parsed options\n"); printf("parsed options\n");
if(!genMode && !updMode && !listMode && !importMode && !exportMode if(!genMode && !updMode && !listMode && !importMode && !exportMode
&& !locateMode && !localMode) && !locateMode && !localMode && !readMode)
{ {
llarp::LogError( llarp::LogError(
"I don't know what to do, no generate or update parameter\n"); "I don't know what to do, no generate or update parameter\n");
@ -295,13 +355,14 @@ main(int argc, char *argv[])
llarp::PubKey binaryPK; llarp::PubKey binaryPK;
llarp::HexDecode(rcfname, binaryPK.data()); llarp::HexDecode(rcfname, binaryPK.data());
// llarp::SetLogLevel(llarp::eLogDebug);
llarp::LogInfo("Queueing job"); llarp::LogInfo("Queueing job");
llarp_router_lookup_job *job = new llarp_router_lookup_job; llarp_router_lookup_job *job = new llarp_router_lookup_job;
job->iterative = true;
job->found = false; job->found = false;
job->hook = &HandleDHTLocate; job->hook = &HandleDHTLocate;
llarp_rc_new(&job->result);
memcpy(job->target, binaryPK, PUBKEYSIZE); // set job's target memcpy(job->target, binaryPK, PUBKEYSIZE); // set job's target
// create query DHT request // create query DHT request
check_online_request *request = new check_online_request; check_online_request *request = new check_online_request;
request->ptr = ctx; request->ptr = ctx;
@ -317,20 +378,15 @@ main(int argc, char *argv[])
} }
if(localMode) if(localMode)
{ {
// llarp::LogInfo("find our local rc file");
// llarp_rc *rc = llarp_rc_read("router.signed");
llarp_rc *rc = llarp_main_getLocalRC(ctx); llarp_rc *rc = llarp_main_getLocalRC(ctx);
char ftmp[68] = {0}; displayRC(rc);
const char *hexPubSigKey = }
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(rc->pubkey, ftmp); if(readMode)
printf("PubSigKey [%s]\n", hexPubSigKey); {
llarp_rc *rc = llarp_rc_read(rcfname);
struct llarp_ai_list_iter iter; displayRC(rc);
// iter.user
iter.visit = &aiLister;
llarp_ai_list_iterate(rc->addrs, &iter);
} }
llarp_main_free(ctx); // it's a unique_ptr, should clean up itself
//llarp_main_free(ctx);
return 1; // success return 1; // success
} }

@ -11,6 +11,7 @@ recursively find an IS by SA
{ {
A: "F", A: "F",
I: 0 or 1 if iterative request,
R: r_counter, R: r_counter,
S: "<32 bytes SA>", S: "<32 bytes SA>",
T: transaction_id_uint64, T: transaction_id_uint64,
@ -123,33 +124,13 @@ find a router by long term RC.k public key
{ {
A: "R", A: "R",
I: 0 or 1 if iterative lookup
K: "<32 byte public key of router>", K: "<32 byte public key of router>",
T: transaction_id_uint64, T: transaction_id_uint64,
V: 0 V: 0
} }
find RC who's RC.k is closest to K: TODO: document me
if A.k is equal to K:
* reply with a GRCM with an R value of just A
if A.k is not equal to K and we are closesr to A.k than anyone we know:
* reply with a GRCM with an empty R value
find a pending transaction id for K, P
if P exists:
* link transaction T to P
if P does not exist:
* generate a new transaction id, U
* start transaction U for A.k
* link transaction U to transaction T
* send FRCM to A.k requesting K
got router contact message (GRCM) got router contact message (GRCM)

@ -516,20 +516,43 @@ B is set to a backoff value.
R contains additional metadata text describing why the exit was rejected. R contains additional metadata text describing why the exit was rejected.
hidden service frame (HSF)
hidden service data message (HSDM) TODO: document this better
signed data sent anonymously over the network to a recipiant from a sender. intro message (variant 1)
sent inside a TDFM encrypted to the hidden service's public encryption key.
{ {
A: "H", A: "H",
H: "<payload bytes>", D: "<N bytes encrypted HSD>",
I: Introducer for reply, H: "<32 bytes ephemeral public encryption key>",
R: SA of recipiant, N: "<32 bytes nonce for key exchange>",
S: SI of sender, S: 0,
V: 0,
Z: "<64 bytes signature of entire message using sender's signing key>"
}
ordered data message (variant 2)
{
A: "H",
D: "<N bytes encrypted HSD>",
N: "<32 bytes nonce for symettric cipher>",
S: sequence_number_uint64,
V: 0, V: 0,
Z: "<64 bytes signature from sender of the entire message>" Z: "<64 bytes signature using sender's signing key>"
}
hidden service data (HSD)
data sent anonymously over the network to a recipiant from a sender.
sent inside a HSFM encrypted with a shared secret.
{
D: "<N bytes payload>",
I: Introduction for reply,
S: SI of sender,
V: 0
} }
transfer data fragment message (TDFM) transfer data fragment message (TDFM)
@ -539,12 +562,11 @@ transfer data between paths.
{ {
A: "T", A: "T",
P: "<16 bytes path id>", P: "<16 bytes path id>",
T: "<N bytes data>", T: message_transfered_between_paths,
V: 0, V: 0
Y: "<32 bytes nounce>"
} }
transfer data to another path with id P on the local router place Y and T values transfer data to another path with id P on the local router place a random 32 byte and T values
into y and z values into a LRDM message (respectively) and send it in the into y and z values into a LRDM message (respectively) and send it in the
downstream direction. downstream direction.

@ -23,10 +23,6 @@ llarp_main_init(const char *fname, bool multiProcess);
void void
llarp_main_signal(struct llarp_main *ptr, int sig); llarp_main_signal(struct llarp_main *ptr, int sig);
/// set custom dht message handler function
void
llarp_main_set_dht_handler(struct llarp_main *ptr, llarp_dht_msg_handler h);
/// setup main context /// setup main context
int int
llarp_main_setup(struct llarp_main *ptr); llarp_main_setup(struct llarp_main *ptr);
@ -35,6 +31,9 @@ llarp_main_setup(struct llarp_main *ptr);
int int
llarp_main_run(struct llarp_main *ptr); llarp_main_run(struct llarp_main *ptr);
void
llarp_main_abort(struct llarp_main *ptr);
/// load nodeDB into memory /// load nodeDB into memory
int int
llarp_main_loadDatabase(struct llarp_main *ptr); llarp_main_loadDatabase(struct llarp_main *ptr);

@ -18,15 +18,14 @@ namespace llarp
bool singleThreaded = false; bool singleThreaded = false;
std::vector< std::thread > netio_threads; std::vector< std::thread > netio_threads;
llarp_crypto crypto; llarp_crypto crypto;
llarp_router *router = nullptr; llarp_router *router = nullptr;
llarp_threadpool *worker = nullptr; llarp_threadpool *worker = nullptr;
llarp_logic *logic = nullptr; llarp_logic *logic = nullptr;
llarp_config *config = nullptr; llarp_config *config = nullptr;
llarp_nodedb *nodedb = nullptr; llarp_nodedb *nodedb = nullptr;
llarp_ev_loop *mainloop = nullptr; llarp_ev_loop *mainloop = nullptr;
llarp_dht_msg_handler custom_dht_func = nullptr; char nodedb_dir[256] = {0};
char nodedb_dir[256] = {0}; char conatctFile[256] = "router.signed";
char conatctFile[256] = "router.signed";
bool bool
LoadConfig(const std::string &fname); LoadConfig(const std::string &fname);

@ -20,23 +20,10 @@ llarp_dht_context_new(struct llarp_router* parent);
void void
llarp_dht_context_free(struct llarp_dht_context* dht); llarp_dht_context_free(struct llarp_dht_context* dht);
struct llarp_dht_msg;
/// handler function
/// f(outmsg, inmsg)
/// returns true if outmsg has been filled otherwise returns false
typedef bool (*llarp_dht_msg_handler)(struct llarp_dht_msg*,
struct llarp_dht_msg*);
/// start dht context with our location in keyspace /// start dht context with our location in keyspace
void void
llarp_dht_context_start(struct llarp_dht_context* ctx, const byte_t* key); llarp_dht_context_start(struct llarp_dht_context* ctx, const byte_t* key);
// override dht message handler with custom handler
void
llarp_dht_set_msg_handler(struct llarp_dht_context* ctx,
llarp_dht_msg_handler func);
struct llarp_router_lookup_job; struct llarp_router_lookup_job;
typedef void (*llarp_router_lookup_handler)(struct llarp_router_lookup_job*); typedef void (*llarp_router_lookup_handler)(struct llarp_router_lookup_job*);
@ -49,7 +36,9 @@ struct llarp_router_lookup_job
struct llarp_dht_context* dht; struct llarp_dht_context* dht;
byte_t target[PUBKEYSIZE]; byte_t target[PUBKEYSIZE];
bool found; bool found;
// make sure you initialize addr and exits
struct llarp_rc result; struct llarp_rc result;
bool iterative;
}; };
/// start allowing dht participation on a context /// start allowing dht participation on a context

@ -20,14 +20,19 @@ namespace llarp
Context(); Context();
~Context(); ~Context();
llarp_dht_msg_handler custom_handler = nullptr;
SearchJob* SearchJob*
FindPendingTX(const Key_t& owner, uint64_t txid); FindPendingTX(const Key_t& owner, uint64_t txid);
void void
RemovePendingLookup(const Key_t& owner, uint64_t txid); RemovePendingLookup(const Key_t& owner, uint64_t txid);
void
LookupServiceDirect(const Key_t& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer,
SearchJob::IntroSetHookFunc handler,
bool iterateive = false,
std::set< Key_t > excludes = {});
void void
LookupRouter(const Key_t& target, const Key_t& whoasked, LookupRouter(const Key_t& target, const Key_t& whoasked,
uint64_t whoaskedTX, const Key_t& askpeer, uint64_t whoaskedTX, const Key_t& askpeer,

@ -9,7 +9,8 @@ namespace llarp
{ {
struct FindIntroMessage : public IMessage struct FindIntroMessage : public IMessage
{ {
uint64_t R = 0; uint64_t R = 0;
bool iterative = false;
llarp::service::Address S; llarp::service::Address S;
uint64_t T = 0; uint64_t T = 0;
@ -25,6 +26,6 @@ namespace llarp
HandleMessage(llarp_dht_context* ctx, HandleMessage(llarp_dht_context* ctx,
std::vector< IMessage* >& replies) const; std::vector< IMessage* >& replies) const;
}; };
} } // namespace dht
} } // namespace llarp
#endif #endif

@ -35,6 +35,9 @@ llarp_logic_cancel_call(struct llarp_logic* logic, uint32_t id);
void void
llarp_logic_remove_call(struct llarp_logic* logic, uint32_t id); llarp_logic_remove_call(struct llarp_logic* logic, uint32_t id);
void
llarp_logic_stop_timer(struct llarp_logic* logic);
void void
llarp_logic_stop(struct llarp_logic* logic); llarp_logic_stop(struct llarp_logic* logic);

@ -0,0 +1,16 @@
#ifndef LLARP_MESSAGES_HIDDEN_SERIVCE_HPP
#define LLARP_MESSAGES_HIDDEN_SERIVCE_HPP
#include <llarp/routing/message.hpp>
namespace llarp
{
namespace routing
{
struct HiddenServiceFrame : public IMessage
{
};
} // namespace routing
} // namespace llarp
#endif

@ -165,9 +165,6 @@ namespace llarp
bool bool
HandleDHTMessage(const llarp::dht::IMessage* msg, llarp_router* r); HandleDHTMessage(const llarp::dht::IMessage* msg, llarp_router* r);
bool
HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r);
// handle data in upstream direction // handle data in upstream direction
bool bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r); HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r);
@ -246,9 +243,6 @@ namespace llarp
bool bool
HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r); HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r);
bool
HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r);
// handle data in upstream direction // handle data in upstream direction
bool bool
HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r); HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r);

@ -20,7 +20,7 @@ struct llarp_pathbuilder_context;
/// alloc /// alloc
struct llarp_pathbuilder_context* struct llarp_pathbuilder_context*
llarp_pathbuilder_context_new(struct llarp_router* router, llarp_pathbuilder_context_new(struct llarp_router* router,
struct llarp_dht_context* dht); struct llarp_dht_context* dht, size_t numpaths);
/// dealloc /// dealloc
void void
llarp_pathbuilder_context_free(struct llarp_pathbuilder_context* ctx); llarp_pathbuilder_context_free(struct llarp_pathbuilder_context* ctx);

@ -10,7 +10,7 @@ struct llarp_pathbuilder_context : public llarp::path::PathSet
struct llarp_dht_context* dht; struct llarp_dht_context* dht;
/// construct /// construct
llarp_pathbuilder_context(llarp_router* p_router, llarp_pathbuilder_context(llarp_router* p_router,
struct llarp_dht_context* p_dht); struct llarp_dht_context* p_dht, size_t numPaths);
virtual ~llarp_pathbuilder_context(){}; virtual ~llarp_pathbuilder_context(){};

@ -1,6 +1,7 @@
#ifndef LLARP_PATHSET_HPP #ifndef LLARP_PATHSET_HPP
#define LLARP_PATHSET_HPP #define LLARP_PATHSET_HPP
#include <llarp/time.h> #include <llarp/time.h>
#include <functional>
#include <list> #include <list>
#include <llarp/path_types.hpp> #include <llarp/path_types.hpp>
#include <llarp/router_id.hpp> #include <llarp/router_id.hpp>
@ -81,6 +82,18 @@ namespace llarp
PublishIntroSet(const llarp::service::IntroSet& introset, PublishIntroSet(const llarp::service::IntroSet& introset,
llarp_router* r); llarp_router* r);
typedef std::function< void(const llarp::service::IntroSet*) >
ServiceLookupHandler;
/// return false if we are already pending a lookup for this address
bool
LookupService(const llarp::service::Address& addr,
ServiceLookupHandler handler);
protected:
void
IssueServiceLookup(const llarp::service::Address& addr);
private: private:
typedef std::pair< RouterID, PathID_t > PathInfo_t; typedef std::pair< RouterID, PathID_t > PathInfo_t;
typedef std::map< PathInfo_t, Path* > PathMap_t; typedef std::map< PathInfo_t, Path* > PathMap_t;
@ -88,6 +101,9 @@ namespace llarp
size_t m_NumPaths; size_t m_NumPaths;
PathMap_t m_Paths; PathMap_t m_Paths;
uint64_t m_CurrentPublishTX = 0; uint64_t m_CurrentPublishTX = 0;
std::unordered_map< llarp::service::Address, ServiceLookupHandler,
llarp::service::Address::Hash >
m_ServiceLookups;
}; };
} // namespace path } // namespace path

@ -42,6 +42,10 @@ struct llarp_rc
void void
llarp_rc_free(struct llarp_rc *rc); llarp_rc_free(struct llarp_rc *rc);
bool
llarp_rc_new(struct llarp_rc *rc);
bool bool
llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc); llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc);

@ -4,6 +4,7 @@
#include <llarp/buffer.h> #include <llarp/buffer.h>
#include <llarp/router.h> #include <llarp/router.h>
#include <llarp/dht.hpp> #include <llarp/dht.hpp>
#include <llarp/messages/hidden_service.hpp>
#include <llarp/messages/path_confirm.hpp> #include <llarp/messages/path_confirm.hpp>
#include <llarp/messages/path_latency.hpp> #include <llarp/messages/path_latency.hpp>
#include <llarp/messages/path_transfer.hpp> #include <llarp/messages/path_transfer.hpp>
@ -20,7 +21,10 @@ namespace llarp
llarp_router *r) = 0; llarp_router *r) = 0;
virtual bool virtual bool
HandleHiddenServiceData(llarp_buffer_t buf, llarp_router *r) = 0; HandleHiddenServiceFrame(const HiddenServiceFrame *msg)
{
return false;
}
virtual bool virtual bool
HandlePathConfirmMessage(const PathConfirmMessage *msg, HandlePathConfirmMessage(const PathConfirmMessage *msg,

@ -20,6 +20,16 @@ namespace llarp
Address(const byte_t* data) : llarp::AlignedBuffer< 32 >(data) Address(const byte_t* data) : llarp::AlignedBuffer< 32 >(data)
{ {
} }
struct Hash
{
size_t
operator()(const Address& addr) const
{
size_t idx = 0;
memcpy(&idx, addr, sizeof(idx));
return idx;
}
};
}; };
} // namespace service } // namespace service

@ -1,5 +1,6 @@
#ifndef LLARP_SERVICE_ENDPOINT_HPP #ifndef LLARP_SERVICE_ENDPOINT_HPP
#define LLARP_SERVICE_ENDPOINT_HPP #define LLARP_SERVICE_ENDPOINT_HPP
#include <llarp/messages/hidden_service.hpp>
#include <llarp/pathbuilder.hpp> #include <llarp/pathbuilder.hpp>
#include <llarp/service/Identity.hpp> #include <llarp/service/Identity.hpp>
@ -24,11 +25,70 @@ namespace llarp
bool bool
HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg); HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg);
bool
HandleHiddenServiceFrame(const llarp::routing::HiddenServiceFrame* msg);
/// return true if we have an established path to a hidden service
bool
HasPathToService(const Address& remote) const;
/// return false if we don't have a path to the service
/// return true if we did and we removed it
bool
ForgetPathToService(const Address& remote);
/// context needed to initiate an outbound hidden service session
struct OutboundContext : public llarp_pathbuilder_context
{
OutboundContext(Endpoint* parent);
~OutboundContext();
/// the remote hidden service's curren intro set
IntroSet currentIntroSet;
uint64_t sequenceNo = 0;
/// encrypt asynchronously and send to remote endpoint from us
/// returns false if we cannot send yet otherwise returns true
bool
AsyncEncryptAndSendTo(llarp_buffer_t D);
/// issues a lookup to find the current intro set of the remote service
void
UpdateIntroSet();
bool
HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg);
private:
llarp::SharedSecret sharedKey;
Endpoint* m_Parent;
};
// passed a sendto context when we have a path established otherwise
// nullptr if the path was not made before the timeout
typedef std::function< void(OutboundContext*) > PathEnsureHook;
/// return false if we have already called this function before for this
/// address
bool
EnsurePathToService(const Address& remote, PathEnsureHook h,
uint64_t timeoutMS);
virtual bool
HandleAuthenticatedDataFrom(const Address& remote, llarp_buffer_t data)
{
/// TODO: imlement me
return true;
}
private: private:
llarp_router* m_Router; llarp_router* m_Router;
std::string m_Keyfile; std::string m_Keyfile;
std::string m_Name; std::string m_Name;
Identity m_Identity; Identity m_Identity;
std::unordered_map< Address, OutboundContext*, Address::Hash >
m_RemoteSessions;
}; };
} // namespace service } // namespace service
} // namespace llarp } // namespace llarp

@ -161,11 +161,6 @@ namespace llarp
llarp::LogError("Failed to configure router"); llarp::LogError("Failed to configure router");
return 1; return 1;
} }
if(custom_dht_func)
{
llarp::LogInfo("using custom dht function");
llarp_dht_set_msg_handler(router->dht, custom_dht_func);
}
// set nodedb, load our RC, establish DHT // set nodedb, load our RC, establish DHT
llarp_run_router(router, nodedb); llarp_run_router(router, nodedb);
@ -325,12 +320,6 @@ llarp_main_init(const char *fname, bool multiProcess)
return m; return m;
} }
void
llarp_main_set_dht_handler(struct llarp_main *ptr, llarp_dht_msg_handler func)
{
ptr->ctx->custom_dht_func = func;
}
void void
llarp_main_signal(struct llarp_main *ptr, int sig) llarp_main_signal(struct llarp_main *ptr, int sig)
{ {
@ -349,6 +338,12 @@ llarp_main_run(struct llarp_main *ptr)
return ptr->ctx->Run(); return ptr->ctx->Run();
} }
void
llarp_main_abort(struct llarp_main *ptr)
{
llarp_logic_stop_timer(ptr->ctx->router->logic);
}
int int
llarp_main_loadDatabase(struct llarp_main *ptr) llarp_main_loadDatabase(struct llarp_main *ptr)
{ {

@ -24,6 +24,7 @@ llarp_dht_put_peer(struct llarp_dht_context *ctx, struct llarp_rc *rc)
{ {
llarp::dht::RCNode n(rc); llarp::dht::RCNode n(rc);
llarp::LogDebug("Adding ", n.ID, " to DHT");
ctx->impl.nodes->PutNode(n); ctx->impl.nodes->PutNode(n);
} }
@ -31,16 +32,10 @@ void
llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id) llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id)
{ {
llarp::dht::Key_t k = id; llarp::dht::Key_t k = id;
llarp::LogDebug("Removing ", k, " to DHT");
ctx->impl.nodes->DelNode(k); ctx->impl.nodes->DelNode(k);
} }
void
llarp_dht_set_msg_handler(struct llarp_dht_context *ctx,
llarp_dht_msg_handler handler)
{
ctx->impl.custom_handler = handler;
}
void void
llarp_dht_allow_transit(llarp_dht_context *ctx) llarp_dht_allow_transit(llarp_dht_context *ctx)
{ {

@ -66,6 +66,9 @@ namespace llarp
else else
{ {
// yeah, ask neighboor recursively // yeah, ask neighboor recursively
// FIXME: we may need to pass a job here...
//auto sj = FindPendingTX(requester, txid);
//LookupRouter(target, requester, txid, next, sj->job);
LookupRouter(target, requester, txid, next); LookupRouter(target, requester, txid, next);
} }
} }
@ -189,8 +192,18 @@ namespace llarp
Context::LookupRouterViaJob(llarp_router_lookup_job *job) Context::LookupRouterViaJob(llarp_router_lookup_job *job)
{ {
Key_t peer; Key_t peer;
/*
llarp::LogInfo("LookupRouterViaJob dumping nodes");
for(const auto &item : nodes->nodes)
{
llarp::LogInfo("LookupRouterViaJob dumping node: ", item.first);
}
*/
llarp::LogInfo("LookupRouterViaJob node count: ", nodes->nodes.size());
llarp::LogInfo("LookupRouterViaJob recursive: ", job->iterative?"yes":"no");
if(nodes->FindClosest(job->target, peer)) if(nodes->FindClosest(job->target, peer))
LookupRouter(job->target, ourKey, 0, peer, job); LookupRouter(job->target, ourKey, 0, peer, job, job->iterative);
else if(job->hook) else if(job->hook)
{ {
job->found = false; job->found = false;
@ -207,4 +220,4 @@ namespace llarp
} }
} // namespace dht } // namespace dht
} // namespace llarp } // namespace llarp

@ -0,0 +1,11 @@
#include <llarp/dht/messages/findintro.hpp>
namespace llarp
{
namespace dht
{
FindIntroMessage::~FindIntroMessage()
{
}
} // namespace dht
} // namespace llarp

@ -61,8 +61,8 @@ namespace llarp
GotRouterMessage::HandleMessage(llarp_dht_context *ctx, GotRouterMessage::HandleMessage(llarp_dht_context *ctx,
std::vector< IMessage * > &replies) const std::vector< IMessage * > &replies) const
{ {
auto &dht = ctx->impl; auto &dht = ctx->impl;
auto pending = dht.FindPendingTX(From, txid); SearchJob *pending = dht.FindPendingTX(From, txid);
if(pending) if(pending)
{ {
if(R.size()) if(R.size())
@ -87,9 +87,10 @@ namespace llarp
" iterating to next peer ", nextPeer, " iterating to next peer ", nextPeer,
" already asked ", pending->exclude.size(), " already asked ", pending->exclude.size(),
" other peers"); " other peers");
// REVIEW: is this ok to relay the pending->job as the current job (seems to make things work)
dht.LookupRouter(pending->target, pending->requester, dht.LookupRouter(pending->target, pending->requester,
pending->requesterTX, nextPeer, nullptr, true, pending->requesterTX, nextPeer, pending->job,
pending->exclude); true, pending->exclude);
} }
else else
{ {
@ -111,4 +112,4 @@ namespace llarp
return false; return false;
} }
} // namespace dht } // namespace dht
} // namespace llarp } // namespace llarp

@ -43,6 +43,13 @@ llarp_free_logic(struct llarp_logic** logic)
*logic = nullptr; *logic = nullptr;
} }
void
llarp_logic_stop_timer(struct llarp_logic* logic)
{
if(logic->timer)
llarp_timer_stop(logic->timer);
}
void void
llarp_logic_stop(struct llarp_logic* logic) llarp_logic_stop(struct llarp_logic* logic)
{ {

@ -381,13 +381,6 @@ namespace llarp
return HandleRoutingMessage(buf, r); return HandleRoutingMessage(buf, r);
} }
bool
Path::HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r)
{
// TODO: implement me
return false;
}
bool bool
Path::HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r) Path::HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r)
{ {

@ -175,9 +175,8 @@ namespace llarp
} // namespace llarp } // namespace llarp
llarp_pathbuilder_context::llarp_pathbuilder_context( llarp_pathbuilder_context::llarp_pathbuilder_context(
llarp_router* p_router, struct llarp_dht_context* p_dht) llarp_router* p_router, struct llarp_dht_context* p_dht, size_t pathNum)
// TODO: hardcoded value : llarp::path::PathSet(pathNum), router(p_router), dht(p_dht)
: llarp::path::PathSet(4), router(p_router), dht(p_dht)
{ {
p_router->paths.AddPathBuilder(this); p_router->paths.AddPathBuilder(this);
} }
@ -196,9 +195,9 @@ llarp_pathbuilder_context::BuildOne()
struct llarp_pathbuilder_context* struct llarp_pathbuilder_context*
llarp_pathbuilder_context_new(struct llarp_router* router, llarp_pathbuilder_context_new(struct llarp_router* router,
struct llarp_dht_context* dht) struct llarp_dht_context* dht, size_t sz)
{ {
return new llarp_pathbuilder_context(router, dht); return new llarp_pathbuilder_context(router, dht, sz);
} }
void void

@ -34,7 +34,6 @@ llarp_router::llarp_router()
, paths(this) , paths(this)
, dht(llarp_dht_context_new(this)) , dht(llarp_dht_context_new(this))
, inbound_link_msg_parser(this) , inbound_link_msg_parser(this)
, explorePool(llarp_pathbuilder_context_new(this, dht))
, hiddenServiceContext(this) , hiddenServiceContext(this)
{ {
@ -165,49 +164,26 @@ llarp_router::HandleDHTLookupForSendTo(llarp_router_lookup_job *job)
void void
llarp_router::try_connect(fs::path rcfile) llarp_router::try_connect(fs::path rcfile)
{ {
// FIXME: update API llarp_rc *remote = new llarp_rc;
byte_t tmp[MAX_RC_SIZE]; llarp_rc_new(remote);
llarp_rc remote = {0}; remote = llarp_rc_read(rcfile.c_str());
llarp_buffer_t buf; if (!remote)
llarp::StackBuffer< decltype(tmp) >(buf, tmp);
// open file
{ {
std::ifstream f(rcfile, std::ios::binary); llarp::LogError("failure to decode or verify of remote RC");
if(f.is_open()) return;
{
f.seekg(0, std::ios::end);
size_t sz = f.tellg();
f.seekg(0, std::ios::beg);
if(sz <= buf.sz)
{
f.read((char *)buf.base, sz);
}
else
llarp::LogError(rcfile, " too large");
}
else
{
llarp::LogError("failed to open ", rcfile);
return;
}
} }
if(llarp_rc_bdecode(&remote, &buf)) if(llarp_rc_verify_sig(&crypto, remote))
{ {
if(llarp_rc_verify_sig(&crypto, &remote)) llarp::LogDebug("verified signature");
if(!llarp_router_try_connect(this, remote, 10))
{ {
llarp::LogDebug("verified signature"); // or error?
if(!llarp_router_try_connect(this, &remote, 10)) llarp::LogWarn("session already made");
{
llarp::LogWarn("session already made");
}
} }
else
llarp::LogError("failed to verify signature of RC", rcfile);
} }
else else
llarp::LogError("failed to decode RC"); llarp::LogError("failed to verify signature of RC", rcfile);
llarp_rc_free(remote);
llarp_rc_free(&remote);
} }
bool bool
@ -267,6 +243,7 @@ llarp_router::SaveRC()
void void
llarp_router::Close() llarp_router::Close()
{ {
llarp::LogInfo("Closing ", inboundLinks.size(), " server bindings");
for(auto link : inboundLinks) for(auto link : inboundLinks)
{ {
link->stop_link(); link->stop_link();
@ -274,6 +251,7 @@ llarp_router::Close()
} }
inboundLinks.clear(); inboundLinks.clear();
llarp::LogInfo("Closing LokiNetwork client");
outboundLink->stop_link(); outboundLink->stop_link();
delete outboundLink; delete outboundLink;
outboundLink = nullptr; outboundLink = nullptr;
@ -638,8 +616,8 @@ llarp_router::Run()
} }
if(a.isPrivate()) if(a.isPrivate())
{ {
llarp::LogWarn("Skipping private network link: ", a); //llarp::LogWarn("Skipping private network link: ", a);
continue; //continue;
} }
llarp::LogInfo("Loading Addr: ", a, " into our RC"); llarp::LogInfo("Loading Addr: ", a, " into our RC");

@ -5,6 +5,15 @@
#include "buffer.hpp" #include "buffer.hpp"
#include "logger.hpp" #include "logger.hpp"
bool
llarp_rc_new(struct llarp_rc *rc)
{
rc->addrs = llarp_ai_list_new();
rc->exits = llarp_xi_list_new();
rc->last_updated = 0;
return true;
}
void void
llarp_rc_free(struct llarp_rc *rc) llarp_rc_free(struct llarp_rc *rc)
{ {

@ -6,7 +6,7 @@ namespace llarp
namespace service namespace service
{ {
Endpoint::Endpoint(const std::string& name, llarp_router* r) Endpoint::Endpoint(const std::string& name, llarp_router* r)
: llarp_pathbuilder_context(r, r->dht), m_Router(r), m_Name(name) : llarp_pathbuilder_context(r, r->dht, 2), m_Router(r), m_Name(name)
{ {
} }
@ -96,5 +96,23 @@ namespace llarp
Endpoint::~Endpoint() Endpoint::~Endpoint()
{ {
} }
}
} Endpoint::OutboundContext::OutboundContext(Endpoint* parent)
: llarp_pathbuilder_context(parent->m_Router, parent->m_Router->dht, 2)
, m_Parent(parent)
{
}
Endpoint::OutboundContext::~OutboundContext()
{
}
bool
Endpoint::OutboundContext::HandleGotIntroMessage(
const llarp::dht::GotIntroMessage* msg)
{
// TODO: implement me
return false;
}
} // namespace service
} // namespace llarp

@ -136,12 +136,5 @@ namespace llarp
return false; return false;
} }
bool
TransitHop::HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r)
{
llarp::LogWarn("unwarrented hidden service data on ", info);
return false;
}
} // namespace path } // namespace path
} // namespace llarp } // namespace llarp

Loading…
Cancel
Save