blob: 8893272582cddb5c3de42c353a4ed9b5db4cfb9b [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 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque298385a2014-02-13 14:13:09 -060022#ifndef NLSR_HPP
23#define NLSR_HPP
24
akmhoquefdbddb12014-05-02 18:35:19 -050025#include <boost/cstdint.hpp>
26#include <stdexcept>
27
akmhoquec8a10f72014-04-25 18:42:55 -050028#include <ndn-cxx/face.hpp>
29#include <ndn-cxx/security/key-chain.hpp>
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070030#include <ndn-cxx/security/certificate-cache-ttl.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050031#include <ndn-cxx/util/scheduler.hpp>
akmhoquec04e7272014-07-02 11:00:14 -050032#include <ndn-cxx/management/nfd-face-event-notification.hpp>
akmhoque060d3022014-08-12 13:35:06 -050033#include <ndn-cxx/management/nfd-face-monitor.hpp>
akmhoque298385a2014-02-13 14:13:09 -060034
Vince Lehman0a7da612014-10-29 14:39:29 -050035#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050036#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050037#include "adjacency-list.hpp"
38#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050039#include "lsdb.hpp"
40#include "sequencing-manager.hpp"
41#include "route/routing-table.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050042#include "route/name-prefix-table.hpp"
akmhoque53353462014-04-22 08:43:45 -050043#include "route/fib.hpp"
akmhoque53353462014-04-22 08:43:45 -050044#include "communication/sync-logic-handler.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050045#include "hello-protocol.hpp"
Vince Lehman7b616582014-10-17 16:25:39 -050046#include "test-access-control.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080047#include "publisher/lsdb-dataset-interest-handler.hpp"
alvy297f4162015-03-03 17:15:33 -060048#include "utility/name-helper.hpp"
49#include "update/prefix-update-processor.hpp"
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070050#include "validator.hpp"
51
akmhoque2bb198e2014-02-28 11:46:27 -060052
akmhoque53353462014-04-22 08:43:45 -050053namespace nlsr {
54
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070055static ndn::Name DEFAULT_BROADCAST_PREFIX("/ndn/broadcast");
56
akmhoque53353462014-04-22 08:43:45 -050057class Nlsr
58{
akmhoquefdbddb12014-05-02 18:35:19 -050059 class Error : public std::runtime_error
60 {
61 public:
62 explicit
63 Error(const std::string& what)
64 : std::runtime_error(what)
65 {
66 }
67 };
68
akmhoque53353462014-04-22 08:43:45 -050069public:
Vince Lehman904c2412014-09-23 19:36:11 -050070 Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face)
71 : m_nlsrFace(face)
Vince Lehman7c603292014-09-11 17:48:16 -050072 , m_scheduler(scheduler)
akmhoque53353462014-04-22 08:43:45 -050073 , m_confParam()
akmhoquec8a10f72014-04-25 18:42:55 -050074 , m_adjacencyList()
75 , m_namePrefixList()
akmhoquec8a10f72014-04-25 18:42:55 -050076 , m_sequencingManager()
akmhoque53353462014-04-22 08:43:45 -050077 , m_isDaemonProcess(false)
78 , m_configFileName("nlsr.conf")
Vince Lehman0bcf9a32014-12-10 11:24:45 -060079 , m_nlsrLsdb(*this, scheduler, m_syncLogicHandler)
akmhoque53353462014-04-22 08:43:45 -050080 , m_adjBuildCount(0)
81 , m_isBuildAdjLsaSheduled(false)
82 , m_isRouteCalculationScheduled(false)
83 , m_isRoutingTableCalculating(false)
Vince Lehman7c603292014-09-11 17:48:16 -050084 , m_routingTable(scheduler)
Vince Lehmanb7079a12014-11-04 12:45:50 -060085 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
akmhoque31d1d4b2014-05-05 22:08:14 -050086 , m_namePrefixTable(*this)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060087 , m_syncLogicHandler(m_nlsrFace, m_nlsrLsdb, m_confParam, m_sequencingManager)
Vince Lehman7c603292014-09-11 17:48:16 -050088 , m_helloProtocol(*this, scheduler)
89 , m_certificateCache(new ndn::CertificateCacheTtl(ioService))
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070090 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache)
alvy297f4162015-03-03 17:15:33 -060091 , m_prefixUpdateProcessor(m_nlsrFace,
92 m_namePrefixList,
93 m_nlsrLsdb,
94 m_syncLogicHandler,
95 DEFAULT_BROADCAST_PREFIX,
96 m_keyChain,
97 m_certificateCache)
akmhoque060d3022014-08-12 13:35:06 -050098 , m_faceMonitor(m_nlsrFace)
Vince Lehman7b616582014-10-17 16:25:39 -050099 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
akmhoque060d3022014-08-12 13:35:06 -0500100 {
Vince Lehmanf8142052015-01-21 11:11:36 -0600101 m_faceMonitor.onNotification.connect(bind(&Nlsr::onFaceEventNotification, this, _1));
akmhoque060d3022014-08-12 13:35:06 -0500102 m_faceMonitor.start();
103 }
akmhoque298385a2014-02-13 14:13:09 -0600104
akmhoque53353462014-04-22 08:43:45 -0500105 void
106 registrationFailed(const ndn::Name& name);
107
108 void
akmhoque157b0a42014-05-13 00:26:37 -0500109 onRegistrationSuccess(const ndn::Name& name);
110
111 void
alvy297f4162015-03-03 17:15:33 -0600112 onLocalhostRegistrationSuccess(const ndn::Name& name);
113
114 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500115 setInfoInterestFilter();
116
117 void
118 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -0500119
120 void
121 startEventLoop();
122
akmhoquefdbddb12014-05-02 18:35:19 -0500123 void
124 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500125
126 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500127 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700128 {
akmhoque53353462014-04-22 08:43:45 -0500129 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700130 }
131
akmhoque53353462014-04-22 08:43:45 -0500132 void
akmhoquefdbddb12014-05-02 18:35:19 -0500133 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500134 {
akmhoque53353462014-04-22 08:43:45 -0500135 m_configFileName = fileName;
136 }
akmhoque5a44dd42014-03-12 18:11:32 -0500137
akmhoque53353462014-04-22 08:43:45 -0500138 bool
139 getIsSetDaemonProcess()
140 {
141 return m_isDaemonProcess;
142 }
akmhoque5a44dd42014-03-12 18:11:32 -0500143
akmhoque53353462014-04-22 08:43:45 -0500144 void
145 setIsDaemonProcess(bool value)
146 {
147 m_isDaemonProcess = value;
148 }
akmhoque5a44dd42014-03-12 18:11:32 -0500149
akmhoque53353462014-04-22 08:43:45 -0500150 ConfParameter&
151 getConfParameter()
152 {
153 return m_confParam;
154 }
akmhoque5a44dd42014-03-12 18:11:32 -0500155
akmhoquec8a10f72014-04-25 18:42:55 -0500156 AdjacencyList&
157 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500158 {
akmhoquec8a10f72014-04-25 18:42:55 -0500159 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500160 }
akmhoque298385a2014-02-13 14:13:09 -0600161
akmhoquec8a10f72014-04-25 18:42:55 -0500162 NamePrefixList&
163 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500164 {
akmhoquec8a10f72014-04-25 18:42:55 -0500165 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500166 }
akmhoque298385a2014-02-13 14:13:09 -0600167
akmhoquefdbddb12014-05-02 18:35:19 -0500168 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500169 getNlsrFace()
170 {
171 return m_nlsrFace;
172 }
akmhoque298385a2014-02-13 14:13:09 -0600173
akmhoque53353462014-04-22 08:43:45 -0500174 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500175 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500176 {
akmhoquec8a10f72014-04-25 18:42:55 -0500177 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500178 }
akmhoque298385a2014-02-13 14:13:09 -0600179
akmhoque53353462014-04-22 08:43:45 -0500180 Lsdb&
181 getLsdb()
182 {
183 return m_nlsrLsdb;
184 }
akmhoque298385a2014-02-13 14:13:09 -0600185
akmhoque53353462014-04-22 08:43:45 -0500186 RoutingTable&
187 getRoutingTable()
188 {
189 return m_routingTable;
190 }
akmhoque298385a2014-02-13 14:13:09 -0600191
akmhoquec8a10f72014-04-25 18:42:55 -0500192 NamePrefixTable&
193 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500194 {
akmhoquec8a10f72014-04-25 18:42:55 -0500195 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500196 }
akmhoque298385a2014-02-13 14:13:09 -0600197
akmhoque53353462014-04-22 08:43:45 -0500198 Fib&
199 getFib()
200 {
201 return m_fib;
202 }
akmhoque298385a2014-02-13 14:13:09 -0600203
akmhoque53353462014-04-22 08:43:45 -0500204 long int
205 getAdjBuildCount()
206 {
207 return m_adjBuildCount;
208 }
akmhoque298385a2014-02-13 14:13:09 -0600209
akmhoque53353462014-04-22 08:43:45 -0500210 void
211 incrementAdjBuildCount()
212 {
213 m_adjBuildCount++;
214 }
akmhoque298385a2014-02-13 14:13:09 -0600215
akmhoque53353462014-04-22 08:43:45 -0500216 void
akmhoquefdbddb12014-05-02 18:35:19 -0500217 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500218 {
219 m_adjBuildCount = abc;
220 }
akmhoque298385a2014-02-13 14:13:09 -0600221
akmhoque157b0a42014-05-13 00:26:37 -0500222 bool
akmhoque53353462014-04-22 08:43:45 -0500223 getIsBuildAdjLsaSheduled()
224 {
225 return m_isBuildAdjLsaSheduled;
226 }
akmhoque298385a2014-02-13 14:13:09 -0600227
akmhoque53353462014-04-22 08:43:45 -0500228 void
229 setIsBuildAdjLsaSheduled(bool iabls)
230 {
231 m_isBuildAdjLsaSheduled = iabls;
232 }
akmhoque298385a2014-02-13 14:13:09 -0600233
akmhoque53353462014-04-22 08:43:45 -0500234 bool
235 getIsRoutingTableCalculating()
236 {
237 return m_isRoutingTableCalculating;
238 }
akmhoque85d88332014-02-17 21:11:21 -0600239
akmhoque53353462014-04-22 08:43:45 -0500240 void
241 setIsRoutingTableCalculating(bool irtc)
242 {
243 m_isRoutingTableCalculating = irtc;
244 }
akmhoque298385a2014-02-13 14:13:09 -0600245
akmhoque53353462014-04-22 08:43:45 -0500246 bool
247 getIsRouteCalculationScheduled()
248 {
249 return m_isRouteCalculationScheduled;
250 }
akmhoque298385a2014-02-13 14:13:09 -0600251
akmhoque53353462014-04-22 08:43:45 -0500252 void
253 setIsRouteCalculationScheduled(bool ircs)
254 {
255 m_isRouteCalculationScheduled = ircs;
256 }
akmhoque298385a2014-02-13 14:13:09 -0600257
akmhoque53353462014-04-22 08:43:45 -0500258 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500259 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500260 {
akmhoquec8a10f72014-04-25 18:42:55 -0500261 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500262 }
akmhoque2bb198e2014-02-28 11:46:27 -0600263
akmhoque53353462014-04-22 08:43:45 -0500264 void
265 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600266
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700267 void
akmhoque443ad812014-07-29 10:26:56 -0500268 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700269
270 void
271 loadValidator(boost::property_tree::ptree section,
272 const std::string& filename)
273 {
274 m_validator.load(section, filename);
275 }
276
277 Validator&
278 getValidator()
279 {
280 return m_validator;
281 }
282
283 void
284 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
285 {
286 if (static_cast<bool>(certificate))
287 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
288 // without version
289 }
290
291 ndn::shared_ptr<const ndn::IdentityCertificate>
292 getCertificate(const ndn::Name& certificateNameWithoutVersion)
293 {
294 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
295
296 if (it != m_certToPublish.end())
297 {
298 return it->second;
299 }
300
301 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
302 }
303
304 ndn::KeyChain&
305 getKeyChain()
306 {
307 return m_keyChain;
308 }
309
310 const ndn::Name&
311 getDefaultCertName()
312 {
313 return m_defaultCertName;
314 }
315
alvy297f4162015-03-03 17:15:33 -0600316 update::PrefixUpdateProcessor&
317 getPrefixUpdateProcessor()
318 {
319 return m_prefixUpdateProcessor;
320 }
321
akmhoquee1765152014-06-30 11:32:01 -0500322 void
323 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500324 const CommandSucceedCallback& onSuccess,
325 const CommandFailCallback& onFailure);
326
327 void
328 destroyFaces();
329
akmhoque157b0a42014-05-13 00:26:37 -0500330 void
akmhoquec04e7272014-07-02 11:00:14 -0500331 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500332
akmhoque0494c252014-07-23 23:46:44 -0500333 void
334 daemonize();
335
Vince Lehman7b616582014-10-17 16:25:39 -0500336 uint32_t
337 getFirstHelloInterval() const
338 {
339 return m_firstHelloInterval;
340 }
341
alvy297f4162015-03-03 17:15:33 -0600342PUBLIC_WITH_TESTS_ELSE_PRIVATE:
343 void
344 addCertificateToCache(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
345 {
346 if (certificate != nullptr) {
347 m_certificateCache->insertCertificate(certificate);
348 }
349 }
350
akmhoque393d4ff2014-07-16 14:27:03 -0500351private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700352 void
353 registerKeyPrefix();
354
355 void
alvy297f4162015-03-03 17:15:33 -0600356 registerLocalhostPrefix();
357
358 void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700359 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
360
361 void
362 onKeyPrefixRegSuccess(const ndn::Name& name);
363
akmhoquee1765152014-06-30 11:32:01 -0500364 void
akmhoquee1765152014-06-30 11:32:01 -0500365 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
366
367 void
368 onDestroyFaceFailure(int32_t code, const std::string& error);
369
370 void
akmhoquec04e7272014-07-02 11:00:14 -0500371 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500372
Vince Lehman7b616582014-10-17 16:25:39 -0500373 void
374 setFirstHelloInterval(uint32_t interval)
375 {
376 m_firstHelloInterval = interval;
377 }
378
alvy297f4162015-03-03 17:15:33 -0600379public:
380 static const ndn::Name LOCALHOST_PREFIX;
381
akmhoque157b0a42014-05-13 00:26:37 -0500382private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700383 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
384
Vince Lehman904c2412014-09-23 19:36:11 -0500385 ndn::Face& m_nlsrFace;
Vince Lehman7c603292014-09-11 17:48:16 -0500386 ndn::Scheduler& m_scheduler;
akmhoque53353462014-04-22 08:43:45 -0500387 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500388 AdjacencyList m_adjacencyList;
389 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500390 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500391 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500392 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500393 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500394 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500395 bool m_isBuildAdjLsaSheduled;
396 bool m_isRouteCalculationScheduled;
397 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500398 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500399 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500400 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500401 SyncLogicHandler m_syncLogicHandler;
akmhoque31d1d4b2014-05-05 22:08:14 -0500402 HelloProtocol m_helloProtocol;
Jiewen Tana0497d82015-02-02 21:59:18 -0800403 std::unique_ptr<LsdbDatasetInterestHandler> m_lsdbDatasetHandler;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700404
Vince Lehman7b616582014-10-17 16:25:39 -0500405private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700406 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
407 CertMap m_certToPublish;
408 Validator m_validator;
409 ndn::KeyChain m_keyChain;
410 ndn::Name m_defaultIdentity;
411 ndn::Name m_defaultCertName;
alvy297f4162015-03-03 17:15:33 -0600412 update::PrefixUpdateProcessor m_prefixUpdateProcessor;
akmhoquee1765152014-06-30 11:32:01 -0500413
akmhoque060d3022014-08-12 13:35:06 -0500414 ndn::nfd::FaceMonitor m_faceMonitor;
Vince Lehman7b616582014-10-17 16:25:39 -0500415
416 uint32_t m_firstHelloInterval;
akmhoque53353462014-04-22 08:43:45 -0500417};
akmhoque298385a2014-02-13 14:13:09 -0600418
akmhoqueb1710aa2014-02-19 17:13:36 -0600419} //namespace nlsr
420
akmhoque53353462014-04-22 08:43:45 -0500421#endif //NLSR_HPP