blob: e33280828a32550c20322713d896969bdaa09b32 [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 **/
akmhoque298385a2014-02-13 14:13:09 -060023#ifndef NLSR_HPP
24#define NLSR_HPP
25
akmhoquefdbddb12014-05-02 18:35:19 -050026#include <boost/cstdint.hpp>
27#include <stdexcept>
28
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>
akmhoquec04e7272014-07-02 11:00:14 -050033#include <ndn-cxx/management/nfd-face-event-notification.hpp>
akmhoque060d3022014-08-12 13:35:06 -050034#include <ndn-cxx/management/nfd-face-monitor.hpp>
akmhoque298385a2014-02-13 14:13:09 -060035
Vince Lehman0a7da612014-10-29 14:39:29 -050036#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050037#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050038#include "adjacency-list.hpp"
39#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050040#include "lsdb.hpp"
41#include "sequencing-manager.hpp"
42#include "route/routing-table.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050043#include "route/name-prefix-table.hpp"
akmhoque53353462014-04-22 08:43:45 -050044#include "route/fib.hpp"
akmhoque53353462014-04-22 08:43:45 -050045#include "communication/sync-logic-handler.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050046#include "hello-protocol.hpp"
Vince Lehman7b616582014-10-17 16:25:39 -050047#include "test-access-control.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060048
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070049#include "validator.hpp"
50
akmhoque2bb198e2014-02-28 11:46:27 -060051
akmhoque53353462014-04-22 08:43:45 -050052namespace nlsr {
53
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070054static ndn::Name DEFAULT_BROADCAST_PREFIX("/ndn/broadcast");
55
akmhoque53353462014-04-22 08:43:45 -050056class Nlsr
57{
akmhoquefdbddb12014-05-02 18:35:19 -050058 class Error : public std::runtime_error
59 {
60 public:
61 explicit
62 Error(const std::string& what)
63 : std::runtime_error(what)
64 {
65 }
66 };
67
akmhoque53353462014-04-22 08:43:45 -050068public:
Vince Lehman904c2412014-09-23 19:36:11 -050069 Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face)
70 : m_nlsrFace(face)
Vince Lehman7c603292014-09-11 17:48:16 -050071 , m_scheduler(scheduler)
akmhoque53353462014-04-22 08:43:45 -050072 , m_confParam()
akmhoquec8a10f72014-04-25 18:42:55 -050073 , m_adjacencyList()
74 , m_namePrefixList()
akmhoquec8a10f72014-04-25 18:42:55 -050075 , m_sequencingManager()
akmhoque53353462014-04-22 08:43:45 -050076 , m_isDaemonProcess(false)
77 , m_configFileName("nlsr.conf")
Vince Lehman0bcf9a32014-12-10 11:24:45 -060078 , m_nlsrLsdb(*this, scheduler, m_syncLogicHandler)
akmhoque53353462014-04-22 08:43:45 -050079 , m_adjBuildCount(0)
80 , m_isBuildAdjLsaSheduled(false)
81 , m_isRouteCalculationScheduled(false)
82 , m_isRoutingTableCalculating(false)
Vince Lehman7c603292014-09-11 17:48:16 -050083 , m_routingTable(scheduler)
Vince Lehmanb7079a12014-11-04 12:45:50 -060084 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
akmhoque31d1d4b2014-05-05 22:08:14 -050085 , m_namePrefixTable(*this)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060086 , m_syncLogicHandler(m_nlsrFace, m_nlsrLsdb, m_confParam, m_sequencingManager)
Vince Lehman7c603292014-09-11 17:48:16 -050087 , m_helloProtocol(*this, scheduler)
88 , m_certificateCache(new ndn::CertificateCacheTtl(ioService))
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070089 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache)
akmhoque060d3022014-08-12 13:35:06 -050090 , m_faceMonitor(m_nlsrFace)
Vince Lehman7b616582014-10-17 16:25:39 -050091 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
akmhoque060d3022014-08-12 13:35:06 -050092 {
93 m_faceMonitor.onNotification += ndn::bind(&Nlsr::onFaceEventNotification, this, _1);
94 m_faceMonitor.start();
95 }
akmhoque298385a2014-02-13 14:13:09 -060096
akmhoque53353462014-04-22 08:43:45 -050097 void
98 registrationFailed(const ndn::Name& name);
99
100 void
akmhoque157b0a42014-05-13 00:26:37 -0500101 onRegistrationSuccess(const ndn::Name& name);
102
103 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 setInfoInterestFilter();
105
106 void
107 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -0500108
109 void
110 startEventLoop();
111
akmhoquefdbddb12014-05-02 18:35:19 -0500112 void
113 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500114
115 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500116 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700117 {
akmhoque53353462014-04-22 08:43:45 -0500118 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700119 }
120
akmhoque53353462014-04-22 08:43:45 -0500121 void
akmhoquefdbddb12014-05-02 18:35:19 -0500122 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500123 {
akmhoque53353462014-04-22 08:43:45 -0500124 m_configFileName = fileName;
125 }
akmhoque5a44dd42014-03-12 18:11:32 -0500126
akmhoque53353462014-04-22 08:43:45 -0500127 bool
128 getIsSetDaemonProcess()
129 {
130 return m_isDaemonProcess;
131 }
akmhoque5a44dd42014-03-12 18:11:32 -0500132
akmhoque53353462014-04-22 08:43:45 -0500133 void
134 setIsDaemonProcess(bool value)
135 {
136 m_isDaemonProcess = value;
137 }
akmhoque5a44dd42014-03-12 18:11:32 -0500138
akmhoque53353462014-04-22 08:43:45 -0500139 ConfParameter&
140 getConfParameter()
141 {
142 return m_confParam;
143 }
akmhoque5a44dd42014-03-12 18:11:32 -0500144
akmhoquec8a10f72014-04-25 18:42:55 -0500145 AdjacencyList&
146 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500147 {
akmhoquec8a10f72014-04-25 18:42:55 -0500148 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500149 }
akmhoque298385a2014-02-13 14:13:09 -0600150
akmhoquec8a10f72014-04-25 18:42:55 -0500151 NamePrefixList&
152 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500153 {
akmhoquec8a10f72014-04-25 18:42:55 -0500154 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500155 }
akmhoque298385a2014-02-13 14:13:09 -0600156
akmhoquefdbddb12014-05-02 18:35:19 -0500157 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500158 getNlsrFace()
159 {
160 return m_nlsrFace;
161 }
akmhoque298385a2014-02-13 14:13:09 -0600162
akmhoque53353462014-04-22 08:43:45 -0500163 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500164 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500165 {
akmhoquec8a10f72014-04-25 18:42:55 -0500166 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500167 }
akmhoque298385a2014-02-13 14:13:09 -0600168
akmhoque53353462014-04-22 08:43:45 -0500169 Lsdb&
170 getLsdb()
171 {
172 return m_nlsrLsdb;
173 }
akmhoque298385a2014-02-13 14:13:09 -0600174
akmhoque53353462014-04-22 08:43:45 -0500175 RoutingTable&
176 getRoutingTable()
177 {
178 return m_routingTable;
179 }
akmhoque298385a2014-02-13 14:13:09 -0600180
akmhoquec8a10f72014-04-25 18:42:55 -0500181 NamePrefixTable&
182 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500183 {
akmhoquec8a10f72014-04-25 18:42:55 -0500184 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500185 }
akmhoque298385a2014-02-13 14:13:09 -0600186
akmhoque53353462014-04-22 08:43:45 -0500187 Fib&
188 getFib()
189 {
190 return m_fib;
191 }
akmhoque298385a2014-02-13 14:13:09 -0600192
akmhoque53353462014-04-22 08:43:45 -0500193 long int
194 getAdjBuildCount()
195 {
196 return m_adjBuildCount;
197 }
akmhoque298385a2014-02-13 14:13:09 -0600198
akmhoque53353462014-04-22 08:43:45 -0500199 void
200 incrementAdjBuildCount()
201 {
202 m_adjBuildCount++;
203 }
akmhoque298385a2014-02-13 14:13:09 -0600204
akmhoque53353462014-04-22 08:43:45 -0500205 void
akmhoquefdbddb12014-05-02 18:35:19 -0500206 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500207 {
208 m_adjBuildCount = abc;
209 }
akmhoque298385a2014-02-13 14:13:09 -0600210
akmhoque157b0a42014-05-13 00:26:37 -0500211 bool
akmhoque53353462014-04-22 08:43:45 -0500212 getIsBuildAdjLsaSheduled()
213 {
214 return m_isBuildAdjLsaSheduled;
215 }
akmhoque298385a2014-02-13 14:13:09 -0600216
akmhoque53353462014-04-22 08:43:45 -0500217 void
218 setIsBuildAdjLsaSheduled(bool iabls)
219 {
220 m_isBuildAdjLsaSheduled = iabls;
221 }
akmhoque298385a2014-02-13 14:13:09 -0600222
akmhoque53353462014-04-22 08:43:45 -0500223 bool
224 getIsRoutingTableCalculating()
225 {
226 return m_isRoutingTableCalculating;
227 }
akmhoque85d88332014-02-17 21:11:21 -0600228
akmhoque53353462014-04-22 08:43:45 -0500229 void
230 setIsRoutingTableCalculating(bool irtc)
231 {
232 m_isRoutingTableCalculating = irtc;
233 }
akmhoque298385a2014-02-13 14:13:09 -0600234
akmhoque53353462014-04-22 08:43:45 -0500235 bool
236 getIsRouteCalculationScheduled()
237 {
238 return m_isRouteCalculationScheduled;
239 }
akmhoque298385a2014-02-13 14:13:09 -0600240
akmhoque53353462014-04-22 08:43:45 -0500241 void
242 setIsRouteCalculationScheduled(bool ircs)
243 {
244 m_isRouteCalculationScheduled = ircs;
245 }
akmhoque298385a2014-02-13 14:13:09 -0600246
akmhoque53353462014-04-22 08:43:45 -0500247 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500248 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500249 {
akmhoquec8a10f72014-04-25 18:42:55 -0500250 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500251 }
akmhoque2bb198e2014-02-28 11:46:27 -0600252
akmhoque53353462014-04-22 08:43:45 -0500253 void
254 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600255
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700256 void
akmhoque443ad812014-07-29 10:26:56 -0500257 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700258
259 void
260 loadValidator(boost::property_tree::ptree section,
261 const std::string& filename)
262 {
263 m_validator.load(section, filename);
264 }
265
266 Validator&
267 getValidator()
268 {
269 return m_validator;
270 }
271
272 void
273 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
274 {
275 if (static_cast<bool>(certificate))
276 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
277 // without version
278 }
279
280 ndn::shared_ptr<const ndn::IdentityCertificate>
281 getCertificate(const ndn::Name& certificateNameWithoutVersion)
282 {
283 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
284
285 if (it != m_certToPublish.end())
286 {
287 return it->second;
288 }
289
290 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
291 }
292
293 ndn::KeyChain&
294 getKeyChain()
295 {
296 return m_keyChain;
297 }
298
299 const ndn::Name&
300 getDefaultCertName()
301 {
302 return m_defaultCertName;
303 }
304
akmhoquee1765152014-06-30 11:32:01 -0500305 void
306 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500307 const CommandSucceedCallback& onSuccess,
308 const CommandFailCallback& onFailure);
309
310 void
311 destroyFaces();
312
akmhoque157b0a42014-05-13 00:26:37 -0500313 void
akmhoquec04e7272014-07-02 11:00:14 -0500314 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500315
akmhoque0494c252014-07-23 23:46:44 -0500316 void
317 daemonize();
318
Vince Lehman7b616582014-10-17 16:25:39 -0500319 uint32_t
320 getFirstHelloInterval() const
321 {
322 return m_firstHelloInterval;
323 }
324
akmhoque393d4ff2014-07-16 14:27:03 -0500325private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700326 void
327 registerKeyPrefix();
328
329 void
330 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
331
332 void
333 onKeyPrefixRegSuccess(const ndn::Name& name);
334
akmhoquee1765152014-06-30 11:32:01 -0500335 void
akmhoquee1765152014-06-30 11:32:01 -0500336 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
337
338 void
339 onDestroyFaceFailure(int32_t code, const std::string& error);
340
341 void
akmhoquec04e7272014-07-02 11:00:14 -0500342 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500343
Vince Lehman7b616582014-10-17 16:25:39 -0500344 void
345 setFirstHelloInterval(uint32_t interval)
346 {
347 m_firstHelloInterval = interval;
348 }
349
akmhoque157b0a42014-05-13 00:26:37 -0500350private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700351 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
352
Vince Lehman904c2412014-09-23 19:36:11 -0500353 ndn::Face& m_nlsrFace;
Vince Lehman7c603292014-09-11 17:48:16 -0500354 ndn::Scheduler& m_scheduler;
akmhoque53353462014-04-22 08:43:45 -0500355 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500356 AdjacencyList m_adjacencyList;
357 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500358 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500359 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500360 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500361 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500362 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500363 bool m_isBuildAdjLsaSheduled;
364 bool m_isRouteCalculationScheduled;
365 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500366 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500367 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500368 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500369 SyncLogicHandler m_syncLogicHandler;
Vince Lehman7b616582014-10-17 16:25:39 -0500370
371PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque31d1d4b2014-05-05 22:08:14 -0500372 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700373
Vince Lehman7b616582014-10-17 16:25:39 -0500374private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700375 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
376 CertMap m_certToPublish;
377 Validator m_validator;
378 ndn::KeyChain m_keyChain;
379 ndn::Name m_defaultIdentity;
380 ndn::Name m_defaultCertName;
akmhoquee1765152014-06-30 11:32:01 -0500381
akmhoque060d3022014-08-12 13:35:06 -0500382 ndn::nfd::FaceMonitor m_faceMonitor;
Vince Lehman7b616582014-10-17 16:25:39 -0500383
384 uint32_t m_firstHelloInterval;
akmhoque53353462014-04-22 08:43:45 -0500385};
akmhoque298385a2014-02-13 14:13:09 -0600386
akmhoqueb1710aa2014-02-19 17:13:36 -0600387} //namespace nlsr
388
akmhoque53353462014-04-22 08:43:45 -0500389#endif //NLSR_HPP