pull/1/head
Jeff Becker 6 years ago
parent 4cc5059cbe
commit 76d885d39e
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

3
.gitignore vendored

@ -11,4 +11,5 @@ llarpd
*.test
*.bin
callgrind.*
callgrind.*
.gdb_history

17
.vscode/tasks.json vendored

@ -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"
}
]
}

@ -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);

@ -5,4 +5,5 @@
#include <llarp/mem.h>
#include <llarp/ev.h>
#include <llarp/logic.h>
#include <llarp/nodedb.h>
#endif

@ -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);

@ -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);
}

@ -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, ...);

@ -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;

@ -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);
};
}

@ -1,25 +0,0 @@
#ifndef LLARP_AI_HPP
#define LLARP_AI_HPP
#include <llarp/address_info.h>
#include <cstring>
#include <list>
struct llarp_ai_list {
llarp_ai *data;
llarp_ai_list *next = nullptr;
};
static std::list<llarp_ai> ai_list_to_std(struct llarp_ai_list *l) {
std::list<llarp_ai> list;
if (l && l->data) {
do {
llarp_ai copy;
memcpy(&copy, l->data, sizeof(llarp_ai));
list.push_back(copy);
l = l->next;
} while (l->next);
}
return list;
}
#endif

@ -3,6 +3,15 @@
#include <stdio.h>
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);

@ -1,8 +1,63 @@
#include <arpa/inet.h>
#include <llarp/bencode.h>
#include <llarp/exit_info.h>
#include <llarp/mem.h>
#include <llarp/string.h>
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, &current->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;

@ -1,17 +0,0 @@
#ifndef LLARP_XI_HPP
#define LLARP_XI_HPP
#include <llarp/exit_info.h>
#include <list>
struct llarp_xi_list {
std::list<llarp_xi> list;
};
static std::list<llarp_xi> xi_list_to_std(struct llarp_xi_list *l) {
std::list<llarp_xi> list;
if (l->list.size())
for (const auto &xi : l->list) list.push_back(xi);
return list;
}
#endif

@ -0,0 +1,37 @@
#include <llarp/bencode.h>
#include <llarp/router_contact.h>
#include <llarp/version.h>
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);
}

@ -1,65 +0,0 @@
#include "router_contact.hpp"
#include <llarp/bencode.h>
#include <llarp/version.h>
#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<llarp_buffer_t *>(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<llarp_buffer_t *>(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);
}
}

@ -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<std::mutex> lock(timersMutex);
uint32_t id = ids++;
uint32_t id = ++ids;
timers[id] = llarp::timer(timeout_ms);
return id;
}

Loading…
Cancel
Save