This commit is contained in:
Jeff Becker 2018-05-16 11:30:05 -04:00
parent e4005102ba
commit f7e52a1878
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
11 changed files with 165 additions and 100 deletions

View File

@ -20,10 +20,11 @@ set(LIB_SRC
llarp/config.cpp
llarp/crypto_async.c
llarp/crypto_libsodium.cpp
llarp/dtls_link.c
llarp/ev.cpp
llarp/exit_info.c
llarp/exit_route.c
llarp/iwp.cpp
llarp/iwp_link.c
llarp/link_intro.cpp
llarp/link_relay_down.cpp
llarp/link_relay_up.cpp

View File

@ -47,10 +47,13 @@ int shutdown_llarp(struct llarp_main *m) {
llarp_ev_loop_stop(m->mainloop);
progress();
llarp_threadpool_stop(m->worker);
if(m->worker)
llarp_threadpool_stop(m->worker);
progress();
llarp_threadpool_join(m->worker);
if(m->worker)
llarp_threadpool_join(m->worker);
progress();
if (m->logic) llarp_logic_stop(m->logic);
@ -93,7 +96,7 @@ int main(int argc, char *argv[]) {
llarp_mem_stdlib();
llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop);
printf("%s loaded\n", LLARP_VERSION);
printf("%s starting up\n", LLARP_VERSION);
if (!llarp_load_config(llarp.config, conffname)) {
printf("Loaded config %s\n", conffname);
struct llarp_config_iterator iter;

19
include/llarp/dtls.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef LLARP_DTLS_H_
#define LLARP_DTLS_H_
#include <llarp/link.h>
#ifdef __cplusplus
extern "C" {
#endif
struct llarp_dtls_args {
char key_file[255];
char cert_file[255];
};
bool dtls_link_init(struct llarp_link* link, struct llarp_dtls_args args,
struct llarp_msg_muxer* muxer);
#endif

View File

@ -11,6 +11,7 @@ extern "C" {
struct iwp_configure_args {
struct llarp_crypto* crypto;
const char * keyfile;
};
bool iwp_link_init(struct llarp_link* link, struct iwp_configure_args args,

View File

@ -50,29 +50,30 @@ struct llarp_link_session_iter {
struct llarp_link_ev_listener {
void *user;
void (*established)(struct llarp_ev_listener *, struct llarp_link_session *,
void (*established)(struct llarp_link_ev_listener *, struct llarp_link_session *,
bool);
void (*timeout)(struct llarp_ev_listener *, struct llarp_link_session *,
void (*timeout)(struct llarp_link_ev_listener *, struct llarp_link_session *,
bool);
void (*tx)(struct llarp_ev_listener *, struct llarp_link_session *, size_t);
void (*rx)(struct llarp_ev_listener *, struct llarp_link_session *, size_t);
void (*error)(struct llarp_ev_listener *, struct llarp_link_session *,
void (*tx)(struct llarp_link_ev_listener *, struct llarp_link_session *, size_t);
void (*rx)(struct llarp_link_ev_listener *, struct llarp_link_session *, size_t);
void (*error)(struct llarp_link_ev_listener *, struct llarp_link_session *,
const char *);
};
struct llarp_link {
void *impl;
const char *(*name)(struct llarp_link *);
/*
int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener);
void (*deregister_listener)(struct llarp_link *, int);
*/
bool (*configure)(struct llarp_link *, const char *, int, uint16_t);
bool (*start_link)(struct llarp_link *, struct llarp_logic *);
bool (*stop_link)(struct llarp_link *);
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*);
void (*try_establish)(struct llarp_link *, struct llarp_link_establish_job,
struct llarp_link_session_listener);
void (*free)(struct llarp_link *);
void (*free_impl)(struct llarp_link *);
};
struct llarp_link_session {

View File

@ -6,6 +6,7 @@
extern "C" {
#endif
/** called with userptr, original timeout, left */
typedef void (*llarp_timer_handler_func)(void *, uint64_t, uint64_t);
struct llarp_timeout_job {

111
llarp/dtls_link.c Normal file
View File

@ -0,0 +1,111 @@
#include <llarp/dtls.h>
struct dtls_link
{
struct llarp_logic * logic;
uint32_t timeout_job_id;
};
static struct dtls_link * dtls_link_alloc(struct llarp_msg_muxer * muxer, char * keyfile, char * certfile)
{
struct dtls_link * link = llarp_g_mem.alloc(sizeof(struct dtls_link), 8);
return link;
}
static const char * dtls_link_name(struct llarp_link * l)
{
(void) l;
return "dtls";
}
// forward declare
static void dtls_link_cleanup_timer(void * l, uint64_t orig, uint64_t left);
static void dtls_link_issue_cleanup_timer(struct dtls_link * link, uint64_t timeout)
{
struct llarp_timeout_job job = {
.timeout = timeout,
.user = link,
.handler = &dtls_link_cleanup_timer
};
link->timeout_job_id = llarp_logic_call_later(link->logic, job);
}
static bool dtls_link_start(struct llarp_link * l, struct llarp_logic * logic)
{
struct dtls_link * link = l->impl;
link->timeout_job_id = 0;
link->logic = logic;
// start cleanup timer
dtls_link_issue_cleanup_timer(link, 1000); // every 1 second
return true;
}
static void dtls_link_cleanup_dead_sessions(struct dtls_link * link)
{
// TODO: implement
}
static void dtls_link_cleanup_timer(void * l, uint64_t orig, uint64_t left)
{
struct dtls_link * link = l;
// clear out previous id of job
link->timeout_job_id = 0;
if(!left)
{
dtls_link_cleanup_dead_sessions(link);
//TODO: exponential backoff for cleanup timer ?
dtls_link_issue_cleanup_timer(link, orig);
}
}
static bool dtls_link_stop(struct llarp_link *l)
{
struct dtls_link * link = l->impl;
if(link->timeout_job_id)
{
llarp_logic_cancel_call(link->logic, link->timeout_job_id);
}
return true;
}
static void dtls_link_iter_sessions(struct llarp_link * l, struct llarp_link_session_iter * iter)
{
/*
struct dtls_link * link = l->impl;
struct llarp_link_session * session;
iter->link = l;
*/
}
static void dtls_link_try_establish(struct llarp_link * link, struct llarp_link_establish_job job, struct llarp_link_session_listener l)
{
}
static void dtls_link_free(struct llarp_link *l)
{
llarp_g_mem.free(l->impl);
}
bool dtls_link_init(struct llarp_link * link, struct llarp_dtls_args args, struct llarp_msg_muxer * muxer)
{
link->impl = dtls_link_alloc(muxer, args.key_file, args.cert_file);
link->name = dtls_link_name;
/*
link->register_listener = dtls_link_reg_listener;
link->deregister_listener = dtls_link_dereg_listener;
*/
link->start_link = dtls_link_start;
link->stop_link = dtls_link_stop;
link->iter_sessions = dtls_link_iter_sessions;
link->try_establish = dtls_link_try_establish;
link->free_impl = dtls_link_free;
return false;
}

View File

@ -1,83 +0,0 @@
#include <llarp/iwp.h>
#include <cstring>
#include <list>
#include <string>
namespace iwp {
struct link_impl {
link_impl(llarp_seckey_t k, llarp_msg_muxer *m) : muxer(m) {
memcpy(seckey, k, sizeof(seckey));
}
std::list<llarp_link_ev_listener> link_listeners;
llarp_seckey_t seckey;
llarp_msg_muxer *muxer;
std::string linkname;
bool configure(const char *ifname, int af, uint16_t port) {
// todo: implement
linkname = std::string(ifname) + std::string("+") + std::to_string(port);
return false;
}
const char *name() { return linkname.c_str(); }
bool start(llarp_logic *logic) {
// todo: implement
return false;
}
bool stop() {
// todo: implement
return false;
}
};
static bool configure(struct llarp_link *l, const char *ifname, int af,
uint16_t port) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->configure(ifname, af, port);
}
static bool start_link(struct llarp_link *l, struct llarp_logic *logic) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->start(logic);
}
static bool stop_link(struct llarp_link *l) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->stop();
}
static void free_link(struct llarp_link *l) {
link_impl *link = static_cast<link_impl *>(l->impl);
delete link;
}
static const char *link_name(struct llarp_link *l) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->name();
}
} // namespace iwp
extern "C" {
bool iwp_link_init(struct llarp_link *link, struct iwp_configure_args args,
struct llarp_msg_muxer *muxer) {
llarp_seckey_t seckey;
args.crypto->keygen(&seckey);
link->impl = new iwp::link_impl(seckey, muxer);
link->name = &iwp::link_name;
link->configure = &iwp::configure;
link->start_link = &iwp::start_link;
link->stop_link = &iwp::stop_link;
link->free = &iwp::free_link;
return true;
}
}

10
llarp/iwp_link.c Normal file
View File

@ -0,0 +1,10 @@
#include <llarp/iwp.h>
bool iwp_link_init(struct llarp_link * link, struct iwp_configure_args args,
struct llarp_msg_muxer * muxer)
{
return true;
}

View File

@ -72,7 +72,7 @@ void llarp_stop_router(struct llarp_router *router) {
void llarp_free_router(struct llarp_router **router) {
if (*router) {
(*router)->ForEachLink([](llarp_link *link) { link->free(link); });
(*router)->ForEachLink([](llarp_link *link) { link->free_impl(link); delete link; });
delete *router;
}
*router = nullptr;
@ -87,25 +87,25 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
if (StrEq(section, "links")) {
if (StrEq(val, "eth")) {
struct llarp_link *link = llarp::Alloc<llarp_link>();
iwp_configure_args args = {.crypto = &self->crypto};
iwp_configure_args args = {.crypto = &self->crypto, .keyfile=self->transport_keyfile};
iwp_link_init(link, args, &self->muxer);
if (link->configure(link, key, AF_PACKET, LLARP_ETH_PROTO)) {
printf("ethernet link configured on %s\n", key);
self->AddLink(link);
} else {
link->free(link);
delete link;
printf("failed to configure ethernet link for %s\n", key);
}
} else {
struct llarp_link *link = llarp::Alloc<llarp_link>();
uint16_t port = std::atoi(val);
iwp_configure_args args = {.crypto = &self->crypto};
iwp_configure_args args = {.crypto = &self->crypto, .keyfile=self->transport_keyfile};
iwp_link_init(link, args, &self->muxer);
if (link->configure(link, key, AF_INET6, port)) {
printf("inet link configured on %s port %d\n", key, port);
self->AddLink(link);
} else {
link->free(link);
delete link;
printf("failed to configure inet link for %s port %d\n", key, port);
}
}

View File

@ -16,6 +16,7 @@ struct router_links {
struct llarp_router {
bool ready;
const char * transport_keyfile = "transport.key";
struct llarp_threadpool *tp;
llarp::router_links links;
llarp_crypto crypto;