blob: b9ab4540988129605761b30e4d8b76ccde9aeecd [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento4b9d30f2020-05-01 02:48:34 -04002/*
Davide Pesavento1954a0c2022-09-30 15:56:04 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040020 */
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
Ashlesh Gawande3909aa12017-07-28 16:01:35 -050022#include "conf-file-processor.hpp"
23#include "adjacent.hpp"
Ashlesh Gawande3909aa12017-07-28 16:01:35 -050024#include "update/prefix-update-processor.hpp"
Davide Pesavento7bc3d432021-10-25 21:08:04 -040025#include "utility/name-helper.hpp"
Ashlesh Gawande3909aa12017-07-28 16:01:35 -050026
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080027#include <ndn-cxx/name.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050028#include <ndn-cxx/net/face-uri.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040029#include <ndn-cxx/util/io.hpp>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080030
Davide Pesavento22520e62021-06-08 22:16:52 -040031#include <boost/filesystem.hpp>
32#include <boost/property_tree/info_parser.hpp>
33
Nick Gordone98480b2017-05-24 11:23:03 -050034#include <fstream>
Davide Pesavento22520e62021-06-08 22:16:52 -040035#include <iostream>
36
37namespace bf = boost::filesystem;
akmhoque53353462014-04-22 08:43:45 -050038
akmhoque53353462014-04-22 08:43:45 -050039namespace nlsr {
40
Vince Lehman7b616582014-10-17 16:25:39 -050041template <class T>
42class ConfigurationVariable
43{
44public:
dmcoomes9f936662017-03-02 10:33:09 -060045 typedef std::function<void(T)> ConfParameterCallback;
Vince Lehman7b616582014-10-17 16:25:39 -050046
47 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
48 : m_key(key)
49 , m_setterCallback(setter)
50 , m_minValue(0)
51 , m_maxValue(0)
52 , m_shouldCheckRange(false)
53 , m_isRequired(true)
54 {
55 }
56
57 bool
58 parseFromConfigSection(const ConfigSection& section)
59 {
60 try {
61 T value = section.get<T>(m_key);
62
63 if (!isValidValue(value)) {
64 return false;
65 }
66
67 m_setterCallback(value);
68 return true;
69 }
70 catch (const std::exception& ex) {
71
72 if (m_isRequired) {
73 std::cerr << ex.what() << std::endl;
74 std::cerr << "Missing required configuration variable" << std::endl;
75 return false;
76 }
77 else {
78 m_setterCallback(m_defaultValue);
79 return true;
80 }
81 }
82
83 return false;
84 }
85
86 void
87 setMinAndMaxValue(T min, T max)
88 {
89 m_minValue = min;
90 m_maxValue = max;
91 m_shouldCheckRange = true;
92 }
93
94 void
95 setOptional(T defaultValue)
96 {
97 m_isRequired = false;
98 m_defaultValue = defaultValue;
99 }
100
101private:
102 void
103 printOutOfRangeError(T value)
104 {
105 std::cerr << "Invalid value for " << m_key << ": "
106 << value << ". "
107 << "Valid values: "
108 << m_minValue << " - "
109 << m_maxValue << std::endl;
110 }
111
112 bool
113 isValidValue(T value)
114 {
115 if (!m_shouldCheckRange) {
116 return true;
117 }
118 else if (value < m_minValue || value > m_maxValue)
119 {
120 printOutOfRangeError(value);
121 return false;
122 }
123
124 return true;
125 }
126
127private:
128 const std::string m_key;
129 const ConfParameterCallback m_setterCallback;
130 T m_defaultValue;
131
132 T m_minValue;
133 T m_maxValue;
134
135 bool m_shouldCheckRange;
136 bool m_isRequired;
137};
138
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600139ConfFileProcessor::ConfFileProcessor(ConfParameter& confParam)
140 : m_confFileName(confParam.getConfFileName())
141 , m_confParam(confParam)
142{
143}
144
akmhoque157b0a42014-05-13 00:26:37 -0500145bool
akmhoqueb6450b12014-04-24 00:01:03 -0500146ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500147{
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400148 std::ifstream inputFile(m_confFileName);
akmhoque157b0a42014-05-13 00:26:37 -0500149 if (!inputFile.is_open()) {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400150 std::cerr << "Failed to read configuration file: " << m_confFileName << std::endl;
akmhoquead5fe952014-06-26 13:34:12 -0500151 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500152 }
Saurab Dulal427e0122019-11-28 11:58:02 -0600153
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400154 if (!load(inputFile)) {
155 return false;
Saurab Dulal427e0122019-11-28 11:58:02 -0600156 }
157
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400158 m_confParam.buildRouterAndSyncUserPrefix();
159 m_confParam.writeLog();
160 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500161}
162
163bool
Nick Gordone98480b2017-05-24 11:23:03 -0500164ConfFileProcessor::load(std::istream& input)
akmhoque157b0a42014-05-13 00:26:37 -0500165{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700166 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500167 try {
168 boost::property_tree::read_info(input, pt);
169 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400170 catch (const boost::property_tree::ptree_error& e) {
171 std::cerr << "Failed to parse configuration file '" << m_confFileName
172 << "': " << e.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500173 return false;
174 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700175
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600176 for (const auto& tn : pt) {
177 if (!processSection(tn.first, tn.second)) {
178 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500179 }
180 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600181 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500182}
183
184bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700185ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500186{
187 bool ret = true;
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400188 if (sectionName == "general") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700189 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500190 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400191 else if (sectionName == "neighbors") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700192 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500193 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400194 else if (sectionName == "hyperbolic") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700195 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500196 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400197 else if (sectionName == "fib") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700198 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500199 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400200 else if (sectionName == "advertising") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700201 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500202 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400203 else if (sectionName == "security") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700204 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700205 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400206 else {
207 std::cerr << "Unknown configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500208 }
209 return ret;
210}
211
212bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700213ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500214{
215 try {
Nick Gordone98480b2017-05-24 11:23:03 -0500216 std::string network = section.get<std::string>("network");
217 std::string site = section.get<std::string>("site");
218 std::string router = section.get<std::string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500219 ndn::Name networkName(network);
220 if (!networkName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600221 m_confParam.setNetwork(networkName);
akmhoque157b0a42014-05-13 00:26:37 -0500222 }
223 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400224 std::cerr << "Network can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500225 return false;
226 }
227 ndn::Name siteName(site);
228 if (!siteName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600229 m_confParam.setSiteName(siteName);
akmhoque157b0a42014-05-13 00:26:37 -0500230 }
231 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400232 std::cerr << "Site can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500233 return false;
234 }
235 ndn::Name routerName(router);
236 if (!routerName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600237 m_confParam.setRouterName(routerName);
akmhoque157b0a42014-05-13 00:26:37 -0500238 }
239 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400240 std::cerr << "Router name can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500241 return false;
242 }
243 }
244 catch (const std::exception& ex) {
Nick Gordone98480b2017-05-24 11:23:03 -0500245 std::cerr << ex.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500246 return false;
247 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700248
alvya2228c62014-12-09 10:25:11 -0600249 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600250 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600251
252 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600253 m_confParam.setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500254 }
alvya2228c62014-12-09 10:25:11 -0600255 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400256 std::cerr << "Invalid value for lsa-refresh-time. "
257 << "Allowed range: " << LSA_REFRESH_TIME_MIN
258 << "-" << LSA_REFRESH_TIME_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600259 return false;
260 }
261
262 // router-dead-interval
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400263 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", 2 * lsaRefreshTime);
alvya2228c62014-12-09 10:25:11 -0600264
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600265 if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
266 m_confParam.setRouterDeadInterval(routerDeadInterval);
alvya2228c62014-12-09 10:25:11 -0600267 }
268 else {
269 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
270 return false;
271 }
272
273 // lsa-interest-lifetime
274 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
275
276 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600277 m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
alvya2228c62014-12-09 10:25:11 -0600278 }
279 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400280 std::cerr << "Invalid value for lsa-interest-timeout. "
281 << "Allowed range: " << LSA_INTEREST_LIFETIME_MIN
282 << "-" << LSA_INTEREST_LIFETIME_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500283 return false;
284 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700285
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500286 // sync-protocol
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700287 std::string syncProtocol = section.get<std::string>("sync-protocol", "psync");
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500288 if (syncProtocol == "chronosync") {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700289#ifdef HAVE_CHRONOSYNC
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400290 m_confParam.setSyncProtocol(SyncProtocol::CHRONOSYNC);
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700291#else
Varun Patil7d2d6892022-10-14 12:50:30 -0700292 std::cerr << "NLSR was compiled without ChronoSync support!\n";
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700293 return false;
294#endif
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500295 }
296 else if (syncProtocol == "psync") {
Varun Patil7d2d6892022-10-14 12:50:30 -0700297#ifdef HAVE_PSYNC
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400298 m_confParam.setSyncProtocol(SyncProtocol::PSYNC);
Varun Patil7d2d6892022-10-14 12:50:30 -0700299#else
300 std::cerr << "NLSR was compiled without PSync support!\n";
301 return false;
302#endif
303 }
304 else if (syncProtocol == "svs") {
305#ifdef HAVE_SVS
306 m_confParam.setSyncProtocol(SyncProtocol::SVS);
307#else
308 std::cerr << "NLSR was compiled without SVS support!\n";
309 return false;
310#endif
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500311 }
312 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400313 std::cerr << "Sync protocol '" << syncProtocol << "' is not supported!\n"
Varun Patil7d2d6892022-10-14 12:50:30 -0700314 << "Use 'chronosync' or 'psync' or 'svs'\n";
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500315 return false;
316 }
317
318 // sync-interest-lifetime
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600319 uint32_t syncInterestLifetime = section.get<uint32_t>("sync-interest-lifetime",
320 SYNC_INTEREST_LIFETIME_DEFAULT);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600321 if (syncInterestLifetime >= SYNC_INTEREST_LIFETIME_MIN &&
322 syncInterestLifetime <= SYNC_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600323 m_confParam.setSyncInterestLifetime(syncInterestLifetime);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600324 }
325 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400326 std::cerr << "Invalid value for sync-interest-lifetime. "
327 << "Allowed range: " << SYNC_INTEREST_LIFETIME_MIN
328 << "-" << SYNC_INTEREST_LIFETIME_MAX << std::endl;
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600329 return false;
330 }
331
akmhoque674b0b12014-05-20 14:33:28 -0500332 try {
dulalsaurab82a34c22019-02-04 17:31:21 +0000333 std::string stateDir = section.get<std::string>("state-dir");
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600334 if (bf::exists(stateDir)) {
335 if (bf::is_directory(stateDir)) {
Davide Pesavento22520e62021-06-08 22:16:52 -0400336 // copying nlsr.conf file to a user-defined directory for possible modification
337 std::string conFileDynamic = (bf::path(stateDir) / "nlsr.conf").string();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600338
339 if (m_confFileName == conFileDynamic) {
340 std::cerr << "Please use nlsr.conf stored at another location "
341 << "or change the state-dir in the configuration." << std::endl;
342 std::cerr << "The file at " << conFileDynamic <<
343 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
344 std::cerr << "The dynamic file can be used for next run "
345 << "after copying to another location." << std::endl;
346 return false;
347 }
348
dulalsaurab82a34c22019-02-04 17:31:21 +0000349 m_confParam.setConfFileNameDynamic(conFileDynamic);
350 try {
Davide Pesavento22520e62021-06-08 22:16:52 -0400351 bf::copy_file(m_confFileName, conFileDynamic,
352#if BOOST_VERSION >= 107400
353 bf::copy_options::overwrite_existing
354#else
355 bf::copy_option::overwrite_if_exists
356#endif
357 );
dulalsaurab82a34c22019-02-04 17:31:21 +0000358 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600359 catch (const bf::filesystem_error& e) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000360 std::cerr << "Error copying conf file to the state directory: " << e.what() << std::endl;
Davide Pesavento22520e62021-06-08 22:16:52 -0400361 return false;
dulalsaurab82a34c22019-02-04 17:31:21 +0000362 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600363
Davide Pesavento22520e62021-06-08 22:16:52 -0400364 std::string testFileName = (bf::path(stateDir) / "test.seq").string();
dulalsaurab82a34c22019-02-04 17:31:21 +0000365 std::ofstream testOutFile(testFileName);
366 if (testOutFile) {
367 m_confParam.setStateFileDir(stateDir);
akmhoque674b0b12014-05-20 14:33:28 -0500368 }
369 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400370 std::cerr << "NLSR does not have read/write permission on the state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500371 return false;
372 }
373 testOutFile.close();
374 remove(testFileName.c_str());
375 }
376 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400377 std::cerr << "Provided path '" << stateDir << "' is not a directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500378 return false;
379 }
380 }
381 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400382 std::cerr << "Provided state directory '" << stateDir << "' does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500383 return false;
384 }
385 }
386 catch (const std::exception& ex) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000387 std::cerr << "You must configure state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500388 std::cerr << ex.what() << std::endl;
389 return false;
390 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700391
akmhoque157b0a42014-05-13 00:26:37 -0500392 return true;
393}
394
395bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700396ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500397{
alvya2228c62014-12-09 10:25:11 -0600398 // hello-retries
399 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
400
401 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600402 m_confParam.setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500403 }
alvya2228c62014-12-09 10:25:11 -0600404 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400405 std::cerr << "Invalid value for hello-retries. "
406 << "Allowed range: " << HELLO_RETRIES_MIN << "-" << HELLO_RETRIES_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500407 return false;
408 }
alvya2228c62014-12-09 10:25:11 -0600409
410 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600411 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600412
413 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600414 m_confParam.setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500415 }
alvya2228c62014-12-09 10:25:11 -0600416 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400417 std::cerr << "Invalid value for hello-timeout. "
418 << "Allowed range: " << HELLO_TIMEOUT_MIN << "-" << HELLO_TIMEOUT_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600419 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500420 }
alvya2228c62014-12-09 10:25:11 -0600421
422 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600423 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600424
425 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600426 m_confParam.setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500427 }
alvya2228c62014-12-09 10:25:11 -0600428 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400429 std::cerr << "Invalid value for hello-interval. "
430 << "Allowed range: " << HELLO_INTERVAL_MIN << "-" << HELLO_INTERVAL_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600431 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500432 }
Vince Lehman7b616582014-10-17 16:25:39 -0500433
434 // Event intervals
435 // adj-lsa-build-interval
436 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600437 std::bind(&ConfParameter::setAdjLsaBuildInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600438 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500439 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
440 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
441
442 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
443 return false;
444 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500445 // Set the retry count for fetching the FaceStatus dataset
446 ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
447 std::bind(&ConfParameter::setFaceDatasetFetchTries,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400448 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500449
450 faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
451 FACE_DATASET_FETCH_TRIES_MAX);
452 faceDatasetFetchTries.setOptional(FACE_DATASET_FETCH_TRIES_DEFAULT);
453
454 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
455 return false;
456 }
457
458 // Set the interval between FaceStatus dataset fetch attempts.
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500459 ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
Davide Pesaventod90338d2021-01-07 17:50:05 -0500460 std::bind(&ConfParameter::setFaceDatasetFetchInterval,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400461 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500462
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500463 faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
464 FACE_DATASET_FETCH_INTERVAL_MAX);
465 faceDatasetFetchInterval.setOptional(FACE_DATASET_FETCH_INTERVAL_DEFAULT);
Nick Gordond5c1a372016-10-31 13:56:23 -0500466
467 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
468 return false;
469 }
Vince Lehman7b616582014-10-17 16:25:39 -0500470
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600471 for (const auto& tn : section) {
472 if (tn.first == "neighbor") {
akmhoque157b0a42014-05-13 00:26:37 -0500473 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600474 ConfigSection CommandAttriTree = tn.second;
akmhoque157b0a42014-05-13 00:26:37 -0500475 std::string name = CommandAttriTree.get<std::string>("name");
Nick Gordone9733ed2017-04-26 10:48:39 -0500476 std::string uriString = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600477
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500478 ndn::FaceUri faceUri;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400479 if (!faceUri.parse(uriString)) {
480 std::cerr << "face-uri parsing failed" << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600481 return false;
482 }
483
484 bool failedToCanonize = false;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400485 faceUri.canonize([&faceUri] (const auto& canonicalUri) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600486 faceUri = canonicalUri;
487 },
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400488 [&faceUri, &failedToCanonize] (const auto& reason) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600489 failedToCanonize = true;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400490 std::cerr << "Could not canonize URI: '" << faceUri
491 << "' because: " << reason << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600492 },
493 m_io,
494 TIME_ALLOWED_FOR_CANONIZATION);
495 m_io.run();
Ashlesh Gawande9ecbdc92019-03-11 13:18:45 -0700496 m_io.reset();
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600497
498 if (failedToCanonize) {
alvy2fe12872014-11-25 10:32:23 -0600499 return false;
500 }
501
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400502 double linkCost = CommandAttriTree.get<double>("link-cost", Adjacent::DEFAULT_LINK_COST);
akmhoque157b0a42014-05-13 00:26:37 -0500503 ndn::Name neighborName(name);
504 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500505 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600506 m_confParam.getAdjacencyList().insert(adj);
akmhoque157b0a42014-05-13 00:26:37 -0500507 }
508 else {
akmhoque674b0b12014-05-20 14:33:28 -0500509 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500510 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500511 }
512 }
akmhoque157b0a42014-05-13 00:26:37 -0500513 catch (const std::exception& ex) {
514 std::cerr << ex.what() << std::endl;
515 return false;
516 }
akmhoque53353462014-04-22 08:43:45 -0500517 }
518 }
akmhoque157b0a42014-05-13 00:26:37 -0500519 return true;
akmhoque53353462014-04-22 08:43:45 -0500520}
521
akmhoque157b0a42014-05-13 00:26:37 -0500522bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700523ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500524{
alvya2228c62014-12-09 10:25:11 -0600525 // state
Nick Gordone98480b2017-05-24 11:23:03 -0500526 std::string state = section.get<std::string>("state", "off");
alvya2228c62014-12-09 10:25:11 -0600527
528 if (boost::iequals(state, "off")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600529 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500530 }
alvya2228c62014-12-09 10:25:11 -0600531 else if (boost::iequals(state, "on")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600532 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
alvya2228c62014-12-09 10:25:11 -0600533 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400534 else if (boost::iequals(state, "dry-run")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600535 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
alvya2228c62014-12-09 10:25:11 -0600536 }
537 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400538 std::cerr << "Invalid setting for hyperbolic state. "
539 << "Allowed values: off, on, dry-run" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500540 return false;
akmhoque53353462014-04-22 08:43:45 -0500541 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700542
akmhoque157b0a42014-05-13 00:26:37 -0500543 try {
Laqin Fan54a43f02017-03-08 12:31:30 -0600544 // Radius and angle(s) are mandatory configuration parameters in hyperbolic section.
545 // Even if router can have hyperbolic routing calculation off but other router
546 // in the network may use hyperbolic routing calculation for FIB generation.
547 // So each router need to advertise its hyperbolic coordinates in the network
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700548 double radius = section.get<double>("radius");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600549 std::string angleString = section.get<std::string>("angle");
550
551 std::stringstream ss(angleString);
552 std::vector<double> angles;
553
554 double angle;
555
Laqin Fan54a43f02017-03-08 12:31:30 -0600556 while (ss >> angle) {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600557 angles.push_back(angle);
Laqin Fan54a43f02017-03-08 12:31:30 -0600558 if (ss.peek() == ',' || ss.peek() == ' ') {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600559 ss.ignore();
560 }
561 }
562
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600563 if (!m_confParam.setCorR(radius)) {
akmhoque157b0a42014-05-13 00:26:37 -0500564 return false;
565 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600566 m_confParam.setCorTheta(angles);
akmhoque53353462014-04-22 08:43:45 -0500567 }
akmhoque157b0a42014-05-13 00:26:37 -0500568 catch (const std::exception& ex) {
569 std::cerr << ex.what() << std::endl;
570 if (state == "on" || state == "dry-run") {
571 return false;
572 }
akmhoque53353462014-04-22 08:43:45 -0500573 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700574
akmhoque157b0a42014-05-13 00:26:37 -0500575 return true;
akmhoque53353462014-04-22 08:43:45 -0500576}
577
akmhoque157b0a42014-05-13 00:26:37 -0500578bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700579ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500580{
alvya2228c62014-12-09 10:25:11 -0600581 // max-faces-per-prefix
582 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
583
584 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400585 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600586 m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500587 }
alvya2228c62014-12-09 10:25:11 -0600588 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400589 std::cerr << "Invalid value for max-faces-per-prefix. "
590 << "Allowed range: " << MAX_FACES_PER_PREFIX_MIN
591 << "-" << MAX_FACES_PER_PREFIX_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500592 return false;
593 }
Vince Lehman7b616582014-10-17 16:25:39 -0500594
595 // routing-calc-interval
596 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600597 std::bind(&ConfParameter::setRoutingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600598 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500599 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
600 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
601
602 if (!routingCalcInterval.parseFromConfigSection(section)) {
603 return false;
604 }
605
akmhoque157b0a42014-05-13 00:26:37 -0500606 return true;
akmhoque53353462014-04-22 08:43:45 -0500607}
608
akmhoque157b0a42014-05-13 00:26:37 -0500609bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700610ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500611{
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600612 for (const auto& tn : section) {
613 if (tn.first == "prefix") {
akmhoque157b0a42014-05-13 00:26:37 -0500614 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600615 ndn::Name namePrefix(tn.second.data());
akmhoque157b0a42014-05-13 00:26:37 -0500616 if (!namePrefix.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600617 m_confParam.getNamePrefixList().insert(namePrefix);
akmhoque157b0a42014-05-13 00:26:37 -0500618 }
619 else {
akmhoque674b0b12014-05-20 14:33:28 -0500620 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500621 return false;
622 }
623 }
624 catch (const std::exception& ex) {
625 std::cerr << ex.what() << std::endl;
626 return false;
627 }
akmhoque53353462014-04-22 08:43:45 -0500628 }
akmhoque53353462014-04-22 08:43:45 -0500629 }
akmhoque157b0a42014-05-13 00:26:37 -0500630 return true;
akmhoque53353462014-04-22 08:43:45 -0500631}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700632
633bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700634ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700635{
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400636 auto it = section.begin();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700637
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500638 if (it == section.end() || it->first != "validator") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400639 std::cerr << "Error: Expected validator section!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500640 return false;
641 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700642
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600643 m_confParam.getValidator().load(it->second, m_confFileName);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500644
akmhoqued57f3672014-06-10 10:41:32 -0500645 it++;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500646 if (it != section.end() && it->first == "prefix-update-validator") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600647 m_confParam.getPrefixUpdateValidator().load(it->second, m_confFileName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700648
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500649 it++;
650 for (; it != section.end(); it++) {
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500651 if (it->first != "cert-to-publish") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400652 std::cerr << "Error: Expected cert-to-publish!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500653 return false;
654 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700655
656 std::string file = it->second.data();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600657 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400658 std::ifstream ifs(certfilePath.string());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700659
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400660 ndn::security::Certificate idCert;
661 try {
662 idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
663 }
664 catch (const std::exception& e) {
665 std::cerr << "Error: Cannot load cert-to-publish '" << file << "': " << e.what() << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500666 return false;
667 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400668
Saurab Dulal427e0122019-11-28 11:58:02 -0600669 m_confParam.addCertPath(certfilePath.string());
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400670 m_confParam.loadCertToValidator(idCert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700671 }
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500672 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700673
674 return true;
675}
676
alvy2fe12872014-11-25 10:32:23 -0600677} // namespace nlsr