blob: 37cb2b422afa0d06f414dda4779e5228f9fdb9ad [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 * \author Minsheng Zhang <mzhang4@memphis.edu>
22 *
23 **/
akmhoque53353462014-04-22 08:43:45 -050024#include <iostream>
25#include <fstream>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080026
27#include <ndn-cxx/name.hpp>
alvy2fe12872014-11-25 10:32:23 -060028#include <ndn-cxx/util/face-uri.hpp>
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080029
30// boost needs to be included after ndn-cxx, otherwise there will be conflict with _1, _2, ...
akmhoque157b0a42014-05-13 00:26:37 -050031#include <boost/algorithm/string.hpp>
32#include <boost/property_tree/info_parser.hpp>
33#include <boost/property_tree/ptree.hpp>
akmhoque674b0b12014-05-20 14:33:28 -050034#include <boost/filesystem.hpp>
akmhoque53353462014-04-22 08:43:45 -050035
akmhoque53353462014-04-22 08:43:45 -050036#include "conf-parameter.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050037#include "conf-file-processor.hpp"
akmhoque53353462014-04-22 08:43:45 -050038#include "adjacent.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050039#include "utility/name-helper.hpp"
akmhoque53353462014-04-22 08:43:45 -050040
akmhoque53353462014-04-22 08:43:45 -050041namespace nlsr {
42
43using namespace std;
44
Vince Lehman7b616582014-10-17 16:25:39 -050045template <class T>
46class ConfigurationVariable
47{
48public:
49 typedef ndn::function<void(T)> ConfParameterCallback;
50 typedef boost::property_tree::ptree ConfigSection;
51
52 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
53 : m_key(key)
54 , m_setterCallback(setter)
55 , m_minValue(0)
56 , m_maxValue(0)
57 , m_shouldCheckRange(false)
58 , m_isRequired(true)
59 {
60 }
61
62 bool
63 parseFromConfigSection(const ConfigSection& section)
64 {
65 try {
66 T value = section.get<T>(m_key);
67
68 if (!isValidValue(value)) {
69 return false;
70 }
71
72 m_setterCallback(value);
73 return true;
74 }
75 catch (const std::exception& ex) {
76
77 if (m_isRequired) {
78 std::cerr << ex.what() << std::endl;
79 std::cerr << "Missing required configuration variable" << std::endl;
80 return false;
81 }
82 else {
83 m_setterCallback(m_defaultValue);
84 return true;
85 }
86 }
87
88 return false;
89 }
90
91 void
92 setMinAndMaxValue(T min, T max)
93 {
94 m_minValue = min;
95 m_maxValue = max;
96 m_shouldCheckRange = true;
97 }
98
99 void
100 setOptional(T defaultValue)
101 {
102 m_isRequired = false;
103 m_defaultValue = defaultValue;
104 }
105
106private:
107 void
108 printOutOfRangeError(T value)
109 {
110 std::cerr << "Invalid value for " << m_key << ": "
111 << value << ". "
112 << "Valid values: "
113 << m_minValue << " - "
114 << m_maxValue << std::endl;
115 }
116
117 bool
118 isValidValue(T value)
119 {
120 if (!m_shouldCheckRange) {
121 return true;
122 }
123 else if (value < m_minValue || value > m_maxValue)
124 {
125 printOutOfRangeError(value);
126 return false;
127 }
128
129 return true;
130 }
131
132private:
133 const std::string m_key;
134 const ConfParameterCallback m_setterCallback;
135 T m_defaultValue;
136
137 T m_minValue;
138 T m_maxValue;
139
140 bool m_shouldCheckRange;
141 bool m_isRequired;
142};
143
akmhoque157b0a42014-05-13 00:26:37 -0500144bool
akmhoqueb6450b12014-04-24 00:01:03 -0500145ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500146{
akmhoque157b0a42014-05-13 00:26:37 -0500147 bool ret = true;
148 ifstream inputFile;
149 inputFile.open(m_confFileName.c_str());
150 if (!inputFile.is_open()) {
151 string msg = "Failed to read configuration file: ";
152 msg += m_confFileName;
153 cerr << msg << endl;
akmhoquead5fe952014-06-26 13:34:12 -0500154 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500155 }
156 ret = load(inputFile);
157 inputFile.close();
158 return ret;
159}
160
161bool
162ConfFileProcessor::load(istream& input)
163{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700164 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500165 bool ret = true;
166 try {
167 boost::property_tree::read_info(input, pt);
168 }
169 catch (const boost::property_tree::info_parser_error& error) {
170 stringstream msg;
171 std::cerr << "Failed to parse configuration file " << std::endl;
172 std::cerr << m_confFileName << std::endl;
173 return false;
174 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700175
176 for (ConfigSection::const_iterator tn = pt.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500177 tn != pt.end(); ++tn) {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700178 ret = processSection(tn->first, tn->second);
akmhoque157b0a42014-05-13 00:26:37 -0500179 if (ret == false) {
180 break;
181 }
182 }
183 return ret;
184}
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 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700225 std::string network = section.get<string>("network");
226 std::string site = section.get<string>("site");
227 std::string router = section.get<string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500228 ndn::Name networkName(network);
229 if (!networkName.empty()) {
230 m_nlsr.getConfParameter().setNetwork(networkName);
231 }
232 else {
233 cerr << " Network can not be null or empty or in bad URI format :(!" << endl;
234 return false;
235 }
236 ndn::Name siteName(site);
237 if (!siteName.empty()) {
238 m_nlsr.getConfParameter().setSiteName(siteName);
239 }
240 else {
241 cerr << "Site can not be null or empty or in bad URI format:( !" << endl;
242 return false;
243 }
244 ndn::Name routerName(router);
245 if (!routerName.empty()) {
246 m_nlsr.getConfParameter().setRouterName(routerName);
247 }
248 else {
249 cerr << " Router name can not be null or empty or in bad URI format:( !" << endl;
250 return false;
251 }
252 }
253 catch (const std::exception& ex) {
254 cerr << ex.what() << endl;
255 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) {
262 m_nlsr.getConfParameter().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 ";
266 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
267 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
275 if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
276 m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
277 }
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) {
287 m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
288 }
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
alvyd5a13cd2015-01-06 16:34:38 -0600297 // log-level
298 std::string logLevel = section.get<string>("log-level", "INFO");
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500299
alvyd5a13cd2015-01-06 16:34:38 -0600300 if (isValidLogLevel(logLevel)) {
301 m_nlsr.getConfParameter().setLogLevel(logLevel);
akmhoque157b0a42014-05-13 00:26:37 -0500302 }
alvyd5a13cd2015-01-06 16:34:38 -0600303 else {
304 std::cerr << "Invalid value for log-level ";
305 std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500306 return false;
307 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700308
akmhoque674b0b12014-05-20 14:33:28 -0500309 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700310 std::string logDir = section.get<string>("log-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500311 if (boost::filesystem::exists(logDir)) {
312 if (boost::filesystem::is_directory(logDir)) {
313 std::string testFileName=logDir+"/test.log";
314 ofstream testOutFile;
315 testOutFile.open(testFileName.c_str());
316 if (testOutFile.is_open() && testOutFile.good()) {
317 m_nlsr.getConfParameter().setLogDir(logDir);
318 }
319 else {
320 std::cerr << "User does not have read and write permission on the directory";
321 std::cerr << std::endl;
322 return false;
323 }
324 testOutFile.close();
325 remove(testFileName.c_str());
326 }
327 else {
328 std::cerr << "Provided path is not a directory" << std::endl;
329 return false;
330 }
331 }
332 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500333 std::cerr << "Provided log directory <" << logDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500334 return false;
335 }
336 }
337 catch (const std::exception& ex) {
338 std::cerr << "You must configure log directory" << std::endl;
339 std::cerr << ex.what() << std::endl;
340 return false;
341 }
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500342
akmhoque674b0b12014-05-20 14:33:28 -0500343 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700344 std::string seqDir = section.get<string>("seq-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500345 if (boost::filesystem::exists(seqDir)) {
346 if (boost::filesystem::is_directory(seqDir)) {
347 std::string testFileName=seqDir+"/test.seq";
348 ofstream testOutFile;
349 testOutFile.open(testFileName.c_str());
350 if (testOutFile.is_open() && testOutFile.good()) {
351 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
352 }
353 else {
354 std::cerr << "User does not have read and write permission on the directory";
355 std::cerr << std::endl;
356 return false;
357 }
358 testOutFile.close();
359 remove(testFileName.c_str());
360 }
361 else {
362 std::cerr << "Provided path is not a directory" << std::endl;
363 return false;
364 }
365 }
366 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500367 std::cerr << "Provided sequence directory <" << seqDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500368 return false;
369 }
370 }
371 catch (const std::exception& ex) {
372 std::cerr << "You must configure sequence directory" << std::endl;
373 std::cerr << ex.what() << std::endl;
374 return false;
375 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700376
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500377 try {
378 std::string log4cxxPath = section.get<string>("log4cxx-conf");
379
380 if (log4cxxPath == "") {
381 std::cerr << "No value provided for log4cxx-conf" << std::endl;
382 return false;
383 }
384
385 if (boost::filesystem::exists(log4cxxPath)) {
386 m_nlsr.getConfParameter().setLog4CxxConfPath(log4cxxPath);
387 }
388 else {
389 std::cerr << "Provided path for log4cxx-conf <" << log4cxxPath
390 << "> does not exist" << std::endl;
391
392 return false;
393 }
394 }
395 catch (const std::exception& ex) {
396 // Variable is optional so default configuration will be used; continue processing file
397 }
398
akmhoque157b0a42014-05-13 00:26:37 -0500399 return true;
400}
401
402bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700403ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500404{
alvya2228c62014-12-09 10:25:11 -0600405 // hello-retries
406 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
407
408 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
409 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500410 }
alvya2228c62014-12-09 10:25:11 -0600411 else {
412 std::cerr << "Wrong value for hello-retries." << std::endl;
413 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
414 std::cerr << HELLO_RETRIES_MAX << std::endl;
415
akmhoque157b0a42014-05-13 00:26:37 -0500416 return false;
417 }
alvya2228c62014-12-09 10:25:11 -0600418
419 // hello-timeout
alvy5a454952014-12-15 12:49:54 -0600420 uint32_t timeOut = section.get<uint32_t>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600421
422 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
423 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500424 }
alvya2228c62014-12-09 10:25:11 -0600425 else {
426 std::cerr << "Wrong value for hello-timeout. ";
427 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
428 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
429
430 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500431 }
alvya2228c62014-12-09 10:25:11 -0600432
433 // hello-interval
alvy5a454952014-12-15 12:49:54 -0600434 uint32_t interval = section.get<uint32_t>("hello-interval", HELLO_INTERVAL_DEFAULT);
alvya2228c62014-12-09 10:25:11 -0600435
436 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
437 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500438 }
alvya2228c62014-12-09 10:25:11 -0600439 else {
440 std::cerr << "Wrong value for hello-interval. ";
441 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
442 std::cerr << HELLO_INTERVAL_MAX << std::endl;
443
444 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500445 }
Vince Lehman7b616582014-10-17 16:25:39 -0500446
447 // Event intervals
448 // adj-lsa-build-interval
449 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
450 bind(&ConfParameter::setAdjLsaBuildInterval,
451 &m_nlsr.getConfParameter(), _1));
452 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
453 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
454
455 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
456 return false;
457 }
458
459 // first-hello-interval
460 ConfigurationVariable<uint32_t> firstHelloInterval("first-hello-interval",
461 bind(&ConfParameter::setFirstHelloInterval,
462 &m_nlsr.getConfParameter(), _1));
463 firstHelloInterval.setMinAndMaxValue(FIRST_HELLO_INTERVAL_MIN, FIRST_HELLO_INTERVAL_MAX);
464 firstHelloInterval.setOptional(FIRST_HELLO_INTERVAL_DEFAULT);
465
466 if (!firstHelloInterval.parseFromConfigSection(section)) {
467 return false;
468 }
469
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700470 for (ConfigSection::const_iterator tn =
471 section.begin(); tn != section.end(); ++tn) {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700472
akmhoque157b0a42014-05-13 00:26:37 -0500473 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500474 {
akmhoque157b0a42014-05-13 00:26:37 -0500475 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700476 ConfigSection CommandAttriTree = tn->second;
akmhoque157b0a42014-05-13 00:26:37 -0500477 std::string name = CommandAttriTree.get<std::string>("name");
478 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600479
480 ndn::util::FaceUri uri;
481
482 if (!uri.parse(faceUri)) {
483 std::cerr << "Malformed face-uri <" << faceUri << "> for " << name << std::endl;
484 return false;
485 }
486
akmhoque157b0a42014-05-13 00:26:37 -0500487 double linkCost = CommandAttriTree.get<double>("link-cost",
488 Adjacent::DEFAULT_LINK_COST);
489 ndn::Name neighborName(name);
490 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500491 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500492 m_nlsr.getAdjacencyList().insert(adj);
493 }
494 else {
akmhoque674b0b12014-05-20 14:33:28 -0500495 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500496 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500497 }
498 }
akmhoque157b0a42014-05-13 00:26:37 -0500499 catch (const std::exception& ex) {
500 std::cerr << ex.what() << std::endl;
501 return false;
502 }
akmhoque53353462014-04-22 08:43:45 -0500503 }
504 }
akmhoque157b0a42014-05-13 00:26:37 -0500505 return true;
akmhoque53353462014-04-22 08:43:45 -0500506}
507
akmhoque157b0a42014-05-13 00:26:37 -0500508bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700509ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500510{
alvya2228c62014-12-09 10:25:11 -0600511 // state
512 std::string state = section.get<string>("state", "off");
513
514 if (boost::iequals(state, "off")) {
515 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500516 }
alvya2228c62014-12-09 10:25:11 -0600517 else if (boost::iequals(state, "on")) {
518 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
519 }
520 else if (state == "dry-run") {
521 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
522 }
523 else {
524 std::cerr << "Wrong format for hyperbolic state." << std::endl;
525 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
526
akmhoque157b0a42014-05-13 00:26:37 -0500527 return false;
akmhoque53353462014-04-22 08:43:45 -0500528 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700529
akmhoque157b0a42014-05-13 00:26:37 -0500530 try {
531 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
532 * Even if router can have hyperbolic routing calculation off but other router
533 * in the network may use hyperbolic routing calculation for FIB generation.
534 * So each router need to advertise its hyperbolic coordinates in the network
535 */
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700536 double radius = section.get<double>("radius");
537 double angle = section.get<double>("angle");
akmhoque157b0a42014-05-13 00:26:37 -0500538 if (!m_nlsr.getConfParameter().setCorR(radius)) {
539 return false;
540 }
541 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500542 }
akmhoque157b0a42014-05-13 00:26:37 -0500543 catch (const std::exception& ex) {
544 std::cerr << ex.what() << std::endl;
545 if (state == "on" || state == "dry-run") {
546 return false;
547 }
akmhoque53353462014-04-22 08:43:45 -0500548 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700549
akmhoque157b0a42014-05-13 00:26:37 -0500550 return true;
akmhoque53353462014-04-22 08:43:45 -0500551}
552
akmhoque157b0a42014-05-13 00:26:37 -0500553bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700554ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500555{
alvya2228c62014-12-09 10:25:11 -0600556 // max-faces-per-prefix
557 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
558
559 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
560 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
561 {
562 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500563 }
alvya2228c62014-12-09 10:25:11 -0600564 else {
565 std::cerr << "Wrong value for max-faces-per-prefix. ";
566 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
567
akmhoque157b0a42014-05-13 00:26:37 -0500568 return false;
569 }
Vince Lehman7b616582014-10-17 16:25:39 -0500570
571 // routing-calc-interval
572 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
573 bind(&ConfParameter::setRoutingCalcInterval,
574 &m_nlsr.getConfParameter(), _1));
575 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
576 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
577
578 if (!routingCalcInterval.parseFromConfigSection(section)) {
579 return false;
580 }
581
akmhoque157b0a42014-05-13 00:26:37 -0500582 return true;
akmhoque53353462014-04-22 08:43:45 -0500583}
584
akmhoque157b0a42014-05-13 00:26:37 -0500585bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700586ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500587{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700588 for (ConfigSection::const_iterator tn =
589 section.begin(); tn != section.end(); ++tn) {
akmhoque157b0a42014-05-13 00:26:37 -0500590 if (tn->first == "prefix") {
591 try {
592 std::string prefix = tn->second.data();
593 ndn::Name namePrefix(prefix);
594 if (!namePrefix.empty()) {
595 m_nlsr.getNamePrefixList().insert(namePrefix);
596 }
597 else {
akmhoque674b0b12014-05-20 14:33:28 -0500598 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500599 return false;
600 }
601 }
602 catch (const std::exception& ex) {
603 std::cerr << ex.what() << std::endl;
604 return false;
605 }
akmhoque53353462014-04-22 08:43:45 -0500606 }
akmhoque53353462014-04-22 08:43:45 -0500607 }
akmhoque157b0a42014-05-13 00:26:37 -0500608 return true;
akmhoque53353462014-04-22 08:43:45 -0500609}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700610
611bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700612ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700613{
614 ConfigSection::const_iterator it = section.begin();
615
616 if (it == section.end() || it->first != "validator")
617 {
618 std::cerr << "Error: Expect validator section!" << std::endl;
619 return false;
620 }
621
622 m_nlsr.loadValidator(it->second, m_confFileName);
akmhoqued57f3672014-06-10 10:41:32 -0500623 it++;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700624
625 for (; it != section.end(); it++)
626 {
627 using namespace boost::filesystem;
628 if (it->first != "cert-to-publish")
629 {
630 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
631 return false;
632 }
633
634 std::string file = it->second.data();
635 path certfilePath = absolute(file, path(m_confFileName).parent_path());
636 ndn::shared_ptr<ndn::IdentityCertificate> idCert =
637 ndn::io::load<ndn::IdentityCertificate>(certfilePath.string());
638
639 if (!static_cast<bool>(idCert))
640 {
641 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
642 return false;
643 }
644
645 m_nlsr.loadCertToPublish(idCert);
646 }
647
648 return true;
649}
650
alvy2fe12872014-11-25 10:32:23 -0600651} // namespace nlsr