27 #include <ndn-cxx/name.hpp>
28 #include <ndn-cxx/net/face-uri.hpp>
36 class ConfigurationVariable
39 typedef std::function<void(T)> ConfParameterCallback;
41 ConfigurationVariable(
const std::string& key,
const ConfParameterCallback& setter)
43 , m_setterCallback(setter)
46 , m_shouldCheckRange(false)
55 T value = section.get<T>(m_key);
57 if (!isValidValue(value)) {
61 m_setterCallback(value);
64 catch (
const std::exception& ex) {
67 std::cerr << ex.what() << std::endl;
68 std::cerr <<
"Missing required configuration variable" << std::endl;
72 m_setterCallback(m_defaultValue);
81 setMinAndMaxValue(T min, T max)
85 m_shouldCheckRange =
true;
89 setOptional(T defaultValue)
92 m_defaultValue = defaultValue;
97 printOutOfRangeError(T value)
99 std::cerr <<
"Invalid value for " << m_key <<
": "
102 << m_minValue <<
" - "
103 << m_maxValue << std::endl;
107 isValidValue(T value)
109 if (!m_shouldCheckRange) {
112 else if (value < m_minValue || value > m_maxValue)
114 printOutOfRangeError(value);
122 const std::string m_key;
123 const ConfParameterCallback m_setterCallback;
129 bool m_shouldCheckRange;
134 : m_confFileName(confParam.getConfFileName())
135 , m_confParam(confParam)
143 std::ifstream inputFile;
144 inputFile.open(m_confFileName.c_str());
145 if (!inputFile.is_open()) {
146 std::string msg =
"Failed to read configuration file: ";
147 msg += m_confFileName;
148 std::cerr << msg << std::endl;
151 ret = load(inputFile);
163 ConfFileProcessor::load(std::istream& input)
167 boost::property_tree::read_info(input, pt);
169 catch (
const boost::property_tree::info_parser_error& error) {
170 std::stringstream msg;
171 std::cerr <<
"Failed to parse configuration file " << std::endl;
172 std::cerr << m_confFileName << 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")
190 ret = processConfSectionGeneral(section);
192 else if (sectionName ==
"neighbors")
194 ret = processConfSectionNeighbors(section);
196 else if (sectionName ==
"hyperbolic")
198 ret = processConfSectionHyperbolic(section);
200 else if (sectionName ==
"fib")
202 ret = processConfSectionFib(section);
204 else if (sectionName ==
"advertising")
206 ret = processConfSectionAdvertising(section);
208 else if (sectionName ==
"security")
210 ret = processConfSectionSecurity(section);
214 std::cerr <<
"Wrong configuration section: " << sectionName << std::endl;
220 ConfFileProcessor::processConfSectionGeneral(
const ConfigSection& section)
223 std::string network = section.get<std::string>(
"network");
224 std::string site = section.get<std::string>(
"site");
225 std::string router = section.get<std::string>(
"router");
226 ndn::Name networkName(network);
227 if (!networkName.empty()) {
231 std::cerr <<
" Network can not be null or empty or in bad URI format :(!" << std::endl;
234 ndn::Name siteName(site);
235 if (!siteName.empty()) {
239 std::cerr <<
"Site can not be null or empty or in bad URI format:( !" << std::endl;
242 ndn::Name routerName(router);
243 if (!routerName.empty()) {
247 std::cerr <<
" Router name can not be null or empty or in bad URI format:( !" << std::endl;
251 catch (
const std::exception& ex) {
252 std::cerr << ex.what() << std::endl;
263 std::cerr <<
"Wrong value for lsa-refresh-time ";
271 uint32_t routerDeadInterval = section.get<uint32_t>(
"router-dead-interval", (2*lsaRefreshTime));
277 std::cerr <<
"Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
288 std::cerr <<
"Wrong value for lsa-interest-timeout. "
296 std::string syncProtocol = section.get<std::string>(
"sync-protocol",
"chronosync");
297 if (syncProtocol ==
"chronosync") {
300 else if (syncProtocol ==
"psync") {
304 std::cerr <<
"Sync protocol " << syncProtocol <<
" is not supported!"
305 <<
"Use chronosync or psync" << std::endl;
310 uint32_t syncInterestLifetime = section.get<uint32_t>(
"sync-interest-lifetime",
317 std::cerr <<
"Wrong value for sync-interest-lifetime. "
325 std::string stateDir = section.get<std::string>(
"state-dir");
326 if (bf::exists(stateDir)) {
327 if (bf::is_directory(stateDir)) {
330 std::string conFileDynamic = (bf::path(stateDir) /
"nlsr.conf").c_str();
332 if (m_confFileName == conFileDynamic) {
333 std::cerr <<
"Please use nlsr.conf stored at another location "
334 <<
"or change the state-dir in the configuration." << std::endl;
335 std::cerr <<
"The file at " << conFileDynamic <<
336 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
337 std::cerr <<
"The dynamic file can be used for next run "
338 <<
"after copying to another location." << std::endl;
344 bf::copy_file(m_confFileName, conFileDynamic, bf::copy_option::overwrite_if_exists);
346 catch (
const bf::filesystem_error& e) {
347 std::cerr <<
"Error copying conf file to the state directory: " << e.what() << std::endl;
350 std::string testFileName = (bf::path(stateDir) /
"test.seq").c_str();
351 std::ofstream testOutFile(testFileName);
356 std::cerr <<
"User does not have read and write permission on the state directory";
357 std::cerr << std::endl;
361 remove(testFileName.c_str());
364 std::cerr <<
"Provided: " << stateDir <<
"is not a directory" << std::endl;
369 std::cerr <<
"Provided state directory <" << stateDir <<
"> does not exist" << std::endl;
373 catch (
const std::exception& ex) {
374 std::cerr <<
"You must configure state directory" << std::endl;
375 std::cerr << ex.what() << std::endl;
383 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
392 std::cerr <<
"Wrong value for hello-retries." << std::endl;
406 std::cerr <<
"Wrong value for hello-timeout. ";
420 std::cerr <<
"Wrong value for hello-interval. ";
429 ConfigurationVariable<uint32_t> adjLsaBuildInterval(
"adj-lsa-build-interval",
435 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
439 ConfigurationVariable<uint32_t> faceDatasetFetchTries(
"face-dataset-fetch-tries",
448 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
453 ConfigurationVariable<uint32_t> faceDatasetFetchInterval(
"face-dataset-fetch-interval",
462 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
466 for (
const auto& tn : section) {
467 if (tn.first ==
"neighbor") {
470 std::string name = CommandAttriTree.get<std::string>(
"name");
471 std::string uriString = CommandAttriTree.get<std::string>(
"face-uri");
473 ndn::FaceUri faceUri;
474 if (!faceUri.parse(uriString)) {
475 std::cerr <<
"face-uri parsing failed" << std::endl;
479 bool failedToCanonize =
false;
480 faceUri.canonize([&faceUri] (
const auto& canonicalUri) {
481 faceUri = canonicalUri;
483 [&faceUri, &failedToCanonize] (
const auto& reason) {
484 failedToCanonize =
true;
485 std::cerr <<
"Could not canonize URI: '" << faceUri
486 <<
"' because: " << reason << std::endl;
493 if (failedToCanonize) {
498 ndn::Name neighborName(name);
499 if (!neighborName.empty()) {
504 std::cerr <<
" Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
505 std::cerr <<
" or bad URI format" << std::endl;
508 catch (
const std::exception& ex) {
509 std::cerr << ex.what() << std::endl;
518 ConfFileProcessor::processConfSectionHyperbolic(
const ConfigSection& section)
521 std::string state = section.get<std::string>(
"state",
"off");
523 if (boost::iequals(state,
"off")) {
526 else if (boost::iequals(state,
"on")) {
529 else if (state ==
"dry-run") {
533 std::cerr <<
"Wrong format for hyperbolic state." << std::endl;
534 std::cerr <<
"Allowed value: off, on, dry-run" << std::endl;
544 double radius = section.get<
double>(
"radius");
545 std::string angleString = section.get<std::string>(
"angle");
547 std::stringstream ss(angleString);
548 std::vector<double> angles;
552 while (ss >> angle) {
553 angles.push_back(angle);
554 if (ss.peek() ==
',' || ss.peek() ==
' ') {
559 if (!m_confParam.
setCorR(radius)) {
564 catch (
const std::exception& ex) {
565 std::cerr << ex.what() << std::endl;
566 if (state ==
"on" || state ==
"dry-run") {
575 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
586 std::cerr <<
"Wrong value for max-faces-per-prefix. ";
593 ConfigurationVariable<uint32_t> routingCalcInterval(
"routing-calc-interval",
599 if (!routingCalcInterval.parseFromConfigSection(section)) {
607 ConfFileProcessor::processConfSectionAdvertising(
const ConfigSection& section)
609 for (
const auto& tn : section) {
610 if (tn.first ==
"prefix") {
612 ndn::Name namePrefix(tn.second.data());
613 if (!namePrefix.empty()) {
617 std::cerr <<
" Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
621 catch (
const std::exception& ex) {
622 std::cerr << ex.what() << std::endl;
631 ConfFileProcessor::processConfSectionSecurity(
const ConfigSection& section)
633 ConfigSection::const_iterator it = section.begin();
635 if (it == section.end() || it->first !=
"validator") {
636 std::cerr <<
"Error: Expect validator section!" << std::endl;
640 m_confParam.
getValidator().load(it->second, m_confFileName);
643 if (it != section.end() && it->first ==
"prefix-update-validator") {
647 for (; it != section.end(); it++) {
649 if (it->first !=
"cert-to-publish") {
650 std::cerr <<
"Error: Expect cert-to-publish!" << std::endl;
654 std::string file = it->second.data();
655 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
656 auto idCert = ndn::io::load<ndn::security::Certificate>(certfilePath.string());
658 if (idCert ==
nullptr) {
659 std::cerr <<
"Error: Cannot load cert-to-publish: " << file <<
"!" << std::endl;
bool insert(const Adjacent &adjacent)
static const 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(int32_t 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,...
const ndn::time::seconds TIME_ALLOWED_FOR_CANONIZATION
@ SYNC_INTEREST_LIFETIME_MIN
@ SYNC_INTEREST_LIFETIME_MAX
@ SYNC_INTEREST_LIFETIME_DEFAULT
@ ROUTING_CALC_INTERVAL_DEFAULT
@ ROUTING_CALC_INTERVAL_MIN
@ ROUTING_CALC_INTERVAL_MAX
@ LSA_REFRESH_TIME_DEFAULT
@ FACE_DATASET_FETCH_INTERVAL_DEFAULT
@ FACE_DATASET_FETCH_INTERVAL_MIN
@ FACE_DATASET_FETCH_INTERVAL_MAX
@ ADJ_LSA_BUILD_INTERVAL_DEFAULT
@ ADJ_LSA_BUILD_INTERVAL_MIN
@ ADJ_LSA_BUILD_INTERVAL_MAX
@ FACE_DATASET_FETCH_TRIES_DEFAULT
@ FACE_DATASET_FETCH_TRIES_MIN
@ FACE_DATASET_FETCH_TRIES_MAX
@ LSA_INTEREST_LIFETIME_MAX
@ LSA_INTEREST_LIFETIME_DEFAULT
@ LSA_INTEREST_LIFETIME_MIN
@ HYPERBOLIC_STATE_DRY_RUN
@ SYNC_PROTOCOL_CHRONOSYNC
boost::property_tree::ptree ConfigSection
@ MAX_FACES_PER_PREFIX_MIN
@ MAX_FACES_PER_PREFIX_DEFAULT
@ MAX_FACES_PER_PREFIX_MAX