pull/1/head
Jeff Becker 6 years ago
parent 72d63a294b
commit 8181670bf3
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -4,7 +4,8 @@
struct llarp_main { struct llarp_main {
struct llarp_router *router; struct llarp_router *router;
struct llarp_threadpool *tp; struct llarp_threadpool *worker;
struct llarp_threadpool *logic;
struct llarp_config *config; struct llarp_config *config;
struct llarp_ev_loop *mainloop; struct llarp_ev_loop *mainloop;
}; };
@ -15,41 +16,57 @@ void iter_main_config(struct llarp_config_iterator *itr, const char *section,
if (!strcmp(section, "router")) { if (!strcmp(section, "router")) {
if (!strcmp(key, "threads")) { if (!strcmp(key, "threads")) {
int workers = atoi(val); int workers = atoi(val);
if (workers > 0 && m->tp == NULL) { if (workers > 0 && m->worker == NULL) {
printf("%s: %d worker threads\n", section, workers); printf("%s: %d worker threads\n", section, workers);
m->tp = llarp_init_threadpool(workers); m->worker = llarp_init_threadpool(workers);
} }
} }
} }
} }
int shutdown_llarp(struct llarp_main *m) { static void progress()
printf("Shutting down ."); {
llarp_stop_router(m->router);
printf("."); printf(".");
fflush(stdout); fflush(stdout);
}
int shutdown_llarp(struct llarp_main *m) {
printf("Shutting down ");
progress();
llarp_stop_router(m->router);
progress();
llarp_ev_loop_stop(m->mainloop); llarp_ev_loop_stop(m->mainloop);
printf("."); progress();
fflush(stdout); llarp_threadpool_stop(m->worker);
llarp_threadpool_join(m->tp); progress();
printf("."); llarp_threadpool_join(m->worker);
fflush(stdout); progress();
if(m->logic)
llarp_threadpool_wait(m->logic);
progress();
if(m->logic)
llarp_threadpool_join(m->logic);
progress();
llarp_free_router(&m->router); llarp_free_router(&m->router);
printf("."); progress();
fflush(stdout);
llarp_free_config(&m->config); llarp_free_config(&m->config);
printf("."); progress();
fflush(stdout);
llarp_ev_loop_free(&m->mainloop); llarp_ev_loop_free(&m->mainloop);
printf("."); progress();
llarp_free_threadpool(&m->worker);
progress();
if(m->logic)
llarp_free_threadpool(&m->logic);
progress();
printf("\n");
fflush(stdout); fflush(stdout);
llarp_free_threadpool(&m->tp);
printf(".\n");
return 0; return 0;
} }
struct llarp_main llarp;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct llarp_main llarp = {NULL, NULL, NULL, NULL}; memset(&llarp, 0, sizeof(struct llarp_main));
const char *conffname = "daemon.ini"; const char *conffname = "daemon.ini";
if (argc > 1) conffname = argv[1]; if (argc > 1) conffname = argv[1];
llarp_mem_stdlib(); llarp_mem_stdlib();
@ -63,12 +80,14 @@ int main(int argc, char *argv[]) {
iter.visit = iter_main_config; iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter); llarp_config_iter(llarp.config, &iter);
if (!llarp.tp) llarp.tp = llarp_init_threadpool(2); if (!llarp.worker) llarp.worker = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.tp); llarp.router = llarp_init_router(llarp.worker);
if (llarp_configure_router(llarp.router, llarp.config)) { if (llarp_configure_router(llarp.router, llarp.config)) {
printf("Running\n"); llarp.logic = llarp_init_threadpool(1);
llarp_run_router(llarp.router, llarp.mainloop); printf("starting router\n");
llarp_run_router(llarp.router, llarp.logic);
printf("running mainloop\n");
llarp_ev_loop_run(llarp.mainloop); llarp_ev_loop_run(llarp.mainloop);
} else } else
printf("Failed to configure router\n"); printf("Failed to configure router\n");

