blob: dd56c1bc0f67fd1fbba866ea10c47d522be22598 [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>
akmhoque298385a2014-02-13 14:13:09 -060033
akmhoque53353462014-04-22 08:43:45 -050034#include "conf-parameter.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050035#include "adjacency-list.hpp"
36#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050037#include "lsdb.hpp"
38#include "sequencing-manager.hpp"
39#include "route/routing-table.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050040#include "route/name-prefix-table.hpp"
akmhoque53353462014-04-22 08:43:45 -050041#include "route/fib.hpp"
akmhoque53353462014-04-22 08:43:45 -050042#include "communication/sync-logic-handler.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050043#include "hello-protocol.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060044
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070045#include "validator.hpp"
46
akmhoque2bb198e2014-02-28 11:46:27 -060047
akmhoque53353462014-04-22 08:43:45 -050048namespace nlsr {
49
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070050static ndn::Name DEFAULT_BROADCAST_PREFIX("/ndn/broadcast");
51
akmhoque53353462014-04-22 08:43:45 -050052class Nlsr
53{
akmhoquefdbddb12014-05-02 18:35:19 -050054 class Error : public std::runtime_error
55 {
56 public:
57 explicit
58 Error(const std::string& what)
59 : std::runtime_error(what)
60 {
61 }
62 };
63
akmhoque53353462014-04-22 08:43:45 -050064public:
akmhoquee1765152014-06-30 11:32:01 -050065 typedef ndn::function<void(const ndn::nfd::ControlParameters&)> CommandSucceedCallback;
66
67 typedef ndn::function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
68
akmhoque53353462014-04-22 08:43:45 -050069 Nlsr()
akmhoquefdbddb12014-05-02 18:35:19 -050070 : m_scheduler(m_nlsrFace.getIoService())
akmhoque53353462014-04-22 08:43:45 -050071 , m_confParam()
akmhoquec8a10f72014-04-25 18:42:55 -050072 , m_adjacencyList()
73 , m_namePrefixList()
akmhoquec8a10f72014-04-25 18:42:55 -050074 , m_sequencingManager()
akmhoque53353462014-04-22 08:43:45 -050075 , m_isDaemonProcess(false)
76 , m_configFileName("nlsr.conf")
akmhoque31d1d4b2014-05-05 22:08:14 -050077 , m_nlsrLsdb(*this)
akmhoque53353462014-04-22 08:43:45 -050078 , m_adjBuildCount(0)
79 , m_isBuildAdjLsaSheduled(false)
80 , m_isRouteCalculationScheduled(false)
81 , m_isRoutingTableCalculating(false)
82 , m_routingTable()
akmhoque31d1d4b2014-05-05 22:08:14 -050083 , m_fib(*this, m_nlsrFace)
84 , m_namePrefixTable(*this)
akmhoquefdbddb12014-05-02 18:35:19 -050085 , m_syncLogicHandler(m_nlsrFace.getIoService())
akmhoque31d1d4b2014-05-05 22:08:14 -050086 , m_helloProtocol(*this)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070087
88 , m_certificateCache(new ndn::CertificateCacheTtl(m_nlsrFace.getIoService()))
89 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache)
akmhoquee1765152014-06-30 11:32:01 -050090
91 , m_controller(m_nlsrFace)
92 , m_nFacesToCreate(0)
93 , m_nFacesCreated(0)
akmhoque53353462014-04-22 08:43:45 -050094 {}
akmhoque298385a2014-02-13 14:13:09 -060095
akmhoque53353462014-04-22 08:43:45 -050096 void
97 registrationFailed(const ndn::Name& name);
98
99 void
akmhoque157b0a42014-05-13 00:26:37 -0500100 onRegistrationSuccess(const ndn::Name& name);
101
102 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 setInfoInterestFilter();
104
105 void
106 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -0500107
108 void
109 startEventLoop();
110
akmhoquefdbddb12014-05-02 18:35:19 -0500111 void
112 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500113
114 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500115 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700116 {
akmhoque53353462014-04-22 08:43:45 -0500117 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700118 }
119
akmhoque53353462014-04-22 08:43:45 -0500120 void
akmhoquefdbddb12014-05-02 18:35:19 -0500121 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500122 {
akmhoque53353462014-04-22 08:43:45 -0500123 m_configFileName = fileName;
124 }
akmhoque5a44dd42014-03-12 18:11:32 -0500125
akmhoque53353462014-04-22 08:43:45 -0500126 bool
127 getIsSetDaemonProcess()
128 {
129 return m_isDaemonProcess;
130 }
akmhoque5a44dd42014-03-12 18:11:32 -0500131
akmhoque53353462014-04-22 08:43:45 -0500132 void
133 setIsDaemonProcess(bool value)
134 {
135 m_isDaemonProcess = value;
136 }
akmhoque5a44dd42014-03-12 18:11:32 -0500137
akmhoque53353462014-04-22 08:43:45 -0500138 ConfParameter&
139 getConfParameter()
140 {
141 return m_confParam;
142 }
akmhoque5a44dd42014-03-12 18:11:32 -0500143
akmhoquec8a10f72014-04-25 18:42:55 -0500144 AdjacencyList&
145 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500146 {
akmhoquec8a10f72014-04-25 18:42:55 -0500147 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500148 }
akmhoque298385a2014-02-13 14:13:09 -0600149
akmhoquec8a10f72014-04-25 18:42:55 -0500150 NamePrefixList&
151 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500152 {
akmhoquec8a10f72014-04-25 18:42:55 -0500153 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500154 }
akmhoque298385a2014-02-13 14:13:09 -0600155
akmhoque53353462014-04-22 08:43:45 -0500156 ndn::Scheduler&
157 getScheduler()
158 {
159 return m_scheduler;
160 }
akmhoque298385a2014-02-13 14:13:09 -0600161
akmhoquefdbddb12014-05-02 18:35:19 -0500162 ndn::Face&
akmhoque53353462014-04-22 08:43:45 -0500163 getNlsrFace()
164 {
165 return m_nlsrFace;
166 }
akmhoque298385a2014-02-13 14:13:09 -0600167
akmhoque53353462014-04-22 08:43:45 -0500168 SequencingManager&
akmhoquec8a10f72014-04-25 18:42:55 -0500169 getSequencingManager()
akmhoque53353462014-04-22 08:43:45 -0500170 {
akmhoquec8a10f72014-04-25 18:42:55 -0500171 return m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500172 }
akmhoque298385a2014-02-13 14:13:09 -0600173
akmhoque53353462014-04-22 08:43:45 -0500174 Lsdb&
175 getLsdb()
176 {
177 return m_nlsrLsdb;
178 }
akmhoque298385a2014-02-13 14:13:09 -0600179
akmhoque53353462014-04-22 08:43:45 -0500180 RoutingTable&
181 getRoutingTable()
182 {
183 return m_routingTable;
184 }
akmhoque298385a2014-02-13 14:13:09 -0600185
akmhoquec8a10f72014-04-25 18:42:55 -0500186 NamePrefixTable&
187 getNamePrefixTable()
akmhoque53353462014-04-22 08:43:45 -0500188 {
akmhoquec8a10f72014-04-25 18:42:55 -0500189 return m_namePrefixTable;
akmhoque53353462014-04-22 08:43:45 -0500190 }
akmhoque298385a2014-02-13 14:13:09 -0600191
akmhoque53353462014-04-22 08:43:45 -0500192 Fib&
193 getFib()
194 {
195 return m_fib;
196 }
akmhoque298385a2014-02-13 14:13:09 -0600197
akmhoque53353462014-04-22 08:43:45 -0500198 long int
199 getAdjBuildCount()
200 {
201 return m_adjBuildCount;
202 }
akmhoque298385a2014-02-13 14:13:09 -0600203
akmhoque53353462014-04-22 08:43:45 -0500204 void
205 incrementAdjBuildCount()
206 {
207 m_adjBuildCount++;
208 }
akmhoque298385a2014-02-13 14:13:09 -0600209
akmhoque53353462014-04-22 08:43:45 -0500210 void
akmhoquefdbddb12014-05-02 18:35:19 -0500211 setAdjBuildCount(int64_t abc)
akmhoque53353462014-04-22 08:43:45 -0500212 {
213 m_adjBuildCount = abc;
214 }
akmhoque298385a2014-02-13 14:13:09 -0600215
akmhoque157b0a42014-05-13 00:26:37 -0500216 bool
akmhoque53353462014-04-22 08:43:45 -0500217 getIsBuildAdjLsaSheduled()
218 {
219 return m_isBuildAdjLsaSheduled;
220 }
akmhoque298385a2014-02-13 14:13:09 -0600221
akmhoque53353462014-04-22 08:43:45 -0500222 void
223 setIsBuildAdjLsaSheduled(bool iabls)
224 {
225 m_isBuildAdjLsaSheduled = iabls;
226 }
akmhoque298385a2014-02-13 14:13:09 -0600227
akmhoque298385a2014-02-13 14:13:09 -0600228
akmhoque53353462014-04-22 08:43:45 -0500229 void
akmhoquefdbddb12014-05-02 18:35:19 -0500230 setApiPort(int32_t ap)
akmhoque53353462014-04-22 08:43:45 -0500231 {
232 m_apiPort = ap;
233 }
akmhoque298385a2014-02-13 14:13:09 -0600234
akmhoquefdbddb12014-05-02 18:35:19 -0500235 int32_t
akmhoque53353462014-04-22 08:43:45 -0500236 getApiPort()
237 {
238 return m_apiPort;
239 }
akmhoque298385a2014-02-13 14:13:09 -0600240
akmhoque53353462014-04-22 08:43:45 -0500241 bool
242 getIsRoutingTableCalculating()
243 {
244 return m_isRoutingTableCalculating;
245 }
akmhoque85d88332014-02-17 21:11:21 -0600246
akmhoque53353462014-04-22 08:43:45 -0500247 void
248 setIsRoutingTableCalculating(bool irtc)
249 {
250 m_isRoutingTableCalculating = irtc;
251 }
akmhoque298385a2014-02-13 14:13:09 -0600252
akmhoque53353462014-04-22 08:43:45 -0500253 bool
254 getIsRouteCalculationScheduled()
255 {
256 return m_isRouteCalculationScheduled;
257 }
akmhoque298385a2014-02-13 14:13:09 -0600258
akmhoque53353462014-04-22 08:43:45 -0500259 void
260 setIsRouteCalculationScheduled(bool ircs)
261 {
262 m_isRouteCalculationScheduled = ircs;
263 }
akmhoque298385a2014-02-13 14:13:09 -0600264
akmhoque53353462014-04-22 08:43:45 -0500265 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500266 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500267 {
akmhoquec8a10f72014-04-25 18:42:55 -0500268 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500269 }
akmhoque2bb198e2014-02-28 11:46:27 -0600270
akmhoque53353462014-04-22 08:43:45 -0500271 void
272 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600273
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700274 void
akmhoquee1765152014-06-30 11:32:01 -0500275 start();
276
277 void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700278 intializeKey();
279
280 void
281 loadValidator(boost::property_tree::ptree section,
282 const std::string& filename)
283 {
284 m_validator.load(section, filename);
285 }
286
287 Validator&
288 getValidator()
289 {
290 return m_validator;
291 }
292
293 void
294 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
295 {
296 if (static_cast<bool>(certificate))
297 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
298 // without version
299 }
300
301 ndn::shared_ptr<const ndn::IdentityCertificate>
302 getCertificate(const ndn::Name& certificateNameWithoutVersion)
303 {
304 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
305
306 if (it != m_certToPublish.end())
307 {
308 return it->second;
309 }
310
311 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
312 }
313
314 ndn::KeyChain&
315 getKeyChain()
316 {
317 return m_keyChain;
318 }
319
320 const ndn::Name&
321 getDefaultCertName()
322 {
323 return m_defaultCertName;
324 }
325
akmhoquee1765152014-06-30 11:32:01 -0500326 void
327 createFace(const std::string& faceUri,
akmhoquee1765152014-06-30 11:32:01 -0500328 const CommandSucceedCallback& onSuccess,
329 const CommandFailCallback& onFailure);
330
331 void
332 destroyFaces();
333
akmhoque53353462014-04-22 08:43:45 -0500334private:
akmhoque157b0a42014-05-13 00:26:37 -0500335 void
336 registerPrefixes();
337
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700338 void
339 registerKeyPrefix();
340
341 void
342 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
343
344 void
345 onKeyPrefixRegSuccess(const ndn::Name& name);
346
akmhoquee1765152014-06-30 11:32:01 -0500347 void
348 onCreateFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
349
350 void
351 onCreateFaceFailure(int32_t code, const std::string& error);
352
353 void
354 createFaces();
355
356 void
357 onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
358
359 void
360 onDestroyFaceFailure(int32_t code, const std::string& error);
361
362 void
363 destroyFace(const std::string& faceUri);
364
365 void
366 destroyFaceInNfd(const ndn::nfd::ControlParameters& faceDestroyResult);
367
akmhoque157b0a42014-05-13 00:26:37 -0500368private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700369 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
370
akmhoquefdbddb12014-05-02 18:35:19 -0500371 ndn::Face m_nlsrFace;
akmhoque53353462014-04-22 08:43:45 -0500372 ndn::Scheduler m_scheduler;
373 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500374 AdjacencyList m_adjacencyList;
375 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500376 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500377 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500378 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500379 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500380 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500381 bool m_isBuildAdjLsaSheduled;
382 bool m_isRouteCalculationScheduled;
383 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500384 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500385 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500386 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500387 SyncLogicHandler m_syncLogicHandler;
akmhoquefdbddb12014-05-02 18:35:19 -0500388 int32_t m_apiPort;
akmhoque31d1d4b2014-05-05 22:08:14 -0500389 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700390
391 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
392 CertMap m_certToPublish;
393 Validator m_validator;
394 ndn::KeyChain m_keyChain;
395 ndn::Name m_defaultIdentity;
396 ndn::Name m_defaultCertName;
akmhoquee1765152014-06-30 11:32:01 -0500397
398 ndn::nfd::Controller m_controller;
399 int32_t m_nFacesToCreate;
400 int32_t m_nFacesCreated;
akmhoque53353462014-04-22 08:43:45 -0500401};
akmhoque298385a2014-02-13 14:13:09 -0600402
akmhoqueb1710aa2014-02-19 17:13:36 -0600403} //namespace nlsr
404
akmhoque53353462014-04-22 08:43:45 -0500405#endif //NLSR_HPP