blob: 4f55af9a2f1a81cac6cbde9daddf47d8c976379a [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05003 * Copyright (c) 2014-2016, The University of Memphis,
4 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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 **/
Junxiao Shi63bd0342016-08-17 16:57:14 +000023
akmhoque31d1d4b2014-05-05 22:08:14 -050024#include "nlsr.hpp"
25#include "lsdb.hpp"
26#include "hello-protocol.hpp"
27#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050028#include "logger.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050029
30namespace nlsr {
31
akmhoque674b0b12014-05-20 14:33:28 -050032INIT_LOGGER("HelloProtocol");
33
akmhoque93f1a072014-06-19 16:24:28 -050034const std::string HelloProtocol::INFO_COMPONENT = "INFO";
35const std::string HelloProtocol::NLSR_COMPONENT = "NLSR";
akmhoque157b0a42014-05-13 00:26:37 -050036
akmhoque31d1d4b2014-05-05 22:08:14 -050037void
38HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
39{
akmhoque674b0b12014-05-20 14:33:28 -050040 _LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050041 ndn::Interest i(interestName);
42 i.setInterestLifetime(ndn::time::seconds(seconds));
43 i.setMustBeFresh(true);
44 m_nlsr.getNlsrFace().expressInterest(i,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070045 ndn::bind(&HelloProtocol::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -050046 this,
47 _1, _2),
48 ndn::bind(&HelloProtocol::processInterestTimedOut,
49 this, _1));
50}
51
52void
53HelloProtocol::sendScheduledInterest(uint32_t seconds)
54{
55 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
56 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050057 ++it) {
akmhoquec04e7272014-07-02 11:00:14 -050058 if((*it).getFaceId() != 0) {
59 /* interest name: /<neighbor>/NLSR/INFO/<router> */
60 ndn::Name interestName = (*it).getName() ;
61 interestName.append(NLSR_COMPONENT);
62 interestName.append(INFO_COMPONENT);
63 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
64 expressInterest(interestName,
65 m_nlsr.getConfParameter().getInterestResendTime());
66 }
67 else {
68 registerPrefixes((*it).getName(), (*it).getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -050069 (*it).getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -050070 }
akmhoque31d1d4b2014-05-05 22:08:14 -050071 }
72 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
73}
74
75void
76HelloProtocol::scheduleInterest(uint32_t seconds)
77{
Vince Lehman50df6b72015-03-03 12:06:40 -060078 _LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
79
Vince Lehman7c603292014-09-11 17:48:16 -050080 m_scheduler.scheduleEvent(ndn::time::seconds(seconds),
81 ndn::bind(&HelloProtocol::sendScheduledInterest, this, seconds));
akmhoque31d1d4b2014-05-05 22:08:14 -050082}
83
84void
85HelloProtocol::processInterest(const ndn::Name& name,
86 const ndn::Interest& interest)
87{
akmhoque93f1a072014-06-19 16:24:28 -050088 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -050089 const ndn::Name interestName = interest.getName();
akmhoque674b0b12014-05-20 14:33:28 -050090 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050091 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050092 return;
93 }
akmhoque157b0a42014-05-13 00:26:37 -050094 ndn::Name neighbor;
95 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque674b0b12014-05-20 14:33:28 -050096 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050097 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque69c9aa92014-07-23 15:15:05 -050098 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
99 data->setName(ndn::Name(interest.getName()).appendVersion());
100 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
101 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
akmhoque157b0a42014-05-13 00:26:37 -0500102 INFO_COMPONENT.size());
akmhoque69c9aa92014-07-23 15:15:05 -0500103 m_nlsr.getKeyChain().sign(*data, m_nlsr.getDefaultCertName());
104 _LOG_DEBUG("Sending out data for name: " << interest.getName());
105 m_nlsr.getNlsrFace().put(*data);
akmhoquec04e7272014-07-02 11:00:14 -0500106 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500107 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
akmhoquec04e7272014-07-02 11:00:14 -0500108 if(adjacent->getFaceId() != 0){
109 /* interest name: /<neighbor>/NLSR/INFO/<router> */
110 ndn::Name interestName(neighbor);
111 interestName.append(NLSR_COMPONENT);
112 interestName.append(INFO_COMPONENT);
113 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
114 expressInterest(interestName,
115 m_nlsr.getConfParameter().getInterestResendTime());
116 }
117 else {
118 registerPrefixes(adjacent->getName(), adjacent->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500119 adjacent->getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -0500120 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 }
122 }
123}
124
125void
126HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
127{
akmhoque93f1a072014-06-19 16:24:28 -0500128 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 const ndn::Name interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500130 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500131 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500132 return;
133 }
akmhoque93f1a072014-06-19 16:24:28 -0500134 ndn::Name neighbor = interestName.getPrefix(-3);
akmhoque674b0b12014-05-20 14:33:28 -0500135 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500136 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500137
138 Adjacent::Status status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
139
akmhoque31d1d4b2014-05-05 22:08:14 -0500140 uint32_t infoIntTimedOutCount =
141 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500142 _LOG_DEBUG("Status: " << status);
143 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500144 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque93f1a072014-06-19 16:24:28 -0500145 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500146 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500147 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500148 interestName.append(INFO_COMPONENT);
149 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 expressInterest(interestName,
151 m_nlsr.getConfParameter().getInterestResendTime());
152 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500153 else if ((status == Adjacent::STATUS_ACTIVE) &&
akmhoque157b0a42014-05-13 00:26:37 -0500154 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500155 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600156
157 m_nlsr.getLsdb().scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500158 }
159}
160
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700161void
162HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
163{
akmhoquedfe615f2014-07-27 14:12:21 -0500164 _LOG_DEBUG("Received data for INFO(name): " << data.getName());
165 if (data.getSignature().hasKeyLocator()) {
166 if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
167 _LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
168 }
169 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700170 m_nlsr.getValidator().validate(data,
171 ndn::bind(&HelloProtocol::onContentValidated, this, _1),
172 ndn::bind(&HelloProtocol::onContentValidationFailed,
173 this, _1, _2));
174}
akmhoque31d1d4b2014-05-05 22:08:14 -0500175
176void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700177HelloProtocol::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500178{
akmhoque93f1a072014-06-19 16:24:28 -0500179 /* data name: /<neighbor>/NLSR/INFO/<router>/<version> */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700180 ndn::Name dataName = data->getName();
akmhoquedfe615f2014-07-27 14:12:21 -0500181 _LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500182 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500183 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500184
185 Adjacent::Status oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
186 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500187 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500188 Adjacent::Status newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
189
akmhoque2f423352014-06-03 11:49:35 -0500190 _LOG_DEBUG("Neighbor : " << neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500191 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500192 // change in Adjacency list
193 if ((oldStatus - newStatus) != 0) {
Vince Lehman50df6b72015-03-03 12:06:40 -0600194 m_nlsr.getLsdb().scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 }
196 }
197}
198
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700199void
200HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
201 const std::string& msg)
202{
203 _LOG_DEBUG("Validation Error: " << msg);
204}
205
akmhoquec04e7272014-07-02 11:00:14 -0500206void
akmhoque8e0252b2014-07-07 16:04:44 -0500207HelloProtocol::registerPrefixes(const ndn::Name& adjName, const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500208 double linkCost, const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500209{
akmhoque060d3022014-08-12 13:35:06 -0500210 m_nlsr.getFib().registerPrefix(adjName, faceUri, linkCost, timeout,
211 ndn::nfd::ROUTE_FLAG_CAPTURE, 0,
akmhoquec04e7272014-07-02 11:00:14 -0500212 ndn::bind(&HelloProtocol::onRegistrationSuccess,
akmhoque102aea42014-08-04 10:22:12 -0500213 this, _1, adjName,timeout),
akmhoquec04e7272014-07-02 11:00:14 -0500214 ndn::bind(&HelloProtocol::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000215 this, _1, adjName));
akmhoquec04e7272014-07-02 11:00:14 -0500216}
217
218void
219HelloProtocol::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
akmhoque102aea42014-08-04 10:22:12 -0500220 const ndn::Name& neighbor,const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500221{
222 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
223 if (adjacent != 0) {
224 adjacent->setFaceId(commandSuccessResult.getFaceId());
akmhoque102aea42014-08-04 10:22:12 -0500225 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
226 broadcastKeyPrefix.append("KEYS");
227 std::string faceUri = adjacent->getConnectingFaceUri();
228 double linkCost = adjacent->getLinkCost();
229 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getChronosyncPrefix(),
akmhoque060d3022014-08-12 13:35:06 -0500230 faceUri, linkCost, timeout,
231 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500232 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getLsaPrefix(),
akmhoque060d3022014-08-12 13:35:06 -0500233 faceUri, linkCost, timeout,
234 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500235 m_nlsr.getFib().registerPrefix(broadcastKeyPrefix,
akmhoque060d3022014-08-12 13:35:06 -0500236 faceUri, linkCost, timeout,
237 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500238 m_nlsr.setStrategies();
239
akmhoquec04e7272014-07-02 11:00:14 -0500240 /* interest name: /<neighbor>/NLSR/INFO/<router> */
241 ndn::Name interestName(neighbor);
242 interestName.append(NLSR_COMPONENT);
243 interestName.append(INFO_COMPONENT);
244 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
245 expressInterest(interestName,
246 m_nlsr.getConfParameter().getInterestResendTime());
247 }
248}
249
250void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000251HelloProtocol::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoquedfe615f2014-07-27 14:12:21 -0500252 const ndn::Name& name)
akmhoquec04e7272014-07-02 11:00:14 -0500253{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000254 _LOG_DEBUG(response.getText() << " (code: " << response.getCode() << ")");
akmhoquedfe615f2014-07-27 14:12:21 -0500255 /*
256 * If NLSR can not create face for given faceUri then it will treat this
257 * failure as one INFO interest timed out. So that NLSR can move on with
258 * building Adj Lsa and calculate routing table. NLSR does not build Adj
259 * Lsa unless all the neighbors are ACTIVE or DEAD. For considering the
260 * missconfigured(link) neighbour dead this is required.
261 */
262 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(name);
263 if (adjacent != 0) {
264 adjacent->setInterestTimedOutNo(adjacent->getInterestTimedOutNo() + 1);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500265 Adjacent::Status status = adjacent->getStatus();
akmhoquedfe615f2014-07-27 14:12:21 -0500266 uint32_t infoIntTimedOutCount = adjacent->getInterestTimedOutNo();
267
268 if (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500269 if (status == Adjacent::STATUS_ACTIVE) {
270 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
akmhoquedfe615f2014-07-27 14:12:21 -0500271 }
Vince Lehman50df6b72015-03-03 12:06:40 -0600272
273 m_nlsr.getLsdb().scheduleAdjLsaBuild();
akmhoquedfe615f2014-07-27 14:12:21 -0500274 }
275 }
akmhoquec04e7272014-07-02 11:00:14 -0500276}
277
Nick Gordonfad8e252016-08-11 14:21:38 -0500278} // namespace nlsr