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