blob: ce77683c2b22005f36f70b4e09bb33098a8f7207 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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"
27#include "publisher/segment-publisher.hpp"
28#include "utility/name-helper.hpp"
29
30#include <ndn-cxx/security/signing-helpers.hpp>
31#include <ndn-cxx/util/segment-fetcher.hpp>
32
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
dmcoomescf8d0ed2017-02-21 11:39:01 -060035INIT_LOGGER(Lsdb);
akmhoque674b0b12014-05-20 14:33:28 -050036
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050037class LsaContentPublisher : public SegmentPublisher<ndn::Face>
38{
39public:
40 LsaContentPublisher(ndn::Face& face,
41 ndn::KeyChain& keyChain,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050042 const ndn::security::SigningInfo& signingInfo,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050043 const ndn::time::milliseconds& freshnessPeriod,
44 const std::string& content)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050045 : SegmentPublisher(face, keyChain, signingInfo, freshnessPeriod)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050046 , m_content(content)
47 {
48 }
49
50 virtual size_t
51 generate(ndn::EncodingBuffer& outBuffer) {
52 size_t totalLength = 0;
53 totalLength += outBuffer.prependByteArray(reinterpret_cast<const uint8_t*>(m_content.c_str()),
54 m_content.size());
55 return totalLength;
56 }
57
58private:
59 const std::string m_content;
60};
61
Jiewen Tana0497d82015-02-02 21:59:18 -080062const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050063const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Nick Gordone98480b2017-05-24 11:23:03 -050064const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
65 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050066
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050067Lsdb::Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050068 : m_nlsr(nlsr)
69 , m_scheduler(scheduler)
Nick Gordon9eac4d92017-08-29 17:31:29 -050070 , m_sync(m_nlsr.getNlsrFace(),
Nick Gordon727d4832017-10-13 18:04:25 -050071 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050072 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050073 return isLsaNew(routerName, lsaType, sequenceNumber);
74 }, m_nlsr.getConfParameter())
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060075 , m_lsaStorage(scheduler)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050076 , m_lsaRefreshTime(0)
77 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050078 , m_sequencingManager()
Nick Gordon9eac4d92017-08-29 17:31:29 -050079 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
80 [this] (const ndn::Name& updateName, const uint64_t& sequenceNumber) {
81 ndn::Name lsaInterest{updateName};
82 lsaInterest.appendNumber(sequenceNumber);
83 expressInterest(lsaInterest, 0);
84 }))
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050085{
86}
87
88void
89Lsdb::onFetchLsaError(uint32_t errorCode,
90 const std::string& msg,
91 ndn::Name& interestName,
92 uint32_t retransmitNo,
93 const ndn::time::steady_clock::TimePoint& deadline,
94 ndn::Name lsaName,
95 uint64_t seqNo)
96{
dmcoomes5bcb39e2017-10-31 15:07:55 -050097 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050098 << ", Message: " << msg);
99
100 if (ndn::time::steady_clock::now() < deadline) {
101 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
102
103 if (it != m_highestSeqNo.end() && it->second == seqNo) {
104 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
105 // immediately since at the least the LSA Interest lifetime has elapsed.
106 // Otherwise, it is necessary to delay the Interest re-expression to prevent
107 // the potential for constant Interest flooding.
108 ndn::time::seconds delay = m_nlsr.getConfParameter().getLsaInterestLifetime();
109
110 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
111 delay = ndn::time::seconds(0);
112 }
113
114 m_scheduler.scheduleEvent(delay, std::bind(&Lsdb::expressInterest, this,
115 interestName, retransmitNo + 1, deadline));
116 }
117 }
118}
119
120void
121Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, ndn::Name& interestName)
122{
dmcoomes9f936662017-03-02 10:33:09 -0600123 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500124 data->setContent(bufferPtr);
125
dmcoomes5bcb39e2017-10-31 15:07:55 -0500126 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500127
128 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
129 uint64_t seqNo = interestName[-1].toNumber();
130
131 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
132 m_highestSeqNo[lsaName] = seqNo;
133 }
134 else if (seqNo > m_highestSeqNo[lsaName]) {
135 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500136 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500137 }
138 else if (seqNo < m_highestSeqNo[lsaName]) {
139 return;
140 }
141
142 onContentValidated(data);
143}
akmhoque53353462014-04-22 08:43:45 -0500144
145void
akmhoque31d1d4b2014-05-05 22:08:14 -0500146Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -0500147{
Vince Lehman7c603292014-09-11 17:48:16 -0500148 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -0500149}
150
Nick G97e34942016-07-11 14:46:27 -0500151 /*! \brief Compares if a name LSA is the same as the one specified by key
152
153 \param nlsa1 A name LSA object
154 \param key A key of an originating router to compare to nlsa1
155 */
akmhoque53353462014-04-22 08:43:45 -0500156static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500157nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500158{
159 return nlsa1.getKey() == key;
160}
161
akmhoque53353462014-04-22 08:43:45 -0500162bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500163Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500164{
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500166 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500167 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500168 m_nlsr.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500169 m_sequencingManager.increaseNameLsaSeq();
170
171 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500172 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500173
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500175}
176
akmhoqueb6450b12014-04-24 00:01:03 -0500177NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500178Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500179{
180 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
181 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600182 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500183 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500184 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500185 }
akmhoqueb6450b12014-04-24 00:01:03 -0500186 return 0;
akmhoque53353462014-04-22 08:43:45 -0500187}
188
189bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500190Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500191{
akmhoqueb6450b12014-04-24 00:01:03 -0500192 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500193 // Is the name in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500194 if (nameLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500195 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500196 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500197 return true;
198 }
akmhoque157b0a42014-05-13 00:26:37 -0500199 else {
akmhoque53353462014-04-22 08:43:45 -0500200 return false;
201 }
202 }
203 return true;
204}
205
206ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500207Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
208 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500209{
Vince Lehman7c603292014-09-11 17:48:16 -0500210 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500211 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500212}
213
214bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500215Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500216{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000217 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700218 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500219 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500220 // Determines if the name LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500221 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500222 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500223 NLSR_LOG_DEBUG("New Name LSA");
224 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500225 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500226
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000227 NLSR_LOG_TRACE("nlsa.getOrigRouter(): " << nlsa.getOrigRouter());
228 NLSR_LOG_TRACE("m_nlsr.getConfParameter().getRouterPrefix(): " << m_nlsr.getConfParameter().getRouterPrefix());
229
akmhoque157b0a42014-05-13 00:26:37 -0500230 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500231 // If this name LSA is from another router, add the advertised
232 // prefixes to the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500233 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
234 nlsa.getOrigRouter());
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500235 for (const auto& name : nlsa.getNpl().getNames()) {
236 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
237 m_nlsr.getNamePrefixTable().addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500238 }
239 }
240 }
akmhoque157b0a42014-05-13 00:26:37 -0500241 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500242 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
243 ndn::time::system_clock::now();
244 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500245 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500246 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500247 nlsa.getLsSeqNo(),
248 timeToExpire));
249 }
Nick G97e34942016-07-11 14:46:27 -0500250 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500251 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000252 NLSR_LOG_TRACE("Known name lsa");
253 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
254 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500255 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500256 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
257 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500258 chkNameLsa->writeLog();
259 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500260 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500261 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500262 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500263 // Obtain the set difference of the current and the incoming
264 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500265 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
266 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
267 std::list<ndn::Name> namesToAdd;
268 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
269 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500270 for (const auto& name : namesToAdd) {
271 chkNameLsa->addName(name);
akmhoque157b0a42014-05-13 00:26:37 -0500272 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500273 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
274 m_nlsr.getNamePrefixTable().addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500275 }
276 }
277 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500278
279 chkNameLsa->getNpl().sort();
280
Nick G97e34942016-07-11 14:46:27 -0500281 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500282 std::list<ndn::Name> namesToRemove;
283 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
284 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500285 for (const auto& name : namesToRemove) {
286 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
287 chkNameLsa->removeName(name);
akmhoque157b0a42014-05-13 00:26:37 -0500288 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500289 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
290 m_nlsr.getNamePrefixTable().removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500291 }
292 }
293 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600294
akmhoque157b0a42014-05-13 00:26:37 -0500295 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500296 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
297 ndn::time::system_clock::now();
298 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500299 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500300 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
301 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500302 nlsa.getLsSeqNo(),
303 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500304 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500305 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500306 }
307 }
308 return true;
309}
310
311bool
312Lsdb::addNameLsa(NameLsa& nlsa)
313{
314 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
315 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600316 std::bind(nameLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500317 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500318 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500319 m_nameLsdb.push_back(nlsa);
320 return true;
321 }
322 return false;
323}
324
325bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500326Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500327{
328 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
329 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600330 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500331 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500332 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500333 (*it).writeLog();
Nick G97e34942016-07-11 14:46:27 -0500334 // If the requested name LSA is not ours, we also need to remove
335 // its entries from the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500336 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500337 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500338 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
339 (*it).getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500340 for (const auto& name : it->getNpl().getNames()) {
341 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
342 m_nlsr.getNamePrefixTable().removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500343 }
344 }
345 }
346 m_nameLsdb.erase(it);
347 return true;
348 }
349 return false;
350}
351
352bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500353Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500354{
355 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
356 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600357 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500358 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500359 return false;
360 }
361 return true;
362}
363
364void
akmhoque2f423352014-06-03 11:49:35 -0500365Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500366{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500367 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500368 for (const auto& nlsa : m_nameLsdb) {
369 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500370 }
371}
372
Jiewen Tana0497d82015-02-02 21:59:18 -0800373const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500374Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800375{
376 return m_nameLsdb;
377}
378
akmhoque53353462014-04-22 08:43:45 -0500379// Cor LSA and LSDB related Functions start here
380
Nick G97e34942016-07-11 14:46:27 -0500381/*! \brief Compares whether an LSA object is the same as a key.
382 \param clsa The cor. LSA to check the identity of.
383 \param key The key of the publishing router to check against.
384*/
akmhoque53353462014-04-22 08:43:45 -0500385static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500386corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500387{
388 return clsa.getKey() == key;
389}
390
391bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500392Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500393{
akmhoque31d1d4b2014-05-05 22:08:14 -0500394 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500395 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500396 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500397 m_nlsr.getConfParameter().getCorR(),
398 m_nlsr.getConfParameter().getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500399
400 // Sync coordinate LSAs if using HR or HR dry run.
401 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500402 m_sequencingManager.increaseCorLsaSeq();
403 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500404 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500405 }
406
akmhoque31d1d4b2014-05-05 22:08:14 -0500407 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500408
akmhoque53353462014-04-22 08:43:45 -0500409 return true;
410}
411
akmhoqueb6450b12014-04-24 00:01:03 -0500412CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500413Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500414{
akmhoqueb6450b12014-04-24 00:01:03 -0500415 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
416 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600417 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500418 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500419 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500420 }
akmhoqueb6450b12014-04-24 00:01:03 -0500421 return 0;
akmhoque53353462014-04-22 08:43:45 -0500422}
423
424bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500425Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500426{
akmhoqueb6450b12014-04-24 00:01:03 -0500427 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500428 // Is the coordinate LSA in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500429 if (clsa != 0) {
Nick G97e34942016-07-11 14:46:27 -0500430 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500431 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500432 return true;
433 }
akmhoque157b0a42014-05-13 00:26:37 -0500434 else {
akmhoque53353462014-04-22 08:43:45 -0500435 return false;
436 }
437 }
438 return true;
439}
440
Nick G97e34942016-07-11 14:46:27 -0500441 // Schedules a refresh/expire event in the scheduler.
442 // \param key The name of the router that published the LSA.
443 // \param seqNo the seq. no. associated with the LSA to check.
444 // \param expTime How long to wait before triggering the event.
akmhoque53353462014-04-22 08:43:45 -0500445ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500446Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500447 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500448{
Vince Lehman7c603292014-09-11 17:48:16 -0500449 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500450 std::bind(&Lsdb::expireOrRefreshCoordinateLsa,
Vince Lehman7c603292014-09-11 17:48:16 -0500451 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500452}
453
454bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500455Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500456{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700457 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500458 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500459 // Checking whether the LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500460 if (chkCorLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500461 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
462 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500463 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500464 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500465
Nick Gordon5c467f02016-07-13 13:40:10 -0500466 // Register the LSA's origin router prefix
akmhoque157b0a42014-05-13 00:26:37 -0500467 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500468 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
469 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500470 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500471 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500472 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500473 }
Nick G97e34942016-07-11 14:46:27 -0500474 // Set the expiration time for the new LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500475 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500476 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
477 ndn::time::system_clock::now();
478 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500479 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500480 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500481 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500482 }
Nick G97e34942016-07-11 14:46:27 -0500483 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500484 else {
485 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500486 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
487 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500488 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500489 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500490 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500491 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500492 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500493 chkCorLsa->setCorRadius(clsa.getCorRadius());
494 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500495 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500496 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500497 }
498 }
Nick G97e34942016-07-11 14:46:27 -0500499 // If this is an LSA from another router, refresh its expiration time.
akmhoque157b0a42014-05-13 00:26:37 -0500500 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500501 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
502 ndn::time::system_clock::now();
503 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500504 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500505 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
506 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500507 clsa.getLsSeqNo(),
508 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500509 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500510 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500511 }
512 }
513 return true;
514}
515
516bool
akmhoqueb6450b12014-04-24 00:01:03 -0500517Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500518{
akmhoqueb6450b12014-04-24 00:01:03 -0500519 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
520 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600521 std::bind(corLsaCompareByKey, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500522 clsa.getKey()));
523 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500524 m_corLsdb.push_back(clsa);
525 return true;
526 }
527 return false;
528}
529
530bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500531Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500532{
akmhoqueb6450b12014-04-24 00:01:03 -0500533 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
534 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600535 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500536 _1, key));
537 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500538 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500539 it->writeLog();
540
541 if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
542 m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500543 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500544
akmhoque53353462014-04-22 08:43:45 -0500545 m_corLsdb.erase(it);
546 return true;
547 }
548 return false;
549}
550
551bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500552Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500553{
akmhoqueb6450b12014-04-24 00:01:03 -0500554 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
555 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600556 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500557 _1, key));
558 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500559 return false;
560 }
561 return true;
562}
563
564void
akmhoque2f423352014-06-03 11:49:35 -0500565Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500566{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500567 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500568 for (const auto& corLsa : m_corLsdb) {
569 corLsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500570 }
571}
572
Jiewen Tana0497d82015-02-02 21:59:18 -0800573const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500574Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800575{
576 return m_corLsdb;
577}
578
akmhoque53353462014-04-22 08:43:45 -0500579// Adj LSA and LSDB related function starts here
580
Nick G97e34942016-07-11 14:46:27 -0500581 /*! \brief Returns whether an adj. LSA object is from some router.
582 \param alsa The adj. LSA object.
583 \param key The router name that you want to compare the LSA with.
584 */
akmhoque53353462014-04-22 08:43:45 -0500585static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500586adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500587{
588 return alsa.getKey() == key;
589}
590
akmhoque53353462014-04-22 08:43:45 -0500591void
Vince Lehman50df6b72015-03-03 12:06:40 -0600592Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500593{
Vince Lehman50df6b72015-03-03 12:06:40 -0600594 m_nlsr.incrementAdjBuildCount();
595
Nick Gordon5c467f02016-07-13 13:40:10 -0500596 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
597 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500598 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500599 return;
600 }
601
Vince Lehman50df6b72015-03-03 12:06:40 -0600602 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500603 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600604
dmcoomes9f936662017-03-02 10:33:09 -0600605 m_scheduler.scheduleEvent(m_adjLsaBuildInterval, std::bind(&Lsdb::buildAdjLsa, this));
Vince Lehman50df6b72015-03-03 12:06:40 -0600606 m_nlsr.setIsBuildAdjLsaSheduled(true);
607 }
608}
609
610void
611Lsdb::buildAdjLsa()
612{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500613 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600614
akmhoque674b0b12014-05-20 14:33:28 -0500615 m_nlsr.setIsBuildAdjLsaSheduled(false);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500616
617 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
618
akmhoque31d1d4b2014-05-05 22:08:14 -0500619 int adjBuildCount = m_nlsr.getAdjBuildCount();
Nick G97e34942016-07-11 14:46:27 -0500620 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500621 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500622 // It only makes sense to do the adjLsa build if we have neighbors
akmhoque157b0a42014-05-13 00:26:37 -0500623 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500624 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500625 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500626 }
Nick G97e34942016-07-11 14:46:27 -0500627 // We have no active neighbors, meaning no one can route through
628 // us. So delete our entry in the LSDB. This prevents this
629 // router from refreshing the LSA, eventually causing other
630 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500631 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500632 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500633 // Get this router's key
akmhoque31d1d4b2014-05-05 22:08:14 -0500634 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500635 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500636
akmhoque31d1d4b2014-05-05 22:08:14 -0500637 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500638 // Recompute routing table after removal
akmhoque31d1d4b2014-05-05 22:08:14 -0500639 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500640 }
Nick G97e34942016-07-11 14:46:27 -0500641 // In the case that during building the adj LSA, the FIB has to
642 // wait on an Interest response, the number of scheduled adj LSA
643 // builds could change, so we shouldn't just set it to 0.
akmhoque31d1d4b2014-05-05 22:08:14 -0500644 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500645 }
646 }
Nick G97e34942016-07-11 14:46:27 -0500647 // We are still waiting to know the adjacency status of some
648 // neighbor, so schedule a build for later (when all that has
649 // hopefully finished)
650 else {
651 m_nlsr.setIsBuildAdjLsaSheduled(true);
652 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
653 m_nlsr.getConfParameter().getInterestResendTime();
654 m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
dmcoomes9f936662017-03-02 10:33:09 -0600655 std::bind(&Lsdb::buildAdjLsa, this));
Nick G97e34942016-07-11 14:46:27 -0500656 }
akmhoque53353462014-04-22 08:43:45 -0500657}
658
akmhoque53353462014-04-22 08:43:45 -0500659bool
660Lsdb::addAdjLsa(AdjLsa& alsa)
661{
662 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
663 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600664 std::bind(adjLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500665 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500666 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500667 m_adjLsdb.push_back(alsa);
668 return true;
669 }
670 return false;
671}
672
akmhoqueb6450b12014-04-24 00:01:03 -0500673AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500674Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500675{
676 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
677 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600678 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500679 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500680 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500681 }
akmhoqueb6450b12014-04-24 00:01:03 -0500682 return 0;
akmhoque53353462014-04-22 08:43:45 -0500683}
684
akmhoque53353462014-04-22 08:43:45 -0500685bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500686Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500687{
akmhoqueb6450b12014-04-24 00:01:03 -0500688 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500689 // If it is in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500690 if (adjLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500691 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500692 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500693 return true;
694 }
akmhoque157b0a42014-05-13 00:26:37 -0500695 else {
akmhoque53353462014-04-22 08:43:45 -0500696 return false;
697 }
698 }
699 return true;
700}
701
akmhoque53353462014-04-22 08:43:45 -0500702ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500703Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
704 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500705{
Vince Lehman7c603292014-09-11 17:48:16 -0500706 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500707 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500708}
709
710bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500711Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500712{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700713 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500714 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500715 // If this adj. LSA is not in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500716 if (chkAdjLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500717 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
718 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500719 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500720 addAdjLsa(alsa);
Nick G97e34942016-07-11 14:46:27 -0500721 // Add any new name prefixes to the NPT
akmhoque31d1d4b2014-05-05 22:08:14 -0500722 alsa.addNptEntries(m_nlsr);
723 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500724 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500725 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
726 ndn::time::system_clock::now();
727 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500728 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500729 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500730 alsa.getLsSeqNo(), timeToExpire);
731 }
akmhoque157b0a42014-05-13 00:26:37 -0500732 else {
733 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500734 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
735 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500736 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500737 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500738 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500739 // If the new adj LSA has new content, update the contents of
740 // the LSDB entry. Additionally, since we've changed the
741 // contents of the LSDB, we have to schedule a routing
742 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500743 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500744 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500745 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500746 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500747 }
akmhoque157b0a42014-05-13 00:26:37 -0500748 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500749 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
750 ndn::time::system_clock::now();
751 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500752 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500753 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
754 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500755 alsa.getLsSeqNo(),
756 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500757 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500758 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500759 }
760 }
761 return true;
762}
763
764bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500765Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500766{
akmhoque31d1d4b2014-05-05 22:08:14 -0500767 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500768 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500769 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500770 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
771 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500772
Nick Gordon5c467f02016-07-13 13:40:10 -0500773 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
774 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500775 m_sequencingManager.increaseAdjLsaSeq();
776 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500777 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500778 }
Vince Lehman904c2412014-09-23 19:36:11 -0500779
Vince Lehman9d097802015-03-16 17:55:59 -0500780 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500781}
782
783bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500784Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500785{
786 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
787 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600788 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500789 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500790 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500791 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500792 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500793 m_adjLsdb.erase(it);
794 return true;
795 }
796 return false;
797}
798
799bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500800Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500801{
802 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
803 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600804 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500805 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500806 return false;
807 }
808 return true;
809}
810
Jiewen Tana0497d82015-02-02 21:59:18 -0800811const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500812Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500813{
814 return m_adjLsdb;
815}
816
817void
Nick Gordone98480b2017-05-24 11:23:03 -0500818Lsdb::setLsaRefreshTime(const ndn::time::seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500819{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700820 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500821}
822
823void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500824Lsdb::setThisRouterPrefix(std::string trp)
akmhoque53353462014-04-22 08:43:45 -0500825{
826 m_thisRouterPrefix = trp;
827}
828
Nick G97e34942016-07-11 14:46:27 -0500829 // This function determines whether a name LSA should be refreshed
830 // or expired. The conditions for getting refreshed are: it is still
831 // in the LSDB, it hasn't been updated by something else already (as
832 // evidenced by its seq. no.), and this is the originating router for
833 // the LSA. Is it let expire in all other cases.
834 // lsaKey is the key of the LSA's publishing router.
835 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500836void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500837Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500838{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500839 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
840 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500841 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500842 // If this name LSA exists in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500843 if (chkNameLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500844 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500845 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500846 if (chkNameLsa->getLsSeqNo() == seqNo) {
847 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500848 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
849 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500850 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500851 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500852 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500853 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500854 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500855 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500856 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500857 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500858 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700859 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500860 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500861 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500862 }
Nick G97e34942016-07-11 14:46:27 -0500863 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500864 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500865 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500866 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500867 }
868 }
869 }
870}
871
Nick G97e34942016-07-11 14:46:27 -0500872 // This function determines whether an adj. LSA should be refreshed
873 // or expired. The conditions for getting refreshed are: it is still
874 // in the LSDB, it hasn't been updated by something else already (as
875 // evidenced by its seq. no.), and this is the originating router for
876 // the LSA. Is it let expire in all other cases.
877 // lsaKey is the key of the LSA's publishing router.
878 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500879void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500880Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500881{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500882 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
883 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500884 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500885 // If this is a valid LSA
akmhoque157b0a42014-05-13 00:26:37 -0500886 if (chkAdjLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500887 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500888 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500889 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500890 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500891 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500892 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
893 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500894 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500895 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500896 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500897 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500898 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500899 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500900 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500901 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500902 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700903 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500904 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500905 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500906 }
Nick G97e34942016-07-11 14:46:27 -0500907 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500908 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500909 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500910 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500911 }
Nick G97e34942016-07-11 14:46:27 -0500912 // We have changed the contents of the LSDB, so we have to
913 // schedule a routing calculation
akmhoque31d1d4b2014-05-05 22:08:14 -0500914 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500915 }
916 }
917}
918
Nick G97e34942016-07-11 14:46:27 -0500919 // This function determines whether an adj. LSA should be refreshed
920 // or expired. The conditions for getting refreshed are: it is still
921 // in the LSDB, it hasn't been updated by something else already (as
922 // evidenced by its seq. no.), and this is the originating router for
923 // the LSA. It is let expire in all other cases.
924 // lsaKey is the key of the LSA's publishing router.
925 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500926void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500927Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500928 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500929{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500930 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
931 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500932 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500933 // Whether the LSA is in the LSDB or not.
akmhoque157b0a42014-05-13 00:26:37 -0500934 if (chkCorLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500935 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500936 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500937 if (chkCorLsa->getLsSeqNo() == seqNo) {
938 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500939 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
940 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500941 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500942 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Nick Gordon5c467f02016-07-13 13:40:10 -0500943 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500944 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500945 }
946
akmhoquec7a79b22014-05-26 08:06:19 -0500947 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500948 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500949 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500950 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500951 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
952 chkCorLsa->getKey(),
953 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700954 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500955 // Only sync coordinate LSAs if link-state routing is disabled
956 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500957 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500958 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500959 }
akmhoque53353462014-04-22 08:43:45 -0500960 }
Nick G97e34942016-07-11 14:46:27 -0500961 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500962 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500963 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500964 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500965 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500966 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500967 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500968 }
969 }
970 }
971}
972
akmhoque53353462014-04-22 08:43:45 -0500973void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700974Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500975 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500976{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600977 // increment SENT_LSA_INTEREST
978 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
979
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500980 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500981 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700982 }
Nick G97e34942016-07-11 14:46:27 -0500983 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500984 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500985 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500986 uint64_t seqNo = interestName[-1].toNumber();
987
Nick G97e34942016-07-11 14:46:27 -0500988 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500989 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
990 m_highestSeqNo[lsaName] = seqNo;
991 }
Nick G97e34942016-07-11 14:46:27 -0500992 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500993 else if (seqNo > m_highestSeqNo[lsaName]) {
994 m_highestSeqNo[lsaName] = seqNo;
995 }
Nick G97e34942016-07-11 14:46:27 -0500996 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500997 else if (seqNo < m_highestSeqNo[lsaName]) {
998 return;
999 }
1000
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001001 ndn::Interest interest(interestName);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -07001002 interest.setInterestLifetime(m_nlsr.getConfParameter().getLsaInterestLifetime());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001003
dmcoomes5bcb39e2017-10-31 15:07:55 -05001004 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001005 shared_ptr<ndn::util::SegmentFetcher> fetcher =
1006 ndn::util::SegmentFetcher::fetch(m_nlsr.getNlsrFace(), interest,
1007 m_nlsr.getValidator(),
1008 std::bind(&Lsdb::afterFetchLsa, this, _1, interestName),
1009 std::bind(&Lsdb::onFetchLsaError, this, _1, _2, interestName,
1010 timeoutCount, deadline, lsaName, seqNo));
1011
1012 m_lsaStorage.connectToFetcher(*fetcher);
1013 m_nlsr.connectToFetcher(*fetcher);
1014
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001015 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001016 Lsa::Type lsaType;
1017 std::istringstream(interestName[-2].toUri()) >> lsaType;
1018 switch (lsaType) {
1019 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001020 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001021 break;
1022 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001023 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001024 break;
1025 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001026 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001027 break;
1028 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001029 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001030 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001031}
1032
1033void
1034Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1035{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001036 // increment RCV_LSA_INTEREST
1037 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1038
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001039 const ndn::Name& interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -05001040 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001041
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001042 std::string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001043 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
1044
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001045 // Forms the name of the router that the Interest packet came from.
1046 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
1047 originRouter.append(interestName.getSubName(lsaPosition + 1,
1048 interest.getName().size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001049
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001050 // if the interest is for this router's LSA
1051 if (originRouter == m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001052
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001053 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001054
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001055 uint64_t seqNo = interestName[-1].toNumber();
1056 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001057
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001058 Lsa::Type interestedLsType;
1059 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
1060
1061 if (interestedLsType == Lsa::Type::NAME) {
1062 processInterestForNameLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1063 seqNo);
1064 }
1065 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1066 processInterestForAdjacencyLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1067 seqNo);
1068 }
1069 else if (interestedLsType == Lsa::Type::COORDINATE) {
1070 processInterestForCoordinateLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1071 seqNo);
1072 }
1073 else {
1074 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1075 }
1076 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001077 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001078 }
1079 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
1080 const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
1081 if (lsaSegment != nullptr) {
1082 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
1083 m_nlsr.getNlsrFace().put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001084 }
akmhoque157b0a42014-05-13 00:26:37 -05001085 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001086 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001087 }
1088 }
1089}
1090
Nick G97e34942016-07-11 14:46:27 -05001091 // \brief Sends LSA data.
1092 // \param interest The Interest that warranted the data.
1093 // \param content The data that the Interest was seeking.
akmhoque31d1d4b2014-05-05 22:08:14 -05001094void
akmhoque69c9aa92014-07-23 15:15:05 -05001095Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
1096{
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001097 LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
1098 m_nlsr.getKeyChain(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001099 m_nlsr.getSigningInfo(),
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001100 m_lsaRefreshTime,
1101 content);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001102 NLSR_LOG_DEBUG("Sending requested data ( " << content << ") for interest (" << interest
dmcoomes9eaf3f42017-02-21 11:39:01 -06001103 << ") to be published and added to face.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001104 publisher.publish(interest.getName());
akmhoque69c9aa92014-07-23 15:15:05 -05001105}
1106
Nick G97e34942016-07-11 14:46:27 -05001107 // \brief Finds and sends a requested name LSA.
1108 // \param interest The interest that seeks the name LSA.
1109 // \param lsaKey The LSA that the Interest is seeking.
1110 // \param seqNo A sequence number to ensure that we are sending the
1111 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001112void
akmhoque31d1d4b2014-05-05 22:08:14 -05001113Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1114 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001115 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001116{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001117 // increment RCV_NAME_LSA_INTEREST
1118 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001119 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001120 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001121 if (nameLsa != nullptr) {
1122 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001123 if (nameLsa->getLsSeqNo() == seqNo) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001124 // if requested lsa belongs to this router then sign it and serve it
Nick Gordonfaf49f42017-10-23 12:36:28 -05001125 std::string content = nameLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001126 putLsaData(interest,content);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001127 // else the requested belongs to neighboring routers, so serve the
1128 // original data packet corresponding to the lsa
1129
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001130 // increment SENT_NAME_LSA_DATA
1131 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001132 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001133 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001134 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001135 }
1136 }
1137 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001138 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001139 }
1140}
1141
Nick G97e34942016-07-11 14:46:27 -05001142 // \brief Finds and sends a requested adj. LSA.
1143 // \param interest The interest that seeks the adj. LSA.
1144 // \param lsaKey The LSA that the Interest is seeking.
1145 // \param seqNo A sequence number to ensure that we are sending the
1146 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001147void
1148Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1149 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001150 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001151{
Nick Gordon5c467f02016-07-13 13:40:10 -05001152 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001153 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001154 }
1155
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001156 // increment RCV_ADJ_LSA_INTEREST
1157 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001158 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001159 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001160 if (adjLsa != nullptr) {
1161 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001162 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001163 std::string content = adjLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001164 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001165 // increment SENT_ADJ_LSA_DATA
1166 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001167 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001168 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001169 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001170 }
1171 }
1172 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001173 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001174 }
1175}
1176
Nick G97e34942016-07-11 14:46:27 -05001177 // \brief Finds and sends a requested cor. LSA.
1178 // \param interest The interest that seeks the cor. LSA.
1179 // \param lsaKey The LSA that the Interest is seeking.
1180 // \param seqNo A sequence number to ensure that we are sending the
1181 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001182void
1183Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1184 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001185 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001186{
Nick Gordon5c467f02016-07-13 13:40:10 -05001187 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001188 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001189 }
1190
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001191 // increment RCV_COORD_LSA_INTEREST
1192 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001193 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001194 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001195 if (corLsa != nullptr) {
1196 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001197 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001198 std::string content = corLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001199 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001200 // increment SENT_COORD_LSA_DATA
1201 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001202 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001203 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001204 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001205 }
1206 }
1207 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001208 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001209 }
1210}
1211
1212void
dmcoomes9f936662017-03-02 10:33:09 -06001213Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001214{
1215 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001216 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001217
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001218 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001219 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001220
akmhoque157b0a42014-05-13 00:26:37 -05001221 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001222
Nick G97e34942016-07-11 14:46:27 -05001223 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001224 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001225 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001226
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001227 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001228 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1229 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001230
Nick Gordon727d4832017-10-13 18:04:25 -05001231 Lsa::Type interestedLsType;
1232 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001233
Nick Gordon727d4832017-10-13 18:04:25 -05001234 if (interestedLsType == Lsa::Type::NAME) {
1235 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1236 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001237 }
Nick Gordon727d4832017-10-13 18:04:25 -05001238 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1239 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1240 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001241 }
Nick Gordon727d4832017-10-13 18:04:25 -05001242 else if (interestedLsType == Lsa::Type::COORDINATE) {
1243 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1244 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001245 }
akmhoque157b0a42014-05-13 00:26:37 -05001246 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001247 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001248 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001249
1250 // increment RCV_LSA_DATA
1251 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001252 }
1253}
1254
1255void
1256Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001257 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001258{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001259 // increment RCV_NAME_LSA_DATA
1260 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001261 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001262 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001263 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001264 installNameLsa(nameLsa);
1265 }
akmhoque157b0a42014-05-13 00:26:37 -05001266 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001267 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001268 }
1269 }
1270}
1271
1272void
1273Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001274 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001275{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001276 // increment RCV_ADJ_LSA_DATA
1277 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001278 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001279 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001280 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001281 installAdjLsa(adjLsa);
1282 }
akmhoque157b0a42014-05-13 00:26:37 -05001283 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001284 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001285 }
1286 }
1287}
1288
1289void
1290Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001291 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001292{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001293 // increment RCV_COORD_LSA_DATA
1294 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001295 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001296 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001297 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001298 installCoordinateLsa(corLsa);
1299 }
akmhoque157b0a42014-05-13 00:26:37 -05001300 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001301 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001302 }
1303 }
1304}
1305
akmhoquec7a79b22014-05-26 08:06:19 -05001306ndn::time::system_clock::TimePoint
1307Lsdb::getLsaExpirationTimePoint()
1308{
1309 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1310 expirationTimePoint = expirationTimePoint +
1311 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1312 return expirationTimePoint;
1313}
akmhoque31d1d4b2014-05-05 22:08:14 -05001314
1315void
akmhoque2f423352014-06-03 11:49:35 -05001316Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001317{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001318 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001319 for (const auto& adj : m_adjLsdb) {
1320 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001321 }
1322}
1323
1324//-----utility function -----
1325bool
Nick Gordon727d4832017-10-13 18:04:25 -05001326Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001327{
Nick Gordon727d4832017-10-13 18:04:25 -05001328 switch (lsType) {
1329 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001330 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001331 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001332 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001333 case Lsa::Type::NAME:
1334 return doesNameLsaExist(key);
1335 default:
1336 return false;
akmhoque53353462014-04-22 08:43:45 -05001337 }
akmhoque53353462014-04-22 08:43:45 -05001338}
1339
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001340bool
Nick Gordon727d4832017-10-13 18:04:25 -05001341Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001342 const uint64_t& sequenceNumber) {
1343 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001344 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001345
Nick Gordon727d4832017-10-13 18:04:25 -05001346 switch (lsaType) {
1347 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001348 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001349 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001350 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001351 case Lsa::Type::NAME:
1352 return isNameLsaNew(lsaKey, sequenceNumber);
1353 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001354 return false;
1355 }
1356}
1357
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001358} // namespace nlsr