IBR-DTNSuite  0.10
Configuration.cpp
Go to the documentation of this file.
1 /*
2  * Configuration.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "config.h"
23 #include "Configuration.h"
26 #include "core/Node.h"
27 
28 #include <ibrdtn/utils/Utils.h>
29 #include <ibrdtn/utils/Clock.h>
30 
32 #include <ibrcommon/Logger.h>
33 
34 #include <getopt.h>
35 #include <unistd.h>
36 
37 #ifdef __DEVELOPMENT_ASSERTIONS__
38 #include <cassert>
39 #endif
40 
41 using namespace dtn::net;
42 using namespace dtn::core;
43 using namespace dtn::utils;
44 using namespace ibrcommon;
45 
46 namespace dtn
47 {
48  namespace daemon
49  {
50  Configuration::NetConfig::NetConfig(std::string n, NetType t, const std::string &u)
51  : name(n), type(t), url(u), mtu(0), port(0)
52  {
53  }
54 
56  : name(n), type(t), iface(i), mtu(1500), port(p)
57  {
58  }
59 
61  : name(n), type(t), iface(), mtu(1500), port(p)
62  {
63  }
64 
66  {
67  }
68 
69  std::string Configuration::version() const
70  {
71  std::stringstream ss;
72  ss << PACKAGE_VERSION;
73  #ifdef BUILD_NUMBER
74  ss << " (build " << BUILD_NUMBER << ")";
75  #endif
76 
77  return ss.str();
78  }
79 
80  Configuration::Configuration()
81  : _filename("config.ini"), _doapi(true)
82  {
83  }
84 
85  Configuration::~Configuration()
86  {}
87 
89  : _enabled(true), _timeout(5), _crosslayer(false) {}
90 
92  : _enabled(false), _quiet(false), _level(0) {}
93 
95  : _quiet(false), _options(0), _timestamps(false), _verbose(false) {}
96 
98  : _routing("default"), _forwarding(true), _tcp_nodelay(true), _tcp_chunksize(4096), _tcp_idle_timeout(0), _default_net("lo"), _use_default_net(false), _auto_connect(0), _fragmentation(false), _scheduling(false)
99  {}
100 
102  : _enabled(false), _tlsEnabled(false), _tlsRequired(false), _tlsOptionalOnBadClock(false), _level(SECURITY_LEVEL_NONE), _disableEncryption(false)
103  {}
104 
106  : _daemonize(false), _kill(false), _threads(0)
107  {}
108 
110  : _reference(true), _sync(false), _discovery(false), _sigma(1.001f), _psi(0.8f), _sync_level(0.10f)
111  {}
112 
114  : _enabled(true), _port(0), _dnsbootstrapping(true), _ipv4(true), _ipv6(true), _blacklist(true), _selfannounce(true),
115  _minRating(1), _allowNeighbourToAnnounceMe(true), _allowNeighbourAnnouncement(true),
116  _ignoreDHTNeighbourInformations(false)
117  {}
118 
120  : _ctrl_path(""), _enabled(false)
121  {}
122 
131 
133  {
134  return _disco;
135  }
136 
138  {
139  return _debug;
140  }
141 
143  {
144  return _logger;
145  }
146 
148  {
149  return _network;
150  }
151 
153  {
154  return _security;
155  }
156 
158  {
159  return _daemon;
160  }
161 
163  {
164  return _timesync;
165  }
166 
168  {
169  return _dht;
170  }
171 
173  {
174  return _p2p;
175  }
176 
178  {
179  static Configuration conf;
180 
181  // reset configuration
182  if (reset) conf = Configuration();
183 
184  return conf;
185  }
186 
187  void Configuration::params(int argc, char *argv[])
188  {
189  int c;
190  int doapi = _doapi;
191  int disco = _disco._enabled;
192  int badclock = dtn::utils::Clock::isBad();
193  int timestamp = _logger._timestamps;
194  int showversion = 0;
195 
196  // set number of threads to the number of available cpus
197  _daemon._threads = static_cast<dtn::data::Size>(ibrcommon::Thread::getNumberOfProcessors());
198 
199  while (1)
200  {
201  static struct option long_options[] =
202  {
203  /* These options set a flag. */
204  {"noapi", no_argument, &doapi, 0},
205  {"nodiscovery", no_argument, &disco, 0},
206  {"badclock", no_argument, &badclock, 1},
207  {"timestamp", no_argument, &timestamp, 1},
208  {"version", no_argument, &showversion, 1},
209 
210  /* These options don't set a flag. We distinguish them by their indices. */
211  {"help", no_argument, 0, 'h'},
212 #ifdef HAVE_LIBDAEMON
213  {"daemon", no_argument, 0, 'D'},
214  {"kill", no_argument, 0, 'k'},
215  {"pidfile", required_argument, 0, 'p'},
216 #endif
217 
218  {"quiet", no_argument, 0, 'q'},
219  {"interface", required_argument, 0, 'i'},
220  {"configuration", required_argument, 0, 'c'},
221  {"debug", required_argument, 0, 'd'},
222  {0, 0, 0, 0}
223  };
224 
225  /* getopt_long stores the option index here. */
226  int option_index = 0;
227 
228 #ifdef HAVE_LIBDAEMON
229  c = getopt_long (argc, argv, "qhDkp:vi:c:d:t:",
230  long_options, &option_index);
231 #else
232  c = getopt_long (argc, argv, "qhvi:c:d:t:",
233  long_options, &option_index);
234 #endif
235 
236  /* Detect the end of the options. */
237  if (c == -1)
238  break;
239 
240  switch (c)
241  {
242  case 0:
243  /* If this option set a flag, do nothing else now. */
244  if (long_options[option_index].flag != 0)
245  break;
246  printf ("option %s", long_options[option_index].name);
247  if (optarg)
248  printf (" with arg %s", optarg);
249  printf ("\n");
250  break;
251 
252  case 'h':
253  std::cout << "IBR-DTN version: " << version() << std::endl;
254  std::cout << "Syntax: dtnd [options]" << std::endl;
255  std::cout << " -h|--help display this text" << std::endl;
256  std::cout << " -c <file> set a configuration file" << std::endl;
257 #ifdef HAVE_LIBDAEMON
258  std::cout << " -D daemonize the process" << std::endl;
259  std::cout << " -k stop the running daemon" << std::endl;
260  std::cout << " -p <file> store the pid in this pidfile" << std::endl;
261 #endif
262  std::cout << " -i <interface> interface to bind on (e.g. eth0)" << std::endl;
263  std::cout << " -d <level> enable debugging and set a verbose level" << std::endl;
264  std::cout << " -q enables the quiet mode (no logging to the console)" << std::endl;
265  std::cout << " -t <threads> specify a number of threads for parallel event processing" << std::endl;
266  std::cout << " -v be verbose - show NOTICE log messages" << std::endl;
267  std::cout << " --version show version and exit" << std::endl;
268  std::cout << " --noapi disable API module" << std::endl;
269  std::cout << " --nodiscovery disable discovery module" << std::endl;
270  std::cout << " --badclock assume a bad clock on the system (use AgeBlock)" << std::endl;
271  std::cout << " --timestamp enables timestamps for logging instead of datetime values" << std::endl;
272  exit(0);
273  break;
274 
275  case 'v':
276  _logger._verbose = true;
277  break;
278 
279  case 'q':
280  _debug._quiet = true;
281  break;
282 
283  case 'c':
284  _filename = optarg;
285  break;
286 
287  case 'i':
288  _network._default_net = ibrcommon::vinterface(optarg);
289  _network._use_default_net = true;
290  break;
291 
292  case 'd':
293  _debug._enabled = true;
294  _debug._level = atoi(optarg);
295  break;
296 
297  case 'D':
298  _daemon._daemonize = true;
299  _debug._quiet = true;
300  break;
301 
302  case 'k':
303  _daemon._daemonize = true;
304  _daemon._kill = true;
305  break;
306 
307  case 'p':
308  _daemon._pidfile = std::string(optarg);
309  break;
310 
311  case 't':
312  _daemon._threads = atoi(optarg);
313  break;
314 
315  case '?':
316  /* getopt_long already printed an error message. */
317  break;
318 
319  default:
320  abort();
321  break;
322  }
323  }
324 
325  if (showversion == 1) {
326  std::cout << "IBR-DTN version: " << version() << std::endl;
327  exit(0);
328  }
329 
330  _doapi = doapi;
331  _disco._enabled = disco;
332  dtn::utils::Clock::setBad(badclock);
333  _logger._timestamps = timestamp;
334  }
335 
337  {
338  load(_filename);
339  }
340 
341  void Configuration::load(string filename)
342  {
343  try {
344  // load main configuration
345  _conf = ibrcommon::ConfigFile(filename);
346  _filename = filename;
347 
348  IBRCOMMON_LOGGER_TAG("Configuration", info) << "Configuration: " << filename << IBRCOMMON_LOGGER_ENDL;
349  } catch (const ibrcommon::ConfigFile::file_not_found&) {
350  IBRCOMMON_LOGGER_TAG("Configuration", info) << "Using default settings. Call with --help for options." << IBRCOMMON_LOGGER_ENDL;
351  _conf = ConfigFile();
352 
353  // set the default user to nobody
354  _conf.add<std::string>("user", "nobody");
355  }
356 
357  // load all configuration extensions
358  _disco.load(_conf);
359  _debug.load(_conf);
360  _logger.load(_conf);
361  _network.load(_conf);
362  _security.load(_conf);
363  _timesync.load(_conf);
364  _dht.load(_conf);
365  _p2p.load(_conf);
366  }
367 
369  {
370  _timeout = conf.read<unsigned int>("discovery_timeout", 5);
371  _crosslayer = (conf.read<std::string>("discovery_crosslayer", "no") == "yes");
372  }
373 
375  {
376  try {
377  _logfile = conf.read<std::string>("logfile");
378  } catch (const ibrcommon::ConfigFile::key_not_found&) {
379  }
380  }
381 
383  {
384  }
385 
387  {
388  }
389 
391  {
392  try {
393  _reference = (conf.read<std::string>("time_reference") == "yes");
394  } catch (const ibrcommon::ConfigFile::key_not_found&) { };
395 
396  try {
397  _sync = (conf.read<std::string>("time_synchronize") == "yes");
398  } catch (const ibrcommon::ConfigFile::key_not_found&) { };
399 
400  try {
401  _discovery = (conf.read<std::string>("time_discovery_announcements") == "yes");
402  } catch (const ibrcommon::ConfigFile::key_not_found&) { };
403 
404  _sigma = conf.read<float>("time_sigma", 1.001f);
405  _psi = conf.read<float>("time_psi", 0.9f);
406  _sync_level = conf.read<float>("time_sync_level", 0.15f);
407 
408  // enable the clock modify feature
409  dtn::utils::Clock::setModifyClock(conf.read<std::string>("time_set_clock", "no") == "yes");
410  }
411 
413  {
414  _enabled = (conf.read<std::string> ("dht_enabled", "no") == "yes");
415  _port = conf.read<int> ("dht_port", 9999);
416  _id = conf.read<string> ("dht_id", "");
417  _blacklist = (conf.read<std::string> ("dht_blacklist", "yes") == "yes");
418  _selfannounce = (conf.read<std::string> ("dht_self_announce", "yes") == "yes");
419  _dnsbootstrapping = (conf.read<std::string> ("dht_bootstrapping", "yes") == "yes");
420  string list = conf.read<string> ("dht_bootstrapping_domains", "");
421  _bootstrappingdomains = dtn::utils::Utils::tokenize(" ", list);
422  list = conf.read<string> ("dht_bootstrapping_ips", "");
423  _bootstrappingips = dtn::utils::Utils::tokenize(";", list);
424  _ipv4bind = conf.read<string> ("dht_bind_ipv4", "");
425  _ipv6bind = conf.read<string> ("dht_bind_ipv6", "");
426  _nodesFilePath = conf.read<string> ("dht_nodes_file", "");
427  _ipv4 = (conf.read<std::string> ("dht_enable_ipv4", "yes") == "yes");
428  _ipv6 = (conf.read<std::string> ("dht_enable_ipv6", "yes") == "yes");
429  _minRating = conf.read<int> ("dht_min_rating", 1);
430  _allowNeighbourToAnnounceMe = (conf.read<std::string> ("dht_allow_neighbours_to_announce_me", "yes") == "yes");
431  _allowNeighbourAnnouncement = (conf.read<std::string> ("dht_allow_neighbour_announcement", "yes") == "yes");
432  _ignoreDHTNeighbourInformations = (conf.read<std::string> ("dht_ignore_neighbour_informations", "no") == "yes");
433 
434  if (_minRating < 0) _minRating = 0;
435  }
436 
438  {
439  try {
440  _ctrl_path = conf.read<std::string>("p2p_ctrlpath");
441  _enabled = true;
442  } catch (const ibrcommon::ConfigFile::key_not_found&) {
443  // do nothing here...
444  _enabled = false;
445  }
446  }
447 
449  {
450  return _quiet;
451  }
452 
454  {
455  return _enabled;
456  }
457 
459  {
460  return _level;
461  }
462 
463  std::string Configuration::getNodename() const
464  {
465  try {
466  return _conf.read<string>("local_uri");
467  } catch (const ibrcommon::ConfigFile::key_not_found&) {
468  std::vector<char> hostname_array(64);
469  if ( gethostname(&hostname_array[0], hostname_array.size()) != 0 )
470  {
471  // error
472  return "local";
473  }
474 
475  return "dtn://" + std::string(&hostname_array[0]);
476  }
477  return "noname";
478  }
479 
480  const std::list<Configuration::NetConfig>& Configuration::Network::getInterfaces() const
481  {
482  return _interfaces;
483  }
484 
485  const std::set<ibrcommon::vaddress> Configuration::Discovery::address() const throw (ParameterNotFoundException)
486  {
487  std::set<ibrcommon::vaddress> ret;
488 
489  try {
490  std::string address_str = Configuration::getInstance()._conf.read<string>("discovery_address");
491  std::vector<std::string> addresses = dtn::utils::Utils::tokenize(" ", address_str);
492 
493  for (std::vector<std::string>::iterator iter = addresses.begin(); iter != addresses.end(); ++iter) {
494  ret.insert( ibrcommon::vaddress(*iter, port(), AF_UNSPEC) );
495  }
496  } catch (const ConfigFile::key_not_found&) {
497  throw ParameterNotFoundException();
498  }
499 
500  return ret;
501  }
502 
504  {
505  return Configuration::getInstance()._conf.read<int>("discovery_port", 4551);
506  }
507 
509  {
510  return _timeout;
511  }
512 
514  {
515  return _crosslayer;
516  }
517 
519  {
520  int port = _conf.read<int>("api_port", 4550);
521 
522  try {
523  std::string interface_name = _conf.read<std::string>("api_interface");
524 
525  if (interface_name == "any")
526  {
528  }
529 
531  } catch (const ConfigFile::key_not_found&) { }
532 
534  }
535 
537  {
538  try {
539  return ibrcommon::File(_conf.read<std::string>("api_socket"));
540  } catch (const ConfigFile::key_not_found&) {
541  throw ParameterNotSetException();
542  }
543 
544  throw ParameterNotSetException();
545  }
546 
547  std::string Configuration::getStorage() const
548  {
549  return _conf.read<std::string>("storage", "default");
550  }
551 
553  return (_conf.read<std::string>("stats_traffic", "no") == "yes");
554  }
555 
557  {
561  _static_routes.clear();
562 
563  string key = "route1";
564  unsigned int keynumber = 1;
565 
566  while (conf.keyExists( key ))
567  {
568  vector<string> route = dtn::utils::Utils::tokenize(" ", conf.read<string>(key, "dtn:none dtn:none"));
569  _static_routes.insert( pair<std::string, std::string>( route.front(), route.back() ) );
570 
571  keynumber++;
572  stringstream ss; ss << "route" << keynumber; ss >> key;
573  }
574 
578  // read the node count
579  int count = 1;
580 
581  // initial prefix
582  std::string prefix = "static1_";
583 
584  while ( conf.keyExists(prefix + "uri") )
585  {
586  const dtn::data::EID node_eid( conf.read<std::string>(prefix + "uri", "dtn:none") );
587 
588  // create a address URI
589  std::stringstream ss;
590  ss << "ip=" << conf.read<std::string>(prefix + "address", "127.0.0.1") << ";port=" << conf.read<unsigned int>(prefix + "port", 4556) << ";";
591 
592  dtn::core::Node::Protocol p = Node::CONN_UNDEFINED;
593 
594  const std::string protocol = conf.read<std::string>(prefix + "proto", "tcp");
595  if (protocol == "tcp") p = Node::CONN_TCPIP;
596  if (protocol == "udp") p = Node::CONN_UDPIP;
597  if (protocol == "lowpan") p = Node::CONN_LOWPAN;
598  if (protocol == "zigbee") p = Node::CONN_LOWPAN; //Legacy: Stay compatible with older config files
599  if (protocol == "bluetooth") p = Node::CONN_BLUETOOTH;
600  if (protocol == "http") p = Node::CONN_HTTP;
601  if (protocol == "file") p = Node::CONN_FILE;
602  if (protocol == "dgram:udp") p = Node::CONN_DGRAM_UDP;
603  if (protocol == "dgram:ethernet") p = Node::CONN_DGRAM_ETHERNET;
604  if (protocol == "dgram:lowpan") p = Node::CONN_DGRAM_LOWPAN;
605 
606  bool node_exists = false;
607 
608  dtn::core::Node::Type t = (conf.read<std::string>(prefix + "global", "no") == "yes") ?
610 
611  // get node
612  for (std::list<Node>::iterator iter = _nodes.begin(); iter != _nodes.end(); ++iter)
613  {
614  dtn::core::Node &n = (*iter);
615 
616  if (n.getEID() == node_eid)
617  {
618  n.add( dtn::core::Node::URI( t, p, ss.str(), 0, 10 ) );
619  n.setConnectImmediately( conf.read<std::string>(prefix + "immediately", "no") == "yes" );
620  node_exists = true;
621  break;
622  }
623  }
624 
625  if (!node_exists)
626  {
627  Node n(node_eid);
628  n.add( dtn::core::Node::URI( t, p, ss.str(), 0, 10 ) );
629  n.setConnectImmediately( conf.read<std::string>(prefix + "immediately", "no") == "yes" );
630  _nodes.push_back(n);
631  }
632 
633  count++;
634 
635  std::stringstream prefix_stream;
636  prefix_stream << "static" << count << "_";
637  prefix = prefix_stream.str();
638  }
639 
643  _routing = conf.read<string>("routing", "default");
644 
645  if(_routing == "prophet"){
646  /* read prophet parameters */
647  _prophet_config.p_encounter_max = conf.read<float>("prophet_p_encounter_max", 0.7f);
648  if(_prophet_config.p_encounter_max > 1 || _prophet_config.p_encounter_max <= 0)
649  _prophet_config.p_encounter_max = 0.7f;
650  _prophet_config.p_encounter_first = conf.read<float>("prophet_p_encounter_first", 0.5f);
651  if(_prophet_config.p_encounter_first > 1 || _prophet_config.p_encounter_first <= 0)
652  _prophet_config.p_encounter_first = 0.5f;
653  _prophet_config.p_first_threshold = conf.read<float>("prophet_p_first_threshold", 0.1f);
654  if(_prophet_config.p_first_threshold < 0 || _prophet_config.p_first_threshold >= _prophet_config.p_encounter_first)
655  _prophet_config.p_first_threshold = 0; //disable first threshold on misconfiguration
656  _prophet_config.beta = conf.read<float>("prophet_beta", 0.9f);
657  if(_prophet_config.beta < 0 || _prophet_config.beta > 1)
658  _prophet_config.beta = 0.9f;
659  _prophet_config.gamma = conf.read<float>("prophet_gamma", 0.999f);
660  if(_prophet_config.gamma <= 0 || _prophet_config.gamma > 1)
661  _prophet_config.gamma = 0.999f;
662  _prophet_config.delta = conf.read<float>("prophet_delta", 0.01f);
663  if(_prophet_config.delta < 0 || _prophet_config.delta > 1)
664  _prophet_config.delta = 0.01f;
665  _prophet_config.time_unit = conf.read<ibrcommon::Timer::time_t>("prophet_time_unit", 1);
666  if(_prophet_config.time_unit < 1)
667  _prophet_config.time_unit = 1;
668  _prophet_config.i_typ = conf.read<ibrcommon::Timer::time_t>("prophet_i_typ", 300);
669  if(_prophet_config.i_typ < 1)
670  _prophet_config.i_typ = 1;
671  _prophet_config.next_exchange_timeout = conf.read<ibrcommon::Timer::time_t>("prophet_next_exchange_timeout", 60);
672  _prophet_config.forwarding_strategy = conf.read<std::string>("prophet_forwarding_strategy", "GRTR");
673  _prophet_config.gtmx_nf_max = conf.read<unsigned int>("prophet_gtmx_nf_max", 30);
674  }
675 
679  _forwarding = (conf.read<std::string>("routing_forwarding", "yes") == "yes");
680 
684  _interfaces.clear();
685 
686  if (_use_default_net)
687  {
688  _interfaces.push_back( Configuration::NetConfig("default", Configuration::NetConfig::NETWORK_TCP, _default_net, 4556) );
689  }
690  else try
691  {
692  vector<string> nets = dtn::utils::Utils::tokenize(" ", conf.read<string>("net_interfaces") );
693  for (vector<string>::const_iterator iter = nets.begin(); iter != nets.end(); ++iter)
694  {
695  std::string netname = (*iter);
696 
697  std::string key_type = "net_" + netname + "_type";
698  std::string key_port = "net_" + netname + "_port";
699  std::string key_interface = "net_" + netname + "_interface";
700  std::string key_address = "net_" + netname + "_address";
701  std::string key_path = "net_" + netname + "_path";
702  std::string key_mtu = "net_" + netname + "_mtu";
703 
704  std::string type_name = conf.read<string>(key_type, "tcp");
706 
707  if (type_name == "tcp") type = Configuration::NetConfig::NETWORK_TCP;
708  if (type_name == "udp") type = Configuration::NetConfig::NETWORK_UDP;
709  if (type_name == "http") type = Configuration::NetConfig::NETWORK_HTTP;
710  if (type_name == "lowpan") type = Configuration::NetConfig::NETWORK_LOWPAN;
711  if (type_name == "file") type = Configuration::NetConfig::NETWORK_FILE;
712  if (type_name == "dgram:udp") type = Configuration::NetConfig::NETWORK_DGRAM_UDP;
713  if (type_name == "dgram:lowpan") type = Configuration::NetConfig::NETWORK_DGRAM_LOWPAN;
714  if (type_name == "dgram:ethernet") type = Configuration::NetConfig::NETWORK_DGRAM_ETHERNET;
715 
716  switch (type)
717  {
719  {
720  Configuration::NetConfig nc(netname, type,
721  conf.read<std::string>(key_address, "http://localhost/"));
722 
723  _interfaces.push_back(nc);
724  break;
725  }
726 
728  {
729  Configuration::NetConfig nc(netname, type,
730  conf.read<std::string>(key_path, ""));
731 
732  _interfaces.push_back(nc);
733  break;
734  }
735 
736  default:
737  {
738  int port = conf.read<int>(key_port, 4556);
739  int mtu = conf.read<int>(key_mtu, 1280);
740 
741  try {
742  ibrcommon::vinterface iface(conf.read<std::string>(key_interface));
743  Configuration::NetConfig nc(netname, type, iface, port);
744  nc.mtu = mtu;
745  _interfaces.push_back(nc);
746  } catch (const ConfigFile::key_not_found&) {
747  Configuration::NetConfig nc(netname, type, port);
748  nc.mtu = mtu;
749  _interfaces.push_back(nc);
750  }
751 
752  break;
753  }
754  }
755  }
756  } catch (const ConfigFile::key_not_found&) {
757  // stop the one network is not found.
758  }
759 
763  _tcp_nodelay = (conf.read<std::string>("tcp_nodelay", "yes") == "yes");
764  _tcp_chunksize = conf.read<unsigned int>("tcp_chunksize", 4096);
765  _tcp_idle_timeout = conf.read<unsigned int>("tcp_idle_timeout", 0);
766 
770  _auto_connect = conf.read<dtn::data::Timeout>("net_autoconnect", 0);
771 
775  _fragmentation = (conf.read<std::string>("fragmentation", "no") == "yes");
776 
780  try {
781  std::vector<string> inets = dtn::utils::Utils::tokenize(" ", conf.read<string>("net_internet") );
782  for (std::vector<string>::const_iterator iter = inets.begin(); iter != inets.end(); ++iter)
783  {
784  ibrcommon::vinterface inet_dev(*iter);
785  _internet_devices.insert(inet_dev);
786  }
787  } catch (const ibrcommon::ConfigFile::key_not_found&) { };
788 
792  _scheduling = (conf.read<std::string>("scheduling", "no") == "yes");
793  }
794 
795  const std::multimap<std::string, std::string>& Configuration::Network::getStaticRoutes() const
796  {
797  return _static_routes;
798  }
799 
800  const std::list<Node>& Configuration::Network::getStaticNodes() const
801  {
802  return _nodes;
803  }
804 
806  {
807  return _conf.read<int>( "timezone", 0 );
808  }
809 
811  {
812  stringstream ss;
813  ss << name << "_path";
814  string key; ss >> key;
815 
816  try {
817  return ibrcommon::File(_conf.read<string>(key));
818  } catch (const ConfigFile::key_not_found&) { }
819 
820  throw ParameterNotSetException();
821  }
822 
824  {
825  return _enabled;
826  }
827 
829  {
830  return (Configuration::getInstance()._conf.read<int>("discovery_announce", 1) == 1);
831  }
832 
834  {
835  return (Configuration::getInstance()._conf.read<int>("discovery_short", 0) == 1);
836  }
837 
839  {
840  return Configuration::getInstance()._conf.read<int>("discovery_version", 2);
841  }
842 
843  bool Configuration::doAPI() const
844  {
845  return _doapi;
846  }
847 
849  {
850  if ( _routing == "none" ) return NO_ROUTING;
851  if ( _routing == "epidemic" ) return EPIDEMIC_ROUTING;
852  if ( _routing == "flooding" ) return FLOOD_ROUTING;
853  if ( _routing == "prophet" ) return PROPHET_ROUTING;
854  return DEFAULT_ROUTING;
855  }
856 
857 
859  {
860  return _forwarding;
861  }
862 
864  {
865  return _fragmentation;
866  }
867 
869  {
870  return _scheduling;
871  }
872 
874  {
875  return _tcp_nodelay;
876  }
877 
879  {
880  return _tcp_chunksize;
881  }
882 
884  {
885  return _tcp_idle_timeout;
886  }
887 
889  {
890  return _auto_connect;
891  }
892 
894  {
895  return _prophet_config;
896  }
897 
898  std::set<ibrcommon::vinterface> Configuration::Network::getInternetDevices() const
899  {
900  return _internet_devices;
901  }
902 
903  dtn::data::Size Configuration::getLimit(const std::string &suffix) const
904  {
905  std::string unparsed = _conf.read<std::string>("limit_" + suffix, "0");
906 
907  std::stringstream ss(unparsed);
908 
909  float value; ss >> value;
910  char multiplier = 0; ss >> multiplier;
911 
912  switch (multiplier)
913  {
914  default:
915  return static_cast<dtn::data::Size>(value);
916  break;
917 
918  case 'G':
919  return static_cast<dtn::data::Size>(value * 1000000000);
920  break;
921 
922  case 'M':
923  return static_cast<dtn::data::Size>(value * 1000000);
924  break;
925 
926  case 'K':
927  return static_cast<dtn::data::Size>(value * 1000);
928  break;
929  }
930 
931  return 0;
932  }
933 
935  {
936  bool withTLS = false;
937 #ifdef WITH_TLS
938  withTLS = true;
939  /* enable TLS if the certificate path, a certificate and the private key was given */
940  bool activateTLS = true;
941 
942  // load public key file
943  try {
944  _cert = conf.read<std::string>("security_certificate");
945 
946  if (!_cert.exists())
947  {
948  IBRCOMMON_LOGGER_TAG("Configuration", warning) << "Certificate file " << _cert.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
949  activateTLS = false;
950  }
951  } catch (const ibrcommon::ConfigFile::key_not_found&) {
952  activateTLS = false;
953  }
954 
955  // load KEY file
956  try {
957  _key = conf.read<std::string>("security_key");
958 
959  if (!_key.exists())
960  {
961  IBRCOMMON_LOGGER_TAG("Configuration", warning) << "KEY file " << _key.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
962  activateTLS = false;
963  }
964  } catch (const ibrcommon::ConfigFile::key_not_found&) {
965  activateTLS = false;
966  }
967 
968  // read trustedCAPath
969  try{
970  _trustedCAPath = conf.read<std::string>("security_trusted_ca_path");
971  if(!_trustedCAPath.isDirectory()){
972  IBRCOMMON_LOGGER_TAG("Configuration", warning) << "Trusted CA Path " << _trustedCAPath.getPath() << " does not exists or is no directory!" << IBRCOMMON_LOGGER_ENDL;
973  activateTLS = false;
974  }
975  } catch (const ibrcommon::ConfigFile::key_not_found&) {
976  activateTLS = false;
977  }
978 
979  // read if encryption should be disabled
980  _disableEncryption = (conf.read<std::string>("security_tls_disable_encryption", "no") == "yes");
981 
982  if (activateTLS)
983  {
984  _tlsEnabled = true;
985 
986  /* read if TLS is required */
987  _tlsRequired = (conf.read<std::string>("security_tls_required", "no") == "yes");
988 
989  /* If the clock is not in sync, SSL will fail. Accept plain connection when the clock is not sync'ed. */
990  _tlsOptionalOnBadClock = (conf.read<std::string>("security_tls_fallback_badclock", "no") == "yes");
991  }
992 #endif
993 
994 #ifdef WITH_BUNDLE_SECURITY
995  // enable security if the security path is set
996  try {
997  _path = conf.read<std::string>("security_path");
998 
999  if (!_path.exists())
1000  {
1002  }
1003 
1004  _enabled = true;
1005  } catch (const ibrcommon::ConfigFile::key_not_found&) {
1006  return;
1007  }
1008 
1009  // load level
1010  _level = Level(conf.read<int>("security_level", 0));
1011 
1012  if ( !withTLS )
1013  {
1014  /* if TLS is enabled, the Certificate file and the key have been read earlier */
1015  // load CA file
1016  try {
1017  _cert = conf.read<std::string>("security_certificate");
1018 
1019  if (!_cert.exists())
1020  {
1021  IBRCOMMON_LOGGER_TAG("Configuration", warning) << "Certificate file " << _cert.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
1022  }
1023  } catch (const ibrcommon::ConfigFile::key_not_found&) { }
1024 
1025  // load KEY file
1026  try {
1027  _key = conf.read<std::string>("security_key");
1028 
1029  if (!_key.exists())
1030  {
1031  IBRCOMMON_LOGGER_TAG("Configuration", warning) << "KEY file " << _key.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
1032  }
1033  } catch (const ibrcommon::ConfigFile::key_not_found&) { }
1034  }
1035 
1036  // load KEY file
1037  try {
1038  _bab_default_key = conf.read<std::string>("security_bab_default_key");
1039 
1040  if (!_bab_default_key.exists())
1041  {
1042  IBRCOMMON_LOGGER_TAG("Configuration", warning) << "KEY file " << _bab_default_key.getPath() << " does not exists!" << IBRCOMMON_LOGGER_ENDL;
1043  }
1044  } catch (const ibrcommon::ConfigFile::key_not_found&) {
1045  }
1046 #endif
1047  }
1048 
1050 
1052  {
1053  return _enabled;
1054  }
1055 
1057  {
1058  return _tlsEnabled;
1059  }
1060 
1062  {
1063  // TLS is only required, if the clock is in sync
1064  if ((dtn::utils::Clock::getRating() == 0) && _tlsOptionalOnBadClock) return false;
1065  return _tlsRequired;
1066  }
1067 
1069  {
1070  return _path;
1071  }
1072 
1074  {
1075  return _level;
1076  }
1077 
1079  {
1080  return _bab_default_key;
1081  }
1082 
1084  {
1085  return _cert;
1086  }
1087 
1089  {
1090  return _key;
1091  }
1092 
1094  {
1095  return _trustedCAPath;
1096  }
1097 
1099  {
1100  return _disableEncryption;
1101  }
1102 
1104  {
1105  return _quiet;
1106  }
1107 
1109  {
1110  if (_logfile.getPath() == "") throw Configuration::ParameterNotSetException();
1111  return _logfile;
1112  }
1113 
1115  {
1116  return _verbose;
1117  }
1118 
1120  {
1121  return _timestamps;
1122  }
1123 
1124  unsigned int Configuration::Logger::options() const
1125  {
1126  return _options;
1127  }
1128 
1129  std::ostream& Configuration::Logger::output() const
1130  {
1131  return std::cout;
1132  }
1133 
1135  {
1136  return _daemonize;
1137  }
1138 
1140  {
1141  return _kill;
1142  }
1143 
1145  {
1146  return _threads;
1147  }
1148 
1150  {
1151  if (_pidfile == ibrcommon::File()) throw ParameterNotSetException();
1152  return _pidfile;
1153  }
1154 
1156  {
1157  return _reference;
1158  }
1159 
1161  {
1162  return _sync;
1163  }
1164 
1166  {
1167  return _discovery;
1168  }
1169 
1171  {
1172  return _sigma;
1173  }
1174 
1176  {
1177  return _psi;
1178  }
1179 
1181  {
1182  return _sync_level;
1183  }
1184 
1186  {
1187  return _enabled;
1188  }
1189 
1191  {
1192  return _port == 0;
1193  }
1194 
1195  unsigned int Configuration::DHT::getPort() const
1196  {
1197  return _port;
1198  }
1199 
1201  {
1202  return _id;
1203  }
1204 
1206  {
1207  return _id == "";
1208  }
1209 
1211  {
1212  return _dnsbootstrapping;
1213  }
1214 
1216  {
1217  return _bootstrappingdomains;
1218  }
1219 
1221  {
1222  return !_bootstrappingips.empty();
1223  }
1224 
1225  std::vector<string> Configuration::DHT::getIPBootstrappingIPs() const
1226  {
1227  return _bootstrappingips;
1228  }
1229 
1231  {
1232  return _ipv4bind;
1233  }
1235  {
1236  return _ipv6bind;
1237  }
1238 
1240  {
1241  return _nodesFilePath;
1242  }
1243 
1245  {
1246  return _ipv4;
1247  }
1248 
1250  {
1251  return _ipv6;
1252  }
1253 
1255  {
1256  return _selfannounce;
1257  }
1258 
1260  {
1261  return _minRating;
1262  }
1263 
1265  {
1266  return _allowNeighbourAnnouncement;
1267  }
1268 
1270  {
1271  return _allowNeighbourToAnnounceMe;
1272  }
1273 
1275  {
1276  return _blacklist;
1277  }
1278 
1280  {
1281  return _ignoreDHTNeighbourInformations;
1282  }
1283 
1285  {
1286  return _enabled;
1287  }
1288 
1289  const std::string Configuration::P2P::getCtrlPath() const
1290  {
1291  return _ctrl_path;
1292  }
1293  }
1294 }