Merge remote-tracking branch 'ryan/master'

pull/1/head
Jeff Becker 6 years ago
commit 71c34231b2
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -7,12 +7,11 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -fPIC ${DEBUG_FLAGS}")
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
find_package(sodium)
set(EXE llarpd)
set(EXE_SRC daemon/main.cpp)
set(LIBS ${sodium_LIBRARY_RELEASE} pthread stdc++fs)
set(LIBS sodium pthread stdc++fs)
set(LIB llarp)
@ -51,3 +50,6 @@ include_directories(${sodium_INCLUDE_DIR})
add_library(${LIB} STATIC ${LIB_SRC})
add_executable(${EXE} ${EXE_SRC})
target_link_libraries(${EXE} ${LIB} ${LIBS})
add_executable(rcutil daemon/rcutil.cpp)
target_link_libraries(rcutil ${LIB} ${LIBS})

@ -0,0 +1,269 @@
#include <llarp.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
static void
progress()
{
printf(".");
fflush(stdout);
}
struct llarp_main
{
struct llarp_alloc mem;
struct llarp_crypto crypto;
struct llarp_router *router = nullptr;
struct llarp_threadpool *worker = nullptr;
struct llarp_threadpool *thread = nullptr;
struct llarp_logic *logic = nullptr;
struct llarp_config *config = nullptr;
struct llarp_nodedb *nodedb = nullptr;
struct llarp_ev_loop *mainloop = nullptr;
char nodedb_dir[256];
int exitcode;
int
shutdown()
{
printf("Shutting down ");
progress();
if(mainloop)
llarp_ev_loop_stop(mainloop);
progress();
if(worker)
llarp_threadpool_stop(worker);
progress();
if(worker)
llarp_threadpool_join(worker);
progress();
if(logic)
llarp_logic_stop(logic);
progress();
if(router)
llarp_stop_router(router);
progress();
llarp_free_router(&router);
progress();
llarp_free_config(&config);
progress();
llarp_ev_loop_free(&mainloop);
progress();
llarp_free_threadpool(&worker);
progress();
llarp_free_logic(&logic);
progress();
printf("\n");
fflush(stdout);
return exitcode;
}
};
void
iter_main_config(struct llarp_config_iterator *itr, const char *section,
const char *key, const char *val)
{
llarp_main *m = static_cast< llarp_main * >(itr->user);
if(!strcmp(section, "router"))
{
if(!strcmp(key, "threads"))
{
int workers = atoi(val);
if(workers > 0 && m->worker == nullptr)
{
m->worker = llarp_init_threadpool(workers, "llarp-worker");
}
}
}
if(!strcmp(section, "netdb"))
{
if(!strcmp(key, "dir"))
{
strncpy(m->nodedb_dir, val, sizeof(m->nodedb_dir));
}
}
}
llarp_main *sllarp = nullptr;
void
run_net(void *user)
{
llarp_ev_loop_run(static_cast< llarp_ev_loop * >(user));
}
void
handle_signal(int sig)
{
printf("\ninterrupted\n");
llarp_ev_loop_stop(sllarp->mainloop);
llarp_logic_stop(sllarp->logic);
}
#include <getopt.h>
#include <llarp/router_contact.h>
#include <llarp/time.h>
#include <fstream>
int
main(int argc, char *argv[])
{
// --generate-blank /path/to/file.signed
// --update-ifs /path/to/file.signed
// --key /path/to/long_term_identity.key
// --generate /path/to/file.signed
// --update /path/to/file.signed
// printf("has [%d]options\n", argc);
if(argc < 3)
{
printf(
"please specify --generate or --update with a path to a router contact "
"file\n");
return 0;
}
bool genMode;
bool updMode;
int c;
char *rcfname;
while(1)
{
static struct option long_options[] = {
{"generate", required_argument, 0, 'g'},
{"update", required_argument, 0, 'u'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "gu", long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 0:
break;
case 'g':
// printf ("option -g with value `%s'\n", optarg);
rcfname = optarg;
genMode = true;
break;
case 'u':
// printf ("option -u with value `%s'\n", optarg);
rcfname = optarg;
updMode = true;
break;
default:
abort();
}
}
printf("parsed options\n");
if(!genMode && !updMode)
{
printf("I don't know what to do, no generate or update parameter\n");
return 1;
}
sllarp = new llarp_main;
llarp_mem_stdlib(&sllarp->mem);
auto mem = &sllarp->mem;
// llarp_new_config(&sllarp->config);
// llarp_ev_loop_alloc(&sllarp->mainloop);
llarp_crypto_libsodium_init(&sllarp->crypto);
llarp_rc tmp;
if(genMode)
{
printf("Creating [%s]\n", rcfname);
// Jeff wanted tmp to be stack created
// do we still need to zero it out?
llarp_rc_clear(&tmp);
// if we zero it out then
// allocate fresh pointers that the bencoder can expect to be ready
tmp.addrs = llarp_ai_list_new(mem);
tmp.exits = llarp_xi_list_new(mem);
// set updated timestamp
tmp.last_updated = llarp_time_now_ms();
// load longterm identity
llarp_crypto crypt;
fs::path ident_keyfile = "identity.key";
llarp_seckey_t identity;
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), &identity);
// get identity public key
uint8_t *pubkey = llarp_seckey_topublic(identity);
llarp_rc_set_pubkey(&tmp, pubkey);
// this causes a segfault
// llarp_rc_sign(&crypt, &identity, &tmp);
// set filename
fs::path our_rc_file = rcfname;
// write file
llarp_rc_write(&tmp, our_rc_file.c_str());
// release memory for tmp lists
llarp_rc_free(&tmp);
}
if(updMode)
{
printf("Loading [%s]\n", rcfname);
fs::path our_rc_file = rcfname;
std::error_code ec;
if(!fs::exists(our_rc_file, ec))
{
printf("File not found\n");
return 0;
}
std::ifstream f(our_rc_file, std::ios::binary);
if(!f.is_open())
{
printf("Can't open file\n");
return 0;
}
byte_t tmpc[MAX_RC_SIZE];
llarp_buffer_t buf;
buf.base = tmpc;
buf.cur = buf.base;
buf.sz = sizeof(tmpc);
f.read((char *)tmpc, sizeof(MAX_RC_SIZE));
printf("contents[%s]\n", tmpc);
if(!llarp_rc_bdecode(mem, &tmp, &buf))
{
printf("Can't decode\n");
return 0;
}
// set updated timestamp
tmp.last_updated = llarp_time_now_ms();
// load longterm identity
llarp_crypto crypt;
fs::path ident_keyfile = "identity.key";
llarp_seckey_t identity;
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), &identity);
// get identity public key
uint8_t *pubkey = llarp_seckey_topublic(identity);
llarp_rc_set_pubkey(&tmp, pubkey);
// set filename
fs::path our_rc_file_out = "update_debug.rc";
// write file
llarp_rc_write(&tmp, our_rc_file_out.c_str());
// release memory for tmp lists
llarp_rc_free(&tmp);
}
delete sllarp;
return 1;
}