@ -3,9 +3,9 @@
#include <llarp/common.h> #include <llarp/common.h>
#include <llarp/mem.h> #include <llarp/mem.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -37,8 +37,9 @@ bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data,
bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...); bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
bool llarp_buffer_readfile(llarp_buffer_t * buff, FILE * f,struct llarp_alloc * mem); bool llarp_buffer_readfile(llarp_buffer_t *buff, FILE *f,
struct llarp_alloc *mem);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -45,10 +45,9 @@ struct llarp_cipher_result {
llarp_cipher_complete_hook hook; llarp_cipher_complete_hook hook;
}; };
struct llarp_async_cipher *llarp_async_cipher_new(llarp_sharedkey_t key, struct llarp_async_cipher *llarp_async_cipher_new(
struct llarp_crypto *crypto, llarp_sharedkey_t key, struct llarp_crypto *crypto,
struct llarp_threadpool *result, struct llarp_threadpool *result, struct llarp_threadpool *worker);
struct llarp_threadpool *worker);
void llarp_async_cipher_free(struct llarp_async_cipher **c); void llarp_async_cipher_free(struct llarp_async_cipher **c);

@ -23,16 +23,16 @@ struct llarp_udp_io {
struct sockaddr_in6 *addr; struct sockaddr_in6 *addr;
void *user; void *user;
void *impl; void *impl;
void (*recvfrom)(struct llarp_udp_io *, const struct sockaddr *, char *, void (*recvfrom)(struct llarp_udp_io *, const struct sockaddr *, void *,
ssize_t); ssize_t);
}; };
int llarp_ev_add_udp(struct llarp_ev_loop *ev,
struct llarp_udp_io *udp);
int llarp_ev_udp_sendto(struct llarp_udp_io * udp, const struct sockaddr * to, const void * data, size_t sz); int llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp);
int llarp_ev_close_udp(struct llarp_udp_io * udp); int llarp_ev_udp_sendto(struct llarp_udp_io *udp, const struct sockaddr *to,
const void *data, size_t sz);
int llarp_ev_close_udp(struct llarp_udp_io *udp);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -65,7 +65,7 @@ struct llarp_link {
int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener); int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener);
void (*deregister_listener)(struct llarp_link *, int); void (*deregister_listener)(struct llarp_link *, int);
bool (*configure)(struct llarp_link *, const char *, int, uint16_t); bool (*configure)(struct llarp_link *, const char *, int, uint16_t);
bool (*start_link)(struct llarp_link *, struct llarp_ev_loop *); bool (*start_link)(struct llarp_link *, struct llarp_threadpool *);
bool (*stop_link)(struct llarp_link *); bool (*stop_link)(struct llarp_link *);
bool (*put_ai)(struct llarp_link *, struct llarp_ai *); bool (*put_ai)(struct llarp_link *, struct llarp_ai *);
void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter); void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter);

