blob: fe0464e56cb7e06654a5ac4c828cefc4451bfe44 [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/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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
Saurab Dulal427e0122019-11-28 11:58:02 -060039Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 NamePrefixTable& namePrefixTable, RoutingTable& routingTable)
41 : m_face(face)
42 , m_scheduler(face.getIoService())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , m_confParam(confParam)
44 , m_namePrefixTable(namePrefixTable)
45 , m_routingTable(routingTable)
46 , m_sync(m_face,
Nick Gordon727d4832017-10-13 18:04:25 -050047 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050048 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050049 return isLsaNew(routerName, lsaType, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060050 }, m_confParam)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060051 , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
52 , m_thisRouterPrefix(m_confParam.getRouterPrefix().toUri())
53 , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
dulalsaurab82a34c22019-02-04 17:31:21 +000054 , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
Nick Gordon9eac4d92017-08-29 17:31:29 -050055 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050056 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
57 const ndn::Name& originRouter) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050058 ndn::Name lsaInterest{updateName};
59 lsaInterest.appendNumber(sequenceNumber);
60 expressInterest(lsaInterest, 0);
61 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060062 , m_segmentPublisher(m_face, keyChain)
63 , m_isBuildAdjLsaSheduled(false)
64 , m_adjBuildCount(0)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050065{
66}
67
Ashlesh Gawande744e4812018-08-22 16:26:24 -050068Lsdb::~Lsdb()
69{
70 for (const auto& sp : m_fetchers) {
71 sp->stop();
72 }
73}
74
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050075void
76Lsdb::onFetchLsaError(uint32_t errorCode,
77 const std::string& msg,
Ashlesh Gawande744e4812018-08-22 16:26:24 -050078 const ndn::Name& interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050079 uint32_t retransmitNo,
80 const ndn::time::steady_clock::TimePoint& deadline,
81 ndn::Name lsaName,
82 uint64_t seqNo)
83{
dmcoomes5bcb39e2017-10-31 15:07:55 -050084 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040085 << ", Message: " << msg);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050086
87 if (ndn::time::steady_clock::now() < deadline) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040088 auto it = m_highestSeqNo.find(lsaName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050089 if (it != m_highestSeqNo.end() && it->second == seqNo) {
90 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
91 // immediately since at the least the LSA Interest lifetime has elapsed.
92 // Otherwise, it is necessary to delay the Interest re-expression to prevent
93 // the potential for constant Interest flooding.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060094 ndn::time::seconds delay = m_confParam.getLsaInterestLifetime();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050095
96 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
97 delay = ndn::time::seconds(0);
98 }
99
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400100 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this,
101 interestName, retransmitNo + 1, deadline));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500102 }
103 }
104}
105
106void
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500107Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500108{
Saurab Dulal427e0122019-11-28 11:58:02 -0600109 auto data = std::make_shared<ndn::Data>(ndn::Name(interestName));
110 try {
111 data->setContent(ndn::Block(bufferPtr));
112 }
113 catch (const std::exception& e) {
114 NDN_LOG_ERROR("LSA content not recognized: " << e.what());
115 return;
116 }
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500117
dmcoomes5bcb39e2017-10-31 15:07:55 -0500118 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500119
120 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
121 uint64_t seqNo = interestName[-1].toNumber();
122
123 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
124 m_highestSeqNo[lsaName] = seqNo;
125 }
126 else if (seqNo > m_highestSeqNo[lsaName]) {
127 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500128 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500129 }
130 else if (seqNo < m_highestSeqNo[lsaName]) {
131 return;
132 }
133
134 onContentValidated(data);
135}
akmhoque53353462014-04-22 08:43:45 -0500136
Nick G97e34942016-07-11 14:46:27 -0500137 /*! \brief Compares if a name LSA is the same as the one specified by key
138
139 \param nlsa1 A name LSA object
140 \param key A key of an originating router to compare to nlsa1
141 */
akmhoque53353462014-04-22 08:43:45 -0500142static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500143nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500144{
145 return nlsa1.getKey() == key;
146}
147
akmhoque53353462014-04-22 08:43:45 -0500148bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500149Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500150{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600151 NameLsa nameLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500152 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500153 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600154 m_confParam.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500155 m_sequencingManager.increaseNameLsaSeq();
156
157 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500158 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500159
akmhoque31d1d4b2014-05-05 22:08:14 -0500160 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500161}
162
akmhoqueb6450b12014-04-24 00:01:03 -0500163NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500164Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500165{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400166 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
167 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500168 if (it != m_nameLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400169 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500170 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400171 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500172}
173
174bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500175Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500176{
akmhoqueb6450b12014-04-24 00:01:03 -0500177 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500178 // Is the name in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400179 if (nameLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500180 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500181 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500182 return true;
183 }
akmhoque157b0a42014-05-13 00:26:37 -0500184 else {
akmhoque53353462014-04-22 08:43:45 -0500185 return false;
186 }
187 }
188 return true;
189}
190
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400191ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500192Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
193 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500194{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400195 return m_scheduler.schedule(expTime + GRACE_PERIOD,
196 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500197}
198
199bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500200Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500201{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000202 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700203 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500204 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500205 // Determines if the name LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400206 if (chkNameLsa == nullptr) {
akmhoque53353462014-04-22 08:43:45 -0500207 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500208 NLSR_LOG_DEBUG("New Name LSA");
209 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500210 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500211
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000212 NLSR_LOG_TRACE("nlsa.getOrigRouter(): " << nlsa.getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600213 NLSR_LOG_TRACE("m_confParam.getRouterPrefix(): " << m_confParam.getRouterPrefix());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000214
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600215 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500216 // If this name LSA is from another router, add the advertised
217 // prefixes to the NPT.
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500218 m_namePrefixTable.addEntry(nlsa.getOrigRouter(), nlsa.getOrigRouter());
219
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500220 for (const auto& name : nlsa.getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600221 if (name != m_confParam.getRouterPrefix()) {
222 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500223 }
224 }
dulalsaurabd0816a32019-07-26 13:11:24 +0000225 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500226 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500227 }
dulalsaurabd0816a32019-07-26 13:11:24 +0000228
akmhoque31d1d4b2014-05-05 22:08:14 -0500229 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500230 nlsa.getLsSeqNo(),
231 timeToExpire));
232 }
Nick G97e34942016-07-11 14:46:27 -0500233 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500234 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000235 NLSR_LOG_TRACE("Known name lsa");
236 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
237 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500238 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500239 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
240 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500241 chkNameLsa->writeLog();
242 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500243 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500244 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500245 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500246 // Obtain the set difference of the current and the incoming
247 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500248 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
249 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
250 std::list<ndn::Name> namesToAdd;
251 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
252 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500253 for (const auto& name : namesToAdd) {
254 chkNameLsa->addName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600255 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
256 if (name != m_confParam.getRouterPrefix()) {
257 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500258 }
259 }
260 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500261
262 chkNameLsa->getNpl().sort();
263
Nick G97e34942016-07-11 14:46:27 -0500264 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500265 std::list<ndn::Name> namesToRemove;
266 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
267 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500268 for (const auto& name : namesToRemove) {
269 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
270 chkNameLsa->removeName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600271 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
272 if (name != m_confParam.getRouterPrefix()) {
273 m_namePrefixTable.removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500274 }
275 }
276 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600277
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600278 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400279 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500280 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500281 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400282 chkNameLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500283 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500284 nlsa.getLsSeqNo(),
285 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500286 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500287 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500288 }
289 }
290 return true;
291}
292
293bool
294Lsdb::addNameLsa(NameLsa& nlsa)
295{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400296 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
297 std::bind(nameLsaCompareByKey, _1, nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500298 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500299 m_nameLsdb.push_back(nlsa);
300 return true;
301 }
302 return false;
303}
304
305bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500306Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500307{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400308 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
309 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500310 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500311 NLSR_LOG_DEBUG("Deleting Name Lsa");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400312 it->writeLog();
Nick G97e34942016-07-11 14:46:27 -0500313 // If the requested name LSA is not ours, we also need to remove
314 // its entries from the NPT.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400315 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
316 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600317
Nick Gordonf14ec352017-07-24 16:09:58 -0500318 for (const auto& name : it->getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600319 if (name != m_confParam.getRouterPrefix()) {
320 m_namePrefixTable.removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500321 }
322 }
323 }
324 m_nameLsdb.erase(it);
325 return true;
326 }
327 return false;
328}
329
330bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500331Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500332{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400333 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
334 std::bind(nameLsaCompareByKey, _1, key));
335 return it != m_nameLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500336}
337
338void
akmhoque2f423352014-06-03 11:49:35 -0500339Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500340{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500341 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500342 for (const auto& nlsa : m_nameLsdb) {
343 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500344 }
345}
346
Jiewen Tana0497d82015-02-02 21:59:18 -0800347const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500348Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800349{
350 return m_nameLsdb;
351}
352
akmhoque53353462014-04-22 08:43:45 -0500353// Cor LSA and LSDB related Functions start here
354
Nick G97e34942016-07-11 14:46:27 -0500355/*! \brief Compares whether an LSA object is the same as a key.
356 \param clsa The cor. LSA to check the identity of.
357 \param key The key of the publishing router to check against.
358*/
akmhoque53353462014-04-22 08:43:45 -0500359static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500360corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500361{
362 return clsa.getKey() == key;
363}
364
365bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500366Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500367{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600368 CoordinateLsa corLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500369 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500370 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600371 m_confParam.getCorR(),
372 m_confParam.getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500373
374 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600375 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500376 m_sequencingManager.increaseCorLsaSeq();
377 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500378 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500379 }
380
akmhoque31d1d4b2014-05-05 22:08:14 -0500381 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500382
akmhoque53353462014-04-22 08:43:45 -0500383 return true;
384}
385
akmhoqueb6450b12014-04-24 00:01:03 -0500386CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500387Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500388{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400389 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
390 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500391 if (it != m_corLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400392 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500393 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400394 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500395}
396
397bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500398Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500399{
akmhoqueb6450b12014-04-24 00:01:03 -0500400 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500401 // Is the coordinate LSA in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400402 if (clsa != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500403 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500404 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500405 return true;
406 }
akmhoque157b0a42014-05-13 00:26:37 -0500407 else {
akmhoque53353462014-04-22 08:43:45 -0500408 return false;
409 }
410 }
411 return true;
412}
413
Nick G97e34942016-07-11 14:46:27 -0500414 // Schedules a refresh/expire event in the scheduler.
415 // \param key The name of the router that published the LSA.
416 // \param seqNo the seq. no. associated with the LSA to check.
417 // \param expTime How long to wait before triggering the event.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400418ndn::scheduler::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500419Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500420 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500421{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400422 return m_scheduler.schedule(expTime + GRACE_PERIOD,
423 std::bind(&Lsdb::expireOrRefreshCoordinateLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500424}
425
426bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500427Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500428{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700429 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500430 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500431 // Checking whether the LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400432 if (chkCorLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500433 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
434 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500435 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500436 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500437
Nick Gordon5c467f02016-07-13 13:40:10 -0500438 // Register the LSA's origin router prefix
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600439 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
440 m_namePrefixTable.addEntry(clsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500441 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500442 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600443 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
444 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500445 }
Nick G97e34942016-07-11 14:46:27 -0500446 // Set the expiration time for the new LSA.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600447 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500448 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
449 ndn::time::system_clock::now();
450 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500451 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500452 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500453 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500454 }
Nick G97e34942016-07-11 14:46:27 -0500455 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500456 else {
457 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500458 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
459 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500460 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500461 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500462 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500463 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500464 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500465 chkCorLsa->setCorRadius(clsa.getCorRadius());
466 chkCorLsa->setCorTheta(clsa.getCorTheta());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600467 if (m_confParam.getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
468 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500469 }
470 }
Nick G97e34942016-07-11 14:46:27 -0500471 // If this is an LSA from another router, refresh its expiration time.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600472 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400473 auto duration = clsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500474 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500475 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400476 chkCorLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500477 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500478 clsa.getLsSeqNo(),
479 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500480 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500481 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500482 }
483 }
484 return true;
485}
486
487bool
akmhoqueb6450b12014-04-24 00:01:03 -0500488Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500489{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400490 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
491 std::bind(corLsaCompareByKey, _1, clsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500492 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500493 m_corLsdb.push_back(clsa);
494 return true;
495 }
496 return false;
497}
498
499bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500500Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500501{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400502 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
503 std::bind(corLsaCompareByKey, _1, key));
504
akmhoque157b0a42014-05-13 00:26:37 -0500505 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500506 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500507 it->writeLog();
508
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600509 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
510 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500511 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500512
akmhoque53353462014-04-22 08:43:45 -0500513 m_corLsdb.erase(it);
514 return true;
515 }
516 return false;
517}
518
519bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500520Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500521{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400522 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
523 std::bind(corLsaCompareByKey, _1, key));
524 return it != m_corLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500525}
526
527void
akmhoque2f423352014-06-03 11:49:35 -0500528Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500529{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500530 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
531 return;
532 }
533
dmcoomes5bcb39e2017-10-31 15:07:55 -0500534 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500535 for (const auto& corLsa : m_corLsdb) {
536 corLsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500537 }
538}
539
Jiewen Tana0497d82015-02-02 21:59:18 -0800540const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500541Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800542{
543 return m_corLsdb;
544}
545
akmhoque53353462014-04-22 08:43:45 -0500546// Adj LSA and LSDB related function starts here
547
Nick G97e34942016-07-11 14:46:27 -0500548 /*! \brief Returns whether an adj. LSA object is from some router.
549 \param alsa The adj. LSA object.
550 \param key The router name that you want to compare the LSA with.
551 */
akmhoque53353462014-04-22 08:43:45 -0500552static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500553adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500554{
555 return alsa.getKey() == key;
556}
557
akmhoque53353462014-04-22 08:43:45 -0500558void
Vince Lehman50df6b72015-03-03 12:06:40 -0600559Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500560{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600561 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600562
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600563 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500564 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500565 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500566 return;
567 }
568
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500569 if (m_isBuildAdjLsaSheduled) {
570 NLSR_LOG_DEBUG("Rescheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
571 }
572 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500573 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600574 m_isBuildAdjLsaSheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600575 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500576 m_scheduledAdjLsaBuild = m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Vince Lehman50df6b72015-03-03 12:06:40 -0600577}
578
579void
580Lsdb::buildAdjLsa()
581{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500582 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600583
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600584 m_isBuildAdjLsaSheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500585
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600586 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500587
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600588 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500589 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500590 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500591 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600592 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500593 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500594 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500595 }
Nick G97e34942016-07-11 14:46:27 -0500596 // We have no active neighbors, meaning no one can route through
597 // us. So delete our entry in the LSDB. This prevents this
598 // router from refreshing the LSA, eventually causing other
599 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500600 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500601 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500602 // Get this router's key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600603 ndn::Name key = m_confParam.getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500604 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500605
akmhoque31d1d4b2014-05-05 22:08:14 -0500606 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500607 // Recompute routing table after removal
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600608 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500609 }
Nick G97e34942016-07-11 14:46:27 -0500610 // In the case that during building the adj LSA, the FIB has to
611 // wait on an Interest response, the number of scheduled adj LSA
612 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600613 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500614 }
615 }
Nick G97e34942016-07-11 14:46:27 -0500616 // We are still waiting to know the adjacency status of some
617 // neighbor, so schedule a build for later (when all that has
618 // hopefully finished)
619 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600620 m_isBuildAdjLsaSheduled = true;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500621 auto schedulingTime = ndn::time::seconds(m_confParam.getInterestRetryNumber() *
622 m_confParam.getInterestResendTime());
623 m_scheduledAdjLsaBuild = m_scheduler.schedule(schedulingTime, [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500624 }
akmhoque53353462014-04-22 08:43:45 -0500625}
626
akmhoque53353462014-04-22 08:43:45 -0500627bool
628Lsdb::addAdjLsa(AdjLsa& alsa)
629{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400630 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
631 std::bind(adjLsaCompareByKey, _1, alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500632 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500633 m_adjLsdb.push_back(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600634 // Add any new name prefixes to the NPT
635 // Only add NPT entries if this is an adj LSA from another router.
636 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
637 // Pass the originating router as both the name to register and
638 // where it came from.
639 m_namePrefixTable.addEntry(alsa.getOrigRouter(), alsa.getOrigRouter());
640 }
akmhoque53353462014-04-22 08:43:45 -0500641 return true;
642 }
643 return false;
644}
645
akmhoqueb6450b12014-04-24 00:01:03 -0500646AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500647Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500648{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400649 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
650 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500651 if (it != m_adjLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400652 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500653 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400654 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500655}
656
akmhoque53353462014-04-22 08:43:45 -0500657bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500658Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500659{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400660 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500661 // If it is in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400662 if (adjLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500663 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500664 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500665 return true;
666 }
akmhoque157b0a42014-05-13 00:26:37 -0500667 else {
akmhoque53353462014-04-22 08:43:45 -0500668 return false;
669 }
670 }
671 return true;
672}
673
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400674ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500675Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
676 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500677{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400678 return m_scheduler.schedule(expTime + GRACE_PERIOD,
679 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500680}
681
682bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500683Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500684{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700685 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500686 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500687 // If this adj. LSA is not in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400688 if (chkAdjLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500689 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
690 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500691 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500692 addAdjLsa(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600693
694 m_routingTable.scheduleRoutingTableCalculation();
695 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500696 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
697 ndn::time::system_clock::now();
698 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500699 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400700 scheduleAdjLsaExpiration(alsa.getKey(), alsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500701 }
akmhoque157b0a42014-05-13 00:26:37 -0500702 else {
703 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500704 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
705 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500706 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500707 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500708 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500709 // If the new adj LSA has new content, update the contents of
710 // the LSDB entry. Additionally, since we've changed the
711 // contents of the LSDB, we have to schedule a routing
712 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500713 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500714 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500715 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600716 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500717 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600718 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400719 auto duration = alsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500720 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500721 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400722 chkAdjLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500723 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500724 alsa.getLsSeqNo(),
725 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500726 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500727 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500728 }
729 }
730 return true;
731}
732
733bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500734Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500735{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600736 AdjLsa adjLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500737 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500738 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600739 m_confParam.getAdjacencyList().getNumOfActiveNeighbor(),
740 m_confParam.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500741
Nick Gordon5c467f02016-07-13 13:40:10 -0500742 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600743 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500744 m_sequencingManager.increaseAdjLsaSeq();
745 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500746 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500747 }
Vince Lehman904c2412014-09-23 19:36:11 -0500748
Vince Lehman9d097802015-03-16 17:55:59 -0500749 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500750}
751
752bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500753Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500754{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400755 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
756 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500757 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500758 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600759 it->writeLog();
760 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
761 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
762 }
akmhoque53353462014-04-22 08:43:45 -0500763 m_adjLsdb.erase(it);
764 return true;
765 }
766 return false;
767}
768
769bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500770Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500771{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400772 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
773 std::bind(adjLsaCompareByKey, _1, key));
774 return it != m_adjLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500775}
776
Jiewen Tana0497d82015-02-02 21:59:18 -0800777const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500778Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500779{
780 return m_adjLsdb;
781}
782
Nick G97e34942016-07-11 14:46:27 -0500783 // This function determines whether a name LSA should be refreshed
784 // or expired. The conditions for getting refreshed are: it is still
785 // in the LSDB, it hasn't been updated by something else already (as
786 // evidenced by its seq. no.), and this is the originating router for
787 // the LSA. Is it let expire in all other cases.
788 // lsaKey is the key of the LSA's publishing router.
789 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500790void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500791Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500792{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500793 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
794 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500795 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500796 // If this name LSA exists in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400797 if (chkNameLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500798 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500799 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500800 if (chkNameLsa->getLsSeqNo() == seqNo) {
801 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500802 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
803 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500804 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500805 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500806 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500807 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500808 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500809 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500810 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500811 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500812 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700813 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500814 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500815 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500816 }
Nick G97e34942016-07-11 14:46:27 -0500817 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500818 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500819 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500820 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500821 }
822 }
823 }
824}
825
Nick G97e34942016-07-11 14:46:27 -0500826 // This function determines whether an adj. LSA should be refreshed
827 // or expired. The conditions for getting refreshed are: it is still
828 // in the LSDB, it hasn't been updated by something else already (as
829 // evidenced by its seq. no.), and this is the originating router for
830 // the LSA. Is it let expire in all other cases.
831 // lsaKey is the key of the LSA's publishing router.
832 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500833void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500834Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500835{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500836 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
837 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500838 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500839 // If this is a valid LSA
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400840 if (chkAdjLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500841 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500842 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500843 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500844 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500845 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500846 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
847 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500848 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500849 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500850 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500851 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500852 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500853 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500854 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500855 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500856 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700857 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500858 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500859 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500860 }
Nick G97e34942016-07-11 14:46:27 -0500861 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500862 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500863 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500864 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500865 }
Nick G97e34942016-07-11 14:46:27 -0500866 // We have changed the contents of the LSDB, so we have to
867 // schedule a routing calculation
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600868 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500869 }
870 }
871}
872
Nick G97e34942016-07-11 14:46:27 -0500873 // This function determines whether an adj. LSA should be refreshed
874 // or expired. The conditions for getting refreshed are: it is still
875 // in the LSDB, it hasn't been updated by something else already (as
876 // evidenced by its seq. no.), and this is the originating router for
877 // the LSA. It is let expire in all other cases.
878 // lsaKey is the key of the LSA's publishing router.
879 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500880void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500881Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500882 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500883{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500884 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
885 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500886 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500887 // Whether the LSA is in the LSDB or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400888 if (chkCorLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500889 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500890 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500891 if (chkCorLsa->getLsSeqNo() == seqNo) {
892 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500893 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
894 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500895 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500896 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600897 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500898 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500899 }
900
akmhoquec7a79b22014-05-26 08:06:19 -0500901 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500902 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500903 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500904 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500905 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
906 chkCorLsa->getKey(),
907 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700908 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500909 // Only sync coordinate LSAs if link-state routing is disabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600910 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500911 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500912 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500913 }
akmhoque53353462014-04-22 08:43:45 -0500914 }
Nick G97e34942016-07-11 14:46:27 -0500915 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500916 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500917 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500918 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500919 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600920 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
921 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500922 }
923 }
924 }
925}
926
akmhoque53353462014-04-22 08:43:45 -0500927void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700928Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500929 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500930{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600931 // increment SENT_LSA_INTEREST
932 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
933
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500934 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500935 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700936 }
Nick G97e34942016-07-11 14:46:27 -0500937 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500938 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500939 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500940 uint64_t seqNo = interestName[-1].toNumber();
941
Nick G97e34942016-07-11 14:46:27 -0500942 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500943 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
944 m_highestSeqNo[lsaName] = seqNo;
945 }
Nick G97e34942016-07-11 14:46:27 -0500946 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500947 else if (seqNo > m_highestSeqNo[lsaName]) {
948 m_highestSeqNo[lsaName] = seqNo;
949 }
Nick G97e34942016-07-11 14:46:27 -0500950 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500951 else if (seqNo < m_highestSeqNo[lsaName]) {
952 return;
953 }
954
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500955 ndn::Interest interest(interestName);
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500956 ndn::util::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600957 options.interestLifetime = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500958
dmcoomes5bcb39e2017-10-31 15:07:55 -0500959 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600960 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest,
961 m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500962
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500963 auto it = m_fetchers.insert(fetcher).first;
964
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600965 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
Ashlesh Gawande15052402018-12-12 20:20:00 -0600966 // Nlsr class subscribes to this to fetch certificates
967 afterSegmentValidatedSignal(data);
968
969 // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
970 auto lsaSegment = std::make_shared<const ndn::Data>(data);
971 m_lsaStorage.insert(*lsaSegment);
972 const ndn::Name& segmentName = lsaSegment->getName();
973 // Schedule deletion of the segment
974 m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
975 [this, segmentName] { m_lsaStorage.erase(segmentName); });
976 });
977
978 fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
979 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
980 afterFetchLsa(bufferPtr, interestName);
981 m_fetchers.erase(it);
982 });
983
984 fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
985 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
986 m_fetchers.erase(it);
987 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000988
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600989 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -0500990 Lsa::Type lsaType;
991 std::istringstream(interestName[-2].toUri()) >> lsaType;
992 switch (lsaType) {
993 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600994 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500995 break;
996 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600997 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500998 break;
999 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001000 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001001 break;
1002 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001003 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001004 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001005}
1006
1007void
1008Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1009{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001010 ndn::Name interestName(interest.getName());
1011 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
1012
1013 if (interestName[-2].isVersion()) {
1014 // Interest for particular segment
1015 if (m_segmentPublisher.replyFromStore(interestName)) {
1016 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
1017 return;
1018 }
1019 // Remove version and segment
1020 interestName = interestName.getSubName(0, interestName.size() - 2);
1021 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
1022 }
1023
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001024 // increment RCV_LSA_INTEREST
1025 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1026
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001027 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001028 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001029
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001030 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001031 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001032 originRouter.append(interestName.getSubName(lsaPosition + 1,
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001033 interestName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001034
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001035 // if the interest is for this router's LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001036 if (originRouter == m_confParam.getRouterPrefix() && lsaPosition >= 0) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001037 uint64_t seqNo = interestName[-1].toNumber();
1038 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001039
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001040 std::string lsaType = interestName[-2].toUri();
1041 Lsa::Type interestedLsType;
1042 std::istringstream(lsaType) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001043
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001044 if (interestedLsType == Lsa::Type::NAME) {
1045 processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001046 }
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001047 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1048 processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
1049 }
1050 else if (interestedLsType == Lsa::Type::COORDINATE) {
1051 processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
1052 }
1053 else {
1054 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1055 }
1056 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001057 }
1058 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
Ashlesh Gawande15052402018-12-12 20:20:00 -06001059 std::shared_ptr<const ndn::Data> lsaSegment = m_lsaStorage.find(interest);
1060 if (lsaSegment) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001061 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001062 m_face.put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001063 }
akmhoque157b0a42014-05-13 00:26:37 -05001064 else {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001065 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001066 }
1067 }
1068}
1069
Nick G97e34942016-07-11 14:46:27 -05001070 // \brief Finds and sends a requested name LSA.
1071 // \param interest The interest that seeks the name LSA.
1072 // \param lsaKey The LSA that the Interest is seeking.
1073 // \param seqNo A sequence number to ensure that we are sending the
1074 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001075void
akmhoque31d1d4b2014-05-05 22:08:14 -05001076Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1077 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001078 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001079{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001080 // increment RCV_NAME_LSA_INTEREST
1081 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001082 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001083 NameLsa* nameLsa = findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001084 if (nameLsa != nullptr) {
1085 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001086 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001087 std::string content = nameLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001088 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1089 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Saurab Dulal427e0122019-11-28 11:58:02 -06001090 m_lsaRefreshTime, m_confParam.getSigningInfo());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001091
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001092 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001093 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001094 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001095 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001096 }
1097 }
1098 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001099 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001100 }
1101}
1102
Nick G97e34942016-07-11 14:46:27 -05001103 // \brief Finds and sends a requested adj. LSA.
1104 // \param interest The interest that seeks the adj. LSA.
1105 // \param lsaKey The LSA that the Interest is seeking.
1106 // \param seqNo A sequence number to ensure that we are sending the
1107 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001108void
1109Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1110 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001111 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001112{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001113 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001114 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001115 }
1116
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001117 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001118 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001119 AdjLsa* adjLsa = findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001120 if (adjLsa != nullptr) {
1121 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001122 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001123 std::string content = adjLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001124 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1125 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Saurab Dulal427e0122019-11-28 11:58:02 -06001126 m_lsaRefreshTime, m_confParam.getSigningInfo());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001127
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001128 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001129 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001130 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001131 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001132 }
1133 }
1134 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001135 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001136 }
1137}
1138
Nick G97e34942016-07-11 14:46:27 -05001139 // \brief Finds and sends a requested cor. LSA.
1140 // \param interest The interest that seeks the cor. LSA.
1141 // \param lsaKey The LSA that the Interest is seeking.
1142 // \param seqNo A sequence number to ensure that we are sending the
1143 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001144void
1145Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1146 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001147 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001148{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001149 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001150 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001151 }
1152
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001153 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001154 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001155 CoordinateLsa* corLsa = findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001156 if (corLsa != nullptr) {
1157 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001158 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001159 std::string content = corLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001160 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1161 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Saurab Dulal427e0122019-11-28 11:58:02 -06001162 m_lsaRefreshTime, m_confParam.getSigningInfo());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001163
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001164 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001165 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001166 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001167 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001168 }
1169 }
1170 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001171 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001172 }
1173}
1174
1175void
dmcoomes9f936662017-03-02 10:33:09 -06001176Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001177{
1178 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001179 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001180
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001181 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001182 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001183
akmhoque157b0a42014-05-13 00:26:37 -05001184 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001185
Nick G97e34942016-07-11 14:46:27 -05001186 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001187 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001188 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001189
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001190 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001191 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1192 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001193
Nick Gordon727d4832017-10-13 18:04:25 -05001194 Lsa::Type interestedLsType;
1195 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001196
Nick Gordon727d4832017-10-13 18:04:25 -05001197 if (interestedLsType == Lsa::Type::NAME) {
1198 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1199 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001200 }
Nick Gordon727d4832017-10-13 18:04:25 -05001201 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1202 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1203 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001204 }
Nick Gordon727d4832017-10-13 18:04:25 -05001205 else if (interestedLsType == Lsa::Type::COORDINATE) {
1206 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1207 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001208 }
akmhoque157b0a42014-05-13 00:26:37 -05001209 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001210 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001211 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001212
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001213 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001214 }
1215}
1216
1217void
1218Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001219 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001220{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001221 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001222 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001223 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001224 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001225 installNameLsa(nameLsa);
1226 }
akmhoque157b0a42014-05-13 00:26:37 -05001227 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001228 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001229 }
1230 }
1231}
1232
1233void
1234Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001235 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001236{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001237 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001238 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001239 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001240 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001241 installAdjLsa(adjLsa);
1242 }
akmhoque157b0a42014-05-13 00:26:37 -05001243 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001244 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001245 }
1246 }
1247}
1248
1249void
1250Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001251 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001252{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001253 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001254 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001255 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001256 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001257 installCoordinateLsa(corLsa);
1258 }
akmhoque157b0a42014-05-13 00:26:37 -05001259 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001260 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001261 }
1262 }
1263}
1264
akmhoquec7a79b22014-05-26 08:06:19 -05001265ndn::time::system_clock::TimePoint
1266Lsdb::getLsaExpirationTimePoint()
1267{
1268 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1269 expirationTimePoint = expirationTimePoint +
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001270 ndn::time::seconds(m_confParam.getRouterDeadInterval());
akmhoquec7a79b22014-05-26 08:06:19 -05001271 return expirationTimePoint;
1272}
akmhoque31d1d4b2014-05-05 22:08:14 -05001273
1274void
akmhoque2f423352014-06-03 11:49:35 -05001275Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001276{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -05001277 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
1278 return;
1279 }
1280
dmcoomes5bcb39e2017-10-31 15:07:55 -05001281 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001282 for (const auto& adj : m_adjLsdb) {
1283 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001284 }
1285}
1286
1287//-----utility function -----
1288bool
Nick Gordon727d4832017-10-13 18:04:25 -05001289Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001290{
Nick Gordon727d4832017-10-13 18:04:25 -05001291 switch (lsType) {
1292 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001293 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001294 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001295 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001296 case Lsa::Type::NAME:
1297 return doesNameLsaExist(key);
1298 default:
1299 return false;
akmhoque53353462014-04-22 08:43:45 -05001300 }
akmhoque53353462014-04-22 08:43:45 -05001301}
1302
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001303bool
Nick Gordon727d4832017-10-13 18:04:25 -05001304Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001305 const uint64_t& sequenceNumber) {
1306 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001307 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001308
Nick Gordon727d4832017-10-13 18:04:25 -05001309 switch (lsaType) {
1310 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001311 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001312 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001313 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001314 case Lsa::Type::NAME:
1315 return isNameLsaNew(lsaKey, sequenceNumber);
1316 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001317 return false;
1318 }
1319}
1320
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001321} // namespace nlsr