2018-02-01 17:07:01 +00:00
|
|
|
#include "router.hpp"
|
|
|
|
#include <llarp/ibfq.h>
|
2018-05-17 20:00:58 +00:00
|
|
|
#include <llarp/dtls.h>
|
2018-05-18 12:39:17 +00:00
|
|
|
#include <llarp/iwp.h>
|
2018-04-05 14:43:16 +00:00
|
|
|
#include <llarp/link.h>
|
2018-04-05 14:23:14 +00:00
|
|
|
#include <llarp/proto.h>
|
2018-04-05 14:43:16 +00:00
|
|
|
#include <llarp/router.h>
|
2018-01-19 16:51:27 +00:00
|
|
|
#include "str.hpp"
|
2018-05-21 13:17:07 +00:00
|
|
|
#include "buffer.hpp"
|
2017-11-28 14:05:31 +00:00
|
|
|
|
2018-05-20 17:45:47 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2018-01-29 14:27:24 +00:00
|
|
|
namespace llarp {
|
|
|
|
void router_iter_config(llarp_config_iterator *iter, const char *section,
|
|
|
|
const char *key, const char *val);
|
2018-02-01 13:21:00 +00:00
|
|
|
} // namespace llarp
|
2017-11-28 14:05:31 +00:00
|
|
|
|
2018-05-16 18:13:18 +00:00
|
|
|
llarp_router::llarp_router(struct llarp_alloc *m) : ready(false), mem(m) { llarp_msg_muxer_init(&muxer); }
|
2018-02-01 17:07:01 +00:00
|
|
|
|
|
|
|
llarp_router::~llarp_router() {}
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-02-01 17:06:49 +00:00
|
|
|
void llarp_router::AddLink(struct llarp_link *link) {
|
|
|
|
llarp::router_links *head = &links;
|
|
|
|
while (head->next && head->link) head = head->next;
|
2018-02-01 17:07:01 +00:00
|
|
|
|
2018-02-01 17:06:49 +00:00
|
|
|
if (head->link)
|
2018-05-16 18:13:18 +00:00
|
|
|
{
|
|
|
|
void * ptr = mem->alloc(mem, sizeof(llarp::router_links), 8);
|
|
|
|
head->next = new (ptr) llarp::router_links{link, nullptr};
|
|
|
|
}
|
2018-02-01 17:06:49 +00:00
|
|
|
else
|
|
|
|
head->link = link;
|
2018-04-05 14:23:14 +00:00
|
|
|
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
|
2018-04-05 14:43:16 +00:00
|
|
|
bool llarp_router::Ready() { return ready; }
|
2018-01-29 14:27:24 +00:00
|
|
|
|
2018-05-21 12:43:32 +00:00
|
|
|
bool llarp_findOrCreateIdentity(llarp_crypto *crypto, fs::path path,
|
|
|
|
llarp_seckey_t *identity) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-20 17:45:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:43:32 +00:00
|
|
|
bool llarp_rc_write(struct llarp_rc *rc, fs::path our_rc_file) {
|
|
|
|
uint8_t tmp[MAX_RC_SIZE];
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = (char*)tmp;
|
|
|
|
buf.cur = (char*) tmp;
|
|
|
|
buf.sz = sizeof(tmp);
|
|
|
|
if(llarp_rc_bencode(rc, &buf))
|
|
|
|
{
|
|
|
|
std::ofstream f(our_rc_file, std::ios::binary);
|
|
|
|
if(f.is_open())
|
|
|
|
{
|
|
|
|
f.write(buf.base, buf.cur - buf.base);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-20 17:45:47 +00:00
|
|
|
bool llarp_router::SaveRC()
|
|
|
|
{
|
|
|
|
printf("verify rc signature... ");
|
|
|
|
if(!llarp_rc_verify_sig(&crypto, &rc))
|
|
|
|
{
|
|
|
|
printf(" BAD!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
printf(" OK.\n");
|
2018-05-21 12:43:32 +00:00
|
|
|
|
2018-05-20 17:45:47 +00:00
|
|
|
uint8_t tmp[MAX_RC_SIZE];
|
2018-05-21 13:17:07 +00:00
|
|
|
auto buf = llarp::StackBuffer<decltype(tmp)>(tmp);
|
|
|
|
|
2018-05-20 17:45:47 +00:00
|
|
|
if(llarp_rc_bencode(&rc, &buf))
|
|
|
|
{
|
|
|
|
std::ofstream f(our_rc_file, std::ios::binary);
|
|
|
|
if(f.is_open())
|
|
|
|
{
|
|
|
|
f.write(buf.base, buf.cur - buf.base);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:06:49 +00:00
|
|
|
void llarp_router::ForEachLink(std::function<void(llarp_link *)> visitor) {
|
2018-02-01 17:07:01 +00:00
|
|
|
llarp::router_links *cur = &links;
|
|
|
|
do {
|
|
|
|
if (cur->link) visitor(cur->link);
|
|
|
|
cur = cur->next;
|
|
|
|
} while (cur);
|
|
|
|
}
|
|
|
|
|
2018-04-05 14:43:16 +00:00
|
|
|
void llarp_router::Close() {
|
|
|
|
ForEachLink([](llarp_link *l) { l->stop_link(l); });
|
|
|
|
}
|
2018-01-27 01:18:10 +00:00
|
|
|
extern "C" {
|
|
|
|
|
2018-05-18 20:08:57 +00:00
|
|
|
struct llarp_router *llarp_init_router(struct llarp_alloc * mem, struct llarp_threadpool *tp, struct llarp_ev_loop * netloop, struct llarp_logic * logic) {
|
2018-05-16 18:13:18 +00:00
|
|
|
void * ptr = mem->alloc(mem, sizeof(llarp_router), 16);
|
|
|
|
if(!ptr) return nullptr;
|
|
|
|
llarp_router *router = new (ptr) llarp_router(mem);
|
|
|
|
if(router)
|
|
|
|
{
|
|
|
|
router->netloop = netloop;
|
|
|
|
router->tp = tp;
|
2018-05-18 20:08:57 +00:00
|
|
|
router->logic = logic;
|
2018-05-16 18:13:18 +00:00
|
|
|
llarp_crypto_libsodium_init(&router->crypto);
|
|
|
|
}
|
2018-01-29 14:27:24 +00:00
|
|
|
return router;
|
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-04-05 14:23:14 +00:00
|
|
|
bool llarp_configure_router(struct llarp_router *router,
|
2018-04-05 14:43:16 +00:00
|
|
|
struct llarp_config *conf) {
|
2018-01-29 14:27:24 +00:00
|
|
|
llarp_config_iterator iter;
|
|
|
|
iter.user = router;
|
|
|
|
iter.visit = llarp::router_iter_config;
|
|
|
|
llarp_config_iter(conf, &iter);
|
2018-05-20 17:45:47 +00:00
|
|
|
if(!router->Ready()) return false;
|
|
|
|
return router->EnsureIdentity();
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-05-21 12:43:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llarp_rc_sign(llarp_crypto *crypto, llarp_seckey_t *identity,
|
|
|
|
struct llarp_rc *rc) {
|
|
|
|
// sign router contact
|
|
|
|
llarp_buffer_t signbuf;
|
|
|
|
char buf[MAX_RC_SIZE];
|
|
|
|
signbuf.base = buf;
|
|
|
|
signbuf.cur = buf;
|
|
|
|
signbuf.sz = sizeof(buf);
|
|
|
|
// encode
|
|
|
|
if(llarp_rc_bencode(rc, &signbuf))
|
|
|
|
{
|
|
|
|
printf("router.cpp::llarp_rc_sign - encoded [%s]\n", buf);
|
|
|
|
// sign
|
|
|
|
signbuf.sz = signbuf.cur - signbuf.base;
|
|
|
|
printf("router.cpp::llarp_rc_sign - sized [%d/%d]\n", signbuf.sz, MAX_RC_SIZE);
|
|
|
|
crypto->sign(rc->signature, *identity, signbuf);
|
|
|
|
printf("router.cpp::llarp_rc_sign - signed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 20:08:57 +00:00
|
|
|
void llarp_run_router(struct llarp_router *router) {
|
2018-05-20 17:45:47 +00:00
|
|
|
|
|
|
|
// zero out router contact
|
|
|
|
llarp::Zero(&router->rc, sizeof(llarp_rc));
|
|
|
|
// fill our address list
|
|
|
|
router->rc.addrs = llarp_ai_list_new(router->mem);
|
|
|
|
router->ForEachLink([router](llarp_link *link) {
|
|
|
|
llarp_ai addr;
|
|
|
|
link->get_our_address(link, &addr);
|
|
|
|
llarp_ai_list_pushback(router->rc.addrs, addr);
|
2018-01-29 14:27:24 +00:00
|
|
|
});
|
2018-05-20 17:45:47 +00:00
|
|
|
// set public key
|
|
|
|
memcpy(router->rc.pubkey, router->pubkey(), 32);
|
2018-05-21 12:43:32 +00:00
|
|
|
|
2018-05-20 17:45:47 +00:00
|
|
|
// sign router contact
|
|
|
|
char buf[MAX_RC_SIZE];
|
2018-05-21 13:17:07 +00:00
|
|
|
auto signbuf = llarp::StackBuffer<decltype(buf)>(buf);
|
2018-05-20 17:45:47 +00:00
|
|
|
// encode
|
|
|
|
if(llarp_rc_bencode(&router->rc, &signbuf))
|
|
|
|
{
|
|
|
|
// sign
|
|
|
|
signbuf.sz = signbuf.cur - signbuf.base;
|
|
|
|
router->crypto.sign(router->rc.signature, router->identity, signbuf);
|
|
|
|
if(router->SaveRC())
|
|
|
|
{
|
|
|
|
printf("saved router contact\n");
|
|
|
|
llarp_logic * logic = router->logic;
|
|
|
|
router->ForEachLink([logic](llarp_link *link) {
|
|
|
|
int result = link->start_link(link, logic);
|
|
|
|
if (result == -1) printf("link %s failed to start\n", link->name());
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("failed to generate rc\n");
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-05-16 13:56:51 +00:00
|
|
|
void llarp_stop_router(struct llarp_router *router) {
|
|
|
|
if(router)
|
|
|
|
router->Close();
|
|
|
|
}
|
2018-01-29 14:19:00 +00:00
|
|
|
|
2018-01-29 14:27:24 +00:00
|
|
|
void llarp_free_router(struct llarp_router **router) {
|
|
|
|
if (*router) {
|
2018-05-16 18:13:18 +00:00
|
|
|
struct llarp_alloc * mem = (*router)->mem;
|
|
|
|
(*router)->ForEachLink([mem](llarp_link *link) { link->free_impl(link); mem->free(mem, link); });
|
|
|
|
(*router)->~llarp_router();
|
|
|
|
mem->free(mem, *router);
|
2018-01-08 13:49:05 +00:00
|
|
|
}
|
2018-01-29 14:27:24 +00:00
|
|
|
*router = nullptr;
|
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-05-17 20:00:58 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-01-29 14:27:24 +00:00
|
|
|
|
|
|
|
void router_iter_config(llarp_config_iterator *iter, const char *section,
|
2018-05-17 20:00:58 +00:00
|
|
|
const char *key, const char *val)
|
|
|
|
{
|
2018-01-29 14:27:24 +00:00
|
|
|
llarp_router *self = static_cast<llarp_router *>(iter->user);
|
2018-05-18 12:39:17 +00:00
|
|
|
int af;
|
|
|
|
uint16_t proto;
|
|
|
|
if (StrEq(val, "eth"))
|
2018-05-17 20:00:58 +00:00
|
|
|
{
|
2018-05-18 12:39:17 +00:00
|
|
|
af = AF_PACKET;
|
|
|
|
proto = LLARP_ETH_PROTO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-18 13:17:58 +00:00
|
|
|
af = AF_INET;
|
2018-05-18 12:39:17 +00:00
|
|
|
proto = std::atoi(val);
|
|
|
|
}
|
|
|
|
|
2018-05-20 17:45:47 +00:00
|
|
|
struct llarp_link *link = nullptr;
|
2018-05-18 14:40:06 +00:00
|
|
|
if (StrEq(section, "iwp-links"))
|
2018-05-18 12:39:17 +00:00
|
|
|
{
|
2018-05-21 12:43:32 +00:00
|
|
|
link = llarp::Alloc<llarp_link>(self->mem);
|
2018-05-18 12:39:17 +00:00
|
|
|
llarp::Zero(link, sizeof(*link));
|
2018-05-21 12:43:32 +00:00
|
|
|
|
2018-05-18 14:40:06 +00:00
|
|
|
llarp_iwp_args args = {
|
2018-05-18 12:39:17 +00:00
|
|
|
.mem = self->mem,
|
|
|
|
.crypto = &self->crypto,
|
2018-05-18 20:08:57 +00:00
|
|
|
.logic = self->logic,
|
|
|
|
.cryptoworker = self->tp,
|
2018-05-20 17:45:47 +00:00
|
|
|
.keyfile = self->transport_keyfile.c_str(),
|
2018-05-18 12:39:17 +00:00
|
|
|
};
|
|
|
|
iwp_link_init(link, args, &self->muxer);
|
|
|
|
}
|
2018-05-20 17:45:47 +00:00
|
|
|
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"))
|
|
|
|
{
|
|
|
|
self->our_rc_file = val;
|
|
|
|
printf("storing signed rc at %s\n", self->our_rc_file.c_str());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-05-18 12:39:17 +00:00
|
|
|
else
|
|
|
|
return;
|
2018-05-21 12:43:32 +00:00
|
|
|
|
2018-05-18 12:39:17 +00:00
|
|
|
if(llarp_link_initialized(link))
|
|
|
|
{
|
2018-05-18 13:17:58 +00:00
|
|
|
printf("link initialized...");
|
2018-05-18 12:39:17 +00:00
|
|
|
if (link->configure(link, self->netloop, key, af, proto))
|
|
|
|
{
|
2018-05-18 13:17:58 +00:00
|
|
|
printf("configured on %s\n", key);
|
2018-05-18 12:39:17 +00:00
|
|
|
self->AddLink(link);
|
|
|
|
return;
|
2018-04-05 14:43:16 +00:00
|
|
|
}
|
2018-01-19 16:51:27 +00:00
|
|
|
}
|
2018-05-18 12:39:17 +00:00
|
|
|
self->mem->free(self->mem, link);
|
|
|
|
printf("failed to configure link for %s\n", key);
|
2018-01-19 16:51:27 +00:00
|
|
|
}
|
2018-05-21 12:43:32 +00:00
|
|
|
|
2018-02-01 13:21:00 +00:00
|
|
|
} // namespace llarp
|