27 #include <ndn-cxx/name.hpp>
28 #include <ndn-cxx/net/face-uri.hpp>
29 #include <ndn-cxx/util/io.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/property_tree/info_parser.hpp>
40 namespace fs = std::filesystem;
43 class ConfigurationVariable
46 typedef std::function<void(T)> ConfParameterCallback;
48 ConfigurationVariable(
const std::string& key,
const ConfParameterCallback& setter)
50 , m_setterCallback(setter)
53 , m_shouldCheckRange(false)
62 T value = section.get<T>(m_key);
64 if (!isValidValue(value)) {
68 m_setterCallback(value);
71 catch (
const std::exception& ex) {
74 std::cerr << ex.what() << std::endl;
75 std::cerr <<
"Missing required configuration variable" << std::endl;
79 m_setterCallback(m_defaultValue);
88 setMinAndMaxValue(T min, T max)
92 m_shouldCheckRange =
true;
96 setOptional(T defaultValue)
99 m_defaultValue = defaultValue;
104 printOutOfRangeError(T value)
106 std::cerr <<
"Invalid value for " << m_key <<
": "
109 << m_minValue <<
" - "
110 << m_maxValue << std::endl;
114 isValidValue(T value)
116 if (!m_shouldCheckRange) {
119 else if (value < m_minValue || value > m_maxValue)
121 printOutOfRangeError(value);
129 const std::string m_key;
130 const ConfParameterCallback m_setterCallback;
136 bool m_shouldCheckRange;
141 : m_confFileName(confParam.getConfFileName())
142 , m_confParam(confParam)
149 std::ifstream inputFile(m_confFileName);
150 if (!inputFile.is_open()) {
151 std::cerr <<
"Failed to read configuration file: " << m_confFileName << std::endl;
155 if (!load(inputFile)) {
165 ConfFileProcessor::load(std::istream& input)
169 boost::property_tree::read_info(input, pt);
171 catch (
const boost::property_tree::ptree_error& e) {
172 std::cerr <<
"Failed to parse configuration file '" << m_confFileName
173 <<
"': " << e.what() << std::endl;
177 for (
const auto& tn : pt) {
178 if (!processSection(tn.first, tn.second)) {
186 ConfFileProcessor::processSection(
const std::string& sectionName,
const ConfigSection& section)
189 if (sectionName ==
"general") {
190 ret = processConfSectionGeneral(section);
192 else if (sectionName ==
"neighbors") {
193 ret = processConfSectionNeighbors(section);
195 else if (sectionName ==
"hyperbolic") {
196 ret = processConfSectionHyperbolic(section);
198 else if (sectionName ==
"fib") {
199 ret = processConfSectionFib(section);
201 else if (sectionName ==
"advertising") {
202 ret = processConfSectionAdvertising(section);
204 else if (sectionName ==
"security") {
205 ret = processConfSectionSecurity(section);
208 std::cerr <<
"Unknown configuration section: " << sectionName << std::endl;
214 ConfFileProcessor::processConfSectionGeneral(
const ConfigSection& section)
217 std::string syncProtocol = section.get<std::string>(
"sync-protocol",
"psync");
218 if (syncProtocol ==
"chronosync") {
219 #ifdef HAVE_CHRONOSYNC
222 std::cerr <<
"NLSR was compiled without ChronoSync support!\n";
226 else if (syncProtocol ==
"psync") {
230 std::cerr <<
"NLSR was compiled without PSync support!\n";
234 else if (syncProtocol ==
"svs") {
238 std::cerr <<
"NLSR was compiled without SVS support!\n";
243 std::cerr <<
"Sync protocol '" << syncProtocol <<
"' is not supported!\n"
244 <<
"Use 'chronosync' or 'psync' or 'svs'\n";
249 std::string network = section.get<std::string>(
"network");
250 std::string site = section.get<std::string>(
"site");
251 std::string router = section.get<std::string>(
"router");
252 ndn::Name networkName(network);
253 if (!networkName.empty()) {
257 std::cerr <<
"Network can not be null or empty or in bad URI format" << std::endl;
260 ndn::Name siteName(site);
261 if (!siteName.empty()) {
265 std::cerr <<
"Site can not be null or empty or in bad URI format" << std::endl;
268 ndn::Name routerName(router);
269 if (!routerName.empty()) {
273 std::cerr <<
"Router name can not be null or empty or in bad URI format" << std::endl;
277 catch (
const std::exception& ex) {
278 std::cerr << ex.what() << std::endl;
289 std::cerr <<
"Invalid value for lsa-refresh-time. "
296 uint32_t routerDeadInterval = section.get<uint32_t>(
"router-dead-interval", 2 * lsaRefreshTime);
302 std::cerr <<
"Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
313 std::cerr <<
"Invalid value for lsa-interest-timeout. "
320 uint32_t syncInterestLifetime = section.get<uint32_t>(
"sync-interest-lifetime",
327 std::cerr <<
"Invalid value for sync-interest-lifetime. "
335 fs::path stateDir(section.get<std::string>(
"state-dir"));
336 if (fs::exists(stateDir)) {
337 if (fs::is_directory(stateDir)) {
339 auto conFileDynamic = stateDir /
"nlsr.conf";
340 if (m_confFileName == conFileDynamic.string()) {
341 std::cerr <<
"Please use nlsr.conf stored at another location "
342 <<
"or change the state-dir in the configuration." << std::endl;
343 std::cerr <<
"The file at " << conFileDynamic <<
344 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
345 std::cerr <<
"The dynamic file can be used for next run "
346 <<
"after copying to another location." << std::endl;
352 fs::copy_file(m_confFileName, conFileDynamic, fs::copy_options::overwrite_existing);
354 catch (
const fs::filesystem_error& e) {
355 std::cerr <<
"Error copying conf file to state-dir: " << e.what() << std::endl;
359 auto testFilePath = stateDir /
"test.seq";
360 std::ofstream testFile(testFilePath);
365 std::cerr <<
"NLSR does not have read/write permission on state-dir" << std::endl;
369 fs::remove(testFilePath);
372 std::cerr <<
"Provided state-dir " << stateDir <<
" is not a directory" << std::endl;
377 std::cerr <<
"Provided state-dir " << stateDir <<
" does not exist" << std::endl;
381 catch (
const std::exception& ex) {
382 std::cerr <<
"You must configure state-dir" << std::endl;
383 std::cerr << ex.what() << std::endl;
391 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
400 std::cerr <<
"Invalid value for hello-retries. "
412 std::cerr <<
"Invalid value for hello-timeout. "
424 std::cerr <<
"Invalid value for hello-interval. "
431 ConfigurationVariable<uint32_t> adjLsaBuildInterval(
"adj-lsa-build-interval",
437 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
441 ConfigurationVariable<uint32_t> faceDatasetFetchTries(
"face-dataset-fetch-tries",
449 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
454 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 (boost::iequals(state,
"dry-run")) {
533 std::cerr <<
"Invalid setting for hyperbolic state. "
534 <<
"Allowed values: off, on, dry-run" << std::endl;
543 double radius = section.get<
double>(
"radius");
544 std::string angleString = section.get<std::string>(
"angle");
546 std::stringstream ss(angleString);
547 std::vector<double> angles;
551 while (ss >> angle) {
552 angles.push_back(angle);
553 if (ss.peek() ==
',' || ss.peek() ==
' ') {
558 if (!m_confParam.
setCorR(radius)) {
563 catch (
const std::exception& ex) {
564 std::cerr << ex.what() << std::endl;
565 if (state ==
"on" || state ==
"dry-run") {
574 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
584 std::cerr <<
"Invalid value for max-faces-per-prefix. "
591 ConfigurationVariable<uint32_t> routingCalcInterval(
"routing-calc-interval",
597 if (!routingCalcInterval.parseFromConfigSection(section)) {
605 ConfFileProcessor::processConfSectionAdvertising(
const ConfigSection& section)
607 for (
const auto& tn : section) {
608 if (tn.first ==
"prefix") {
610 ndn::Name namePrefix(tn.second.data());
611 if (!namePrefix.empty()) {
615 std::cerr <<
" Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
619 catch (
const std::exception& ex) {
620 std::cerr << ex.what() << std::endl;
629 ConfFileProcessor::processConfSectionSecurity(
const ConfigSection& section)
631 auto it = section.begin();
633 if (it == section.end() || it->first !=
"validator") {
634 std::cerr <<
"Error: Expected validator section!" << std::endl;
638 m_confParam.
getValidator().load(it->second, m_confFileName);
641 if (it != section.end() && it->first ==
"prefix-update-validator") {
645 for (; it != section.end(); it++) {
646 if (it->first !=
"cert-to-publish") {
647 std::cerr <<
"Error: Expected cert-to-publish!" << std::endl;
651 fs::path certPath = fs::canonical(fs::path(m_confFileName).parent_path() / it->second.data());
652 std::ifstream ifs(certPath);
654 ndn::security::Certificate idCert;
656 idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
658 catch (
const std::exception& e) {
659 std::cerr <<
"Error: Cannot load cert-to-publish " << certPath <<
": " << 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 setHyperbolicState(HyperbolicState ihc)
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 setNetwork(const ndn::Name &networkName)
void buildRouterAndSyncUserPrefix()
void setRoutingCalcInterval(uint32_t interval)
bool insert(const ndn::Name &name, const std::string &source="")
Inserts name and source combination.
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