blob: 19bb3cf23e974c79940c68ac75321d317363dbb8 [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 **/
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050023
akmhoque298385a2014-02-13 14:13:09 -060024#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -060025#include <string>
akmhoque298385a2014-02-13 14:13:09 -060026#include <sstream>
akmhoque05d5fcf2014-04-15 14:58:45 -050027#include <cstdio>
akmhoque0494c252014-07-23 23:46:44 -050028#include <unistd.h>
akmhoque298385a2014-02-13 14:13:09 -060029
30#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050031#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050032#include "logger.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060033
akmhoque298385a2014-02-13 14:13:09 -060034
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
36
akmhoque674b0b12014-05-20 14:33:28 -050037INIT_LOGGER("nlsr");
38
akmhoque53353462014-04-22 08:43:45 -050039using namespace ndn;
40using namespace std;
41
42void
43Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060044{
akmhoquefdbddb12014-05-02 18:35:19 -050045 std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << endl;
46 throw Error("Error: Prefix registration failed");
akmhoque53353462014-04-22 08:43:45 -050047}
akmhoque1fd8c1e2014-02-19 19:41:49 -060048
akmhoque157b0a42014-05-13 00:26:37 -050049void
50Nlsr::onRegistrationSuccess(const ndn::Name& name)
51{
52}
akmhoque1fd8c1e2014-02-19 19:41:49 -060053
akmhoque53353462014-04-22 08:43:45 -050054void
akmhoque31d1d4b2014-05-05 22:08:14 -050055Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050056{
akmhoque31d1d4b2014-05-05 22:08:14 -050057 ndn::Name name(m_confParam.getRouterPrefix());
akmhoque674b0b12014-05-20 14:33:28 -050058 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoquefdbddb12014-05-02 18:35:19 -050059 getNlsrFace().setInterestFilter(name,
akmhoque31d1d4b2014-05-05 22:08:14 -050060 ndn::bind(&HelloProtocol::processInterest,
61 &m_helloProtocol, _1, _2),
akmhoque157b0a42014-05-13 00:26:37 -050062 ndn::bind(&Nlsr::onRegistrationSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -050063 ndn::bind(&Nlsr::registrationFailed, this, _1),
64 m_defaultIdentity,
65 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -050066}
67
68void
69Nlsr::setLsaInterestFilter()
70{
akmhoque157b0a42014-05-13 00:26:37 -050071 ndn::Name name = m_confParam.getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -050072 name.append(m_confParam.getSiteName());
73 name.append(m_confParam.getRouterName());
akmhoque674b0b12014-05-20 14:33:28 -050074 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -050075 getNlsrFace().setInterestFilter(name,
76 ndn::bind(&Lsdb::processInterest,
77 &m_nlsrLsdb, _1, _2),
akmhoque157b0a42014-05-13 00:26:37 -050078 ndn::bind(&Nlsr::onRegistrationSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -050079 ndn::bind(&Nlsr::registrationFailed, this, _1),
80 m_defaultIdentity,
81 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -050082}
83
84void
akmhoquec04e7272014-07-02 11:00:14 -050085Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -050086{
87 std::string strategy("ndn:/localhost/nfd/strategy/broadcast");
akmhoque3cb0cfc2014-06-24 10:32:24 -050088 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
89 broadcastKeyPrefix.append("KEYS");
akmhoque393d4ff2014-07-16 14:27:03 -050090 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
91 m_fib.setStrategy(broadcastKeyPrefix, strategy, 0);
92 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -050093}
94
95void
akmhoque0494c252014-07-23 23:46:44 -050096Nlsr::daemonize()
97{
98 pid_t process_id = 0;
99 pid_t sid = 0;
100 process_id = fork();
101 if (process_id < 0){
102 std::cerr << "Daemonization failed!" << std::endl;
103 throw Error("Error: Daemonization process- fork failed!");
104 }
105 if (process_id > 0) {
106 _LOG_DEBUG("Process daemonized. Process id: " << process_id);
107 exit(0);
108 }
109
110 umask(0);
111 sid = setsid();
112 if(sid < 0) {
113 throw Error("Error: Daemonization process- setting id failed!");
114 }
115
116 if (chdir("/") < 0) {
117 throw Error("Error: Daemonization process-chdir failed!");
118 }
119}
120
121void
akmhoque53353462014-04-22 08:43:45 -0500122Nlsr::initialize()
123{
akmhoque674b0b12014-05-20 14:33:28 -0500124 _LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500125 m_confParam.buildRouterPrefix();
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700126 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500128 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
akmhoquec8a10f72014-04-25 18:42:55 -0500129 m_sequencingManager.setSeqFileName(m_confParam.getSeqFileDir());
130 m_sequencingManager.initiateSeqNoFromFile();
akmhoquee1765152014-06-30 11:32:01 -0500131 m_syncLogicHandler.setSyncPrefix(m_confParam.getChronosyncPrefix().toUri());
akmhoque674b0b12014-05-20 14:33:28 -0500132 /* Logging start */
133 m_confParam.writeLog();
134 m_adjacencyList.writeLog();
135 m_namePrefixList.writeLog();
136 /* Logging end */
akmhoque443ad812014-07-29 10:26:56 -0500137 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500138 setStrategies();
akmhoque060d3022014-08-12 13:35:06 -0500139 _LOG_DEBUG("Default NLSR identity: " << m_defaultIdentity);
akmhoque31d1d4b2014-05-05 22:08:14 -0500140 setInfoInterestFilter();
141 setLsaInterestFilter();
akmhoque674b0b12014-05-20 14:33:28 -0500142 m_nlsrLsdb.buildAndInstallOwnNameLsa();
143 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
Vince Lehman904c2412014-09-23 19:36:11 -0500144
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700145 registerKeyPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500146
147 // Set event intervals
148 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
149 m_helloProtocol.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
150 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
151
152 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500153
154 // Need to set direct neighbors' costs to 0 for hyperbolic routing
155 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
156
157 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
158
159 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
160 it->setLinkCost(0);
161 }
162 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700163}
164
165void
akmhoque443ad812014-07-29 10:26:56 -0500166Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700167{
168 m_defaultIdentity = m_confParam.getRouterPrefix();
169 m_defaultIdentity.append("NLSR");
170
akmhoque102aea42014-08-04 10:22:12 -0500171 try
172 {
173 m_keyChain.deleteIdentity(m_defaultIdentity);
174 }
175 catch (std::exception& e)
176 {
177 }
akmhoque443ad812014-07-29 10:26:56 -0500178
Yingdi Yu9a18bfb2014-06-19 14:06:21 -0700179 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(m_defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700180
Yingdi Yu191f5fa2014-08-06 17:08:52 -0700181 ndn::shared_ptr<ndn::IdentityCertificate> certificate =
182 ndn::make_shared<ndn::IdentityCertificate>();
183 ndn::shared_ptr<ndn::PublicKey> pubKey = m_keyChain.getPublicKey(keyName);
184 Name certificateName = keyName.getPrefix(-1);
185 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
186 certificate->setName(certificateName);
187 certificate->setNotBefore(time::system_clock::now() - time::days(1));
188 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
189 certificate->setPublicKeyInfo(*pubKey);
190 certificate->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
191 keyName.toUri()));
192 certificate->encode();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700193 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
194
195 m_keyChain.addCertificateAsIdentityDefault(*certificate);
196 loadCertToPublish(certificate);
197
198 m_defaultCertName = certificate->getName();
199}
200
201void
202Nlsr::registerKeyPrefix()
203{
204 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
205 keyPrefix.append("KEYS");
206 m_nlsrFace.setInterestFilter(keyPrefix,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700207 ndn::bind(&Nlsr::onKeyInterest,
208 this, _1, _2),
209 ndn::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -0500210 ndn::bind(&Nlsr::registrationFailed, this, _1),
211 m_defaultIdentity,
212 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700213
214}
215
216void
217Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
218{
219 const ndn::Name& interestName = interest.getName();
220
221 ndn::Name certName = interestName.getSubName(name.size());
222
223 if (certName[-2].toUri() == "ID-CERT")
224 {
225 certName = certName.getPrefix(-1);
226 }
227 else if (certName[-1].toUri() != "ID-CERT")
228 return; //Wrong key interest.
229
230 ndn::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
231
232 if (!static_cast<bool>(cert))
233 return; // cert is not found
234
akmhoque69c9aa92014-07-23 15:15:05 -0500235 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
236 data->setName(interestName);
237 data->setContent(cert->wireEncode());
238 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700239
akmhoque69c9aa92014-07-23 15:15:05 -0500240 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700241}
242
243void
244Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
245{
akmhoque53353462014-04-22 08:43:45 -0500246}
akmhoque5a44dd42014-03-12 18:11:32 -0500247
akmhoque53353462014-04-22 08:43:45 -0500248void
akmhoquee1765152014-06-30 11:32:01 -0500249Nlsr::onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
250{
akmhoquee1765152014-06-30 11:32:01 -0500251}
252
253void
254Nlsr::onDestroyFaceFailure(int32_t code, const std::string& error)
255{
256 std::cerr << error << " (code: " << code << ")";
257 throw Error("Error: Face destruction failed");
258}
259
260void
261Nlsr::destroyFaces()
262{
263 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
264 for (std::list<Adjacent>::iterator it = adjacents.begin();
265 it != adjacents.end(); it++) {
akmhoquec04e7272014-07-02 11:00:14 -0500266 m_fib.destroyFace((*it).getConnectingFaceUri(),
267 ndn::bind(&Nlsr::onDestroyFaceSuccess, this, _1),
268 ndn::bind(&Nlsr::onDestroyFaceFailure, this, _1, _2));
akmhoquee1765152014-06-30 11:32:01 -0500269 }
270}
271
akmhoquec04e7272014-07-02 11:00:14 -0500272
273
akmhoquee1765152014-06-30 11:32:01 -0500274
275void
akmhoquec04e7272014-07-02 11:00:14 -0500276Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500277{
akmhoquec04e7272014-07-02 11:00:14 -0500278 ndn::nfd::FaceEventKind kind = faceEventNotification.getKind();
akmhoque060d3022014-08-12 13:35:06 -0500279 _LOG_DEBUG("Nlsr::onFaceEventNotification called");
akmhoquec04e7272014-07-02 11:00:14 -0500280 if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
281 uint64_t faceId = faceEventNotification.getFaceId();
282 Adjacent *adjacent = m_adjacencyList.findAdjacent(faceId);
283 if (adjacent != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500284 _LOG_DEBUG("Face to " << adjacent->getName() << " with face id: "
285 << faceId << " deleted");
akmhoquec04e7272014-07-02 11:00:14 -0500286 adjacent->setFaceId(0);
287 }
288 }
akmhoquee1765152014-06-30 11:32:01 -0500289}
290
291
292void
akmhoque53353462014-04-22 08:43:45 -0500293Nlsr::startEventLoop()
294{
akmhoquefdbddb12014-05-02 18:35:19 -0500295 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500296}
akmhoque5a44dd42014-03-12 18:11:32 -0500297
akmhoquefdbddb12014-05-02 18:35:19 -0500298void
akmhoque53353462014-04-22 08:43:45 -0500299Nlsr::usage(const string& progname)
300{
akmhoque48265992014-08-10 08:27:13 -0500301 std::cout << "Usage: " << progname << " [OPTIONS...]" << std::endl;
302 std::cout << " NDN routing...." << std::endl;
Vince Lehmanb722b102014-08-24 16:33:49 -0500303 std::cout << " -d Run in daemon mode" << std::endl;
304 std::cout << " -f <FILE> Specify configuration file name" << std::endl;
305 std::cout << " -V Display version information" << std::endl;
306 std::cout << " -h Display this help message" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500307}
akmhoque298385a2014-02-13 14:13:09 -0600308
akmhoqueb1710aa2014-02-19 17:13:36 -0600309
310} // namespace nlsr