27 #include <boost/cstdint.hpp> 29 #include <ndn-cxx/name.hpp> 30 #include <ndn-cxx/net/face-uri.hpp> 46 , m_setterCallback(setter)
49 , m_shouldCheckRange(false)
58 T value = section.get<T>(m_key);
60 if (!isValidValue(value)) {
64 m_setterCallback(value);
67 catch (
const std::exception& ex) {
70 std::cerr << ex.what() << std::endl;
71 std::cerr <<
"Missing required configuration variable" << std::endl;
75 m_setterCallback(m_defaultValue);
88 m_shouldCheckRange =
true;
95 m_defaultValue = defaultValue;
100 printOutOfRangeError(T value)
102 std::cerr <<
"Invalid value for " << m_key <<
": " 105 << m_minValue <<
" - " 106 << m_maxValue << std::endl;
110 isValidValue(T value)
112 if (!m_shouldCheckRange) {
115 else if (value < m_minValue || value > m_maxValue)
117 printOutOfRangeError(value);
125 const std::string m_key;
126 const ConfParameterCallback m_setterCallback;
132 bool m_shouldCheckRange;
137 : m_confFileName(confParam.getConfFileName())
138 , m_confParam(confParam)
146 std::ifstream inputFile;
147 inputFile.open(m_confFileName.c_str());
148 if (!inputFile.is_open()) {
149 std::string msg =
"Failed to read configuration file: ";
150 msg += m_confFileName;
151 std::cerr << msg << std::endl;
154 ret = load(inputFile);
160 ConfFileProcessor::load(std::istream& input)
165 boost::property_tree::read_info(input, pt);
167 catch (
const boost::property_tree::info_parser_error& error) {
168 std::stringstream msg;
169 std::cerr <<
"Failed to parse configuration file " << std::endl;
170 std::cerr << m_confFileName << std::endl;
174 for (ConfigSection::const_iterator tn = pt.begin();
175 tn != pt.end(); ++tn) {
176 ret = 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 (boost::filesystem::exists(stateDir)) {
327 if (boost::filesystem::is_directory(stateDir)) {
330 std::string conFileDynamic = (boost::filesystem::path(stateDir) /
"nlsr.conf").c_str();
333 copy_file(m_confFileName, conFileDynamic, boost::filesystem::copy_option::overwrite_if_exists);
335 catch (
const boost::filesystem::filesystem_error& e) {
336 std::cerr <<
"Error copying conf file to the state directory: " << e.what() << std::endl;
338 std::string testFileName = (boost::filesystem::path(stateDir) /
"test.seq").c_str();
339 std::ofstream testOutFile(testFileName);
344 std::cerr <<
"User does not have read and write permission on the state directory";
345 std::cerr << std::endl;
349 remove(testFileName.c_str());
352 std::cerr <<
"Provided: " << stateDir <<
"is not a directory" << std::endl;
357 std::cerr <<
"Provided state directory <" << stateDir <<
"> does not exist" << std::endl;
361 catch (
const std::exception& ex) {
362 std::cerr <<
"You must configure state directory" << std::endl;
363 std::cerr << ex.what() << std::endl;
371 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
380 std::cerr <<
"Wrong value for hello-retries." << std::endl;
394 std::cerr <<
"Wrong value for hello-timeout. ";
408 std::cerr <<
"Wrong value for hello-interval. ";
465 for (ConfigSection::const_iterator tn =
466 section.begin(); tn != section.end(); ++tn) {
468 if (tn->first ==
"neighbor") {
470 ConfigSection CommandAttriTree = tn->second;
471 std::string name = CommandAttriTree.get<std::string>(
"name");
472 std::string uriString = CommandAttriTree.get<std::string>(
"face-uri");
474 ndn::FaceUri faceUri;
475 if (! faceUri.parse(uriString)) {
476 std::cerr <<
"Parsing failed!" << std::endl;
480 bool failedToCanonize =
false;
481 faceUri.canonize([&faceUri] (ndn::FaceUri canonicalUri) {
482 faceUri = canonicalUri;
484 [&faceUri, &failedToCanonize] (
const std::string& reason) {
485 failedToCanonize =
true;
486 std::cerr <<
"Could not canonize URI: " << faceUri
487 <<
"because: " << reason << std::endl;
493 if (failedToCanonize) {
497 double linkCost = CommandAttriTree.get<
double>(
"link-cost",
499 ndn::Name neighborName(name);
500 if (!neighborName.empty()) {
505 std::cerr <<
" Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
506 std::cerr <<
" or bad URI format" << std::endl;
509 catch (
const std::exception& ex) {
510 std::cerr << ex.what() << std::endl;
519 ConfFileProcessor::processConfSectionHyperbolic(
const ConfigSection& section)
522 std::string state = section.get<std::string>(
"state",
"off");
524 if (boost::iequals(state,
"off")) {
527 else if (boost::iequals(state,
"on")) {
530 else if (state ==
"dry-run") {
534 std::cerr <<
"Wrong format for hyperbolic state." << std::endl;
535 std::cerr <<
"Allowed value: off, on, dry-run" << std::endl;
545 double radius = section.get<
double>(
"radius");
546 std::string angleString = section.get<std::string>(
"angle");
548 std::stringstream ss(angleString);
549 std::vector<double> angles;
553 while (ss >> angle) {
554 angles.push_back(angle);
555 if (ss.peek() ==
',' || ss.peek() ==
' ') {
560 if (!m_confParam.
setCorR(radius)) {
565 catch (
const std::exception& ex) {
566 std::cerr << ex.what() << std::endl;
567 if (state ==
"on" || state ==
"dry-run") {
576 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
587 std::cerr <<
"Wrong value for max-faces-per-prefix. ";
608 ConfFileProcessor::processConfSectionAdvertising(
const ConfigSection& section)
610 for (ConfigSection::const_iterator tn =
611 section.begin(); tn != section.end(); ++tn) {
612 if (tn->first ==
"prefix") {
614 std::string prefix = tn->second.data();
615 ndn::Name namePrefix(prefix);
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++) {
651 using namespace boost::filesystem;
653 if (it->first !=
"cert-to-publish") {
654 std::cerr <<
"Error: Expect cert-to-publish!" << std::endl;
658 std::string file = it->second.data();
659 path certfilePath = absolute(file, path(m_confFileName).parent_path());
660 std::shared_ptr<ndn::security::v2::Certificate> idCert =
661 ndn::io::load<ndn::security::v2::Certificate>(certfilePath.string());
663 if (idCert ==
nullptr) {
664 std::cerr <<
"Error: Cannot load cert-to-publish: " << file <<
"!" << std::endl;
668 m_confParam.getCertStore().insert(*idCert);
669 m_confParam.getValidator().loadAnchor(
"Authoritative-Certificate",
670 ndn::security::v2::Certificate(*idCert));
671 m_confParam.getPrefixUpdateValidator().loadAnchor(
"Authoritative-Certificate",
672 ndn::security::v2::Certificate(*idCert));
ndn::security::ValidatorConfig & getValidator()
A class to house all the configuration parameters for NLSR.
void setLsaInterestLifetime(const ndn::time::seconds &lifetime)
static const float DEFAULT_LINK_COST
void setStateFileDir(const std::string &ssfd)
void setMaxFacesPerPrefix(uint32_t mfpp)
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)
boost::property_tree::ptree ConfigSection
void setRouterName(const ndn::Name &routerName)
void setFaceDatasetFetchInterval(uint32_t interval)
uint32_t getLsaRefreshTime() const
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 setOptional(T defaultValue)
Copyright (c) 2014-2018, 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 setFirstHelloInterval(uint32_t interval)
void setLsaRefreshTime(uint32_t lrt)
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.