blob: a38dd55dad3d4453a631a6c4c0321bab8e852473 [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());
Nick Gordonf14ec352017-07-24 16:09:58 -0500235 std::list<ndn::Name> nameList = nlsa.getNpl().getNames();
akmhoque31d1d4b2014-05-05 22:08:14 -0500236 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -0500237 it++) {
238 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500239 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500240 }
241 }
242 }
akmhoque157b0a42014-05-13 00:26:37 -0500243 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500244 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
245 ndn::time::system_clock::now();
246 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500247 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500248 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500249 nlsa.getLsSeqNo(),
250 timeToExpire));
251 }
Nick G97e34942016-07-11 14:46:27 -0500252 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500253 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000254 NLSR_LOG_TRACE("Known name lsa");
255 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
256 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500257 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500258 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
259 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500260 chkNameLsa->writeLog();
261 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500262 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500263 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500264 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500265 // Obtain the set difference of the current and the incoming
266 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500267 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
268 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
269 std::list<ndn::Name> namesToAdd;
270 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
271 std::inserter(namesToAdd, namesToAdd.begin()));
272 for (std::list<ndn::Name>::iterator it = namesToAdd.begin();
273 it != namesToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500274 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500275 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
276 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500277 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500278 }
279 }
280 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500281
282 chkNameLsa->getNpl().sort();
283
Nick G97e34942016-07-11 14:46:27 -0500284 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500285 std::list<ndn::Name> namesToRemove;
286 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
287 std::inserter(namesToRemove, namesToRemove.begin()));
288 for (std::list<ndn::Name>::iterator it = namesToRemove.begin();
289 it != namesToRemove.end(); ++it) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500290 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << (*it).toUri());
akmhoqueb6450b12014-04-24 00:01:03 -0500291 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500292 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
293 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500294 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500295 }
296 }
297 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600298
akmhoque157b0a42014-05-13 00:26:37 -0500299 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500300 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
301 ndn::time::system_clock::now();
302 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500303 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500304 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
305 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500306 nlsa.getLsSeqNo(),
307 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500308 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500309 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500310 }
311 }
312 return true;
313}
314
315bool
316Lsdb::addNameLsa(NameLsa& nlsa)
317{
318 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
319 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600320 std::bind(nameLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500321 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500322 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500323 m_nameLsdb.push_back(nlsa);
324 return true;
325 }
326 return false;
327}
328
329bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500330Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500331{
332 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
333 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600334 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500335 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500336 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500337 (*it).writeLog();
Nick G97e34942016-07-11 14:46:27 -0500338 // If the requested name LSA is not ours, we also need to remove
339 // its entries from the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500340 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500341 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500342 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
343 (*it).getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500344 for (const auto& name : it->getNpl().getNames()) {
345 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
346 m_nlsr.getNamePrefixTable().removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500347 }
348 }
349 }
350 m_nameLsdb.erase(it);
351 return true;
352 }
353 return false;
354}
355
356bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500357Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500358{
359 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
360 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600361 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500362 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500363 return false;
364 }
365 return true;
366}
367
368void
akmhoque2f423352014-06-03 11:49:35 -0500369Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500370{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500371 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500372 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500373 it != m_nameLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500374 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500375 }
376}
377
Jiewen Tana0497d82015-02-02 21:59:18 -0800378const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500379Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800380{
381 return m_nameLsdb;
382}
383
akmhoque53353462014-04-22 08:43:45 -0500384// Cor LSA and LSDB related Functions start here
385
Nick G97e34942016-07-11 14:46:27 -0500386/*! \brief Compares whether an LSA object is the same as a key.
387 \param clsa The cor. LSA to check the identity of.
388 \param key The key of the publishing router to check against.
389*/
akmhoque53353462014-04-22 08:43:45 -0500390static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500391corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500392{
393 return clsa.getKey() == key;
394}
395
396bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500397Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500398{
akmhoque31d1d4b2014-05-05 22:08:14 -0500399 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500400 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500401 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500402 m_nlsr.getConfParameter().getCorR(),
403 m_nlsr.getConfParameter().getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500404
405 // Sync coordinate LSAs if using HR or HR dry run.
406 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500407 m_sequencingManager.increaseCorLsaSeq();
408 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500409 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500410 }
411
akmhoque31d1d4b2014-05-05 22:08:14 -0500412 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500413
akmhoque53353462014-04-22 08:43:45 -0500414 return true;
415}
416
akmhoqueb6450b12014-04-24 00:01:03 -0500417CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500418Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500419{
akmhoqueb6450b12014-04-24 00:01:03 -0500420 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
421 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600422 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500423 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500424 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500425 }
akmhoqueb6450b12014-04-24 00:01:03 -0500426 return 0;
akmhoque53353462014-04-22 08:43:45 -0500427}
428
429bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500430Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500431{
akmhoqueb6450b12014-04-24 00:01:03 -0500432 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500433 // Is the coordinate LSA in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500434 if (clsa != 0) {
Nick G97e34942016-07-11 14:46:27 -0500435 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500436 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500437 return true;
438 }
akmhoque157b0a42014-05-13 00:26:37 -0500439 else {
akmhoque53353462014-04-22 08:43:45 -0500440 return false;
441 }
442 }
443 return true;
444}
445
Nick G97e34942016-07-11 14:46:27 -0500446 // Schedules a refresh/expire event in the scheduler.
447 // \param key The name of the router that published the LSA.
448 // \param seqNo the seq. no. associated with the LSA to check.
449 // \param expTime How long to wait before triggering the event.
akmhoque53353462014-04-22 08:43:45 -0500450ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500451Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500452 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500453{
Vince Lehman7c603292014-09-11 17:48:16 -0500454 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500455 std::bind(&Lsdb::expireOrRefreshCoordinateLsa,
Vince Lehman7c603292014-09-11 17:48:16 -0500456 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500457}
458
459bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500460Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500461{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700462 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500463 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500464 // Checking whether the LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500465 if (chkCorLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500466 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
467 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500468 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500469 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500470
Nick Gordon5c467f02016-07-13 13:40:10 -0500471 // Register the LSA's origin router prefix
akmhoque157b0a42014-05-13 00:26:37 -0500472 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500473 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
474 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500475 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500476 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500477 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500478 }
Nick G97e34942016-07-11 14:46:27 -0500479 // Set the expiration time for the new LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500480 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500481 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
482 ndn::time::system_clock::now();
483 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500484 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500485 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500486 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500487 }
Nick G97e34942016-07-11 14:46:27 -0500488 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500489 else {
490 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500491 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
492 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500493 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500494 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500495 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500496 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500497 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500498 chkCorLsa->setCorRadius(clsa.getCorRadius());
499 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500500 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500501 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500502 }
503 }
Nick G97e34942016-07-11 14:46:27 -0500504 // If this is an LSA from another router, refresh its expiration time.
akmhoque157b0a42014-05-13 00:26:37 -0500505 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500506 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
507 ndn::time::system_clock::now();
508 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500509 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500510 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
511 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500512 clsa.getLsSeqNo(),
513 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500514 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500515 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500516 }
517 }
518 return true;
519}
520
521bool
akmhoqueb6450b12014-04-24 00:01:03 -0500522Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500523{
akmhoqueb6450b12014-04-24 00:01:03 -0500524 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
525 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600526 std::bind(corLsaCompareByKey, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500527 clsa.getKey()));
528 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500529 m_corLsdb.push_back(clsa);
530 return true;
531 }
532 return false;
533}
534
535bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500536Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500537{
akmhoqueb6450b12014-04-24 00:01:03 -0500538 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
539 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600540 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500541 _1, key));
542 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500543 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500544 it->writeLog();
545
546 if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
547 m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500548 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500549
akmhoque53353462014-04-22 08:43:45 -0500550 m_corLsdb.erase(it);
551 return true;
552 }
553 return false;
554}
555
556bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500557Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500558{
akmhoqueb6450b12014-04-24 00:01:03 -0500559 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
560 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600561 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500562 _1, key));
563 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500564 return false;
565 }
566 return true;
567}
568
569void
akmhoque2f423352014-06-03 11:49:35 -0500570Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500571{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500572 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
akmhoqueb6450b12014-04-24 00:01:03 -0500573 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500574 it != m_corLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500575 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500576 }
577}
578
Jiewen Tana0497d82015-02-02 21:59:18 -0800579const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500580Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800581{
582 return m_corLsdb;
583}
584
akmhoque53353462014-04-22 08:43:45 -0500585// Adj LSA and LSDB related function starts here
586
Nick G97e34942016-07-11 14:46:27 -0500587 /*! \brief Returns whether an adj. LSA object is from some router.
588 \param alsa The adj. LSA object.
589 \param key The router name that you want to compare the LSA with.
590 */
akmhoque53353462014-04-22 08:43:45 -0500591static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500592adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500593{
594 return alsa.getKey() == key;
595}
596
akmhoque53353462014-04-22 08:43:45 -0500597void
Vince Lehman50df6b72015-03-03 12:06:40 -0600598Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500599{
Vince Lehman50df6b72015-03-03 12:06:40 -0600600 m_nlsr.incrementAdjBuildCount();
601
Nick Gordon5c467f02016-07-13 13:40:10 -0500602 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
603 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500604 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500605 return;
606 }
607
Vince Lehman50df6b72015-03-03 12:06:40 -0600608 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500609 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600610
dmcoomes9f936662017-03-02 10:33:09 -0600611 m_scheduler.scheduleEvent(m_adjLsaBuildInterval, std::bind(&Lsdb::buildAdjLsa, this));
Vince Lehman50df6b72015-03-03 12:06:40 -0600612 m_nlsr.setIsBuildAdjLsaSheduled(true);
613 }
614}
615
616void
617Lsdb::buildAdjLsa()
618{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500619 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600620
akmhoque674b0b12014-05-20 14:33:28 -0500621 m_nlsr.setIsBuildAdjLsaSheduled(false);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500622
623 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
624
akmhoque31d1d4b2014-05-05 22:08:14 -0500625 int adjBuildCount = m_nlsr.getAdjBuildCount();
Nick G97e34942016-07-11 14:46:27 -0500626 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500627 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500628 // It only makes sense to do the adjLsa build if we have neighbors
akmhoque157b0a42014-05-13 00:26:37 -0500629 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500630 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500631 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500632 }
Nick G97e34942016-07-11 14:46:27 -0500633 // We have no active neighbors, meaning no one can route through
634 // us. So delete our entry in the LSDB. This prevents this
635 // router from refreshing the LSA, eventually causing other
636 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500637 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500638 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500639 // Get this router's key
akmhoque31d1d4b2014-05-05 22:08:14 -0500640 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500641 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500642
akmhoque31d1d4b2014-05-05 22:08:14 -0500643 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500644 // Recompute routing table after removal
akmhoque31d1d4b2014-05-05 22:08:14 -0500645 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500646 }
Nick G97e34942016-07-11 14:46:27 -0500647 // In the case that during building the adj LSA, the FIB has to
648 // wait on an Interest response, the number of scheduled adj LSA
649 // builds could change, so we shouldn't just set it to 0.
akmhoque31d1d4b2014-05-05 22:08:14 -0500650 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500651 }
652 }
Nick G97e34942016-07-11 14:46:27 -0500653 // We are still waiting to know the adjacency status of some
654 // neighbor, so schedule a build for later (when all that has
655 // hopefully finished)
656 else {
657 m_nlsr.setIsBuildAdjLsaSheduled(true);
658 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
659 m_nlsr.getConfParameter().getInterestResendTime();
660 m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
dmcoomes9f936662017-03-02 10:33:09 -0600661 std::bind(&Lsdb::buildAdjLsa, this));
Nick G97e34942016-07-11 14:46:27 -0500662 }
akmhoque53353462014-04-22 08:43:45 -0500663}
664
akmhoque53353462014-04-22 08:43:45 -0500665bool
666Lsdb::addAdjLsa(AdjLsa& alsa)
667{
668 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
669 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600670 std::bind(adjLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500671 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500672 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500673 m_adjLsdb.push_back(alsa);
674 return true;
675 }
676 return false;
677}
678
akmhoqueb6450b12014-04-24 00:01:03 -0500679AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500680Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500681{
682 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
683 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600684 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500685 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500686 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500687 }
akmhoqueb6450b12014-04-24 00:01:03 -0500688 return 0;
akmhoque53353462014-04-22 08:43:45 -0500689}
690
akmhoque53353462014-04-22 08:43:45 -0500691bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500692Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500693{
akmhoqueb6450b12014-04-24 00:01:03 -0500694 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500695 // If it is in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500696 if (adjLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500697 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500698 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500699 return true;
700 }
akmhoque157b0a42014-05-13 00:26:37 -0500701 else {
akmhoque53353462014-04-22 08:43:45 -0500702 return false;
703 }
704 }
705 return true;
706}
707
akmhoque53353462014-04-22 08:43:45 -0500708ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500709Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
710 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500711{
Vince Lehman7c603292014-09-11 17:48:16 -0500712 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500713 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500714}
715
716bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500717Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500718{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700719 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500720 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500721 // If this adj. LSA is not in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500722 if (chkAdjLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500723 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
724 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500725 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500726 addAdjLsa(alsa);
Nick G97e34942016-07-11 14:46:27 -0500727 // Add any new name prefixes to the NPT
akmhoque31d1d4b2014-05-05 22:08:14 -0500728 alsa.addNptEntries(m_nlsr);
729 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500730 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500731 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
732 ndn::time::system_clock::now();
733 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500734 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500735 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500736 alsa.getLsSeqNo(), timeToExpire);
737 }
akmhoque157b0a42014-05-13 00:26:37 -0500738 else {
739 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500740 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
741 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500742 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500743 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500744 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500745 // If the new adj LSA has new content, update the contents of
746 // the LSDB entry. Additionally, since we've changed the
747 // contents of the LSDB, we have to schedule a routing
748 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500749 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500750 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500751 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500752 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500753 }
akmhoque157b0a42014-05-13 00:26:37 -0500754 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500755 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
756 ndn::time::system_clock::now();
757 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500758 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500759 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
760 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500761 alsa.getLsSeqNo(),
762 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500763 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500764 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500765 }
766 }
767 return true;
768}
769
770bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500771Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500772{
akmhoque31d1d4b2014-05-05 22:08:14 -0500773 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500774 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500775 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500776 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
777 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500778
Nick Gordon5c467f02016-07-13 13:40:10 -0500779 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
780 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500781 m_sequencingManager.increaseAdjLsaSeq();
782 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500783 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500784 }
Vince Lehman904c2412014-09-23 19:36:11 -0500785
Vince Lehman9d097802015-03-16 17:55:59 -0500786 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500787}
788
789bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500790Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500791{
792 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
793 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600794 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500795 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500796 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500797 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500798 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500799 m_adjLsdb.erase(it);
800 return true;
801 }
802 return false;
803}
804
805bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500806Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500807{
808 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
809 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600810 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500811 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500812 return false;
813 }
814 return true;
815}
816
Jiewen Tana0497d82015-02-02 21:59:18 -0800817const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500818Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500819{
820 return m_adjLsdb;
821}
822
823void
Nick Gordone98480b2017-05-24 11:23:03 -0500824Lsdb::setLsaRefreshTime(const ndn::time::seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500825{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700826 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500827}
828
829void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500830Lsdb::setThisRouterPrefix(std::string trp)
akmhoque53353462014-04-22 08:43:45 -0500831{
832 m_thisRouterPrefix = trp;
833}
834
Nick G97e34942016-07-11 14:46:27 -0500835 // This function determines whether a name LSA should be refreshed
836 // or expired. The conditions for getting refreshed are: it is still
837 // in the LSDB, it hasn't been updated by something else already (as
838 // evidenced by its seq. no.), and this is the originating router for
839 // the LSA. Is it let expire in all other cases.
840 // lsaKey is the key of the LSA's publishing router.
841 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500842void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500843Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500844{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500845 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
846 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500847 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500848 // If this name LSA exists in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500849 if (chkNameLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500850 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500851 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500852 if (chkNameLsa->getLsSeqNo() == seqNo) {
853 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500854 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
855 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500856 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500857 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500858 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500859 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500860 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500861 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500862 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500863 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500864 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700865 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500866 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500867 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500868 }
Nick G97e34942016-07-11 14:46:27 -0500869 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500870 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500871 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500872 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500873 }
874 }
875 }
876}
877
Nick G97e34942016-07-11 14:46:27 -0500878 // This function determines whether an adj. LSA should be refreshed
879 // or expired. The conditions for getting refreshed are: it is still
880 // in the LSDB, it hasn't been updated by something else already (as
881 // evidenced by its seq. no.), and this is the originating router for
882 // the LSA. Is it let expire in all other cases.
883 // lsaKey is the key of the LSA's publishing router.
884 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500885void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500886Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500887{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500888 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
889 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500890 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500891 // If this is a valid LSA
akmhoque157b0a42014-05-13 00:26:37 -0500892 if (chkAdjLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500893 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500894 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500895 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500896 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500897 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500898 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
899 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500900 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500901 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500902 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500903 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500904 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500905 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500906 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500907 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500908 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700909 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500910 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500911 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500912 }
Nick G97e34942016-07-11 14:46:27 -0500913 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500914 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500915 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500916 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500917 }
Nick G97e34942016-07-11 14:46:27 -0500918 // We have changed the contents of the LSDB, so we have to
919 // schedule a routing calculation
akmhoque31d1d4b2014-05-05 22:08:14 -0500920 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500921 }
922 }
923}
924
Nick G97e34942016-07-11 14:46:27 -0500925 // This function determines whether an adj. LSA should be refreshed
926 // or expired. The conditions for getting refreshed are: it is still
927 // in the LSDB, it hasn't been updated by something else already (as
928 // evidenced by its seq. no.), and this is the originating router for
929 // the LSA. It is let expire in all other cases.
930 // lsaKey is the key of the LSA's publishing router.
931 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500932void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500933Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500934 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500935{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500936 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
937 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500938 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500939 // Whether the LSA is in the LSDB or not.
akmhoque157b0a42014-05-13 00:26:37 -0500940 if (chkCorLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500941 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500942 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500943 if (chkCorLsa->getLsSeqNo() == seqNo) {
944 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500945 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
946 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500947 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500948 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Nick Gordon5c467f02016-07-13 13:40:10 -0500949 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500950 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500951 }
952
akmhoquec7a79b22014-05-26 08:06:19 -0500953 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500954 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500955 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500956 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500957 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
958 chkCorLsa->getKey(),
959 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700960 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500961 // Only sync coordinate LSAs if link-state routing is disabled
962 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500963 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500964 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500965 }
akmhoque53353462014-04-22 08:43:45 -0500966 }
Nick G97e34942016-07-11 14:46:27 -0500967 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500968 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500969 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500970 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500971 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500972 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500973 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500974 }
975 }
976 }
977}
978
akmhoque53353462014-04-22 08:43:45 -0500979void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700980Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500981 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500982{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600983 // increment SENT_LSA_INTEREST
984 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
985
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500986 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500987 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700988 }
Nick G97e34942016-07-11 14:46:27 -0500989 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500990 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500991 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500992 uint64_t seqNo = interestName[-1].toNumber();
993
Nick G97e34942016-07-11 14:46:27 -0500994 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500995 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
996 m_highestSeqNo[lsaName] = seqNo;
997 }
Nick G97e34942016-07-11 14:46:27 -0500998 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500999 else if (seqNo > m_highestSeqNo[lsaName]) {
1000 m_highestSeqNo[lsaName] = seqNo;
1001 }
Nick G97e34942016-07-11 14:46:27 -05001002 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001003 else if (seqNo < m_highestSeqNo[lsaName]) {
1004 return;
1005 }
1006
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001007 ndn::Interest interest(interestName);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -07001008 interest.setInterestLifetime(m_nlsr.getConfParameter().getLsaInterestLifetime());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001009
dmcoomes5bcb39e2017-10-31 15:07:55 -05001010 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001011 shared_ptr<ndn::util::SegmentFetcher> fetcher =
1012 ndn::util::SegmentFetcher::fetch(m_nlsr.getNlsrFace(), interest,
1013 m_nlsr.getValidator(),
1014 std::bind(&Lsdb::afterFetchLsa, this, _1, interestName),
1015 std::bind(&Lsdb::onFetchLsaError, this, _1, _2, interestName,
1016 timeoutCount, deadline, lsaName, seqNo));
1017
1018 m_lsaStorage.connectToFetcher(*fetcher);
1019 m_nlsr.connectToFetcher(*fetcher);
1020
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001021 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001022 Lsa::Type lsaType;
1023 std::istringstream(interestName[-2].toUri()) >> lsaType;
1024 switch (lsaType) {
1025 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001026 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001027 break;
1028 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001029 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001030 break;
1031 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001032 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001033 break;
1034 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001035 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001036 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001037}
1038
1039void
1040Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1041{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001042 // increment RCV_LSA_INTEREST
1043 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1044
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001045 const ndn::Name& interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -05001046 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001047
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001048 std::string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001049 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
1050
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001051 // Forms the name of the router that the Interest packet came from.
1052 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
1053 originRouter.append(interestName.getSubName(lsaPosition + 1,
1054 interest.getName().size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001055
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001056 // if the interest is for this router's LSA
1057 if (originRouter == m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001058
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001059 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001060
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001061 uint64_t seqNo = interestName[-1].toNumber();
1062 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001063
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001064 Lsa::Type interestedLsType;
1065 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
1066
1067 if (interestedLsType == Lsa::Type::NAME) {
1068 processInterestForNameLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1069 seqNo);
1070 }
1071 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1072 processInterestForAdjacencyLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1073 seqNo);
1074 }
1075 else if (interestedLsType == Lsa::Type::COORDINATE) {
1076 processInterestForCoordinateLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1077 seqNo);
1078 }
1079 else {
1080 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1081 }
1082 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001083 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001084 }
1085 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
1086 const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
1087 if (lsaSegment != nullptr) {
1088 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
1089 m_nlsr.getNlsrFace().put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001090 }
akmhoque157b0a42014-05-13 00:26:37 -05001091 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001092 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001093 }
1094 }
1095}
1096
Nick G97e34942016-07-11 14:46:27 -05001097 // \brief Sends LSA data.
1098 // \param interest The Interest that warranted the data.
1099 // \param content The data that the Interest was seeking.
akmhoque31d1d4b2014-05-05 22:08:14 -05001100void
akmhoque69c9aa92014-07-23 15:15:05 -05001101Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
1102{
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001103 LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
1104 m_nlsr.getKeyChain(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001105 m_nlsr.getSigningInfo(),
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001106 m_lsaRefreshTime,
1107 content);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001108 NLSR_LOG_DEBUG("Sending requested data ( " << content << ") for interest (" << interest
dmcoomes9eaf3f42017-02-21 11:39:01 -06001109 << ") to be published and added to face.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001110 publisher.publish(interest.getName());
akmhoque69c9aa92014-07-23 15:15:05 -05001111}
1112
Nick G97e34942016-07-11 14:46:27 -05001113 // \brief Finds and sends a requested name LSA.
1114 // \param interest The interest that seeks the name LSA.
1115 // \param lsaKey The LSA that the Interest is seeking.
1116 // \param seqNo A sequence number to ensure that we are sending the
1117 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001118void
akmhoque31d1d4b2014-05-05 22:08:14 -05001119Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1120 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001121 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001122{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001123 // increment RCV_NAME_LSA_INTEREST
1124 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001125 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001126 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001127 if (nameLsa != nullptr) {
1128 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001129 if (nameLsa->getLsSeqNo() == seqNo) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001130 // if requested lsa belongs to this router then sign it and serve it
Nick Gordonfaf49f42017-10-23 12:36:28 -05001131 std::string content = nameLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001132 putLsaData(interest,content);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001133 // else the requested belongs to neighboring routers, so serve the
1134 // original data packet corresponding to the lsa
1135
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001136 // increment SENT_NAME_LSA_DATA
1137 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001138 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001139 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001140 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001141 }
1142 }
1143 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001144 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001145 }
1146}
1147
Nick G97e34942016-07-11 14:46:27 -05001148 // \brief Finds and sends a requested adj. LSA.
1149 // \param interest The interest that seeks the adj. LSA.
1150 // \param lsaKey The LSA that the Interest is seeking.
1151 // \param seqNo A sequence number to ensure that we are sending the
1152 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001153void
1154Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1155 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001156 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001157{
Nick Gordon5c467f02016-07-13 13:40:10 -05001158 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001159 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001160 }
1161
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001162 // increment RCV_ADJ_LSA_INTEREST
1163 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001164 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001165 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001166 if (adjLsa != nullptr) {
1167 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001168 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001169 std::string content = adjLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001170 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001171 // increment SENT_ADJ_LSA_DATA
1172 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001173 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001174 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001175 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001176 }
1177 }
1178 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001179 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001180 }
1181}
1182
Nick G97e34942016-07-11 14:46:27 -05001183 // \brief Finds and sends a requested cor. LSA.
1184 // \param interest The interest that seeks the cor. LSA.
1185 // \param lsaKey The LSA that the Interest is seeking.
1186 // \param seqNo A sequence number to ensure that we are sending the
1187 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001188void
1189Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1190 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001191 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001192{
Nick Gordon5c467f02016-07-13 13:40:10 -05001193 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001194 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001195 }
1196
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001197 // increment RCV_COORD_LSA_INTEREST
1198 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001199 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001200 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001201 if (corLsa != nullptr) {
1202 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001203 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001204 std::string content = corLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001205 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001206 // increment SENT_COORD_LSA_DATA
1207 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001208 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001209 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001210 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001211 }
1212 }
1213 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001214 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001215 }
1216}
1217
1218void
dmcoomes9f936662017-03-02 10:33:09 -06001219Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001220{
1221 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001222 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001223
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001224 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001225 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001226
akmhoque157b0a42014-05-13 00:26:37 -05001227 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001228
Nick G97e34942016-07-11 14:46:27 -05001229 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001230 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001231 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001232
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001233 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001234 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1235 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001236
Nick Gordon727d4832017-10-13 18:04:25 -05001237 Lsa::Type interestedLsType;
1238 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001239
Nick Gordon727d4832017-10-13 18:04:25 -05001240 if (interestedLsType == Lsa::Type::NAME) {
1241 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1242 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001243 }
Nick Gordon727d4832017-10-13 18:04:25 -05001244 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1245 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1246 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001247 }
Nick Gordon727d4832017-10-13 18:04:25 -05001248 else if (interestedLsType == Lsa::Type::COORDINATE) {
1249 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1250 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001251 }
akmhoque157b0a42014-05-13 00:26:37 -05001252 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001253 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001254 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001255
1256 // increment RCV_LSA_DATA
1257 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001258 }
1259}
1260
1261void
1262Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001263 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001264{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001265 // increment RCV_NAME_LSA_DATA
1266 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001267 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001268 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001269 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001270 installNameLsa(nameLsa);
1271 }
akmhoque157b0a42014-05-13 00:26:37 -05001272 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001273 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001274 }
1275 }
1276}
1277
1278void
1279Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001280 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001281{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001282 // increment RCV_ADJ_LSA_DATA
1283 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001284 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001285 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001286 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001287 installAdjLsa(adjLsa);
1288 }
akmhoque157b0a42014-05-13 00:26:37 -05001289 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001290 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001291 }
1292 }
1293}
1294
1295void
1296Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001297 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001298{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001299 // increment RCV_COORD_LSA_DATA
1300 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001301 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001302 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001303 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001304 installCoordinateLsa(corLsa);
1305 }
akmhoque157b0a42014-05-13 00:26:37 -05001306 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001307 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001308 }
1309 }
1310}
1311
akmhoquec7a79b22014-05-26 08:06:19 -05001312ndn::time::system_clock::TimePoint
1313Lsdb::getLsaExpirationTimePoint()
1314{
1315 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1316 expirationTimePoint = expirationTimePoint +
1317 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1318 return expirationTimePoint;
1319}
akmhoque31d1d4b2014-05-05 22:08:14 -05001320
1321void
akmhoque2f423352014-06-03 11:49:35 -05001322Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001323{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001324 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -05001325 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -05001326 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -05001327 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -05001328 }
1329}
1330
1331//-----utility function -----
1332bool
Nick Gordon727d4832017-10-13 18:04:25 -05001333Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001334{
Nick Gordon727d4832017-10-13 18:04:25 -05001335 switch (lsType) {
1336 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001337 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001338 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001339 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001340 case Lsa::Type::NAME:
1341 return doesNameLsaExist(key);
1342 default:
1343 return false;
akmhoque53353462014-04-22 08:43:45 -05001344 }
akmhoque53353462014-04-22 08:43:45 -05001345}
1346
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001347bool
Nick Gordon727d4832017-10-13 18:04:25 -05001348Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001349 const uint64_t& sequenceNumber) {
1350 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001351 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001352
Nick Gordon727d4832017-10-13 18:04:25 -05001353 switch (lsaType) {
1354 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001355 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001356 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001357 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001358 case Lsa::Type::NAME:
1359 return isNameLsaNew(lsaKey, sequenceNumber);
1360 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001361 return false;
1362 }
1363}
1364
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001365} // namespace nlsr