blob: cefaa29eececfafc7e140c1402498f00452b10f4 [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 **/
akmhoque31d1d4b2014-05-05 22:08:14 -050023#include "nlsr.hpp"
24#include "lsdb.hpp"
25#include "hello-protocol.hpp"
26#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050027#include "logger.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050028
29namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("HelloProtocol");
32
akmhoque93f1a072014-06-19 16:24:28 -050033const std::string HelloProtocol::INFO_COMPONENT = "INFO";
34const std::string HelloProtocol::NLSR_COMPONENT = "NLSR";
akmhoque157b0a42014-05-13 00:26:37 -050035
akmhoque31d1d4b2014-05-05 22:08:14 -050036void
37HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
38{
akmhoque674b0b12014-05-20 14:33:28 -050039 _LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050040 ndn::Interest i(interestName);
41 i.setInterestLifetime(ndn::time::seconds(seconds));
42 i.setMustBeFresh(true);
43 m_nlsr.getNlsrFace().expressInterest(i,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070044 ndn::bind(&HelloProtocol::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -050045 this,
46 _1, _2),
47 ndn::bind(&HelloProtocol::processInterestTimedOut,
48 this, _1));
49}
50
51void
52HelloProtocol::sendScheduledInterest(uint32_t seconds)
53{
54 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
55 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050056 ++it) {
akmhoquec04e7272014-07-02 11:00:14 -050057 if((*it).getFaceId() != 0) {
58 /* interest name: /<neighbor>/NLSR/INFO/<router> */
59 ndn::Name interestName = (*it).getName() ;
60 interestName.append(NLSR_COMPONENT);
61 interestName.append(INFO_COMPONENT);
62 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
63 expressInterest(interestName,
64 m_nlsr.getConfParameter().getInterestResendTime());
65 }
66 else {
67 registerPrefixes((*it).getName(), (*it).getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -050068 (*it).getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -050069 }
akmhoque31d1d4b2014-05-05 22:08:14 -050070 }
71 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
72}
73
74void
75HelloProtocol::scheduleInterest(uint32_t seconds)
76{
Vince Lehman7c603292014-09-11 17:48:16 -050077 m_scheduler.scheduleEvent(ndn::time::seconds(seconds),
78 ndn::bind(&HelloProtocol::sendScheduledInterest, this, seconds));
akmhoque31d1d4b2014-05-05 22:08:14 -050079}
80
81void
82HelloProtocol::processInterest(const ndn::Name& name,
83 const ndn::Interest& interest)
84{
akmhoque93f1a072014-06-19 16:24:28 -050085 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -050086 const ndn::Name interestName = interest.getName();
akmhoque674b0b12014-05-20 14:33:28 -050087 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050088 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050089 return;
90 }
akmhoque157b0a42014-05-13 00:26:37 -050091 ndn::Name neighbor;
92 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque674b0b12014-05-20 14:33:28 -050093 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050094 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque69c9aa92014-07-23 15:15:05 -050095 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
96 data->setName(ndn::Name(interest.getName()).appendVersion());
97 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
98 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
akmhoque157b0a42014-05-13 00:26:37 -050099 INFO_COMPONENT.size());
akmhoque69c9aa92014-07-23 15:15:05 -0500100 m_nlsr.getKeyChain().sign(*data, m_nlsr.getDefaultCertName());
101 _LOG_DEBUG("Sending out data for name: " << interest.getName());
102 m_nlsr.getNlsrFace().put(*data);
akmhoquec04e7272014-07-02 11:00:14 -0500103 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500104 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
akmhoquec04e7272014-07-02 11:00:14 -0500105 if(adjacent->getFaceId() != 0){
106 /* interest name: /<neighbor>/NLSR/INFO/<router> */
107 ndn::Name interestName(neighbor);
108 interestName.append(NLSR_COMPONENT);
109 interestName.append(INFO_COMPONENT);
110 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
111 expressInterest(interestName,
112 m_nlsr.getConfParameter().getInterestResendTime());
113 }
114 else {
115 registerPrefixes(adjacent->getName(), adjacent->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500116 adjacent->getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -0500117 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 }
119 }
120}
121
122void
123HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
124{
akmhoque93f1a072014-06-19 16:24:28 -0500125 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 const ndn::Name interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500127 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500128 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 return;
130 }
akmhoque93f1a072014-06-19 16:24:28 -0500131 ndn::Name neighbor = interestName.getPrefix(-3);
akmhoque674b0b12014-05-20 14:33:28 -0500132 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500133 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500134
135 Adjacent::Status status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
136
akmhoque31d1d4b2014-05-05 22:08:14 -0500137 uint32_t infoIntTimedOutCount =
138 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500139 _LOG_DEBUG("Status: " << status);
140 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500141 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque93f1a072014-06-19 16:24:28 -0500142 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500144 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500145 interestName.append(INFO_COMPONENT);
146 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 expressInterest(interestName,
148 m_nlsr.getConfParameter().getInterestResendTime());
149 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500150 else if ((status == Adjacent::STATUS_ACTIVE) &&
akmhoque157b0a42014-05-13 00:26:37 -0500151 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500152 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500153 m_nlsr.incrementAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500154 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500155 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500156 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500157 // event here
Vince Lehman7c603292014-09-11 17:48:16 -0500158 m_scheduler.scheduleEvent(ndn::time::seconds(5),
159 ndn::bind(&Lsdb::scheduledAdjLsaBuild, &m_nlsr.getLsdb()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500160 }
161 }
162}
163
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700164void
165HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
166{
akmhoquedfe615f2014-07-27 14:12:21 -0500167 _LOG_DEBUG("Received data for INFO(name): " << data.getName());
168 if (data.getSignature().hasKeyLocator()) {
169 if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
170 _LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
171 }
172 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700173 m_nlsr.getValidator().validate(data,
174 ndn::bind(&HelloProtocol::onContentValidated, this, _1),
175 ndn::bind(&HelloProtocol::onContentValidationFailed,
176 this, _1, _2));
177}
akmhoque31d1d4b2014-05-05 22:08:14 -0500178
179void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700180HelloProtocol::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500181{
akmhoque93f1a072014-06-19 16:24:28 -0500182 /* data name: /<neighbor>/NLSR/INFO/<router>/<version> */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700183 ndn::Name dataName = data->getName();
akmhoquedfe615f2014-07-27 14:12:21 -0500184 _LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500185 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500186 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500187
188 Adjacent::Status oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
189 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500190 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500191 Adjacent::Status newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
192
akmhoque2f423352014-06-03 11:49:35 -0500193 _LOG_DEBUG("Neighbor : " << neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500194 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500195 // change in Adjacency list
196 if ((oldStatus - newStatus) != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500197 m_nlsr.incrementAdjBuildCount();
akmhoque2f423352014-06-03 11:49:35 -0500198 // Need to schedule event for Adjacency LSA building
akmhoque157b0a42014-05-13 00:26:37 -0500199 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500200 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500201 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500202 // event here
Vince Lehman7c603292014-09-11 17:48:16 -0500203 m_scheduler.scheduleEvent(ndn::time::seconds(5),
204 ndn::bind(&Lsdb::scheduledAdjLsaBuild, &m_nlsr.getLsdb()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500205 }
206 }
207 }
208}
209
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700210void
211HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
212 const std::string& msg)
213{
214 _LOG_DEBUG("Validation Error: " << msg);
215}
216
akmhoquec04e7272014-07-02 11:00:14 -0500217void
akmhoque8e0252b2014-07-07 16:04:44 -0500218HelloProtocol::registerPrefixes(const ndn::Name& adjName, const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500219 double linkCost, const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500220{
akmhoque060d3022014-08-12 13:35:06 -0500221 m_nlsr.getFib().registerPrefix(adjName, faceUri, linkCost, timeout,
222 ndn::nfd::ROUTE_FLAG_CAPTURE, 0,
akmhoquec04e7272014-07-02 11:00:14 -0500223 ndn::bind(&HelloProtocol::onRegistrationSuccess,
akmhoque102aea42014-08-04 10:22:12 -0500224 this, _1, adjName,timeout),
akmhoquec04e7272014-07-02 11:00:14 -0500225 ndn::bind(&HelloProtocol::onRegistrationFailure,
akmhoquedfe615f2014-07-27 14:12:21 -0500226 this, _1, _2, adjName));
akmhoquec04e7272014-07-02 11:00:14 -0500227}
228
229void
230HelloProtocol::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
akmhoque102aea42014-08-04 10:22:12 -0500231 const ndn::Name& neighbor,const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500232{
233 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
234 if (adjacent != 0) {
235 adjacent->setFaceId(commandSuccessResult.getFaceId());
akmhoque102aea42014-08-04 10:22:12 -0500236 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
237 broadcastKeyPrefix.append("KEYS");
238 std::string faceUri = adjacent->getConnectingFaceUri();
239 double linkCost = adjacent->getLinkCost();
240 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getChronosyncPrefix(),
akmhoque060d3022014-08-12 13:35:06 -0500241 faceUri, linkCost, timeout,
242 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500243 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getLsaPrefix(),
akmhoque060d3022014-08-12 13:35:06 -0500244 faceUri, linkCost, timeout,
245 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500246 m_nlsr.getFib().registerPrefix(broadcastKeyPrefix,
akmhoque060d3022014-08-12 13:35:06 -0500247 faceUri, linkCost, timeout,
248 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500249 m_nlsr.setStrategies();
250
akmhoquec04e7272014-07-02 11:00:14 -0500251 /* interest name: /<neighbor>/NLSR/INFO/<router> */
252 ndn::Name interestName(neighbor);
253 interestName.append(NLSR_COMPONENT);
254 interestName.append(INFO_COMPONENT);
255 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
256 expressInterest(interestName,
257 m_nlsr.getConfParameter().getInterestResendTime());
258 }
259}
260
261void
akmhoquedfe615f2014-07-27 14:12:21 -0500262HelloProtocol::onRegistrationFailure(uint32_t code, const std::string& error,
263 const ndn::Name& name)
akmhoquec04e7272014-07-02 11:00:14 -0500264{
265 _LOG_DEBUG(error << " (code: " << code << ")");
akmhoquedfe615f2014-07-27 14:12:21 -0500266 /*
267 * If NLSR can not create face for given faceUri then it will treat this
268 * failure as one INFO interest timed out. So that NLSR can move on with
269 * building Adj Lsa and calculate routing table. NLSR does not build Adj
270 * Lsa unless all the neighbors are ACTIVE or DEAD. For considering the
271 * missconfigured(link) neighbour dead this is required.
272 */
273 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(name);
274 if (adjacent != 0) {
275 adjacent->setInterestTimedOutNo(adjacent->getInterestTimedOutNo() + 1);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500276 Adjacent::Status status = adjacent->getStatus();
akmhoquedfe615f2014-07-27 14:12:21 -0500277 uint32_t infoIntTimedOutCount = adjacent->getInterestTimedOutNo();
278
279 if (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500280 if (status == Adjacent::STATUS_ACTIVE) {
281 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
akmhoquedfe615f2014-07-27 14:12:21 -0500282 }
283 m_nlsr.incrementAdjBuildCount();
284 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
285 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
286 m_nlsr.setIsBuildAdjLsaSheduled(true);
287 // event here
Vince Lehman7c603292014-09-11 17:48:16 -0500288 m_scheduler.scheduleEvent(ndn::time::seconds(5),
289 ndn::bind(&Lsdb::scheduledAdjLsaBuild, &m_nlsr.getLsdb()));
akmhoquedfe615f2014-07-27 14:12:21 -0500290 }
291 }
292 }
akmhoquec04e7272014-07-02 11:00:14 -0500293}
294
akmhoque31d1d4b2014-05-05 22:08:14 -0500295} //namespace nlsr