ndppd/src/conf.cc

149 lines
3.3 KiB
C++
Raw Normal View History

2011-09-13 21:26:12 +02:00
// ndppd - NDP Proxy Daemon
// Copyright (C) 2011 Daniel Adolfsson <daniel.adolfsson@tuhox.com>
//
// 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
void conf::error_printf(cfg_t *cfg, const char *fmt, va_list ap)
2011-09-13 21:26:12 +02:00
{
char buf[256];
if(vsnprintf(buf, sizeof(buf), fmt, ap) <= 0)
2011-09-13 21:26:12 +02:00
return;
ERR("[Config] %s", buf);
}
int conf::validate_rule(cfg_t *cfg, cfg_opt_t *opt)
2011-09-13 21:26:12 +02:00
{
struct in6_addr addr, mask;
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;
// TODO: Maybe we should validate IP here?
2011-09-13 21:26:12 +02:00
return 0;
}
bool conf::setup(cfg_t *cfg)
2011-09-13 21:26:12 +02:00
{
int i;
for(i = 0; i < cfg_size(cfg, "proxy"); i++)
2011-09-13 21:26:12 +02:00
{
cfg_t *proxy_cfg = cfg_getnsec(cfg, "proxy", i);
2011-09-13 21:26:12 +02:00
if(proxy_cfg)
{
cfg_t *rule_cfg;
2011-09-13 21:26:12 +02:00
int i2;
strong_ptr<proxy> pr = proxy::open(cfg_title(proxy_cfg));
if(pr.is_null())
continue;
2011-09-13 21:26:12 +02:00
pr->router(cfg_getbool(proxy_cfg, "router"));
for(i2 = 0; i2 < cfg_size(proxy_cfg, "rule"); i2++)
2011-09-13 21:26:12 +02:00
{
cfg_t *rule_cfg;
2011-09-13 21:26:12 +02:00
if(!(rule_cfg = cfg_getnsec(proxy_cfg, "rule", i2)))
2011-09-13 21:26:12 +02:00
continue;
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
2011-09-18 02:46:51 +02:00
if(ifname.empty())
{
if(addr.prefix() <= 120)
NCE("Static rule prefix /%d <= 120 - is this what you want?", addr.prefix());
2011-09-13 21:26:12 +02:00
pr->add_rule(addr);
2011-09-18 02:46:51 +02:00
}
2011-09-13 21:26:12 +02:00
else
2011-09-18 02:46:51 +02:00
{
2011-09-16 17:06:36 +02:00
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;
}
bool conf::load(const std::string& path)
{
cfg_t *cfg;
2011-09-13 21:26:12 +02:00
int i, sz;
#define _S (char *)
static cfg_opt_t rule_opts[] =
2011-09-13 21:26:12 +02:00
{
CFG_STR (_S "iface", _S "", CFGF_NONE),
CFG_END ()
2011-09-13 21:26:12 +02:00
};
static cfg_opt_t proxy_opts[] =
2011-09-13 21:26:12 +02:00
{
CFG_SEC (_S "rule", rule_opts, CFGF_MULTI | CFGF_TITLE),
CFG_BOOL (_S "router", cfg_true, CFGF_NONE),
CFG_END ()
2011-09-13 21:26:12 +02:00
};
static cfg_opt_t opts[] =
2011-09-13 21:26:12 +02:00
{
CFG_SEC (_S "proxy", proxy_opts, CFGF_MULTI | CFGF_TITLE),
CFG_FUNC (_S "include", &cfg_include),
2011-09-13 21:26:12 +02:00
CFG_END()
};
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()))
2011-09-13 21:26:12 +02:00
{
case CFG_SUCCESS:
break;
default:
ERR("Failed to load configuration file '%s'", path.c_str());
return false;
}
setup(cfg);
cfg_free(cfg);
2011-09-13 21:26:12 +02:00
return true;
}
__NDPPD_NS_END