blob: 4140e0dc31b7773b87a91e8f7d400328255e87e1 [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"
Ashlesh Gawande3909aa12017-07-28 16:01:35 -050024#include "update/prefix-update-processor.hpp"
Davide Pesavento7bc3d432021-10-25 21:08:04 -040025#include "utility/name-helper.hpp"
Ashlesh Gawande3909aa12017-07-28 16:01:35 -050026
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080027#include <ndn-cxx/name.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050028#include <ndn-cxx/net/face-uri.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040029#include <ndn-cxx/util/io.hpp>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080030
Davide Pesavento22520e62021-06-08 22:16:52 -040031#include <boost/filesystem.hpp>
32#include <boost/property_tree/info_parser.hpp>
33
Nick Gordone98480b2017-05-24 11:23:03 -050034#include <fstream>
Davide Pesavento22520e62021-06-08 22:16:52 -040035#include <iostream>
36
37namespace bf = boost::filesystem;
akmhoque53353462014-04-22 08:43:45 -050038
akmhoque53353462014-04-22 08:43:45 -050039namespace nlsr {
40
Vince Lehman7b616582014-10-17 16:25:39 -050041template <class T>
42class ConfigurationVariable
43{
44public:
dmcoomes9f936662017-03-02 10:33:09 -060045 typedef std::function<void(T)> ConfParameterCallback;
Vince Lehman7b616582014-10-17 16:25:39 -050046
47 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
48 : m_key(key)
49 , m_setterCallback(setter)
50 , m_minValue(0)
51 , m_maxValue(0)
52 , m_shouldCheckRange(false)
53 , m_isRequired(true)
54 {
55 }
56
57 bool
58 parseFromConfigSection(const ConfigSection& section)
59 {
60 try {
61 T value = section.get<T>(m_key);
62
63 if (!isValidValue(value)) {
64 return false;
65 }
66
67 m_setterCallback(value);
68 return true;
69 }
70 catch (const std::exception& ex) {
71
72 if (m_isRequired) {
73 std::cerr << ex.what() << std::endl;
74 std::cerr << "Missing required configuration variable" << std::endl;
75 return false;
76 }
77 else {
78 m_setterCallback(m_defaultValue);
79 return true;
80 }
81 }
82
83 return false;
84 }
85
86 void
87 setMinAndMaxValue(T min, T max)
88 {
89 m_minValue = min;
90 m_maxValue = max;
91 m_shouldCheckRange = true;
92 }
93
94 void
95 setOptional(T defaultValue)
96 {
97 m_isRequired = false;
98 m_defaultValue = defaultValue;
99 }
100
101private:
102 void
103 printOutOfRangeError(T value)
104 {
105 std::cerr << "Invalid value for " << m_key << ": "
106 << value << ". "
107 << "Valid values: "
108 << m_minValue << " - "
109 << m_maxValue << std::endl;
110 }
111
112 bool
113 isValidValue(T value)
114 {
115 if (!m_shouldCheckRange) {
116 return true;
117 }
118 else if (value < m_minValue || value > m_maxValue)
119 {
120 printOutOfRangeError(value);
121 return false;
122 }
123
124 return true;
125 }
126
127private:
128 const std::string m_key;
129 const ConfParameterCallback m_setterCallback;
130 T m_defaultValue;
131
132 T m_minValue;
133 T m_maxValue;
134
135 bool m_shouldCheckRange;
136 bool m_isRequired;
137};
138
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600139ConfFileProcessor::ConfFileProcessor(ConfParameter& confParam)
140 : m_confFileName(confParam.getConfFileName())
141 , m_confParam(confParam)
142{
143}
144
akmhoque157b0a42014-05-13 00:26:37 -0500145bool
akmhoqueb6450b12014-04-24 00:01:03 -0500146ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500147{
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400148 std::ifstream inputFile(m_confFileName);
akmhoque157b0a42014-05-13 00:26:37 -0500149 if (!inputFile.is_open()) {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400150 std::cerr << "Failed to read configuration file: " << m_confFileName << std::endl;
akmhoquead5fe952014-06-26 13:34:12 -0500151 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500152 }
Saurab Dulal427e0122019-11-28 11:58:02 -0600153
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400154 if (!load(inputFile)) {
155 return false;
Saurab Dulal427e0122019-11-28 11:58:02 -0600156 }
157
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400158 m_confParam.buildRouterAndSyncUserPrefix();
159 m_confParam.writeLog();
160 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500161}
162
163bool
Nick Gordone98480b2017-05-24 11:23:03 -0500164ConfFileProcessor::load(std::istream& input)
akmhoque157b0a42014-05-13 00:26:37 -0500165{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700166 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500167 try {
168 boost::property_tree::read_info(input, pt);
169 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400170 catch (const boost::property_tree::ptree_error& e) {
171 std::cerr << "Failed to parse configuration file '" << m_confFileName
172 << "': " << e.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500173 return false;
174 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700175
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600176 for (const auto& tn : pt) {
177 if (!processSection(tn.first, tn.second)) {
178 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500179 }
180 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600181 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500182}
183
184bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700185ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500186{
187 bool ret = true;
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400188 if (sectionName == "general") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700189 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500190 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400191 else if (sectionName == "neighbors") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700192 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500193 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400194 else if (sectionName == "hyperbolic") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700195 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500196 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400197 else if (sectionName == "fib") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700198 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500199 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400200 else if (sectionName == "advertising") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700201 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500202 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400203 else if (sectionName == "security") {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700204 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700205 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400206 else {
207 std::cerr << "Unknown configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500208 }
209 return ret;
210}
211
212bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700213ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500214{
215 try {
Nick Gordone98480b2017-05-24 11:23:03 -0500216 std::string network = section.get<std::string>("network");
217 std::string site = section.get<std::string>("site");
218 std::string router = section.get<std::string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500219 ndn::Name networkName(network);
220 if (!networkName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600221 m_confParam.setNetwork(networkName);
akmhoque157b0a42014-05-13 00:26:37 -0500222 }
223 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400224 std::cerr << "Network can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500225 return false;
226 }
227 ndn::Name siteName(site);
228 if (!siteName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600229 m_confParam.setSiteName(siteName);
akmhoque157b0a42014-05-13 00:26:37 -0500230 }
231 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400232 std::cerr << "Site can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500233 return false;
234 }
235 ndn::Name routerName(router);
236 if (!routerName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600237 m_confParam.setRouterName(routerName);
akmhoque157b0a42014-05-13 00:26:37 -0500238 }
239 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400240 std::cerr << "Router name can not be null or empty or in bad URI format" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500241 return false;
242 }
243 }
244 catch (const std::exception& ex) {
Nick Gordone98480b2017-05-24 11:23:03 -0500245 std::cerr << ex.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500246 return false;
247 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700248
alvya2228c62014-12-09 10:25:11 -0600249 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600250 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600251
252 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600253 m_confParam.setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500254 }
alvya2228c62014-12-09 10:25:11 -0600255 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400256 std::cerr << "Invalid value for lsa-refresh-time. "
257 << "Allowed range: " << LSA_REFRESH_TIME_MIN
258 << "-" << LSA_REFRESH_TIME_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600259 return false;
260 }
261
262 // router-dead-interval
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400263 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", 2 * lsaRefreshTime);
alvya2228c62014-12-09 10:25:11 -0600264
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600265 if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
266 m_confParam.setRouterDeadInterval(routerDeadInterval);
alvya2228c62014-12-09 10:25:11 -0600267 }
268 else {
269 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
270 return false;
271 }
272
273 // lsa-interest-lifetime
274 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
275
276 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600277 m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
alvya2228c62014-12-09 10:25:11 -0600278 }
279 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400280 std::cerr << "Invalid value for lsa-interest-timeout. "
281 << "Allowed range: " << LSA_INTEREST_LIFETIME_MIN
282 << "-" << LSA_INTEREST_LIFETIME_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500283 return false;
284 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700285
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500286 // sync-protocol
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700287 std::string syncProtocol = section.get<std::string>("sync-protocol", "psync");
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500288 if (syncProtocol == "chronosync") {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700289#ifdef HAVE_CHRONOSYNC
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600290 m_confParam.setSyncProtocol(SYNC_PROTOCOL_CHRONOSYNC);
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700291#else
292 std::cerr << "NLSR was compiled without Chronosync support!" << std::endl;
293 std::cerr << "Only PSync support is currently available ('sync-protocol psync')" << std::endl;
294 return false;
295#endif
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500296 }
297 else if (syncProtocol == "psync") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600298 m_confParam.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500299 }
300 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400301 std::cerr << "Sync protocol '" << syncProtocol << "' is not supported!\n"
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500302 << "Use chronosync or psync" << std::endl;
303 return false;
304 }
305
306 // sync-interest-lifetime
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600307 uint32_t syncInterestLifetime = section.get<uint32_t>("sync-interest-lifetime",
308 SYNC_INTEREST_LIFETIME_DEFAULT);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600309 if (syncInterestLifetime >= SYNC_INTEREST_LIFETIME_MIN &&
310 syncInterestLifetime <= SYNC_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600311 m_confParam.setSyncInterestLifetime(syncInterestLifetime);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600312 }
313 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400314 std::cerr << "Invalid value for sync-interest-lifetime. "
315 << "Allowed range: " << SYNC_INTEREST_LIFETIME_MIN
316 << "-" << SYNC_INTEREST_LIFETIME_MAX << std::endl;
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600317 return false;
318 }
319
akmhoque674b0b12014-05-20 14:33:28 -0500320 try {
dulalsaurab82a34c22019-02-04 17:31:21 +0000321 std::string stateDir = section.get<std::string>("state-dir");
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600322 if (bf::exists(stateDir)) {
323 if (bf::is_directory(stateDir)) {
Davide Pesavento22520e62021-06-08 22:16:52 -0400324 // copying nlsr.conf file to a user-defined directory for possible modification
325 std::string conFileDynamic = (bf::path(stateDir) / "nlsr.conf").string();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600326
327 if (m_confFileName == conFileDynamic) {
328 std::cerr << "Please use nlsr.conf stored at another location "
329 << "or change the state-dir in the configuration." << std::endl;
330 std::cerr << "The file at " << conFileDynamic <<
331 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
332 std::cerr << "The dynamic file can be used for next run "
333 << "after copying to another location." << std::endl;
334 return false;
335 }
336
dulalsaurab82a34c22019-02-04 17:31:21 +0000337 m_confParam.setConfFileNameDynamic(conFileDynamic);
338 try {
Davide Pesavento22520e62021-06-08 22:16:52 -0400339 bf::copy_file(m_confFileName, conFileDynamic,
340#if BOOST_VERSION >= 107400
341 bf::copy_options::overwrite_existing
342#else
343 bf::copy_option::overwrite_if_exists
344#endif
345 );
dulalsaurab82a34c22019-02-04 17:31:21 +0000346 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600347 catch (const bf::filesystem_error& e) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000348 std::cerr << "Error copying conf file to the state directory: " << e.what() << std::endl;
Davide Pesavento22520e62021-06-08 22:16:52 -0400349 return false;
dulalsaurab82a34c22019-02-04 17:31:21 +0000350 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600351
Davide Pesavento22520e62021-06-08 22:16:52 -0400352 std::string testFileName = (bf::path(stateDir) / "test.seq").string();
dulalsaurab82a34c22019-02-04 17:31:21 +0000353 std::ofstream testOutFile(testFileName);
354 if (testOutFile) {
355 m_confParam.setStateFileDir(stateDir);
akmhoque674b0b12014-05-20 14:33:28 -0500356 }
357 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400358 std::cerr << "NLSR does not have read/write permission on the state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500359 return false;
360 }
361 testOutFile.close();
362 remove(testFileName.c_str());
363 }
364 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400365 std::cerr << "Provided path '" << stateDir << "' is not a directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500366 return false;
367 }
368 }
369 else {
Davide Pesavento22520e62021-06-08 22:16:52 -0400370 std::cerr << "Provided state directory '" << stateDir << "' does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500371 return false;
372 }
373 }
374 catch (const std::exception& ex) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000375 std::cerr << "You must configure state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500376 std::cerr << ex.what() << std::endl;
377 return false;
378 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700379
akmhoque157b0a42014-05-13 00:26:37 -0500380 return true;
381}
382
383bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700384ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500385{
alvya2228c62014-12-09 10:25:11 -0600386 // hello-retries
387 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
388
389 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600390 m_confParam.setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500391 }
alvya2228c62014-12-09 10:25:11 -0600392 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400393 std::cerr << "Invalid value for hello-retries. "
394 << "Allowed range: " << HELLO_RETRIES_MIN << "-" << HELLO_RETRIES_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500395 return false;
396 }
alvya2228c62014-12-09 10:25:11 -0600397
398 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600399 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600400
401 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600402 m_confParam.setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500403 }
alvya2228c62014-12-09 10:25:11 -0600404 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400405 std::cerr << "Invalid value for hello-timeout. "
406 << "Allowed range: " << HELLO_TIMEOUT_MIN << "-" << HELLO_TIMEOUT_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600407 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500408 }
alvya2228c62014-12-09 10:25:11 -0600409
410 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600411 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600412
413 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600414 m_confParam.setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500415 }
alvya2228c62014-12-09 10:25:11 -0600416 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400417 std::cerr << "Invalid value for hello-interval. "
418 << "Allowed range: " << HELLO_INTERVAL_MIN << "-" << HELLO_INTERVAL_MAX << std::endl;
alvya2228c62014-12-09 10:25:11 -0600419 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500420 }
Vince Lehman7b616582014-10-17 16:25:39 -0500421
422 // Event intervals
423 // adj-lsa-build-interval
424 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600425 std::bind(&ConfParameter::setAdjLsaBuildInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600426 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500427 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
428 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
429
430 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
431 return false;
432 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500433 // Set the retry count for fetching the FaceStatus dataset
434 ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
435 std::bind(&ConfParameter::setFaceDatasetFetchTries,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400436 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500437
438 faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
439 FACE_DATASET_FETCH_TRIES_MAX);
440 faceDatasetFetchTries.setOptional(FACE_DATASET_FETCH_TRIES_DEFAULT);
441
442 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
443 return false;
444 }
445
446 // Set the interval between FaceStatus dataset fetch attempts.
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500447 ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
Davide Pesaventod90338d2021-01-07 17:50:05 -0500448 std::bind(&ConfParameter::setFaceDatasetFetchInterval,
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400449 &m_confParam, _1));
Nick Gordond5c1a372016-10-31 13:56:23 -0500450
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500451 faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
452 FACE_DATASET_FETCH_INTERVAL_MAX);
453 faceDatasetFetchInterval.setOptional(FACE_DATASET_FETCH_INTERVAL_DEFAULT);
Nick Gordond5c1a372016-10-31 13:56:23 -0500454
455 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
456 return false;
457 }
Vince Lehman7b616582014-10-17 16:25:39 -0500458
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600459 for (const auto& tn : section) {
460 if (tn.first == "neighbor") {
akmhoque157b0a42014-05-13 00:26:37 -0500461 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600462 ConfigSection CommandAttriTree = tn.second;
akmhoque157b0a42014-05-13 00:26:37 -0500463 std::string name = CommandAttriTree.get<std::string>("name");
Nick Gordone9733ed2017-04-26 10:48:39 -0500464 std::string uriString = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600465
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500466 ndn::FaceUri faceUri;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400467 if (!faceUri.parse(uriString)) {
468 std::cerr << "face-uri parsing failed" << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600469 return false;
470 }
471
472 bool failedToCanonize = false;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400473 faceUri.canonize([&faceUri] (const auto& canonicalUri) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600474 faceUri = canonicalUri;
475 },
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400476 [&faceUri, &failedToCanonize] (const auto& reason) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600477 failedToCanonize = true;
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400478 std::cerr << "Could not canonize URI: '" << faceUri
479 << "' because: " << reason << std::endl;
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600480 },
481 m_io,
482 TIME_ALLOWED_FOR_CANONIZATION);
483 m_io.run();
Ashlesh Gawande9ecbdc92019-03-11 13:18:45 -0700484 m_io.reset();
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600485
486 if (failedToCanonize) {
alvy2fe12872014-11-25 10:32:23 -0600487 return false;
488 }
489
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400490 double linkCost = CommandAttriTree.get<double>("link-cost", Adjacent::DEFAULT_LINK_COST);
akmhoque157b0a42014-05-13 00:26:37 -0500491 ndn::Name neighborName(name);
492 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500493 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600494 m_confParam.getAdjacencyList().insert(adj);
akmhoque157b0a42014-05-13 00:26:37 -0500495 }
496 else {
akmhoque674b0b12014-05-20 14:33:28 -0500497 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500498 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500499 }
500 }
akmhoque157b0a42014-05-13 00:26:37 -0500501 catch (const std::exception& ex) {
502 std::cerr << ex.what() << std::endl;
503 return false;
504 }
akmhoque53353462014-04-22 08:43:45 -0500505 }
506 }
akmhoque157b0a42014-05-13 00:26:37 -0500507 return true;
akmhoque53353462014-04-22 08:43:45 -0500508}
509
akmhoque157b0a42014-05-13 00:26:37 -0500510bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700511ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500512{
alvya2228c62014-12-09 10:25:11 -0600513 // state
Nick Gordone98480b2017-05-24 11:23:03 -0500514 std::string state = section.get<std::string>("state", "off");
alvya2228c62014-12-09 10:25:11 -0600515
516 if (boost::iequals(state, "off")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600517 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500518 }
alvya2228c62014-12-09 10:25:11 -0600519 else if (boost::iequals(state, "on")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600520 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
alvya2228c62014-12-09 10:25:11 -0600521 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400522 else if (boost::iequals(state, "dry-run")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600523 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
alvya2228c62014-12-09 10:25:11 -0600524 }
525 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400526 std::cerr << "Invalid setting for hyperbolic state. "
527 << "Allowed values: off, on, dry-run" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500528 return false;
akmhoque53353462014-04-22 08:43:45 -0500529 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700530
akmhoque157b0a42014-05-13 00:26:37 -0500531 try {
Laqin Fan54a43f02017-03-08 12:31:30 -0600532 // Radius and angle(s) are mandatory configuration parameters in hyperbolic section.
533 // Even if router can have hyperbolic routing calculation off but other router
534 // in the network may use hyperbolic routing calculation for FIB generation.
535 // So each router need to advertise its hyperbolic coordinates in the network
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700536 double radius = section.get<double>("radius");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600537 std::string angleString = section.get<std::string>("angle");
538
539 std::stringstream ss(angleString);
540 std::vector<double> angles;
541
542 double angle;
543
Laqin Fan54a43f02017-03-08 12:31:30 -0600544 while (ss >> angle) {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600545 angles.push_back(angle);
Laqin Fan54a43f02017-03-08 12:31:30 -0600546 if (ss.peek() == ',' || ss.peek() == ' ') {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600547 ss.ignore();
548 }
549 }
550
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600551 if (!m_confParam.setCorR(radius)) {
akmhoque157b0a42014-05-13 00:26:37 -0500552 return false;
553 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600554 m_confParam.setCorTheta(angles);
akmhoque53353462014-04-22 08:43:45 -0500555 }
akmhoque157b0a42014-05-13 00:26:37 -0500556 catch (const std::exception& ex) {
557 std::cerr << ex.what() << std::endl;
558 if (state == "on" || state == "dry-run") {
559 return false;
560 }
akmhoque53353462014-04-22 08:43:45 -0500561 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700562
akmhoque157b0a42014-05-13 00:26:37 -0500563 return true;
akmhoque53353462014-04-22 08:43:45 -0500564}
565
akmhoque157b0a42014-05-13 00:26:37 -0500566bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700567ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500568{
alvya2228c62014-12-09 10:25:11 -0600569 // max-faces-per-prefix
570 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
571
572 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400573 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600574 m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500575 }
alvya2228c62014-12-09 10:25:11 -0600576 else {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400577 std::cerr << "Invalid value for max-faces-per-prefix. "
578 << "Allowed range: " << MAX_FACES_PER_PREFIX_MIN
579 << "-" << MAX_FACES_PER_PREFIX_MAX << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500580 return false;
581 }
Vince Lehman7b616582014-10-17 16:25:39 -0500582
583 // routing-calc-interval
584 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600585 std::bind(&ConfParameter::setRoutingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600586 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500587 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
588 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
589
590 if (!routingCalcInterval.parseFromConfigSection(section)) {
591 return false;
592 }
593
akmhoque157b0a42014-05-13 00:26:37 -0500594 return true;
akmhoque53353462014-04-22 08:43:45 -0500595}
596
akmhoque157b0a42014-05-13 00:26:37 -0500597bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700598ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500599{
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600600 for (const auto& tn : section) {
601 if (tn.first == "prefix") {
akmhoque157b0a42014-05-13 00:26:37 -0500602 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600603 ndn::Name namePrefix(tn.second.data());
akmhoque157b0a42014-05-13 00:26:37 -0500604 if (!namePrefix.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600605 m_confParam.getNamePrefixList().insert(namePrefix);
akmhoque157b0a42014-05-13 00:26:37 -0500606 }
607 else {
akmhoque674b0b12014-05-20 14:33:28 -0500608 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500609 return false;
610 }
611 }
612 catch (const std::exception& ex) {
613 std::cerr << ex.what() << std::endl;
614 return false;
615 }
akmhoque53353462014-04-22 08:43:45 -0500616 }
akmhoque53353462014-04-22 08:43:45 -0500617 }
akmhoque157b0a42014-05-13 00:26:37 -0500618 return true;
akmhoque53353462014-04-22 08:43:45 -0500619}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700620
621bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700622ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700623{
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400624 auto it = section.begin();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700625
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500626 if (it == section.end() || it->first != "validator") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400627 std::cerr << "Error: Expected validator section!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500628 return false;
629 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700630
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600631 m_confParam.getValidator().load(it->second, m_confFileName);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500632
akmhoqued57f3672014-06-10 10:41:32 -0500633 it++;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500634 if (it != section.end() && it->first == "prefix-update-validator") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600635 m_confParam.getPrefixUpdateValidator().load(it->second, m_confFileName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700636
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500637 it++;
638 for (; it != section.end(); it++) {
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500639 if (it->first != "cert-to-publish") {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400640 std::cerr << "Error: Expected cert-to-publish!" << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500641 return false;
642 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700643
644 std::string file = it->second.data();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600645 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400646 std::ifstream ifs(certfilePath.string());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700647
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400648 ndn::security::Certificate idCert;
649 try {
650 idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
651 }
652 catch (const std::exception& e) {
653 std::cerr << "Error: Cannot load cert-to-publish '" << file << "': " << e.what() << std::endl;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500654 return false;
655 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400656
Saurab Dulal427e0122019-11-28 11:58:02 -0600657 m_confParam.addCertPath(certfilePath.string());
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400658 m_confParam.loadCertToValidator(idCert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700659 }
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500660 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700661
662 return true;
663}
664
alvy2fe12872014-11-25 10:32:23 -0600665} // namespace nlsr