mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-10-29 11:05:43 +00:00
315798a0c4
* fix various crashes and race conditions * rename iwp-connect to connect in config * rename iwp-links to bind in config * always make a link just for outbound sessions even if no bind section is provided
82 lines
1.9 KiB
C++
82 lines
1.9 KiB
C++
#include "config.hpp"
|
|
#include <llarp/config.h>
|
|
#include "ini.hpp"
|
|
#include "mem.hpp"
|
|
|
|
namespace llarp
|
|
{
|
|
template < typename Config, typename Section >
|
|
static const Section &
|
|
find_section(Config &c, const std::string &name, const Section &fallback)
|
|
{
|
|
if(c.sections.find(name) == c.sections.end())
|
|
return fallback;
|
|
return c.sections[name].values;
|
|
}
|
|
|
|
bool
|
|
Config::Load(const char *fname)
|
|
{
|
|
std::ifstream f;
|
|
f.open(fname);
|
|
if(f.is_open())
|
|
{
|
|
ini::Parser parser(f);
|
|
auto &top = parser.top();
|
|
router = find_section(top, "router", section_t{});
|
|
network = find_section(top, "network", section_t{});
|
|
connect = find_section(top, "connect", section_t{});
|
|
netdb = find_section(top, "netdb", section_t{});
|
|
iwp_links = find_section(top, "bind", section_t{});
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
} // namespace llarp
|
|
|
|
extern "C" {
|
|
|
|
void
|
|
llarp_new_config(struct llarp_config **conf)
|
|
{
|
|
llarp_config *c = new llarp_config;
|
|
*conf = c;
|
|
}
|
|
|
|
void
|
|
llarp_free_config(struct llarp_config **conf)
|
|
{
|
|
if(*conf)
|
|
delete *conf;
|
|
*conf = nullptr;
|
|
}
|
|
|
|
int
|
|
llarp_load_config(struct llarp_config *conf, const char *fname)
|
|
{
|
|
if(!conf->impl.Load(fname))
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
llarp_config_iter(struct llarp_config *conf, struct llarp_config_iterator *iter)
|
|
{
|
|
iter->conf = conf;
|
|
std::map< std::string, llarp::Config::section_t & > sections = {
|
|
{"network", conf->impl.network},
|
|
{"connect", conf->impl.connect},
|
|
{"bind", conf->impl.iwp_links},
|
|
{"netdb", conf->impl.netdb}};
|
|
|
|
for(const auto item : conf->impl.router)
|
|
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
|
|
|
|
for(const auto section : sections)
|
|
for(const auto item : section.second)
|
|
iter->visit(iter, section.first.c_str(), item.first.c_str(),
|
|
item.second.c_str());
|
|
}
|
|
}
|