2018-01-25 17:24:33 +01:00
|
|
|
#include <llarp/router.h>
|
|
|
|
#include <llarp/link.h>
|
2017-11-28 15:05:31 +01:00
|
|
|
#include "link.hpp"
|
2018-01-26 15:17:51 +01:00
|
|
|
#include "mem.hpp"
|
2018-01-19 17:51:27 +01:00
|
|
|
#include "str.hpp"
|
2017-11-28 15:05:31 +01:00
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
namespace llarp
|
2017-11-28 15:05:31 +01:00
|
|
|
{
|
2018-01-25 17:24:33 +01:00
|
|
|
void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val);
|
2018-01-27 02:18:10 +01:00
|
|
|
struct router_links
|
2017-11-28 15:05:31 +01:00
|
|
|
{
|
2018-01-27 02:18:10 +01:00
|
|
|
struct llarp_link * link = nullptr;
|
|
|
|
struct router_links * next = nullptr;
|
2017-11-28 15:05:31 +01:00
|
|
|
};
|
2018-01-08 14:49:05 +01:00
|
|
|
|
2017-11-28 15:05:31 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 02:18:10 +01:00
|
|
|
struct llarp_router
|
|
|
|
{
|
|
|
|
struct llarp_threadpool * tp;
|
|
|
|
llarp::router_links links;
|
|
|
|
llarp_crypto crypto;
|
2018-01-19 17:51:27 +01:00
|
|
|
|
2018-01-08 14:49:05 +01:00
|
|
|
|
2018-01-27 02:18:10 +01:00
|
|
|
static void * operator new(size_t sz)
|
2018-01-08 14:49:05 +01:00
|
|
|
{
|
2018-01-27 02:18:10 +01:00
|
|
|
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void operator delete(void * ptr)
|
|
|
|
{
|
|
|
|
llarp_g_mem.free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddLink(struct llarp_link * link)
|
|
|
|
{
|
|
|
|
llarp::router_links * head = &links;
|
|
|
|
while(head->next && head->link)
|
|
|
|
head = head->next;
|
|
|
|
|
|
|
|
if(head->link)
|
|
|
|
head->next = new llarp::router_links{link, nullptr};
|
|
|
|
else
|
|
|
|
head->link = link;
|
|
|
|
}
|
2018-01-08 14:49:05 +01:00
|
|
|
|
2018-01-27 02:18:10 +01:00
|
|
|
void ForEachLink(std::function<void(llarp_link *)> visitor)
|
2018-01-08 14:49:05 +01:00
|
|
|
{
|
2018-01-27 02:18:10 +01:00
|
|
|
llarp::router_links * cur = &links;
|
|
|
|
do
|
2018-01-19 17:51:27 +01:00
|
|
|
{
|
2018-01-27 02:18:10 +01:00
|
|
|
if(cur->link)
|
|
|
|
visitor(cur->link);
|
|
|
|
cur = cur->next;
|
2018-01-19 17:51:27 +01:00
|
|
|
}
|
2018-01-27 02:18:10 +01:00
|
|
|
while(cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close()
|
|
|
|
{
|
|
|
|
ForEachLink(llarp_link_stop);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
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 14:49:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
int llarp_configure_router(struct llarp_router * router, struct llarp_config * conf)
|
2018-01-08 14:49:05 +01:00
|
|
|
{
|
2018-01-25 17:24:33 +01:00
|
|
|
llarp_config_iterator iter;
|
2018-01-08 14:49:05 +01:00
|
|
|
iter.user = router;
|
2018-01-25 17:24:33 +01:00
|
|
|
iter.visit = llarp::router_iter_config;
|
|
|
|
llarp_config_iter(conf, &iter);
|
2018-01-27 02:18:10 +01:00
|
|
|
return 0;
|
2018-01-08 14:49:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
void llarp_run_router(struct llarp_router * router, struct llarp_ev_loop * loop)
|
2018-01-08 14:49:05 +01:00
|
|
|
{
|
2018-01-27 02:18:10 +01:00
|
|
|
router->ForEachLink([loop](llarp_link * link) {
|
|
|
|
llarp_ev_add_udp_listener(loop, llarp_link_udp_listener(link));
|
|
|
|
});
|
2018-01-08 14:49:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 15:19:00 +01:00
|
|
|
void llarp_stop_router(struct llarp_router * router)
|
|
|
|
{
|
|
|
|
router->Close();
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
void llarp_free_router(struct llarp_router ** router)
|
2018-01-08 14:49:05 +01:00
|
|
|
{
|
|
|
|
if(*router)
|
2018-01-19 17:51:27 +01:00
|
|
|
{
|
2018-01-29 15:19:00 +01:00
|
|
|
(*router)->ForEachLink([](llarp_link * link) { llarp_g_mem.free(link); });
|
|
|
|
delete *router;
|
2018-01-19 17:51:27 +01:00
|
|
|
}
|
2018-01-08 14:49:05 +01:00
|
|
|
*router = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 17:51:27 +01:00
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
namespace llarp
|
2018-01-19 17:51:27 +01:00
|
|
|
{
|
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
void router_iter_config(llarp_config_iterator * iter, const char * section, const char * key, const char * val)
|
2018-01-19 17:51:27 +01:00
|
|
|
{
|
2018-01-25 17:24:33 +01:00
|
|
|
llarp_router * self = static_cast<llarp_router *>(iter->user);
|
2018-01-26 15:17:51 +01:00
|
|
|
if (StrEq(section, "links"))
|
2018-01-19 17:51:27 +01:00
|
|
|
{
|
2018-01-26 15:17:51 +01:00
|
|
|
if(StrEq(val, "ip"))
|
2018-01-19 17:51:27 +01:00
|
|
|
{
|
2018-01-27 02:18:10 +01:00
|
|
|
struct llarp_link * link = llarp_link_alloc();
|
|
|
|
if(llarp_link_configure(link, key, AF_INET6))
|
|
|
|
self->AddLink(link);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp_link_free(&link);
|
|
|
|
printf("failed to configure %s link for %s\n", val, key);
|
|
|
|
}
|
2018-01-19 17:51:27 +01:00
|
|
|
}
|
2018-01-26 15:17:51 +01:00
|
|
|
else if (StrEq(val, "eth"))
|
2018-01-19 17:51:27 +01:00
|
|
|
{
|
|
|
|
/** todo: ethernet link */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|