blob: 67c948ff547f9717c94cc14bc731b81260fb3c60 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 **/
Nick Gordond0a7df32017-05-30 16:44:34 -050020#include "hello-protocol.hpp"
Junxiao Shi63bd0342016-08-17 16:57:14 +000021
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060022#include "hello-protocol.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050023#include "nlsr.hpp"
24#include "lsdb.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050025#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050026#include "logger.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050027
28namespace nlsr {
29
dmcoomescf8d0ed2017-02-21 11:39:01 -060030INIT_LOGGER(HelloProtocol);
akmhoque674b0b12014-05-20 14:33:28 -050031
akmhoque93f1a072014-06-19 16:24:28 -050032const std::string HelloProtocol::INFO_COMPONENT = "INFO";
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -050033const std::string HelloProtocol::NLSR_COMPONENT = "nlsr";
akmhoque157b0a42014-05-13 00:26:37 -050034
Ashlesh Gawande85998a12017-12-07 22:22:13 -060035HelloProtocol::HelloProtocol(ndn::Face& face, ndn::KeyChain& keyChain,
36 ndn::security::SigningInfo& signingInfo,
37 ConfParameter& confParam, RoutingTable& routingTable,
38 Lsdb& lsdb)
39 : m_face(face)
40 , m_scheduler(m_face.getIoService())
41 , m_keyChain(keyChain)
42 , m_signingInfo(signingInfo)
43 , m_confParam(confParam)
44 , m_routingTable(routingTable)
45 , m_lsdb(lsdb)
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{
dmcoomes5bcb39e2017-10-31 15:07:55 -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,
58 std::bind(&HelloProtocol::onContent, this, _1, _2),
59 [this] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
60 {
61 NDN_LOG_TRACE("Received Nack with reason " << nack.getReason());
62 NDN_LOG_TRACE("Treating as timeout");
63 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 Gawande85998a12017-12-07 22:22:13 -060072HelloProtocol::sendScheduledInterest()
akmhoque31d1d4b2014-05-05 22:08:14 -050073{
Ashlesh Gawande85998a12017-12-07 22:22:13 -060074 for (const auto& adjacent : m_confParam.getAdjacencyList().getAdjList()) {
Nick G97e34942016-07-11 14:46:27 -050075 // If this adjacency has a Face, just proceed as usual.
Ashlesh Gawande85998a12017-12-07 22:22:13 -060076 if(adjacent.getFaceId() != 0) {
dmcoomes9f936662017-03-02 10:33:09 -060077 // interest name: /<neighbor>/NLSR/INFO/<router>
Ashlesh Gawande85998a12017-12-07 22:22:13 -060078 ndn::Name interestName = adjacent.getName() ;
akmhoquec04e7272014-07-02 11:00:14 -050079 interestName.append(NLSR_COMPONENT);
80 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060081 interestName.append(m_confParam.getRouterPrefix().wireEncode());
82 expressInterest(interestName, m_confParam.getInterestResendTime());
dmcoomes5bcb39e2017-10-31 15:07:55 -050083 NLSR_LOG_DEBUG("Sending scheduled interest: " << interestName);
akmhoquec04e7272014-07-02 11:00:14 -050084 }
akmhoque31d1d4b2014-05-05 22:08:14 -050085 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -060086 scheduleInterest(m_confParam.getInfoInterestInterval());
akmhoque31d1d4b2014-05-05 22:08:14 -050087}
88
89void
90HelloProtocol::scheduleInterest(uint32_t seconds)
91{
dmcoomes5bcb39e2017-10-31 15:07:55 -050092 NLSR_LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
Vince Lehman50df6b72015-03-03 12:06:40 -060093
Vince Lehman7c603292014-09-11 17:48:16 -050094 m_scheduler.scheduleEvent(ndn::time::seconds(seconds),
Ashlesh Gawande85998a12017-12-07 22:22:13 -060095 [this] {
96 sendScheduledInterest();
97 });
akmhoque31d1d4b2014-05-05 22:08:14 -050098}
99
100void
101HelloProtocol::processInterest(const ndn::Name& name,
102 const ndn::Interest& interest)
103{
dmcoomes9f936662017-03-02 10:33:09 -0600104 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500105 const ndn::Name interestName = interest.getName();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600106
107 // increment RCV_HELLO_INTEREST
108 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_INTEREST);
109
dmcoomes5bcb39e2017-10-31 15:07:55 -0500110 NLSR_LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500111 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500112 NLSR_LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName
dmcoomes9eaf3f42017-02-21 11:39:01 -0600113 << " does not match expression");
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 return;
115 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600116
akmhoque157b0a42014-05-13 00:26:37 -0500117 ndn::Name neighbor;
118 neighbor.wireDecode(interestName.get(-1).blockFromValue());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500119 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600120 if (m_confParam.getAdjacencyList().isNeighbor(neighbor)) {
dmcoomes9f936662017-03-02 10:33:09 -0600121 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
akmhoque69c9aa92014-07-23 15:15:05 -0500122 data->setName(ndn::Name(interest.getName()).appendVersion());
123 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
124 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600125 INFO_COMPONENT.size());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500126
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600127 m_keyChain.sign(*data, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500128
dmcoomes5bcb39e2017-10-31 15:07:55 -0500129 NLSR_LOG_DEBUG("Sending out data for name: " << interest.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500130
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600131 m_face.put(*data);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600132 // increment SENT_HELLO_DATA
133 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_DATA);
Nick Gordonc780a692017-04-27 18:03:02 -0500134
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600135 auto adjacent = m_confParam.getAdjacencyList().findAdjacent(neighbor);
Nick G97e34942016-07-11 14:46:27 -0500136 // If this neighbor was previously inactive, send our own hello interest, too
Vince Lehmancb76ade2014-08-28 21:24:41 -0500137 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
Nick G97e34942016-07-11 14:46:27 -0500138 // We can only do that if the neighbor currently has a face.
akmhoquec04e7272014-07-02 11:00:14 -0500139 if(adjacent->getFaceId() != 0){
dmcoomes9f936662017-03-02 10:33:09 -0600140 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoquec04e7272014-07-02 11:00:14 -0500141 ndn::Name interestName(neighbor);
142 interestName.append(NLSR_COMPONENT);
143 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600144 interestName.append(m_confParam.getRouterPrefix().wireEncode());
145 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoquec04e7272014-07-02 11:00:14 -0500146 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 }
148 }
149}
150
151void
152HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
153{
dmcoomes9f936662017-03-02 10:33:09 -0600154 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 const ndn::Name interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500156 NLSR_LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500157 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500158 return;
159 }
akmhoque93f1a072014-06-19 16:24:28 -0500160 ndn::Name neighbor = interestName.getPrefix(-3);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500161 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600162 m_confParam.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500163
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600164 Adjacent::Status status = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500165
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 uint32_t infoIntTimedOutCount =
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600167 m_confParam.getAdjacencyList().getTimedOutInterestCount(neighbor);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500168 NLSR_LOG_DEBUG("Status: " << status);
169 NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600170 if (infoIntTimedOutCount < m_confParam.getInterestRetryNumber()) {
dmcoomes9f936662017-03-02 10:33:09 -0600171 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500173 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500174 interestName.append(INFO_COMPONENT);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600175 interestName.append(m_confParam.getRouterPrefix().wireEncode());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500176 NLSR_LOG_DEBUG("Resending interest: " << interestName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600177 expressInterest(interestName, m_confParam.getInterestResendTime());
akmhoque31d1d4b2014-05-05 22:08:14 -0500178 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500179 else if ((status == Adjacent::STATUS_ACTIVE) &&
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600180 (infoIntTimedOutCount == m_confParam.getInterestRetryNumber())) {
181 m_confParam.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600182
dmcoomes5bcb39e2017-10-31 15:07:55 -0500183 NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
dmcoomes9eaf3f42017-02-21 11:39:01 -0600184
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600185 m_lsdb.scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500186 }
187}
188
Nick G97e34942016-07-11 14:46:27 -0500189 // This is the first function that incoming Hello data will
190 // see. This checks if the data appears to be signed, and passes it
191 // on to validate the content of the data.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700192void
193HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
194{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500195 NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500196 if (data.getSignature().hasKeyLocator()) {
197 if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500198 NLSR_LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500199 }
200 }
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600201 m_confParam.getValidator().validate(data,
202 std::bind(&HelloProtocol::onContentValidated, this, _1),
203 std::bind(&HelloProtocol::onContentValidationFailed,
204 this, _1, _2));
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700205}
akmhoque31d1d4b2014-05-05 22:08:14 -0500206
207void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500208HelloProtocol::onContentValidated(const ndn::Data& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500209{
dmcoomes9f936662017-03-02 10:33:09 -0600210 // data name: /<neighbor>/NLSR/INFO/<router>/<version>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500211 ndn::Name dataName = data.getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500212 NLSR_LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500213
akmhoque157b0a42014-05-13 00:26:37 -0500214 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500215 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500216
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600217 Adjacent::Status oldStatus = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
218 m_confParam.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
219 m_confParam.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
220 Adjacent::Status newStatus = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500221
dmcoomes5bcb39e2017-10-31 15:07:55 -0500222 NLSR_LOG_DEBUG("Neighbor : " << neighbor);
223 NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500224 // change in Adjacency list
225 if ((oldStatus - newStatus) != 0) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600226 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
227 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600228 }
229 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600230 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600231 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500232 }
233 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600234 // increment RCV_HELLO_DATA
235 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -0500236}
237
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700238void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500239HelloProtocol::onContentValidationFailed(const ndn::Data& data,
240 const ndn::security::v2::ValidationError& ve)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700241{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500242 NLSR_LOG_DEBUG("Validation Error: " << ve);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700243}
244
Nick Gordonfad8e252016-08-11 14:21:38 -0500245} // namespace nlsr