blob: 226efafcea128e5da2e65ef8a51ad3252731dece [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <cstdlib>
3
4
5#include <ndn-cpp-dev/security/identity-certificate.hpp>
6#include <ndn-cpp-dev/util/io.hpp>
7
8#include "nlsr.hpp"
9#include "interest-manager.hpp"
10#include "data-manager.hpp"
11#include "utility/tokenizer.hpp"
12#include "lsdb.hpp"
13
14namespace nlsr {
15
16using namespace std;
17using namespace ndn;
18
19void
akmhoqueb6450b12014-04-24 00:01:03 -050020InterestManager::processInterest(const ndn::Name& name,
akmhoque53353462014-04-22 08:43:45 -050021 const ndn::Interest& interest)
22{
23 cout << "<< I: " << interest << endl;
24 string intName = interest.getName().toUri();
25 cout << "Interest Received for Name: " << intName << endl;
26 Tokenizer nt(intName, "/");
27 string chkString("info");
28 if (nt.doesTokenExist(chkString))
29 {
30 string nbr = nt.getTokenString(nt.getTokenPosition(chkString) + 1);
31 cout << "Neighbor: " << nbr << endl;
akmhoqueb6450b12014-04-24 00:01:03 -050032 processInterestInfo(nbr, interest);
akmhoque53353462014-04-22 08:43:45 -050033 }
34 chkString = "LSA";
35 if (nt.doesTokenExist(chkString))
36 {
akmhoqueb6450b12014-04-24 00:01:03 -050037 processInterestLsa(interest);
akmhoque53353462014-04-22 08:43:45 -050038 }
39 chkString = "keys";
40 if (nt.doesTokenExist(chkString))
41 {
akmhoqueb6450b12014-04-24 00:01:03 -050042 processInterestKeys(interest);
akmhoque53353462014-04-22 08:43:45 -050043 }
44}
45
46void
akmhoqueb6450b12014-04-24 00:01:03 -050047InterestManager::processInterestInfo(const string& neighbor,
akmhoque53353462014-04-22 08:43:45 -050048 const ndn::Interest& interest)
49{
akmhoqueb6450b12014-04-24 00:01:03 -050050 if (m_nlsr.getAdl().isNeighbor(neighbor))
akmhoque53353462014-04-22 08:43:45 -050051 {
52 Data data(ndn::Name(interest.getName()).appendVersion());
53 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
54 data.setContent((const uint8_t*)"info", sizeof("info"));
akmhoqueb6450b12014-04-24 00:01:03 -050055 m_nlsr.getKeyManager().signData(data);
akmhoque53353462014-04-22 08:43:45 -050056 cout << ">> D: " << data << endl;
akmhoqueb6450b12014-04-24 00:01:03 -050057 m_nlsr.getNlsrFace()->put(data);
58 int status = m_nlsr.getAdl().getStatusOfNeighbor(neighbor);
akmhoque53353462014-04-22 08:43:45 -050059 if (status == 0)
60 {
61 string intName = neighbor + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -050062 m_nlsr.getConfParameter().getRouterPrefix();
63 expressInterest(intName, 2,
64 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -050065 }
66 }
67}
68
69void
akmhoqueb6450b12014-04-24 00:01:03 -050070InterestManager::processInterestLsa(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -050071{
72 string intName = interest.getName().toUri();
73 Tokenizer nt(intName, "/");
74 string chkString("LSA");
75 string origRouter = nt.getTokenString(nt.getTokenPosition(chkString) + 1,
76 nt.getTokenNumber() - 3);
77 string lsTypeString = nt.getToken(nt.getTokenNumber() - 2);
78 string lsSeqString = nt.getToken(nt.getTokenNumber() - 1);
79 std::cout << "Router Name: " << origRouter << std::endl;
80 std::cout << "Ls Type : " << lsTypeString << std::endl;
81 std::cout << "Ls Seq : " << lsSeqString << endl;
82 uint8_t interestedLsType;
83 uint32_t interestedLsSeqNo;
84 try
85 {
86 interestedLsType = boost::lexical_cast<uint8_t>(lsTypeString);
87 interestedLsSeqNo = boost::lexical_cast<uint32_t>(lsSeqString);
88 }
89 catch (std::exception& e)
90 {
91 return;
92 }
93 std::cout << "Ls Type: " << interestedLsType << std::endl;
94 if (lsTypeString == "1") //Name Lsa
95 {
akmhoqueb6450b12014-04-24 00:01:03 -050096 processInterestForNameLsa(interest,
akmhoque53353462014-04-22 08:43:45 -050097 origRouter + "/" + lsTypeString, interestedLsSeqNo);
98 }
99 else if (lsTypeString == "2") //Adj Lsa
100 {
akmhoqueb6450b12014-04-24 00:01:03 -0500101 processInterestForAdjLsa(interest,
akmhoque53353462014-04-22 08:43:45 -0500102 origRouter + "/" + lsTypeString, interestedLsSeqNo);
103 }
104 else if (lsTypeString == "3") //Cor Lsa
105 {
akmhoqueb6450b12014-04-24 00:01:03 -0500106 processInterestForCorLsa(interest,
akmhoque53353462014-04-22 08:43:45 -0500107 origRouter + "/" + lsTypeString, interestedLsSeqNo);
108 }
109 else
110 {
111 cout << "Unrecognized LSA Type :(" << endl;
112 }
113}
114
115void
akmhoqueb6450b12014-04-24 00:01:03 -0500116InterestManager::processInterestForNameLsa(const ndn::Interest& interest,
117 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500118{
akmhoqueb6450b12014-04-24 00:01:03 -0500119 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
120 if (nameLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500121 {
akmhoqueb6450b12014-04-24 00:01:03 -0500122 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500123 {
124 Data data(ndn::Name(interest.getName()).appendVersion());
125 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500126 string content = nameLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500127 data.setContent((const uint8_t*)content.c_str(), content.size());
akmhoqueb6450b12014-04-24 00:01:03 -0500128 m_nlsr.getKeyManager().signData(data);
akmhoque53353462014-04-22 08:43:45 -0500129 std::cout << ">> D: " << data << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500130 m_nlsr.getNlsrFace()->put(data);
akmhoque53353462014-04-22 08:43:45 -0500131 }
132 }
133}
134
135void
akmhoqueb6450b12014-04-24 00:01:03 -0500136InterestManager::processInterestForAdjLsa(const ndn::Interest& interest,
137 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500138{
akmhoqueb6450b12014-04-24 00:01:03 -0500139 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
140 if (adjLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500141 {
akmhoqueb6450b12014-04-24 00:01:03 -0500142 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500143 {
144 Data data(ndn::Name(interest.getName()).appendVersion());
145 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500146 string content = adjLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500147 data.setContent((const uint8_t*)content.c_str(), content.size());
akmhoqueb6450b12014-04-24 00:01:03 -0500148 m_nlsr.getKeyManager().signData(data);
akmhoque53353462014-04-22 08:43:45 -0500149 std::cout << ">> D: " << data << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500150 m_nlsr.getNlsrFace()->put(data);
akmhoque53353462014-04-22 08:43:45 -0500151 }
152 }
153}
154
155void
akmhoqueb6450b12014-04-24 00:01:03 -0500156InterestManager::processInterestForCorLsa(const ndn::Interest& interest,
157 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500158{
akmhoqueb6450b12014-04-24 00:01:03 -0500159 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
160 if (corLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500161 {
akmhoqueb6450b12014-04-24 00:01:03 -0500162 if (corLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500163 {
164 Data data(ndn::Name(interest.getName()).appendVersion());
165 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500166 string content = corLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500167 data.setContent((const uint8_t*)content.c_str(), content.size());
akmhoqueb6450b12014-04-24 00:01:03 -0500168 m_nlsr.getKeyManager().signData(data);
akmhoque53353462014-04-22 08:43:45 -0500169 std::cout << ">> D: " << data << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500170 m_nlsr.getNlsrFace()->put(data);
akmhoque53353462014-04-22 08:43:45 -0500171 }
172 }
173}
174
175void
akmhoqueb6450b12014-04-24 00:01:03 -0500176InterestManager::processInterestKeys(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500177{
178 std::cout << "processInterestKeys called " << std::endl;
179 string intName = interest.getName().toUri();
180 std::cout << "Interest Name for Key: " << intName << std::endl;
181 Tokenizer nt(intName, "/");
182 std::string chkString("ID-CERT");
183 std::string certName;
184 uint32_t seqNum;
185 ndn::Name dataName;
186 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert;
187 if (nt.getTokenPosition(chkString) == nt.getTokenNumber() - 1)
188 {
189 certName = nt.getTokenString(0, nt.getTokenNumber() - 1);
190 cout << "Cert Name: " << certName << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500191 chkCert = m_nlsr.getKeyManager().getCertificateFromStore(certName);
akmhoque53353462014-04-22 08:43:45 -0500192 }
193 else
194 {
195 certName = nt.getTokenString(0, nt.getTokenNumber() - 2);
196 seqNum = boost::lexical_cast<uint32_t>(nt.getToken(nt.getTokenNumber() - 1));
197 std::cout << "Cert Name: " << certName << " Seq Num: " << seqNum << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500198 chkCert = m_nlsr.getKeyManager().getCertificateFromStore(certName, seqNum);
akmhoque53353462014-04-22 08:43:45 -0500199 }
200 if (chkCert.second)
201 {
202 if (nt.getTokenPosition(chkString) == nt.getTokenNumber() - 1)
203 {
204 std::string dn;
205 dataName = ndn::Name(interest.getName()).appendVersion();
206 std::pair<uint32_t, bool> seqChk =
akmhoqueb6450b12014-04-24 00:01:03 -0500207 m_nlsr.getKeyManager().getCertificateSeqNum(certName);
akmhoque53353462014-04-22 08:43:45 -0500208 if (seqChk.second)
209 {
210 dn = dataName.toUri() + "/" + boost::lexical_cast<std::string>(seqChk.first);
211 dataName = ndn::Name(dn);
212 }
213 else
214 {
215 dn = dataName.toUri() + "/" + boost::lexical_cast<std::string>(10);
216 dataName = ndn::Name(dn);
217 }
akmhoque53353462014-04-22 08:43:45 -0500218 }
219 else
220 {
221 dataName = ndn::Name(interest.getName());
222 }
223 Data data(dataName.appendVersion());
224 data.setFreshnessPeriod(time::seconds(10)); //10 sec
225 data.setContent(chkCert.first->wireEncode());
akmhoqueb6450b12014-04-24 00:01:03 -0500226 m_nlsr.getKeyManager().signData(data);
227 m_nlsr.getNlsrFace()->put(data);
akmhoque53353462014-04-22 08:43:45 -0500228 }
229}
230
231
232void
akmhoqueb6450b12014-04-24 00:01:03 -0500233InterestManager::processInterestTimedOut(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500234{
235 std::cout << "Timed out interest : " << interest.getName().toUri() << std::endl;
236 string intName = interest.getName().toUri();
237 Tokenizer nt(intName, "/");
238 string chkString("info");
239 if (nt.doesTokenExist(chkString))
240 {
241 string nbr = nt.getTokenString(0, nt.getTokenPosition(chkString) - 1);
akmhoqueb6450b12014-04-24 00:01:03 -0500242 processInterestTimedOutInfo(nbr , interest);
akmhoque53353462014-04-22 08:43:45 -0500243 }
244 chkString = "LSA";
245 if (nt.doesTokenExist(chkString))
246 {
akmhoqueb6450b12014-04-24 00:01:03 -0500247 processInterestTimedOutLsa(interest);
akmhoque53353462014-04-22 08:43:45 -0500248 }
249}
250
251void
akmhoqueb6450b12014-04-24 00:01:03 -0500252InterestManager::processInterestTimedOutInfo(const string& neighbor,
akmhoque53353462014-04-22 08:43:45 -0500253 const ndn::Interest& interest)
254{
akmhoqueb6450b12014-04-24 00:01:03 -0500255 m_nlsr.getAdl().incrementTimedOutInterestCount(neighbor);
256 int status = m_nlsr.getAdl().getStatusOfNeighbor(neighbor);
257 int infoIntTimedOutCount = m_nlsr.getAdl().getTimedOutInterestCount(neighbor);
akmhoque53353462014-04-22 08:43:45 -0500258 std::cout << "Neighbor: " << neighbor << std::endl;
259 std::cout << "Status: " << status << std::endl;
260 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500261 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber()))
akmhoque53353462014-04-22 08:43:45 -0500262 {
263 string intName = neighbor + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -0500264 m_nlsr.getConfParameter().getRouterPrefix();
265 expressInterest(intName, 2,
266 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -0500267 }
268 else if ((status == 1) &&
akmhoqueb6450b12014-04-24 00:01:03 -0500269 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber()))
akmhoque53353462014-04-22 08:43:45 -0500270 {
akmhoqueb6450b12014-04-24 00:01:03 -0500271 m_nlsr.getAdl().setStatusOfNeighbor(neighbor, 0);
272 m_nlsr.incrementAdjBuildCount();
273 if (m_nlsr.getIsBuildAdjLsaSheduled() == 0)
akmhoque53353462014-04-22 08:43:45 -0500274 {
akmhoqueb6450b12014-04-24 00:01:03 -0500275 m_nlsr.setIsBuildAdjLsaSheduled(1);
akmhoque53353462014-04-22 08:43:45 -0500276 // event here
akmhoqueb6450b12014-04-24 00:01:03 -0500277 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
278 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
279 &m_nlsr.getLsdb(),
280 boost::ref(m_nlsr)));
akmhoque53353462014-04-22 08:43:45 -0500281 }
282 }
283}
284
285void
akmhoqueb6450b12014-04-24 00:01:03 -0500286InterestManager::processInterestTimedOutLsa(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500287{
288}
289
290void
akmhoqueb6450b12014-04-24 00:01:03 -0500291InterestManager::expressInterest(const string& interestNamePrefix,
akmhoque53353462014-04-22 08:43:45 -0500292 int scope, int seconds)
293{
294 std::cout << "Expressing Interest :" << interestNamePrefix << std::endl;
295 ndn::Interest i((ndn::Name(interestNamePrefix)));
296 i.setInterestLifetime(time::seconds(seconds));
297 i.setMustBeFresh(true);
akmhoqueb6450b12014-04-24 00:01:03 -0500298 m_nlsr.getNlsrFace()->expressInterest(i,
299 ndn::bind(&DataManager::processContent,
300 &m_nlsr.getDm(),
301 _1, _2, boost::ref(*this)),
302 ndn::bind(&InterestManager::processInterestTimedOut,
303 this, _1));
akmhoque53353462014-04-22 08:43:45 -0500304}
305
306
307void
akmhoqueb6450b12014-04-24 00:01:03 -0500308InterestManager::sendScheduledInfoInterest(int seconds)
akmhoque53353462014-04-22 08:43:45 -0500309{
akmhoqueb6450b12014-04-24 00:01:03 -0500310 std::list<Adjacent> adjList = m_nlsr.getAdl().getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500311 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
312 ++it)
313 {
314 string adjName = (*it).getName() + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -0500315 m_nlsr.getConfParameter().getRouterPrefix();
316 expressInterest(adjName, 2,
317 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -0500318 }
akmhoqueb6450b12014-04-24 00:01:03 -0500319 scheduleInfoInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
akmhoque53353462014-04-22 08:43:45 -0500320}
321
322void
akmhoqueb6450b12014-04-24 00:01:03 -0500323InterestManager::scheduleInfoInterest(int seconds)
akmhoque53353462014-04-22 08:43:45 -0500324{
akmhoqueb6450b12014-04-24 00:01:03 -0500325 EventId eid = m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
326 ndn::bind(&InterestManager::sendScheduledInfoInterest, this,
327 seconds));
akmhoque53353462014-04-22 08:43:45 -0500328}
329
330
331} //namespace nlsr