blob: 7ab54e5a9f1477c7d9cdbfcfded8ac6fae3d9bf2 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehmanc2e51f62015-01-20 15:03:11 -06003 * Copyright (c) 2014-2015, The University of Memphis,
4 * 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 **/
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050021
akmhoque53353462014-04-22 08:43:45 -050022#ifndef NLSR_LSDB_HPP
23#define NLSR_LSDB_HPP
24
25#include <utility>
akmhoquefdbddb12014-05-02 18:35:19 -050026#include <boost/cstdint.hpp>
akmhoque31d1d4b2014-05-05 22:08:14 -050027#include <ndn-cxx/security/key-chain.hpp>
akmhoquec7a79b22014-05-26 08:06:19 -050028#include <ndn-cxx/util/time.hpp>
akmhoquefdbddb12014-05-02 18:35:19 -050029
Vince Lehman50df6b72015-03-03 12:06:40 -060030#include "conf-parameter.hpp"
akmhoque53353462014-04-22 08:43:45 -050031#include "lsa.hpp"
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050032#include "test-access-control.hpp"
akmhoque53353462014-04-22 08:43:45 -050033
34namespace nlsr {
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -070035
36using namespace ndn::time;
37
akmhoque53353462014-04-22 08:43:45 -050038class Nlsr;
Vince Lehman0bcf9a32014-12-10 11:24:45 -060039class SyncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -050040
41class Lsdb
42{
43public:
Vince Lehman0bcf9a32014-12-10 11:24:45 -060044 Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler, SyncLogicHandler& sync)
akmhoque31d1d4b2014-05-05 22:08:14 -050045 : m_nlsr(nlsr)
Vince Lehman7c603292014-09-11 17:48:16 -050046 , m_scheduler(scheduler)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060047 , m_sync(sync)
Vince Lehman904c2412014-09-23 19:36:11 -050048 , m_hasSyncPrefixBeenRegistered(false)
akmhoque31d1d4b2014-05-05 22:08:14 -050049 , m_lsaRefreshTime(0)
Vince Lehman50df6b72015-03-03 12:06:40 -060050 , m_adjLsaBuildInterval(static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT))
akmhoque53353462014-04-22 08:43:45 -050051 {
52 }
53
akmhoque53353462014-04-22 08:43:45 -050054 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050055 doesLsaExist(const ndn::Name& key, const std::string& lsType);
akmhoque53353462014-04-22 08:43:45 -050056 // function related to Name LSDB
57
58 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050059 buildAndInstallOwnNameLsa();
akmhoque53353462014-04-22 08:43:45 -050060
akmhoqueb6450b12014-04-24 00:01:03 -050061 NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050062 findNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050063
64 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050065 installNameLsa(NameLsa& nlsa);
akmhoque53353462014-04-22 08:43:45 -050066
67 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050068 removeNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050069
70 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050071 isNameLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050072
73 void
akmhoque2f423352014-06-03 11:49:35 -050074 writeNameLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050075
Jiewen Tana0497d82015-02-02 21:59:18 -080076 const std::list<NameLsa>&
77 getNameLsdb();
78
akmhoque53353462014-04-22 08:43:45 -050079 //function related to Cor LSDB
80 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050081 buildAndInstallOwnCoordinateLsa();
akmhoque53353462014-04-22 08:43:45 -050082
akmhoqueb6450b12014-04-24 00:01:03 -050083 CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050084 findCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050085
86 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050087 installCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -050088
89 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050090 removeCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050091
92 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050093 isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050094
95 void
akmhoque2f423352014-06-03 11:49:35 -050096 writeCorLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050097
Jiewen Tana0497d82015-02-02 21:59:18 -080098 const std::list<CoordinateLsa>&
99 getCoordinateLsdb();
100
akmhoque53353462014-04-22 08:43:45 -0500101 //function related to Adj LSDB
Vince Lehman50df6b72015-03-03 12:06:40 -0600102
akmhoque53353462014-04-22 08:43:45 -0500103 void
Vince Lehman50df6b72015-03-03 12:06:40 -0600104 scheduleAdjLsaBuild();
akmhoque53353462014-04-22 08:43:45 -0500105
106 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500107 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500108
109 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500110 removeAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500111
112 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800114
akmhoque53353462014-04-22 08:43:45 -0500115 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500116 installAdjLsa(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500117
akmhoqueb6450b12014-04-24 00:01:03 -0500118 AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500119 findAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500120
Jiewen Tana0497d82015-02-02 21:59:18 -0800121 const std::list<AdjLsa>&
akmhoque53353462014-04-22 08:43:45 -0500122 getAdjLsdb();
123
124 void
Vince Lehman50df6b72015-03-03 12:06:40 -0600125 setAdjLsaBuildInterval(uint32_t interval)
126 {
127 m_adjLsaBuildInterval = ndn::time::seconds(interval);
128 }
129
130 const ndn::time::seconds&
131 getAdjLsaBuildInterval() const
132 {
133 return m_adjLsaBuildInterval;
134 }
135
136 void
akmhoque2f423352014-06-03 11:49:35 -0500137 writeAdjLsdbLog();
akmhoque53353462014-04-22 08:43:45 -0500138
akmhoque53353462014-04-22 08:43:45 -0500139 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700140 setLsaRefreshTime(const seconds& lsaRefreshTime);
akmhoque53353462014-04-22 08:43:45 -0500141
142 void
143 setThisRouterPrefix(std::string trp);
144
145private:
146 bool
147 addNameLsa(NameLsa& nlsa);
148
149 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 doesNameLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500151
152
153 bool
akmhoqueb6450b12014-04-24 00:01:03 -0500154 addCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500155
156 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500157 doesCoordinateLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500158
Vince Lehman50df6b72015-03-03 12:06:40 -0600159 void
160 buildAdjLsa();
161
akmhoque53353462014-04-22 08:43:45 -0500162 bool
163 addAdjLsa(AdjLsa& alsa);
164
165 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 doesAdjLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500167
168 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500169 scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700170 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500171
172 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500173 exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500174
175 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500176 scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700177 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500178
179 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500180 exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500181
182 ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700184 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500185
186 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500187 exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500188 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500189public:
190 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700191 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500192 steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
akmhoque53353462014-04-22 08:43:45 -0500193
akmhoque31d1d4b2014-05-05 22:08:14 -0500194 void
195 processInterest(const ndn::Name& name, const ndn::Interest& interest);
akmhoque53353462014-04-22 08:43:45 -0500196
197private:
198 void
akmhoque69c9aa92014-07-23 15:15:05 -0500199 putLsaData(const ndn::Interest& interest, const std::string& content);
200
201 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500202 processInterestForNameLsa(const ndn::Interest& interest,
203 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500204 uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500205
akmhoque31d1d4b2014-05-05 22:08:14 -0500206 void
207 processInterestForAdjacencyLsa(const ndn::Interest& interest,
208 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500209 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500210
211 void
212 processInterestForCoordinateLsa(const ndn::Interest& interest,
213 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500214 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500215
216 void
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500217 onContent(const ndn::Data& data, const steady_clock::TimePoint& deadline,
218 ndn::Name lsaName, uint64_t seqNo);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700219
220 /**
221 * @brief Retry validation after it fails
222 *
223 * Data packet validation can fail either because the packet does not have
224 * valid signature (fatal) or because some of the certificate chain Data packets
225 * failed to be fetched (non-fatal). Currently, the library does not provide
226 * clear indication (besides plain-text message in error callback) of what is
227 * the reason for failure and we will try to re-validate for as long as it the deadline.
228 */
229 void
230 retryContentValidation(const ndn::shared_ptr<const ndn::Data>& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500231 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
232 uint64_t seqNo);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700233
234 void
235 onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
236
237 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700238 onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500239 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
240 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500241
242 void
243 processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500244 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500245
246 void
247 processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500248 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500249
250 void
251 processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500252 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500253
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500254PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500255 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700256 processInterestTimedOut(const ndn::Interest& interest, uint32_t retransmitNo,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500257 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
258 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500259
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500260private:
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700261 system_clock::TimePoint
akmhoquec7a79b22014-05-26 08:06:19 -0500262 getLsaExpirationTimePoint();
263
akmhoque31d1d4b2014-05-05 22:08:14 -0500264 void
265 cancelScheduleLsaExpiringEvent(ndn::EventId eid);
266
Jiewen Tana0497d82015-02-02 21:59:18 -0800267public:
268 static const ndn::Name::Component NAME_COMPONENT;
269
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500270private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500271 Nlsr& m_nlsr;
Vince Lehman7c603292014-09-11 17:48:16 -0500272 ndn::Scheduler& m_scheduler;
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600273 SyncLogicHandler& m_sync;
Vince Lehman7c603292014-09-11 17:48:16 -0500274
akmhoque53353462014-04-22 08:43:45 -0500275 std::list<NameLsa> m_nameLsdb;
276 std::list<AdjLsa> m_adjLsdb;
akmhoqueb6450b12014-04-24 00:01:03 -0500277 std::list<CoordinateLsa> m_corLsdb;
akmhoque53353462014-04-22 08:43:45 -0500278
Vince Lehman904c2412014-09-23 19:36:11 -0500279 bool m_hasSyncPrefixBeenRegistered;
280
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700281 seconds m_lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500282 std::string m_thisRouterPrefix;
283
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500284 typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
285
286 // Maps the name of an LSA to its highest known sequence number from sync;
287 // Used to stop NLSR from trying to fetch outdated LSAs
288 SequenceNumberMap m_highestSeqNo;
289
Vince Lehman18841082014-08-19 17:15:24 -0500290 static const ndn::time::seconds GRACE_PERIOD;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500291 static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
Vince Lehman50df6b72015-03-03 12:06:40 -0600292
293 ndn::time::seconds m_adjLsaBuildInterval;
akmhoque53353462014-04-22 08:43:45 -0500294};
295
296}//namespace nlsr
297
298#endif //NLSR_LSDB_HPP