@ -6,53 +6,52 @@
extern "C" { extern "C" {
#endif #endif
struct llarp_nodedb; struct llarp_nodedb;
/** create an empty nodedb */ /** create an empty nodedb */
struct llarp_nodedb * llarp_nodedb_new(); struct llarp_nodedb *llarp_nodedb_new();
/** free a nodedb and all loaded rc */ /** free a nodedb and all loaded rc */
void llarp_nodedb_free(struct llarp_nodedb ** n); void llarp_nodedb_free(struct llarp_nodedb **n);
/** ensure a nodedb fs skiplist structure is at dir /** ensure a nodedb fs skiplist structure is at dir
create if not there. create if not there.
*/ */
bool llarp_nodedb_ensure_dir(const char * dir); bool llarp_nodedb_ensure_dir(const char *dir);
/** load entire nodedb from fs skiplist at dir */ /** load entire nodedb from fs skiplist at dir */
ssize_t llarp_nodedb_load_dir(struct llarp_nodedb * n, const char * dir); ssize_t llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir);
/** store entire nodedb to fs skiplist at dir */ /** store entire nodedb to fs skiplist at dir */
ssize_t llarp_nodedb_store_dir(struct llarp_nodedb * n, const char * dir); ssize_t llarp_nodedb_store_dir(struct llarp_nodedb *n, const char *dir);
struct llarp_nodedb_iter struct llarp_nodedb_iter {
{ void *user;
void * user; struct llarp_rc *rc;
struct llarp_rc * rc; bool (*visit)(struct llarp_nodedb_iter *);
bool (*visit)(struct llarp_nodedb_iter *); };
};
/**
/** iterate over all loaded rc with an iterator
iterate over all loaded rc with an iterator */
*/ void llarp_nodedb_iterate_all(struct llarp_nodedb *n,
void llarp_nodedb_iterate_all(struct llarp_nodedb * n, struct llarp_nodedb_iter i); struct llarp_nodedb_iter i);
/** /**
find rc by rc.k being value k find rc by rc.k being value k
returns true if found otherwise returns false returns true if found otherwise returns false
*/ */
bool llarp_nodedb_find_rc(struct llarp_nodedb * n, struct llarp_rc * rc, llarp_pubkey_t k); bool llarp_nodedb_find_rc(struct llarp_nodedb *n, struct llarp_rc *rc,
llarp_pubkey_t k);
/**
put an rc into the node db /**
overwrites with new contents if already present put an rc into the node db
flushes the single entry to disk overwrites with new contents if already present
returns true on success and false on error flushes the single entry to disk
*/ returns true on success and false on error
bool llarp_nodedb_put_rc(struct llarp_nodedb * n, struct llarp_rc * rc); */
bool llarp_nodedb_put_rc(struct llarp_nodedb *n, struct llarp_rc *rc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -12,13 +12,13 @@ extern "C" {
struct llarp_router; struct llarp_router;
struct llarp_router *llarp_init_router(struct llarp_threadpool *tp); struct llarp_router *llarp_init_router(struct llarp_threadpool *worker);
void llarp_free_router(struct llarp_router **router); void llarp_free_router(struct llarp_router **router);
bool llarp_configure_router(struct llarp_router *router, bool llarp_configure_router(struct llarp_router *router,
struct llarp_config *conf); struct llarp_config *conf);
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop); void llarp_run_router(struct llarp_router *router, struct llarp_threadpool * logic);
void llarp_stop_router(struct llarp_router *router); void llarp_stop_router(struct llarp_router *router);
/** get router's inbound link level frame queue */ /** get router's inbound link level frame queue */

@ -20,7 +20,6 @@ void llarp_rc_free(struct llarp_rc *rc);
bool llarp_rc_verify_sig(struct llarp_rc *rc); bool llarp_rc_verify_sig(struct llarp_rc *rc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

@ -24,9 +24,9 @@ void llarp_threadpool_queue_job(struct llarp_threadpool *tp,
struct llarp_thread_job j); struct llarp_thread_job j);
void llarp_threadpool_start(struct llarp_threadpool *tp); void llarp_threadpool_start(struct llarp_threadpool *tp);
void llarp_threadpool_stop(struct llarp_threadpool * tp); void llarp_threadpool_stop(struct llarp_threadpool *tp);
void llarp_threadpool_join(struct llarp_threadpool *tp); void llarp_threadpool_join(struct llarp_threadpool *tp);
void llarp_threadpool_wait(struct llarp_threadpool *tp); void llarp_threadpool_wait(struct llarp_threadpool *tp);
#ifdef __cplusplus #ifdef __cplusplus

@ -15,21 +15,19 @@ bool llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) {
return true; return true;
} }
bool llarp_buffer_readfile(llarp_buffer_t * buff, FILE * f, llarp_alloc * mem) bool llarp_buffer_readfile(llarp_buffer_t* buff, FILE* f, llarp_alloc* mem) {
{ ssize_t len;
ssize_t len; fseek(f, 0, SEEK_END);
fseek(f, 0, SEEK_END); len = ftell(f);
len = ftell(f); rewind(f);
if (len > 0) {
buff->base = static_cast<char*>(mem->alloc(len, 8));
buff->cur = buff->base;
buff->sz = len;
size_t sz = fread(buff->base, len, 1, f);
rewind(f); rewind(f);
if(len > 0) return sz == len;
{
buff->base = static_cast<char *>(mem->alloc(len, 8));
buff->cur = buff->base;
buff->sz = len;
size_t sz = fread(buff->base, len, 1, f);
rewind(f);
return sz == len;
}
return false;
} }
return false;
}
} }

@ -1,12 +1,11 @@
#ifndef LLARP_CRYPTO_HPP #ifndef LLARP_CRYPTO_HPP
#define LLARP_CRYPTO_HPP #define LLARP_CRYPTO_HPP
#include <array>
#include <llarp/crypto.h> #include <llarp/crypto.h>
#include <array>
namespace llarp namespace llarp {
{ typedef std::array<uint8_t, sizeof(llarp_pubkey_t)> pubkey;
typedef std::array<uint8_t, sizeof(llarp_pubkey_t)> pubkey;
} }
#endif #endif

