blob: e11f0e4571ff27ba1f664249fa6bf7f9dd60d1e3 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
Ashlesh Gawandee63b7fa2021-04-06 21:43:17 -07003 * Copyright (c) 2014-2021, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * 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/>.
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040019 */
Junxiao Shi63bd0342016-08-17 16:57:14 +000020
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060021#include "hello-protocol.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050022#include "nlsr.hpp"
23#include "lsdb.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050024#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050025#include "logger.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050026
Ashlesh Gawande214032e2020-12-22 17:20:14 -050027#include <ndn-cxx/encoding/nfd-constants.hpp>
28
akmhoque31d1d4b2014-05-05 22:08:14 -050029namespace nlsr {
30
dmcoomescf8d0ed2017-02-21 11:39:01 -060031INIT_LOGGER(HelloProtocol);
akmhoque674b0b12014-05-20 14:33:28 -050032
akmhoque93f1a072014-06-19 16:24:28 -050033const std::string HelloProtocol::INFO_COMPONENT = "INFO";
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -050034const std::string HelloProtocol::NLSR_COMPONENT = "nlsr";
akmhoque157b0a42014-05-13 00:26:37 -050035
Ashlesh Gawande85998a12017-12-07 22:22:13 -060036HelloProtocol::HelloProtocol(ndn::Face& face, ndn::KeyChain& keyChain,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060037 ConfParameter& confParam, RoutingTable& routingTable,
38 Lsdb& lsdb)
39 : m_face(face)
40 , m_scheduler(m_face.getIoService())
41 , m_keyChain(keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060042 , m_signingInfo(confParam.getSigningInfo())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , m_confParam(confParam)
44 , m_routingTable(routingTable)
45 , m_lsdb(lsdb)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050046 , m_adjacencyList(m_confParam.getAdjacencyList())
Nick Gordone18296c2017-10-11 16:05:24 -050047{
Ashlesh Gawande214032e2020-12-22 17:20:14 -050048 ndn::Name name(m_confParam.getRouterPrefix());
49 name.append(NLSR_COMPONENT);
50 name.append(INFO_COMPONENT);
51
52 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
53
54 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
55 [this] (const auto& name, const auto& interest) {
56 processInterest(name, interest);
57 },
58 [] (const auto& name) {
59 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
60 },
61 [] (const auto& name, const auto& resp) {
62 NLSR_LOG_ERROR("Failed to register prefix " << name);
63 NDN_THROW(std::runtime_error("Failed to register hello prefix: " + resp));
64 },
65 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Nick Gordone18296c2017-10-11 16:05:24 -050066}
67
akmhoque31d1d4b2014-05-05 22:08:14 -050068void
69HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
70{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050071 NLSR_LOG_DEBUG("Expressing Interest: " << interestName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072 ndn::Interest interest(interestName);
73 interest.setInterestLifetime(ndn::time::seconds(seconds));
74 interest.setMustBeFresh(true);
75 interest.setCanBePrefix(true);
76 m_face.expressInterest(interest,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050077 std::bind(&HelloProtocol::onContent, this, _1, _2),
78 [this, seconds] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
79 {
80 NDN_LOG_TRACE("Received Nack with reason: " << nack.getReason());
81 NDN_LOG_TRACE("Will treat as timeout in " << 2 * seconds << " seconds");
82 m_scheduler.schedule(ndn::time::seconds(2 * seconds),
83 [this, interest] { processInterestTimedOut(interest); });
84 },
85 std::bind(&HelloProtocol::processInterestTimedOut, this, _1));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060086
87 // increment SENT_HELLO_INTEREST
88 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_INTEREST);
akmhoque31d1d4b2014-05-05 22:08:14 -050089}
90
91void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050092HelloProtocol::sendHelloInterest(const ndn::Name& neighbor)
akmhoque31d1d4b2014-05-05 22:08:14 -050093{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050094 auto adjacent = m_adjacencyList.findAdjacent(neighbor);
95
96 if (adjacent == m_adjacencyList.end()) {
97 return;
akmhoque31d1d4b2014-05-05 22:08:14 -050098 }
akmhoque31d1d4b2014-05-05 22:08:14 -050099
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500100 // If this adjacency has a Face, just proceed as usual.
101 if(adjacent->getFaceId() != 0) {
102 // interest name: /<neighbor>/NLSR/INFO/<router>
103 ndn::Name interestName = adjacent->getName() ;
104 interestName.append(NLSR_COMPONENT);
105 interestName.append(INFO_COMPONENT);
106 interestName.append(m_confParam.getRouterPrefix().wireEncode());
107 expressInterest(interestName, m_confParam.getInterestResendTime());
108 NLSR_LOG_DEBUG("Sending HELLO interest: " << interestName);
109 }
Vince Lehman50df6b72015-03-03 12:06:40 -0600110
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500111 m_scheduler.schedule(ndn::time::seconds(m_confParam.getInfoInterestInterval()),
112 [this, neighbor] { sendHelloInterest(neighbor); });
akmhoque31d1d4b2014-05-05 22:08:14 -0500113}
114
115void
116HelloProtocol::processInterest(const ndn::Name& name,
117 const ndn::Interest& interest)
118{
dmcoomes9f936662017-03-02 10:33:09 -0600119 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500120 const ndn::Name interestName = interest.getName();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600121
122 // increment RCV_HELLO_INTEREST
123 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_INTEREST);
124
dmcoomes5bcb39e2017-10-31 15:07:55 -0500125 NLSR_LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500126 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500127 NLSR_LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName
dmcoomes9eaf3f42017-02-21 11:39:01 -0600128 << " does not match expression");
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 return;
130 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600131
akmhoque157b0a42014-05-13 00:26:37 -0500132 ndn::Name neighbor;
133 neighbor.wireDecode(interestName.get(-1).blockFromValue());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500134 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500135 if (m_adjacencyList.isNeighbor(neighbor)) {
dmcoomes9f936662017-03-02 10:33:09 -0600136 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
akmhoque69c9aa92014-07-23 15:15:05 -0500137 data->setName(ndn::Name(interest.getName()).appendVersion());
138 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
139 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140 INFO_COMPONENT.size());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500141
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600142 m_keyChain.sign(*data, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500143
dmcoomes5bcb39e2017-10-31 15:07:55 -0500144 NLSR_LOG_DEBUG("Sending out data for name: " << interest.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500145
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600146 m_face.put(*data);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600147 // increment SENT_HELLO_DATA
148 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_DATA);
Nick Gordonc780a692017-04-27 18:03:02 -0500149
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500150 auto adjacent = m_adjacencyList.findAdjacent(neighbor);
Nick G97e34942016-07-11 14:46:27 -0500151 // If this neighbor was previously inactive, send our own hello interest, too
Vince Lehmancb76ade2014-08-28 21:24:41 -0500152 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
Nick G97e34942016-07-11 14:46:27 -0500153 // We can only do that if the neighbor currently has a face.
akmhoquec04e7272014-07-02 11:00:14 -0500154 if(adjacent->getFaceId() != 0){
dmcoomes9f936662017-03-02 10:33:09 -0600155 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoquec04e7272014-07-02 11:00:14 -0500156 ndn::Name interestName(neighbor);
157 interestName.append(NLSR_COMPONENT);
158 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600159 interestName.append(m_confParam.getRouterPrefix().wireEncode());
160 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoquec04e7272014-07-02 11:00:14 -0500161 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500162 }
163 }
164}
165
166void
167HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
168{
dmcoomes9f936662017-03-02 10:33:09 -0600169 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500170 const ndn::Name interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500171 NLSR_LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500172 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500173 return;
174 }
akmhoque93f1a072014-06-19 16:24:28 -0500175 ndn::Name neighbor = interestName.getPrefix(-3);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500176 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500177 m_adjacencyList.incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500178
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500179 Adjacent::Status status = m_adjacencyList.getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500180
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500181 uint32_t infoIntTimedOutCount = m_adjacencyList.getTimedOutInterestCount(neighbor);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500182 NLSR_LOG_DEBUG("Status: " << status);
183 NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500184 if (infoIntTimedOutCount < m_confParam.getInterestRetryNumber()) {
dmcoomes9f936662017-03-02 10:33:09 -0600185 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500186 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500187 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500188 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600189 interestName.append(m_confParam.getRouterPrefix().wireEncode());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500190 NLSR_LOG_DEBUG("Resending interest: " << interestName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600191 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoque31d1d4b2014-05-05 22:08:14 -0500192 }
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500193 else if (status == Adjacent::STATUS_ACTIVE) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500194 m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600195
dmcoomes5bcb39e2017-10-31 15:07:55 -0500196 NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
dmcoomes9eaf3f42017-02-21 11:39:01 -0600197
Ashlesh Gawande214032e2020-12-22 17:20:14 -0500198 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
199 m_routingTable.scheduleRoutingTableCalculation();
200 }
201 else {
202 m_lsdb.scheduleAdjLsaBuild();
203 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500204 }
205}
206
Nick G97e34942016-07-11 14:46:27 -0500207 // This is the first function that incoming Hello data will
208 // see. This checks if the data appears to be signed, and passes it
209 // on to validate the content of the data.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700210void
211HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
212{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500213 NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700214 auto kl = data.getKeyLocator();
215 if (kl && kl->getType() == ndn::tlv::Name) {
216 NLSR_LOG_DEBUG("Data signed with: " << kl->getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500217 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600218 m_confParam.getValidator().validate(data,
219 std::bind(&HelloProtocol::onContentValidated, this, _1),
220 std::bind(&HelloProtocol::onContentValidationFailed,
221 this, _1, _2));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700222}
akmhoque31d1d4b2014-05-05 22:08:14 -0500223
224void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500225HelloProtocol::onContentValidated(const ndn::Data& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500226{
dmcoomes9f936662017-03-02 10:33:09 -0600227 // data name: /<neighbor>/NLSR/INFO/<router>/<version>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500228 ndn::Name dataName = data.getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500229 NLSR_LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500230
akmhoque157b0a42014-05-13 00:26:37 -0500231 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500232 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500233
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500234 Adjacent::Status oldStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
235 m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
236 m_adjacencyList.setTimedOutInterestCount(neighbor, 0);
237 Adjacent::Status newStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500238
dmcoomes5bcb39e2017-10-31 15:07:55 -0500239 NLSR_LOG_DEBUG("Neighbor : " << neighbor);
240 NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500241 // change in Adjacency list
242 if ((oldStatus - newStatus) != 0) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600243 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
244 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600245 }
246 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600247 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600248 }
Ashlesh Gawandee63b7fa2021-04-06 21:43:17 -0700249 onInitialHelloDataValidated(neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500250 }
251 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600252 // increment RCV_HELLO_DATA
253 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -0500254}
255
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700256void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500257HelloProtocol::onContentValidationFailed(const ndn::Data& data,
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400258 const ndn::security::ValidationError& ve)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700259{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500260 NLSR_LOG_DEBUG("Validation Error: " << ve);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700261}
262
Nick Gordonfad8e252016-08-11 14:21:38 -0500263} // namespace nlsr