hello-protocol.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, The University of Memphis,
4  * Regents of the University of California
5  *
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/>.
19  */
20 
21 #include "hello-protocol.hpp"
22 #include "nlsr.hpp"
23 #include "lsdb.hpp"
24 #include "utility/name-helper.hpp"
25 #include "logger.hpp"
26 
27 #include <ndn-cxx/encoding/nfd-constants.hpp>
28 
29 namespace nlsr {
30 
31 INIT_LOGGER(HelloProtocol);
32 
33 const std::string HelloProtocol::INFO_COMPONENT = "INFO";
34 const std::string HelloProtocol::NLSR_COMPONENT = "nlsr";
35 
36 HelloProtocol::HelloProtocol(ndn::Face& face, ndn::KeyChain& keyChain,
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(confParam.getSigningInfo())
43  , m_confParam(confParam)
44  , m_routingTable(routingTable)
45  , m_lsdb(lsdb)
46  , m_adjacencyList(m_confParam.getAdjacencyList())
47 {
48  ndn::Name name(m_confParam.getRouterPrefix());
49  name.append(NLSR_COMPONENT);
50  name.append(INFO_COMPONENT);
51 
52  NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
53 
54  m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
55  [this] (const auto& name, const auto& interest) {
56  processInterest(name, interest);
57  },
58  [] (const auto& name) {
59  NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
60  },
61  [] (const auto& name, const auto& resp) {
62  NLSR_LOG_ERROR("Failed to register prefix " << name);
63  NDN_THROW(std::runtime_error("Failed to register hello prefix: " + resp));
64  },
65  m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
66 }
67 
68 void
69 HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
70 {
71  NLSR_LOG_DEBUG("Expressing Interest: " << interestName);
72  ndn::Interest interest(interestName);
73  interest.setInterestLifetime(ndn::time::seconds(seconds));
74  interest.setMustBeFresh(true);
75  interest.setCanBePrefix(true);
76  m_face.expressInterest(interest,
77  std::bind(&HelloProtocol::onContent, this, _1, _2),
78  [this, seconds] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
79  {
80  NDN_LOG_TRACE("Received Nack with reason: " << nack.getReason());
81  NDN_LOG_TRACE("Will treat as timeout in " << 2 * seconds << " seconds");
82  m_scheduler.schedule(ndn::time::seconds(2 * seconds),
83  [this, interest] { processInterestTimedOut(interest); });
84  },
85  std::bind(&HelloProtocol::processInterestTimedOut, this, _1));
86 
87  // increment SENT_HELLO_INTEREST
89 }
90 
91 void
92 HelloProtocol::sendHelloInterest(const ndn::Name& neighbor)
93 {
94  auto adjacent = m_adjacencyList.findAdjacent(neighbor);
95 
96  if (adjacent == m_adjacencyList.end()) {
97  return;
98  }
99 
100  // If this adjacency has a Face, just proceed as usual.
101  if(adjacent->getFaceId() != 0) {
102  // interest name: /<neighbor>/NLSR/INFO/<router>
103  ndn::Name interestName = adjacent->getName() ;
104  interestName.append(NLSR_COMPONENT);
105  interestName.append(INFO_COMPONENT);
106  interestName.append(m_confParam.getRouterPrefix().wireEncode());
107  expressInterest(interestName, m_confParam.getInterestResendTime());
108  NLSR_LOG_DEBUG("Sending HELLO interest: " << interestName);
109  }
110 
111  m_scheduler.schedule(ndn::time::seconds(m_confParam.getInfoInterestInterval()),
112  [this, neighbor] { sendHelloInterest(neighbor); });
113 }
114 
115 void
116 HelloProtocol::processInterest(const ndn::Name& name,
117  const ndn::Interest& interest)
118 {
119  // interest name: /<neighbor>/NLSR/INFO/<router>
120  const ndn::Name interestName = interest.getName();
121 
122  // increment RCV_HELLO_INTEREST
124 
125  NLSR_LOG_DEBUG("Interest Received for Name: " << interestName);
126  if (interestName.get(-2).toUri() != INFO_COMPONENT) {
127  NLSR_LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName
128  << " does not match expression");
129  return;
130  }
131 
132  ndn::Name neighbor;
133  neighbor.wireDecode(interestName.get(-1).blockFromValue());
134  NLSR_LOG_DEBUG("Neighbor: " << neighbor);
135  if (m_adjacencyList.isNeighbor(neighbor)) {
136  std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
137  data->setName(ndn::Name(interest.getName()).appendVersion());
138  data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
139  data->setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
140  INFO_COMPONENT.size());
141 
142  m_keyChain.sign(*data, m_signingInfo);
143 
144  NLSR_LOG_DEBUG("Sending out data for name: " << interest.getName());
145 
146  m_face.put(*data);
147  // increment SENT_HELLO_DATA
149 
150  auto adjacent = m_adjacencyList.findAdjacent(neighbor);
151  // If this neighbor was previously inactive, send our own hello interest, too
152  if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
153  // We can only do that if the neighbor currently has a face.
154  if(adjacent->getFaceId() != 0){
155  // interest name: /<neighbor>/NLSR/INFO/<router>
156  ndn::Name interestName(neighbor);
157  interestName.append(NLSR_COMPONENT);
158  interestName.append(INFO_COMPONENT);
159  interestName.append(m_confParam.getRouterPrefix().wireEncode());
160  expressInterest(interestName, m_confParam.getInterestResendTime());
161  }
162  }
163  }
164 }
165 
166 void
167 HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
168 {
169  // interest name: /<neighbor>/NLSR/INFO/<router>
170  const ndn::Name interestName(interest.getName());
171  NLSR_LOG_DEBUG("Interest timed out for Name: " << interestName);
172  if (interestName.get(-2).toUri() != INFO_COMPONENT) {
173  return;
174  }
175  ndn::Name neighbor = interestName.getPrefix(-3);
176  NLSR_LOG_DEBUG("Neighbor: " << neighbor);
177  m_adjacencyList.incrementTimedOutInterestCount(neighbor);
178 
179  Adjacent::Status status = m_adjacencyList.getStatusOfNeighbor(neighbor);
180 
181  uint32_t infoIntTimedOutCount = m_adjacencyList.getTimedOutInterestCount(neighbor);
182  NLSR_LOG_DEBUG("Status: " << status);
183  NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
184  if (infoIntTimedOutCount < m_confParam.getInterestRetryNumber()) {
185  // interest name: /<neighbor>/NLSR/INFO/<router>
186  ndn::Name interestName(neighbor);
187  interestName.append(NLSR_COMPONENT);
188  interestName.append(INFO_COMPONENT);
189  interestName.append(m_confParam.getRouterPrefix().wireEncode());
190  NLSR_LOG_DEBUG("Resending interest: " << interestName);
191  expressInterest(interestName, m_confParam.getInterestResendTime());
192  }
193  else if (status == Adjacent::STATUS_ACTIVE) {
194  m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
195 
196  NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
197 
198  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
199  m_routingTable.scheduleRoutingTableCalculation();
200  }
201  else {
202  m_lsdb.scheduleAdjLsaBuild();
203  }
204  }
205 }
206 
207  // This is the first function that incoming Hello data will
208  // see. This checks if the data appears to be signed, and passes it
209  // on to validate the content of the data.
210 void
211 HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
212 {
213  NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
214  auto kl = data.getKeyLocator();
215  if (kl && kl->getType() == ndn::tlv::Name) {
216  NLSR_LOG_DEBUG("Data signed with: " << kl->getName());
217  }
218  m_confParam.getValidator().validate(data,
219  std::bind(&HelloProtocol::onContentValidated, this, _1),
220  std::bind(&HelloProtocol::onContentValidationFailed,
221  this, _1, _2));
222 }
223 
224 void
225 HelloProtocol::onContentValidated(const ndn::Data& data)
226 {
227  // data name: /<neighbor>/NLSR/INFO/<router>/<version>
228  ndn::Name dataName = data.getName();
229  NLSR_LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
230 
231  if (dataName.get(-3).toUri() == INFO_COMPONENT) {
232  ndn::Name neighbor = dataName.getPrefix(-4);
233 
234  Adjacent::Status oldStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
235  m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
236  m_adjacencyList.setTimedOutInterestCount(neighbor, 0);
237  Adjacent::Status newStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
238 
239  NLSR_LOG_DEBUG("Neighbor : " << neighbor);
240  NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
241  // change in Adjacency list
242  if ((oldStatus - newStatus) != 0) {
243  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
244  m_routingTable.scheduleRoutingTableCalculation();
245  }
246  else {
247  m_lsdb.scheduleAdjLsaBuild();
248  }
249  }
250 
251  onHelloDataValidated(neighbor);
252  }
253  // increment RCV_HELLO_DATA
255 }
256 
257 void
258 HelloProtocol::onContentValidationFailed(const ndn::Data& data,
259  const ndn::security::ValidationError& ve)
260 {
261  NLSR_LOG_DEBUG("Validation Error: " << ve);
262 }
263 
264 } // namespace nlsr
int32_t getTimedOutInterestCount(const ndn::Name &neighbor) const
void incrementTimedOutInterestCount(const ndn::Name &neighbor)
void setTimedOutInterestCount(const ndn::Name &neighbor, uint32_t count)
const_iterator end() const
Adjacent::Status getStatusOfNeighbor(const ndn::Name &neighbor) const
bool isNeighbor(const ndn::Name &adjName) const
void setStatusOfNeighbor(const ndn::Name &neighbor, Adjacent::Status status)
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
A class to house all the configuration parameters for NLSR.
uint32_t getInfoInterestInterval() const
int32_t getHyperbolicState() const
uint32_t getInterestRetryNumber() const
const ndn::Name & getRouterPrefix() const
uint32_t getInterestResendTime() const
ndn::security::ValidatorConfig & getValidator()
HelloProtocol(ndn::Face &face, ndn::KeyChain &keyChain, ConfParameter &confParam, RoutingTable &routingTable, Lsdb &lsdb)
void sendHelloInterest(const ndn::Name &neighbor)
Sends Hello Interests to all neighbors.
ndn::util::signal::Signal< HelloProtocol, Statistics::PacketType > hpIncrementSignal
ndn::util::Signal< HelloProtocol, const ndn::Name & > onHelloDataValidated
void processInterest(const ndn::Name &name, const ndn::Interest &interest)
Processes a Hello Interest from a neighbor.
void expressInterest(const ndn::Name &interestNamePrefix, uint32_t seconds)
Sends a Hello Interest packet.
void scheduleAdjLsaBuild()
Schedules a build of this router's LSA.
Definition: lsdb.cpp:94
void scheduleRoutingTableCalculation()
Schedules a calculation event in the event scheduler only if one isn't already scheduled.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California,...
@ HYPERBOLIC_STATE_ON