blob: 1d3b781b0ff0db4d0d0af395fc18726eafab097e [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 Pesaventod90338d2021-01-07 17:50:05 -05003 * Copyright (c) 2014-2021, 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"
24#include "utility/name-helper.hpp"
25#include "update/prefix-update-processor.hpp"
26
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>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080029
Nick Gordone98480b2017-05-24 11:23:03 -050030#include <iostream>
31#include <fstream>
akmhoque53353462014-04-22 08:43:45 -050032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
Vince Lehman7b616582014-10-17 16:25:39 -050035template <class T>
36class ConfigurationVariable
37{
38public:
dmcoomes9f936662017-03-02 10:33:09 -060039 typedef std::function<void(T)> ConfParameterCallback;
Vince Lehman7b616582014-10-17 16:25:39 -050040
41 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
42 : m_key(key)
43 , m_setterCallback(setter)
44 , m_minValue(0)
45 , m_maxValue(0)
46 , m_shouldCheckRange(false)
47 , m_isRequired(true)
48 {
49 }
50
51 bool
52 parseFromConfigSection(const ConfigSection& section)
53 {
54 try {
55 T value = section.get<T>(m_key);
56
57 if (!isValidValue(value)) {
58 return false;
59 }
60
61 m_setterCallback(value);
62 return true;
63 }
64 catch (const std::exception& ex) {
65
66 if (m_isRequired) {
67 std::cerr << ex.what() << std::endl;
68 std::cerr << "Missing required configuration variable" << std::endl;
69 return false;
70 }
71 else {
72 m_setterCallback(m_defaultValue);
73 return true;
74 }
75 }
76
77 return false;
78 }
79
80 void
81 setMinAndMaxValue(T min, T max)
82 {
83 m_minValue = min;
84 m_maxValue = max;
85 m_shouldCheckRange = true;
86 }
87
88 void
89 setOptional(T defaultValue)
90 {
91 m_isRequired = false;
92 m_defaultValue = defaultValue;
93 }
94
95private:
96 void
97 printOutOfRangeError(T value)
98 {
99 std::cerr << "Invalid value for " << m_key << ": "
100 << value << ". "
101 << "Valid values: "
102 << m_minValue << " - "
103 << m_maxValue << std::endl;
104 }
105
106 bool
107 isValidValue(T value)
108 {
109 if (!m_shouldCheckRange) {
110 return true;
111 }
112 else if (value < m_minValue || value > m_maxValue)
113 {
114 printOutOfRangeError(value);
115 return false;
116 }
117
118 return true;
119 }
120
121private:
122 const std::string m_key;
123 const ConfParameterCallback m_setterCallback;
124 T m_defaultValue;
125
126 T m_minValue;
127 T m_maxValue;
128
129 bool m_shouldCheckRange;
130 bool m_isRequired;
131};
132
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600133ConfFileProcessor::ConfFileProcessor(ConfParameter& confParam)
134 : m_confFileName(confParam.getConfFileName())
135 , m_confParam(confParam)
136{
137}
138
akmhoque157b0a42014-05-13 00:26:37 -0500139bool
akmhoqueb6450b12014-04-24 00:01:03 -0500140ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500141{
akmhoque157b0a42014-05-13 00:26:37 -0500142 bool ret = true;
Nick Gordone98480b2017-05-24 11:23:03 -0500143 std::ifstream inputFile;
akmhoque157b0a42014-05-13 00:26:37 -0500144 inputFile.open(m_confFileName.c_str());
145 if (!inputFile.is_open()) {
Nick Gordone98480b2017-05-24 11:23:03 -0500146 std::string msg = "Failed to read configuration file: ";
akmhoque157b0a42014-05-13 00:26:37 -0500147 msg += m_confFileName;
Nick Gordone98480b2017-05-24 11:23:03 -0500148 std::cerr << msg << std::endl;
akmhoquead5fe952014-06-26 13:34:12 -0500149 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500150 }
151 ret = load(inputFile);
152 inputFile.close();
Saurab Dulal427e0122019-11-28 11:58:02 -0600153
154 if (ret) {
155 m_confParam.buildRouterAndSyncUserPrefix();
156 m_confParam.writeLog();
157 }
158
akmhoque157b0a42014-05-13 00:26:37 -0500159 return ret;
160}
161
162bool
Nick Gordone98480b2017-05-24 11:23:03 -0500163ConfFileProcessor::load(std::istream& input)
akmhoque157b0a42014-05-13 00:26:37 -0500164{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700165 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500166 try {
167 boost::property_tree::read_info(input, pt);
168 }
169 catch (const boost::property_tree::info_parser_error& error) {
Nick Gordone98480b2017-05-24 11:23:03 -0500170 std::stringstream msg;
akmhoque157b0a42014-05-13 00:26:37 -0500171 std::cerr << "Failed to parse configuration file " << std::endl;
172 std::cerr << m_confFileName << std::endl;
173 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;
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700188 if (sectionName == "general")
akmhoque53353462014-04-22 08:43:45 -0500189 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700190 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500191 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700192 else if (sectionName == "neighbors")
akmhoque157b0a42014-05-13 00:26:37 -0500193 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700194 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500195 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700196 else if (sectionName == "hyperbolic")
akmhoque157b0a42014-05-13 00:26:37 -0500197 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700198 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500199 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700200 else if (sectionName == "fib")
akmhoque157b0a42014-05-13 00:26:37 -0500201 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700202 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500203 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700204 else if (sectionName == "advertising")
akmhoque157b0a42014-05-13 00:26:37 -0500205 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700206 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500207 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700208 else if (sectionName == "security")
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700209 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700210 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700211 }
akmhoque157b0a42014-05-13 00:26:37 -0500212 else
213 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700214 std::cerr << "Wrong configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500215 }
216 return ret;
217}
218
219bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700220ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500221{
222 try {
Nick Gordone98480b2017-05-24 11:23:03 -0500223 std::string network = section.get<std::string>("network");
224 std::string site = section.get<std::string>("site");
225 std::string router = section.get<std::string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500226 ndn::Name networkName(network);
227 if (!networkName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600228 m_confParam.setNetwork(networkName);
akmhoque157b0a42014-05-13 00:26:37 -0500229 }
230 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500231 std::cerr << " Network can not be null or empty or in bad URI format :(!" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500232 return false;
233 }
234 ndn::Name siteName(site);
235 if (!siteName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600236 m_confParam.setSiteName(siteName);
akmhoque157b0a42014-05-13 00:26:37 -0500237 }
238 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500239 std::cerr << "Site can not be null or empty or in bad URI format:( !" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500240 return false;
241 }
242 ndn::Name routerName(router);
243 if (!routerName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600244 m_confParam.setRouterName(routerName);
akmhoque157b0a42014-05-13 00:26:37 -0500245 }
246 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500247 std::cerr << " Router name can not be null or empty or in bad URI format:( !" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500248 return false;
249 }
250 }
251 catch (const std::exception& ex) {
Nick Gordone98480b2017-05-24 11:23:03 -0500252 std::cerr << ex.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500253 return false;
254 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700255
alvya2228c62014-12-09 10:25:11 -0600256 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600257 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600258
259 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600260 m_confParam.setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500261 }
alvya2228c62014-12-09 10:25:11 -0600262 else {
263 std::cerr << "Wrong value for lsa-refresh-time ";
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500264 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";
alvya2228c62014-12-09 10:25:11 -0600265 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
266
267 return false;
268 }
269
270 // router-dead-interval
alvy5a454952014-12-15 12:49:54 -0600271 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", (2*lsaRefreshTime));
alvya2228c62014-12-09 10:25:11 -0600272
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600273 if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
274 m_confParam.setRouterDeadInterval(routerDeadInterval);
alvya2228c62014-12-09 10:25:11 -0600275 }
276 else {
277 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
278 return false;
279 }
280
281 // lsa-interest-lifetime
282 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
283
284 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600285 m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
alvya2228c62014-12-09 10:25:11 -0600286 }
287 else {
288 std::cerr << "Wrong value for lsa-interest-timeout. "
289 << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
290 << LSA_INTEREST_LIFETIME_MAX << std::endl;
291
akmhoque157b0a42014-05-13 00:26:37 -0500292 return false;
293 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700294
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500295 // sync-protocol
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700296 std::string syncProtocol = section.get<std::string>("sync-protocol", "psync");
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500297 if (syncProtocol == "chronosync") {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700298#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600299 m_confParam.setSyncProtocol(SYNC_PROTOCOL_CHRONOSYNC);
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700300#else
301 std::cerr << "NLSR was compiled without Chronosync support!" << std::endl;
302 std::cerr << "Only PSync support is currently available ('sync-protocol psync')" << std::endl;
303 return false;
304#endif
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500305 }
306 else if (syncProtocol == "psync") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600307 m_confParam.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500308 }
309 else {
310 std::cerr << "Sync protocol " << syncProtocol << " is not supported!"
311 << "Use chronosync or psync" << std::endl;
312 return false;
313 }
314
315 // sync-interest-lifetime
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600316 uint32_t syncInterestLifetime = section.get<uint32_t>("sync-interest-lifetime",
317 SYNC_INTEREST_LIFETIME_DEFAULT);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600318 if (syncInterestLifetime >= SYNC_INTEREST_LIFETIME_MIN &&
319 syncInterestLifetime <= SYNC_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600320 m_confParam.setSyncInterestLifetime(syncInterestLifetime);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600321 }
322 else {
323 std::cerr << "Wrong value for sync-interest-lifetime. "
324 << "Allowed value:" << SYNC_INTEREST_LIFETIME_MIN << "-"
325 << SYNC_INTEREST_LIFETIME_MAX << std::endl;
326
327 return false;
328 }
329
akmhoque674b0b12014-05-20 14:33:28 -0500330 try {
dulalsaurab82a34c22019-02-04 17:31:21 +0000331 std::string stateDir = section.get<std::string>("state-dir");
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600332 if (bf::exists(stateDir)) {
333 if (bf::is_directory(stateDir)) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000334
335 // copying nlsr.conf file to a user define directory for possible modification
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600336 std::string conFileDynamic = (bf::path(stateDir) / "nlsr.conf").c_str();
337
338 if (m_confFileName == conFileDynamic) {
339 std::cerr << "Please use nlsr.conf stored at another location "
340 << "or change the state-dir in the configuration." << std::endl;
341 std::cerr << "The file at " << conFileDynamic <<
342 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
343 std::cerr << "The dynamic file can be used for next run "
344 << "after copying to another location." << std::endl;
345 return false;
346 }
347
dulalsaurab82a34c22019-02-04 17:31:21 +0000348 m_confParam.setConfFileNameDynamic(conFileDynamic);
349 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600350 bf::copy_file(m_confFileName, conFileDynamic, bf::copy_option::overwrite_if_exists);
dulalsaurab82a34c22019-02-04 17:31:21 +0000351 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600352 catch (const bf::filesystem_error& e) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000353 std::cerr << "Error copying conf file to the state directory: " << e.what() << std::endl;
354 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600355
356 std::string testFileName = (bf::path(stateDir) / "test.seq").c_str();
dulalsaurab82a34c22019-02-04 17:31:21 +0000357 std::ofstream testOutFile(testFileName);
358 if (testOutFile) {
359 m_confParam.setStateFileDir(stateDir);
akmhoque674b0b12014-05-20 14:33:28 -0500360 }
361 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000362 std::cerr << "User does not have read and write permission on the state directory";
akmhoque674b0b12014-05-20 14:33:28 -0500363 std::cerr << std::endl;
364 return false;
365 }
366 testOutFile.close();
367 remove(testFileName.c_str());
368 }
369 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000370 std::cerr << "Provided: " << stateDir << "is not a directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500371 return false;
372 }
373 }
374 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000375 std::cerr << "Provided state directory <" << stateDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500376 return false;
377 }
378 }
379 catch (const std::exception& ex) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000380 std::cerr << "You must configure state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500381 std::cerr << ex.what() << std::endl;
382 return false;
383 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700384
akmhoque157b0a42014-05-13 00:26:37 -0500385 return true;
386}
387
388bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700389ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500390{
alvya2228c62014-12-09 10:25:11 -0600391 // hello-retries
392 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
393
394 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600395 m_confParam.setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500396 }
alvya2228c62014-12-09 10:25:11 -0600397 else {
398 std::cerr << "Wrong value for hello-retries." << std::endl;
399 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
400 std::cerr << HELLO_RETRIES_MAX << std::endl;
401
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 {
412 std::cerr << "Wrong value for hello-timeout. ";
413 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
414 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
415
416 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500417 }
alvya2228c62014-12-09 10:25:11 -0600418
419 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600420 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600421
422 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600423 m_confParam.setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500424 }
alvya2228c62014-12-09 10:25:11 -0600425 else {
426 std::cerr << "Wrong value for hello-interval. ";
427 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
428 std::cerr << HELLO_INTERVAL_MAX << std::endl;
429
430 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500431 }
Vince Lehman7b616582014-10-17 16:25:39 -0500432
433 // Event intervals
434 // adj-lsa-build-interval
435 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600436 std::bind(&ConfParameter::setAdjLsaBuildInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600437 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500438 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
439 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
440
441 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
442 return false;
443 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500444 // Set the retry count for fetching the FaceStatus dataset
445 ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
446 std::bind(&ConfParameter::setFaceDatasetFetchTries,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600447 &m_confParam,
Nick Gordond5c1a372016-10-31 13:56:23 -0500448 _1));
449
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,
461 &m_confParam,
462 _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();
Ashlesh Gawande9ecbdc92019-03-11 13:18:45 -0700497 m_io.reset();
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 }
535 else if (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 {
539 std::cerr << "Wrong format for hyperbolic state." << std::endl;
540 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
541
akmhoque157b0a42014-05-13 00:26:37 -0500542 return false;
akmhoque53353462014-04-22 08:43:45 -0500543 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700544
akmhoque157b0a42014-05-13 00:26:37 -0500545 try {
Laqin Fan54a43f02017-03-08 12:31:30 -0600546 // Radius and angle(s) are mandatory configuration parameters in hyperbolic section.
547 // Even if router can have hyperbolic routing calculation off but other router
548 // in the network may use hyperbolic routing calculation for FIB generation.
549 // So each router need to advertise its hyperbolic coordinates in the network
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700550 double radius = section.get<double>("radius");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600551 std::string angleString = section.get<std::string>("angle");
552
553 std::stringstream ss(angleString);
554 std::vector<double> angles;
555
556 double angle;
557
Laqin Fan54a43f02017-03-08 12:31:30 -0600558 while (ss >> angle) {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600559 angles.push_back(angle);
Laqin Fan54a43f02017-03-08 12:31:30 -0600560 if (ss.peek() == ',' || ss.peek() == ' ') {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600561 ss.ignore();
562 }
563 }
564
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600565 if (!m_confParam.setCorR(radius)) {
akmhoque157b0a42014-05-13 00:26:37 -0500566 return false;
567 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600568 m_confParam.setCorTheta(angles);
akmhoque53353462014-04-22 08:43:45 -0500569 }
akmhoque157b0a42014-05-13 00:26:37 -0500570 catch (const std::exception& ex) {
571 std::cerr << ex.what() << std::endl;
572 if (state == "on" || state == "dry-run") {
573 return false;
574 }
akmhoque53353462014-04-22 08:43:45 -0500575 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700576
akmhoque157b0a42014-05-13 00:26:37 -0500577 return true;
akmhoque53353462014-04-22 08:43:45 -0500578}
579
akmhoque157b0a42014-05-13 00:26:37 -0500580bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700581ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500582{
alvya2228c62014-12-09 10:25:11 -0600583 // max-faces-per-prefix
584 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
585
586 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
587 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
588 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600589 m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500590 }
alvya2228c62014-12-09 10:25:11 -0600591 else {
592 std::cerr << "Wrong value for max-faces-per-prefix. ";
593 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
594
akmhoque157b0a42014-05-13 00:26:37 -0500595 return false;
596 }
Vince Lehman7b616582014-10-17 16:25:39 -0500597
598 // routing-calc-interval
599 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600600 std::bind(&ConfParameter::setRoutingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600601 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500602 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
603 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
604
605 if (!routingCalcInterval.parseFromConfigSection(section)) {
606 return false;
607 }
608
akmhoque157b0a42014-05-13 00:26:37 -0500609 return true;
akmhoque53353462014-04-22 08:43:45 -0500610}
611
akmhoque157b0a42014-05-13 00:26:37 -0500612bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700613ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500614{
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600615 for (const auto& tn : section) {
616 if (tn.first == "prefix") {
akmhoque157b0a42014-05-13 00:26:37 -0500617 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600618 ndn::Name namePrefix(tn.second.data());
akmhoque157b0a42014-05-13 00:26:37 -0500619 if (!namePrefix.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600620 m_confParam.getNamePrefixList().insert(namePrefix);
akmhoque157b0a42014-05-13 00:26:37 -0500621 }
622 else {
akmhoque674b0b12014-05-20 14:33:28 -0500623 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500624 return false;
625 }
626 }
627 catch (const std::exception& ex) {
628 std::cerr << ex.what() << std::endl;
629 return false;
630 }
akmhoque53353462014-04-22 08:43:45 -0500631 }
akmhoque53353462014-04-22 08:43:45 -0500632 }
akmhoque157b0a42014-05-13 00:26:37 -0500633 return true;
akmhoque53353462014-04-22 08:43:45 -0500634}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700635
636bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700637ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700638{
639 ConfigSection::const_iterator it = section.begin();
640
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500641 if (it == section.end() || it->first != "validator") {
642 std::cerr << "Error: Expect validator section!" << std::endl;
643 return false;
644 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700645
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600646 m_confParam.getValidator().load(it->second, m_confFileName);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500647
akmhoqued57f3672014-06-10 10:41:32 -0500648 it++;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500649 if (it != section.end() && it->first == "prefix-update-validator") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600650 m_confParam.getPrefixUpdateValidator().load(it->second, m_confFileName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700651
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500652 it++;
653 for (; it != section.end(); it++) {
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500654
655 if (it->first != "cert-to-publish") {
656 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
657 return false;
658 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700659
660 std::string file = it->second.data();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600661 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400662 auto idCert = ndn::io::load<ndn::security::Certificate>(certfilePath.string());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700663
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500664 if (idCert == nullptr) {
665 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
666 return false;
667 }
Saurab Dulal427e0122019-11-28 11:58:02 -0600668 m_confParam.addCertPath(certfilePath.string());
669 m_confParam.loadCertToValidator(*idCert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700670 }
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500671 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700672
673 return true;
674}
675
alvy2fe12872014-11-25 10:32:23 -0600676} // namespace nlsr