blob: 930dc330946a15bfb1e4f6c1d3c2acea790603e8 [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#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -060024#include <string>
akmhoque298385a2014-02-13 14:13:09 -060025#include <sstream>
akmhoque05d5fcf2014-04-15 14:58:45 -050026#include <cstdio>
akmhoque0494c252014-07-23 23:46:44 -050027#include <unistd.h>
akmhoque298385a2014-02-13 14:13:09 -060028
29#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050030#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050031#include "logger.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060032
akmhoque298385a2014-02-13 14:13:09 -060033
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
35
akmhoque674b0b12014-05-20 14:33:28 -050036INIT_LOGGER("nlsr");
37
akmhoque53353462014-04-22 08:43:45 -050038using namespace ndn;
39using namespace std;
40
41void
42Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060043{
akmhoquefdbddb12014-05-02 18:35:19 -050044 std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << endl;
45 throw Error("Error: Prefix registration failed");
akmhoque53353462014-04-22 08:43:45 -050046}
akmhoque1fd8c1e2014-02-19 19:41:49 -060047
akmhoque157b0a42014-05-13 00:26:37 -050048void
49Nlsr::onRegistrationSuccess(const ndn::Name& name)
50{
51}
akmhoque1fd8c1e2014-02-19 19:41:49 -060052
akmhoque53353462014-04-22 08:43:45 -050053void
akmhoque31d1d4b2014-05-05 22:08:14 -050054Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050055{
akmhoque31d1d4b2014-05-05 22:08:14 -050056 ndn::Name name(m_confParam.getRouterPrefix());
akmhoque674b0b12014-05-20 14:33:28 -050057 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoquefdbddb12014-05-02 18:35:19 -050058 getNlsrFace().setInterestFilter(name,
akmhoque31d1d4b2014-05-05 22:08:14 -050059 ndn::bind(&HelloProtocol::processInterest,
60 &m_helloProtocol, _1, _2),
akmhoque157b0a42014-05-13 00:26:37 -050061 ndn::bind(&Nlsr::onRegistrationSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -050062 ndn::bind(&Nlsr::registrationFailed, this, _1),
63 m_defaultIdentity,
64 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -050065}
66
67void
68Nlsr::setLsaInterestFilter()
69{
akmhoque157b0a42014-05-13 00:26:37 -050070 ndn::Name name = m_confParam.getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -050071 name.append(m_confParam.getSiteName());
72 name.append(m_confParam.getRouterName());
akmhoque674b0b12014-05-20 14:33:28 -050073 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -050074 getNlsrFace().setInterestFilter(name,
75 ndn::bind(&Lsdb::processInterest,
76 &m_nlsrLsdb, _1, _2),
akmhoque157b0a42014-05-13 00:26:37 -050077 ndn::bind(&Nlsr::onRegistrationSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -050078 ndn::bind(&Nlsr::registrationFailed, this, _1),
79 m_defaultIdentity,
80 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -050081}
82
83void
akmhoquec04e7272014-07-02 11:00:14 -050084Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -050085{
86 std::string strategy("ndn:/localhost/nfd/strategy/broadcast");
akmhoque3cb0cfc2014-06-24 10:32:24 -050087 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
88 broadcastKeyPrefix.append("KEYS");
akmhoque393d4ff2014-07-16 14:27:03 -050089 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
90 m_fib.setStrategy(broadcastKeyPrefix, strategy, 0);
91 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -050092}
93
94void
akmhoque0494c252014-07-23 23:46:44 -050095Nlsr::daemonize()
96{
97 pid_t process_id = 0;
98 pid_t sid = 0;
99 process_id = fork();
100 if (process_id < 0){
101 std::cerr << "Daemonization failed!" << std::endl;
102 throw Error("Error: Daemonization process- fork failed!");
103 }
104 if (process_id > 0) {
105 _LOG_DEBUG("Process daemonized. Process id: " << process_id);
106 exit(0);
107 }
108
109 umask(0);
110 sid = setsid();
111 if(sid < 0) {
112 throw Error("Error: Daemonization process- setting id failed!");
113 }
114
115 if (chdir("/") < 0) {
116 throw Error("Error: Daemonization process-chdir failed!");
117 }
118}
119
120void
akmhoque53353462014-04-22 08:43:45 -0500121Nlsr::initialize()
122{
akmhoque674b0b12014-05-20 14:33:28 -0500123 _LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500124 m_confParam.buildRouterPrefix();
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700125 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500127 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
akmhoquec8a10f72014-04-25 18:42:55 -0500128 m_sequencingManager.setSeqFileName(m_confParam.getSeqFileDir());
129 m_sequencingManager.initiateSeqNoFromFile();
akmhoquee1765152014-06-30 11:32:01 -0500130 m_syncLogicHandler.setSyncPrefix(m_confParam.getChronosyncPrefix().toUri());
akmhoque674b0b12014-05-20 14:33:28 -0500131 /* Logging start */
132 m_confParam.writeLog();
133 m_adjacencyList.writeLog();
134 m_namePrefixList.writeLog();
135 /* Logging end */
akmhoque443ad812014-07-29 10:26:56 -0500136 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500137 setStrategies();
akmhoque060d3022014-08-12 13:35:06 -0500138 _LOG_DEBUG("Default NLSR identity: " << m_defaultIdentity);
akmhoque31d1d4b2014-05-05 22:08:14 -0500139 setInfoInterestFilter();
140 setLsaInterestFilter();
akmhoque674b0b12014-05-20 14:33:28 -0500141 m_nlsrLsdb.buildAndInstallOwnNameLsa();
142 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
Vince Lehman904c2412014-09-23 19:36:11 -0500143
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700144 registerKeyPrefix();
akmhoquee1765152014-06-30 11:32:01 -0500145 m_helloProtocol.scheduleInterest(10);
Vince Lehman09131122014-09-09 17:10:11 -0500146
147 // Need to set direct neighbors' costs to 0 for hyperbolic routing
148 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
149
150 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
151
152 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
153 it->setLinkCost(0);
154 }
155 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700156}
157
158void
akmhoque443ad812014-07-29 10:26:56 -0500159Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700160{
161 m_defaultIdentity = m_confParam.getRouterPrefix();
162 m_defaultIdentity.append("NLSR");
163
akmhoque102aea42014-08-04 10:22:12 -0500164 try
165 {
166 m_keyChain.deleteIdentity(m_defaultIdentity);
167 }
168 catch (std::exception& e)
169 {
170 }
akmhoque443ad812014-07-29 10:26:56 -0500171
Yingdi Yu9a18bfb2014-06-19 14:06:21 -0700172 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(m_defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700173
Yingdi Yu191f5fa2014-08-06 17:08:52 -0700174 ndn::shared_ptr<ndn::IdentityCertificate> certificate =
175 ndn::make_shared<ndn::IdentityCertificate>();
176 ndn::shared_ptr<ndn::PublicKey> pubKey = m_keyChain.getPublicKey(keyName);
177 Name certificateName = keyName.getPrefix(-1);
178 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
179 certificate->setName(certificateName);
180 certificate->setNotBefore(time::system_clock::now() - time::days(1));
181 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
182 certificate->setPublicKeyInfo(*pubKey);
183 certificate->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
184 keyName.toUri()));
185 certificate->encode();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700186 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
187
188 m_keyChain.addCertificateAsIdentityDefault(*certificate);
189 loadCertToPublish(certificate);
190
191 m_defaultCertName = certificate->getName();
192}
193
194void
195Nlsr::registerKeyPrefix()
196{
197 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
198 keyPrefix.append("KEYS");
199 m_nlsrFace.setInterestFilter(keyPrefix,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700200 ndn::bind(&Nlsr::onKeyInterest,
201 this, _1, _2),
202 ndn::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -0500203 ndn::bind(&Nlsr::registrationFailed, this, _1),
204 m_defaultIdentity,
205 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700206
207}
208
209void
210Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
211{
212 const ndn::Name& interestName = interest.getName();
213
214 ndn::Name certName = interestName.getSubName(name.size());
215
216 if (certName[-2].toUri() == "ID-CERT")
217 {
218 certName = certName.getPrefix(-1);
219 }
220 else if (certName[-1].toUri() != "ID-CERT")
221 return; //Wrong key interest.
222
223 ndn::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
224
225 if (!static_cast<bool>(cert))
226 return; // cert is not found
227
akmhoque69c9aa92014-07-23 15:15:05 -0500228 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
229 data->setName(interestName);
230 data->setContent(cert->wireEncode());
231 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700232
akmhoque69c9aa92014-07-23 15:15:05 -0500233 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700234}
235
236void
237Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
238{
akmhoque53353462014-04-22 08:43:45 -0500239}
akmhoque5a44dd42014-03-12 18:11:32 -0500240
akmhoque53353462014-04-22 08:43:45 -0500241void
akmhoquee1765152014-06-30 11:32:01 -0500242Nlsr::onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
243{
akmhoquee1765152014-06-30 11:32:01 -0500244}
245
246void
247Nlsr::onDestroyFaceFailure(int32_t code, const std::string& error)
248{
249 std::cerr << error << " (code: " << code << ")";
250 throw Error("Error: Face destruction failed");
251}
252
253void
254Nlsr::destroyFaces()
255{
256 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
257 for (std::list<Adjacent>::iterator it = adjacents.begin();
258 it != adjacents.end(); it++) {
akmhoquec04e7272014-07-02 11:00:14 -0500259 m_fib.destroyFace((*it).getConnectingFaceUri(),
260 ndn::bind(&Nlsr::onDestroyFaceSuccess, this, _1),
261 ndn::bind(&Nlsr::onDestroyFaceFailure, this, _1, _2));
akmhoquee1765152014-06-30 11:32:01 -0500262 }
263}
264
akmhoquec04e7272014-07-02 11:00:14 -0500265
266
akmhoquee1765152014-06-30 11:32:01 -0500267
268void
akmhoquec04e7272014-07-02 11:00:14 -0500269Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500270{
akmhoquec04e7272014-07-02 11:00:14 -0500271 ndn::nfd::FaceEventKind kind = faceEventNotification.getKind();
akmhoque060d3022014-08-12 13:35:06 -0500272 _LOG_DEBUG("Nlsr::onFaceEventNotification called");
akmhoquec04e7272014-07-02 11:00:14 -0500273 if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
274 uint64_t faceId = faceEventNotification.getFaceId();
275 Adjacent *adjacent = m_adjacencyList.findAdjacent(faceId);
276 if (adjacent != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500277 _LOG_DEBUG("Face to " << adjacent->getName() << " with face id: "
278 << faceId << " deleted");
akmhoquec04e7272014-07-02 11:00:14 -0500279 adjacent->setFaceId(0);
280 }
281 }
akmhoquee1765152014-06-30 11:32:01 -0500282}
283
284
285void
akmhoque53353462014-04-22 08:43:45 -0500286Nlsr::startEventLoop()
287{
akmhoquefdbddb12014-05-02 18:35:19 -0500288 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500289}
akmhoque5a44dd42014-03-12 18:11:32 -0500290
akmhoquefdbddb12014-05-02 18:35:19 -0500291void
akmhoque53353462014-04-22 08:43:45 -0500292Nlsr::usage(const string& progname)
293{
akmhoque48265992014-08-10 08:27:13 -0500294 std::cout << "Usage: " << progname << " [OPTIONS...]" << std::endl;
295 std::cout << " NDN routing...." << std::endl;
Vince Lehmanb722b102014-08-24 16:33:49 -0500296 std::cout << " -d Run in daemon mode" << std::endl;
297 std::cout << " -f <FILE> Specify configuration file name" << std::endl;
298 std::cout << " -V Display version information" << std::endl;
299 std::cout << " -h Display this help message" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500300}
akmhoque298385a2014-02-13 14:13:09 -0600301
akmhoqueb1710aa2014-02-19 17:13:36 -0600302
303} // namespace nlsr