blob: f56dd790677807b7023f31ad5ad2ee61022a0f48 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <cstdlib>
3
4#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
5#include <ndn-cpp-dev/security/identity-certificate.hpp>
6#include <ndn-cpp-dev/util/io.hpp>
7
8#include "nlsr.hpp"
9#include "data-manager.hpp"
10#include "utility/tokenizer.hpp"
11#include "lsdb.hpp"
Yingdi Yu0c217822014-04-24 14:22:42 -070012// #include "security/key-manager.hpp"
akmhoque53353462014-04-22 08:43:45 -050013
14namespace nlsr {
15
16using namespace std;
17using namespace ndn;
18
19void
akmhoqueb6450b12014-04-24 00:01:03 -050020DataManager::processContent(const ndn::Interest& interest,
akmhoque53353462014-04-22 08:43:45 -050021 const ndn::Data& data, InterestManager& im)
22{
23 std::cout << "I: " << interest.toUri() << std::endl;
24 string dataName(data.getName().toUri());
25 Tokenizer nt(dataName, "/");
26 std::string chkString("keys");
27 if (nt.doesTokenExist(chkString))
28 {
akmhoqueb6450b12014-04-24 00:01:03 -050029 processContentKeys(data);
akmhoque53353462014-04-22 08:43:45 -050030 }
31 else
32 {
Yingdi Yu0c217822014-04-24 14:22:42 -070033 // if (m_nlsr.getKeyManager().verify(data))
akmhoque53353462014-04-22 08:43:45 -050034 {
35 std::cout << "Verified Data Content" << std::endl;
36 chkString = "info";
37 if (nt.doesTokenExist(chkString))
38 {
39 string dataContent((char*)data.getContent().value());
akmhoqueb6450b12014-04-24 00:01:03 -050040 processContentInfo(dataName, dataContent);
akmhoque53353462014-04-22 08:43:45 -050041 }
42 chkString = "LSA";
43 if (nt.doesTokenExist(chkString))
44 {
45 string dataContent((char*)data.getContent().value());
akmhoqueb6450b12014-04-24 00:01:03 -050046 processContentLsa(dataName, dataContent);
akmhoque53353462014-04-22 08:43:45 -050047 }
48 }
Yingdi Yu0c217822014-04-24 14:22:42 -070049 // else
50 // {
51 // std::cout << "Unverified Data Content. Discarded" << std::endl;
52 // }
akmhoque53353462014-04-22 08:43:45 -050053 }
54}
55
56void
akmhoqueb6450b12014-04-24 00:01:03 -050057DataManager::processContentInfo(const string& dataName,
akmhoque53353462014-04-22 08:43:45 -050058 string& dataContent)
59{
60 Tokenizer nt(dataName, "/");
61 string chkString("info");
62 string neighbor = nt.getTokenString(0, nt.getTokenPosition(chkString) - 1);
akmhoqueb6450b12014-04-24 00:01:03 -050063 int oldStatus = m_nlsr.getAdl().getStatusOfNeighbor(neighbor);
64 int infoIntTimedOutCount = m_nlsr.getAdl().getTimedOutInterestCount(neighbor);
akmhoque53353462014-04-22 08:43:45 -050065 //debugging purpose start
66 std::cout << "Before Updates: " << std::endl;
67 std::cout << "Neighbor : " << neighbor << std::endl;
68 std::cout << "Status: " << oldStatus << std::endl;
69 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
70 //debugging purpose end
akmhoqueb6450b12014-04-24 00:01:03 -050071 m_nlsr.getAdl().setStatusOfNeighbor(neighbor, 1);
72 m_nlsr.getAdl().setTimedOutInterestCount(neighbor, 0);
73 int newStatus = m_nlsr.getAdl().getStatusOfNeighbor(neighbor);
74 infoIntTimedOutCount = m_nlsr.getAdl().getTimedOutInterestCount(neighbor);
akmhoque53353462014-04-22 08:43:45 -050075 //debugging purpose
76 std::cout << "After Updates: " << std::endl;
77 std::cout << "Neighbor : " << neighbor << std::endl;
78 std::cout << "Status: " << newStatus << std::endl;
79 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
80 //debugging purpose end
81 if ((oldStatus - newStatus) != 0) // change in Adjacency list
82 {
akmhoqueb6450b12014-04-24 00:01:03 -050083 m_nlsr.incrementAdjBuildCount();
akmhoque53353462014-04-22 08:43:45 -050084 /* Need to schedule event for Adjacency LSA building */
akmhoqueb6450b12014-04-24 00:01:03 -050085 if (m_nlsr.getIsBuildAdjLsaSheduled() == 0)
akmhoque53353462014-04-22 08:43:45 -050086 {
akmhoqueb6450b12014-04-24 00:01:03 -050087 m_nlsr.setIsBuildAdjLsaSheduled(1);
akmhoque53353462014-04-22 08:43:45 -050088 // event here
akmhoqueb6450b12014-04-24 00:01:03 -050089 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
90 ndn::bind(&Lsdb::scheduledAdjLsaBuild, m_nlsr.getLsdb(),
91 boost::ref(m_nlsr)));
akmhoque53353462014-04-22 08:43:45 -050092 }
93 }
94}
95
96void
akmhoqueb6450b12014-04-24 00:01:03 -050097DataManager::processContentLsa(const string& dataName, string& dataContent)
akmhoque53353462014-04-22 08:43:45 -050098{
99 Tokenizer nt(dataName, "/");
100 string chkString("LSA");
101 string origRouter = nt.getTokenString(nt.getTokenPosition(chkString) + 1,
102 nt.getTokenNumber() - 4);
103 string lsTypeString = nt.getToken(nt.getTokenNumber() - 3);
104 string lsSeNoString = nt.getToken(nt.getTokenNumber() - 2);
105 uint32_t interestedLsSeqNo;
106 try
107 {
108 interestedLsSeqNo = boost::lexical_cast<uint32_t>(lsSeNoString);
109 }
110 catch (std::exception& e)
111 {
112 return;
113 }
114 if (lsTypeString == "1") //Name Lsa
115 {
akmhoqueb6450b12014-04-24 00:01:03 -0500116 processContentNameLsa(origRouter + "/" + lsTypeString,
akmhoque53353462014-04-22 08:43:45 -0500117 interestedLsSeqNo, dataContent);
118 }
119 else if (lsTypeString == "2") //Adj Lsa
120 {
akmhoqueb6450b12014-04-24 00:01:03 -0500121 processContentAdjLsa(origRouter + "/" + lsTypeString,
akmhoque53353462014-04-22 08:43:45 -0500122 interestedLsSeqNo, dataContent);
123 }
124 else if (lsTypeString == "3") //Cor Lsa
125 {
akmhoqueb6450b12014-04-24 00:01:03 -0500126 processContentCorLsa(origRouter + "/" + lsTypeString,
akmhoque53353462014-04-22 08:43:45 -0500127 interestedLsSeqNo, dataContent);
128 }
129 else
130 {
131 cout << "Unrecognized LSA Type :(" << endl;
132 }
133}
134
135void
akmhoqueb6450b12014-04-24 00:01:03 -0500136DataManager::processContentNameLsa(const string& lsaKey,
akmhoque53353462014-04-22 08:43:45 -0500137 uint32_t lsSeqNo, string& dataContent)
138{
akmhoqueb6450b12014-04-24 00:01:03 -0500139 if (m_nlsr.getLsdb().isNameLsaNew(lsaKey, lsSeqNo))
akmhoque53353462014-04-22 08:43:45 -0500140 {
141 NameLsa nameLsa;
142 if (nameLsa.initializeFromContent(dataContent))
143 {
akmhoqueb6450b12014-04-24 00:01:03 -0500144 m_nlsr.getLsdb().installNameLsa(m_nlsr, nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500145 }
146 else
147 {
148 std::cout << "LSA data decoding error :(" << std::endl;
149 }
150 }
151}
152
153void
akmhoqueb6450b12014-04-24 00:01:03 -0500154DataManager::processContentAdjLsa(const string& lsaKey,
akmhoque53353462014-04-22 08:43:45 -0500155 uint32_t lsSeqNo, string& dataContent)
156{
akmhoqueb6450b12014-04-24 00:01:03 -0500157 if (m_nlsr.getLsdb().isAdjLsaNew(lsaKey, lsSeqNo))
akmhoque53353462014-04-22 08:43:45 -0500158 {
159 AdjLsa adjLsa;
160 if (adjLsa.initializeFromContent(dataContent))
161 {
akmhoqueb6450b12014-04-24 00:01:03 -0500162 m_nlsr.getLsdb().installAdjLsa(m_nlsr, adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500163 }
164 else
165 {
166 std::cout << "LSA data decoding error :(" << std::endl;
167 }
168 }
169}
170
171void
akmhoqueb6450b12014-04-24 00:01:03 -0500172DataManager::processContentCorLsa(const string& lsaKey,
akmhoque53353462014-04-22 08:43:45 -0500173 uint32_t lsSeqNo, string& dataContent)
174{
akmhoqueb6450b12014-04-24 00:01:03 -0500175 if (m_nlsr.getLsdb().isCoordinateLsaNew(lsaKey, lsSeqNo))
akmhoque53353462014-04-22 08:43:45 -0500176 {
akmhoqueb6450b12014-04-24 00:01:03 -0500177 CoordinateLsa corLsa;
akmhoque53353462014-04-22 08:43:45 -0500178 if (corLsa.initializeFromContent(dataContent))
179 {
akmhoqueb6450b12014-04-24 00:01:03 -0500180 m_nlsr.getLsdb().installCoordinateLsa(m_nlsr, corLsa);
akmhoque53353462014-04-22 08:43:45 -0500181 }
182 else
183 {
184 std::cout << "LSA data decoding error :(" << std::endl;
185 }
186 }
187}
188
189void
akmhoqueb6450b12014-04-24 00:01:03 -0500190DataManager::processContentKeys(const ndn::Data& data)
akmhoque53353462014-04-22 08:43:45 -0500191{
192 std::cout << " processContentKeys called " << std::endl;
193 ndn::shared_ptr<ndn::IdentityCertificate> cert =
194 ndn::make_shared<ndn::IdentityCertificate>();
195 cert->wireDecode(data.getContent().blockFromValue());
196 std::cout << *(cert) << std::endl;
197 std::string dataName = data.getName().toUri();
198 Tokenizer nt(dataName, "/");
199 std::string certName = nt.getTokenString(0, nt.getTokenNumber() - 3);
200 uint32_t seqNum = boost::lexical_cast<uint32_t>(nt.getToken(
201 nt.getTokenNumber() - 2));
202 std::cout << "Cert Name: " << certName << " Seq Num: " << seqNum << std::endl;
Yingdi Yu0c217822014-04-24 14:22:42 -0700203 // if (m_nlsr.getKeyManager().verify(m_nlsr, *(cert)))
204 // {
205 // m_nlsr.getKeyManager().addCertificate(cert, seqNum, true);
206 // }
207 // else
208 // {
209 // m_nlsr.getKeyManager().addCertificate(cert, seqNum, false);
210 // }
211 // m_nlsr.getKeyManager().printCertStore();
akmhoque53353462014-04-22 08:43:45 -0500212}
213}//namespace nlsr