blob: 1b5dc6130d9fc7fe039fcebcbe460f508665cd58 [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
259 int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time", LSA_REFRESH_TIME_DEFAULT);
260
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
273 int32_t routerDeadInterval = section.get<int32_t>("router-dead-interval", (2*lsaRefreshTime));
274
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
akmhoque157b0a42014-05-13 00:26:37 -0500297 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700298 std::string logLevel = section.get<string>("log-level");
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500299
300 if (isValidLogLevel(logLevel)) {
akmhoque157b0a42014-05-13 00:26:37 -0500301 m_nlsr.getConfParameter().setLogLevel(logLevel);
302 }
303 else {
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500304 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 }
308 }
309 catch (const std::exception& ex) {
310 std::cerr << ex.what() << std::endl;
311 return false;
312 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700313
akmhoque674b0b12014-05-20 14:33:28 -0500314 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700315 std::string logDir = section.get<string>("log-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500316 if (boost::filesystem::exists(logDir)) {
317 if (boost::filesystem::is_directory(logDir)) {
318 std::string testFileName=logDir+"/test.log";
319 ofstream testOutFile;
320 testOutFile.open(testFileName.c_str());
321 if (testOutFile.is_open() && testOutFile.good()) {
322 m_nlsr.getConfParameter().setLogDir(logDir);
323 }
324 else {
325 std::cerr << "User does not have read and write permission on the directory";
326 std::cerr << std::endl;
327 return false;
328 }
329 testOutFile.close();
330 remove(testFileName.c_str());
331 }
332 else {
333 std::cerr << "Provided path is not a directory" << std::endl;
334 return false;
335 }
336 }
337 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500338 std::cerr << "Provided log directory <" << logDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500339 return false;
340 }
341 }
342 catch (const std::exception& ex) {
343 std::cerr << "You must configure log directory" << std::endl;
344 std::cerr << ex.what() << std::endl;
345 return false;
346 }
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500347
akmhoque674b0b12014-05-20 14:33:28 -0500348 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700349 std::string seqDir = section.get<string>("seq-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500350 if (boost::filesystem::exists(seqDir)) {
351 if (boost::filesystem::is_directory(seqDir)) {
352 std::string testFileName=seqDir+"/test.seq";
353 ofstream testOutFile;
354 testOutFile.open(testFileName.c_str());
355 if (testOutFile.is_open() && testOutFile.good()) {
356 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
357 }
358 else {
359 std::cerr << "User does not have read and write permission on the directory";
360 std::cerr << std::endl;
361 return false;
362 }
363 testOutFile.close();
364 remove(testFileName.c_str());
365 }
366 else {
367 std::cerr << "Provided path is not a directory" << std::endl;
368 return false;
369 }
370 }
371 else {
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500372 std::cerr << "Provided sequence directory <" << seqDir << "> does not exist" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500373 return false;
374 }
375 }
376 catch (const std::exception& ex) {
377 std::cerr << "You must configure sequence directory" << std::endl;
378 std::cerr << ex.what() << std::endl;
379 return false;
380 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700381
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500382 try {
383 std::string log4cxxPath = section.get<string>("log4cxx-conf");
384
385 if (log4cxxPath == "") {
386 std::cerr << "No value provided for log4cxx-conf" << std::endl;
387 return false;
388 }
389
390 if (boost::filesystem::exists(log4cxxPath)) {
391 m_nlsr.getConfParameter().setLog4CxxConfPath(log4cxxPath);
392 }
393 else {
394 std::cerr << "Provided path for log4cxx-conf <" << log4cxxPath
395 << "> does not exist" << std::endl;
396
397 return false;
398 }
399 }
400 catch (const std::exception& ex) {
401 // Variable is optional so default configuration will be used; continue processing file
402 }
403
akmhoque157b0a42014-05-13 00:26:37 -0500404 return true;
405}
406
407bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700408ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500409{
alvya2228c62014-12-09 10:25:11 -0600410 // hello-retries
411 int retrials = section.get<int>("hello-retries", HELLO_RETRIES_DEFAULT);
412
413 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
414 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
akmhoque157b0a42014-05-13 00:26:37 -0500415 }
alvya2228c62014-12-09 10:25:11 -0600416 else {
417 std::cerr << "Wrong value for hello-retries." << std::endl;
418 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
419 std::cerr << HELLO_RETRIES_MAX << std::endl;
420
akmhoque157b0a42014-05-13 00:26:37 -0500421 return false;
422 }
alvya2228c62014-12-09 10:25:11 -0600423
424 // hello-timeout
425 int timeOut = section.get<int>("hello-timeout", HELLO_TIMEOUT_DEFAULT);
426
427 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
428 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
akmhoque157b0a42014-05-13 00:26:37 -0500429 }
alvya2228c62014-12-09 10:25:11 -0600430 else {
431 std::cerr << "Wrong value for hello-timeout. ";
432 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
433 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
434
435 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500436 }
alvya2228c62014-12-09 10:25:11 -0600437
438 // hello-interval
439 int interval = section.get<int>("hello-interval", HELLO_INTERVAL_DEFAULT);
440
441 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
442 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
akmhoque157b0a42014-05-13 00:26:37 -0500443 }
alvya2228c62014-12-09 10:25:11 -0600444 else {
445 std::cerr << "Wrong value for hello-interval. ";
446 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
447 std::cerr << HELLO_INTERVAL_MAX << std::endl;
448
449 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500450 }
Vince Lehman7b616582014-10-17 16:25:39 -0500451
452 // Event intervals
453 // adj-lsa-build-interval
454 ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval",
455 bind(&ConfParameter::setAdjLsaBuildInterval,
456 &m_nlsr.getConfParameter(), _1));
457 adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX);
458 adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT);
459
460 if (!adjLsaBuildInterval.parseFromConfigSection(section)) {
461 return false;
462 }
463
464 // first-hello-interval
465 ConfigurationVariable<uint32_t> firstHelloInterval("first-hello-interval",
466 bind(&ConfParameter::setFirstHelloInterval,
467 &m_nlsr.getConfParameter(), _1));
468 firstHelloInterval.setMinAndMaxValue(FIRST_HELLO_INTERVAL_MIN, FIRST_HELLO_INTERVAL_MAX);
469 firstHelloInterval.setOptional(FIRST_HELLO_INTERVAL_DEFAULT);
470
471 if (!firstHelloInterval.parseFromConfigSection(section)) {
472 return false;
473 }
474
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700475 for (ConfigSection::const_iterator tn =
476 section.begin(); tn != section.end(); ++tn) {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700477
akmhoque157b0a42014-05-13 00:26:37 -0500478 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500479 {
akmhoque157b0a42014-05-13 00:26:37 -0500480 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700481 ConfigSection CommandAttriTree = tn->second;
akmhoque157b0a42014-05-13 00:26:37 -0500482 std::string name = CommandAttriTree.get<std::string>("name");
483 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
alvy2fe12872014-11-25 10:32:23 -0600484
485 ndn::util::FaceUri uri;
486
487 if (!uri.parse(faceUri)) {
488 std::cerr << "Malformed face-uri <" << faceUri << "> for " << name << std::endl;
489 return false;
490 }
491
akmhoque157b0a42014-05-13 00:26:37 -0500492 double linkCost = CommandAttriTree.get<double>("link-cost",
493 Adjacent::DEFAULT_LINK_COST);
494 ndn::Name neighborName(name);
495 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500496 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500497 m_nlsr.getAdjacencyList().insert(adj);
498 }
499 else {
akmhoque674b0b12014-05-20 14:33:28 -0500500 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500501 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500502 }
503 }
akmhoque157b0a42014-05-13 00:26:37 -0500504 catch (const std::exception& ex) {
505 std::cerr << ex.what() << std::endl;
506 return false;
507 }
akmhoque53353462014-04-22 08:43:45 -0500508 }
509 }
akmhoque157b0a42014-05-13 00:26:37 -0500510 return true;
akmhoque53353462014-04-22 08:43:45 -0500511}
512
akmhoque157b0a42014-05-13 00:26:37 -0500513bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700514ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500515{
alvya2228c62014-12-09 10:25:11 -0600516 // state
517 std::string state = section.get<string>("state", "off");
518
519 if (boost::iequals(state, "off")) {
520 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
akmhoque53353462014-04-22 08:43:45 -0500521 }
alvya2228c62014-12-09 10:25:11 -0600522 else if (boost::iequals(state, "on")) {
523 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
524 }
525 else if (state == "dry-run") {
526 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
527 }
528 else {
529 std::cerr << "Wrong format for hyperbolic state." << std::endl;
530 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
531
akmhoque157b0a42014-05-13 00:26:37 -0500532 return false;
akmhoque53353462014-04-22 08:43:45 -0500533 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700534
akmhoque157b0a42014-05-13 00:26:37 -0500535 try {
536 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
537 * Even if router can have hyperbolic routing calculation off but other router
538 * in the network may use hyperbolic routing calculation for FIB generation.
539 * So each router need to advertise its hyperbolic coordinates in the network
540 */
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700541 double radius = section.get<double>("radius");
542 double angle = section.get<double>("angle");
akmhoque157b0a42014-05-13 00:26:37 -0500543 if (!m_nlsr.getConfParameter().setCorR(radius)) {
544 return false;
545 }
546 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500547 }
akmhoque157b0a42014-05-13 00:26:37 -0500548 catch (const std::exception& ex) {
549 std::cerr << ex.what() << std::endl;
550 if (state == "on" || state == "dry-run") {
551 return false;
552 }
akmhoque53353462014-04-22 08:43:45 -0500553 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700554
akmhoque157b0a42014-05-13 00:26:37 -0500555 return true;
akmhoque53353462014-04-22 08:43:45 -0500556}
557
akmhoque157b0a42014-05-13 00:26:37 -0500558bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700559ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500560{
alvya2228c62014-12-09 10:25:11 -0600561 // max-faces-per-prefix
562 int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
563
564 if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
565 maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
566 {
567 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefix);
akmhoque53353462014-04-22 08:43:45 -0500568 }
alvya2228c62014-12-09 10:25:11 -0600569 else {
570 std::cerr << "Wrong value for max-faces-per-prefix. ";
571 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
572
akmhoque157b0a42014-05-13 00:26:37 -0500573 return false;
574 }
Vince Lehman7b616582014-10-17 16:25:39 -0500575
576 // routing-calc-interval
577 ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval",
578 bind(&ConfParameter::setRoutingCalcInterval,
579 &m_nlsr.getConfParameter(), _1));
580 routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX);
581 routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT);
582
583 if (!routingCalcInterval.parseFromConfigSection(section)) {
584 return false;
585 }
586
akmhoque157b0a42014-05-13 00:26:37 -0500587 return true;
akmhoque53353462014-04-22 08:43:45 -0500588}
589
akmhoque157b0a42014-05-13 00:26:37 -0500590bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700591ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500592{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700593 for (ConfigSection::const_iterator tn =
594 section.begin(); tn != section.end(); ++tn) {
akmhoque157b0a42014-05-13 00:26:37 -0500595 if (tn->first == "prefix") {
596 try {
597 std::string prefix = tn->second.data();
598 ndn::Name namePrefix(prefix);
599 if (!namePrefix.empty()) {
600 m_nlsr.getNamePrefixList().insert(namePrefix);
601 }
602 else {
akmhoque674b0b12014-05-20 14:33:28 -0500603 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500604 return false;
605 }
606 }
607 catch (const std::exception& ex) {
608 std::cerr << ex.what() << std::endl;
609 return false;
610 }
akmhoque53353462014-04-22 08:43:45 -0500611 }
akmhoque53353462014-04-22 08:43:45 -0500612 }
akmhoque157b0a42014-05-13 00:26:37 -0500613 return true;
akmhoque53353462014-04-22 08:43:45 -0500614}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700615
616bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700617ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700618{
619 ConfigSection::const_iterator it = section.begin();
620
621 if (it == section.end() || it->first != "validator")
622 {
623 std::cerr << "Error: Expect validator section!" << std::endl;
624 return false;
625 }
626
627 m_nlsr.loadValidator(it->second, m_confFileName);
akmhoqued57f3672014-06-10 10:41:32 -0500628 it++;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700629
630 for (; it != section.end(); it++)
631 {
632 using namespace boost::filesystem;
633 if (it->first != "cert-to-publish")
634 {
635 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
636 return false;
637 }
638
639 std::string file = it->second.data();
640 path certfilePath = absolute(file, path(m_confFileName).parent_path());
641 ndn::shared_ptr<ndn::IdentityCertificate> idCert =
642 ndn::io::load<ndn::IdentityCertificate>(certfilePath.string());
643
644 if (!static_cast<bool>(idCert))
645 {
646 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
647 return false;
648 }
649
650 m_nlsr.loadCertToPublish(idCert);
651 }
652
653 return true;
654}
655
alvy2fe12872014-11-25 10:32:23 -0600656} // namespace nlsr