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-2019, 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 namespace nlsr {
28 
29 INIT_LOGGER(HelloProtocol);
30 
31 const std::string HelloProtocol::INFO_COMPONENT = "INFO";
32 const std::string HelloProtocol::NLSR_COMPONENT = "nlsr";
33 
34 HelloProtocol::HelloProtocol(ndn::Face& face, ndn::KeyChain& keyChain,
35  ndn::security::SigningInfo& signingInfo,
36  ConfParameter& confParam, RoutingTable& routingTable,
37  Lsdb& lsdb)
38  : m_face(face)
39  , m_scheduler(m_face.getIoService())
40  , m_keyChain(keyChain)
41  , m_signingInfo(signingInfo)
42  , m_confParam(confParam)
43  , m_routingTable(routingTable)
44  , m_lsdb(lsdb)
45 {
46 }
47 
48 void
49 HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
50 {
51  NLSR_LOG_DEBUG("Expressing Interest :" << interestName);
52  ndn::Interest interest(interestName);
53  interest.setInterestLifetime(ndn::time::seconds(seconds));
54  interest.setMustBeFresh(true);
55  interest.setCanBePrefix(true);
56  m_face.expressInterest(interest,
57  std::bind(&HelloProtocol::onContent, this, _1, _2),
58  [this] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
59  {
60  NDN_LOG_TRACE("Received Nack with reason " << nack.getReason());
61  NDN_LOG_TRACE("Treating as timeout");
62  processInterestTimedOut(interest);
63  },
64  std::bind(&HelloProtocol::processInterestTimedOut, this, _1));
65 
66  // increment SENT_HELLO_INTEREST
68 }
69 
70 void
72 {
73  for (const auto& adjacent : m_confParam.getAdjacencyList().getAdjList()) {
74  // If this adjacency has a Face, just proceed as usual.
75  if(adjacent.getFaceId() != 0) {
76  // interest name: /<neighbor>/NLSR/INFO/<router>
77  ndn::Name interestName = adjacent.getName() ;
78  interestName.append(NLSR_COMPONENT);
79  interestName.append(INFO_COMPONENT);
80  interestName.append(m_confParam.getRouterPrefix().wireEncode());
81  expressInterest(interestName, m_confParam.getInterestResendTime());
82  NLSR_LOG_DEBUG("Sending scheduled interest: " << interestName);
83  }
84  }
86 }
87 
88 void
90 {
91  NLSR_LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
92 
93  m_scheduler.schedule(ndn::time::seconds(seconds), [this] { sendScheduledInterest(); });
94 }
95 
96 void
97 HelloProtocol::processInterest(const ndn::Name& name,
98  const ndn::Interest& interest)
99 {
100  // interest name: /<neighbor>/NLSR/INFO/<router>
101  const ndn::Name interestName = interest.getName();
102 
103  // increment RCV_HELLO_INTEREST
105 
106  NLSR_LOG_DEBUG("Interest Received for Name: " << interestName);
107  if (interestName.get(-2).toUri() != INFO_COMPONENT) {
108  NLSR_LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName
109  << " does not match expression");
110  return;
111  }
112 
113  ndn::Name neighbor;
114  neighbor.wireDecode(interestName.get(-1).blockFromValue());
115  NLSR_LOG_DEBUG("Neighbor: " << neighbor);
116  if (m_confParam.getAdjacencyList().isNeighbor(neighbor)) {
117  std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
118  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()),
121  INFO_COMPONENT.size());
122 
123  m_keyChain.sign(*data, m_signingInfo);
124 
125  NLSR_LOG_DEBUG("Sending out data for name: " << interest.getName());
126 
127  m_face.put(*data);
128  // increment SENT_HELLO_DATA
130 
131  auto adjacent = m_confParam.getAdjacencyList().findAdjacent(neighbor);
132  // If this neighbor was previously inactive, send our own hello interest, too
133  if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
134  // We can only do that if the neighbor currently has a face.
135  if(adjacent->getFaceId() != 0){
136  // interest name: /<neighbor>/NLSR/INFO/<router>
137  ndn::Name interestName(neighbor);
138  interestName.append(NLSR_COMPONENT);
139  interestName.append(INFO_COMPONENT);
140  interestName.append(m_confParam.getRouterPrefix().wireEncode());
141  expressInterest(interestName, m_confParam.getInterestResendTime());
142  }
143  }
144  }
145 }
146 
147 void
148 HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
149 {
150  // interest name: /<neighbor>/NLSR/INFO/<router>
151  const ndn::Name interestName(interest.getName());
152  NLSR_LOG_DEBUG("Interest timed out for Name: " << interestName);
153  if (interestName.get(-2).toUri() != INFO_COMPONENT) {
154  return;
155  }
156  ndn::Name neighbor = interestName.getPrefix(-3);
157  NLSR_LOG_DEBUG("Neighbor: " << neighbor);
158  m_confParam.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
159 
160  Adjacent::Status status = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
161 
162  uint32_t infoIntTimedOutCount =
163  m_confParam.getAdjacencyList().getTimedOutInterestCount(neighbor);
164  NLSR_LOG_DEBUG("Status: " << status);
165  NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
166  if (infoIntTimedOutCount < m_confParam.getInterestRetryNumber()) {
167  // interest name: /<neighbor>/NLSR/INFO/<router>
168  ndn::Name interestName(neighbor);
169  interestName.append(NLSR_COMPONENT);
170  interestName.append(INFO_COMPONENT);
171  interestName.append(m_confParam.getRouterPrefix().wireEncode());
172  NLSR_LOG_DEBUG("Resending interest: " << interestName);
173  expressInterest(interestName, m_confParam.getInterestResendTime());
174  }
175  else if ((status == Adjacent::STATUS_ACTIVE) &&
176  (infoIntTimedOutCount == m_confParam.getInterestRetryNumber())) {
178 
179  NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
180 
181  m_lsdb.scheduleAdjLsaBuild();
182  }
183 }
184 
185  // This is the first function that incoming Hello data will
186  // see. This checks if the data appears to be signed, and passes it
187  // on to validate the content of the data.
188 void
189 HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
190 {
191  NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
192  if (data.getSignature().hasKeyLocator()) {
193  if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
194  NLSR_LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
195  }
196  }
197  m_confParam.getValidator().validate(data,
198  std::bind(&HelloProtocol::onContentValidated, this, _1),
199  std::bind(&HelloProtocol::onContentValidationFailed,
200  this, _1, _2));
201 }
202 
203 void
204 HelloProtocol::onContentValidated(const ndn::Data& data)
205 {
206  // data name: /<neighbor>/NLSR/INFO/<router>/<version>
207  ndn::Name dataName = data.getName();
208  NLSR_LOG_DEBUG("Data validation successful for INFO(name): " << dataName);
209 
210  if (dataName.get(-3).toUri() == INFO_COMPONENT) {
211  ndn::Name neighbor = dataName.getPrefix(-4);
212 
213  Adjacent::Status oldStatus = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
215  m_confParam.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
216  Adjacent::Status newStatus = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
217 
218  NLSR_LOG_DEBUG("Neighbor : " << neighbor);
219  NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
220  // change in Adjacency list
221  if ((oldStatus - newStatus) != 0) {
222  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
223  m_routingTable.scheduleRoutingTableCalculation();
224  }
225  else {
226  m_lsdb.scheduleAdjLsaBuild();
227  }
228  }
229  }
230  // increment RCV_HELLO_DATA
232 }
233 
234 void
235 HelloProtocol::onContentValidationFailed(const ndn::Data& data,
236  const ndn::security::v2::ValidationError& ve)
237 {
238  NLSR_LOG_DEBUG("Validation Error: " << ve);
239 }
240 
241 } // namespace nlsr
uint32_t getInterestResendTime() const
void setTimedOutInterestCount(const ndn::Name &neighbor, uint32_t count)
ndn::security::ValidatorConfig & getValidator()
A class to house all the configuration parameters for NLSR.
void scheduleAdjLsaBuild()
Schedules a build of this router&#39;s LSA.
Definition: lsdb.cpp:553
ndn::util::signal::Signal< HelloProtocol, Statistics::PacketType > hpIncrementSignal
void scheduleRoutingTableCalculation()
Schedules a calculation event in the event scheduler only if one isn&#39;t already scheduled.
bool isNeighbor(const ndn::Name &adjName) const
AdjacencyList & getAdjacencyList()
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
uint32_t getInfoInterestInterval() const
const ndn::Name & getRouterPrefix() const
Adjacent::Status getStatusOfNeighbor(const ndn::Name &neighbor) const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
void expressInterest(const ndn::Name &interestNamePrefix, uint32_t seconds)
Sends a Hello Interest packet.
HelloProtocol(ndn::Face &face, ndn::KeyChain &keyChain, ndn::security::SigningInfo &signingInfo, ConfParameter &confParam, RoutingTable &routingTable, Lsdb &lsdb)
void scheduleInterest(uint32_t seconds)
Schedules a Hello Interest event.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
uint32_t getInterestRetryNumber() const
void processInterest(const ndn::Name &name, const ndn::Interest &interest)
Processes a Hello Interest from a neighbor.
int32_t getTimedOutInterestCount(const ndn::Name &neighbor) const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
int32_t getHyperbolicState() const
void sendScheduledInterest()
Sends Hello Interests to all neighbors.
void incrementTimedOutInterestCount(const ndn::Name &neighbor)
std::list< Adjacent > & getAdjList()
void setStatusOfNeighbor(const ndn::Name &neighbor, Adjacent::Status status)