diff --git a/.gitignore b/.gitignore index ae1054b9a..bc0c35b2e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.a *.o *.plist +*.so llarpd diff --git a/Makefile b/Makefile index a558c4fb4..e7f188cef 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,14 @@ REPO := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) EXE = $(REPO)/llarpd STATIC_LIB = $(REPO)/libllarp.a +SHARED_LIB = $(REPO)/libllarp.so STATIC_SRC_CPP = $(wildcard $(REPO)/llarp/*.cpp) STATIC_SRC_C = $(wildcard $(REPO)/llarp/*.c) STATIC_OBJ = $(STATIC_SRC_CPP:.cpp=.cpp.o) $(STATIC_SRC_C:.c=.c.o) + DAEMON_SRC = $(wildcard $(REPO)/daemon/*.c) DAEMON_OBJ = $(DAEMON_SRC:.c=.c.o) @@ -39,9 +41,10 @@ ifneq ($(GIT_VERSION),"") VER_FLAGS=-DGIT_REV=\"$(GIT_VERSION)\" endif -REQUIRED_CFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c99 $(CFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -REQUIRED_CXXFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c++17 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -REQUIRED_LDFLAGS = $(LDFLAGS) -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++ +REQUIRED_CFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c99 $(CFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC +REQUIRED_CXXFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c++17 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC +LIB_LDFLAGS = -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++ +REQUIRED_LDFLAGS = -L$(REPO) -lllarp all: build @@ -53,20 +56,20 @@ build: $(EXE) test: $(TEST_OBJ_CPP) $(TEST_OBJ_C) -$(TEST_SRC): $(STATIC_LIB) +$(TEST_SRC): $(SHARED_LIB) $(TEST_OBJ_CPP): $(TEST_SRC_CPP) - $(CXX) $(REQUIRED_CXXFLAGS) $< -o $<.bin $(STATIC_LIB) $(REQUIRED_LDFLAGS) + $(CXX) $(REQUIRED_CXXFLAGS) $< -o $<.bin $(REQUIRED_LDFLAGS) mv $<.bin $<.test $<.test $(TEST_OBJ_C): $(TEST_SRC_C) - $(CC) $(REQUIRED_CFLAGS) $< -o $<.bin $(STATIC_LIB) $(REQUIRED_LDFLAGS) + $(CC) $(REQUIRED_CFLAGS) $< -o $<.bin $(REQUIRED_LDFLAGS) mv $<.bin $<.test $<.test $(EXE): $(DAEMON_OBJ) $(STATIC_LIB) - $(CXX) $(DAEMON_OBJ) $(STATIC_LIB) $(REQUIRED_LDFLAGS) -o $(EXE) + $(CXX) $(DAEMON_OBJ) $(STATIC_LIB) $(LIB_LDFLAGS) -o $(EXE) %.cpp.o: %.cpp $(CXX) $(REQUIRED_CXXFLAGS) -c $< -o $<.o @@ -74,8 +77,11 @@ $(EXE): $(DAEMON_OBJ) $(STATIC_LIB) %.c.o: %.c $(CC) $(REQUIRED_CFLAGS) -c $< -o $<.o -$(STATIC_LIB): $(STATIC_OBJ) +$(STATIC_LIB): $(STATIC_OBJ) $(AR) -r $(STATIC_LIB) $(STATIC_OBJ) +$(SHARED_LIB): $(STATIC_OBJ) + $(CXX) -shared -o $(SHARED_LIB) $(STATIC_OBJ) $(LIB_LDFLAGS) + clean: - $(RM) $(DAEMON_OBJ) $(EXE) $(STATIC_OBJ) $(STATIC_LIB) $(TEST_OBJ) + $(RM) $(DAEMON_OBJ) $(EXE) $(STATIC_OBJ) $(STATIC_LIB) $(TEST_OBJ) $(SHARED_LIB) diff --git a/daemon.ini b/daemon.ini index 2a379aa19..144ebd0a4 100644 --- a/daemon.ini +++ b/daemon.ini @@ -1,4 +1,4 @@ [router] [links] -eth0=ip \ No newline at end of file +lo=ip diff --git a/daemon/main.c b/daemon/main.c index ee5ff839c..f5887df62 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -38,6 +38,7 @@ int shutdown_llarp(struct llarp_main *m) { printf("."); llarp_free_threadpool(&m->tp); printf(".\n"); + return 0; } int main(int argc, char *argv[]) { diff --git a/include/llarp/dht.h b/include/llarp/dht.h new file mode 100644 index 000000000..fdb380d34 --- /dev/null +++ b/include/llarp/dht.h @@ -0,0 +1,16 @@ +#ifndef LLARP_DHT_H_ +#define LLARP_DHT_H_ +#ifdef __cplusplus +extern "C" { +#endif + + struct llarp_dht_context; + + struct llarp_dht_context * llarp_dht_context_new(); + void llarp_dht_context_free(struct llarp_dht_context * dht); + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/include/llarp/ev.h b/include/llarp/ev.h index f9eab59b5..b103db07c 100644 --- a/include/llarp/ev.h +++ b/include/llarp/ev.h @@ -20,8 +20,7 @@ int llarp_ev_loop_run(struct llarp_ev_loop *ev); void llarp_ev_loop_stop(struct llarp_ev_loop *ev); struct llarp_udp_listener { - char *host; - uint16_t port; + struct sockaddr_in6 * addr; void *user; void *impl; void (*recvfrom)(struct llarp_udp_listener *, const struct sockaddr *, char *, diff --git a/include/llarp/ibfq.h b/include/llarp/ibfq.h index 1957774fe..cb2d87aa8 100644 --- a/include/llarp/ibfq.h +++ b/include/llarp/ibfq.h @@ -7,22 +7,6 @@ extern "C" { #endif -// forward declare -struct llarp_msg_muxer; - -struct llarp_link_queue; - -struct llarp_link_queue *llarp_init_link_queue(); -void llarp_free_link_queue(struct llarp_link_queue **queue); -/** - offer a full frame to the inbound frame queue - return true if successfully added - return false if the queue is full - */ -bool llarp_link_offer_frame(struct llarp_link_queue *queue, llarp_buffer_t msg); -/** return true if we have more messages to process */ -bool llarp_link_queue_process(struct llarp_link_queue *queue, - struct llarp_msg_muxer *muxer); #ifdef __cplusplus } diff --git a/include/llarp/link.h b/include/llarp/link.h index 2cbd85c9c..985dfa638 100644 --- a/include/llarp/link.h +++ b/include/llarp/link.h @@ -1,24 +1,31 @@ #ifndef LLARP_LINK_H_ #define LLARP_LINK_H_ #include -#include +#include #include #include +#include +#include + #ifdef __cplusplus extern "C" { #endif struct llarp_link; -struct llarp_link *llarp_link_alloc(); +struct llarp_link *llarp_link_alloc(struct llarp_msg_muxer * muxer); void llarp_link_free(struct llarp_link **link); -bool llarp_link_configure(struct llarp_link *link, const char *ifname, int af); +bool llarp_link_configure_addr(struct llarp_link *link, const char *ifname, int af, uint16_t port); /** get link listener for events */ struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *link); +void llarp_link_start(struct llarp_link * link); + + struct llarp_link_queue * llarp_link_frame_queue(struct llarp_link*link); + void llarp_link_stop(struct llarp_link *link); struct llarp_link_session; diff --git a/include/llarp/msg_handler.h b/include/llarp/msg_handler.h index 5f9f0c624..9bda61777 100644 --- a/include/llarp/msg_handler.h +++ b/include/llarp/msg_handler.h @@ -1,33 +1,53 @@ #ifndef LLARP_MSG_HANDLER_H_ #define LLARP_MSG_HANDLER_H_ #include -#include +#include #include #ifdef __cplusplus extern "C" { #endif + /* foward declare */ + struct llarp_msg_muxer; + struct llarp_link_session; + struct llarp_router; + struct llarp_frame_handler { - struct llarp_obmd *outbound; - struct llarp_ibmq *inbound; + + /** + * participating paths + */ + struct llarp_path_context * paths; + + + /** + * parent muxer + */ + struct llarp_msg_muxer * parent; + + /** + handle fully formed frame from link session + */ bool (*process)(struct llarp_frame_handler *, struct llarp_link_session *, llarp_buffer_t); }; struct llarp_msg_handler { struct llarp_path_context *paths; + struct llarp_dht_context *dht; + bool (*process)(struct llarp_msg_handler *, llarp_buffer_t); }; struct llarp_msg_muxer { /** get a message handler for a link level message given msg.a */ - struct llarp_frame_handler *(*link_handler_for)(const char *); + struct llarp_frame_handler *(*link_handler_for)(struct llarp_router *, const char); /** get a message handler for a routing layer message given msg.A */ - struct llarp_msg_handler *(*routing_handler_for)(const char *); + struct llarp_msg_handler *(*routing_handler_for)(struct llarp_router *, const char); }; /** fill function pointers with default values */ -void llarp_msg_handler_mux_init(struct llarp_msg_muxer *muxer); +void llarp_msg_muxer_init(struct llarp_msg_muxer *muxer); #ifdef __cplusplus } diff --git a/llarp/address_info.cpp b/llarp/address_info.cpp index f7147a63a..21b235e80 100644 --- a/llarp/address_info.cpp +++ b/llarp/address_info.cpp @@ -3,12 +3,20 @@ #include "str.hpp" namespace llarp { -static void *ai_alloc(size_t sz, size_t align) {} + template<> + bool BEncode(const llarp_ai &a, llarp_buffer_t *buff) { + return bencodeDict(buff) && bencodeDict_Int(buff, "c", a.rank) && + bencodeDict_Bytes(buff, "e", a.enc_key, sizeof(a.enc_key)) && + bencodeDict_Bytes(buff, "d", a.dialect, + UStrLen(a.dialect, sizeof(a.dialect))) && + bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) && + bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) && + bencodeEnd(buff); +} } // namespace llarp -extern "C" { -struct llarp_alloc *llarp_ai_alloc = &llarp_g_mem; +extern "C" { bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) { return llarp::BEncode(*ai, buff); @@ -24,15 +32,3 @@ void llarp_ai_list_iterate(struct llarp_ai_list *l, } while (cur->next); } } - -namespace llarp { -bool BEncode(const llarp_ai &a, llarp_buffer_t *buff) { - return bencodeDict(buff) && bencodeDict_Int(buff, "c", a.rank) && - bencodeDict_Bytes(buff, "e", a.enc_key, sizeof(a.enc_key)) && - bencodeDict_Bytes(buff, "d", a.dialect, - UStrLen(a.dialect, sizeof(a.dialect))) && - bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) && - bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) && - bencodeEnd(buff); -} -} // namespace llarp diff --git a/llarp/buffer.cpp b/llarp/buffer.cpp index e0e3b69ef..fd164f319 100644 --- a/llarp/buffer.cpp +++ b/llarp/buffer.cpp @@ -1,4 +1,5 @@ #include +#include extern "C" { @@ -11,4 +12,16 @@ size_t llarp_buffer_size_left(llarp_buffer_t *buff) { else return buff->sz - diff; } + +bool llarp_buffer_write(llarp_buffer_t * buff, const void * data, size_t sz) +{ + size_t left = llarp_buffer_size_left(buff); + if (sz > left) + { + return false; + } + std::memcpy(buff->cur, data, sz); + buff->cur += sz; + return true; +} } diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 6f5abceb8..05af0f253 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -97,6 +97,7 @@ static void udp_close_cb(uv_handle_t *handle) { l->closed(); delete l; } + } // namespace llarp namespace llarp { @@ -134,8 +135,6 @@ int llarp_ev_loop_run(struct llarp_ev_loop *ev) { int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev, struct llarp_udp_listener *listener) { - sockaddr_in6 addr; - uv_ip6_addr(listener->host, listener->port, &addr); int ret = 0; llarp::udp_listener *l = new llarp::udp_listener; listener->impl = l; @@ -144,10 +143,13 @@ int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev, ret = uv_udp_init(ev->loop(), l->udp()); if (ret == 0) { - ret = uv_udp_bind(l->udp(), (const sockaddr *)&addr, 0); + ret = uv_udp_bind(l->udp(), (sockaddr*) listener->addr, 0); if (ret == 0) { + char addr [128] = {0}; + uv_ip6_name(listener->addr, addr, sizeof(addr)); + printf("bound udp listener at %s port %d\n", addr, ntohs(listener->addr->sin6_port)); ret = - uv_udp_recv_start(l->udp(), llarp::udp_alloc_cb, llarp::udp_recv_cb); + uv_udp_recv_start(l->udp(), &llarp::udp_alloc_cb, &llarp::udp_recv_cb); } } return ret; @@ -159,8 +161,7 @@ int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) { llarp::udp_listener *l = static_cast(listener->impl); if (l) { if (!uv_udp_recv_stop(l->udp())) { - l->closed(); - delete l; + uv_close((uv_handle_t*)l->udp(), &llarp::udp_close_cb); ret = 0; } } @@ -190,6 +191,6 @@ bool llarp_ev_call_async(struct llarp_ev_caller *caller, void *user) { } void llarp_ev_caller_stop(struct llarp_ev_caller *caller) { - uv_close((uv_handle_t *)&caller->async, llarp::ev_caller_async_closed); + uv_close((uv_handle_t *)&caller->async, &llarp::ev_caller_async_closed); } } diff --git a/llarp/exit_info.cpp b/llarp/exit_info.cpp index cac0be313..10abd1c36 100644 --- a/llarp/exit_info.cpp +++ b/llarp/exit_info.cpp @@ -1,17 +1,19 @@ #include "exit_info.hpp" #include "bencode.hpp" -extern "C" { -bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) { - return llarp::BEncode(*xi, buff); -} -} - namespace llarp { -bool BEncode(const llarp_xi &xi, llarp_buffer_t *buff) { + template<> + bool BEncode(const struct llarp_xi &xi, llarp_buffer_t *buff) { return bencodeDict(buff) && bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) && bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) && bencodeDict_Int(buff, "v", 0) && bencodeEnd(buff); } } // namespace llarp + + +extern "C" { +bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) { + return llarp::BEncode(*xi, buff); +} +} diff --git a/llarp/link.cpp b/llarp/link.cpp index b471e4ba4..d77e4e8bd 100644 --- a/llarp/link.cpp +++ b/llarp/link.cpp @@ -1,6 +1,9 @@ #include "link.hpp" +#include "net.hpp" #include #include +#include +#include bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) { return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr, @@ -8,6 +11,11 @@ bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) { addr0.sin6_port < addr1.sin6_port; } + +namespace llarp +{ +} + extern "C" { struct llarp_link *llarp_link_alloc() { return new llarp_link; @@ -22,9 +30,21 @@ struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *l) { return &l->listener; } -bool llarp_link_configure(struct llarp_link *link, const char *ifname, int af) { - return false; -} + bool llarp_link_configure_addr(struct llarp_link *link, const char *ifname, int af, uint16_t port) { + af = AF_INET6; + link->af = af; + if(llarp::net::GetIfAddr(ifname, link->af, (sockaddr*)&link->localaddr)) + { + link->localaddr.sin6_family = af; + link->localaddr.sin6_port = htons(port); + link->listener.addr = &link->localaddr; + char buff[128] = {0}; + uv_ip6_name(&link->localaddr, buff, sizeof(buff)); + printf("link %s configured with address %s\n", ifname, buff); + return true; + } + return false; + } void llarp_link_stop(struct llarp_link *link) { llarp_ev_close_udp_listener(&link->listener); diff --git a/llarp/link_handlers.hpp b/llarp/link_handlers.hpp new file mode 100644 index 000000000..a17351d3c --- /dev/null +++ b/llarp/link_handlers.hpp @@ -0,0 +1,19 @@ +#ifndef LLARP_LINK_HANDLERS_HPP +#define LLARP_LINK_HANDLERS_HPP +#include + +namespace llarp +{ + namespace frame + { + bool process_intro(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); + bool process_relay_commit(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); + bool process_relay_down(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); + bool process_relay_up(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); + bool process_relay_accept(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); + bool process_relay_status(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); + bool process_relay_exit(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); + } +} + +#endif diff --git a/llarp/link_intro.cpp b/llarp/link_intro.cpp new file mode 100644 index 000000000..487febeb8 --- /dev/null +++ b/llarp/link_intro.cpp @@ -0,0 +1,12 @@ +#include "link_handlers.hpp" + +namespace llarp +{ + namespace frame + { + bool process_intro(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg) + { + return false; + } + } +} diff --git a/llarp/link_relay_down.cpp b/llarp/link_relay_down.cpp new file mode 100644 index 000000000..091973305 --- /dev/null +++ b/llarp/link_relay_down.cpp @@ -0,0 +1,12 @@ +#include "link_handlers.hpp" + +namespace llarp +{ + namespace frame + { + bool process_relay_down(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg) + { + return false; + } + } +} diff --git a/llarp/link_relay_up.cpp b/llarp/link_relay_up.cpp new file mode 100644 index 000000000..4190be3e8 --- /dev/null +++ b/llarp/link_relay_up.cpp @@ -0,0 +1,12 @@ +#include "link_handlers.hpp" + +namespace llarp +{ + namespace frame + { + bool process_relay_up(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg) + { + return false; + } + } +} diff --git a/llarp/muxer.cpp b/llarp/muxer.cpp new file mode 100644 index 000000000..719a965cd --- /dev/null +++ b/llarp/muxer.cpp @@ -0,0 +1,53 @@ +#include +#include "router.hpp" +#include "link_handlers.hpp" + +namespace llarp +{ + struct llarp_frame_handler introduce_handler = { + .paths = nullptr, + .parent = nullptr, + .process = &llarp::frame::process_intro + }; + + struct llarp_frame_handler lrdm_handler = { + .paths = nullptr, + .parent = nullptr, + .process = &llarp::frame::process_relay_down + }; + + struct llarp_frame_handler lrum_handler = { + .paths = nullptr, + .parent = nullptr, + .process = &llarp::frame::process_relay_up + }; + + static struct llarp_frame_handler * find_frame_handler(struct llarp_router * r, const char ch) + { + struct llarp_frame_handler * handler = nullptr; + switch(ch) + { + case 'i': + handler = &introduce_handler; + } + if(handler) + { + handler->paths = r->paths; + handler->parent = &r->muxer; + } + return handler; + } + + static struct llarp_msg_handler * find_msg_handler(struct llarp_router * r, const char ch) + { + return nullptr; + } +} + +extern "C" { + void llarp_msg_muxer_init(struct llarp_msg_muxer * muxer) + { + muxer->link_handler_for = &llarp::find_frame_handler; + muxer->routing_handler_for = &llarp::find_msg_handler; + } +} diff --git a/llarp/net.cpp b/llarp/net.cpp new file mode 100644 index 000000000..c6cd652d5 --- /dev/null +++ b/llarp/net.cpp @@ -0,0 +1,36 @@ +#include "net.hpp" +#include "str.hpp" + +#include +#include + +namespace llarp +{ + namespace net + { + bool GetIfAddr(const std::string & ifname, int af, sockaddr * addr) + { + ifaddrs * ifa = nullptr; + bool found = false; + socklen_t sl = sizeof(sockaddr_in6); + if(af == AF_INET) + sl = sizeof(sockaddr_in); + + if(getifaddrs(&ifa) == -1) + return false; + ifaddrs * i = ifa; + while(i) + { + if(llarp::StrEq(i->ifa_name, ifname.c_str()) && i->ifa_addr && i->ifa_addr->sa_family == af) + { + memcpy(addr, i->ifa_addr, sl); + found = true; + break; + } + i = i->ifa_next; + } + if(ifa) freeifaddrs(ifa); + return found; + } + } +} diff --git a/llarp/net.hpp b/llarp/net.hpp new file mode 100644 index 000000000..9bcd3acba --- /dev/null +++ b/llarp/net.hpp @@ -0,0 +1,14 @@ +#ifndef LLARP_NET_HPP +#define LLARP_NET_HPP +#include +#include + +namespace llarp +{ + namespace net + { + bool GetIfAddr(const std::string & ifname, int af, sockaddr * addr); + } +} + +#endif diff --git a/llarp/router.cpp b/llarp/router.cpp index e415ac6fa..fbd551970 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -1,41 +1,35 @@ #include #include +#include +#include "router.hpp" #include "link.hpp" -#include "mem.hpp" #include "str.hpp" namespace llarp { void router_iter_config(llarp_config_iterator *iter, const char *section, const char *key, const char *val); -struct router_links { - struct llarp_link *link = nullptr; - struct router_links *next = nullptr; -}; - } // namespace llarp -struct llarp_router { - struct llarp_threadpool *tp; - llarp::router_links links; - llarp_crypto crypto; - - static void *operator new(size_t sz) { - return llarp_g_mem.alloc(sz, llarp::alignment()); - } - - static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } - - void AddLink(struct llarp_link *link) { - llarp::router_links *head = &links; - while (head->next && head->link) head = head->next; +llarp_router::llarp_router() +{ + llarp_msg_muxer_init(&muxer); +} - if (head->link) - head->next = new llarp::router_links{link, nullptr}; - else - head->link = link; - } +llarp_router::~llarp_router() +{ +} + +void llarp_router::AddLink(struct llarp_link *link) { + llarp::router_links *head = &links; + while (head->next && head->link) head = head->next; + + if (head->link) + head->next = new llarp::router_links{link, nullptr}; + else + head->link = link; +} - void ForEachLink(std::function visitor) { +void llarp_router::ForEachLink(std::function visitor) { llarp::router_links *cur = &links; do { if (cur->link) visitor(cur->link); @@ -43,8 +37,7 @@ struct llarp_router { } while (cur); } - void Close() { ForEachLink(llarp_link_stop); } -}; + void llarp_router::Close() { ForEachLink(llarp_link_stop); } extern "C" { @@ -88,8 +81,8 @@ void router_iter_config(llarp_config_iterator *iter, const char *section, llarp_router *self = static_cast(iter->user); if (StrEq(section, "links")) { if (StrEq(val, "ip")) { - struct llarp_link *link = llarp_link_alloc(); - if (llarp_link_configure(link, key, AF_INET6)) + struct llarp_link *link = llarp_link_alloc(&self->muxer); + if (llarp_link_configure_addr(link, key, AF_INET6, 7000)) self->AddLink(link); else { llarp_link_free(&link); diff --git a/llarp/router.hpp b/llarp/router.hpp new file mode 100644 index 000000000..ba177e403 --- /dev/null +++ b/llarp/router.hpp @@ -0,0 +1,43 @@ +#ifndef LLARP_ROUTER_HPP +#define LLARP_ROUTER_HPP +#include +#include +#include + +#include "mem.hpp" + +namespace llarp +{ + struct router_links + { + llarp_link * link = nullptr; + router_links * next = nullptr; + }; + +} // namespace llarp + + struct llarp_router { + struct llarp_threadpool *tp; + llarp::router_links links; + llarp_crypto crypto; + llarp_msg_muxer muxer; + llarp_path_context * paths; + + static void *operator new(size_t sz) { + return llarp_g_mem.alloc(sz, llarp::alignment()); + } + + static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } + + llarp_router(); + ~llarp_router(); + + void AddLink(struct llarp_link *link); + + void ForEachLink(std::function visitor); + + void Close(); +}; + + +#endif diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 6a11ebd45..488e60b0c 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -3,12 +3,6 @@ #include "bencode.hpp" #include "exit_info.hpp" -extern "C" { -bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) { - return llarp::BEncode(*rc, buff); -} -} - namespace llarp { bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) { std::list addresses = ai_list_to_std(a.addrs); @@ -22,3 +16,11 @@ bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) { bencodeEnd(buff); } } // namespace llarp + + + +extern "C" { +bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) { + return llarp::BEncode(*rc, buff); +} +}