From 9bf57991095c6b101eae10e27bd831f1f2051c49 Mon Sep 17 00:00:00 2001 From: Daniel Adolfsson Date: Sun, 18 Sep 2011 02:25:43 +0200 Subject: [PATCH] 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. --- src/Makefile | 8 ++++- src/conf.cc | 97 ++++++++++++++++++++++---------------------------- src/iface.cc | 4 +-- src/iface.h | 2 +- src/ndppd.cc | 2 ++ src/proxy.cc | 10 ++++++ src/proxy.h | 6 ++++ src/session.cc | 2 +- 8 files changed, 72 insertions(+), 59 deletions(-) diff --git a/src/Makefile b/src/Makefile index ce47775..c32d05e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,8 +1,14 @@ -CC = g++ +ifdef DEBUG CFLAGS = -g -DDEBUG +else +CFLAGS = -O3 +endif + +CC = g++ LDFLAGS = -lconfuse OBJ = log.o ndppd.o iface.o proxy.o address.o rule.o session.o conf.o + all: ndppd ndppd: ${OBJ} diff --git a/src/conf.cc b/src/conf.cc index a062a69..d3d31bf 100644 --- a/src/conf.cc +++ b/src/conf.cc @@ -22,75 +22,62 @@ __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]; - if(::vsnprintf(buf, sizeof(buf), fmt, ap) <= 0) + if(vsnprintf(buf, sizeof(buf), fmt, ap) <= 0) return; 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; - ::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) return -1; - if(::cfg_getbool(rule_cfg, "static") == ::cfg_true) - { - 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; - } - } + // TODO: Maybe we should validate IP here? return 0; } -bool conf::setup(::cfg_t *cfg) +bool conf::setup(cfg_t *cfg) { 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) { - ::cfg_t *rule_cfg; + cfg_t *rule_cfg; int i2; - strong_ptr pr = proxy::open(::cfg_title(proxy_cfg)); + strong_ptr pr = proxy::open(cfg_title(proxy_cfg)); if(pr.is_null()) continue; - for(i2 = 0; i2 < ::cfg_size(proxy_cfg, "rule"); i2++) - { - ::cfg_t *rule_cfg; + pr->router(cfg_getbool(proxy_cfg, "router")); - 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; - 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); else 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) { - ::cfg_t *cfg; + cfg_t *cfg; 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((char *)"iface", (char *)"static", CFGF_NONE), + CFG_STR (_S "iface", _S "", CFGF_NONE), + CFG_END () + }; + + 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_END () + }; + + static cfg_opt_t opts[] = + { + CFG_SEC (_S "proxy", proxy_opts, CFGF_MULTI | CFGF_TITLE), + CFG_FUNC (_S "include", &cfg_include), CFG_END() }; - static ::cfg_opt_t proxy_opts[] = - { - CFG_SEC((char *)"rule", rule_opts, CFGF_MULTI | CFGF_TITLE), - CFG_END() - }; + cfg = cfg_init(opts, CFGF_NOCASE); - static ::cfg_opt_t opts[] = - { - CFG_SEC((char *)"proxy", proxy_opts, CFGF_MULTI | CFGF_TITLE), - CFG_FUNC((char *)"include", &::cfg_include), - CFG_END() - }; + cfg_set_error_function(cfg, &error_printf); - cfg = ::cfg_init(opts, CFGF_NOCASE); + cfg_set_validate_func(cfg, "proxy|rule", &validate_rule); - ::cfg_set_error_function(cfg, &error_printf); - - ::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: break; @@ -144,7 +133,7 @@ bool conf::load(const std::string& path) setup(cfg); - ::cfg_free(cfg); + cfg_free(cfg); return true; } diff --git a/src/iface.cc b/src/iface.cc index 070ea5d..d879551 100644 --- a/src/iface.cc +++ b/src/iface.cc @@ -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); } -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]; @@ -432,7 +432,7 @@ ssize_t iface::write_advert(const address& daddr, const address& taddr) opt->nd_opt_len = 1; 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)); diff --git a/src/iface.h b/src/iface.h index 8fa7422..9c09cf3 100644 --- a/src/iface.h +++ b/src/iface.h @@ -94,7 +94,7 @@ public: ssize_t write_solicit(const address& taddr); // 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. ssize_t read_solicit(address& saddr, address& daddr, address& taddr); diff --git a/src/ndppd.cc b/src/ndppd.cc index 5d11e63..3ca3a10 100644 --- a/src/ndppd.cc +++ b/src/ndppd.cc @@ -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()); if(!conf::load(config_path)) diff --git a/src/proxy.cc b/src/proxy.cc index 8b2b8ec..da76b79 100644 --- a/src/proxy.cc +++ b/src/proxy.cc @@ -132,5 +132,15 @@ const strong_ptr& proxy::ifa() const return _ifa; } +bool proxy::router() const +{ + return _router; +} + +void proxy::router(bool val) +{ + _router = val; +} + __NDPPD_NS_END diff --git a/src/proxy.h b/src/proxy.h index 009c54c..4d0d8dd 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -40,6 +40,8 @@ private: std::list > _sessions; + bool _router; + proxy(); public: @@ -57,6 +59,10 @@ public: strong_ptr add_rule(const address& addr); const strong_ptr& ifa() const; + + bool router() const; + + void router(bool val); }; __NDPPD_NS_END diff --git a/src/session.cc b/src/session.cc index 7be895c..68215cb 100644 --- a/src/session.cc +++ b/src/session.cc @@ -105,7 +105,7 @@ void session::send_solicit() void session::send_advert() { - _pr->ifa()->write_advert(_saddr, _taddr); + _pr->ifa()->write_advert(_saddr, _taddr, _pr->router()); } void session::handle_advert()