blob: a7008e7d4f5db9f1f06ac65cf3819b9aa3a61f46 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -04002/*
Davide Pesaventoe28d8752022-03-19 03:55:25 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040019 */
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050020
akmhoque674b0b12014-05-20 14:33:28 -050021#include "conf-parameter.hpp"
22#include "logger.hpp"
Junxiao Shib032fcb2022-04-28 01:28:50 +000023#include <ndn-cxx/security/signing-helpers.hpp>
akmhoque674b0b12014-05-20 14:33:28 -050024
25namespace nlsr {
26
dmcoomescf8d0ed2017-02-21 11:39:01 -060027INIT_LOGGER(ConfParameter);
akmhoque674b0b12014-05-20 14:33:28 -050028
Ashlesh Gawande982a58f2018-02-14 17:39:12 -060029// To be changed when breaking changes are made to sync
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070030const uint64_t ConfParameter::SYNC_VERSION = 10;
Ashlesh Gawande982a58f2018-02-14 17:39:12 -060031
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040032static std::unique_ptr<ndn::security::CertificateFetcherDirectFetch>
Ashlesh Gawandedec85d22019-03-21 21:39:39 -050033makeCertificateFetcher(ndn::Face& face)
34{
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040035 auto fetcher = std::make_unique<ndn::security::CertificateFetcherDirectFetch>(face);
Ashlesh Gawandedec85d22019-03-21 21:39:39 -050036 fetcher->setSendDirectInterestOnly(true);
37 return fetcher;
38}
39
Saurab Dulal427e0122019-11-28 11:58:02 -060040ConfParameter::ConfParameter(ndn::Face& face, ndn::KeyChain& keyChain,
41 const std::string& confFileName)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 : m_confFileName(confFileName)
43 , m_lsaRefreshTime(LSA_REFRESH_TIME_DEFAULT)
44 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 , m_routingCalcInterval(ROUTING_CALC_INTERVAL_DEFAULT)
46 , m_faceDatasetFetchInterval(ndn::time::seconds(static_cast<int>(FACE_DATASET_FETCH_INTERVAL_DEFAULT)))
47 , m_lsaInterestLifetime(ndn::time::seconds(static_cast<int>(LSA_INTEREST_LIFETIME_DEFAULT)))
48 , m_routerDeadInterval(2 * LSA_REFRESH_TIME_DEFAULT)
49 , m_interestRetryNumber(HELLO_RETRIES_DEFAULT)
50 , m_interestResendTime(HELLO_TIMEOUT_DEFAULT)
51 , m_infoInterestInterval(HELLO_INTERVAL_DEFAULT)
52 , m_hyperbolicState(HYPERBOLIC_STATE_OFF)
53 , m_corR(0)
54 , m_maxFacesPerPrefix(MAX_FACES_PER_PREFIX_MIN)
55 , m_syncInterestLifetime(ndn::time::milliseconds(SYNC_INTEREST_LIFETIME_DEFAULT))
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070056 , m_syncProtocol(SYNC_PROTOCOL_PSYNC)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060057 , m_adjl()
58 , m_npl()
Ashlesh Gawandedec85d22019-03-21 21:39:39 -050059 , m_validator(makeCertificateFetcher(face))
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040060 , m_prefixUpdateValidator(std::make_unique<ndn::security::CertificateFetcherDirectFetch>(face))
Saurab Dulal427e0122019-11-28 11:58:02 -060061 , m_keyChain(keyChain)
Ashlesh Gawandedec85d22019-03-21 21:39:39 -050062{
63}
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064
akmhoque674b0b12014-05-20 14:33:28 -050065void
66ConfParameter::writeLog()
67{
dmcoomes5bcb39e2017-10-31 15:07:55 -050068 NLSR_LOG_INFO("Router Name: " << m_routerName);
69 NLSR_LOG_INFO("Site Name: " << m_siteName);
70 NLSR_LOG_INFO("Network: " << m_network);
71 NLSR_LOG_INFO("Router Prefix: " << m_routerPrefix);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072 NLSR_LOG_INFO("Sync Prefix: " << m_syncPrefix);
73 NLSR_LOG_INFO("Sync LSA prefix: " << m_lsaPrefix);
dmcoomes5bcb39e2017-10-31 15:07:55 -050074 NLSR_LOG_INFO("Hello Interest retry number: " << m_interestRetryNumber);
75 NLSR_LOG_INFO("Hello Interest resend second: " << m_interestResendTime);
76 NLSR_LOG_INFO("Info Interest interval: " << m_infoInterestInterval);
77 NLSR_LOG_INFO("LSA refresh time: " << m_lsaRefreshTime);
78 NLSR_LOG_INFO("FIB Entry refresh time: " << m_lsaRefreshTime * 2);
79 NLSR_LOG_INFO("LSA Interest lifetime: " << getLsaInterestLifetime());
80 NLSR_LOG_INFO("Router dead interval: " << getRouterDeadInterval());
81 NLSR_LOG_INFO("Max Faces Per Prefix: " << m_maxFacesPerPrefix);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050082 if (m_hyperbolicState == HYPERBOLIC_STATE_ON || m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
83 NLSR_LOG_INFO("Hyperbolic Routing: " << m_hyperbolicState);
84 NLSR_LOG_INFO("Hyp R: " << m_corR);
85 int i=0;
86 for (auto const& value: m_corTheta) {
87 NLSR_LOG_INFO("Hyp Angle " << i++ << ": "<< value);
88 }
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060089 }
dulalsaurab82a34c22019-02-04 17:31:21 +000090 NLSR_LOG_INFO("State Directory: " << m_stateFileDir);
Vince Lehman7b616582014-10-17 16:25:39 -050091
92 // Event Intervals
dmcoomes5bcb39e2017-10-31 15:07:55 -050093 NLSR_LOG_INFO("Adjacency LSA build interval: " << m_adjLsaBuildInterval);
dmcoomes5bcb39e2017-10-31 15:07:55 -050094 NLSR_LOG_INFO("Routing calculation interval: " << m_routingCalcInterval);
akmhoque674b0b12014-05-20 14:33:28 -050095}
96
Ashlesh Gawande982a58f2018-02-14 17:39:12 -060097void
98ConfParameter::setNetwork(const ndn::Name& networkName)
99{
100 m_network = networkName;
101
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102 m_syncPrefix.append("localhop");
103 m_syncPrefix.append(m_network);
104 m_syncPrefix.append("nlsr");
105 m_syncPrefix.append("sync");
106 m_syncPrefix.appendVersion(SYNC_VERSION);
Ashlesh Gawande982a58f2018-02-14 17:39:12 -0600107
108 m_lsaPrefix.append("localhop");
109 m_lsaPrefix.append(m_network);
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500110 m_lsaPrefix.append("nlsr");
Ashlesh Gawande982a58f2018-02-14 17:39:12 -0600111 m_lsaPrefix.append("LSA");
112}
113
Saurab Dulal427e0122019-11-28 11:58:02 -0600114void
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400115ConfParameter::loadCertToValidator(const ndn::security::Certificate& cert)
Saurab Dulal427e0122019-11-28 11:58:02 -0600116{
117 NLSR_LOG_TRACE("Loading Certificate Name: " << cert.getName());
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400118 m_validator.loadAnchor("Authoritative-Certificate", ndn::security::Certificate(cert));
119 m_prefixUpdateValidator.loadAnchor("Authoritative-Certificate", ndn::security::Certificate(cert));
Saurab Dulal427e0122019-11-28 11:58:02 -0600120}
121
Junxiao Shib032fcb2022-04-28 01:28:50 +0000122ndn::optional<ndn::security::Certificate>
Saurab Dulal427e0122019-11-28 11:58:02 -0600123ConfParameter::initializeKey()
124{
Junxiao Shib032fcb2022-04-28 01:28:50 +0000125 using namespace ndn::security;
Saurab Dulal427e0122019-11-28 11:58:02 -0600126 NLSR_LOG_DEBUG("Initializing Key ...");
127
Junxiao Shib032fcb2022-04-28 01:28:50 +0000128 Identity routerIdentity;
Saurab Dulal427e0122019-11-28 11:58:02 -0600129 try {
Junxiao Shib032fcb2022-04-28 01:28:50 +0000130 routerIdentity = m_keyChain.getPib().getIdentity(m_routerPrefix);
Saurab Dulal427e0122019-11-28 11:58:02 -0600131 }
Junxiao Shib032fcb2022-04-28 01:28:50 +0000132 catch (const Pib::Error&) {
133 NLSR_LOG_ERROR("Router identity " << m_routerPrefix << " not found. "
134 "NLSR is running without security. "
Saurab Dulal427e0122019-11-28 11:58:02 -0600135 "If security is enabled in the configuration, NLSR will not converge.");
Junxiao Shib032fcb2022-04-28 01:28:50 +0000136 return ndn::nullopt;
Saurab Dulal427e0122019-11-28 11:58:02 -0600137 }
Dylanfabe9722022-05-16 12:47:39 -0500138 catch (const std::invalid_argument& e) {
139 // This is (probably) needed for the dummy keychain patch.
140 // https://github.com/named-data/mini-ndn/blob/master/util/patches/ndn-cxx-dummy-keychain.patch
141 NLSR_LOG_DEBUG(e.what());
142 }
Saurab Dulal427e0122019-11-28 11:58:02 -0600143
Junxiao Shib032fcb2022-04-28 01:28:50 +0000144 auto instanceName = ndn::Name(m_routerPrefix).append("nlsr");
145 try {
146 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(instanceName));
147 }
148 catch (const Pib::Error&) {
149 // old instance identity does not exist
150 }
Dylanfabe9722022-05-16 12:47:39 -0500151 catch (const std::invalid_argument& e) {
152 // This is needed for the dummy-keychain patch (ref above) to handle the error that it generates.
153 const std::string exceptionText = e.what();
154 if (exceptionText.find("does not match identity") == std::string::npos) {
155 NLSR_LOG_ERROR(exceptionText);
156 throw;
157 }
158 return ndn::nullopt;
159 }
Saurab Dulal427e0122019-11-28 11:58:02 -0600160
Junxiao Shib032fcb2022-04-28 01:28:50 +0000161 auto key = m_keyChain.createIdentity(instanceName).getDefaultKey();
162 auto cert = m_keyChain.makeCertificate(key, signingByIdentity(routerIdentity));
163 m_keyChain.setDefaultCertificate(key, cert);
Saurab Dulal427e0122019-11-28 11:58:02 -0600164
Junxiao Shib032fcb2022-04-28 01:28:50 +0000165 m_signingInfo = signingByCertificate(cert);
166 loadCertToValidator(cert);
167 return cert;
Saurab Dulal427e0122019-11-28 11:58:02 -0600168}
169
akmhoque674b0b12014-05-20 14:33:28 -0500170} // namespace nlsr