lokinet/llarp/router.cpp

121 lines
3.4 KiB
C++
Raw Normal View History

2018-02-01 17:07:01 +00:00
#include "router.hpp"
#include <llarp/ibfq.h>
2018-04-04 13:54:37 +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"
2017-11-28 14:05:31 +00:00
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-04-05 14:23:14 +00:00
llarp_router::llarp_router() : ready(false) { 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)
head->next = new llarp::router_links{link, nullptr};
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-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-01-29 14:27:24 +00:00
struct llarp_router *llarp_init_router(struct llarp_threadpool *tp) {
llarp_router *router = new llarp_router;
router->tp = tp;
llarp_crypto_libsodium_init(&router->crypto);
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-04-05 14:23:14 +00:00
return router->Ready();
2018-01-29 14:27:24 +00:00
}
2018-01-08 13:49:05 +00:00
2018-04-30 18:18:34 +00:00
void llarp_run_router(struct llarp_router *router, struct llarp_logic *logic) {
2018-04-30 16:14:20 +00:00
router->ForEachLink([logic](llarp_link *link) {
int result = link->start_link(link, logic);
2018-04-05 14:43:16 +00:00
if (result == -1) printf("link %s failed to start\n", link->name(link));
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 15:30:05 +00:00
(*router)->ForEachLink([](llarp_link *link) { link->free_impl(link); delete link; });
2018-01-29 14:27:24 +00:00
delete *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-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) {
llarp_router *self = static_cast<llarp_router *>(iter->user);
if (StrEq(section, "links")) {
2018-04-05 14:23:14 +00:00
if (StrEq(val, "eth")) {
2018-04-04 13:54:37 +00:00
struct llarp_link *link = llarp::Alloc<llarp_link>();
2018-05-16 15:30:05 +00:00
iwp_configure_args args = {.crypto = &self->crypto, .keyfile=self->transport_keyfile};
2018-05-16 15:49:16 +00:00
if(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);
return;
}
2018-01-19 16:51:27 +00:00
}
2018-05-16 15:49:16 +00:00
delete link;
printf("failed to configure ethernet link for %s\n", key);
2018-04-05 14:23:14 +00:00
} else {
struct llarp_link *link = llarp::Alloc<llarp_link>();
uint16_t port = std::atoi(val);
2018-05-16 15:30:05 +00:00
iwp_configure_args args = {.crypto = &self->crypto, .keyfile=self->transport_keyfile};
2018-05-16 15:49:16 +00:00
if(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);
return;
}
2018-04-04 13:54:37 +00:00
}
2018-05-16 15:49:16 +00:00
delete link;
printf("failed to configure inet link for %s port %d\n", key, port);
2018-04-05 14:43:16 +00:00
}
2018-01-19 16:51:27 +00:00
}
}
2018-02-01 13:21:00 +00:00
} // namespace llarp