blob: 2bb7aefd924531a3fcef76e62104dd67fc6daf79 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehmanc2e51f62015-01-20 15:03:11 -06003 * Copyright (c) 2014-2015, The University of Memphis,
4 * 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
akmhoque53353462014-04-22 08:43:45 -050022#include <iostream>
23#include <fstream>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080024
25#include <ndn-cxx/name.hpp>
alvy2fe12872014-11-25 10:32:23 -060026#include <ndn-cxx/util/face-uri.hpp>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080027
28// boost needs to be included after ndn-cxx, otherwise there will be conflict with _1, _2, ...
akmhoque157b0a42014-05-13 00:26:37 -050029#include <boost/algorithm/string.hpp>
30#include <boost/property_tree/info_parser.hpp>
31#include <boost/property_tree/ptree.hpp>
akmhoque674b0b12014-05-20 14:33:28 -050032#include <boost/filesystem.hpp>
akmhoque53353462014-04-22 08:43:45 -050033
akmhoque53353462014-04-22 08:43:45 -050034#include "conf-parameter.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050035#include "conf-file-processor.hpp"
akmhoque53353462014-04-22 08:43:45 -050036#include "adjacent.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050037#include "utility/name-helper.hpp"
alvy297f4162015-03-03 17:15:33 -060038#include "update/prefix-update-processor.hpp"
akmhoque53353462014-04-22 08:43:45 -050039
akmhoque53353462014-04-22 08:43:45 -050040namespace nlsr {
41
42using namespace std;
43
Vince Lehman7b616582014-10-17 16:25:39 -050044template <class T>
45class ConfigurationVariable
46{
47public:
48 typedef ndn::function<void(T)> ConfParameterCallback;
49 typedef boost::property_tree::ptree ConfigSection;
50
51 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
52 : m_key(key)
53 , m_setterCallback(setter)
54 , m_minValue(0)
55 , m_maxValue(0)
56 , m_shouldCheckRange(false)
57 , m_isRequired(true)
58 {
59 }
60
61 bool
62 parseFromConfigSection(const ConfigSection& section)
63 {
64 try {
65 T value = section.get<T>(m_key);
66
67 if (!isValidValue(value)) {
68 return false;
69 }
70
71 m_setterCallback(value);
72 return true;
73 }
74 catch (const std::exception& ex) {
75
76 if (m_isRequired) {
77 std::cerr << ex.what() << std::endl;
78 std::cerr << "Missing required configuration variable" << std::endl;
79 return false;
80 }
81 else {
82 m_setterCallback(m_defaultValue);
83 return true;
84 }
85 }
86
87 return false;
88 }
89
90 void
91 setMinAndMaxValue(T min, T max)
92 {
93 m_minValue = min;
94 m_maxValue = max;
95 m_shouldCheckRange = true;
96 }
97
98 void
99 setOptional(T defaultValue)
100 {
101 m_isRequired = false;
102 m_defaultValue = defaultValue;
103 }
104
105private:
106 void
107 printOutOfRangeError(T value)
108 {
109 std::cerr << "Invalid value for " << m_key << ": "
110 << value << ". "
111 << "Valid values: "
112 << m_minValue << " - "
113 << m_maxValue << std::endl;
114 }
115
116 bool
117 isValidValue(T value)
118 {
119 if (!m_shouldCheckRange) {
120 return true;
121 }
122 else if (value < m_minValue || value > m_maxValue)
123 {
124 printOutOfRangeError(value);
125 return false;
126 }
127
128 return true;
129 }
130
131private:
132 const std::string m_key;
133 const ConfParameterCallback m_setterCallback;
134 T m_defaultValue;
135
136 T m_minValue;
137 T m_maxValue;
138
139 bool m_shouldCheckRange;
140 bool m_isRequired;
141};
142
akmhoque157b0a42014-05-13 00:26:37 -0500143bool
akmhoqueb6450b12014-04-24 00:01:03 -0500144ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500145{
akmhoque157b0a42014-05-13 00:26:37 -0500146 bool ret = true;
147 ifstream inputFile;
148 inputFile.open(m_confFileName.c_str());
149 if (!inputFile.is_open()) {
150 string msg = "Failed to read configuration file: ";
151 msg += m_confFileName;
152 cerr << msg << endl;
akmhoquead5fe952014-06-26 13:34:12 -0500153 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500154 }
155 ret = load(inputFile);
156 inputFile.close();
157 return ret;
158}
159
160bool
161ConfFileProcessor::load(istream& input)
162{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700163 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500164 bool ret = true;
165 try {
166 boost::property_tree::read_info(input, pt);
167 }
168 catch (const boost::property_tree::info_parser_error& error) {
169 stringstream msg;
170 std::cerr << "Failed to parse configuration file " << std::endl;
171 std::cerr << m_confFileName << std::endl;
172 return false;
173 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700174
175 for (ConfigSection::const_iterator tn = pt.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500176 tn != pt.end(); ++tn) {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700177 ret = processSection(tn->first, tn->second);
akmhoque157b0a42014-05-13 00:26:37 -0500178 if (ret == false) {
179 break;
180 }
181 }
182 return ret;
183}
184
185bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700186ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500187{
188 bool ret = true;
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700189 if (sectionName == "general")
akmhoque53353462014-04-22 08:43:45 -0500190 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700191 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500192 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700193 else if (sectionName == "neighbors")
akmhoque157b0a42014-05-13 00:26:37 -0500194 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700195 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500196 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700197 else if (sectionName == "hyperbolic")
akmhoque157b0a42014-05-13 00:26:37 -0500198 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700199 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500200 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700201 else if (sectionName == "fib")
akmhoque157b0a42014-05-13 00:26:37 -0500202 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700203 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500204 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700205 else if (sectionName == "advertising")
akmhoque157b0a42014-05-13 00:26:37 -0500206 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700207 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500208 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700209 else if (sectionName == "security")
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700210 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700211 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700212 }
akmhoque157b0a42014-05-13 00:26:37 -0500213 else
214 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700215 std::cerr << "Wrong configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500216 }
217 return ret;
218}
219
220bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700221ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500222{
223 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700224 std::string network = section.get<string>("network");
225 std::string site = section.get<string>("site");
226 std::string router = section.get<string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500227 ndn::Name networkName(network);
228 if (!networkName.empty()) {
229 m_nlsr.getConfParameter().setNetwork(networkName);
230 }
231 else {
232 cerr << " Network can not be null or empty or in bad URI format :(!" << endl;
233 return false;
234 }
235 ndn::Name siteName(site);
236 if (!siteName.empty()) {
237 m_nlsr.getConfParameter().setSiteName(siteName);
238 }
239 else {
240 cerr << "Site can not be null or empty or in bad URI format:( !" << endl;
241 return false;
242 }
243 ndn::Name routerName(router);
244 if (!routerName.empty()) {
245 m_nlsr.getConfParameter().setRouterName(routerName);
246 }
247 else {
248 cerr << " Router name can not be null or empty or in bad URI format:( !" << endl;
249 return false;
250 }
251 }
252 catch (const std::exception& ex) {
253 cerr << ex.what() << endl;
254 return false;
255 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700256
alvya2228c62014-12-09 10:25:11 -0600257 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600258 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600259
260 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
261 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500262 }
alvya2228c62014-12-09 10:25:11 -0600263 else {
264 std::cerr << "Wrong value for lsa-refresh-time ";
265 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
266 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
267
268 return false;
269 }
270
271 // router-dead-interval
alvy5a454952014-12-15 12:49:54 -0600272 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", (2*lsaRefreshTime));
alvya2228c62014-12-09 10:25:11 -0600273
274 if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
275 m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
276 }
277 else {
278 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
279 return false;
280 }
281
282 // lsa-interest-lifetime
283 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
284
285 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
286 m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
287 }
288 else {
289 std::cerr << "Wrong value for lsa-interest-timeout. "
290 << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
291 << LSA_INTEREST_LIFETIME_MAX << std::endl;
292
akmhoque157b0a42014-05-13 00:26:37 -0500293 return false;
294 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700295
alvyd5a13cd2015-01-06 16:34:38 -0600296 // log-level
297 std::string logLevel = section.get<string>("log-level", "INFO");
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500298
alvyd5a13cd2015-01-06 16:34:38 -0600299 if (isValidLogLevel(logLevel)) {
300 m_nlsr.getConfParameter().setLogLevel(logLevel);
akmhoque157b0a42014-05-13 00:26:37 -0500301 }
alvyd5a13cd2015-01-06 16:34:38 -0600302 else {
303 std::cerr << "Invalid value for log-level ";
304 std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500305 return false;
306 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700307
akmhoque674b0b12014-05-20 14:33:28 -0500308 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700309 std::string logDir = section.get<string>("log-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500310 if (boost::filesystem::exists(logDir)) {
311 if (boost::filesystem::is_directory(logDir)) {
312 std::string testFileName=logDir+"/test.log";
313 ofstream testOutFile;
314 testOutFile.open(testFileName.c_str());
315 if (testOutFile.is_open() && testOutFile.good()) {
316 m_nlsr.getConfParameter().setLogDir(logDir);
317 }
318 else {
319 std::cerr << "User does not have read and write permission on the directory";
320 std::cerr << std::endl;
321 return false;
322 }
323 testOutFile.close();
324 remove(testFileName.c_str());
325 }
326 else {
327 std::cerr << "Provided path is not a directory" << std::endl;
328 return false;
329 }
330 }
331 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500332 std::cerr << "Provided log directory <" << logDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500333 return false;
334 }
335 }
336 catch (const std::exception& ex) {
337 std::cerr << "You must configure log directory" << std::endl;
338 std::cerr << ex.what() << std::endl;
339 return false;
340 }
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500341
akmhoque674b0b12014-05-20 14:33:28 -0500342 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700343 std::string seqDir = section.get<string>("seq-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500344 if (boost::filesystem::exists(seqDir)) {
345 if (boost::filesystem::is_directory(seqDir)) {
346 std::string testFileName=seqDir+"/test.seq";
347 ofstream testOutFile;
348 testOutFile.open(testFileName.c_str());
349 if (testOutFile.is_open() && testOutFile.good()) {
350 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
351 }
352 else {
353 std::cerr << "User does not have read and write permission on the directory";
354 std::cerr << std::endl;
355 return false;
356 }
357 testOutFile.close();
358 remove(testFileName.c_str());
359 }
360 else {
361 std::cerr << "Provided path is not a directory" << std::endl;
362 return false;
363 }
364 }
365 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500366 std::cerr << "Provided sequence directory <" << seqDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500367 return false;
368 }
369 }
370 catch (const std::exception& ex) {
371 std::cerr << "You must configure sequence directory" << std::endl;
372 std::cerr << ex.what() << std::endl;
373 return false;
374 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700375
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500376 try {
377 std::string log4cxxPath = section.get<string>("log4cxx-conf");
378
379 if (log4cxxPath == "") {
380 std::cerr << "No value provided for log4cxx-conf" << std::endl;
381 return false;
382 }
383
384 if (boost::filesystem::exists(log4cxxPath)) {
385 m_nlsr.getConfParameter().setLog4CxxConfPath(log4cxxPath);
386 }
387 else {
388 std::cerr << "Provided path for log4cxx-conf <" << log4cxxPath
389 << "> does not exist" << std::endl;
390
391 return false;
392 }
393 }
394 catch (const std::exception& ex) {
395 // Variable is optional so default configuration will be used; continue processing file
396 }
397
akmhoque157b0a42014-05-13 00:26:37 -0500398 return true;
399}
400
401bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700402ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500403{
alvya2228c62014-12-09 10:25:11 -0600404 // hello-retries
405 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
406
407 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
408 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500409 }
alvya2228c62014-12-09 10:25:11 -0600410 else {
411 std::cerr << "Wrong value for hello-retries." << std::endl;
412 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
413 std::cerr << HELLO_RETRIES_MAX << std::endl;
414
akmhoque157b0a42014-05-13 00:26:37 -0500415 return false;
416 }
alvya2228c62014-12-09 10:25:11 -0600417
418 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600419 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600420
421 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
422 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500423 }
alvya2228c62014-12-09 10:25:11 -0600424 else {
425 std::cerr << "Wrong value for hello-timeout. ";
426 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
427 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
428
429 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500430 }
alvya2228c62014-12-09 10:25:11 -0600431
432 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600433 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600434
435 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
436 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500437 }
alvya2228c62014-12-09 10:25:11 -0600438 else {
439 std::cerr << "Wrong value for hello-interval. ";
440 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
441 std::cerr << HELLO_INTERVAL_MAX << std::endl;
442
443 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500444 }
Vince Lehman7b616582014-10-17 16:25:39 -0500445
446 // Event intervals
447 // adj-lsa-build-interval
448 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
449 bind(&ConfParameter::setAdjLsaBuildInterval,
450 &m_nlsr.getConfParameter(), _1));
451 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
452 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
453
454 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
455 return false;
456 }
457
458 // first-hello-interval
459 ConfigurationVariable<uint32_t> firstHelloInterval("first-hello-interval",
460 bind(&ConfParameter::setFirstHelloInterval,
461 &m_nlsr.getConfParameter(), _1));
462 firstHelloInterval.setMinAndMaxValue(FIRST_HELLO_INTERVAL_MIN, FIRST_HELLO_INTERVAL_MAX);
463 firstHelloInterval.setOptional(FIRST_HELLO_INTERVAL_DEFAULT);
464
465 if (!firstHelloInterval.parseFromConfigSection(section)) {
466 return false;
467 }
468
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700469 for (ConfigSection::const_iterator tn =
470 section.begin(); tn != section.end(); ++tn) {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700471
akmhoque157b0a42014-05-13 00:26:37 -0500472 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500473 {
akmhoque157b0a42014-05-13 00:26:37 -0500474 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700475 ConfigSection CommandAttriTree = tn->second;
akmhoque157b0a42014-05-13 00:26:37 -0500476 std::string name = CommandAttriTree.get<std::string>("name");
477 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600478
479 ndn::util::FaceUri uri;
480
481 if (!uri.parse(faceUri)) {
482 std::cerr << "Malformed face-uri <" << faceUri << "> for " << name << std::endl;
483 return false;
484 }
485
akmhoque157b0a42014-05-13 00:26:37 -0500486 double linkCost = CommandAttriTree.get<double>("link-cost",
487 Adjacent::DEFAULT_LINK_COST);
488 ndn::Name neighborName(name);
489 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500490 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500491 m_nlsr.getAdjacencyList().insert(adj);
492 }
493 else {
akmhoque674b0b12014-05-20 14:33:28 -0500494 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500495 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500496 }
497 }
akmhoque157b0a42014-05-13 00:26:37 -0500498 catch (const std::exception& ex) {
499 std::cerr << ex.what() << std::endl;
500 return false;
501 }
akmhoque53353462014-04-22 08:43:45 -0500502 }
503 }
akmhoque157b0a42014-05-13 00:26:37 -0500504 return true;
akmhoque53353462014-04-22 08:43:45 -0500505}
506
akmhoque157b0a42014-05-13 00:26:37 -0500507bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700508ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500509{
alvya2228c62014-12-09 10:25:11 -0600510 // state
511 std::string state = section.get<string>("state", "off");
512
513 if (boost::iequals(state, "off")) {
514 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500515 }
alvya2228c62014-12-09 10:25:11 -0600516 else if (boost::iequals(state, "on")) {
517 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
518 }
519 else if (state == "dry-run") {
520 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
521 }
522 else {
523 std::cerr << "Wrong format for hyperbolic state." << std::endl;
524 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
525
akmhoque157b0a42014-05-13 00:26:37 -0500526 return false;
akmhoque53353462014-04-22 08:43:45 -0500527 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700528
akmhoque157b0a42014-05-13 00:26:37 -0500529 try {
530 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
531 * Even if router can have hyperbolic routing calculation off but other router
532 * in the network may use hyperbolic routing calculation for FIB generation.
533 * So each router need to advertise its hyperbolic coordinates in the network
534 */
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700535 double radius = section.get<double>("radius");
536 double angle = section.get<double>("angle");
akmhoque157b0a42014-05-13 00:26:37 -0500537 if (!m_nlsr.getConfParameter().setCorR(radius)) {
538 return false;
539 }
540 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500541 }
akmhoque157b0a42014-05-13 00:26:37 -0500542 catch (const std::exception& ex) {
543 std::cerr << ex.what() << std::endl;
544 if (state == "on" || state == "dry-run") {
545 return false;
546 }
akmhoque53353462014-04-22 08:43:45 -0500547 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700548
akmhoque157b0a42014-05-13 00:26:37 -0500549 return true;
akmhoque53353462014-04-22 08:43:45 -0500550}
551
akmhoque157b0a42014-05-13 00:26:37 -0500552bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700553ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500554{
alvya2228c62014-12-09 10:25:11 -0600555 // max-faces-per-prefix
556 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
557
558 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
559 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
560 {
561 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500562 }
alvya2228c62014-12-09 10:25:11 -0600563 else {
564 std::cerr << "Wrong value for max-faces-per-prefix. ";
565 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
566
akmhoque157b0a42014-05-13 00:26:37 -0500567 return false;
568 }
Vince Lehman7b616582014-10-17 16:25:39 -0500569
570 // routing-calc-interval
571 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
572 bind(&ConfParameter::setRoutingCalcInterval,
573 &m_nlsr.getConfParameter(), _1));
574 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
575 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
576
577 if (!routingCalcInterval.parseFromConfigSection(section)) {
578 return false;
579 }
580
akmhoque157b0a42014-05-13 00:26:37 -0500581 return true;
akmhoque53353462014-04-22 08:43:45 -0500582}
583
akmhoque157b0a42014-05-13 00:26:37 -0500584bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700585ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500586{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700587 for (ConfigSection::const_iterator tn =
588 section.begin(); tn != section.end(); ++tn) {
akmhoque157b0a42014-05-13 00:26:37 -0500589 if (tn->first == "prefix") {
590 try {
591 std::string prefix = tn->second.data();
592 ndn::Name namePrefix(prefix);
593 if (!namePrefix.empty()) {
594 m_nlsr.getNamePrefixList().insert(namePrefix);
595 }
596 else {
akmhoque674b0b12014-05-20 14:33:28 -0500597 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500598 return false;
599 }
600 }
601 catch (const std::exception& ex) {
602 std::cerr << ex.what() << std::endl;
603 return false;
604 }
akmhoque53353462014-04-22 08:43:45 -0500605 }
akmhoque53353462014-04-22 08:43:45 -0500606 }
akmhoque157b0a42014-05-13 00:26:37 -0500607 return true;
akmhoque53353462014-04-22 08:43:45 -0500608}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700609
610bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700611ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700612{
613 ConfigSection::const_iterator it = section.begin();
614
615 if (it == section.end() || it->first != "validator")
616 {
617 std::cerr << "Error: Expect validator section!" << std::endl;
618 return false;
619 }
620
621 m_nlsr.loadValidator(it->second, m_confFileName);
akmhoqued57f3672014-06-10 10:41:32 -0500622 it++;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700623
alvy297f4162015-03-03 17:15:33 -0600624 if (it == section.end() || it->first != "prefix-update-validator")
625 {
626 std::cerr << "Error: Expect prefix-update-validator section" << std::endl;
627 return false;
628 }
629
630 m_nlsr.getPrefixUpdateProcessor().loadValidator(it->second, m_confFileName);
631 it++;
632
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700633 for (; it != section.end(); it++)
634 {
635 using namespace boost::filesystem;
636 if (it->first != "cert-to-publish")
637 {
638 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
639 return false;
640 }
641
642 std::string file = it->second.data();
643 path certfilePath = absolute(file, path(m_confFileName).parent_path());
644 ndn::shared_ptr<ndn::IdentityCertificate> idCert =
645 ndn::io::load<ndn::IdentityCertificate>(certfilePath.string());
646
647 if (!static_cast<bool>(idCert))
648 {
649 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
650 return false;
651 }
652
653 m_nlsr.loadCertToPublish(idCert);
654 }
655
656 return true;
657}
658
alvy2fe12872014-11-25 10:32:23 -0600659} // namespace nlsr