blob: afbd564dc7ec47d2be7ec42291aae2ffa49d66db [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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
akmhoque674b0b12014-05-20 14:33:28 -050030INIT_LOGGER("HelloProtocol");
31
akmhoque93f1a072014-06-19 16:24:28 -050032const std::string HelloProtocol::INFO_COMPONENT = "INFO";
33const std::string HelloProtocol::NLSR_COMPONENT = "NLSR";
akmhoque157b0a42014-05-13 00:26:37 -050034
akmhoque31d1d4b2014-05-05 22:08:14 -050035void
36HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
37{
akmhoque674b0b12014-05-20 14:33:28 -050038 _LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050039 ndn::Interest i(interestName);
40 i.setInterestLifetime(ndn::time::seconds(seconds));
41 i.setMustBeFresh(true);
42 m_nlsr.getNlsrFace().expressInterest(i,
dmcoomes9f936662017-03-02 10:33:09 -060043 std::bind(&HelloProtocol::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -050044 this,
45 _1, _2),
Alexander Afanasyev1de901f2017-03-09 12:43:57 -080046 std::bind(&HelloProtocol::processInterestTimedOut, // Nack
47 this, _1),
dmcoomes9f936662017-03-02 10:33:09 -060048 std::bind(&HelloProtocol::processInterestTimedOut,
akmhoque31d1d4b2014-05-05 22:08:14 -050049 this, _1));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060050
51 // increment SENT_HELLO_INTEREST
52 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_INTEREST);
akmhoque31d1d4b2014-05-05 22:08:14 -050053}
54
55void
56HelloProtocol::sendScheduledInterest(uint32_t seconds)
57{
58 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
59 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050060 ++it) {
Nick G97e34942016-07-11 14:46:27 -050061 // If this adjacency has a Face, just proceed as usual.
akmhoquec04e7272014-07-02 11:00:14 -050062 if((*it).getFaceId() != 0) {
dmcoomes9f936662017-03-02 10:33:09 -060063 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoquec04e7272014-07-02 11:00:14 -050064 ndn::Name interestName = (*it).getName() ;
65 interestName.append(NLSR_COMPONENT);
66 interestName.append(INFO_COMPONENT);
67 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
68 expressInterest(interestName,
69 m_nlsr.getConfParameter().getInterestResendTime());
dmcoomes9eaf3f42017-02-21 11:39:01 -060070 _LOG_DEBUG("Sending scheduled interest: " << interestName);
akmhoquec04e7272014-07-02 11:00:14 -050071 }
akmhoque31d1d4b2014-05-05 22:08:14 -050072 }
73 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
74}
75
76void
77HelloProtocol::scheduleInterest(uint32_t seconds)
78{
Vince Lehman50df6b72015-03-03 12:06:40 -060079 _LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
80
Vince Lehman7c603292014-09-11 17:48:16 -050081 m_scheduler.scheduleEvent(ndn::time::seconds(seconds),
dmcoomes9f936662017-03-02 10:33:09 -060082 std::bind(&HelloProtocol::sendScheduledInterest, this, seconds));
akmhoque31d1d4b2014-05-05 22:08:14 -050083}
84
85void
86HelloProtocol::processInterest(const ndn::Name& name,
87 const ndn::Interest& interest)
88{
dmcoomes9f936662017-03-02 10:33:09 -060089 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -050090 const ndn::Name interestName = interest.getName();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060091
92 // increment RCV_HELLO_INTEREST
93 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_INTEREST);
94
akmhoque674b0b12014-05-20 14:33:28 -050095 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050096 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
dmcoomes9eaf3f42017-02-21 11:39:01 -060097 _LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName
98 << " does not match expression");
akmhoque31d1d4b2014-05-05 22:08:14 -050099 return;
100 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600101
akmhoque157b0a42014-05-13 00:26:37 -0500102 ndn::Name neighbor;
103 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque674b0b12014-05-20 14:33:28 -0500104 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500105 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
dmcoomes9f936662017-03-02 10:33:09 -0600106 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
akmhoque69c9aa92014-07-23 15:15:05 -0500107 data->setName(ndn::Name(interest.getName()).appendVersion());
108 data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
109 data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
akmhoque157b0a42014-05-13 00:26:37 -0500110 INFO_COMPONENT.size());
akmhoque69c9aa92014-07-23 15:15:05 -0500111 m_nlsr.getKeyChain().sign(*data, m_nlsr.getDefaultCertName());
112 _LOG_DEBUG("Sending out data for name: " << interest.getName());
113 m_nlsr.getNlsrFace().put(*data);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600114 // increment SENT_HELLO_DATA
115 hpIncrementSignal(Statistics::PacketType::SENT_HELLO_DATA);
Nick Gordonc780a692017-04-27 18:03:02 -0500116
117 auto adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
Nick G97e34942016-07-11 14:46:27 -0500118 // If this neighbor was previously inactive, send our own hello interest, too
Vince Lehmancb76ade2014-08-28 21:24:41 -0500119 if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
Nick G97e34942016-07-11 14:46:27 -0500120 // We can only do that if the neighbor currently has a face.
akmhoquec04e7272014-07-02 11:00:14 -0500121 if(adjacent->getFaceId() != 0){
dmcoomes9f936662017-03-02 10:33:09 -0600122 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoquec04e7272014-07-02 11:00:14 -0500123 ndn::Name interestName(neighbor);
124 interestName.append(NLSR_COMPONENT);
125 interestName.append(INFO_COMPONENT);
126 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
127 expressInterest(interestName,
128 m_nlsr.getConfParameter().getInterestResendTime());
129 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500130 }
131 }
132}
133
134void
135HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
136{
dmcoomes9f936662017-03-02 10:33:09 -0600137 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500138 const ndn::Name interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500139 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500140 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500141 return;
142 }
akmhoque93f1a072014-06-19 16:24:28 -0500143 ndn::Name neighbor = interestName.getPrefix(-3);
akmhoque674b0b12014-05-20 14:33:28 -0500144 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500146
147 Adjacent::Status status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
148
akmhoque31d1d4b2014-05-05 22:08:14 -0500149 uint32_t infoIntTimedOutCount =
150 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500151 _LOG_DEBUG("Status: " << status);
152 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
Nick Gordond0a7df32017-05-30 16:44:34 -0500153 if (infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber()) {
dmcoomes9f936662017-03-02 10:33:09 -0600154 // interest name: /<neighbor>/NLSR/INFO/<router>
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500156 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500157 interestName.append(INFO_COMPONENT);
158 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
dmcoomes9eaf3f42017-02-21 11:39:01 -0600159 _LOG_DEBUG("Resending interest: " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500160 expressInterest(interestName,
161 m_nlsr.getConfParameter().getInterestResendTime());
162 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500163 else if ((status == Adjacent::STATUS_ACTIVE) &&
akmhoque157b0a42014-05-13 00:26:37 -0500164 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500165 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
Vince Lehman50df6b72015-03-03 12:06:40 -0600166
dmcoomes9eaf3f42017-02-21 11:39:01 -0600167 _LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
168
Vince Lehman50df6b72015-03-03 12:06:40 -0600169 m_nlsr.getLsdb().scheduleAdjLsaBuild();
akmhoque31d1d4b2014-05-05 22:08:14 -0500170 }
171}
172
Nick G97e34942016-07-11 14:46:27 -0500173 // This is the first function that incoming Hello data will
174 // see. This checks if the data appears to be signed, and passes it
175 // on to validate the content of the data.
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700176void
177HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
178{
akmhoquedfe615f2014-07-27 14:12:21 -0500179 _LOG_DEBUG("Received data for INFO(name): " << data.getName());
180 if (data.getSignature().hasKeyLocator()) {
181 if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
182 _LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
183 }
184 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700185 m_nlsr.getValidator().validate(data,
dmcoomes9f936662017-03-02 10:33:09 -0600186 std::bind(&HelloProtocol::onContentValidated, this, _1),
187 std::bind(&HelloProtocol::onContentValidationFailed,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700188 this, _1, _2));
189}
akmhoque31d1d4b2014-05-05 22:08:14 -0500190
191void
dmcoomes9f936662017-03-02 10:33:09 -0600192HelloProtocol::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500193{
dmcoomes9f936662017-03-02 10:33:09 -0600194 // data name: /<neighbor>/NLSR/INFO/<router>/<version>
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700195 ndn::Name dataName = data->getName();
akmhoquedfe615f2014-07-27 14:12:21 -0500196 _LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500197 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500198 ndn::Name neighbor = dataName.getPrefix(-4);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500199
200 Adjacent::Status oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
201 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500202 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500203 Adjacent::Status newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
204
akmhoque2f423352014-06-03 11:49:35 -0500205 _LOG_DEBUG("Neighbor : " << neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500206 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500207 // change in Adjacency list
208 if ((oldStatus - newStatus) != 0) {
Ashlesh Gawandec5fa3202016-12-05 13:21:51 -0600209 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
210 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
211 }
212 else {
213 m_nlsr.getLsdb().scheduleAdjLsaBuild();
214 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500215 }
216 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600217 // increment RCV_HELLO_DATA
218 hpIncrementSignal(Statistics::PacketType::RCV_HELLO_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -0500219}
220
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700221void
dmcoomes9f936662017-03-02 10:33:09 -0600222HelloProtocol::onContentValidationFailed(const std::shared_ptr<const ndn::Data>& data,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700223 const std::string& msg)
224{
225 _LOG_DEBUG("Validation Error: " << msg);
226}
227
Nick Gordonfad8e252016-08-11 14:21:38 -0500228} // namespace nlsr