blob: 1e3cf1bd5484e617f88feea7d58599ba725eced2 [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 Gawande08bce9c2019-04-05 11:08:07 -050058 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
59 const ndn::Name& originRouter) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050060 ndn::Name lsaInterest{updateName};
61 lsaInterest.appendNumber(sequenceNumber);
62 expressInterest(lsaInterest, 0);
63 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064 , m_segmentPublisher(m_face, keyChain)
65 , m_isBuildAdjLsaSheduled(false)
66 , m_adjBuildCount(0)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050067{
68}
69
Ashlesh Gawande744e4812018-08-22 16:26:24 -050070Lsdb::~Lsdb()
71{
72 for (const auto& sp : m_fetchers) {
73 sp->stop();
74 }
75}
76
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050077void
78Lsdb::onFetchLsaError(uint32_t errorCode,
79 const std::string& msg,
Ashlesh Gawande744e4812018-08-22 16:26:24 -050080 const ndn::Name& interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050081 uint32_t retransmitNo,
82 const ndn::time::steady_clock::TimePoint& deadline,
83 ndn::Name lsaName,
84 uint64_t seqNo)
85{
dmcoomes5bcb39e2017-10-31 15:07:55 -050086 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040087 << ", Message: " << msg);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050088
89 if (ndn::time::steady_clock::now() < deadline) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040090 auto it = m_highestSeqNo.find(lsaName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050091 if (it != m_highestSeqNo.end() && it->second == seqNo) {
92 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
93 // immediately since at the least the LSA Interest lifetime has elapsed.
94 // Otherwise, it is necessary to delay the Interest re-expression to prevent
95 // the potential for constant Interest flooding.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060096 ndn::time::seconds delay = m_confParam.getLsaInterestLifetime();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050097
98 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
99 delay = ndn::time::seconds(0);
100 }
101
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400102 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this,
103 interestName, retransmitNo + 1, deadline));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500104 }
105 }
106}
107
108void
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500109Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500110{
dmcoomes9f936662017-03-02 10:33:09 -0600111 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600112 data->setContent(ndn::Block(bufferPtr));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500113
dmcoomes5bcb39e2017-10-31 15:07:55 -0500114 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500115
116 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
117 uint64_t seqNo = interestName[-1].toNumber();
118
119 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
120 m_highestSeqNo[lsaName] = seqNo;
121 }
122 else if (seqNo > m_highestSeqNo[lsaName]) {
123 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500124 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500125 }
126 else if (seqNo < m_highestSeqNo[lsaName]) {
127 return;
128 }
129
130 onContentValidated(data);
131}
akmhoque53353462014-04-22 08:43:45 -0500132
Nick G97e34942016-07-11 14:46:27 -0500133 /*! \brief Compares if a name LSA is the same as the one specified by key
134
135 \param nlsa1 A name LSA object
136 \param key A key of an originating router to compare to nlsa1
137 */
akmhoque53353462014-04-22 08:43:45 -0500138static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500139nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500140{
141 return nlsa1.getKey() == key;
142}
143
akmhoque53353462014-04-22 08:43:45 -0500144bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500145Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500146{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 NameLsa nameLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500148 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500149 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600150 m_confParam.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500151 m_sequencingManager.increaseNameLsaSeq();
152
153 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500154 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500155
akmhoque31d1d4b2014-05-05 22:08:14 -0500156 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500157}
158
akmhoqueb6450b12014-04-24 00:01:03 -0500159NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500160Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500161{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400162 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
163 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500164 if (it != m_nameLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400165 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500166 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400167 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500168}
169
170bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500171Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500172{
akmhoqueb6450b12014-04-24 00:01:03 -0500173 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500174 // Is the name in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400175 if (nameLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500176 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500177 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500178 return true;
179 }
akmhoque157b0a42014-05-13 00:26:37 -0500180 else {
akmhoque53353462014-04-22 08:43:45 -0500181 return false;
182 }
183 }
184 return true;
185}
186
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400187ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500188Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
189 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500190{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400191 return m_scheduler.schedule(expTime + GRACE_PERIOD,
192 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500193}
194
195bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500196Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500197{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000198 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700199 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500200 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500201 // Determines if the name LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400202 if (chkNameLsa == nullptr) {
akmhoque53353462014-04-22 08:43:45 -0500203 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500204 NLSR_LOG_DEBUG("New Name LSA");
205 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500206 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500207
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000208 NLSR_LOG_TRACE("nlsa.getOrigRouter(): " << nlsa.getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600209 NLSR_LOG_TRACE("m_confParam.getRouterPrefix(): " << m_confParam.getRouterPrefix());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000210
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600211 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500212 // If this name LSA is from another router, add the advertised
213 // prefixes to the NPT.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600214 m_namePrefixTable.addEntry(nlsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500215 nlsa.getOrigRouter());
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500216 for (const auto& name : nlsa.getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600217 if (name != m_confParam.getRouterPrefix()) {
218 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500219 }
220 }
dulalsaurabd0816a32019-07-26 13:11:24 +0000221 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500222 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500223 }
dulalsaurabd0816a32019-07-26 13:11:24 +0000224
akmhoque31d1d4b2014-05-05 22:08:14 -0500225 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500226 nlsa.getLsSeqNo(),
227 timeToExpire));
228 }
Nick G97e34942016-07-11 14:46:27 -0500229 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500230 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000231 NLSR_LOG_TRACE("Known name lsa");
232 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
233 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500234 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500235 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
236 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500237 chkNameLsa->writeLog();
238 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500239 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500240 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500241 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500242 // Obtain the set difference of the current and the incoming
243 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500244 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
245 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
246 std::list<ndn::Name> namesToAdd;
247 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
248 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500249 for (const auto& name : namesToAdd) {
250 chkNameLsa->addName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600251 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
252 if (name != m_confParam.getRouterPrefix()) {
253 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500254 }
255 }
256 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500257
258 chkNameLsa->getNpl().sort();
259
Nick G97e34942016-07-11 14:46:27 -0500260 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500261 std::list<ndn::Name> namesToRemove;
262 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
263 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500264 for (const auto& name : namesToRemove) {
265 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
266 chkNameLsa->removeName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600267 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
268 if (name != m_confParam.getRouterPrefix()) {
269 m_namePrefixTable.removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500270 }
271 }
272 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600273
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600274 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400275 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500276 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500277 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400278 chkNameLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500279 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500280 nlsa.getLsSeqNo(),
281 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500282 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500283 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500284 }
285 }
286 return true;
287}
288
289bool
290Lsdb::addNameLsa(NameLsa& nlsa)
291{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400292 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
293 std::bind(nameLsaCompareByKey, _1, nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500294 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500295 m_nameLsdb.push_back(nlsa);
296 return true;
297 }
298 return false;
299}
300
301bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500302Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500303{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400304 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
305 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500306 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500307 NLSR_LOG_DEBUG("Deleting Name Lsa");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400308 it->writeLog();
Nick G97e34942016-07-11 14:46:27 -0500309 // If the requested name LSA is not ours, we also need to remove
310 // its entries from the NPT.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400311 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
312 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600313
Nick Gordonf14ec352017-07-24 16:09:58 -0500314 for (const auto& name : it->getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600315 if (name != m_confParam.getRouterPrefix()) {
316 m_namePrefixTable.removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500317 }
318 }
319 }
320 m_nameLsdb.erase(it);
321 return true;
322 }
323 return false;
324}
325
326bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500327Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500328{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400329 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
330 std::bind(nameLsaCompareByKey, _1, key));
331 return it != m_nameLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500332}
333
334void
akmhoque2f423352014-06-03 11:49:35 -0500335Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500336{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500337 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500338 for (const auto& nlsa : m_nameLsdb) {
339 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500340 }
341}
342
Jiewen Tana0497d82015-02-02 21:59:18 -0800343const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500344Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800345{
346 return m_nameLsdb;
347}
348
akmhoque53353462014-04-22 08:43:45 -0500349// Cor LSA and LSDB related Functions start here
350
Nick G97e34942016-07-11 14:46:27 -0500351/*! \brief Compares whether an LSA object is the same as a key.
352 \param clsa The cor. LSA to check the identity of.
353 \param key The key of the publishing router to check against.
354*/
akmhoque53353462014-04-22 08:43:45 -0500355static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500356corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500357{
358 return clsa.getKey() == key;
359}
360
361bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500362Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500363{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600364 CoordinateLsa corLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500365 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500366 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600367 m_confParam.getCorR(),
368 m_confParam.getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500369
370 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600371 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500372 m_sequencingManager.increaseCorLsaSeq();
373 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500374 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500375 }
376
akmhoque31d1d4b2014-05-05 22:08:14 -0500377 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500378
akmhoque53353462014-04-22 08:43:45 -0500379 return true;
380}
381
akmhoqueb6450b12014-04-24 00:01:03 -0500382CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500383Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500384{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400385 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
386 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500387 if (it != m_corLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400388 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500389 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400390 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500391}
392
393bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500394Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500395{
akmhoqueb6450b12014-04-24 00:01:03 -0500396 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500397 // Is the coordinate LSA in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400398 if (clsa != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500399 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500400 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500401 return true;
402 }
akmhoque157b0a42014-05-13 00:26:37 -0500403 else {
akmhoque53353462014-04-22 08:43:45 -0500404 return false;
405 }
406 }
407 return true;
408}
409
Nick G97e34942016-07-11 14:46:27 -0500410 // Schedules a refresh/expire event in the scheduler.
411 // \param key The name of the router that published the LSA.
412 // \param seqNo the seq. no. associated with the LSA to check.
413 // \param expTime How long to wait before triggering the event.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400414ndn::scheduler::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500415Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500416 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500417{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400418 return m_scheduler.schedule(expTime + GRACE_PERIOD,
419 std::bind(&Lsdb::expireOrRefreshCoordinateLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500420}
421
422bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500423Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500424{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700425 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500426 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500427 // Checking whether the LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400428 if (chkCorLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500429 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
430 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500431 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500432 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500433
Nick Gordon5c467f02016-07-13 13:40:10 -0500434 // Register the LSA's origin router prefix
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600435 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
436 m_namePrefixTable.addEntry(clsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500437 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500438 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600439 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
440 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500441 }
Nick G97e34942016-07-11 14:46:27 -0500442 // Set the expiration time for the new LSA.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600443 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500444 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
445 ndn::time::system_clock::now();
446 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500447 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500448 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500449 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500450 }
Nick G97e34942016-07-11 14:46:27 -0500451 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500452 else {
453 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500454 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
455 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500456 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500457 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500458 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500459 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500460 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500461 chkCorLsa->setCorRadius(clsa.getCorRadius());
462 chkCorLsa->setCorTheta(clsa.getCorTheta());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600463 if (m_confParam.getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
464 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500465 }
466 }
Nick G97e34942016-07-11 14:46:27 -0500467 // If this is an LSA from another router, refresh its expiration time.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600468 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400469 auto duration = clsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500470 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500471 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400472 chkCorLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500473 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500474 clsa.getLsSeqNo(),
475 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500476 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500477 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500478 }
479 }
480 return true;
481}
482
483bool
akmhoqueb6450b12014-04-24 00:01:03 -0500484Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500485{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400486 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
487 std::bind(corLsaCompareByKey, _1, clsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500488 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500489 m_corLsdb.push_back(clsa);
490 return true;
491 }
492 return false;
493}
494
495bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500496Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500497{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400498 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
499 std::bind(corLsaCompareByKey, _1, key));
500
akmhoque157b0a42014-05-13 00:26:37 -0500501 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500502 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500503 it->writeLog();
504
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600505 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
506 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500507 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500508
akmhoque53353462014-04-22 08:43:45 -0500509 m_corLsdb.erase(it);
510 return true;
511 }
512 return false;
513}
514
515bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500516Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500517{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400518 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
519 std::bind(corLsaCompareByKey, _1, key));
520 return it != m_corLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500521}
522
523void
akmhoque2f423352014-06-03 11:49:35 -0500524Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500525{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500526 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500527 for (const auto& corLsa : m_corLsdb) {
528 corLsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500529 }
530}
531
Jiewen Tana0497d82015-02-02 21:59:18 -0800532const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500533Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800534{
535 return m_corLsdb;
536}
537
akmhoque53353462014-04-22 08:43:45 -0500538// Adj LSA and LSDB related function starts here
539
Nick G97e34942016-07-11 14:46:27 -0500540 /*! \brief Returns whether an adj. LSA object is from some router.
541 \param alsa The adj. LSA object.
542 \param key The router name that you want to compare the LSA with.
543 */
akmhoque53353462014-04-22 08:43:45 -0500544static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500545adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500546{
547 return alsa.getKey() == key;
548}
549
akmhoque53353462014-04-22 08:43:45 -0500550void
Vince Lehman50df6b72015-03-03 12:06:40 -0600551Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500552{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600553 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600554
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600555 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500556 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500557 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500558 return;
559 }
560
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600561 if (m_isBuildAdjLsaSheduled == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500562 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600563
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400564 m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600565 m_isBuildAdjLsaSheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600566 }
567}
568
569void
570Lsdb::buildAdjLsa()
571{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500572 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600573
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600574 m_isBuildAdjLsaSheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500575
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600576 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500577
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600578 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500579 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500580 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500581 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600582 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500583 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500584 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500585 }
Nick G97e34942016-07-11 14:46:27 -0500586 // We have no active neighbors, meaning no one can route through
587 // us. So delete our entry in the LSDB. This prevents this
588 // router from refreshing the LSA, eventually causing other
589 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500590 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500591 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500592 // Get this router's key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600593 ndn::Name key = m_confParam.getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500594 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500595
akmhoque31d1d4b2014-05-05 22:08:14 -0500596 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500597 // Recompute routing table after removal
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600598 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500599 }
Nick G97e34942016-07-11 14:46:27 -0500600 // In the case that during building the adj LSA, the FIB has to
601 // wait on an Interest response, the number of scheduled adj LSA
602 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600603 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500604 }
605 }
Nick G97e34942016-07-11 14:46:27 -0500606 // We are still waiting to know the adjacency status of some
607 // neighbor, so schedule a build for later (when all that has
608 // hopefully finished)
609 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600610 m_isBuildAdjLsaSheduled = true;
611 int schedulingTime = m_confParam.getInterestRetryNumber() *
612 m_confParam.getInterestResendTime();
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400613 m_scheduler.schedule(ndn::time::seconds(schedulingTime), [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500614 }
akmhoque53353462014-04-22 08:43:45 -0500615}
616
akmhoque53353462014-04-22 08:43:45 -0500617bool
618Lsdb::addAdjLsa(AdjLsa& alsa)
619{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400620 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
621 std::bind(adjLsaCompareByKey, _1, alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500622 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500623 m_adjLsdb.push_back(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600624 // Add any new name prefixes to the NPT
625 // Only add NPT entries if this is an adj LSA from another router.
626 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
627 // Pass the originating router as both the name to register and
628 // where it came from.
629 m_namePrefixTable.addEntry(alsa.getOrigRouter(), alsa.getOrigRouter());
630 }
akmhoque53353462014-04-22 08:43:45 -0500631 return true;
632 }
633 return false;
634}
635
akmhoqueb6450b12014-04-24 00:01:03 -0500636AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500637Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500638{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400639 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
640 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500641 if (it != m_adjLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400642 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500643 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400644 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500645}
646
akmhoque53353462014-04-22 08:43:45 -0500647bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500648Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500649{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400650 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500651 // If it is in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400652 if (adjLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500653 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500654 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500655 return true;
656 }
akmhoque157b0a42014-05-13 00:26:37 -0500657 else {
akmhoque53353462014-04-22 08:43:45 -0500658 return false;
659 }
660 }
661 return true;
662}
663
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400664ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500665Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
666 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500667{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400668 return m_scheduler.schedule(expTime + GRACE_PERIOD,
669 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500670}
671
672bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500673Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500674{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700675 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500676 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500677 // If this adj. LSA is not in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400678 if (chkAdjLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500679 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
680 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500681 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500682 addAdjLsa(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600683
684 m_routingTable.scheduleRoutingTableCalculation();
685 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500686 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
687 ndn::time::system_clock::now();
688 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500689 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400690 scheduleAdjLsaExpiration(alsa.getKey(), alsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500691 }
akmhoque157b0a42014-05-13 00:26:37 -0500692 else {
693 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500694 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
695 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500696 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500697 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500698 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500699 // If the new adj LSA has new content, update the contents of
700 // the LSDB entry. Additionally, since we've changed the
701 // contents of the LSDB, we have to schedule a routing
702 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500703 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500704 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500705 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600706 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500707 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600708 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400709 auto duration = alsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500710 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500711 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400712 chkAdjLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500713 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500714 alsa.getLsSeqNo(),
715 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500716 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500717 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500718 }
719 }
720 return true;
721}
722
723bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500724Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500725{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600726 AdjLsa adjLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500727 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500728 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600729 m_confParam.getAdjacencyList().getNumOfActiveNeighbor(),
730 m_confParam.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500731
Nick Gordon5c467f02016-07-13 13:40:10 -0500732 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600733 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500734 m_sequencingManager.increaseAdjLsaSeq();
735 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500736 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500737 }
Vince Lehman904c2412014-09-23 19:36:11 -0500738
Vince Lehman9d097802015-03-16 17:55:59 -0500739 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500740}
741
742bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500743Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500744{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400745 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
746 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500747 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500748 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600749 it->writeLog();
750 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
751 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
752 }
akmhoque53353462014-04-22 08:43:45 -0500753 m_adjLsdb.erase(it);
754 return true;
755 }
756 return false;
757}
758
759bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500760Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500761{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400762 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
763 std::bind(adjLsaCompareByKey, _1, key));
764 return it != m_adjLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500765}
766
Jiewen Tana0497d82015-02-02 21:59:18 -0800767const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500768Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500769{
770 return m_adjLsdb;
771}
772
Nick G97e34942016-07-11 14:46:27 -0500773 // This function determines whether a name LSA should be refreshed
774 // or expired. The conditions for getting refreshed are: it is still
775 // in the LSDB, it hasn't been updated by something else already (as
776 // evidenced by its seq. no.), and this is the originating router for
777 // the LSA. Is it let expire in all other cases.
778 // lsaKey is the key of the LSA's publishing router.
779 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500780void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500781Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500782{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500783 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
784 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500785 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500786 // If this name LSA exists in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400787 if (chkNameLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500788 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500789 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500790 if (chkNameLsa->getLsSeqNo() == seqNo) {
791 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500792 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
793 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500794 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500795 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500796 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500797 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500798 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500799 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500800 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500801 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500802 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700803 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500804 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500805 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500806 }
Nick G97e34942016-07-11 14:46:27 -0500807 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500808 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500809 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500810 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500811 }
812 }
813 }
814}
815
Nick G97e34942016-07-11 14:46:27 -0500816 // This function determines whether an adj. LSA should be refreshed
817 // or expired. The conditions for getting refreshed are: it is still
818 // in the LSDB, it hasn't been updated by something else already (as
819 // evidenced by its seq. no.), and this is the originating router for
820 // the LSA. Is it let expire in all other cases.
821 // lsaKey is the key of the LSA's publishing router.
822 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500823void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500824Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500825{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500826 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
827 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500828 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500829 // If this is a valid LSA
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400830 if (chkAdjLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500831 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500832 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500833 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500834 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500835 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500836 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
837 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500838 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500839 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500840 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500841 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500842 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500843 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500844 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500845 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500846 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700847 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500848 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500849 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500850 }
Nick G97e34942016-07-11 14:46:27 -0500851 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500852 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500853 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500854 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500855 }
Nick G97e34942016-07-11 14:46:27 -0500856 // We have changed the contents of the LSDB, so we have to
857 // schedule a routing calculation
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600858 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500859 }
860 }
861}
862
Nick G97e34942016-07-11 14:46:27 -0500863 // This function determines whether an adj. LSA should be refreshed
864 // or expired. The conditions for getting refreshed are: it is still
865 // in the LSDB, it hasn't been updated by something else already (as
866 // evidenced by its seq. no.), and this is the originating router for
867 // the LSA. It is let expire in all other cases.
868 // lsaKey is the key of the LSA's publishing router.
869 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500870void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500871Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500872 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500873{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500874 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
875 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500876 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500877 // Whether the LSA is in the LSDB or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400878 if (chkCorLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500879 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500880 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500881 if (chkCorLsa->getLsSeqNo() == seqNo) {
882 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500883 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
884 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500885 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500886 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600887 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500888 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500889 }
890
akmhoquec7a79b22014-05-26 08:06:19 -0500891 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500892 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500893 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500894 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500895 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
896 chkCorLsa->getKey(),
897 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700898 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500899 // Only sync coordinate LSAs if link-state routing is disabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600900 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500901 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500902 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500903 }
akmhoque53353462014-04-22 08:43:45 -0500904 }
Nick G97e34942016-07-11 14:46:27 -0500905 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500906 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500907 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500908 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500909 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600910 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
911 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500912 }
913 }
914 }
915}
916
akmhoque53353462014-04-22 08:43:45 -0500917void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700918Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500919 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500920{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600921 // increment SENT_LSA_INTEREST
922 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
923
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500924 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500925 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700926 }
Nick G97e34942016-07-11 14:46:27 -0500927 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500928 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500929 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500930 uint64_t seqNo = interestName[-1].toNumber();
931
Nick G97e34942016-07-11 14:46:27 -0500932 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500933 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
934 m_highestSeqNo[lsaName] = seqNo;
935 }
Nick G97e34942016-07-11 14:46:27 -0500936 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500937 else if (seqNo > m_highestSeqNo[lsaName]) {
938 m_highestSeqNo[lsaName] = seqNo;
939 }
Nick G97e34942016-07-11 14:46:27 -0500940 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500941 else if (seqNo < m_highestSeqNo[lsaName]) {
942 return;
943 }
944
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500945 ndn::Interest interest(interestName);
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500946 ndn::util::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600947 options.interestLifetime = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500948
dmcoomes5bcb39e2017-10-31 15:07:55 -0500949 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600950 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest,
951 m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500952
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500953 auto it = m_fetchers.insert(fetcher).first;
954
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600955 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
Ashlesh Gawande15052402018-12-12 20:20:00 -0600956 // Nlsr class subscribes to this to fetch certificates
957 afterSegmentValidatedSignal(data);
958
959 // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
960 auto lsaSegment = std::make_shared<const ndn::Data>(data);
961 m_lsaStorage.insert(*lsaSegment);
962 const ndn::Name& segmentName = lsaSegment->getName();
963 // Schedule deletion of the segment
964 m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
965 [this, segmentName] { m_lsaStorage.erase(segmentName); });
966 });
967
968 fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
969 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
970 afterFetchLsa(bufferPtr, interestName);
971 m_fetchers.erase(it);
972 });
973
974 fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
975 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
976 m_fetchers.erase(it);
977 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000978
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600979 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -0500980 Lsa::Type lsaType;
981 std::istringstream(interestName[-2].toUri()) >> lsaType;
982 switch (lsaType) {
983 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600984 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500985 break;
986 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600987 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500988 break;
989 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600990 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500991 break;
992 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -0500993 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600994 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500995}
996
997void
998Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
999{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001000 ndn::Name interestName(interest.getName());
1001 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
1002
1003 if (interestName[-2].isVersion()) {
1004 // Interest for particular segment
1005 if (m_segmentPublisher.replyFromStore(interestName)) {
1006 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
1007 return;
1008 }
1009 // Remove version and segment
1010 interestName = interestName.getSubName(0, interestName.size() - 2);
1011 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
1012 }
1013
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001014 // increment RCV_LSA_INTEREST
1015 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1016
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001017 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001018 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001019
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001020 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001021 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001022 originRouter.append(interestName.getSubName(lsaPosition + 1,
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001023 interestName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001024
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001025 // if the interest is for this router's LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001026 if (originRouter == m_confParam.getRouterPrefix() && lsaPosition >= 0) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001027 uint64_t seqNo = interestName[-1].toNumber();
1028 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001029
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001030 std::string lsaType = interestName[-2].toUri();
1031 Lsa::Type interestedLsType;
1032 std::istringstream(lsaType) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001033
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001034 if (interestedLsType == Lsa::Type::NAME) {
1035 processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001036 }
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001037 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1038 processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
1039 }
1040 else if (interestedLsType == Lsa::Type::COORDINATE) {
1041 processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
1042 }
1043 else {
1044 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1045 }
1046 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001047 }
1048 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
Ashlesh Gawande15052402018-12-12 20:20:00 -06001049 std::shared_ptr<const ndn::Data> lsaSegment = m_lsaStorage.find(interest);
1050 if (lsaSegment) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001051 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001052 m_face.put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001053 }
akmhoque157b0a42014-05-13 00:26:37 -05001054 else {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001055 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001056 }
1057 }
1058}
1059
Nick G97e34942016-07-11 14:46:27 -05001060 // \brief Finds and sends a requested name LSA.
1061 // \param interest The interest that seeks the name LSA.
1062 // \param lsaKey The LSA that the Interest is seeking.
1063 // \param seqNo A sequence number to ensure that we are sending the
1064 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001065void
akmhoque31d1d4b2014-05-05 22:08:14 -05001066Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1067 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001068 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001069{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001070 // increment RCV_NAME_LSA_INTEREST
1071 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001072 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001073 NameLsa* nameLsa = findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001074 if (nameLsa != nullptr) {
1075 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001076 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001077 std::string content = nameLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001078 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1079 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001080 m_lsaRefreshTime, m_signingInfo);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001081
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001082 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001083 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001084 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001085 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001086 }
1087 }
1088 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001089 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001090 }
1091}
1092
Nick G97e34942016-07-11 14:46:27 -05001093 // \brief Finds and sends a requested adj. LSA.
1094 // \param interest The interest that seeks the adj. LSA.
1095 // \param lsaKey The LSA that the Interest is seeking.
1096 // \param seqNo A sequence number to ensure that we are sending the
1097 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001098void
1099Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1100 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001101 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001102{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001103 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001104 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001105 }
1106
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001107 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001108 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001109 AdjLsa* adjLsa = findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001110 if (adjLsa != nullptr) {
1111 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001112 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001113 std::string content = adjLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001114 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1115 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001116 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001117
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001118 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001119 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001120 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001121 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001122 }
1123 }
1124 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001125 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001126 }
1127}
1128
Nick G97e34942016-07-11 14:46:27 -05001129 // \brief Finds and sends a requested cor. LSA.
1130 // \param interest The interest that seeks the cor. LSA.
1131 // \param lsaKey The LSA that the Interest is seeking.
1132 // \param seqNo A sequence number to ensure that we are sending the
1133 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001134void
1135Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1136 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001137 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001138{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001139 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001140 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001141 }
1142
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001143 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001144 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001145 CoordinateLsa* corLsa = findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001146 if (corLsa != nullptr) {
1147 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001148 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001149 std::string content = corLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001150 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1151 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001152 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001153
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001154 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001155 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001156 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001157 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001158 }
1159 }
1160 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001161 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001162 }
1163}
1164
1165void
dmcoomes9f936662017-03-02 10:33:09 -06001166Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001167{
1168 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001169 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001170
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001171 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001172 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001173
akmhoque157b0a42014-05-13 00:26:37 -05001174 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001175
Nick G97e34942016-07-11 14:46:27 -05001176 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001177 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001178 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001179
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001180 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001181 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1182 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001183
Nick Gordon727d4832017-10-13 18:04:25 -05001184 Lsa::Type interestedLsType;
1185 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001186
Nick Gordon727d4832017-10-13 18:04:25 -05001187 if (interestedLsType == Lsa::Type::NAME) {
1188 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1189 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001190 }
Nick Gordon727d4832017-10-13 18:04:25 -05001191 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1192 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1193 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001194 }
Nick Gordon727d4832017-10-13 18:04:25 -05001195 else if (interestedLsType == Lsa::Type::COORDINATE) {
1196 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1197 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001198 }
akmhoque157b0a42014-05-13 00:26:37 -05001199 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001200 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001201 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001202
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001203 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001204 }
1205}
1206
1207void
1208Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001209 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001210{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001211 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001212 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001213 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001214 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001215 installNameLsa(nameLsa);
1216 }
akmhoque157b0a42014-05-13 00:26:37 -05001217 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001218 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001219 }
1220 }
1221}
1222
1223void
1224Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001225 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001226{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001227 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001228 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001229 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001230 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001231 installAdjLsa(adjLsa);
1232 }
akmhoque157b0a42014-05-13 00:26:37 -05001233 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001234 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001235 }
1236 }
1237}
1238
1239void
1240Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001241 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001242{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001243 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001244 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001245 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001246 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001247 installCoordinateLsa(corLsa);
1248 }
akmhoque157b0a42014-05-13 00:26:37 -05001249 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001250 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001251 }
1252 }
1253}
1254
akmhoquec7a79b22014-05-26 08:06:19 -05001255ndn::time::system_clock::TimePoint
1256Lsdb::getLsaExpirationTimePoint()
1257{
1258 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1259 expirationTimePoint = expirationTimePoint +
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001260 ndn::time::seconds(m_confParam.getRouterDeadInterval());
akmhoquec7a79b22014-05-26 08:06:19 -05001261 return expirationTimePoint;
1262}
akmhoque31d1d4b2014-05-05 22:08:14 -05001263
1264void
akmhoque2f423352014-06-03 11:49:35 -05001265Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001266{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001267 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001268 for (const auto& adj : m_adjLsdb) {
1269 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001270 }
1271}
1272
1273//-----utility function -----
1274bool
Nick Gordon727d4832017-10-13 18:04:25 -05001275Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001276{
Nick Gordon727d4832017-10-13 18:04:25 -05001277 switch (lsType) {
1278 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001279 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001280 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001281 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001282 case Lsa::Type::NAME:
1283 return doesNameLsaExist(key);
1284 default:
1285 return false;
akmhoque53353462014-04-22 08:43:45 -05001286 }
akmhoque53353462014-04-22 08:43:45 -05001287}
1288
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001289bool
Nick Gordon727d4832017-10-13 18:04:25 -05001290Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001291 const uint64_t& sequenceNumber) {
1292 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001293 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001294
Nick Gordon727d4832017-10-13 18:04:25 -05001295 switch (lsaType) {
1296 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001297 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001298 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001299 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001300 case Lsa::Type::NAME:
1301 return isNameLsaNew(lsaKey, sequenceNumber);
1302 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001303 return false;
1304 }
1305}
1306
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001307} // namespace nlsr