blob: b8cda8ed99268c0f556a8f8ce2236e6f76aaa879 [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>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050031
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
dmcoomescf8d0ed2017-02-21 11:39:01 -060034INIT_LOGGER(Lsdb);
akmhoque674b0b12014-05-20 14:33:28 -050035
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050036class LsaContentPublisher : public SegmentPublisher<ndn::Face>
37{
38public:
39 LsaContentPublisher(ndn::Face& face,
40 ndn::KeyChain& keyChain,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050041 const ndn::security::SigningInfo& signingInfo,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050042 const ndn::time::milliseconds& freshnessPeriod,
43 const std::string& content)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050044 : SegmentPublisher(face, keyChain, signingInfo, freshnessPeriod)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050045 , m_content(content)
46 {
47 }
48
49 virtual size_t
50 generate(ndn::EncodingBuffer& outBuffer) {
51 size_t totalLength = 0;
52 totalLength += outBuffer.prependByteArray(reinterpret_cast<const uint8_t*>(m_content.c_str()),
53 m_content.size());
54 return totalLength;
55 }
56
57private:
58 const std::string m_content;
59};
60
Jiewen Tana0497d82015-02-02 21:59:18 -080061const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050062const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Nick Gordone98480b2017-05-24 11:23:03 -050063const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
64 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050065
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050066Lsdb::Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050067 : m_nlsr(nlsr)
68 , m_scheduler(scheduler)
Nick Gordon9eac4d92017-08-29 17:31:29 -050069 , m_sync(m_nlsr.getNlsrFace(),
Nick Gordon727d4832017-10-13 18:04:25 -050070 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050071 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050072 return isLsaNew(routerName, lsaType, sequenceNumber);
73 }, m_nlsr.getConfParameter())
Ashlesh Gawande8c6d8c82018-02-28 21:41:31 -060074 , m_lsaStorage(scheduler)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050075 , m_lsaRefreshTime(0)
76 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050077 , m_sequencingManager()
Nick Gordon9eac4d92017-08-29 17:31:29 -050078 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
79 [this] (const ndn::Name& updateName, const uint64_t& sequenceNumber) {
80 ndn::Name lsaInterest{updateName};
81 lsaInterest.appendNumber(sequenceNumber);
82 expressInterest(lsaInterest, 0);
83 }))
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050084{
85}
86
Ashlesh Gawande744e4812018-08-22 16:26:24 -050087Lsdb::~Lsdb()
88{
89 for (const auto& sp : m_fetchers) {
90 sp->stop();
91 }
92}
93
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050094void
95Lsdb::onFetchLsaError(uint32_t errorCode,
96 const std::string& msg,
Ashlesh Gawande744e4812018-08-22 16:26:24 -050097 const ndn::Name& interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050098 uint32_t retransmitNo,
99 const ndn::time::steady_clock::TimePoint& deadline,
100 ndn::Name lsaName,
101 uint64_t seqNo)
102{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500103 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500104 << ", Message: " << msg);
105
106 if (ndn::time::steady_clock::now() < deadline) {
107 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
108
109 if (it != m_highestSeqNo.end() && it->second == seqNo) {
110 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
111 // immediately since at the least the LSA Interest lifetime has elapsed.
112 // Otherwise, it is necessary to delay the Interest re-expression to prevent
113 // the potential for constant Interest flooding.
114 ndn::time::seconds delay = m_nlsr.getConfParameter().getLsaInterestLifetime();
115
116 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
117 delay = ndn::time::seconds(0);
118 }
119
120 m_scheduler.scheduleEvent(delay, std::bind(&Lsdb::expressInterest, this,
121 interestName, retransmitNo + 1, deadline));
122 }
123 }
124}
125
126void
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500127Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500128{
dmcoomes9f936662017-03-02 10:33:09 -0600129 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500130 data->setContent(bufferPtr);
131
dmcoomes5bcb39e2017-10-31 15:07:55 -0500132 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500133
134 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
135 uint64_t seqNo = interestName[-1].toNumber();
136
137 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
138 m_highestSeqNo[lsaName] = seqNo;
139 }
140 else if (seqNo > m_highestSeqNo[lsaName]) {
141 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500142 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500143 }
144 else if (seqNo < m_highestSeqNo[lsaName]) {
145 return;
146 }
147
148 onContentValidated(data);
149}
akmhoque53353462014-04-22 08:43:45 -0500150
151void
akmhoque31d1d4b2014-05-05 22:08:14 -0500152Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -0500153{
Vince Lehman7c603292014-09-11 17:48:16 -0500154 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -0500155}
156
Nick G97e34942016-07-11 14:46:27 -0500157 /*! \brief Compares if a name LSA is the same as the one specified by key
158
159 \param nlsa1 A name LSA object
160 \param key A key of an originating router to compare to nlsa1
161 */
akmhoque53353462014-04-22 08:43:45 -0500162static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500163nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500164{
165 return nlsa1.getKey() == key;
166}
167
akmhoque53353462014-04-22 08:43:45 -0500168bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500169Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500170{
akmhoque31d1d4b2014-05-05 22:08:14 -0500171 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500172 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500173 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 m_nlsr.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500175 m_sequencingManager.increaseNameLsaSeq();
176
177 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500178 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500179
akmhoque31d1d4b2014-05-05 22:08:14 -0500180 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500181}
182
akmhoqueb6450b12014-04-24 00:01:03 -0500183NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500184Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500185{
186 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
187 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600188 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500189 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500190 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500191 }
akmhoqueb6450b12014-04-24 00:01:03 -0500192 return 0;
akmhoque53353462014-04-22 08:43:45 -0500193}
194
195bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500196Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500197{
akmhoqueb6450b12014-04-24 00:01:03 -0500198 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500199 // Is the name in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500200 if (nameLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500201 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500202 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500203 return true;
204 }
akmhoque157b0a42014-05-13 00:26:37 -0500205 else {
akmhoque53353462014-04-22 08:43:45 -0500206 return false;
207 }
208 }
209 return true;
210}
211
212ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500213Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
214 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500215{
Vince Lehman7c603292014-09-11 17:48:16 -0500216 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500217 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500218}
219
220bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500221Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500222{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000223 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700224 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500225 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500226 // Determines if the name LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500227 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500228 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500229 NLSR_LOG_DEBUG("New Name LSA");
230 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500231 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500232
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000233 NLSR_LOG_TRACE("nlsa.getOrigRouter(): " << nlsa.getOrigRouter());
234 NLSR_LOG_TRACE("m_nlsr.getConfParameter().getRouterPrefix(): " << m_nlsr.getConfParameter().getRouterPrefix());
235
akmhoque157b0a42014-05-13 00:26:37 -0500236 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500237 // If this name LSA is from another router, add the advertised
238 // prefixes to the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500239 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
240 nlsa.getOrigRouter());
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500241 for (const auto& name : nlsa.getNpl().getNames()) {
242 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
243 m_nlsr.getNamePrefixTable().addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500244 }
245 }
246 }
akmhoque157b0a42014-05-13 00:26:37 -0500247 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500248 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
249 ndn::time::system_clock::now();
250 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500251 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500252 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500253 nlsa.getLsSeqNo(),
254 timeToExpire));
255 }
Nick G97e34942016-07-11 14:46:27 -0500256 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500257 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000258 NLSR_LOG_TRACE("Known name lsa");
259 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
260 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500261 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500262 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
263 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500264 chkNameLsa->writeLog();
265 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500266 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500267 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500268 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500269 // Obtain the set difference of the current and the incoming
270 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500271 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
272 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
273 std::list<ndn::Name> namesToAdd;
274 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
275 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500276 for (const auto& name : namesToAdd) {
277 chkNameLsa->addName(name);
akmhoque157b0a42014-05-13 00:26:37 -0500278 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500279 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
280 m_nlsr.getNamePrefixTable().addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500281 }
282 }
283 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500284
285 chkNameLsa->getNpl().sort();
286
Nick G97e34942016-07-11 14:46:27 -0500287 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500288 std::list<ndn::Name> namesToRemove;
289 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
290 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500291 for (const auto& name : namesToRemove) {
292 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
293 chkNameLsa->removeName(name);
akmhoque157b0a42014-05-13 00:26:37 -0500294 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500295 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
296 m_nlsr.getNamePrefixTable().removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500297 }
298 }
299 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600300
akmhoque157b0a42014-05-13 00:26:37 -0500301 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500302 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
303 ndn::time::system_clock::now();
304 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500305 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500306 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
307 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500308 nlsa.getLsSeqNo(),
309 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500310 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500311 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500312 }
313 }
314 return true;
315}
316
317bool
318Lsdb::addNameLsa(NameLsa& nlsa)
319{
320 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
321 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600322 std::bind(nameLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500323 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500324 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500325 m_nameLsdb.push_back(nlsa);
326 return true;
327 }
328 return false;
329}
330
331bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500332Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500333{
334 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
335 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600336 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500337 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500338 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500339 (*it).writeLog();
Nick G97e34942016-07-11 14:46:27 -0500340 // If the requested name LSA is not ours, we also need to remove
341 // its entries from the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500342 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500343 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500344 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
345 (*it).getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500346 for (const auto& name : it->getNpl().getNames()) {
347 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
348 m_nlsr.getNamePrefixTable().removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500349 }
350 }
351 }
352 m_nameLsdb.erase(it);
353 return true;
354 }
355 return false;
356}
357
358bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500359Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500360{
361 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
362 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600363 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500364 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500365 return false;
366 }
367 return true;
368}
369
370void
akmhoque2f423352014-06-03 11:49:35 -0500371Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500372{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500373 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500374 for (const auto& nlsa : m_nameLsdb) {
375 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500376 }
377}
378
Jiewen Tana0497d82015-02-02 21:59:18 -0800379const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500380Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800381{
382 return m_nameLsdb;
383}
384
akmhoque53353462014-04-22 08:43:45 -0500385// Cor LSA and LSDB related Functions start here
386
Nick G97e34942016-07-11 14:46:27 -0500387/*! \brief Compares whether an LSA object is the same as a key.
388 \param clsa The cor. LSA to check the identity of.
389 \param key The key of the publishing router to check against.
390*/
akmhoque53353462014-04-22 08:43:45 -0500391static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500392corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500393{
394 return clsa.getKey() == key;
395}
396
397bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500398Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500399{
akmhoque31d1d4b2014-05-05 22:08:14 -0500400 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500401 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500402 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500403 m_nlsr.getConfParameter().getCorR(),
404 m_nlsr.getConfParameter().getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500405
406 // Sync coordinate LSAs if using HR or HR dry run.
407 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500408 m_sequencingManager.increaseCorLsaSeq();
409 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500410 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500411 }
412
akmhoque31d1d4b2014-05-05 22:08:14 -0500413 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500414
akmhoque53353462014-04-22 08:43:45 -0500415 return true;
416}
417
akmhoqueb6450b12014-04-24 00:01:03 -0500418CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500419Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500420{
akmhoqueb6450b12014-04-24 00:01:03 -0500421 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
422 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600423 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500424 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500425 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500426 }
akmhoqueb6450b12014-04-24 00:01:03 -0500427 return 0;
akmhoque53353462014-04-22 08:43:45 -0500428}
429
430bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500431Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500432{
akmhoqueb6450b12014-04-24 00:01:03 -0500433 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500434 // Is the coordinate LSA in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500435 if (clsa != 0) {
Nick G97e34942016-07-11 14:46:27 -0500436 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500437 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500438 return true;
439 }
akmhoque157b0a42014-05-13 00:26:37 -0500440 else {
akmhoque53353462014-04-22 08:43:45 -0500441 return false;
442 }
443 }
444 return true;
445}
446
Nick G97e34942016-07-11 14:46:27 -0500447 // Schedules a refresh/expire event in the scheduler.
448 // \param key The name of the router that published the LSA.
449 // \param seqNo the seq. no. associated with the LSA to check.
450 // \param expTime How long to wait before triggering the event.
akmhoque53353462014-04-22 08:43:45 -0500451ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500452Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500453 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500454{
Vince Lehman7c603292014-09-11 17:48:16 -0500455 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500456 std::bind(&Lsdb::expireOrRefreshCoordinateLsa,
Vince Lehman7c603292014-09-11 17:48:16 -0500457 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500458}
459
460bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500461Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500462{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700463 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500464 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500465 // Checking whether the LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500466 if (chkCorLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500467 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
468 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500469 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500470 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500471
Nick Gordon5c467f02016-07-13 13:40:10 -0500472 // Register the LSA's origin router prefix
akmhoque157b0a42014-05-13 00:26:37 -0500473 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500474 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
475 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500476 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500477 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500478 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500479 }
Nick G97e34942016-07-11 14:46:27 -0500480 // Set the expiration time for the new LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500481 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500482 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
483 ndn::time::system_clock::now();
484 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500485 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500486 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500487 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500488 }
Nick G97e34942016-07-11 14:46:27 -0500489 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500490 else {
491 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500492 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
493 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500494 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500495 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500496 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500497 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500498 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500499 chkCorLsa->setCorRadius(clsa.getCorRadius());
500 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500501 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500502 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500503 }
504 }
Nick G97e34942016-07-11 14:46:27 -0500505 // If this is an LSA from another router, refresh its expiration time.
akmhoque157b0a42014-05-13 00:26:37 -0500506 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500507 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
508 ndn::time::system_clock::now();
509 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500510 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500511 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
512 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500513 clsa.getLsSeqNo(),
514 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500515 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500516 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500517 }
518 }
519 return true;
520}
521
522bool
akmhoqueb6450b12014-04-24 00:01:03 -0500523Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500524{
akmhoqueb6450b12014-04-24 00:01:03 -0500525 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
526 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600527 std::bind(corLsaCompareByKey, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500528 clsa.getKey()));
529 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500530 m_corLsdb.push_back(clsa);
531 return true;
532 }
533 return false;
534}
535
536bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500537Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500538{
akmhoqueb6450b12014-04-24 00:01:03 -0500539 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
540 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600541 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500542 _1, key));
543 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500544 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500545 it->writeLog();
546
547 if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
548 m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500549 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500550
akmhoque53353462014-04-22 08:43:45 -0500551 m_corLsdb.erase(it);
552 return true;
553 }
554 return false;
555}
556
557bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500558Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500559{
akmhoqueb6450b12014-04-24 00:01:03 -0500560 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
561 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600562 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500563 _1, key));
564 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500565 return false;
566 }
567 return true;
568}
569
570void
akmhoque2f423352014-06-03 11:49:35 -0500571Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500572{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500573 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500574 for (const auto& corLsa : m_corLsdb) {
575 corLsa.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);
Ashlesh Gawande744e4812018-08-22 16:26:24 -05001008 ndn::util::SegmentFetcher::Options options;
1009 options.interestLifetime = m_nlsr.getConfParameter().getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001010
dmcoomes5bcb39e2017-10-31 15:07:55 -05001011 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -05001012 auto fetcher = ndn::util::SegmentFetcher::start(m_nlsr.getNlsrFace(),
Ashlesh Gawande744e4812018-08-22 16:26:24 -05001013 interest, m_nlsr.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -05001014
Ashlesh Gawande744e4812018-08-22 16:26:24 -05001015 auto it = m_fetchers.insert(fetcher).first;
1016
1017 fetcher->onComplete.connect([this, interestName, it] (ndn::ConstBufferPtr bufferPtr) {
1018 afterFetchLsa(bufferPtr, interestName);
1019 m_fetchers.erase(it);
1020 });
1021
1022 fetcher->onError.connect([this, interestName, timeoutCount, deadline, lsaName, seqNo, &fetcher, it]
1023 (uint32_t errorCode, const std::string& msg) {
1024 onFetchLsaError(errorCode, msg, interestName,
1025 timeoutCount, deadline, lsaName, seqNo);
1026 m_fetchers.erase(it);
1027 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001028
1029 m_lsaStorage.connectToFetcher(*fetcher);
1030 m_nlsr.connectToFetcher(*fetcher);
1031
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001032 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001033 Lsa::Type lsaType;
1034 std::istringstream(interestName[-2].toUri()) >> lsaType;
1035 switch (lsaType) {
1036 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001037 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001038 break;
1039 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001040 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001041 break;
1042 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001043 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001044 break;
1045 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001046 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001047 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001048}
1049
1050void
1051Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1052{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001053 // increment RCV_LSA_INTEREST
1054 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1055
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001056 const ndn::Name& interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -05001057 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001058
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001059 std::string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001060 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
1061
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001062 // Forms the name of the router that the Interest packet came from.
1063 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
1064 originRouter.append(interestName.getSubName(lsaPosition + 1,
1065 interest.getName().size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001066
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001067 // if the interest is for this router's LSA
1068 if (originRouter == m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001069
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001070 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001071
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001072 uint64_t seqNo = interestName[-1].toNumber();
1073 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001074
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001075 Lsa::Type interestedLsType;
1076 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
1077
1078 if (interestedLsType == Lsa::Type::NAME) {
1079 processInterestForNameLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1080 seqNo);
1081 }
1082 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1083 processInterestForAdjacencyLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1084 seqNo);
1085 }
1086 else if (interestedLsType == Lsa::Type::COORDINATE) {
1087 processInterestForCoordinateLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1088 seqNo);
1089 }
1090 else {
1091 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1092 }
1093 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001094 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001095 }
1096 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
1097 const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
1098 if (lsaSegment != nullptr) {
1099 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
1100 m_nlsr.getNlsrFace().put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001101 }
akmhoque157b0a42014-05-13 00:26:37 -05001102 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001103 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001104 }
1105 }
1106}
1107
Nick G97e34942016-07-11 14:46:27 -05001108 // \brief Sends LSA data.
1109 // \param interest The Interest that warranted the data.
1110 // \param content The data that the Interest was seeking.
akmhoque31d1d4b2014-05-05 22:08:14 -05001111void
akmhoque69c9aa92014-07-23 15:15:05 -05001112Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
1113{
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001114 LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
1115 m_nlsr.getKeyChain(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001116 m_nlsr.getSigningInfo(),
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001117 m_lsaRefreshTime,
1118 content);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001119 NLSR_LOG_DEBUG("Sending requested data ( " << content << ") for interest (" << interest
dmcoomes9eaf3f42017-02-21 11:39:01 -06001120 << ") to be published and added to face.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001121 publisher.publish(interest.getName());
akmhoque69c9aa92014-07-23 15:15:05 -05001122}
1123
Nick G97e34942016-07-11 14:46:27 -05001124 // \brief Finds and sends a requested name LSA.
1125 // \param interest The interest that seeks the name LSA.
1126 // \param lsaKey The LSA that the Interest is seeking.
1127 // \param seqNo A sequence number to ensure that we are sending the
1128 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001129void
akmhoque31d1d4b2014-05-05 22:08:14 -05001130Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1131 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001132 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001133{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001134 // increment RCV_NAME_LSA_INTEREST
1135 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001136 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001137 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001138 if (nameLsa != nullptr) {
1139 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001140 if (nameLsa->getLsSeqNo() == seqNo) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001141 // if requested lsa belongs to this router then sign it and serve it
Nick Gordonfaf49f42017-10-23 12:36:28 -05001142 std::string content = nameLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001143 putLsaData(interest,content);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001144 // else the requested belongs to neighboring routers, so serve the
1145 // original data packet corresponding to the lsa
1146
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001147 // increment SENT_NAME_LSA_DATA
1148 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001149 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001150 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001151 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001152 }
1153 }
1154 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001155 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001156 }
1157}
1158
Nick G97e34942016-07-11 14:46:27 -05001159 // \brief Finds and sends a requested adj. LSA.
1160 // \param interest The interest that seeks the adj. LSA.
1161 // \param lsaKey The LSA that the Interest is seeking.
1162 // \param seqNo A sequence number to ensure that we are sending the
1163 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001164void
1165Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1166 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001167 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001168{
Nick Gordon5c467f02016-07-13 13:40:10 -05001169 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001170 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001171 }
1172
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001173 // increment RCV_ADJ_LSA_INTEREST
1174 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001175 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001176 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001177 if (adjLsa != nullptr) {
1178 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001179 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001180 std::string content = adjLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001181 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001182 // increment SENT_ADJ_LSA_DATA
1183 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001184 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001185 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001186 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001187 }
1188 }
1189 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001190 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001191 }
1192}
1193
Nick G97e34942016-07-11 14:46:27 -05001194 // \brief Finds and sends a requested cor. LSA.
1195 // \param interest The interest that seeks the cor. LSA.
1196 // \param lsaKey The LSA that the Interest is seeking.
1197 // \param seqNo A sequence number to ensure that we are sending the
1198 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001199void
1200Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1201 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001202 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001203{
Nick Gordon5c467f02016-07-13 13:40:10 -05001204 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001205 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001206 }
1207
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001208 // increment RCV_COORD_LSA_INTEREST
1209 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001210 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001211 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001212 if (corLsa != nullptr) {
1213 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001214 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001215 std::string content = corLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001216 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001217 // increment SENT_COORD_LSA_DATA
1218 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001219 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001220 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001221 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001222 }
1223 }
1224 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001225 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001226 }
1227}
1228
1229void
dmcoomes9f936662017-03-02 10:33:09 -06001230Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001231{
1232 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001233 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001234
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001235 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001236 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001237
akmhoque157b0a42014-05-13 00:26:37 -05001238 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001239
Nick G97e34942016-07-11 14:46:27 -05001240 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001241 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001242 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001243
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001244 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001245 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1246 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001247
Nick Gordon727d4832017-10-13 18:04:25 -05001248 Lsa::Type interestedLsType;
1249 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001250
Nick Gordon727d4832017-10-13 18:04:25 -05001251 if (interestedLsType == Lsa::Type::NAME) {
1252 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1253 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001254 }
Nick Gordon727d4832017-10-13 18:04:25 -05001255 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1256 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1257 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001258 }
Nick Gordon727d4832017-10-13 18:04:25 -05001259 else if (interestedLsType == Lsa::Type::COORDINATE) {
1260 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1261 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001262 }
akmhoque157b0a42014-05-13 00:26:37 -05001263 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001264 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001265 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001266
1267 // increment RCV_LSA_DATA
1268 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001269 }
1270}
1271
1272void
1273Lsdb::processContentNameLsa(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_NAME_LSA_DATA
1277 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001278 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001279 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001280 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001281 installNameLsa(nameLsa);
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::processContentAdjacencyLsa(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_ADJ_LSA_DATA
1294 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001295 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001296 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001297 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001298 installAdjLsa(adjLsa);
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
1306void
1307Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001308 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001309{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001310 // increment RCV_COORD_LSA_DATA
1311 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001312 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001313 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001314 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001315 installCoordinateLsa(corLsa);
1316 }
akmhoque157b0a42014-05-13 00:26:37 -05001317 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001318 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001319 }
1320 }
1321}
1322
akmhoquec7a79b22014-05-26 08:06:19 -05001323ndn::time::system_clock::TimePoint
1324Lsdb::getLsaExpirationTimePoint()
1325{
1326 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1327 expirationTimePoint = expirationTimePoint +
1328 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1329 return expirationTimePoint;
1330}
akmhoque31d1d4b2014-05-05 22:08:14 -05001331
1332void
akmhoque2f423352014-06-03 11:49:35 -05001333Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001334{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001335 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001336 for (const auto& adj : m_adjLsdb) {
1337 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001338 }
1339}
1340
1341//-----utility function -----
1342bool
Nick Gordon727d4832017-10-13 18:04:25 -05001343Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001344{
Nick Gordon727d4832017-10-13 18:04:25 -05001345 switch (lsType) {
1346 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001347 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001348 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001349 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001350 case Lsa::Type::NAME:
1351 return doesNameLsaExist(key);
1352 default:
1353 return false;
akmhoque53353462014-04-22 08:43:45 -05001354 }
akmhoque53353462014-04-22 08:43:45 -05001355}
1356
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001357bool
Nick Gordon727d4832017-10-13 18:04:25 -05001358Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001359 const uint64_t& sequenceNumber) {
1360 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001361 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001362
Nick Gordon727d4832017-10-13 18:04:25 -05001363 switch (lsaType) {
1364 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001365 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001366 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001367 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001368 case Lsa::Type::NAME:
1369 return isNameLsaNew(lsaKey, sequenceNumber);
1370 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001371 return false;
1372 }
1373}
1374
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001375} // namespace nlsr