blob: 41df359c09297bf214654a54775eec6823140dfe [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 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 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque31d1d4b2014-05-05 22:08:14 -050023#include "nlsr.hpp"
24#include "lsdb.hpp"
25#include "hello-protocol.hpp"
26#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050027#include "logger.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050028
29namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("HelloProtocol");
32
akmhoque93f1a072014-06-19 16:24:28 -050033const std::string HelloProtocol::INFO_COMPONENT = "INFO";
34const std::string HelloProtocol::NLSR_COMPONENT = "NLSR";
akmhoque157b0a42014-05-13 00:26:37 -050035
akmhoque31d1d4b2014-05-05 22:08:14 -050036void
37HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
38{
akmhoque674b0b12014-05-20 14:33:28 -050039 _LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050040 ndn::Interest i(interestName);
41 i.setInterestLifetime(ndn::time::seconds(seconds));
42 i.setMustBeFresh(true);
43 m_nlsr.getNlsrFace().expressInterest(i,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070044 ndn::bind(&HelloProtocol::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -050045 this,
46 _1, _2),
47 ndn::bind(&HelloProtocol::processInterestTimedOut,
48 this, _1));
49}
50
51void
52HelloProtocol::sendScheduledInterest(uint32_t seconds)
53{
54 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
55 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050056 ++it) {
akmhoquec04e7272014-07-02 11:00:14 -050057 if((*it).getFaceId() != 0) {
58 /* interest name: /<neighbor>/NLSR/INFO/<router> */
59 ndn::Name interestName = (*it).getName() ;
60 interestName.append(NLSR_COMPONENT);
61 interestName.append(INFO_COMPONENT);
62 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
63 expressInterest(interestName,
64 m_nlsr.getConfParameter().getInterestResendTime());
65 }
66 else {
67 registerPrefixes((*it).getName(), (*it).getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -050068 (*it).getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -050069 }
akmhoque31d1d4b2014-05-05 22:08:14 -050070 }
71 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
72}
73
74void
75HelloProtocol::scheduleInterest(uint32_t seconds)
76{
77 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
78 ndn::bind(&HelloProtocol::sendScheduledInterest,
79 this, seconds));
80}
81
82void
83HelloProtocol::processInterest(const ndn::Name& name,
84 const ndn::Interest& interest)
85{
akmhoque93f1a072014-06-19 16:24:28 -050086 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -050087 const ndn::Name interestName = interest.getName();
akmhoque674b0b12014-05-20 14:33:28 -050088 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050089 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050090 return;
91 }
akmhoque157b0a42014-05-13 00:26:37 -050092 ndn::Name neighbor;
93 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque674b0b12014-05-20 14:33:28 -050094 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050095 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque31d1d4b2014-05-05 22:08:14 -050096 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
97 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
akmhoque157b0a42014-05-13 00:26:37 -050098 data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
99 INFO_COMPONENT.size());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700100 m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName());
akmhoque674b0b12014-05-20 14:33:28 -0500101 _LOG_DEBUG("Sending out data for name: " << data.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 m_nlsr.getNlsrFace().put(data);
akmhoquec04e7272014-07-02 11:00:14 -0500103 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
104 if (adjacent->getStatus() == 0) {
105 if(adjacent->getFaceId() != 0){
106 /* interest name: /<neighbor>/NLSR/INFO/<router> */
107 ndn::Name interestName(neighbor);
108 interestName.append(NLSR_COMPONENT);
109 interestName.append(INFO_COMPONENT);
110 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
111 expressInterest(interestName,
112 m_nlsr.getConfParameter().getInterestResendTime());
113 }
114 else {
115 registerPrefixes(adjacent->getName(), adjacent->getConnectingFaceUri(),
akmhoquebf11c5f2014-07-21 14:49:47 -0500116 adjacent->getLinkCost(), ndn::time::milliseconds::max());
akmhoquec04e7272014-07-02 11:00:14 -0500117 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 }
119 }
120}
121
122void
123HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
124{
akmhoque93f1a072014-06-19 16:24:28 -0500125 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 const ndn::Name interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500127 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500128 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 return;
130 }
akmhoque93f1a072014-06-19 16:24:28 -0500131 ndn::Name neighbor = interestName.getPrefix(-3);
akmhoque674b0b12014-05-20 14:33:28 -0500132 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500133 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
134 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
135 uint32_t infoIntTimedOutCount =
136 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500137 _LOG_DEBUG("Status: " << status);
138 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500139 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque93f1a072014-06-19 16:24:28 -0500140 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500141 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500142 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500143 interestName.append(INFO_COMPONENT);
144 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 expressInterest(interestName,
146 m_nlsr.getConfParameter().getInterestResendTime());
147 }
148 else if ((status == 1) &&
akmhoque157b0a42014-05-13 00:26:37 -0500149 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0);
151 m_nlsr.incrementAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500152 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500153 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500154 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 // event here
156 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
157 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
158 &m_nlsr.getLsdb()));
159 }
160 }
161}
162
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700163void
164HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
165{
166 m_nlsr.getValidator().validate(data,
167 ndn::bind(&HelloProtocol::onContentValidated, this, _1),
168 ndn::bind(&HelloProtocol::onContentValidationFailed,
169 this, _1, _2));
170}
akmhoque31d1d4b2014-05-05 22:08:14 -0500171
172void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700173HelloProtocol::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500174{
akmhoque93f1a072014-06-19 16:24:28 -0500175 /* data name: /<neighbor>/NLSR/INFO/<router>/<version> */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700176 ndn::Name dataName = data->getName();
akmhoque674b0b12014-05-20 14:33:28 -0500177 _LOG_DEBUG("Data received for name: " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500178 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
akmhoque93f1a072014-06-19 16:24:28 -0500179 ndn::Name neighbor = dataName.getPrefix(-4);
akmhoque31d1d4b2014-05-05 22:08:14 -0500180 int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1);
182 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
183 int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque2f423352014-06-03 11:49:35 -0500184 _LOG_DEBUG("Neighbor : " << neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500185 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500186 // change in Adjacency list
187 if ((oldStatus - newStatus) != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500188 m_nlsr.incrementAdjBuildCount();
akmhoque2f423352014-06-03 11:49:35 -0500189 // Need to schedule event for Adjacency LSA building
akmhoque157b0a42014-05-13 00:26:37 -0500190 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500191 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500192 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500193 // event here
194 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
195 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
akmhoque157b0a42014-05-13 00:26:37 -0500196 ndn::ref(m_nlsr.getLsdb())));
akmhoque31d1d4b2014-05-05 22:08:14 -0500197 }
198 }
199 }
200}
201
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700202void
203HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
204 const std::string& msg)
205{
206 _LOG_DEBUG("Validation Error: " << msg);
207}
208
akmhoquec04e7272014-07-02 11:00:14 -0500209void
akmhoque8e0252b2014-07-07 16:04:44 -0500210HelloProtocol::registerPrefixes(const ndn::Name& adjName, const std::string& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500211 double linkCost, const ndn::time::milliseconds& timeout)
akmhoquec04e7272014-07-02 11:00:14 -0500212{
213 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
214 broadcastKeyPrefix.append("KEYS");
215 m_nlsr.getFib().registerPrefix(adjName, faceUri, linkCost, timeout,
216 ndn::bind(&HelloProtocol::onRegistrationSuccess,
akmhoque8e0252b2014-07-07 16:04:44 -0500217 this, _1, adjName),
akmhoquec04e7272014-07-02 11:00:14 -0500218 ndn::bind(&HelloProtocol::onRegistrationFailure,
219 this, _1, _2));
220 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getChronosyncPrefix(),
221 faceUri, linkCost, timeout);
222 m_nlsr.getFib().registerPrefix(m_nlsr.getConfParameter().getLsaPrefix(),
223 faceUri, linkCost, timeout);
224 m_nlsr.getFib().registerPrefix(broadcastKeyPrefix,
225 faceUri, linkCost, timeout);
akmhoque393d4ff2014-07-16 14:27:03 -0500226 m_nlsr.setStrategies();
akmhoquec04e7272014-07-02 11:00:14 -0500227}
228
229void
230HelloProtocol::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
akmhoque8e0252b2014-07-07 16:04:44 -0500231 const ndn::Name& neighbor)
akmhoquec04e7272014-07-02 11:00:14 -0500232{
233 Adjacent *adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
234 if (adjacent != 0) {
235 adjacent->setFaceId(commandSuccessResult.getFaceId());
236 /* interest name: /<neighbor>/NLSR/INFO/<router> */
237 ndn::Name interestName(neighbor);
238 interestName.append(NLSR_COMPONENT);
239 interestName.append(INFO_COMPONENT);
240 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
241 expressInterest(interestName,
242 m_nlsr.getConfParameter().getInterestResendTime());
243 }
244}
245
246void
247HelloProtocol::onRegistrationFailure(uint32_t code, const std::string& error)
248{
249 _LOG_DEBUG(error << " (code: " << code << ")");
250}
251
akmhoque31d1d4b2014-05-05 22:08:14 -0500252} //namespace nlsr