blob: 9a100ecd5d13977d6ddb2663909204c12d905c9d [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
akmhoque157b0a42014-05-13 00:26:37 -0500258 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700259 int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time");
akmhoque157b0a42014-05-13 00:26:37 -0500260 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN &&
261 lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
262 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
263 }
264 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 return false;
269 }
270 }
271 catch (const std::exception& ex) {
272 std::cerr << ex.what() << std::endl;
273 return false;
274 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700275
akmhoque157b0a42014-05-13 00:26:37 -0500276 try {
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700277 int32_t routerDeadInterval = section.get<int32_t>("router-dead-interval");
278
279 if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
280 m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
281 }
282 else {
283 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time"
284 << std::endl;
285 return false;
286 }
287 }
288 catch (const std::exception& ex) {
289 std::cerr << ex.what() << std::endl;
290 // non-critical error. default value is 2 * lsa-refresh-time
291 }
292
293 try {
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700294 int lifetime = section.get<int>("lsa-interest-lifetime");
295 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
296 m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
297 }
298 else {
299 std::cerr << "Wrong value for lsa-interest-timeout. "
300 << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
301 << LSA_INTEREST_LIFETIME_MAX << std::endl;
302 return false;
303 }
304 }
305 catch (const std::exception& ex) {
306 std::cerr << ex.what() << std::endl;
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700307 // non-critical error. default value is 4
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700308 }
309
310 try {
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500311
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700312 std::string logLevel = section.get<string>("log-level");
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500313
314 if (isValidLogLevel(logLevel)) {
akmhoque157b0a42014-05-13 00:26:37 -0500315 m_nlsr.getConfParameter().setLogLevel(logLevel);
316 }
317 else {
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500318 std::cerr << "Invalid value for log-level ";
319 std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500320 return false;
321 }
322 }
323 catch (const std::exception& ex) {
324 std::cerr << ex.what() << std::endl;
325 return false;
326 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700327
akmhoque674b0b12014-05-20 14:33:28 -0500328 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700329 std::string logDir = section.get<string>("log-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500330 if (boost::filesystem::exists(logDir)) {
331 if (boost::filesystem::is_directory(logDir)) {
332 std::string testFileName=logDir+"/test.log";
333 ofstream testOutFile;
334 testOutFile.open(testFileName.c_str());
335 if (testOutFile.is_open() && testOutFile.good()) {
336 m_nlsr.getConfParameter().setLogDir(logDir);
337 }
338 else {
339 std::cerr << "User does not have read and write permission on the directory";
340 std::cerr << std::endl;
341 return false;
342 }
343 testOutFile.close();
344 remove(testFileName.c_str());
345 }
346 else {
347 std::cerr << "Provided path is not a directory" << std::endl;
348 return false;
349 }
350 }
351 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500352 std::cerr << "Provided log directory <" << logDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500353 return false;
354 }
355 }
356 catch (const std::exception& ex) {
357 std::cerr << "You must configure log directory" << std::endl;
358 std::cerr << ex.what() << std::endl;
359 return false;
360 }
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500361
akmhoque674b0b12014-05-20 14:33:28 -0500362 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700363 std::string seqDir = section.get<string>("seq-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500364 if (boost::filesystem::exists(seqDir)) {
365 if (boost::filesystem::is_directory(seqDir)) {
366 std::string testFileName=seqDir+"/test.seq";
367 ofstream testOutFile;
368 testOutFile.open(testFileName.c_str());
369 if (testOutFile.is_open() && testOutFile.good()) {
370 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
371 }
372 else {
373 std::cerr << "User does not have read and write permission on the directory";
374 std::cerr << std::endl;
375 return false;
376 }
377 testOutFile.close();
378 remove(testFileName.c_str());
379 }
380 else {
381 std::cerr << "Provided path is not a directory" << std::endl;
382 return false;
383 }
384 }
385 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500386 std::cerr << "Provided sequence directory <" << seqDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500387 return false;
388 }
389 }
390 catch (const std::exception& ex) {
391 std::cerr << "You must configure sequence directory" << std::endl;
392 std::cerr << ex.what() << std::endl;
393 return false;
394 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700395
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500396 try {
397 std::string log4cxxPath = section.get<string>("log4cxx-conf");
398
399 if (log4cxxPath == "") {
400 std::cerr << "No value provided for log4cxx-conf" << std::endl;
401 return false;
402 }
403
404 if (boost::filesystem::exists(log4cxxPath)) {
405 m_nlsr.getConfParameter().setLog4CxxConfPath(log4cxxPath);
406 }
407 else {
408 std::cerr << "Provided path for log4cxx-conf <" << log4cxxPath
409 << "> does not exist" << std::endl;
410
411 return false;
412 }
413 }
414 catch (const std::exception& ex) {
415 // Variable is optional so default configuration will be used; continue processing file
416 }
417
akmhoque157b0a42014-05-13 00:26:37 -0500418 return true;
419}
420
421bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700422ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500423{
424 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700425 int retrials = section.get<int>("hello-retries");
akmhoque157b0a42014-05-13 00:26:37 -0500426 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
427 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
428 }
429 else {
430 std::cerr << "Wrong value for hello-retries. ";
431 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
432 std::cerr << HELLO_RETRIES_MAX << std::endl;
433 return false;
434 }
435 }
436 catch (const std::exception& ex) {
437 std::cerr << ex.what() << std::endl;
438 return false;
439 }
440 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700441 int timeOut = section.get<int>("hello-timeout");
akmhoque157b0a42014-05-13 00:26:37 -0500442 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
443 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
444 }
445 else {
446 std::cerr << "Wrong value for hello-timeout. ";
447 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
448 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
449 return false;
450 }
451 }
452 catch (const std::exception& ex) {
453 std::cerr << ex.what() << std::endl;
454 }
455 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700456 int interval = section.get<int>("hello-interval");
akmhoque157b0a42014-05-13 00:26:37 -0500457 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
458 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
459 }
460 else {
461 std::cerr << "Wrong value for hello-interval. ";
462 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
463 std::cerr << HELLO_INTERVAL_MAX << std::endl;
464 return false;
465 }
466 }
467 catch (const std::exception& ex) {
468 std::cerr << ex.what() << std::endl;
469 }
Vince Lehman7b616582014-10-17 16:25:39 -0500470
471 // Event intervals
472 // adj-lsa-build-interval
473 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
474 bind(&ConfParameter::setAdjLsaBuildInterval,
475 &m_nlsr.getConfParameter(), _1));
476 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
477 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
478
479 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
480 return false;
481 }
482
483 // first-hello-interval
484 ConfigurationVariable<uint32_t> firstHelloInterval("first-hello-interval",
485 bind(&ConfParameter::setFirstHelloInterval,
486 &m_nlsr.getConfParameter(), _1));
487 firstHelloInterval.setMinAndMaxValue(FIRST_HELLO_INTERVAL_MIN, FIRST_HELLO_INTERVAL_MAX);
488 firstHelloInterval.setOptional(FIRST_HELLO_INTERVAL_DEFAULT);
489
490 if (!firstHelloInterval.parseFromConfigSection(section)) {
491 return false;
492 }
493
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700494 for (ConfigSection::const_iterator tn =
495 section.begin(); tn != section.end(); ++tn) {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700496
akmhoque157b0a42014-05-13 00:26:37 -0500497 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500498 {
akmhoque157b0a42014-05-13 00:26:37 -0500499 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700500 ConfigSection CommandAttriTree = tn->second;
akmhoque157b0a42014-05-13 00:26:37 -0500501 std::string name = CommandAttriTree.get<std::string>("name");
502 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600503
504 ndn::util::FaceUri uri;
505
506 if (!uri.parse(faceUri)) {
507 std::cerr << "Malformed face-uri <" << faceUri << "> for " << name << std::endl;
508 return false;
509 }
510
akmhoque157b0a42014-05-13 00:26:37 -0500511 double linkCost = CommandAttriTree.get<double>("link-cost",
512 Adjacent::DEFAULT_LINK_COST);
513 ndn::Name neighborName(name);
514 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500515 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500516 m_nlsr.getAdjacencyList().insert(adj);
517 }
518 else {
akmhoque674b0b12014-05-20 14:33:28 -0500519 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500520 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500521 }
522 }
akmhoque157b0a42014-05-13 00:26:37 -0500523 catch (const std::exception& ex) {
524 std::cerr << ex.what() << std::endl;
525 return false;
526 }
akmhoque53353462014-04-22 08:43:45 -0500527 }
528 }
akmhoque157b0a42014-05-13 00:26:37 -0500529 return true;
akmhoque53353462014-04-22 08:43:45 -0500530}
531
akmhoque157b0a42014-05-13 00:26:37 -0500532bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700533ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500534{
akmhoque157b0a42014-05-13 00:26:37 -0500535 std::string state;
536 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700537 state= section.get<string>("state","off");
akmhoque157b0a42014-05-13 00:26:37 -0500538 if (boost::iequals(state, "off")) {
539 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
540 }
541 else if (boost::iequals(state, "on")) {
542 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
543 }
544 else if (state == "dry-run") {
545 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
546 }
547 else {
548 std::cerr << "Wrong format for hyperbolic state." << std::endl;
549 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
550 return false;
551 }
akmhoque53353462014-04-22 08:43:45 -0500552 }
akmhoque157b0a42014-05-13 00:26:37 -0500553 catch (const std::exception& ex) {
554 std::cerr << ex.what() << std::endl;
555 return false;
akmhoque53353462014-04-22 08:43:45 -0500556 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700557
akmhoque157b0a42014-05-13 00:26:37 -0500558 try {
559 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
560 * Even if router can have hyperbolic routing calculation off but other router
561 * in the network may use hyperbolic routing calculation for FIB generation.
562 * So each router need to advertise its hyperbolic coordinates in the network
563 */
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700564 double radius = section.get<double>("radius");
565 double angle = section.get<double>("angle");
akmhoque157b0a42014-05-13 00:26:37 -0500566 if (!m_nlsr.getConfParameter().setCorR(radius)) {
567 return false;
568 }
569 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500570 }
akmhoque157b0a42014-05-13 00:26:37 -0500571 catch (const std::exception& ex) {
572 std::cerr << ex.what() << std::endl;
573 if (state == "on" || state == "dry-run") {
574 return false;
575 }
akmhoque53353462014-04-22 08:43:45 -0500576 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700577
akmhoque157b0a42014-05-13 00:26:37 -0500578 return true;
akmhoque53353462014-04-22 08:43:45 -0500579}
580
akmhoque157b0a42014-05-13 00:26:37 -0500581bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700582ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500583{
akmhoque157b0a42014-05-13 00:26:37 -0500584 try {
585 int maxFacesPerPrefixNumber =
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700586 section.get<int>("max-faces-per-prefix");
akmhoque157b0a42014-05-13 00:26:37 -0500587 if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN &&
588 maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX)
akmhoque53353462014-04-22 08:43:45 -0500589 {
akmhoque157b0a42014-05-13 00:26:37 -0500590 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber);
akmhoque53353462014-04-22 08:43:45 -0500591 }
akmhoque157b0a42014-05-13 00:26:37 -0500592 else {
593 std::cerr << "Wrong value for max-faces-per-prefix. ";
akmhoque157b0a42014-05-13 00:26:37 -0500594 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
595 return false;
akmhoque53353462014-04-22 08:43:45 -0500596 }
akmhoque53353462014-04-22 08:43:45 -0500597 }
akmhoque157b0a42014-05-13 00:26:37 -0500598 catch (const std::exception& ex) {
599 cerr << ex.what() << endl;
600 return false;
601 }
Vince Lehman7b616582014-10-17 16:25:39 -0500602
603 // routing-calc-interval
604 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
605 bind(&ConfParameter::setRoutingCalcInterval,
606 &m_nlsr.getConfParameter(), _1));
607 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
608 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
609
610 if (!routingCalcInterval.parseFromConfigSection(section)) {
611 return false;
612 }
613
akmhoque157b0a42014-05-13 00:26:37 -0500614 return true;
akmhoque53353462014-04-22 08:43:45 -0500615}
616
akmhoque157b0a42014-05-13 00:26:37 -0500617bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700618ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500619{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700620 for (ConfigSection::const_iterator tn =
621 section.begin(); tn != section.end(); ++tn) {
akmhoque157b0a42014-05-13 00:26:37 -0500622 if (tn->first == "prefix") {
623 try {
624 std::string prefix = tn->second.data();
625 ndn::Name namePrefix(prefix);
626 if (!namePrefix.empty()) {
627 m_nlsr.getNamePrefixList().insert(namePrefix);
628 }
629 else {
akmhoque674b0b12014-05-20 14:33:28 -0500630 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500631 return false;
632 }
633 }
634 catch (const std::exception& ex) {
635 std::cerr << ex.what() << std::endl;
636 return false;
637 }
akmhoque53353462014-04-22 08:43:45 -0500638 }
akmhoque53353462014-04-22 08:43:45 -0500639 }
akmhoque157b0a42014-05-13 00:26:37 -0500640 return true;
akmhoque53353462014-04-22 08:43:45 -0500641}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700642
643bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700644ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700645{
646 ConfigSection::const_iterator it = section.begin();
647
648 if (it == section.end() || it->first != "validator")
649 {
650 std::cerr << "Error: Expect validator section!" << std::endl;
651 return false;
652 }
653
654 m_nlsr.loadValidator(it->second, m_confFileName);
akmhoqued57f3672014-06-10 10:41:32 -0500655 it++;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700656
657 for (; it != section.end(); it++)
658 {
659 using namespace boost::filesystem;
660 if (it->first != "cert-to-publish")
661 {
662 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
663 return false;
664 }
665
666 std::string file = it->second.data();
667 path certfilePath = absolute(file, path(m_confFileName).parent_path());
668 ndn::shared_ptr<ndn::IdentityCertificate> idCert =
669 ndn::io::load<ndn::IdentityCertificate>(certfilePath.string());
670
671 if (!static_cast<bool>(idCert))
672 {
673 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
674 return false;
675 }
676
677 m_nlsr.loadCertToPublish(idCert);
678 }
679
680 return true;
681}
682
alvy2fe12872014-11-25 10:32:23 -0600683} // namespace nlsr