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> |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 26 | #include <boost/algorithm/string.hpp> |
| 27 | #include <boost/property_tree/info_parser.hpp> |
| 28 | #include <boost/property_tree/ptree.hpp> |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 29 | #include <boost/filesystem.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 30 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/name.hpp> |
| 32 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | #include "conf-parameter.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 34 | #include "conf-file-processor.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | #include "adjacent.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 36 | #include "utility/name-helper.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | namespace nlsr { |
| 39 | |
| 40 | using namespace std; |
| 41 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 42 | template <class T> |
| 43 | class ConfigurationVariable |
| 44 | { |
| 45 | public: |
| 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 | |
| 103 | private: |
| 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 | |
| 129 | private: |
| 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 141 | bool |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 142 | ConfFileProcessor::processConfFile() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 144 | 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; |
akmhoque | ad5fe95 | 2014-06-26 13:34:12 -0500 | [diff] [blame] | 151 | return false; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 152 | } |
| 153 | ret = load(inputFile); |
| 154 | inputFile.close(); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | bool |
| 159 | ConfFileProcessor::load(istream& input) |
| 160 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 161 | ConfigSection pt; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 162 | 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 172 | |
| 173 | for (ConfigSection::const_iterator tn = pt.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 174 | tn != pt.end(); ++tn) { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 175 | ret = processSection(tn->first, tn->second); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 176 | if (ret == false) { |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 184 | ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 185 | { |
| 186 | bool ret = true; |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 187 | if (sectionName == "general") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 188 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 189 | ret = processConfSectionGeneral(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 190 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 191 | else if (sectionName == "neighbors") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 192 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 193 | ret = processConfSectionNeighbors(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 194 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 195 | else if (sectionName == "hyperbolic") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 196 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 197 | ret = processConfSectionHyperbolic(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 198 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 199 | else if (sectionName == "fib") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 200 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 201 | ret = processConfSectionFib(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 202 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 203 | else if (sectionName == "advertising") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 204 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 205 | ret = processConfSectionAdvertising(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 206 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 207 | else if (sectionName == "security") |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 208 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 209 | ret = processConfSectionSecurity(section); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 210 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 211 | else |
| 212 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 213 | std::cerr << "Wrong configuration section: " << sectionName << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 214 | } |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 219 | ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 220 | { |
| 221 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 222 | std::string network = section.get<string>("network"); |
| 223 | std::string site = section.get<string>("site"); |
| 224 | std::string router = section.get<string>("router"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 225 | 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 Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 254 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 255 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 256 | int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 257 | 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 Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 272 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 273 | try { |
Alexander Afanasyev | 1cf1e10 | 2014-08-17 19:47:57 -0700 | [diff] [blame] | 274 | 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 Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 291 | 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 Afanasyev | 1cf1e10 | 2014-08-17 19:47:57 -0700 | [diff] [blame] | 304 | // non-critical error. default value is 4 |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | try { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 308 | |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 309 | std::string logLevel = section.get<string>("log-level"); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 310 | |
| 311 | if (isValidLogLevel(logLevel)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 312 | m_nlsr.getConfParameter().setLogLevel(logLevel); |
| 313 | } |
| 314 | else { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 315 | std::cerr << "Invalid value for log-level "; |
| 316 | std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | catch (const std::exception& ex) { |
| 321 | std::cerr << ex.what() << std::endl; |
| 322 | return false; |
| 323 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 324 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 325 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 326 | std::string logDir = section.get<string>("log-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 327 | 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 359 | std::string seqDir = section.get<string>("seq-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 360 | 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 Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 391 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 392 | return true; |
| 393 | } |
| 394 | |
| 395 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 396 | ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 397 | { |
| 398 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 399 | int retrials = section.get<int>("hello-retries"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 400 | 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 415 | int timeOut = section.get<int>("hello-timeout"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 416 | 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 430 | int interval = section.get<int>("hello-interval"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 431 | 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 Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 444 | |
| 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 468 | for (ConfigSection::const_iterator tn = |
| 469 | section.begin(); tn != section.end(); ++tn) { |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 470 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 471 | if (tn->first == "neighbor") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 472 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 473 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 474 | ConfigSection CommandAttriTree = tn->second; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 475 | 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 Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 481 | Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 482 | m_nlsr.getAdjacencyList().insert(adj); |
| 483 | } |
| 484 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 485 | std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]"; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 486 | std::cerr << " or bad URI format" << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 487 | } |
| 488 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 489 | catch (const std::exception& ex) { |
| 490 | std::cerr << ex.what() << std::endl; |
| 491 | return false; |
| 492 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 493 | } |
| 494 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 495 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 496 | } |
| 497 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 498 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 499 | ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 500 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 501 | std::string state; |
| 502 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 503 | state= section.get<string>("state","off"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 504 | 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 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 518 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 519 | catch (const std::exception& ex) { |
| 520 | std::cerr << ex.what() << std::endl; |
| 521 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 522 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 523 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 524 | 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 530 | double radius = section.get<double>("radius"); |
| 531 | double angle = section.get<double>("angle"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 532 | if (!m_nlsr.getConfParameter().setCorR(radius)) { |
| 533 | return false; |
| 534 | } |
| 535 | m_nlsr.getConfParameter().setCorTheta(angle); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 536 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 537 | catch (const std::exception& ex) { |
| 538 | std::cerr << ex.what() << std::endl; |
| 539 | if (state == "on" || state == "dry-run") { |
| 540 | return false; |
| 541 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 542 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 543 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 544 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 545 | } |
| 546 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 547 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 548 | ConfFileProcessor::processConfSectionFib(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 549 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 550 | try { |
| 551 | int maxFacesPerPrefixNumber = |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 552 | section.get<int>("max-faces-per-prefix"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 553 | if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN && |
| 554 | maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 555 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 556 | m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 557 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 558 | else { |
| 559 | std::cerr << "Wrong value for max-faces-per-prefix. "; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 560 | std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl; |
| 561 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 562 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 563 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 564 | catch (const std::exception& ex) { |
| 565 | cerr << ex.what() << endl; |
| 566 | return false; |
| 567 | } |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 568 | |
| 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 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 580 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 581 | } |
| 582 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 583 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 584 | ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 585 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 586 | for (ConfigSection::const_iterator tn = |
| 587 | section.begin(); tn != section.end(); ++tn) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 588 | 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 { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 596 | std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 597 | return false; |
| 598 | } |
| 599 | } |
| 600 | catch (const std::exception& ex) { |
| 601 | std::cerr << ex.what() << std::endl; |
| 602 | return false; |
| 603 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 604 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 605 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 606 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 607 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 608 | |
| 609 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 610 | ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 611 | { |
| 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); |
akmhoque | d57f367 | 2014-06-10 10:41:32 -0500 | [diff] [blame] | 621 | it++; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 622 | |
| 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 Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 649 | } // namespace nlsr |