blob: a66e6aa05021e54213cbda099d59bc09528a23ee [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();
Vince Lehman7b616582014-10-17 16:25:39 -0500145
146 // Set event intervals
147 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
148 m_helloProtocol.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
149 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
150
151 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500152
153 // Need to set direct neighbors' costs to 0 for hyperbolic routing
154 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
155
156 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
157
158 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
159 it->setLinkCost(0);
160 }
161 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700162}
163
164void
akmhoque443ad812014-07-29 10:26:56 -0500165Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700166{
167 m_defaultIdentity = m_confParam.getRouterPrefix();
168 m_defaultIdentity.append("NLSR");
169
akmhoque102aea42014-08-04 10:22:12 -0500170 try
171 {
172 m_keyChain.deleteIdentity(m_defaultIdentity);
173 }
174 catch (std::exception& e)
175 {
176 }
akmhoque443ad812014-07-29 10:26:56 -0500177
Yingdi Yu9a18bfb2014-06-19 14:06:21 -0700178 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(m_defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700179
Yingdi Yu191f5fa2014-08-06 17:08:52 -0700180 ndn::shared_ptr<ndn::IdentityCertificate> certificate =
181 ndn::make_shared<ndn::IdentityCertificate>();
182 ndn::shared_ptr<ndn::PublicKey> pubKey = m_keyChain.getPublicKey(keyName);
183 Name certificateName = keyName.getPrefix(-1);
184 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
185 certificate->setName(certificateName);
186 certificate->setNotBefore(time::system_clock::now() - time::days(1));
187 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
188 certificate->setPublicKeyInfo(*pubKey);
189 certificate->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
190 keyName.toUri()));
191 certificate->encode();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700192 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
193
194 m_keyChain.addCertificateAsIdentityDefault(*certificate);
195 loadCertToPublish(certificate);
196
197 m_defaultCertName = certificate->getName();
198}
199
200void
201Nlsr::registerKeyPrefix()
202{
203 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
204 keyPrefix.append("KEYS");
205 m_nlsrFace.setInterestFilter(keyPrefix,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700206 ndn::bind(&Nlsr::onKeyInterest,
207 this, _1, _2),
208 ndn::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
akmhoque060d3022014-08-12 13:35:06 -0500209 ndn::bind(&Nlsr::registrationFailed, this, _1),
210 m_defaultIdentity,
211 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700212
213}
214
215void
216Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
217{
218 const ndn::Name& interestName = interest.getName();
219
220 ndn::Name certName = interestName.getSubName(name.size());
221
222 if (certName[-2].toUri() == "ID-CERT")
223 {
224 certName = certName.getPrefix(-1);
225 }
226 else if (certName[-1].toUri() != "ID-CERT")
227 return; //Wrong key interest.
228
229 ndn::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
230
231 if (!static_cast<bool>(cert))
232 return; // cert is not found
233
akmhoque69c9aa92014-07-23 15:15:05 -0500234 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
235 data->setName(interestName);
236 data->setContent(cert->wireEncode());
237 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700238
akmhoque69c9aa92014-07-23 15:15:05 -0500239 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700240}
241
242void
243Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
244{
akmhoque53353462014-04-22 08:43:45 -0500245}
akmhoque5a44dd42014-03-12 18:11:32 -0500246
akmhoque53353462014-04-22 08:43:45 -0500247void
akmhoquee1765152014-06-30 11:32:01 -0500248Nlsr::onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
249{
akmhoquee1765152014-06-30 11:32:01 -0500250}
251
252void
253Nlsr::onDestroyFaceFailure(int32_t code, const std::string& error)
254{
255 std::cerr << error << " (code: " << code << ")";
256 throw Error("Error: Face destruction failed");
257}
258
259void
260Nlsr::destroyFaces()
261{
262 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
263 for (std::list<Adjacent>::iterator it = adjacents.begin();
264 it != adjacents.end(); it++) {
akmhoquec04e7272014-07-02 11:00:14 -0500265 m_fib.destroyFace((*it).getConnectingFaceUri(),
266 ndn::bind(&Nlsr::onDestroyFaceSuccess, this, _1),
267 ndn::bind(&Nlsr::onDestroyFaceFailure, this, _1, _2));
akmhoquee1765152014-06-30 11:32:01 -0500268 }
269}
270
akmhoquec04e7272014-07-02 11:00:14 -0500271
272
akmhoquee1765152014-06-30 11:32:01 -0500273
274void
akmhoquec04e7272014-07-02 11:00:14 -0500275Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500276{
akmhoquec04e7272014-07-02 11:00:14 -0500277 ndn::nfd::FaceEventKind kind = faceEventNotification.getKind();
akmhoque060d3022014-08-12 13:35:06 -0500278 _LOG_DEBUG("Nlsr::onFaceEventNotification called");
akmhoquec04e7272014-07-02 11:00:14 -0500279 if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
280 uint64_t faceId = faceEventNotification.getFaceId();
281 Adjacent *adjacent = m_adjacencyList.findAdjacent(faceId);
282 if (adjacent != 0) {
akmhoque060d3022014-08-12 13:35:06 -0500283 _LOG_DEBUG("Face to " << adjacent->getName() << " with face id: "
284 << faceId << " deleted");
akmhoquec04e7272014-07-02 11:00:14 -0500285 adjacent->setFaceId(0);
286 }
287 }
akmhoquee1765152014-06-30 11:32:01 -0500288}
289
290
291void
akmhoque53353462014-04-22 08:43:45 -0500292Nlsr::startEventLoop()
293{
akmhoquefdbddb12014-05-02 18:35:19 -0500294 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500295}
akmhoque5a44dd42014-03-12 18:11:32 -0500296
akmhoquefdbddb12014-05-02 18:35:19 -0500297void
akmhoque53353462014-04-22 08:43:45 -0500298Nlsr::usage(const string& progname)
299{
akmhoque48265992014-08-10 08:27:13 -0500300 std::cout << "Usage: " << progname << " [OPTIONS...]" << std::endl;
301 std::cout << " NDN routing...." << std::endl;
Vince Lehmanb722b102014-08-24 16:33:49 -0500302 std::cout << " -d Run in daemon mode" << std::endl;
303 std::cout << " -f <FILE> Specify configuration file name" << std::endl;
304 std::cout << " -V Display version information" << std::endl;
305 std::cout << " -h Display this help message" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500306}
akmhoque298385a2014-02-13 14:13:09 -0600307
akmhoqueb1710aa2014-02-19 17:13:36 -0600308
309} // namespace nlsr