ndppd/src/conf.cc

151 lines
3.7 KiB
C++
Raw Normal View History

2011-09-13 21:26:12 +02:00
// ndppd - NDP Proxy Daemon
2012-01-26 11:21:07 +01:00
// Copyright (C) 2011 Daniel Adolfsson <daniel@priv.nu>
2011-09-13 21:26:12 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <cstdio>
#include <cstdarg>
#include <netinet/ip6.h>
#include <confuse.h>
#include "ndppd.h"
NDPPD_NS_BEGIN
2011-09-13 21:26:12 +02:00
void conf::error_printf(cfg_t *cfg, const char *fmt, va_list ap)
2011-09-13 21:26:12 +02:00
{
char buf[256];
2011-09-13 21:26:12 +02:00
if (vsnprintf(buf, sizeof(buf), fmt, ap) <= 0)
return;
2011-09-13 21:26:12 +02:00
logger::error() << "[Config] " << buf;
2011-09-13 21:26:12 +02:00
}
int conf::validate_rule(cfg_t *cfg, cfg_opt_t *opt)
2011-09-13 21:26:12 +02:00
{
struct in6_addr addr, mask;
2011-09-13 21:26:12 +02:00
cfg_t *rule_cfg = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1);
2011-09-13 21:26:12 +02:00
if (!rule_cfg)
return -1;
2011-09-13 21:26:12 +02:00
// TODO: Maybe we should validate IP here?
2011-09-13 21:26:12 +02:00
return 0;
2011-09-13 21:26:12 +02:00
}
bool conf::setup(cfg_t *cfg)
2011-09-13 21:26:12 +02:00
{
int i;
2011-09-13 21:26:12 +02:00
for (i = 0; i < cfg_size(cfg, "proxy"); i++) {
cfg_t *proxy_cfg = cfg_getnsec(cfg, "proxy", i);
2011-09-13 21:26:12 +02:00
if (proxy_cfg) {
cfg_t *rule_cfg;
int i2;
2011-09-13 21:26:12 +02:00
std::shared_ptr<proxy> pr = proxy::open(cfg_title(proxy_cfg));
if (!pr)
continue;
2011-09-13 21:26:12 +02:00
pr->router(cfg_getbool(proxy_cfg, "router"));
pr->ttl(cfg_getint(proxy_cfg, "ttl"));
pr->timeout(cfg_getint(proxy_cfg, "timeout"));
for (i2 = 0; i2 < cfg_size(proxy_cfg, "rule"); i2++) {
cfg_t *rule_cfg;
2011-09-13 21:26:12 +02:00
if (!(rule_cfg = cfg_getnsec(proxy_cfg, "rule", i2)))
continue;
2011-09-13 21:26:12 +02:00
address addr(cfg_title(rule_cfg));
2011-09-13 21:26:12 +02:00
std::string ifname(cfg_getstr(rule_cfg, "iface"));
2011-09-13 21:26:12 +02:00
if (ifname.empty()) {
if (addr.prefix() <= 120) {
logger::warning() << "Static rule prefix /" << addr.prefix() << " <= 120 - is this what you want?";
}
2011-09-18 02:46:51 +02:00
pr->add_rule(addr);
}
else
{
pr->add_rule(addr, iface::open_ifd(ifname));
}
2011-09-18 02:46:51 +02:00
}
}
}
2011-09-13 21:26:12 +02:00
return 0;
2011-09-13 21:26:12 +02:00
}
bool conf::load(const std::string& path)
{
cfg_t *cfg;
int i, sz;
2011-09-13 21:26:12 +02:00
#define _S (char *)
static cfg_opt_t rule_opts[] =
{
CFG_STR (_S "iface", _S "", CFGF_NONE),
CFG_END ()
};
2011-09-13 21:26:12 +02:00
static cfg_opt_t proxy_opts[] =
{
CFG_SEC (_S "rule", rule_opts, CFGF_MULTI | CFGF_TITLE),
CFG_BOOL (_S "router", cfg_true, CFGF_NONE),
CFG_INT (_S "ttl", 30000, CFGF_NONE),
CFG_INT (_S "timeout", 500, CFGF_NONE),
CFG_END ()
};
2011-09-13 21:26:12 +02:00
static cfg_opt_t opts[] =
{
CFG_SEC (_S "proxy", proxy_opts, CFGF_MULTI | CFGF_TITLE),
CFG_FUNC (_S "include", &cfg_include),
CFG_END()
};
2011-09-13 21:26:12 +02:00
cfg = cfg_init(opts, CFGF_NOCASE);
2011-09-13 21:26:12 +02:00
cfg_set_error_function(cfg, &error_printf);
2011-09-13 21:26:12 +02:00
cfg_set_validate_func(cfg, "proxy|rule", &validate_rule);
2011-09-13 21:26:12 +02:00
switch (cfg_parse(cfg, path.c_str())) {
case CFG_SUCCESS:
break;
2011-09-13 21:26:12 +02:00
default:
logger::error() << "Failed to load configuration file '" << path << "'";
return false;
}
2011-09-13 21:26:12 +02:00
setup(cfg);
2011-09-13 21:26:12 +02:00
cfg_free(cfg);
2011-09-13 21:26:12 +02:00
return true;
2011-09-13 21:26:12 +02:00
}
NDPPD_NS_END