@ -15,23 +15,20 @@ struct llarp_dh_internal {
llarp_tunnel_nounce_t nounce; llarp_tunnel_nounce_t nounce;
struct llarp_dh_result result; struct llarp_dh_result result;
llarp_dh_func func; llarp_dh_func func;
struct llarp_async_dh * parent; struct llarp_async_dh *parent;
}; };
static void llarp_crypto_dh_result(void *call) { static void llarp_crypto_dh_result(void *call) {
struct llarp_dh_internal *impl = (struct llarp_dh_internal *)call; struct llarp_dh_internal *impl = (struct llarp_dh_internal *)call;
impl->result.hook(&impl->result); impl->result.hook(&impl->result);
llarp_g_mem.free(impl); llarp_g_mem.free(impl);
} }
static void llarp_crypto_dh_work(void *user) { static void llarp_crypto_dh_work(void *user) {
struct llarp_dh_internal *impl = (struct llarp_dh_internal *)user; struct llarp_dh_internal *impl = (struct llarp_dh_internal *)user;
impl->func(&impl->result.sharedkey, impl->theirkey, impl->nounce, impl->func(&impl->result.sharedkey, impl->theirkey, impl->nounce,
impl->parent->ourkey); impl->parent->ourkey);
struct llarp_thread_job job = { .user = impl, struct llarp_thread_job job = {.user = impl, .work = &llarp_crypto_dh_result};
.work = &llarp_crypto_dh_result };
llarp_threadpool_queue_job(impl->parent->result, job); llarp_threadpool_queue_job(impl->parent->result, job);
} }
@ -41,8 +38,7 @@ static void llarp_async_dh_exec(struct llarp_async_dh *dh, llarp_dh_func func,
llarp_dh_complete_hook result, void *user) { llarp_dh_complete_hook result, void *user) {
struct llarp_dh_internal *impl = struct llarp_dh_internal *impl =
llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32); llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32);
struct llarp_thread_job job = {.user = impl, struct llarp_thread_job job = {.user = impl, .work = &llarp_crypto_dh_work};
.work = &llarp_crypto_dh_work};
memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t)); memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t));
memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t)); memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t));
impl->parent = dh; impl->parent = dh;
@ -88,7 +84,7 @@ void llarp_async_dh_free(struct llarp_async_dh **dh) {
struct llarp_async_cipher_internal { struct llarp_async_cipher_internal {
llarp_nounce_t nounce; llarp_nounce_t nounce;
struct llarp_cipher_result result; struct llarp_cipher_result result;
struct llarp_async_cipher * parent; struct llarp_async_cipher *parent;
llarp_sym_cipher_func func; llarp_sym_cipher_func func;
}; };
@ -110,10 +106,8 @@ static void llarp_crypto_cipher_work(void *work) {
struct llarp_async_cipher_internal *impl = struct llarp_async_cipher_internal *impl =
(struct llarp_async_cipher_internal *)work; (struct llarp_async_cipher_internal *)work;
impl->func(impl->result.buff, impl->parent->key, impl->nounce); impl->func(impl->result.buff, impl->parent->key, impl->nounce);
struct llarp_thread_job job = { struct llarp_thread_job job = {.user = impl,
.user = impl, .work = &llarp_crypto_cipher_result};
.work = &llarp_crypto_cipher_result
};
llarp_threadpool_queue_job(impl->parent->result, job); llarp_threadpool_queue_job(impl->parent->result, job);
} }
@ -134,10 +128,9 @@ void llarp_async_cipher_queue_op(struct llarp_async_cipher *c,
llarp_threadpool_queue_job(c->worker, job); llarp_threadpool_queue_job(c->worker, job);
} }
struct llarp_async_cipher *llarp_async_cipher_new(llarp_sharedkey_t key, struct llarp_async_cipher *llarp_async_cipher_new(
struct llarp_crypto *crypto, llarp_sharedkey_t key, struct llarp_crypto *crypto,
struct llarp_threadpool *result, struct llarp_threadpool *result, struct llarp_threadpool *worker) {
struct llarp_threadpool *worker) {
struct llarp_async_cipher *cipher = struct llarp_async_cipher *cipher =
llarp_g_mem.alloc(sizeof(struct llarp_async_cipher), 16); llarp_g_mem.alloc(sizeof(struct llarp_async_cipher), 16);
cipher->func = crypto->xchacha20; cipher->func = crypto->xchacha20;

@ -23,9 +23,7 @@ void llarp_ev_loop_free(struct llarp_ev_loop **ev) {
*ev = nullptr; *ev = nullptr;
} }
int llarp_ev_loop_run(struct llarp_ev_loop *ev) { int llarp_ev_loop_run(struct llarp_ev_loop *ev) { return ev->run(); }
return ev->run();
}
int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev, int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev,
struct llarp_udp_listener *listener) { struct llarp_udp_listener *listener) {
@ -39,6 +37,4 @@ int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) {
} }
void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { loop->stop(); } void llarp_ev_loop_stop(struct llarp_ev_loop *loop) { loop->stop(); }
} }

