blob: 536d154a7986db1321df857f999a6e55358b8132 [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 R Chowdhuryaa3b0852015-08-06 13:08:56 -050025#include "nlsr.hpp"
26#include "publisher/segment-publisher.hpp"
27#include "utility/name-helper.hpp"
28
29#include <ndn-cxx/security/signing-helpers.hpp>
30#include <ndn-cxx/util/segment-fetcher.hpp>
31
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())
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050074 , m_lsaRefreshTime(0)
75 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050076 , m_sequencingManager()
Nick Gordon9eac4d92017-08-29 17:31:29 -050077 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
78 [this] (const ndn::Name& updateName, const uint64_t& sequenceNumber) {
79 ndn::Name lsaInterest{updateName};
80 lsaInterest.appendNumber(sequenceNumber);
81 expressInterest(lsaInterest, 0);
82 }))
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050083{
84}
85
86void
87Lsdb::onFetchLsaError(uint32_t errorCode,
88 const std::string& msg,
89 ndn::Name& interestName,
90 uint32_t retransmitNo,
91 const ndn::time::steady_clock::TimePoint& deadline,
92 ndn::Name lsaName,
93 uint64_t seqNo)
94{
dmcoomes5bcb39e2017-10-31 15:07:55 -050095 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050096 << ", Message: " << msg);
97
98 if (ndn::time::steady_clock::now() < deadline) {
99 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
100
101 if (it != m_highestSeqNo.end() && it->second == seqNo) {
102 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
103 // immediately since at the least the LSA Interest lifetime has elapsed.
104 // Otherwise, it is necessary to delay the Interest re-expression to prevent
105 // the potential for constant Interest flooding.
106 ndn::time::seconds delay = m_nlsr.getConfParameter().getLsaInterestLifetime();
107
108 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
109 delay = ndn::time::seconds(0);
110 }
111
112 m_scheduler.scheduleEvent(delay, std::bind(&Lsdb::expressInterest, this,
113 interestName, retransmitNo + 1, deadline));
114 }
115 }
116}
117
118void
119Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, ndn::Name& interestName)
120{
dmcoomes9f936662017-03-02 10:33:09 -0600121 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500122 data->setContent(bufferPtr);
123
dmcoomes5bcb39e2017-10-31 15:07:55 -0500124 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500125
126 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
127 uint64_t seqNo = interestName[-1].toNumber();
128
129 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
130 m_highestSeqNo[lsaName] = seqNo;
131 }
132 else if (seqNo > m_highestSeqNo[lsaName]) {
133 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500134 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500135 }
136 else if (seqNo < m_highestSeqNo[lsaName]) {
137 return;
138 }
139
140 onContentValidated(data);
141}
akmhoque53353462014-04-22 08:43:45 -0500142
143void
akmhoque31d1d4b2014-05-05 22:08:14 -0500144Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -0500145{
Vince Lehman7c603292014-09-11 17:48:16 -0500146 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -0500147}
148
Nick G97e34942016-07-11 14:46:27 -0500149 /*! \brief Compares if a name LSA is the same as the one specified by key
150
151 \param nlsa1 A name LSA object
152 \param key A key of an originating router to compare to nlsa1
153 */
akmhoque53353462014-04-22 08:43:45 -0500154static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500155nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500156{
157 return nlsa1.getKey() == key;
158}
159
akmhoque53353462014-04-22 08:43:45 -0500160bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500161Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500162{
akmhoque31d1d4b2014-05-05 22:08:14 -0500163 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500164 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500165 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 m_nlsr.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500167 m_sequencingManager.increaseNameLsaSeq();
168
169 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500170 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500171
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500173}
174
akmhoqueb6450b12014-04-24 00:01:03 -0500175NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500176Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500177{
178 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
179 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600180 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500181 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500182 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500183 }
akmhoqueb6450b12014-04-24 00:01:03 -0500184 return 0;
akmhoque53353462014-04-22 08:43:45 -0500185}
186
187bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500188Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500189{
akmhoqueb6450b12014-04-24 00:01:03 -0500190 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500191 // Is the name in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500192 if (nameLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500193 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500194 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500195 return true;
196 }
akmhoque157b0a42014-05-13 00:26:37 -0500197 else {
akmhoque53353462014-04-22 08:43:45 -0500198 return false;
199 }
200 }
201 return true;
202}
203
204ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500205Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
206 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500207{
Vince Lehman7c603292014-09-11 17:48:16 -0500208 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500209 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500210}
211
212bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500213Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500214{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700215 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500216 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500217 // Determines if the name LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500218 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500219 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500220 NLSR_LOG_DEBUG("New Name LSA");
221 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500222 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500223
akmhoque157b0a42014-05-13 00:26:37 -0500224 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500225 // If this name LSA is from another router, add the advertised
226 // prefixes to the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500227 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
228 nlsa.getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500229 std::list<ndn::Name> nameList = nlsa.getNpl().getNames();
akmhoque31d1d4b2014-05-05 22:08:14 -0500230 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -0500231 it++) {
232 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500233 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500234 }
235 }
236 }
akmhoque157b0a42014-05-13 00:26:37 -0500237 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500238 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
239 ndn::time::system_clock::now();
240 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500241 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500242 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500243 nlsa.getLsSeqNo(),
244 timeToExpire));
245 }
Nick G97e34942016-07-11 14:46:27 -0500246 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500247 else {
248 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500249 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
250 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500251 chkNameLsa->writeLog();
252 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500253 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500254 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500255 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500256 // Obtain the set difference of the current and the incoming
257 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500258 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
259 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
260 std::list<ndn::Name> namesToAdd;
261 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
262 std::inserter(namesToAdd, namesToAdd.begin()));
263 for (std::list<ndn::Name>::iterator it = namesToAdd.begin();
264 it != namesToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500265 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500266 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
267 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500268 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500269 }
270 }
271 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500272
273 chkNameLsa->getNpl().sort();
274
Nick G97e34942016-07-11 14:46:27 -0500275 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500276 std::list<ndn::Name> namesToRemove;
277 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
278 std::inserter(namesToRemove, namesToRemove.begin()));
279 for (std::list<ndn::Name>::iterator it = namesToRemove.begin();
280 it != namesToRemove.end(); ++it) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500281 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << (*it).toUri());
akmhoqueb6450b12014-04-24 00:01:03 -0500282 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500283 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
284 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500285 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500286 }
287 }
288 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600289
akmhoque157b0a42014-05-13 00:26:37 -0500290 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500291 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
292 ndn::time::system_clock::now();
293 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500294 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500295 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
296 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500297 nlsa.getLsSeqNo(),
298 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500299 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500300 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500301 }
302 }
303 return true;
304}
305
306bool
307Lsdb::addNameLsa(NameLsa& nlsa)
308{
309 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
310 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600311 std::bind(nameLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500312 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500313 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500314 m_nameLsdb.push_back(nlsa);
315 return true;
316 }
317 return false;
318}
319
320bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500321Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500322{
323 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
324 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600325 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500326 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500327 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500328 (*it).writeLog();
Nick G97e34942016-07-11 14:46:27 -0500329 // If the requested name LSA is not ours, we also need to remove
330 // its entries from the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500331 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500332 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500333 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
334 (*it).getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500335 for (const auto& name : it->getNpl().getNames()) {
336 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
337 m_nlsr.getNamePrefixTable().removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500338 }
339 }
340 }
341 m_nameLsdb.erase(it);
342 return true;
343 }
344 return false;
345}
346
347bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500348Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500349{
350 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
351 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600352 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500353 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500354 return false;
355 }
356 return true;
357}
358
359void
akmhoque2f423352014-06-03 11:49:35 -0500360Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500361{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500362 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500363 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500364 it != m_nameLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500365 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500366 }
367}
368
Jiewen Tana0497d82015-02-02 21:59:18 -0800369const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500370Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800371{
372 return m_nameLsdb;
373}
374
akmhoque53353462014-04-22 08:43:45 -0500375// Cor LSA and LSDB related Functions start here
376
Nick G97e34942016-07-11 14:46:27 -0500377/*! \brief Compares whether an LSA object is the same as a key.
378 \param clsa The cor. LSA to check the identity of.
379 \param key The key of the publishing router to check against.
380*/
akmhoque53353462014-04-22 08:43:45 -0500381static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500382corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500383{
384 return clsa.getKey() == key;
385}
386
387bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500388Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500389{
akmhoque31d1d4b2014-05-05 22:08:14 -0500390 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500391 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500392 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500393 m_nlsr.getConfParameter().getCorR(),
394 m_nlsr.getConfParameter().getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500395
396 // Sync coordinate LSAs if using HR or HR dry run.
397 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500398 m_sequencingManager.increaseCorLsaSeq();
399 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500400 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500401 }
402
akmhoque31d1d4b2014-05-05 22:08:14 -0500403 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500404
akmhoque53353462014-04-22 08:43:45 -0500405 return true;
406}
407
akmhoqueb6450b12014-04-24 00:01:03 -0500408CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500409Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500410{
akmhoqueb6450b12014-04-24 00:01:03 -0500411 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
412 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600413 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500414 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500415 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500416 }
akmhoqueb6450b12014-04-24 00:01:03 -0500417 return 0;
akmhoque53353462014-04-22 08:43:45 -0500418}
419
420bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500421Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500422{
akmhoqueb6450b12014-04-24 00:01:03 -0500423 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500424 // Is the coordinate LSA in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500425 if (clsa != 0) {
Nick G97e34942016-07-11 14:46:27 -0500426 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500427 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500428 return true;
429 }
akmhoque157b0a42014-05-13 00:26:37 -0500430 else {
akmhoque53353462014-04-22 08:43:45 -0500431 return false;
432 }
433 }
434 return true;
435}
436
Nick G97e34942016-07-11 14:46:27 -0500437 // Schedules a refresh/expire event in the scheduler.
438 // \param key The name of the router that published the LSA.
439 // \param seqNo the seq. no. associated with the LSA to check.
440 // \param expTime How long to wait before triggering the event.
akmhoque53353462014-04-22 08:43:45 -0500441ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500442Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500443 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500444{
Vince Lehman7c603292014-09-11 17:48:16 -0500445 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500446 std::bind(&Lsdb::expireOrRefreshCoordinateLsa,
Vince Lehman7c603292014-09-11 17:48:16 -0500447 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500448}
449
450bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500451Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500452{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700453 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500454 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500455 // Checking whether the LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500456 if (chkCorLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500457 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
458 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500459 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500460 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500461
Nick Gordon5c467f02016-07-13 13:40:10 -0500462 // Register the LSA's origin router prefix
akmhoque157b0a42014-05-13 00:26:37 -0500463 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500464 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
465 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500466 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500467 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500468 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500469 }
Nick G97e34942016-07-11 14:46:27 -0500470 // Set the expiration time for the new LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500471 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500472 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
473 ndn::time::system_clock::now();
474 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500475 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500476 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500477 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500478 }
Nick G97e34942016-07-11 14:46:27 -0500479 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500480 else {
481 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500482 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
483 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500484 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500485 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500486 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500487 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500488 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500489 chkCorLsa->setCorRadius(clsa.getCorRadius());
490 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500491 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500492 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500493 }
494 }
Nick G97e34942016-07-11 14:46:27 -0500495 // If this is an LSA from another router, refresh its expiration time.
akmhoque157b0a42014-05-13 00:26:37 -0500496 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500497 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
498 ndn::time::system_clock::now();
499 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500500 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500501 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
502 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500503 clsa.getLsSeqNo(),
504 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500505 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500506 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500507 }
508 }
509 return true;
510}
511
512bool
akmhoqueb6450b12014-04-24 00:01:03 -0500513Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500514{
akmhoqueb6450b12014-04-24 00:01:03 -0500515 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
516 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600517 std::bind(corLsaCompareByKey, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500518 clsa.getKey()));
519 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500520 m_corLsdb.push_back(clsa);
521 return true;
522 }
523 return false;
524}
525
526bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500527Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500528{
akmhoqueb6450b12014-04-24 00:01:03 -0500529 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
530 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600531 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500532 _1, key));
533 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500534 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500535 it->writeLog();
536
537 if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
538 m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500539 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500540
akmhoque53353462014-04-22 08:43:45 -0500541 m_corLsdb.erase(it);
542 return true;
543 }
544 return false;
545}
546
547bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500548Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500549{
akmhoqueb6450b12014-04-24 00:01:03 -0500550 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
551 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600552 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500553 _1, key));
554 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500555 return false;
556 }
557 return true;
558}
559
560void
akmhoque2f423352014-06-03 11:49:35 -0500561Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500562{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500563 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
akmhoqueb6450b12014-04-24 00:01:03 -0500564 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500565 it != m_corLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500566 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500567 }
568}
569
Jiewen Tana0497d82015-02-02 21:59:18 -0800570const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500571Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800572{
573 return m_corLsdb;
574}
575
akmhoque53353462014-04-22 08:43:45 -0500576// Adj LSA and LSDB related function starts here
577
Nick G97e34942016-07-11 14:46:27 -0500578 /*! \brief Returns whether an adj. LSA object is from some router.
579 \param alsa The adj. LSA object.
580 \param key The router name that you want to compare the LSA with.
581 */
akmhoque53353462014-04-22 08:43:45 -0500582static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500583adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500584{
585 return alsa.getKey() == key;
586}
587
akmhoque53353462014-04-22 08:43:45 -0500588void
Vince Lehman50df6b72015-03-03 12:06:40 -0600589Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500590{
Vince Lehman50df6b72015-03-03 12:06:40 -0600591 m_nlsr.incrementAdjBuildCount();
592
Nick Gordon5c467f02016-07-13 13:40:10 -0500593 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
594 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500595 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500596 return;
597 }
598
Vince Lehman50df6b72015-03-03 12:06:40 -0600599 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500600 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600601
dmcoomes9f936662017-03-02 10:33:09 -0600602 m_scheduler.scheduleEvent(m_adjLsaBuildInterval, std::bind(&Lsdb::buildAdjLsa, this));
Vince Lehman50df6b72015-03-03 12:06:40 -0600603 m_nlsr.setIsBuildAdjLsaSheduled(true);
604 }
605}
606
607void
608Lsdb::buildAdjLsa()
609{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500610 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600611
akmhoque674b0b12014-05-20 14:33:28 -0500612 m_nlsr.setIsBuildAdjLsaSheduled(false);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500613
614 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
615
akmhoque31d1d4b2014-05-05 22:08:14 -0500616 int adjBuildCount = m_nlsr.getAdjBuildCount();
Nick G97e34942016-07-11 14:46:27 -0500617 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500618 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500619 // It only makes sense to do the adjLsa build if we have neighbors
akmhoque157b0a42014-05-13 00:26:37 -0500620 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500621 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500622 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500623 }
Nick G97e34942016-07-11 14:46:27 -0500624 // We have no active neighbors, meaning no one can route through
625 // us. So delete our entry in the LSDB. This prevents this
626 // router from refreshing the LSA, eventually causing other
627 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500628 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500629 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500630 // Get this router's key
akmhoque31d1d4b2014-05-05 22:08:14 -0500631 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500632 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500633
akmhoque31d1d4b2014-05-05 22:08:14 -0500634 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500635 // Recompute routing table after removal
akmhoque31d1d4b2014-05-05 22:08:14 -0500636 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500637 }
Nick G97e34942016-07-11 14:46:27 -0500638 // In the case that during building the adj LSA, the FIB has to
639 // wait on an Interest response, the number of scheduled adj LSA
640 // builds could change, so we shouldn't just set it to 0.
akmhoque31d1d4b2014-05-05 22:08:14 -0500641 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500642 }
643 }
Nick G97e34942016-07-11 14:46:27 -0500644 // We are still waiting to know the adjacency status of some
645 // neighbor, so schedule a build for later (when all that has
646 // hopefully finished)
647 else {
648 m_nlsr.setIsBuildAdjLsaSheduled(true);
649 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
650 m_nlsr.getConfParameter().getInterestResendTime();
651 m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
dmcoomes9f936662017-03-02 10:33:09 -0600652 std::bind(&Lsdb::buildAdjLsa, this));
Nick G97e34942016-07-11 14:46:27 -0500653 }
akmhoque53353462014-04-22 08:43:45 -0500654}
655
akmhoque53353462014-04-22 08:43:45 -0500656bool
657Lsdb::addAdjLsa(AdjLsa& alsa)
658{
659 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
660 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600661 std::bind(adjLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500662 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500663 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500664 m_adjLsdb.push_back(alsa);
665 return true;
666 }
667 return false;
668}
669
akmhoqueb6450b12014-04-24 00:01:03 -0500670AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500671Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500672{
673 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
674 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600675 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500676 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500677 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500678 }
akmhoqueb6450b12014-04-24 00:01:03 -0500679 return 0;
akmhoque53353462014-04-22 08:43:45 -0500680}
681
akmhoque53353462014-04-22 08:43:45 -0500682bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500683Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500684{
akmhoqueb6450b12014-04-24 00:01:03 -0500685 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500686 // If it is in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500687 if (adjLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500688 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500689 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500690 return true;
691 }
akmhoque157b0a42014-05-13 00:26:37 -0500692 else {
akmhoque53353462014-04-22 08:43:45 -0500693 return false;
694 }
695 }
696 return true;
697}
698
akmhoque53353462014-04-22 08:43:45 -0500699ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500700Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
701 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500702{
Vince Lehman7c603292014-09-11 17:48:16 -0500703 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500704 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500705}
706
707bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500708Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500709{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700710 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500711 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500712 // If this adj. LSA is not in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500713 if (chkAdjLsa == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500714 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
715 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500716 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500717 addAdjLsa(alsa);
Nick G97e34942016-07-11 14:46:27 -0500718 // Add any new name prefixes to the NPT
akmhoque31d1d4b2014-05-05 22:08:14 -0500719 alsa.addNptEntries(m_nlsr);
720 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500721 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500722 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
723 ndn::time::system_clock::now();
724 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500725 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500726 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500727 alsa.getLsSeqNo(), timeToExpire);
728 }
akmhoque157b0a42014-05-13 00:26:37 -0500729 else {
730 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500731 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
732 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500733 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500734 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500735 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500736 // If the new adj LSA has new content, update the contents of
737 // the LSDB entry. Additionally, since we've changed the
738 // contents of the LSDB, we have to schedule a routing
739 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500740 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500741 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500742 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500743 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500744 }
akmhoque157b0a42014-05-13 00:26:37 -0500745 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500746 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
747 ndn::time::system_clock::now();
748 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500749 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500750 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
751 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500752 alsa.getLsSeqNo(),
753 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500754 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500755 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500756 }
757 }
758 return true;
759}
760
761bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500762Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500763{
akmhoque31d1d4b2014-05-05 22:08:14 -0500764 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500765 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500766 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500767 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
768 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500769
Nick Gordon5c467f02016-07-13 13:40:10 -0500770 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
771 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500772 m_sequencingManager.increaseAdjLsaSeq();
773 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500774 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500775 }
Vince Lehman904c2412014-09-23 19:36:11 -0500776
Vince Lehman9d097802015-03-16 17:55:59 -0500777 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500778}
779
780bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500781Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500782{
783 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
784 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600785 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500786 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500787 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500788 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500789 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500790 m_adjLsdb.erase(it);
791 return true;
792 }
793 return false;
794}
795
796bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500797Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500798{
799 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
800 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600801 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500802 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500803 return false;
804 }
805 return true;
806}
807
Jiewen Tana0497d82015-02-02 21:59:18 -0800808const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500809Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500810{
811 return m_adjLsdb;
812}
813
814void
Nick Gordone98480b2017-05-24 11:23:03 -0500815Lsdb::setLsaRefreshTime(const ndn::time::seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500816{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700817 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500818}
819
820void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500821Lsdb::setThisRouterPrefix(std::string trp)
akmhoque53353462014-04-22 08:43:45 -0500822{
823 m_thisRouterPrefix = trp;
824}
825
Nick G97e34942016-07-11 14:46:27 -0500826 // This function determines whether a name LSA should be refreshed
827 // or expired. The conditions for getting refreshed are: it is still
828 // in the LSDB, it hasn't been updated by something else already (as
829 // evidenced by its seq. no.), and this is the originating router for
830 // the LSA. Is it let expire in all other cases.
831 // lsaKey is the key of the LSA's publishing router.
832 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500833void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500834Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500835{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500836 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
837 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500838 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500839 // If this name LSA exists in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500840 if (chkNameLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500841 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500842 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500843 if (chkNameLsa->getLsSeqNo() == seqNo) {
844 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500845 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
846 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500847 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500848 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500849 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500850 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500851 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500852 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500853 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500854 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500855 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700856 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500857 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500858 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500859 }
Nick G97e34942016-07-11 14:46:27 -0500860 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500861 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500862 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500863 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500864 }
865 }
866 }
867}
868
Nick G97e34942016-07-11 14:46:27 -0500869 // This function determines whether an adj. LSA should be refreshed
870 // or expired. The conditions for getting refreshed are: it is still
871 // in the LSDB, it hasn't been updated by something else already (as
872 // evidenced by its seq. no.), and this is the originating router for
873 // the LSA. Is it let expire in all other cases.
874 // lsaKey is the key of the LSA's publishing router.
875 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500876void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500877Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500878{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500879 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
880 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500881 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500882 // If this is a valid LSA
akmhoque157b0a42014-05-13 00:26:37 -0500883 if (chkAdjLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500884 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500885 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500886 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500887 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500888 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500889 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
890 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500891 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500892 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500893 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500894 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500895 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500896 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500897 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500898 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500899 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700900 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500901 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500902 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500903 }
Nick G97e34942016-07-11 14:46:27 -0500904 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500905 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500906 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500907 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500908 }
Nick G97e34942016-07-11 14:46:27 -0500909 // We have changed the contents of the LSDB, so we have to
910 // schedule a routing calculation
akmhoque31d1d4b2014-05-05 22:08:14 -0500911 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500912 }
913 }
914}
915
Nick G97e34942016-07-11 14:46:27 -0500916 // This function determines whether an adj. LSA should be refreshed
917 // or expired. The conditions for getting refreshed are: it is still
918 // in the LSDB, it hasn't been updated by something else already (as
919 // evidenced by its seq. no.), and this is the originating router for
920 // the LSA. It is let expire in all other cases.
921 // lsaKey is the key of the LSA's publishing router.
922 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500923void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500924Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500925 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500926{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500927 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
928 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500929 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500930 // Whether the LSA is in the LSDB or not.
akmhoque157b0a42014-05-13 00:26:37 -0500931 if (chkCorLsa != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500932 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500933 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500934 if (chkCorLsa->getLsSeqNo() == seqNo) {
935 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500936 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
937 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500938 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500939 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Nick Gordon5c467f02016-07-13 13:40:10 -0500940 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500941 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500942 }
943
akmhoquec7a79b22014-05-26 08:06:19 -0500944 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500945 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500946 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500947 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500948 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
949 chkCorLsa->getKey(),
950 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700951 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500952 // Only sync coordinate LSAs if link-state routing is disabled
953 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500954 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500955 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500956 }
akmhoque53353462014-04-22 08:43:45 -0500957 }
Nick G97e34942016-07-11 14:46:27 -0500958 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500959 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500960 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500961 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500962 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500963 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500964 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500965 }
966 }
967 }
968}
969
akmhoque53353462014-04-22 08:43:45 -0500970void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700971Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500972 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500973{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600974 // increment SENT_LSA_INTEREST
975 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
976
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500977 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500978 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700979 }
Nick G97e34942016-07-11 14:46:27 -0500980 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500981 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500982 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500983 uint64_t seqNo = interestName[-1].toNumber();
984
Nick G97e34942016-07-11 14:46:27 -0500985 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500986 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
987 m_highestSeqNo[lsaName] = seqNo;
988 }
Nick G97e34942016-07-11 14:46:27 -0500989 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500990 else if (seqNo > m_highestSeqNo[lsaName]) {
991 m_highestSeqNo[lsaName] = seqNo;
992 }
Nick G97e34942016-07-11 14:46:27 -0500993 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500994 else if (seqNo < m_highestSeqNo[lsaName]) {
995 return;
996 }
997
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500998 ndn::Interest interest(interestName);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700999 interest.setInterestLifetime(m_nlsr.getConfParameter().getLsaInterestLifetime());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001000
dmcoomes5bcb39e2017-10-31 15:07:55 -05001001 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001002 ndn::util::SegmentFetcher::fetch(m_nlsr.getNlsrFace(), interest,
1003 m_nlsr.getValidator(),
dmcoomes9f936662017-03-02 10:33:09 -06001004 std::bind(&Lsdb::afterFetchLsa, this, _1, interestName),
1005 std::bind(&Lsdb::onFetchLsaError, this, _1, _2, interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001006 timeoutCount, deadline, lsaName, seqNo));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001007 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001008 Lsa::Type lsaType;
1009 std::istringstream(interestName[-2].toUri()) >> lsaType;
1010 switch (lsaType) {
1011 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001012 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001013 break;
1014 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001015 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001016 break;
1017 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001018 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001019 break;
1020 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -05001021 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001022 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001023}
1024
1025void
1026Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1027{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001028 // increment RCV_LSA_INTEREST
1029 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1030
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001031 const ndn::Name& interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -05001032 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001033
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001034 std::string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001035 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
1036
akmhoque157b0a42014-05-13 00:26:37 -05001037 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001038
Nick G97e34942016-07-11 14:46:27 -05001039 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001040 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
1041 originRouter.append(interestName.getSubName(lsaPosition + 1,
1042 interest.getName().size() - lsaPosition - 3));
1043
1044 uint64_t seqNo = interestName[-1].toNumber();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001045 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001046
Nick Gordon727d4832017-10-13 18:04:25 -05001047 Lsa::Type interestedLsType;
1048 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001049
Nick Gordon727d4832017-10-13 18:04:25 -05001050 if (interestedLsType == Lsa::Type::NAME) {
1051 processInterestForNameLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1052 seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001053 }
Nick Gordon727d4832017-10-13 18:04:25 -05001054 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1055 processInterestForAdjacencyLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1056 seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001057 }
Nick Gordon727d4832017-10-13 18:04:25 -05001058 else if (interestedLsType == Lsa::Type::COORDINATE) {
1059 processInterestForCoordinateLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1060 seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001061 }
akmhoque157b0a42014-05-13 00:26:37 -05001062 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001063 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001064 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001065 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001066 }
1067}
1068
Nick G97e34942016-07-11 14:46:27 -05001069 // \brief Sends LSA data.
1070 // \param interest The Interest that warranted the data.
1071 // \param content The data that the Interest was seeking.
akmhoque31d1d4b2014-05-05 22:08:14 -05001072void
akmhoque69c9aa92014-07-23 15:15:05 -05001073Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
1074{
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001075 LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
1076 m_nlsr.getKeyChain(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001077 m_nlsr.getSigningInfo(),
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001078 m_lsaRefreshTime,
1079 content);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001080 NLSR_LOG_DEBUG("Sending requested data ( " << content << ") for interest (" << interest
dmcoomes9eaf3f42017-02-21 11:39:01 -06001081 << ") to be published and added to face.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001082 publisher.publish(interest.getName());
akmhoque69c9aa92014-07-23 15:15:05 -05001083}
1084
Nick G97e34942016-07-11 14:46:27 -05001085 // \brief Finds and sends a requested name LSA.
1086 // \param interest The interest that seeks the name LSA.
1087 // \param lsaKey The LSA that the Interest is seeking.
1088 // \param seqNo A sequence number to ensure that we are sending the
1089 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001090void
akmhoque31d1d4b2014-05-05 22:08:14 -05001091Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1092 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001093 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001094{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001095 // increment RCV_NAME_LSA_INTEREST
1096 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001097 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001098 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001099 if (nameLsa != nullptr) {
1100 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001101 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001102 std::string content = nameLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001103 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001104 // increment SENT_NAME_LSA_DATA
1105 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001106 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001107 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001108 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001109 }
1110 }
1111 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001112 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001113 }
1114}
1115
Nick G97e34942016-07-11 14:46:27 -05001116 // \brief Finds and sends a requested adj. LSA.
1117 // \param interest The interest that seeks the adj. LSA.
1118 // \param lsaKey The LSA that the Interest is seeking.
1119 // \param seqNo A sequence number to ensure that we are sending the
1120 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001121void
1122Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1123 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001124 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001125{
Nick Gordon5c467f02016-07-13 13:40:10 -05001126 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001127 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001128 }
1129
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001130 // increment RCV_ADJ_LSA_INTEREST
1131 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001132 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001133 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001134 if (adjLsa != nullptr) {
1135 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001136 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001137 std::string content = adjLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001138 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001139 // increment SENT_ADJ_LSA_DATA
1140 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001141 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001142 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001143 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001144 }
1145 }
1146 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001147 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001148 }
1149}
1150
Nick G97e34942016-07-11 14:46:27 -05001151 // \brief Finds and sends a requested cor. LSA.
1152 // \param interest The interest that seeks the cor. LSA.
1153 // \param lsaKey The LSA that the Interest is seeking.
1154 // \param seqNo A sequence number to ensure that we are sending the
1155 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001156void
1157Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1158 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001159 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001160{
Nick Gordon5c467f02016-07-13 13:40:10 -05001161 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001162 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001163 }
1164
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001165 // increment RCV_COORD_LSA_INTEREST
1166 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001167 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001168 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001169 if (corLsa != nullptr) {
1170 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001171 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001172 std::string content = corLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001173 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001174 // increment SENT_COORD_LSA_DATA
1175 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001176 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001177 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001178 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001179 }
1180 }
1181 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001182 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001183 }
1184}
1185
1186void
dmcoomes9f936662017-03-02 10:33:09 -06001187Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001188{
1189 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001190 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001191
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001192 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001193 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001194
akmhoque157b0a42014-05-13 00:26:37 -05001195 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001196
Nick G97e34942016-07-11 14:46:27 -05001197 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001198 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001199 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001200
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001201 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001202 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1203 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001204
Nick Gordon727d4832017-10-13 18:04:25 -05001205 Lsa::Type interestedLsType;
1206 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001207
Nick Gordon727d4832017-10-13 18:04:25 -05001208 if (interestedLsType == Lsa::Type::NAME) {
1209 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1210 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001211 }
Nick Gordon727d4832017-10-13 18:04:25 -05001212 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1213 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1214 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001215 }
Nick Gordon727d4832017-10-13 18:04:25 -05001216 else if (interestedLsType == Lsa::Type::COORDINATE) {
1217 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1218 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001219 }
akmhoque157b0a42014-05-13 00:26:37 -05001220 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001221 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001222 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001223
1224 // increment RCV_LSA_DATA
1225 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001226 }
1227}
1228
1229void
1230Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001231 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001232{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001233 // increment RCV_NAME_LSA_DATA
1234 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001235 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001236 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001237 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001238 installNameLsa(nameLsa);
1239 }
akmhoque157b0a42014-05-13 00:26:37 -05001240 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001241 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001242 }
1243 }
1244}
1245
1246void
1247Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001248 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001249{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001250 // increment RCV_ADJ_LSA_DATA
1251 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001252 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001253 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001254 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001255 installAdjLsa(adjLsa);
1256 }
akmhoque157b0a42014-05-13 00:26:37 -05001257 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001258 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001259 }
1260 }
1261}
1262
1263void
1264Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001265 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001266{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001267 // increment RCV_COORD_LSA_DATA
1268 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001269 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001270 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001271 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001272 installCoordinateLsa(corLsa);
1273 }
akmhoque157b0a42014-05-13 00:26:37 -05001274 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001275 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001276 }
1277 }
1278}
1279
akmhoquec7a79b22014-05-26 08:06:19 -05001280ndn::time::system_clock::TimePoint
1281Lsdb::getLsaExpirationTimePoint()
1282{
1283 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1284 expirationTimePoint = expirationTimePoint +
1285 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1286 return expirationTimePoint;
1287}
akmhoque31d1d4b2014-05-05 22:08:14 -05001288
1289void
akmhoque2f423352014-06-03 11:49:35 -05001290Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001291{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001292 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -05001293 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -05001294 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -05001295 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -05001296 }
1297}
1298
1299//-----utility function -----
1300bool
Nick Gordon727d4832017-10-13 18:04:25 -05001301Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001302{
Nick Gordon727d4832017-10-13 18:04:25 -05001303 switch (lsType) {
1304 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001305 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001306 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001307 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001308 case Lsa::Type::NAME:
1309 return doesNameLsaExist(key);
1310 default:
1311 return false;
akmhoque53353462014-04-22 08:43:45 -05001312 }
akmhoque53353462014-04-22 08:43:45 -05001313}
1314
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001315bool
Nick Gordon727d4832017-10-13 18:04:25 -05001316Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001317 const uint64_t& sequenceNumber) {
1318 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001319 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001320
Nick Gordon727d4832017-10-13 18:04:25 -05001321 switch (lsaType) {
1322 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001323 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001324 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001325 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001326 case Lsa::Type::NAME:
1327 return isNameLsaNew(lsaKey, sequenceNumber);
1328 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001329 return false;
1330 }
1331}
1332
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001333} // namespace nlsr