blob: d917fa6ec5d4251ded1c3cb756c6ec6e4fadf492 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
laqinfan7c13ba52018-01-15 21:17:24 +00003 * Copyright (c) 2014-2019, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include "lsdb.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050023
akmhoque674b0b12014-05-20 14:33:28 -050024#include "logger.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050025#include "nlsr.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050026#include "utility/name-helper.hpp"
27
28#include <ndn-cxx/security/signing-helpers.hpp>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050029
akmhoque53353462014-04-22 08:43:45 -050030namespace nlsr {
31
dmcoomescf8d0ed2017-02-21 11:39:01 -060032INIT_LOGGER(Lsdb);
akmhoque674b0b12014-05-20 14:33:28 -050033
Jiewen Tana0497d82015-02-02 21:59:18 -080034const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050035const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Nick Gordone98480b2017-05-24 11:23:03 -050036const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
37 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050038
Ashlesh Gawande85998a12017-12-07 22:22:13 -060039Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain,
40 ndn::security::SigningInfo& signingInfo, ConfParameter& confParam,
41 NamePrefixTable& namePrefixTable, RoutingTable& routingTable)
42 : m_face(face)
43 , m_scheduler(face.getIoService())
44 , m_signingInfo(signingInfo)
45 , m_confParam(confParam)
46 , m_namePrefixTable(namePrefixTable)
47 , m_routingTable(routingTable)
48 , m_sync(m_face,
Nick Gordon727d4832017-10-13 18:04:25 -050049 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050050 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050051 return isLsaNew(routerName, lsaType, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060052 }, m_confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
54 , m_thisRouterPrefix(m_confParam.getRouterPrefix().toUri())
55 , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
dulalsaurab82a34c22019-02-04 17:31:21 +000056 , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
Nick Gordon9eac4d92017-08-29 17:31:29 -050057 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
Ashlesh Gawande939b6f82018-12-09 16:51:09 -060058 [this] (const ndn::Name& updateName, uint64_t sequenceNumber) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050059 ndn::Name lsaInterest{updateName};
60 lsaInterest.appendNumber(sequenceNumber);
61 expressInterest(lsaInterest, 0);
62 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060063 , m_segmentPublisher(m_face, keyChain)
64 , m_isBuildAdjLsaSheduled(false)
65 , m_adjBuildCount(0)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050066{
67}
68
Ashlesh Gawande744e4812018-08-22 16:26:24 -050069Lsdb::~Lsdb()
70{
71 for (const auto& sp : m_fetchers) {
72 sp->stop();
73 }
74}
75
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050076void
77Lsdb::onFetchLsaError(uint32_t errorCode,
78 const std::string& msg,
Ashlesh Gawande744e4812018-08-22 16:26:24 -050079 const ndn::Name& interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050080 uint32_t retransmitNo,
81 const ndn::time::steady_clock::TimePoint& deadline,
82 ndn::Name lsaName,
83 uint64_t seqNo)
84{
dmcoomes5bcb39e2017-10-31 15:07:55 -050085 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040086 << ", Message: " << msg);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050087
88 if (ndn::time::steady_clock::now() < deadline) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040089 auto it = m_highestSeqNo.find(lsaName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050090 if (it != m_highestSeqNo.end() && it->second == seqNo) {
91 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
92 // immediately since at the least the LSA Interest lifetime has elapsed.
93 // Otherwise, it is necessary to delay the Interest re-expression to prevent
94 // the potential for constant Interest flooding.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060095 ndn::time::seconds delay = m_confParam.getLsaInterestLifetime();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050096
97 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
98 delay = ndn::time::seconds(0);
99 }
100
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400101 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this,
102 interestName, retransmitNo + 1, deadline));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500103 }
104 }
105}
106
107void
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500108Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500109{
dmcoomes9f936662017-03-02 10:33:09 -0600110 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600111 data->setContent(ndn::Block(bufferPtr));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500112
dmcoomes5bcb39e2017-10-31 15:07:55 -0500113 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500114
115 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
116 uint64_t seqNo = interestName[-1].toNumber();
117
118 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
119 m_highestSeqNo[lsaName] = seqNo;
120 }
121 else if (seqNo > m_highestSeqNo[lsaName]) {
122 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500123 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500124 }
125 else if (seqNo < m_highestSeqNo[lsaName]) {
126 return;
127 }
128
129 onContentValidated(data);
130}
akmhoque53353462014-04-22 08:43:45 -0500131
Nick G97e34942016-07-11 14:46:27 -0500132 /*! \brief Compares if a name LSA is the same as the one specified by key
133
134 \param nlsa1 A name LSA object
135 \param key A key of an originating router to compare to nlsa1
136 */
akmhoque53353462014-04-22 08:43:45 -0500137static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500138nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500139{
140 return nlsa1.getKey() == key;
141}
142
akmhoque53353462014-04-22 08:43:45 -0500143bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500144Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500145{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600146 NameLsa nameLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500147 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500148 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600149 m_confParam.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500150 m_sequencingManager.increaseNameLsaSeq();
151
152 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500153 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500154
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500156}
157
akmhoqueb6450b12014-04-24 00:01:03 -0500158NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500159Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500160{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400161 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
162 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500163 if (it != m_nameLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400164 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500165 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400166 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500167}
168
169bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500170Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500171{
akmhoqueb6450b12014-04-24 00:01:03 -0500172 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500173 // Is the name in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400174 if (nameLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500175 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500176 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500177 return true;
178 }
akmhoque157b0a42014-05-13 00:26:37 -0500179 else {
akmhoque53353462014-04-22 08:43:45 -0500180 return false;
181 }
182 }
183 return true;
184}
185
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400186ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500187Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
188 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500189{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400190 return m_scheduler.schedule(expTime + GRACE_PERIOD,
191 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500192}
193
194bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500195Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500196{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000197 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700198 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500199 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500200 // Determines if the name LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400201 if (chkNameLsa == nullptr) {
akmhoque53353462014-04-22 08:43:45 -0500202 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500203 NLSR_LOG_DEBUG("New Name LSA");
204 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500205 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500206
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000207 NLSR_LOG_TRACE("nlsa.getOrigRouter(): " << nlsa.getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600208 NLSR_LOG_TRACE("m_confParam.getRouterPrefix(): " << m_confParam.getRouterPrefix());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000209
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600210 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500211 // If this name LSA is from another router, add the advertised
212 // prefixes to the NPT.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600213 m_namePrefixTable.addEntry(nlsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500214 nlsa.getOrigRouter());
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500215 for (const auto& name : nlsa.getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600216 if (name != m_confParam.getRouterPrefix()) {
217 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500218 }
219 }
220 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600221 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500222 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
223 ndn::time::system_clock::now();
224 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500225 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500226 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500227 nlsa.getLsSeqNo(),
228 timeToExpire));
229 }
Nick G97e34942016-07-11 14:46:27 -0500230 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500231 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000232 NLSR_LOG_TRACE("Known name lsa");
233 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
234 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500235 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500236 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
237 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500238 chkNameLsa->writeLog();
239 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500240 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500241 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500242 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500243 // Obtain the set difference of the current and the incoming
244 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500245 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
246 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
247 std::list<ndn::Name> namesToAdd;
248 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
249 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500250 for (const auto& name : namesToAdd) {
251 chkNameLsa->addName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600252 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
253 if (name != m_confParam.getRouterPrefix()) {
254 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500255 }
256 }
257 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500258
259 chkNameLsa->getNpl().sort();
260
Nick G97e34942016-07-11 14:46:27 -0500261 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500262 std::list<ndn::Name> namesToRemove;
263 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
264 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500265 for (const auto& name : namesToRemove) {
266 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
267 chkNameLsa->removeName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600268 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
269 if (name != m_confParam.getRouterPrefix()) {
270 m_namePrefixTable.removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500271 }
272 }
273 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600274
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600275 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400276 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500277 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500278 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400279 chkNameLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500280 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500281 nlsa.getLsSeqNo(),
282 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500283 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500284 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500285 }
286 }
287 return true;
288}
289
290bool
291Lsdb::addNameLsa(NameLsa& nlsa)
292{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400293 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
294 std::bind(nameLsaCompareByKey, _1, nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500295 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500296 m_nameLsdb.push_back(nlsa);
297 return true;
298 }
299 return false;
300}
301
302bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500303Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500304{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400305 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
306 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500307 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500308 NLSR_LOG_DEBUG("Deleting Name Lsa");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400309 it->writeLog();
Nick G97e34942016-07-11 14:46:27 -0500310 // If the requested name LSA is not ours, we also need to remove
311 // its entries from the NPT.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400312 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
313 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600314
Nick Gordonf14ec352017-07-24 16:09:58 -0500315 for (const auto& name : it->getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600316 if (name != m_confParam.getRouterPrefix()) {
317 m_namePrefixTable.removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500318 }
319 }
320 }
321 m_nameLsdb.erase(it);
322 return true;
323 }
324 return false;
325}
326
327bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500328Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500329{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400330 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
331 std::bind(nameLsaCompareByKey, _1, key));
332 return it != m_nameLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500333}
334
335void
akmhoque2f423352014-06-03 11:49:35 -0500336Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500337{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500338 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500339 for (const auto& nlsa : m_nameLsdb) {
340 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500341 }
342}
343
Jiewen Tana0497d82015-02-02 21:59:18 -0800344const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500345Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800346{
347 return m_nameLsdb;
348}
349
akmhoque53353462014-04-22 08:43:45 -0500350// Cor LSA and LSDB related Functions start here
351
Nick G97e34942016-07-11 14:46:27 -0500352/*! \brief Compares whether an LSA object is the same as a key.
353 \param clsa The cor. LSA to check the identity of.
354 \param key The key of the publishing router to check against.
355*/
akmhoque53353462014-04-22 08:43:45 -0500356static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500357corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500358{
359 return clsa.getKey() == key;
360}
361
362bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500363Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500364{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600365 CoordinateLsa corLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500366 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500367 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600368 m_confParam.getCorR(),
369 m_confParam.getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500370
371 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600372 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500373 m_sequencingManager.increaseCorLsaSeq();
374 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500375 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500376 }
377
akmhoque31d1d4b2014-05-05 22:08:14 -0500378 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500379
akmhoque53353462014-04-22 08:43:45 -0500380 return true;
381}
382
akmhoqueb6450b12014-04-24 00:01:03 -0500383CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500384Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500385{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400386 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
387 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500388 if (it != m_corLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400389 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500390 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400391 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500392}
393
394bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500395Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500396{
akmhoqueb6450b12014-04-24 00:01:03 -0500397 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500398 // Is the coordinate LSA in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400399 if (clsa != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500400 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500401 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500402 return true;
403 }
akmhoque157b0a42014-05-13 00:26:37 -0500404 else {
akmhoque53353462014-04-22 08:43:45 -0500405 return false;
406 }
407 }
408 return true;
409}
410
Nick G97e34942016-07-11 14:46:27 -0500411 // Schedules a refresh/expire event in the scheduler.
412 // \param key The name of the router that published the LSA.
413 // \param seqNo the seq. no. associated with the LSA to check.
414 // \param expTime How long to wait before triggering the event.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400415ndn::scheduler::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500416Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500417 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500418{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400419 return m_scheduler.schedule(expTime + GRACE_PERIOD,
420 std::bind(&Lsdb::expireOrRefreshCoordinateLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500421}
422
423bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500424Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500425{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700426 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500427 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500428 // Checking whether the LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400429 if (chkCorLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500430 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
431 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500432 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500433 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500434
Nick Gordon5c467f02016-07-13 13:40:10 -0500435 // Register the LSA's origin router prefix
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600436 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
437 m_namePrefixTable.addEntry(clsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500438 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500439 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600440 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
441 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500442 }
Nick G97e34942016-07-11 14:46:27 -0500443 // Set the expiration time for the new LSA.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600444 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500445 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
446 ndn::time::system_clock::now();
447 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500448 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500449 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500450 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500451 }
Nick G97e34942016-07-11 14:46:27 -0500452 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500453 else {
454 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500455 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
456 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500457 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500458 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500459 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500460 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500461 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500462 chkCorLsa->setCorRadius(clsa.getCorRadius());
463 chkCorLsa->setCorTheta(clsa.getCorTheta());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600464 if (m_confParam.getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
465 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500466 }
467 }
Nick G97e34942016-07-11 14:46:27 -0500468 // If this is an LSA from another router, refresh its expiration time.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600469 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400470 auto duration = clsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500471 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500472 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400473 chkCorLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500474 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500475 clsa.getLsSeqNo(),
476 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500477 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500478 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500479 }
480 }
481 return true;
482}
483
484bool
akmhoqueb6450b12014-04-24 00:01:03 -0500485Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500486{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400487 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
488 std::bind(corLsaCompareByKey, _1, clsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500489 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500490 m_corLsdb.push_back(clsa);
491 return true;
492 }
493 return false;
494}
495
496bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500497Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500498{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400499 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
500 std::bind(corLsaCompareByKey, _1, key));
501
akmhoque157b0a42014-05-13 00:26:37 -0500502 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500503 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500504 it->writeLog();
505
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600506 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
507 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500508 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500509
akmhoque53353462014-04-22 08:43:45 -0500510 m_corLsdb.erase(it);
511 return true;
512 }
513 return false;
514}
515
516bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500517Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500518{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400519 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
520 std::bind(corLsaCompareByKey, _1, key));
521 return it != m_corLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500522}
523
524void
akmhoque2f423352014-06-03 11:49:35 -0500525Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500526{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500527 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500528 for (const auto& corLsa : m_corLsdb) {
529 corLsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500530 }
531}
532
Jiewen Tana0497d82015-02-02 21:59:18 -0800533const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500534Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800535{
536 return m_corLsdb;
537}
538
akmhoque53353462014-04-22 08:43:45 -0500539// Adj LSA and LSDB related function starts here
540
Nick G97e34942016-07-11 14:46:27 -0500541 /*! \brief Returns whether an adj. LSA object is from some router.
542 \param alsa The adj. LSA object.
543 \param key The router name that you want to compare the LSA with.
544 */
akmhoque53353462014-04-22 08:43:45 -0500545static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500546adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500547{
548 return alsa.getKey() == key;
549}
550
akmhoque53353462014-04-22 08:43:45 -0500551void
Vince Lehman50df6b72015-03-03 12:06:40 -0600552Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500553{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600554 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600555
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600556 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500557 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500558 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500559 return;
560 }
561
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600562 if (m_isBuildAdjLsaSheduled == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500563 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600564
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400565 m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600566 m_isBuildAdjLsaSheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600567 }
568}
569
570void
571Lsdb::buildAdjLsa()
572{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500573 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600574
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600575 m_isBuildAdjLsaSheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500576
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600577 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500578
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600579 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500580 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500581 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500582 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600583 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500584 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500585 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500586 }
Nick G97e34942016-07-11 14:46:27 -0500587 // We have no active neighbors, meaning no one can route through
588 // us. So delete our entry in the LSDB. This prevents this
589 // router from refreshing the LSA, eventually causing other
590 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500591 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500592 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500593 // Get this router's key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600594 ndn::Name key = m_confParam.getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500595 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500596
akmhoque31d1d4b2014-05-05 22:08:14 -0500597 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500598 // Recompute routing table after removal
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600599 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500600 }
Nick G97e34942016-07-11 14:46:27 -0500601 // In the case that during building the adj LSA, the FIB has to
602 // wait on an Interest response, the number of scheduled adj LSA
603 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600604 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500605 }
606 }
Nick G97e34942016-07-11 14:46:27 -0500607 // We are still waiting to know the adjacency status of some
608 // neighbor, so schedule a build for later (when all that has
609 // hopefully finished)
610 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600611 m_isBuildAdjLsaSheduled = true;
612 int schedulingTime = m_confParam.getInterestRetryNumber() *
613 m_confParam.getInterestResendTime();
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400614 m_scheduler.schedule(ndn::time::seconds(schedulingTime), [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500615 }
akmhoque53353462014-04-22 08:43:45 -0500616}
617
akmhoque53353462014-04-22 08:43:45 -0500618bool
619Lsdb::addAdjLsa(AdjLsa& alsa)
620{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400621 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
622 std::bind(adjLsaCompareByKey, _1, alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500623 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500624 m_adjLsdb.push_back(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600625 // Add any new name prefixes to the NPT
626 // Only add NPT entries if this is an adj LSA from another router.
627 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
628 // Pass the originating router as both the name to register and
629 // where it came from.
630 m_namePrefixTable.addEntry(alsa.getOrigRouter(), alsa.getOrigRouter());
631 }
akmhoque53353462014-04-22 08:43:45 -0500632 return true;
633 }
634 return false;
635}
636
akmhoqueb6450b12014-04-24 00:01:03 -0500637AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500638Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500639{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400640 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
641 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500642 if (it != m_adjLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400643 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500644 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400645 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500646}
647
akmhoque53353462014-04-22 08:43:45 -0500648bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500649Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500650{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400651 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500652 // If it is in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400653 if (adjLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500654 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500655 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500656 return true;
657 }
akmhoque157b0a42014-05-13 00:26:37 -0500658 else {
akmhoque53353462014-04-22 08:43:45 -0500659 return false;
660 }
661 }
662 return true;
663}
664
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400665ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500666Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
667 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500668{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400669 return m_scheduler.schedule(expTime + GRACE_PERIOD,
670 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500671}
672
673bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500674Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500675{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700676 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500677 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500678 // If this adj. LSA is not in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400679 if (chkAdjLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500680 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
681 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500682 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500683 addAdjLsa(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600684
685 m_routingTable.scheduleRoutingTableCalculation();
686 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500687 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
688 ndn::time::system_clock::now();
689 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500690 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400691 scheduleAdjLsaExpiration(alsa.getKey(), alsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500692 }
akmhoque157b0a42014-05-13 00:26:37 -0500693 else {
694 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500695 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
696 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500697 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500698 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500699 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500700 // If the new adj LSA has new content, update the contents of
701 // the LSDB entry. Additionally, since we've changed the
702 // contents of the LSDB, we have to schedule a routing
703 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500704 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500705 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500706 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600707 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500708 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600709 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400710 auto duration = alsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500711 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500712 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400713 chkAdjLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500714 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500715 alsa.getLsSeqNo(),
716 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500717 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500718 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500719 }
720 }
721 return true;
722}
723
724bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500725Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500726{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600727 AdjLsa adjLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500728 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500729 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600730 m_confParam.getAdjacencyList().getNumOfActiveNeighbor(),
731 m_confParam.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500732
Nick Gordon5c467f02016-07-13 13:40:10 -0500733 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600734 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500735 m_sequencingManager.increaseAdjLsaSeq();
736 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500737 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500738 }
Vince Lehman904c2412014-09-23 19:36:11 -0500739
Vince Lehman9d097802015-03-16 17:55:59 -0500740 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500741}
742
743bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500744Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500745{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400746 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
747 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500748 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500749 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600750 it->writeLog();
751 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
752 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
753 }
akmhoque53353462014-04-22 08:43:45 -0500754 m_adjLsdb.erase(it);
755 return true;
756 }
757 return false;
758}
759
760bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500761Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500762{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400763 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
764 std::bind(adjLsaCompareByKey, _1, key));
765 return it != m_adjLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500766}
767
Jiewen Tana0497d82015-02-02 21:59:18 -0800768const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500769Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500770{
771 return m_adjLsdb;
772}
773
Nick G97e34942016-07-11 14:46:27 -0500774 // This function determines whether a name LSA should be refreshed
775 // or expired. The conditions for getting refreshed are: it is still
776 // in the LSDB, it hasn't been updated by something else already (as
777 // evidenced by its seq. no.), and this is the originating router for
778 // the LSA. Is it let expire in all other cases.
779 // lsaKey is the key of the LSA's publishing router.
780 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500781void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500782Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500783{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500784 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
785 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500786 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500787 // If this name LSA exists in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400788 if (chkNameLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500789 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500790 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500791 if (chkNameLsa->getLsSeqNo() == seqNo) {
792 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500793 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
794 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500795 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500796 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500797 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500798 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500799 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500800 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500801 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500802 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500803 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700804 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500805 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500806 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500807 }
Nick G97e34942016-07-11 14:46:27 -0500808 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500809 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500810 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500811 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500812 }
813 }
814 }
815}
816
Nick G97e34942016-07-11 14:46:27 -0500817 // This function determines whether an adj. LSA should be refreshed
818 // or expired. The conditions for getting refreshed are: it is still
819 // in the LSDB, it hasn't been updated by something else already (as
820 // evidenced by its seq. no.), and this is the originating router for
821 // the LSA. Is it let expire in all other cases.
822 // lsaKey is the key of the LSA's publishing router.
823 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500824void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500825Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500826{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500827 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
828 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500829 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500830 // If this is a valid LSA
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400831 if (chkAdjLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500832 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500833 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500834 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500835 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500836 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500837 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
838 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500839 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500840 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500841 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500842 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500843 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500844 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500845 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500846 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500847 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700848 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500849 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500850 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500851 }
Nick G97e34942016-07-11 14:46:27 -0500852 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500853 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500854 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500855 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500856 }
Nick G97e34942016-07-11 14:46:27 -0500857 // We have changed the contents of the LSDB, so we have to
858 // schedule a routing calculation
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600859 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500860 }
861 }
862}
863
Nick G97e34942016-07-11 14:46:27 -0500864 // This function determines whether an adj. LSA should be refreshed
865 // or expired. The conditions for getting refreshed are: it is still
866 // in the LSDB, it hasn't been updated by something else already (as
867 // evidenced by its seq. no.), and this is the originating router for
868 // the LSA. It is let expire in all other cases.
869 // lsaKey is the key of the LSA's publishing router.
870 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500871void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500872Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500873 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500874{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500875 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
876 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500877 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500878 // Whether the LSA is in the LSDB or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400879 if (chkCorLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500880 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500881 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500882 if (chkCorLsa->getLsSeqNo() == seqNo) {
883 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500884 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
885 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500886 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500887 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600888 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500889 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500890 }
891
akmhoquec7a79b22014-05-26 08:06:19 -0500892 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500893 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500894 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500895 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500896 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
897 chkCorLsa->getKey(),
898 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700899 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500900 // Only sync coordinate LSAs if link-state routing is disabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600901 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500902 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500903 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500904 }
akmhoque53353462014-04-22 08:43:45 -0500905 }
Nick G97e34942016-07-11 14:46:27 -0500906 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500907 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500908 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500909 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500910 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600911 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
912 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500913 }
914 }
915 }
916}
917
akmhoque53353462014-04-22 08:43:45 -0500918void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700919Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500920 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500921{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600922 // increment SENT_LSA_INTEREST
923 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
924
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500925 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500926 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700927 }
Nick G97e34942016-07-11 14:46:27 -0500928 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500929 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500930 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500931 uint64_t seqNo = interestName[-1].toNumber();
932
Nick G97e34942016-07-11 14:46:27 -0500933 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500934 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
935 m_highestSeqNo[lsaName] = seqNo;
936 }
Nick G97e34942016-07-11 14:46:27 -0500937 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500938 else if (seqNo > m_highestSeqNo[lsaName]) {
939 m_highestSeqNo[lsaName] = seqNo;
940 }
Nick G97e34942016-07-11 14:46:27 -0500941 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500942 else if (seqNo < m_highestSeqNo[lsaName]) {
943 return;
944 }
945
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500946 ndn::Interest interest(interestName);
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500947 ndn::util::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600948 options.interestLifetime = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500949
dmcoomes5bcb39e2017-10-31 15:07:55 -0500950 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600951 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest,
952 m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500953
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500954 auto it = m_fetchers.insert(fetcher).first;
955
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600956 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
Ashlesh Gawande15052402018-12-12 20:20:00 -0600957 // Nlsr class subscribes to this to fetch certificates
958 afterSegmentValidatedSignal(data);
959
960 // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
961 auto lsaSegment = std::make_shared<const ndn::Data>(data);
962 m_lsaStorage.insert(*lsaSegment);
963 const ndn::Name& segmentName = lsaSegment->getName();
964 // Schedule deletion of the segment
965 m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
966 [this, segmentName] { m_lsaStorage.erase(segmentName); });
967 });
968
969 fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
970 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
971 afterFetchLsa(bufferPtr, interestName);
972 m_fetchers.erase(it);
973 });
974
975 fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
976 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
977 m_fetchers.erase(it);
978 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000979
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600980 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -0500981 Lsa::Type lsaType;
982 std::istringstream(interestName[-2].toUri()) >> lsaType;
983 switch (lsaType) {
984 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600985 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500986 break;
987 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600988 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500989 break;
990 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600991 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500992 break;
993 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -0500994 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600995 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500996}
997
998void
999Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1000{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001001 ndn::Name interestName(interest.getName());
1002 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
1003
1004 if (interestName[-2].isVersion()) {
1005 // Interest for particular segment
1006 if (m_segmentPublisher.replyFromStore(interestName)) {
1007 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
1008 return;
1009 }
1010 // Remove version and segment
1011 interestName = interestName.getSubName(0, interestName.size() - 2);
1012 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
1013 }
1014
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001015 // increment RCV_LSA_INTEREST
1016 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1017
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001018 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001019 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001020
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001021 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001022 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001023 originRouter.append(interestName.getSubName(lsaPosition + 1,
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001024 interestName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001025
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001026 // if the interest is for this router's LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001027 if (originRouter == m_confParam.getRouterPrefix() && lsaPosition >= 0) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001028 uint64_t seqNo = interestName[-1].toNumber();
1029 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001030
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001031 std::string lsaType = interestName[-2].toUri();
1032 Lsa::Type interestedLsType;
1033 std::istringstream(lsaType) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001034
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001035 if (interestedLsType == Lsa::Type::NAME) {
1036 processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001037 }
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001038 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1039 processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
1040 }
1041 else if (interestedLsType == Lsa::Type::COORDINATE) {
1042 processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
1043 }
1044 else {
1045 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1046 }
1047 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001048 }
1049 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
Ashlesh Gawande15052402018-12-12 20:20:00 -06001050 std::shared_ptr<const ndn::Data> lsaSegment = m_lsaStorage.find(interest);
1051 if (lsaSegment) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001052 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001053 m_face.put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001054 }
akmhoque157b0a42014-05-13 00:26:37 -05001055 else {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001056 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001057 }
1058 }
1059}
1060
Nick G97e34942016-07-11 14:46:27 -05001061 // \brief Finds and sends a requested name LSA.
1062 // \param interest The interest that seeks the name LSA.
1063 // \param lsaKey The LSA that the Interest is seeking.
1064 // \param seqNo A sequence number to ensure that we are sending the
1065 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001066void
akmhoque31d1d4b2014-05-05 22:08:14 -05001067Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1068 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001069 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001070{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001071 // increment RCV_NAME_LSA_INTEREST
1072 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001073 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001074 NameLsa* nameLsa = findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001075 if (nameLsa != nullptr) {
1076 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001077 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001078 std::string content = nameLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001079 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1080 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001081 m_lsaRefreshTime, m_signingInfo);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001082
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001083 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001084 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001085 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001086 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001087 }
1088 }
1089 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001090 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001091 }
1092}
1093
Nick G97e34942016-07-11 14:46:27 -05001094 // \brief Finds and sends a requested adj. LSA.
1095 // \param interest The interest that seeks the adj. LSA.
1096 // \param lsaKey The LSA that the Interest is seeking.
1097 // \param seqNo A sequence number to ensure that we are sending the
1098 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001099void
1100Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1101 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001102 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001103{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001104 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001105 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001106 }
1107
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001108 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001109 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001110 AdjLsa* adjLsa = findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001111 if (adjLsa != nullptr) {
1112 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001113 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001114 std::string content = adjLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001115 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1116 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001117 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001118
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001119 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001120 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001121 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001122 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001123 }
1124 }
1125 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001126 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001127 }
1128}
1129
Nick G97e34942016-07-11 14:46:27 -05001130 // \brief Finds and sends a requested cor. LSA.
1131 // \param interest The interest that seeks the cor. LSA.
1132 // \param lsaKey The LSA that the Interest is seeking.
1133 // \param seqNo A sequence number to ensure that we are sending the
1134 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001135void
1136Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1137 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001138 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001139{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001140 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001141 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001142 }
1143
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001144 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001145 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001146 CoordinateLsa* corLsa = findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001147 if (corLsa != nullptr) {
1148 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001149 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001150 std::string content = corLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001151 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1152 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001153 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001154
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001155 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001156 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001157 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001158 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001159 }
1160 }
1161 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001162 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001163 }
1164}
1165
1166void
dmcoomes9f936662017-03-02 10:33:09 -06001167Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001168{
1169 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001170 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001171
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001172 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001173 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001174
akmhoque157b0a42014-05-13 00:26:37 -05001175 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001176
Nick G97e34942016-07-11 14:46:27 -05001177 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001178 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001179 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001180
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001181 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001182 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1183 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001184
Nick Gordon727d4832017-10-13 18:04:25 -05001185 Lsa::Type interestedLsType;
1186 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001187
Nick Gordon727d4832017-10-13 18:04:25 -05001188 if (interestedLsType == Lsa::Type::NAME) {
1189 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1190 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001191 }
Nick Gordon727d4832017-10-13 18:04:25 -05001192 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1193 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1194 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001195 }
Nick Gordon727d4832017-10-13 18:04:25 -05001196 else if (interestedLsType == Lsa::Type::COORDINATE) {
1197 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1198 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001199 }
akmhoque157b0a42014-05-13 00:26:37 -05001200 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001201 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001202 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001203
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001204 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001205 }
1206}
1207
1208void
1209Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001210 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001211{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001212 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001213 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001214 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001215 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001216 installNameLsa(nameLsa);
1217 }
akmhoque157b0a42014-05-13 00:26:37 -05001218 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001219 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001220 }
1221 }
1222}
1223
1224void
1225Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001226 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001227{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001228 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001229 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001230 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001231 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001232 installAdjLsa(adjLsa);
1233 }
akmhoque157b0a42014-05-13 00:26:37 -05001234 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001235 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001236 }
1237 }
1238}
1239
1240void
1241Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001242 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001243{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001244 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001245 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001246 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001247 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001248 installCoordinateLsa(corLsa);
1249 }
akmhoque157b0a42014-05-13 00:26:37 -05001250 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001251 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001252 }
1253 }
1254}
1255
akmhoquec7a79b22014-05-26 08:06:19 -05001256ndn::time::system_clock::TimePoint
1257Lsdb::getLsaExpirationTimePoint()
1258{
1259 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1260 expirationTimePoint = expirationTimePoint +
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001261 ndn::time::seconds(m_confParam.getRouterDeadInterval());
akmhoquec7a79b22014-05-26 08:06:19 -05001262 return expirationTimePoint;
1263}
akmhoque31d1d4b2014-05-05 22:08:14 -05001264
1265void
akmhoque2f423352014-06-03 11:49:35 -05001266Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001267{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001268 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001269 for (const auto& adj : m_adjLsdb) {
1270 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001271 }
1272}
1273
1274//-----utility function -----
1275bool
Nick Gordon727d4832017-10-13 18:04:25 -05001276Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001277{
Nick Gordon727d4832017-10-13 18:04:25 -05001278 switch (lsType) {
1279 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001280 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001281 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001282 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001283 case Lsa::Type::NAME:
1284 return doesNameLsaExist(key);
1285 default:
1286 return false;
akmhoque53353462014-04-22 08:43:45 -05001287 }
akmhoque53353462014-04-22 08:43:45 -05001288}
1289
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001290bool
Nick Gordon727d4832017-10-13 18:04:25 -05001291Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001292 const uint64_t& sequenceNumber) {
1293 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001294 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001295
Nick Gordon727d4832017-10-13 18:04:25 -05001296 switch (lsaType) {
1297 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001298 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001299 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001300 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001301 case Lsa::Type::NAME:
1302 return isNameLsaNew(lsaKey, sequenceNumber);
1303 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001304 return false;
1305 }
1306}
1307
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001308} // namespace nlsr