akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- 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 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | #include <iostream> |
| 25 | #include <fstream> |
Alexander Afanasyev | b669f9c | 2014-11-14 12:41:54 -0800 | [diff] [blame] | 26 | |
| 27 | #include <ndn-cxx/name.hpp> |
alvy | 2fe1287 | 2014-11-25 10:32:23 -0600 | [diff] [blame] | 28 | #include <ndn-cxx/util/face-uri.hpp> |
Alexander Afanasyev | b669f9c | 2014-11-14 12:41:54 -0800 | [diff] [blame] | 29 | |
| 30 | // boost needs to be included after ndn-cxx, otherwise there will be conflict with _1, _2, ... |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 31 | #include <boost/algorithm/string.hpp> |
| 32 | #include <boost/property_tree/info_parser.hpp> |
| 33 | #include <boost/property_tree/ptree.hpp> |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | #include <boost/filesystem.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | #include "conf-parameter.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 37 | #include "conf-file-processor.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | #include "adjacent.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 39 | #include "utility/name-helper.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | namespace nlsr { |
| 42 | |
| 43 | using namespace std; |
| 44 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 45 | template <class T> |
| 46 | class ConfigurationVariable |
| 47 | { |
| 48 | public: |
| 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 | |
| 106 | private: |
| 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 | |
| 132 | private: |
| 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 144 | bool |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 145 | ConfFileProcessor::processConfFile() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 146 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 147 | 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; |
akmhoque | ad5fe95 | 2014-06-26 13:34:12 -0500 | [diff] [blame] | 154 | return false; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 155 | } |
| 156 | ret = load(inputFile); |
| 157 | inputFile.close(); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | bool |
| 162 | ConfFileProcessor::load(istream& input) |
| 163 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 164 | ConfigSection pt; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 165 | 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 175 | |
| 176 | for (ConfigSection::const_iterator tn = pt.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 177 | tn != pt.end(); ++tn) { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 178 | ret = processSection(tn->first, tn->second); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 179 | if (ret == false) { |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 187 | ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 188 | { |
| 189 | bool ret = true; |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 190 | if (sectionName == "general") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 192 | ret = processConfSectionGeneral(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 193 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 194 | else if (sectionName == "neighbors") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 195 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 196 | ret = processConfSectionNeighbors(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 197 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 198 | else if (sectionName == "hyperbolic") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 199 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 200 | ret = processConfSectionHyperbolic(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 201 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 202 | else if (sectionName == "fib") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 203 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 204 | ret = processConfSectionFib(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 205 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 206 | else if (sectionName == "advertising") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 208 | ret = processConfSectionAdvertising(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 209 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 210 | else if (sectionName == "security") |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 211 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 212 | ret = processConfSectionSecurity(section); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 213 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 214 | else |
| 215 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 216 | std::cerr << "Wrong configuration section: " << sectionName << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 217 | } |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 222 | ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 223 | { |
| 224 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 225 | std::string network = section.get<string>("network"); |
| 226 | std::string site = section.get<string>("site"); |
| 227 | std::string router = section.get<string>("router"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 228 | 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 Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 257 | |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 258 | // 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); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 263 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 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 | |
| 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 294 | return false; |
| 295 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 296 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 297 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 298 | std::string logLevel = section.get<string>("log-level"); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 299 | |
| 300 | if (isValidLogLevel(logLevel)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 301 | m_nlsr.getConfParameter().setLogLevel(logLevel); |
| 302 | } |
| 303 | else { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 304 | std::cerr << "Invalid value for log-level "; |
| 305 | std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | } |
| 309 | catch (const std::exception& ex) { |
| 310 | std::cerr << ex.what() << std::endl; |
| 311 | return false; |
| 312 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 313 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 314 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 315 | std::string logDir = section.get<string>("log-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 316 | 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 Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 338 | std::cerr << "Provided log directory <" << logDir << "> does not exist" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 339 | 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 Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 347 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 348 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 349 | std::string seqDir = section.get<string>("seq-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 350 | 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 Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 372 | std::cerr << "Provided sequence directory <" << seqDir << "> does not exist" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 373 | 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 Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 381 | |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 382 | 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | |
| 407 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 408 | ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 409 | { |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 410 | // 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); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 415 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 416 | 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 421 | return false; |
| 422 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 423 | |
| 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); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 429 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 430 | 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; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 436 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 437 | |
| 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); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 443 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 444 | 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; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 450 | } |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 451 | |
| 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 475 | for (ConfigSection::const_iterator tn = |
| 476 | section.begin(); tn != section.end(); ++tn) { |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 477 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 478 | if (tn->first == "neighbor") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 479 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 480 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 481 | ConfigSection CommandAttriTree = tn->second; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 482 | std::string name = CommandAttriTree.get<std::string>("name"); |
| 483 | std::string faceUri = CommandAttriTree.get<std::string>("face-uri"); |
alvy | 2fe1287 | 2014-11-25 10:32:23 -0600 | [diff] [blame] | 484 | |
| 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 492 | double linkCost = CommandAttriTree.get<double>("link-cost", |
| 493 | Adjacent::DEFAULT_LINK_COST); |
| 494 | ndn::Name neighborName(name); |
| 495 | if (!neighborName.empty()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 496 | Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 497 | m_nlsr.getAdjacencyList().insert(adj); |
| 498 | } |
| 499 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 500 | std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]"; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 501 | std::cerr << " or bad URI format" << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 502 | } |
| 503 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 504 | catch (const std::exception& ex) { |
| 505 | std::cerr << ex.what() << std::endl; |
| 506 | return false; |
| 507 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 508 | } |
| 509 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 510 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 511 | } |
| 512 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 513 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 514 | ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 515 | { |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 516 | // 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); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 521 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 522 | 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 532 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 533 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 534 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 535 | 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 541 | double radius = section.get<double>("radius"); |
| 542 | double angle = section.get<double>("angle"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 543 | if (!m_nlsr.getConfParameter().setCorR(radius)) { |
| 544 | return false; |
| 545 | } |
| 546 | m_nlsr.getConfParameter().setCorTheta(angle); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 547 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 548 | catch (const std::exception& ex) { |
| 549 | std::cerr << ex.what() << std::endl; |
| 550 | if (state == "on" || state == "dry-run") { |
| 551 | return false; |
| 552 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 553 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 554 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 555 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 556 | } |
| 557 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 558 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 559 | ConfFileProcessor::processConfSectionFib(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 560 | { |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 561 | // 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); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 568 | } |
alvy | a2228c6 | 2014-12-09 10:25:11 -0600 | [diff] [blame^] | 569 | else { |
| 570 | std::cerr << "Wrong value for max-faces-per-prefix. "; |
| 571 | std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl; |
| 572 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 573 | return false; |
| 574 | } |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 575 | |
| 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 587 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 588 | } |
| 589 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 590 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 591 | ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 592 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 593 | for (ConfigSection::const_iterator tn = |
| 594 | section.begin(); tn != section.end(); ++tn) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 595 | 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 { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 603 | std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | } |
| 607 | catch (const std::exception& ex) { |
| 608 | std::cerr << ex.what() << std::endl; |
| 609 | return false; |
| 610 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 611 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 612 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 613 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 614 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 615 | |
| 616 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 617 | ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 618 | { |
| 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); |
akmhoque | d57f367 | 2014-06-10 10:41:32 -0500 | [diff] [blame] | 628 | it++; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 629 | |
| 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 | |
alvy | 2fe1287 | 2014-11-25 10:32:23 -0600 | [diff] [blame] | 656 | } // namespace nlsr |