blob: ffb85e37912a0f9d6b7bf211849727d254e7ed70 [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"
akmhoqueeb764c52014-03-11 16:01:09 -050012#include "security/nlsr_km.hpp"
akmhoqueba094742014-02-28 11:47:21 -060013
14namespace nlsr
15{
16
akmhoque5a44dd42014-03-12 18:11:32 -050017 using namespace std;
18 using namespace ndn;
akmhoqueba094742014-02-28 11:47:21 -060019
akmhoque5a44dd42014-03-12 18:11:32 -050020 void
21 DataManager::processContent(Nlsr& pnlsr, const ndn::Interest &interest,
22 const ndn::Data & data, interestManager& im)
23 {
24 cout << "I: " << interest.toUri() << endl;
25 string dataName(data.getName().toUri());
26 nlsrTokenizer nt(dataName,"/");
27 std::string chkString("keys");
28 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060029 {
akmhoque5a44dd42014-03-12 18:11:32 -050030 processContentKeys(pnlsr, data);
31 }
32 else
33 {
34 if ( pnlsr.getKeyManager().verify(data))
35 {
36 std::cout<<"Verified Data Content"<<std::endl;
37 chkString="info";
akmhoqueba094742014-02-28 11:47:21 -060038 if( nt.doesTokenExist(chkString) )
39 {
akmhoque5a44dd42014-03-12 18:11:32 -050040 string dataContent((char *)data.getContent().value());
41 processContentInfo(pnlsr,dataName,dataContent);
akmhoqueba094742014-02-28 11:47:21 -060042 }
43 chkString="LSA";
44 if( nt.doesTokenExist(chkString) )
45 {
akmhoque5a44dd42014-03-12 18:11:32 -050046 string dataContent((char *)data.getContent().value());
47 processContentLsa(pnlsr, dataName, dataContent);
akmhoqueba094742014-02-28 11:47:21 -060048 }
akmhoque5a44dd42014-03-12 18:11:32 -050049 }
50 else
51 {
52 std::cout<<"Unverified Data Content. Discarded"<<std::endl;
53 }
akmhoqueba094742014-02-28 11:47:21 -060054 }
akmhoque5a44dd42014-03-12 18:11:32 -050055 }
akmhoqueba094742014-02-28 11:47:21 -060056
akmhoque5a44dd42014-03-12 18:11:32 -050057 void
58 DataManager::processContentInfo(Nlsr& pnlsr, string& dataName,
59 string& dataContent)
60 {
61 nlsrTokenizer nt(dataName,"/");
62 string chkString("info");
63 string neighbor=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
64 int oldStatus=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
65 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
66 //debugging purpose start
67 cout <<"Before Updates: " <<endl;
68 cout <<"Neighbor : "<<neighbor<<endl;
69 cout<<"Status: "<< oldStatus << endl;
70 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
71 //debugging purpose end
72 pnlsr.getAdl().setStatusOfNeighbor(neighbor,1);
73 pnlsr.getAdl().setTimedOutInterestCount(neighbor,0);
74 int newStatus=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
75 infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
76 //debugging purpose
77 cout <<"After Updates: " <<endl;
78 cout <<"Neighbor : "<<neighbor<<endl;
79 cout<<"Status: "<< newStatus << endl;
80 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
81 //debugging purpose end
82 if ( ( oldStatus-newStatus)!= 0 ) // change in Adjacency list
akmhoqueba094742014-02-28 11:47:21 -060083 {
akmhoque5a44dd42014-03-12 18:11:32 -050084 pnlsr.incrementAdjBuildCount();
85 /* Need to schedule event for Adjacency LSA building */
86 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
87 {
88 pnlsr.setIsBuildAdjLsaSheduled(1);
89 // event here
90 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
91 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
92 boost::ref(pnlsr)));
93 }
akmhoqueba094742014-02-28 11:47:21 -060094 }
akmhoque5a44dd42014-03-12 18:11:32 -050095 }
akmhoqueba094742014-02-28 11:47:21 -060096
akmhoque5a44dd42014-03-12 18:11:32 -050097 void
98 DataManager::processContentLsa(Nlsr& pnlsr, string& dataName,
99 string& dataContent)
100 {
101 nlsrTokenizer 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
akmhoqueba094742014-02-28 11:47:21 -0600109 {
akmhoque5a44dd42014-03-12 18:11:32 -0500110 interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeNoString);
akmhoqueba094742014-02-28 11:47:21 -0600111 }
akmhoque5a44dd42014-03-12 18:11:32 -0500112 catch(std::exception &e)
113 {
114 return;
115 }
116 if( lsTypeString == "1" ) //Name Lsa
117 {
118 processContentNameLsa(pnlsr, origRouter+"/"+lsTypeString,
119 interestedLsSeqNo, dataContent);
120 }
121 else if( lsTypeString == "2" ) //Adj Lsa
122 {
123 processContentAdjLsa(pnlsr, origRouter+"/"+lsTypeString,
124 interestedLsSeqNo, dataContent);
125 }
126 else if( lsTypeString == "3" ) //Cor Lsa
127 {
128 processContentCorLsa(pnlsr, origRouter+"/"+lsTypeString,
129 interestedLsSeqNo, dataContent);
130 }
131 else
132 {
133 cout<<"Unrecognized LSA Type :("<<endl;
134 }
135 }
akmhoqueba094742014-02-28 11:47:21 -0600136
akmhoque5a44dd42014-03-12 18:11:32 -0500137 void
138 DataManager::processContentNameLsa(Nlsr& pnlsr, string lsaKey,
139 uint32_t lsSeqNo, string& dataContent)
140 {
141 if ( pnlsr.getLsdb().isNameLsaNew(lsaKey,lsSeqNo))
akmhoqueba094742014-02-28 11:47:21 -0600142 {
akmhoque5a44dd42014-03-12 18:11:32 -0500143 NameLsa nameLsa;
144 if( nameLsa.initNameLsaFromContent(dataContent) )
145 {
146 pnlsr.getLsdb().installNameLsa(pnlsr, nameLsa);
147 }
148 else
149 {
150 cout<<"LSA data decoding error :("<<endl;
151 }
akmhoqueba094742014-02-28 11:47:21 -0600152 }
akmhoque5a44dd42014-03-12 18:11:32 -0500153 }
akmhoqueba094742014-02-28 11:47:21 -0600154
akmhoque5a44dd42014-03-12 18:11:32 -0500155 void
156 DataManager::processContentAdjLsa(Nlsr& pnlsr, string lsaKey,
157 uint32_t lsSeqNo, string& dataContent)
158 {
159 if ( pnlsr.getLsdb().isAdjLsaNew(lsaKey,lsSeqNo))
akmhoqueba094742014-02-28 11:47:21 -0600160 {
akmhoque5a44dd42014-03-12 18:11:32 -0500161 AdjLsa adjLsa;
162 if( adjLsa.initAdjLsaFromContent(dataContent) )
163 {
164 pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
165 }
166 else
167 {
168 cout<<"LSA data decoding error :("<<endl;
169 }
akmhoqueba094742014-02-28 11:47:21 -0600170 }
akmhoque5a44dd42014-03-12 18:11:32 -0500171 }
akmhoqueba094742014-02-28 11:47:21 -0600172
akmhoque5a44dd42014-03-12 18:11:32 -0500173 void
174 DataManager::processContentCorLsa(Nlsr& pnlsr, string lsaKey,
175 uint32_t lsSeqNo, string& dataContent)
176 {
177 if ( pnlsr.getLsdb().isCorLsaNew(lsaKey,lsSeqNo))
akmhoqueba094742014-02-28 11:47:21 -0600178 {
akmhoque5a44dd42014-03-12 18:11:32 -0500179 CorLsa corLsa;
180 if( corLsa.initCorLsaFromContent(dataContent) )
181 {
182 pnlsr.getLsdb().installCorLsa(pnlsr, corLsa);
183 }
184 else
185 {
186 cout<<"LSA data decoding error :("<<endl;
187 }
akmhoqueba094742014-02-28 11:47:21 -0600188 }
akmhoque5a44dd42014-03-12 18:11:32 -0500189 }
190
191 void
192 DataManager::processContentKeys(Nlsr& pnlsr, const ndn::Data& data)
193 {
akmhoquefa8ee9b2014-03-14 09:06:24 -0500194 std::cout<<" processContentKeys called "<<endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500195 ndn::shared_ptr<ndn::IdentityCertificate> cert=
196 ndn::make_shared<ndn::IdentityCertificate>();
197 cert->wireDecode(data.getContent().blockFromValue());
akmhoquefa8ee9b2014-03-14 09:06:24 -0500198 std::cout<<*(cert)<<endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500199 std::string dataName=data.getName().toUri();
200 nlsrTokenizer 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 cout<<"Cert Name: "<<certName<<" Seq Num: "<<seqNum<<std::endl;
akmhoquefa8ee9b2014-03-14 09:06:24 -0500205 if ( pnlsr.getKeyManager().verify(pnlsr, *(cert)))
akmhoqueba094742014-02-28 11:47:21 -0600206 {
akmhoque5a44dd42014-03-12 18:11:32 -0500207 pnlsr.getKeyManager().addCertificate(cert, seqNum, true);
akmhoqueba094742014-02-28 11:47:21 -0600208 }
akmhoquefa8ee9b2014-03-14 09:06:24 -0500209 else
210 {
211 pnlsr.getKeyManager().addCertificate(cert, seqNum, false);
212 }
213
214 pnlsr.getKeyManager().printCertStore();
akmhoque5a44dd42014-03-12 18:11:32 -0500215 }
akmhoqueba094742014-02-28 11:47:21 -0600216}//namespace nlsr