blob: 5ebef485bdbd541f3dc2c7a80577c48418a44708 [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())
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000075 , m_lsaStorage(scheduler,
76 ndn::time::seconds(m_nlsr.getConfParameter().getLsaRefreshTime()))
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050077 , m_lsaRefreshTime(0)
78 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050079 , m_sequencingManager()
Nick Gordon9eac4d92017-08-29 17:31:29 -050080 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
81 [this] (const ndn::Name& updateName, const uint64_t& sequenceNumber) {
82 ndn::Name lsaInterest{updateName};
83 lsaInterest.appendNumber(sequenceNumber);
84 expressInterest(lsaInterest, 0);
85 }))
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050086{
87}
88
89void
90Lsdb::onFetchLsaError(uint32_t errorCode,
91 const std::string& msg,
92 ndn::Name& interestName,
93 uint32_t retransmitNo,
94 const ndn::time::steady_clock::TimePoint& deadline,
95 ndn::Name lsaName,
96 uint64_t seqNo)
97{
dmcoomes5bcb39e2017-10-31 15:07:55 -050098 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050099 << ", Message: " << msg);
100
101 if (ndn::time::steady_clock::now() < deadline) {
102 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
103
104 if (it != m_highestSeqNo.end() && it->second == seqNo) {
105 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
106 // immediately since at the least the LSA Interest lifetime has elapsed.
107 // Otherwise, it is necessary to delay the Interest re-expression to prevent
108 // the potential for constant Interest flooding.
109 ndn::time::seconds delay = m_nlsr.getConfParameter().getLsaInterestLifetime();
110
111 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
112 delay = ndn::time::seconds(0);
113 }
114
115 m_scheduler.scheduleEvent(delay, std::bind(&Lsdb::expressInterest, this,
116 interestName, retransmitNo + 1, deadline));
117 }
118 }
119}
120
121void
122Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, ndn::Name& interestName)
123{
dmcoomes9f936662017-03-02 10:33:09 -0600124 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500125 data->setContent(bufferPtr);
126
dmcoomes5bcb39e2017-10-31 15:07:55 -0500127 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500128
129 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
130 uint64_t seqNo = interestName[-1].toNumber();
131
132 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
133 m_highestSeqNo[lsaName] = seqNo;
134 }
135 else if (seqNo > m_highestSeqNo[lsaName]) {
136 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500137 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500138 }
139 else if (seqNo < m_highestSeqNo[lsaName]) {
140 return;
141 }
142
143 onContentValidated(data);
144}
akmhoque53353462014-04-22 08:43:45 -0500145
146void
akmhoque31d1d4b2014-05-05 22:08:14 -0500147Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -0500148{
Vince Lehman7c603292014-09-11 17:48:16 -0500149 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -0500150}
151
Nick G97e34942016-07-11 14:46:27 -0500152 /*! \brief Compares if a name LSA is the same as the one specified by key
153
154 \param nlsa1 A name LSA object
155 \param key A key of an originating router to compare to nlsa1
156 */
akmhoque53353462014-04-22 08:43:45 -0500157static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500158nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500159{
160 return nlsa1.getKey() == key;
161}
162
akmhoque53353462014-04-22 08:43:45 -0500163bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500164Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500165{
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500167 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500168 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500169 m_nlsr.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500170 m_sequencingManager.increaseNameLsaSeq();
171
172 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500173 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500174
akmhoque31d1d4b2014-05-05 22:08:14 -0500175 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500176}
177
akmhoqueb6450b12014-04-24 00:01:03 -0500178NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500179Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500180{
181 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
182 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600183 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500184 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500185 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500186 }
akmhoqueb6450b12014-04-24 00:01:03 -0500187 return 0;
akmhoque53353462014-04-22 08:43:45 -0500188}
189
190bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500191Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500192{
akmhoqueb6450b12014-04-24 00:01:03 -0500193 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500194 // Is the name in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500195 if (nameLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500196 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500197 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500198 return true;
199 }
akmhoque157b0a42014-05-13 00:26:37 -0500200 else {
akmhoque53353462014-04-22 08:43:45 -0500201 return false;
202 }
203 }
204 return true;
205}
206
207ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500208Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
209 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500210{
Vince Lehman7c603292014-09-11 17:48:16 -0500211 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500212 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500213}
214
215bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500216Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500217{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000218 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700219 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500220 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500221 // Determines if the name LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500222 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500223 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500224 NLSR_LOG_DEBUG("New Name LSA");
225 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500226 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500227
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000228 NLSR_LOG_TRACE("nlsa.getOrigRouter(): " << nlsa.getOrigRouter());
229 NLSR_LOG_TRACE("m_nlsr.getConfParameter().getRouterPrefix(): " << m_nlsr.getConfParameter().getRouterPrefix());
230
akmhoque157b0a42014-05-13 00:26:37 -0500231 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500232 // If this name LSA is from another router, add the advertised
233 // prefixes to the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500234 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
235 nlsa.getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500236 std::list<ndn::Name> nameList = nlsa.getNpl().getNames();
akmhoque31d1d4b2014-05-05 22:08:14 -0500237 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -0500238 it++) {
239 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500240 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500241 }
242 }
243 }
akmhoque157b0a42014-05-13 00:26:37 -0500244 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500245 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
246 ndn::time::system_clock::now();
247 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500248 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500249 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500250 nlsa.getLsSeqNo(),
251 timeToExpire));
252 }
Nick G97e34942016-07-11 14:46:27 -0500253 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500254 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000255 NLSR_LOG_TRACE("Known name lsa");
256 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
257 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500258 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500259 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
260 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500261 chkNameLsa->writeLog();
262 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500263 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500264 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500265 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500266 // Obtain the set difference of the current and the incoming
267 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500268 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
269 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
270 std::list<ndn::Name> namesToAdd;
271 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
272 std::inserter(namesToAdd, namesToAdd.begin()));
273 for (std::list<ndn::Name>::iterator it = namesToAdd.begin();
274 it != namesToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500275 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500276 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
277 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500278 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500279 }
280 }
281 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500282
283 chkNameLsa->getNpl().sort();
284
Nick G97e34942016-07-11 14:46:27 -0500285 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500286 std::list<ndn::Name> namesToRemove;
287 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
288 std::inserter(namesToRemove, namesToRemove.begin()));
289 for (std::list<ndn::Name>::iterator it = namesToRemove.begin();
290 it != namesToRemove.end(); ++it) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500291 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << (*it).toUri());
akmhoqueb6450b12014-04-24 00:01:03 -0500292 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500293 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
294 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500295 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500296 }
297 }
298 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600299
akmhoque157b0a42014-05-13 00:26:37 -0500300 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500301 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
302 ndn::time::system_clock::now();
303 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500304 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500305 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
306 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500307 nlsa.getLsSeqNo(),
308 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500309 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500310 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500311 }
312 }
313 return true;
314}
315
316bool
317Lsdb::addNameLsa(NameLsa& nlsa)
318{
319 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
320 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600321 std::bind(nameLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500322 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500323 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500324 m_nameLsdb.push_back(nlsa);
325 return true;
326 }
327 return false;
328}
329
330bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500331Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500332{
333 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
334 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600335 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500336 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500337 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500338 (*it).writeLog();
Nick G97e34942016-07-11 14:46:27 -0500339 // If the requested name LSA is not ours, we also need to remove
340 // its entries from the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500341 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500342 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500343 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
344 (*it).getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500345 for (const auto& name : it->getNpl().getNames()) {
346 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
347 m_nlsr.getNamePrefixTable().removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500348 }
349 }
350 }
351 m_nameLsdb.erase(it);
352 return true;
353 }
354 return false;
355}
356
357bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500358Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500359{
360 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
361 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600362 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500363 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500364 return false;
365 }
366 return true;
367}
368
369void
akmhoque2f423352014-06-03 11:49:35 -0500370Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500371{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500372 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500373 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500374 it != m_nameLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500375 (*it).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-------------------");
akmhoqueb6450b12014-04-24 00:01:03 -0500574 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500575 it != m_corLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500576 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500577 }
578}
579
Jiewen Tana0497d82015-02-02 21:59:18 -0800580const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500581Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800582{
583 return m_corLsdb;
584}
585
akmhoque53353462014-04-22 08:43:45 -0500586// Adj LSA and LSDB related function starts here
587
Nick G97e34942016-07-11 14:46:27 -0500588 /*! \brief Returns whether an adj. LSA object is from some router.
589 \param alsa The adj. LSA object.
590 \param key The router name that you want to compare the LSA with.
591 */
akmhoque53353462014-04-22 08:43:45 -0500592static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500593adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500594{
595 return alsa.getKey() == key;
596}
597
akmhoque53353462014-04-22 08:43:45 -0500598void
Vince Lehman50df6b72015-03-03 12:06:40 -0600599Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500600{
Vince Lehman50df6b72015-03-03 12:06:40 -0600601 m_nlsr.incrementAdjBuildCount();
602
Nick Gordon5c467f02016-07-13 13:40:10 -0500603 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
604 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500605 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500606 return;
607 }
608
Vince Lehman50df6b72015-03-03 12:06:40 -0600609 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500610 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600611
dmcoomes9f936662017-03-02 10:33:09 -0600612 m_scheduler.scheduleEvent(m_adjLsaBuildInterval, std::bind(&Lsdb::buildAdjLsa, this));
Vince Lehman50df6b72015-03-03 12:06:40 -0600613 m_nlsr.setIsBuildAdjLsaSheduled(true);
614 }
615}
616
617void
618Lsdb::buildAdjLsa()
619{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500620 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600621
akmhoque674b0b12014-05-20 14:33:28 -0500622 m_nlsr.setIsBuildAdjLsaSheduled(false);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500623
624 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
625
akmhoque31d1d4b2014-05-05 22:08:14 -0500626 int adjBuildCount = m_nlsr.getAdjBuildCount();
Nick G97e34942016-07-11 14:46:27 -0500627 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500628 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500629 // It only makes sense to do the adjLsa build if we have neighbors
akmhoque157b0a42014-05-13 00:26:37 -0500630 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500631 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500632 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500633 }
Nick G97e34942016-07-11 14:46:27 -0500634 // We have no active neighbors, meaning no one can route through
635 // us. So delete our entry in the LSDB. This prevents this
636 // router from refreshing the LSA, eventually causing other
637 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500638 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500639 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500640 // Get this router's key
akmhoque31d1d4b2014-05-05 22:08:14 -0500641 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500642 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500643
akmhoque31d1d4b2014-05-05 22:08:14 -0500644 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500645 // Recompute routing table after removal
akmhoque31d1d4b2014-05-05 22:08:14 -0500646 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500647 }
Nick G97e34942016-07-11 14:46:27 -0500648 // In the case that during building the adj LSA, the FIB has to
649 // wait on an Interest response, the number of scheduled adj LSA
650 // builds could change, so we shouldn't just set it to 0.
akmhoque31d1d4b2014-05-05 22:08:14 -0500651 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500652 }
653 }
Nick G97e34942016-07-11 14:46:27 -0500654 // We are still waiting to know the adjacency status of some
655 // neighbor, so schedule a build for later (when all that has
656 // hopefully finished)
657 else {
658 m_nlsr.setIsBuildAdjLsaSheduled(true);
659 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
660 m_nlsr.getConfParameter().getInterestResendTime();
661 m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
dmcoomes9f936662017-03-02 10:33:09 -0600662 std::bind(&Lsdb::buildAdjLsa, this));
Nick G97e34942016-07-11 14:46:27 -0500663 }
akmhoque53353462014-04-22 08:43:45 -0500664}
665
akmhoque53353462014-04-22 08:43:45 -0500666bool
667Lsdb::addAdjLsa(AdjLsa& alsa)
668{
669 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
670 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600671 std::bind(adjLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500672 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500673 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500674 m_adjLsdb.push_back(alsa);
675 return true;
676 }
677 return false;
678}
679
akmhoqueb6450b12014-04-24 00:01:03 -0500680AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500681Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500682{
683 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
684 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600685 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500686 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500687 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500688 }
akmhoqueb6450b12014-04-24 00:01:03 -0500689 return 0;
akmhoque53353462014-04-22 08:43:45 -0500690}
691
akmhoque53353462014-04-22 08:43:45 -0500692bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500693Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500694{
akmhoqueb6450b12014-04-24 00:01:03 -0500695 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500696 // If it is in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500697 if (adjLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500698 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500699 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500700 return true;
701 }
akmhoque157b0a42014-05-13 00:26:37 -0500702 else {
akmhoque53353462014-04-22 08:43:45 -0500703 return false;
704 }
705 }
706 return true;
707}
708
akmhoque53353462014-04-22 08:43:45 -0500709ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500710Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
711 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500712{
Vince Lehman7c603292014-09-11 17:48:16 -0500713 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500714 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500715}
716
717bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500718Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500719{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700720 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500721 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500722 // If this adj. LSA is not in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500723 if (chkAdjLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500724 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
725 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500726 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500727 addAdjLsa(alsa);
Nick G97e34942016-07-11 14:46:27 -0500728 // Add any new name prefixes to the NPT
akmhoque31d1d4b2014-05-05 22:08:14 -0500729 alsa.addNptEntries(m_nlsr);
730 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500731 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500732 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
733 ndn::time::system_clock::now();
734 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500735 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500736 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500737 alsa.getLsSeqNo(), timeToExpire);
738 }
akmhoque157b0a42014-05-13 00:26:37 -0500739 else {
740 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500741 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
742 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500743 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500744 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500745 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500746 // If the new adj LSA has new content, update the contents of
747 // the LSDB entry. Additionally, since we've changed the
748 // contents of the LSDB, we have to schedule a routing
749 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500750 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500751 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500752 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500753 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500754 }
akmhoque157b0a42014-05-13 00:26:37 -0500755 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500756 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
757 ndn::time::system_clock::now();
758 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500759 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500760 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
761 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500762 alsa.getLsSeqNo(),
763 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500764 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500765 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500766 }
767 }
768 return true;
769}
770
771bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500772Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500773{
akmhoque31d1d4b2014-05-05 22:08:14 -0500774 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500775 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500776 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500777 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
778 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500779
Nick Gordon5c467f02016-07-13 13:40:10 -0500780 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
781 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500782 m_sequencingManager.increaseAdjLsaSeq();
783 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500784 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500785 }
Vince Lehman904c2412014-09-23 19:36:11 -0500786
Vince Lehman9d097802015-03-16 17:55:59 -0500787 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500788}
789
790bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500791Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500792{
793 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
794 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600795 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500796 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500797 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500798 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500799 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500800 m_adjLsdb.erase(it);
801 return true;
802 }
803 return false;
804}
805
806bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500807Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500808{
809 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
810 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600811 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500812 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500813 return false;
814 }
815 return true;
816}
817
Jiewen Tana0497d82015-02-02 21:59:18 -0800818const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500819Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500820{
821 return m_adjLsdb;
822}
823
824void
Nick Gordone98480b2017-05-24 11:23:03 -0500825Lsdb::setLsaRefreshTime(const ndn::time::seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500826{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700827 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500828}
829
830void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500831Lsdb::setThisRouterPrefix(std::string trp)
akmhoque53353462014-04-22 08:43:45 -0500832{
833 m_thisRouterPrefix = trp;
834}
835
Nick G97e34942016-07-11 14:46:27 -0500836 // This function determines whether a name LSA should be refreshed
837 // or expired. The conditions for getting refreshed are: it is still
838 // in the LSDB, it hasn't been updated by something else already (as
839 // evidenced by its seq. no.), and this is the originating router for
840 // the LSA. Is it let expire in all other cases.
841 // lsaKey is the key of the LSA's publishing router.
842 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500843void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500844Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500845{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500846 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
847 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500848 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500849 // If this name LSA exists in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500850 if (chkNameLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500851 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500852 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500853 if (chkNameLsa->getLsSeqNo() == seqNo) {
854 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500855 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
856 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500857 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500858 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500859 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500860 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500861 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500862 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500863 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500864 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500865 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700866 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500867 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500868 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500869 }
Nick G97e34942016-07-11 14:46:27 -0500870 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500871 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500872 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500873 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500874 }
875 }
876 }
877}
878
Nick G97e34942016-07-11 14:46:27 -0500879 // This function determines whether an adj. LSA should be refreshed
880 // or expired. The conditions for getting refreshed are: it is still
881 // in the LSDB, it hasn't been updated by something else already (as
882 // evidenced by its seq. no.), and this is the originating router for
883 // the LSA. Is it let expire in all other cases.
884 // lsaKey is the key of the LSA's publishing router.
885 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500886void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500887Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500888{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500889 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
890 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500891 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500892 // If this is a valid LSA
akmhoque157b0a42014-05-13 00:26:37 -0500893 if (chkAdjLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500894 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500895 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500896 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500897 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500898 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500899 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
900 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500901 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500902 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500903 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500904 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500905 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500906 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500907 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500908 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500909 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700910 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500911 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500912 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500913 }
Nick G97e34942016-07-11 14:46:27 -0500914 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500915 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500916 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500917 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500918 }
Nick G97e34942016-07-11 14:46:27 -0500919 // We have changed the contents of the LSDB, so we have to
920 // schedule a routing calculation
akmhoque31d1d4b2014-05-05 22:08:14 -0500921 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500922 }
923 }
924}
925
Nick G97e34942016-07-11 14:46:27 -0500926 // This function determines whether an adj. LSA should be refreshed
927 // or expired. The conditions for getting refreshed are: it is still
928 // in the LSDB, it hasn't been updated by something else already (as
929 // evidenced by its seq. no.), and this is the originating router for
930 // the LSA. It is let expire in all other cases.
931 // lsaKey is the key of the LSA's publishing router.
932 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500933void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500934Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500935 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500936{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500937 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
938 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500939 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500940 // Whether the LSA is in the LSDB or not.
akmhoque157b0a42014-05-13 00:26:37 -0500941 if (chkCorLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500942 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500943 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500944 if (chkCorLsa->getLsSeqNo() == seqNo) {
945 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500946 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
947 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500948 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500949 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Nick Gordon5c467f02016-07-13 13:40:10 -0500950 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500951 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500952 }
953
akmhoquec7a79b22014-05-26 08:06:19 -0500954 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500955 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500956 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500957 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500958 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
959 chkCorLsa->getKey(),
960 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700961 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500962 // Only sync coordinate LSAs if link-state routing is disabled
963 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500964 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500965 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500966 }
akmhoque53353462014-04-22 08:43:45 -0500967 }
Nick G97e34942016-07-11 14:46:27 -0500968 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500969 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500970 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500971 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500972 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500973 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500974 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500975 }
976 }
977 }
978}
979
akmhoque53353462014-04-22 08:43:45 -0500980void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700981Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500982 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500983{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600984 // increment SENT_LSA_INTEREST
985 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
986
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500987 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500988 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700989 }
Nick G97e34942016-07-11 14:46:27 -0500990 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500991 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500992 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500993 uint64_t seqNo = interestName[-1].toNumber();
994
Nick G97e34942016-07-11 14:46:27 -0500995 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500996 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
997 m_highestSeqNo[lsaName] = seqNo;
998 }
Nick G97e34942016-07-11 14:46:27 -0500999 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001000 else if (seqNo > m_highestSeqNo[lsaName]) {
1001 m_highestSeqNo[lsaName] = seqNo;
1002 }
Nick G97e34942016-07-11 14:46:27 -05001003 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001004 else if (seqNo < m_highestSeqNo[lsaName]) {
1005 return;
1006 }
1007
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001008 ndn::Interest interest(interestName);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -07001009 interest.setInterestLifetime(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);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001012 shared_ptr<ndn::util::SegmentFetcher> fetcher =
1013 ndn::util::SegmentFetcher::fetch(m_nlsr.getNlsrFace(), interest,
1014 m_nlsr.getValidator(),
1015 std::bind(&Lsdb::afterFetchLsa, this, _1, interestName),
1016 std::bind(&Lsdb::onFetchLsaError, this, _1, _2, interestName,
1017 timeoutCount, deadline, lsaName, seqNo));
1018
1019 m_lsaStorage.connectToFetcher(*fetcher);
1020 m_nlsr.connectToFetcher(*fetcher);
1021
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001022 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001023 Lsa::Type lsaType;
1024 std::istringstream(interestName[-2].toUri()) >> lsaType;
1025 switch (lsaType) {
1026 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001027 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001028 break;
1029 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001030 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001031 break;
1032 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001033 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001034 break;
1035 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001036 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001037 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001038}
1039
1040void
1041Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1042{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001043 // increment RCV_LSA_INTEREST
1044 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1045
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001046 const ndn::Name& interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -05001047 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001048
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001049 std::string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001050 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
1051
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001052 // Forms the name of the router that the Interest packet came from.
1053 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
1054 originRouter.append(interestName.getSubName(lsaPosition + 1,
1055 interest.getName().size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001056
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001057 // if the interest is for this router's LSA
1058 if (originRouter == m_nlsr.getConfParameter().getRouterPrefix()) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001059
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001060 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001061
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001062 uint64_t seqNo = interestName[-1].toNumber();
1063 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001064
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001065 Lsa::Type interestedLsType;
1066 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
1067
1068 if (interestedLsType == Lsa::Type::NAME) {
1069 processInterestForNameLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1070 seqNo);
1071 }
1072 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1073 processInterestForAdjacencyLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1074 seqNo);
1075 }
1076 else if (interestedLsType == Lsa::Type::COORDINATE) {
1077 processInterestForCoordinateLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1078 seqNo);
1079 }
1080 else {
1081 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1082 }
1083 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001084 }
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001085 }
1086 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
1087 const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
1088 if (lsaSegment != nullptr) {
1089 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
1090 m_nlsr.getNlsrFace().put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001091 }
akmhoque157b0a42014-05-13 00:26:37 -05001092 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001093 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001094 }
1095 }
1096}
1097
Nick G97e34942016-07-11 14:46:27 -05001098 // \brief Sends LSA data.
1099 // \param interest The Interest that warranted the data.
1100 // \param content The data that the Interest was seeking.
akmhoque31d1d4b2014-05-05 22:08:14 -05001101void
akmhoque69c9aa92014-07-23 15:15:05 -05001102Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
1103{
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001104 LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
1105 m_nlsr.getKeyChain(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001106 m_nlsr.getSigningInfo(),
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001107 m_lsaRefreshTime,
1108 content);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001109 NLSR_LOG_DEBUG("Sending requested data ( " << content << ") for interest (" << interest
dmcoomes9eaf3f42017-02-21 11:39:01 -06001110 << ") to be published and added to face.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001111 publisher.publish(interest.getName());
akmhoque69c9aa92014-07-23 15:15:05 -05001112}
1113
Nick G97e34942016-07-11 14:46:27 -05001114 // \brief Finds and sends a requested name LSA.
1115 // \param interest The interest that seeks the name LSA.
1116 // \param lsaKey The LSA that the Interest is seeking.
1117 // \param seqNo A sequence number to ensure that we are sending the
1118 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001119void
akmhoque31d1d4b2014-05-05 22:08:14 -05001120Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1121 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001122 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001123{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001124 // increment RCV_NAME_LSA_INTEREST
1125 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001126 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001127 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001128 if (nameLsa != nullptr) {
1129 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001130 if (nameLsa->getLsSeqNo() == seqNo) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001131 // if requested lsa belongs to this router then sign it and serve it
Nick Gordonfaf49f42017-10-23 12:36:28 -05001132 std::string content = nameLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001133 putLsaData(interest,content);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001134 // else the requested belongs to neighboring routers, so serve the
1135 // original data packet corresponding to the lsa
1136
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001137 // increment SENT_NAME_LSA_DATA
1138 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001139 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001140 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001141 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001142 }
1143 }
1144 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001145 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001146 }
1147}
1148
Nick G97e34942016-07-11 14:46:27 -05001149 // \brief Finds and sends a requested adj. LSA.
1150 // \param interest The interest that seeks the adj. LSA.
1151 // \param lsaKey The LSA that the Interest is seeking.
1152 // \param seqNo A sequence number to ensure that we are sending the
1153 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001154void
1155Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1156 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001157 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001158{
Nick Gordon5c467f02016-07-13 13:40:10 -05001159 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001160 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001161 }
1162
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001163 // increment RCV_ADJ_LSA_INTEREST
1164 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001165 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001166 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001167 if (adjLsa != nullptr) {
1168 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001169 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001170 std::string content = adjLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001171 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001172 // increment SENT_ADJ_LSA_DATA
1173 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001174 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001175 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001176 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001177 }
1178 }
1179 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001180 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001181 }
1182}
1183
Nick G97e34942016-07-11 14:46:27 -05001184 // \brief Finds and sends a requested cor. LSA.
1185 // \param interest The interest that seeks the cor. LSA.
1186 // \param lsaKey The LSA that the Interest is seeking.
1187 // \param seqNo A sequence number to ensure that we are sending the
1188 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001189void
1190Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1191 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001192 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001193{
Nick Gordon5c467f02016-07-13 13:40:10 -05001194 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001195 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001196 }
1197
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001198 // increment RCV_COORD_LSA_INTEREST
1199 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001200 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001201 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001202 if (corLsa != nullptr) {
1203 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001204 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001205 std::string content = corLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001206 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001207 // increment SENT_COORD_LSA_DATA
1208 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001209 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001210 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001211 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001212 }
1213 }
1214 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001215 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001216 }
1217}
1218
1219void
dmcoomes9f936662017-03-02 10:33:09 -06001220Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001221{
1222 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001223 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001224
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001225 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001226 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001227
akmhoque157b0a42014-05-13 00:26:37 -05001228 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001229
Nick G97e34942016-07-11 14:46:27 -05001230 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001231 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001232 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001233
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001234 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001235 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1236 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001237
Nick Gordon727d4832017-10-13 18:04:25 -05001238 Lsa::Type interestedLsType;
1239 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001240
Nick Gordon727d4832017-10-13 18:04:25 -05001241 if (interestedLsType == Lsa::Type::NAME) {
1242 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1243 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001244 }
Nick Gordon727d4832017-10-13 18:04:25 -05001245 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1246 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1247 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001248 }
Nick Gordon727d4832017-10-13 18:04:25 -05001249 else if (interestedLsType == Lsa::Type::COORDINATE) {
1250 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1251 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001252 }
akmhoque157b0a42014-05-13 00:26:37 -05001253 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001254 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001255 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001256
1257 // increment RCV_LSA_DATA
1258 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001259 }
1260}
1261
1262void
1263Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001264 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001265{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001266 // increment RCV_NAME_LSA_DATA
1267 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001268 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001269 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001270 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001271 installNameLsa(nameLsa);
1272 }
akmhoque157b0a42014-05-13 00:26:37 -05001273 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001274 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001275 }
1276 }
1277}
1278
1279void
1280Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001281 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001282{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001283 // increment RCV_ADJ_LSA_DATA
1284 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001285 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001286 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001287 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001288 installAdjLsa(adjLsa);
1289 }
akmhoque157b0a42014-05-13 00:26:37 -05001290 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001291 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001292 }
1293 }
1294}
1295
1296void
1297Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001298 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001299{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001300 // increment RCV_COORD_LSA_DATA
1301 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001302 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001303 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001304 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001305 installCoordinateLsa(corLsa);
1306 }
akmhoque157b0a42014-05-13 00:26:37 -05001307 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001308 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001309 }
1310 }
1311}
1312
akmhoquec7a79b22014-05-26 08:06:19 -05001313ndn::time::system_clock::TimePoint
1314Lsdb::getLsaExpirationTimePoint()
1315{
1316 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1317 expirationTimePoint = expirationTimePoint +
1318 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1319 return expirationTimePoint;
1320}
akmhoque31d1d4b2014-05-05 22:08:14 -05001321
1322void
akmhoque2f423352014-06-03 11:49:35 -05001323Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001324{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001325 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -05001326 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -05001327 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -05001328 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -05001329 }
1330}
1331
1332//-----utility function -----
1333bool
Nick Gordon727d4832017-10-13 18:04:25 -05001334Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001335{
Nick Gordon727d4832017-10-13 18:04:25 -05001336 switch (lsType) {
1337 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001338 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001339 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001340 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001341 case Lsa::Type::NAME:
1342 return doesNameLsaExist(key);
1343 default:
1344 return false;
akmhoque53353462014-04-22 08:43:45 -05001345 }
akmhoque53353462014-04-22 08:43:45 -05001346}
1347
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001348bool
Nick Gordon727d4832017-10-13 18:04:25 -05001349Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001350 const uint64_t& sequenceNumber) {
1351 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001352 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001353
Nick Gordon727d4832017-10-13 18:04:25 -05001354 switch (lsaType) {
1355 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001356 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001357 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001358 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001359 case Lsa::Type::NAME:
1360 return isNameLsaNew(lsaKey, sequenceNumber);
1361 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001362 return false;
1363 }
1364}
1365
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001366} // namespace nlsr