blob: 58963f4b64306edbad3a215f17b2c0e1af6060b2 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
laqinfan7c13ba52018-01-15 21:17:24 +00003 * Copyright (c) 2014-2019, 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"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050027#include "utility/name-helper.hpp"
28
29#include <ndn-cxx/security/signing-helpers.hpp>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050030
akmhoque53353462014-04-22 08:43:45 -050031namespace nlsr {
32
dmcoomescf8d0ed2017-02-21 11:39:01 -060033INIT_LOGGER(Lsdb);
akmhoque674b0b12014-05-20 14:33:28 -050034
Jiewen Tana0497d82015-02-02 21:59:18 -080035const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050036const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Nick Gordone98480b2017-05-24 11:23:03 -050037const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
38 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050039
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain,
41 ndn::security::SigningInfo& signingInfo, ConfParameter& confParam,
42 NamePrefixTable& namePrefixTable, RoutingTable& routingTable)
43 : m_face(face)
44 , m_scheduler(face.getIoService())
45 , m_signingInfo(signingInfo)
46 , m_confParam(confParam)
47 , m_namePrefixTable(namePrefixTable)
48 , m_routingTable(routingTable)
49 , m_sync(m_face,
Nick Gordon727d4832017-10-13 18:04:25 -050050 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050051 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050052 return isLsaNew(routerName, lsaType, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 }, m_confParam)
54 , m_lsaStorage(m_scheduler)
55 , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
56 , m_thisRouterPrefix(m_confParam.getRouterPrefix().toUri())
57 , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
dulalsaurab82a34c22019-02-04 17:31:21 +000058 , m_sequencingManager(m_confParam.getStateFileDir(), m_confParam.getHyperbolicState())
Nick Gordon9eac4d92017-08-29 17:31:29 -050059 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
Ashlesh Gawande939b6f82018-12-09 16:51:09 -060060 [this] (const ndn::Name& updateName, uint64_t sequenceNumber) {
Nick Gordon9eac4d92017-08-29 17:31:29 -050061 ndn::Name lsaInterest{updateName};
62 lsaInterest.appendNumber(sequenceNumber);
63 expressInterest(lsaInterest, 0);
64 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060065 , m_segmentPublisher(m_face, keyChain)
66 , m_isBuildAdjLsaSheduled(false)
67 , m_adjBuildCount(0)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050068{
69}
70
Ashlesh Gawande744e4812018-08-22 16:26:24 -050071Lsdb::~Lsdb()
72{
73 for (const auto& sp : m_fetchers) {
74 sp->stop();
75 }
76}
77
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050078void
79Lsdb::onFetchLsaError(uint32_t errorCode,
80 const std::string& msg,
Ashlesh Gawande744e4812018-08-22 16:26:24 -050081 const ndn::Name& interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050082 uint32_t retransmitNo,
83 const ndn::time::steady_clock::TimePoint& deadline,
84 ndn::Name lsaName,
85 uint64_t seqNo)
86{
dmcoomes5bcb39e2017-10-31 15:07:55 -050087 NLSR_LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040088 << ", Message: " << msg);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050089
90 if (ndn::time::steady_clock::now() < deadline) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040091 auto it = m_highestSeqNo.find(lsaName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050092 if (it != m_highestSeqNo.end() && it->second == seqNo) {
93 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
94 // immediately since at the least the LSA Interest lifetime has elapsed.
95 // Otherwise, it is necessary to delay the Interest re-expression to prevent
96 // the potential for constant Interest flooding.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060097 ndn::time::seconds delay = m_confParam.getLsaInterestLifetime();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050098
99 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
100 delay = ndn::time::seconds(0);
101 }
102
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400103 m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this,
104 interestName, retransmitNo + 1, deadline));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500105 }
106 }
107}
108
109void
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500110Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& interestName)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500111{
dmcoomes9f936662017-03-02 10:33:09 -0600112 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600113 data->setContent(ndn::Block(bufferPtr));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500114
dmcoomes5bcb39e2017-10-31 15:07:55 -0500115 NLSR_LOG_DEBUG("Received data for LSA(name): " << data->getName());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500116
117 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
118 uint64_t seqNo = interestName[-1].toNumber();
119
120 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
121 m_highestSeqNo[lsaName] = seqNo;
122 }
123 else if (seqNo > m_highestSeqNo[lsaName]) {
124 m_highestSeqNo[lsaName] = seqNo;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500125 NLSR_LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500126 }
127 else if (seqNo < m_highestSeqNo[lsaName]) {
128 return;
129 }
130
131 onContentValidated(data);
132}
akmhoque53353462014-04-22 08:43:45 -0500133
Nick G97e34942016-07-11 14:46:27 -0500134 /*! \brief Compares if a name LSA is the same as the one specified by key
135
136 \param nlsa1 A name LSA object
137 \param key A key of an originating router to compare to nlsa1
138 */
akmhoque53353462014-04-22 08:43:45 -0500139static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500140nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500141{
142 return nlsa1.getKey() == key;
143}
144
akmhoque53353462014-04-22 08:43:45 -0500145bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500146Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500147{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600148 NameLsa nameLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500149 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500150 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600151 m_confParam.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500152 m_sequencingManager.increaseNameLsaSeq();
153
154 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500155 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500156
akmhoque31d1d4b2014-05-05 22:08:14 -0500157 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500158}
159
akmhoqueb6450b12014-04-24 00:01:03 -0500160NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500161Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500162{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400163 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
164 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500165 if (it != m_nameLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400166 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500167 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400168 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500169}
170
171bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500172Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500173{
akmhoqueb6450b12014-04-24 00:01:03 -0500174 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500175 // Is the name in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400176 if (nameLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500177 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500178 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500179 return true;
180 }
akmhoque157b0a42014-05-13 00:26:37 -0500181 else {
akmhoque53353462014-04-22 08:43:45 -0500182 return false;
183 }
184 }
185 return true;
186}
187
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400188ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500189Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
190 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500191{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400192 return m_scheduler.schedule(expTime + GRACE_PERIOD,
193 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500194}
195
196bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500197Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500198{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000199 NLSR_LOG_TRACE("installNameLsa");
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700200 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500201 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500202 // Determines if the name LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400203 if (chkNameLsa == nullptr) {
akmhoque53353462014-04-22 08:43:45 -0500204 addNameLsa(nlsa);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500205 NLSR_LOG_DEBUG("New Name LSA");
206 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500207 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500208
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000209 NLSR_LOG_TRACE("nlsa.getOrigRouter(): " << nlsa.getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600210 NLSR_LOG_TRACE("m_confParam.getRouterPrefix(): " << m_confParam.getRouterPrefix());
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000211
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600212 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500213 // If this name LSA is from another router, add the advertised
214 // prefixes to the NPT.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600215 m_namePrefixTable.addEntry(nlsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500216 nlsa.getOrigRouter());
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500217 for (const auto& name : nlsa.getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600218 if (name != m_confParam.getRouterPrefix()) {
219 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500220 }
221 }
222 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600223 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500224 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
225 ndn::time::system_clock::now();
226 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500227 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500228 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500229 nlsa.getLsSeqNo(),
230 timeToExpire));
231 }
Nick G97e34942016-07-11 14:46:27 -0500232 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500233 else {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000234 NLSR_LOG_TRACE("Known name lsa");
235 NLSR_LOG_TRACE("chkNameLsa->getLsSeqNo(): " << chkNameLsa->getLsSeqNo());
236 NLSR_LOG_TRACE("nlsa.getLsSeqNo(): " << nlsa.getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500237 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500238 NLSR_LOG_DEBUG("Updated Name LSA. Updating LSDB");
239 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500240 chkNameLsa->writeLog();
241 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500242 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500243 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500244 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500245 // Obtain the set difference of the current and the incoming
246 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500247 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
248 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
249 std::list<ndn::Name> namesToAdd;
250 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
251 std::inserter(namesToAdd, namesToAdd.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500252 for (const auto& name : namesToAdd) {
253 chkNameLsa->addName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600254 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
255 if (name != m_confParam.getRouterPrefix()) {
256 m_namePrefixTable.addEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500257 }
258 }
259 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500260
261 chkNameLsa->getNpl().sort();
262
Nick G97e34942016-07-11 14:46:27 -0500263 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500264 std::list<ndn::Name> namesToRemove;
265 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
266 std::inserter(namesToRemove, namesToRemove.begin()));
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500267 for (const auto& name : namesToRemove) {
268 NLSR_LOG_DEBUG("Removing name LSA no longer advertised: " << name);
269 chkNameLsa->removeName(name);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600270 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
271 if (name != m_confParam.getRouterPrefix()) {
272 m_namePrefixTable.removeEntry(name, nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500273 }
274 }
275 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600276
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600277 if (nlsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400278 auto duration = nlsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500279 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500280 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400281 chkNameLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500282 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500283 nlsa.getLsSeqNo(),
284 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500285 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500286 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500287 }
288 }
289 return true;
290}
291
292bool
293Lsdb::addNameLsa(NameLsa& nlsa)
294{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400295 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
296 std::bind(nameLsaCompareByKey, _1, nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500297 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500298 m_nameLsdb.push_back(nlsa);
299 return true;
300 }
301 return false;
302}
303
304bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500305Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500306{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400307 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
308 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500309 if (it != m_nameLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500310 NLSR_LOG_DEBUG("Deleting Name Lsa");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400311 it->writeLog();
Nick G97e34942016-07-11 14:46:27 -0500312 // If the requested name LSA is not ours, we also need to remove
313 // its entries from the NPT.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400314 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
315 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600316
Nick Gordonf14ec352017-07-24 16:09:58 -0500317 for (const auto& name : it->getNpl().getNames()) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600318 if (name != m_confParam.getRouterPrefix()) {
319 m_namePrefixTable.removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500320 }
321 }
322 }
323 m_nameLsdb.erase(it);
324 return true;
325 }
326 return false;
327}
328
329bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500330Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500331{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400332 auto it = std::find_if(m_nameLsdb.begin(), m_nameLsdb.end(),
333 std::bind(nameLsaCompareByKey, _1, key));
334 return it != m_nameLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500335}
336
337void
akmhoque2f423352014-06-03 11:49:35 -0500338Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500339{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500340 NLSR_LOG_DEBUG("---------------Name LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500341 for (const auto& nlsa : m_nameLsdb) {
342 nlsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500343 }
344}
345
Jiewen Tana0497d82015-02-02 21:59:18 -0800346const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500347Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800348{
349 return m_nameLsdb;
350}
351
akmhoque53353462014-04-22 08:43:45 -0500352// Cor LSA and LSDB related Functions start here
353
Nick G97e34942016-07-11 14:46:27 -0500354/*! \brief Compares whether an LSA object is the same as a key.
355 \param clsa The cor. LSA to check the identity of.
356 \param key The key of the publishing router to check against.
357*/
akmhoque53353462014-04-22 08:43:45 -0500358static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500359corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500360{
361 return clsa.getKey() == key;
362}
363
364bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500365Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500366{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600367 CoordinateLsa corLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500368 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500369 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600370 m_confParam.getCorR(),
371 m_confParam.getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500372
373 // Sync coordinate LSAs if using HR or HR dry run.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600374 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500375 m_sequencingManager.increaseCorLsaSeq();
376 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500377 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500378 }
379
akmhoque31d1d4b2014-05-05 22:08:14 -0500380 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500381
akmhoque53353462014-04-22 08:43:45 -0500382 return true;
383}
384
akmhoqueb6450b12014-04-24 00:01:03 -0500385CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500386Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500387{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400388 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
389 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500390 if (it != m_corLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400391 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500392 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400393 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500394}
395
396bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500397Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500398{
akmhoqueb6450b12014-04-24 00:01:03 -0500399 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500400 // Is the coordinate LSA in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400401 if (clsa != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500402 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500403 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500404 return true;
405 }
akmhoque157b0a42014-05-13 00:26:37 -0500406 else {
akmhoque53353462014-04-22 08:43:45 -0500407 return false;
408 }
409 }
410 return true;
411}
412
Nick G97e34942016-07-11 14:46:27 -0500413 // Schedules a refresh/expire event in the scheduler.
414 // \param key The name of the router that published the LSA.
415 // \param seqNo the seq. no. associated with the LSA to check.
416 // \param expTime How long to wait before triggering the event.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400417ndn::scheduler::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500418Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500419 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500420{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400421 return m_scheduler.schedule(expTime + GRACE_PERIOD,
422 std::bind(&Lsdb::expireOrRefreshCoordinateLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500423}
424
425bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500426Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500427{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700428 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500429 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500430 // Checking whether the LSA is new or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400431 if (chkCorLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500432 NLSR_LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
433 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500434 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500435 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500436
Nick Gordon5c467f02016-07-13 13:40:10 -0500437 // Register the LSA's origin router prefix
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600438 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
439 m_namePrefixTable.addEntry(clsa.getOrigRouter(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500440 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500441 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600442 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
443 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500444 }
Nick G97e34942016-07-11 14:46:27 -0500445 // Set the expiration time for the new LSA.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600446 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500447 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
448 ndn::time::system_clock::now();
449 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500450 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500451 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500452 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500453 }
Nick G97e34942016-07-11 14:46:27 -0500454 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500455 else {
456 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500457 NLSR_LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
458 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500459 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500460 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500461 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500462 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500463 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500464 chkCorLsa->setCorRadius(clsa.getCorRadius());
465 chkCorLsa->setCorTheta(clsa.getCorTheta());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600466 if (m_confParam.getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
467 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500468 }
469 }
Nick G97e34942016-07-11 14:46:27 -0500470 // If this is an LSA from another router, refresh its expiration time.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600471 if (clsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400472 auto duration = clsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500473 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500474 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400475 chkCorLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500476 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500477 clsa.getLsSeqNo(),
478 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500479 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500480 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500481 }
482 }
483 return true;
484}
485
486bool
akmhoqueb6450b12014-04-24 00:01:03 -0500487Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500488{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400489 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
490 std::bind(corLsaCompareByKey, _1, clsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500491 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500492 m_corLsdb.push_back(clsa);
493 return true;
494 }
495 return false;
496}
497
498bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500499Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500500{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400501 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
502 std::bind(corLsaCompareByKey, _1, key));
503
akmhoque157b0a42014-05-13 00:26:37 -0500504 if (it != m_corLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500505 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500506 it->writeLog();
507
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600508 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
509 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500510 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500511
akmhoque53353462014-04-22 08:43:45 -0500512 m_corLsdb.erase(it);
513 return true;
514 }
515 return false;
516}
517
518bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500519Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500520{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400521 auto it = std::find_if(m_corLsdb.begin(), m_corLsdb.end(),
522 std::bind(corLsaCompareByKey, _1, key));
523 return it != m_corLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500524}
525
526void
akmhoque2f423352014-06-03 11:49:35 -0500527Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500528{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500529 NLSR_LOG_DEBUG("---------------Cor LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500530 for (const auto& corLsa : m_corLsdb) {
531 corLsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500532 }
533}
534
Jiewen Tana0497d82015-02-02 21:59:18 -0800535const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500536Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800537{
538 return m_corLsdb;
539}
540
akmhoque53353462014-04-22 08:43:45 -0500541// Adj LSA and LSDB related function starts here
542
Nick G97e34942016-07-11 14:46:27 -0500543 /*! \brief Returns whether an adj. LSA object is from some router.
544 \param alsa The adj. LSA object.
545 \param key The router name that you want to compare the LSA with.
546 */
akmhoque53353462014-04-22 08:43:45 -0500547static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500548adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500549{
550 return alsa.getKey() == key;
551}
552
akmhoque53353462014-04-22 08:43:45 -0500553void
Vince Lehman50df6b72015-03-03 12:06:40 -0600554Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500555{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600556 m_adjBuildCount++;
Vince Lehman50df6b72015-03-03 12:06:40 -0600557
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600558 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Nick Gordon5c467f02016-07-13 13:40:10 -0500559 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes5bcb39e2017-10-31 15:07:55 -0500560 NLSR_LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500561 return;
562 }
563
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600564 if (m_isBuildAdjLsaSheduled == false) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500565 NLSR_LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
Vince Lehman50df6b72015-03-03 12:06:40 -0600566
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400567 m_scheduler.schedule(m_adjLsaBuildInterval, [this] { buildAdjLsa(); });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600568 m_isBuildAdjLsaSheduled = true;
Vince Lehman50df6b72015-03-03 12:06:40 -0600569 }
570}
571
572void
573Lsdb::buildAdjLsa()
574{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500575 NLSR_LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600576
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600577 m_isBuildAdjLsaSheduled = false;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500578
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600579 if (m_confParam.getAdjacencyList().isAdjLsaBuildable(m_confParam.getInterestRetryNumber())) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500580
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600581 int adjBuildCount = m_adjBuildCount;
Nick G97e34942016-07-11 14:46:27 -0500582 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500583 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500584 // It only makes sense to do the adjLsa build if we have neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600585 if (m_confParam.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500586 NLSR_LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500587 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500588 }
Nick G97e34942016-07-11 14:46:27 -0500589 // We have no active neighbors, meaning no one can route through
590 // us. So delete our entry in the LSDB. This prevents this
591 // router from refreshing the LSA, eventually causing other
592 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500593 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500594 NLSR_LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500595 // Get this router's key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600596 ndn::Name key = m_confParam.getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500597 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500598
akmhoque31d1d4b2014-05-05 22:08:14 -0500599 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500600 // Recompute routing table after removal
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600601 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500602 }
Nick G97e34942016-07-11 14:46:27 -0500603 // In the case that during building the adj LSA, the FIB has to
604 // wait on an Interest response, the number of scheduled adj LSA
605 // builds could change, so we shouldn't just set it to 0.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600606 m_adjBuildCount = m_adjBuildCount - adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500607 }
608 }
Nick G97e34942016-07-11 14:46:27 -0500609 // We are still waiting to know the adjacency status of some
610 // neighbor, so schedule a build for later (when all that has
611 // hopefully finished)
612 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600613 m_isBuildAdjLsaSheduled = true;
614 int schedulingTime = m_confParam.getInterestRetryNumber() *
615 m_confParam.getInterestResendTime();
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400616 m_scheduler.schedule(ndn::time::seconds(schedulingTime), [this] { buildAdjLsa(); });
Nick G97e34942016-07-11 14:46:27 -0500617 }
akmhoque53353462014-04-22 08:43:45 -0500618}
619
akmhoque53353462014-04-22 08:43:45 -0500620bool
621Lsdb::addAdjLsa(AdjLsa& alsa)
622{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400623 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
624 std::bind(adjLsaCompareByKey, _1, alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500625 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500626 m_adjLsdb.push_back(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600627 // Add any new name prefixes to the NPT
628 // Only add NPT entries if this is an adj LSA from another router.
629 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
630 // Pass the originating router as both the name to register and
631 // where it came from.
632 m_namePrefixTable.addEntry(alsa.getOrigRouter(), alsa.getOrigRouter());
633 }
akmhoque53353462014-04-22 08:43:45 -0500634 return true;
635 }
636 return false;
637}
638
akmhoqueb6450b12014-04-24 00:01:03 -0500639AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500640Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500641{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400642 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
643 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500644 if (it != m_adjLsdb.end()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400645 return &*it;
akmhoque53353462014-04-22 08:43:45 -0500646 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400647 return nullptr;
akmhoque53353462014-04-22 08:43:45 -0500648}
649
akmhoque53353462014-04-22 08:43:45 -0500650bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500651Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500652{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400653 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500654 // If it is in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400655 if (adjLsaCheck != nullptr) {
Nick G97e34942016-07-11 14:46:27 -0500656 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500657 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500658 return true;
659 }
akmhoque157b0a42014-05-13 00:26:37 -0500660 else {
akmhoque53353462014-04-22 08:43:45 -0500661 return false;
662 }
663 }
664 return true;
665}
666
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400667ndn::scheduler::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500668Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
669 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500670{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400671 return m_scheduler.schedule(expTime + GRACE_PERIOD,
672 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500673}
674
675bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500676Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500677{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700678 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500679 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500680 // If this adj. LSA is not in the LSDB already
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400681 if (chkAdjLsa == nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500682 NLSR_LOG_DEBUG("New Adj LSA. Adding to LSDB");
683 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500684 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500685 addAdjLsa(alsa);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600686
687 m_routingTable.scheduleRoutingTableCalculation();
688 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500689 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
690 ndn::time::system_clock::now();
691 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500692 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400693 scheduleAdjLsaExpiration(alsa.getKey(), alsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500694 }
akmhoque157b0a42014-05-13 00:26:37 -0500695 else {
696 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500697 NLSR_LOG_DEBUG("Updated Adj LSA. Updating LSDB");
698 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500699 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500700 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500701 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500702 // If the new adj LSA has new content, update the contents of
703 // the LSDB entry. Additionally, since we've changed the
704 // contents of the LSDB, we have to schedule a routing
705 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500706 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500707 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500708 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600709 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500710 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600711 if (alsa.getOrigRouter() != m_confParam.getRouterPrefix()) {
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400712 auto duration = alsa.getExpirationTimePoint() - ndn::time::system_clock::now();
akmhoquec7a79b22014-05-26 08:06:19 -0500713 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500714 }
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400715 chkAdjLsa->getExpiringEventId().cancel();
akmhoque31d1d4b2014-05-05 22:08:14 -0500716 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500717 alsa.getLsSeqNo(),
718 timeToExpire));
dmcoomes5bcb39e2017-10-31 15:07:55 -0500719 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500720 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500721 }
722 }
723 return true;
724}
725
726bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500727Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500728{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600729 AdjLsa adjLsa(m_confParam.getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500730 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500731 getLsaExpirationTimePoint(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600732 m_confParam.getAdjacencyList().getNumOfActiveNeighbor(),
733 m_confParam.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500734
Nick Gordon5c467f02016-07-13 13:40:10 -0500735 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600736 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500737 m_sequencingManager.increaseAdjLsaSeq();
738 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500739 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500740 }
Vince Lehman904c2412014-09-23 19:36:11 -0500741
Vince Lehman9d097802015-03-16 17:55:59 -0500742 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500743}
744
745bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500746Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500747{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400748 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
749 std::bind(adjLsaCompareByKey, _1, key));
750
akmhoque157b0a42014-05-13 00:26:37 -0500751 if (it != m_adjLsdb.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500752 NLSR_LOG_DEBUG("Deleting Adj Lsa");
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600753 it->writeLog();
754 if (it->getOrigRouter() != m_confParam.getRouterPrefix()) {
755 m_namePrefixTable.removeEntry(it->getOrigRouter(), it->getOrigRouter());
756 }
akmhoque53353462014-04-22 08:43:45 -0500757 m_adjLsdb.erase(it);
758 return true;
759 }
760 return false;
761}
762
763bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500764Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500765{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400766 auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
767 std::bind(adjLsaCompareByKey, _1, key));
768 return it != m_adjLsdb.end();
akmhoque53353462014-04-22 08:43:45 -0500769}
770
Jiewen Tana0497d82015-02-02 21:59:18 -0800771const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500772Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500773{
774 return m_adjLsdb;
775}
776
Nick G97e34942016-07-11 14:46:27 -0500777 // This function determines whether a name LSA should be refreshed
778 // or expired. The conditions for getting refreshed are: it is still
779 // in the LSDB, it hasn't been updated by something else already (as
780 // evidenced by its seq. no.), and this is the originating router for
781 // the LSA. Is it let expire in all other cases.
782 // lsaKey is the key of the LSA's publishing router.
783 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500784void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500785Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500786{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500787 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
788 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500789 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500790 // If this name LSA exists in the LSDB
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400791 if (chkNameLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500792 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500793 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500794 if (chkNameLsa->getLsSeqNo() == seqNo) {
795 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500796 NLSR_LOG_DEBUG("Own Name LSA, so refreshing it");
797 NLSR_LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500798 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500799 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500800 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500801 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500802 NLSR_LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500803 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500804 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500805 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500806 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700807 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500808 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500809 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500810 }
Nick G97e34942016-07-11 14:46:27 -0500811 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500812 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500813 NLSR_LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500814 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500815 }
816 }
817 }
818}
819
Nick G97e34942016-07-11 14:46:27 -0500820 // This function determines whether an adj. LSA should be refreshed
821 // or expired. The conditions for getting refreshed are: it is still
822 // in the LSDB, it hasn't been updated by something else already (as
823 // evidenced by its seq. no.), and this is the originating router for
824 // the LSA. Is it let expire in all other cases.
825 // lsaKey is the key of the LSA's publishing router.
826 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500827void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500828Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500829{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500830 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
831 NLSR_LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500832 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500833 // If this is a valid LSA
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400834 if (chkAdjLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500835 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500836 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500837 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500838 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500839 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500840 NLSR_LOG_DEBUG("Own Adj LSA, so refreshing it");
841 NLSR_LOG_DEBUG("Deleting Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500842 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500843 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500844 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500845 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500846 NLSR_LOG_DEBUG("Adding Adj Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500847 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500848 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500849 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500850 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700851 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500852 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500853 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500854 }
Nick G97e34942016-07-11 14:46:27 -0500855 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500856 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500857 NLSR_LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500858 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500859 }
Nick G97e34942016-07-11 14:46:27 -0500860 // We have changed the contents of the LSDB, so we have to
861 // schedule a routing calculation
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600862 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500863 }
864 }
865}
866
Nick G97e34942016-07-11 14:46:27 -0500867 // This function determines whether an adj. LSA should be refreshed
868 // or expired. The conditions for getting refreshed are: it is still
869 // in the LSDB, it hasn't been updated by something else already (as
870 // evidenced by its seq. no.), and this is the originating router for
871 // the LSA. It is let expire in all other cases.
872 // lsaKey is the key of the LSA's publishing router.
873 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500874void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500875Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500876 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500877{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500878 NLSR_LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
879 NLSR_LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500880 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500881 // Whether the LSA is in the LSDB or not.
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400882 if (chkCorLsa != nullptr) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500883 NLSR_LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500884 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500885 if (chkCorLsa->getLsSeqNo() == seqNo) {
886 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500887 NLSR_LOG_DEBUG("Own Cor LSA, so refreshing it");
888 NLSR_LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500889 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500890 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600891 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500892 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500893 }
894
akmhoquec7a79b22014-05-26 08:06:19 -0500895 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500896 NLSR_LOG_DEBUG("Adding Coordinate Lsa");
akmhoque2f423352014-06-03 11:49:35 -0500897 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500898 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500899 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
900 chkCorLsa->getKey(),
901 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700902 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500903 // Only sync coordinate LSAs if link-state routing is disabled
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600904 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500905 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500906 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500907 }
akmhoque53353462014-04-22 08:43:45 -0500908 }
Nick G97e34942016-07-11 14:46:27 -0500909 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500910 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500911 NLSR_LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500912 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500913 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600914 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
915 m_routingTable.scheduleRoutingTableCalculation();
akmhoque53353462014-04-22 08:43:45 -0500916 }
917 }
918 }
919}
920
akmhoque53353462014-04-22 08:43:45 -0500921void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700922Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500923 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500924{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600925 // increment SENT_LSA_INTEREST
926 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
927
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500928 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500929 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700930 }
Nick G97e34942016-07-11 14:46:27 -0500931 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500932 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500933 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500934 uint64_t seqNo = interestName[-1].toNumber();
935
Nick G97e34942016-07-11 14:46:27 -0500936 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500937 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
938 m_highestSeqNo[lsaName] = seqNo;
939 }
Nick G97e34942016-07-11 14:46:27 -0500940 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500941 else if (seqNo > m_highestSeqNo[lsaName]) {
942 m_highestSeqNo[lsaName] = seqNo;
943 }
Nick G97e34942016-07-11 14:46:27 -0500944 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500945 else if (seqNo < m_highestSeqNo[lsaName]) {
946 return;
947 }
948
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500949 ndn::Interest interest(interestName);
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500950 ndn::util::SegmentFetcher::Options options;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600951 options.interestLifetime = m_confParam.getLsaInterestLifetime();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500952
dmcoomes5bcb39e2017-10-31 15:07:55 -0500953 NLSR_LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600954 auto fetcher = ndn::util::SegmentFetcher::start(m_face, interest,
955 m_confParam.getValidator(), options);
Ashlesh Gawande05cb7282018-08-30 14:39:41 -0500956
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500957 auto it = m_fetchers.insert(fetcher).first;
958
959 fetcher->onComplete.connect([this, interestName, it] (ndn::ConstBufferPtr bufferPtr) {
960 afterFetchLsa(bufferPtr, interestName);
961 m_fetchers.erase(it);
962 });
963
laqinfan7c13ba52018-01-15 21:17:24 +0000964 fetcher->onError.connect([this, interestName, timeoutCount, deadline, lsaName, seqNo, it]
Ashlesh Gawande744e4812018-08-22 16:26:24 -0500965 (uint32_t errorCode, const std::string& msg) {
966 onFetchLsaError(errorCode, msg, interestName,
967 timeoutCount, deadline, lsaName, seqNo);
968 m_fetchers.erase(it);
969 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000970
971 m_lsaStorage.connectToFetcher(*fetcher);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600972 fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
973 afterSegmentValidatedSignal(data);
974 });
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000975
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600976 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -0500977 Lsa::Type lsaType;
978 std::istringstream(interestName[-2].toUri()) >> lsaType;
979 switch (lsaType) {
980 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600981 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500982 break;
983 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600984 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500985 break;
986 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600987 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -0500988 break;
989 default:
dmcoomes5bcb39e2017-10-31 15:07:55 -0500990 NLSR_LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600991 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500992}
993
994void
995Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
996{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600997 ndn::Name interestName(interest.getName());
998 NLSR_LOG_DEBUG("Interest received for LSA: " << interestName);
999
1000 if (interestName[-2].isVersion()) {
1001 // Interest for particular segment
1002 if (m_segmentPublisher.replyFromStore(interestName)) {
1003 NLSR_LOG_TRACE("Reply from SegmentPublisher storage");
1004 return;
1005 }
1006 // Remove version and segment
1007 interestName = interestName.getSubName(0, interestName.size() - 2);
1008 NLSR_LOG_TRACE("Interest w/o segment and version: " << interestName);
1009 }
1010
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001011 // increment RCV_LSA_INTEREST
1012 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1013
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001014 std::string chkString("LSA");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001015 int32_t lsaPosition = util::getNameComponentPosition(interestName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001016
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001017 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001018 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001019 originRouter.append(interestName.getSubName(lsaPosition + 1,
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001020 interestName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001021
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001022 // if the interest is for this router's LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001023 if (originRouter == m_confParam.getRouterPrefix() && lsaPosition >= 0) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001024 uint64_t seqNo = interestName[-1].toNumber();
1025 NLSR_LOG_DEBUG("LSA sequence number from interest: " << seqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001026
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001027 std::string lsaType = interestName[-2].toUri();
1028 Lsa::Type interestedLsType;
1029 std::istringstream(lsaType) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001030
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001031 if (interestedLsType == Lsa::Type::NAME) {
1032 processInterestForNameLsa(interest, originRouter.append(lsaType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001033 }
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001034 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1035 processInterestForAdjacencyLsa(interest, originRouter.append(lsaType), seqNo);
1036 }
1037 else if (interestedLsType == Lsa::Type::COORDINATE) {
1038 processInterestForCoordinateLsa(interest, originRouter.append(lsaType), seqNo);
1039 }
1040 else {
1041 NLSR_LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
1042 }
1043 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001044 }
1045 else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
1046 const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
1047 if (lsaSegment != nullptr) {
1048 NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001049 m_face.put(*lsaSegment);
akmhoque31d1d4b2014-05-05 22:08:14 -05001050 }
akmhoque157b0a42014-05-13 00:26:37 -05001051 else {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001052 NLSR_LOG_TRACE(interest << " was not found in this lsa storage.");
akmhoque31d1d4b2014-05-05 22:08:14 -05001053 }
1054 }
1055}
1056
Nick G97e34942016-07-11 14:46:27 -05001057 // \brief Finds and sends a requested name LSA.
1058 // \param interest The interest that seeks the name LSA.
1059 // \param lsaKey The LSA that the Interest is seeking.
1060 // \param seqNo A sequence number to ensure that we are sending the
1061 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001062void
akmhoque31d1d4b2014-05-05 22:08:14 -05001063Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1064 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001065 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001066{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001067 // increment RCV_NAME_LSA_INTEREST
1068 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001069 NLSR_LOG_DEBUG("nameLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001070 NameLsa* nameLsa = findNameLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001071 if (nameLsa != nullptr) {
1072 NLSR_LOG_TRACE("Verifying SeqNo for NameLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001073 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001074 std::string content = nameLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001075 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1076 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001077 m_lsaRefreshTime, m_signingInfo);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +00001078
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001079 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001080 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001081 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001082 NLSR_LOG_TRACE("SeqNo for nameLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001083 }
1084 }
1085 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001086 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001087 }
1088}
1089
Nick G97e34942016-07-11 14:46:27 -05001090 // \brief Finds and sends a requested adj. LSA.
1091 // \param interest The interest that seeks the adj. LSA.
1092 // \param lsaKey The LSA that the Interest is seeking.
1093 // \param seqNo A sequence number to ensure that we are sending the
1094 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001095void
1096Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1097 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001098 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001099{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001100 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001101 NLSR_LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001102 }
1103
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001104 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001105 NLSR_LOG_DEBUG("AdjLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001106 AdjLsa* adjLsa = findAdjLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001107 if (adjLsa != nullptr) {
1108 NLSR_LOG_TRACE("Verifying SeqNo for AdjLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001109 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001110 std::string content = adjLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001111 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1112 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001113 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001114
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001115 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001116 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001117 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001118 NLSR_LOG_TRACE("SeqNo for AdjLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001119 }
1120 }
1121 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001122 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001123 }
1124}
1125
Nick G97e34942016-07-11 14:46:27 -05001126 // \brief Finds and sends a requested cor. LSA.
1127 // \param interest The interest that seeks the cor. LSA.
1128 // \param lsaKey The LSA that the Interest is seeking.
1129 // \param seqNo A sequence number to ensure that we are sending the
1130 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001131void
1132Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1133 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001134 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001135{
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001136 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001137 NLSR_LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001138 }
1139
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001140 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes5bcb39e2017-10-31 15:07:55 -05001141 NLSR_LOG_DEBUG("CoordinateLsa interest " << interest << " received");
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001142 CoordinateLsa* corLsa = findCoordinateLsa(lsaKey);
dmcoomescf8d0ed2017-02-21 11:39:01 -06001143 if (corLsa != nullptr) {
1144 NLSR_LOG_TRACE("Verifying SeqNo for CoordinateLsa is same as requested.");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001145 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001146 std::string content = corLsa->serialize();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001147 m_segmentPublisher.publish(interest.getName(), interest.getName(),
1148 ndn::encoding::makeStringBlock(ndn::tlv::Content, content),
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001149 m_lsaRefreshTime, m_signingInfo);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -06001150
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001151 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001152 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001153 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001154 NLSR_LOG_TRACE("SeqNo for CoordinateLsa does not match");
dmcoomes9eaf3f42017-02-21 11:39:01 -06001155 }
1156 }
1157 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001158 NLSR_LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001159 }
1160}
1161
1162void
dmcoomes9f936662017-03-02 10:33:09 -06001163Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001164{
1165 const ndn::Name& dataName = data->getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -05001166 NLSR_LOG_DEBUG("Data validation successful for LSA: " << dataName);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001167
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001168 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001169 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001170
akmhoque157b0a42014-05-13 00:26:37 -05001171 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001172
Nick G97e34942016-07-11 14:46:27 -05001173 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001174 ndn::Name originRouter = m_confParam.getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001175 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001176
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001177 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001178 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1179 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001180
Nick Gordon727d4832017-10-13 18:04:25 -05001181 Lsa::Type interestedLsType;
1182 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001183
Nick Gordon727d4832017-10-13 18:04:25 -05001184 if (interestedLsType == Lsa::Type::NAME) {
1185 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1186 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001187 }
Nick Gordon727d4832017-10-13 18:04:25 -05001188 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1189 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1190 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001191 }
Nick Gordon727d4832017-10-13 18:04:25 -05001192 else if (interestedLsType == Lsa::Type::COORDINATE) {
1193 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1194 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001195 }
akmhoque157b0a42014-05-13 00:26:37 -05001196 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001197 NLSR_LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001198 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001199
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001200 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001201 }
1202}
1203
1204void
1205Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001206 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001207{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001208 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001209 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001210 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001211 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001212 installNameLsa(nameLsa);
1213 }
akmhoque157b0a42014-05-13 00:26:37 -05001214 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001215 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001216 }
1217 }
1218}
1219
1220void
1221Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001222 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001223{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001224 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001225 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001226 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001227 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001228 installAdjLsa(adjLsa);
1229 }
akmhoque157b0a42014-05-13 00:26:37 -05001230 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001231 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001232 }
1233 }
1234}
1235
1236void
1237Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001238 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001239{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001240 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001241 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001242 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001243 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001244 installCoordinateLsa(corLsa);
1245 }
akmhoque157b0a42014-05-13 00:26:37 -05001246 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -05001247 NLSR_LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001248 }
1249 }
1250}
1251
akmhoquec7a79b22014-05-26 08:06:19 -05001252ndn::time::system_clock::TimePoint
1253Lsdb::getLsaExpirationTimePoint()
1254{
1255 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1256 expirationTimePoint = expirationTimePoint +
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001257 ndn::time::seconds(m_confParam.getRouterDeadInterval());
akmhoquec7a79b22014-05-26 08:06:19 -05001258 return expirationTimePoint;
1259}
akmhoque31d1d4b2014-05-05 22:08:14 -05001260
1261void
akmhoque2f423352014-06-03 11:49:35 -05001262Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001263{
dmcoomes5bcb39e2017-10-31 15:07:55 -05001264 NLSR_LOG_DEBUG("---------------Adj LSDB-------------------");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -05001265 for (const auto& adj : m_adjLsdb) {
1266 adj.writeLog();
akmhoque53353462014-04-22 08:43:45 -05001267 }
1268}
1269
1270//-----utility function -----
1271bool
Nick Gordon727d4832017-10-13 18:04:25 -05001272Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001273{
Nick Gordon727d4832017-10-13 18:04:25 -05001274 switch (lsType) {
1275 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001276 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001277 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001278 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001279 case Lsa::Type::NAME:
1280 return doesNameLsaExist(key);
1281 default:
1282 return false;
akmhoque53353462014-04-22 08:43:45 -05001283 }
akmhoque53353462014-04-22 08:43:45 -05001284}
1285
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001286bool
Nick Gordon727d4832017-10-13 18:04:25 -05001287Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001288 const uint64_t& sequenceNumber) {
1289 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001290 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001291
Nick Gordon727d4832017-10-13 18:04:25 -05001292 switch (lsaType) {
1293 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001294 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001295 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001296 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001297 case Lsa::Type::NAME:
1298 return isNameLsaNew(lsaKey, sequenceNumber);
1299 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001300 return false;
1301 }
1302}
1303
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001304} // namespace nlsr