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.
This commit is contained in:
Daniel Adolfsson 2011-09-17 16:37:24 +02:00
parent 70933e0830
commit eefa9cb881
8 changed files with 95 additions and 33 deletions

View File

@ -542,7 +542,7 @@ int iface::poll_all()
continue;
}
for(std::list<strong_ptr<session> >::iterator s_it = ifa->_sessions.begin();
for(std::list<weak_ptr<session> >::iterator s_it = ifa->_sessions.begin();
s_it != ifa->_sessions.end(); s_it++)
{
if(((*s_it)->taddr() == taddr) && ((*s_it)->status() == session::WAITING))

View File

@ -61,7 +61,7 @@ private:
// An array of sessions that are monitoring this interface for
// ND_NEIGHBOR_ADVERT messages.
std::list<strong_ptr<session> > _sessions;
std::list<weak_ptr<session> > _sessions;
strong_ptr<proxy> _pr;

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <cstdio>
#include <cstdarg>
#include <syslog.h>
#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

View File

@ -16,18 +16,19 @@
#ifndef __NDPPD_LOG_H
#define __NDPPD_LOG_H
#include <syslog.h>
#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

View File

@ -19,17 +19,40 @@
#include <getopt.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#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;
}
}

View File

@ -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;

View File

@ -32,14 +32,14 @@ class rule;
class proxy
{
private:
weak_ptr<proxy> _ptr;
strong_ptr<iface> _ifa;
std::list<strong_ptr<rule> > _rules;
std::list<strong_ptr<session> > _sessions;
weak_ptr<proxy> _ptr;
proxy();
public:

View File

@ -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<strong_ptr<iface> >::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();
}