blob: 417c978376a25f5fb89d426adcc881e10b4d156a [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)
akmhoque31d1d4b2014-05-05 22:08:14 -050048 , m_lsaRefreshTime(0)
Vince Lehman50df6b72015-03-03 12:06:40 -060049 , m_adjLsaBuildInterval(static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT))
akmhoque53353462014-04-22 08:43:45 -050050 {
51 }
52
akmhoque53353462014-04-22 08:43:45 -050053 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050054 doesLsaExist(const ndn::Name& key, const std::string& lsType);
akmhoque53353462014-04-22 08:43:45 -050055 // function related to Name LSDB
56
57 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050058 buildAndInstallOwnNameLsa();
akmhoque53353462014-04-22 08:43:45 -050059
akmhoqueb6450b12014-04-24 00:01:03 -050060 NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050061 findNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050062
63 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050064 installNameLsa(NameLsa& nlsa);
akmhoque53353462014-04-22 08:43:45 -050065
66 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050067 removeNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050068
69 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050070 isNameLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050071
72 void
akmhoque2f423352014-06-03 11:49:35 -050073 writeNameLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050074
Jiewen Tana0497d82015-02-02 21:59:18 -080075 const std::list<NameLsa>&
76 getNameLsdb();
77
akmhoque53353462014-04-22 08:43:45 -050078 //function related to Cor LSDB
79 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050080 buildAndInstallOwnCoordinateLsa();
akmhoque53353462014-04-22 08:43:45 -050081
akmhoqueb6450b12014-04-24 00:01:03 -050082 CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050083 findCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050084
85 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050086 installCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -050087
88 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050089 removeCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050090
91 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050092 isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050093
94 void
akmhoque2f423352014-06-03 11:49:35 -050095 writeCorLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050096
Jiewen Tana0497d82015-02-02 21:59:18 -080097 const std::list<CoordinateLsa>&
98 getCoordinateLsdb();
99
akmhoque53353462014-04-22 08:43:45 -0500100 //function related to Adj LSDB
Vince Lehman50df6b72015-03-03 12:06:40 -0600101
akmhoque53353462014-04-22 08:43:45 -0500102 void
Vince Lehman50df6b72015-03-03 12:06:40 -0600103 scheduleAdjLsaBuild();
akmhoque53353462014-04-22 08:43:45 -0500104
105 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500107
108 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 removeAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500110
111 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500112 isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800113
akmhoque53353462014-04-22 08:43:45 -0500114 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500115 installAdjLsa(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500116
akmhoqueb6450b12014-04-24 00:01:03 -0500117 AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 findAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500119
Jiewen Tana0497d82015-02-02 21:59:18 -0800120 const std::list<AdjLsa>&
akmhoque53353462014-04-22 08:43:45 -0500121 getAdjLsdb();
122
123 void
Vince Lehman50df6b72015-03-03 12:06:40 -0600124 setAdjLsaBuildInterval(uint32_t interval)
125 {
126 m_adjLsaBuildInterval = ndn::time::seconds(interval);
127 }
128
129 const ndn::time::seconds&
130 getAdjLsaBuildInterval() const
131 {
132 return m_adjLsaBuildInterval;
133 }
134
135 void
akmhoque2f423352014-06-03 11:49:35 -0500136 writeAdjLsdbLog();
akmhoque53353462014-04-22 08:43:45 -0500137
akmhoque53353462014-04-22 08:43:45 -0500138 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700139 setLsaRefreshTime(const seconds& lsaRefreshTime);
akmhoque53353462014-04-22 08:43:45 -0500140
141 void
142 setThisRouterPrefix(std::string trp);
143
144private:
145 bool
146 addNameLsa(NameLsa& nlsa);
147
148 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500149 doesNameLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500150
151
152 bool
akmhoqueb6450b12014-04-24 00:01:03 -0500153 addCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500154
155 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500156 doesCoordinateLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500157
Vince Lehman50df6b72015-03-03 12:06:40 -0600158 void
159 buildAdjLsa();
160
akmhoque53353462014-04-22 08:43:45 -0500161 bool
162 addAdjLsa(AdjLsa& alsa);
163
164 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 doesAdjLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500166
167 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500168 scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700169 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500170
171 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500173
Vince Lehman199e9cf2015-04-07 13:22:16 -0500174PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque53353462014-04-22 08:43:45 -0500175 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
Vince Lehman199e9cf2015-04-07 13:22:16 -0500179private:
akmhoque53353462014-04-22 08:43:45 -0500180 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500182
183 ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500184 scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700185 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500186
187 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500188 exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500189 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500190public:
191 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700192 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500193 steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
akmhoque53353462014-04-22 08:43:45 -0500194
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 void
196 processInterest(const ndn::Name& name, const ndn::Interest& interest);
akmhoque53353462014-04-22 08:43:45 -0500197
198private:
199 void
akmhoque69c9aa92014-07-23 15:15:05 -0500200 putLsaData(const ndn::Interest& interest, const std::string& content);
201
202 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500203 processInterestForNameLsa(const ndn::Interest& interest,
204 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500205 uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500206
akmhoque31d1d4b2014-05-05 22:08:14 -0500207 void
208 processInterestForAdjacencyLsa(const ndn::Interest& interest,
209 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500210 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500211
212 void
213 processInterestForCoordinateLsa(const ndn::Interest& interest,
214 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500215 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500216
217 void
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500218 onContent(const ndn::Data& data, const steady_clock::TimePoint& deadline,
219 ndn::Name lsaName, uint64_t seqNo);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700220
221 /**
222 * @brief Retry validation after it fails
223 *
224 * Data packet validation can fail either because the packet does not have
225 * valid signature (fatal) or because some of the certificate chain Data packets
226 * failed to be fetched (non-fatal). Currently, the library does not provide
227 * clear indication (besides plain-text message in error callback) of what is
228 * the reason for failure and we will try to re-validate for as long as it the deadline.
229 */
230 void
231 retryContentValidation(const ndn::shared_ptr<const ndn::Data>& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500232 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
233 uint64_t seqNo);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700234
235 void
236 onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
237
238 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700239 onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500240 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
241 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500242
243 void
244 processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500245 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500246
247 void
248 processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500249 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500250
251 void
252 processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500253 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500254
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500255PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500256 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700257 processInterestTimedOut(const ndn::Interest& interest, uint32_t retransmitNo,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500258 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
259 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500260
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500261private:
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700262 system_clock::TimePoint
akmhoquec7a79b22014-05-26 08:06:19 -0500263 getLsaExpirationTimePoint();
264
akmhoque31d1d4b2014-05-05 22:08:14 -0500265 void
266 cancelScheduleLsaExpiringEvent(ndn::EventId eid);
267
Jiewen Tana0497d82015-02-02 21:59:18 -0800268public:
269 static const ndn::Name::Component NAME_COMPONENT;
270
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500271private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500272 Nlsr& m_nlsr;
Vince Lehman7c603292014-09-11 17:48:16 -0500273 ndn::Scheduler& m_scheduler;
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600274 SyncLogicHandler& m_sync;
Vince Lehman7c603292014-09-11 17:48:16 -0500275
akmhoque53353462014-04-22 08:43:45 -0500276 std::list<NameLsa> m_nameLsdb;
277 std::list<AdjLsa> m_adjLsdb;
akmhoqueb6450b12014-04-24 00:01:03 -0500278 std::list<CoordinateLsa> m_corLsdb;
akmhoque53353462014-04-22 08:43:45 -0500279
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700280 seconds m_lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500281 std::string m_thisRouterPrefix;
282
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500283 typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
284
285 // Maps the name of an LSA to its highest known sequence number from sync;
286 // Used to stop NLSR from trying to fetch outdated LSAs
287 SequenceNumberMap m_highestSeqNo;
288
Vince Lehman18841082014-08-19 17:15:24 -0500289 static const ndn::time::seconds GRACE_PERIOD;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500290 static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
Vince Lehman50df6b72015-03-03 12:06:40 -0600291
292 ndn::time::seconds m_adjLsaBuildInterval;
akmhoque53353462014-04-22 08:43:45 -0500293};
294
295}//namespace nlsr
296
297#endif //NLSR_LSDB_HPP