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/filesystem.hpp>
33 #include <boost/property_tree/info_parser.hpp>
38 namespace bf = boost::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. "
334 std::string stateDir = section.get<std::string>(
"state-dir");
335 if (bf::exists(stateDir)) {
336 if (bf::is_directory(stateDir)) {
338 std::string conFileDynamic = (bf::path(stateDir) /
"nlsr.conf").
string();
340 if (m_confFileName == conFileDynamic) {
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 bf::copy_file(m_confFileName, conFileDynamic,
353 #
if BOOST_VERSION >= 107400
354 bf::copy_options::overwrite_existing
356 bf::copy_option::overwrite_if_exists
360 catch (
const bf::filesystem_error& e) {
361 std::cerr <<
"Error copying conf file to the state directory: " << e.what() << std::endl;
365 std::string testFileName = (bf::path(stateDir) /
"test.seq").
string();
366 std::ofstream testOutFile(testFileName);
371 std::cerr <<
"NLSR does not have read/write permission on the state directory" << std::endl;
375 remove(testFileName.c_str());
378 std::cerr <<
"Provided path '" << stateDir <<
"' is not a directory" << std::endl;
383 std::cerr <<
"Provided state directory '" << stateDir <<
"' does not exist" << std::endl;
387 catch (
const std::exception& ex) {
388 std::cerr <<
"You must configure state directory" << std::endl;
389 std::cerr << ex.what() << std::endl;
397 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
406 std::cerr <<
"Invalid value for hello-retries. "
418 std::cerr <<
"Invalid value for hello-timeout. "
430 std::cerr <<
"Invalid value for hello-interval. "
437 ConfigurationVariable<uint32_t> adjLsaBuildInterval(
"adj-lsa-build-interval",
443 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
447 ConfigurationVariable<uint32_t> faceDatasetFetchTries(
"face-dataset-fetch-tries",
455 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
460 ConfigurationVariable<uint32_t> faceDatasetFetchInterval(
"face-dataset-fetch-interval",
468 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
472 for (
const auto& tn : section) {
473 if (tn.first ==
"neighbor") {
476 std::string name = CommandAttriTree.get<std::string>(
"name");
477 std::string uriString = CommandAttriTree.get<std::string>(
"face-uri");
479 ndn::FaceUri faceUri;
480 if (!faceUri.parse(uriString)) {
481 std::cerr <<
"face-uri parsing failed" << std::endl;
485 bool failedToCanonize =
false;
486 faceUri.canonize([&faceUri] (
const auto& canonicalUri) {
487 faceUri = canonicalUri;
489 [&faceUri, &failedToCanonize] (
const auto& reason) {
490 failedToCanonize =
true;
491 std::cerr <<
"Could not canonize URI: '" << faceUri
492 <<
"' because: " << reason << std::endl;
499 if (failedToCanonize) {
504 ndn::Name neighborName(name);
505 if (!neighborName.empty()) {
510 std::cerr <<
" Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
511 std::cerr <<
" or bad URI format" << std::endl;
514 catch (
const std::exception& ex) {
515 std::cerr << ex.what() << std::endl;
524 ConfFileProcessor::processConfSectionHyperbolic(
const ConfigSection& section)
527 std::string state = section.get<std::string>(
"state",
"off");
529 if (boost::iequals(state,
"off")) {
532 else if (boost::iequals(state,
"on")) {
535 else if (boost::iequals(state,
"dry-run")) {
539 std::cerr <<
"Invalid setting for hyperbolic state. "
540 <<
"Allowed values: off, on, dry-run" << std::endl;
549 double radius = section.get<
double>(
"radius");
550 std::string angleString = section.get<std::string>(
"angle");
552 std::stringstream ss(angleString);
553 std::vector<double> angles;
557 while (ss >> angle) {
558 angles.push_back(angle);
559 if (ss.peek() ==
',' || ss.peek() ==
' ') {
564 if (!m_confParam.
setCorR(radius)) {
569 catch (
const std::exception& ex) {
570 std::cerr << ex.what() << std::endl;
571 if (state ==
"on" || state ==
"dry-run") {
580 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
590 std::cerr <<
"Invalid value for max-faces-per-prefix. "
597 ConfigurationVariable<uint32_t> routingCalcInterval(
"routing-calc-interval",
603 if (!routingCalcInterval.parseFromConfigSection(section)) {
611 ConfFileProcessor::processConfSectionAdvertising(
const ConfigSection& section)
613 for (
const auto& tn : section) {
614 if (tn.first ==
"prefix") {
616 ndn::Name namePrefix(tn.second.data());
617 if (!namePrefix.empty()) {
621 std::cerr <<
" Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
625 catch (
const std::exception& ex) {
626 std::cerr << ex.what() << std::endl;
635 ConfFileProcessor::processConfSectionSecurity(
const ConfigSection& section)
637 auto it = section.begin();
639 if (it == section.end() || it->first !=
"validator") {
640 std::cerr <<
"Error: Expected validator section!" << std::endl;
644 m_confParam.
getValidator().load(it->second, m_confFileName);
647 if (it != section.end() && it->first ==
"prefix-update-validator") {
651 for (; it != section.end(); it++) {
652 if (it->first !=
"cert-to-publish") {
653 std::cerr <<
"Error: Expected cert-to-publish!" << std::endl;
657 std::string file = it->second.data();
658 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
659 std::ifstream ifs(certfilePath.string());
661 ndn::security::Certificate idCert;
663 idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
665 catch (
const std::exception& e) {
666 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 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