blob: dc422f5505a1e4c1bcce4c9d9469226f7bd1dcfb [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -06003 * Copyright (c) 2014-2019, 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();
155 return ret;
156}
157
158bool
Nick Gordone98480b2017-05-24 11:23:03 -0500159ConfFileProcessor::load(std::istream& input)
akmhoque157b0a42014-05-13 00:26:37 -0500160{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700161 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500162 try {
163 boost::property_tree::read_info(input, pt);
164 }
165 catch (const boost::property_tree::info_parser_error& error) {
Nick Gordone98480b2017-05-24 11:23:03 -0500166 std::stringstream msg;
akmhoque157b0a42014-05-13 00:26:37 -0500167 std::cerr << "Failed to parse configuration file " << std::endl;
168 std::cerr << m_confFileName << std::endl;
169 return false;
170 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700171
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600172 for (const auto& tn : pt) {
173 if (!processSection(tn.first, tn.second)) {
174 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500175 }
176 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600177 return true;
akmhoque157b0a42014-05-13 00:26:37 -0500178}
179
180bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700181ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500182{
183 bool ret = true;
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700184 if (sectionName == "general")
akmhoque53353462014-04-22 08:43:45 -0500185 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700186 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500187 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700188 else if (sectionName == "neighbors")
akmhoque157b0a42014-05-13 00:26:37 -0500189 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700190 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500191 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700192 else if (sectionName == "hyperbolic")
akmhoque157b0a42014-05-13 00:26:37 -0500193 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700194 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500195 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700196 else if (sectionName == "fib")
akmhoque157b0a42014-05-13 00:26:37 -0500197 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700198 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500199 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700200 else if (sectionName == "advertising")
akmhoque157b0a42014-05-13 00:26:37 -0500201 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700202 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500203 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700204 else if (sectionName == "security")
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700205 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700206 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700207 }
akmhoque157b0a42014-05-13 00:26:37 -0500208 else
209 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700210 std::cerr << "Wrong configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500211 }
212 return ret;
213}
214
215bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700216ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500217{
218 try {
Nick Gordone98480b2017-05-24 11:23:03 -0500219 std::string network = section.get<std::string>("network");
220 std::string site = section.get<std::string>("site");
221 std::string router = section.get<std::string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500222 ndn::Name networkName(network);
223 if (!networkName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600224 m_confParam.setNetwork(networkName);
akmhoque157b0a42014-05-13 00:26:37 -0500225 }
226 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500227 std::cerr << " Network can not be null or empty or in bad URI format :(!" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500228 return false;
229 }
230 ndn::Name siteName(site);
231 if (!siteName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600232 m_confParam.setSiteName(siteName);
akmhoque157b0a42014-05-13 00:26:37 -0500233 }
234 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500235 std::cerr << "Site can not be null or empty or in bad URI format:( !" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500236 return false;
237 }
238 ndn::Name routerName(router);
239 if (!routerName.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600240 m_confParam.setRouterName(routerName);
akmhoque157b0a42014-05-13 00:26:37 -0500241 }
242 else {
Nick Gordone98480b2017-05-24 11:23:03 -0500243 std::cerr << " Router name can not be null or empty or in bad URI format:( !" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500244 return false;
245 }
246 }
247 catch (const std::exception& ex) {
Nick Gordone98480b2017-05-24 11:23:03 -0500248 std::cerr << ex.what() << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500249 return false;
250 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700251
alvya2228c62014-12-09 10:25:11 -0600252 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600253 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600254
255 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600256 m_confParam.setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500257 }
alvya2228c62014-12-09 10:25:11 -0600258 else {
259 std::cerr << "Wrong value for lsa-refresh-time ";
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500260 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";
alvya2228c62014-12-09 10:25:11 -0600261 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
262
263 return false;
264 }
265
266 // router-dead-interval
alvy5a454952014-12-15 12:49:54 -0600267 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", (2*lsaRefreshTime));
alvya2228c62014-12-09 10:25:11 -0600268
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600269 if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
270 m_confParam.setRouterDeadInterval(routerDeadInterval);
alvya2228c62014-12-09 10:25:11 -0600271 }
272 else {
273 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
274 return false;
275 }
276
277 // lsa-interest-lifetime
278 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
279
280 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600281 m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
alvya2228c62014-12-09 10:25:11 -0600282 }
283 else {
284 std::cerr << "Wrong value for lsa-interest-timeout. "
285 << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
286 << LSA_INTEREST_LIFETIME_MAX << std::endl;
287
akmhoque157b0a42014-05-13 00:26:37 -0500288 return false;
289 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700290
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500291 // sync-protocol
292 std::string syncProtocol = section.get<std::string>("sync-protocol", "chronosync");
293 if (syncProtocol == "chronosync") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600294 m_confParam.setSyncProtocol(SYNC_PROTOCOL_CHRONOSYNC);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500295 }
296 else if (syncProtocol == "psync") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600297 m_confParam.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500298 }
299 else {
300 std::cerr << "Sync protocol " << syncProtocol << " is not supported!"
301 << "Use chronosync or psync" << std::endl;
302 return false;
303 }
304
305 // sync-interest-lifetime
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600306 uint32_t syncInterestLifetime = section.get<uint32_t>("sync-interest-lifetime",
307 SYNC_INTEREST_LIFETIME_DEFAULT);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600308 if (syncInterestLifetime >= SYNC_INTEREST_LIFETIME_MIN &&
309 syncInterestLifetime <= SYNC_INTEREST_LIFETIME_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600310 m_confParam.setSyncInterestLifetime(syncInterestLifetime);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600311 }
312 else {
313 std::cerr << "Wrong value for sync-interest-lifetime. "
314 << "Allowed value:" << SYNC_INTEREST_LIFETIME_MIN << "-"
315 << SYNC_INTEREST_LIFETIME_MAX << std::endl;
316
317 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)) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000324
325 // copying nlsr.conf file to a user define directory for possible modification
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600326 std::string conFileDynamic = (bf::path(stateDir) / "nlsr.conf").c_str();
327
328 if (m_confFileName == conFileDynamic) {
329 std::cerr << "Please use nlsr.conf stored at another location "
330 << "or change the state-dir in the configuration." << std::endl;
331 std::cerr << "The file at " << conFileDynamic <<
332 " is used as dynamic file for saving NLSR runtime changes." << std::endl;
333 std::cerr << "The dynamic file can be used for next run "
334 << "after copying to another location." << std::endl;
335 return false;
336 }
337
dulalsaurab82a34c22019-02-04 17:31:21 +0000338 m_confParam.setConfFileNameDynamic(conFileDynamic);
339 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600340 bf::copy_file(m_confFileName, conFileDynamic, bf::copy_option::overwrite_if_exists);
dulalsaurab82a34c22019-02-04 17:31:21 +0000341 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600342 catch (const bf::filesystem_error& e) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000343 std::cerr << "Error copying conf file to the state directory: " << e.what() << std::endl;
344 }
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600345
346 std::string testFileName = (bf::path(stateDir) / "test.seq").c_str();
dulalsaurab82a34c22019-02-04 17:31:21 +0000347 std::ofstream testOutFile(testFileName);
348 if (testOutFile) {
349 m_confParam.setStateFileDir(stateDir);
akmhoque674b0b12014-05-20 14:33:28 -0500350 }
351 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000352 std::cerr << "User does not have read and write permission on the state directory";
akmhoque674b0b12014-05-20 14:33:28 -0500353 std::cerr << std::endl;
354 return false;
355 }
356 testOutFile.close();
357 remove(testFileName.c_str());
358 }
359 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000360 std::cerr << "Provided: " << stateDir << "is not a directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500361 return false;
362 }
363 }
364 else {
dulalsaurab82a34c22019-02-04 17:31:21 +0000365 std::cerr << "Provided state directory <" << stateDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500366 return false;
367 }
368 }
369 catch (const std::exception& ex) {
dulalsaurab82a34c22019-02-04 17:31:21 +0000370 std::cerr << "You must configure state directory" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500371 std::cerr << ex.what() << std::endl;
372 return false;
373 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700374
akmhoque157b0a42014-05-13 00:26:37 -0500375 return true;
376}
377
378bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700379ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500380{
alvya2228c62014-12-09 10:25:11 -0600381 // hello-retries
382 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
383
384 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600385 m_confParam.setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500386 }
alvya2228c62014-12-09 10:25:11 -0600387 else {
388 std::cerr << "Wrong value for hello-retries." << std::endl;
389 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
390 std::cerr << HELLO_RETRIES_MAX << std::endl;
391
akmhoque157b0a42014-05-13 00:26:37 -0500392 return false;
393 }
alvya2228c62014-12-09 10:25:11 -0600394
395 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600396 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600397
398 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600399 m_confParam.setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500400 }
alvya2228c62014-12-09 10:25:11 -0600401 else {
402 std::cerr << "Wrong value for hello-timeout. ";
403 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
404 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
405
406 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500407 }
alvya2228c62014-12-09 10:25:11 -0600408
409 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600410 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600411
412 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600413 m_confParam.setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500414 }
alvya2228c62014-12-09 10:25:11 -0600415 else {
416 std::cerr << "Wrong value for hello-interval. ";
417 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
418 std::cerr << HELLO_INTERVAL_MAX << std::endl;
419
420 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500421 }
Vince Lehman7b616582014-10-17 16:25:39 -0500422
423 // Event intervals
424 // adj-lsa-build-interval
425 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600426 std::bind(&ConfParameter::setAdjLsaBuildInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600427 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500428 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
429 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
430
431 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
432 return false;
433 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500434 // Set the retry count for fetching the FaceStatus dataset
435 ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
436 std::bind(&ConfParameter::setFaceDatasetFetchTries,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600437 &m_confParam,
Nick Gordond5c1a372016-10-31 13:56:23 -0500438 _1));
439
440 faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
441 FACE_DATASET_FETCH_TRIES_MAX);
442 faceDatasetFetchTries.setOptional(FACE_DATASET_FETCH_TRIES_DEFAULT);
443
444 if (!faceDatasetFetchTries.parseFromConfigSection(section)) {
445 return false;
446 }
447
448 // Set the interval between FaceStatus dataset fetch attempts.
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500449 ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
Nick Gordond5c1a372016-10-31 13:56:23 -0500450 bind(&ConfParameter::setFaceDatasetFetchInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600451 &m_confParam,
Nick Gordond5c1a372016-10-31 13:56:23 -0500452 _1));
453
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500454 faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
455 FACE_DATASET_FETCH_INTERVAL_MAX);
456 faceDatasetFetchInterval.setOptional(FACE_DATASET_FETCH_INTERVAL_DEFAULT);
Nick Gordond5c1a372016-10-31 13:56:23 -0500457
458 if (!faceDatasetFetchInterval.parseFromConfigSection(section)) {
459 return false;
460 }
Vince Lehman7b616582014-10-17 16:25:39 -0500461
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600462 for (const auto& tn : section) {
463 if (tn.first == "neighbor") {
akmhoque157b0a42014-05-13 00:26:37 -0500464 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600465 ConfigSection CommandAttriTree = tn.second;
akmhoque157b0a42014-05-13 00:26:37 -0500466 std::string name = CommandAttriTree.get<std::string>("name");
Nick Gordone9733ed2017-04-26 10:48:39 -0500467 std::string uriString = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600468
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500469 ndn::FaceUri faceUri;
Laqin Fan54a43f02017-03-08 12:31:30 -0600470 if (! faceUri.parse(uriString)) {
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600471 std::cerr << "Parsing failed!" << std::endl;
472 return false;
473 }
474
475 bool failedToCanonize = false;
476 faceUri.canonize([&faceUri] (ndn::FaceUri canonicalUri) {
477 faceUri = canonicalUri;
478 },
479 [&faceUri, &failedToCanonize] (const std::string& reason) {
480 failedToCanonize = true;
481 std::cerr << "Could not canonize URI: " << faceUri
482 << "because: " << reason << std::endl;
483 },
484 m_io,
485 TIME_ALLOWED_FOR_CANONIZATION);
486 m_io.run();
Ashlesh Gawande9ecbdc92019-03-11 13:18:45 -0700487 m_io.reset();
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600488
489 if (failedToCanonize) {
alvy2fe12872014-11-25 10:32:23 -0600490 return false;
491 }
492
akmhoque157b0a42014-05-13 00:26:37 -0500493 double linkCost = CommandAttriTree.get<double>("link-cost",
494 Adjacent::DEFAULT_LINK_COST);
495 ndn::Name neighborName(name);
496 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500497 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600498 m_confParam.getAdjacencyList().insert(adj);
akmhoque157b0a42014-05-13 00:26:37 -0500499 }
500 else {
akmhoque674b0b12014-05-20 14:33:28 -0500501 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500502 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500503 }
504 }
akmhoque157b0a42014-05-13 00:26:37 -0500505 catch (const std::exception& ex) {
506 std::cerr << ex.what() << std::endl;
507 return false;
508 }
akmhoque53353462014-04-22 08:43:45 -0500509 }
510 }
akmhoque157b0a42014-05-13 00:26:37 -0500511 return true;
akmhoque53353462014-04-22 08:43:45 -0500512}
513
akmhoque157b0a42014-05-13 00:26:37 -0500514bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700515ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500516{
alvya2228c62014-12-09 10:25:11 -0600517 // state
Nick Gordone98480b2017-05-24 11:23:03 -0500518 std::string state = section.get<std::string>("state", "off");
alvya2228c62014-12-09 10:25:11 -0600519
520 if (boost::iequals(state, "off")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600521 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500522 }
alvya2228c62014-12-09 10:25:11 -0600523 else if (boost::iequals(state, "on")) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600524 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
alvya2228c62014-12-09 10:25:11 -0600525 }
526 else if (state == "dry-run") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600527 m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
alvya2228c62014-12-09 10:25:11 -0600528 }
529 else {
530 std::cerr << "Wrong format for hyperbolic state." << std::endl;
531 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
532
akmhoque157b0a42014-05-13 00:26:37 -0500533 return false;
akmhoque53353462014-04-22 08:43:45 -0500534 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700535
akmhoque157b0a42014-05-13 00:26:37 -0500536 try {
Laqin Fan54a43f02017-03-08 12:31:30 -0600537 // Radius and angle(s) are mandatory configuration parameters in hyperbolic section.
538 // Even if router can have hyperbolic routing calculation off but other router
539 // in the network may use hyperbolic routing calculation for FIB generation.
540 // So each router need to advertise its hyperbolic coordinates in the network
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700541 double radius = section.get<double>("radius");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600542 std::string angleString = section.get<std::string>("angle");
543
544 std::stringstream ss(angleString);
545 std::vector<double> angles;
546
547 double angle;
548
Laqin Fan54a43f02017-03-08 12:31:30 -0600549 while (ss >> angle) {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600550 angles.push_back(angle);
Laqin Fan54a43f02017-03-08 12:31:30 -0600551 if (ss.peek() == ',' || ss.peek() == ' ') {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600552 ss.ignore();
553 }
554 }
555
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600556 if (!m_confParam.setCorR(radius)) {
akmhoque157b0a42014-05-13 00:26:37 -0500557 return false;
558 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600559 m_confParam.setCorTheta(angles);
akmhoque53353462014-04-22 08:43:45 -0500560 }
akmhoque157b0a42014-05-13 00:26:37 -0500561 catch (const std::exception& ex) {
562 std::cerr << ex.what() << std::endl;
563 if (state == "on" || state == "dry-run") {
564 return false;
565 }
akmhoque53353462014-04-22 08:43:45 -0500566 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700567
akmhoque157b0a42014-05-13 00:26:37 -0500568 return true;
akmhoque53353462014-04-22 08:43:45 -0500569}
570
akmhoque157b0a42014-05-13 00:26:37 -0500571bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700572ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500573{
alvya2228c62014-12-09 10:25:11 -0600574 // max-faces-per-prefix
575 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
576
577 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
578 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
579 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600580 m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500581 }
alvya2228c62014-12-09 10:25:11 -0600582 else {
583 std::cerr << "Wrong value for max-faces-per-prefix. ";
584 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
585
akmhoque157b0a42014-05-13 00:26:37 -0500586 return false;
587 }
Vince Lehman7b616582014-10-17 16:25:39 -0500588
589 // routing-calc-interval
590 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
dmcoomes9f936662017-03-02 10:33:09 -0600591 std::bind(&ConfParameter::setRoutingCalcInterval,
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600592 &m_confParam, _1));
Vince Lehman7b616582014-10-17 16:25:39 -0500593 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
594 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
595
596 if (!routingCalcInterval.parseFromConfigSection(section)) {
597 return false;
598 }
599
akmhoque157b0a42014-05-13 00:26:37 -0500600 return true;
akmhoque53353462014-04-22 08:43:45 -0500601}
602
akmhoque157b0a42014-05-13 00:26:37 -0500603bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700604ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500605{
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600606 for (const auto& tn : section) {
607 if (tn.first == "prefix") {
akmhoque157b0a42014-05-13 00:26:37 -0500608 try {
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600609 ndn::Name namePrefix(tn.second.data());
akmhoque157b0a42014-05-13 00:26:37 -0500610 if (!namePrefix.empty()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600611 m_confParam.getNamePrefixList().insert(namePrefix);
akmhoque157b0a42014-05-13 00:26:37 -0500612 }
613 else {
akmhoque674b0b12014-05-20 14:33:28 -0500614 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500615 return false;
616 }
617 }
618 catch (const std::exception& ex) {
619 std::cerr << ex.what() << std::endl;
620 return false;
621 }
akmhoque53353462014-04-22 08:43:45 -0500622 }
akmhoque53353462014-04-22 08:43:45 -0500623 }
akmhoque157b0a42014-05-13 00:26:37 -0500624 return true;
akmhoque53353462014-04-22 08:43:45 -0500625}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700626
627bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700628ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700629{
630 ConfigSection::const_iterator it = section.begin();
631
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500632 if (it == section.end() || it->first != "validator") {
633 std::cerr << "Error: Expect validator section!" << std::endl;
634 return false;
635 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700636
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600637 m_confParam.getValidator().load(it->second, m_confFileName);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500638
akmhoqued57f3672014-06-10 10:41:32 -0500639 it++;
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500640 if (it != section.end() && it->first == "prefix-update-validator") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600641 m_confParam.getPrefixUpdateValidator().load(it->second, m_confFileName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700642
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500643 it++;
644 for (; it != section.end(); it++) {
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500645
646 if (it->first != "cert-to-publish") {
647 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
648 return false;
649 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700650
651 std::string file = it->second.data();
Ashlesh Gawande328fc112019-12-12 17:06:44 -0600652 bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
653 auto idCert = ndn::io::load<ndn::security::v2::Certificate>(certfilePath.string());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700654
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500655 if (idCert == nullptr) {
656 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
657 return false;
658 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700659
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600660 m_confParam.getCertStore().insert(*idCert);
661 m_confParam.getValidator().loadAnchor("Authoritative-Certificate",
662 ndn::security::v2::Certificate(*idCert));
663 m_confParam.getPrefixUpdateValidator().loadAnchor("Authoritative-Certificate",
664 ndn::security::v2::Certificate(*idCert));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700665 }
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500666 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700667
668 return true;
669}
670
alvy2fe12872014-11-25 10:32:23 -0600671} // namespace nlsr