blob: c3b479568e0a1af6f5e7532b2c416de2d8ca8fe6 [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>
akmhoque298385a2014-02-13 14:13:09 -060034
akmhoque53353462014-04-22 08:43:45 -050035#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050036#include "adjacency-list.hpp"
37#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050038#include "lsdb.hpp"
39#include "sequencing-manager.hpp"
40#include "route/routing-table.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050041#include "route/name-prefix-table.hpp"
akmhoque53353462014-04-22 08:43:45 -050042#include "route/fib.hpp"
akmhoque53353462014-04-22 08:43:45 -050043#include "communication/sync-logic-handler.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050044#include "hello-protocol.hpp"
akmhoquec04e7272014-07-02 11:00:14 -050045#include "face-monitor.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
akmhoquec04e7272014-07-02 11:00:14 -050089 , m_faceMonitor(m_nlsrFace.getIoService(),
90 ndn::bind(&Nlsr::onFaceEventNotification, this, _1))
akmhoque53353462014-04-22 08:43:45 -050091 {}
akmhoque298385a2014-02-13 14:13:09 -060092
akmhoque53353462014-04-22 08:43:45 -050093 void
94 registrationFailed(const ndn::Name& name);
95
96 void
akmhoque157b0a42014-05-13 00:26:37 -050097 onRegistrationSuccess(const ndn::Name& name);
98
99 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 setInfoInterestFilter();
101
102 void
103 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -0500104
105 void
106 startEventLoop();
107
akmhoquefdbddb12014-05-02 18:35:19 -0500108 void
109 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500110
111 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500112 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700113 {
akmhoque53353462014-04-22 08:43:45 -0500114 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700115 }
116
akmhoque53353462014-04-22 08:43:45 -0500117 void
akmhoquefdbddb12014-05-02 18:35:19 -0500118 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500119 {
akmhoque53353462014-04-22 08:43:45 -0500120 m_configFileName = fileName;
121 }
akmhoque5a44dd42014-03-12 18:11:32 -0500122
akmhoque53353462014-04-22 08:43:45 -0500123 bool
124 getIsSetDaemonProcess()
125 {
126 return m_isDaemonProcess;
127 }
akmhoque5a44dd42014-03-12 18:11:32 -0500128
akmhoque53353462014-04-22 08:43:45 -0500129 void
130 setIsDaemonProcess(bool value)
131 {
132 m_isDaemonProcess = value;
133 }
akmhoque5a44dd42014-03-12 18:11:32 -0500134
akmhoque53353462014-04-22 08:43:45 -0500135 ConfParameter&
136 getConfParameter()
137 {
138 return m_confParam;
139 }
akmhoque5a44dd42014-03-12 18:11:32 -0500140
akmhoquec8a10f72014-04-25 18:42:55 -0500141 AdjacencyList&
142 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500143 {
akmhoquec8a10f72014-04-25 18:42:55 -0500144 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500145 }
akmhoque298385a2014-02-13 14:13:09 -0600146
akmhoquec8a10f72014-04-25 18:42:55 -0500147 NamePrefixList&
148 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500149 {
akmhoquec8a10f72014-04-25 18:42:55 -0500150 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500151 }
akmhoque298385a2014-02-13 14:13:09 -0600152
akmhoque53353462014-04-22 08:43:45 -0500153 ndn::Scheduler&
154 getScheduler()
155 {
156 return m_scheduler;
157 }
akmhoque298385a2014-02-13 14:13:09 -0600158
akmhoquefdbddb12014-05-02 18:35:19 -0500159 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500160 getNlsrFace()
161 {
162 return m_nlsrFace;
163 }
akmhoque298385a2014-02-13 14:13:09 -0600164
akmhoque53353462014-04-22 08:43:45 -0500165 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500166 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500167 {
akmhoquec8a10f72014-04-25 18:42:55 -0500168 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500169 }
akmhoque298385a2014-02-13 14:13:09 -0600170
akmhoque53353462014-04-22 08:43:45 -0500171 Lsdb&
172 getLsdb()
173 {
174 return m_nlsrLsdb;
175 }
akmhoque298385a2014-02-13 14:13:09 -0600176
akmhoque53353462014-04-22 08:43:45 -0500177 RoutingTable&
178 getRoutingTable()
179 {
180 return m_routingTable;
181 }
akmhoque298385a2014-02-13 14:13:09 -0600182
akmhoquec8a10f72014-04-25 18:42:55 -0500183 NamePrefixTable&
184 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500185 {
akmhoquec8a10f72014-04-25 18:42:55 -0500186 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500187 }
akmhoque298385a2014-02-13 14:13:09 -0600188
akmhoque53353462014-04-22 08:43:45 -0500189 Fib&
190 getFib()
191 {
192 return m_fib;
193 }
akmhoque298385a2014-02-13 14:13:09 -0600194
akmhoque53353462014-04-22 08:43:45 -0500195 long int
196 getAdjBuildCount()
197 {
198 return m_adjBuildCount;
199 }
akmhoque298385a2014-02-13 14:13:09 -0600200
akmhoque53353462014-04-22 08:43:45 -0500201 void
202 incrementAdjBuildCount()
203 {
204 m_adjBuildCount++;
205 }
akmhoque298385a2014-02-13 14:13:09 -0600206
akmhoque53353462014-04-22 08:43:45 -0500207 void
akmhoquefdbddb12014-05-02 18:35:19 -0500208 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500209 {
210 m_adjBuildCount = abc;
211 }
akmhoque298385a2014-02-13 14:13:09 -0600212
akmhoque157b0a42014-05-13 00:26:37 -0500213 bool
akmhoque53353462014-04-22 08:43:45 -0500214 getIsBuildAdjLsaSheduled()
215 {
216 return m_isBuildAdjLsaSheduled;
217 }
akmhoque298385a2014-02-13 14:13:09 -0600218
akmhoque53353462014-04-22 08:43:45 -0500219 void
220 setIsBuildAdjLsaSheduled(bool iabls)
221 {
222 m_isBuildAdjLsaSheduled = iabls;
223 }
akmhoque298385a2014-02-13 14:13:09 -0600224
akmhoque298385a2014-02-13 14:13:09 -0600225
akmhoque53353462014-04-22 08:43:45 -0500226 void
akmhoquefdbddb12014-05-02 18:35:19 -0500227 setApiPort(int32_t ap)
akmhoque53353462014-04-22 08:43:45 -0500228 {
229 m_apiPort = ap;
230 }
akmhoque298385a2014-02-13 14:13:09 -0600231
akmhoquefdbddb12014-05-02 18:35:19 -0500232 int32_t
akmhoque53353462014-04-22 08:43:45 -0500233 getApiPort()
234 {
235 return m_apiPort;
236 }
akmhoque298385a2014-02-13 14:13:09 -0600237
akmhoque53353462014-04-22 08:43:45 -0500238 bool
239 getIsRoutingTableCalculating()
240 {
241 return m_isRoutingTableCalculating;
242 }
akmhoque85d88332014-02-17 21:11:21 -0600243
akmhoque53353462014-04-22 08:43:45 -0500244 void
245 setIsRoutingTableCalculating(bool irtc)
246 {
247 m_isRoutingTableCalculating = irtc;
248 }
akmhoque298385a2014-02-13 14:13:09 -0600249
akmhoque53353462014-04-22 08:43:45 -0500250 bool
251 getIsRouteCalculationScheduled()
252 {
253 return m_isRouteCalculationScheduled;
254 }
akmhoque298385a2014-02-13 14:13:09 -0600255
akmhoque53353462014-04-22 08:43:45 -0500256 void
257 setIsRouteCalculationScheduled(bool ircs)
258 {
259 m_isRouteCalculationScheduled = ircs;
260 }
akmhoque298385a2014-02-13 14:13:09 -0600261
akmhoque53353462014-04-22 08:43:45 -0500262 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500263 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500264 {
akmhoquec8a10f72014-04-25 18:42:55 -0500265 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500266 }
akmhoque2bb198e2014-02-28 11:46:27 -0600267
akmhoque53353462014-04-22 08:43:45 -0500268 void
269 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600270
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700271 void
272 intializeKey();
273
274 void
275 loadValidator(boost::property_tree::ptree section,
276 const std::string& filename)
277 {
278 m_validator.load(section, filename);
279 }
280
281 Validator&
282 getValidator()
283 {
284 return m_validator;
285 }
286
287 void
288 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
289 {
290 if (static_cast<bool>(certificate))
291 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
292 // without version
293 }
294
295 ndn::shared_ptr<const ndn::IdentityCertificate>
296 getCertificate(const ndn::Name& certificateNameWithoutVersion)
297 {
298 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
299
300 if (it != m_certToPublish.end())
301 {
302 return it->second;
303 }
304
305 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
306 }
307
308 ndn::KeyChain&
309 getKeyChain()
310 {
311 return m_keyChain;
312 }
313
314 const ndn::Name&
315 getDefaultCertName()
316 {
317 return m_defaultCertName;
318 }
319
akmhoquee1765152014-06-30 11:32:01 -0500320 void
321 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500322 const CommandSucceedCallback& onSuccess,
323 const CommandFailCallback& onFailure);
324
325 void
326 destroyFaces();
327
akmhoque53353462014-04-22 08:43:45 -0500328private:
akmhoque157b0a42014-05-13 00:26:37 -0500329 void
akmhoquec04e7272014-07-02 11:00:14 -0500330 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500331
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700332 void
333 registerKeyPrefix();
334
335 void
336 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
337
338 void
339 onKeyPrefixRegSuccess(const ndn::Name& name);
340
akmhoquee1765152014-06-30 11:32:01 -0500341 void
akmhoquee1765152014-06-30 11:32:01 -0500342 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
343
344 void
345 onDestroyFaceFailure(int32_t code, const std::string& error);
346
347 void
akmhoquec04e7272014-07-02 11:00:14 -0500348 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500349
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
akmhoquefdbddb12014-05-02 18:35:19 -0500353 ndn::Face m_nlsrFace;
akmhoque53353462014-04-22 08:43:45 -0500354 ndn::Scheduler m_scheduler;
355 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;
akmhoquefdbddb12014-05-02 18:35:19 -0500370 int32_t m_apiPort;
akmhoque31d1d4b2014-05-05 22:08:14 -0500371 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700372
373 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
374 CertMap m_certToPublish;
375 Validator m_validator;
376 ndn::KeyChain m_keyChain;
377 ndn::Name m_defaultIdentity;
378 ndn::Name m_defaultCertName;
akmhoquee1765152014-06-30 11:32:01 -0500379
akmhoquec04e7272014-07-02 11:00:14 -0500380 FaceMonitor m_faceMonitor;
akmhoque53353462014-04-22 08:43:45 -0500381};
akmhoque298385a2014-02-13 14:13:09 -0600382
akmhoqueb1710aa2014-02-19 17:13:36 -0600383} //namespace nlsr
384
akmhoque53353462014-04-22 08:43:45 -0500385#endif //NLSR_HPP