blob: cf26a7bedd24a149fa860726ade97f5c0045fba9 [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();
Vince Lehmanc11cc202015-01-20 11:41:33 -0600131
akmhoquee1765152014-06-30 11:32:01 -0500132 m_syncLogicHandler.setSyncPrefix(m_confParam.getChronosyncPrefix().toUri());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600133 m_syncLogicHandler.buildUpdatePrefix();
134
akmhoque674b0b12014-05-20 14:33:28 -0500135 /* Logging start */
136 m_confParam.writeLog();
137 m_adjacencyList.writeLog();
138 m_namePrefixList.writeLog();
139 /* Logging end */
akmhoque443ad812014-07-29 10:26:56 -0500140 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500141 setStrategies();
akmhoque060d3022014-08-12 13:35:06 -0500142 _LOG_DEBUG("Default NLSR identity: " << m_defaultIdentity);
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 setInfoInterestFilter();
144 setLsaInterestFilter();
akmhoque674b0b12014-05-20 14:33:28 -0500145 m_nlsrLsdb.buildAndInstallOwnNameLsa();
146 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
Vince Lehman904c2412014-09-23 19:36:11 -0500147
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700148 registerKeyPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500149
150 // Set event intervals
151 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
152 m_helloProtocol.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
153 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
154
155 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500156
157 // Need to set direct neighbors' costs to 0 for hyperbolic routing
158 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
159
160 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
161
162 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
163 it->setLinkCost(0);
164 }
165 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700166}
167
168void
akmhoque443ad812014-07-29 10:26:56 -0500169Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700170{
171 m_defaultIdentity = m_confParam.getRouterPrefix();
172 m_defaultIdentity.append("NLSR");
173
akmhoque102aea42014-08-04 10:22:12 -0500174 try
175 {
176 m_keyChain.deleteIdentity(m_defaultIdentity);
177 }
178 catch (std::exception& e)
179 {
180 }
akmhoque443ad812014-07-29 10:26:56 -0500181
Yingdi Yu9a18bfb2014-06-19 14:06:21 -0700182 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(m_defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700183
Yingdi Yu191f5fa2014-08-06 17:08:52 -0700184 ndn::shared_ptr<ndn::IdentityCertificate> certificate =
185 ndn::make_shared<ndn::IdentityCertificate>();
186 ndn::shared_ptr<ndn::PublicKey> pubKey = m_keyChain.getPublicKey(keyName);
187 Name certificateName = keyName.getPrefix(-1);
188 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
189 certificate->setName(certificateName);
190 certificate->setNotBefore(time::system_clock::now() - time::days(1));
191 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
192 certificate->setPublicKeyInfo(*pubKey);
193 certificate->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
194 keyName.toUri()));
195 certificate->encode();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700196 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
197
198 m_keyChain.addCertificateAsIdentityDefault(*certificate);
199 loadCertToPublish(certificate);
200
201 m_defaultCertName = certificate->getName();
202}
203
204void
205Nlsr::registerKeyPrefix()
206{
207 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
208 keyPrefix.append("KEYS");
209 m_nlsrFace.setInterestFilter(keyPrefix,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700210 ndn::bind(&Nlsr::onKeyInterest,
211 this, _1, _2),
212 ndn::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -0500213 ndn::bind(&Nlsr::registrationFailed, this, _1),
214 m_defaultIdentity,
215 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700216
217}
218
219void
220Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
221{
222 const ndn::Name& interestName = interest.getName();
223
224 ndn::Name certName = interestName.getSubName(name.size());
225
226 if (certName[-2].toUri() == "ID-CERT")
227 {
228 certName = certName.getPrefix(-1);
229 }
230 else if (certName[-1].toUri() != "ID-CERT")
231 return; //Wrong key interest.
232
233 ndn::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
234
235 if (!static_cast<bool>(cert))
236 return; // cert is not found
237
akmhoque69c9aa92014-07-23 15:15:05 -0500238 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
239 data->setName(interestName);
240 data->setContent(cert->wireEncode());
241 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700242
akmhoque69c9aa92014-07-23 15:15:05 -0500243 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700244}
245
246void
247Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
248{
akmhoque53353462014-04-22 08:43:45 -0500249}
akmhoque5a44dd42014-03-12 18:11:32 -0500250
akmhoque53353462014-04-22 08:43:45 -0500251void
akmhoquee1765152014-06-30 11:32:01 -0500252Nlsr::onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
253{
akmhoquee1765152014-06-30 11:32:01 -0500254}
255
256void
257Nlsr::onDestroyFaceFailure(int32_t code, const std::string& error)
258{
259 std::cerr << error << " (code: " << code << ")";
260 throw Error("Error: Face destruction failed");
261}
262
263void
264Nlsr::destroyFaces()
265{
266 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
267 for (std::list<Adjacent>::iterator it = adjacents.begin();
268 it != adjacents.end(); it++) {
akmhoquec04e7272014-07-02 11:00:14 -0500269 m_fib.destroyFace((*it).getConnectingFaceUri(),
270 ndn::bind(&Nlsr::onDestroyFaceSuccess, this, _1),
271 ndn::bind(&Nlsr::onDestroyFaceFailure, this, _1, _2));
akmhoquee1765152014-06-30 11:32:01 -0500272 }
273}
274
akmhoquec04e7272014-07-02 11:00:14 -0500275
276
akmhoquee1765152014-06-30 11:32:01 -0500277
278void
akmhoquec04e7272014-07-02 11:00:14 -0500279Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500280{
akmhoquec04e7272014-07-02 11:00:14 -0500281 ndn::nfd::FaceEventKind kind = faceEventNotification.getKind();
akmhoque060d3022014-08-12 13:35:06 -0500282 _LOG_DEBUG("Nlsr::onFaceEventNotification called");
akmhoquec04e7272014-07-02 11:00:14 -0500283 if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
284 uint64_t faceId = faceEventNotification.getFaceId();
285 Adjacent *adjacent = m_adjacencyList.findAdjacent(faceId);
286 if (adjacent != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500287 _LOG_DEBUG("Face to " << adjacent->getName() << " with face id: "
288 << faceId << " deleted");
akmhoquec04e7272014-07-02 11:00:14 -0500289 adjacent->setFaceId(0);
290 }
291 }
akmhoquee1765152014-06-30 11:32:01 -0500292}
293
294
295void
akmhoque53353462014-04-22 08:43:45 -0500296Nlsr::startEventLoop()
297{
akmhoquefdbddb12014-05-02 18:35:19 -0500298 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500299}
akmhoque5a44dd42014-03-12 18:11:32 -0500300
akmhoquefdbddb12014-05-02 18:35:19 -0500301void
akmhoque53353462014-04-22 08:43:45 -0500302Nlsr::usage(const string& progname)
303{
akmhoque48265992014-08-10 08:27:13 -0500304 std::cout << "Usage: " << progname << " [OPTIONS...]" << std::endl;
305 std::cout << " NDN routing...." << std::endl;
Vince Lehmanb722b102014-08-24 16:33:49 -0500306 std::cout << " -d Run in daemon mode" << std::endl;
307 std::cout << " -f <FILE> Specify configuration file name" << std::endl;
308 std::cout << " -V Display version information" << std::endl;
309 std::cout << " -h Display this help message" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500310}
akmhoque298385a2014-02-13 14:13:09 -0600311
akmhoqueb1710aa2014-02-19 17:13:36 -0600312
313} // namespace nlsr