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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 258 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 259 | int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 260 | if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && |
| 261 | lsaRefreshTime <= LSA_REFRESH_TIME_MAX) { |
| 262 | m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime); |
| 263 | } |
| 264 | else { |
| 265 | std::cerr << "Wrong value for lsa-refresh-time "; |
| 266 | std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";; |
| 267 | std::cerr << LSA_REFRESH_TIME_MAX << std::endl; |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | catch (const std::exception& ex) { |
| 272 | std::cerr << ex.what() << std::endl; |
| 273 | return false; |
| 274 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 275 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 276 | try { |
Alexander Afanasyev | 1cf1e10 | 2014-08-17 19:47:57 -0700 | [diff] [blame] | 277 | int32_t routerDeadInterval = section.get<int32_t>("router-dead-interval"); |
| 278 | |
| 279 | if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) { |
| 280 | m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval); |
| 281 | } |
| 282 | else { |
| 283 | std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" |
| 284 | << std::endl; |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | catch (const std::exception& ex) { |
| 289 | std::cerr << ex.what() << std::endl; |
| 290 | // non-critical error. default value is 2 * lsa-refresh-time |
| 291 | } |
| 292 | |
| 293 | try { |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 294 | int lifetime = section.get<int>("lsa-interest-lifetime"); |
| 295 | if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) { |
| 296 | m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime)); |
| 297 | } |
| 298 | else { |
| 299 | std::cerr << "Wrong value for lsa-interest-timeout. " |
| 300 | << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-" |
| 301 | << LSA_INTEREST_LIFETIME_MAX << std::endl; |
| 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | catch (const std::exception& ex) { |
| 306 | std::cerr << ex.what() << std::endl; |
Alexander Afanasyev | 1cf1e10 | 2014-08-17 19:47:57 -0700 | [diff] [blame] | 307 | // non-critical error. default value is 4 |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | try { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 311 | |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 312 | std::string logLevel = section.get<string>("log-level"); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 313 | |
| 314 | if (isValidLogLevel(logLevel)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 315 | m_nlsr.getConfParameter().setLogLevel(logLevel); |
| 316 | } |
| 317 | else { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 318 | std::cerr << "Invalid value for log-level "; |
| 319 | std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | catch (const std::exception& ex) { |
| 324 | std::cerr << ex.what() << std::endl; |
| 325 | return false; |
| 326 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 327 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 328 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 329 | std::string logDir = section.get<string>("log-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 330 | if (boost::filesystem::exists(logDir)) { |
| 331 | if (boost::filesystem::is_directory(logDir)) { |
| 332 | std::string testFileName=logDir+"/test.log"; |
| 333 | ofstream testOutFile; |
| 334 | testOutFile.open(testFileName.c_str()); |
| 335 | if (testOutFile.is_open() && testOutFile.good()) { |
| 336 | m_nlsr.getConfParameter().setLogDir(logDir); |
| 337 | } |
| 338 | else { |
| 339 | std::cerr << "User does not have read and write permission on the directory"; |
| 340 | std::cerr << std::endl; |
| 341 | return false; |
| 342 | } |
| 343 | testOutFile.close(); |
| 344 | remove(testFileName.c_str()); |
| 345 | } |
| 346 | else { |
| 347 | std::cerr << "Provided path is not a directory" << std::endl; |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | else { |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 352 | std::cerr << "Provided log directory <" << logDir << "> does not exist" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | catch (const std::exception& ex) { |
| 357 | std::cerr << "You must configure log directory" << std::endl; |
| 358 | std::cerr << ex.what() << std::endl; |
| 359 | return false; |
| 360 | } |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 361 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 362 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 363 | std::string seqDir = section.get<string>("seq-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 364 | if (boost::filesystem::exists(seqDir)) { |
| 365 | if (boost::filesystem::is_directory(seqDir)) { |
| 366 | std::string testFileName=seqDir+"/test.seq"; |
| 367 | ofstream testOutFile; |
| 368 | testOutFile.open(testFileName.c_str()); |
| 369 | if (testOutFile.is_open() && testOutFile.good()) { |
| 370 | m_nlsr.getConfParameter().setSeqFileDir(seqDir); |
| 371 | } |
| 372 | else { |
| 373 | std::cerr << "User does not have read and write permission on the directory"; |
| 374 | std::cerr << std::endl; |
| 375 | return false; |
| 376 | } |
| 377 | testOutFile.close(); |
| 378 | remove(testFileName.c_str()); |
| 379 | } |
| 380 | else { |
| 381 | std::cerr << "Provided path is not a directory" << std::endl; |
| 382 | return false; |
| 383 | } |
| 384 | } |
| 385 | else { |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 386 | std::cerr << "Provided sequence directory <" << seqDir << "> does not exist" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | } |
| 390 | catch (const std::exception& ex) { |
| 391 | std::cerr << "You must configure sequence directory" << std::endl; |
| 392 | std::cerr << ex.what() << std::endl; |
| 393 | return false; |
| 394 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 395 | |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 396 | try { |
| 397 | std::string log4cxxPath = section.get<string>("log4cxx-conf"); |
| 398 | |
| 399 | if (log4cxxPath == "") { |
| 400 | std::cerr << "No value provided for log4cxx-conf" << std::endl; |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | if (boost::filesystem::exists(log4cxxPath)) { |
| 405 | m_nlsr.getConfParameter().setLog4CxxConfPath(log4cxxPath); |
| 406 | } |
| 407 | else { |
| 408 | std::cerr << "Provided path for log4cxx-conf <" << log4cxxPath |
| 409 | << "> does not exist" << std::endl; |
| 410 | |
| 411 | return false; |
| 412 | } |
| 413 | } |
| 414 | catch (const std::exception& ex) { |
| 415 | // Variable is optional so default configuration will be used; continue processing file |
| 416 | } |
| 417 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 418 | return true; |
| 419 | } |
| 420 | |
| 421 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 422 | ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 423 | { |
| 424 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 425 | int retrials = section.get<int>("hello-retries"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 426 | if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) { |
| 427 | m_nlsr.getConfParameter().setInterestRetryNumber(retrials); |
| 428 | } |
| 429 | else { |
| 430 | std::cerr << "Wrong value for hello-retries. "; |
| 431 | std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-"; |
| 432 | std::cerr << HELLO_RETRIES_MAX << std::endl; |
| 433 | return false; |
| 434 | } |
| 435 | } |
| 436 | catch (const std::exception& ex) { |
| 437 | std::cerr << ex.what() << std::endl; |
| 438 | return false; |
| 439 | } |
| 440 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 441 | int timeOut = section.get<int>("hello-timeout"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 442 | if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) { |
| 443 | m_nlsr.getConfParameter().setInterestResendTime(timeOut); |
| 444 | } |
| 445 | else { |
| 446 | std::cerr << "Wrong value for hello-timeout. "; |
| 447 | std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-"; |
| 448 | std::cerr << HELLO_TIMEOUT_MAX << std::endl; |
| 449 | return false; |
| 450 | } |
| 451 | } |
| 452 | catch (const std::exception& ex) { |
| 453 | std::cerr << ex.what() << std::endl; |
| 454 | } |
| 455 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 456 | int interval = section.get<int>("hello-interval"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 457 | if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) { |
| 458 | m_nlsr.getConfParameter().setInfoInterestInterval(interval); |
| 459 | } |
| 460 | else { |
| 461 | std::cerr << "Wrong value for hello-interval. "; |
| 462 | std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-"; |
| 463 | std::cerr << HELLO_INTERVAL_MAX << std::endl; |
| 464 | return false; |
| 465 | } |
| 466 | } |
| 467 | catch (const std::exception& ex) { |
| 468 | std::cerr << ex.what() << std::endl; |
| 469 | } |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 470 | |
| 471 | // Event intervals |
| 472 | // adj-lsa-build-interval |
| 473 | ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval", |
| 474 | bind(&ConfParameter::setAdjLsaBuildInterval, |
| 475 | &m_nlsr.getConfParameter(), _1)); |
| 476 | adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX); |
| 477 | adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT); |
| 478 | |
| 479 | if (!adjLsaBuildInterval.parseFromConfigSection(section)) { |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | // first-hello-interval |
| 484 | ConfigurationVariable<uint32_t> firstHelloInterval("first-hello-interval", |
| 485 | bind(&ConfParameter::setFirstHelloInterval, |
| 486 | &m_nlsr.getConfParameter(), _1)); |
| 487 | firstHelloInterval.setMinAndMaxValue(FIRST_HELLO_INTERVAL_MIN, FIRST_HELLO_INTERVAL_MAX); |
| 488 | firstHelloInterval.setOptional(FIRST_HELLO_INTERVAL_DEFAULT); |
| 489 | |
| 490 | if (!firstHelloInterval.parseFromConfigSection(section)) { |
| 491 | return false; |
| 492 | } |
| 493 | |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 494 | for (ConfigSection::const_iterator tn = |
| 495 | section.begin(); tn != section.end(); ++tn) { |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 496 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 497 | if (tn->first == "neighbor") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 498 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 499 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 500 | ConfigSection CommandAttriTree = tn->second; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 501 | std::string name = CommandAttriTree.get<std::string>("name"); |
| 502 | std::string faceUri = CommandAttriTree.get<std::string>("face-uri"); |
alvy | 2fe1287 | 2014-11-25 10:32:23 -0600 | [diff] [blame^] | 503 | |
| 504 | ndn::util::FaceUri uri; |
| 505 | |
| 506 | if (!uri.parse(faceUri)) { |
| 507 | std::cerr << "Malformed face-uri <" << faceUri << "> for " << name << std::endl; |
| 508 | return false; |
| 509 | } |
| 510 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 511 | double linkCost = CommandAttriTree.get<double>("link-cost", |
| 512 | Adjacent::DEFAULT_LINK_COST); |
| 513 | ndn::Name neighborName(name); |
| 514 | if (!neighborName.empty()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 515 | Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 516 | m_nlsr.getAdjacencyList().insert(adj); |
| 517 | } |
| 518 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 519 | std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]"; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 520 | std::cerr << " or bad URI format" << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 521 | } |
| 522 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 523 | catch (const std::exception& ex) { |
| 524 | std::cerr << ex.what() << std::endl; |
| 525 | return false; |
| 526 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 527 | } |
| 528 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 529 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 530 | } |
| 531 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 532 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 533 | ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 534 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 535 | std::string state; |
| 536 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 537 | state= section.get<string>("state","off"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 538 | if (boost::iequals(state, "off")) { |
| 539 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF); |
| 540 | } |
| 541 | else if (boost::iequals(state, "on")) { |
| 542 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON); |
| 543 | } |
| 544 | else if (state == "dry-run") { |
| 545 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN); |
| 546 | } |
| 547 | else { |
| 548 | std::cerr << "Wrong format for hyperbolic state." << std::endl; |
| 549 | std::cerr << "Allowed value: off, on, dry-run" << std::endl; |
| 550 | return false; |
| 551 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 552 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 553 | catch (const std::exception& ex) { |
| 554 | std::cerr << ex.what() << std::endl; |
| 555 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 556 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 557 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 558 | try { |
| 559 | /* Radius and angle is mandatory configuration parameter in hyperbolic section. |
| 560 | * Even if router can have hyperbolic routing calculation off but other router |
| 561 | * in the network may use hyperbolic routing calculation for FIB generation. |
| 562 | * So each router need to advertise its hyperbolic coordinates in the network |
| 563 | */ |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 564 | double radius = section.get<double>("radius"); |
| 565 | double angle = section.get<double>("angle"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 566 | if (!m_nlsr.getConfParameter().setCorR(radius)) { |
| 567 | return false; |
| 568 | } |
| 569 | m_nlsr.getConfParameter().setCorTheta(angle); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 570 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 571 | catch (const std::exception& ex) { |
| 572 | std::cerr << ex.what() << std::endl; |
| 573 | if (state == "on" || state == "dry-run") { |
| 574 | return false; |
| 575 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 576 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 577 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 578 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 579 | } |
| 580 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 581 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 582 | ConfFileProcessor::processConfSectionFib(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 583 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 584 | try { |
| 585 | int maxFacesPerPrefixNumber = |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 586 | section.get<int>("max-faces-per-prefix"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 587 | if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN && |
| 588 | maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 589 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 590 | m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 591 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 592 | else { |
| 593 | std::cerr << "Wrong value for max-faces-per-prefix. "; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 594 | std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl; |
| 595 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 596 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 597 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 598 | catch (const std::exception& ex) { |
| 599 | cerr << ex.what() << endl; |
| 600 | return false; |
| 601 | } |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 602 | |
| 603 | // routing-calc-interval |
| 604 | ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval", |
| 605 | bind(&ConfParameter::setRoutingCalcInterval, |
| 606 | &m_nlsr.getConfParameter(), _1)); |
| 607 | routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX); |
| 608 | routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT); |
| 609 | |
| 610 | if (!routingCalcInterval.parseFromConfigSection(section)) { |
| 611 | return false; |
| 612 | } |
| 613 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 614 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 615 | } |
| 616 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 617 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 618 | ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 619 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 620 | for (ConfigSection::const_iterator tn = |
| 621 | section.begin(); tn != section.end(); ++tn) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 622 | if (tn->first == "prefix") { |
| 623 | try { |
| 624 | std::string prefix = tn->second.data(); |
| 625 | ndn::Name namePrefix(prefix); |
| 626 | if (!namePrefix.empty()) { |
| 627 | m_nlsr.getNamePrefixList().insert(namePrefix); |
| 628 | } |
| 629 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 630 | std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | } |
| 634 | catch (const std::exception& ex) { |
| 635 | std::cerr << ex.what() << std::endl; |
| 636 | return false; |
| 637 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 638 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 639 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 640 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 641 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 642 | |
| 643 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 644 | ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 645 | { |
| 646 | ConfigSection::const_iterator it = section.begin(); |
| 647 | |
| 648 | if (it == section.end() || it->first != "validator") |
| 649 | { |
| 650 | std::cerr << "Error: Expect validator section!" << std::endl; |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | m_nlsr.loadValidator(it->second, m_confFileName); |
akmhoque | d57f367 | 2014-06-10 10:41:32 -0500 | [diff] [blame] | 655 | it++; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 656 | |
| 657 | for (; it != section.end(); it++) |
| 658 | { |
| 659 | using namespace boost::filesystem; |
| 660 | if (it->first != "cert-to-publish") |
| 661 | { |
| 662 | std::cerr << "Error: Expect cert-to-publish!" << std::endl; |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | std::string file = it->second.data(); |
| 667 | path certfilePath = absolute(file, path(m_confFileName).parent_path()); |
| 668 | ndn::shared_ptr<ndn::IdentityCertificate> idCert = |
| 669 | ndn::io::load<ndn::IdentityCertificate>(certfilePath.string()); |
| 670 | |
| 671 | if (!static_cast<bool>(idCert)) |
| 672 | { |
| 673 | std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl; |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | m_nlsr.loadCertToPublish(idCert); |
| 678 | } |
| 679 | |
| 680 | return true; |
| 681 | } |
| 682 | |
alvy | 2fe1287 | 2014-11-25 10:32:23 -0600 | [diff] [blame^] | 683 | } // namespace nlsr |