IBR-DTNSuite 0.6

daemon/src/Configuration.cpp

Go to the documentation of this file.
00001 #include "config.h"
00002 #include "Configuration.h"
00003 #include "net/DiscoveryAnnouncement.h"
00004 #include "net/DiscoveryAnnouncement.h"
00005 #include "core/Node.h"
00006 
00007 #include <ibrdtn/utils/Utils.h>
00008 #include <ibrdtn/utils/Clock.h>
00009 
00010 #include <ibrcommon/net/vinterface.h>
00011 #include <ibrcommon/Logger.h>
00012 
00013 #include <getopt.h>
00014 
00015 #ifdef __DEVELOPMENT_ASSERTIONS__
00016 #include <cassert>
00017 #endif
00018 
00019 using namespace dtn::net;
00020 using namespace dtn::core;
00021 using namespace dtn::utils;
00022 using namespace ibrcommon;
00023 
00024 namespace dtn
00025 {
00026         namespace daemon
00027         {
00028                 Configuration::NetConfig::NetConfig(std::string n, NetType t, const std::string &u, bool d)
00029                  : name(n), type(t), url(u), port(0), discovery(d)
00030                 {
00031                 }
00032 
00033                 Configuration::NetConfig::NetConfig(std::string n, NetType t, const ibrcommon::vinterface &i, int p, bool d)
00034                  : name(n), type(t), interface(i), port(p), discovery(d)
00035                 {
00036                 }
00037 
00038                 Configuration::NetConfig::NetConfig(std::string n, NetType t, const ibrcommon::vaddress &a, int p, bool d)
00039                  : name(n), type(t), interface(), address(a), port(p), discovery(d)
00040                 {
00041                 }
00042 
00043                 Configuration::NetConfig::NetConfig(std::string n, NetType t, int p, bool d)
00044                  : name(n), type(t), interface(), port(p), discovery(d)
00045                 {
00046                 }
00047 
00048                 Configuration::NetConfig::~NetConfig()
00049                 {
00050                 }
00051 
00052                 std::string Configuration::version()
00053                 {
00054                         std::stringstream ss;
00055                         ss << PACKAGE_VERSION;
00056                 #ifdef SVN_REVISION
00057                         ss << " (build " << SVN_REVISION << ")";
00058                 #endif
00059 
00060                         return ss.str();
00061                 }
00062 
00063                 Configuration::Configuration()
00064                  : _filename("config.ini"), _doapi(true)
00065                 {
00066                 }
00067 
00068                 Configuration::~Configuration()
00069                 {}
00070 
00071                 Configuration::Discovery::Discovery()
00072                  : _enabled(true), _timeout(5) {};
00073 
00074                 Configuration::Statistic::Statistic() {};
00075 
00076                 Configuration::Debug::Debug()
00077                  : _enabled(false), _quiet(false), _level(0) {};
00078 
00079                 Configuration::Logger::Logger()
00080                  : _quiet(false), _options(0), _timestamps(false) {};
00081 
00082                 Configuration::Network::Network()
00083                  : _routing("default"), _forwarding(true), _tcp_nodelay(true), _tcp_chunksize(1024), _default_net("lo"), _use_default_net(false), _auto_connect(0) {};
00084 
00085                 Configuration::Security::Security()
00086                  : _enabled(false), _tlsEnabled(false), _tlsRequired(false)
00087                 {};
00088 
00089                 Configuration::Daemon::Daemon()
00090                  : _daemonize(false), _kill(false)
00091                 {};
00092 
00093                 Configuration::Discovery::~Discovery() {};
00094                 Configuration::Statistic::~Statistic() {};
00095                 Configuration::Debug::~Debug() {};
00096                 Configuration::Logger::~Logger() {};
00097                 Configuration::Network::~Network() {};
00098                 Configuration::Daemon::~Daemon() {};
00099 
00100                 const Configuration::Discovery& Configuration::getDiscovery() const
00101                 {
00102                         return _disco;
00103                 }
00104 
00105                 const Configuration::Statistic& Configuration::getStatistic() const
00106                 {
00107                         return _stats;
00108                 }
00109 
00110                 const Configuration::Debug& Configuration::getDebug() const
00111                 {
00112                         return _debug;
00113                 }
00114 
00115                 const Configuration::Logger& Configuration::getLogger() const
00116                 {
00117                         return _logger;
00118                 }
00119 
00120                 const Configuration::Network& Configuration::getNetwork() const
00121                 {
00122                         return _network;
00123                 }
00124 
00125                 const Configuration::Security& Configuration::getSecurity() const
00126                 {
00127                         return _security;
00128                 }
00129 
00130                 const Configuration::Daemon& Configuration::getDaemon() const
00131                 {
00132                         return _daemon;
00133                 }
00134 
00135                 Configuration& Configuration::getInstance()
00136                 {
00137                         static Configuration conf;
00138                         return conf;
00139                 }
00140 
00141                 void Configuration::params(int argc, char *argv[])
00142                 {
00143                         int c;
00144                         int doapi = _doapi;
00145                         int disco = _disco._enabled;
00146                         int badclock = dtn::utils::Clock::badclock;
00147                         int timestamp = _logger._timestamps;
00148 
00149                         while (1)
00150                         {
00151                                 static struct option long_options[] =
00152                                 {
00153                                                 /* These options set a flag. */
00154                                                 {"noapi", no_argument, &doapi, 0},
00155                                                 {"nodiscovery", no_argument, &disco, 0},
00156                                                 {"badclock", no_argument, &badclock, 1},
00157                                                 {"timestamp", no_argument, &timestamp, 1},
00158 
00159                                                 /* These options don't set a flag. We distinguish them by their indices. */
00160                                                 {"help", no_argument, 0, 'h'},
00161 #ifdef HAVE_LIBDAEMON
00162                                                 {"daemon", no_argument, 0, 'D'},
00163                                                 {"kill", no_argument, 0, 'k'},
00164                                                 {"pidfile", required_argument, 0, 'p'},
00165 #endif
00166 
00167                                                 {"quiet", no_argument, 0, 'q'},
00168                                                 {"version", no_argument, 0, 'v'},
00169                                                 {"interface", required_argument, 0, 'i'},
00170                                                 {"configuration", required_argument, 0, 'c'},
00171                                                 {"debug", required_argument, 0, 'd'},
00172                                                 {0, 0, 0, 0}
00173                                 };
00174 
00175                                 /* getopt_long stores the option index here. */
00176                                 int option_index = 0;
00177 
00178 #ifdef HAVE_LIBDAEMON
00179                                 c = getopt_long (argc, argv, "qhDkp:vi:c:d:",
00180                                                 long_options, &option_index);
00181 #else
00182                                 c = getopt_long (argc, argv, "qhvi:c:d:",
00183                                                 long_options, &option_index);
00184 #endif
00185 
00186                                 /* Detect the end of the options. */
00187                                 if (c == -1)
00188                                         break;
00189 
00190                                 switch (c)
00191                                 {
00192                                 case 0:
00193                                         /* If this option set a flag, do nothing else now. */
00194                                         if (long_options[option_index].flag != 0)
00195                                                 break;
00196                                         printf ("option %s", long_options[option_index].name);
00197                                         if (optarg)
00198                                                 printf (" with arg %s", optarg);
00199                                         printf ("\n");
00200                                         break;
00201 
00202                                 case 'h':
00203                                         std::cout << "IBR-DTN version: " << version() << std::endl;
00204                                         std::cout << "Syntax: dtnd [options]"  << std::endl;
00205                                         std::cout << " -h|--help       display this text" << std::endl;
00206                                         std::cout << " -c <file>       set a configuration file" << std::endl;
00207 #ifdef HAVE_LIBDAEMON
00208                                         std::cout << " -D              daemonize the process" << std::endl;
00209                                         std::cout << " -k              stop the running daemon" << std::endl;
00210                                         std::cout << " -p <file>       store the pid in this pidfile" << std::endl;
00211 #endif
00212                                         std::cout << " -i <interface>  interface to bind on (e.g. eth0)" << std::endl;
00213                                         std::cout << " -d <level>      enable debugging and set a verbose level" << std::endl;
00214                                         std::cout << " -q              enables the quiet mode (no logging to the console)" << std::endl;
00215                                         std::cout << " --noapi         disable API module" << std::endl;
00216                                         std::cout << " --nodiscovery   disable discovery module" << std::endl;
00217                                         std::cout << " --badclock      assume a bad clock on the system (use AgeBlock)" << std::endl;
00218                                         std::cout << " --timestamp     enables timestamps for logging instead of datetime values" << std::endl;
00219                                         exit(0);
00220                                         break;
00221 
00222                                 case 'v':
00223                                         std::cout << "IBR-DTN version: " << version() << std::endl;
00224                                         exit(0);
00225                                         break;
00226 
00227                                 case 'q':
00228                                         _debug._quiet = true;
00229                                         break;
00230 
00231                                 case 'c':
00232                                         _filename = optarg;
00233                                         break;
00234 
00235                                 case 'i':
00236                                         _network._default_net = ibrcommon::vinterface(optarg);
00237                                         _network._use_default_net = true;
00238                                         break;
00239 
00240                                 case 'd':
00241                                         _debug._enabled = true;
00242                                         _debug._level = atoi(optarg);
00243                                         break;
00244 
00245                                 case 'D':
00246                                         _daemon._daemonize = true;
00247                                         _debug._quiet = true;
00248                                         break;
00249 
00250                                 case 'k':
00251                                         _daemon._daemonize = true;
00252                                         _daemon._kill = true;
00253                                         break;
00254 
00255                                 case 'p':
00256                                         _daemon._pidfile = std::string(optarg);
00257                                         break;
00258 
00259                                 case '?':
00260                                         /* getopt_long already printed an error message. */
00261                                         break;
00262 
00263                                 default:
00264                                         abort ();
00265                                 }
00266                         }
00267 
00268                         _doapi = doapi;
00269                         _disco._enabled = disco;
00270                         dtn::utils::Clock::badclock = badclock;
00271                         _logger._timestamps = timestamp;
00272                 }
00273 
00274                 void Configuration::load()
00275                 {
00276                         load(_filename);
00277                 }
00278 
00279                 void Configuration::load(string filename)
00280                 {
00281                         try {
00282                                 // load main configuration
00283                                 _conf = ibrcommon::ConfigFile(filename);
00284                                 _filename = filename;
00285 
00286                                 IBRCOMMON_LOGGER(info) << "Configuration: " << filename << IBRCOMMON_LOGGER_ENDL;
00287                         } catch (const ibrcommon::ConfigFile::file_not_found&) {
00288                                 IBRCOMMON_LOGGER(info) << "Using default settings. Call with --help for options." << IBRCOMMON_LOGGER_ENDL;
00289                                 _conf = ConfigFile();
00290 
00291                                 // set the default user to nobody
00292                                 _conf.add<std::string>("user", "nobody");
00293                         }
00294 
00295                         // load all configuration extensions
00296                         _disco.load(_conf);
00297                         _stats.load(_conf);
00298                         _debug.load(_conf);
00299                         _logger.load(_conf);
00300                         _network.load(_conf);
00301                         _security.load(_conf);
00302                 }
00303 
00304                 void Configuration::Discovery::load(const ibrcommon::ConfigFile &conf)
00305                 {
00306                         _timeout = conf.read<unsigned int>("discovery_timeout", 5);
00307                 }
00308 
00309                 void Configuration::Statistic::load(const ibrcommon::ConfigFile&)
00310                 {
00311                 }
00312 
00313                 void Configuration::Logger::load(const ibrcommon::ConfigFile &conf)
00314                 {
00315                         try {
00316                                 _logfile = conf.read<std::string>("logfile");
00317                         } catch (const ibrcommon::ConfigFile::key_not_found&) {
00318                         }
00319                 }
00320 
00321                 void Configuration::Debug::load(const ibrcommon::ConfigFile&)
00322                 {
00323                 }
00324 
00325                 void Configuration::Daemon::load(const ibrcommon::ConfigFile&)
00326                 {
00327                 }
00328 
00329                 bool Configuration::Debug::quiet() const
00330                 {
00331                         return _quiet;
00332                 }
00333 
00334                 bool Configuration::Debug::enabled() const
00335                 {
00336                         return _enabled;
00337                 }
00338 
00339                 int Configuration::Debug::level() const
00340                 {
00341                         return _level;
00342                 }
00343 
00344                 string Configuration::getNodename()
00345                 {
00346                         try {
00347                                 return _conf.read<string>("local_uri");
00348                         } catch (const ibrcommon::ConfigFile::key_not_found&) {
00349                                 char *hostname_array = new char[64];
00350                                 if ( gethostname(hostname_array, 64) != 0 )
00351                                 {
00352                                         // error
00353                                         delete[] hostname_array;
00354                                         return "local";
00355                                 }
00356 
00357                                 string hostname(hostname_array);
00358                                 delete[] hostname_array;
00359 
00360                                 stringstream ss;
00361                                 ss << "dtn://" << hostname;
00362                                 ss >> hostname;
00363 
00364                                 return hostname;
00365                         }
00366                 }
00367 
00368                 const std::list<Configuration::NetConfig>& Configuration::Network::getInterfaces() const
00369                 {
00370                         return _interfaces;
00371                 }
00372 
00373                 const ibrcommon::vaddress Configuration::Discovery::address() const throw (ParameterNotFoundException)
00374                 {
00375                         try {
00376                                 return ibrcommon::vaddress( ibrcommon::vaddress::VADDRESS_INET,
00377                                                 Configuration::getInstance()._conf.read<string>("discovery_address"));
00378                         } catch (const ConfigFile::key_not_found&) {
00379                                 throw ParameterNotFoundException();
00380                         }
00381                 }
00382 
00383                 int Configuration::Discovery::port() const
00384                 {
00385                         return Configuration::getInstance()._conf.read<int>("discovery_port", 4551);
00386                 }
00387 
00388                 unsigned int Configuration::Discovery::timeout() const
00389                 {
00390                         return _timeout;
00391                 }
00392 
00393                 Configuration::NetConfig Configuration::getAPIInterface()
00394                 {
00395                         size_t port = 4550;
00396                         std::string interface_name = "lo";
00397 
00398                         try {
00399                                 port = _conf.read<size_t>("api_port");
00400                         } catch (const ConfigFile::key_not_found&) { };
00401 
00402                         try {
00403                                 interface_name = _conf.read<std::string>("api_interface");
00404                         } catch (const ConfigFile::key_not_found&) { };
00405 
00406                         if (interface_name == "any")
00407                         {
00408                                 return Configuration::NetConfig("api", Configuration::NetConfig::NETWORK_TCP, ibrcommon::vinterface(), port);
00409                         }
00410 
00411                         return Configuration::NetConfig("api", Configuration::NetConfig::NETWORK_TCP, ibrcommon::vinterface(interface_name), port);
00412                 }
00413 
00414                 ibrcommon::File Configuration::getAPISocket()
00415                 {
00416                         try {
00417                                 return ibrcommon::File(_conf.read<std::string>("api_socket"));
00418                         } catch (const ConfigFile::key_not_found&) {
00419                                 throw ParameterNotSetException();
00420                         }
00421 
00422                         throw ParameterNotSetException();
00423                 }
00424 
00425                 std::string Configuration::getStorage() const
00426                 {
00427                         return _conf.read<std::string>("storage", "default");
00428                 }
00429 
00430                 void Configuration::Network::load(const ibrcommon::ConfigFile &conf)
00431                 {
00435                         _static_routes.clear();
00436 
00437                         string key = "route1";
00438                         unsigned int keynumber = 1;
00439 
00440                         while (conf.keyExists( key ))
00441                         {
00442                                 vector<string> route = dtn::utils::Utils::tokenize(" ", conf.read<string>(key, "dtn:none dtn:none"));
00443                                 _static_routes.push_back( dtn::routing::StaticRoutingExtension::StaticRoute( route.front(), route.back() ) );
00444 
00445                                 keynumber++;
00446                                 stringstream ss; ss << "route" << keynumber; ss >> key;
00447                         }
00448 
00452                         // read the node count
00453                         int count = 1;
00454 
00455                         // initial prefix
00456                         std::string prefix = "static1_";
00457 
00458                         while ( conf.keyExists(prefix + "uri") )
00459                         {
00460                                 const dtn::data::EID node_eid( conf.read<std::string>(prefix + "uri", "dtn:none") );
00461 
00462                                 // create a address URI
00463                                 std::stringstream ss;
00464                                 ss << "ip=" << conf.read<std::string>(prefix + "address", "127.0.0.1") << ";port=" << conf.read<unsigned int>(prefix + "port", 4556);
00465 
00466                                 dtn::core::Node::Protocol p = Node::CONN_UNDEFINED;
00467 
00468                                 const std::string protocol = conf.read<std::string>(prefix + "proto", "tcp");
00469                                 if (protocol == "tcp") p = Node::CONN_TCPIP;
00470                                 if (protocol == "udp") p = Node::CONN_UDPIP;
00471                                 if (protocol == "lowpan") p = Node::CONN_LOWPAN;
00472                                 if (protocol == "zigbee") p = Node::CONN_LOWPAN; //Legacy: Stay compatible with older config files
00473                                 if (protocol == "bluetooth") p = Node::CONN_BLUETOOTH;
00474                                 if (protocol == "http") p = Node::CONN_HTTP;
00475 
00476                                 bool node_exists = false;
00477 
00478                                 // get node
00479                                 for (std::list<Node>::iterator iter = _nodes.begin(); iter != _nodes.end(); iter++)
00480                                 {
00481                                         dtn::core::Node &n = (*iter);
00482 
00483                                         if (n.getEID() == node_eid)
00484                                         {
00485                                                 n.add( dtn::core::Node::URI( dtn::core::Node::NODE_STATIC, p, ss.str() ) );
00486                                                 n.setConnectImmediately( conf.read<std::string>(prefix + "immediately", "no") == "yes" );
00487                                                 node_exists = true;
00488                                                 break;
00489                                         }
00490                                 }
00491 
00492                                 if (!node_exists)
00493                                 {
00494                                         Node n(node_eid);
00495                                         n.add( dtn::core::Node::URI( dtn::core::Node::NODE_STATIC, p, ss.str() ) );
00496                                         n.setConnectImmediately( conf.read<std::string>(prefix + "immediately", "no") == "yes" );
00497                                         _nodes.push_back(n);
00498                                 }
00499 
00500                                 count++;
00501 
00502                                 std::stringstream prefix_stream;
00503                                 prefix_stream << "static" << count << "_";
00504                                 prefix = prefix_stream.str();
00505                         }
00506 
00510                         _routing = conf.read<string>("routing", "default");
00511 
00515                         _forwarding = (conf.read<std::string>("routing_forwarding", "yes") == "yes");
00516 
00520                         _interfaces.clear();
00521 
00522                         if (_use_default_net)
00523                         {
00524                                 _interfaces.push_back( Configuration::NetConfig("default", Configuration::NetConfig::NETWORK_TCP, _default_net, 4556) );
00525                         }
00526                         else try
00527                         {
00528                                 vector<string> nets = dtn::utils::Utils::tokenize(" ", conf.read<string>("net_interfaces") );
00529                                 for (vector<string>::const_iterator iter = nets.begin(); iter != nets.end(); iter++)
00530                                 {
00531                                         std::string netname = (*iter);
00532 
00533                                         std::string key_type = "net_" + netname + "_type";
00534                                         std::string key_port = "net_" + netname + "_port";
00535                                         std::string key_interface = "net_" + netname + "_interface";
00536                                         std::string key_address = "net_" + netname + "_address";
00537                                         std::string key_discovery = "net_" + netname + "_discovery";
00538 
00539                                         std::string type_name = conf.read<string>(key_type, "tcp");
00540                                         Configuration::NetConfig::NetType type = Configuration::NetConfig::NETWORK_UNKNOWN;
00541 
00542                                         if (type_name == "tcp") type = Configuration::NetConfig::NETWORK_TCP;
00543                                         if (type_name == "udp") type = Configuration::NetConfig::NETWORK_UDP;
00544                                         if (type_name == "http") type = Configuration::NetConfig::NETWORK_HTTP;
00545                                         if (type_name == "lowpan") type = Configuration::NetConfig::NETWORK_LOWPAN;
00546 
00547                                         switch (type)
00548                                         {
00549                                                 case Configuration::NetConfig::NETWORK_HTTP:
00550                                                 {
00551                                                         Configuration::NetConfig nc(netname, type,
00552                                                                         conf.read<std::string>(key_address, "http://localhost/"),
00553                                                                         conf.read<std::string>(key_discovery, "yes") == "no");
00554 
00555                                                         _interfaces.push_back(nc);
00556                                                         break;
00557                                                 }
00558 
00559                                                 default:
00560                                                 {
00561                                                         int port = conf.read<int>(key_port, 4556);
00562                                                         bool discovery = (conf.read<std::string>(key_discovery, "yes") == "yes");
00563 
00564                                                         try {
00565                                                                 ibrcommon::vinterface interface(conf.read<std::string>(key_interface));
00566                                                                 Configuration::NetConfig nc(netname, type, interface, port, discovery);
00567                                                                 _interfaces.push_back(nc);
00568                                                         } catch (const ConfigFile::key_not_found&) {
00569                                                                 ibrcommon::vaddress addr;
00570                                                                 Configuration::NetConfig nc(netname, type, addr, port, discovery);
00571                                                                 _interfaces.push_back(nc);
00572                                                         }
00573 
00574                                                         break;
00575                                                 }
00576                                         }
00577                                 }
00578                         } catch (const ConfigFile::key_not_found&) {
00579                                 // stop the one network is not found.
00580                         }
00581 
00585                         _tcp_nodelay = (conf.read<std::string>("tcp_nodelay", "yes") == "yes");
00586                         _tcp_chunksize = conf.read<unsigned int>("tcp_chunksize", 1024);
00587 
00591                         _dynamic_rebind = (conf.read<std::string>("net_rebind", "no") == "yes");
00592 
00596                         _auto_connect = conf.read<size_t>("net_autoconnect", 0);
00597                 }
00598 
00599                 const list<dtn::routing::StaticRoutingExtension::StaticRoute>& Configuration::Network::getStaticRoutes() const
00600                 {
00601                         return _static_routes;
00602                 }
00603 
00604                 const std::list<Node>& Configuration::Network::getStaticNodes() const
00605                 {
00606                         return _nodes;
00607                 }
00608 
00609                 int Configuration::getTimezone()
00610                 {
00611                         return _conf.read<int>( "timezone", 0 );
00612                 }
00613 
00614                 ibrcommon::File Configuration::getPath(string name)
00615                 {
00616                         stringstream ss;
00617                         ss << name << "_path";
00618                         string key; ss >> key;
00619 
00620                         try {
00621                                 return ibrcommon::File(_conf.read<string>(key));
00622                         } catch (const ConfigFile::key_not_found&) {
00623                                 throw ParameterNotSetException();
00624                         }
00625                 }
00626 
00627                 const std::string Configuration::getUser() const
00628                 {
00629                         try {
00630                                 return _conf.read<std::string>("user");
00631                         } catch (const ConfigFile::key_not_found&) {
00632                                 throw ParameterNotSetException();
00633                         }
00634                 }
00635 
00636                 unsigned int Configuration::getUID() const
00637                 {
00638                         try {
00639                                 return _conf.read<unsigned int>("uid");
00640                         } catch (const ConfigFile::key_not_found&) {
00641                                 throw ParameterNotSetException();
00642                         }
00643                 }
00644 
00645                 unsigned int Configuration::getGID() const
00646                 {
00647                         try {
00648                                 return _conf.read<unsigned int>("gid");
00649                         } catch (const ConfigFile::key_not_found&) {
00650                                 throw ParameterNotSetException();
00651                         }
00652                 }
00653 
00654 
00655                 bool Configuration::Discovery::enabled() const
00656                 {
00657                         return _enabled;
00658                 }
00659 
00660                 bool Configuration::Discovery::announce() const
00661                 {
00662                         return (Configuration::getInstance()._conf.read<int>("discovery_announce", 1) == 1);
00663                 }
00664 
00665                 bool Configuration::Discovery::shortbeacon() const
00666                 {
00667                         return (Configuration::getInstance()._conf.read<int>("discovery_short", 0) == 1);
00668                 }
00669 
00670                 char Configuration::Discovery::version() const
00671                 {
00672                         return Configuration::getInstance()._conf.read<int>("discovery_version", 2);
00673                 }
00674 
00675                 bool Configuration::doAPI()
00676                 {
00677                         return _doapi;
00678                 }
00679 
00680                 string Configuration::getNotifyCommand()
00681                 {
00682                         try {
00683                                 return _conf.read<string>("notify_cmd");
00684                         } catch (const ConfigFile::key_not_found&) {
00685                                 throw ParameterNotSetException();
00686                         }
00687                 }
00688 
00689                 Configuration::RoutingExtension Configuration::Network::getRoutingExtension() const
00690                 {
00691                         if ( _routing == "epidemic" ) return EPIDEMIC_ROUTING;
00692                         if ( _routing == "flooding" ) return FLOOD_ROUTING;
00693                         return DEFAULT_ROUTING;
00694                 }
00695 
00696 
00697                 bool Configuration::Network::doForwarding() const
00698                 {
00699                         return _forwarding;
00700                 }
00701 
00702                 bool Configuration::Network::getTCPOptionNoDelay() const
00703                 {
00704                         return _tcp_nodelay;
00705                 }
00706 
00707                 size_t Configuration::Network::getTCPChunkSize() const
00708                 {
00709                         return _tcp_chunksize;
00710                 }
00711 
00712                 bool Configuration::Network::doDynamicRebind() const
00713                 {
00714                         return _dynamic_rebind;
00715                 }
00716 
00717                 size_t Configuration::Network::getAutoConnect() const
00718                 {
00719                         return _auto_connect;
00720                 }
00721 
00722                 bool Configuration::Statistic::enabled() const
00723                 {
00724                         return Configuration::getInstance()._conf.keyExists("statistic_type");
00725                 }
00726 
00727                 ibrcommon::File Configuration::Statistic::logfile() const throw (ParameterNotSetException)
00728                 {
00729                         try {
00730                                 return ibrcommon::File(Configuration::getInstance()._conf.read<std::string>("statistic_file"));
00731                         } catch (const ConfigFile::key_not_found&) {
00732                                 throw ParameterNotSetException();
00733                         }
00734                 }
00735 
00736                 std::string Configuration::Statistic::type() const
00737                 {
00738                         return Configuration::getInstance()._conf.read<std::string>("statistic_type", "stdout");
00739                 }
00740 
00741                 unsigned int Configuration::Statistic::interval() const
00742                 {
00743                         return Configuration::getInstance()._conf.read<unsigned int>("statistic_interval", 300);
00744                 }
00745 
00746                 std::string Configuration::Statistic::address() const
00747                 {
00748                         return Configuration::getInstance()._conf.read<std::string>("statistic_address", "127.0.0.1");
00749                 }
00750 
00751                 unsigned int Configuration::Statistic::port() const
00752                 {
00753                         return Configuration::getInstance()._conf.read<unsigned int>("statistic_port", 1234);
00754                 }
00755 
00756                 size_t Configuration::getLimit(std::string suffix)
00757                 {
00758                         std::string unparsed = _conf.read<std::string>("limit_" + suffix, "0");
00759 
00760                         std::stringstream ss(unparsed);
00761 
00762                         float value; ss >> value;
00763                         char multiplier = 0; ss >> multiplier;
00764 
00765                         switch (multiplier)
00766                         {
00767                         default:
00768                                 return (size_t)value;
00769                                 break;
00770 
00771                         case 'G':
00772                                 return (size_t)(value * 1000000000);
00773                                 break;
00774 
00775                         case 'M':
00776                                 return (size_t)(value * 1000000);
00777                                 break;
00778 
00779                         case 'K':
00780                                 return (size_t)(value * 1000);
00781                                 break;
00782                         }
00783 
00784                         return 0;
00785                 }
00786 
00787                 void Configuration::Security::load(const ibrcommon::ConfigFile &conf)
00788                 {
00789                         bool withTLS = false;
00790 #ifdef WITH_TLS
00791                         withTLS = true;
00792                         /* enable TLS if the certificate path, a certificate and the private key was given */
00793                         bool activateTLS = true;
00794 
00795                         // load CA file
00796                         try {
00797                                 _ca = conf.read<std::string>("security_ca");
00798 
00799                                 if (!_ca.exists())
00800                                 {
00801                                         IBRCOMMON_LOGGER(warning) << "CA file " << _ca.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
00802                                         activateTLS = false;
00803                                 }
00804                         } catch (const ibrcommon::ConfigFile::key_not_found&) {
00805                                 activateTLS = false;
00806                         }
00807 
00808                         // load KEY file
00809                         try {
00810                                 _key = conf.read<std::string>("security_key");
00811 
00812                                 if (!_key.exists())
00813                                 {
00814                                         IBRCOMMON_LOGGER(warning) << "KEY file " << _key.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
00815                                         activateTLS = false;
00816                                 }
00817                         } catch (const ibrcommon::ConfigFile::key_not_found&) {
00818                                 activateTLS = false;
00819                         }
00820 
00821                         // read trustedCAPath
00822                         try{
00823                                 _trustedCAPath = conf.read<std::string>("security_trusted_ca_path");
00824                                 if(!_trustedCAPath.isDirectory()){
00825                                         IBRCOMMON_LOGGER(warning) << "Trusted CA Path " << _trustedCAPath.getPath() << " does not exists or is no directory!" << IBRCOMMON_LOGGER_ENDL;
00826                                         activateTLS = false;
00827                                 }
00828                         } catch (const ibrcommon::ConfigFile::key_not_found&) {
00829                                 activateTLS = false;
00830                         }
00831 
00832                         // read if encryption should be disabled
00833                         _disableEncryption = (conf.read<std::string>("security_tls_disable_encryption", "no") == "yes");
00834 
00835                         if (activateTLS)
00836                         {
00837                                 _tlsEnabled = true;
00838 
00839                                 /* read if TLS is required */
00840                                 _tlsRequired = (conf.read<std::string>("security_tls_required", "no") == "yes");
00841                         }
00842 #endif
00843 
00844 #ifdef WITH_BUNDLE_SECURITY
00845                         // enable security if the security path is set
00846                         try {
00847                                 _path = conf.read<std::string>("security_path");
00848 
00849                                 if (!_path.exists())
00850                                 {
00851                                         ibrcommon::File::createDirectory(_path);
00852                                 }
00853 
00854                                 _enabled = true;
00855                         } catch (const ibrcommon::ConfigFile::key_not_found&) {
00856                                 return;
00857                         }
00858 
00859                         // load level
00860                         _level = Level(conf.read<int>("security_level", 0));
00861 
00862                         if ( !withTLS )
00863                         {
00864                                 /* if TLS is enabled, the CA file and the key have been read earlier */
00865                                 // load CA file
00866                                 try {
00867                                         _ca = conf.read<std::string>("security_ca");
00868 
00869                                         if (!_ca.exists())
00870                                         {
00871                                                 IBRCOMMON_LOGGER(warning) << "CA file " << _ca.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
00872                                         }
00873                                 } catch (const ibrcommon::ConfigFile::key_not_found&) { }
00874 
00875                                 // load KEY file
00876                                 try {
00877                                         _key = conf.read<std::string>("security_key");
00878 
00879                                         if (!_key.exists())
00880                                         {
00881                                                 IBRCOMMON_LOGGER(warning) << "KEY file " << _key.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
00882                                         }
00883                                 } catch (const ibrcommon::ConfigFile::key_not_found&) { }
00884                         }
00885 
00886                         // load KEY file
00887                         try {
00888                                 _bab_default_key = conf.read<std::string>("security_bab_default_key");
00889 
00890                                 if (!_bab_default_key.exists())
00891                                 {
00892                                         IBRCOMMON_LOGGER(warning) << "KEY file " << _bab_default_key.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
00893                                 }
00894                         } catch (const ibrcommon::ConfigFile::key_not_found&) {
00895                         }
00896 #endif
00897                 };
00898 
00899                 Configuration::Security::~Security() {};
00900 
00901                 bool Configuration::Security::enabled() const
00902                 {
00903                         return _enabled;
00904                 }
00905 
00906                 bool Configuration::Security::doTLS() const
00907                 {
00908                         return _tlsEnabled;
00909                 }
00910 
00911                 bool Configuration::Security::TLSRequired() const
00912                 {
00913                         return _tlsRequired;
00914                 }
00915 
00916 #ifdef WITH_BUNDLE_SECURITY
00917                 const ibrcommon::File& Configuration::Security::getPath() const
00918                 {
00919                         return _path;
00920                 }
00921 
00922                 Configuration::Security::Level Configuration::Security::getLevel() const
00923                 {
00924                         return _level;
00925                 }
00926 
00927                 const ibrcommon::File& Configuration::Security::getBABDefaultKey() const
00928                 {
00929                         return _bab_default_key;
00930                 }
00931 #endif
00932 #if defined WITH_BUNDLE_SECURITY || defined WITH_TLS
00933                 const ibrcommon::File& Configuration::Security::getCA() const
00934                 {
00935                         return _ca;
00936                 }
00937 
00938                 const ibrcommon::File& Configuration::Security::getKey() const
00939                 {
00940                         return _key;
00941                 }
00942 #endif
00943 #ifdef WITH_TLS
00944                 const ibrcommon::File& Configuration::Security::getTrustedCAPath() const
00945                 {
00946                         return _trustedCAPath;
00947                 }
00948 
00949                 bool Configuration::Security::TLSEncryptionDisabled() const
00950                 {
00951                         return _disableEncryption;
00952                 }
00953 #endif
00954 
00955                 bool Configuration::Logger::quiet() const
00956                 {
00957                         return _quiet;
00958                 }
00959 
00960                 const ibrcommon::File& Configuration::Logger::getLogfile() const
00961                 {
00962                         if (_logfile.getPath() == "") throw Configuration::ParameterNotSetException();
00963                         return _logfile;
00964                 }
00965 
00966                 bool Configuration::Logger::display_timestamps() const
00967                 {
00968                         return _timestamps;
00969                 }
00970 
00971                 unsigned int Configuration::Logger::options() const
00972                 {
00973                         return _options;
00974                 }
00975 
00976                 std::ostream& Configuration::Logger::output() const
00977                 {
00978                         return std::cout;
00979                 }
00980 
00981                 bool Configuration::Daemon::daemonize() const
00982                 {
00983                         return _daemonize;
00984                 }
00985 
00986                 bool Configuration::Daemon::kill_daemon() const
00987                 {
00988                         return _kill;
00989                 }
00990 
00991                 const ibrcommon::File& Configuration::Daemon::getPidFile() const
00992                 {
00993                         if (_pidfile == ibrcommon::File()) throw ParameterNotSetException();
00994                         return _pidfile;
00995                 }
00996         }
00997 }