From 0c8f9edfb62b680e0c9332fccf6331d6a278ff79 Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 7 Dec 2018 18:36:53 -0500 Subject: [PATCH 1/3] always hook mx --- llarp/handlers/tun.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 6118ce347..b1beadd5a 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -269,8 +269,13 @@ namespace llarp llarp::service::Address addr; if(msg.questions.size() == 1) { + // always hook mx records + if(msg.questions[0].qtype == 15) + return true; + // always hook .loki if(addr.FromString(msg.questions[0].qname, ".loki")) return true; + // always hook .snode if(addr.FromString(msg.questions[0].qname, ".snode")) return true; if(msg.questions[0].qtype == 12) From b44e32567ec7eea0bd863a9c2d5e2c086b5f30c6 Mon Sep 17 00:00:00 2001 From: Lilac Date: Sat, 8 Dec 2018 14:37:48 +1000 Subject: [PATCH 2/3] fix lokinet bootstrap script --- lokinet-bootstrap | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lokinet-bootstrap b/lokinet-bootstrap index b92856a25..3d0972190 100755 --- a/lokinet-bootstrap +++ b/lokinet-bootstrap @@ -2,9 +2,18 @@ # this shell script will be replaced by a proper program in the future (probably) # -if [ "X$1" = "X" ] ; then url="https://i2p.rocks/i2procks.signed" ; else url="$1" ; fi +if [ -z "$1" ] +then + url="https://i2p.rocks/i2procks.signed" +else + url="$1" +fi + echo "downloading $url" -if [ ! -d $HOME/.lokinet/]; then + +if [ ! -d "$HOME/.lokinet" ] +then mkdir $HOME/.lokinet fi + wget -O $HOME/.lokinet/bootstrap.signed "$url" || echo "failed to download bootstrap from $url" From 30e9dca2e5626784b5d20079f717cc3d0fb6605f Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 10 Dec 2018 14:14:55 +0000 Subject: [PATCH 3/3] Convert llarp_logic to be a C++ class --- daemon/dns.cpp | 10 +- include/llarp.hpp | 2 +- include/llarp/dns_dotlokilookup.hpp | 2 +- include/llarp/dnsc.hpp | 5 +- include/llarp/dnsd.hpp | 3 +- include/llarp/ev.h | 10 +- include/llarp/iwp.hpp | 2 +- include/llarp/link/server.hpp | 4 +- include/llarp/logic.hpp | 69 +++++++------ include/llarp/nodedb.hpp | 13 ++- include/llarp/path.hpp | 2 +- include/llarp/router.h | 2 +- include/llarp/service/endpoint.hpp | 6 +- include/llarp/service/protocol.hpp | 2 +- libabyss/include/abyss/server.hpp | 4 +- libabyss/main.cpp | 2 +- libabyss/src/server.cpp | 2 +- llarp/context.cpp | 14 +-- llarp/dht.cpp | 2 +- llarp/dht/context.cpp | 10 +- llarp/dns_dotlokilookup.cpp | 4 +- llarp/dnsc.cpp | 3 +- llarp/dnsd.cpp | 3 +- llarp/ev.cpp | 8 +- llarp/handlers/tun.cpp | 4 +- llarp/link/server.cpp | 7 +- llarp/logic.cpp | 146 +++++++++++----------------- llarp/nodedb.cpp | 10 +- llarp/path.cpp | 2 +- llarp/pathbuilder.cpp | 6 +- llarp/relay_commit.cpp | 4 +- llarp/router.cpp | 7 +- llarp/router.hpp | 2 +- llarp/service/endpoint.cpp | 10 +- llarp/service/protocol.cpp | 11 +-- test/jsonrpc_unittest.cpp | 8 +- test/test_dns_unit.cpp | 10 +- test/test_dnsc_unit.cpp | 6 +- test/test_dnsd_unit.cpp | 2 +- 39 files changed, 195 insertions(+), 224 deletions(-) diff --git a/daemon/dns.cpp b/daemon/dns.cpp index 4611ddd5b..0583182ce 100644 --- a/daemon/dns.cpp +++ b/daemon/dns.cpp @@ -171,11 +171,11 @@ main(int argc, char *argv[]) // libev version llarp_ev_loop *netloop = nullptr; llarp_threadpool *worker = nullptr; - llarp_logic *logic = nullptr; + llarp::Logic *logic = nullptr; llarp_ev_loop_alloc(&netloop); // set up netio worker worker = llarp_init_same_process_threadpool(); - logic = llarp_init_single_process_logic(worker); // set up logic worker + logic = new llarp::Logic(worker); // set up logic worker // configure main netloop struct dnsd_context dnsd; @@ -202,10 +202,8 @@ main(int argc, char *argv[]) else { // need this for timer stuff - llarp_threadpool *worker = nullptr; - llarp_logic *logic = nullptr; - worker = llarp_init_same_process_threadpool(); - logic = llarp_init_single_process_logic(worker); // set up logic worker + llarp_threadpool *worker = llarp_init_same_process_threadpool(); + llarp::Logic *logic = new llarp::Logic(worker); // configure main netloop struct dnsd_context dnsd; diff --git a/include/llarp.hpp b/include/llarp.hpp index 82119a3c5..94ec620c5 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -19,7 +19,7 @@ namespace llarp llarp_crypto crypto; llarp_router *router = nullptr; llarp_threadpool *worker = nullptr; - llarp_logic *logic = nullptr; + llarp::Logic *logic = nullptr; llarp_config *config = nullptr; llarp_nodedb *nodedb = nullptr; llarp_ev_loop *mainloop = nullptr; diff --git a/include/llarp/dns_dotlokilookup.hpp b/include/llarp/dns_dotlokilookup.hpp index cef0cb9e2..2eb70cf72 100644 --- a/include/llarp/dns_dotlokilookup.hpp +++ b/include/llarp/dns_dotlokilookup.hpp @@ -14,7 +14,7 @@ struct dotLokiLookup /// for timers (MAYBEFIXME? maybe we decouple this, yes pls have a generic /// passed in) // we can use DNSc for access to the logic - struct llarp_logic *logic; + llarp::Logic *logic; /// which ip tracker to use struct dns_iptracker *ip_tracker; diff --git a/include/llarp/dnsc.hpp b/include/llarp/dnsc.hpp index 5bb1573bf..7443167d0 100644 --- a/include/llarp/dnsc.hpp +++ b/include/llarp/dnsc.hpp @@ -85,7 +85,7 @@ struct dnsc_context // where to create the new sockets struct llarp_udp_io *udp; /// We will likely need something for timing events (timeouts) - struct llarp_logic *logic; + llarp::Logic *logic; }; /// async (blocking w/callback) resolve a hostname using generic socks @@ -108,8 +108,7 @@ llarp_host_resolved(dnsc_answer_request *const request); /// initialize dns subsystem and bind socket /// returns true on bind success otherwise returns false bool -llarp_dnsc_init(struct dnsc_context *const dnsc, - struct llarp_logic *const logic, +llarp_dnsc_init(struct dnsc_context *const dnsc, llarp::Logic *const logic, struct llarp_ev_loop *const netloop, const llarp::Addr &dnsc_sockaddr); diff --git a/include/llarp/dnsd.hpp b/include/llarp/dnsd.hpp index c2157174a..2afa59944 100644 --- a/include/llarp/dnsd.hpp +++ b/include/llarp/dnsd.hpp @@ -111,8 +111,7 @@ struct dnsd_context /// initialize dns subsystem and bind socket /// returns true on bind success otherwise returns false bool -llarp_dnsd_init(struct dnsd_context *const dnsd, - struct llarp_logic *const logic, +llarp_dnsd_init(struct dnsd_context *const dnsd, llarp::Logic *const logic, struct llarp_ev_loop *const netloop, const llarp::Addr &dnsd_sockaddr, const llarp::Addr &dnsc_sockaddr); diff --git a/include/llarp/ev.h b/include/llarp/ev.h index 11c7670c0..7e4ed449f 100644 --- a/include/llarp/ev.h +++ b/include/llarp/ev.h @@ -26,10 +26,14 @@ // forward declare struct llarp_threadpool; -struct llarp_logic; struct llarp_ev_loop; +namespace llarp +{ + class Logic; +} + /// allocator void llarp_ev_loop_alloc(struct llarp_ev_loop **ev); @@ -40,12 +44,12 @@ llarp_ev_loop_free(struct llarp_ev_loop **ev); /// run main loop int -llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic); +llarp_ev_loop_run(struct llarp_ev_loop *ev, llarp::Logic *logic); void llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev, struct llarp_threadpool *tp, - struct llarp_logic *logic); + llarp::Logic *logic); /// get the current time on the event loop llarp_time_t diff --git a/include/llarp/iwp.hpp b/include/llarp/iwp.hpp index 35776ebb0..7ce082723 100644 --- a/include/llarp/iwp.hpp +++ b/include/llarp/iwp.hpp @@ -6,7 +6,7 @@ struct llarp_iwp_args { struct llarp_crypto* crypto; - struct llarp_logic* logic; + llarp::Logic* logic; struct llarp_threadpool* cryptoworker; struct llarp_router* router; bool permitInbound; diff --git a/include/llarp/link/server.hpp b/include/llarp/link/server.hpp index a0fce9f0a..9e74762ff 100644 --- a/include/llarp/link/server.hpp +++ b/include/llarp/link/server.hpp @@ -71,7 +71,7 @@ namespace llarp TryEstablishTo(const RouterContact& rc); bool - Start(llarp_logic* l); + Start(llarp::Logic* l); void Stop(); @@ -139,7 +139,7 @@ namespace llarp void PutSession(ILinkSession* s); - llarp_logic* m_Logic = nullptr; + llarp::Logic* m_Logic = nullptr; llarp_ev_loop* m_Loop = nullptr; Addr m_ourAddr; llarp_udp_io m_udp; diff --git a/include/llarp/logic.hpp b/include/llarp/logic.hpp index 6b2fbf832..2f87286c5 100644 --- a/include/llarp/logic.hpp +++ b/include/llarp/logic.hpp @@ -4,49 +4,54 @@ #include #include -struct llarp_logic +namespace llarp { - struct llarp_threadpool* thread; - struct llarp_timer_context* timer; -}; + class Logic + { + private: + struct llarp_threadpool* thread; + struct llarp_timer_context* timer; -struct llarp_logic* -llarp_init_logic(); + public: + Logic() + : thread(llarp_init_same_process_threadpool()) + , timer(llarp_init_timer()) + { + } -/// single threaded mode logic event loop -struct llarp_logic* -llarp_init_single_process_logic(struct llarp_threadpool* tp); + Logic(struct llarp_threadpool* tp) : thread(tp), timer(llarp_init_timer()) + { + } -/// single threaded tick -void -llarp_logic_tick(struct llarp_logic* logic, llarp_time_t now); + /// single threaded tick + void + tick(llarp_time_t now); -/// isolated tick -void -llarp_logic_tick_async(struct llarp_logic* logic, llarp_time_t now); + /// isolated tick + void + tick_async(llarp_time_t now); -void -llarp_free_logic(struct llarp_logic** logic); + void + stop_timer(); -void -llarp_logic_queue_job(struct llarp_logic* logic, struct llarp_thread_job job); + void + stop(); -uint32_t -llarp_logic_call_later(struct llarp_logic* logic, struct llarp_timeout_job job); + void + mainloop(); -void -llarp_logic_cancel_call(struct llarp_logic* logic, uint32_t id); + void + queue_job(struct llarp_thread_job job); -void -llarp_logic_remove_call(struct llarp_logic* logic, uint32_t id); + uint32_t + call_later(struct llarp_timeout_job job); -void -llarp_logic_stop_timer(struct llarp_logic* logic); + void + cancel_call(uint32_t id); -void -llarp_logic_stop(struct llarp_logic* logic); - -void -llarp_logic_mainloop(struct llarp_logic* logic); + void + remove_call(uint32_t id); + }; +} // namespace llarp #endif diff --git a/include/llarp/nodedb.hpp b/include/llarp/nodedb.hpp index eee7b3e6c..8e658d23b 100644 --- a/include/llarp/nodedb.hpp +++ b/include/llarp/nodedb.hpp @@ -13,6 +13,11 @@ struct llarp_nodedb; +namespace llarp +{ + class Logic; +} + /// create an empty nodedb struct llarp_nodedb * llarp_nodedb_new(struct llarp_crypto *crypto); @@ -93,8 +98,8 @@ struct llarp_async_verify_rc void *user; /// nodedb storage struct llarp_nodedb *nodedb; - // llarp_logic for llarp_logic_queue_job - struct llarp_logic *logic; // includes a llarp_threadpool + // llarp::Logic for queue_job + llarp::Logic *logic; // includes a llarp_threadpool // struct llarp_crypto *crypto; // probably don't need this because we have // it in the nodedb struct llarp_threadpool *cryptoworker; @@ -128,8 +133,8 @@ struct llarp_async_load_rc void *user; /// nodedb storage struct llarp_nodedb *nodedb; - /// llarp_logic for calling hook - struct llarp_logic *logic; + /// llarp::Logic for calling hook + llarp::Logic *logic; /// disk worker threadpool struct llarp_threadpool *diskworker; /// target pubkey diff --git a/include/llarp/path.hpp b/include/llarp/path.hpp index 73b19dad8..e969b911e 100644 --- a/include/llarp/path.hpp +++ b/include/llarp/path.hpp @@ -628,7 +628,7 @@ namespace llarp llarp_crypto* Crypto(); - llarp_logic* + llarp::Logic* Logic(); llarp_router* diff --git a/include/llarp/router.h b/include/llarp/router.h index 6185d132e..d240bc24a 100644 --- a/include/llarp/router.h +++ b/include/llarp/router.h @@ -15,7 +15,7 @@ llarp_findOrCreateIdentity(struct llarp_crypto *crypto, const char *path, struct llarp_router * llarp_init_router(struct llarp_threadpool *worker, - struct llarp_ev_loop *netloop, struct llarp_logic *logic); + struct llarp_ev_loop *netloop, llarp::Logic *logic); void llarp_free_router(struct llarp_router **router); bool diff --git a/include/llarp/service/endpoint.hpp b/include/llarp/service/endpoint.hpp index 8d596dd39..671d4bdbb 100644 --- a/include/llarp/service/endpoint.hpp +++ b/include/llarp/service/endpoint.hpp @@ -47,11 +47,11 @@ namespace llarp Tick(llarp_time_t now); /// router's logic - llarp_logic* + llarp::Logic* RouterLogic(); /// endpoint's logic - llarp_logic* + llarp::Logic* EndpointLogic(); /// endpoint's net loop for sending data to user @@ -430,7 +430,7 @@ namespace llarp private: llarp_router* m_Router; llarp_threadpool* m_IsolatedWorker = nullptr; - llarp_logic* m_IsolatedLogic = nullptr; + llarp::Logic* m_IsolatedLogic = nullptr; llarp_ev_loop* m_IsolatedNetLoop = nullptr; std::string m_Keyfile; std::string m_Name; diff --git a/include/llarp/service/protocol.hpp b/include/llarp/service/protocol.hpp index e22743900..f551ae8c7 100644 --- a/include/llarp/service/protocol.hpp +++ b/include/llarp/service/protocol.hpp @@ -95,7 +95,7 @@ namespace llarp const byte_t* sharedkey, const Identity& localIdent); bool - AsyncDecryptAndVerify(llarp_logic* logic, llarp_crypto* c, + AsyncDecryptAndVerify(llarp::Logic* logic, llarp_crypto* c, const PathID_t& srcpath, llarp_threadpool* worker, const Identity& localIdent, IDataHandler* handler) const; diff --git a/libabyss/include/abyss/server.hpp b/libabyss/include/abyss/server.hpp index 860a3ae02..ed4092883 100644 --- a/libabyss/include/abyss/server.hpp +++ b/libabyss/include/abyss/server.hpp @@ -44,7 +44,7 @@ namespace abyss ~BaseReqHandler(); bool - ServeAsync(llarp_ev_loop* loop, llarp_logic* logic, + ServeAsync(llarp_ev_loop* loop, llarp::Logic* logic, const sockaddr* bindaddr); void @@ -75,7 +75,7 @@ namespace abyss OnAccept(struct llarp_tcp_acceptor*, struct llarp_tcp_conn*); llarp_ev_loop* m_loop; - llarp_logic* m_Logic; + llarp::Logic* m_Logic; llarp_tcp_acceptor m_acceptor; std::list< std::unique_ptr< IRPCHandler > > m_Conns; llarp_time_t m_ReqTimeout; diff --git a/libabyss/main.cpp b/libabyss/main.cpp index 9c45a59a4..4f734f682 100644 --- a/libabyss/main.cpp +++ b/libabyss/main.cpp @@ -104,7 +104,7 @@ main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[]) llarp_threadpool* threadpool = llarp_init_same_process_threadpool(); llarp_ev_loop* loop = nullptr; llarp_ev_loop_alloc(&loop); - llarp_logic* logic = llarp_init_single_process_logic(threadpool); + llarp::Logic* logic = new llarp::Logic(threadpool); sockaddr_in addr; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons(1222); diff --git a/libabyss/src/server.cpp b/libabyss/src/server.cpp index 518fbda61..dd5907db4 100644 --- a/libabyss/src/server.cpp +++ b/libabyss/src/server.cpp @@ -337,7 +337,7 @@ namespace abyss } bool - BaseReqHandler::ServeAsync(llarp_ev_loop* loop, llarp_logic* logic, + BaseReqHandler::ServeAsync(llarp_ev_loop* loop, llarp::Logic* logic, const sockaddr* bindaddr) { m_loop = loop; diff --git a/llarp/context.cpp b/llarp/context.cpp index f8f0eb262..a64bbf9f9 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -144,10 +144,10 @@ namespace llarp // ensure netio thread if(singleThreaded) { - logic = llarp_init_single_process_logic(worker); + logic = new Logic(worker); } else - logic = llarp_init_logic(); + logic = new Logic; router = llarp_init_router(worker, mainloop, logic); @@ -205,7 +205,7 @@ namespace llarp { llarp::LogDebug("stopping logic"); if(logic) - llarp_logic_stop(logic); + logic->stop(); llarp::LogDebug("stopping event loop"); if(mainloop) @@ -239,7 +239,7 @@ namespace llarp llarp_free_router(&router); llarp::LogDebug("free logic"); - llarp_free_logic(&logic); + delete logic; } bool @@ -318,7 +318,7 @@ extern "C" void llarp_main_abort(struct llarp_main *ptr) { - llarp_logic_stop_timer(ptr->ctx->router->logic); + ptr->ctx->router->logic->stop_timer(); } void @@ -443,8 +443,8 @@ extern "C" { // llarp::Info("llarp_main_queryDHT - setting up timer"); request->hook = &llarp_main_queryDHT_online; - llarp_logic_call_later(request->ptr->ctx->router->logic, - {1000, request, &llarp_main_checkOnline}); + request->ptr->ctx->router->logic->call_later( + {1000, request, &llarp_main_checkOnline}); // llarp_dht_lookup_router(ptr->ctx->router->dht, job); } diff --git a/llarp/dht.cpp b/llarp/dht.cpp index b614f0007..5b0a943bf 100644 --- a/llarp/dht.cpp +++ b/llarp/dht.cpp @@ -47,7 +47,7 @@ llarp_dht_lookup_router(struct llarp_dht_context *ctx, // llarp_rc_clear(&job->result); llarp::LogError("implement me llarp_dht_lookup_router"); /* - llarp_logic_queue_job(ctx->parent->logic, + ctx->parent->logic->queue_job( {job, &llarp::dht::Context::queue_router_lookup}); */ } diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 9888936da..77e2bca9e 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -103,8 +103,7 @@ namespace llarp return; Context *ctx = static_cast< Context * >(u); ctx->Explore(1); - llarp_logic_call_later(ctx->router->logic, - {orig, ctx, &handle_explore_timer}); + ctx->router->logic->call_later({orig, ctx, &handle_explore_timer}); } void @@ -269,8 +268,8 @@ namespace llarp services = new Bucket< ISNode >(ourKey); llarp::LogDebug("intialize dht with key ", ourKey); // start exploring - llarp_logic_call_later( - r->logic, + + r->logic->call_later( {exploreInterval, this, &llarp::dht::Context::handle_explore_timer}); // start cleanup timer ScheduleCleanupTimer(); @@ -279,8 +278,7 @@ namespace llarp void Context::ScheduleCleanupTimer() { - llarp_logic_call_later(router->logic, - {1000, this, &handle_cleaner_timer}); + router->logic->call_later({1000, this, &handle_cleaner_timer}); } void diff --git a/llarp/dns_dotlokilookup.cpp b/llarp/dns_dotlokilookup.cpp index bbdc50f7a..66123b3ca 100644 --- a/llarp/dns_dotlokilookup.cpp +++ b/llarp/dns_dotlokilookup.cpp @@ -465,8 +465,8 @@ llarp_dotlokilookup_handler(std::string name, } // nslookup on osx is about 5 sec before a retry, 2s on linux - llarp_logic_call_later(request->context->client.logic, - {2000, qr, &llarp_dotlokilookup_checkQuery}); + request->context->client.logic->call_later( + {2000, qr, &llarp_dotlokilookup_checkQuery}); response->dontSendResponse = true; // will send it shortly } diff --git a/llarp/dnsc.cpp b/llarp/dnsc.cpp index 7bcdfb774..17cea1e4f 100644 --- a/llarp/dnsc.cpp +++ b/llarp/dnsc.cpp @@ -785,8 +785,7 @@ llarp_host_resolved(dnsc_answer_request *const request) } bool -llarp_dnsc_init(struct dnsc_context *const dnsc, - struct llarp_logic *const logic, +llarp_dnsc_init(struct dnsc_context *const dnsc, llarp::Logic *const logic, struct llarp_ev_loop *const netloop, const llarp::Addr &dnsc_sockaddr) { diff --git a/llarp/dnsd.cpp b/llarp/dnsd.cpp index 134d07da3..42db0c03c 100644 --- a/llarp/dnsd.cpp +++ b/llarp/dnsd.cpp @@ -550,8 +550,7 @@ raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr, } bool -llarp_dnsd_init(struct dnsd_context *const dnsd, - struct llarp_logic *const logic, +llarp_dnsd_init(struct dnsd_context *const dnsd, llarp::Logic *const logic, struct llarp_ev_loop *const netloop, const llarp::Addr &dnsd_sockaddr, const llarp::Addr &dnsc_sockaddr) diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 53d6e3731..ddcd7bc7f 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -45,14 +45,14 @@ llarp_ev_loop_free(struct llarp_ev_loop **ev) } int -llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic) +llarp_ev_loop_run(struct llarp_ev_loop *ev, llarp::Logic *logic) { while(ev->running()) { ev->_now = llarp::time_now_ms(); ev->tick(EV_TICK_INTERVAL); if(ev->running()) - llarp_logic_tick(logic, ev->_now); + logic->tick(ev->_now); } return 0; } @@ -66,7 +66,7 @@ llarp_fd_promise_wait_for_value(struct llarp_fd_promise *p) void llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev, struct llarp_threadpool *tp, - struct llarp_logic *logic) + llarp::Logic *logic) { while(ev->running()) { @@ -74,7 +74,7 @@ llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev, ev->tick(EV_TICK_INTERVAL); if(ev->running()) { - llarp_logic_tick_async(logic, ev->_now); + logic->tick_async(ev->_now); llarp_threadpool_tick(tp); } } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 82117a3ac..ad39f9f39 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -431,7 +431,7 @@ namespace llarp TunEndpoint::Tick(llarp_time_t now) { // call tun code in endpoint logic in case of network isolation - // llarp_logic_queue_job(EndpointLogic(), {this, handleTickTun}); + // EndpointLogic()->queue_job({this, handleTickTun}); FlushSend(); Endpoint::Tick(now); } @@ -628,7 +628,7 @@ namespace llarp llarp::LogWarn("packet dropped"); }); if(self->m_UserToNetworkPktQueue.Size()) - llarp_logic_queue_job(self->RouterLogic(), {self, &handleNetSend}); + self->RouterLogic()->queue_job({self, &handleNetSend}); } void diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index 03ccf0f71..f90783f30 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -137,7 +137,7 @@ namespace llarp } bool - ILinkLayer::Start(llarp_logic* l) + ILinkLayer::Start(Logic* l) { m_Logic = l; ScheduleTick(100); @@ -148,7 +148,7 @@ namespace llarp ILinkLayer::Stop() { if(m_Logic && tick_id) - llarp_logic_remove_call(m_Logic, tick_id); + m_Logic->remove_call(tick_id); { Lock l(m_AuthedLinksMutex); auto itr = m_AuthedLinks.begin(); @@ -284,8 +284,7 @@ namespace llarp void ILinkLayer::ScheduleTick(uint64_t interval) { - tick_id = llarp_logic_call_later( - m_Logic, {interval, this, &ILinkLayer::on_timer_tick}); + tick_id = m_Logic->call_later({interval, this, &ILinkLayer::on_timer_tick}); } } // namespace llarp diff --git a/llarp/logic.cpp b/llarp/logic.cpp index e775bd7ca..98c91a476 100644 --- a/llarp/logic.cpp +++ b/llarp/logic.cpp @@ -2,110 +2,78 @@ #include #include "logger.hpp" -struct llarp_logic* -llarp_init_logic() +namespace llarp { - llarp_logic* logic = new llarp_logic; - if(logic) + void + Logic::tick(llarp_time_t now) { - logic->thread = llarp_init_same_process_threadpool(); - logic->timer = llarp_init_timer(); + llarp_timer_set_time(this->timer, now); + llarp_timer_tick_all(this->timer); + llarp_threadpool_tick(this->thread); } - return logic; -}; -struct llarp_logic* -llarp_init_single_process_logic(struct llarp_threadpool* tp) -{ - llarp_logic* logic = new llarp_logic; - if(logic) + void + Logic::tick_async(llarp_time_t now) { - logic->thread = tp; - logic->timer = llarp_init_timer(); + llarp_timer_tick_all_async(this->timer, this->thread, now); + llarp_threadpool_tick(this->thread); } - return logic; -} - -void -llarp_logic_tick(struct llarp_logic* logic, llarp_time_t now) -{ - llarp_timer_set_time(logic->timer, now); - llarp_timer_tick_all(logic->timer); - llarp_threadpool_tick(logic->thread); -} -void -llarp_logic_tick_async(struct llarp_logic* logic, llarp_time_t now) -{ - llarp_timer_tick_all_async(logic->timer, logic->thread, now); - llarp_threadpool_tick(logic->thread); -} - -void -llarp_free_logic(struct llarp_logic** logic) -{ - if(*logic) + void + Logic::stop_timer() { - // llarp_free_timer(&(*logic)->timer); - delete *logic; + llarp_timer_stop(this->timer); } - *logic = nullptr; -} -void -llarp_logic_stop_timer(struct llarp_logic* logic) -{ - if(logic->timer) - llarp_timer_stop(logic->timer); -} - -void -llarp_logic_stop(struct llarp_logic* logic) -{ - llarp::LogDebug("logic thread stop"); - if(logic->thread) + void + Logic::queue_job(struct llarp_thread_job job) { - llarp_threadpool_stop(logic->thread); - llarp_threadpool_join(logic->thread); + if(job.user && job.work) + llarp_threadpool_queue_job(this->thread, {job.user, job.work}); } - llarp_free_threadpool(&logic->thread); - llarp::LogDebug("logic timer stop"); - if(logic->timer) - llarp_timer_stop(logic->timer); -} + void + Logic::stop() + { + llarp::LogDebug("logic thread stop"); + if(this->thread) + { + llarp_threadpool_stop(this->thread); + llarp_threadpool_join(this->thread); + } + llarp_free_threadpool(&this->thread); -void -llarp_logic_mainloop(struct llarp_logic* logic) -{ - llarp_timer_run(logic->timer, logic->thread); -} + llarp::LogDebug("logic timer stop"); + if(this->timer) + llarp_timer_stop(this->timer); + } -void -llarp_logic_queue_job(struct llarp_logic* logic, struct llarp_thread_job job) -{ - if(job.user && job.work) - llarp_threadpool_queue_job(logic->thread, {job.user, job.work}); -} + void + Logic::mainloop() + { + llarp_timer_run(this->timer, this->thread); + } -uint32_t -llarp_logic_call_later(struct llarp_logic* logic, struct llarp_timeout_job job) -{ - llarp_timeout_job j; - j.user = job.user; - j.timeout = job.timeout; - j.handler = job.handler; - return llarp_timer_call_later(logic->timer, j); -} + uint32_t + Logic::call_later(struct llarp_timeout_job job) + { + llarp_timeout_job j; + j.user = job.user; + j.timeout = job.timeout; + j.handler = job.handler; + return llarp_timer_call_later(this->timer, j); + } -void -llarp_logic_cancel_call(struct llarp_logic* logic, uint32_t id) -{ - llarp_timer_cancel_job(logic->timer, id); -} + void + Logic::cancel_call(uint32_t id) + { + llarp_timer_cancel_job(this->timer, id); + } -void -llarp_logic_remove_call(struct llarp_logic* logic, uint32_t id) -{ - llarp_timer_remove_job(logic->timer, id); -} + void + Logic::remove_call(uint32_t id) + { + llarp_timer_remove_job(this->timer, id); + } + +} // namespace llarp diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index e28a03b04..8f018fa83 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -232,8 +232,8 @@ disk_threadworker_setRC(void *user) static_cast< llarp_async_verify_rc * >(user); verify_request->valid = verify_request->nodedb->Insert(verify_request->rc); if(verify_request->logic) - llarp_logic_queue_job(verify_request->logic, - {verify_request, &logic_threadworker_callback}); + verify_request->logic->queue_job( + {verify_request, &logic_threadworker_callback}); } // we run the crypto verify in the crypto threadpool worker @@ -256,8 +256,8 @@ crypto_threadworker_verifyrc(void *user) // callback to logic thread if(!verify_request->valid) llarp::LogWarn("RC is not valid, can't save to disk"); - llarp_logic_queue_job(verify_request->logic, - {verify_request, &logic_threadworker_callback}); + verify_request->logic->queue_job( + {verify_request, &logic_threadworker_callback}); } } @@ -279,7 +279,7 @@ nodedb_async_load_rc(void *user) { job->nodedb->Get(job->pubkey, job->result); } - llarp_logic_queue_job(job->logic, {job, &nodedb_inform_load_rc}); + job->logic->queue_job({job, &nodedb_inform_load_rc}); } struct llarp_nodedb * diff --git a/llarp/path.cpp b/llarp/path.cpp index 54b770ed4..ed4306484 100644 --- a/llarp/path.cpp +++ b/llarp/path.cpp @@ -45,7 +45,7 @@ namespace llarp return &m_Router->crypto; } - llarp_logic* + llarp::Logic* PathContext::Logic() { return m_Router->logic; diff --git a/llarp/pathbuilder.cpp b/llarp/pathbuilder.cpp index bbcffd9ca..1b984ba09 100644 --- a/llarp/pathbuilder.cpp +++ b/llarp/pathbuilder.cpp @@ -22,7 +22,7 @@ namespace llarp size_t idx = 0; llarp_router* router = nullptr; llarp_threadpool* worker = nullptr; - llarp_logic* logic = nullptr; + llarp::Logic* logic = nullptr; llarp_crypto* crypto = nullptr; LR_CommitMessage LRCM; @@ -102,7 +102,7 @@ namespace llarp if(isFarthestHop) { // farthest hop - llarp_logic_queue_job(ctx->logic, {ctx, &HandleDone}); + ctx->logic->queue_job({ctx, &HandleDone}); } else { @@ -117,7 +117,7 @@ namespace llarp /// Generate all keys asynchronously and call hadler when done void - AsyncGenerateKeys(Path_t* p, llarp_logic* l, llarp_threadpool* pool, + AsyncGenerateKeys(Path_t* p, llarp::Logic* l, llarp_threadpool* pool, User* u, Handler func) { path = p; diff --git a/llarp/relay_commit.cpp b/llarp/relay_commit.cpp index 2b84b3ac5..3f829301a 100644 --- a/llarp/relay_commit.cpp +++ b/llarp/relay_commit.cpp @@ -303,13 +303,13 @@ namespace llarp // we are the farthest hop llarp::LogDebug("We are the farthest hop for ", info); // send a LRAM down the path - llarp_logic_queue_job(self->context->Logic(), {self, &SendPathConfirm}); + self->context->Logic()->queue_job({self, &SendPathConfirm}); } else { // forward upstream // we are still in the worker thread so post job to logic - llarp_logic_queue_job(self->context->Logic(), {self, &SendLRCM}); + self->context->Logic()->queue_job({self, &SendLRCM}); } } }; diff --git a/llarp/router.cpp b/llarp/router.cpp index f0b056d66..4be75083d 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -116,7 +116,7 @@ llarp_router_try_connect(struct llarp_router *router, std::make_unique< TryConnectJob >(remote, link, numretries, router))); TryConnectJob *job = itr.first->second.get(); // try establishing async - llarp_logic_queue_job(router->logic, {job, &on_try_connecting}); + router->logic->queue_job({job, &on_try_connecting}); return true; } @@ -630,8 +630,7 @@ llarp_router::SendTo(llarp::RouterID remote, const llarp::ILinkMessage *msg, void llarp_router::ScheduleTicker(uint64_t ms) { - ticker_job_id = - llarp_logic_call_later(logic, {ms, this, &handle_router_ticker}); + ticker_job_id = logic->call_later({ms, this, &handle_router_ticker}); } void @@ -1036,7 +1035,7 @@ llarp_router::HasPendingConnectJob(const llarp::RouterID &remote) struct llarp_router * llarp_init_router(struct llarp_threadpool *tp, struct llarp_ev_loop *netloop, - struct llarp_logic *logic) + llarp::Logic *logic) { llarp_router *router = new llarp_router(); if(router) diff --git a/llarp/router.hpp b/llarp/router.hpp index 41b18329b..6ae15279f 100644 --- a/llarp/router.hpp +++ b/llarp/router.hpp @@ -70,7 +70,7 @@ struct llarp_router llarp_ev_loop *netloop; llarp_threadpool *tp; - llarp_logic *logic; + llarp::Logic *logic; llarp_crypto crypto; llarp::path::PathContext paths; llarp::exit::Context exitContext; diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 6162badb6..d5638d710 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -1363,7 +1363,7 @@ namespace llarp struct AsyncKeyExchange { - llarp_logic* logic; + llarp::Logic* logic; llarp_crypto* crypto; SharedSecret sharedKey; ServiceInfo remote; @@ -1377,7 +1377,7 @@ namespace llarp IDataHandler* handler; ConvoTag tag; - AsyncKeyExchange(llarp_logic* l, llarp_crypto* c, const ServiceInfo& r, + AsyncKeyExchange(llarp::Logic* l, llarp_crypto* c, const ServiceInfo& r, const Identity& localident, const PQPubKey& introsetPubKey, const Introduction& remote, IDataHandler* h, @@ -1437,7 +1437,7 @@ namespace llarp // encrypt and sign if(self->frame.EncryptAndSign(self->crypto, self->msg, K, self->m_LocalIdentity)) - llarp_logic_queue_job(self->logic, {self, &Result}); + self->logic->queue_job({self, &Result}); else { llarp::LogError("failed to encrypt and sign"); @@ -1711,13 +1711,13 @@ namespace llarp } } - llarp_logic* + llarp::Logic* Endpoint::RouterLogic() { return m_Router->logic; } - llarp_logic* + llarp::Logic* Endpoint::EndpointLogic() { return m_IsolatedLogic ? m_IsolatedLogic : m_Router->logic; diff --git a/llarp/service/protocol.cpp b/llarp/service/protocol.cpp index ccb6f5706..5af7783ce 100644 --- a/llarp/service/protocol.cpp +++ b/llarp/service/protocol.cpp @@ -208,13 +208,13 @@ namespace llarp struct AsyncFrameDecrypt { llarp_crypto* crypto; - llarp_logic* logic; + llarp::Logic* logic; ProtocolMessage* msg; const Identity& m_LocalIdentity; IDataHandler* handler; const ProtocolFrame frame; - AsyncFrameDecrypt(llarp_logic* l, llarp_crypto* c, + AsyncFrameDecrypt(llarp::Logic* l, llarp_crypto* c, const Identity& localIdent, IDataHandler* h, ProtocolMessage* m, const ProtocolFrame& f) : crypto(c) @@ -286,8 +286,7 @@ namespace llarp self->handler->PutCachedSessionKeyFor(self->msg->tag, sharedKey); self->msg->handler = self->handler; - llarp_logic_queue_job(self->logic, - {self->msg, &ProtocolMessage::ProcessAsync}); + self->logic->queue_job({self->msg, &ProtocolMessage::ProcessAsync}); delete self; } }; @@ -306,7 +305,7 @@ namespace llarp } bool - ProtocolFrame::AsyncDecryptAndVerify(llarp_logic* logic, llarp_crypto* c, + ProtocolFrame::AsyncDecryptAndVerify(llarp::Logic* logic, llarp_crypto* c, const PathID_t& srcPath, llarp_threadpool* worker, const Identity& localIdent, @@ -349,7 +348,7 @@ namespace llarp } msg->srcPath = srcPath; msg->handler = handler; - llarp_logic_queue_job(logic, {msg, &ProtocolMessage::ProcessAsync}); + logic->queue_job({msg, &ProtocolMessage::ProcessAsync}); return true; } diff --git a/test/jsonrpc_unittest.cpp b/test/jsonrpc_unittest.cpp index 3fade3a4f..798a75b9c 100644 --- a/test/jsonrpc_unittest.cpp +++ b/test/jsonrpc_unittest.cpp @@ -9,7 +9,7 @@ struct AbyssTestBase : public ::testing::Test llarp_crypto crypto; llarp_threadpool* threadpool = nullptr; llarp_ev_loop* loop = nullptr; - llarp_logic* logic = nullptr; + llarp::Logic* logic = nullptr; abyss::httpd::BaseReqHandler* server = nullptr; abyss::http::JSONRPC* client = nullptr; const std::string method = "test.method"; @@ -53,7 +53,7 @@ struct AbyssTestBase : public ::testing::Test if(server->ServeAsync(loop, logic, a)) { client->RunAsync(loop, a.ToString()); - llarp_logic_call_later(logic, {1000, this, &CancelIt}); + logic->call_later({1000, this, &CancelIt}); return; } std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -65,7 +65,7 @@ struct AbyssTestBase : public ::testing::Test { if(server) server->Close(); - llarp_logic_stop(logic); + logic->stop(); llarp_ev_loop_stop(loop); llarp_threadpool_stop(threadpool); } @@ -168,7 +168,7 @@ struct AbyssTest : public AbyssTestBase, void AsyncFlush() { - llarp_logic_queue_job(logic, {this, &FlushIt}); + logic->queue_job({this, &FlushIt}); } void diff --git a/test/test_dns_unit.cpp b/test/test_dns_unit.cpp index 25ff22ced..b9f789eff 100644 --- a/test/test_dns_unit.cpp +++ b/test/test_dns_unit.cpp @@ -1,7 +1,7 @@ #include -#include // for llarp_main_init -#include // for threadpool/llarp_logic -#include "llarp/net.hpp" // for llarp::Addr +#include // for llarp_main_init +#include // for threadpool/llarp::Logic +#include "llarp/net.hpp" // for llarp::Addr #include "llarp/dns.hpp" #include "llarp/dnsc.hpp" @@ -91,7 +91,6 @@ TEST_F(DNSTest, TestCodeDomain) // test decoders TEST_F(DNSTest, TestDecodeHdr) { - dns_msg_header hdr; ASSERT_TRUE(decode_hdr(&this->buffer_t, &hdr)); // rewind @@ -197,7 +196,8 @@ TEST_F(DNSTest, handleDNSrecvFrom) llarp::Zero(&buffer, 15); ssize_t sz = 0; // hdr->qr decides dnsc (1) or dnsd (0) - llarp_handle_dns_recvfrom((llarp_udp_io *)&udp, &addr, llarp::InitBuffer(buffer, sz)); + llarp_handle_dns_recvfrom((llarp_udp_io *)&udp, &addr, + llarp::InitBuffer(buffer, sz)); // llarp_handle_dnsc_recvfrom // llarp_handle_dnsd_recvfrom } diff --git a/test/test_dnsc_unit.cpp b/test/test_dnsc_unit.cpp index e585fbea5..711b1cbbc 100644 --- a/test/test_dnsc_unit.cpp +++ b/test/test_dnsc_unit.cpp @@ -1,5 +1,5 @@ #include -#include // for llarp_main_init -#include // for threadpool/llarp_logic -#include "llarp/net.hpp" // for llarp::Addr +#include // for llarp_main_init +#include // for threadpool/llarp::Logic +#include "llarp/net.hpp" // for llarp::Addr #include "llarp/dnsc.hpp" diff --git a/test/test_dnsd_unit.cpp b/test/test_dnsd_unit.cpp index cca1e4d43..6f4382bd2 100644 --- a/test/test_dnsd_unit.cpp +++ b/test/test_dnsd_unit.cpp @@ -1,6 +1,6 @@ #include #include // for llarp_main_init -#include // for threadpool/llarp_logic +#include // for threadpool/llarp::Logic #include "llarp/net.hpp" // for llarp::Addr #include "llarp/dnsd.hpp"