blob: 20a6a6827369c33f4c60291d5de88f56df4a1f4a [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
38
39namespace nlsr {
40
41using namespace std;
42
akmhoque157b0a42014-05-13 00:26:37 -050043bool
akmhoqueb6450b12014-04-24 00:01:03 -050044ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -050045{
akmhoque157b0a42014-05-13 00:26:37 -050046 bool ret = true;
47 ifstream inputFile;
48 inputFile.open(m_confFileName.c_str());
49 if (!inputFile.is_open()) {
50 string msg = "Failed to read configuration file: ";
51 msg += m_confFileName;
52 cerr << msg << endl;
53 ret = false;
54 }
55 ret = load(inputFile);
56 inputFile.close();
57 return ret;
58}
59
60bool
61ConfFileProcessor::load(istream& input)
62{
63 boost::property_tree::ptree pt;
64 bool ret = true;
65 try {
66 boost::property_tree::read_info(input, pt);
67 }
68 catch (const boost::property_tree::info_parser_error& error) {
69 stringstream msg;
70 std::cerr << "Failed to parse configuration file " << std::endl;
71 std::cerr << m_confFileName << std::endl;
72 return false;
73 }
74 for (boost::property_tree::ptree::const_iterator tn = pt.begin();
75 tn != pt.end(); ++tn) {
76 std::string section = tn->first;
77 boost::property_tree::ptree SectionAttributeTree = tn ->second;
78 ret = processSection(section, SectionAttributeTree);
79 if (ret == false) {
80 break;
81 }
82 }
83 return ret;
84}
85
86bool
87ConfFileProcessor::processSection(const std::string& section,
88 boost::property_tree::ptree SectionAttributeTree)
89{
90 bool ret = true;
91 if (section == "general")
akmhoque53353462014-04-22 08:43:45 -050092 {
akmhoque157b0a42014-05-13 00:26:37 -050093 ret = processConfSectionGeneral(SectionAttributeTree);
94 }
95 else if (section == "neighbors")
96 {
97 ret = processConfSectionNeighbors(SectionAttributeTree);
98 }
99 else if (section == "hyperbolic")
100 {
101 ret = processConfSectionHyperbolic(SectionAttributeTree);
102 }
103 else if (section == "fib")
104 {
105 ret = processConfSectionFib(SectionAttributeTree);
106 }
107 else if (section == "advertising")
108 {
109 ret = processConfSectionAdvertising(SectionAttributeTree);
110 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700111 else if (section == "security")
112 {
113 ret = processConfSectionSecurity(SectionAttributeTree);
114 }
akmhoque157b0a42014-05-13 00:26:37 -0500115 else
116 {
117 std::cerr << "Wrong configuration Command: " << section << std::endl;
118 }
119 return ret;
120}
121
122bool
123ConfFileProcessor::processConfSectionGeneral(boost::property_tree::ptree
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700124 SectionAttributeTree)
akmhoque157b0a42014-05-13 00:26:37 -0500125{
126 try {
127 std::string network = SectionAttributeTree.get<string>("network");
128 std::string site = SectionAttributeTree.get<string>("site");
129 std::string router = SectionAttributeTree.get<string>("router");
130 ndn::Name networkName(network);
131 if (!networkName.empty()) {
132 m_nlsr.getConfParameter().setNetwork(networkName);
133 }
134 else {
135 cerr << " Network can not be null or empty or in bad URI format :(!" << endl;
136 return false;
137 }
138 ndn::Name siteName(site);
139 if (!siteName.empty()) {
140 m_nlsr.getConfParameter().setSiteName(siteName);
141 }
142 else {
143 cerr << "Site can not be null or empty or in bad URI format:( !" << endl;
144 return false;
145 }
146 ndn::Name routerName(router);
147 if (!routerName.empty()) {
148 m_nlsr.getConfParameter().setRouterName(routerName);
149 }
150 else {
151 cerr << " Router name can not be null or empty or in bad URI format:( !" << endl;
152 return false;
153 }
154 }
155 catch (const std::exception& ex) {
156 cerr << ex.what() << endl;
157 return false;
158 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700159
akmhoque157b0a42014-05-13 00:26:37 -0500160 try {
161 int32_t lsaRefreshTime = SectionAttributeTree.get<int32_t>("lsa-refresh-time");
162 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN &&
163 lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
164 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
165 }
166 else {
167 std::cerr << "Wrong value for lsa-refresh-time ";
168 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
169 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
170 return false;
171 }
172 }
173 catch (const std::exception& ex) {
174 std::cerr << ex.what() << std::endl;
175 return false;
176 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700177
akmhoque157b0a42014-05-13 00:26:37 -0500178 try {
179 std::string logLevel = SectionAttributeTree.get<string>("log-level");
180 if ( boost::iequals(logLevel, "info") || boost::iequals(logLevel, "debug")) {
181 m_nlsr.getConfParameter().setLogLevel(logLevel);
182 }
183 else {
184 std::cerr << "Wrong value for log-level ";
185 std::cerr << "Allowed value: INFO, DEBUG" << std::endl;
186 return false;
187 }
188 }
189 catch (const std::exception& ex) {
190 std::cerr << ex.what() << std::endl;
191 return false;
192 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700193
akmhoque674b0b12014-05-20 14:33:28 -0500194 try {
195 std::string logDir = SectionAttributeTree.get<string>("log-dir");
196 if (boost::filesystem::exists(logDir)) {
197 if (boost::filesystem::is_directory(logDir)) {
198 std::string testFileName=logDir+"/test.log";
199 ofstream testOutFile;
200 testOutFile.open(testFileName.c_str());
201 if (testOutFile.is_open() && testOutFile.good()) {
202 m_nlsr.getConfParameter().setLogDir(logDir);
203 }
204 else {
205 std::cerr << "User does not have read and write permission on the directory";
206 std::cerr << std::endl;
207 return false;
208 }
209 testOutFile.close();
210 remove(testFileName.c_str());
211 }
212 else {
213 std::cerr << "Provided path is not a directory" << std::endl;
214 return false;
215 }
216 }
217 else {
218 std::cerr << "Log directory provided does not exists" << std::endl;
219 return false;
220 }
221 }
222 catch (const std::exception& ex) {
223 std::cerr << "You must configure log directory" << std::endl;
224 std::cerr << ex.what() << std::endl;
225 return false;
226 }
227 try {
228 std::string seqDir = SectionAttributeTree.get<string>("seq-dir");
229 if (boost::filesystem::exists(seqDir)) {
230 if (boost::filesystem::is_directory(seqDir)) {
231 std::string testFileName=seqDir+"/test.seq";
232 ofstream testOutFile;
233 testOutFile.open(testFileName.c_str());
234 if (testOutFile.is_open() && testOutFile.good()) {
235 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
236 }
237 else {
238 std::cerr << "User does not have read and write permission on the directory";
239 std::cerr << std::endl;
240 return false;
241 }
242 testOutFile.close();
243 remove(testFileName.c_str());
244 }
245 else {
246 std::cerr << "Provided path is not a directory" << std::endl;
247 return false;
248 }
249 }
250 else {
251 std::cerr << "Seq directory provided does not exists" << std::endl;
252 return false;
253 }
254 }
255 catch (const std::exception& ex) {
256 std::cerr << "You must configure sequence directory" << std::endl;
257 std::cerr << ex.what() << std::endl;
258 return false;
259 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700260
akmhoque157b0a42014-05-13 00:26:37 -0500261 return true;
262}
263
264bool
265ConfFileProcessor::processConfSectionNeighbors(boost::property_tree::ptree
266 SectionAttributeTree)
267{
268 try {
269 int retrials = SectionAttributeTree.get<int>("hello-retries");
270 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
271 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
272 }
273 else {
274 std::cerr << "Wrong value for hello-retries. ";
275 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
276 std::cerr << HELLO_RETRIES_MAX << std::endl;
277 return false;
278 }
279 }
280 catch (const std::exception& ex) {
281 std::cerr << ex.what() << std::endl;
282 return false;
283 }
284 try {
285 int timeOut = SectionAttributeTree.get<int>("hello-timeout");
286 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
287 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
288 }
289 else {
290 std::cerr << "Wrong value for hello-timeout. ";
291 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
292 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
293 return false;
294 }
295 }
296 catch (const std::exception& ex) {
297 std::cerr << ex.what() << std::endl;
298 }
299 try {
300 int interval = SectionAttributeTree.get<int>("hello-interval");
301 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
302 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
303 }
304 else {
305 std::cerr << "Wrong value for hello-interval. ";
306 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
307 std::cerr << HELLO_INTERVAL_MAX << std::endl;
308 return false;
309 }
310 }
311 catch (const std::exception& ex) {
312 std::cerr << ex.what() << std::endl;
313 }
314 for (boost::property_tree::ptree::const_iterator tn =
315 SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) {
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700316
akmhoque157b0a42014-05-13 00:26:37 -0500317 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500318 {
akmhoque157b0a42014-05-13 00:26:37 -0500319 try {
320 boost::property_tree::ptree CommandAttriTree = tn->second;
321 std::string name = CommandAttriTree.get<std::string>("name");
322 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
323 double linkCost = CommandAttriTree.get<double>("link-cost",
324 Adjacent::DEFAULT_LINK_COST);
325 ndn::Name neighborName(name);
326 if (!neighborName.empty()) {
327 Adjacent adj(name, faceUri, linkCost, ADJACENT_STATUS_INACTIVE, 0);
328 m_nlsr.getAdjacencyList().insert(adj);
329 }
330 else {
akmhoque674b0b12014-05-20 14:33:28 -0500331 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500332 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500333 }
334 }
akmhoque157b0a42014-05-13 00:26:37 -0500335 catch (const std::exception& ex) {
336 std::cerr << ex.what() << std::endl;
337 return false;
338 }
akmhoque53353462014-04-22 08:43:45 -0500339 }
340 }
akmhoque157b0a42014-05-13 00:26:37 -0500341 return true;
akmhoque53353462014-04-22 08:43:45 -0500342}
343
akmhoque157b0a42014-05-13 00:26:37 -0500344bool
345ConfFileProcessor::processConfSectionHyperbolic(boost::property_tree::ptree
346 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500347{
akmhoque157b0a42014-05-13 00:26:37 -0500348 std::string state;
349 try {
350 state= SectionAttributeTree.get<string>("state","off");
351 if (boost::iequals(state, "off")) {
352 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
353 }
354 else if (boost::iequals(state, "on")) {
355 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
356 }
357 else if (state == "dry-run") {
358 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
359 }
360 else {
361 std::cerr << "Wrong format for hyperbolic state." << std::endl;
362 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
363 return false;
364 }
akmhoque53353462014-04-22 08:43:45 -0500365 }
akmhoque157b0a42014-05-13 00:26:37 -0500366 catch (const std::exception& ex) {
367 std::cerr << ex.what() << std::endl;
368 return false;
akmhoque53353462014-04-22 08:43:45 -0500369 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700370
akmhoque157b0a42014-05-13 00:26:37 -0500371 try {
372 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
373 * Even if router can have hyperbolic routing calculation off but other router
374 * in the network may use hyperbolic routing calculation for FIB generation.
375 * So each router need to advertise its hyperbolic coordinates in the network
376 */
377 double radius = SectionAttributeTree.get<double>("radius");
378 double angle = SectionAttributeTree.get<double>("angle");
379 if (!m_nlsr.getConfParameter().setCorR(radius)) {
380 return false;
381 }
382 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500383 }
akmhoque157b0a42014-05-13 00:26:37 -0500384 catch (const std::exception& ex) {
385 std::cerr << ex.what() << std::endl;
386 if (state == "on" || state == "dry-run") {
387 return false;
388 }
akmhoque53353462014-04-22 08:43:45 -0500389 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700390
akmhoque157b0a42014-05-13 00:26:37 -0500391 return true;
akmhoque53353462014-04-22 08:43:45 -0500392}
393
akmhoque157b0a42014-05-13 00:26:37 -0500394bool
395ConfFileProcessor::processConfSectionFib(boost::property_tree::ptree
396 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500397{
akmhoque157b0a42014-05-13 00:26:37 -0500398 try {
399 int maxFacesPerPrefixNumber =
400 SectionAttributeTree.get<int>("max-faces-per-prefix");
401 if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN &&
402 maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX)
akmhoque53353462014-04-22 08:43:45 -0500403 {
akmhoque157b0a42014-05-13 00:26:37 -0500404 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber);
akmhoque53353462014-04-22 08:43:45 -0500405 }
akmhoque157b0a42014-05-13 00:26:37 -0500406 else {
407 std::cerr << "Wrong value for max-faces-per-prefix. ";
akmhoque157b0a42014-05-13 00:26:37 -0500408 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
409 return false;
akmhoque53353462014-04-22 08:43:45 -0500410 }
akmhoque53353462014-04-22 08:43:45 -0500411 }
akmhoque157b0a42014-05-13 00:26:37 -0500412 catch (const std::exception& ex) {
413 cerr << ex.what() << endl;
414 return false;
415 }
416 return true;
akmhoque53353462014-04-22 08:43:45 -0500417}
418
akmhoque157b0a42014-05-13 00:26:37 -0500419bool
420ConfFileProcessor::processConfSectionAdvertising(boost::property_tree::ptree
421 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500422{
akmhoque157b0a42014-05-13 00:26:37 -0500423 for (boost::property_tree::ptree::const_iterator tn =
424 SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) {
425 if (tn->first == "prefix") {
426 try {
427 std::string prefix = tn->second.data();
428 ndn::Name namePrefix(prefix);
429 if (!namePrefix.empty()) {
430 m_nlsr.getNamePrefixList().insert(namePrefix);
431 }
432 else {
akmhoque674b0b12014-05-20 14:33:28 -0500433 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500434 return false;
435 }
436 }
437 catch (const std::exception& ex) {
438 std::cerr << ex.what() << std::endl;
439 return false;
440 }
akmhoque53353462014-04-22 08:43:45 -0500441 }
akmhoque53353462014-04-22 08:43:45 -0500442 }
akmhoque157b0a42014-05-13 00:26:37 -0500443 return true;
akmhoque53353462014-04-22 08:43:45 -0500444}
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700445
446bool
447ConfFileProcessor::processConfSectionSecurity(boost::property_tree::ptree section)
448{
449 ConfigSection::const_iterator it = section.begin();
450
451 if (it == section.end() || it->first != "validator")
452 {
453 std::cerr << "Error: Expect validator section!" << std::endl;
454 return false;
455 }
456
457 m_nlsr.loadValidator(it->second, m_confFileName);
458
459 for (; it != section.end(); it++)
460 {
461 using namespace boost::filesystem;
462 if (it->first != "cert-to-publish")
463 {
464 std::cerr << "Error: Expect cert-to-publish!" << std::endl;
465 return false;
466 }
467
468 std::string file = it->second.data();
469 path certfilePath = absolute(file, path(m_confFileName).parent_path());
470 ndn::shared_ptr<ndn::IdentityCertificate> idCert =
471 ndn::io::load<ndn::IdentityCertificate>(certfilePath.string());
472
473 if (!static_cast<bool>(idCert))
474 {
475 std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
476 return false;
477 }
478
479 m_nlsr.loadCertToPublish(idCert);
480 }
481
482 return true;
483}
484
akmhoque157b0a42014-05-13 00:26:37 -0500485}//namespace NLSR