@ -4,32 +4,25 @@
#include <unistd.h> #include <unistd.h>
namespace llarp namespace llarp {
{ struct ev_io {
struct ev_io char buff[2048];
{ int fd;
char buff[2048]; ev_io(int f) : fd(f){};
int fd; virtual int read(void * buf, size_t sz) = 0;
ev_io(int f) : fd(f) {}; virtual int sendto(const sockaddr* dst, const void* data, size_t sz) = 0;
virtual int read() = 0; virtual ~ev_io() { ::close(fd); };
virtual int sendto(const sockaddr * dst, const void * data, size_t sz) = 0;
virtual ~ev_io()
{
::close(fd);
};
};
}; };
}; // namespace llarp
struct llarp_ev_loop { struct llarp_ev_loop {
virtual bool init() = 0; virtual bool init() = 0;
virtual int run() = 0; virtual int run() = 0;
virtual void stop() = 0; virtual void stop() = 0;
virtual bool udp_listen(llarp_udp_io * l) = 0; virtual bool udp_listen(llarp_udp_io* l) = 0;
virtual ~llarp_ev_loop() {}; virtual ~llarp_ev_loop(){};
}; };
#endif #endif

@ -1,38 +1,30 @@
#ifndef EV_EPOLL_HPP #ifndef EV_EPOLL_HPP
#define EV_EPOLL_HPP #define EV_EPOLL_HPP
#include "ev.hpp"
#include <unistd.h>
#include <sys/epoll.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/epoll.h>
#include <unistd.h>
#include "ev.hpp"
namespace llarp {
struct udp_listener : public ev_io {
llarp_udp_io* udp;
namespace llarp udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
{
struct udp_listener : public ev_io
{
llarp_udp_io * udp;
udp_listener(int fd, llarp_udp_io * u) :
ev_io(fd),
udp(u) {};
~udp_listener() {} ~udp_listener() {}
virtual int read()
{
sockaddr src;
socklen_t slen;
int ret = ::recvfrom(fd, buff, sizeof(buff), 0, &src, &slen);
if (ret == -1) return -1;
udp->recvfrom(udp, &src, buff, ret);
return 0;
}
virtual int sendto(const sockaddr * to, const void * data, size_t sz) virtual int read(void * buf, size_t sz) {
{ sockaddr src;
socklen_t slen; socklen_t slen;
switch(to->sa_family) int ret = ::recvfrom(fd, buf, sz, 0, &src, &slen);
{ if (ret == -1) return -1;
udp->recvfrom(udp, &src, buf, ret);
return 0;
}
virtual int sendto(const sockaddr* to, const void* data, size_t sz) {
socklen_t slen;
switch (to->sa_family) {
case AF_INET: case AF_INET:
slen = sizeof(struct sockaddr_in); slen = sizeof(struct sockaddr_in);
break; break;
@ -41,106 +33,84 @@ namespace llarp
break; break;
default: default:
return -1; return -1;
}
return ::sendto(fd, data, sz, 0, to, slen);
} }
}; return ::sendto(fd, data, sz, 0, to, slen);
}
}; };
}; // namespace llarp
struct llarp_epoll_loop : public llarp_ev_loop struct llarp_epoll_loop : public llarp_ev_loop {
{
int epollfd; int epollfd;
llarp_epoll_loop() : epollfd(-1)
{
}
~llarp_epoll_loop() llarp_epoll_loop() : epollfd(-1) {}
{
} ~llarp_epoll_loop() {}
bool init() bool init() {
{ if (epollfd == -1) epollfd = epoll_create1(EPOLL_CLOEXEC);
if(epollfd == -1)
epollfd = epoll_create1(EPOLL_CLOEXEC);
return epollfd != -1; return epollfd != -1;
} }
int run() int run() {
{
epoll_event events[1024]; epoll_event events[1024];
int result; int result;
do char readbuf[2048];
{ do {
result = epoll_wait(epollfd, events, 1024, 0); result = epoll_wait(epollfd, events, 1024, 0);
if(result > 0) if (result > 0) {
{
int idx = 0; int idx = 0;
while(idx < result) while (idx < result) {
{ llarp::ev_io* ev = static_cast<llarp::ev_io*>(events[idx].data.ptr);
llarp::ev_io * ev = static_cast<llarp::ev_io*>(events[idx].data.ptr); if (events[idx].events & EPOLLIN) {
if(events[idx].events & EPOLLIN) if (ev->read(readbuf, sizeof(readbuf)) == -1) {
{
if ( ev->read() == -1)
{
epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, nullptr); epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, nullptr);
} }
} }
++idx; ++idx;
} }
} }
} } while (result != -1);
while(result != -1);
return result; return result;
} }
int udp_bind(const sockaddr * addr) int udp_bind(const sockaddr* addr) {
{
socklen_t slen; socklen_t slen;
switch(addr->sa_family) switch (addr->sa_family) {
{ case AF_INET:
case AF_INET: slen = sizeof(struct sockaddr_in);
slen = sizeof(struct sockaddr_in); break;
break; case AF_INET6:
case AF_INET6: slen = sizeof(struct sockaddr_in6);
slen = sizeof(struct sockaddr_in6); break;
break; default:
default: return -1;
return -1;
} }
int fd = socket(addr->sa_family, SOCK_DGRAM, 0); int fd = socket(addr->sa_family, SOCK_DGRAM, 0);
if (fd == -1) return -1; if (fd == -1) return -1;
if(bind(fd, addr, slen) == -1) if (bind(fd, addr, slen) == -1) {
{
close(fd); close(fd);
return -1; return -1;
} }
return fd; return fd;
} }
bool udp_listen(llarp_udp_io * l) bool udp_listen(llarp_udp_io* l) {
{
int fd = udp_bind((sockaddr*)l->addr); int fd = udp_bind((sockaddr*)l->addr);
if (fd == -1) return false; if (fd == -1) return false;
llarp::udp_listener * listener = new llarp::udp_listener(fd, l); llarp::udp_listener* listener = new llarp::udp_listener(fd, l);
epoll_event ev; epoll_event ev;
ev.data.ptr = listener; ev.data.ptr = listener;
ev.events = EPOLLIN; ev.events = EPOLLIN;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
{
delete listener; delete listener;
return false; return false;
} }
return true; return true;
} }
void stop() void stop() {
{ if (epollfd != -1) ::close(epollfd);
if(epollfd != -1)
::close(epollfd);
epollfd = -1; epollfd = -1;
} }
}; };

