conf-parameter.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, The 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 
21 #include "conf-parameter.hpp"
22 #include "logger.hpp"
23 
24 #include <ndn-cxx/security/signing-helpers.hpp>
25 
26 namespace nlsr {
27 
28 INIT_LOGGER(ConfParameter);
29 
30 static std::unique_ptr<ndn::security::CertificateFetcherDirectFetch>
31 makeCertificateFetcher(ndn::Face& face)
32 {
33  auto fetcher = std::make_unique<ndn::security::CertificateFetcherDirectFetch>(face);
34  fetcher->setSendDirectInterestOnly(true);
35  return fetcher;
36 }
37 
38 ConfParameter::ConfParameter(ndn::Face& face, ndn::KeyChain& keyChain,
39  const std::string& confFileName)
40  : m_confFileName(confFileName)
41  , m_lsaRefreshTime(LSA_REFRESH_TIME_DEFAULT)
42  , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
43  , m_routingCalcInterval(ROUTING_CALC_INTERVAL_DEFAULT)
44  , m_faceDatasetFetchInterval(ndn::time::seconds(static_cast<int>(FACE_DATASET_FETCH_INTERVAL_DEFAULT)))
45  , m_lsaInterestLifetime(ndn::time::seconds(static_cast<int>(LSA_INTEREST_LIFETIME_DEFAULT)))
46  , m_routerDeadInterval(2 * LSA_REFRESH_TIME_DEFAULT)
47  , m_interestRetryNumber(HELLO_RETRIES_DEFAULT)
48  , m_interestResendTime(HELLO_TIMEOUT_DEFAULT)
49  , m_infoInterestInterval(HELLO_INTERVAL_DEFAULT)
50  , m_hyperbolicState(HYPERBOLIC_STATE_OFF)
51  , m_corR(0)
52  , m_maxFacesPerPrefix(MAX_FACES_PER_PREFIX_MIN)
53  , m_syncInterestLifetime(ndn::time::milliseconds(SYNC_INTEREST_LIFETIME_DEFAULT))
54  , m_adjl()
55  , m_npl()
56  , m_validator(makeCertificateFetcher(face))
57  , m_prefixUpdateValidator(std::make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
58  , m_keyChain(keyChain)
59 {
60 }
61 
62 void
64 {
65  NLSR_LOG_INFO("Router Name: " << m_routerName);
66  NLSR_LOG_INFO("Site Name: " << m_siteName);
67  NLSR_LOG_INFO("Network: " << m_network);
68  NLSR_LOG_INFO("Router Prefix: " << m_routerPrefix);
69  NLSR_LOG_INFO("Sync Prefix: " << m_syncPrefix);
70  NLSR_LOG_INFO("Sync LSA prefix: " << m_lsaPrefix);
71  NLSR_LOG_INFO("Hello Interest retry number: " << m_interestRetryNumber);
72  NLSR_LOG_INFO("Hello Interest resend second: " << m_interestResendTime);
73  NLSR_LOG_INFO("Info Interest interval: " << m_infoInterestInterval);
74  NLSR_LOG_INFO("LSA refresh time: " << m_lsaRefreshTime);
75  NLSR_LOG_INFO("FIB Entry refresh time: " << m_lsaRefreshTime * 2);
76  NLSR_LOG_INFO("LSA Interest lifetime: " << getLsaInterestLifetime());
77  NLSR_LOG_INFO("Router dead interval: " << getRouterDeadInterval());
78  NLSR_LOG_INFO("Max Faces Per Prefix: " << m_maxFacesPerPrefix);
79  if (m_hyperbolicState == HYPERBOLIC_STATE_ON || m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
80  NLSR_LOG_INFO("Hyperbolic Routing: " << m_hyperbolicState);
81  NLSR_LOG_INFO("Hyp R: " << m_corR);
82  int i=0;
83  for (auto const& value: m_corTheta) {
84  NLSR_LOG_INFO("Hyp Angle " << i++ << ": "<< value);
85  }
86  }
87  NLSR_LOG_INFO("State Directory: " << m_stateFileDir);
88 
89  // Event Intervals
90  NLSR_LOG_INFO("Adjacency LSA build interval: " << m_adjLsaBuildInterval);
91  NLSR_LOG_INFO("Routing calculation interval: " << m_routingCalcInterval);
92 }
93 
94 void
95 ConfParameter::setNetwork(const ndn::Name& networkName)
96 {
97  m_network = networkName;
98 
99  if (m_syncProtocol != SyncProtocol::SVS) {
100  m_syncPrefix.append("localhop");
101  }
102  m_syncPrefix.append(m_network);
103  m_syncPrefix.append("nlsr");
104  m_syncPrefix.append("sync");
105  m_syncPrefix.appendVersion(SYNC_VERSION);
106 
107  m_lsaPrefix.append("localhop");
108  m_lsaPrefix.append(m_network);
109  m_lsaPrefix.append("nlsr");
110  m_lsaPrefix.append("LSA");
111 }
112 
113 void
114 ConfParameter::loadCertToValidator(const ndn::security::Certificate& cert)
115 {
116  NLSR_LOG_TRACE("Loading Certificate Name: " << cert.getName());
117  m_validator.loadAnchor("Authoritative-Certificate", ndn::security::Certificate(cert));
118  m_prefixUpdateValidator.loadAnchor("Authoritative-Certificate", ndn::security::Certificate(cert));
119 }
120 
121 std::optional<ndn::security::Certificate>
123 {
124  using namespace ndn::security;
125  NLSR_LOG_DEBUG("Initializing Key ...");
126 
127  Identity routerIdentity;
128  try {
129  routerIdentity = m_keyChain.getPib().getIdentity(m_routerPrefix);
130  }
131  catch (const Pib::Error&) {
132  NLSR_LOG_ERROR("Router identity " << m_routerPrefix << " not found. "
133  "NLSR is running without security. "
134  "If security is enabled in the configuration, NLSR will not converge.");
135  return std::nullopt;
136  }
137  catch (const std::invalid_argument& e) {
138  // This is (probably) needed for the dummy keychain patch.
139  // https://github.com/named-data/mini-ndn/blob/master/util/patches/ndn-cxx-dummy-keychain.patch
140  NLSR_LOG_DEBUG(e.what());
141  }
142 
143  auto instanceName = ndn::Name(m_routerPrefix).append("nlsr");
144  try {
145  m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(instanceName));
146  }
147  catch (const Pib::Error&) {
148  // old instance identity does not exist
149  }
150  catch (const std::invalid_argument& e) {
151  // This is needed for the dummy-keychain patch (ref above) to handle the error that it generates.
152  const std::string exceptionText = e.what();
153  if (exceptionText.find("does not match identity") == std::string::npos) {
154  NLSR_LOG_ERROR(exceptionText);
155  throw;
156  }
157  return std::nullopt;
158  }
159 
160  auto key = m_keyChain.createIdentity(instanceName).getDefaultKey();
161  auto cert = m_keyChain.makeCertificate(key, signingByIdentity(routerIdentity));
162  m_keyChain.setDefaultCertificate(key, cert);
163 
164  m_signingInfo = signingByCertificate(cert);
165  loadCertToValidator(cert);
166  return cert;
167 }
168 
169 } // namespace nlsr
std::optional< ndn::security::Certificate > initializeKey()
void writeLog()
Dump the current state of all attributes to the log.
uint32_t getRouterDeadInterval() const
void loadCertToValidator(const ndn::security::Certificate &cert)
ConfParameter(ndn::Face &face, ndn::KeyChain &keyChain, const std::string &confFileName="nlsr.conf")
const ndn::time::seconds & getLsaInterestLifetime() const
void setNetwork(const ndn::Name &networkName)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:37
#define NLSR_LOG_INFO(x)
Definition: logger.hpp:39
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
static std::unique_ptr< ndn::security::CertificateFetcherDirectFetch > makeCertificateFetcher(ndn::Face &face)
@ LSA_REFRESH_TIME_DEFAULT
@ HELLO_RETRIES_DEFAULT
@ ROUTING_CALC_INTERVAL_DEFAULT
@ MAX_FACES_PER_PREFIX_MIN
@ FACE_DATASET_FETCH_INTERVAL_DEFAULT
@ ADJ_LSA_BUILD_INTERVAL_DEFAULT
@ HYPERBOLIC_STATE_ON
@ HYPERBOLIC_STATE_DRY_RUN
@ HYPERBOLIC_STATE_OFF
@ LSA_INTEREST_LIFETIME_DEFAULT
@ HELLO_INTERVAL_DEFAULT
@ HELLO_TIMEOUT_DEFAULT
@ SYNC_INTEREST_LIFETIME_DEFAULT