blob: 10667f1e829de57ca8065f4cc82c12076bb64e1e [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"
akmhoque53353462014-04-22 08:43:45 -050038
akmhoque53353462014-04-22 08:43:45 -050039namespace nlsr {
40
41using namespace std;
42
Vince Lehman7b616582014-10-17 16:25:39 -050043template <class T>
44class ConfigurationVariable
45{
46public:
47 typedef ndn::function<void(T)> ConfParameterCallback;
48 typedef boost::property_tree::ptree ConfigSection;
49
50 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
51 : m_key(key)
52 , m_setterCallback(setter)
53 , m_minValue(0)
54 , m_maxValue(0)
55 , m_shouldCheckRange(false)
56 , m_isRequired(true)
57 {
58 }
59
60 bool
61 parseFromConfigSection(const ConfigSection& section)
62 {
63 try {
64 T value = section.get<T>(m_key);
65
66 if (!isValidValue(value)) {
67 return false;
68 }
69
70 m_setterCallback(value);
71 return true;
72 }
73 catch (const std::exception& ex) {
74
75 if (m_isRequired) {
76 std::cerr << ex.what() << std::endl;
77 std::cerr << "Missing required configuration variable" << std::endl;
78 return false;
79 }
80 else {
81 m_setterCallback(m_defaultValue);
82 return true;
83 }
84 }
85
86 return false;
87 }
88
89 void
90 setMinAndMaxValue(T min, T max)
91 {
92 m_minValue = min;
93 m_maxValue = max;
94 m_shouldCheckRange = true;
95 }
96
97 void
98 setOptional(T defaultValue)
99 {
100 m_isRequired = false;
101 m_defaultValue = defaultValue;
102 }
103
104private:
105 void
106 printOutOfRangeError(T value)
107 {
108 std::cerr << "Invalid value for " << m_key << ": "
109 << value << ". "
110 << "Valid values: "
111 << m_minValue << " - "
112 << m_maxValue << std::endl;
113 }
114
115 bool
116 isValidValue(T value)
117 {
118 if (!m_shouldCheckRange) {
119 return true;
120 }
121 else if (value < m_minValue || value > m_maxValue)
122 {
123 printOutOfRangeError(value);
124 return false;
125 }
126
127 return true;
128 }
129
130private:
131 const std::string m_key;
132 const ConfParameterCallback m_setterCallback;
133 T m_defaultValue;
134
135 T m_minValue;
136 T m_maxValue;
137
138 bool m_shouldCheckRange;
139 bool m_isRequired;
140};
141
akmhoque157b0a42014-05-13 00:26:37 -0500142bool
akmhoqueb6450b12014-04-24 00:01:03 -0500143ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500144{
akmhoque157b0a42014-05-13 00:26:37 -0500145 bool ret = true;
146 ifstream inputFile;
147 inputFile.open(m_confFileName.c_str());
148 if (!inputFile.is_open()) {
149 string msg = "Failed to read configuration file: ";
150 msg += m_confFileName;
151 cerr << msg << endl;
akmhoquead5fe952014-06-26 13:34:12 -0500152 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500153 }
154 ret = load(inputFile);
155 inputFile.close();
156 return ret;
157}
158
159bool
160ConfFileProcessor::load(istream& input)
161{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700162 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500163 bool ret = true;
164 try {
165 boost::property_tree::read_info(input, pt);
166 }
167 catch (const boost::property_tree::info_parser_error& error) {
168 stringstream msg;
169 std::cerr << "Failed to parse configuration file " << std::endl;
170 std::cerr << m_confFileName << std::endl;
171 return false;
172 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700173
174 for (ConfigSection::const_iterator tn = pt.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500175 tn != pt.end(); ++tn) {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700176 ret = processSection(tn->first, tn->second);
akmhoque157b0a42014-05-13 00:26:37 -0500177 if (ret == false) {
178 break;
179 }
180 }
181 return ret;
182}
183
184bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700185ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500186{
187 bool ret = true;
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700188 if (sectionName == "general")
akmhoque53353462014-04-22 08:43:45 -0500189 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700190 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500191 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700192 else if (sectionName == "neighbors")
akmhoque157b0a42014-05-13 00:26:37 -0500193 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700194 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500195 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700196 else if (sectionName == "hyperbolic")
akmhoque157b0a42014-05-13 00:26:37 -0500197 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700198 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500199 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700200 else if (sectionName == "fib")
akmhoque157b0a42014-05-13 00:26:37 -0500201 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700202 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500203 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700204 else if (sectionName == "advertising")
akmhoque157b0a42014-05-13 00:26:37 -0500205 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700206 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500207 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700208 else if (sectionName == "security")
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700209 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700210 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700211 }
akmhoque157b0a42014-05-13 00:26:37 -0500212 else
213 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700214 std::cerr << "Wrong configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500215 }
216 return ret;
217}
218
219bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700220ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500221{
222 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700223 std::string network = section.get<string>("network");
224 std::string site = section.get<string>("site");
225 std::string router = section.get<string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500226 ndn::Name networkName(network);
227 if (!networkName.empty()) {
228 m_nlsr.getConfParameter().setNetwork(networkName);
229 }
230 else {
231 cerr << " Network can not be null or empty or in bad URI format :(!" << endl;
232 return false;
233 }
234 ndn::Name siteName(site);
235 if (!siteName.empty()) {
236 m_nlsr.getConfParameter().setSiteName(siteName);
237 }
238 else {
239 cerr << "Site can not be null or empty or in bad URI format:( !" << endl;
240 return false;
241 }
242 ndn::Name routerName(router);
243 if (!routerName.empty()) {
244 m_nlsr.getConfParameter().setRouterName(routerName);
245 }
246 else {
247 cerr << " Router name can not be null or empty or in bad URI format:( !" << endl;
248 return false;
249 }
250 }
251 catch (const std::exception& ex) {
252 cerr << ex.what() << endl;
253 return false;
254 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700255
alvya2228c62014-12-09 10:25:11 -0600256 // lsa-refresh-time
alvy5a454952014-12-15 12:49:54 -0600257 uint32_t lsaRefreshTime = section.get<uint32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600258
259 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
260 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
akmhoque157b0a42014-05-13 00:26:37 -0500261 }
alvya2228c62014-12-09 10:25:11 -0600262 else {
263 std::cerr << "Wrong value for lsa-refresh-time ";
264 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
265 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
266
267 return false;
268 }
269
270 // router-dead-interval
alvy5a454952014-12-15 12:49:54 -0600271 uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", (2*lsaRefreshTime));
alvya2228c62014-12-09 10:25:11 -0600272
273 if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
274 m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
275 }
276 else {
277 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
278 return false;
279 }
280
281 // lsa-interest-lifetime
282 int lifetime = section.get<int>("lsa-interest-lifetime", LSA_INTEREST_LIFETIME_DEFAULT);
283
284 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
285 m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
286 }
287 else {
288 std::cerr << "Wrong value for lsa-interest-timeout. "
289 << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
290 << LSA_INTEREST_LIFETIME_MAX << std::endl;
291
akmhoque157b0a42014-05-13 00:26:37 -0500292 return false;
293 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700294
alvyd5a13cd2015-01-06 16:34:38 -0600295 // log-level
296 std::string logLevel = section.get<string>("log-level", "INFO");
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500297
alvyd5a13cd2015-01-06 16:34:38 -0600298 if (isValidLogLevel(logLevel)) {
299 m_nlsr.getConfParameter().setLogLevel(logLevel);
akmhoque157b0a42014-05-13 00:26:37 -0500300 }
alvyd5a13cd2015-01-06 16:34:38 -0600301 else {
302 std::cerr << "Invalid value for log-level ";
303 std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500304 return false;
305 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700306
akmhoque674b0b12014-05-20 14:33:28 -0500307 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700308 std::string logDir = section.get<string>("log-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500309 if (boost::filesystem::exists(logDir)) {
310 if (boost::filesystem::is_directory(logDir)) {
311 std::string testFileName=logDir+"/test.log";
312 ofstream testOutFile;
313 testOutFile.open(testFileName.c_str());
314 if (testOutFile.is_open() && testOutFile.good()) {
315 m_nlsr.getConfParameter().setLogDir(logDir);
316 }
317 else {
318 std::cerr << "User does not have read and write permission on the directory";
319 std::cerr << std::endl;
320 return false;
321 }
322 testOutFile.close();
323 remove(testFileName.c_str());
324 }
325 else {
326 std::cerr << "Provided path is not a directory" << std::endl;
327 return false;
328 }
329 }
330 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500331 std::cerr << "Provided log directory <" << logDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500332 return false;
333 }
334 }
335 catch (const std::exception& ex) {
336 std::cerr << "You must configure log directory" << std::endl;
337 std::cerr << ex.what() << std::endl;
338 return false;
339 }
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500340
akmhoque674b0b12014-05-20 14:33:28 -0500341 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700342 std::string seqDir = section.get<string>("seq-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500343 if (boost::filesystem::exists(seqDir)) {
344 if (boost::filesystem::is_directory(seqDir)) {
345 std::string testFileName=seqDir+"/test.seq";
346 ofstream testOutFile;
347 testOutFile.open(testFileName.c_str());
348 if (testOutFile.is_open() && testOutFile.good()) {
349 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
350 }
351 else {
352 std::cerr << "User does not have read and write permission on the directory";
353 std::cerr << std::endl;
354 return false;
355 }
356 testOutFile.close();
357 remove(testFileName.c_str());
358 }
359 else {
360 std::cerr << "Provided path is not a directory" << std::endl;
361 return false;
362 }
363 }
364 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500365 std::cerr << "Provided sequence directory <" << seqDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500366 return false;
367 }
368 }
369 catch (const std::exception& ex) {
370 std::cerr << "You must configure sequence directory" << std::endl;
371 std::cerr << ex.what() << std::endl;
372 return false;
373 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700374
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500375 try {
376 std::string log4cxxPath = section.get<string>("log4cxx-conf");
377
378 if (log4cxxPath == "") {
379 std::cerr << "No value provided for log4cxx-conf" << std::endl;
380 return false;
381 }
382
383 if (boost::filesystem::exists(log4cxxPath)) {
384 m_nlsr.getConfParameter().setLog4CxxConfPath(log4cxxPath);
385 }
386 else {
387 std::cerr << "Provided path for log4cxx-conf <" << log4cxxPath
388 << "> does not exist" << std::endl;
389
390 return false;
391 }
392 }
393 catch (const std::exception& ex) {
394 // Variable is optional so default configuration will be used; continue processing file
395 }
396
akmhoque157b0a42014-05-13 00:26:37 -0500397 return true;
398}
399
400bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700401ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500402{
alvya2228c62014-12-09 10:25:11 -0600403 // hello-retries
404 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
405
406 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
407 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500408 }
alvya2228c62014-12-09 10:25:11 -0600409 else {
410 std::cerr << "Wrong value for hello-retries." << std::endl;
411 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
412 std::cerr << HELLO_RETRIES_MAX << std::endl;
413
akmhoque157b0a42014-05-13 00:26:37 -0500414 return false;
415 }
alvya2228c62014-12-09 10:25:11 -0600416
417 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600418 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600419
420 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
421 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500422 }
alvya2228c62014-12-09 10:25:11 -0600423 else {
424 std::cerr << "Wrong value for hello-timeout. ";
425 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
426 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
427
428 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500429 }
alvya2228c62014-12-09 10:25:11 -0600430
431 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600432 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600433
434 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
435 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500436 }
alvya2228c62014-12-09 10:25:11 -0600437 else {
438 std::cerr << "Wrong value for hello-interval. ";
439 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
440 std::cerr << HELLO_INTERVAL_MAX << std::endl;
441
442 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500443 }
Vince Lehman7b616582014-10-17 16:25:39 -0500444
445 // Event intervals
446 // adj-lsa-build-interval
447 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
448 bind(&ConfParameter::setAdjLsaBuildInterval,
449 &m_nlsr.getConfParameter(), _1));
450 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
451 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
452
453 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
454 return false;
455 }
456
457 // first-hello-interval
458 ConfigurationVariable<uint32_t> firstHelloInterval("first-hello-interval",
459 bind(&ConfParameter::setFirstHelloInterval,
460 &m_nlsr.getConfParameter(), _1));
461 firstHelloInterval.setMinAndMaxValue(FIRST_HELLO_INTERVAL_MIN, FIRST_HELLO_INTERVAL_MAX);
462 firstHelloInterval.setOptional(FIRST_HELLO_INTERVAL_DEFAULT);
463
464 if (!firstHelloInterval.parseFromConfigSection(section)) {
465 return false;
466 }
467
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700468 for (ConfigSection::const_iterator tn =
469 section.begin(); tn != section.end(); ++tn) {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700470
akmhoque157b0a42014-05-13 00:26:37 -0500471 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500472 {
akmhoque157b0a42014-05-13 00:26:37 -0500473 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700474 ConfigSection CommandAttriTree = tn->second;
akmhoque157b0a42014-05-13 00:26:37 -0500475 std::string name = CommandAttriTree.get<std::string>("name");
476 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600477
478 ndn::util::FaceUri uri;
479
480 if (!uri.parse(faceUri)) {
481 std::cerr << "Malformed face-uri <" << faceUri << "> for " << name << std::endl;
482 return false;
483 }
484
akmhoque157b0a42014-05-13 00:26:37 -0500485 double linkCost = CommandAttriTree.get<double>("link-cost",
486 Adjacent::DEFAULT_LINK_COST);
487 ndn::Name neighborName(name);
488 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500489 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500490 m_nlsr.getAdjacencyList().insert(adj);
491 }
492 else {
akmhoque674b0b12014-05-20 14:33:28 -0500493 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500494 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500495 }
496 }
akmhoque157b0a42014-05-13 00:26:37 -0500497 catch (const std::exception& ex) {
498 std::cerr << ex.what() << std::endl;
499 return false;
500 }
akmhoque53353462014-04-22 08:43:45 -0500501 }
502 }
akmhoque157b0a42014-05-13 00:26:37 -0500503 return true;
akmhoque53353462014-04-22 08:43:45 -0500504}
505
akmhoque157b0a42014-05-13 00:26:37 -0500506bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700507ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500508{
alvya2228c62014-12-09 10:25:11 -0600509 // state
510 std::string state = section.get<string>("state", "off");
511
512 if (boost::iequals(state, "off")) {
513 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500514 }
alvya2228c62014-12-09 10:25:11 -0600515 else if (boost::iequals(state, "on")) {
516 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
517 }
518 else if (state == "dry-run") {
519 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
520 }
521 else {
522 std::cerr << "Wrong format for hyperbolic state." << std::endl;
523 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
524
akmhoque157b0a42014-05-13 00:26:37 -0500525 return false;
akmhoque53353462014-04-22 08:43:45 -0500526 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700527
akmhoque157b0a42014-05-13 00:26:37 -0500528 try {
529 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
530 * Even if router can have hyperbolic routing calculation off but other router
531 * in the network may use hyperbolic routing calculation for FIB generation.
532 * So each router need to advertise its hyperbolic coordinates in the network
533 */
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700534 double radius = section.get<double>("radius");
535 double angle = section.get<double>("angle");
akmhoque157b0a42014-05-13 00:26:37 -0500536 if (!m_nlsr.getConfParameter().setCorR(radius)) {
537 return false;
538 }
539 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500540 }
akmhoque157b0a42014-05-13 00:26:37 -0500541 catch (const std::exception& ex) {
542 std::cerr << ex.what() << std::endl;
543 if (state == "on" || state == "dry-run") {
544 return false;
545 }
akmhoque53353462014-04-22 08:43:45 -0500546 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700547
akmhoque157b0a42014-05-13 00:26:37 -0500548 return true;
akmhoque53353462014-04-22 08:43:45 -0500549}
550
akmhoque157b0a42014-05-13 00:26:37 -0500551bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700552ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500553{
alvya2228c62014-12-09 10:25:11 -0600554 // max-faces-per-prefix
555 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
556
557 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
558 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
559 {
560 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500561 }
alvya2228c62014-12-09 10:25:11 -0600562 else {
563 std::cerr << "Wrong value for max-faces-per-prefix. ";
564 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
565
akmhoque157b0a42014-05-13 00:26:37 -0500566 return false;
567 }
Vince Lehman7b616582014-10-17 16:25:39 -0500568
569 // routing-calc-interval
570 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
571 bind(&ConfParameter::setRoutingCalcInterval,
572 &m_nlsr.getConfParameter(), _1));
573 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
574 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
575
576 if (!routingCalcInterval.parseFromConfigSection(section)) {
577 return false;
578 }
579
akmhoque157b0a42014-05-13 00:26:37 -0500580 return true;
akmhoque53353462014-04-22 08:43:45 -0500581}
582
akmhoque157b0a42014-05-13 00:26:37 -0500583bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700584ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500585{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700586 for (ConfigSection::const_iterator tn =
587 section.begin(); tn != section.end(); ++tn) {
akmhoque157b0a42014-05-13 00:26:37 -0500588 if (tn->first == "prefix") {
589 try {
590 std::string prefix = tn->second.data();
591 ndn::Name namePrefix(prefix);
592 if (!namePrefix.empty()) {
593 m_nlsr.getNamePrefixList().insert(namePrefix);
594 }
595 else {
akmhoque674b0b12014-05-20 14:33:28 -0500596 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500597 return false;
598 }
599 }
600 catch (const std::exception& ex) {
601 std::cerr << ex.what() << std::endl;
602 return false;
603 }
akmhoque53353462014-04-22 08:43:45 -0500604 }
akmhoque53353462014-04-22 08:43:45 -0500605 }
akmhoque157b0a42014-05-13 00:26:37 -0500606 return true;
akmhoque53353462014-04-22 08:43:45 -0500607}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700608
609bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700610ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700611{
612 ConfigSection::const_iterator it = section.begin();
613
614 if (it == section.end() || it->first != "validator")
615 {
616 std::cerr << "Error: Expect validator section!" << std::endl;
617 return false;
618 }
619
620 m_nlsr.loadValidator(it->second, m_confFileName);
akmhoqued57f3672014-06-10 10:41:32 -0500621 it++;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700622
623 for (; it != section.end(); it++)
624 {
625 using namespace boost::filesystem;
626 if (it->first != "cert-to-publish")
627 {
628 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
629 return false;
630 }
631
632 std::string file = it->second.data();
633 path certfilePath = absolute(file, path(m_confFileName).parent_path());
634 ndn::shared_ptr<ndn::IdentityCertificate> idCert =
635 ndn::io::load<ndn::IdentityCertificate>(certfilePath.string());
636
637 if (!static_cast<bool>(idCert))
638 {
639 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
640 return false;
641 }
642
643 m_nlsr.loadCertToPublish(idCert);
644 }
645
646 return true;
647}
648
alvy2fe12872014-11-25 10:32:23 -0600649} // namespace nlsr