@ -26,7 +26,7 @@ struct link_impl {
const char *name() { return linkname.c_str(); } const char *name() { return linkname.c_str(); }
bool start(llarp_ev_loop *loop) { bool start(llarp_threadpool *logic) {
// todo: implement // todo: implement
return false; return false;
} }
@ -43,9 +43,9 @@ static bool configure(struct llarp_link *l, const char *ifname, int af,
return link->configure(ifname, af, port); return link->configure(ifname, af, port);
} }
static bool start_link(struct llarp_link *l, struct llarp_ev_loop *loop) { static bool start_link(struct llarp_link *l, struct llarp_threadpool *logic) {
link_impl *link = static_cast<link_impl *>(l->impl); link_impl *link = static_cast<link_impl *>(l->impl);
return link->start(loop); return link->start(logic);
} }
static bool stop_link(struct llarp_link *l) { static bool stop_link(struct llarp_link *l) {

@ -1,53 +1,43 @@
#include <llarp/nodedb.h> #include <llarp/nodedb.h>
#include <llarp/router_contact.h> #include <llarp/router_contact.h>
#include <map>
#include "crypto.hpp" #include "crypto.hpp"
#include "fs.hpp" #include "fs.hpp"
#include <map>
static const char skiplist_subdirs[] = "0123456789ABCDEF"; static const char skiplist_subdirs[] = "0123456789ABCDEF";
struct llarp_nodedb struct llarp_nodedb {
{
std::map<llarp::pubkey, llarp_rc *> entries; std::map<llarp::pubkey, llarp_rc *> entries;
ssize_t Load(const fs::path & path) ssize_t Load(const fs::path &path) {
{
std::error_code ec; std::error_code ec;
if(!fs::exists(path, ec)) if (!fs::exists(path, ec)) {
{
return -1; return -1;
} }
ssize_t loaded = 0; ssize_t loaded = 0;
for (const char & ch : skiplist_subdirs) for (const char &ch : skiplist_subdirs) {
{
fs::path sub = path / std::string(ch, 1); fs::path sub = path / std::string(ch, 1);
for(auto & f : fs::directory_iterator(sub)) for (auto &f : fs::directory_iterator(sub)) {
{
ssize_t l = loadSubdir(f); ssize_t l = loadSubdir(f);
if(l > 0) loaded += l; if (l > 0) loaded += l;
} }
} }
return loaded; return loaded;
} }
bool loadfile(const fs::path & fpath) bool loadfile(const fs::path &fpath) {
{ llarp_rc *rc = new llarp_rc;
llarp_rc * rc = new llarp_rc;
llarp_buffer_t buff; llarp_buffer_t buff;
FILE * f = fopen(fpath.c_str(), "rb"); FILE *f = fopen(fpath.c_str(), "rb");
if(!f) return false; if (!f) return false;
if(!llarp_buffer_readfile(&buff, f, &llarp_g_mem)) if (!llarp_buffer_readfile(&buff, f, &llarp_g_mem)) {
{
fclose(f); fclose(f);
return false; return false;
} }
fclose(f); fclose(f);
if(llarp_rc_bdecode(rc, &buff)) if (llarp_rc_bdecode(rc, &buff)) {
{ if (llarp_rc_verify_sig(rc)) {
if(llarp_rc_verify_sig(rc))
{
llarp::pubkey pk; llarp::pubkey pk;
memcpy(pk.data(), rc->pubkey, pk.size()); memcpy(pk.data(), rc->pubkey, pk.size());
entries[pk] = rc; entries[pk] = rc;
@ -58,58 +48,45 @@ struct llarp_nodedb
delete rc; delete rc;
return false; return false;
} }
ssize_t loadSubdir(const fs::path & dir) ssize_t loadSubdir(const fs::path &dir) {
{
ssize_t sz = 0; ssize_t sz = 0;
for (auto & path : fs::directory_iterator(dir)) for (auto &path : fs::directory_iterator(dir)) {
{ if (loadfile(path)) sz++;
if(loadfile(path)) sz++;
} }
return sz; return sz;
} }
}; };
extern "C" { extern "C" {
struct llarp_nodedb * llarp_nodedb_new() struct llarp_nodedb *llarp_nodedb_new() {
{ return new llarp_nodedb;
return new llarp_nodedb; }
}
void llarp_nodedb_free(struct llarp_nodedb ** n) void llarp_nodedb_free(struct llarp_nodedb **n) {
{ if (*n) delete *n;
if(*n) *n = nullptr;
delete *n; }
*n = nullptr;
}
bool llarp_nodedb_ensure_dir(const char *dir) {
fs::path path(dir);
std::error_code ec;
if (!fs::exists(dir, ec)) fs::create_directories(path, ec);
bool llarp_nodedb_ensure_dir(const char * dir) if (ec) return false;
{
fs::path path(dir);
std::error_code ec;
if(!fs::exists(dir, ec))
fs::create_directories(path, ec);
if(ec)
return false;
if (!fs::is_directory(path))
return false;
for (const char & ch : skiplist_subdirs) if (!fs::is_directory(path)) return false;
{
fs::path sub = path / std::string(ch, 1); for (const char &ch : skiplist_subdirs) {
fs::create_directory(sub, ec); fs::path sub = path / std::string(ch, 1);
if(ec) return false; fs::create_directory(sub, ec);
} if (ec) return false;
return true;
}
ssize_t llarp_nodedb_load_dir(struct llarp_nodedb * n, const char * dir)
{
return n->Load(dir);
} }
return true;
}
ssize_t llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir) {
return n->Load(dir);
}
} }

@ -58,9 +58,9 @@ bool llarp_configure_router(struct llarp_router *router,
return router->Ready(); return router->Ready();
} }
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) { void llarp_run_router(struct llarp_router *router, struct llarp_threadpool * logic) {
router->ForEachLink([loop](llarp_link *link) { router->ForEachLink([logic](llarp_link *link) {
int result = link->start_link(link, loop); int result = link->start_link(link, logic);
if (result == -1) printf("link %s failed to start\n", link->name(link)); if (result == -1) printf("link %s failed to start\n", link->name(link));
}); });
} }

@ -21,14 +21,11 @@ static bool bencode_rc_xi_write(struct llarp_xi_list_iter *i,
extern "C" { extern "C" {
void llarp_rc_free(struct llarp_rc *rc) void llarp_rc_free(struct llarp_rc *rc) {
{ if (rc->exits) llarp_xi_list_free(rc->exits);
if(rc->exits) if (rc->addrs) llarp_ai_list_free(rc->addrs);
llarp_xi_list_free(rc->exits); }
if(rc->addrs)
llarp_ai_list_free(rc->addrs);
}
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) { bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
/* write dict begin */ /* write dict begin */
if (!bencode_start_dict(buff)) return false; if (!bencode_start_dict(buff)) return false;

@ -23,8 +23,7 @@ Pool::Pool(size_t workers) {
} }
} }
void Pool::Stop() void Pool::Stop() {
{
{ {
lock_t lock(queue_mutex); lock_t lock(queue_mutex);
stop = true; stop = true;
@ -72,13 +71,9 @@ void llarp_threadpool_join(struct llarp_threadpool *pool) { pool->impl.Join(); }
void llarp_threadpool_start(struct llarp_threadpool *pool) { /** no op */ void llarp_threadpool_start(struct llarp_threadpool *pool) { /** no op */
} }
void llarp_threadpool_stop(struct llarp_threadpool *pool) void llarp_threadpool_stop(struct llarp_threadpool *pool) { pool->impl.Stop(); }
{
pool->impl.Stop();
}
void llarp_threadpool_wait(struct llarp_threadpool *pool) void llarp_threadpool_wait(struct llarp_threadpool *pool) {
{
std::mutex mtx; std::mutex mtx;
{ {
std::unique_lock<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);

@ -7,7 +7,7 @@ struct bench_main {
size_t completed; size_t completed;
size_t num; size_t num;
size_t jobs; size_t jobs;
struct llarp_threadpool * pool; struct llarp_threadpool *pool;
struct llarp_async_cipher *cipher; struct llarp_async_cipher *cipher;
struct llarp_crypto crypto; struct llarp_crypto crypto;
}; };
@ -42,8 +42,8 @@ int main(int argc, char *argv[]) {
llarp_threadpool_start(b_main.pool); llarp_threadpool_start(b_main.pool);
tp = llarp_init_threadpool(8); tp = llarp_init_threadpool(8);
b_main.num = 500000; b_main.num = 5000000;
b_main.jobs = 10; b_main.jobs = 5000;
b_main.completed = 0; b_main.completed = 0;
llarp_sharedkey_t key; llarp_sharedkey_t key;
b_main.crypto.randbytes(key, sizeof(llarp_sharedkey_t)); b_main.crypto.randbytes(key, sizeof(llarp_sharedkey_t));
@ -69,7 +69,7 @@ int main(int argc, char *argv[]) {
llarp_async_cipher_queue_op(b_main.cipher, msg, nounce, llarp_async_cipher_queue_op(b_main.cipher, msg, nounce,
handle_cipher_complete, &b_main); handle_cipher_complete, &b_main);
} }
printf("started %ld jobs\n", b_main.jobs);
llarp_threadpool_wait(b_main.pool); llarp_threadpool_wait(b_main.pool);
llarp_threadpool_join(b_main.pool); llarp_threadpool_join(b_main.pool);
llarp_threadpool_stop(tp); llarp_threadpool_stop(tp);

@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
tp = llarp_init_threadpool(8); tp = llarp_init_threadpool(8);
dh_main.result = llarp_init_threadpool(1); dh_main.result = llarp_init_threadpool(1);
llarp_threadpool_start(dh_main.result); llarp_threadpool_start(dh_main.result);
dh_main.num = 500000; dh_main.num = 500000;
dh_main.completed = 0; dh_main.completed = 0;
llarp_seckey_t ourkey; llarp_seckey_t ourkey;
@ -65,10 +65,10 @@ int main(int argc, char *argv[]) {
llarp_threadpool_join(dh_main.result); llarp_threadpool_join(dh_main.result);
llarp_threadpool_stop(tp); llarp_threadpool_stop(tp);
llarp_threadpool_join(tp); llarp_threadpool_join(tp);
llarp_free_threadpool(&tp); llarp_free_threadpool(&tp);
llarp_free_threadpool(&dh_main.result); llarp_free_threadpool(&dh_main.result);
llarp_async_dh_free(&dh_main.dh); llarp_async_dh_free(&dh_main.dh);
printf("did %ld of %ld work\n", dh_main.completed, dh_main.num); printf("did %ld of %ld work\n", dh_main.completed, dh_main.num);
return 0; return 0;

Loading…
Cancel
Save