blob: 29f22cf24961a40670819d481ca3abc7e3b4ef76 [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 Pesaventob0716542024-12-16 19:12:11 -05003 * Copyright (c) 2014-2024, 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/property_tree/info_parser.hpp>
33
Davide Pesaventob0716542024-12-16 19:12:11 -050034#include <filesystem>
Nick Gordone98480b2017-05-24 11:23:03 -050035#include <fstream>
Davide Pesavento22520e62021-06-08 22:16:52 -040036#include <iostream>
37
akmhoque53353462014-04-22 08:43:45 -050038namespace nlsr {
39
Davide Pesaventob0716542024-12-16 19:12:11 -050040namespace fs = std::filesystem;
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
Davide Pesaventob0716542024-12-16 19:12:11 -0500333 // state-dir
akmhoque674b0b12014-05-20 14:33:28 -0500334 try {
Davide Pesaventob0716542024-12-16 19:12:11 -0500335 fs::path stateDir(section.get<std::string>("state-dir"));
336 if (fs::exists(stateDir)) {
337 if (fs::is_directory(stateDir)) {
Davide Pesavento22520e62021-06-08 22:16:52 -0400338 // copying nlsr.conf file to a user-defined directory for possible modification
Davide Pesaventob0716542024-12-16 19:12:11 -0500339 auto conFileDynamic = stateDir / "nlsr.conf";
340 if (m_confFileName == conFileDynamic.string()) {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600341 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
Davide Pesaventob0716542024-12-16 19:12:11 -0500350 m_confParam.setConfFileNameDynamic(conFileDynamic.string());
dulalsaurab82a34c22019-02-04 17:31:21 +0000351 try {
Davide Pesaventob0716542024-12-16 19:12:11 -0500352 fs::copy_file(m_confFileName, conFileDynamic, fs::copy_options::overwrite_existing);
dulalsaurab82a34c22019-02-04 17:31:21 +0000353 }
Davide Pesaventob0716542024-12-16 19:12:11 -0500354 catch (const fs::filesystem_error& e) {
355 std::cerr << "Error copying conf file to state-dir: " << e.what() << std::endl;
Davide Pesavento22520e62021-06-08 22:16:52 -0400356 return false;
dulalsaurab82a34c22019-02-04 17:31:21 +0000357 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600358
Davide Pesaventob0716542024-12-16 19:12:11 -0500359 auto testFilePath = stateDir / "test.seq";
360 std::ofstream testFile(testFilePath);
361 if (testFile) {
362 m_confParam.setStateFileDir(stateDir.string());
akmhoque674b0b12014-05-20 14:33:28 -0500363 }
364 else {
Davide Pesaventob0716542024-12-16 19:12:11 -0500365 std::cerr << "NLSR does not have read/write permission on state-dir" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500366 return false;
367 }
Davide Pesaventob0716542024-12-16 19:12:11 -0500368 testFile.close();
369 fs::remove(testFilePath);
akmhoque674b0b12014-05-20 14:33:28 -0500370 }
371 else {
Davide Pesaventob0716542024-12-16 19:12:11 -0500372 std::cerr << "Provided state-dir " << stateDir << " is not a directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500373 return false;
374 }
375 }
376 else {
Davide Pesaventob0716542024-12-16 19:12:11 -0500377 std::cerr << "Provided state-dir " << stateDir << " does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500378 return false;
379 }
380 }
381 catch (const std::exception& ex) {
Davide Pesaventob0716542024-12-16 19:12:11 -0500382 std::cerr << "You must configure state-dir" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500383 std::cerr << ex.what() << std::endl;
384 return false;
385 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700386
akmhoque157b0a42014-05-13 00:26:37 -0500387 return true;
388}
389
390bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700391ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500392{
alvya2228c62014-12-09 10:25:11 -0600393 // hello-retries
394 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
395
396 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600397 m_confParam.setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500398 }
alvya2228c62014-12-09 10:25:11 -0600399 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400400 std::cerr << "Invalid value for hello-retries. "
401 << "Allowed range: " << HELLO_RETRIES_MIN << "-" << HELLO_RETRIES_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500402 return false;
403 }
alvya2228c62014-12-09 10:25:11 -0600404
405 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600406 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600407
408 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600409 m_confParam.setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500410 }
alvya2228c62014-12-09 10:25:11 -0600411 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400412 std::cerr << "Invalid value for hello-timeout. "
413 << "Allowed range: " << HELLO_TIMEOUT_MIN << "-" << HELLO_TIMEOUT_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600414 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500415 }
alvya2228c62014-12-09 10:25:11 -0600416
417 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600418 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600419
420 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600421 m_confParam.setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500422 }
alvya2228c62014-12-09 10:25:11 -0600423 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400424 std::cerr << "Invalid value for hello-interval. "
425 << "Allowed range: " << HELLO_INTERVAL_MIN << "-" << HELLO_INTERVAL_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600426 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500427 }
Vince Lehman7b616582014-10-17 16:25:39 -0500428
429 // Event intervals
430 // adj-lsa-build-interval
431 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600432 std::bind(&ConfParameter::setAdjLsaBuildInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600433 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500434 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
435 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
436
437 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
438 return false;
439 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500440 // Set the retry count for fetching the FaceStatus dataset
441 ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
442 std::bind(&ConfParameter::setFaceDatasetFetchTries,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400443 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500444
445 faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
446 FACE_DATASET_FETCH_TRIES_MAX);
447 faceDatasetFetchTries.setOptional(FACE_DATASET_FETCH_TRIES_DEFAULT);
448
449 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
450 return false;
451 }
452
453 // Set the interval between FaceStatus dataset fetch attempts.
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500454 ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
Davide Pesaventod90338d2021-01-07 17:50:05 -0500455 std::bind(&ConfParameter::setFaceDatasetFetchInterval,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400456 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500457
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500458 faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
459 FACE_DATASET_FETCH_INTERVAL_MAX);
460 faceDatasetFetchInterval.setOptional(FACE_DATASET_FETCH_INTERVAL_DEFAULT);
Nick Gordond5c1a372016-10-31 13:56:23 -0500461
462 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
463 return false;
464 }
Vince Lehman7b616582014-10-17 16:25:39 -0500465
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600466 for (const auto& tn : section) {
467 if (tn.first == "neighbor") {
akmhoque157b0a42014-05-13 00:26:37 -0500468 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600469 ConfigSection CommandAttriTree = tn.second;
akmhoque157b0a42014-05-13 00:26:37 -0500470 std::string name = CommandAttriTree.get<std::string>("name");
Nick Gordone9733ed2017-04-26 10:48:39 -0500471 std::string uriString = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600472
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500473 ndn::FaceUri faceUri;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400474 if (!faceUri.parse(uriString)) {
475 std::cerr << "face-uri parsing failed" << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600476 return false;
477 }
478
479 bool failedToCanonize = false;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400480 faceUri.canonize([&faceUri] (const auto& canonicalUri) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600481 faceUri = canonicalUri;
482 },
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400483 [&faceUri, &failedToCanonize] (const auto& reason) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600484 failedToCanonize = true;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400485 std::cerr << "Could not canonize URI: '" << faceUri
486 << "' because: " << reason << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600487 },
488 m_io,
489 TIME_ALLOWED_FOR_CANONIZATION);
490 m_io.run();
Davide Pesavento5849ee72023-11-12 20:00:21 -0500491 m_io.restart();
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600492
493 if (failedToCanonize) {
alvy2fe12872014-11-25 10:32:23 -0600494 return false;
495 }
496
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400497 double linkCost = CommandAttriTree.get<double>("link-cost", Adjacent::DEFAULT_LINK_COST);
akmhoque157b0a42014-05-13 00:26:37 -0500498 ndn::Name neighborName(name);
499 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500500 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600501 m_confParam.getAdjacencyList().insert(adj);
akmhoque157b0a42014-05-13 00:26:37 -0500502 }
503 else {
akmhoque674b0b12014-05-20 14:33:28 -0500504 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500505 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500506 }
507 }
akmhoque157b0a42014-05-13 00:26:37 -0500508 catch (const std::exception& ex) {
509 std::cerr << ex.what() << std::endl;
510 return false;
511 }
akmhoque53353462014-04-22 08:43:45 -0500512 }
513 }
akmhoque157b0a42014-05-13 00:26:37 -0500514 return true;
akmhoque53353462014-04-22 08:43:45 -0500515}
516
akmhoque157b0a42014-05-13 00:26:37 -0500517bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700518ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500519{
alvya2228c62014-12-09 10:25:11 -0600520 // state
Nick Gordone98480b2017-05-24 11:23:03 -0500521 std::string state = section.get<std::string>("state", "off");
alvya2228c62014-12-09 10:25:11 -0600522
523 if (boost::iequals(state, "off")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600524 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500525 }
alvya2228c62014-12-09 10:25:11 -0600526 else if (boost::iequals(state, "on")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600527 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
alvya2228c62014-12-09 10:25:11 -0600528 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400529 else if (boost::iequals(state, "dry-run")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600530 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
alvya2228c62014-12-09 10:25:11 -0600531 }
532 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400533 std::cerr << "Invalid setting for hyperbolic state. "
534 << "Allowed values: off, on, dry-run" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500535 return false;
akmhoque53353462014-04-22 08:43:45 -0500536 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700537
akmhoque157b0a42014-05-13 00:26:37 -0500538 try {
Laqin Fan54a43f02017-03-08 12:31:30 -0600539 // Radius and angle(s) are mandatory configuration parameters in hyperbolic section.
540 // Even if router can have hyperbolic routing calculation off but other router
541 // in the network may use hyperbolic routing calculation for FIB generation.
542 // So each router need to advertise its hyperbolic coordinates in the network
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700543 double radius = section.get<double>("radius");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600544 std::string angleString = section.get<std::string>("angle");
545
546 std::stringstream ss(angleString);
547 std::vector<double> angles;
548
549 double angle;
550
Laqin Fan54a43f02017-03-08 12:31:30 -0600551 while (ss >> angle) {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600552 angles.push_back(angle);
Laqin Fan54a43f02017-03-08 12:31:30 -0600553 if (ss.peek() == ',' || ss.peek() == ' ') {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600554 ss.ignore();
555 }
556 }
557
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600558 if (!m_confParam.setCorR(radius)) {
akmhoque157b0a42014-05-13 00:26:37 -0500559 return false;
560 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600561 m_confParam.setCorTheta(angles);
akmhoque53353462014-04-22 08:43:45 -0500562 }
akmhoque157b0a42014-05-13 00:26:37 -0500563 catch (const std::exception& ex) {
564 std::cerr << ex.what() << std::endl;
565 if (state == "on" || state == "dry-run") {
566 return false;
567 }
akmhoque53353462014-04-22 08:43:45 -0500568 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700569
akmhoque157b0a42014-05-13 00:26:37 -0500570 return true;
akmhoque53353462014-04-22 08:43:45 -0500571}
572
akmhoque157b0a42014-05-13 00:26:37 -0500573bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700574ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500575{
alvya2228c62014-12-09 10:25:11 -0600576 // max-faces-per-prefix
577 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
578
579 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400580 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600581 m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500582 }
alvya2228c62014-12-09 10:25:11 -0600583 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400584 std::cerr << "Invalid value for max-faces-per-prefix. "
585 << "Allowed range: " << MAX_FACES_PER_PREFIX_MIN
586 << "-" << MAX_FACES_PER_PREFIX_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500587 return false;
588 }
Vince Lehman7b616582014-10-17 16:25:39 -0500589
590 // routing-calc-interval
591 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600592 std::bind(&ConfParameter::setRoutingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600593 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500594 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
595 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
596
597 if (!routingCalcInterval.parseFromConfigSection(section)) {
598 return false;
599 }
600
akmhoque157b0a42014-05-13 00:26:37 -0500601 return true;
akmhoque53353462014-04-22 08:43:45 -0500602}
603
akmhoque157b0a42014-05-13 00:26:37 -0500604bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700605ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500606{
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600607 for (const auto& tn : section) {
608 if (tn.first == "prefix") {
akmhoque157b0a42014-05-13 00:26:37 -0500609 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600610 ndn::Name namePrefix(tn.second.data());
akmhoque157b0a42014-05-13 00:26:37 -0500611 if (!namePrefix.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600612 m_confParam.getNamePrefixList().insert(namePrefix);
akmhoque157b0a42014-05-13 00:26:37 -0500613 }
614 else {
akmhoque674b0b12014-05-20 14:33:28 -0500615 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500616 return false;
617 }
618 }
619 catch (const std::exception& ex) {
620 std::cerr << ex.what() << std::endl;
621 return false;
622 }
akmhoque53353462014-04-22 08:43:45 -0500623 }
akmhoque53353462014-04-22 08:43:45 -0500624 }
akmhoque157b0a42014-05-13 00:26:37 -0500625 return true;
akmhoque53353462014-04-22 08:43:45 -0500626}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700627
628bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700629ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700630{
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400631 auto it = section.begin();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700632
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500633 if (it == section.end() || it->first != "validator") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400634 std::cerr << "Error: Expected validator section!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500635 return false;
636 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700637
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600638 m_confParam.getValidator().load(it->second, m_confFileName);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500639
akmhoqued57f3672014-06-10 10:41:32 -0500640 it++;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500641 if (it != section.end() && it->first == "prefix-update-validator") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600642 m_confParam.getPrefixUpdateValidator().load(it->second, m_confFileName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700643
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500644 it++;
645 for (; it != section.end(); it++) {
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500646 if (it->first != "cert-to-publish") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400647 std::cerr << "Error: Expected cert-to-publish!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500648 return false;
649 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700650
Davide Pesaventob0716542024-12-16 19:12:11 -0500651 fs::path certPath = fs::canonical(fs::path(m_confFileName).parent_path() / it->second.data());
652 std::ifstream ifs(certPath);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700653
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400654 ndn::security::Certificate idCert;
655 try {
656 idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
657 }
658 catch (const std::exception& e) {
Davide Pesaventob0716542024-12-16 19:12:11 -0500659 std::cerr << "Error: Cannot load cert-to-publish " << certPath << ": " << e.what() << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500660 return false;
661 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400662
Davide Pesaventob0716542024-12-16 19:12:11 -0500663 m_confParam.addCertPath(certPath.string());
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400664 m_confParam.loadCertToValidator(idCert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700665 }
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500666 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700667
668 return true;
669}
670
alvy2fe12872014-11-25 10:32:23 -0600671} // namespace nlsr