@ -73,7 +73,7 @@ bencode_write_version_entry(llarp_buffer_t* buff)
}
static bool INLINE
bdecode_read_integer(struct llarp_buffer_t* buffer, int64_t* result)
bdecode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result)
{
size_t len;
if(*buffer->cur != 'i')

@ -15,25 +15,45 @@ extern "C" {
struct llarp_router;
bool
llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *path,
llarp_seckey_t *identity);
bool
llarp_rc_write(struct llarp_rc *rc, const char *our_rc_file);
struct llarp_router *
llarp_init_router(struct llarp_alloc *mem, struct llarp_threadpool *worker,
struct llarp_ev_loop *netloop, struct llarp_logic *logic);
void
llarp_free_router(struct llarp_router **router);
void
llarp_rc_clear(struct llarp_rc *rc);
bool
llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai);
bool
llarp_router_try_connect(struct llarp_router *router, struct llarp_rc *remtoe);
bool
llarp_configure_router(struct llarp_router *router, struct llarp_config *conf);
void
llarp_rc_set_addrs(struct llarp_rc *rc, struct llarp_alloc *mem,
struct llarp_ai_list *addr);
void
llarp_rc_set_pubkey(struct llarp_rc *rc, uint8_t *pubkey);
void
llarp_rc_sign(llarp_crypto *crypto, llarp_seckey_t *identity,
struct llarp_rc *rc);
void
llarp_run_router(struct llarp_router *router);
void
llarp_stop_router(struct llarp_router *router);
/** return false if we already have a session pending or made */
bool
llarp_router_try_connect(struct llarp_router *router, struct llarp_rc *remote);
/** get router's inbound link level frame queue */
struct llarp_link_queue *
llarp_router_link_queue(struct llarp_router *router);

