lokinet/llarp/router.cpp

122 lines
3.3 KiB
C++
Raw Normal View History

2018-02-01 17:07:01 +00:00
#include "router.hpp"
#include <llarp/ibfq.h>
2018-02-01 13:21:00 +00:00
#include <llarp/link.h>
#include <llarp/router.h>
2018-04-04 13:54:37 +00:00
#include <llarp/iwp.h>
2018-04-05 14:23:14 +00:00
#include <llarp/proto.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;
}
bool llarp_router::Ready()
{
return ready;
2018-02-01 17:06:49 +00:00
}
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-04 13:54:37 +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-01-29 14:27:24 +00:00
struct llarp_config *conf) {
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-01-29 14:27:24 +00:00
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) {
router->ForEachLink([loop](llarp_link *link) {
2018-04-05 14:23:14 +00:00
int result = link->start_link(link, loop);
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-01-29 14:27:24 +00:00
void llarp_stop_router(struct llarp_router *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-04-04 13:54:37 +00:00
(*router)->ForEachLink([](llarp_link *link) { link->free(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-04-05 14:23:14 +00:00
iwp_configure_args args = {
.crypto = &self->crypto
};
2018-04-04 13:54:37 +00:00
iwp_link_init(link, args, &self->muxer);
2018-04-05 14:23:14 +00:00
if (link->configure(link, key, AF_PACKET, LLARP_ETH_PROTO))
{
printf("ethernet link configured on %s\n", key);
2018-01-29 14:27:24 +00:00
self->AddLink(link);
2018-04-05 14:23:14 +00:00
}
2018-01-29 14:27:24 +00:00
else {
2018-04-04 13:54:37 +00:00
link->free(link);
2018-04-05 14:23:14 +00:00
printf("failed to configure ethernetn link for %s\n", key);
2018-01-19 16:51:27 +00:00
}
2018-04-05 14:23:14 +00:00
} else {
struct llarp_link *link = llarp::Alloc<llarp_link>();
uint16_t port = std::atoi(val);
iwp_configure_args args = {
.crypto = &self->crypto
};
2018-04-04 13:54:37 +00:00
iwp_link_init(link, args, &self->muxer);
2018-04-05 14:23:14 +00:00
if (link->configure(link, key, AF_INET6, port))
{
printf("inet link configured on %s port %d\n", key, port);
2018-04-04 13:54:37 +00:00
self->AddLink(link);
2018-04-05 14:23:14 +00:00
}
2018-04-04 13:54:37 +00:00
else {
link->free(link);
2018-04-05 14:23:14 +00:00
printf("failed to configure inet link for %s port %d\n", key, port);
2018-04-04 13:54:37 +00:00
}
2018-04-05 14:23:14 +00:00
}
2018-01-19 16:51:27 +00:00
}
}
2018-02-01 13:21:00 +00:00
} // namespace llarp