blob: eac7770f3909a8975d6f408356f281e9579bce42 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05003 * Copyright (c) 2014-2016, 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 **/
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>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050027
akmhoque31d1d4b2014-05-05 22:08:14 -050028#include <ndn-cxx/security/key-chain.hpp>
akmhoquec7a79b22014-05-26 08:06:19 -050029#include <ndn-cxx/util/time.hpp>
akmhoquefdbddb12014-05-02 18:35:19 -050030
Vince Lehman50df6b72015-03-03 12:06:40 -060031#include "conf-parameter.hpp"
akmhoque53353462014-04-22 08:43:45 -050032#include "lsa.hpp"
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050033#include "test-access-control.hpp"
akmhoque53353462014-04-22 08:43:45 -050034
35namespace nlsr {
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -070036
37using namespace ndn::time;
38
akmhoque53353462014-04-22 08:43:45 -050039class Nlsr;
Vince Lehman0bcf9a32014-12-10 11:24:45 -060040class SyncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -050041
42class Lsdb
43{
44public:
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050045 Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler, SyncLogicHandler& sync);
akmhoque53353462014-04-22 08:43:45 -050046
akmhoque53353462014-04-22 08:43:45 -050047 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050048 doesLsaExist(const ndn::Name& key, const std::string& lsType);
akmhoque53353462014-04-22 08:43:45 -050049
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050050 // functions related to Name LSDB
akmhoque53353462014-04-22 08:43:45 -050051 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050052 buildAndInstallOwnNameLsa();
akmhoque53353462014-04-22 08:43:45 -050053
akmhoqueb6450b12014-04-24 00:01:03 -050054 NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050055 findNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050056
57 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050058 installNameLsa(NameLsa& nlsa);
akmhoque53353462014-04-22 08:43:45 -050059
60 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050061 removeNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050062
63 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050064 isNameLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050065
66 void
akmhoque2f423352014-06-03 11:49:35 -050067 writeNameLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050068
Jiewen Tana0497d82015-02-02 21:59:18 -080069 const std::list<NameLsa>&
70 getNameLsdb();
71
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050072 // functions related to Cor LSDB
akmhoque53353462014-04-22 08:43:45 -050073 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050074 buildAndInstallOwnCoordinateLsa();
akmhoque53353462014-04-22 08:43:45 -050075
akmhoqueb6450b12014-04-24 00:01:03 -050076 CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050077 findCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050078
79 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050080 installCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -050081
82 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050083 removeCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050084
85 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050086 isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050087
88 void
akmhoque2f423352014-06-03 11:49:35 -050089 writeCorLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050090
Jiewen Tana0497d82015-02-02 21:59:18 -080091 const std::list<CoordinateLsa>&
92 getCoordinateLsdb();
93
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050094 // functions related to Adj LSDB
akmhoque53353462014-04-22 08:43:45 -050095 void
Vince Lehman50df6b72015-03-03 12:06:40 -060096 scheduleAdjLsaBuild();
akmhoque53353462014-04-22 08:43:45 -050097
98 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050099 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500100
101 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 removeAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500103
104 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500105 isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800106
akmhoque53353462014-04-22 08:43:45 -0500107 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500108 installAdjLsa(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500109
akmhoqueb6450b12014-04-24 00:01:03 -0500110 AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 findAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500112
Jiewen Tana0497d82015-02-02 21:59:18 -0800113 const std::list<AdjLsa>&
akmhoque53353462014-04-22 08:43:45 -0500114 getAdjLsdb();
115
116 void
Vince Lehman50df6b72015-03-03 12:06:40 -0600117 setAdjLsaBuildInterval(uint32_t interval)
118 {
119 m_adjLsaBuildInterval = ndn::time::seconds(interval);
120 }
121
122 const ndn::time::seconds&
123 getAdjLsaBuildInterval() const
124 {
125 return m_adjLsaBuildInterval;
126 }
127
128 void
akmhoque2f423352014-06-03 11:49:35 -0500129 writeAdjLsdbLog();
akmhoque53353462014-04-22 08:43:45 -0500130
akmhoque53353462014-04-22 08:43:45 -0500131 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700132 setLsaRefreshTime(const seconds& lsaRefreshTime);
akmhoque53353462014-04-22 08:43:45 -0500133
134 void
135 setThisRouterPrefix(std::string trp);
136
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500137 void
138 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
139 steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
140
141 void
142 processInterest(const ndn::Name& name, const ndn::Interest& interest);
143
akmhoque53353462014-04-22 08:43:45 -0500144private:
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);
akmhoque53353462014-04-22 08:43:45 -0500190
akmhoque31d1d4b2014-05-05 22:08:14 -0500191 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
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700210 onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
211
212 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500213 processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500214 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500215
216 void
217 processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500218 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500219
220 void
221 processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500222 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500223
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500224PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500225 /**
226 * @brief Error callback when SegmentFetcher fails to return an LSA
227 *
228 * In all error cases, a reattempt to fetch the LSA will be made.
229 *
230 * Segment validation can fail either because the packet does not have a
231 * valid signature (fatal) or because some of the certificates in the trust chain
232 * could not be fetched (non-fatal).
233 *
234 * Currently, the library does not provide clear indication (besides a plain-text message
235 * in the error callback) of the reason for the failure nor the segment that failed
236 * to be validated, thus we will continue to try to fetch the LSA until the deadline
237 * is reached.
238 */
akmhoque31d1d4b2014-05-05 22:08:14 -0500239 void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500240 onFetchLsaError(uint32_t errorCode,
241 const std::string& msg,
242 ndn::Name& interestName,
243 uint32_t retransmitNo,
244 const ndn::time::steady_clock::TimePoint& deadline,
245 ndn::Name lsaName,
246 uint64_t seqNo);
247
248 /**
249 * @brief Success callback when SegmentFetcher returns a valid LSA
250 *
251 * \param The base Interest used to fetch the LSA in the format:
252 * /<network>/NLSR/LSA/<site>/%C1.Router/<router>/<lsa-type>/<seqNo>
253 */
254 void
255 afterFetchLsa(const ndn::ConstBufferPtr& data, ndn::Name& interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500256
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500257private:
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700258 system_clock::TimePoint
akmhoquec7a79b22014-05-26 08:06:19 -0500259 getLsaExpirationTimePoint();
260
akmhoque31d1d4b2014-05-05 22:08:14 -0500261 void
262 cancelScheduleLsaExpiringEvent(ndn::EventId eid);
263
Jiewen Tana0497d82015-02-02 21:59:18 -0800264public:
265 static const ndn::Name::Component NAME_COMPONENT;
266
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500267private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500268 Nlsr& m_nlsr;
Vince Lehman7c603292014-09-11 17:48:16 -0500269 ndn::Scheduler& m_scheduler;
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600270 SyncLogicHandler& m_sync;
Vince Lehman7c603292014-09-11 17:48:16 -0500271
akmhoque53353462014-04-22 08:43:45 -0500272 std::list<NameLsa> m_nameLsdb;
273 std::list<AdjLsa> m_adjLsdb;
akmhoqueb6450b12014-04-24 00:01:03 -0500274 std::list<CoordinateLsa> m_corLsdb;
akmhoque53353462014-04-22 08:43:45 -0500275
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700276 seconds m_lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500277 std::string m_thisRouterPrefix;
278
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500279 typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
280
281 // Maps the name of an LSA to its highest known sequence number from sync;
282 // Used to stop NLSR from trying to fetch outdated LSAs
283 SequenceNumberMap m_highestSeqNo;
284
Vince Lehman18841082014-08-19 17:15:24 -0500285 static const ndn::time::seconds GRACE_PERIOD;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500286 static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
Vince Lehman50df6b72015-03-03 12:06:40 -0600287
288 ndn::time::seconds m_adjLsaBuildInterval;
akmhoque53353462014-04-22 08:43:45 -0500289};
290
291}//namespace nlsr
292
293#endif //NLSR_LSDB_HPP