blob: f5c0929bd59f7eb54efae25a9a8bf54ac6a2752a [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/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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
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,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060035 ConfParameter& confParam, RoutingTable& routingTable,
36 Lsdb& lsdb)
37 : m_face(face)
38 , m_scheduler(m_face.getIoService())
39 , m_keyChain(keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060040 , m_signingInfo(confParam.getSigningInfo())
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041 , m_confParam(confParam)
42 , m_routingTable(routingTable)
43 , m_lsdb(lsdb)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050044 , m_adjacencyList(m_confParam.getAdjacencyList())
Nick Gordone18296c2017-10-11 16:05:24 -050045{
46}
47
akmhoque31d1d4b2014-05-05 22:08:14 -050048void
49HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
50{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050051 NLSR_LOG_DEBUG("Expressing Interest: " << interestName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060052 ndn::Interest interest(interestName);
53 interest.setInterestLifetime(ndn::time::seconds(seconds));
54 interest.setMustBeFresh(true);
55 interest.setCanBePrefix(true);
56 m_face.expressInterest(interest,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050057 std::bind(&HelloProtocol::onContent, this, _1, _2),
58 [this, seconds] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
59 {
60 NDN_LOG_TRACE("Received Nack with reason: " << nack.getReason());
61 NDN_LOG_TRACE("Will treat as timeout in " << 2 * seconds << " seconds");
62 m_scheduler.schedule(ndn::time::seconds(2 * seconds),
63 [this, interest] { processInterestTimedOut(interest); });
64 },
65 std::bind(&HelloProtocol::processInterestTimedOut, this, _1));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060066
67 // increment SENT_HELLO_INTEREST
68 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_INTEREST);
akmhoque31d1d4b2014-05-05 22:08:14 -050069}
70
71void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050072HelloProtocol::sendHelloInterest(const ndn::Name& neighbor)
akmhoque31d1d4b2014-05-05 22:08:14 -050073{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050074 auto adjacent = m_adjacencyList.findAdjacent(neighbor);
75
76 if (adjacent == m_adjacencyList.end()) {
77 return;
akmhoque31d1d4b2014-05-05 22:08:14 -050078 }
akmhoque31d1d4b2014-05-05 22:08:14 -050079
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050080 // If this adjacency has a Face, just proceed as usual.
81 if(adjacent->getFaceId() != 0) {
82 // interest name: /<neighbor>/NLSR/INFO/<router>
83 ndn::Name interestName = adjacent->getName() ;
84 interestName.append(NLSR_COMPONENT);
85 interestName.append(INFO_COMPONENT);
86 interestName.append(m_confParam.getRouterPrefix().wireEncode());
87 expressInterest(interestName, m_confParam.getInterestResendTime());
88 NLSR_LOG_DEBUG("Sending HELLO interest: " << interestName);
89 }
Vince Lehman50df6b72015-03-03 12:06:40 -060090
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050091 m_scheduler.schedule(ndn::time::seconds(m_confParam.getInfoInterestInterval()),
92 [this, neighbor] { sendHelloInterest(neighbor); });
akmhoque31d1d4b2014-05-05 22:08:14 -050093}
94
95void
96HelloProtocol::processInterest(const ndn::Name& name,
97 const ndn::Interest& interest)
98{
dmcoomes9f936662017-03-02 10:33:09 -060099 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 const ndn::Name interestName = interest.getName();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600101
102 // increment RCV_HELLO_INTEREST
103 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_INTEREST);
104
dmcoomes5bcb39e2017-10-31 15:07:55 -0500105 NLSR_LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500106 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500107 NLSR_LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName
dmcoomes9eaf3f42017-02-21 11:39:01 -0600108 << " does not match expression");
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 return;
110 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600111
akmhoque157b0a42014-05-13 00:26:37 -0500112 ndn::Name neighbor;
113 neighbor.wireDecode(interestName.get(-1).blockFromValue());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500114 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500115 if (m_adjacencyList.isNeighbor(neighbor)) {
dmcoomes9f936662017-03-02 10:33:09 -0600116 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
akmhoque69c9aa92014-07-23 15:15:05 -0500117 data->setName(ndn::Name(interest.getName()).appendVersion());
118 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
119 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600120 INFO_COMPONENT.size());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500121
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600122 m_keyChain.sign(*data, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500123
dmcoomes5bcb39e2017-10-31 15:07:55 -0500124 NLSR_LOG_DEBUG("Sending out data for name: " << interest.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500125
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600126 m_face.put(*data);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600127 // increment SENT_HELLO_DATA
128 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_DATA);
Nick Gordonc780a692017-04-27 18:03:02 -0500129
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500130 auto adjacent = m_adjacencyList.findAdjacent(neighbor);
Nick G97e34942016-07-11 14:46:27 -0500131 // If this neighbor was previously inactive, send our own hello interest, too
Vince Lehmancb76ade2014-08-28 21:24:41 -0500132 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
Nick G97e34942016-07-11 14:46:27 -0500133 // We can only do that if the neighbor currently has a face.
akmhoquec04e7272014-07-02 11:00:14 -0500134 if(adjacent->getFaceId() != 0){
dmcoomes9f936662017-03-02 10:33:09 -0600135 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoquec04e7272014-07-02 11:00:14 -0500136 ndn::Name interestName(neighbor);
137 interestName.append(NLSR_COMPONENT);
138 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600139 interestName.append(m_confParam.getRouterPrefix().wireEncode());
140 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoquec04e7272014-07-02 11:00:14 -0500141 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500142 }
143 }
144}
145
146void
147HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
148{
dmcoomes9f936662017-03-02 10:33:09 -0600149 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 const ndn::Name interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500151 NLSR_LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500152 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500153 return;
154 }
akmhoque93f1a072014-06-19 16:24:28 -0500155 ndn::Name neighbor = interestName.getPrefix(-3);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500156 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500157 m_adjacencyList.incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500158
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500159 Adjacent::Status status = m_adjacencyList.getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500160
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500161 uint32_t infoIntTimedOutCount = m_adjacencyList.getTimedOutInterestCount(neighbor);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500162 NLSR_LOG_DEBUG("Status: " << status);
163 NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500164 if (infoIntTimedOutCount <= m_confParam.getInterestRetryNumber()) {
dmcoomes9f936662017-03-02 10:33:09 -0600165 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500167 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500168 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600169 interestName.append(m_confParam.getRouterPrefix().wireEncode());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500170 NLSR_LOG_DEBUG("Resending interest: " << interestName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600171 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500173 else if ((status == Adjacent::STATUS_ACTIVE) &&
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174 (infoIntTimedOutCount == m_confParam.getInterestRetryNumber())) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500175 m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600176
dmcoomes5bcb39e2017-10-31 15:07:55 -0500177 NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
dmcoomes9eaf3f42017-02-21 11:39:01 -0600178
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600179 m_lsdb.scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500180 }
181}
182
Nick G97e34942016-07-11 14:46:27 -0500183 // This is the first function that incoming Hello data will
184 // see. This checks if the data appears to be signed, and passes it
185 // on to validate the content of the data.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700186void
187HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
188{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500189 NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500190 if (data.getSignature().hasKeyLocator() &&
191 data.getSignature().getKeyLocator().getType() == ndn::tlv::Name) {
192 NLSR_LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500193 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600194 m_confParam.getValidator().validate(data,
195 std::bind(&HelloProtocol::onContentValidated, this, _1),
196 std::bind(&HelloProtocol::onContentValidationFailed,
197 this, _1, _2));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700198}
akmhoque31d1d4b2014-05-05 22:08:14 -0500199
200void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500201HelloProtocol::onContentValidated(const ndn::Data& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500202{
dmcoomes9f936662017-03-02 10:33:09 -0600203 // data name: /<neighbor>/NLSR/INFO/<router>/<version>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500204 ndn::Name dataName = data.getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500205 NLSR_LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500206
akmhoque157b0a42014-05-13 00:26:37 -0500207 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500208 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500209
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500210 Adjacent::Status oldStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
211 m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
212 m_adjacencyList.setTimedOutInterestCount(neighbor, 0);
213 Adjacent::Status newStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500214
dmcoomes5bcb39e2017-10-31 15:07:55 -0500215 NLSR_LOG_DEBUG("Neighbor : " << neighbor);
216 NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500217 // change in Adjacency list
218 if ((oldStatus - newStatus) != 0) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600219 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
220 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600221 }
222 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600223 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600224 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500225 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500226
227 onHelloDataValidated(neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500228 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600229 // increment RCV_HELLO_DATA
230 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -0500231}
232
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700233void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500234HelloProtocol::onContentValidationFailed(const ndn::Data& data,
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400235 const ndn::security::ValidationError& ve)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700236{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500237 NLSR_LOG_DEBUG("Validation Error: " << ve);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700238}
239
Nick Gordonfad8e252016-08-11 14:21:38 -0500240} // namespace nlsr