blob: b893bf8aa0cde19f196fa439e24cf118e52b7f06 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
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
Nick Gordone98480b2017-05-24 11:23:03 -050027#include <boost/cstdint.hpp>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080028
29#include <ndn-cxx/name.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050030#include <ndn-cxx/net/face-uri.hpp>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080031
Nick Gordone98480b2017-05-24 11:23:03 -050032#include <iostream>
33#include <fstream>
akmhoque53353462014-04-22 08:43:45 -050034
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
36
Vince Lehman7b616582014-10-17 16:25:39 -050037template <class T>
38class ConfigurationVariable
39{
40public:
dmcoomes9f936662017-03-02 10:33:09 -060041 typedef std::function<void(T)> ConfParameterCallback;
Vince Lehman7b616582014-10-17 16:25:39 -050042
43 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
44 : m_key(key)
45 , m_setterCallback(setter)
46 , m_minValue(0)
47 , m_maxValue(0)
48 , m_shouldCheckRange(false)
49 , m_isRequired(true)
50 {
51 }
52
53 bool
54 parseFromConfigSection(const ConfigSection& section)
55 {
56 try {
57 T value = section.get<T>(m_key);
58
59 if (!isValidValue(value)) {
60 return false;
61 }
62
63 m_setterCallback(value);
64 return true;
65 }
66 catch (const std::exception& ex) {
67
68 if (m_isRequired) {
69 std::cerr << ex.what() << std::endl;
70 std::cerr << "Missing required configuration variable" << std::endl;
71 return false;
72 }
73 else {
74 m_setterCallback(m_defaultValue);
75 return true;
76 }
77 }
78
79 return false;
80 }
81
82 void
83 setMinAndMaxValue(T min, T max)
84 {
85 m_minValue = min;
86 m_maxValue = max;
87 m_shouldCheckRange = true;
88 }
89
90 void
91 setOptional(T defaultValue)
92 {
93 m_isRequired = false;
94 m_defaultValue = defaultValue;
95 }
96
97private:
98 void
99 printOutOfRangeError(T value)
100 {
101 std::cerr << "Invalid value for " << m_key << ": "
102 << value << ". "
103 << "Valid values: "
104 << m_minValue << " - "
105 << m_maxValue << std::endl;
106 }
107
108 bool
109 isValidValue(T value)
110 {
111 if (!m_shouldCheckRange) {
112 return true;
113 }
114 else if (value < m_minValue || value > m_maxValue)
115 {
116 printOutOfRangeError(value);
117 return false;
118 }
119
120 return true;
121 }
122
123private:
124 const std::string m_key;
125 const ConfParameterCallback m_setterCallback;
126 T m_defaultValue;
127
128 T m_minValue;
129 T m_maxValue;
130
131 bool m_shouldCheckRange;
132 bool m_isRequired;
133};
134
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600135ConfFileProcessor::ConfFileProcessor(ConfParameter& confParam)
136 : m_confFileName(confParam.getConfFileName())
137 , m_confParam(confParam)
138{
139}
140
akmhoque157b0a42014-05-13 00:26:37 -0500141bool
akmhoqueb6450b12014-04-24 00:01:03 -0500142ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500143{
akmhoque157b0a42014-05-13 00:26:37 -0500144 bool ret = true;
Nick Gordone98480b2017-05-24 11:23:03 -0500145 std::ifstream inputFile;
akmhoque157b0a42014-05-13 00:26:37 -0500146 inputFile.open(m_confFileName.c_str());
147 if (!inputFile.is_open()) {
Nick Gordone98480b2017-05-24 11:23:03 -0500148 std::string msg = "Failed to read configuration file: ";
akmhoque157b0a42014-05-13 00:26:37 -0500149 msg += m_confFileName;
Nick Gordone98480b2017-05-24 11:23:03 -0500150 std::cerr << msg << std::endl;
akmhoquead5fe952014-06-26 13:34:12 -0500151 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500152 }
153 ret = load(inputFile);
154 inputFile.close();
Saurab Dulal427e0122019-11-28 11:58:02 -0600155
156 if (ret) {
157 m_confParam.buildRouterAndSyncUserPrefix();
158 m_confParam.writeLog();
159 }
160
akmhoque157b0a42014-05-13 00:26:37 -0500161 return ret;
162}
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 }
171 catch (const boost::property_tree::info_parser_error& error) {
Nick Gordone98480b2017-05-24 11:23:03 -0500172 std::stringstream msg;
akmhoque157b0a42014-05-13 00:26:37 -0500173 std::cerr << "Failed to parse configuration file " << std::endl;
174 std::cerr << m_confFileName << std::endl;
175 return false;
176 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700177
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600178 for (const auto& tn : pt) {
179 if (!processSection(tn.first, tn.second)) {
180 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500181 }
182 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600183 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500184}
185
186bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700187ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500188{
189 bool ret = true;
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700190 if (sectionName == "general")
akmhoque53353462014-04-22 08:43:45 -0500191 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700192 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500193 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700194 else if (sectionName == "neighbors")
akmhoque157b0a42014-05-13 00:26:37 -0500195 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700196 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500197 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700198 else if (sectionName == "hyperbolic")
akmhoque157b0a42014-05-13 00:26:37 -0500199 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700200 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500201 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700202 else if (sectionName == "fib")
akmhoque157b0a42014-05-13 00:26:37 -0500203 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700204 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500205 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700206 else if (sectionName == "advertising")
akmhoque157b0a42014-05-13 00:26:37 -0500207 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700208 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500209 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700210 else if (sectionName == "security")
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700211 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700212 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700213 }
akmhoque157b0a42014-05-13 00:26:37 -0500214 else
215 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700216 std::cerr << "Wrong configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500217 }
218 return ret;
219}
220
221bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700222ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500223{
224 try {
Nick Gordone98480b2017-05-24 11:23:03 -0500225 std::string network = section.get<std::string>("network");
226 std::string site = section.get<std::string>("site");
227 std::string router = section.get<std::string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500228 ndn::Name networkName(network);
229 if (!networkName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600230 m_confParam.setNetwork(networkName);
akmhoque157b0a42014-05-13 00:26:37 -0500231 }
232 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500233 std::cerr << " Network can not be null or empty or in bad URI format :(!" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500234 return false;
235 }
236 ndn::Name siteName(site);
237 if (!siteName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600238 m_confParam.setSiteName(siteName);
akmhoque157b0a42014-05-13 00:26:37 -0500239 }
240 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500241 std::cerr << "Site can not be null or empty or in bad URI format:( !" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500242 return false;
243 }
244 ndn::Name routerName(router);
245 if (!routerName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600246 m_confParam.setRouterName(routerName);
akmhoque157b0a42014-05-13 00:26:37 -0500247 }
248 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500249 std::cerr << " Router name can not be null or empty or in bad URI format:( !" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500250 return false;
251 }
252 }
253 catch (const std::exception& ex) {
Nick Gordone98480b2017-05-24 11:23:03 -0500254 std::cerr << ex.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500255 return false;
256 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700257
alvya2228c62014-12-09 10:25:11 -0600258 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600259 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600260
261 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600262 m_confParam.setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500263 }
alvya2228c62014-12-09 10:25:11 -0600264 else {
265 std::cerr << "Wrong value for lsa-refresh-time ";
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500266 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";
alvya2228c62014-12-09 10:25:11 -0600267 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
268
269 return false;
270 }
271
272 // router-dead-interval
alvy5a454952014-12-15 12:49:54 -0600273 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", (2*lsaRefreshTime));
alvya2228c62014-12-09 10:25:11 -0600274
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600275 if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
276 m_confParam.setRouterDeadInterval(routerDeadInterval);
alvya2228c62014-12-09 10:25:11 -0600277 }
278 else {
279 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
280 return false;
281 }
282
283 // lsa-interest-lifetime
284 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
285
286 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600287 m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
alvya2228c62014-12-09 10:25:11 -0600288 }
289 else {
290 std::cerr << "Wrong value for lsa-interest-timeout. "
291 << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
292 << LSA_INTEREST_LIFETIME_MAX << std::endl;
293
akmhoque157b0a42014-05-13 00:26:37 -0500294 return false;
295 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700296
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500297 // sync-protocol
298 std::string syncProtocol = section.get<std::string>("sync-protocol", "chronosync");
299 if (syncProtocol == "chronosync") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600300 m_confParam.setSyncProtocol(SYNC_PROTOCOL_CHRONOSYNC);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500301 }
302 else if (syncProtocol == "psync") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600303 m_confParam.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500304 }
305 else {
306 std::cerr << "Sync protocol " << syncProtocol << " is not supported!"
307 << "Use chronosync or psync" << std::endl;
308 return false;
309 }
310
311 // sync-interest-lifetime
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600312 uint32_t syncInterestLifetime = section.get<uint32_t>("sync-interest-lifetime",
313 SYNC_INTEREST_LIFETIME_DEFAULT);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600314 if (syncInterestLifetime >= SYNC_INTEREST_LIFETIME_MIN &&
315 syncInterestLifetime <= SYNC_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600316 m_confParam.setSyncInterestLifetime(syncInterestLifetime);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600317 }
318 else {
319 std::cerr << "Wrong value for sync-interest-lifetime. "
320 << "Allowed value:" << SYNC_INTEREST_LIFETIME_MIN << "-"
321 << SYNC_INTEREST_LIFETIME_MAX << std::endl;
322
323 return false;
324 }
325
akmhoque674b0b12014-05-20 14:33:28 -0500326 try {
dulalsaurab82a34c22019-02-04 17:31:21 +0000327 std::string stateDir = section.get<std::string>("state-dir");
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600328 if (bf::exists(stateDir)) {
329 if (bf::is_directory(stateDir)) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000330
331 // copying nlsr.conf file to a user define directory for possible modification
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600332 std::string conFileDynamic = (bf::path(stateDir) / "nlsr.conf").c_str();
333
334 if (m_confFileName == conFileDynamic) {
335 std::cerr << "Please use nlsr.conf stored at another location "
336 << "or change the state-dir in the configuration." << std::endl;
337 std::cerr << "The file at " << conFileDynamic <<
338 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
339 std::cerr << "The dynamic file can be used for next run "
340 << "after copying to another location." << std::endl;
341 return false;
342 }
343
dulalsaurab82a34c22019-02-04 17:31:21 +0000344 m_confParam.setConfFileNameDynamic(conFileDynamic);
345 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600346 bf::copy_file(m_confFileName, conFileDynamic, bf::copy_option::overwrite_if_exists);
dulalsaurab82a34c22019-02-04 17:31:21 +0000347 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600348 catch (const bf::filesystem_error& e) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000349 std::cerr << "Error copying conf file to the state directory: " << e.what() << std::endl;
350 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600351
352 std::string testFileName = (bf::path(stateDir) / "test.seq").c_str();
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 {
dulalsaurab82a34c22019-02-04 17:31:21 +0000358 std::cerr << "User does not have read and write permission on the state directory";
akmhoque674b0b12014-05-20 14:33:28 -0500359 std::cerr << std::endl;
360 return false;
361 }
362 testOutFile.close();
363 remove(testFileName.c_str());
364 }
365 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000366 std::cerr << "Provided: " << stateDir << "is not a directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500367 return false;
368 }
369 }
370 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000371 std::cerr << "Provided state directory <" << stateDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500372 return false;
373 }
374 }
375 catch (const std::exception& ex) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000376 std::cerr << "You must configure state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500377 std::cerr << ex.what() << std::endl;
378 return false;
379 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700380
akmhoque157b0a42014-05-13 00:26:37 -0500381 return true;
382}
383
384bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700385ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500386{
alvya2228c62014-12-09 10:25:11 -0600387 // hello-retries
388 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
389
390 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600391 m_confParam.setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500392 }
alvya2228c62014-12-09 10:25:11 -0600393 else {
394 std::cerr << "Wrong value for hello-retries." << std::endl;
395 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
396 std::cerr << HELLO_RETRIES_MAX << std::endl;
397
akmhoque157b0a42014-05-13 00:26:37 -0500398 return false;
399 }
alvya2228c62014-12-09 10:25:11 -0600400
401 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600402 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600403
404 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600405 m_confParam.setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500406 }
alvya2228c62014-12-09 10:25:11 -0600407 else {
408 std::cerr << "Wrong value for hello-timeout. ";
409 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
410 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
411
412 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500413 }
alvya2228c62014-12-09 10:25:11 -0600414
415 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600416 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600417
418 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600419 m_confParam.setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500420 }
alvya2228c62014-12-09 10:25:11 -0600421 else {
422 std::cerr << "Wrong value for hello-interval. ";
423 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
424 std::cerr << HELLO_INTERVAL_MAX << std::endl;
425
426 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,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600443 &m_confParam,
Nick Gordond5c1a372016-10-31 13:56:23 -0500444 _1));
445
446 faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
447 FACE_DATASET_FETCH_TRIES_MAX);
448 faceDatasetFetchTries.setOptional(FACE_DATASET_FETCH_TRIES_DEFAULT);
449
450 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
451 return false;
452 }
453
454 // Set the interval between FaceStatus dataset fetch attempts.
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500455 ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
Nick Gordond5c1a372016-10-31 13:56:23 -0500456 bind(&ConfParameter::setFaceDatasetFetchInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600457 &m_confParam,
Nick Gordond5c1a372016-10-31 13:56:23 -0500458 _1));
459
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500460 faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
461 FACE_DATASET_FETCH_INTERVAL_MAX);
462 faceDatasetFetchInterval.setOptional(FACE_DATASET_FETCH_INTERVAL_DEFAULT);
Nick Gordond5c1a372016-10-31 13:56:23 -0500463
464 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
465 return false;
466 }
Vince Lehman7b616582014-10-17 16:25:39 -0500467
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600468 for (const auto& tn : section) {
469 if (tn.first == "neighbor") {
akmhoque157b0a42014-05-13 00:26:37 -0500470 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600471 ConfigSection CommandAttriTree = tn.second;
akmhoque157b0a42014-05-13 00:26:37 -0500472 std::string name = CommandAttriTree.get<std::string>("name");
Nick Gordone9733ed2017-04-26 10:48:39 -0500473 std::string uriString = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600474
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500475 ndn::FaceUri faceUri;
Laqin Fan54a43f02017-03-08 12:31:30 -0600476 if (! faceUri.parse(uriString)) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600477 std::cerr << "Parsing failed!" << std::endl;
478 return false;
479 }
480
481 bool failedToCanonize = false;
482 faceUri.canonize([&faceUri] (ndn::FaceUri canonicalUri) {
483 faceUri = canonicalUri;
484 },
485 [&faceUri, &failedToCanonize] (const std::string& reason) {
486 failedToCanonize = true;
487 std::cerr << "Could not canonize URI: " << faceUri
488 << "because: " << reason << std::endl;
489 },
490 m_io,
491 TIME_ALLOWED_FOR_CANONIZATION);
492 m_io.run();
Ashlesh Gawande9ecbdc92019-03-11 13:18:45 -0700493 m_io.reset();
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600494
495 if (failedToCanonize) {
alvy2fe12872014-11-25 10:32:23 -0600496 return false;
497 }
498
akmhoque157b0a42014-05-13 00:26:37 -0500499 double linkCost = CommandAttriTree.get<double>("link-cost",
500 Adjacent::DEFAULT_LINK_COST);
501 ndn::Name neighborName(name);
502 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500503 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600504 m_confParam.getAdjacencyList().insert(adj);
akmhoque157b0a42014-05-13 00:26:37 -0500505 }
506 else {
akmhoque674b0b12014-05-20 14:33:28 -0500507 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500508 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500509 }
510 }
akmhoque157b0a42014-05-13 00:26:37 -0500511 catch (const std::exception& ex) {
512 std::cerr << ex.what() << std::endl;
513 return false;
514 }
akmhoque53353462014-04-22 08:43:45 -0500515 }
516 }
akmhoque157b0a42014-05-13 00:26:37 -0500517 return true;
akmhoque53353462014-04-22 08:43:45 -0500518}
519
akmhoque157b0a42014-05-13 00:26:37 -0500520bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700521ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500522{
alvya2228c62014-12-09 10:25:11 -0600523 // state
Nick Gordone98480b2017-05-24 11:23:03 -0500524 std::string state = section.get<std::string>("state", "off");
alvya2228c62014-12-09 10:25:11 -0600525
526 if (boost::iequals(state, "off")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600527 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500528 }
alvya2228c62014-12-09 10:25:11 -0600529 else if (boost::iequals(state, "on")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600530 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
alvya2228c62014-12-09 10:25:11 -0600531 }
532 else if (state == "dry-run") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600533 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
alvya2228c62014-12-09 10:25:11 -0600534 }
535 else {
536 std::cerr << "Wrong format for hyperbolic state." << std::endl;
537 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
538
akmhoque157b0a42014-05-13 00:26:37 -0500539 return false;
akmhoque53353462014-04-22 08:43:45 -0500540 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700541
akmhoque157b0a42014-05-13 00:26:37 -0500542 try {
Laqin Fan54a43f02017-03-08 12:31:30 -0600543 // Radius and angle(s) are mandatory configuration parameters in hyperbolic section.
544 // Even if router can have hyperbolic routing calculation off but other router
545 // in the network may use hyperbolic routing calculation for FIB generation.
546 // So each router need to advertise its hyperbolic coordinates in the network
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700547 double radius = section.get<double>("radius");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600548 std::string angleString = section.get<std::string>("angle");
549
550 std::stringstream ss(angleString);
551 std::vector<double> angles;
552
553 double angle;
554
Laqin Fan54a43f02017-03-08 12:31:30 -0600555 while (ss >> angle) {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600556 angles.push_back(angle);
Laqin Fan54a43f02017-03-08 12:31:30 -0600557 if (ss.peek() == ',' || ss.peek() == ' ') {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600558 ss.ignore();
559 }
560 }
561
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600562 if (!m_confParam.setCorR(radius)) {
akmhoque157b0a42014-05-13 00:26:37 -0500563 return false;
564 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600565 m_confParam.setCorTheta(angles);
akmhoque53353462014-04-22 08:43:45 -0500566 }
akmhoque157b0a42014-05-13 00:26:37 -0500567 catch (const std::exception& ex) {
568 std::cerr << ex.what() << std::endl;
569 if (state == "on" || state == "dry-run") {
570 return false;
571 }
akmhoque53353462014-04-22 08:43:45 -0500572 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700573
akmhoque157b0a42014-05-13 00:26:37 -0500574 return true;
akmhoque53353462014-04-22 08:43:45 -0500575}
576
akmhoque157b0a42014-05-13 00:26:37 -0500577bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700578ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500579{
alvya2228c62014-12-09 10:25:11 -0600580 // max-faces-per-prefix
581 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
582
583 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
584 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
585 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600586 m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500587 }
alvya2228c62014-12-09 10:25:11 -0600588 else {
589 std::cerr << "Wrong value for max-faces-per-prefix. ";
590 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
591
akmhoque157b0a42014-05-13 00:26:37 -0500592 return false;
593 }
Vince Lehman7b616582014-10-17 16:25:39 -0500594
595 // routing-calc-interval
596 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600597 std::bind(&ConfParameter::setRoutingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600598 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500599 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
600 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
601
602 if (!routingCalcInterval.parseFromConfigSection(section)) {
603 return false;
604 }
605
akmhoque157b0a42014-05-13 00:26:37 -0500606 return true;
akmhoque53353462014-04-22 08:43:45 -0500607}
608
akmhoque157b0a42014-05-13 00:26:37 -0500609bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700610ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500611{
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600612 for (const auto& tn : section) {
613 if (tn.first == "prefix") {
akmhoque157b0a42014-05-13 00:26:37 -0500614 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600615 ndn::Name namePrefix(tn.second.data());
akmhoque157b0a42014-05-13 00:26:37 -0500616 if (!namePrefix.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600617 m_confParam.getNamePrefixList().insert(namePrefix);
akmhoque157b0a42014-05-13 00:26:37 -0500618 }
619 else {
akmhoque674b0b12014-05-20 14:33:28 -0500620 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500621 return false;
622 }
623 }
624 catch (const std::exception& ex) {
625 std::cerr << ex.what() << std::endl;
626 return false;
627 }
akmhoque53353462014-04-22 08:43:45 -0500628 }
akmhoque53353462014-04-22 08:43:45 -0500629 }
akmhoque157b0a42014-05-13 00:26:37 -0500630 return true;
akmhoque53353462014-04-22 08:43:45 -0500631}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700632
633bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700634ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700635{
636 ConfigSection::const_iterator it = section.begin();
637
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500638 if (it == section.end() || it->first != "validator") {
639 std::cerr << "Error: Expect validator section!" << std::endl;
640 return false;
641 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700642
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600643 m_confParam.getValidator().load(it->second, m_confFileName);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500644
akmhoqued57f3672014-06-10 10:41:32 -0500645 it++;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500646 if (it != section.end() && it->first == "prefix-update-validator") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600647 m_confParam.getPrefixUpdateValidator().load(it->second, m_confFileName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700648
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500649 it++;
650 for (; it != section.end(); it++) {
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500651
652 if (it->first != "cert-to-publish") {
653 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
654 return false;
655 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700656
657 std::string file = it->second.data();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600658 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
659 auto idCert = ndn::io::load<ndn::security::v2::Certificate>(certfilePath.string());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700660
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500661 if (idCert == nullptr) {
662 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
663 return false;
664 }
Saurab Dulal427e0122019-11-28 11:58:02 -0600665 m_confParam.addCertPath(certfilePath.string());
666 m_confParam.loadCertToValidator(*idCert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700667 }
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500668 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700669
670 return true;
671}
672
alvy2fe12872014-11-25 10:32:23 -0600673} // namespace nlsr