blob: efef94cc8b4c187cdb8ffba46ced87a05e0a2c6b [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050023
akmhoque53353462014-04-22 08:43:45 -050024#ifndef NLSR_LSDB_HPP
25#define NLSR_LSDB_HPP
26
27#include <utility>
akmhoquefdbddb12014-05-02 18:35:19 -050028#include <boost/cstdint.hpp>
akmhoque31d1d4b2014-05-05 22:08:14 -050029#include <ndn-cxx/security/key-chain.hpp>
akmhoquec7a79b22014-05-26 08:06:19 -050030#include <ndn-cxx/util/time.hpp>
akmhoquefdbddb12014-05-02 18:35:19 -050031
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:
Vince Lehman0bcf9a32014-12-10 11:24:45 -060045 Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler, SyncLogicHandler& sync)
akmhoque31d1d4b2014-05-05 22:08:14 -050046 : m_nlsr(nlsr)
Vince Lehman7c603292014-09-11 17:48:16 -050047 , m_scheduler(scheduler)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060048 , m_sync(sync)
Vince Lehman904c2412014-09-23 19:36:11 -050049 , m_hasSyncPrefixBeenRegistered(false)
akmhoque31d1d4b2014-05-05 22:08:14 -050050 , m_lsaRefreshTime(0)
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
96 void
akmhoque31d1d4b2014-05-05 22:08:14 -050097 scheduledAdjLsaBuild();
akmhoque53353462014-04-22 08:43:45 -050098
99 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500101
102 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 removeAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500104
105 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
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
113 std::list<AdjLsa>&
114 getAdjLsdb();
115
116 void
akmhoque2f423352014-06-03 11:49:35 -0500117 writeAdjLsdbLog();
akmhoque53353462014-04-22 08:43:45 -0500118
akmhoque53353462014-04-22 08:43:45 -0500119 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700120 setLsaRefreshTime(const seconds& lsaRefreshTime);
akmhoque53353462014-04-22 08:43:45 -0500121
122 void
123 setThisRouterPrefix(std::string trp);
124
125private:
126 bool
127 addNameLsa(NameLsa& nlsa);
128
129 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500130 doesNameLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500131
132
133 bool
akmhoqueb6450b12014-04-24 00:01:03 -0500134 addCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500135
136 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500137 doesCoordinateLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500138
139 bool
140 addAdjLsa(AdjLsa& alsa);
141
142 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 doesAdjLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500144
145 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500146 scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700147 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500148
149 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500151
152 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500153 scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700154 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500155
156 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500157 exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500158
159 ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500160 scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700161 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500162
163 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500164 exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500165 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500166public:
167 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700168 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500169 steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
akmhoque53353462014-04-22 08:43:45 -0500170
akmhoque31d1d4b2014-05-05 22:08:14 -0500171 void
172 processInterest(const ndn::Name& name, const ndn::Interest& interest);
akmhoque53353462014-04-22 08:43:45 -0500173
174private:
175 void
akmhoque69c9aa92014-07-23 15:15:05 -0500176 putLsaData(const ndn::Interest& interest, const std::string& content);
177
178 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500179 processInterestForNameLsa(const ndn::Interest& interest,
180 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500181 uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500182
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 void
184 processInterestForAdjacencyLsa(const ndn::Interest& interest,
185 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500186 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500187
188 void
189 processInterestForCoordinateLsa(const ndn::Interest& interest,
190 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500191 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500192
193 void
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500194 onContent(const ndn::Data& data, const steady_clock::TimePoint& deadline,
195 ndn::Name lsaName, uint64_t seqNo);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700196
197 /**
198 * @brief Retry validation after it fails
199 *
200 * Data packet validation can fail either because the packet does not have
201 * valid signature (fatal) or because some of the certificate chain Data packets
202 * failed to be fetched (non-fatal). Currently, the library does not provide
203 * clear indication (besides plain-text message in error callback) of what is
204 * the reason for failure and we will try to re-validate for as long as it the deadline.
205 */
206 void
207 retryContentValidation(const ndn::shared_ptr<const ndn::Data>& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500208 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
209 uint64_t seqNo);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700210
211 void
212 onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
213
214 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700215 onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500216 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
217 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500218
219 void
220 processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500221 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500222
223 void
224 processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500225 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500226
227 void
228 processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500229 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500230
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500231PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500232 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700233 processInterestTimedOut(const ndn::Interest& interest, uint32_t retransmitNo,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500234 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
235 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500236
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500237private:
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700238 system_clock::TimePoint
akmhoquec7a79b22014-05-26 08:06:19 -0500239 getLsaExpirationTimePoint();
240
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 void
242 cancelScheduleLsaExpiringEvent(ndn::EventId eid);
243
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500244private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500245 Nlsr& m_nlsr;
Vince Lehman7c603292014-09-11 17:48:16 -0500246 ndn::Scheduler& m_scheduler;
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600247 SyncLogicHandler& m_sync;
Vince Lehman7c603292014-09-11 17:48:16 -0500248
akmhoque53353462014-04-22 08:43:45 -0500249 std::list<NameLsa> m_nameLsdb;
250 std::list<AdjLsa> m_adjLsdb;
akmhoqueb6450b12014-04-24 00:01:03 -0500251 std::list<CoordinateLsa> m_corLsdb;
akmhoque53353462014-04-22 08:43:45 -0500252
Vince Lehman904c2412014-09-23 19:36:11 -0500253 bool m_hasSyncPrefixBeenRegistered;
254
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700255 seconds m_lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500256 std::string m_thisRouterPrefix;
257
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500258 typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
259
260 // Maps the name of an LSA to its highest known sequence number from sync;
261 // Used to stop NLSR from trying to fetch outdated LSAs
262 SequenceNumberMap m_highestSeqNo;
263
Vince Lehman18841082014-08-19 17:15:24 -0500264 static const ndn::time::seconds GRACE_PERIOD;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500265 static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
akmhoque53353462014-04-22 08:43:45 -0500266};
267
268}//namespace nlsr
269
270#endif //NLSR_LSDB_HPP