blob: 574e53a02e6a1c43cf9b8bafccfe9ba6f13214db [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonc6a85222017-01-03 16:54:34 -06003 * Copyright (c) 2014-2017, 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 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
dmcoomes9f936662017-03-02 10:33:09 -060022#ifndef NLSR_NLSR_HPP
23#define NLSR_NLSR_HPP
akmhoque298385a2014-02-13 14:13:09 -060024
akmhoquefdbddb12014-05-02 18:35:19 -050025#include <boost/cstdint.hpp>
26#include <stdexcept>
dmcoomes9f936662017-03-02 10:33:09 -060027#include <boost/throw_exception.hpp>
akmhoquefdbddb12014-05-02 18:35:19 -050028
akmhoquec8a10f72014-04-25 18:42:55 -050029#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/security/key-chain.hpp>
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070031#include <ndn-cxx/security/certificate-cache-ttl.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050032#include <ndn-cxx/util/scheduler.hpp>
Junxiao Shi3e5120c2016-09-10 16:58:34 +000033#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
34#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
Nick Gordond1f4f332017-01-20 13:40:57 -060035#include <ndn-cxx/mgmt/dispatcher.hpp>
akmhoque298385a2014-02-13 14:13:09 -060036
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050037#include "adjacency-list.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050038#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050039#include "conf-parameter.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050040#include "hello-protocol.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050041#include "lsdb.hpp"
42#include "name-prefix-list.hpp"
43#include "sequencing-manager.hpp"
Vince Lehman7b616582014-10-17 16:25:39 -050044#include "test-access-control.hpp"
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070045#include "validator.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050046#include "communication/sync-logic-handler.hpp"
47#include "publisher/lsdb-dataset-interest-handler.hpp"
48#include "route/fib.hpp"
49#include "route/name-prefix-table.hpp"
50#include "route/routing-table.hpp"
51#include "security/certificate-store.hpp"
52#include "update/prefix-update-processor.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060053#include "update/nfd-rib-command-processor.hpp"
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050054#include "utility/name-helper.hpp"
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070055
akmhoque53353462014-04-22 08:43:45 -050056namespace nlsr {
57
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070058static ndn::Name DEFAULT_BROADCAST_PREFIX("/ndn/broadcast");
59
akmhoque53353462014-04-22 08:43:45 -050060class Nlsr
61{
Nick Gordon9461afb2017-04-25 15:54:50 -050062 friend class NlsrRunner;
63
akmhoquefdbddb12014-05-02 18:35:19 -050064 class Error : public std::runtime_error
65 {
66 public:
67 explicit
68 Error(const std::string& what)
69 : std::runtime_error(what)
70 {
71 }
72 };
73
akmhoque53353462014-04-22 08:43:45 -050074public:
Laqin Fana4cf4022017-01-03 18:57:35 +000075 Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain);
akmhoque298385a2014-02-13 14:13:09 -060076
akmhoque53353462014-04-22 08:43:45 -050077 void
78 registrationFailed(const ndn::Name& name);
79
80 void
akmhoque157b0a42014-05-13 00:26:37 -050081 onRegistrationSuccess(const ndn::Name& name);
82
83 void
alvy297f4162015-03-03 17:15:33 -060084 onLocalhostRegistrationSuccess(const ndn::Name& name);
85
86 void
akmhoque31d1d4b2014-05-05 22:08:14 -050087 setInfoInterestFilter();
88
89 void
90 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -050091
92 void
93 startEventLoop();
94
akmhoque53353462014-04-22 08:43:45 -050095 std::string
akmhoquefdbddb12014-05-02 18:35:19 -050096 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -070097 {
akmhoque53353462014-04-22 08:43:45 -050098 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -070099 }
100
akmhoque53353462014-04-22 08:43:45 -0500101 void
akmhoquefdbddb12014-05-02 18:35:19 -0500102 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500103 {
akmhoque53353462014-04-22 08:43:45 -0500104 m_configFileName = fileName;
105 }
akmhoque5a44dd42014-03-12 18:11:32 -0500106
akmhoque53353462014-04-22 08:43:45 -0500107 bool
108 getIsSetDaemonProcess()
109 {
110 return m_isDaemonProcess;
111 }
akmhoque5a44dd42014-03-12 18:11:32 -0500112
akmhoque53353462014-04-22 08:43:45 -0500113 void
114 setIsDaemonProcess(bool value)
115 {
116 m_isDaemonProcess = value;
117 }
akmhoque5a44dd42014-03-12 18:11:32 -0500118
akmhoque53353462014-04-22 08:43:45 -0500119 ConfParameter&
120 getConfParameter()
121 {
122 return m_confParam;
123 }
akmhoque5a44dd42014-03-12 18:11:32 -0500124
akmhoquec8a10f72014-04-25 18:42:55 -0500125 AdjacencyList&
126 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500127 {
akmhoquec8a10f72014-04-25 18:42:55 -0500128 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500129 }
akmhoque298385a2014-02-13 14:13:09 -0600130
akmhoquec8a10f72014-04-25 18:42:55 -0500131 NamePrefixList&
132 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500133 {
akmhoquec8a10f72014-04-25 18:42:55 -0500134 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500135 }
akmhoque298385a2014-02-13 14:13:09 -0600136
akmhoquefdbddb12014-05-02 18:35:19 -0500137 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500138 getNlsrFace()
139 {
140 return m_nlsrFace;
141 }
akmhoque298385a2014-02-13 14:13:09 -0600142
akmhoque53353462014-04-22 08:43:45 -0500143 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500144 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500145 {
akmhoquec8a10f72014-04-25 18:42:55 -0500146 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500147 }
akmhoque298385a2014-02-13 14:13:09 -0600148
akmhoque53353462014-04-22 08:43:45 -0500149 Lsdb&
150 getLsdb()
151 {
152 return m_nlsrLsdb;
153 }
akmhoque298385a2014-02-13 14:13:09 -0600154
akmhoque53353462014-04-22 08:43:45 -0500155 RoutingTable&
156 getRoutingTable()
157 {
158 return m_routingTable;
159 }
akmhoque298385a2014-02-13 14:13:09 -0600160
akmhoquec8a10f72014-04-25 18:42:55 -0500161 NamePrefixTable&
162 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500163 {
akmhoquec8a10f72014-04-25 18:42:55 -0500164 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500165 }
akmhoque298385a2014-02-13 14:13:09 -0600166
akmhoque53353462014-04-22 08:43:45 -0500167 Fib&
168 getFib()
169 {
170 return m_fib;
171 }
akmhoque298385a2014-02-13 14:13:09 -0600172
akmhoque53353462014-04-22 08:43:45 -0500173 long int
174 getAdjBuildCount()
175 {
176 return m_adjBuildCount;
177 }
akmhoque298385a2014-02-13 14:13:09 -0600178
akmhoque53353462014-04-22 08:43:45 -0500179 void
180 incrementAdjBuildCount()
181 {
182 m_adjBuildCount++;
183 }
akmhoque298385a2014-02-13 14:13:09 -0600184
akmhoque53353462014-04-22 08:43:45 -0500185 void
akmhoquefdbddb12014-05-02 18:35:19 -0500186 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500187 {
188 m_adjBuildCount = abc;
189 }
akmhoque298385a2014-02-13 14:13:09 -0600190
akmhoque157b0a42014-05-13 00:26:37 -0500191 bool
akmhoque53353462014-04-22 08:43:45 -0500192 getIsBuildAdjLsaSheduled()
193 {
194 return m_isBuildAdjLsaSheduled;
195 }
akmhoque298385a2014-02-13 14:13:09 -0600196
akmhoque53353462014-04-22 08:43:45 -0500197 void
198 setIsBuildAdjLsaSheduled(bool iabls)
199 {
200 m_isBuildAdjLsaSheduled = iabls;
201 }
akmhoque298385a2014-02-13 14:13:09 -0600202
akmhoque53353462014-04-22 08:43:45 -0500203 bool
204 getIsRoutingTableCalculating()
205 {
206 return m_isRoutingTableCalculating;
207 }
akmhoque85d88332014-02-17 21:11:21 -0600208
akmhoque53353462014-04-22 08:43:45 -0500209 void
210 setIsRoutingTableCalculating(bool irtc)
211 {
212 m_isRoutingTableCalculating = irtc;
213 }
akmhoque298385a2014-02-13 14:13:09 -0600214
akmhoque53353462014-04-22 08:43:45 -0500215 bool
216 getIsRouteCalculationScheduled()
217 {
218 return m_isRouteCalculationScheduled;
219 }
akmhoque298385a2014-02-13 14:13:09 -0600220
akmhoque53353462014-04-22 08:43:45 -0500221 void
222 setIsRouteCalculationScheduled(bool ircs)
223 {
224 m_isRouteCalculationScheduled = ircs;
225 }
akmhoque298385a2014-02-13 14:13:09 -0600226
akmhoque53353462014-04-22 08:43:45 -0500227 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500228 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500229 {
akmhoquec8a10f72014-04-25 18:42:55 -0500230 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500231 }
akmhoque2bb198e2014-02-28 11:46:27 -0600232
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500233 LsdbDatasetInterestHandler&
234 getLsdbDatasetHandler()
235 {
236 return m_lsdbDatasetHandler;
237 }
238
akmhoque53353462014-04-22 08:43:45 -0500239 void
240 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600241
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700242 void
akmhoque443ad812014-07-29 10:26:56 -0500243 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700244
245 void
246 loadValidator(boost::property_tree::ptree section,
247 const std::string& filename)
248 {
249 m_validator.load(section, filename);
250 }
251
252 Validator&
253 getValidator()
254 {
255 return m_validator;
256 }
257
258 void
dmcoomes9f936662017-03-02 10:33:09 -0600259 loadCertToPublish(std::shared_ptr<ndn::IdentityCertificate> certificate)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700260 {
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500261 m_certStore.insert(certificate);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700262 }
263
dmcoomes9f936662017-03-02 10:33:09 -0600264 std::shared_ptr<const ndn::IdentityCertificate>
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700265 getCertificate(const ndn::Name& certificateNameWithoutVersion)
266 {
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500267 shared_ptr<const ndn::IdentityCertificate> cert =
268 m_certStore.find(certificateNameWithoutVersion);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700269
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500270 if (cert != nullptr) {
271 return cert;
272 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700273
274 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
275 }
276
277 ndn::KeyChain&
278 getKeyChain()
279 {
280 return m_keyChain;
281 }
282
283 const ndn::Name&
284 getDefaultCertName()
285 {
286 return m_defaultCertName;
287 }
288
alvy297f4162015-03-03 17:15:33 -0600289 update::PrefixUpdateProcessor&
290 getPrefixUpdateProcessor()
291 {
292 return m_prefixUpdateProcessor;
293 }
294
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600295 update::NfdRibCommandProcessor&
296 getNfdRibCommandProcessor()
297 {
298 return m_nfdRibCommandProcessor;
299 }
300
Nick Gordond1f4f332017-01-20 13:40:57 -0600301 ndn::mgmt::Dispatcher&
302 getDispatcher()
303 {
304 return m_dispatcher;
305 }
306
akmhoquee1765152014-06-30 11:32:01 -0500307 void
308 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500309 const CommandSucceedCallback& onSuccess,
310 const CommandFailCallback& onFailure);
311
312 void
313 destroyFaces();
314
akmhoque157b0a42014-05-13 00:26:37 -0500315 void
akmhoquec04e7272014-07-02 11:00:14 -0500316 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500317
akmhoque0494c252014-07-23 23:46:44 -0500318 void
319 daemonize();
320
Vince Lehman7b616582014-10-17 16:25:39 -0500321 uint32_t
322 getFirstHelloInterval() const
323 {
324 return m_firstHelloInterval;
325 }
326
Nick Gordon9461afb2017-04-25 15:54:50 -0500327 /**
328 * \brief Canonize the URI for this and all proceeding neighbors in a list.
329 *
330 * This function canonizes the URI of the Adjacent object pointed to
331 * by currentNeighbor. It then executes the then callback, providing
332 * the next iterator in the list to the callback. A standard
333 * invocation would be to pass the begin() iterator of NLSR's
334 * adjacency list, and to provide Nlsr::canonizeContinuation as the
335 * callback. Because every URI must be canonical before we begin
336 * operations, the canonize function must call initialize itself.
337 *
338 * \sa Nlsr::canonizeContinuation
339 * \sa Nlsr::initialize
340 * \sa NlsrRunner::run
341 */
342 void
343 canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
344 std::function<void(std::list<Adjacent>::iterator)> then);
345
346
alvy297f4162015-03-03 17:15:33 -0600347PUBLIC_WITH_TESTS_ELSE_PRIVATE:
348 void
dmcoomes9f936662017-03-02 10:33:09 -0600349 addCertificateToCache(std::shared_ptr<ndn::IdentityCertificate> certificate)
alvy297f4162015-03-03 17:15:33 -0600350 {
351 if (certificate != nullptr) {
352 m_certificateCache->insertCertificate(certificate);
353 }
354 }
355
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500356 security::CertificateStore&
357 getCertificateStore()
358 {
359 return m_certStore;
360 }
361
akmhoque393d4ff2014-07-16 14:27:03 -0500362private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700363 void
364 registerKeyPrefix();
365
366 void
alvy297f4162015-03-03 17:15:33 -0600367 registerLocalhostPrefix();
368
369 void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700370 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
371
372 void
373 onKeyPrefixRegSuccess(const ndn::Name& name);
374
akmhoquee1765152014-06-30 11:32:01 -0500375 void
akmhoquee1765152014-06-30 11:32:01 -0500376 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
377
378 void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000379 onDestroyFaceFailure(const ndn::nfd::ControlResponse& response);
akmhoquee1765152014-06-30 11:32:01 -0500380
381 void
akmhoquec04e7272014-07-02 11:00:14 -0500382 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500383
Vince Lehman7b616582014-10-17 16:25:39 -0500384 void
385 setFirstHelloInterval(uint32_t interval)
386 {
387 m_firstHelloInterval = interval;
388 }
389
Nick Gordon9461afb2017-04-25 15:54:50 -0500390 /**
391 * \brief Continues canonizing neighbor URIs.
392 *
393 * For testability reasons, we want what each instance of
394 * canonization does after completion to be controllable. The best
395 * way to do this is to control that by simply passing a
396 * continuation function.
397 */
398 void
399 canonizeContinuation(std::list<Adjacent>::iterator iterator);
400
alvy297f4162015-03-03 17:15:33 -0600401public:
402 static const ndn::Name LOCALHOST_PREFIX;
403
akmhoque157b0a42014-05-13 00:26:37 -0500404private:
Vince Lehman904c2412014-09-23 19:36:11 -0500405 ndn::Face& m_nlsrFace;
Vince Lehman7c603292014-09-11 17:48:16 -0500406 ndn::Scheduler& m_scheduler;
Laqin Fana4cf4022017-01-03 18:57:35 +0000407 ndn::KeyChain& m_keyChain;
akmhoque53353462014-04-22 08:43:45 -0500408 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500409 AdjacencyList m_adjacencyList;
410 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500411 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500412 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500413 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500414 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500415 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500416 bool m_isBuildAdjLsaSheduled;
417 bool m_isRouteCalculationScheduled;
418 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500419 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500420 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500421 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500422 SyncLogicHandler m_syncLogicHandler;
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500423 LsdbDatasetInterestHandler m_lsdbDatasetHandler;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700424
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500425PUBLIC_WITH_TESTS_ELSE_PRIVATE:
426 HelloProtocol m_helloProtocol;
427
Vince Lehman7b616582014-10-17 16:25:39 -0500428private:
dmcoomes9f936662017-03-02 10:33:09 -0600429 std::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500430 security::CertificateStore m_certStore;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700431 Validator m_validator;
Joao Pereira97473d42015-07-03 16:57:27 -0400432 ndn::security::SigningInfo m_signingInfo;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700433 ndn::Name m_defaultCertName;
alvy297f4162015-03-03 17:15:33 -0600434 update::PrefixUpdateProcessor m_prefixUpdateProcessor;
Nick Gordond1f4f332017-01-20 13:40:57 -0600435 ndn::mgmt::Dispatcher m_dispatcher;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600436 update::NfdRibCommandProcessor m_nfdRibCommandProcessor;
akmhoquee1765152014-06-30 11:32:01 -0500437
akmhoque060d3022014-08-12 13:35:06 -0500438 ndn::nfd::FaceMonitor m_faceMonitor;
Vince Lehman7b616582014-10-17 16:25:39 -0500439
440 uint32_t m_firstHelloInterval;
akmhoque53353462014-04-22 08:43:45 -0500441};
akmhoque298385a2014-02-13 14:13:09 -0600442
Nick Gordonfad8e252016-08-11 14:21:38 -0500443} // namespace nlsr
akmhoqueb1710aa2014-02-19 17:13:36 -0600444
dmcoomes9f936662017-03-02 10:33:09 -0600445#endif // NLSR_NLSR_HPP