blob: e4e37fb7f1af0a51b2e3a6c84120b170063d6ef7 [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);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700146}
147
148void
akmhoque443ad812014-07-29 10:26:56 -0500149Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700150{
151 m_defaultIdentity = m_confParam.getRouterPrefix();
152 m_defaultIdentity.append("NLSR");
153
akmhoque102aea42014-08-04 10:22:12 -0500154 try
155 {
156 m_keyChain.deleteIdentity(m_defaultIdentity);
157 }
158 catch (std::exception& e)
159 {
160 }
akmhoque443ad812014-07-29 10:26:56 -0500161
Yingdi Yu9a18bfb2014-06-19 14:06:21 -0700162 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(m_defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700163
Yingdi Yu191f5fa2014-08-06 17:08:52 -0700164 ndn::shared_ptr<ndn::IdentityCertificate> certificate =
165 ndn::make_shared<ndn::IdentityCertificate>();
166 ndn::shared_ptr<ndn::PublicKey> pubKey = m_keyChain.getPublicKey(keyName);
167 Name certificateName = keyName.getPrefix(-1);
168 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
169 certificate->setName(certificateName);
170 certificate->setNotBefore(time::system_clock::now() - time::days(1));
171 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
172 certificate->setPublicKeyInfo(*pubKey);
173 certificate->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
174 keyName.toUri()));
175 certificate->encode();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700176 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
177
178 m_keyChain.addCertificateAsIdentityDefault(*certificate);
179 loadCertToPublish(certificate);
180
181 m_defaultCertName = certificate->getName();
182}
183
184void
185Nlsr::registerKeyPrefix()
186{
187 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
188 keyPrefix.append("KEYS");
189 m_nlsrFace.setInterestFilter(keyPrefix,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700190 ndn::bind(&Nlsr::onKeyInterest,
191 this, _1, _2),
192 ndn::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -0500193 ndn::bind(&Nlsr::registrationFailed, this, _1),
194 m_defaultIdentity,
195 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700196
197}
198
199void
200Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
201{
202 const ndn::Name& interestName = interest.getName();
203
204 ndn::Name certName = interestName.getSubName(name.size());
205
206 if (certName[-2].toUri() == "ID-CERT")
207 {
208 certName = certName.getPrefix(-1);
209 }
210 else if (certName[-1].toUri() != "ID-CERT")
211 return; //Wrong key interest.
212
213 ndn::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
214
215 if (!static_cast<bool>(cert))
216 return; // cert is not found
217
akmhoque69c9aa92014-07-23 15:15:05 -0500218 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
219 data->setName(interestName);
220 data->setContent(cert->wireEncode());
221 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700222
akmhoque69c9aa92014-07-23 15:15:05 -0500223 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700224}
225
226void
227Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
228{
akmhoque53353462014-04-22 08:43:45 -0500229}
akmhoque5a44dd42014-03-12 18:11:32 -0500230
akmhoque53353462014-04-22 08:43:45 -0500231void
akmhoquee1765152014-06-30 11:32:01 -0500232Nlsr::onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
233{
akmhoquee1765152014-06-30 11:32:01 -0500234}
235
236void
237Nlsr::onDestroyFaceFailure(int32_t code, const std::string& error)
238{
239 std::cerr << error << " (code: " << code << ")";
240 throw Error("Error: Face destruction failed");
241}
242
243void
244Nlsr::destroyFaces()
245{
246 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
247 for (std::list<Adjacent>::iterator it = adjacents.begin();
248 it != adjacents.end(); it++) {
akmhoquec04e7272014-07-02 11:00:14 -0500249 m_fib.destroyFace((*it).getConnectingFaceUri(),
250 ndn::bind(&Nlsr::onDestroyFaceSuccess, this, _1),
251 ndn::bind(&Nlsr::onDestroyFaceFailure, this, _1, _2));
akmhoquee1765152014-06-30 11:32:01 -0500252 }
253}
254
akmhoquec04e7272014-07-02 11:00:14 -0500255
256
akmhoquee1765152014-06-30 11:32:01 -0500257
258void
akmhoquec04e7272014-07-02 11:00:14 -0500259Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500260{
akmhoquec04e7272014-07-02 11:00:14 -0500261 ndn::nfd::FaceEventKind kind = faceEventNotification.getKind();
akmhoque060d3022014-08-12 13:35:06 -0500262 _LOG_DEBUG("Nlsr::onFaceEventNotification called");
akmhoquec04e7272014-07-02 11:00:14 -0500263 if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
264 uint64_t faceId = faceEventNotification.getFaceId();
265 Adjacent *adjacent = m_adjacencyList.findAdjacent(faceId);
266 if (adjacent != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500267 _LOG_DEBUG("Face to " << adjacent->getName() << " with face id: "
268 << faceId << " deleted");
akmhoquec04e7272014-07-02 11:00:14 -0500269 adjacent->setFaceId(0);
270 }
271 }
akmhoquee1765152014-06-30 11:32:01 -0500272}
273
274
275void
akmhoque53353462014-04-22 08:43:45 -0500276Nlsr::startEventLoop()
277{
akmhoquefdbddb12014-05-02 18:35:19 -0500278 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500279}
akmhoque5a44dd42014-03-12 18:11:32 -0500280
akmhoquefdbddb12014-05-02 18:35:19 -0500281void
akmhoque53353462014-04-22 08:43:45 -0500282Nlsr::usage(const string& progname)
283{
akmhoque48265992014-08-10 08:27:13 -0500284 std::cout << "Usage: " << progname << " [OPTIONS...]" << std::endl;
285 std::cout << " NDN routing...." << std::endl;
Vince Lehmanb722b102014-08-24 16:33:49 -0500286 std::cout << " -d Run in daemon mode" << std::endl;
287 std::cout << " -f <FILE> Specify configuration file name" << std::endl;
288 std::cout << " -V Display version information" << std::endl;
289 std::cout << " -h Display this help message" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500290}
akmhoque298385a2014-02-13 14:13:09 -0600291
akmhoqueb1710aa2014-02-19 17:13:36 -0600292
293} // namespace nlsr