blob: dd3c99b769a1699131ccaf91dfe58c7c85d03c3a [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
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"
akmhoque2bb198e2014-02-28 11:46:27 -060046
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070047#include "validator.hpp"
48
akmhoque2bb198e2014-02-28 11:46:27 -060049
akmhoque53353462014-04-22 08:43:45 -050050namespace nlsr {
51
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070052static ndn::Name DEFAULT_BROADCAST_PREFIX("/ndn/broadcast");
53
akmhoque53353462014-04-22 08:43:45 -050054class Nlsr
55{
akmhoquefdbddb12014-05-02 18:35:19 -050056 class Error : public std::runtime_error
57 {
58 public:
59 explicit
60 Error(const std::string& what)
61 : std::runtime_error(what)
62 {
63 }
64 };
65
akmhoque53353462014-04-22 08:43:45 -050066public:
67 Nlsr()
akmhoquefdbddb12014-05-02 18:35:19 -050068 : m_scheduler(m_nlsrFace.getIoService())
akmhoque53353462014-04-22 08:43:45 -050069 , m_confParam()
akmhoquec8a10f72014-04-25 18:42:55 -050070 , m_adjacencyList()
71 , m_namePrefixList()
akmhoquec8a10f72014-04-25 18:42:55 -050072 , m_sequencingManager()
akmhoque53353462014-04-22 08:43:45 -050073 , m_isDaemonProcess(false)
74 , m_configFileName("nlsr.conf")
akmhoque31d1d4b2014-05-05 22:08:14 -050075 , m_nlsrLsdb(*this)
akmhoque53353462014-04-22 08:43:45 -050076 , m_adjBuildCount(0)
77 , m_isBuildAdjLsaSheduled(false)
78 , m_isRouteCalculationScheduled(false)
79 , m_isRoutingTableCalculating(false)
80 , m_routingTable()
akmhoque31d1d4b2014-05-05 22:08:14 -050081 , m_fib(*this, m_nlsrFace)
82 , m_namePrefixTable(*this)
akmhoquefdbddb12014-05-02 18:35:19 -050083 , m_syncLogicHandler(m_nlsrFace.getIoService())
akmhoque31d1d4b2014-05-05 22:08:14 -050084 , m_helloProtocol(*this)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070085
86 , m_certificateCache(new ndn::CertificateCacheTtl(m_nlsrFace.getIoService()))
87 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache)
akmhoquee1765152014-06-30 11:32:01 -050088
akmhoque060d3022014-08-12 13:35:06 -050089 , m_faceMonitor(m_nlsrFace)
90 {
91 m_faceMonitor.onNotification += ndn::bind(&Nlsr::onFaceEventNotification, this, _1);
92 m_faceMonitor.start();
93 }
akmhoque298385a2014-02-13 14:13:09 -060094
akmhoque53353462014-04-22 08:43:45 -050095 void
96 registrationFailed(const ndn::Name& name);
97
98 void
akmhoque157b0a42014-05-13 00:26:37 -050099 onRegistrationSuccess(const ndn::Name& name);
100
101 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 setInfoInterestFilter();
103
104 void
105 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -0500106
107 void
108 startEventLoop();
109
akmhoquefdbddb12014-05-02 18:35:19 -0500110 void
111 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500112
113 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500114 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700115 {
akmhoque53353462014-04-22 08:43:45 -0500116 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700117 }
118
akmhoque53353462014-04-22 08:43:45 -0500119 void
akmhoquefdbddb12014-05-02 18:35:19 -0500120 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500121 {
akmhoque53353462014-04-22 08:43:45 -0500122 m_configFileName = fileName;
123 }
akmhoque5a44dd42014-03-12 18:11:32 -0500124
akmhoque53353462014-04-22 08:43:45 -0500125 bool
126 getIsSetDaemonProcess()
127 {
128 return m_isDaemonProcess;
129 }
akmhoque5a44dd42014-03-12 18:11:32 -0500130
akmhoque53353462014-04-22 08:43:45 -0500131 void
132 setIsDaemonProcess(bool value)
133 {
134 m_isDaemonProcess = value;
135 }
akmhoque5a44dd42014-03-12 18:11:32 -0500136
akmhoque53353462014-04-22 08:43:45 -0500137 ConfParameter&
138 getConfParameter()
139 {
140 return m_confParam;
141 }
akmhoque5a44dd42014-03-12 18:11:32 -0500142
akmhoquec8a10f72014-04-25 18:42:55 -0500143 AdjacencyList&
144 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500145 {
akmhoquec8a10f72014-04-25 18:42:55 -0500146 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500147 }
akmhoque298385a2014-02-13 14:13:09 -0600148
akmhoquec8a10f72014-04-25 18:42:55 -0500149 NamePrefixList&
150 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500151 {
akmhoquec8a10f72014-04-25 18:42:55 -0500152 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500153 }
akmhoque298385a2014-02-13 14:13:09 -0600154
akmhoque53353462014-04-22 08:43:45 -0500155 ndn::Scheduler&
156 getScheduler()
157 {
158 return m_scheduler;
159 }
akmhoque298385a2014-02-13 14:13:09 -0600160
akmhoquefdbddb12014-05-02 18:35:19 -0500161 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500162 getNlsrFace()
163 {
164 return m_nlsrFace;
165 }
akmhoque298385a2014-02-13 14:13:09 -0600166
akmhoque53353462014-04-22 08:43:45 -0500167 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500168 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500169 {
akmhoquec8a10f72014-04-25 18:42:55 -0500170 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500171 }
akmhoque298385a2014-02-13 14:13:09 -0600172
akmhoque53353462014-04-22 08:43:45 -0500173 Lsdb&
174 getLsdb()
175 {
176 return m_nlsrLsdb;
177 }
akmhoque298385a2014-02-13 14:13:09 -0600178
akmhoque53353462014-04-22 08:43:45 -0500179 RoutingTable&
180 getRoutingTable()
181 {
182 return m_routingTable;
183 }
akmhoque298385a2014-02-13 14:13:09 -0600184
akmhoquec8a10f72014-04-25 18:42:55 -0500185 NamePrefixTable&
186 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500187 {
akmhoquec8a10f72014-04-25 18:42:55 -0500188 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500189 }
akmhoque298385a2014-02-13 14:13:09 -0600190
akmhoque53353462014-04-22 08:43:45 -0500191 Fib&
192 getFib()
193 {
194 return m_fib;
195 }
akmhoque298385a2014-02-13 14:13:09 -0600196
akmhoque53353462014-04-22 08:43:45 -0500197 long int
198 getAdjBuildCount()
199 {
200 return m_adjBuildCount;
201 }
akmhoque298385a2014-02-13 14:13:09 -0600202
akmhoque53353462014-04-22 08:43:45 -0500203 void
204 incrementAdjBuildCount()
205 {
206 m_adjBuildCount++;
207 }
akmhoque298385a2014-02-13 14:13:09 -0600208
akmhoque53353462014-04-22 08:43:45 -0500209 void
akmhoquefdbddb12014-05-02 18:35:19 -0500210 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500211 {
212 m_adjBuildCount = abc;
213 }
akmhoque298385a2014-02-13 14:13:09 -0600214
akmhoque157b0a42014-05-13 00:26:37 -0500215 bool
akmhoque53353462014-04-22 08:43:45 -0500216 getIsBuildAdjLsaSheduled()
217 {
218 return m_isBuildAdjLsaSheduled;
219 }
akmhoque298385a2014-02-13 14:13:09 -0600220
akmhoque53353462014-04-22 08:43:45 -0500221 void
222 setIsBuildAdjLsaSheduled(bool iabls)
223 {
224 m_isBuildAdjLsaSheduled = iabls;
225 }
akmhoque298385a2014-02-13 14:13:09 -0600226
akmhoque298385a2014-02-13 14:13:09 -0600227
akmhoque53353462014-04-22 08:43:45 -0500228 void
akmhoquefdbddb12014-05-02 18:35:19 -0500229 setApiPort(int32_t ap)
akmhoque53353462014-04-22 08:43:45 -0500230 {
231 m_apiPort = ap;
232 }
akmhoque298385a2014-02-13 14:13:09 -0600233
akmhoquefdbddb12014-05-02 18:35:19 -0500234 int32_t
akmhoque53353462014-04-22 08:43:45 -0500235 getApiPort()
236 {
237 return m_apiPort;
238 }
akmhoque298385a2014-02-13 14:13:09 -0600239
akmhoque53353462014-04-22 08:43:45 -0500240 bool
241 getIsRoutingTableCalculating()
242 {
243 return m_isRoutingTableCalculating;
244 }
akmhoque85d88332014-02-17 21:11:21 -0600245
akmhoque53353462014-04-22 08:43:45 -0500246 void
247 setIsRoutingTableCalculating(bool irtc)
248 {
249 m_isRoutingTableCalculating = irtc;
250 }
akmhoque298385a2014-02-13 14:13:09 -0600251
akmhoque53353462014-04-22 08:43:45 -0500252 bool
253 getIsRouteCalculationScheduled()
254 {
255 return m_isRouteCalculationScheduled;
256 }
akmhoque298385a2014-02-13 14:13:09 -0600257
akmhoque53353462014-04-22 08:43:45 -0500258 void
259 setIsRouteCalculationScheduled(bool ircs)
260 {
261 m_isRouteCalculationScheduled = ircs;
262 }
akmhoque298385a2014-02-13 14:13:09 -0600263
akmhoque53353462014-04-22 08:43:45 -0500264 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500265 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500266 {
akmhoquec8a10f72014-04-25 18:42:55 -0500267 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500268 }
akmhoque2bb198e2014-02-28 11:46:27 -0600269
akmhoque53353462014-04-22 08:43:45 -0500270 void
271 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600272
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700273 void
akmhoque443ad812014-07-29 10:26:56 -0500274 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700275
276 void
277 loadValidator(boost::property_tree::ptree section,
278 const std::string& filename)
279 {
280 m_validator.load(section, filename);
281 }
282
283 Validator&
284 getValidator()
285 {
286 return m_validator;
287 }
288
289 void
290 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
291 {
292 if (static_cast<bool>(certificate))
293 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
294 // without version
295 }
296
297 ndn::shared_ptr<const ndn::IdentityCertificate>
298 getCertificate(const ndn::Name& certificateNameWithoutVersion)
299 {
300 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
301
302 if (it != m_certToPublish.end())
303 {
304 return it->second;
305 }
306
307 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
308 }
309
310 ndn::KeyChain&
311 getKeyChain()
312 {
313 return m_keyChain;
314 }
315
316 const ndn::Name&
317 getDefaultCertName()
318 {
319 return m_defaultCertName;
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
akmhoque393d4ff2014-07-16 14:27:03 -0500336private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700337 void
338 registerKeyPrefix();
339
340 void
341 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
342
343 void
344 onKeyPrefixRegSuccess(const ndn::Name& name);
345
akmhoquee1765152014-06-30 11:32:01 -0500346 void
akmhoquee1765152014-06-30 11:32:01 -0500347 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
348
349 void
350 onDestroyFaceFailure(int32_t code, const std::string& error);
351
352 void
akmhoquec04e7272014-07-02 11:00:14 -0500353 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500354
akmhoque157b0a42014-05-13 00:26:37 -0500355private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700356 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
357
akmhoquefdbddb12014-05-02 18:35:19 -0500358 ndn::Face m_nlsrFace;
akmhoque53353462014-04-22 08:43:45 -0500359 ndn::Scheduler m_scheduler;
360 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500361 AdjacencyList m_adjacencyList;
362 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500363 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500364 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500365 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500366 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500367 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500368 bool m_isBuildAdjLsaSheduled;
369 bool m_isRouteCalculationScheduled;
370 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500371 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500372 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500373 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500374 SyncLogicHandler m_syncLogicHandler;
akmhoquefdbddb12014-05-02 18:35:19 -0500375 int32_t m_apiPort;
akmhoque31d1d4b2014-05-05 22:08:14 -0500376 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700377
378 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
379 CertMap m_certToPublish;
380 Validator m_validator;
381 ndn::KeyChain m_keyChain;
382 ndn::Name m_defaultIdentity;
383 ndn::Name m_defaultCertName;
akmhoquee1765152014-06-30 11:32:01 -0500384
akmhoque060d3022014-08-12 13:35:06 -0500385 ndn::nfd::FaceMonitor m_faceMonitor;
akmhoque53353462014-04-22 08:43:45 -0500386};
akmhoque298385a2014-02-13 14:13:09 -0600387
akmhoqueb1710aa2014-02-19 17:13:36 -0600388} //namespace nlsr
389
akmhoque53353462014-04-22 08:43:45 -0500390#endif //NLSR_HPP