From eefa9cb88100e9e6930043ea5f6d57892439b84b Mon Sep 17 00:00:00 2001 From: Daniel Adolfsson Date: Sat, 17 Sep 2011 16:37:24 +0200 Subject: [PATCH] Multiple changes * Add '-d' to "daemonize". Will also enable syslogging. * Fix a couple of bugs in 'session'. * Clean up 'log', and use LOG_* macros instead. * Add syslog(bool) to 'log' to enable/disable the use of syslog. --- src/iface.cc | 2 +- src/iface.h | 2 +- src/log.cc | 35 +++++++++++++++++++++++++++++++---- src/log.h | 31 ++++++++++++------------------- src/ndppd.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++---- src/proxy.cc | 2 +- src/proxy.h | 4 ++-- src/session.cc | 4 +++- 8 files changed, 95 insertions(+), 33 deletions(-) diff --git a/src/iface.cc b/src/iface.cc index 001692e..77997cd 100644 --- a/src/iface.cc +++ b/src/iface.cc @@ -542,7 +542,7 @@ int iface::poll_all() continue; } - for(std::list >::iterator s_it = ifa->_sessions.begin(); + for(std::list >::iterator s_it = ifa->_sessions.begin(); s_it != ifa->_sessions.end(); s_it++) { if(((*s_it)->taddr() == taddr) && ((*s_it)->status() == session::WAITING)) diff --git a/src/iface.h b/src/iface.h index fc744ea..5e00b09 100644 --- a/src/iface.h +++ b/src/iface.h @@ -61,7 +61,7 @@ private: // An array of sessions that are monitoring this interface for // ND_NEIGHBOR_ADVERT messages. - std::list > _sessions; + std::list > _sessions; strong_ptr _pr; diff --git a/src/log.cc b/src/log.cc index ae49356..0d810bb 100644 --- a/src/log.cc +++ b/src/log.cc @@ -15,6 +15,7 @@ // along with this program. If not, see . #include #include +#include #include "ndppd.h" @@ -25,24 +26,30 @@ __NDPPD_NS_BEGIN const char *log::_level_str[] = { "fatal", + "alert", + "critical", "error", "warning", - "bug", "notice", "info", "debug" }; +bool log::_syslog = false; + void log::puts(int level, const char *str) { const char *ls; - if((level < 0) || (level >= MAX_L)) + if((level < 0) || (level > LOG_DEBUG)) ls = "unknown"; else ls = _level_str[level]; - fprintf(stderr, "% 7s : %s\n", ls, str); + if(_syslog) + ::syslog(level, "(%s) %s", ls, str); + else + fprintf(stderr, "(% 8s) %s\n", ls, str); } void log::printf(int level, const char *fmt, ...) @@ -55,11 +62,31 @@ void log::printf(int level, const char *fmt, ...) if(vsnprintf(buf, sizeof(buf), fmt, args) > 0) { - puts(level, buf); } va_end(args); } +void log::syslog(bool sl) +{ + if(sl == _syslog) + return; + + if(_syslog = sl) + { +#ifdef DEBUG + setlogmask(LOG_UPTO(LOG_DEBUG)); + openlog("ndppd", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); +#else + setlogmask(LOG_UPTO(LOG_INFO)); + openlog("ndppd", LOG_CONS, LOG_USER); +#endif + } + else + { + closelog(); + } +} + __NDPPD_NS_END diff --git a/src/log.h b/src/log.h index 7090835..839edb4 100644 --- a/src/log.h +++ b/src/log.h @@ -16,18 +16,19 @@ #ifndef __NDPPD_LOG_H #define __NDPPD_LOG_H +#include + #ifdef DEBUG -#define DBG(...) log::printf(log::L_DEBUG, __VA_ARGS__) +#define DBG(...) log::printf(LOG_DEBUG, __VA_ARGS__) #else #define DBG(...) #endif -#define ERR(...) log::printf(log::L_ERROR, __VA_ARGS__) -#define WRN(...) log::printf(log::L_WARNING, __VA_ARGS__) -#define BUG(...) log::printf(log::L_BUG, __VA_ARGS__) -#define NFO(...) log::printf(log::L_INFO, __VA_ARGS__) -#define NCE(...) log::printf(log::L_NOTICE, __VA_ARGS__) -#define FTL(...) log::printf(log::L_FATAL, __VA_ARGS__) +#define ERR(...) log::printf(LOG_ERR, __VA_ARGS__) +#define WRN(...) log::printf(LOG_WARNING, __VA_ARGS__) +#define CRT(...) log::printf(LOG_CRIT, __VA_ARGS__) +#define NFO(...) log::printf(LOG_INFO, __VA_ARGS__) +#define NCE(...) log::printf(LOG_NOTICE, __VA_ARGS__) __NDPPD_NS_BEGIN @@ -36,22 +37,14 @@ class log private: static const char *_level_str[]; -public: - enum - { - L_FATAL, - L_ERROR, - L_WARNING, - L_BUG, - L_NOTICE, - L_INFO, - L_DEBUG, - MAX_L - }; + static bool _syslog; +public: static void puts(int level, const char *str); static void printf(int level, const char *fmt, ...); + + static void syslog(bool enable); }; __NDPPD_NS_END diff --git a/src/ndppd.cc b/src/ndppd.cc index c3387e5..326d418 100644 --- a/src/ndppd.cc +++ b/src/ndppd.cc @@ -19,17 +19,40 @@ #include #include +#include +#include + #include "ndppd.h" using namespace ndppd; +int daemonize() +{ + pid_t pid = fork(); + + if(pid < 0) + return -1; + + if(pid > 0) + exit(0); + + pid_t sid = setsid(); + + if(sid < 0) + return -1; + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + return 0; +} + int main(int argc, char *argv[], char *env[]) { // std::string config_path("/etc/ndppd.conf"); std::string config_path("../ndppd.conf"); - - std::cout << "ndppd - NDP Proxy Daemon" << std::endl; - std::cout << "Version " NDPPD_VERSION << std::endl; + bool daemon = false; while(1) { @@ -38,10 +61,11 @@ int main(int argc, char *argv[], char *env[]) static struct option long_options[] = { { "config", 1, 0, 'c' }, + { "daemon", 0, 0, 'd' }, { 0, 0, 0, 0} }; - c = getopt_long(argc, argv, "c:v", long_options, &opt); + c = getopt_long(argc, argv, "c:d", long_options, &opt); if(c == -1) break; @@ -56,6 +80,22 @@ int main(int argc, char *argv[], char *env[]) } config_path = optarg; + break; + + case 'd': + daemon = true; + break; + } + } + + if(daemon) + { + log::syslog(true); + + if(daemonize() < 0) + { + ERR("Failed to daemonize process"); + return 1; } } diff --git a/src/proxy.cc b/src/proxy.cc index 8a9973c..8b2b8ec 100644 --- a/src/proxy.cc +++ b/src/proxy.cc @@ -74,7 +74,7 @@ void proxy::handle_solicit(const address& saddr, const address& daddr, break; case session::VALID: - (*sit)->send_solicit(); + (*sit)->send_advert(); } return; diff --git a/src/proxy.h b/src/proxy.h index 699ef60..009c54c 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -32,14 +32,14 @@ class rule; class proxy { private: + weak_ptr _ptr; + strong_ptr _ifa; std::list > _rules; std::list > _sessions; - weak_ptr _ptr; - proxy(); public: diff --git a/src/session.cc b/src/session.cc index c17ee19..7be895c 100644 --- a/src/session.cc +++ b/src/session.cc @@ -50,6 +50,8 @@ void session::update_all(int elapsed_time) session::~session() { + DBG("session::~session() this=%x", this); + _sessions.remove(_ptr); for(std::list >::iterator it = _ifaces.begin(); @@ -109,7 +111,7 @@ void session::send_advert() void session::handle_advert() { _status = VALID; - _ttl = 500; + _ttl = 30 * 1000; send_advert(); }