00001 #include "config.h"
00002 #include "Configuration.h"
00003 #include "ibrdtn/utils/Utils.h"
00004 #include "core/Node.h"
00005 #include "ibrcommon/net/NetInterface.h"
00006 #include <ibrcommon/Logger.h>
00007
00008 using namespace dtn::net;
00009 using namespace dtn::core;
00010 using namespace dtn::utils;
00011 using namespace ibrcommon;
00012
00013 namespace dtn
00014 {
00015 namespace daemon
00016 {
00017 Configuration::NetConfig::NetConfig(std::string n, NetType t, const ibrcommon::NetInterface &i, int p, bool d)
00018 : name(n), type(t), interface(i), port(p), discovery(d)
00019 {
00020 }
00021
00022 Configuration::NetConfig::NetConfig(std::string n, NetType t, const std::string &a, int p, bool d)
00023 : name(n), type(t), interface("lo"), address(a), port(p), discovery(d)
00024 {
00025 }
00026
00027 Configuration::NetConfig::~NetConfig()
00028 {
00029 }
00030
00031 std::string Configuration::version()
00032 {
00033 std::stringstream ss;
00034 ss << PACKAGE_VERSION;
00035 #ifdef SVN_REVISION
00036 ss << " (build " << SVN_REVISION << ")";
00037 #endif
00038
00039 return ss.str();
00040 }
00041
00042 Configuration::Configuration()
00043 : _filename("config.ini"), _default_net("lo"), _use_default_net(false), _doapi(true), _dodiscovery(true), _debuglevel(0), _debug(false), _quiet(false)
00044 {
00045 }
00046
00047 Configuration::~Configuration()
00048 {}
00049
00050 Configuration& Configuration::getInstance()
00051 {
00052 static Configuration conf;
00053 return conf;
00054 }
00055
00056 void Configuration::params(int argc, char *argv[])
00057 {
00058 for (int i = 0; i < argc; i++)
00059 {
00060 std::string arg = argv[i];
00061
00062 if (arg == "-c" && argc > i)
00063 {
00064 _filename = argv[i + 1];
00065 }
00066
00067 if (arg == "-i" && argc > i)
00068 {
00069 _default_net = ibrcommon::NetInterface(argv[i + 1]);
00070 _use_default_net = true;
00071 }
00072
00073 if (arg == "--noapi")
00074 {
00075 _doapi = false;
00076 }
00077
00078 if ((arg == "--version") || (arg == "-v"))
00079 {
00080 std::cout << "IBR-DTN version: " << version() << std::endl;
00081 exit(0);
00082 }
00083
00084 if (arg == "--nodiscovery")
00085 {
00086 _dodiscovery = false;
00087 }
00088
00089 if (arg == "-d")
00090 {
00091 _debuglevel = atoi(argv[i + 1]);
00092 _debug = true;
00093 }
00094
00095 if (arg == "-q")
00096 {
00097 _quiet = true;
00098 }
00099 }
00100 }
00101
00102 void Configuration::load()
00103 {
00104 load(_filename);
00105 }
00106
00107 void Configuration::load(string filename)
00108 {
00109 try {
00110 _conf = ibrcommon::ConfigFile(filename);;
00111 IBRCOMMON_LOGGER(info) << "Configuration: " << filename << IBRCOMMON_LOGGER_ENDL;
00112 } catch (ibrcommon::ConfigFile::file_not_found ex) {
00113 IBRCOMMON_LOGGER(info) << "Using defaults. To use custom config file use parameter -c configfile." << IBRCOMMON_LOGGER_ENDL;
00114 _conf = ConfigFile();
00115 }
00116 }
00117
00118 bool Configuration::beQuiet() const
00119 {
00120 return _quiet;
00121 }
00122
00123 bool Configuration::doDebug() const
00124 {
00125 return _debug;
00126 }
00127
00128 int Configuration::getDebugLevel() const
00129 {
00130 return _debuglevel;
00131 }
00132
00133 string Configuration::getNodename()
00134 {
00135 try {
00136 return _conf.read<string>("local_uri");
00137 } catch (ibrcommon::ConfigFile::key_not_found ex) {
00138 char *hostname_array = new char[64];
00139 if ( gethostname(hostname_array, 64) != 0 )
00140 {
00141
00142 delete[] hostname_array;
00143 return "local";
00144 }
00145
00146 string hostname(hostname_array);
00147 delete[] hostname_array;
00148
00149 stringstream ss;
00150 ss << "dtn://" << hostname;
00151 ss >> hostname;
00152
00153 return hostname;
00154 }
00155 }
00156
00157 std::list<Configuration::NetConfig> Configuration::getInterfaces()
00158 {
00159 std::list<NetConfig> ret;
00160
00161 if (_use_default_net)
00162 {
00163 ret.push_back( Configuration::NetConfig("default", Configuration::NetConfig::NETWORK_TCP, _default_net, 4556) );
00164 return ret;
00165 }
00166
00167 try {
00168 vector<string> nets = dtn::utils::Utils::tokenize(" ", _conf.read<string>("net_interfaces") );
00169 for (vector<string>::const_iterator iter = nets.begin(); iter != nets.end(); iter++)
00170 {
00171 std::string netname = (*iter);
00172
00173 std::string key_type = "net_" + netname + "_type";
00174 std::string key_port = "net_" + netname + "_port";
00175 std::string key_interface = "net_" + netname + "_interface";
00176 std::string key_address = "net_" + netname + "_address";
00177 std::string key_discovery = "net_" + netname + "_discovery";
00178
00179 std::string type_name = _conf.read<string>(key_type, "tcp");
00180 Configuration::NetConfig::NetType type = Configuration::NetConfig::NETWORK_UNKNOWN;
00181
00182 if (type_name == "tcp") type = Configuration::NetConfig::NETWORK_TCP;
00183 if (type_name == "udp") type = Configuration::NetConfig::NETWORK_UDP;
00184 if (type_name == "http") type = Configuration::NetConfig::NETWORK_HTTP;
00185
00186 switch (type)
00187 {
00188 case Configuration::NetConfig::NETWORK_HTTP:
00189 {
00190 Configuration::NetConfig::NetConfig nc(netname, type,
00191 _conf.read<std::string>(key_address, "http://localhost/"), 0,
00192 _conf.read<std::string>(key_discovery, "yes") == "no");
00193
00194 ret.push_back(nc);
00195 break;
00196 }
00197
00198 default:
00199 {
00200 Configuration::NetConfig::NetConfig nc(netname, type,
00201 ibrcommon::NetInterface(_conf.read<std::string>(key_interface, "lo")),
00202 _conf.read<unsigned int>(key_port, 4556),
00203 _conf.read<std::string>(key_discovery, "yes") == "yes");
00204
00205 ret.push_back(nc);
00206 break;
00207 }
00208 }
00209 }
00210 } catch (ConfigFile::key_not_found ex) {
00211 return ret;
00212 }
00213
00214 return ret;
00215 }
00216
00217 std::string Configuration::getDiscoveryAddress()
00218 {
00219 try {
00220 return _conf.read<string>("discovery_address");
00221 } catch (ConfigFile::key_not_found ex) {
00222 throw ParameterNotFoundException();
00223 }
00224 }
00225
00226 int Configuration::getDiscoveryPort()
00227 {
00228 return _conf.read<int>("discovery_port", 4551);
00229 }
00230
00231 Configuration::NetConfig Configuration::getAPIInterface()
00232 {
00233 return Configuration::NetConfig("local", Configuration::NetConfig::NETWORK_TCP, ibrcommon::NetInterface("lo"), 4550);
00234 }
00235
00236 list<dtn::routing::StaticRoutingExtension::StaticRoute> Configuration::getStaticRoutes()
00237 {
00238 list<dtn::routing::StaticRoutingExtension::StaticRoute> ret;
00239 string key = "route1";
00240 unsigned int keynumber = 1;
00241
00242 while (_conf.keyExists( key ))
00243 {
00244 vector<string> route = dtn::utils::Utils::tokenize(" ", _conf.read<string>(key, "dtn:none dtn:none"));
00245 ret.push_back( dtn::routing::StaticRoutingExtension::StaticRoute( route.front(), route.back() ) );
00246
00247 keynumber++;
00248 stringstream ss; ss << "route" << keynumber; ss >> key;
00249 }
00250
00251 return ret;
00252 }
00253
00254 list<Node> Configuration::getStaticNodes()
00255 {
00256 std::list<Node> nodes;
00257
00258
00259 int count = 1;
00260
00261
00262 std::string prefix = "static1_";
00263
00264 while ( _conf.keyExists(prefix + "uri") )
00265 {
00266 Node n(Node::NODE_PERMANENT);
00267
00268 n.setAddress( _conf.read<std::string>(prefix + "address", "127.0.0.1") );
00269 n.setPort( _conf.read<unsigned int>(prefix + "port", 4556) );
00270 n.setURI( _conf.read<std::string>(prefix + "uri", "dtn:none") );
00271
00272 std::string protocol = _conf.read<std::string>(prefix + "proto", "tcp");
00273 if (protocol == "tcp") n.setProtocol(Node::CONN_TCPIP);
00274 if (protocol == "udp") n.setProtocol(Node::CONN_UDPIP);
00275 if (protocol == "zigbee") n.setProtocol(Node::CONN_ZIGBEE);
00276 if (protocol == "bluetooth") n.setProtocol(Node::CONN_BLUETOOTH);
00277 if (protocol == "http") n.setProtocol(Node::CONN_HTTP);
00278
00279 count++;
00280
00281 std::stringstream prefix_stream;
00282 prefix_stream << "static" << count << "_";
00283 prefix = prefix_stream.str();
00284
00285 nodes.push_back(n);
00286 }
00287
00288 return nodes;
00289 }
00290
00291 int Configuration::getTimezone()
00292 {
00293 return _conf.read<int>( "timezone", 0 );
00294 }
00295
00296 ibrcommon::File Configuration::getPath(string name)
00297 {
00298 stringstream ss;
00299 ss << name << "_path";
00300 string key; ss >> key;
00301
00302 try {
00303 return ibrcommon::File(_conf.read<string>(key));
00304 } catch (ConfigFile::key_not_found ex) {
00305 throw ParameterNotSetException();
00306 }
00307 }
00308
00309 unsigned int Configuration::getUID()
00310 {
00311 try {
00312 return _conf.read<unsigned int>("user");
00313 } catch (ConfigFile::key_not_found ex) {
00314 throw ParameterNotSetException();
00315 }
00316 }
00317
00318 unsigned int Configuration::getGID()
00319 {
00320 try {
00321 return _conf.read<unsigned int>("group");
00322 } catch (ConfigFile::key_not_found ex) {
00323 throw ParameterNotSetException();
00324 }
00325 }
00326
00327
00328 bool Configuration::doDiscovery()
00329 {
00330 return _dodiscovery;
00331 }
00332
00333 bool Configuration::doAPI()
00334 {
00335 return _doapi;
00336 }
00337
00338 string Configuration::getNotifyCommand()
00339 {
00340 try {
00341 return _conf.read<string>("notify_cmd");
00342 } catch (ConfigFile::key_not_found ex) {
00343 throw ParameterNotSetException();
00344 }
00345 }
00346
00347 Configuration::RoutingExtension Configuration::getRoutingExtension()
00348 {
00349 try {
00350 string mode = _conf.read<string>("routing");
00351 if ( mode == "epidemic" ) return EPIDEMIC_ROUTING;
00352 return DEFAULT_ROUTING;
00353 } catch (ConfigFile::key_not_found ex) {
00354 return DEFAULT_ROUTING;
00355 }
00356 }
00357
00358
00359 bool Configuration::doForwarding()
00360 {
00361 try {
00362 if (_conf.read<std::string>("routing_forwarding") == "yes")
00363 {
00364 return true;
00365 }
00366 else
00367 {
00368 return false;
00369 }
00370 } catch (ConfigFile::key_not_found ex) {
00371 return true;
00372 }
00373 }
00374
00375 bool Configuration::useStatLogger()
00376 {
00377 return _conf.keyExists("statistic_type");
00378 }
00379
00380 ibrcommon::File Configuration::getStatLogfile()
00381 {
00382 try {
00383 return ibrcommon::File(_conf.read<std::string>("statistic_file"));
00384 } catch (ConfigFile::key_not_found ex) {
00385 throw ParameterNotSetException();
00386 }
00387 }
00388
00389 std::string Configuration::getStatLogType()
00390 {
00391 return _conf.read<std::string>("statistic_type", "stdout");
00392 }
00393
00394 unsigned int Configuration::getStatLogInterval()
00395 {
00396 return _conf.read<unsigned int>("statistic_interval", 300);
00397 }
00398
00399 std::string Configuration::getStatAddress()
00400 {
00401 return _conf.read<std::string>("statistic_address", "127.0.0.1");
00402 }
00403
00404 unsigned int Configuration::getStatPort()
00405 {
00406 return _conf.read<unsigned int>("statistic_port", 1234);
00407 }
00408
00409 size_t Configuration::getLimit(std::string suffix)
00410 {
00411 std::string unparsed = _conf.read<std::string>("limit_" + suffix, "0");
00412
00413 std::stringstream ss(unparsed);
00414
00415 float value; ss >> value;
00416 char multiplier = 0; ss >> multiplier;
00417
00418 switch (multiplier)
00419 {
00420 default:
00421 return (size_t)value;
00422 break;
00423
00424 case 'G':
00425 return (size_t)(value * 1000000000);
00426 break;
00427
00428 case 'M':
00429 return (size_t)(value * 1000000);
00430 break;
00431
00432 case 'K':
00433 return (size_t)(value * 1000);
00434 break;
00435 }
00436
00437 return 0;
00438 }
00439 }
00440 }