blob: 5d66f01c81d834f9132427b7d7b51377315e483d [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;
40
41class Lsdb
42{
43public:
Vince Lehman7c603292014-09-11 17:48:16 -050044 Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler)
akmhoque31d1d4b2014-05-05 22:08:14 -050045 : m_nlsr(nlsr)
Vince Lehman7c603292014-09-11 17:48:16 -050046 , m_scheduler(scheduler)
Vince Lehman904c2412014-09-23 19:36:11 -050047 , m_hasSyncPrefixBeenRegistered(false)
akmhoque31d1d4b2014-05-05 22:08:14 -050048 , m_lsaRefreshTime(0)
akmhoque53353462014-04-22 08:43:45 -050049 {
50 }
51
akmhoque53353462014-04-22 08:43:45 -050052 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050053 doesLsaExist(const ndn::Name& key, const std::string& lsType);
akmhoque53353462014-04-22 08:43:45 -050054 // function related to Name LSDB
55
56 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050057 buildAndInstallOwnNameLsa();
akmhoque53353462014-04-22 08:43:45 -050058
akmhoqueb6450b12014-04-24 00:01:03 -050059 NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050060 findNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050061
62 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050063 installNameLsa(NameLsa& nlsa);
akmhoque53353462014-04-22 08:43:45 -050064
65 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050066 removeNameLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050067
68 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050069 isNameLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050070
71 void
akmhoque2f423352014-06-03 11:49:35 -050072 writeNameLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050073
74 //function related to Cor LSDB
75 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050076 buildAndInstallOwnCoordinateLsa();
akmhoque53353462014-04-22 08:43:45 -050077
akmhoqueb6450b12014-04-24 00:01:03 -050078 CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050079 findCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050080
81 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050082 installCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -050083
84 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050085 removeCoordinateLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -050086
87 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050088 isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -050089
90 void
akmhoque2f423352014-06-03 11:49:35 -050091 writeCorLsdbLog();
akmhoque53353462014-04-22 08:43:45 -050092
93 //function related to Adj LSDB
94 void
akmhoque31d1d4b2014-05-05 22:08:14 -050095 scheduledAdjLsaBuild();
akmhoque53353462014-04-22 08:43:45 -050096
97 bool
akmhoque31d1d4b2014-05-05 22:08:14 -050098 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -050099
100 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 removeAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500102
103 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500105 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 installAdjLsa(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500107
akmhoqueb6450b12014-04-24 00:01:03 -0500108 AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 findAdjLsa(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500110
111 std::list<AdjLsa>&
112 getAdjLsdb();
113
114 void
akmhoque2f423352014-06-03 11:49:35 -0500115 writeAdjLsdbLog();
akmhoque53353462014-04-22 08:43:45 -0500116
akmhoque53353462014-04-22 08:43:45 -0500117 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700118 setLsaRefreshTime(const seconds& lsaRefreshTime);
akmhoque53353462014-04-22 08:43:45 -0500119
120 void
121 setThisRouterPrefix(std::string trp);
122
123private:
124 bool
125 addNameLsa(NameLsa& nlsa);
126
127 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 doesNameLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500129
130
131 bool
akmhoqueb6450b12014-04-24 00:01:03 -0500132 addCoordinateLsa(CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500133
134 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500135 doesCoordinateLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500136
137 bool
138 addAdjLsa(AdjLsa& alsa);
139
140 bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500141 doesAdjLsaExist(const ndn::Name& key);
akmhoque53353462014-04-22 08:43:45 -0500142
143 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500144 scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700145 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500146
147 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500148 exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500149
150 ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500151 scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700152 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500153
154 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500156
157 ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500158 scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700159 const seconds& expTime);
akmhoque53353462014-04-22 08:43:45 -0500160
161 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500162 exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500163 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500164public:
165 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700166 expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500167 steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
akmhoque53353462014-04-22 08:43:45 -0500168
akmhoque31d1d4b2014-05-05 22:08:14 -0500169 void
170 processInterest(const ndn::Name& name, const ndn::Interest& interest);
akmhoque53353462014-04-22 08:43:45 -0500171
172private:
173 void
akmhoque69c9aa92014-07-23 15:15:05 -0500174 putLsaData(const ndn::Interest& interest, const std::string& content);
175
176 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500177 processInterestForNameLsa(const ndn::Interest& interest,
178 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500179 uint64_t seqNo);
akmhoque53353462014-04-22 08:43:45 -0500180
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 void
182 processInterestForAdjacencyLsa(const ndn::Interest& interest,
183 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500184 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500185
186 void
187 processInterestForCoordinateLsa(const ndn::Interest& interest,
188 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500189 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500190
191 void
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500192 onContent(const ndn::Data& data, const steady_clock::TimePoint& deadline,
193 ndn::Name lsaName, uint64_t seqNo);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700194
195 /**
196 * @brief Retry validation after it fails
197 *
198 * Data packet validation can fail either because the packet does not have
199 * valid signature (fatal) or because some of the certificate chain Data packets
200 * failed to be fetched (non-fatal). Currently, the library does not provide
201 * clear indication (besides plain-text message in error callback) of what is
202 * the reason for failure and we will try to re-validate for as long as it the deadline.
203 */
204 void
205 retryContentValidation(const ndn::shared_ptr<const ndn::Data>& data,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500206 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
207 uint64_t seqNo);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700208
209 void
210 onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
211
212 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700213 onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500214 const steady_clock::TimePoint& deadline, ndn::Name lsaName,
215 uint64_t seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500216
217 void
218 processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500219 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500220
221 void
222 processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500223 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500224
225 void
226 processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500227 uint64_t lsSeqNo, std::string& dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -0500228
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500229PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500230 void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700231 processInterestTimedOut(const ndn::Interest& interest, uint32_t retransmitNo,
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
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500235private:
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700236 system_clock::TimePoint
akmhoquec7a79b22014-05-26 08:06:19 -0500237 getLsaExpirationTimePoint();
238
akmhoque31d1d4b2014-05-05 22:08:14 -0500239 void
240 cancelScheduleLsaExpiringEvent(ndn::EventId eid);
241
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500242private:
akmhoque31d1d4b2014-05-05 22:08:14 -0500243 Nlsr& m_nlsr;
Vince Lehman7c603292014-09-11 17:48:16 -0500244 ndn::Scheduler& m_scheduler;
245
akmhoque53353462014-04-22 08:43:45 -0500246 std::list<NameLsa> m_nameLsdb;
247 std::list<AdjLsa> m_adjLsdb;
akmhoqueb6450b12014-04-24 00:01:03 -0500248 std::list<CoordinateLsa> m_corLsdb;
akmhoque53353462014-04-22 08:43:45 -0500249
Vince Lehman904c2412014-09-23 19:36:11 -0500250 bool m_hasSyncPrefixBeenRegistered;
251
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700252 seconds m_lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500253 std::string m_thisRouterPrefix;
254
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500255 typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
256
257 // Maps the name of an LSA to its highest known sequence number from sync;
258 // Used to stop NLSR from trying to fetch outdated LSAs
259 SequenceNumberMap m_highestSeqNo;
260
Vince Lehman18841082014-08-19 17:15:24 -0500261 static const ndn::time::seconds GRACE_PERIOD;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500262 static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
akmhoque53353462014-04-22 08:43:45 -0500263};
264
265}//namespace nlsr
266
267#endif //NLSR_LSDB_HPP