Multiple changes

* Change Makefile so release is the default mode, use DEBUG=1 to turn
  on debugging.

* Clean up 'conf' a bit.

* Add a new option 'router' (valid in 'proxy' section) to turn on/off
  the ND_NA_FLAG_ROUTER flag for adverts.
This commit is contained in:
Daniel Adolfsson 2011-09-18 02:25:43 +02:00
parent f65d5c0e14
commit 9bf5799109
8 changed files with 72 additions and 59 deletions

View File

@ -1,8 +1,14 @@
CC = g++ ifdef DEBUG
CFLAGS = -g -DDEBUG CFLAGS = -g -DDEBUG
else
CFLAGS = -O3
endif
CC = g++
LDFLAGS = -lconfuse LDFLAGS = -lconfuse
OBJ = log.o ndppd.o iface.o proxy.o address.o rule.o session.o conf.o OBJ = log.o ndppd.o iface.o proxy.o address.o rule.o session.o conf.o
all: ndppd all: ndppd
ndppd: ${OBJ} ndppd: ${OBJ}

View File

@ -22,75 +22,62 @@
__NDPPD_NS_BEGIN __NDPPD_NS_BEGIN
void conf::error_printf(::cfg_t *cfg, const char *fmt, va_list ap) void conf::error_printf(cfg_t *cfg, const char *fmt, va_list ap)
{ {
char buf[256]; char buf[256];
if(::vsnprintf(buf, sizeof(buf), fmt, ap) <= 0) if(vsnprintf(buf, sizeof(buf), fmt, ap) <= 0)
return; return;
ERR("[Config] %s", buf); ERR("[Config] %s", buf);
} }
int conf::validate_rule(::cfg_t *cfg, ::cfg_opt_t *opt) int conf::validate_rule(cfg_t *cfg, cfg_opt_t *opt)
{ {
struct in6_addr addr, mask; struct in6_addr addr, mask;
::cfg_t *rule_cfg = ::cfg_opt_getnsec(opt, ::cfg_opt_size(opt) - 1); cfg_t *rule_cfg = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1);
if(!rule_cfg) if(!rule_cfg)
return -1; return -1;
if(::cfg_getbool(rule_cfg, "static") == ::cfg_true) // TODO: Maybe we should validate IP here?
{
if(::cfg_getstr(rule_cfg, "iface") != 0)
{
::cfg_error(rule_cfg, "'static' cannot be 'true' if 'iface' is set");
return -1;
}
}
else
{
if(::cfg_getstr(rule_cfg, "iface") == 0)
{
::cfg_error(rule_cfg, "'iface' must be set unless 'static' is 'true'");
return -1;
}
}
return 0; return 0;
} }
bool conf::setup(::cfg_t *cfg) bool conf::setup(cfg_t *cfg)
{ {
int i; int i;
for(i = 0; i < ::cfg_size(cfg, "proxy"); i++) for(i = 0; i < cfg_size(cfg, "proxy"); i++)
{ {
::cfg_t *proxy_cfg = ::cfg_getnsec(cfg, "proxy", i); cfg_t *proxy_cfg = cfg_getnsec(cfg, "proxy", i);
if(proxy_cfg) if(proxy_cfg)
{ {
::cfg_t *rule_cfg; cfg_t *rule_cfg;
int i2; int i2;
strong_ptr<proxy> pr = proxy::open(::cfg_title(proxy_cfg)); strong_ptr<proxy> pr = proxy::open(cfg_title(proxy_cfg));
if(pr.is_null()) if(pr.is_null())
continue; continue;
for(i2 = 0; i2 < ::cfg_size(proxy_cfg, "rule"); i2++) pr->router(cfg_getbool(proxy_cfg, "router"));
{
::cfg_t *rule_cfg;
if(!(rule_cfg = ::cfg_getnsec(proxy_cfg, "rule", i2))) for(i2 = 0; i2 < cfg_size(proxy_cfg, "rule"); i2++)
{
cfg_t *rule_cfg;
if(!(rule_cfg = cfg_getnsec(proxy_cfg, "rule", i2)))
continue; continue;
address addr(::cfg_title(rule_cfg)); address addr(cfg_title(rule_cfg));
std::string ifname(::cfg_getstr(rule_cfg, "iface")); std::string ifname(cfg_getstr(rule_cfg, "iface"));
if(ifname == "static") if(ifname == "")
pr->add_rule(addr); pr->add_rule(addr);
else else
pr->add_rule(addr, iface::open_ifd(ifname)); pr->add_rule(addr, iface::open_ifd(ifname));
@ -103,36 +90,38 @@ bool conf::setup(::cfg_t *cfg)
bool conf::load(const std::string& path) bool conf::load(const std::string& path)
{ {
::cfg_t *cfg; cfg_t *cfg;
int i, sz; int i, sz;
static ::cfg_opt_t rule_opts[] = #define _S (char *)
static cfg_opt_t rule_opts[] =
{ {
CFG_BOOL((char *)"static", ::cfg_false, CFGF_NONE), CFG_STR (_S "iface", _S "", CFGF_NONE),
CFG_STR((char *)"iface", (char *)"static", CFGF_NONE),
CFG_END () CFG_END ()
}; };
static ::cfg_opt_t proxy_opts[] = static cfg_opt_t proxy_opts[] =
{ {
CFG_SEC((char *)"rule", rule_opts, CFGF_MULTI | CFGF_TITLE), CFG_SEC (_S "rule", rule_opts, CFGF_MULTI | CFGF_TITLE),
CFG_BOOL (_S "router", cfg_true, CFGF_NONE),
CFG_END () CFG_END ()
}; };
static ::cfg_opt_t opts[] = static cfg_opt_t opts[] =
{ {
CFG_SEC((char *)"proxy", proxy_opts, CFGF_MULTI | CFGF_TITLE), CFG_SEC (_S "proxy", proxy_opts, CFGF_MULTI | CFGF_TITLE),
CFG_FUNC((char *)"include", &::cfg_include), CFG_FUNC (_S "include", &cfg_include),
CFG_END() CFG_END()
}; };
cfg = ::cfg_init(opts, CFGF_NOCASE); cfg = cfg_init(opts, CFGF_NOCASE);
::cfg_set_error_function(cfg, &error_printf); cfg_set_error_function(cfg, &error_printf);
::cfg_set_validate_func(cfg, "proxy|rule", &validate_rule); cfg_set_validate_func(cfg, "proxy|rule", &validate_rule);
switch(::cfg_parse(cfg, path.c_str())) switch(cfg_parse(cfg, path.c_str()))
{ {
case CFG_SUCCESS: case CFG_SUCCESS:
break; break;
@ -144,7 +133,7 @@ bool conf::load(const std::string& path)
setup(cfg); setup(cfg);
::cfg_free(cfg); cfg_free(cfg);
return true; return true;
} }

View File

@ -416,7 +416,7 @@ ssize_t iface::write_solicit(const address& taddr)
return write(_ifd, daddr, (uint8_t *)buf, sizeof(struct nd_neighbor_solicit) + sizeof(struct nd_opt_hdr) + 6); return write(_ifd, daddr, (uint8_t *)buf, sizeof(struct nd_neighbor_solicit) + sizeof(struct nd_opt_hdr) + 6);
} }
ssize_t iface::write_advert(const address& daddr, const address& taddr) ssize_t iface::write_advert(const address& daddr, const address& taddr, bool router)
{ {
char buf[128]; char buf[128];
@ -432,7 +432,7 @@ ssize_t iface::write_advert(const address& daddr, const address& taddr)
opt->nd_opt_len = 1; opt->nd_opt_len = 1;
na->nd_na_type = ND_NEIGHBOR_ADVERT; na->nd_na_type = ND_NEIGHBOR_ADVERT;
na->nd_na_flags_reserved = ND_NA_FLAG_SOLICITED; // | ND_NA_FLAG_ROUTER; na->nd_na_flags_reserved = ND_NA_FLAG_SOLICITED | (router ? ND_NA_FLAG_ROUTER : 0);
memcpy(&na->nd_na_target, &taddr.const_addr(), sizeof(struct in6_addr)); memcpy(&na->nd_na_target, &taddr.const_addr(), sizeof(struct in6_addr));

View File

@ -94,7 +94,7 @@ public:
ssize_t write_solicit(const address& taddr); ssize_t write_solicit(const address& taddr);
// Writes a NB_NEIGHBOR_ADVERT message to the _ifd socket; // Writes a NB_NEIGHBOR_ADVERT message to the _ifd socket;
ssize_t write_advert(const address& daddr, const address& taddr); ssize_t write_advert(const address& daddr, const address& taddr, bool router);
// Reads a NB_NEIGHBOR_SOLICIT message from the _pfd socket. // Reads a NB_NEIGHBOR_SOLICIT message from the _pfd socket.
ssize_t read_solicit(address& saddr, address& daddr, address& taddr); ssize_t read_solicit(address& saddr, address& daddr, address& taddr);

View File

@ -98,6 +98,8 @@ int main(int argc, char *argv[], char *env[])
} }
} }
NFO("ndppd (NDP Proxy Daemon) version " NDPPD_VERSION);
NFO("Using configuration file '%s'", config_path.c_str()); NFO("Using configuration file '%s'", config_path.c_str());
if(!conf::load(config_path)) if(!conf::load(config_path))

View File

@ -132,5 +132,15 @@ const strong_ptr<iface>& proxy::ifa() const
return _ifa; return _ifa;
} }
bool proxy::router() const
{
return _router;
}
void proxy::router(bool val)
{
_router = val;
}
__NDPPD_NS_END __NDPPD_NS_END

View File

@ -40,6 +40,8 @@ private:
std::list<strong_ptr<session> > _sessions; std::list<strong_ptr<session> > _sessions;
bool _router;
proxy(); proxy();
public: public:
@ -57,6 +59,10 @@ public:
strong_ptr<rule> add_rule(const address& addr); strong_ptr<rule> add_rule(const address& addr);
const strong_ptr<iface>& ifa() const; const strong_ptr<iface>& ifa() const;
bool router() const;
void router(bool val);
}; };
__NDPPD_NS_END __NDPPD_NS_END

View File

@ -105,7 +105,7 @@ void session::send_solicit()
void session::send_advert() void session::send_advert()
{ {
_pr->ifa()->write_advert(_saddr, _taddr); _pr->ifa()->write_advert(_saddr, _taddr, _pr->router());
} }
void session::handle_advert() void session::handle_advert()