blob: 61975884ef52014f9db14a7e5c19a2b55288c486 [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
akmhoque53353462014-04-22 08:43:45 -0500227 bool
228 getIsRoutingTableCalculating()
229 {
230 return m_isRoutingTableCalculating;
231 }
akmhoque85d88332014-02-17 21:11:21 -0600232
akmhoque53353462014-04-22 08:43:45 -0500233 void
234 setIsRoutingTableCalculating(bool irtc)
235 {
236 m_isRoutingTableCalculating = irtc;
237 }
akmhoque298385a2014-02-13 14:13:09 -0600238
akmhoque53353462014-04-22 08:43:45 -0500239 bool
240 getIsRouteCalculationScheduled()
241 {
242 return m_isRouteCalculationScheduled;
243 }
akmhoque298385a2014-02-13 14:13:09 -0600244
akmhoque53353462014-04-22 08:43:45 -0500245 void
246 setIsRouteCalculationScheduled(bool ircs)
247 {
248 m_isRouteCalculationScheduled = ircs;
249 }
akmhoque298385a2014-02-13 14:13:09 -0600250
akmhoque53353462014-04-22 08:43:45 -0500251 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500252 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500253 {
akmhoquec8a10f72014-04-25 18:42:55 -0500254 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500255 }
akmhoque2bb198e2014-02-28 11:46:27 -0600256
akmhoque53353462014-04-22 08:43:45 -0500257 void
258 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600259
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700260 void
akmhoque443ad812014-07-29 10:26:56 -0500261 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700262
263 void
264 loadValidator(boost::property_tree::ptree section,
265 const std::string& filename)
266 {
267 m_validator.load(section, filename);
268 }
269
270 Validator&
271 getValidator()
272 {
273 return m_validator;
274 }
275
276 void
277 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
278 {
279 if (static_cast<bool>(certificate))
280 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
281 // without version
282 }
283
284 ndn::shared_ptr<const ndn::IdentityCertificate>
285 getCertificate(const ndn::Name& certificateNameWithoutVersion)
286 {
287 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
288
289 if (it != m_certToPublish.end())
290 {
291 return it->second;
292 }
293
294 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
295 }
296
297 ndn::KeyChain&
298 getKeyChain()
299 {
300 return m_keyChain;
301 }
302
303 const ndn::Name&
304 getDefaultCertName()
305 {
306 return m_defaultCertName;
307 }
308
akmhoquee1765152014-06-30 11:32:01 -0500309 void
310 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500311 const CommandSucceedCallback& onSuccess,
312 const CommandFailCallback& onFailure);
313
314 void
315 destroyFaces();
316
akmhoque157b0a42014-05-13 00:26:37 -0500317 void
akmhoquec04e7272014-07-02 11:00:14 -0500318 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500319
akmhoque0494c252014-07-23 23:46:44 -0500320 void
321 daemonize();
322
akmhoque393d4ff2014-07-16 14:27:03 -0500323private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700324 void
325 registerKeyPrefix();
326
327 void
328 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
329
330 void
331 onKeyPrefixRegSuccess(const ndn::Name& name);
332
akmhoquee1765152014-06-30 11:32:01 -0500333 void
akmhoquee1765152014-06-30 11:32:01 -0500334 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
335
336 void
337 onDestroyFaceFailure(int32_t code, const std::string& error);
338
339 void
akmhoquec04e7272014-07-02 11:00:14 -0500340 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500341
akmhoque157b0a42014-05-13 00:26:37 -0500342private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700343 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
344
akmhoquefdbddb12014-05-02 18:35:19 -0500345 ndn::Face m_nlsrFace;
akmhoque53353462014-04-22 08:43:45 -0500346 ndn::Scheduler m_scheduler;
347 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500348 AdjacencyList m_adjacencyList;
349 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500350 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500351 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500352 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500353 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500354 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500355 bool m_isBuildAdjLsaSheduled;
356 bool m_isRouteCalculationScheduled;
357 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500358 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500359 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500360 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500361 SyncLogicHandler m_syncLogicHandler;
akmhoque31d1d4b2014-05-05 22:08:14 -0500362 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700363
364 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
365 CertMap m_certToPublish;
366 Validator m_validator;
367 ndn::KeyChain m_keyChain;
368 ndn::Name m_defaultIdentity;
369 ndn::Name m_defaultCertName;
akmhoquee1765152014-06-30 11:32:01 -0500370
akmhoque060d3022014-08-12 13:35:06 -0500371 ndn::nfd::FaceMonitor m_faceMonitor;
akmhoque53353462014-04-22 08:43:45 -0500372};
akmhoque298385a2014-02-13 14:13:09 -0600373
akmhoqueb1710aa2014-02-19 17:13:36 -0600374} //namespace nlsr
375
akmhoque53353462014-04-22 08:43:45 -0500376#endif //NLSR_HPP