2017-11-28 14:05:31 +00:00
|
|
|
#include "config.hpp"
|
2018-02-01 13:21:00 +00:00
|
|
|
#include <llarp/config.h>
|
2018-08-09 13:55:51 +00:00
|
|
|
#include <llarp/defaults.h>
|
2018-07-27 00:21:57 +00:00
|
|
|
#include <llarp/net.hpp>
|
|
|
|
#include "fs.hpp"
|
2017-10-03 19:14:46 +00:00
|
|
|
#include "ini.hpp"
|
2018-07-27 00:21:57 +00:00
|
|
|
#include "logger.hpp"
|
2018-01-26 14:17:51 +00:00
|
|
|
#include "mem.hpp"
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
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;
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
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{});
|
2018-06-07 16:22:49 +00:00
|
|
|
connect = find_section(top, "connect", section_t{});
|
2018-05-22 15:54:19 +00:00
|
|
|
netdb = find_section(top, "netdb", section_t{});
|
2018-07-26 10:53:27 +00:00
|
|
|
dns = find_section(top, "dns", section_t{});
|
2018-06-07 16:22:49 +00:00
|
|
|
iwp_links = find_section(top, "bind", section_t{});
|
2018-07-09 17:32:11 +00:00
|
|
|
services = find_section(top, "services", section_t{});
|
2018-07-26 21:08:56 +00:00
|
|
|
dns = find_section(top, "dns", section_t{});
|
2018-08-09 13:55:51 +00:00
|
|
|
system = find_section(top, "system", section_t{});
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-02-01 13:21:00 +00:00
|
|
|
} // namespace llarp
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
extern "C"
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-07-27 04:25:34 +00:00
|
|
|
void
|
|
|
|
llarp_new_config(struct llarp_config **conf)
|
|
|
|
{
|
2018-08-09 13:55:51 +00:00
|
|
|
llarp_config *c = new llarp_config();
|
2018-07-27 04:25:34 +00:00
|
|
|
*conf = c;
|
|
|
|
}
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
void
|
|
|
|
llarp_free_config(struct llarp_config **conf)
|
|
|
|
{
|
|
|
|
if(*conf)
|
|
|
|
delete *conf;
|
|
|
|
*conf = nullptr;
|
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
int
|
|
|
|
llarp_load_config(struct llarp_config *conf, const char *fname)
|
|
|
|
{
|
|
|
|
if(!conf->impl.Load(fname))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-06 17:02:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
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 = {
|
2018-08-09 13:55:51 +00:00
|
|
|
{"network", conf->impl.network}, {"connect", conf->impl.connect},
|
|
|
|
{"system", conf->impl.system}, {"bind", conf->impl.iwp_links},
|
|
|
|
{"netdb", conf->impl.netdb}, {"dns", conf->impl.dns},
|
|
|
|
{"services", conf->impl.services}};
|
2018-06-06 17:02:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
for(const auto item : conf->impl.router)
|
|
|
|
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
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());
|
2018-07-27 00:21:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
bool
|
|
|
|
llarp_ensure_config(const char *fname)
|
2018-07-27 00:21:57 +00:00
|
|
|
{
|
2018-07-27 04:25:34 +00:00
|
|
|
std::error_code ec;
|
|
|
|
if(fs::exists(fname, ec))
|
|
|
|
return true;
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
llarp::LogError(ec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream f(fname);
|
|
|
|
if(!f.is_open())
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to open ", fname, " for writing");
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-09 13:38:44 +00:00
|
|
|
f << "# this configuration was auto generated with 'sane' defaults"
|
|
|
|
<< std::endl;
|
|
|
|
f << "# change these values as desired" << std::endl;
|
|
|
|
f << std::endl;
|
2018-08-09 13:55:51 +00:00
|
|
|
|
|
|
|
f << "# system settings for priviledges and such" << std::endl;
|
|
|
|
f << "[system]" << std::endl;
|
|
|
|
#ifdef _WIN32
|
|
|
|
f << "# ";
|
|
|
|
#endif
|
|
|
|
f << "user=" << DEFAULT_LOKINET_USER << std::endl;
|
|
|
|
#ifdef _WIN32
|
|
|
|
f << "# ";
|
|
|
|
#endif
|
|
|
|
f << "group=" << DEFAULT_LOKINET_GROUP << std::endl;
|
|
|
|
|
2018-08-09 13:38:44 +00:00
|
|
|
f << "# configuration for lokinet network interface" << std::endl;
|
|
|
|
f << "[network]" << std::endl;
|
|
|
|
f << "# interface name" << std::endl;
|
|
|
|
#ifdef _WIN32
|
|
|
|
// comment out ifname section for windows
|
|
|
|
f << "# ";
|
|
|
|
#endif
|
|
|
|
f << "ifname=lokitun0" << std::endl;
|
|
|
|
|
|
|
|
f << "# ip range for interface" << std::endl;
|
|
|
|
f << "addr=10.0.0.1/16" << std::endl;
|
|
|
|
f << std::endl;
|
|
|
|
|
|
|
|
f << "# dns provider configuration section" << std::endl;
|
|
|
|
f << "[dns]" << std::endl;
|
|
|
|
f << "# opennic us resolver" << std::endl;
|
|
|
|
f << "upstream=" << DEFAULT_RESOLVER_US << std::endl;
|
|
|
|
f << "# opennic eu resolver" << std::endl;
|
|
|
|
f << "upstream=" << DEFAULT_RESOLVER_EU << std::endl;
|
|
|
|
f << "# opennic au resolver" << std::endl;
|
|
|
|
f << "upstream=" << DEFAULT_RESOLVER_AU << std::endl;
|
|
|
|
f << "bind=127.3.2.1:53" << std::endl;
|
|
|
|
f << std::endl;
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
f << "[netdb]" << std::endl;
|
|
|
|
f << "dir=netdb" << std::endl;
|
2018-08-09 13:38:44 +00:00
|
|
|
f << std::endl;
|
|
|
|
f << "# publish network interfaces for handling inbound traffic"
|
|
|
|
<< std::endl;
|
2018-07-27 04:25:34 +00:00
|
|
|
f << "[bind]" << std::endl;
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
std::string ifname;
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
if(llarp::GetBestNetIF(ifname, AF_INET))
|
|
|
|
f << ifname << "=1090" << std::endl;
|
2018-08-09 13:38:44 +00:00
|
|
|
else
|
|
|
|
f << "# could not autodetect network interface" << std::endl
|
|
|
|
<< "# eth0=1090" << std::endl;
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-08-09 13:38:44 +00:00
|
|
|
f << std::endl;
|
2018-07-27 04:25:34 +00:00
|
|
|
llarp::LogInfo("Generated new config ", fname);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-27 00:21:57 +00:00
|
|
|
}
|