diff --git a/.gitignore b/.gitignore index ee6fc24fb..0cac5965e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ llarpd *.test *.bin -callgrind.* \ No newline at end of file +callgrind.* +.gdb_history diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 000000000..c406e22d8 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,17 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "make" + }, + { + "label": "rebuild", + "type": "shell", + "command": "make clean all" + } + ] +} \ No newline at end of file diff --git a/daemon/main.c b/daemon/main.c index 86dc1c486..08395d0ff 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -7,7 +7,9 @@ struct llarp_main { struct llarp_threadpool *worker; struct llarp_logic *logic; struct llarp_config *config; + struct llarp_nodedb *nodedb; struct llarp_ev_loop *mainloop; + char nodedb_dir[256]; }; void iter_main_config(struct llarp_config_iterator *itr, const char *section, @@ -22,6 +24,11 @@ void iter_main_config(struct llarp_config_iterator *itr, const char *section, } } } + if (!strcmp(section, "nodedb")) { + if (!strcmp(key, "dir")) { + strncpy(m->nodedb_dir, val, sizeof(m->nodedb_dir)); + } + } } static void progress() { @@ -76,17 +83,26 @@ int main(int argc, char *argv[]) { iter.visit = iter_main_config; llarp_config_iter(llarp.config, &iter); - if (!llarp.worker) llarp.worker = llarp_init_threadpool(2); - llarp.router = llarp_init_router(llarp.worker); + llarp.nodedb = llarp_nodedb_new(); - if (llarp_configure_router(llarp.router, llarp.config)) { - llarp.logic = llarp_init_logic(); - printf("starting router\n"); - llarp_run_router(llarp.router, llarp.logic); - printf("running mainloop\n"); - llarp_ev_loop_run(llarp.mainloop); - } else - printf("Failed to configure router\n"); + if (llarp.nodedb_dir[0]) { + llarp.nodedb_dir[sizeof(llarp.nodedb_dir) - 1] = 0; + char *dir = llarp.nodedb_dir; + if (llarp_nodedb_ensure_dir(dir)) { + if (!llarp.worker) llarp.worker = llarp_init_threadpool(2); + llarp.router = llarp_init_router(llarp.worker); + + if (llarp_configure_router(llarp.router, llarp.config)) { + llarp.logic = llarp_init_logic(); + printf("starting router\n"); + llarp_run_router(llarp.router, llarp.logic); + printf("running mainloop\n"); + llarp_ev_loop_run(llarp.mainloop); + } else + printf("Failed to configure router\n"); + } else + printf("failed to initialize nodedb at %s\n", dir); + } } else printf("Failed to load config %s\n", conffname); diff --git a/include/llarp.h b/include/llarp.h index bca5595a9..2da899d05 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -5,4 +5,5 @@ #include #include #include +#include #endif diff --git a/include/llarp/address_info.h b/include/llarp/address_info.h index 71224e5bd..f259cf400 100644 --- a/include/llarp/address_info.h +++ b/include/llarp/address_info.h @@ -20,12 +20,15 @@ struct llarp_ai { }; bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff); -bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t buff); +bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t *buff); struct llarp_ai_list; struct llarp_ai_list *llarp_ai_list_new(); -void llarp_ai_list_free(struct llarp_ai_list *l); +void llarp_ai_list_free(struct llarp_ai_list **l); + +bool llarp_ai_list_bencode(struct llarp_ai_list *l, llarp_buffer_t *buff); +bool llarp_ai_list_bdecode(struct llarp_ai_list *l, llarp_buffer_t *buff); struct llarp_ai llarp_ai_list_popfront(struct llarp_ai_list *l); void llarp_ai_list_pushback(struct llarp_ai_list *l, struct llarp_ai *ai); diff --git a/include/llarp/bencode.h b/include/llarp/bencode.h index 9a839085f..f9a57c1b1 100644 --- a/include/llarp/bencode.h +++ b/include/llarp/bencode.h @@ -10,45 +10,45 @@ extern "C" { #endif -bool INLINE bencode_write_bytestring(llarp_buffer_t* buff, const void* data, - size_t sz) { +static bool INLINE bencode_write_bytestring(llarp_buffer_t* buff, + const void* data, size_t sz) { if (!llarp_buffer_writef(buff, "%ld:", sz)) return false; return llarp_buffer_write(buff, data, sz); } -bool INLINE bencode_write_int(llarp_buffer_t* buff, int i) { +static bool INLINE bencode_write_int(llarp_buffer_t* buff, int i) { return llarp_buffer_writef(buff, "i%de", i); } -bool INLINE bencode_write_uint16(llarp_buffer_t* buff, uint16_t i) { +static bool INLINE bencode_write_uint16(llarp_buffer_t* buff, uint16_t i) { return llarp_buffer_writef(buff, "i%de", i); } -bool INLINE bencode_write_int64(llarp_buffer_t* buff, int64_t i) { +static bool INLINE bencode_write_int64(llarp_buffer_t* buff, int64_t i) { return llarp_buffer_writef(buff, "i%lde", i); } -bool INLINE bencode_write_uint64(llarp_buffer_t* buff, uint64_t i) { +static bool INLINE bencode_write_uint64(llarp_buffer_t* buff, uint64_t i) { return llarp_buffer_writef(buff, "i%lde", i); } -bool INLINE bencode_write_sizeint(llarp_buffer_t* buff, size_t i) { +static bool INLINE bencode_write_sizeint(llarp_buffer_t* buff, size_t i) { return llarp_buffer_writef(buff, "i%lde", i); } -bool INLINE bencode_start_list(llarp_buffer_t* buff) { +static bool INLINE bencode_start_list(llarp_buffer_t* buff) { return llarp_buffer_write(buff, "l", 1); } -bool INLINE bencode_start_dict(llarp_buffer_t* buff) { +static bool INLINE bencode_start_dict(llarp_buffer_t* buff) { return llarp_buffer_write(buff, "d", 1); } -bool INLINE bencode_end(llarp_buffer_t* buff) { +static bool INLINE bencode_end(llarp_buffer_t* buff) { return llarp_buffer_write(buff, "e", 1); } -bool INLINE bencode_write_version_entry(llarp_buffer_t* buff) { +static bool INLINE bencode_write_version_entry(llarp_buffer_t* buff) { return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION); } diff --git a/include/llarp/buffer.h b/include/llarp/buffer.h index 109b7f717..0240d4939 100644 --- a/include/llarp/buffer.h +++ b/include/llarp/buffer.h @@ -16,24 +16,9 @@ typedef struct llarp_buffer_t { char *cur; } llarp_buffer_t; -size_t INLINE llarp_buffer_size_left(llarp_buffer_t *buff) { - size_t diff = buff->cur - buff->base; - if (diff > buff->sz) - return 0; - else - return buff->sz - diff; -} +size_t llarp_buffer_size_left(llarp_buffer_t *buff); -bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data, - size_t sz) { - size_t left = llarp_buffer_size_left(buff); - if (left >= sz) { - memcpy(buff->cur, data, sz); - buff->cur += sz; - return true; - } - return false; -} +bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz); bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...); diff --git a/include/llarp/exit_info.h b/include/llarp/exit_info.h index 6d7a41b6e..bc507d53d 100644 --- a/include/llarp/exit_info.h +++ b/include/llarp/exit_info.h @@ -20,7 +20,10 @@ bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buf); struct llarp_xi_list; struct llarp_xi_list *llarp_xi_list_new(); -void llarp_xi_list_free(struct llarp_xi_list *l); +void llarp_xi_list_free(struct llarp_xi_list **l); + +bool llarp_xi_list_bdecode(struct llarp_xi_list *l, llarp_buffer_t *buf); +bool llarp_xi_list_bencode(struct llarp_xi_list *l, llarp_buffer_t *buf); struct llarp_xi_list_iter { void *user; diff --git a/llarp/address_info.c b/llarp/address_info.c index 18dc91bd9..3253f6b22 100644 --- a/llarp/address_info.c +++ b/llarp/address_info.c @@ -36,31 +36,55 @@ bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) { return bencode_end(buff); } -struct llarp_ai_list { - struct llarp_ai_list *next; +static bool llarp_ai_list_iter_bencode(struct llarp_ai_list_iter *iter, + struct llarp_ai *ai) { + return llarp_ai_bencode(ai, iter->user); +} + +bool llarp_ai_list_bencode(struct llarp_ai_list *l, llarp_buffer_t *buff) { + if (!bencode_start_list(buff)) return false; + struct llarp_ai_list_iter ai_itr = {.user = buff, + .visit = &llarp_ai_list_iter_bencode}; + llarp_ai_list_iterate(l, &ai_itr); + return bencode_end(buff); +} + +struct llarp_ai_list_node { struct llarp_ai data; + struct llarp_ai_list_node *next; +}; + +struct llarp_ai_list { + struct llarp_ai_list_node *root; }; struct llarp_ai_list *llarp_ai_list_new() { - return llarp_g_mem.alloc(sizeof(struct llarp_ai_list), 8); + struct llarp_ai_list *l = llarp_g_mem.alloc(sizeof(struct llarp_ai_list), 8); + if (l) { + l->root = NULL; + } + return l; } -void llarp_ai_list_free(struct llarp_ai_list *l) { - struct llarp_ai_list *cur = l; - struct llarp_ai_list *tmp; - do { - tmp = cur->next; - llarp_g_mem.free(cur); - cur = tmp; - } while (cur->next); +void llarp_ai_list_free(struct llarp_ai_list **l) { + if (*l) { + struct llarp_ai_list_node *cur = (*l)->root; + while (cur) { + struct llarp_ai_list_node *tmp = cur->next; + llarp_g_mem.free(cur); + cur = tmp; + } + llarp_g_mem.free(*l); + *l = NULL; + } } void llarp_ai_list_iterate(struct llarp_ai_list *l, struct llarp_ai_list_iter *itr) { - struct llarp_ai_list *cur = l; + struct llarp_ai_list_node *cur = l->root; itr->list = l; - do { + while (cur) { if (!itr->visit(itr, &cur->data)) return; cur = cur->next; - } while (cur->next); + }; } diff --git a/llarp/address_info.hpp b/llarp/address_info.hpp deleted file mode 100644 index aec492cdf..000000000 --- a/llarp/address_info.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef LLARP_AI_HPP -#define LLARP_AI_HPP -#include -#include -#include - -struct llarp_ai_list { - llarp_ai *data; - llarp_ai_list *next = nullptr; -}; - -static std::list ai_list_to_std(struct llarp_ai_list *l) { - std::list list; - if (l && l->data) { - do { - llarp_ai copy; - memcpy(©, l->data, sizeof(llarp_ai)); - list.push_back(copy); - l = l->next; - } while (l->next); - } - return list; -} - -#endif diff --git a/llarp/buffer.cpp b/llarp/buffer.cpp index 7f30314bc..90a2bce06 100644 --- a/llarp/buffer.cpp +++ b/llarp/buffer.cpp @@ -3,6 +3,15 @@ #include extern "C" { + +size_t llarp_buffer_size_left(llarp_buffer_t* buff) { + size_t diff = buff->cur - buff->base; + if (diff > buff->sz) + return 0; + else + return buff->sz - diff; +} + bool llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) { int written; size_t sz = llarp_buffer_size_left(buff); @@ -15,6 +24,16 @@ bool llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) { return true; } +bool llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz) { + size_t left = llarp_buffer_size_left(buff); + if (left >= sz) { + memcpy(buff->cur, data, sz); + buff->cur += sz; + return true; + } + return false; +} + bool llarp_buffer_readfile(llarp_buffer_t* buff, FILE* f, llarp_alloc* mem) { ssize_t len; fseek(f, 0, SEEK_END); diff --git a/llarp/exit_info.c b/llarp/exit_info.c index 4fe29e79a..113995adf 100644 --- a/llarp/exit_info.c +++ b/llarp/exit_info.c @@ -1,8 +1,63 @@ #include #include #include +#include #include +struct llarp_xi_list_node { + struct llarp_xi data; + struct llarp_xi_list_node *next; +}; + +struct llarp_xi_list { + struct llarp_xi_list_node *root; +}; + +struct llarp_xi_list *llarp_xi_list_new() { + struct llarp_xi_list *l = llarp_g_mem.alloc(sizeof(struct llarp_xi_list), 8); + if (l) { + l->root = NULL; + } + return l; +} + +void llarp_xi_list_free(struct llarp_xi_list **l) { + if (*l) { + struct llarp_xi_list_node *current = (*l)->root; + while (current) { + struct llarp_xi_list_node *tmp = current->next; + llarp_g_mem.free(current); + current = tmp; + } + llarp_g_mem.free(*l); + *l = NULL; + } +} + +static bool llarp_xi_iter_bencode(struct llarp_xi_list_iter *iter, + struct llarp_xi *xi) { + return llarp_xi_bencode(xi, iter->user); +} + +bool llarp_xi_list_bencode(struct llarp_xi_list *l, llarp_buffer_t *buff) { + if (!bencode_start_list(buff)) return false; + struct llarp_xi_list_iter xi_itr = {.user = buff, + .visit = &llarp_xi_iter_bencode}; + llarp_xi_list_iterate(l, &xi_itr); + return bencode_end(buff); +} + +void llarp_xi_list_iterate(struct llarp_xi_list *l, + struct llarp_xi_list_iter *iter) { + struct llarp_xi_list_node *current = l->root; + iter->list = l; + while (current) { + if (!iter->visit(iter, ¤t->data)) return; + + current = current->next; + } +} + bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) { char addr_buff[128] = {0}; const char *addr; diff --git a/llarp/exit_info.hpp b/llarp/exit_info.hpp deleted file mode 100644 index e81b6ea69..000000000 --- a/llarp/exit_info.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef LLARP_XI_HPP -#define LLARP_XI_HPP -#include -#include - -struct llarp_xi_list { - std::list list; -}; - -static std::list xi_list_to_std(struct llarp_xi_list *l) { - std::list list; - if (l->list.size()) - for (const auto &xi : l->list) list.push_back(xi); - return list; -} - -#endif diff --git a/llarp/router_contact.c b/llarp/router_contact.c new file mode 100644 index 000000000..e9fe9df37 --- /dev/null +++ b/llarp/router_contact.c @@ -0,0 +1,37 @@ +#include +#include +#include + +void llarp_rc_free(struct llarp_rc *rc) { + llarp_xi_list_free(&rc->exits); + llarp_ai_list_free(&rc->addrs); +} + +bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) { + /* write dict begin */ + if (!bencode_start_dict(buff)) return false; + if (rc->addrs) { + /* write ai if they exist */ + if (!bencode_write_bytestring(buff, "a", 1)) return false; + if (!llarp_ai_list_bencode(rc->addrs, buff)) return false; + } + /* write pubkey */ + if (!bencode_write_bytestring(buff, "k", 1)) return false; + if (!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t))) + return false; + + /* write version */ + if (!bencode_write_version_entry(buff)) return false; + + if (rc->exits) { + /* write ai if they exist */ + if (!bencode_write_bytestring(buff, "x", 1)) return false; + if (!llarp_xi_list_bencode(rc->exits, buff)) return false; + } + + /* write signature */ + if (!bencode_write_bytestring(buff, "z", 1)) return false; + if (!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t))) + return false; + return bencode_end(buff); +} diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp deleted file mode 100644 index 863910579..000000000 --- a/llarp/router_contact.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "router_contact.hpp" -#include -#include -#include "address_info.hpp" -#include "exit_info.hpp" - -namespace llarp { - -static bool bencode_rc_ai_write(struct llarp_ai_list_iter *i, - struct llarp_ai *ai) { - llarp_buffer_t *buff = static_cast(i->user); - return llarp_ai_bencode(ai, buff); -} - -static bool bencode_rc_xi_write(struct llarp_xi_list_iter *i, - struct llarp_xi *xi) { - llarp_buffer_t *buff = static_cast(i->user); - return llarp_xi_bencode(xi, buff); -} -} // namespace llarp - -extern "C" { - -void llarp_rc_free(struct llarp_rc *rc) { - if (rc->exits) 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) { - /* write dict begin */ - if (!bencode_start_dict(buff)) return false; - if (rc->addrs) { - /* write ai if they exist */ - if (!bencode_write_bytestring(buff, "a", 1)) return false; - if (!bencode_start_list(buff)) return false; - struct llarp_ai_list_iter ai_itr = { - .user = buff, .list = nullptr, .visit = &llarp::bencode_rc_ai_write}; - llarp_ai_list_iterate(rc->addrs, &ai_itr); - if (!bencode_end(buff)) return false; - } - /* write pubkey */ - if (!bencode_write_bytestring(buff, "k", 1)) return false; - if (!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t))) - return false; - - /* write version */ - if (!bencode_write_version_entry(buff)) return false; - - if (rc->exits) { - /* write ai if they exist */ - if (!bencode_write_bytestring(buff, "x", 1)) return false; - if (!bencode_start_list(buff)) return false; - struct llarp_xi_list_iter xi_itr = { - .user = buff, .list = nullptr, .visit = &llarp::bencode_rc_xi_write}; - llarp_xi_list_iterate(rc->exits, &xi_itr); - if (!bencode_end(buff)) return false; - } - - /* write signature */ - if (!bencode_write_bytestring(buff, "z", 1)) return false; - if (!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t))) - return false; - return bencode_end(buff); -} -} diff --git a/llarp/timer.cpp b/llarp/timer.cpp index 9c3847706..0231fde39 100644 --- a/llarp/timer.cpp +++ b/llarp/timer.cpp @@ -64,7 +64,7 @@ struct llarp_timer_context { uint32_t call_later(void* user, llarp_timer_handler_func func, uint64_t timeout_ms) { std::unique_lock lock(timersMutex); - uint32_t id = ids++; + uint32_t id = ++ids; timers[id] = llarp::timer(timeout_ms); return id; }