blob: 3728a09dfad2222e7b433423d209571d155ff01b [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>
akmhoque157b0a42014-05-13 00:26:37 -050026#include <boost/algorithm/string.hpp>
27#include <boost/property_tree/info_parser.hpp>
28#include <boost/property_tree/ptree.hpp>
akmhoque674b0b12014-05-20 14:33:28 -050029#include <boost/filesystem.hpp>
akmhoque53353462014-04-22 08:43:45 -050030
akmhoque157b0a42014-05-13 00:26:37 -050031#include <ndn-cxx/name.hpp>
32
akmhoque53353462014-04-22 08:43:45 -050033#include "conf-parameter.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050034#include "conf-file-processor.hpp"
akmhoque53353462014-04-22 08:43:45 -050035#include "adjacent.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050036#include "utility/name-helper.hpp"
akmhoque53353462014-04-22 08:43:45 -050037
akmhoque53353462014-04-22 08:43:45 -050038namespace nlsr {
39
40using namespace std;
41
Vince Lehman7b616582014-10-17 16:25:39 -050042template <class T>
43class ConfigurationVariable
44{
45public:
46 typedef ndn::function<void(T)> ConfParameterCallback;
47 typedef boost::property_tree::ptree ConfigSection;
48
49 ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter)
50 : m_key(key)
51 , m_setterCallback(setter)
52 , m_minValue(0)
53 , m_maxValue(0)
54 , m_shouldCheckRange(false)
55 , m_isRequired(true)
56 {
57 }
58
59 bool
60 parseFromConfigSection(const ConfigSection& section)
61 {
62 try {
63 T value = section.get<T>(m_key);
64
65 if (!isValidValue(value)) {
66 return false;
67 }
68
69 m_setterCallback(value);
70 return true;
71 }
72 catch (const std::exception& ex) {
73
74 if (m_isRequired) {
75 std::cerr << ex.what() << std::endl;
76 std::cerr << "Missing required configuration variable" << std::endl;
77 return false;
78 }
79 else {
80 m_setterCallback(m_defaultValue);
81 return true;
82 }
83 }
84
85 return false;
86 }
87
88 void
89 setMinAndMaxValue(T min, T max)
90 {
91 m_minValue = min;
92 m_maxValue = max;
93 m_shouldCheckRange = true;
94 }
95
96 void
97 setOptional(T defaultValue)
98 {
99 m_isRequired = false;
100 m_defaultValue = defaultValue;
101 }
102
103private:
104 void
105 printOutOfRangeError(T value)
106 {
107 std::cerr << "Invalid value for " << m_key << ": "
108 << value << ". "
109 << "Valid values: "
110 << m_minValue << " - "
111 << m_maxValue << std::endl;
112 }
113
114 bool
115 isValidValue(T value)
116 {
117 if (!m_shouldCheckRange) {
118 return true;
119 }
120 else if (value < m_minValue || value > m_maxValue)
121 {
122 printOutOfRangeError(value);
123 return false;
124 }
125
126 return true;
127 }
128
129private:
130 const std::string m_key;
131 const ConfParameterCallback m_setterCallback;
132 T m_defaultValue;
133
134 T m_minValue;
135 T m_maxValue;
136
137 bool m_shouldCheckRange;
138 bool m_isRequired;
139};
140
akmhoque157b0a42014-05-13 00:26:37 -0500141bool
akmhoqueb6450b12014-04-24 00:01:03 -0500142ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -0500143{
akmhoque157b0a42014-05-13 00:26:37 -0500144 bool ret = true;
145 ifstream inputFile;
146 inputFile.open(m_confFileName.c_str());
147 if (!inputFile.is_open()) {
148 string msg = "Failed to read configuration file: ";
149 msg += m_confFileName;
150 cerr << msg << endl;
akmhoquead5fe952014-06-26 13:34:12 -0500151 return false;
akmhoque157b0a42014-05-13 00:26:37 -0500152 }
153 ret = load(inputFile);
154 inputFile.close();
155 return ret;
156}
157
158bool
159ConfFileProcessor::load(istream& input)
160{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700161 ConfigSection pt;
akmhoque157b0a42014-05-13 00:26:37 -0500162 bool ret = true;
163 try {
164 boost::property_tree::read_info(input, pt);
165 }
166 catch (const boost::property_tree::info_parser_error& error) {
167 stringstream msg;
168 std::cerr << "Failed to parse configuration file " << std::endl;
169 std::cerr << m_confFileName << std::endl;
170 return false;
171 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700172
173 for (ConfigSection::const_iterator tn = pt.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500174 tn != pt.end(); ++tn) {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700175 ret = processSection(tn->first, tn->second);
akmhoque157b0a42014-05-13 00:26:37 -0500176 if (ret == false) {
177 break;
178 }
179 }
180 return ret;
181}
182
183bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700184ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500185{
186 bool ret = true;
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700187 if (sectionName == "general")
akmhoque53353462014-04-22 08:43:45 -0500188 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700189 ret = processConfSectionGeneral(section);
akmhoque157b0a42014-05-13 00:26:37 -0500190 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700191 else if (sectionName == "neighbors")
akmhoque157b0a42014-05-13 00:26:37 -0500192 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700193 ret = processConfSectionNeighbors(section);
akmhoque157b0a42014-05-13 00:26:37 -0500194 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700195 else if (sectionName == "hyperbolic")
akmhoque157b0a42014-05-13 00:26:37 -0500196 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700197 ret = processConfSectionHyperbolic(section);
akmhoque157b0a42014-05-13 00:26:37 -0500198 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700199 else if (sectionName == "fib")
akmhoque157b0a42014-05-13 00:26:37 -0500200 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700201 ret = processConfSectionFib(section);
akmhoque157b0a42014-05-13 00:26:37 -0500202 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700203 else if (sectionName == "advertising")
akmhoque157b0a42014-05-13 00:26:37 -0500204 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700205 ret = processConfSectionAdvertising(section);
akmhoque157b0a42014-05-13 00:26:37 -0500206 }
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700207 else if (sectionName == "security")
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700208 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700209 ret = processConfSectionSecurity(section);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700210 }
akmhoque157b0a42014-05-13 00:26:37 -0500211 else
212 {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700213 std::cerr << "Wrong configuration section: " << sectionName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500214 }
215 return ret;
216}
217
218bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700219ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500220{
221 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700222 std::string network = section.get<string>("network");
223 std::string site = section.get<string>("site");
224 std::string router = section.get<string>("router");
akmhoque157b0a42014-05-13 00:26:37 -0500225 ndn::Name networkName(network);
226 if (!networkName.empty()) {
227 m_nlsr.getConfParameter().setNetwork(networkName);
228 }
229 else {
230 cerr << " Network can not be null or empty or in bad URI format :(!" << endl;
231 return false;
232 }
233 ndn::Name siteName(site);
234 if (!siteName.empty()) {
235 m_nlsr.getConfParameter().setSiteName(siteName);
236 }
237 else {
238 cerr << "Site can not be null or empty or in bad URI format:( !" << endl;
239 return false;
240 }
241 ndn::Name routerName(router);
242 if (!routerName.empty()) {
243 m_nlsr.getConfParameter().setRouterName(routerName);
244 }
245 else {
246 cerr << " Router name can not be null or empty or in bad URI format:( !" << endl;
247 return false;
248 }
249 }
250 catch (const std::exception& ex) {
251 cerr << ex.what() << endl;
252 return false;
253 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700254
akmhoque157b0a42014-05-13 00:26:37 -0500255 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700256 int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time");
akmhoque157b0a42014-05-13 00:26:37 -0500257 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN &&
258 lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
259 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
260 }
261 else {
262 std::cerr << "Wrong value for lsa-refresh-time ";
263 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
264 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
265 return false;
266 }
267 }
268 catch (const std::exception& ex) {
269 std::cerr << ex.what() << std::endl;
270 return false;
271 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700272
akmhoque157b0a42014-05-13 00:26:37 -0500273 try {
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700274 int32_t routerDeadInterval = section.get<int32_t>("router-dead-interval");
275
276 if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
277 m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
278 }
279 else {
280 std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time"
281 << std::endl;
282 return false;
283 }
284 }
285 catch (const std::exception& ex) {
286 std::cerr << ex.what() << std::endl;
287 // non-critical error. default value is 2 * lsa-refresh-time
288 }
289
290 try {
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700291 int lifetime = section.get<int>("lsa-interest-lifetime");
292 if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) {
293 m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
294 }
295 else {
296 std::cerr << "Wrong value for lsa-interest-timeout. "
297 << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
298 << LSA_INTEREST_LIFETIME_MAX << std::endl;
299 return false;
300 }
301 }
302 catch (const std::exception& ex) {
303 std::cerr << ex.what() << std::endl;
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700304 // non-critical error. default value is 4
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700305 }
306
307 try {
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500308
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700309 std::string logLevel = section.get<string>("log-level");
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500310
311 if (isValidLogLevel(logLevel)) {
akmhoque157b0a42014-05-13 00:26:37 -0500312 m_nlsr.getConfParameter().setLogLevel(logLevel);
313 }
314 else {
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500315 std::cerr << "Invalid value for log-level ";
316 std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500317 return false;
318 }
319 }
320 catch (const std::exception& ex) {
321 std::cerr << ex.what() << std::endl;
322 return false;
323 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700324
akmhoque674b0b12014-05-20 14:33:28 -0500325 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700326 std::string logDir = section.get<string>("log-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500327 if (boost::filesystem::exists(logDir)) {
328 if (boost::filesystem::is_directory(logDir)) {
329 std::string testFileName=logDir+"/test.log";
330 ofstream testOutFile;
331 testOutFile.open(testFileName.c_str());
332 if (testOutFile.is_open() && testOutFile.good()) {
333 m_nlsr.getConfParameter().setLogDir(logDir);
334 }
335 else {
336 std::cerr << "User does not have read and write permission on the directory";
337 std::cerr << std::endl;
338 return false;
339 }
340 testOutFile.close();
341 remove(testFileName.c_str());
342 }
343 else {
344 std::cerr << "Provided path is not a directory" << std::endl;
345 return false;
346 }
347 }
348 else {
349 std::cerr << "Log directory provided does not exists" << std::endl;
350 return false;
351 }
352 }
353 catch (const std::exception& ex) {
354 std::cerr << "You must configure log directory" << std::endl;
355 std::cerr << ex.what() << std::endl;
356 return false;
357 }
358 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700359 std::string seqDir = section.get<string>("seq-dir");
akmhoque674b0b12014-05-20 14:33:28 -0500360 if (boost::filesystem::exists(seqDir)) {
361 if (boost::filesystem::is_directory(seqDir)) {
362 std::string testFileName=seqDir+"/test.seq";
363 ofstream testOutFile;
364 testOutFile.open(testFileName.c_str());
365 if (testOutFile.is_open() && testOutFile.good()) {
366 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
367 }
368 else {
369 std::cerr << "User does not have read and write permission on the directory";
370 std::cerr << std::endl;
371 return false;
372 }
373 testOutFile.close();
374 remove(testFileName.c_str());
375 }
376 else {
377 std::cerr << "Provided path is not a directory" << std::endl;
378 return false;
379 }
380 }
381 else {
382 std::cerr << "Seq directory provided does not exists" << std::endl;
383 return false;
384 }
385 }
386 catch (const std::exception& ex) {
387 std::cerr << "You must configure sequence directory" << std::endl;
388 std::cerr << ex.what() << std::endl;
389 return false;
390 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700391
akmhoque157b0a42014-05-13 00:26:37 -0500392 return true;
393}
394
395bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700396ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section)
akmhoque157b0a42014-05-13 00:26:37 -0500397{
398 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700399 int retrials = section.get<int>("hello-retries");
akmhoque157b0a42014-05-13 00:26:37 -0500400 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
401 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
402 }
403 else {
404 std::cerr << "Wrong value for hello-retries. ";
405 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
406 std::cerr << HELLO_RETRIES_MAX << std::endl;
407 return false;
408 }
409 }
410 catch (const std::exception& ex) {
411 std::cerr << ex.what() << std::endl;
412 return false;
413 }
414 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700415 int timeOut = section.get<int>("hello-timeout");
akmhoque157b0a42014-05-13 00:26:37 -0500416 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
417 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
418 }
419 else {
420 std::cerr << "Wrong value for hello-timeout. ";
421 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
422 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
423 return false;
424 }
425 }
426 catch (const std::exception& ex) {
427 std::cerr << ex.what() << std::endl;
428 }
429 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700430 int interval = section.get<int>("hello-interval");
akmhoque157b0a42014-05-13 00:26:37 -0500431 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
432 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
433 }
434 else {
435 std::cerr << "Wrong value for hello-interval. ";
436 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
437 std::cerr << HELLO_INTERVAL_MAX << std::endl;
438 return false;
439 }
440 }
441 catch (const std::exception& ex) {
442 std::cerr << ex.what() << std::endl;
443 }
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");
477 double linkCost = CommandAttriTree.get<double>("link-cost",
478 Adjacent::DEFAULT_LINK_COST);
479 ndn::Name neighborName(name);
480 if (!neighborName.empty()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500481 Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500482 m_nlsr.getAdjacencyList().insert(adj);
483 }
484 else {
akmhoque674b0b12014-05-20 14:33:28 -0500485 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500486 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500487 }
488 }
akmhoque157b0a42014-05-13 00:26:37 -0500489 catch (const std::exception& ex) {
490 std::cerr << ex.what() << std::endl;
491 return false;
492 }
akmhoque53353462014-04-22 08:43:45 -0500493 }
494 }
akmhoque157b0a42014-05-13 00:26:37 -0500495 return true;
akmhoque53353462014-04-22 08:43:45 -0500496}
497
akmhoque157b0a42014-05-13 00:26:37 -0500498bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700499ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500500{
akmhoque157b0a42014-05-13 00:26:37 -0500501 std::string state;
502 try {
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700503 state= section.get<string>("state","off");
akmhoque157b0a42014-05-13 00:26:37 -0500504 if (boost::iequals(state, "off")) {
505 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
506 }
507 else if (boost::iequals(state, "on")) {
508 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
509 }
510 else if (state == "dry-run") {
511 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
512 }
513 else {
514 std::cerr << "Wrong format for hyperbolic state." << std::endl;
515 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
516 return false;
517 }
akmhoque53353462014-04-22 08:43:45 -0500518 }
akmhoque157b0a42014-05-13 00:26:37 -0500519 catch (const std::exception& ex) {
520 std::cerr << ex.what() << std::endl;
521 return false;
akmhoque53353462014-04-22 08:43:45 -0500522 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700523
akmhoque157b0a42014-05-13 00:26:37 -0500524 try {
525 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
526 * Even if router can have hyperbolic routing calculation off but other router
527 * in the network may use hyperbolic routing calculation for FIB generation.
528 * So each router need to advertise its hyperbolic coordinates in the network
529 */
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700530 double radius = section.get<double>("radius");
531 double angle = section.get<double>("angle");
akmhoque157b0a42014-05-13 00:26:37 -0500532 if (!m_nlsr.getConfParameter().setCorR(radius)) {
533 return false;
534 }
535 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500536 }
akmhoque157b0a42014-05-13 00:26:37 -0500537 catch (const std::exception& ex) {
538 std::cerr << ex.what() << std::endl;
539 if (state == "on" || state == "dry-run") {
540 return false;
541 }
akmhoque53353462014-04-22 08:43:45 -0500542 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700543
akmhoque157b0a42014-05-13 00:26:37 -0500544 return true;
akmhoque53353462014-04-22 08:43:45 -0500545}
546
akmhoque157b0a42014-05-13 00:26:37 -0500547bool
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700548ConfFileProcessor::processConfSectionFib(const ConfigSection& section)
akmhoque53353462014-04-22 08:43:45 -0500549{
akmhoque157b0a42014-05-13 00:26:37 -0500550 try {
551 int maxFacesPerPrefixNumber =
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700552 section.get<int>("max-faces-per-prefix");
akmhoque157b0a42014-05-13 00:26:37 -0500553 if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN &&
554 maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX)
akmhoque53353462014-04-22 08:43:45 -0500555 {
akmhoque157b0a42014-05-13 00:26:37 -0500556 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber);
akmhoque53353462014-04-22 08:43:45 -0500557 }
akmhoque157b0a42014-05-13 00:26:37 -0500558 else {
559 std::cerr << "Wrong value for max-faces-per-prefix. ";
akmhoque157b0a42014-05-13 00:26:37 -0500560 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
561 return false;
akmhoque53353462014-04-22 08:43:45 -0500562 }
akmhoque53353462014-04-22 08:43:45 -0500563 }
akmhoque157b0a42014-05-13 00:26:37 -0500564 catch (const std::exception& ex) {
565 cerr << ex.what() << endl;
566 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
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700649} // namespace nlsr