blob: ee6f0323cb8219bf2a6622a46432cf975f36f6dd [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"
akmhoque2bb198e2014-02-28 11:46:27 -060047
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070048#include "validator.hpp"
49
akmhoque2bb198e2014-02-28 11:46:27 -060050
akmhoque53353462014-04-22 08:43:45 -050051namespace nlsr {
52
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070053static ndn::Name DEFAULT_BROADCAST_PREFIX("/ndn/broadcast");
54
akmhoque53353462014-04-22 08:43:45 -050055class Nlsr
56{
akmhoquefdbddb12014-05-02 18:35:19 -050057 class Error : public std::runtime_error
58 {
59 public:
60 explicit
61 Error(const std::string& what)
62 : std::runtime_error(what)
63 {
64 }
65 };
66
akmhoque53353462014-04-22 08:43:45 -050067public:
Vince Lehman904c2412014-09-23 19:36:11 -050068 Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face)
69 : m_nlsrFace(face)
Vince Lehman7c603292014-09-11 17:48:16 -050070 , m_scheduler(scheduler)
akmhoque53353462014-04-22 08:43:45 -050071 , m_confParam()
akmhoquec8a10f72014-04-25 18:42:55 -050072 , m_adjacencyList()
73 , m_namePrefixList()
akmhoquec8a10f72014-04-25 18:42:55 -050074 , m_sequencingManager()
akmhoque53353462014-04-22 08:43:45 -050075 , m_isDaemonProcess(false)
76 , m_configFileName("nlsr.conf")
Vince Lehman0bcf9a32014-12-10 11:24:45 -060077 , m_nlsrLsdb(*this, scheduler, m_syncLogicHandler)
akmhoque53353462014-04-22 08:43:45 -050078 , m_adjBuildCount(0)
79 , m_isBuildAdjLsaSheduled(false)
80 , m_isRouteCalculationScheduled(false)
81 , m_isRoutingTableCalculating(false)
Vince Lehman7c603292014-09-11 17:48:16 -050082 , m_routingTable(scheduler)
Vince Lehmanb7079a12014-11-04 12:45:50 -060083 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
akmhoque31d1d4b2014-05-05 22:08:14 -050084 , m_namePrefixTable(*this)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060085 , m_syncLogicHandler(m_nlsrFace, m_nlsrLsdb, m_confParam, m_sequencingManager)
Vince Lehman7c603292014-09-11 17:48:16 -050086 , m_helloProtocol(*this, scheduler)
87 , m_certificateCache(new ndn::CertificateCacheTtl(ioService))
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070088 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache)
akmhoque060d3022014-08-12 13:35:06 -050089 , m_faceMonitor(m_nlsrFace)
Vince Lehman7b616582014-10-17 16:25:39 -050090 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
akmhoque060d3022014-08-12 13:35:06 -050091 {
92 m_faceMonitor.onNotification += ndn::bind(&Nlsr::onFaceEventNotification, this, _1);
93 m_faceMonitor.start();
94 }
akmhoque298385a2014-02-13 14:13:09 -060095
akmhoque53353462014-04-22 08:43:45 -050096 void
97 registrationFailed(const ndn::Name& name);
98
99 void
akmhoque157b0a42014-05-13 00:26:37 -0500100 onRegistrationSuccess(const ndn::Name& name);
101
102 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 setInfoInterestFilter();
104
105 void
106 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -0500107
108 void
109 startEventLoop();
110
akmhoquefdbddb12014-05-02 18:35:19 -0500111 void
112 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500113
114 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500115 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700116 {
akmhoque53353462014-04-22 08:43:45 -0500117 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700118 }
119
akmhoque53353462014-04-22 08:43:45 -0500120 void
akmhoquefdbddb12014-05-02 18:35:19 -0500121 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500122 {
akmhoque53353462014-04-22 08:43:45 -0500123 m_configFileName = fileName;
124 }
akmhoque5a44dd42014-03-12 18:11:32 -0500125
akmhoque53353462014-04-22 08:43:45 -0500126 bool
127 getIsSetDaemonProcess()
128 {
129 return m_isDaemonProcess;
130 }
akmhoque5a44dd42014-03-12 18:11:32 -0500131
akmhoque53353462014-04-22 08:43:45 -0500132 void
133 setIsDaemonProcess(bool value)
134 {
135 m_isDaemonProcess = value;
136 }
akmhoque5a44dd42014-03-12 18:11:32 -0500137
akmhoque53353462014-04-22 08:43:45 -0500138 ConfParameter&
139 getConfParameter()
140 {
141 return m_confParam;
142 }
akmhoque5a44dd42014-03-12 18:11:32 -0500143
akmhoquec8a10f72014-04-25 18:42:55 -0500144 AdjacencyList&
145 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500146 {
akmhoquec8a10f72014-04-25 18:42:55 -0500147 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500148 }
akmhoque298385a2014-02-13 14:13:09 -0600149
akmhoquec8a10f72014-04-25 18:42:55 -0500150 NamePrefixList&
151 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500152 {
akmhoquec8a10f72014-04-25 18:42:55 -0500153 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500154 }
akmhoque298385a2014-02-13 14:13:09 -0600155
akmhoquefdbddb12014-05-02 18:35:19 -0500156 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500157 getNlsrFace()
158 {
159 return m_nlsrFace;
160 }
akmhoque298385a2014-02-13 14:13:09 -0600161
akmhoque53353462014-04-22 08:43:45 -0500162 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500163 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500164 {
akmhoquec8a10f72014-04-25 18:42:55 -0500165 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500166 }
akmhoque298385a2014-02-13 14:13:09 -0600167
akmhoque53353462014-04-22 08:43:45 -0500168 Lsdb&
169 getLsdb()
170 {
171 return m_nlsrLsdb;
172 }
akmhoque298385a2014-02-13 14:13:09 -0600173
akmhoque53353462014-04-22 08:43:45 -0500174 RoutingTable&
175 getRoutingTable()
176 {
177 return m_routingTable;
178 }
akmhoque298385a2014-02-13 14:13:09 -0600179
akmhoquec8a10f72014-04-25 18:42:55 -0500180 NamePrefixTable&
181 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500182 {
akmhoquec8a10f72014-04-25 18:42:55 -0500183 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500184 }
akmhoque298385a2014-02-13 14:13:09 -0600185
akmhoque53353462014-04-22 08:43:45 -0500186 Fib&
187 getFib()
188 {
189 return m_fib;
190 }
akmhoque298385a2014-02-13 14:13:09 -0600191
akmhoque53353462014-04-22 08:43:45 -0500192 long int
193 getAdjBuildCount()
194 {
195 return m_adjBuildCount;
196 }
akmhoque298385a2014-02-13 14:13:09 -0600197
akmhoque53353462014-04-22 08:43:45 -0500198 void
199 incrementAdjBuildCount()
200 {
201 m_adjBuildCount++;
202 }
akmhoque298385a2014-02-13 14:13:09 -0600203
akmhoque53353462014-04-22 08:43:45 -0500204 void
akmhoquefdbddb12014-05-02 18:35:19 -0500205 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500206 {
207 m_adjBuildCount = abc;
208 }
akmhoque298385a2014-02-13 14:13:09 -0600209
akmhoque157b0a42014-05-13 00:26:37 -0500210 bool
akmhoque53353462014-04-22 08:43:45 -0500211 getIsBuildAdjLsaSheduled()
212 {
213 return m_isBuildAdjLsaSheduled;
214 }
akmhoque298385a2014-02-13 14:13:09 -0600215
akmhoque53353462014-04-22 08:43:45 -0500216 void
217 setIsBuildAdjLsaSheduled(bool iabls)
218 {
219 m_isBuildAdjLsaSheduled = iabls;
220 }
akmhoque298385a2014-02-13 14:13:09 -0600221
akmhoque53353462014-04-22 08:43:45 -0500222 bool
223 getIsRoutingTableCalculating()
224 {
225 return m_isRoutingTableCalculating;
226 }
akmhoque85d88332014-02-17 21:11:21 -0600227
akmhoque53353462014-04-22 08:43:45 -0500228 void
229 setIsRoutingTableCalculating(bool irtc)
230 {
231 m_isRoutingTableCalculating = irtc;
232 }
akmhoque298385a2014-02-13 14:13:09 -0600233
akmhoque53353462014-04-22 08:43:45 -0500234 bool
235 getIsRouteCalculationScheduled()
236 {
237 return m_isRouteCalculationScheduled;
238 }
akmhoque298385a2014-02-13 14:13:09 -0600239
akmhoque53353462014-04-22 08:43:45 -0500240 void
241 setIsRouteCalculationScheduled(bool ircs)
242 {
243 m_isRouteCalculationScheduled = ircs;
244 }
akmhoque298385a2014-02-13 14:13:09 -0600245
akmhoque53353462014-04-22 08:43:45 -0500246 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500247 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500248 {
akmhoquec8a10f72014-04-25 18:42:55 -0500249 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500250 }
akmhoque2bb198e2014-02-28 11:46:27 -0600251
akmhoque53353462014-04-22 08:43:45 -0500252 void
253 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600254
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700255 void
akmhoque443ad812014-07-29 10:26:56 -0500256 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700257
258 void
259 loadValidator(boost::property_tree::ptree section,
260 const std::string& filename)
261 {
262 m_validator.load(section, filename);
263 }
264
265 Validator&
266 getValidator()
267 {
268 return m_validator;
269 }
270
271 void
272 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
273 {
274 if (static_cast<bool>(certificate))
275 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
276 // without version
277 }
278
279 ndn::shared_ptr<const ndn::IdentityCertificate>
280 getCertificate(const ndn::Name& certificateNameWithoutVersion)
281 {
282 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
283
284 if (it != m_certToPublish.end())
285 {
286 return it->second;
287 }
288
289 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
290 }
291
292 ndn::KeyChain&
293 getKeyChain()
294 {
295 return m_keyChain;
296 }
297
298 const ndn::Name&
299 getDefaultCertName()
300 {
301 return m_defaultCertName;
302 }
303
akmhoquee1765152014-06-30 11:32:01 -0500304 void
305 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500306 const CommandSucceedCallback& onSuccess,
307 const CommandFailCallback& onFailure);
308
309 void
310 destroyFaces();
311
akmhoque157b0a42014-05-13 00:26:37 -0500312 void
akmhoquec04e7272014-07-02 11:00:14 -0500313 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500314
akmhoque0494c252014-07-23 23:46:44 -0500315 void
316 daemonize();
317
Vince Lehman7b616582014-10-17 16:25:39 -0500318 uint32_t
319 getFirstHelloInterval() const
320 {
321 return m_firstHelloInterval;
322 }
323
akmhoque393d4ff2014-07-16 14:27:03 -0500324private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700325 void
326 registerKeyPrefix();
327
328 void
329 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
330
331 void
332 onKeyPrefixRegSuccess(const ndn::Name& name);
333
akmhoquee1765152014-06-30 11:32:01 -0500334 void
akmhoquee1765152014-06-30 11:32:01 -0500335 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
336
337 void
338 onDestroyFaceFailure(int32_t code, const std::string& error);
339
340 void
akmhoquec04e7272014-07-02 11:00:14 -0500341 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500342
Vince Lehman7b616582014-10-17 16:25:39 -0500343 void
344 setFirstHelloInterval(uint32_t interval)
345 {
346 m_firstHelloInterval = interval;
347 }
348
akmhoque157b0a42014-05-13 00:26:37 -0500349private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700350 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
351
Vince Lehman904c2412014-09-23 19:36:11 -0500352 ndn::Face& m_nlsrFace;
Vince Lehman7c603292014-09-11 17:48:16 -0500353 ndn::Scheduler& m_scheduler;
akmhoque53353462014-04-22 08:43:45 -0500354 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500355 AdjacencyList m_adjacencyList;
356 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500357 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500358 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500359 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500360 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500361 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500362 bool m_isBuildAdjLsaSheduled;
363 bool m_isRouteCalculationScheduled;
364 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500365 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500366 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500367 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500368 SyncLogicHandler m_syncLogicHandler;
Vince Lehman7b616582014-10-17 16:25:39 -0500369
370PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500371 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700372
Vince Lehman7b616582014-10-17 16:25:39 -0500373private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700374 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
375 CertMap m_certToPublish;
376 Validator m_validator;
377 ndn::KeyChain m_keyChain;
378 ndn::Name m_defaultIdentity;
379 ndn::Name m_defaultCertName;
akmhoquee1765152014-06-30 11:32:01 -0500380
akmhoque060d3022014-08-12 13:35:06 -0500381 ndn::nfd::FaceMonitor m_faceMonitor;
Vince Lehman7b616582014-10-17 16:25:39 -0500382
383 uint32_t m_firstHelloInterval;
akmhoque53353462014-04-22 08:43:45 -0500384};
akmhoque298385a2014-02-13 14:13:09 -0600385
akmhoqueb1710aa2014-02-19 17:13:36 -0600386} //namespace nlsr
387
akmhoque53353462014-04-22 08:43:45 -0500388#endif //NLSR_HPP