blob: 34d75621f27f3a0807eb1e15c4b5690b3c1f142d [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
76 //function related to Cor LSDB
77 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050078 buildAndInstallOwnCoordinateLsa();
akmhoque53353462014-04-22 08:43:45 -050079
akmhoqueb6450b12014-04-24 00:01:03 -050080 CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050081 findCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050082
83 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050084 installCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -050085
86 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050087 removeCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050088
89 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050090 isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050091
92 void
akmhoque2f423352014-06-03 11:49:35 -050093 writeCorLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050094
95 //function related to Adj LSDB
Vince Lehman50df6b72015-03-03 12:06:40 -060096
akmhoque53353462014-04-22 08:43:45 -050097 void
Vince Lehman50df6b72015-03-03 12:06:40 -060098 scheduleAdjLsaBuild();
akmhoque53353462014-04-22 08:43:45 -050099
100 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500102
103 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 removeAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500105
106 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500107 isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500108 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 installAdjLsa(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500110
akmhoqueb6450b12014-04-24 00:01:03 -0500111 AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500112 findAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500113
114 std::list<AdjLsa>&
115 getAdjLsdb();
116
117 void
Vince Lehman50df6b72015-03-03 12:06:40 -0600118 setAdjLsaBuildInterval(uint32_t interval)
119 {
120 m_adjLsaBuildInterval = ndn::time::seconds(interval);
121 }
122
123 const ndn::time::seconds&
124 getAdjLsaBuildInterval() const
125 {
126 return m_adjLsaBuildInterval;
127 }
128
129 void
akmhoque2f423352014-06-03 11:49:35 -0500130 writeAdjLsdbLog();
akmhoque53353462014-04-22 08:43:45 -0500131
akmhoque53353462014-04-22 08:43:45 -0500132 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700133 setLsaRefreshTime(const seconds& lsaRefreshTime);
akmhoque53353462014-04-22 08:43:45 -0500134
135 void
136 setThisRouterPrefix(std::string trp);
137
138private:
139 bool
140 addNameLsa(NameLsa& nlsa);
141
142 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 doesNameLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500144
145
146 bool
akmhoqueb6450b12014-04-24 00:01:03 -0500147 addCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500148
149 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 doesCoordinateLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500151
Vince Lehman50df6b72015-03-03 12:06:40 -0600152 void
153 buildAdjLsa();
154
akmhoque53353462014-04-22 08:43:45 -0500155 bool
156 addAdjLsa(AdjLsa& alsa);
157
158 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500159 doesAdjLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500160
161 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500162 scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700163 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500164
165 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500167
168 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500169 scheduleAdjLsaExpiration(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 exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500174
175 ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500176 scheduleCoordinateLsaExpiration(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 exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500181 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500182public:
183 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700184 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500185 steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
akmhoque53353462014-04-22 08:43:45 -0500186
akmhoque31d1d4b2014-05-05 22:08:14 -0500187 void
188 processInterest(const ndn::Name& name, const ndn::Interest& interest);
akmhoque53353462014-04-22 08:43:45 -0500189
190private:
191 void
akmhoque69c9aa92014-07-23 15:15:05 -0500192 putLsaData(const ndn::Interest& interest, const std::string& content);
193
194 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 processInterestForNameLsa(const ndn::Interest& interest,
196 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500197 uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500198
akmhoque31d1d4b2014-05-05 22:08:14 -0500199 void
200 processInterestForAdjacencyLsa(const ndn::Interest& interest,
201 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500202 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500203
204 void
205 processInterestForCoordinateLsa(const ndn::Interest& interest,
206 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500207 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500208
209 void
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500210 onContent(const ndn::Data& data, const steady_clock::TimePoint& deadline,
211 ndn::Name lsaName, uint64_t seqNo);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700212
213 /**
214 * @brief Retry validation after it fails
215 *
216 * Data packet validation can fail either because the packet does not have
217 * valid signature (fatal) or because some of the certificate chain Data packets
218 * failed to be fetched (non-fatal). Currently, the library does not provide
219 * clear indication (besides plain-text message in error callback) of what is
220 * the reason for failure and we will try to re-validate for as long as it the deadline.
221 */
222 void
223 retryContentValidation(const ndn::shared_ptr<const ndn::Data>& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500224 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
225 uint64_t seqNo);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700226
227 void
228 onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
229
230 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700231 onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500232 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
233 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500234
235 void
236 processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500237 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500238
239 void
240 processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500241 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500242
243 void
244 processContentCoordinateLsa(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
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500247PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500248 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700249 processInterestTimedOut(const ndn::Interest& interest, uint32_t retransmitNo,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500250 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
251 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500252
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500253private:
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700254 system_clock::TimePoint
akmhoquec7a79b22014-05-26 08:06:19 -0500255 getLsaExpirationTimePoint();
256
akmhoque31d1d4b2014-05-05 22:08:14 -0500257 void
258 cancelScheduleLsaExpiringEvent(ndn::EventId eid);
259
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500260private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500261 Nlsr& m_nlsr;
Vince Lehman7c603292014-09-11 17:48:16 -0500262 ndn::Scheduler& m_scheduler;
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600263 SyncLogicHandler& m_sync;
Vince Lehman7c603292014-09-11 17:48:16 -0500264
akmhoque53353462014-04-22 08:43:45 -0500265 std::list<NameLsa> m_nameLsdb;
266 std::list<AdjLsa> m_adjLsdb;
akmhoqueb6450b12014-04-24 00:01:03 -0500267 std::list<CoordinateLsa> m_corLsdb;
akmhoque53353462014-04-22 08:43:45 -0500268
Vince Lehman904c2412014-09-23 19:36:11 -0500269 bool m_hasSyncPrefixBeenRegistered;
270
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700271 seconds m_lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500272 std::string m_thisRouterPrefix;
273
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500274 typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
275
276 // Maps the name of an LSA to its highest known sequence number from sync;
277 // Used to stop NLSR from trying to fetch outdated LSAs
278 SequenceNumberMap m_highestSeqNo;
279
Vince Lehman18841082014-08-19 17:15:24 -0500280 static const ndn::time::seconds GRACE_PERIOD;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500281 static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
Vince Lehman50df6b72015-03-03 12:06:40 -0600282
283 ndn::time::seconds m_adjLsaBuildInterval;
akmhoque53353462014-04-22 08:43:45 -0500284};
285
286}//namespace nlsr
287
288#endif //NLSR_LSDB_HPP