blob: 8525ce77f3ae83556bb882a54d9eaba3e67fbf4c [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/signature-sha256-with-rsa.hpp>
5#include <ndn-cxx/security/identity-certificate.hpp>
6#include <ndn-cxx/util/io.hpp>
akmhoque53353462014-04-22 08:43:45 -05007
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);
akmhoquec8a10f72014-04-25 18:42:55 -050063 int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
64 int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
65 neighbor);
akmhoque53353462014-04-22 08:43:45 -050066 //debugging purpose start
67 std::cout << "Before Updates: " << std::endl;
68 std::cout << "Neighbor : " << neighbor << std::endl;
69 std::cout << "Status: " << oldStatus << std::endl;
70 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
71 //debugging purpose end
akmhoquec8a10f72014-04-25 18:42:55 -050072 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1);
73 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
74 int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
75 infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
76 neighbor);
akmhoque53353462014-04-22 08:43:45 -050077 //debugging purpose
78 std::cout << "After Updates: " << std::endl;
79 std::cout << "Neighbor : " << neighbor << std::endl;
80 std::cout << "Status: " << newStatus << std::endl;
81 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
82 //debugging purpose end
83 if ((oldStatus - newStatus) != 0) // change in Adjacency list
84 {
akmhoqueb6450b12014-04-24 00:01:03 -050085 m_nlsr.incrementAdjBuildCount();
akmhoque53353462014-04-22 08:43:45 -050086 /* Need to schedule event for Adjacency LSA building */
akmhoqueb6450b12014-04-24 00:01:03 -050087 if (m_nlsr.getIsBuildAdjLsaSheduled() == 0)
akmhoque53353462014-04-22 08:43:45 -050088 {
akmhoqueb6450b12014-04-24 00:01:03 -050089 m_nlsr.setIsBuildAdjLsaSheduled(1);
akmhoque53353462014-04-22 08:43:45 -050090 // event here
akmhoqueb6450b12014-04-24 00:01:03 -050091 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
92 ndn::bind(&Lsdb::scheduledAdjLsaBuild, m_nlsr.getLsdb(),
93 boost::ref(m_nlsr)));
akmhoque53353462014-04-22 08:43:45 -050094 }
95 }
96}
97
98void
akmhoqueb6450b12014-04-24 00:01:03 -050099DataManager::processContentLsa(const string& dataName, string& dataContent)
akmhoque53353462014-04-22 08:43:45 -0500100{
101 Tokenizer nt(dataName, "/");
102 string chkString("LSA");
103 string origRouter = nt.getTokenString(nt.getTokenPosition(chkString) + 1,
104 nt.getTokenNumber() - 4);
105 string lsTypeString = nt.getToken(nt.getTokenNumber() - 3);
106 string lsSeNoString = nt.getToken(nt.getTokenNumber() - 2);
107 uint32_t interestedLsSeqNo;
108 try
109 {
110 interestedLsSeqNo = boost::lexical_cast<uint32_t>(lsSeNoString);
111 }
112 catch (std::exception& e)
113 {
114 return;
115 }
116 if (lsTypeString == "1") //Name Lsa
117 {
akmhoqueb6450b12014-04-24 00:01:03 -0500118 processContentNameLsa(origRouter + "/" + lsTypeString,
akmhoque53353462014-04-22 08:43:45 -0500119 interestedLsSeqNo, dataContent);
120 }
121 else if (lsTypeString == "2") //Adj Lsa
122 {
akmhoqueb6450b12014-04-24 00:01:03 -0500123 processContentAdjLsa(origRouter + "/" + lsTypeString,
akmhoque53353462014-04-22 08:43:45 -0500124 interestedLsSeqNo, dataContent);
125 }
126 else if (lsTypeString == "3") //Cor Lsa
127 {
akmhoqueb6450b12014-04-24 00:01:03 -0500128 processContentCorLsa(origRouter + "/" + lsTypeString,
akmhoque53353462014-04-22 08:43:45 -0500129 interestedLsSeqNo, dataContent);
130 }
131 else
132 {
133 cout << "Unrecognized LSA Type :(" << endl;
134 }
135}
136
137void
akmhoqueb6450b12014-04-24 00:01:03 -0500138DataManager::processContentNameLsa(const string& lsaKey,
akmhoque53353462014-04-22 08:43:45 -0500139 uint32_t lsSeqNo, string& dataContent)
140{
akmhoqueb6450b12014-04-24 00:01:03 -0500141 if (m_nlsr.getLsdb().isNameLsaNew(lsaKey, lsSeqNo))
akmhoque53353462014-04-22 08:43:45 -0500142 {
143 NameLsa nameLsa;
144 if (nameLsa.initializeFromContent(dataContent))
145 {
akmhoqueb6450b12014-04-24 00:01:03 -0500146 m_nlsr.getLsdb().installNameLsa(m_nlsr, nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500147 }
148 else
149 {
150 std::cout << "LSA data decoding error :(" << std::endl;
151 }
152 }
153}
154
155void
akmhoqueb6450b12014-04-24 00:01:03 -0500156DataManager::processContentAdjLsa(const string& lsaKey,
akmhoque53353462014-04-22 08:43:45 -0500157 uint32_t lsSeqNo, string& dataContent)
158{
akmhoqueb6450b12014-04-24 00:01:03 -0500159 if (m_nlsr.getLsdb().isAdjLsaNew(lsaKey, lsSeqNo))
akmhoque53353462014-04-22 08:43:45 -0500160 {
161 AdjLsa adjLsa;
162 if (adjLsa.initializeFromContent(dataContent))
163 {
akmhoqueb6450b12014-04-24 00:01:03 -0500164 m_nlsr.getLsdb().installAdjLsa(m_nlsr, adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500165 }
166 else
167 {
168 std::cout << "LSA data decoding error :(" << std::endl;
169 }
170 }
171}
172
173void
akmhoqueb6450b12014-04-24 00:01:03 -0500174DataManager::processContentCorLsa(const string& lsaKey,
akmhoque53353462014-04-22 08:43:45 -0500175 uint32_t lsSeqNo, string& dataContent)
176{
akmhoqueb6450b12014-04-24 00:01:03 -0500177 if (m_nlsr.getLsdb().isCoordinateLsaNew(lsaKey, lsSeqNo))
akmhoque53353462014-04-22 08:43:45 -0500178 {
akmhoqueb6450b12014-04-24 00:01:03 -0500179 CoordinateLsa corLsa;
akmhoque53353462014-04-22 08:43:45 -0500180 if (corLsa.initializeFromContent(dataContent))
181 {
akmhoqueb6450b12014-04-24 00:01:03 -0500182 m_nlsr.getLsdb().installCoordinateLsa(m_nlsr, corLsa);
akmhoque53353462014-04-22 08:43:45 -0500183 }
184 else
185 {
186 std::cout << "LSA data decoding error :(" << std::endl;
187 }
188 }
189}
190
191void
akmhoqueb6450b12014-04-24 00:01:03 -0500192DataManager::processContentKeys(const ndn::Data& data)
akmhoque53353462014-04-22 08:43:45 -0500193{
194 std::cout << " processContentKeys called " << std::endl;
195 ndn::shared_ptr<ndn::IdentityCertificate> cert =
196 ndn::make_shared<ndn::IdentityCertificate>();
197 cert->wireDecode(data.getContent().blockFromValue());
198 std::cout << *(cert) << std::endl;
199 std::string dataName = data.getName().toUri();
200 Tokenizer nt(dataName, "/");
201 std::string certName = nt.getTokenString(0, nt.getTokenNumber() - 3);
202 uint32_t seqNum = boost::lexical_cast<uint32_t>(nt.getToken(
203 nt.getTokenNumber() - 2));
204 std::cout << "Cert Name: " << certName << " Seq Num: " << seqNum << std::endl;
Yingdi Yu0c217822014-04-24 14:22:42 -0700205 // if (m_nlsr.getKeyManager().verify(m_nlsr, *(cert)))
206 // {
207 // m_nlsr.getKeyManager().addCertificate(cert, seqNum, true);
208 // }
209 // else
210 // {
211 // m_nlsr.getKeyManager().addCertificate(cert, seqNum, false);
212 // }
213 // m_nlsr.getKeyManager().printCertStore();
akmhoque53353462014-04-22 08:43:45 -0500214}
215}//namespace nlsr