blob: 43b86bc95e259fe27ebcb35766493d5586ee2039 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 Chowdhuryc3ea26f2018-01-05 21:40:59 +000025#include "lsa-segment-storage.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050026#include "nlsr.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050027#include "utility/name-helper.hpp"
28
29#include <ndn-cxx/security/signing-helpers.hpp>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050030
akmhoque53353462014-04-22 08:43:45 -050031namespace nlsr {
32
dmcoomescf8d0ed2017-02-21 11:39:01 -060033INIT_LOGGER(Lsdb);
akmhoque674b0b12014-05-20 14:33:28 -050034
Jiewen Tana0497d82015-02-02 21:59:18 -080035const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050036const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Nick Gordone98480b2017-05-24 11:23:03 -050037const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
38 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050039
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050040Lsdb::Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050041 : m_nlsr(nlsr)
42 , m_scheduler(scheduler)
Nick Gordon9eac4d92017-08-29 17:31:29 -050043 , m_sync(m_nlsr.getNlsrFace(),
Nick Gordon727d4832017-10-13 18:04:25 -050044 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050045 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050046 return isLsaNew(routerName, lsaType, sequenceNumber);
47 }, m_nlsr.getConfParameter())
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060048 , m_lsaStorage(scheduler)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050049 , m_lsaRefreshTime(0)
50 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050051 , m_sequencingManager()
Nick Gordon9eac4d92017-08-29 17:31:29 -050052 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
Ashlesh Gawande939b6f82018-12-09 16:51:09 -060053 [this] (const ndn::Name& updateName, uint64_t sequenceNumber) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050054 ndn::Name lsaInterest{updateName};
55 lsaInterest.appendNumber(sequenceNumber);
56 expressInterest(lsaInterest, 0);
57 }))
Ashlesh Gawande939b6f82018-12-09 16:51:09 -060058 , m_segmentPublisher(m_nlsr.getNlsrFace(), m_nlsr.getKeyChain())
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050059{
60}
61
Ashlesh Gawande744e4812018-08-22 16:26:24 -050062Lsdb::~Lsdb()
63{
64 for (const auto& sp : m_fetchers) {
65 sp->stop();
66 }
67}
68
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050069void
70Lsdb::onFetchLsaError(uint32_t errorCode,
71 const std::string& msg,
Ashlesh Gawande744e4812018-08-22 16:26:24 -050072 const ndn::Name& interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050073 uint32_t retransmitNo,
74 const ndn::time::steady_clock::TimePoint& deadline,
75 ndn::Name lsaName,
76 uint64_t seqNo)
77{
dmcoomes5bcb39e2017-10-31 15:07:55 -050078 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050079 << ", Message: " << msg);
80
81 if (ndn::time::steady_clock::now() < deadline) {
82 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
83
84 if (it != m_highestSeqNo.end() && it->second == seqNo) {
85 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
86 // immediately since at the least the LSA Interest lifetime has elapsed.
87 // Otherwise, it is necessary to delay the Interest re-expression to prevent
88 // the potential for constant Interest flooding.
89 ndn::time::seconds delay = m_nlsr.getConfParameter().getLsaInterestLifetime();
90
91 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
92 delay = ndn::time::seconds(0);
93 }
94
95 m_scheduler.scheduleEvent(delay, std::bind(&Lsdb::expressInterest, this,
96 interestName, retransmitNo + 1, deadline));
97 }
98 }
99}
100
101void
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500102Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500103{
dmcoomes9f936662017-03-02 10:33:09 -0600104 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600105 data->setContent(ndn::Block(bufferPtr));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500106
dmcoomes5bcb39e2017-10-31 15:07:55 -0500107 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500108
109 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
110 uint64_t seqNo = interestName[-1].toNumber();
111
112 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
113 m_highestSeqNo[lsaName] = seqNo;
114 }
115 else if (seqNo > m_highestSeqNo[lsaName]) {
116 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500117 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500118 }
119 else if (seqNo < m_highestSeqNo[lsaName]) {
120 return;
121 }
122
123 onContentValidated(data);
124}
akmhoque53353462014-04-22 08:43:45 -0500125
126void
akmhoque31d1d4b2014-05-05 22:08:14 -0500127Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -0500128{
Vince Lehman7c603292014-09-11 17:48:16 -0500129 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -0500130}
131
Nick G97e34942016-07-11 14:46:27 -0500132 /*! \brief Compares if a name LSA is the same as the one specified by key
133
134 \param nlsa1 A name LSA object
135 \param key A key of an originating router to compare to nlsa1
136 */
akmhoque53353462014-04-22 08:43:45 -0500137static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500138nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500139{
140 return nlsa1.getKey() == key;
141}
142
akmhoque53353462014-04-22 08:43:45 -0500143bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500144Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500145{
akmhoque31d1d4b2014-05-05 22:08:14 -0500146 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500147 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500148 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500149 m_nlsr.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500150 m_sequencingManager.increaseNameLsaSeq();
151
152 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500153 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500154
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500156}
157
akmhoqueb6450b12014-04-24 00:01:03 -0500158NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500159Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500160{
161 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
162 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600163 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500164 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500165 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500166 }
akmhoqueb6450b12014-04-24 00:01:03 -0500167 return 0;
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
akmhoque157b0a42014-05-13 00:26:37 -0500175 if (nameLsaCheck != 0) {
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
187ndn::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{
Vince Lehman7c603292014-09-11 17:48:16 -0500191 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500192 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.
akmhoque157b0a42014-05-13 00:26:37 -0500202 if (chkNameLsa == 0) {
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());
209 NLSR_LOG_TRACE("m_nlsr.getConfParameter().getRouterPrefix(): " << m_nlsr.getConfParameter().getRouterPrefix());
210
akmhoque157b0a42014-05-13 00:26:37 -0500211 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().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.
akmhoque31d1d4b2014-05-05 22:08:14 -0500214 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
215 nlsa.getOrigRouter());
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500216 for (const auto& name : nlsa.getNpl().getNames()) {
217 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
218 m_nlsr.getNamePrefixTable().addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500219 }
220 }
221 }
akmhoque157b0a42014-05-13 00:26:37 -0500222 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().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);
akmhoque157b0a42014-05-13 00:26:37 -0500253 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500254 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
255 m_nlsr.getNamePrefixTable().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);
akmhoque157b0a42014-05-13 00:26:37 -0500269 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500270 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
271 m_nlsr.getNamePrefixTable().removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500272 }
273 }
274 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600275
akmhoque157b0a42014-05-13 00:26:37 -0500276 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500277 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
278 ndn::time::system_clock::now();
279 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500280 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500281 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
282 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500283 nlsa.getLsSeqNo(),
284 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500285 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500286 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500287 }
288 }
289 return true;
290}
291
292bool
293Lsdb::addNameLsa(NameLsa& nlsa)
294{
295 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
296 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600297 std::bind(nameLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500298 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500299 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500300 m_nameLsdb.push_back(nlsa);
301 return true;
302 }
303 return false;
304}
305
306bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500307Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500308{
309 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
310 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600311 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500312 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500313 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500314 (*it).writeLog();
Nick G97e34942016-07-11 14:46:27 -0500315 // If the requested name LSA is not ours, we also need to remove
316 // its entries from the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500317 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500318 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500319 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
320 (*it).getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500321 for (const auto& name : it->getNpl().getNames()) {
322 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
323 m_nlsr.getNamePrefixTable().removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500324 }
325 }
326 }
327 m_nameLsdb.erase(it);
328 return true;
329 }
330 return false;
331}
332
333bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500334Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500335{
336 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
337 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600338 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500339 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500340 return false;
341 }
342 return true;
343}
344
345void
akmhoque2f423352014-06-03 11:49:35 -0500346Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500347{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500348 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500349 for (const auto& nlsa : m_nameLsdb) {
350 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500351 }
352}
353
Jiewen Tana0497d82015-02-02 21:59:18 -0800354const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500355Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800356{
357 return m_nameLsdb;
358}
359
akmhoque53353462014-04-22 08:43:45 -0500360// Cor LSA and LSDB related Functions start here
361
Nick G97e34942016-07-11 14:46:27 -0500362/*! \brief Compares whether an LSA object is the same as a key.
363 \param clsa The cor. LSA to check the identity of.
364 \param key The key of the publishing router to check against.
365*/
akmhoque53353462014-04-22 08:43:45 -0500366static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500367corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500368{
369 return clsa.getKey() == key;
370}
371
372bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500373Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500374{
akmhoque31d1d4b2014-05-05 22:08:14 -0500375 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500376 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500377 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500378 m_nlsr.getConfParameter().getCorR(),
379 m_nlsr.getConfParameter().getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500380
381 // Sync coordinate LSAs if using HR or HR dry run.
382 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500383 m_sequencingManager.increaseCorLsaSeq();
384 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500385 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500386 }
387
akmhoque31d1d4b2014-05-05 22:08:14 -0500388 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500389
akmhoque53353462014-04-22 08:43:45 -0500390 return true;
391}
392
akmhoqueb6450b12014-04-24 00:01:03 -0500393CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500394Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500395{
akmhoqueb6450b12014-04-24 00:01:03 -0500396 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
397 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600398 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500399 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500400 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500401 }
akmhoqueb6450b12014-04-24 00:01:03 -0500402 return 0;
akmhoque53353462014-04-22 08:43:45 -0500403}
404
405bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500406Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500407{
akmhoqueb6450b12014-04-24 00:01:03 -0500408 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500409 // Is the coordinate LSA in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500410 if (clsa != 0) {
Nick G97e34942016-07-11 14:46:27 -0500411 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500412 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500413 return true;
414 }
akmhoque157b0a42014-05-13 00:26:37 -0500415 else {
akmhoque53353462014-04-22 08:43:45 -0500416 return false;
417 }
418 }
419 return true;
420}
421
Nick G97e34942016-07-11 14:46:27 -0500422 // Schedules a refresh/expire event in the scheduler.
423 // \param key The name of the router that published the LSA.
424 // \param seqNo the seq. no. associated with the LSA to check.
425 // \param expTime How long to wait before triggering the event.
akmhoque53353462014-04-22 08:43:45 -0500426ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500427Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500428 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500429{
Vince Lehman7c603292014-09-11 17:48:16 -0500430 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500431 std::bind(&Lsdb::expireOrRefreshCoordinateLsa,
Vince Lehman7c603292014-09-11 17:48:16 -0500432 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500433}
434
435bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500436Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500437{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700438 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500439 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500440 // Checking whether the LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500441 if (chkCorLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500442 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
443 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500444 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500445 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500446
Nick Gordon5c467f02016-07-13 13:40:10 -0500447 // Register the LSA's origin router prefix
akmhoque157b0a42014-05-13 00:26:37 -0500448 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500449 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
450 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500451 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500452 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500453 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500454 }
Nick G97e34942016-07-11 14:46:27 -0500455 // Set the expiration time for the new LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500456 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500457 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
458 ndn::time::system_clock::now();
459 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500460 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500461 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500462 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500463 }
Nick G97e34942016-07-11 14:46:27 -0500464 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500465 else {
466 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500467 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
468 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500469 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500470 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500471 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500472 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500473 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500474 chkCorLsa->setCorRadius(clsa.getCorRadius());
475 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500476 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500477 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500478 }
479 }
Nick G97e34942016-07-11 14:46:27 -0500480 // If this is an LSA from another router, refresh its expiration time.
akmhoque157b0a42014-05-13 00:26:37 -0500481 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500482 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
483 ndn::time::system_clock::now();
484 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500485 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500486 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
487 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500488 clsa.getLsSeqNo(),
489 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500490 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500491 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500492 }
493 }
494 return true;
495}
496
497bool
akmhoqueb6450b12014-04-24 00:01:03 -0500498Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500499{
akmhoqueb6450b12014-04-24 00:01:03 -0500500 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
501 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600502 std::bind(corLsaCompareByKey, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500503 clsa.getKey()));
504 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500505 m_corLsdb.push_back(clsa);
506 return true;
507 }
508 return false;
509}
510
511bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500512Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500513{
akmhoqueb6450b12014-04-24 00:01:03 -0500514 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
515 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600516 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500517 _1, key));
518 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500519 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500520 it->writeLog();
521
522 if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
523 m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500524 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500525
akmhoque53353462014-04-22 08:43:45 -0500526 m_corLsdb.erase(it);
527 return true;
528 }
529 return false;
530}
531
532bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500533Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500534{
akmhoqueb6450b12014-04-24 00:01:03 -0500535 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
536 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600537 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500538 _1, key));
539 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500540 return false;
541 }
542 return true;
543}
544
545void
akmhoque2f423352014-06-03 11:49:35 -0500546Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500547{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500548 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500549 for (const auto& corLsa : m_corLsdb) {
550 corLsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500551 }
552}
553
Jiewen Tana0497d82015-02-02 21:59:18 -0800554const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500555Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800556{
557 return m_corLsdb;
558}
559
akmhoque53353462014-04-22 08:43:45 -0500560// Adj LSA and LSDB related function starts here
561
Nick G97e34942016-07-11 14:46:27 -0500562 /*! \brief Returns whether an adj. LSA object is from some router.
563 \param alsa The adj. LSA object.
564 \param key The router name that you want to compare the LSA with.
565 */
akmhoque53353462014-04-22 08:43:45 -0500566static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500567adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500568{
569 return alsa.getKey() == key;
570}
571
akmhoque53353462014-04-22 08:43:45 -0500572void
Vince Lehman50df6b72015-03-03 12:06:40 -0600573Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500574{
Vince Lehman50df6b72015-03-03 12:06:40 -0600575 m_nlsr.incrementAdjBuildCount();
576
Nick Gordon5c467f02016-07-13 13:40:10 -0500577 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
578 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500579 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500580 return;
581 }
582
Vince Lehman50df6b72015-03-03 12:06:40 -0600583 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500584 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600585
dmcoomes9f936662017-03-02 10:33:09 -0600586 m_scheduler.scheduleEvent(m_adjLsaBuildInterval, std::bind(&Lsdb::buildAdjLsa, this));
Vince Lehman50df6b72015-03-03 12:06:40 -0600587 m_nlsr.setIsBuildAdjLsaSheduled(true);
588 }
589}
590
591void
592Lsdb::buildAdjLsa()
593{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500594 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600595
akmhoque674b0b12014-05-20 14:33:28 -0500596 m_nlsr.setIsBuildAdjLsaSheduled(false);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500597
598 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
599
akmhoque31d1d4b2014-05-05 22:08:14 -0500600 int adjBuildCount = m_nlsr.getAdjBuildCount();
Nick G97e34942016-07-11 14:46:27 -0500601 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500602 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500603 // It only makes sense to do the adjLsa build if we have neighbors
akmhoque157b0a42014-05-13 00:26:37 -0500604 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500605 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500606 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500607 }
Nick G97e34942016-07-11 14:46:27 -0500608 // We have no active neighbors, meaning no one can route through
609 // us. So delete our entry in the LSDB. This prevents this
610 // router from refreshing the LSA, eventually causing other
611 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500612 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500613 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500614 // Get this router's key
akmhoque31d1d4b2014-05-05 22:08:14 -0500615 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500616 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500617
akmhoque31d1d4b2014-05-05 22:08:14 -0500618 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500619 // Recompute routing table after removal
akmhoque31d1d4b2014-05-05 22:08:14 -0500620 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500621 }
Nick G97e34942016-07-11 14:46:27 -0500622 // In the case that during building the adj LSA, the FIB has to
623 // wait on an Interest response, the number of scheduled adj LSA
624 // builds could change, so we shouldn't just set it to 0.
akmhoque31d1d4b2014-05-05 22:08:14 -0500625 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500626 }
627 }
Nick G97e34942016-07-11 14:46:27 -0500628 // We are still waiting to know the adjacency status of some
629 // neighbor, so schedule a build for later (when all that has
630 // hopefully finished)
631 else {
632 m_nlsr.setIsBuildAdjLsaSheduled(true);
633 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
634 m_nlsr.getConfParameter().getInterestResendTime();
635 m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
dmcoomes9f936662017-03-02 10:33:09 -0600636 std::bind(&Lsdb::buildAdjLsa, this));
Nick G97e34942016-07-11 14:46:27 -0500637 }
akmhoque53353462014-04-22 08:43:45 -0500638}
639
akmhoque53353462014-04-22 08:43:45 -0500640bool
641Lsdb::addAdjLsa(AdjLsa& alsa)
642{
643 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
644 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600645 std::bind(adjLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500646 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500647 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500648 m_adjLsdb.push_back(alsa);
649 return true;
650 }
651 return false;
652}
653
akmhoqueb6450b12014-04-24 00:01:03 -0500654AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500655Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500656{
657 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
658 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600659 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500660 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500661 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500662 }
akmhoqueb6450b12014-04-24 00:01:03 -0500663 return 0;
akmhoque53353462014-04-22 08:43:45 -0500664}
665
akmhoque53353462014-04-22 08:43:45 -0500666bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500667Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500668{
akmhoqueb6450b12014-04-24 00:01:03 -0500669 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500670 // If it is in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500671 if (adjLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500672 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500673 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500674 return true;
675 }
akmhoque157b0a42014-05-13 00:26:37 -0500676 else {
akmhoque53353462014-04-22 08:43:45 -0500677 return false;
678 }
679 }
680 return true;
681}
682
akmhoque53353462014-04-22 08:43:45 -0500683ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500684Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
685 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500686{
Vince Lehman7c603292014-09-11 17:48:16 -0500687 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500688 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500689}
690
691bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500692Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500693{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700694 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500695 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500696 // If this adj. LSA is not in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500697 if (chkAdjLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500698 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
699 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500700 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500701 addAdjLsa(alsa);
Nick G97e34942016-07-11 14:46:27 -0500702 // Add any new name prefixes to the NPT
akmhoque31d1d4b2014-05-05 22:08:14 -0500703 alsa.addNptEntries(m_nlsr);
704 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500705 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500706 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
707 ndn::time::system_clock::now();
708 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500709 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500710 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500711 alsa.getLsSeqNo(), timeToExpire);
712 }
akmhoque157b0a42014-05-13 00:26:37 -0500713 else {
714 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500715 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
716 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500717 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500718 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500719 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500720 // If the new adj LSA has new content, update the contents of
721 // the LSDB entry. Additionally, since we've changed the
722 // contents of the LSDB, we have to schedule a routing
723 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500724 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500725 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500726 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500727 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500728 }
akmhoque157b0a42014-05-13 00:26:37 -0500729 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500730 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
731 ndn::time::system_clock::now();
732 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500733 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500734 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
735 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500736 alsa.getLsSeqNo(),
737 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500738 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500739 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500740 }
741 }
742 return true;
743}
744
745bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500746Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500747{
akmhoque31d1d4b2014-05-05 22:08:14 -0500748 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500749 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500750 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500751 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
752 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500753
Nick Gordon5c467f02016-07-13 13:40:10 -0500754 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
755 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500756 m_sequencingManager.increaseAdjLsaSeq();
757 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500758 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500759 }
Vince Lehman904c2412014-09-23 19:36:11 -0500760
Vince Lehman9d097802015-03-16 17:55:59 -0500761 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500762}
763
764bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500765Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500766{
767 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
768 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600769 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500770 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500771 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500772 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500773 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500774 m_adjLsdb.erase(it);
775 return true;
776 }
777 return false;
778}
779
780bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500781Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500782{
783 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
784 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600785 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500786 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500787 return false;
788 }
789 return true;
790}
791
Jiewen Tana0497d82015-02-02 21:59:18 -0800792const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500793Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500794{
795 return m_adjLsdb;
796}
797
798void
Nick Gordone98480b2017-05-24 11:23:03 -0500799Lsdb::setLsaRefreshTime(const ndn::time::seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500800{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700801 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500802}
803
804void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500805Lsdb::setThisRouterPrefix(std::string trp)
akmhoque53353462014-04-22 08:43:45 -0500806{
807 m_thisRouterPrefix = trp;
808}
809
Nick G97e34942016-07-11 14:46:27 -0500810 // This function determines whether a name LSA should be refreshed
811 // or expired. The conditions for getting refreshed are: it is still
812 // in the LSDB, it hasn't been updated by something else already (as
813 // evidenced by its seq. no.), and this is the originating router for
814 // the LSA. Is it let expire in all other cases.
815 // lsaKey is the key of the LSA's publishing router.
816 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500817void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500818Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500819{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500820 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
821 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500822 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500823 // If this name LSA exists in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500824 if (chkNameLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500825 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500826 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500827 if (chkNameLsa->getLsSeqNo() == seqNo) {
828 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500829 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
830 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500831 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500832 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500833 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500834 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500835 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500836 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500837 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500838 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500839 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700840 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500841 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500842 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500843 }
Nick G97e34942016-07-11 14:46:27 -0500844 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500845 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500846 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500847 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500848 }
849 }
850 }
851}
852
Nick G97e34942016-07-11 14:46:27 -0500853 // This function determines whether an adj. LSA should be refreshed
854 // or expired. The conditions for getting refreshed are: it is still
855 // in the LSDB, it hasn't been updated by something else already (as
856 // evidenced by its seq. no.), and this is the originating router for
857 // the LSA. Is it let expire in all other cases.
858 // lsaKey is the key of the LSA's publishing router.
859 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500860void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500861Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500862{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500863 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
864 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500865 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500866 // If this is a valid LSA
akmhoque157b0a42014-05-13 00:26:37 -0500867 if (chkAdjLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500868 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500869 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500870 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500871 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500872 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500873 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
874 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500875 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500876 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500877 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500878 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500879 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500880 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500881 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500882 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500883 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700884 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500885 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500886 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500887 }
Nick G97e34942016-07-11 14:46:27 -0500888 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500889 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500890 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500891 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500892 }
Nick G97e34942016-07-11 14:46:27 -0500893 // We have changed the contents of the LSDB, so we have to
894 // schedule a routing calculation
akmhoque31d1d4b2014-05-05 22:08:14 -0500895 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500896 }
897 }
898}
899
Nick G97e34942016-07-11 14:46:27 -0500900 // This function determines whether an adj. LSA should be refreshed
901 // or expired. The conditions for getting refreshed are: it is still
902 // in the LSDB, it hasn't been updated by something else already (as
903 // evidenced by its seq. no.), and this is the originating router for
904 // the LSA. It is let expire in all other cases.
905 // lsaKey is the key of the LSA's publishing router.
906 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500907void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500908Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500909 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500910{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500911 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
912 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500913 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500914 // Whether the LSA is in the LSDB or not.
akmhoque157b0a42014-05-13 00:26:37 -0500915 if (chkCorLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500916 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500917 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500918 if (chkCorLsa->getLsSeqNo() == seqNo) {
919 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500920 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
921 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500922 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500923 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Nick Gordon5c467f02016-07-13 13:40:10 -0500924 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500925 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500926 }
927
akmhoquec7a79b22014-05-26 08:06:19 -0500928 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500929 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500930 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500931 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500932 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
933 chkCorLsa->getKey(),
934 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700935 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500936 // Only sync coordinate LSAs if link-state routing is disabled
937 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500938 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500939 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500940 }
akmhoque53353462014-04-22 08:43:45 -0500941 }
Nick G97e34942016-07-11 14:46:27 -0500942 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500943 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500944 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500945 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500946 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500947 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500948 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500949 }
950 }
951 }
952}
953
akmhoque53353462014-04-22 08:43:45 -0500954void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700955Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500956 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500957{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600958 // increment SENT_LSA_INTEREST
959 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
960
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500961 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500962 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700963 }
Nick G97e34942016-07-11 14:46:27 -0500964 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500965 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500966 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500967 uint64_t seqNo = interestName[-1].toNumber();
968
Nick G97e34942016-07-11 14:46:27 -0500969 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500970 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
971 m_highestSeqNo[lsaName] = seqNo;
972 }
Nick G97e34942016-07-11 14:46:27 -0500973 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500974 else if (seqNo > m_highestSeqNo[lsaName]) {
975 m_highestSeqNo[lsaName] = seqNo;
976 }
Nick G97e34942016-07-11 14:46:27 -0500977 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500978 else if (seqNo < m_highestSeqNo[lsaName]) {
979 return;
980 }
981
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500982 ndn::Interest interest(interestName);
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500983 ndn::util::SegmentFetcher::Options options;
984 options.interestLifetime = m_nlsr.getConfParameter().getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500985
dmcoomes5bcb39e2017-10-31 15:07:55 -0500986 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500987 auto fetcher = ndn::util::SegmentFetcher::start(m_nlsr.getNlsrFace(),
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500988 interest, m_nlsr.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500989
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500990 auto it = m_fetchers.insert(fetcher).first;
991
992 fetcher->onComplete.connect([this, interestName, it] (ndn::ConstBufferPtr bufferPtr) {
993 afterFetchLsa(bufferPtr, interestName);
994 m_fetchers.erase(it);
995 });
996
laqinfan7c13ba52018-01-15 21:17:24 +0000997 fetcher->onError.connect([this, interestName, timeoutCount, deadline, lsaName, seqNo, it]
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500998 (uint32_t errorCode, const std::string& msg) {
999 onFetchLsaError(errorCode, msg, interestName,
1000 timeoutCount, deadline, lsaName, seqNo);
1001 m_fetchers.erase(it);
1002 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001003
1004 m_lsaStorage.connectToFetcher(*fetcher);
1005 m_nlsr.connectToFetcher(*fetcher);
1006
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001007 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001008 Lsa::Type lsaType;
1009 std::istringstream(interestName[-2].toUri()) >> lsaType;
1010 switch (lsaType) {
1011 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001012 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001013 break;
1014 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001015 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001016 break;
1017 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001018 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001019 break;
1020 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001021 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001022 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001023}
1024
1025void
1026Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1027{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001028 ndn::Name interestName(interest.getName());
1029 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
1030
1031 if (interestName[-2].isVersion()) {
1032 // Interest for particular segment
1033 if (m_segmentPublisher.replyFromStore(interestName)) {
1034 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
1035 return;
1036 }
1037 // Remove version and segment
1038 interestName = interestName.getSubName(0, interestName.size() - 2);
1039 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
1040 }
1041
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001042 // increment RCV_LSA_INTEREST
1043 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1044
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001045 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001046 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001047
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001048 // Forms the name of the router that the Interest packet came from.
1049 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
1050 originRouter.append(interestName.getSubName(lsaPosition + 1,
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001051 interestName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001052
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001053 // if the interest is for this router's LSA
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001054 if (originRouter == m_nlsr.getConfParameter().getRouterPrefix() && lsaPosition >= 0) {
1055 uint64_t seqNo = interestName[-1].toNumber();
1056 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001057
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001058 std::string lsaType = interestName[-2].toUri();
1059 Lsa::Type interestedLsType;
1060 std::istringstream(lsaType) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001061
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001062 if (interestedLsType == Lsa::Type::NAME) {
1063 processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001064 }
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001065 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1066 processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
1067 }
1068 else if (interestedLsType == Lsa::Type::COORDINATE) {
1069 processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
1070 }
1071 else {
1072 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1073 }
1074 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001075 }
1076 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
1077 const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
1078 if (lsaSegment != nullptr) {
1079 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
1080 m_nlsr.getNlsrFace().put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001081 }
akmhoque157b0a42014-05-13 00:26:37 -05001082 else {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001083 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001084 }
1085 }
1086}
1087
Nick G97e34942016-07-11 14:46:27 -05001088 // \brief Finds and sends a requested name LSA.
1089 // \param interest The interest that seeks the name LSA.
1090 // \param lsaKey The LSA that the Interest is seeking.
1091 // \param seqNo A sequence number to ensure that we are sending the
1092 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001093void
akmhoque31d1d4b2014-05-05 22:08:14 -05001094Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1095 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001096 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001097{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001098 // increment RCV_NAME_LSA_INTEREST
1099 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001100 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001101 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001102 if (nameLsa != nullptr) {
1103 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001104 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001105 std::string content = nameLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001106 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1107 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
1108 m_lsaRefreshTime, m_nlsr.getSigningInfo());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001109
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001110 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001111 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001112 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001113 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001114 }
1115 }
1116 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001117 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001118 }
1119}
1120
Nick G97e34942016-07-11 14:46:27 -05001121 // \brief Finds and sends a requested adj. LSA.
1122 // \param interest The interest that seeks the adj. LSA.
1123 // \param lsaKey The LSA that the Interest is seeking.
1124 // \param seqNo A sequence number to ensure that we are sending the
1125 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001126void
1127Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1128 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001129 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001130{
Nick Gordon5c467f02016-07-13 13:40:10 -05001131 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001132 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001133 }
1134
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001135 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001136 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001137 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001138 if (adjLsa != nullptr) {
1139 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001140 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001141 std::string content = adjLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001142 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1143 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
1144 m_lsaRefreshTime, m_nlsr.getSigningInfo());
1145
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001146 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001147 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001148 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001149 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001150 }
1151 }
1152 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001153 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001154 }
1155}
1156
Nick G97e34942016-07-11 14:46:27 -05001157 // \brief Finds and sends a requested cor. LSA.
1158 // \param interest The interest that seeks the cor. LSA.
1159 // \param lsaKey The LSA that the Interest is seeking.
1160 // \param seqNo A sequence number to ensure that we are sending the
1161 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001162void
1163Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1164 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001165 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001166{
Nick Gordon5c467f02016-07-13 13:40:10 -05001167 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001168 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001169 }
1170
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001171 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001172 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001173 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001174 if (corLsa != nullptr) {
1175 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001176 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001177 std::string content = corLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001178 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1179 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
1180 m_lsaRefreshTime, m_nlsr.getSigningInfo());
1181
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001182 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001183 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001184 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001185 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001186 }
1187 }
1188 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001189 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001190 }
1191}
1192
1193void
dmcoomes9f936662017-03-02 10:33:09 -06001194Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001195{
1196 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001197 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001198
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001199 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001200 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001201
akmhoque157b0a42014-05-13 00:26:37 -05001202 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001203
Nick G97e34942016-07-11 14:46:27 -05001204 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001205 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001206 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001207
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001208 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001209 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1210 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001211
Nick Gordon727d4832017-10-13 18:04:25 -05001212 Lsa::Type interestedLsType;
1213 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001214
Nick Gordon727d4832017-10-13 18:04:25 -05001215 if (interestedLsType == Lsa::Type::NAME) {
1216 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1217 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001218 }
Nick Gordon727d4832017-10-13 18:04:25 -05001219 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1220 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1221 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001222 }
Nick Gordon727d4832017-10-13 18:04:25 -05001223 else if (interestedLsType == Lsa::Type::COORDINATE) {
1224 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1225 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001226 }
akmhoque157b0a42014-05-13 00:26:37 -05001227 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001228 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001229 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001230
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001231 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001232 }
1233}
1234
1235void
1236Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001237 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001238{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001239 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001240 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001241 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001242 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001243 installNameLsa(nameLsa);
1244 }
akmhoque157b0a42014-05-13 00:26:37 -05001245 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001246 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001247 }
1248 }
1249}
1250
1251void
1252Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001253 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001254{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001255 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001256 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001257 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001258 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001259 installAdjLsa(adjLsa);
1260 }
akmhoque157b0a42014-05-13 00:26:37 -05001261 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001262 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001263 }
1264 }
1265}
1266
1267void
1268Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001269 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001270{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001271 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001272 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001273 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001274 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001275 installCoordinateLsa(corLsa);
1276 }
akmhoque157b0a42014-05-13 00:26:37 -05001277 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001278 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001279 }
1280 }
1281}
1282
akmhoquec7a79b22014-05-26 08:06:19 -05001283ndn::time::system_clock::TimePoint
1284Lsdb::getLsaExpirationTimePoint()
1285{
1286 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1287 expirationTimePoint = expirationTimePoint +
1288 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1289 return expirationTimePoint;
1290}
akmhoque31d1d4b2014-05-05 22:08:14 -05001291
1292void
akmhoque2f423352014-06-03 11:49:35 -05001293Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001294{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001295 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001296 for (const auto& adj : m_adjLsdb) {
1297 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001298 }
1299}
1300
1301//-----utility function -----
1302bool
Nick Gordon727d4832017-10-13 18:04:25 -05001303Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001304{
Nick Gordon727d4832017-10-13 18:04:25 -05001305 switch (lsType) {
1306 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001307 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001308 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001309 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001310 case Lsa::Type::NAME:
1311 return doesNameLsaExist(key);
1312 default:
1313 return false;
akmhoque53353462014-04-22 08:43:45 -05001314 }
akmhoque53353462014-04-22 08:43:45 -05001315}
1316
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001317bool
Nick Gordon727d4832017-10-13 18:04:25 -05001318Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001319 const uint64_t& sequenceNumber) {
1320 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001321 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001322
Nick Gordon727d4832017-10-13 18:04:25 -05001323 switch (lsaType) {
1324 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001325 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001326 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001327 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001328 case Lsa::Type::NAME:
1329 return isNameLsaNew(lsaKey, sequenceNumber);
1330 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001331 return false;
1332 }
1333}
1334
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001335} // namespace nlsr