blob: a5f935cb5cd5af8929cb52737bea3dfb18810f35 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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
Nick Gordone18296c2017-10-11 16:05:24 -050035HelloProtocol::HelloProtocol(Nlsr& nlsr, ndn::Scheduler& scheduler)
36 : m_nlsr(nlsr)
37 , m_scheduler(scheduler)
38{
39}
40
akmhoque31d1d4b2014-05-05 22:08:14 -050041void
42HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
43{
dmcoomes5bcb39e2017-10-31 15:07:55 -050044 NLSR_LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050045 ndn::Interest i(interestName);
46 i.setInterestLifetime(ndn::time::seconds(seconds));
47 i.setMustBeFresh(true);
Ashlesh Gawande05700542018-08-03 10:40:19 -050048 i.setCanBePrefix(true);
akmhoque31d1d4b2014-05-05 22:08:14 -050049 m_nlsr.getNlsrFace().expressInterest(i,
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060050 std::bind(&HelloProtocol::onContent, this, _1, _2),
51 [this] (const ndn::Interest& interest,
52 const ndn::lp::Nack& nack)
53 {
54 NDN_LOG_TRACE("Received Nack with reason " <<
55 nack.getReason());
56 NDN_LOG_TRACE("Treating as timeout");
57 processInterestTimedOut(interest);
58 },
dmcoomes9f936662017-03-02 10:33:09 -060059 std::bind(&HelloProtocol::processInterestTimedOut,
akmhoque31d1d4b2014-05-05 22:08:14 -050060 this, _1));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060061
62 // increment SENT_HELLO_INTEREST
63 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_INTEREST);
akmhoque31d1d4b2014-05-05 22:08:14 -050064}
65
66void
67HelloProtocol::sendScheduledInterest(uint32_t seconds)
68{
69 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
70 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050071 ++it) {
Nick G97e34942016-07-11 14:46:27 -050072 // If this adjacency has a Face, just proceed as usual.
akmhoquec04e7272014-07-02 11:00:14 -050073 if((*it).getFaceId() != 0) {
dmcoomes9f936662017-03-02 10:33:09 -060074 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoquec04e7272014-07-02 11:00:14 -050075 ndn::Name interestName = (*it).getName() ;
76 interestName.append(NLSR_COMPONENT);
77 interestName.append(INFO_COMPONENT);
78 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
79 expressInterest(interestName,
80 m_nlsr.getConfParameter().getInterestResendTime());
dmcoomes5bcb39e2017-10-31 15:07:55 -050081 NLSR_LOG_DEBUG("Sending scheduled interest: " << interestName);
akmhoquec04e7272014-07-02 11:00:14 -050082 }
akmhoque31d1d4b2014-05-05 22:08:14 -050083 }
84 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
85}
86
87void
88HelloProtocol::scheduleInterest(uint32_t seconds)
89{
dmcoomes5bcb39e2017-10-31 15:07:55 -050090 NLSR_LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
Vince Lehman50df6b72015-03-03 12:06:40 -060091
Vince Lehman7c603292014-09-11 17:48:16 -050092 m_scheduler.scheduleEvent(ndn::time::seconds(seconds),
dmcoomes9f936662017-03-02 10:33:09 -060093 std::bind(&HelloProtocol::sendScheduledInterest, this, seconds));
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);
akmhoque157b0a42014-05-13 00:26:37 -0500116 if (m_nlsr.getAdjacencyList().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()),
akmhoque157b0a42014-05-13 00:26:37 -0500121 INFO_COMPONENT.size());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500122
123 m_nlsr.getKeyChain().sign(*data, m_nlsr.getSigningInfo());
124
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
akmhoque69c9aa92014-07-23 15:15:05 -0500127 m_nlsr.getNlsrFace().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
131 auto adjacent = m_nlsr.getAdjacencyList().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);
140 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
141 expressInterest(interestName,
142 m_nlsr.getConfParameter().getInterestResendTime());
143 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500144 }
145 }
146}
147
148void
149HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
150{
dmcoomes9f936662017-03-02 10:33:09 -0600151 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500152 const ndn::Name interestName(interest.getName());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500153 NLSR_LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500154 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 return;
156 }
akmhoque93f1a072014-06-19 16:24:28 -0500157 ndn::Name neighbor = interestName.getPrefix(-3);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500158 NLSR_LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500159 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500160
161 Adjacent::Status status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
162
akmhoque31d1d4b2014-05-05 22:08:14 -0500163 uint32_t infoIntTimedOutCount =
164 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
dmcoomes5bcb39e2017-10-31 15:07:55 -0500165 NLSR_LOG_DEBUG("Status: " << status);
166 NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
Nick Gordond0a7df32017-05-30 16:44:34 -0500167 if (infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber()) {
dmcoomes9f936662017-03-02 10:33:09 -0600168 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500169 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500170 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500171 interestName.append(INFO_COMPONENT);
172 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500173 NLSR_LOG_DEBUG("Resending interest: " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 expressInterest(interestName,
175 m_nlsr.getConfParameter().getInterestResendTime());
176 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500177 else if ((status == Adjacent::STATUS_ACTIVE) &&
akmhoque157b0a42014-05-13 00:26:37 -0500178 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500179 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600180
dmcoomes5bcb39e2017-10-31 15:07:55 -0500181 NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
dmcoomes9eaf3f42017-02-21 11:39:01 -0600182
Vince Lehman50df6b72015-03-03 12:06:40 -0600183 m_nlsr.getLsdb().scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500184 }
185}
186
Nick G97e34942016-07-11 14:46:27 -0500187 // This is the first function that incoming Hello data will
188 // see. This checks if the data appears to be signed, and passes it
189 // on to validate the content of the data.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700190void
191HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
192{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500193 NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500194 if (data.getSignature().hasKeyLocator()) {
195 if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500196 NLSR_LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
akmhoquedfe615f2014-07-27 14:12:21 -0500197 }
198 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700199 m_nlsr.getValidator().validate(data,
dmcoomes9f936662017-03-02 10:33:09 -0600200 std::bind(&HelloProtocol::onContentValidated, this, _1),
201 std::bind(&HelloProtocol::onContentValidationFailed,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700202 this, _1, _2));
203}
akmhoque31d1d4b2014-05-05 22:08:14 -0500204
205void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500206HelloProtocol::onContentValidated(const ndn::Data& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500207{
dmcoomes9f936662017-03-02 10:33:09 -0600208 // data name: /<neighbor>/NLSR/INFO/<router>/<version>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500209 ndn::Name dataName = data.getName();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500210 NLSR_LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500211
akmhoque157b0a42014-05-13 00:26:37 -0500212 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500213 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500214
215 Adjacent::Status oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
216 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500217 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500218 Adjacent::Status newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
219
dmcoomes5bcb39e2017-10-31 15:07:55 -0500220 NLSR_LOG_DEBUG("Neighbor : " << neighbor);
221 NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500222 // change in Adjacency list
223 if ((oldStatus - newStatus) != 0) {
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600224 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
225 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
226 }
227 else {
228 m_nlsr.getLsdb().scheduleAdjLsaBuild();
229 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500230 }
231 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600232 // increment RCV_HELLO_DATA
233 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -0500234}
235
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700236void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500237HelloProtocol::onContentValidationFailed(const ndn::Data& data,
238 const ndn::security::v2::ValidationError& ve)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700239{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500240 NLSR_LOG_DEBUG("Validation Error: " << ve);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700241}
242
Nick Gordonfad8e252016-08-11 14:21:38 -0500243} // namespace nlsr