From feacde7864ae617476ffaad9834ca81161ade828 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 23 Mar 2020 10:15:00 -0600 Subject: [PATCH] Hook up 'undeclared handlers' to mop up loosely-structured INI values --- llarp/config/config.cpp | 104 ++++++++++++++++++++-------------------- llarp/config/config.hpp | 6 ++- 2 files changed, 58 insertions(+), 52 deletions(-) diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index abb48d31d..df705fe1e 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -249,77 +249,79 @@ namespace llarp }); } + LinksConfig::LinkInfo + LinksConfig::LinkInfoFromINIValues(string_view name, string_view value) + { + // we treat the INI k:v pair as: + // k: interface name, * indicating outbound + // v: a comma-separated list of values, an int indicating port (everything else ignored) + // this is somewhat of a backwards- and forwards-compatibility thing + + LinkInfo info; + info.addressFamily = AF_INET; + info.interface = str(name); + + std::vector splits = split(value, ','); + for (string_view str : splits) + { + int asNum = std::atoi(str.data()); + if (asNum > 0) + info.port = asNum; + + // otherwise, ignore ("future-proofing") + } + + return info; + } + void LinksConfig::defineConfigOptions(Configuration& conf) { - /* - uint16_t proto = 0; + conf.addUndeclaredHandler("bind", [&](string_view, string_view name, string_view value) { + LinkInfo info = LinkInfoFromINIValues(name, value); - std::unordered_set parsed_opts; - std::string::size_type idx; - static constexpr char delimiter = ','; - do - { - idx = val.find_first_of(delimiter); - if (idx != string_view::npos) - { - parsed_opts.emplace(TrimWhitespace(val.substr(0, idx))); - val.remove_prefix(idx + 1); - } - else - { - parsed_opts.emplace(TrimWhitespace(val)); - } - } while (idx != string_view::npos); - std::unordered_set opts; - /// for each option - for (const auto& item : parsed_opts) - { - /// see if it's a number - auto port = std::atoi(item.c_str()); - if (port > 0) + if (info.port <= 0) + throw std::invalid_argument(stringify("Invalid [bind] port specified on interface", name)); + + if(name == "*") { - /// set port - if (proto == 0) - { - proto = port; - } + info.port = fromEnv(info.port, "OUTBOUND_PORT"); + m_OutboundLink = std::move(info); } else { - opts.insert(item); + m_InboundLinks.emplace_back(std::move(info)); } - } - if (key == "*") - { - m_OutboundLink = - std::make_tuple("*", AF_INET, fromEnv(proto, "OUTBOUND_PORT"), std::move(opts)); - } - else - { - // str() here for gcc 5 compat - m_InboundLinks.emplace_back(str(key), AF_INET, proto, std::move(opts)); - } - */ - (void)conf; - // throw std::runtime_error("FIXME"); + return true; + }); + } void ConnectConfig::defineConfigOptions(Configuration& conf) { - // routers.emplace_back(val.begin(), val.end()); - (void)conf; - // throw std::runtime_error("FIXME"); + + conf.addUndeclaredHandler("connect", [this](string_view section, + string_view name, + string_view value) { + (void)section; + (void)name; + routers.emplace_back(value); + return true; + }); } void ServicesConfig::defineConfigOptions(Configuration& conf) { - // services.emplace_back(str(key), str(val)); // str()'s here for gcc 5 compat - (void)conf; - // throw std::runtime_error("FIXME"); + conf.addUndeclaredHandler("services", [this](string_view section, + string_view name, + string_view value) { + (void)section; + services.emplace_back(name, value); + return true; + }); } void diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index a6d9c076d..30deb2332 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -146,8 +146,12 @@ namespace llarp { std::string interface; int addressFamily; - uint16_t port; + uint16_t port = -1; }; + /// Create a LinkInfo from the given string. + /// @throws if str does not represent a LinkInfo. + LinkInfo + LinkInfoFromINIValues(string_view name, string_view value); public: LinkInfo m_OutboundLink;