blob: af0ad522f86b125692fb5b733b65f03d0eb73286 [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/*
Varun Patila2599da2023-07-12 16:37:15 -07003 * Copyright (c) 2014-2023, 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 Pesavento5849ee72023-11-12 20:00:21 -050031#include <boost/algorithm/string.hpp>
Davide Pesavento22520e62021-06-08 22:16:52 -040032#include <boost/filesystem.hpp>
33#include <boost/property_tree/info_parser.hpp>
34
Nick Gordone98480b2017-05-24 11:23:03 -050035#include <fstream>
Davide Pesavento22520e62021-06-08 22:16:52 -040036#include <iostream>
37
38namespace bf = boost::filesystem;
akmhoque53353462014-04-22 08:43:45 -050039
akmhoque53353462014-04-22 08:43:45 -050040namespace nlsr {
41
Vince Lehman7b616582014-10-17 16:25:39 -050042template <class T>
43class ConfigurationVariable
44{
45public:
dmcoomes9f936662017-03-02 10:33:09 -060046 typedef std::function<void(T)> ConfParameterCallback;
Vince Lehman7b616582014-10-17 16:25:39 -050047
48 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
49 : m_key(key)
50 , m_setterCallback(setter)
51 , m_minValue(0)
52 , m_maxValue(0)
53 , m_shouldCheckRange(false)
54 , m_isRequired(true)
55 {
56 }
57
58 bool
59 parseFromConfigSection(const ConfigSection& section)
60 {
61 try {
62 T value = section.get<T>(m_key);
63
64 if (!isValidValue(value)) {
65 return false;
66 }
67
68 m_setterCallback(value);
69 return true;
70 }
71 catch (const std::exception& ex) {
72
73 if (m_isRequired) {
74 std::cerr << ex.what() << std::endl;
75 std::cerr << "Missing required configuration variable" << std::endl;
76 return false;
77 }
78 else {
79 m_setterCallback(m_defaultValue);
80 return true;
81 }
82 }
83
84 return false;
85 }
86
87 void
88 setMinAndMaxValue(T min, T max)
89 {
90 m_minValue = min;
91 m_maxValue = max;
92 m_shouldCheckRange = true;
93 }
94
95 void
96 setOptional(T defaultValue)
97 {
98 m_isRequired = false;
99 m_defaultValue = defaultValue;
100 }
101
102private:
103 void
104 printOutOfRangeError(T value)
105 {
106 std::cerr << "Invalid value for " << m_key << ": "
107 << value << ". "
108 << "Valid values: "
109 << m_minValue << " - "
110 << m_maxValue << std::endl;
111 }
112
113 bool
114 isValidValue(T value)
115 {
116 if (!m_shouldCheckRange) {
117 return true;
118 }
119 else if (value < m_minValue || value > m_maxValue)
120 {
121 printOutOfRangeError(value);
122 return false;
123 }
124
125 return true;
126 }
127
128private:
129 const std::string m_key;
130 const ConfParameterCallback m_setterCallback;
Vince Lehman7b616582014-10-17 16:25:39 -0500131
Davide Pesavento5849ee72023-11-12 20:00:21 -0500132 T m_defaultValue;
Vince Lehman7b616582014-10-17 16:25:39 -0500133 T m_minValue;
134 T m_maxValue;
135
136 bool m_shouldCheckRange;
137 bool m_isRequired;
138};
139
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140ConfFileProcessor::ConfFileProcessor(ConfParameter& confParam)
141 : m_confFileName(confParam.getConfFileName())
142 , m_confParam(confParam)
143{
144}
145
akmhoque157b0a42014-05-13 00:26:37 -0500146bool
akmhoqueb6450b12014-04-24 00:01:03 -0500147ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500148{
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400149 std::ifstream inputFile(m_confFileName);
akmhoque157b0a42014-05-13 00:26:37 -0500150 if (!inputFile.is_open()) {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400151 std::cerr << "Failed to read configuration file: " << m_confFileName << std::endl;
akmhoquead5fe952014-06-26 13:34:12 -0500152 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500153 }
Saurab Dulal427e0122019-11-28 11:58:02 -0600154
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400155 if (!load(inputFile)) {
156 return false;
Saurab Dulal427e0122019-11-28 11:58:02 -0600157 }
158
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400159 m_confParam.buildRouterAndSyncUserPrefix();
160 m_confParam.writeLog();
161 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500162}
163
164bool
Nick Gordone98480b2017-05-24 11:23:03 -0500165ConfFileProcessor::load(std::istream& input)
akmhoque157b0a42014-05-13 00:26:37 -0500166{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700167 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500168 try {
169 boost::property_tree::read_info(input, pt);
170 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400171 catch (const boost::property_tree::ptree_error& e) {
172 std::cerr << "Failed to parse configuration file '" << m_confFileName
173 << "': " << e.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500174 return false;
175 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700176
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600177 for (const auto& tn : pt) {
178 if (!processSection(tn.first, tn.second)) {
179 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500180 }
181 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600182 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500183}
184
185bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700186ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500187{
188 bool ret = true;
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400189 if (sectionName == "general") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700190 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500191 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400192 else if (sectionName == "neighbors") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700193 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500194 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400195 else if (sectionName == "hyperbolic") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700196 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500197 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400198 else if (sectionName == "fib") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700199 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500200 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400201 else if (sectionName == "advertising") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700202 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500203 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400204 else if (sectionName == "security") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700205 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700206 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400207 else {
208 std::cerr << "Unknown configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500209 }
210 return ret;
211}
212
213bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700214ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500215{
Varun Patila2599da2023-07-12 16:37:15 -0700216 // sync-protocol
217 std::string syncProtocol = section.get<std::string>("sync-protocol", "psync");
218 if (syncProtocol == "chronosync") {
219#ifdef HAVE_CHRONOSYNC
220 m_confParam.setSyncProtocol(SyncProtocol::CHRONOSYNC);
221#else
222 std::cerr << "NLSR was compiled without ChronoSync support!\n";
223 return false;
224#endif
225 }
226 else if (syncProtocol == "psync") {
227#ifdef HAVE_PSYNC
228 m_confParam.setSyncProtocol(SyncProtocol::PSYNC);
229#else
230 std::cerr << "NLSR was compiled without PSync support!\n";
231 return false;
232#endif
233 }
234 else if (syncProtocol == "svs") {
235#ifdef HAVE_SVS
236 m_confParam.setSyncProtocol(SyncProtocol::SVS);
237#else
238 std::cerr << "NLSR was compiled without SVS support!\n";
239 return false;
240#endif
241 }
242 else {
243 std::cerr << "Sync protocol '" << syncProtocol << "' is not supported!\n"
244 << "Use 'chronosync' or 'psync' or 'svs'\n";
245 return false;
246 }
247
akmhoque157b0a42014-05-13 00:26:37 -0500248 try {
Nick Gordone98480b2017-05-24 11:23:03 -0500249 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");
akmhoque157b0a42014-05-13 00:26:37 -0500252 ndn::Name networkName(network);
253 if (!networkName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600254 m_confParam.setNetwork(networkName);
akmhoque157b0a42014-05-13 00:26:37 -0500255 }
256 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400257 std::cerr << "Network can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500258 return false;
259 }
260 ndn::Name siteName(site);
261 if (!siteName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600262 m_confParam.setSiteName(siteName);
akmhoque157b0a42014-05-13 00:26:37 -0500263 }
264 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400265 std::cerr << "Site can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500266 return false;
267 }
268 ndn::Name routerName(router);
269 if (!routerName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600270 m_confParam.setRouterName(routerName);
akmhoque157b0a42014-05-13 00:26:37 -0500271 }
272 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400273 std::cerr << "Router name can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500274 return false;
275 }
276 }
277 catch (const std::exception& ex) {
Nick Gordone98480b2017-05-24 11:23:03 -0500278 std::cerr << ex.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500279 return false;
280 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700281
alvya2228c62014-12-09 10:25:11 -0600282 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600283 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600284
285 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600286 m_confParam.setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500287 }
alvya2228c62014-12-09 10:25:11 -0600288 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400289 std::cerr << "Invalid value for lsa-refresh-time. "
290 << "Allowed range: " << LSA_REFRESH_TIME_MIN
291 << "-" << LSA_REFRESH_TIME_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600292 return false;
293 }
294
295 // router-dead-interval
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400296 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", 2 * lsaRefreshTime);
alvya2228c62014-12-09 10:25:11 -0600297
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600298 if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
299 m_confParam.setRouterDeadInterval(routerDeadInterval);
alvya2228c62014-12-09 10:25:11 -0600300 }
301 else {
302 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
303 return false;
304 }
305
306 // lsa-interest-lifetime
307 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
308
309 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600310 m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
alvya2228c62014-12-09 10:25:11 -0600311 }
312 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400313 std::cerr << "Invalid value for lsa-interest-timeout. "
314 << "Allowed range: " << LSA_INTEREST_LIFETIME_MIN
315 << "-" << LSA_INTEREST_LIFETIME_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500316 return false;
317 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700318
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500319 // sync-interest-lifetime
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600320 uint32_t syncInterestLifetime = section.get<uint32_t>("sync-interest-lifetime",
321 SYNC_INTEREST_LIFETIME_DEFAULT);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600322 if (syncInterestLifetime >= SYNC_INTEREST_LIFETIME_MIN &&
323 syncInterestLifetime <= SYNC_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600324 m_confParam.setSyncInterestLifetime(syncInterestLifetime);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600325 }
326 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400327 std::cerr << "Invalid value for sync-interest-lifetime. "
328 << "Allowed range: " << SYNC_INTEREST_LIFETIME_MIN
329 << "-" << SYNC_INTEREST_LIFETIME_MAX << std::endl;
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600330 return false;
331 }
332
akmhoque674b0b12014-05-20 14:33:28 -0500333 try {
dulalsaurab82a34c22019-02-04 17:31:21 +0000334 std::string stateDir = section.get<std::string>("state-dir");
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600335 if (bf::exists(stateDir)) {
336 if (bf::is_directory(stateDir)) {
Davide Pesavento22520e62021-06-08 22:16:52 -0400337 // copying nlsr.conf file to a user-defined directory for possible modification
338 std::string conFileDynamic = (bf::path(stateDir) / "nlsr.conf").string();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600339
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;
347 return false;
348 }
349
dulalsaurab82a34c22019-02-04 17:31:21 +0000350 m_confParam.setConfFileNameDynamic(conFileDynamic);
351 try {
Davide Pesavento22520e62021-06-08 22:16:52 -0400352 bf::copy_file(m_confFileName, conFileDynamic,
353#if BOOST_VERSION >= 107400
354 bf::copy_options::overwrite_existing
355#else
356 bf::copy_option::overwrite_if_exists
357#endif
358 );
dulalsaurab82a34c22019-02-04 17:31:21 +0000359 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600360 catch (const bf::filesystem_error& e) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000361 std::cerr << "Error copying conf file to the state directory: " << e.what() << std::endl;
Davide Pesavento22520e62021-06-08 22:16:52 -0400362 return false;
dulalsaurab82a34c22019-02-04 17:31:21 +0000363 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600364
Davide Pesavento22520e62021-06-08 22:16:52 -0400365 std::string testFileName = (bf::path(stateDir) / "test.seq").string();
dulalsaurab82a34c22019-02-04 17:31:21 +0000366 std::ofstream testOutFile(testFileName);
367 if (testOutFile) {
368 m_confParam.setStateFileDir(stateDir);
akmhoque674b0b12014-05-20 14:33:28 -0500369 }
370 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400371 std::cerr << "NLSR does not have read/write permission on the state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500372 return false;
373 }
374 testOutFile.close();
375 remove(testFileName.c_str());
376 }
377 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400378 std::cerr << "Provided path '" << stateDir << "' is not a directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500379 return false;
380 }
381 }
382 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400383 std::cerr << "Provided state directory '" << stateDir << "' does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500384 return false;
385 }
386 }
387 catch (const std::exception& ex) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000388 std::cerr << "You must configure state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500389 std::cerr << ex.what() << std::endl;
390 return false;
391 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700392
akmhoque157b0a42014-05-13 00:26:37 -0500393 return true;
394}
395
396bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700397ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500398{
alvya2228c62014-12-09 10:25:11 -0600399 // hello-retries
400 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
401
402 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600403 m_confParam.setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500404 }
alvya2228c62014-12-09 10:25:11 -0600405 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400406 std::cerr << "Invalid value for hello-retries. "
407 << "Allowed range: " << HELLO_RETRIES_MIN << "-" << HELLO_RETRIES_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500408 return false;
409 }
alvya2228c62014-12-09 10:25:11 -0600410
411 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600412 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600413
414 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600415 m_confParam.setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500416 }
alvya2228c62014-12-09 10:25:11 -0600417 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400418 std::cerr << "Invalid value for hello-timeout. "
419 << "Allowed range: " << HELLO_TIMEOUT_MIN << "-" << HELLO_TIMEOUT_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600420 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500421 }
alvya2228c62014-12-09 10:25:11 -0600422
423 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600424 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600425
426 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600427 m_confParam.setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500428 }
alvya2228c62014-12-09 10:25:11 -0600429 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400430 std::cerr << "Invalid value for hello-interval. "
431 << "Allowed range: " << HELLO_INTERVAL_MIN << "-" << HELLO_INTERVAL_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600432 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500433 }
Vince Lehman7b616582014-10-17 16:25:39 -0500434
435 // Event intervals
436 // adj-lsa-build-interval
437 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600438 std::bind(&ConfParameter::setAdjLsaBuildInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600439 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500440 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
441 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
442
443 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
444 return false;
445 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500446 // Set the retry count for fetching the FaceStatus dataset
447 ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
448 std::bind(&ConfParameter::setFaceDatasetFetchTries,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400449 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500450
451 faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
452 FACE_DATASET_FETCH_TRIES_MAX);
453 faceDatasetFetchTries.setOptional(FACE_DATASET_FETCH_TRIES_DEFAULT);
454
455 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
456 return false;
457 }
458
459 // Set the interval between FaceStatus dataset fetch attempts.
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500460 ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
Davide Pesaventod90338d2021-01-07 17:50:05 -0500461 std::bind(&ConfParameter::setFaceDatasetFetchInterval,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400462 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500463
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500464 faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
465 FACE_DATASET_FETCH_INTERVAL_MAX);
466 faceDatasetFetchInterval.setOptional(FACE_DATASET_FETCH_INTERVAL_DEFAULT);
Nick Gordond5c1a372016-10-31 13:56:23 -0500467
468 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
469 return false;
470 }
Vince Lehman7b616582014-10-17 16:25:39 -0500471
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600472 for (const auto& tn : section) {
473 if (tn.first == "neighbor") {
akmhoque157b0a42014-05-13 00:26:37 -0500474 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600475 ConfigSection CommandAttriTree = tn.second;
akmhoque157b0a42014-05-13 00:26:37 -0500476 std::string name = CommandAttriTree.get<std::string>("name");
Nick Gordone9733ed2017-04-26 10:48:39 -0500477 std::string uriString = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600478
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500479 ndn::FaceUri faceUri;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400480 if (!faceUri.parse(uriString)) {
481 std::cerr << "face-uri parsing failed" << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600482 return false;
483 }
484
485 bool failedToCanonize = false;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400486 faceUri.canonize([&faceUri] (const auto& canonicalUri) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600487 faceUri = canonicalUri;
488 },
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400489 [&faceUri, &failedToCanonize] (const auto& reason) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600490 failedToCanonize = true;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400491 std::cerr << "Could not canonize URI: '" << faceUri
492 << "' because: " << reason << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600493 },
494 m_io,
495 TIME_ALLOWED_FOR_CANONIZATION);
496 m_io.run();
Davide Pesavento5849ee72023-11-12 20:00:21 -0500497 m_io.restart();
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600498
499 if (failedToCanonize) {
alvy2fe12872014-11-25 10:32:23 -0600500 return false;
501 }
502
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400503 double linkCost = CommandAttriTree.get<double>("link-cost", Adjacent::DEFAULT_LINK_COST);
akmhoque157b0a42014-05-13 00:26:37 -0500504 ndn::Name neighborName(name);
505 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500506 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600507 m_confParam.getAdjacencyList().insert(adj);
akmhoque157b0a42014-05-13 00:26:37 -0500508 }
509 else {
akmhoque674b0b12014-05-20 14:33:28 -0500510 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500511 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500512 }
513 }
akmhoque157b0a42014-05-13 00:26:37 -0500514 catch (const std::exception& ex) {
515 std::cerr << ex.what() << std::endl;
516 return false;
517 }
akmhoque53353462014-04-22 08:43:45 -0500518 }
519 }
akmhoque157b0a42014-05-13 00:26:37 -0500520 return true;
akmhoque53353462014-04-22 08:43:45 -0500521}
522
akmhoque157b0a42014-05-13 00:26:37 -0500523bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700524ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500525{
alvya2228c62014-12-09 10:25:11 -0600526 // state
Nick Gordone98480b2017-05-24 11:23:03 -0500527 std::string state = section.get<std::string>("state", "off");
alvya2228c62014-12-09 10:25:11 -0600528
529 if (boost::iequals(state, "off")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600530 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500531 }
alvya2228c62014-12-09 10:25:11 -0600532 else if (boost::iequals(state, "on")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600533 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
alvya2228c62014-12-09 10:25:11 -0600534 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400535 else if (boost::iequals(state, "dry-run")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600536 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
alvya2228c62014-12-09 10:25:11 -0600537 }
538 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400539 std::cerr << "Invalid setting for hyperbolic state. "
540 << "Allowed values: off, on, dry-run" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500541 return false;
akmhoque53353462014-04-22 08:43:45 -0500542 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700543
akmhoque157b0a42014-05-13 00:26:37 -0500544 try {
Laqin Fan54a43f02017-03-08 12:31:30 -0600545 // Radius and angle(s) are mandatory configuration parameters in hyperbolic section.
546 // Even if router can have hyperbolic routing calculation off but other router
547 // in the network may use hyperbolic routing calculation for FIB generation.
548 // So each router need to advertise its hyperbolic coordinates in the network
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700549 double radius = section.get<double>("radius");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600550 std::string angleString = section.get<std::string>("angle");
551
552 std::stringstream ss(angleString);
553 std::vector<double> angles;
554
555 double angle;
556
Laqin Fan54a43f02017-03-08 12:31:30 -0600557 while (ss >> angle) {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600558 angles.push_back(angle);
Laqin Fan54a43f02017-03-08 12:31:30 -0600559 if (ss.peek() == ',' || ss.peek() == ' ') {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600560 ss.ignore();
561 }
562 }
563
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600564 if (!m_confParam.setCorR(radius)) {
akmhoque157b0a42014-05-13 00:26:37 -0500565 return false;
566 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600567 m_confParam.setCorTheta(angles);
akmhoque53353462014-04-22 08:43:45 -0500568 }
akmhoque157b0a42014-05-13 00:26:37 -0500569 catch (const std::exception& ex) {
570 std::cerr << ex.what() << std::endl;
571 if (state == "on" || state == "dry-run") {
572 return false;
573 }
akmhoque53353462014-04-22 08:43:45 -0500574 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700575
akmhoque157b0a42014-05-13 00:26:37 -0500576 return true;
akmhoque53353462014-04-22 08:43:45 -0500577}
578
akmhoque157b0a42014-05-13 00:26:37 -0500579bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700580ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500581{
alvya2228c62014-12-09 10:25:11 -0600582 // max-faces-per-prefix
583 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
584
585 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400586 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600587 m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500588 }
alvya2228c62014-12-09 10:25:11 -0600589 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400590 std::cerr << "Invalid value for max-faces-per-prefix. "
591 << "Allowed range: " << MAX_FACES_PER_PREFIX_MIN
592 << "-" << MAX_FACES_PER_PREFIX_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500593 return false;
594 }
Vince Lehman7b616582014-10-17 16:25:39 -0500595
596 // routing-calc-interval
597 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600598 std::bind(&ConfParameter::setRoutingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600599 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500600 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
601 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
602
603 if (!routingCalcInterval.parseFromConfigSection(section)) {
604 return false;
605 }
606
akmhoque157b0a42014-05-13 00:26:37 -0500607 return true;
akmhoque53353462014-04-22 08:43:45 -0500608}
609
akmhoque157b0a42014-05-13 00:26:37 -0500610bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700611ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500612{
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600613 for (const auto& tn : section) {
614 if (tn.first == "prefix") {
akmhoque157b0a42014-05-13 00:26:37 -0500615 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600616 ndn::Name namePrefix(tn.second.data());
akmhoque157b0a42014-05-13 00:26:37 -0500617 if (!namePrefix.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600618 m_confParam.getNamePrefixList().insert(namePrefix);
akmhoque157b0a42014-05-13 00:26:37 -0500619 }
620 else {
akmhoque674b0b12014-05-20 14:33:28 -0500621 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500622 return false;
623 }
624 }
625 catch (const std::exception& ex) {
626 std::cerr << ex.what() << std::endl;
627 return false;
628 }
akmhoque53353462014-04-22 08:43:45 -0500629 }
akmhoque53353462014-04-22 08:43:45 -0500630 }
akmhoque157b0a42014-05-13 00:26:37 -0500631 return true;
akmhoque53353462014-04-22 08:43:45 -0500632}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700633
634bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700635ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700636{
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400637 auto it = section.begin();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700638
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500639 if (it == section.end() || it->first != "validator") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400640 std::cerr << "Error: Expected validator section!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500641 return false;
642 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700643
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600644 m_confParam.getValidator().load(it->second, m_confFileName);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500645
akmhoqued57f3672014-06-10 10:41:32 -0500646 it++;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500647 if (it != section.end() && it->first == "prefix-update-validator") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600648 m_confParam.getPrefixUpdateValidator().load(it->second, m_confFileName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700649
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500650 it++;
651 for (; it != section.end(); it++) {
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500652 if (it->first != "cert-to-publish") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400653 std::cerr << "Error: Expected cert-to-publish!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500654 return false;
655 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700656
657 std::string file = it->second.data();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600658 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400659 std::ifstream ifs(certfilePath.string());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700660
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400661 ndn::security::Certificate idCert;
662 try {
663 idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
664 }
665 catch (const std::exception& e) {
666 std::cerr << "Error: Cannot load cert-to-publish '" << file << "': " << e.what() << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500667 return false;
668 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400669
Saurab Dulal427e0122019-11-28 11:58:02 -0600670 m_confParam.addCertPath(certfilePath.string());
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400671 m_confParam.loadCertToValidator(idCert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700672 }
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500673 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700674
675 return true;
676}
677
alvy2fe12872014-11-25 10:32:23 -0600678} // namespace nlsr