blob: 23ce5799d39bebeeec9ad903a66395a7e0d0449f [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <cstdlib>
3
akmhoquec8a10f72014-04-25 18:42:55 -05004#include <ndn-cxx/security/identity-certificate.hpp>
5#include <ndn-cxx/util/io.hpp>
akmhoque53353462014-04-22 08:43:45 -05006
7#include "nlsr.hpp"
8#include "interest-manager.hpp"
9#include "data-manager.hpp"
10#include "utility/tokenizer.hpp"
11#include "lsdb.hpp"
12
13namespace nlsr {
14
15using namespace std;
16using namespace ndn;
17
18void
akmhoqueb6450b12014-04-24 00:01:03 -050019InterestManager::processInterest(const ndn::Name& name,
akmhoque53353462014-04-22 08:43:45 -050020 const ndn::Interest& interest)
21{
22 cout << "<< I: " << interest << endl;
23 string intName = interest.getName().toUri();
24 cout << "Interest Received for Name: " << intName << endl;
25 Tokenizer nt(intName, "/");
26 string chkString("info");
27 if (nt.doesTokenExist(chkString))
28 {
29 string nbr = nt.getTokenString(nt.getTokenPosition(chkString) + 1);
30 cout << "Neighbor: " << nbr << endl;
akmhoqueb6450b12014-04-24 00:01:03 -050031 processInterestInfo(nbr, interest);
akmhoque53353462014-04-22 08:43:45 -050032 }
33 chkString = "LSA";
34 if (nt.doesTokenExist(chkString))
35 {
akmhoqueb6450b12014-04-24 00:01:03 -050036 processInterestLsa(interest);
akmhoque53353462014-04-22 08:43:45 -050037 }
38 chkString = "keys";
39 if (nt.doesTokenExist(chkString))
40 {
akmhoqueb6450b12014-04-24 00:01:03 -050041 processInterestKeys(interest);
akmhoque53353462014-04-22 08:43:45 -050042 }
43}
44
45void
akmhoqueb6450b12014-04-24 00:01:03 -050046InterestManager::processInterestInfo(const string& neighbor,
akmhoque53353462014-04-22 08:43:45 -050047 const ndn::Interest& interest)
48{
akmhoquec8a10f72014-04-25 18:42:55 -050049 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor))
akmhoque53353462014-04-22 08:43:45 -050050 {
51 Data data(ndn::Name(interest.getName()).appendVersion());
52 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
53 data.setContent((const uint8_t*)"info", sizeof("info"));
Yingdi Yu0c217822014-04-24 14:22:42 -070054 // m_nlsr.getKeyManager().signData(data);
55 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -050056 cout << ">> D: " << data << endl;
akmhoquefdbddb12014-05-02 18:35:19 -050057 m_nlsr.getNlsrFace().put(data);
akmhoquec8a10f72014-04-25 18:42:55 -050058 int status = m_nlsr.getAdjacencyList().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());
Yingdi Yu0c217822014-04-24 14:22:42 -0700128 // m_nlsr.getKeyManager().signData(data);
129 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -0500130 std::cout << ">> D: " << data << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500131 m_nlsr.getNlsrFace().put(data);
akmhoque53353462014-04-22 08:43:45 -0500132 }
133 }
134}
135
136void
akmhoqueb6450b12014-04-24 00:01:03 -0500137InterestManager::processInterestForAdjLsa(const ndn::Interest& interest,
138 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500139{
akmhoqueb6450b12014-04-24 00:01:03 -0500140 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
141 if (adjLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500142 {
akmhoqueb6450b12014-04-24 00:01:03 -0500143 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500144 {
145 Data data(ndn::Name(interest.getName()).appendVersion());
146 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500147 string content = adjLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500148 data.setContent((const uint8_t*)content.c_str(), content.size());
Yingdi Yu0c217822014-04-24 14:22:42 -0700149 // m_nlsr.getKeyManager().signData(data);
150 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -0500151 std::cout << ">> D: " << data << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500152 m_nlsr.getNlsrFace().put(data);
akmhoque53353462014-04-22 08:43:45 -0500153 }
154 }
155}
156
157void
akmhoqueb6450b12014-04-24 00:01:03 -0500158InterestManager::processInterestForCorLsa(const ndn::Interest& interest,
159 const string& lsaKey, uint32_t interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500160{
akmhoqueb6450b12014-04-24 00:01:03 -0500161 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
162 if (corLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500163 {
akmhoqueb6450b12014-04-24 00:01:03 -0500164 if (corLsa->getLsSeqNo() >= interestedlsSeqNo)
akmhoque53353462014-04-22 08:43:45 -0500165 {
166 Data data(ndn::Name(interest.getName()).appendVersion());
167 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoqueb6450b12014-04-24 00:01:03 -0500168 string content = corLsa->getData();
akmhoque53353462014-04-22 08:43:45 -0500169 data.setContent((const uint8_t*)content.c_str(), content.size());
Yingdi Yu0c217822014-04-24 14:22:42 -0700170 // m_nlsr.getKeyManager().signData(data);
171 m_keyChain.sign(data);
akmhoque53353462014-04-22 08:43:45 -0500172 std::cout << ">> D: " << data << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500173 m_nlsr.getNlsrFace().put(data);
akmhoque53353462014-04-22 08:43:45 -0500174 }
175 }
176}
177
178void
akmhoqueb6450b12014-04-24 00:01:03 -0500179InterestManager::processInterestKeys(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500180{
181 std::cout << "processInterestKeys called " << std::endl;
Yingdi Yu0c217822014-04-24 14:22:42 -0700182 // string intName = interest.getName().toUri();
183 // std::cout << "Interest Name for Key: " << intName << std::endl;
184 // Tokenizer nt(intName, "/");
185 // std::string chkString("ID-CERT");
186 // std::string certName;
187 // uint32_t seqNum;
188 // ndn::Name dataName;
189 // std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert;
190 // if (nt.getTokenPosition(chkString) == nt.getTokenNumber() - 1)
191 // {
192 // certName = nt.getTokenString(0, nt.getTokenNumber() - 1);
193 // cout << "Cert Name: " << certName << std::endl;
194 // chkCert = m_nlsr.getKeyManager().getCertificateFromStore(certName);
195 // }
196 // else
197 // {
198 // certName = nt.getTokenString(0, nt.getTokenNumber() - 2);
199 // seqNum = boost::lexical_cast<uint32_t>(nt.getToken(nt.getTokenNumber() - 1));
200 // std::cout << "Cert Name: " << certName << " Seq Num: " << seqNum << std::endl;
201 // chkCert = m_nlsr.getKeyManager().getCertificateFromStore(certName, seqNum);
202 // }
203 // if (chkCert.second)
204 // {
205 // if (nt.getTokenPosition(chkString) == nt.getTokenNumber() - 1)
206 // {
207 // std::string dn;
208 // dataName = ndn::Name(interest.getName()).appendVersion();
209 // std::pair<uint32_t, bool> seqChk =
210 // m_nlsr.getKeyManager().getCertificateSeqNum(certName);
211 // if (seqChk.second)
212 // {
213 // dn = dataName.toUri() + "/" + boost::lexical_cast<std::string>(seqChk.first);
214 // dataName = ndn::Name(dn);
215 // }
216 // else
217 // {
218 // dn = dataName.toUri() + "/" + boost::lexical_cast<std::string>(10);
219 // dataName = ndn::Name(dn);
220 // }
221 // }
222 // else
223 // {
224 // dataName = ndn::Name(interest.getName());
225 // }
226 // Data data(dataName.appendVersion());
227 // data.setFreshnessPeriod(time::seconds(10)); //10 sec
228 // data.setContent(chkCert.first->wireEncode());
229 // m_nlsr.getKeyManager().signData(data);
230 // m_nlsr.getNlsrFace()->put(data);
231 // }
akmhoque53353462014-04-22 08:43:45 -0500232}
233
234
235void
akmhoqueb6450b12014-04-24 00:01:03 -0500236InterestManager::processInterestTimedOut(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500237{
238 std::cout << "Timed out interest : " << interest.getName().toUri() << std::endl;
239 string intName = interest.getName().toUri();
240 Tokenizer nt(intName, "/");
241 string chkString("info");
242 if (nt.doesTokenExist(chkString))
243 {
244 string nbr = nt.getTokenString(0, nt.getTokenPosition(chkString) - 1);
akmhoqueb6450b12014-04-24 00:01:03 -0500245 processInterestTimedOutInfo(nbr , interest);
akmhoque53353462014-04-22 08:43:45 -0500246 }
247 chkString = "LSA";
248 if (nt.doesTokenExist(chkString))
249 {
akmhoqueb6450b12014-04-24 00:01:03 -0500250 processInterestTimedOutLsa(interest);
akmhoque53353462014-04-22 08:43:45 -0500251 }
252}
253
254void
akmhoqueb6450b12014-04-24 00:01:03 -0500255InterestManager::processInterestTimedOutInfo(const string& neighbor,
akmhoque53353462014-04-22 08:43:45 -0500256 const ndn::Interest& interest)
257{
akmhoquec8a10f72014-04-25 18:42:55 -0500258 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
259 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoquefdbddb12014-05-02 18:35:19 -0500260 uint32_t infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque53353462014-04-22 08:43:45 -0500261 std::cout << "Neighbor: " << neighbor << std::endl;
262 std::cout << "Status: " << status << std::endl;
263 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500264 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber()))
akmhoque53353462014-04-22 08:43:45 -0500265 {
266 string intName = neighbor + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -0500267 m_nlsr.getConfParameter().getRouterPrefix();
268 expressInterest(intName, 2,
269 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -0500270 }
271 else if ((status == 1) &&
akmhoqueb6450b12014-04-24 00:01:03 -0500272 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber()))
akmhoque53353462014-04-22 08:43:45 -0500273 {
akmhoquec8a10f72014-04-25 18:42:55 -0500274 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0);
akmhoqueb6450b12014-04-24 00:01:03 -0500275 m_nlsr.incrementAdjBuildCount();
276 if (m_nlsr.getIsBuildAdjLsaSheduled() == 0)
akmhoque53353462014-04-22 08:43:45 -0500277 {
akmhoqueb6450b12014-04-24 00:01:03 -0500278 m_nlsr.setIsBuildAdjLsaSheduled(1);
akmhoque53353462014-04-22 08:43:45 -0500279 // event here
akmhoqueb6450b12014-04-24 00:01:03 -0500280 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
281 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
282 &m_nlsr.getLsdb(),
283 boost::ref(m_nlsr)));
akmhoque53353462014-04-22 08:43:45 -0500284 }
285 }
286}
287
288void
akmhoqueb6450b12014-04-24 00:01:03 -0500289InterestManager::processInterestTimedOutLsa(const ndn::Interest& interest)
akmhoque53353462014-04-22 08:43:45 -0500290{
291}
292
293void
akmhoqueb6450b12014-04-24 00:01:03 -0500294InterestManager::expressInterest(const string& interestNamePrefix,
akmhoquefdbddb12014-05-02 18:35:19 -0500295 int32_t scope, int32_t seconds)
akmhoque53353462014-04-22 08:43:45 -0500296{
297 std::cout << "Expressing Interest :" << interestNamePrefix << std::endl;
298 ndn::Interest i((ndn::Name(interestNamePrefix)));
299 i.setInterestLifetime(time::seconds(seconds));
300 i.setMustBeFresh(true);
akmhoquefdbddb12014-05-02 18:35:19 -0500301 m_nlsr.getNlsrFace().expressInterest(i,
302 ndn::bind(&DataManager::processContent,
303 &m_nlsr.getDataManager(),
304 _1, _2, boost::ref(*this)),
305 ndn::bind(&InterestManager::processInterestTimedOut,
306 this, _1));
akmhoque53353462014-04-22 08:43:45 -0500307}
308
309
310void
akmhoquefdbddb12014-05-02 18:35:19 -0500311InterestManager::sendScheduledInfoInterest(int32_t seconds)
akmhoque53353462014-04-22 08:43:45 -0500312{
akmhoquec8a10f72014-04-25 18:42:55 -0500313 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500314 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
315 ++it)
316 {
317 string adjName = (*it).getName() + "/" + "info" +
akmhoqueb6450b12014-04-24 00:01:03 -0500318 m_nlsr.getConfParameter().getRouterPrefix();
319 expressInterest(adjName, 2,
320 m_nlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -0500321 }
akmhoqueb6450b12014-04-24 00:01:03 -0500322 scheduleInfoInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
akmhoque53353462014-04-22 08:43:45 -0500323}
324
325void
akmhoquefdbddb12014-05-02 18:35:19 -0500326InterestManager::scheduleInfoInterest(int32_t seconds)
akmhoque53353462014-04-22 08:43:45 -0500327{
akmhoqueb6450b12014-04-24 00:01:03 -0500328 EventId eid = m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
329 ndn::bind(&InterestManager::sendScheduledInfoInterest, this,
330 seconds));
akmhoque53353462014-04-22 08:43:45 -0500331}
332
333
334} //namespace nlsr