blob: 3d67771e93c929d8a42511d0c5d5d6e2d4248b6a [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 Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, 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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
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
27namespace nlsr {
28
dmcoomescf8d0ed2017-02-21 11:39:01 -060029INIT_LOGGER(HelloProtocol);
akmhoque674b0b12014-05-20 14:33:28 -050030
akmhoque93f1a072014-06-19 16:24:28 -050031const std::string HelloProtocol::INFO_COMPONENT = "INFO";
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -050032const std::string HelloProtocol::NLSR_COMPONENT = "nlsr";
akmhoque157b0a42014-05-13 00:26:37 -050033
Ashlesh Gawande85998a12017-12-07 22:22:13 -060034HelloProtocol::HelloProtocol(ndn::Face& face, ndn::KeyChain& keyChain,
35 ndn::security::SigningInfo& signingInfo,
36 ConfParameter& confParam, RoutingTable& routingTable,
37 Lsdb& lsdb)
38 : m_face(face)
39 , m_scheduler(m_face.getIoService())
40 , m_keyChain(keyChain)
41 , m_signingInfo(signingInfo)
42 , m_confParam(confParam)
43 , m_routingTable(routingTable)
44 , m_lsdb(lsdb)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050045 , m_adjacencyList(m_confParam.getAdjacencyList())
Nick Gordone18296c2017-10-11 16:05:24 -050046{
47}
48
akmhoque31d1d4b2014-05-05 22:08:14 -050049void
50HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
51{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050052 NLSR_LOG_DEBUG("Expressing Interest: " << interestName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 ndn::Interest interest(interestName);
54 interest.setInterestLifetime(ndn::time::seconds(seconds));
55 interest.setMustBeFresh(true);
56 interest.setCanBePrefix(true);
57 m_face.expressInterest(interest,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050058 std::bind(&HelloProtocol::onContent, this, _1, _2),
59 [this, seconds] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
60 {
61 NDN_LOG_TRACE("Received Nack with reason: " << nack.getReason());
62 NDN_LOG_TRACE("Will treat as timeout in " << 2 * seconds << " seconds");
63 m_scheduler.schedule(ndn::time::seconds(2 * seconds),
64 [this, interest] { processInterestTimedOut(interest); });
65 },
66 std::bind(&HelloProtocol::processInterestTimedOut, this, _1));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060067
68 // increment SENT_HELLO_INTEREST
69 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_INTEREST);
akmhoque31d1d4b2014-05-05 22:08:14 -050070}
71
72void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050073HelloProtocol::sendHelloInterest(const ndn::Name& neighbor)
akmhoque31d1d4b2014-05-05 22:08:14 -050074{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050075 auto adjacent = m_adjacencyList.findAdjacent(neighbor);
76
77 if (adjacent == m_adjacencyList.end()) {
78 return;
akmhoque31d1d4b2014-05-05 22:08:14 -050079 }
akmhoque31d1d4b2014-05-05 22:08:14 -050080
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050081 // If this adjacency has a Face, just proceed as usual.
82 if(adjacent->getFaceId() != 0) {
83 // interest name: /<neighbor>/NLSR/INFO/<router>
84 ndn::Name interestName = adjacent->getName() ;
85 interestName.append(NLSR_COMPONENT);
86 interestName.append(INFO_COMPONENT);
87 interestName.append(m_confParam.getRouterPrefix().wireEncode());
88 expressInterest(interestName, m_confParam.getInterestResendTime());
89 NLSR_LOG_DEBUG("Sending HELLO interest: " << interestName);
90 }
Vince Lehman50df6b72015-03-03 12:06:40 -060091
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050092 m_scheduler.schedule(ndn::time::seconds(m_confParam.getInfoInterestInterval()),
93 [this, neighbor] { sendHelloInterest(neighbor); });
akmhoque31d1d4b2014-05-05 22:08:14 -050094}
95
96void
97HelloProtocol::processInterest(const ndn::Name& name,
98 const ndn::Interest& interest)
99{
dmcoomes9f936662017-03-02 10:33:09 -0600100 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 const ndn::Name interestName = interest.getName();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600102
103 // increment RCV_HELLO_INTEREST
104 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_INTEREST);
105
dmcoomes5bcb39e2017-10-31 15:07:55 -0500106 NLSR_LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500107 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500108 NLSR_LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName
dmcoomes9eaf3f42017-02-21 11:39:01 -0600109 << " does not match expression");
akmhoque31d1d4b2014-05-05 22:08:14 -0500110 return;
111 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600112
akmhoque157b0a42014-05-13 00:26:37 -0500113 ndn::Name neighbor;
114 neighbor.wireDecode(interestName.get(-1).blockFromValue());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500115 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500116 if (m_adjacencyList.isNeighbor(neighbor)) {
dmcoomes9f936662017-03-02 10:33:09 -0600117 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
akmhoque69c9aa92014-07-23 15:15:05 -0500118 data->setName(ndn::Name(interest.getName()).appendVersion());
119 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
120 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600121 INFO_COMPONENT.size());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500122
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123 m_keyChain.sign(*data, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500124
dmcoomes5bcb39e2017-10-31 15:07:55 -0500125 NLSR_LOG_DEBUG("Sending out data for name: " << interest.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500126
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600127 m_face.put(*data);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600128 // increment SENT_HELLO_DATA
129 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_DATA);
Nick Gordonc780a692017-04-27 18:03:02 -0500130
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500131 auto adjacent = m_adjacencyList.findAdjacent(neighbor);
Nick G97e34942016-07-11 14:46:27 -0500132 // If this neighbor was previously inactive, send our own hello interest, too
Vince Lehmancb76ade2014-08-28 21:24:41 -0500133 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
Nick G97e34942016-07-11 14:46:27 -0500134 // We can only do that if the neighbor currently has a face.
akmhoquec04e7272014-07-02 11:00:14 -0500135 if(adjacent->getFaceId() != 0){
dmcoomes9f936662017-03-02 10:33:09 -0600136 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoquec04e7272014-07-02 11:00:14 -0500137 ndn::Name interestName(neighbor);
138 interestName.append(NLSR_COMPONENT);
139 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140 interestName.append(m_confParam.getRouterPrefix().wireEncode());
141 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoquec04e7272014-07-02 11:00:14 -0500142 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 }
144 }
145}
146
147void
148HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
149{
dmcoomes9f936662017-03-02 10:33:09 -0600150 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500151 const ndn::Name interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500152 NLSR_LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500153 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500154 return;
155 }
akmhoque93f1a072014-06-19 16:24:28 -0500156 ndn::Name neighbor = interestName.getPrefix(-3);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500157 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500158 m_adjacencyList.incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500159
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500160 Adjacent::Status status = m_adjacencyList.getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500161
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500162 uint32_t infoIntTimedOutCount = m_adjacencyList.getTimedOutInterestCount(neighbor);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500163 NLSR_LOG_DEBUG("Status: " << status);
164 NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500165 if (infoIntTimedOutCount <= m_confParam.getInterestRetryNumber()) {
dmcoomes9f936662017-03-02 10:33:09 -0600166 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500167 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500168 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500169 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600170 interestName.append(m_confParam.getRouterPrefix().wireEncode());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500171 NLSR_LOG_DEBUG("Resending interest: " << interestName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600172 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoque31d1d4b2014-05-05 22:08:14 -0500173 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500174 else if ((status == Adjacent::STATUS_ACTIVE) &&
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600175 (infoIntTimedOutCount == m_confParam.getInterestRetryNumber())) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500176 m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600177
dmcoomes5bcb39e2017-10-31 15:07:55 -0500178 NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
dmcoomes9eaf3f42017-02-21 11:39:01 -0600179
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600180 m_lsdb.scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 }
182}
183
Nick G97e34942016-07-11 14:46:27 -0500184 // This is the first function that incoming Hello data will
185 // see. This checks if the data appears to be signed, and passes it
186 // on to validate the content of the data.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700187void
188HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
189{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500190 NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500191 if (data.getSignature().hasKeyLocator() &&
192 data.getSignature().getKeyLocator().getType() == ndn::tlv::Name) {
193 NLSR_LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500194 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600195 m_confParam.getValidator().validate(data,
196 std::bind(&HelloProtocol::onContentValidated, this, _1),
197 std::bind(&HelloProtocol::onContentValidationFailed,
198 this, _1, _2));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700199}
akmhoque31d1d4b2014-05-05 22:08:14 -0500200
201void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500202HelloProtocol::onContentValidated(const ndn::Data& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500203{
dmcoomes9f936662017-03-02 10:33:09 -0600204 // data name: /<neighbor>/NLSR/INFO/<router>/<version>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500205 ndn::Name dataName = data.getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500206 NLSR_LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500207
akmhoque157b0a42014-05-13 00:26:37 -0500208 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500209 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500210
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500211 Adjacent::Status oldStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
212 m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
213 m_adjacencyList.setTimedOutInterestCount(neighbor, 0);
214 Adjacent::Status newStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500215
dmcoomes5bcb39e2017-10-31 15:07:55 -0500216 NLSR_LOG_DEBUG("Neighbor : " << neighbor);
217 NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500218 // change in Adjacency list
219 if ((oldStatus - newStatus) != 0) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600220 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
221 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600222 }
223 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600224 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600225 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500226 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500227
228 onHelloDataValidated(neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500229 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600230 // increment RCV_HELLO_DATA
231 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -0500232}
233
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700234void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500235HelloProtocol::onContentValidationFailed(const ndn::Data& data,
236 const ndn::security::v2::ValidationError& ve)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700237{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500238 NLSR_LOG_DEBUG("Validation Error: " << ve);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700239}
240
Nick Gordonfad8e252016-08-11 14:21:38 -0500241} // namespace nlsr