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