2011-09-13 21:26:12 +02:00
|
|
|
// ndppd - NDP Proxy Daemon
|
2012-01-26 11:21:07 +01:00
|
|
|
// Copyright (C) 2011 Daniel Adolfsson <daniel@priv.nu>
|
2011-09-13 21:26:12 +02:00
|
|
|
//
|
|
|
|
// 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 <iostream>
|
2011-09-18 15:22:30 +02:00
|
|
|
#include <fstream>
|
2011-09-13 21:26:12 +02:00
|
|
|
#include <string>
|
2012-01-31 23:53:45 +01:00
|
|
|
#include <memory>
|
2011-09-13 21:26:12 +02:00
|
|
|
|
|
|
|
#include <getopt.h>
|
2011-09-14 10:53:21 +02:00
|
|
|
#include <sys/time.h>
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2011-09-17 16:37:24 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-09-13 21:26:12 +02:00
|
|
|
#include "ndppd.h"
|
|
|
|
|
|
|
|
using namespace ndppd;
|
|
|
|
|
2011-09-17 16:37:24 +02:00
|
|
|
int daemonize()
|
|
|
|
{
|
2012-01-28 20:25:57 +01:00
|
|
|
pid_t pid = fork();
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
if (pid < 0)
|
|
|
|
return -1;
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
if (pid > 0)
|
|
|
|
exit(0);
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
pid_t sid = setsid();
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
if (sid < 0)
|
|
|
|
return -1;
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
close(STDIN_FILENO);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
return 0;
|
2011-09-17 16:37:24 +02:00
|
|
|
}
|
|
|
|
|
2012-01-31 23:53:45 +01:00
|
|
|
bool configure(const std::string &path)
|
|
|
|
{
|
|
|
|
std::shared_ptr<conf> cf;
|
|
|
|
|
|
|
|
if (!(cf = conf::load(path)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<conf> >::const_iterator p_it;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<conf> > proxies(cf->find("proxy"));
|
|
|
|
|
|
|
|
for (p_it = proxies.begin(); p_it != proxies.end(); p_it++) {
|
|
|
|
std::shared_ptr<conf> pr_cf = *p_it, x_cf;
|
|
|
|
|
|
|
|
if (pr_cf->value() == "") {
|
|
|
|
logger::error() << "'proxy' section is missing interface name";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<proxy> pr = proxy::open(pr_cf->value());
|
|
|
|
|
|
|
|
if (!pr) {
|
|
|
|
logger::error() << "Configuration failed for proxy '" << pr_cf->value() << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(x_cf = (*pr_cf)["router"]))
|
|
|
|
pr->router(true);
|
|
|
|
else
|
|
|
|
pr->router(x_cf->bool_value());
|
|
|
|
|
|
|
|
if (!(x_cf = (*pr_cf)["ttl"]))
|
|
|
|
pr->ttl(30000);
|
|
|
|
else
|
|
|
|
pr->ttl(x_cf->int_value());
|
|
|
|
|
|
|
|
if (!(x_cf = (*pr_cf)["timeout"]))
|
|
|
|
pr->timeout(500);
|
|
|
|
else
|
|
|
|
pr->timeout(x_cf->int_value());
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<conf> >::const_iterator r_it;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<conf> > rules(pr_cf->find("rule"));
|
|
|
|
|
|
|
|
for (r_it = rules.begin(); r_it != rules.end(); r_it++) {
|
|
|
|
std::shared_ptr<conf> ru_cf = *r_it;
|
|
|
|
|
|
|
|
if (ru_cf->value() == "") {
|
|
|
|
logger::error() << "'rule' is missing an IPv6 address/net";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
address addr(ru_cf->value());
|
|
|
|
|
|
|
|
if (!(x_cf = (*ru_cf)["iface"])) {
|
|
|
|
if (addr.prefix() <= 120) {
|
|
|
|
logger::warning() << "Static rule prefix /" << addr.prefix() << " <= 120 - is this what you want?";
|
|
|
|
pr->add_rule(addr);
|
|
|
|
}
|
|
|
|
} else if (x_cf->value() == "") {
|
|
|
|
logger::error() << "'iface' expected an interface name or 'auto' as argument";
|
|
|
|
} else {
|
|
|
|
pr->add_rule(addr, iface::open_ifd(x_cf->value()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-13 21:26:12 +02:00
|
|
|
int main(int argc, char *argv[], char *env[])
|
|
|
|
{
|
2012-01-28 20:25:57 +01:00
|
|
|
std::string config_path("/etc/ndppd.conf");
|
|
|
|
std::string pidfile;
|
2012-01-31 20:17:19 +01:00
|
|
|
std::string verbosity;
|
2012-01-28 20:25:57 +01:00
|
|
|
bool daemon = false;
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
while (1) {
|
|
|
|
int c, opt;
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
2012-01-31 20:17:19 +01:00
|
|
|
{ "config", 1, 0, 'c' },
|
|
|
|
{ "daemon", 0, 0, 'd' },
|
|
|
|
{ "verbose", 1, 0, 'v' },
|
2012-01-28 20:25:57 +01:00
|
|
|
{ 0, 0, 0, 0}
|
|
|
|
};
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-31 20:17:19 +01:00
|
|
|
c = getopt_long(argc, argv, "c:dp:v::", long_options, &opt);
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
config_path = optarg;
|
|
|
|
break;
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
case 'd':
|
|
|
|
daemon = true;
|
|
|
|
break;
|
2011-09-18 15:22:30 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
case 'p':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
2012-01-31 20:17:19 +01:00
|
|
|
|
|
|
|
case 'v':
|
|
|
|
if (optarg) {
|
|
|
|
if (!logger::verbosity(optarg))
|
|
|
|
logger::error() << "Unknown verbosity level '" << optarg << "'";
|
|
|
|
} else {
|
|
|
|
logger::max_pri(LOG_INFO);
|
|
|
|
}
|
|
|
|
break;
|
2012-01-28 20:25:57 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
if (daemon) {
|
2012-01-31 20:17:19 +01:00
|
|
|
logger::syslog(true);
|
2011-09-17 16:37:24 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
if (daemonize() < 0) {
|
2012-01-31 20:17:19 +01:00
|
|
|
logger::error() << "Failed to daemonize process";
|
2012-01-28 20:25:57 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
if (!pidfile.empty()) {
|
|
|
|
std::ofstream pf;
|
|
|
|
pf.open(pidfile.c_str(), std::ios::out | std::ios::trunc);
|
|
|
|
pf << getpid() << std::endl;
|
|
|
|
pf.close();
|
|
|
|
}
|
2011-09-18 15:22:30 +02:00
|
|
|
|
2012-01-31 20:17:19 +01:00
|
|
|
logger::info().force_log()
|
|
|
|
<< "ndppd (NDP Proxy Daemon) version " NDPPD_VERSION << logger::endl
|
|
|
|
<< "Using configuration file '" << config_path << "'";
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-31 23:53:45 +01:00
|
|
|
// Load configuration.
|
|
|
|
|
|
|
|
if (!configure(config_path))
|
2012-01-28 20:25:57 +01:00
|
|
|
return -1;
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-31 23:53:45 +01:00
|
|
|
// Time stuff.
|
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
struct timeval t1, t2;
|
2011-09-14 10:53:21 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
gettimeofday(&t1, 0);
|
2011-09-14 10:53:21 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
while (iface::poll_all() >= 0) {
|
|
|
|
int elapsed_time;
|
|
|
|
gettimeofday(&t2, 0);
|
2011-09-14 10:53:21 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
elapsed_time =
|
|
|
|
((t2.tv_sec - t1.tv_sec) * 1000) +
|
|
|
|
((t2.tv_usec - t1.tv_usec) / 1000);
|
2011-09-14 10:53:21 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
t1.tv_sec = t2.tv_sec;
|
|
|
|
t1.tv_usec = t2.tv_usec;
|
2011-09-14 10:53:21 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
session::update_all(elapsed_time);
|
|
|
|
}
|
2011-09-13 21:26:12 +02:00
|
|
|
|
2012-01-31 20:17:19 +01:00
|
|
|
logger::error() << "iface::poll_all() failed";
|
2011-09-17 01:10:23 +02:00
|
|
|
|
2012-01-28 20:25:57 +01:00
|
|
|
return 0;
|
2011-09-13 21:26:12 +02:00
|
|
|
}
|
|
|
|
|