blob: 92a911df6cb5f9c07dd250d2d1928b63cdddc90e [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#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 "nlsr_dm.hpp"
10#include "utility/nlsr_tokenizer.hpp"
11#include "nlsr_lsdb.hpp"
12
13namespace nlsr
14{
15
16 using namespace std;
17 using namespace ndn;
18
19 void
20 DataManager::processContent(Nlsr& pnlsr, const ndn::Interest &interest,
21 const ndn::Data & data, interestManager& im)
22 {
23 cout << "I: " << interest.toUri() << endl;
24 string dataName(data.getName().toUri());
25 //cout << "D: " << dataName << endl;
26 //cout << "Data Content: " << dataContent << endl;
27 nlsrTokenizer nt(dataName,"/");
28 //SignatureSha256WithRsa sig(data.getSignature());
29 //ndn::Name keyName=sig.getKeyLocator().getName();
30 //cout<<"Key Locator Name: "<<keyName.toUri()<<endl;
31 string chkString("info");
32 if( nt.doesTokenExist(chkString) )
33 {
34 string dataContent((char *)data.getContent().value());
35 processContentInfo(pnlsr,dataName,dataContent);
36 }
37 chkString="LSA";
38 if( nt.doesTokenExist(chkString) )
39 {
40 string dataContent((char *)data.getContent().value());
41 processContentLsa(pnlsr, dataName, dataContent);
42 }
43 chkString="keys";
44 if( nt.doesTokenExist(chkString) )
45 {
46 processContentKeys(pnlsr, data);
47 }
48 }
49
50 void
51 DataManager::processContentInfo(Nlsr& pnlsr, string& dataName,
52 string& dataContent)
53 {
54 nlsrTokenizer nt(dataName,"/");
55 string chkString("info");
56 string neighbor=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
57 int oldStatus=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
58 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
59 //debugging purpose start
60 cout <<"Before Updates: " <<endl;
61 cout <<"Neighbor : "<<neighbor<<endl;
62 cout<<"Status: "<< oldStatus << endl;
63 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
64 //debugging purpose end
65 pnlsr.getAdl().setStatusOfNeighbor(neighbor,1);
66 pnlsr.getAdl().setTimedOutInterestCount(neighbor,0);
67 int newStatus=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
68 infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
69 //debugging purpose
70 cout <<"After Updates: " <<endl;
71 cout <<"Neighbor : "<<neighbor<<endl;
72 cout<<"Status: "<< newStatus << endl;
73 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
74 //debugging purpose end
75 if ( ( oldStatus-newStatus)!= 0 ) // change in Adjacency list
76 {
77 pnlsr.incrementAdjBuildCount();
78 /* Need to schedule event for Adjacency LSA building */
79 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
80 {
81 pnlsr.setIsBuildAdjLsaSheduled(1);
82 // event here
83 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
84 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
85 boost::ref(pnlsr)));
86 }
87 }
88 }
89
90 void
91 DataManager::processContentLsa(Nlsr& pnlsr, string& dataName,
92 string& dataContent)
93 {
94 nlsrTokenizer nt(dataName,"/");
95 string chkString("LSA");
96 string origRouter=nt.getTokenString(nt.getTokenPosition(chkString)+1,
97 nt.getTokenNumber()-4);
98 string lsTypeString=nt.getToken(nt.getTokenNumber()-3);
99 string lsSeNoString=nt.getToken(nt.getTokenNumber()-2);
100 uint32_t interestedLsSeqNo;
101 try
102 {
103 interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeNoString);
104 }
105 catch(std::exception &e)
106 {
107 return;
108 }
109 if( lsTypeString == "1" ) //Name Lsa
110 {
111 processContentNameLsa(pnlsr, origRouter+"/"+lsTypeString,
112 interestedLsSeqNo, dataContent);
113 }
114 else if( lsTypeString == "2" ) //Adj Lsa
115 {
116 processContentAdjLsa(pnlsr, origRouter+"/"+lsTypeString,
117 interestedLsSeqNo, dataContent);
118 }
119 else if( lsTypeString == "3" ) //Cor Lsa
120 {
121 processContentCorLsa(pnlsr, origRouter+"/"+lsTypeString,
122 interestedLsSeqNo, dataContent);
123 }
124 else
125 {
126 cout<<"Unrecognized LSA Type :("<<endl;
127 }
128 }
129
130 void
131 DataManager::processContentNameLsa(Nlsr& pnlsr, string lsaKey,
132 uint32_t lsSeqNo, string& dataContent)
133 {
134 if ( pnlsr.getLsdb().isNameLsaNew(lsaKey,lsSeqNo))
135 {
136 NameLsa nameLsa;
137 if( nameLsa.initNameLsaFromContent(dataContent) )
138 {
139 pnlsr.getLsdb().installNameLsa(pnlsr, nameLsa);
140 }
141 else
142 {
143 cout<<"LSA data decoding error :("<<endl;
144 }
145 }
146 }
147
148 void
149 DataManager::processContentAdjLsa(Nlsr& pnlsr, string lsaKey,
150 uint32_t lsSeqNo, string& dataContent)
151 {
152 if ( pnlsr.getLsdb().isAdjLsaNew(lsaKey,lsSeqNo))
153 {
154 AdjLsa adjLsa;
155 if( adjLsa.initAdjLsaFromContent(dataContent) )
156 {
157 pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
158 }
159 else
160 {
161 cout<<"LSA data decoding error :("<<endl;
162 }
163 }
164 }
165
166 void
167 DataManager::processContentCorLsa(Nlsr& pnlsr, string lsaKey,
168 uint32_t lsSeqNo, string& dataContent)
169 {
170 if ( pnlsr.getLsdb().isCorLsaNew(lsaKey,lsSeqNo))
171 {
172 CorLsa corLsa;
173 if( corLsa.initCorLsaFromContent(dataContent) )
174 {
175 pnlsr.getLsdb().installCorLsa(pnlsr, corLsa);
176 }
177 else
178 {
179 cout<<"LSA data decoding error :("<<endl;
180 }
181 }
182 }
183
184 void
185 DataManager::processContentKeys(Nlsr& pnlsr, const ndn::Data& data)
186 {
187 std::ofstream outFile("data_received");
188 ndn::io::save(data,outFile,ndn::io::NO_ENCODING);
189 cout<<" processContentKeys called "<<endl;
190 SignatureSha256WithRsa signature(data.getSignature());
191 cout<<"D: <<"<<data<<endl;
192 cout<<"Key Locator: "<<signature.getKeyLocator().getName().toUri()<<endl;
193 ndn::shared_ptr<ndn::IdentityCertificate> cert=ndn::make_shared<ndn::IdentityCertificate>();
194 cert->wireDecode(data.getContent().blockFromValue());
195 cout<<*(cert)<<endl;
196
197 if( pnlsr.getKeyManager().verifySignature(*(cert),cert->getPublicKeyInfo()))
198 {
199 cout<<"Verified Data"<<endl;
200 }
201 }
202}//namespace nlsr