blob: b0d5126d5def009e053da0dd7551473f1d6089ed [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:
65 Nlsr()
akmhoquefdbddb12014-05-02 18:35:19 -050066 : m_scheduler(m_nlsrFace.getIoService())
akmhoque53353462014-04-22 08:43:45 -050067 , m_confParam()
akmhoquec8a10f72014-04-25 18:42:55 -050068 , m_adjacencyList()
69 , m_namePrefixList()
akmhoquec8a10f72014-04-25 18:42:55 -050070 , m_sequencingManager()
akmhoque53353462014-04-22 08:43:45 -050071 , m_isDaemonProcess(false)
72 , m_configFileName("nlsr.conf")
akmhoque31d1d4b2014-05-05 22:08:14 -050073 , m_nlsrLsdb(*this)
akmhoque53353462014-04-22 08:43:45 -050074 , m_adjBuildCount(0)
75 , m_isBuildAdjLsaSheduled(false)
76 , m_isRouteCalculationScheduled(false)
77 , m_isRoutingTableCalculating(false)
78 , m_routingTable()
akmhoque31d1d4b2014-05-05 22:08:14 -050079 , m_fib(*this, m_nlsrFace)
80 , m_namePrefixTable(*this)
akmhoquefdbddb12014-05-02 18:35:19 -050081 , m_syncLogicHandler(m_nlsrFace.getIoService())
akmhoque31d1d4b2014-05-05 22:08:14 -050082 , m_helloProtocol(*this)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070083
84 , m_certificateCache(new ndn::CertificateCacheTtl(m_nlsrFace.getIoService()))
85 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache)
akmhoque53353462014-04-22 08:43:45 -050086 {}
akmhoque298385a2014-02-13 14:13:09 -060087
akmhoque53353462014-04-22 08:43:45 -050088 void
89 registrationFailed(const ndn::Name& name);
90
91 void
akmhoque157b0a42014-05-13 00:26:37 -050092 onRegistrationSuccess(const ndn::Name& name);
93
94 void
akmhoque31d1d4b2014-05-05 22:08:14 -050095 setInfoInterestFilter();
96
97 void
98 setLsaInterestFilter();
akmhoque53353462014-04-22 08:43:45 -050099
100 void
101 startEventLoop();
102
akmhoquefdbddb12014-05-02 18:35:19 -0500103 void
104 usage(const std::string& progname);
akmhoque53353462014-04-22 08:43:45 -0500105
106 std::string
akmhoquefdbddb12014-05-02 18:35:19 -0500107 getConfFileName() const
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700108 {
akmhoque53353462014-04-22 08:43:45 -0500109 return m_configFileName;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700110 }
111
akmhoque53353462014-04-22 08:43:45 -0500112 void
akmhoquefdbddb12014-05-02 18:35:19 -0500113 setConfFileName(const std::string& fileName)
akmhoque5a44dd42014-03-12 18:11:32 -0500114 {
akmhoque53353462014-04-22 08:43:45 -0500115 m_configFileName = fileName;
116 }
akmhoque5a44dd42014-03-12 18:11:32 -0500117
akmhoque53353462014-04-22 08:43:45 -0500118 bool
119 getIsSetDaemonProcess()
120 {
121 return m_isDaemonProcess;
122 }
akmhoque5a44dd42014-03-12 18:11:32 -0500123
akmhoque53353462014-04-22 08:43:45 -0500124 void
125 setIsDaemonProcess(bool value)
126 {
127 m_isDaemonProcess = value;
128 }
akmhoque5a44dd42014-03-12 18:11:32 -0500129
akmhoque53353462014-04-22 08:43:45 -0500130 ConfParameter&
131 getConfParameter()
132 {
133 return m_confParam;
134 }
akmhoque5a44dd42014-03-12 18:11:32 -0500135
akmhoquec8a10f72014-04-25 18:42:55 -0500136 AdjacencyList&
137 getAdjacencyList()
akmhoque53353462014-04-22 08:43:45 -0500138 {
akmhoquec8a10f72014-04-25 18:42:55 -0500139 return m_adjacencyList;
akmhoque53353462014-04-22 08:43:45 -0500140 }
akmhoque298385a2014-02-13 14:13:09 -0600141
akmhoquec8a10f72014-04-25 18:42:55 -0500142 NamePrefixList&
143 getNamePrefixList()
akmhoque53353462014-04-22 08:43:45 -0500144 {
akmhoquec8a10f72014-04-25 18:42:55 -0500145 return m_namePrefixList;
akmhoque53353462014-04-22 08:43:45 -0500146 }
akmhoque298385a2014-02-13 14:13:09 -0600147
akmhoque53353462014-04-22 08:43:45 -0500148 ndn::Scheduler&
149 getScheduler()
150 {
151 return m_scheduler;
152 }
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
akmhoque298385a2014-02-13 14:13:09 -0600220
akmhoque53353462014-04-22 08:43:45 -0500221 void
akmhoquefdbddb12014-05-02 18:35:19 -0500222 setApiPort(int32_t ap)
akmhoque53353462014-04-22 08:43:45 -0500223 {
224 m_apiPort = ap;
225 }
akmhoque298385a2014-02-13 14:13:09 -0600226
akmhoquefdbddb12014-05-02 18:35:19 -0500227 int32_t
akmhoque53353462014-04-22 08:43:45 -0500228 getApiPort()
229 {
230 return m_apiPort;
231 }
akmhoque298385a2014-02-13 14:13:09 -0600232
akmhoque53353462014-04-22 08:43:45 -0500233 bool
234 getIsRoutingTableCalculating()
235 {
236 return m_isRoutingTableCalculating;
237 }
akmhoque85d88332014-02-17 21:11:21 -0600238
akmhoque53353462014-04-22 08:43:45 -0500239 void
240 setIsRoutingTableCalculating(bool irtc)
241 {
242 m_isRoutingTableCalculating = irtc;
243 }
akmhoque298385a2014-02-13 14:13:09 -0600244
akmhoque53353462014-04-22 08:43:45 -0500245 bool
246 getIsRouteCalculationScheduled()
247 {
248 return m_isRouteCalculationScheduled;
249 }
akmhoque298385a2014-02-13 14:13:09 -0600250
akmhoque53353462014-04-22 08:43:45 -0500251 void
252 setIsRouteCalculationScheduled(bool ircs)
253 {
254 m_isRouteCalculationScheduled = ircs;
255 }
akmhoque298385a2014-02-13 14:13:09 -0600256
akmhoque53353462014-04-22 08:43:45 -0500257 SyncLogicHandler&
akmhoquec8a10f72014-04-25 18:42:55 -0500258 getSyncLogicHandler()
akmhoque53353462014-04-22 08:43:45 -0500259 {
akmhoquec8a10f72014-04-25 18:42:55 -0500260 return m_syncLogicHandler;
akmhoque53353462014-04-22 08:43:45 -0500261 }
akmhoque2bb198e2014-02-28 11:46:27 -0600262
akmhoque53353462014-04-22 08:43:45 -0500263 void
264 initialize();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600265
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700266 void
267 intializeKey();
268
269 void
270 loadValidator(boost::property_tree::ptree section,
271 const std::string& filename)
272 {
273 m_validator.load(section, filename);
274 }
275
276 Validator&
277 getValidator()
278 {
279 return m_validator;
280 }
281
282 void
283 loadCertToPublish(ndn::shared_ptr<ndn::IdentityCertificate> certificate)
284 {
285 if (static_cast<bool>(certificate))
286 m_certToPublish[certificate->getName().getPrefix(-1)] = certificate; // key is cert name
287 // without version
288 }
289
290 ndn::shared_ptr<const ndn::IdentityCertificate>
291 getCertificate(const ndn::Name& certificateNameWithoutVersion)
292 {
293 CertMap::iterator it = m_certToPublish.find(certificateNameWithoutVersion);
294
295 if (it != m_certToPublish.end())
296 {
297 return it->second;
298 }
299
300 return m_certificateCache->getCertificate(certificateNameWithoutVersion);
301 }
302
303 ndn::KeyChain&
304 getKeyChain()
305 {
306 return m_keyChain;
307 }
308
309 const ndn::Name&
310 getDefaultCertName()
311 {
312 return m_defaultCertName;
313 }
314
akmhoque53353462014-04-22 08:43:45 -0500315private:
akmhoque157b0a42014-05-13 00:26:37 -0500316 void
317 registerPrefixes();
318
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700319 void
320 registerKeyPrefix();
321
322 void
323 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest);
324
325 void
326 onKeyPrefixRegSuccess(const ndn::Name& name);
327
akmhoque157b0a42014-05-13 00:26:37 -0500328private:
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700329 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > CertMap;
330
akmhoquefdbddb12014-05-02 18:35:19 -0500331 ndn::Face m_nlsrFace;
akmhoque53353462014-04-22 08:43:45 -0500332 ndn::Scheduler m_scheduler;
333 ConfParameter m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -0500334 AdjacencyList m_adjacencyList;
335 NamePrefixList m_namePrefixList;
akmhoquec8a10f72014-04-25 18:42:55 -0500336 SequencingManager m_sequencingManager;
akmhoque53353462014-04-22 08:43:45 -0500337 bool m_isDaemonProcess;
akmhoquefdbddb12014-05-02 18:35:19 -0500338 std::string m_configFileName;
akmhoque53353462014-04-22 08:43:45 -0500339 Lsdb m_nlsrLsdb;
akmhoquefdbddb12014-05-02 18:35:19 -0500340 int64_t m_adjBuildCount;
akmhoque53353462014-04-22 08:43:45 -0500341 bool m_isBuildAdjLsaSheduled;
342 bool m_isRouteCalculationScheduled;
343 bool m_isRoutingTableCalculating;
akmhoque53353462014-04-22 08:43:45 -0500344 RoutingTable m_routingTable;
akmhoque53353462014-04-22 08:43:45 -0500345 Fib m_fib;
akmhoque31d1d4b2014-05-05 22:08:14 -0500346 NamePrefixTable m_namePrefixTable;
akmhoquec8a10f72014-04-25 18:42:55 -0500347 SyncLogicHandler m_syncLogicHandler;
akmhoquefdbddb12014-05-02 18:35:19 -0500348 int32_t m_apiPort;
akmhoque31d1d4b2014-05-05 22:08:14 -0500349 HelloProtocol m_helloProtocol;
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700350
351 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
352 CertMap m_certToPublish;
353 Validator m_validator;
354 ndn::KeyChain m_keyChain;
355 ndn::Name m_defaultIdentity;
356 ndn::Name m_defaultCertName;
akmhoque53353462014-04-22 08:43:45 -0500357};
akmhoque298385a2014-02-13 14:13:09 -0600358
akmhoqueb1710aa2014-02-19 17:13:36 -0600359} //namespace nlsr
360
akmhoque53353462014-04-22 08:43:45 -0500361#endif //NLSR_HPP