blob: 3cf74c6b54b82b59189630d3c92529dc6af7f373 [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
Vince Lehman199e9cf2015-04-07 13:22:16 -0500175PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque53353462014-04-22 08:43:45 -0500176 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500177 scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700178 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500179
Vince Lehman199e9cf2015-04-07 13:22:16 -0500180private:
akmhoque53353462014-04-22 08:43:45 -0500181 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500182 exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500183
184 ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500185 scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700186 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500187
188 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500189 exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500190 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500191public:
192 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700193 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500194 steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
akmhoque53353462014-04-22 08:43:45 -0500195
akmhoque31d1d4b2014-05-05 22:08:14 -0500196 void
197 processInterest(const ndn::Name& name, const ndn::Interest& interest);
akmhoque53353462014-04-22 08:43:45 -0500198
199private:
200 void
akmhoque69c9aa92014-07-23 15:15:05 -0500201 putLsaData(const ndn::Interest& interest, const std::string& content);
202
203 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500204 processInterestForNameLsa(const ndn::Interest& interest,
205 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500206 uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500207
akmhoque31d1d4b2014-05-05 22:08:14 -0500208 void
209 processInterestForAdjacencyLsa(const ndn::Interest& interest,
210 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500211 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500212
213 void
214 processInterestForCoordinateLsa(const ndn::Interest& interest,
215 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500216 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500217
218 void
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500219 onContent(const ndn::Data& data, const steady_clock::TimePoint& deadline,
220 ndn::Name lsaName, uint64_t seqNo);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700221
222 /**
223 * @brief Retry validation after it fails
224 *
225 * Data packet validation can fail either because the packet does not have
226 * valid signature (fatal) or because some of the certificate chain Data packets
227 * failed to be fetched (non-fatal). Currently, the library does not provide
228 * clear indication (besides plain-text message in error callback) of what is
229 * the reason for failure and we will try to re-validate for as long as it the deadline.
230 */
231 void
232 retryContentValidation(const ndn::shared_ptr<const ndn::Data>& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500233 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
234 uint64_t seqNo);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700235
236 void
237 onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
238
239 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700240 onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500241 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
242 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500243
244 void
245 processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500246 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500247
248 void
249 processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500250 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500251
252 void
253 processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500254 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500255
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500256PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500257 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700258 processInterestTimedOut(const ndn::Interest& interest, uint32_t retransmitNo,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500259 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
260 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500261
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500262private:
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700263 system_clock::TimePoint
akmhoquec7a79b22014-05-26 08:06:19 -0500264 getLsaExpirationTimePoint();
265
akmhoque31d1d4b2014-05-05 22:08:14 -0500266 void
267 cancelScheduleLsaExpiringEvent(ndn::EventId eid);
268
Jiewen Tana0497d82015-02-02 21:59:18 -0800269public:
270 static const ndn::Name::Component NAME_COMPONENT;
271
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500272private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500273 Nlsr& m_nlsr;
Vince Lehman7c603292014-09-11 17:48:16 -0500274 ndn::Scheduler& m_scheduler;
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600275 SyncLogicHandler& m_sync;
Vince Lehman7c603292014-09-11 17:48:16 -0500276
akmhoque53353462014-04-22 08:43:45 -0500277 std::list<NameLsa> m_nameLsdb;
278 std::list<AdjLsa> m_adjLsdb;
akmhoqueb6450b12014-04-24 00:01:03 -0500279 std::list<CoordinateLsa> m_corLsdb;
akmhoque53353462014-04-22 08:43:45 -0500280
Vince Lehman904c2412014-09-23 19:36:11 -0500281 bool m_hasSyncPrefixBeenRegistered;
282
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700283 seconds m_lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500284 std::string m_thisRouterPrefix;
285
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500286 typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
287
288 // Maps the name of an LSA to its highest known sequence number from sync;
289 // Used to stop NLSR from trying to fetch outdated LSAs
290 SequenceNumberMap m_highestSeqNo;
291
Vince Lehman18841082014-08-19 17:15:24 -0500292 static const ndn::time::seconds GRACE_PERIOD;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500293 static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
Vince Lehman50df6b72015-03-03 12:06:40 -0600294
295 ndn::time::seconds m_adjLsaBuildInterval;
akmhoque53353462014-04-22 08:43:45 -0500296};
297
298}//namespace nlsr
299
300#endif //NLSR_LSDB_HPP