blob: 00b78772f5f03dad2fffa9794d49a13e93e8d5da [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:
Vince Lehman904c2412014-09-23 19:36:11 -050067 Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face)
68 : m_nlsrFace(face)
Vince Lehman7c603292014-09-11 17:48:16 -050069 , m_scheduler(scheduler)
akmhoque53353462014-04-22 08:43:45 -050070 , m_confParam()
akmhoquec8a10f72014-04-25 18:42:55 -050071 , m_adjacencyList()
72 , m_namePrefixList()
akmhoquec8a10f72014-04-25 18:42:55 -050073 , m_sequencingManager()
akmhoque53353462014-04-22 08:43:45 -050074 , m_isDaemonProcess(false)
75 , m_configFileName("nlsr.conf")
Vince Lehman7c603292014-09-11 17:48:16 -050076 , m_nlsrLsdb(*this, scheduler)
akmhoque53353462014-04-22 08:43:45 -050077 , m_adjBuildCount(0)
78 , m_isBuildAdjLsaSheduled(false)
79 , m_isRouteCalculationScheduled(false)
80 , m_isRoutingTableCalculating(false)
Vince Lehman7c603292014-09-11 17:48:16 -050081 , m_routingTable(scheduler)
Vince Lehman942eb7b2014-10-02 10:09:27 -050082 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam)
akmhoque31d1d4b2014-05-05 22:08:14 -050083 , m_namePrefixTable(*this)
Vince Lehman904c2412014-09-23 19:36:11 -050084 , m_syncLogicHandler(m_nlsrFace, m_nlsrLsdb, m_confParam)
Vince Lehman7c603292014-09-11 17:48:16 -050085 , m_helloProtocol(*this, scheduler)
86 , m_certificateCache(new ndn::CertificateCacheTtl(ioService))
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070087 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache)
akmhoque060d3022014-08-12 13:35:06 -050088 , m_faceMonitor(m_nlsrFace)
89 {
90 m_faceMonitor.onNotification += ndn::bind(&Nlsr::onFaceEventNotification, this, _1);
91 m_faceMonitor.start();
92 }
akmhoque298385a2014-02-13 14:13:09 -060093
akmhoque53353462014-04-22 08:43:45 -050094 void
95 registrationFailed(const ndn::Name& name);
96
97 void
akmhoque157b0a42014-05-13 00:26:37 -050098 onRegistrationSuccess(const ndn::Name& name);
99
100 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 setInfoInterestFilter();
102
103 void
104 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -0500105
106 void
107 startEventLoop();
108
akmhoquefdbddb12014-05-02 18:35:19 -0500109 void
110 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500111
112 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500113 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700114 {
akmhoque53353462014-04-22 08:43:45 -0500115 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700116 }
117
akmhoque53353462014-04-22 08:43:45 -0500118 void
akmhoquefdbddb12014-05-02 18:35:19 -0500119 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500120 {
akmhoque53353462014-04-22 08:43:45 -0500121 m_configFileName = fileName;
122 }
akmhoque5a44dd42014-03-12 18:11:32 -0500123
akmhoque53353462014-04-22 08:43:45 -0500124 bool
125 getIsSetDaemonProcess()
126 {
127 return m_isDaemonProcess;
128 }
akmhoque5a44dd42014-03-12 18:11:32 -0500129
akmhoque53353462014-04-22 08:43:45 -0500130 void
131 setIsDaemonProcess(bool value)
132 {
133 m_isDaemonProcess = value;
134 }
akmhoque5a44dd42014-03-12 18:11:32 -0500135
akmhoque53353462014-04-22 08:43:45 -0500136 ConfParameter&
137 getConfParameter()
138 {
139 return m_confParam;
140 }
akmhoque5a44dd42014-03-12 18:11:32 -0500141
akmhoquec8a10f72014-04-25 18:42:55 -0500142 AdjacencyList&
143 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500144 {
akmhoquec8a10f72014-04-25 18:42:55 -0500145 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500146 }
akmhoque298385a2014-02-13 14:13:09 -0600147
akmhoquec8a10f72014-04-25 18:42:55 -0500148 NamePrefixList&
149 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500150 {
akmhoquec8a10f72014-04-25 18:42:55 -0500151 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500152 }
akmhoque298385a2014-02-13 14:13:09 -0600153
akmhoquefdbddb12014-05-02 18:35:19 -0500154 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500155 getNlsrFace()
156 {
157 return m_nlsrFace;
158 }
akmhoque298385a2014-02-13 14:13:09 -0600159
akmhoque53353462014-04-22 08:43:45 -0500160 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500161 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500162 {
akmhoquec8a10f72014-04-25 18:42:55 -0500163 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500164 }
akmhoque298385a2014-02-13 14:13:09 -0600165
akmhoque53353462014-04-22 08:43:45 -0500166 Lsdb&
167 getLsdb()
168 {
169 return m_nlsrLsdb;
170 }
akmhoque298385a2014-02-13 14:13:09 -0600171
akmhoque53353462014-04-22 08:43:45 -0500172 RoutingTable&
173 getRoutingTable()
174 {
175 return m_routingTable;
176 }
akmhoque298385a2014-02-13 14:13:09 -0600177
akmhoquec8a10f72014-04-25 18:42:55 -0500178 NamePrefixTable&
179 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500180 {
akmhoquec8a10f72014-04-25 18:42:55 -0500181 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500182 }
akmhoque298385a2014-02-13 14:13:09 -0600183
akmhoque53353462014-04-22 08:43:45 -0500184 Fib&
185 getFib()
186 {
187 return m_fib;
188 }
akmhoque298385a2014-02-13 14:13:09 -0600189
akmhoque53353462014-04-22 08:43:45 -0500190 long int
191 getAdjBuildCount()
192 {
193 return m_adjBuildCount;
194 }
akmhoque298385a2014-02-13 14:13:09 -0600195
akmhoque53353462014-04-22 08:43:45 -0500196 void
197 incrementAdjBuildCount()
198 {
199 m_adjBuildCount++;
200 }
akmhoque298385a2014-02-13 14:13:09 -0600201
akmhoque53353462014-04-22 08:43:45 -0500202 void
akmhoquefdbddb12014-05-02 18:35:19 -0500203 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500204 {
205 m_adjBuildCount = abc;
206 }
akmhoque298385a2014-02-13 14:13:09 -0600207
akmhoque157b0a42014-05-13 00:26:37 -0500208 bool
akmhoque53353462014-04-22 08:43:45 -0500209 getIsBuildAdjLsaSheduled()
210 {
211 return m_isBuildAdjLsaSheduled;
212 }
akmhoque298385a2014-02-13 14:13:09 -0600213
akmhoque53353462014-04-22 08:43:45 -0500214 void
215 setIsBuildAdjLsaSheduled(bool iabls)
216 {
217 m_isBuildAdjLsaSheduled = iabls;
218 }
akmhoque298385a2014-02-13 14:13:09 -0600219
akmhoque53353462014-04-22 08:43:45 -0500220 bool
221 getIsRoutingTableCalculating()
222 {
223 return m_isRoutingTableCalculating;
224 }
akmhoque85d88332014-02-17 21:11:21 -0600225
akmhoque53353462014-04-22 08:43:45 -0500226 void
227 setIsRoutingTableCalculating(bool irtc)
228 {
229 m_isRoutingTableCalculating = irtc;
230 }
akmhoque298385a2014-02-13 14:13:09 -0600231
akmhoque53353462014-04-22 08:43:45 -0500232 bool
233 getIsRouteCalculationScheduled()
234 {
235 return m_isRouteCalculationScheduled;
236 }
akmhoque298385a2014-02-13 14:13:09 -0600237
akmhoque53353462014-04-22 08:43:45 -0500238 void
239 setIsRouteCalculationScheduled(bool ircs)
240 {
241 m_isRouteCalculationScheduled = ircs;
242 }
akmhoque298385a2014-02-13 14:13:09 -0600243
akmhoque53353462014-04-22 08:43:45 -0500244 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500245 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500246 {
akmhoquec8a10f72014-04-25 18:42:55 -0500247 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500248 }
akmhoque2bb198e2014-02-28 11:46:27 -0600249
akmhoque53353462014-04-22 08:43:45 -0500250 void
251 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600252
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700253 void
akmhoque443ad812014-07-29 10:26:56 -0500254 initializeKey();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700255
256 void
257 loadValidator(boost::property_tree::ptree section,
258 const std::string& filename)
259 {
260 m_validator.load(section, filename);
261 }
262
263 Validator&
264 getValidator()
265 {
266 return m_validator;
267 }
268
269 void
270 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
271 {
272 if (static_cast<bool>(certificate))
273 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
274 // without version
275 }
276
277 ndn::shared_ptr<const ndn::IdentityCertificate>
278 getCertificate(const ndn::Name& certificateNameWithoutVersion)
279 {
280 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
281
282 if (it != m_certToPublish.end())
283 {
284 return it->second;
285 }
286
287 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
288 }
289
290 ndn::KeyChain&
291 getKeyChain()
292 {
293 return m_keyChain;
294 }
295
296 const ndn::Name&
297 getDefaultCertName()
298 {
299 return m_defaultCertName;
300 }
301
akmhoquee1765152014-06-30 11:32:01 -0500302 void
303 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500304 const CommandSucceedCallback& onSuccess,
305 const CommandFailCallback& onFailure);
306
307 void
308 destroyFaces();
309
akmhoque157b0a42014-05-13 00:26:37 -0500310 void
akmhoquec04e7272014-07-02 11:00:14 -0500311 setStrategies();
akmhoque157b0a42014-05-13 00:26:37 -0500312
akmhoque0494c252014-07-23 23:46:44 -0500313 void
314 daemonize();
315
akmhoque393d4ff2014-07-16 14:27:03 -0500316private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700317 void
318 registerKeyPrefix();
319
320 void
321 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
322
323 void
324 onKeyPrefixRegSuccess(const ndn::Name& name);
325
akmhoquee1765152014-06-30 11:32:01 -0500326 void
akmhoquee1765152014-06-30 11:32:01 -0500327 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
328
329 void
330 onDestroyFaceFailure(int32_t code, const std::string& error);
331
332 void
akmhoquec04e7272014-07-02 11:00:14 -0500333 onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification);
akmhoquee1765152014-06-30 11:32:01 -0500334
akmhoque157b0a42014-05-13 00:26:37 -0500335private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700336 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
337
Vince Lehman904c2412014-09-23 19:36:11 -0500338 ndn::Face& m_nlsrFace;
Vince Lehman7c603292014-09-11 17:48:16 -0500339 ndn::Scheduler& m_scheduler;
akmhoque53353462014-04-22 08:43:45 -0500340 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500341 AdjacencyList m_adjacencyList;
342 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500343 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500344 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500345 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500346 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500347 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500348 bool m_isBuildAdjLsaSheduled;
349 bool m_isRouteCalculationScheduled;
350 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500351 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500352 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500353 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500354 SyncLogicHandler m_syncLogicHandler;
akmhoque31d1d4b2014-05-05 22:08:14 -0500355 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700356
357 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
358 CertMap m_certToPublish;
359 Validator m_validator;
360 ndn::KeyChain m_keyChain;
361 ndn::Name m_defaultIdentity;
362 ndn::Name m_defaultCertName;
akmhoquee1765152014-06-30 11:32:01 -0500363
akmhoque060d3022014-08-12 13:35:06 -0500364 ndn::nfd::FaceMonitor m_faceMonitor;
akmhoque53353462014-04-22 08:43:45 -0500365};
akmhoque298385a2014-02-13 14:13:09 -0600366
akmhoqueb1710aa2014-02-19 17:13:36 -0600367} //namespace nlsr
368
akmhoque53353462014-04-22 08:43:45 -0500369#endif //NLSR_HPP