blob: 01c1ee00caba0d8f6f45619d855bb383430742dc [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 }
221 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600222 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500223 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
224 ndn::time::system_clock::now();
225 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500226 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500227 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500228 nlsa.getLsSeqNo(),
229 timeToExpire));
230 }
Nick G97e34942016-07-11 14:46:27 -0500231 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500232 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000233 NLSR_LOG_TRACE("Known name lsa");
234 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
235 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500236 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500237 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
238 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500239 chkNameLsa->writeLog();
240 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500241 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500242 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500243 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500244 // Obtain the set difference of the current and the incoming
245 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500246 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
247 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
248 std::list<ndn::Name> namesToAdd;
249 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
250 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500251 for (const auto& name : namesToAdd) {
252 chkNameLsa->addName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600253 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
254 if (name != m_confParam.getRouterPrefix()) {
255 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500256 }
257 }
258 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500259
260 chkNameLsa->getNpl().sort();
261
Nick G97e34942016-07-11 14:46:27 -0500262 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500263 std::list<ndn::Name> namesToRemove;
264 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
265 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500266 for (const auto& name : namesToRemove) {
267 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
268 chkNameLsa->removeName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600269 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
270 if (name != m_confParam.getRouterPrefix()) {
271 m_namePrefixTable.removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500272 }
273 }
274 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600275
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600276 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400277 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500278 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500279 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400280 chkNameLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500281 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500282 nlsa.getLsSeqNo(),
283 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500284 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500285 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500286 }
287 }
288 return true;
289}
290
291bool
292Lsdb::addNameLsa(NameLsa& nlsa)
293{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400294 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
295 std::bind(nameLsaCompareByKey, _1, nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500296 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500297 m_nameLsdb.push_back(nlsa);
298 return true;
299 }
300 return false;
301}
302
303bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500304Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500305{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400306 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
307 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500308 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500309 NLSR_LOG_DEBUG("Deleting Name Lsa");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400310 it->writeLog();
Nick G97e34942016-07-11 14:46:27 -0500311 // If the requested name LSA is not ours, we also need to remove
312 // its entries from the NPT.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400313 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
314 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600315
Nick Gordonf14ec352017-07-24 16:09:58 -0500316 for (const auto& name : it->getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600317 if (name != m_confParam.getRouterPrefix()) {
318 m_namePrefixTable.removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500319 }
320 }
321 }
322 m_nameLsdb.erase(it);
323 return true;
324 }
325 return false;
326}
327
328bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500329Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500330{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400331 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
332 std::bind(nameLsaCompareByKey, _1, key));
333 return it != m_nameLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500334}
335
336void
akmhoque2f423352014-06-03 11:49:35 -0500337Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500338{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500339 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500340 for (const auto& nlsa : m_nameLsdb) {
341 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500342 }
343}
344
Jiewen Tana0497d82015-02-02 21:59:18 -0800345const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500346Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800347{
348 return m_nameLsdb;
349}
350
akmhoque53353462014-04-22 08:43:45 -0500351// Cor LSA and LSDB related Functions start here
352
Nick G97e34942016-07-11 14:46:27 -0500353/*! \brief Compares whether an LSA object is the same as a key.
354 \param clsa The cor. LSA to check the identity of.
355 \param key The key of the publishing router to check against.
356*/
akmhoque53353462014-04-22 08:43:45 -0500357static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500358corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500359{
360 return clsa.getKey() == key;
361}
362
363bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500364Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500365{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600366 CoordinateLsa corLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500367 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500368 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600369 m_confParam.getCorR(),
370 m_confParam.getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500371
372 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600373 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500374 m_sequencingManager.increaseCorLsaSeq();
375 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500376 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500377 }
378
akmhoque31d1d4b2014-05-05 22:08:14 -0500379 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500380
akmhoque53353462014-04-22 08:43:45 -0500381 return true;
382}
383
akmhoqueb6450b12014-04-24 00:01:03 -0500384CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500385Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500386{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400387 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
388 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500389 if (it != m_corLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400390 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500391 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400392 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500393}
394
395bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500396Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500397{
akmhoqueb6450b12014-04-24 00:01:03 -0500398 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500399 // Is the coordinate LSA in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400400 if (clsa != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500401 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500402 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500403 return true;
404 }
akmhoque157b0a42014-05-13 00:26:37 -0500405 else {
akmhoque53353462014-04-22 08:43:45 -0500406 return false;
407 }
408 }
409 return true;
410}
411
Nick G97e34942016-07-11 14:46:27 -0500412 // Schedules a refresh/expire event in the scheduler.
413 // \param key The name of the router that published the LSA.
414 // \param seqNo the seq. no. associated with the LSA to check.
415 // \param expTime How long to wait before triggering the event.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400416ndn::scheduler::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500417Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500418 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500419{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400420 return m_scheduler.schedule(expTime + GRACE_PERIOD,
421 std::bind(&Lsdb::expireOrRefreshCoordinateLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500422}
423
424bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500425Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500426{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700427 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500428 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500429 // Checking whether the LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400430 if (chkCorLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500431 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
432 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500433 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500434 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500435
Nick Gordon5c467f02016-07-13 13:40:10 -0500436 // Register the LSA's origin router prefix
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600437 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
438 m_namePrefixTable.addEntry(clsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500439 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500440 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600441 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
442 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500443 }
Nick G97e34942016-07-11 14:46:27 -0500444 // Set the expiration time for the new LSA.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600445 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500446 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
447 ndn::time::system_clock::now();
448 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500449 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500450 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500451 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500452 }
Nick G97e34942016-07-11 14:46:27 -0500453 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500454 else {
455 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500456 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
457 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500458 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500459 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500460 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500461 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500462 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500463 chkCorLsa->setCorRadius(clsa.getCorRadius());
464 chkCorLsa->setCorTheta(clsa.getCorTheta());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600465 if (m_confParam.getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
466 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500467 }
468 }
Nick G97e34942016-07-11 14:46:27 -0500469 // If this is an LSA from another router, refresh its expiration time.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600470 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400471 auto duration = clsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500472 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500473 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400474 chkCorLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500475 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500476 clsa.getLsSeqNo(),
477 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500478 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500479 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500480 }
481 }
482 return true;
483}
484
485bool
akmhoqueb6450b12014-04-24 00:01:03 -0500486Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500487{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400488 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
489 std::bind(corLsaCompareByKey, _1, clsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500490 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500491 m_corLsdb.push_back(clsa);
492 return true;
493 }
494 return false;
495}
496
497bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500498Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500499{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400500 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
501 std::bind(corLsaCompareByKey, _1, key));
502
akmhoque157b0a42014-05-13 00:26:37 -0500503 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500504 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500505 it->writeLog();
506
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600507 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
508 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500509 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500510
akmhoque53353462014-04-22 08:43:45 -0500511 m_corLsdb.erase(it);
512 return true;
513 }
514 return false;
515}
516
517bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500518Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500519{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400520 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
521 std::bind(corLsaCompareByKey, _1, key));
522 return it != m_corLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500523}
524
525void
akmhoque2f423352014-06-03 11:49:35 -0500526Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500527{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500528 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500529 for (const auto& corLsa : m_corLsdb) {
530 corLsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500531 }
532}
533
Jiewen Tana0497d82015-02-02 21:59:18 -0800534const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500535Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800536{
537 return m_corLsdb;
538}
539
akmhoque53353462014-04-22 08:43:45 -0500540// Adj LSA and LSDB related function starts here
541
Nick G97e34942016-07-11 14:46:27 -0500542 /*! \brief Returns whether an adj. LSA object is from some router.
543 \param alsa The adj. LSA object.
544 \param key The router name that you want to compare the LSA with.
545 */
akmhoque53353462014-04-22 08:43:45 -0500546static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500547adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500548{
549 return alsa.getKey() == key;
550}
551
akmhoque53353462014-04-22 08:43:45 -0500552void
Vince Lehman50df6b72015-03-03 12:06:40 -0600553Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500554{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600555 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600556
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600557 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500558 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500559 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500560 return;
561 }
562
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600563 if (m_isBuildAdjLsaSheduled == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500564 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600565
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400566 m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600567 m_isBuildAdjLsaSheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600568 }
569}
570
571void
572Lsdb::buildAdjLsa()
573{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500574 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600575
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600576 m_isBuildAdjLsaSheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500577
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600578 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500579
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600580 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500581 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500582 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500583 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600584 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500585 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500586 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500587 }
Nick G97e34942016-07-11 14:46:27 -0500588 // We have no active neighbors, meaning no one can route through
589 // us. So delete our entry in the LSDB. This prevents this
590 // router from refreshing the LSA, eventually causing other
591 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500592 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500593 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500594 // Get this router's key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600595 ndn::Name key = m_confParam.getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500596 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500597
akmhoque31d1d4b2014-05-05 22:08:14 -0500598 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500599 // Recompute routing table after removal
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600600 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500601 }
Nick G97e34942016-07-11 14:46:27 -0500602 // In the case that during building the adj LSA, the FIB has to
603 // wait on an Interest response, the number of scheduled adj LSA
604 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600605 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500606 }
607 }
Nick G97e34942016-07-11 14:46:27 -0500608 // We are still waiting to know the adjacency status of some
609 // neighbor, so schedule a build for later (when all that has
610 // hopefully finished)
611 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600612 m_isBuildAdjLsaSheduled = true;
613 int schedulingTime = m_confParam.getInterestRetryNumber() *
614 m_confParam.getInterestResendTime();
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400615 m_scheduler.schedule(ndn::time::seconds(schedulingTime), [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500616 }
akmhoque53353462014-04-22 08:43:45 -0500617}
618
akmhoque53353462014-04-22 08:43:45 -0500619bool
620Lsdb::addAdjLsa(AdjLsa& alsa)
621{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400622 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
623 std::bind(adjLsaCompareByKey, _1, alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500624 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500625 m_adjLsdb.push_back(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600626 // Add any new name prefixes to the NPT
627 // Only add NPT entries if this is an adj LSA from another router.
628 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
629 // Pass the originating router as both the name to register and
630 // where it came from.
631 m_namePrefixTable.addEntry(alsa.getOrigRouter(), alsa.getOrigRouter());
632 }
akmhoque53353462014-04-22 08:43:45 -0500633 return true;
634 }
635 return false;
636}
637
akmhoqueb6450b12014-04-24 00:01:03 -0500638AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500639Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500640{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400641 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
642 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500643 if (it != m_adjLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400644 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500645 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400646 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500647}
648
akmhoque53353462014-04-22 08:43:45 -0500649bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500650Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500651{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400652 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500653 // If it is in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400654 if (adjLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500655 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500656 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500657 return true;
658 }
akmhoque157b0a42014-05-13 00:26:37 -0500659 else {
akmhoque53353462014-04-22 08:43:45 -0500660 return false;
661 }
662 }
663 return true;
664}
665
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400666ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500667Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
668 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500669{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400670 return m_scheduler.schedule(expTime + GRACE_PERIOD,
671 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500672}
673
674bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500675Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500676{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700677 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500678 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500679 // If this adj. LSA is not in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400680 if (chkAdjLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500681 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
682 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500683 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500684 addAdjLsa(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600685
686 m_routingTable.scheduleRoutingTableCalculation();
687 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500688 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
689 ndn::time::system_clock::now();
690 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500691 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400692 scheduleAdjLsaExpiration(alsa.getKey(), alsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500693 }
akmhoque157b0a42014-05-13 00:26:37 -0500694 else {
695 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500696 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
697 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500698 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500699 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500700 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500701 // If the new adj LSA has new content, update the contents of
702 // the LSDB entry. Additionally, since we've changed the
703 // contents of the LSDB, we have to schedule a routing
704 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500705 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500706 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500707 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600708 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500709 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600710 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400711 auto duration = alsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500712 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500713 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400714 chkAdjLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500715 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500716 alsa.getLsSeqNo(),
717 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500718 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500719 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500720 }
721 }
722 return true;
723}
724
725bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500726Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500727{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600728 AdjLsa adjLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500729 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500730 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600731 m_confParam.getAdjacencyList().getNumOfActiveNeighbor(),
732 m_confParam.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500733
Nick Gordon5c467f02016-07-13 13:40:10 -0500734 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600735 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500736 m_sequencingManager.increaseAdjLsaSeq();
737 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500738 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500739 }
Vince Lehman904c2412014-09-23 19:36:11 -0500740
Vince Lehman9d097802015-03-16 17:55:59 -0500741 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500742}
743
744bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500745Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500746{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400747 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
748 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500749 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500750 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600751 it->writeLog();
752 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
753 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
754 }
akmhoque53353462014-04-22 08:43:45 -0500755 m_adjLsdb.erase(it);
756 return true;
757 }
758 return false;
759}
760
761bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500762Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500763{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400764 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
765 std::bind(adjLsaCompareByKey, _1, key));
766 return it != m_adjLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500767}
768
Jiewen Tana0497d82015-02-02 21:59:18 -0800769const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500770Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500771{
772 return m_adjLsdb;
773}
774
Nick G97e34942016-07-11 14:46:27 -0500775 // This function determines whether a name LSA should be refreshed
776 // or expired. The conditions for getting refreshed are: it is still
777 // in the LSDB, it hasn't been updated by something else already (as
778 // evidenced by its seq. no.), and this is the originating router for
779 // the LSA. Is it let expire in all other cases.
780 // lsaKey is the key of the LSA's publishing router.
781 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500782void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500783Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500784{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500785 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
786 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500787 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500788 // If this name LSA exists in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400789 if (chkNameLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500790 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500791 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500792 if (chkNameLsa->getLsSeqNo() == seqNo) {
793 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500794 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
795 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500796 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500797 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500798 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500799 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500800 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500801 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500802 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500803 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500804 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700805 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500806 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500807 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500808 }
Nick G97e34942016-07-11 14:46:27 -0500809 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500810 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500811 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500812 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500813 }
814 }
815 }
816}
817
Nick G97e34942016-07-11 14:46:27 -0500818 // This function determines whether an adj. LSA should be refreshed
819 // or expired. The conditions for getting refreshed are: it is still
820 // in the LSDB, it hasn't been updated by something else already (as
821 // evidenced by its seq. no.), and this is the originating router for
822 // the LSA. Is it let expire in all other cases.
823 // lsaKey is the key of the LSA's publishing router.
824 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500825void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500826Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500827{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500828 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
829 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500830 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500831 // If this is a valid LSA
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400832 if (chkAdjLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500833 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500834 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500835 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500836 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500837 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500838 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
839 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500840 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500841 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500842 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500843 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500844 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500845 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500846 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500847 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500848 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700849 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500850 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500851 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500852 }
Nick G97e34942016-07-11 14:46:27 -0500853 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500854 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500855 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500856 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500857 }
Nick G97e34942016-07-11 14:46:27 -0500858 // We have changed the contents of the LSDB, so we have to
859 // schedule a routing calculation
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600860 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500861 }
862 }
863}
864
Nick G97e34942016-07-11 14:46:27 -0500865 // This function determines whether an adj. LSA should be refreshed
866 // or expired. The conditions for getting refreshed are: it is still
867 // in the LSDB, it hasn't been updated by something else already (as
868 // evidenced by its seq. no.), and this is the originating router for
869 // the LSA. It is let expire in all other cases.
870 // lsaKey is the key of the LSA's publishing router.
871 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500872void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500873Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500874 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500875{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500876 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
877 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500878 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500879 // Whether the LSA is in the LSDB or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400880 if (chkCorLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500881 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500882 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500883 if (chkCorLsa->getLsSeqNo() == seqNo) {
884 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500885 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
886 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500887 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500888 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600889 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500890 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500891 }
892
akmhoquec7a79b22014-05-26 08:06:19 -0500893 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500894 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500895 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500896 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500897 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
898 chkCorLsa->getKey(),
899 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700900 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500901 // Only sync coordinate LSAs if link-state routing is disabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600902 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500903 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500904 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500905 }
akmhoque53353462014-04-22 08:43:45 -0500906 }
Nick G97e34942016-07-11 14:46:27 -0500907 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500908 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500909 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500910 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500911 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600912 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
913 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500914 }
915 }
916 }
917}
918
akmhoque53353462014-04-22 08:43:45 -0500919void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700920Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500921 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500922{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600923 // increment SENT_LSA_INTEREST
924 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
925
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500926 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500927 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700928 }
Nick G97e34942016-07-11 14:46:27 -0500929 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500930 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500931 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500932 uint64_t seqNo = interestName[-1].toNumber();
933
Nick G97e34942016-07-11 14:46:27 -0500934 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500935 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
936 m_highestSeqNo[lsaName] = seqNo;
937 }
Nick G97e34942016-07-11 14:46:27 -0500938 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500939 else if (seqNo > m_highestSeqNo[lsaName]) {
940 m_highestSeqNo[lsaName] = seqNo;
941 }
Nick G97e34942016-07-11 14:46:27 -0500942 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500943 else if (seqNo < m_highestSeqNo[lsaName]) {
944 return;
945 }
946
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500947 ndn::Interest interest(interestName);
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500948 ndn::util::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600949 options.interestLifetime = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500950
dmcoomes5bcb39e2017-10-31 15:07:55 -0500951 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600952 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest,
953 m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500954
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500955 auto it = m_fetchers.insert(fetcher).first;
956
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600957 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
Ashlesh Gawande15052402018-12-12 20:20:00 -0600958 // Nlsr class subscribes to this to fetch certificates
959 afterSegmentValidatedSignal(data);
960
961 // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
962 auto lsaSegment = std::make_shared<const ndn::Data>(data);
963 m_lsaStorage.insert(*lsaSegment);
964 const ndn::Name& segmentName = lsaSegment->getName();
965 // Schedule deletion of the segment
966 m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
967 [this, segmentName] { m_lsaStorage.erase(segmentName); });
968 });
969
970 fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
971 m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
972 afterFetchLsa(bufferPtr, interestName);
973 m_fetchers.erase(it);
974 });
975
976 fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
977 onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
978 m_fetchers.erase(it);
979 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000980
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600981 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -0500982 Lsa::Type lsaType;
983 std::istringstream(interestName[-2].toUri()) >> lsaType;
984 switch (lsaType) {
985 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600986 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500987 break;
988 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600989 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500990 break;
991 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600992 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500993 break;
994 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -0500995 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600996 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500997}
998
999void
1000Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1001{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001002 ndn::Name interestName(interest.getName());
1003 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
1004
1005 if (interestName[-2].isVersion()) {
1006 // Interest for particular segment
1007 if (m_segmentPublisher.replyFromStore(interestName)) {
1008 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
1009 return;
1010 }
1011 // Remove version and segment
1012 interestName = interestName.getSubName(0, interestName.size() - 2);
1013 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
1014 }
1015
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001016 // increment RCV_LSA_INTEREST
1017 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1018
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001019 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001020 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001021
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001022 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001023 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001024 originRouter.append(interestName.getSubName(lsaPosition + 1,
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001025 interestName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001026
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001027 // if the interest is for this router's LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001028 if (originRouter == m_confParam.getRouterPrefix() && lsaPosition >= 0) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001029 uint64_t seqNo = interestName[-1].toNumber();
1030 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001031
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001032 std::string lsaType = interestName[-2].toUri();
1033 Lsa::Type interestedLsType;
1034 std::istringstream(lsaType) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001035
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001036 if (interestedLsType == Lsa::Type::NAME) {
1037 processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001038 }
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001039 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1040 processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
1041 }
1042 else if (interestedLsType == Lsa::Type::COORDINATE) {
1043 processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
1044 }
1045 else {
1046 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1047 }
1048 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001049 }
1050 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
Ashlesh Gawande15052402018-12-12 20:20:00 -06001051 std::shared_ptr<const ndn::Data> lsaSegment = m_lsaStorage.find(interest);
1052 if (lsaSegment) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001053 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001054 m_face.put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001055 }
akmhoque157b0a42014-05-13 00:26:37 -05001056 else {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001057 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001058 }
1059 }
1060}
1061
Nick G97e34942016-07-11 14:46:27 -05001062 // \brief Finds and sends a requested name LSA.
1063 // \param interest The interest that seeks the name LSA.
1064 // \param lsaKey The LSA that the Interest is seeking.
1065 // \param seqNo A sequence number to ensure that we are sending the
1066 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001067void
akmhoque31d1d4b2014-05-05 22:08:14 -05001068Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1069 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001070 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001071{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001072 // increment RCV_NAME_LSA_INTEREST
1073 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001074 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001075 NameLsa* nameLsa = findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001076 if (nameLsa != nullptr) {
1077 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001078 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001079 std::string content = nameLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001080 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1081 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001082 m_lsaRefreshTime, m_signingInfo);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001083
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001084 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001085 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001086 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001087 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001088 }
1089 }
1090 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001091 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001092 }
1093}
1094
Nick G97e34942016-07-11 14:46:27 -05001095 // \brief Finds and sends a requested adj. LSA.
1096 // \param interest The interest that seeks the adj. LSA.
1097 // \param lsaKey The LSA that the Interest is seeking.
1098 // \param seqNo A sequence number to ensure that we are sending the
1099 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001100void
1101Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1102 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001103 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001104{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001105 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001106 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001107 }
1108
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001109 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001110 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001111 AdjLsa* adjLsa = findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001112 if (adjLsa != nullptr) {
1113 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001114 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001115 std::string content = adjLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001116 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1117 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001118 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001119
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001120 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001121 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001122 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001123 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001124 }
1125 }
1126 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001127 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001128 }
1129}
1130
Nick G97e34942016-07-11 14:46:27 -05001131 // \brief Finds and sends a requested cor. LSA.
1132 // \param interest The interest that seeks the cor. LSA.
1133 // \param lsaKey The LSA that the Interest is seeking.
1134 // \param seqNo A sequence number to ensure that we are sending the
1135 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001136void
1137Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1138 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001139 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001140{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001141 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001142 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001143 }
1144
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001145 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001146 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001147 CoordinateLsa* corLsa = findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001148 if (corLsa != nullptr) {
1149 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001150 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001151 std::string content = corLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001152 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1153 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001154 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001155
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001156 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001157 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001158 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001159 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001160 }
1161 }
1162 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001163 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001164 }
1165}
1166
1167void
dmcoomes9f936662017-03-02 10:33:09 -06001168Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001169{
1170 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001171 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001172
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001173 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001174 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001175
akmhoque157b0a42014-05-13 00:26:37 -05001176 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001177
Nick G97e34942016-07-11 14:46:27 -05001178 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001179 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001180 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001181
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001182 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001183 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1184 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001185
Nick Gordon727d4832017-10-13 18:04:25 -05001186 Lsa::Type interestedLsType;
1187 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001188
Nick Gordon727d4832017-10-13 18:04:25 -05001189 if (interestedLsType == Lsa::Type::NAME) {
1190 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1191 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001192 }
Nick Gordon727d4832017-10-13 18:04:25 -05001193 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1194 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1195 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001196 }
Nick Gordon727d4832017-10-13 18:04:25 -05001197 else if (interestedLsType == Lsa::Type::COORDINATE) {
1198 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1199 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001200 }
akmhoque157b0a42014-05-13 00:26:37 -05001201 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001202 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001203 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001204
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001205 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001206 }
1207}
1208
1209void
1210Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001211 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001212{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001213 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001214 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001215 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001216 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001217 installNameLsa(nameLsa);
1218 }
akmhoque157b0a42014-05-13 00:26:37 -05001219 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001220 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001221 }
1222 }
1223}
1224
1225void
1226Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001227 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001228{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001229 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001230 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001231 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001232 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001233 installAdjLsa(adjLsa);
1234 }
akmhoque157b0a42014-05-13 00:26:37 -05001235 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001236 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001237 }
1238 }
1239}
1240
1241void
1242Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001243 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001244{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001245 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001246 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001247 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001248 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001249 installCoordinateLsa(corLsa);
1250 }
akmhoque157b0a42014-05-13 00:26:37 -05001251 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001252 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001253 }
1254 }
1255}
1256
akmhoquec7a79b22014-05-26 08:06:19 -05001257ndn::time::system_clock::TimePoint
1258Lsdb::getLsaExpirationTimePoint()
1259{
1260 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1261 expirationTimePoint = expirationTimePoint +
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001262 ndn::time::seconds(m_confParam.getRouterDeadInterval());
akmhoquec7a79b22014-05-26 08:06:19 -05001263 return expirationTimePoint;
1264}
akmhoque31d1d4b2014-05-05 22:08:14 -05001265
1266void
akmhoque2f423352014-06-03 11:49:35 -05001267Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001268{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001269 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001270 for (const auto& adj : m_adjLsdb) {
1271 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001272 }
1273}
1274
1275//-----utility function -----
1276bool
Nick Gordon727d4832017-10-13 18:04:25 -05001277Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001278{
Nick Gordon727d4832017-10-13 18:04:25 -05001279 switch (lsType) {
1280 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001281 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001282 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001283 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001284 case Lsa::Type::NAME:
1285 return doesNameLsaExist(key);
1286 default:
1287 return false;
akmhoque53353462014-04-22 08:43:45 -05001288 }
akmhoque53353462014-04-22 08:43:45 -05001289}
1290
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001291bool
Nick Gordon727d4832017-10-13 18:04:25 -05001292Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001293 const uint64_t& sequenceNumber) {
1294 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001295 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001296
Nick Gordon727d4832017-10-13 18:04:25 -05001297 switch (lsaType) {
1298 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001299 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001300 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001301 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001302 case Lsa::Type::NAME:
1303 return isNameLsaNew(lsaKey, sequenceNumber);
1304 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001305 return false;
1306 }
1307}
1308
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001309} // namespace nlsr