lokinet/llarp/router.cpp
Jeff Becker 045a692522
more
2018-01-25 11:11:45 -05:00

102 lines
2.0 KiB
C++

#include <sarp/router.h>
#include <sarp/link.h>
#include "link.hpp"
#include <list>
#include "str.hpp"
namespace sarp
{
void router_iter_config(sarp_config_iterator * iter, const char * section, const char * key, const char * val);
struct Router
{
std::list<Link *> Links;
sarp_crypto * crypto;
void Close()
{
if(Links.size())
{
for(auto & itr : Links)
{
sarp_ev_close_udp_listener(itr->Listener());
}
Links.clear();
}
}
bool Configured()
{
if(Links.size()) return true;
return false;
}
};
}
extern "C" {
struct sarp_router
{
sarp::Router impl;
sarp_crypto crypto;
};
void sarp_init_router(struct sarp_router ** router)
{
*router = static_cast<sarp_router *>(sarp_g_mem.malloc(sizeof(sarp_router)));
if(*router)
{
sarp_crypto_libsodium_init(&(*router)->crypto);
}
}
int sarp_configure_router(struct sarp_router * router, struct sarp_config * conf)
{
sarp_config_iterator iter;
iter.user = router;
iter.visit = sarp::router_iter_config;
sarp_config_iter(conf, &iter);
return router->impl.Configured() ? 0 : -1;
}
void sarp_run_router(struct sarp_router * router, struct sarp_ev_loop * loop)
{
if(router->impl.Links.size())
for(auto & iter : router->impl.Links)
sarp_ev_add_udp_listener(loop, iter->Listener());
}
void sarp_free_router(struct sarp_router ** router)
{
if(*router)
{
sarp_router * r = *router;
r->impl.Close();
sarp_g_mem.free(*router);
}
*router = nullptr;
}
}
namespace sarp
{
void router_iter_config(sarp_config_iterator * iter, const char * section, const char * key, const char * val)
{
sarp_router * self = static_cast<sarp_router *>(iter->user);
if (streq(section, "links"))
{
if(streq(val, "ip"))
{
self->impl.Links.push_back(new Link(&self->crypto));
}
else if (streq(val, "eth"))
{
/** todo: ethernet link */
}
}
}
}