27 #include <boost/cstdint.hpp> 29 #include <ndn-cxx/name.hpp> 30 #include <ndn-cxx/net/face-uri.hpp> 45 , m_setterCallback(setter)
48 , m_shouldCheckRange(false)
57 T value = section.get<T>(m_key);
59 if (!isValidValue(value)) {
63 m_setterCallback(value);
66 catch (
const std::exception& ex) {
69 std::cerr << ex.what() << std::endl;
70 std::cerr <<
"Missing required configuration variable" << std::endl;
74 m_setterCallback(m_defaultValue);
87 m_shouldCheckRange =
true;
94 m_defaultValue = defaultValue;
99 printOutOfRangeError(T value)
101 std::cerr <<
"Invalid value for " << m_key <<
": " 104 << m_minValue <<
" - " 105 << m_maxValue << std::endl;
109 isValidValue(T value)
111 if (!m_shouldCheckRange) {
114 else if (value < m_minValue || value > m_maxValue)
116 printOutOfRangeError(value);
124 const std::string m_key;
125 const ConfParameterCallback m_setterCallback;
131 bool m_shouldCheckRange;
136 : m_confFileName(confParam.getConfFileName())
137 , m_confParam(confParam)
145 std::ifstream inputFile;
146 inputFile.open(m_confFileName.c_str());
147 if (!inputFile.is_open()) {
148 std::string msg =
"Failed to read configuration file: ";
149 msg += m_confFileName;
150 std::cerr << msg << std::endl;
153 ret = load(inputFile);
165 ConfFileProcessor::load(std::istream& input)
169 boost::property_tree::read_info(input, pt);
171 catch (
const boost::property_tree::info_parser_error& error) {
172 std::stringstream msg;
173 std::cerr <<
"Failed to parse configuration file " << std::endl;
174 std::cerr << m_confFileName << std::endl;
178 for (
const auto& tn : pt) {
179 if (!processSection(tn.first, tn.second)) {
187 ConfFileProcessor::processSection(
const std::string& sectionName,
const ConfigSection& section)
190 if (sectionName ==
"general")
192 ret = processConfSectionGeneral(section);
194 else if (sectionName ==
"neighbors")
196 ret = processConfSectionNeighbors(section);
198 else if (sectionName ==
"hyperbolic")
200 ret = processConfSectionHyperbolic(section);
202 else if (sectionName ==
"fib")
204 ret = processConfSectionFib(section);
206 else if (sectionName ==
"advertising")
208 ret = processConfSectionAdvertising(section);
210 else if (sectionName ==
"security")
212 ret = processConfSectionSecurity(section);
216 std::cerr <<
"Wrong configuration section: " << sectionName << std::endl;
222 ConfFileProcessor::processConfSectionGeneral(
const ConfigSection& section)
225 std::string network = section.get<std::string>(
"network");
226 std::string site = section.get<std::string>(
"site");
227 std::string router = section.get<std::string>(
"router");
228 ndn::Name networkName(network);
229 if (!networkName.empty()) {
233 std::cerr <<
" Network can not be null or empty or in bad URI format :(!" << std::endl;
236 ndn::Name siteName(site);
237 if (!siteName.empty()) {
241 std::cerr <<
"Site can not be null or empty or in bad URI format:( !" << std::endl;
244 ndn::Name routerName(router);
245 if (!routerName.empty()) {
249 std::cerr <<
" Router name can not be null or empty or in bad URI format:( !" << std::endl;
253 catch (
const std::exception& ex) {
254 std::cerr << ex.what() << std::endl;
265 std::cerr <<
"Wrong value for lsa-refresh-time ";
273 uint32_t routerDeadInterval = section.get<uint32_t>(
"router-dead-interval", (2*lsaRefreshTime));
279 std::cerr <<
"Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
290 std::cerr <<
"Wrong value for lsa-interest-timeout. " 298 std::string syncProtocol = section.get<std::string>(
"sync-protocol",
"chronosync");
299 if (syncProtocol ==
"chronosync") {
302 else if (syncProtocol ==
"psync") {
306 std::cerr <<
"Sync protocol " << syncProtocol <<
" is not supported!" 307 <<
"Use chronosync or psync" << std::endl;
312 uint32_t syncInterestLifetime = section.get<uint32_t>(
"sync-interest-lifetime",
319 std::cerr <<
"Wrong value for sync-interest-lifetime. " 327 std::string stateDir = section.get<std::string>(
"state-dir");
328 if (bf::exists(stateDir)) {
329 if (bf::is_directory(stateDir)) {
332 std::string conFileDynamic = (bf::path(stateDir) /
"nlsr.conf").c_str();
334 if (m_confFileName == conFileDynamic) {
335 std::cerr <<
"Please use nlsr.conf stored at another location " 336 <<
"or change the state-dir in the configuration." << std::endl;
337 std::cerr <<
"The file at " << conFileDynamic <<
338 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
339 std::cerr <<
"The dynamic file can be used for next run " 340 <<
"after copying to another location." << std::endl;
346 bf::copy_file(m_confFileName, conFileDynamic, bf::copy_option::overwrite_if_exists);
348 catch (
const bf::filesystem_error& e) {
349 std::cerr <<
"Error copying conf file to the state directory: " << e.what() << std::endl;
352 std::string testFileName = (bf::path(stateDir) /
"test.seq").c_str();
353 std::ofstream testOutFile(testFileName);
358 std::cerr <<
"User does not have read and write permission on the state directory";
359 std::cerr << std::endl;
363 remove(testFileName.c_str());
366 std::cerr <<
"Provided: " << stateDir <<
"is not a directory" << std::endl;
371 std::cerr <<
"Provided state directory <" << stateDir <<
"> does not exist" << std::endl;
375 catch (
const std::exception& ex) {
376 std::cerr <<
"You must configure state directory" << std::endl;
377 std::cerr << ex.what() << std::endl;
385 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
394 std::cerr <<
"Wrong value for hello-retries." << std::endl;
408 std::cerr <<
"Wrong value for hello-timeout. ";
422 std::cerr <<
"Wrong value for hello-interval. ";
468 for (
const auto& tn : section) {
469 if (tn.first ==
"neighbor") {
472 std::string name = CommandAttriTree.get<std::string>(
"name");
473 std::string uriString = CommandAttriTree.get<std::string>(
"face-uri");
475 ndn::FaceUri faceUri;
476 if (! faceUri.parse(uriString)) {
477 std::cerr <<
"Parsing failed!" << std::endl;
481 bool failedToCanonize =
false;
482 faceUri.canonize([&faceUri] (ndn::FaceUri canonicalUri) {
483 faceUri = canonicalUri;
485 [&faceUri, &failedToCanonize] (
const std::string& reason) {
486 failedToCanonize =
true;
487 std::cerr <<
"Could not canonize URI: " << faceUri
488 <<
"because: " << reason << std::endl;
495 if (failedToCanonize) {
499 double linkCost = CommandAttriTree.get<
double>(
"link-cost",
501 ndn::Name neighborName(name);
502 if (!neighborName.empty()) {
507 std::cerr <<
" Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
508 std::cerr <<
" or bad URI format" << std::endl;
511 catch (
const std::exception& ex) {
512 std::cerr << ex.what() << std::endl;
521 ConfFileProcessor::processConfSectionHyperbolic(
const ConfigSection& section)
524 std::string state = section.get<std::string>(
"state",
"off");
526 if (boost::iequals(state,
"off")) {
529 else if (boost::iequals(state,
"on")) {
532 else if (state ==
"dry-run") {
536 std::cerr <<
"Wrong format for hyperbolic state." << std::endl;
537 std::cerr <<
"Allowed value: off, on, dry-run" << std::endl;
547 double radius = section.get<
double>(
"radius");
548 std::string angleString = section.get<std::string>(
"angle");
550 std::stringstream ss(angleString);
551 std::vector<double> angles;
555 while (ss >> angle) {
556 angles.push_back(angle);
557 if (ss.peek() ==
',' || ss.peek() ==
' ') {
562 if (!m_confParam.
setCorR(radius)) {
567 catch (
const std::exception& ex) {
568 std::cerr << ex.what() << std::endl;
569 if (state ==
"on" || state ==
"dry-run") {
578 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
589 std::cerr <<
"Wrong value for max-faces-per-prefix. ";
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 ConfigSection::const_iterator it = section.begin();
638 if (it == section.end() || it->first !=
"validator") {
639 std::cerr <<
"Error: Expect 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++) {
652 if (it->first !=
"cert-to-publish") {
653 std::cerr <<
"Error: Expect 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 auto idCert = ndn::io::load<ndn::security::v2::Certificate>(certfilePath.string());
661 if (idCert ==
nullptr) {
662 std::cerr <<
"Error: Cannot load cert-to-publish: " << file <<
"!" << std::endl;
ndn::security::ValidatorConfig & getValidator()
A class to house all the configuration parameters for NLSR.
void buildRouterAndSyncUserPrefix()
uint32_t getLsaRefreshTime() const
void setLsaInterestLifetime(const ndn::time::seconds &lifetime)
void setStateFileDir(const std::string &ssfd)
void setMaxFacesPerPrefix(uint32_t mfpp)
static const double DEFAULT_LINK_COST
void setRoutingCalcInterval(uint32_t interval)
void setNetwork(const ndn::Name &networkName)
AdjacencyList & getAdjacencyList()
void setAdjLsaBuildInterval(uint32_t interval)
void setSiteName(const ndn::Name &siteName)
ndn::security::ValidatorConfig & getPrefixUpdateValidator()
void setInterestResendTime(uint32_t irt)
void setInfoInterestInterval(uint32_t iii)
bool insert(const ndn::Name &name, const std::string &source="")
inserts name into NamePrefixList
void setCorTheta(const std::vector< double > &ct)
void setSyncInterestLifetime(uint32_t syncInterestLifetime)
ConfigurationVariable(const std::string &key, const ConfParameterCallback &setter)
void setSyncProtocol(int32_t syncProtocol)
void setRouterName(const ndn::Name &routerName)
void setFaceDatasetFetchInterval(uint32_t interval)
void setInterestRetryNumber(uint32_t irn)
void setMinAndMaxValue(T min, T max)
int32_t insert(Adjacent &adjacent)
Inserts an adjacency into the list.
A neighbor reachable over a Face.
void addCertPath(const std::string &certPath)
void setOptional(T defaultValue)
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void setConfFileNameDynamic(const std::string &confFileDynamic)
void setRouterDeadInterval(uint32_t rdt)
NamePrefixList & getNamePrefixList()
const ndn::time::seconds TIME_ALLOWED_FOR_CANONIZATION
void loadCertToValidator(const ndn::security::v2::Certificate &cert)
void setLsaRefreshTime(uint32_t lrt)
boost::property_tree::ptree ConfigSection
ConfFileProcessor(ConfParameter &confParam)
void setHyperbolicState(int32_t ihc)
void setFaceDatasetFetchTries(uint32_t count)
std::function< void(T)> ConfParameterCallback
bool parseFromConfigSection(const ConfigSection §ion)
bool processConfFile()
Load and parse the configuration file, then populate NLSR.
void writeLog()
Dump the current state of all attributes to the log.