blob: 9f8472a2818a4c63f78bce143d12280a504cb2f6 [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) {
Nick G97e34942016-07-11 14:46:27 -050058 // If this adjacency has a Face, just proceed as usual.
akmhoquec04e7272014-07-02 11:00:14 -050059 if((*it).getFaceId() != 0) {
60 /* interest name: /<neighbor>/NLSR/INFO/<router> */
61 ndn::Name interestName = (*it).getName() ;
62 interestName.append(NLSR_COMPONENT);
63 interestName.append(INFO_COMPONENT);
64 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
65 expressInterest(interestName,
66 m_nlsr.getConfParameter().getInterestResendTime());
67 }
Nick G97e34942016-07-11 14:46:27 -050068 // If it does not have a Face, we need to give it one. A
69 // successful registration prompts a callback that sends the hello
70 // Interest to the new Face.
akmhoquec04e7272014-07-02 11:00:14 -050071 else {
72 registerPrefixes((*it).getName(), (*it).getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -050073 (*it).getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -050074 }
akmhoque31d1d4b2014-05-05 22:08:14 -050075 }
76 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
77}
78
79void
80HelloProtocol::scheduleInterest(uint32_t seconds)
81{
Vince Lehman50df6b72015-03-03 12:06:40 -060082 _LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
83
Vince Lehman7c603292014-09-11 17:48:16 -050084 m_scheduler.scheduleEvent(ndn::time::seconds(seconds),
85 ndn::bind(&HelloProtocol::sendScheduledInterest, this, seconds));
akmhoque31d1d4b2014-05-05 22:08:14 -050086}
87
88void
89HelloProtocol::processInterest(const ndn::Name& name,
90 const ndn::Interest& interest)
91{
akmhoque93f1a072014-06-19 16:24:28 -050092 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -050093 const ndn::Name interestName = interest.getName();
akmhoque674b0b12014-05-20 14:33:28 -050094 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050095 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050096 return;
97 }
akmhoque157b0a42014-05-13 00:26:37 -050098 ndn::Name neighbor;
99 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque674b0b12014-05-20 14:33:28 -0500100 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500101 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque69c9aa92014-07-23 15:15:05 -0500102 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
103 data->setName(ndn::Name(interest.getName()).appendVersion());
104 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
105 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
akmhoque157b0a42014-05-13 00:26:37 -0500106 INFO_COMPONENT.size());
akmhoque69c9aa92014-07-23 15:15:05 -0500107 m_nlsr.getKeyChain().sign(*data, m_nlsr.getDefaultCertName());
108 _LOG_DEBUG("Sending out data for name: " << interest.getName());
109 m_nlsr.getNlsrFace().put(*data);
akmhoquec04e7272014-07-02 11:00:14 -0500110 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
Nick G97e34942016-07-11 14:46:27 -0500111 // If this neighbor was previously inactive, send our own hello interest, too
Vince Lehmancb76ade2014-08-28 21:24:41 -0500112 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
Nick G97e34942016-07-11 14:46:27 -0500113 // We can only do that if the neighbor currently has a face.
akmhoquec04e7272014-07-02 11:00:14 -0500114 if(adjacent->getFaceId() != 0){
115 /* interest name: /<neighbor>/NLSR/INFO/<router> */
116 ndn::Name interestName(neighbor);
117 interestName.append(NLSR_COMPONENT);
118 interestName.append(INFO_COMPONENT);
119 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
120 expressInterest(interestName,
121 m_nlsr.getConfParameter().getInterestResendTime());
122 }
Nick G97e34942016-07-11 14:46:27 -0500123 // If the originator of the Interest currently lacks a Face, we
124 // need to give it one.
akmhoquec04e7272014-07-02 11:00:14 -0500125 else {
126 registerPrefixes(adjacent->getName(), adjacent->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500127 adjacent->getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -0500128 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 }
130 }
131}
132
133void
134HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
135{
akmhoque93f1a072014-06-19 16:24:28 -0500136 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500137 const ndn::Name interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500138 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500139 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500140 return;
141 }
akmhoque93f1a072014-06-19 16:24:28 -0500142 ndn::Name neighbor = interestName.getPrefix(-3);
akmhoque674b0b12014-05-20 14:33:28 -0500143 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500144 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500145
146 Adjacent::Status status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
147
akmhoque31d1d4b2014-05-05 22:08:14 -0500148 uint32_t infoIntTimedOutCount =
149 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500150 _LOG_DEBUG("Status: " << status);
151 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500152 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque93f1a072014-06-19 16:24:28 -0500153 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500154 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500155 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500156 interestName.append(INFO_COMPONENT);
157 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500158 expressInterest(interestName,
159 m_nlsr.getConfParameter().getInterestResendTime());
160 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500161 else if ((status == Adjacent::STATUS_ACTIVE) &&
akmhoque157b0a42014-05-13 00:26:37 -0500162 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500163 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600164
165 m_nlsr.getLsdb().scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 }
167}
168
Nick G97e34942016-07-11 14:46:27 -0500169 // This is the first function that incoming Hello data will
170 // see. This checks if the data appears to be signed, and passes it
171 // on to validate the content of the data.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700172void
173HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
174{
akmhoquedfe615f2014-07-27 14:12:21 -0500175 _LOG_DEBUG("Received data for INFO(name): " << data.getName());
176 if (data.getSignature().hasKeyLocator()) {
177 if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
178 _LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
179 }
180 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700181 m_nlsr.getValidator().validate(data,
182 ndn::bind(&HelloProtocol::onContentValidated, this, _1),
183 ndn::bind(&HelloProtocol::onContentValidationFailed,
184 this, _1, _2));
185}
akmhoque31d1d4b2014-05-05 22:08:14 -0500186
Nick G97e34942016-07-11 14:46:27 -0500187 // A validator is called on the incoming data, and if the data
188 // passes the validator's description/definitions, this function is
189 // called. Set the neighbor's status to active and refresh its
190 // LSA. If there was a change in status, we schedule an adjacency
191 // LSA build.
akmhoque31d1d4b2014-05-05 22:08:14 -0500192void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700193HelloProtocol::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500194{
akmhoque93f1a072014-06-19 16:24:28 -0500195 /* data name: /<neighbor>/NLSR/INFO/<router>/<version> */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700196 ndn::Name dataName = data->getName();
akmhoquedfe615f2014-07-27 14:12:21 -0500197 _LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500198 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500199 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500200
201 Adjacent::Status oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
202 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500203 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500204 Adjacent::Status newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
205
akmhoque2f423352014-06-03 11:49:35 -0500206 _LOG_DEBUG("Neighbor : " << neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500207 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500208 // change in Adjacency list
209 if ((oldStatus - newStatus) != 0) {
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600210 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
211 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
212 }
213 else {
214 m_nlsr.getLsdb().scheduleAdjLsaBuild();
215 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500216 }
217 }
218}
219
Nick G97e34942016-07-11 14:46:27 -0500220 // Simply logs a debug message that the content could not be
221 // validated (and is implicitly being discarded as a result).
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700222void
223HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
224 const std::string& msg)
225{
226 _LOG_DEBUG("Validation Error: " << msg);
227}
228
Nick G97e34942016-07-11 14:46:27 -0500229
230 // Asks the FIB to register the supplied adjacency (in other words,
231 // create a Face for it).
akmhoquec04e7272014-07-02 11:00:14 -0500232void
akmhoque8e0252b2014-07-07 16:04:44 -0500233HelloProtocol::registerPrefixes(const ndn::Name& adjName, const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500234 double linkCost, const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500235{
akmhoque060d3022014-08-12 13:35:06 -0500236 m_nlsr.getFib().registerPrefix(adjName, faceUri, linkCost, timeout,
237 ndn::nfd::ROUTE_FLAG_CAPTURE, 0,
akmhoquec04e7272014-07-02 11:00:14 -0500238 ndn::bind(&HelloProtocol::onRegistrationSuccess,
akmhoque102aea42014-08-04 10:22:12 -0500239 this, _1, adjName,timeout),
akmhoquec04e7272014-07-02 11:00:14 -0500240 ndn::bind(&HelloProtocol::onRegistrationFailure,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000241 this, _1, adjName));
akmhoquec04e7272014-07-02 11:00:14 -0500242}
243
Nick G97e34942016-07-11 14:46:27 -0500244 // After we create a new Face, we need to set it up for use. This
245 // function sets the controlling strategy, registers prefixes in
246 // sync, broadcast, and LSA.
akmhoquec04e7272014-07-02 11:00:14 -0500247void
248HelloProtocol::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
akmhoque102aea42014-08-04 10:22:12 -0500249 const ndn::Name& neighbor,const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500250{
251 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
252 if (adjacent != 0) {
253 adjacent->setFaceId(commandSuccessResult.getFaceId());
akmhoque102aea42014-08-04 10:22:12 -0500254 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
255 broadcastKeyPrefix.append("KEYS");
256 std::string faceUri = adjacent->getConnectingFaceUri();
257 double linkCost = adjacent->getLinkCost();
258 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getChronosyncPrefix(),
akmhoque060d3022014-08-12 13:35:06 -0500259 faceUri, linkCost, timeout,
260 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500261 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getLsaPrefix(),
akmhoque060d3022014-08-12 13:35:06 -0500262 faceUri, linkCost, timeout,
263 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500264 m_nlsr.getFib().registerPrefix(broadcastKeyPrefix,
akmhoque060d3022014-08-12 13:35:06 -0500265 faceUri, linkCost, timeout,
266 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
akmhoque102aea42014-08-04 10:22:12 -0500267
Nick G97e34942016-07-11 14:46:27 -0500268 // Sends a Hello Interest to determine status before the next scheduled.
akmhoquec04e7272014-07-02 11:00:14 -0500269 /* interest name: /<neighbor>/NLSR/INFO/<router> */
270 ndn::Name interestName(neighbor);
271 interestName.append(NLSR_COMPONENT);
272 interestName.append(INFO_COMPONENT);
273 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
274 expressInterest(interestName,
275 m_nlsr.getConfParameter().getInterestResendTime());
276 }
277}
278
279void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000280HelloProtocol::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoquedfe615f2014-07-27 14:12:21 -0500281 const ndn::Name& name)
akmhoquec04e7272014-07-02 11:00:14 -0500282{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000283 _LOG_DEBUG(response.getText() << " (code: " << response.getCode() << ")");
akmhoquedfe615f2014-07-27 14:12:21 -0500284 /*
285 * If NLSR can not create face for given faceUri then it will treat this
286 * failure as one INFO interest timed out. So that NLSR can move on with
287 * building Adj Lsa and calculate routing table. NLSR does not build Adj
288 * Lsa unless all the neighbors are ACTIVE or DEAD. For considering the
289 * missconfigured(link) neighbour dead this is required.
290 */
291 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(name);
292 if (adjacent != 0) {
293 adjacent->setInterestTimedOutNo(adjacent->getInterestTimedOutNo() + 1);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500294 Adjacent::Status status = adjacent->getStatus();
akmhoquedfe615f2014-07-27 14:12:21 -0500295 uint32_t infoIntTimedOutCount = adjacent->getInterestTimedOutNo();
296
297 if (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500298 if (status == Adjacent::STATUS_ACTIVE) {
299 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
akmhoquedfe615f2014-07-27 14:12:21 -0500300 }
Vince Lehman50df6b72015-03-03 12:06:40 -0600301
302 m_nlsr.getLsdb().scheduleAdjLsaBuild();
akmhoquedfe615f2014-07-27 14:12:21 -0500303 }
304 }
akmhoquec04e7272014-07-02 11:00:14 -0500305}
306
Nick Gordonfad8e252016-08-11 14:21:38 -0500307} // namespace nlsr