27 #include <ndn-cxx/name.hpp>
28 #include <ndn-cxx/net/face-uri.hpp>
29 #include <ndn-cxx/util/io.hpp>
31 #include <boost/filesystem.hpp>
32 #include <boost/property_tree/info_parser.hpp>
37 namespace bf = boost::filesystem;
42 class ConfigurationVariable
45 typedef std::function<void(T)> ConfParameterCallback;
47 ConfigurationVariable(
const std::string& key,
const ConfParameterCallback& setter)
49 , m_setterCallback(setter)
52 , m_shouldCheckRange(false)
61 T value = section.get<T>(m_key);
63 if (!isValidValue(value)) {
67 m_setterCallback(value);
70 catch (
const std::exception& ex) {
73 std::cerr << ex.what() << std::endl;
74 std::cerr <<
"Missing required configuration variable" << std::endl;
78 m_setterCallback(m_defaultValue);
87 setMinAndMaxValue(T min, T max)
91 m_shouldCheckRange =
true;
95 setOptional(T defaultValue)
98 m_defaultValue = defaultValue;
103 printOutOfRangeError(T value)
105 std::cerr <<
"Invalid value for " << m_key <<
": "
108 << m_minValue <<
" - "
109 << m_maxValue << std::endl;
113 isValidValue(T value)
115 if (!m_shouldCheckRange) {
118 else if (value < m_minValue || value > m_maxValue)
120 printOutOfRangeError(value);
128 const std::string m_key;
129 const ConfParameterCallback m_setterCallback;
135 bool m_shouldCheckRange;
140 : m_confFileName(confParam.getConfFileName())
141 , m_confParam(confParam)
148 std::ifstream inputFile(m_confFileName);
149 if (!inputFile.is_open()) {
150 std::cerr <<
"Failed to read configuration file: " << m_confFileName << std::endl;
154 if (!load(inputFile)) {
164 ConfFileProcessor::load(std::istream& input)
168 boost::property_tree::read_info(input, pt);
170 catch (
const boost::property_tree::ptree_error& e) {
171 std::cerr <<
"Failed to parse configuration file '" << m_confFileName
172 <<
"': " << e.what() << std::endl;
176 for (
const auto& tn : pt) {
177 if (!processSection(tn.first, tn.second)) {
185 ConfFileProcessor::processSection(
const std::string& sectionName,
const ConfigSection& section)
188 if (sectionName ==
"general") {
189 ret = processConfSectionGeneral(section);
191 else if (sectionName ==
"neighbors") {
192 ret = processConfSectionNeighbors(section);
194 else if (sectionName ==
"hyperbolic") {
195 ret = processConfSectionHyperbolic(section);
197 else if (sectionName ==
"fib") {
198 ret = processConfSectionFib(section);
200 else if (sectionName ==
"advertising") {
201 ret = processConfSectionAdvertising(section);
203 else if (sectionName ==
"security") {
204 ret = processConfSectionSecurity(section);
207 std::cerr <<
"Unknown configuration section: " << sectionName << std::endl;
213 ConfFileProcessor::processConfSectionGeneral(
const ConfigSection& section)
216 std::string syncProtocol = section.get<std::string>(
"sync-protocol",
"psync");
217 if (syncProtocol ==
"chronosync") {
218 #ifdef HAVE_CHRONOSYNC
221 std::cerr <<
"NLSR was compiled without ChronoSync support!\n";
225 else if (syncProtocol ==
"psync") {
229 std::cerr <<
"NLSR was compiled without PSync support!\n";
233 else if (syncProtocol ==
"svs") {
237 std::cerr <<
"NLSR was compiled without SVS support!\n";
242 std::cerr <<
"Sync protocol '" << syncProtocol <<
"' is not supported!\n"
243 <<
"Use 'chronosync' or 'psync' or 'svs'\n";
248 std::string network = section.get<std::string>(
"network");
249 std::string site = section.get<std::string>(
"site");
250 std::string router = section.get<std::string>(
"router");
251 ndn::Name networkName(network);
252 if (!networkName.empty()) {
256 std::cerr <<
"Network can not be null or empty or in bad URI format" << std::endl;
259 ndn::Name siteName(site);
260 if (!siteName.empty()) {
264 std::cerr <<
"Site can not be null or empty or in bad URI format" << std::endl;
267 ndn::Name routerName(router);
268 if (!routerName.empty()) {
272 std::cerr <<
"Router name can not be null or empty or in bad URI format" << std::endl;
276 catch (
const std::exception& ex) {
277 std::cerr << ex.what() << std::endl;
288 std::cerr <<
"Invalid value for lsa-refresh-time. "
295 uint32_t routerDeadInterval = section.get<uint32_t>(
"router-dead-interval", 2 * lsaRefreshTime);
301 std::cerr <<
"Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
312 std::cerr <<
"Invalid value for lsa-interest-timeout. "
319 uint32_t syncInterestLifetime = section.get<uint32_t>(
"sync-interest-lifetime",
326 std::cerr <<
"Invalid value for sync-interest-lifetime. "
333 std::string stateDir = section.get<std::string>(
"state-dir");
334 if (bf::exists(stateDir)) {
335 if (bf::is_directory(stateDir)) {
337 std::string conFileDynamic = (bf::path(stateDir) /
"nlsr.conf").
string();
339 if (m_confFileName == conFileDynamic) {
340 std::cerr <<
"Please use nlsr.conf stored at another location "
341 <<
"or change the state-dir in the configuration." << std::endl;
342 std::cerr <<
"The file at " << conFileDynamic <<
343 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
344 std::cerr <<
"The dynamic file can be used for next run "
345 <<
"after copying to another location." << std::endl;
351 bf::copy_file(m_confFileName, conFileDynamic,
352 #
if BOOST_VERSION >= 107400
353 bf::copy_options::overwrite_existing
355 bf::copy_option::overwrite_if_exists
359 catch (
const bf::filesystem_error& e) {
360 std::cerr <<
"Error copying conf file to the state directory: " << e.what() << std::endl;
364 std::string testFileName = (bf::path(stateDir) /
"test.seq").
string();
365 std::ofstream testOutFile(testFileName);
370 std::cerr <<
"NLSR does not have read/write permission on the state directory" << std::endl;
374 remove(testFileName.c_str());
377 std::cerr <<
"Provided path '" << stateDir <<
"' is not a directory" << std::endl;
382 std::cerr <<
"Provided state directory '" << stateDir <<
"' does not exist" << std::endl;
386 catch (
const std::exception& ex) {
387 std::cerr <<
"You must configure state directory" << std::endl;
388 std::cerr << ex.what() << std::endl;
396 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
405 std::cerr <<
"Invalid value for hello-retries. "
417 std::cerr <<
"Invalid value for hello-timeout. "
429 std::cerr <<
"Invalid value for hello-interval. "
436 ConfigurationVariable<uint32_t> adjLsaBuildInterval(
"adj-lsa-build-interval",
442 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
446 ConfigurationVariable<uint32_t> faceDatasetFetchTries(
"face-dataset-fetch-tries",
454 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
459 ConfigurationVariable<uint32_t> faceDatasetFetchInterval(
"face-dataset-fetch-interval",
467 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
471 for (
const auto& tn : section) {
472 if (tn.first ==
"neighbor") {
475 std::string name = CommandAttriTree.get<std::string>(
"name");
476 std::string uriString = CommandAttriTree.get<std::string>(
"face-uri");
478 ndn::FaceUri faceUri;
479 if (!faceUri.parse(uriString)) {
480 std::cerr <<
"face-uri parsing failed" << std::endl;
484 bool failedToCanonize =
false;
485 faceUri.canonize([&faceUri] (
const auto& canonicalUri) {
486 faceUri = canonicalUri;
488 [&faceUri, &failedToCanonize] (
const auto& reason) {
489 failedToCanonize =
true;
490 std::cerr <<
"Could not canonize URI: '" << faceUri
491 <<
"' because: " << reason << std::endl;
498 if (failedToCanonize) {
503 ndn::Name neighborName(name);
504 if (!neighborName.empty()) {
509 std::cerr <<
" Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
510 std::cerr <<
" or bad URI format" << std::endl;
513 catch (
const std::exception& ex) {
514 std::cerr << ex.what() << std::endl;
523 ConfFileProcessor::processConfSectionHyperbolic(
const ConfigSection& section)
526 std::string state = section.get<std::string>(
"state",
"off");
528 if (boost::iequals(state,
"off")) {
531 else if (boost::iequals(state,
"on")) {
534 else if (boost::iequals(state,
"dry-run")) {
538 std::cerr <<
"Invalid setting for hyperbolic state. "
539 <<
"Allowed values: off, on, dry-run" << std::endl;
548 double radius = section.get<
double>(
"radius");
549 std::string angleString = section.get<std::string>(
"angle");
551 std::stringstream ss(angleString);
552 std::vector<double> angles;
556 while (ss >> angle) {
557 angles.push_back(angle);
558 if (ss.peek() ==
',' || ss.peek() ==
' ') {
563 if (!m_confParam.
setCorR(radius)) {
568 catch (
const std::exception& ex) {
569 std::cerr << ex.what() << std::endl;
570 if (state ==
"on" || state ==
"dry-run") {
579 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
589 std::cerr <<
"Invalid value for max-faces-per-prefix. "
596 ConfigurationVariable<uint32_t> routingCalcInterval(
"routing-calc-interval",
602 if (!routingCalcInterval.parseFromConfigSection(section)) {
610 ConfFileProcessor::processConfSectionAdvertising(
const ConfigSection& section)
612 for (
const auto& tn : section) {
613 if (tn.first ==
"prefix") {
615 ndn::Name namePrefix(tn.second.data());
616 if (!namePrefix.empty()) {
620 std::cerr <<
" Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
624 catch (
const std::exception& ex) {
625 std::cerr << ex.what() << std::endl;
634 ConfFileProcessor::processConfSectionSecurity(
const ConfigSection& section)
636 auto it = section.begin();
638 if (it == section.end() || it->first !=
"validator") {
639 std::cerr <<
"Error: Expected validator section!" << std::endl;
643 m_confParam.
getValidator().load(it->second, m_confFileName);
646 if (it != section.end() && it->first ==
"prefix-update-validator") {
650 for (; it != section.end(); it++) {
651 if (it->first !=
"cert-to-publish") {
652 std::cerr <<
"Error: Expected cert-to-publish!" << std::endl;
656 std::string file = it->second.data();
657 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
658 std::ifstream ifs(certfilePath.string());
660 ndn::security::Certificate idCert;
662 idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
664 catch (
const std::exception& e) {
665 std::cerr <<
"Error: Cannot load cert-to-publish '" << file <<
"': " << e.what() << std::endl;
bool insert(const Adjacent &adjacent)
static constexpr double DEFAULT_LINK_COST
ConfFileProcessor(ConfParameter &confParam)
bool processConfFile()
Load and parse the configuration file, then populate NLSR.
A class to house all the configuration parameters for NLSR.
void setRouterName(const ndn::Name &routerName)
void setSiteName(const ndn::Name &siteName)
void setInterestRetryNumber(uint32_t irn)
void setMaxFacesPerPrefix(uint32_t mfpp)
void setRouterDeadInterval(uint32_t rdt)
void writeLog()
Dump the current state of all attributes to the log.
void setSyncProtocol(SyncProtocol syncProtocol)
void setStateFileDir(const std::string &ssfd)
void setLsaRefreshTime(uint32_t lrt)
void setInterestResendTime(uint32_t irt)
void loadCertToValidator(const ndn::security::Certificate &cert)
uint32_t getLsaRefreshTime() const
void setAdjLsaBuildInterval(uint32_t interval)
void setConfFileNameDynamic(const std::string &confFileDynamic)
void setInfoInterestInterval(uint32_t iii)
NamePrefixList & getNamePrefixList()
void addCertPath(const std::string &certPath)
AdjacencyList & getAdjacencyList()
void setFaceDatasetFetchTries(uint32_t count)
ndn::security::ValidatorConfig & getValidator()
void setLsaInterestLifetime(const ndn::time::seconds &lifetime)
void setSyncInterestLifetime(uint32_t syncInterestLifetime)
void setCorTheta(const std::vector< double > &ct)
ndn::security::ValidatorConfig & getPrefixUpdateValidator()
void setFaceDatasetFetchInterval(uint32_t interval)
void setHyperbolicState(int32_t ihc)
void setNetwork(const ndn::Name &networkName)
void buildRouterAndSyncUserPrefix()
void setRoutingCalcInterval(uint32_t interval)
bool insert(const ndn::Name &name, const std::string &source="")
inserts name into NamePrefixList
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
@ FACE_DATASET_FETCH_TRIES_DEFAULT
@ FACE_DATASET_FETCH_TRIES_MIN
@ FACE_DATASET_FETCH_TRIES_MAX
@ LSA_REFRESH_TIME_DEFAULT
@ ROUTING_CALC_INTERVAL_DEFAULT
@ ROUTING_CALC_INTERVAL_MIN
@ ROUTING_CALC_INTERVAL_MAX
@ MAX_FACES_PER_PREFIX_MIN
@ MAX_FACES_PER_PREFIX_DEFAULT
@ MAX_FACES_PER_PREFIX_MAX
@ FACE_DATASET_FETCH_INTERVAL_DEFAULT
@ FACE_DATASET_FETCH_INTERVAL_MIN
@ FACE_DATASET_FETCH_INTERVAL_MAX
constexpr ndn::time::seconds TIME_ALLOWED_FOR_CANONIZATION
@ ADJ_LSA_BUILD_INTERVAL_DEFAULT
@ ADJ_LSA_BUILD_INTERVAL_MIN
@ ADJ_LSA_BUILD_INTERVAL_MAX
@ HYPERBOLIC_STATE_DRY_RUN
@ LSA_INTEREST_LIFETIME_MAX
@ LSA_INTEREST_LIFETIME_DEFAULT
@ LSA_INTEREST_LIFETIME_MIN
boost::property_tree::ptree ConfigSection
@ SYNC_INTEREST_LIFETIME_MIN
@ SYNC_INTEREST_LIFETIME_MAX
@ SYNC_INTEREST_LIFETIME_DEFAULT