@ -15,6 +15,7 @@ struct llarp_rc
llarp_pubkey_t pubkey;
struct llarp_xi_list *exits;
llarp_sig_t signature;
uint64_t last_updated;
};
bool

@ -18,7 +18,7 @@ struct llarp_ai_list
static bool
llarp_ai_decode_key(struct dict_reader *r, llarp_buffer_t *key)
{
int64_t i;
uint64_t i;
char tmp[128] = {0};
llarp_buffer_t strbuf;

@ -123,7 +123,7 @@ llarp_xi_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
struct llarp_xi *xi = r->user;
llarp_buffer_t strbuf;
int64_t v;
uint64_t v;
char tmp[128] = {0};
// address

@ -77,6 +77,12 @@ llarp_router::try_connect(fs::path rcfile)
llarp_rc_free(&remote);
}
bool
llarp_router::EnsureIdentity()
{
return llarp_findOrCreateIdentity(&crypto, ident_keyfile.c_str(), &identity);
}
void
llarp_router::AddLink(struct llarp_link *link)
{
@ -90,28 +96,6 @@ llarp_router::Ready()
return ready;
}
bool
llarp_router::EnsureIdentity()
{
std::error_code ec;
if(!fs::exists(ident_keyfile, ec))
{
crypto.keygen(identity);
std::ofstream f(ident_keyfile, std::ios::binary);
if(f.is_open())
{
f.write((char *)identity, sizeof(identity));
}
}
std::ifstream f(ident_keyfile, std::ios::binary);
if(f.is_open())
{
f.read((char *)identity, sizeof(identity));
return true;
}
return false;
}
bool
llarp_router::SaveRC()
{
@ -124,8 +108,7 @@ llarp_router::SaveRC()
printf(" OK.\n");
byte_t tmp[MAX_RC_SIZE];
llarp_buffer_t buf;
llarp::StackBuffer< decltype(tmp) >(buf, tmp);
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(llarp_rc_bencode(&rc, &buf))
{
@ -293,6 +276,101 @@ llarp_router_try_connect(struct llarp_router *router, struct llarp_rc *remote)
return false;
}
void
llarp_rc_clear(struct llarp_rc *rc)
{
// zero out router contact
llarp::Zero(rc, sizeof(llarp_rc));
}
bool
llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai)
{
struct llarp_rc *rc = (llarp_rc *)iter->user;
llarp_ai_list_pushback(rc->addrs, ai);
return true;
}
void
llarp_rc_set_addrs(struct llarp_rc *rc, struct llarp_alloc *mem,
struct llarp_ai_list *addr)
{
rc->addrs = llarp_ai_list_new(mem);
struct llarp_ai_list_iter ai_itr;
ai_itr.user = rc;
ai_itr.visit = &llarp_rc_addr_list_iter;
llarp_ai_list_iterate(addr, &ai_itr);
}
void
llarp_rc_set_pubkey(struct llarp_rc *rc, uint8_t *pubkey)
{
// set public key
memcpy(rc->pubkey, pubkey, 32);
}
bool
llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath,
llarp_seckey_t *identity)
{
fs::path path(fpath);
std::error_code ec;
if(!fs::exists(path, ec))
{
crypto->keygen(*identity);
std::ofstream f(path, std::ios::binary);
if(f.is_open())
{
f.write((char *)*identity, sizeof(identity));
}
}
std::ifstream f(path, std::ios::binary);
if(f.is_open())
{
f.read((char *)*identity, sizeof(identity));
return true;
}
return false;
}
bool
llarp_rc_write(struct llarp_rc *rc, const char *fpath)
{
fs::path our_rc_file(fpath);
byte_t tmp[MAX_RC_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(llarp_rc_bencode(rc, &buf))
{
std::ofstream f(our_rc_file, std::ios::binary);
if(f.is_open())
{
f.write((char *)buf.base, buf.cur - buf.base);
return true;
}
}
return false;
}
void
llarp_rc_sign(llarp_crypto *crypto, llarp_seckey_t *identity,
struct llarp_rc *rc)
{
// sign router contact
byte_t buf[MAX_RC_SIZE];
auto signbuf = llarp::StackBuffer< decltype(buf) >(buf);
// encode
if(llarp_rc_bencode(rc, &signbuf))
{
// sign
signbuf.sz = signbuf.cur - signbuf.base;
printf("router.cpp::llarp_rc_sign - sized [%zu/%zu]\n", signbuf.sz,
MAX_RC_SIZE);
crypto->sign(rc->signature, *identity, signbuf);
printf("router.cpp::llarp_rc_sign - signed\n");
}
}
void
llarp_stop_router(struct llarp_router *router)
{
@ -348,8 +426,8 @@ namespace llarp
struct llarp_link *link = nullptr;
if(StrEq(section, "iwp-links"))
{
link = new llarp_link;
llarp::Zero(link, sizeof(llarp_link));
link = llarp::Alloc< llarp_link >(self->mem);
llarp::Zero(link, sizeof(*link));
llarp_iwp_args args = {
.mem = self->mem,
@ -359,43 +437,23 @@ namespace llarp
.keyfile = self->transport_keyfile.c_str(),
};
iwp_link_init(link, args, &self->muxer);
}
else if(StrEq(section, "iwp-connect"))
{
std::error_code ec;
if(fs::exists(val, ec))
self->connect.try_emplace(key, val);
else
printf("cannot read %s\n", val);
return;
}
else if(StrEq(section, "router"))
{
if(StrEq(key, "contact-file"))
if(llarp_link_initialized(link))
{
self->our_rc_file = val;
printf("storing signed rc at %s\n", self->our_rc_file.c_str());
if(link->configure(link, self->netloop, key, af, proto))
{
llarp_ai ai;
link->get_our_address(link, &ai);
llarp::Addr addr = ai;
printf("link %s bound to %s\n", key, addr.to_string().c_str());
return;
}
}
return;
printf("link %s failed to configure\n", key);
}
else
return;
if(llarp_link_initialized(link))
else if(StrEq(section, "iwp-connect"))
{
printf("link initialized...");
if(link->configure(link, self->netloop, key, af, proto))
{
llarp_ai ai;
link->get_our_address(link, &ai);
llarp::Addr addr = ai;
printf("configured on %s as %s\n", key, addr.to_string().c_str());
self->AddLink(link);
return;
}
self->connect[key] = val;
}
self->mem->free(self->mem, link);
printf("failed to configure link for %s\n", key);
}
} // namespace llarp

@ -23,7 +23,7 @@ struct llarp_rc_decoder
static bool
llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
{
int64_t v;
uint64_t v;
llarp_buffer_t strbuf;
struct llarp_rc_decoder *dec = r->user;
struct llarp_alloc *mem = dec->mem;
@ -52,6 +52,13 @@ llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
return true;
}
if(llarp_buffer_eq(*key, "u"))
{
if(!bdecode_read_integer(r->buffer, &rc->last_updated))
return false;
return true;
}
if(llarp_buffer_eq(*key, "v"))
{
if(!bdecode_read_integer(r->buffer, &v))
@ -142,6 +149,12 @@ llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff)
if(!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t)))
return false;
/* write last updated */
if(!bencode_write_bytestring(buff, "u", 1))
return false;
if(!bencode_write_uint64(buff, rc->last_updated))
return false;
/* write version */
if(!bencode_write_version_entry(buff))
return false;

Loading…
Cancel
Save