blob: 4065482c5794cd3cfd0d5b5c0b1c3cec431be011 [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),
akmhoque31d1d4b2014-05-05 22:08:14 -050062 ndn::bind(&Nlsr::registrationFailed, this, _1));
63}
64
65void
66Nlsr::setLsaInterestFilter()
67{
akmhoque157b0a42014-05-13 00:26:37 -050068 ndn::Name name = m_confParam.getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -050069 name.append(m_confParam.getSiteName());
70 name.append(m_confParam.getRouterName());
akmhoque674b0b12014-05-20 14:33:28 -050071 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -050072 getNlsrFace().setInterestFilter(name,
73 ndn::bind(&Lsdb::processInterest,
74 &m_nlsrLsdb, _1, _2),
akmhoque157b0a42014-05-13 00:26:37 -050075 ndn::bind(&Nlsr::onRegistrationSuccess, this, _1),
akmhoquefdbddb12014-05-02 18:35:19 -050076 ndn::bind(&Nlsr::registrationFailed, this, _1));
akmhoque53353462014-04-22 08:43:45 -050077}
78
79void
akmhoquec04e7272014-07-02 11:00:14 -050080Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -050081{
82 std::string strategy("ndn:/localhost/nfd/strategy/broadcast");
akmhoque3cb0cfc2014-06-24 10:32:24 -050083 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
84 broadcastKeyPrefix.append("KEYS");
akmhoque393d4ff2014-07-16 14:27:03 -050085 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
86 m_fib.setStrategy(broadcastKeyPrefix, strategy, 0);
87 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -050088}
89
90void
akmhoque0494c252014-07-23 23:46:44 -050091Nlsr::daemonize()
92{
93 pid_t process_id = 0;
94 pid_t sid = 0;
95 process_id = fork();
96 if (process_id < 0){
97 std::cerr << "Daemonization failed!" << std::endl;
98 throw Error("Error: Daemonization process- fork failed!");
99 }
100 if (process_id > 0) {
101 _LOG_DEBUG("Process daemonized. Process id: " << process_id);
102 exit(0);
103 }
104
105 umask(0);
106 sid = setsid();
107 if(sid < 0) {
108 throw Error("Error: Daemonization process- setting id failed!");
109 }
110
111 if (chdir("/") < 0) {
112 throw Error("Error: Daemonization process-chdir failed!");
113 }
114}
115
116void
akmhoque53353462014-04-22 08:43:45 -0500117Nlsr::initialize()
118{
akmhoque674b0b12014-05-20 14:33:28 -0500119 _LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500120 m_confParam.buildRouterPrefix();
121 m_nlsrLsdb.setLsaRefreshTime(m_confParam.getLsaRefreshTime());
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500123 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
akmhoquec8a10f72014-04-25 18:42:55 -0500124 m_sequencingManager.setSeqFileName(m_confParam.getSeqFileDir());
125 m_sequencingManager.initiateSeqNoFromFile();
akmhoquee1765152014-06-30 11:32:01 -0500126 m_syncLogicHandler.setSyncPrefix(m_confParam.getChronosyncPrefix().toUri());
akmhoque674b0b12014-05-20 14:33:28 -0500127 /* Logging start */
128 m_confParam.writeLog();
129 m_adjacencyList.writeLog();
130 m_namePrefixList.writeLog();
131 /* Logging end */
akmhoque443ad812014-07-29 10:26:56 -0500132 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500133 setStrategies();
akmhoque31d1d4b2014-05-05 22:08:14 -0500134 setInfoInterestFilter();
135 setLsaInterestFilter();
akmhoque674b0b12014-05-20 14:33:28 -0500136 m_nlsrLsdb.buildAndInstallOwnNameLsa();
137 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
akmhoquec8a10f72014-04-25 18:42:55 -0500138 m_syncLogicHandler.createSyncSocket(boost::ref(*this));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700139 registerKeyPrefix();
akmhoquee1765152014-06-30 11:32:01 -0500140 m_helloProtocol.scheduleInterest(10);
akmhoque18f861a2014-07-07 18:32:04 -0500141
142 m_faceMonitor.startNotification();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700143}
144
145void
akmhoque443ad812014-07-29 10:26:56 -0500146Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700147{
148 m_defaultIdentity = m_confParam.getRouterPrefix();
149 m_defaultIdentity.append("NLSR");
150
akmhoque102aea42014-08-04 10:22:12 -0500151 try
152 {
153 m_keyChain.deleteIdentity(m_defaultIdentity);
154 }
155 catch (std::exception& e)
156 {
157 }
akmhoque443ad812014-07-29 10:26:56 -0500158
Yingdi Yu9a18bfb2014-06-19 14:06:21 -0700159 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(m_defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700160
161 ndn::shared_ptr<ndn::IdentityCertificate> certificate = m_keyChain.selfSign(keyName);
162 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
163
164 m_keyChain.addCertificateAsIdentityDefault(*certificate);
165 loadCertToPublish(certificate);
166
167 m_defaultCertName = certificate->getName();
168}
169
170void
171Nlsr::registerKeyPrefix()
172{
173 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
174 keyPrefix.append("KEYS");
175 m_nlsrFace.setInterestFilter(keyPrefix,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700176 ndn::bind(&Nlsr::onKeyInterest,
177 this, _1, _2),
178 ndn::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
179 ndn::bind(&Nlsr::registrationFailed, this, _1));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700180
181}
182
183void
184Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
185{
186 const ndn::Name& interestName = interest.getName();
187
188 ndn::Name certName = interestName.getSubName(name.size());
189
190 if (certName[-2].toUri() == "ID-CERT")
191 {
192 certName = certName.getPrefix(-1);
193 }
194 else if (certName[-1].toUri() != "ID-CERT")
195 return; //Wrong key interest.
196
197 ndn::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
198
199 if (!static_cast<bool>(cert))
200 return; // cert is not found
201
akmhoque69c9aa92014-07-23 15:15:05 -0500202 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
203 data->setName(interestName);
204 data->setContent(cert->wireEncode());
205 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700206
akmhoque69c9aa92014-07-23 15:15:05 -0500207 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700208}
209
210void
211Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
212{
akmhoque53353462014-04-22 08:43:45 -0500213}
akmhoque5a44dd42014-03-12 18:11:32 -0500214
akmhoque53353462014-04-22 08:43:45 -0500215void
akmhoquee1765152014-06-30 11:32:01 -0500216Nlsr::onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
217{
akmhoquee1765152014-06-30 11:32:01 -0500218}
219
220void
221Nlsr::onDestroyFaceFailure(int32_t code, const std::string& error)
222{
223 std::cerr << error << " (code: " << code << ")";
224 throw Error("Error: Face destruction failed");
225}
226
227void
228Nlsr::destroyFaces()
229{
230 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
231 for (std::list<Adjacent>::iterator it = adjacents.begin();
232 it != adjacents.end(); it++) {
akmhoquec04e7272014-07-02 11:00:14 -0500233 m_fib.destroyFace((*it).getConnectingFaceUri(),
234 ndn::bind(&Nlsr::onDestroyFaceSuccess, this, _1),
235 ndn::bind(&Nlsr::onDestroyFaceFailure, this, _1, _2));
akmhoquee1765152014-06-30 11:32:01 -0500236 }
237}
238
akmhoquec04e7272014-07-02 11:00:14 -0500239
240
akmhoquee1765152014-06-30 11:32:01 -0500241
242void
akmhoquec04e7272014-07-02 11:00:14 -0500243Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500244{
akmhoquec04e7272014-07-02 11:00:14 -0500245 ndn::nfd::FaceEventKind kind = faceEventNotification.getKind();
246 if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
247 uint64_t faceId = faceEventNotification.getFaceId();
248 Adjacent *adjacent = m_adjacencyList.findAdjacent(faceId);
249 if (adjacent != 0) {
250 _LOG_DEBUG("Face to " << adjacent->getName() << "deleted");
251 adjacent->setFaceId(0);
252 }
253 }
akmhoquee1765152014-06-30 11:32:01 -0500254}
255
256
257void
akmhoque53353462014-04-22 08:43:45 -0500258Nlsr::startEventLoop()
259{
akmhoquefdbddb12014-05-02 18:35:19 -0500260 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500261}
akmhoque5a44dd42014-03-12 18:11:32 -0500262
akmhoquefdbddb12014-05-02 18:35:19 -0500263void
akmhoque53353462014-04-22 08:43:45 -0500264Nlsr::usage(const string& progname)
265{
266 cout << "Usage: " << progname << " [OPTIONS...]" << endl;
267 cout << " NDN routing...." << endl;
268 cout << " -d, --daemon Run in daemon mode" << endl;
269 cout << " -f, --config_file Specify configuration file name" << endl;
270 cout << " -p, --api_port port where api client will connect" << endl;
271 cout << " -h, --help Display this help message" << endl;
akmhoque53353462014-04-22 08:43:45 -0500272}
akmhoque298385a2014-02-13 14:13:09 -0600273
akmhoqueb1710aa2014-02-19 17:13:36 -0600274
275} // namespace nlsr