blob: 249b5e7c563c3e9f194bbd2c965ba59d5d26286b [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"
akmhoque05d5fcf2014-04-15 14:58:45 -050013#include "utility/nlsr_logger.hpp"
14
15#define THIS_FILE "nlsr_dm.cpp"
akmhoqueba094742014-02-28 11:47:21 -060016
17namespace nlsr
18{
19
akmhoque5a44dd42014-03-12 18:11:32 -050020 using namespace std;
21 using namespace ndn;
akmhoqueba094742014-02-28 11:47:21 -060022
akmhoque5a44dd42014-03-12 18:11:32 -050023 void
24 DataManager::processContent(Nlsr& pnlsr, const ndn::Interest &interest,
akmhoque05d5fcf2014-04-15 14:58:45 -050025 const ndn::Data & data, InterestManager& im)
akmhoque5a44dd42014-03-12 18:11:32 -050026 {
27 cout << "I: " << interest.toUri() << endl;
28 string dataName(data.getName().toUri());
29 nlsrTokenizer nt(dataName,"/");
30 std::string chkString("keys");
31 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060032 {
akmhoque5a44dd42014-03-12 18:11:32 -050033 processContentKeys(pnlsr, data);
34 }
35 else
36 {
37 if ( pnlsr.getKeyManager().verify(data))
38 {
39 std::cout<<"Verified Data Content"<<std::endl;
40 chkString="info";
akmhoqueba094742014-02-28 11:47:21 -060041 if( nt.doesTokenExist(chkString) )
42 {
akmhoque5a44dd42014-03-12 18:11:32 -050043 string dataContent((char *)data.getContent().value());
44 processContentInfo(pnlsr,dataName,dataContent);
akmhoqueba094742014-02-28 11:47:21 -060045 }
46 chkString="LSA";
47 if( nt.doesTokenExist(chkString) )
48 {
akmhoque5a44dd42014-03-12 18:11:32 -050049 string dataContent((char *)data.getContent().value());
50 processContentLsa(pnlsr, dataName, dataContent);
akmhoqueba094742014-02-28 11:47:21 -060051 }
akmhoque5a44dd42014-03-12 18:11:32 -050052 }
53 else
54 {
55 std::cout<<"Unverified Data Content. Discarded"<<std::endl;
56 }
akmhoqueba094742014-02-28 11:47:21 -060057 }
akmhoque5a44dd42014-03-12 18:11:32 -050058 }
akmhoqueba094742014-02-28 11:47:21 -060059
akmhoque5a44dd42014-03-12 18:11:32 -050060 void
61 DataManager::processContentInfo(Nlsr& pnlsr, string& dataName,
62 string& dataContent)
63 {
64 nlsrTokenizer nt(dataName,"/");
65 string chkString("info");
66 string neighbor=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
67 int oldStatus=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
68 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
69 //debugging purpose start
70 cout <<"Before Updates: " <<endl;
71 cout <<"Neighbor : "<<neighbor<<endl;
72 cout<<"Status: "<< oldStatus << endl;
73 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
74 //debugging purpose end
75 pnlsr.getAdl().setStatusOfNeighbor(neighbor,1);
76 pnlsr.getAdl().setTimedOutInterestCount(neighbor,0);
77 int newStatus=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
78 infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
79 //debugging purpose
80 cout <<"After Updates: " <<endl;
81 cout <<"Neighbor : "<<neighbor<<endl;
82 cout<<"Status: "<< newStatus << endl;
83 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
84 //debugging purpose end
85 if ( ( oldStatus-newStatus)!= 0 ) // change in Adjacency list
akmhoqueba094742014-02-28 11:47:21 -060086 {
akmhoque5a44dd42014-03-12 18:11:32 -050087 pnlsr.incrementAdjBuildCount();
88 /* Need to schedule event for Adjacency LSA building */
89 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
90 {
91 pnlsr.setIsBuildAdjLsaSheduled(1);
92 // event here
93 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
94 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
95 boost::ref(pnlsr)));
96 }
akmhoqueba094742014-02-28 11:47:21 -060097 }
akmhoque5a44dd42014-03-12 18:11:32 -050098 }
akmhoqueba094742014-02-28 11:47:21 -060099
akmhoque5a44dd42014-03-12 18:11:32 -0500100 void
101 DataManager::processContentLsa(Nlsr& pnlsr, string& dataName,
102 string& dataContent)
103 {
104 nlsrTokenizer nt(dataName,"/");
105 string chkString("LSA");
106 string origRouter=nt.getTokenString(nt.getTokenPosition(chkString)+1,
107 nt.getTokenNumber()-4);
108 string lsTypeString=nt.getToken(nt.getTokenNumber()-3);
109 string lsSeNoString=nt.getToken(nt.getTokenNumber()-2);
110 uint32_t interestedLsSeqNo;
111 try
akmhoqueba094742014-02-28 11:47:21 -0600112 {
akmhoque5a44dd42014-03-12 18:11:32 -0500113 interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeNoString);
akmhoqueba094742014-02-28 11:47:21 -0600114 }
akmhoque5a44dd42014-03-12 18:11:32 -0500115 catch(std::exception &e)
116 {
117 return;
118 }
119 if( lsTypeString == "1" ) //Name Lsa
120 {
121 processContentNameLsa(pnlsr, origRouter+"/"+lsTypeString,
122 interestedLsSeqNo, dataContent);
123 }
124 else if( lsTypeString == "2" ) //Adj Lsa
125 {
126 processContentAdjLsa(pnlsr, origRouter+"/"+lsTypeString,
127 interestedLsSeqNo, dataContent);
128 }
129 else if( lsTypeString == "3" ) //Cor Lsa
130 {
131 processContentCorLsa(pnlsr, origRouter+"/"+lsTypeString,
132 interestedLsSeqNo, dataContent);
133 }
134 else
135 {
136 cout<<"Unrecognized LSA Type :("<<endl;
137 }
138 }
akmhoqueba094742014-02-28 11:47:21 -0600139
akmhoque5a44dd42014-03-12 18:11:32 -0500140 void
141 DataManager::processContentNameLsa(Nlsr& pnlsr, string lsaKey,
142 uint32_t lsSeqNo, string& dataContent)
143 {
144 if ( pnlsr.getLsdb().isNameLsaNew(lsaKey,lsSeqNo))
akmhoqueba094742014-02-28 11:47:21 -0600145 {
akmhoque5a44dd42014-03-12 18:11:32 -0500146 NameLsa nameLsa;
akmhoque05d5fcf2014-04-15 14:58:45 -0500147 if( nameLsa.initializeFromContent(dataContent) )
akmhoque5a44dd42014-03-12 18:11:32 -0500148 {
149 pnlsr.getLsdb().installNameLsa(pnlsr, nameLsa);
150 }
151 else
152 {
153 cout<<"LSA data decoding error :("<<endl;
154 }
akmhoqueba094742014-02-28 11:47:21 -0600155 }
akmhoque5a44dd42014-03-12 18:11:32 -0500156 }
akmhoqueba094742014-02-28 11:47:21 -0600157
akmhoque5a44dd42014-03-12 18:11:32 -0500158 void
159 DataManager::processContentAdjLsa(Nlsr& pnlsr, string lsaKey,
160 uint32_t lsSeqNo, string& dataContent)
161 {
162 if ( pnlsr.getLsdb().isAdjLsaNew(lsaKey,lsSeqNo))
akmhoqueba094742014-02-28 11:47:21 -0600163 {
akmhoque5a44dd42014-03-12 18:11:32 -0500164 AdjLsa adjLsa;
akmhoque05d5fcf2014-04-15 14:58:45 -0500165 if( adjLsa.initializeFromContent(dataContent) )
akmhoque5a44dd42014-03-12 18:11:32 -0500166 {
167 pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
168 }
169 else
170 {
171 cout<<"LSA data decoding error :("<<endl;
172 }
akmhoqueba094742014-02-28 11:47:21 -0600173 }
akmhoque5a44dd42014-03-12 18:11:32 -0500174 }
akmhoqueba094742014-02-28 11:47:21 -0600175
akmhoque5a44dd42014-03-12 18:11:32 -0500176 void
177 DataManager::processContentCorLsa(Nlsr& pnlsr, string lsaKey,
178 uint32_t lsSeqNo, string& dataContent)
179 {
180 if ( pnlsr.getLsdb().isCorLsaNew(lsaKey,lsSeqNo))
akmhoqueba094742014-02-28 11:47:21 -0600181 {
akmhoque5a44dd42014-03-12 18:11:32 -0500182 CorLsa corLsa;
akmhoque05d5fcf2014-04-15 14:58:45 -0500183 if( corLsa.initializeFromContent(dataContent) )
akmhoque5a44dd42014-03-12 18:11:32 -0500184 {
185 pnlsr.getLsdb().installCorLsa(pnlsr, corLsa);
186 }
187 else
188 {
189 cout<<"LSA data decoding error :("<<endl;
190 }
akmhoqueba094742014-02-28 11:47:21 -0600191 }
akmhoque5a44dd42014-03-12 18:11:32 -0500192 }
193
194 void
195 DataManager::processContentKeys(Nlsr& pnlsr, const ndn::Data& data)
196 {
akmhoquefa8ee9b2014-03-14 09:06:24 -0500197 std::cout<<" processContentKeys called "<<endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500198 ndn::shared_ptr<ndn::IdentityCertificate> cert=
199 ndn::make_shared<ndn::IdentityCertificate>();
200 cert->wireDecode(data.getContent().blockFromValue());
akmhoquefa8ee9b2014-03-14 09:06:24 -0500201 std::cout<<*(cert)<<endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500202 std::string dataName=data.getName().toUri();
203 nlsrTokenizer nt(dataName,"/");
204 std::string certName=nt.getTokenString(0,nt.getTokenNumber()-3);
205 uint32_t seqNum=boost::lexical_cast<uint32_t>(nt.getToken(
206 nt.getTokenNumber()-2));
207 cout<<"Cert Name: "<<certName<<" Seq Num: "<<seqNum<<std::endl;
akmhoquefa8ee9b2014-03-14 09:06:24 -0500208 if ( pnlsr.getKeyManager().verify(pnlsr, *(cert)))
akmhoqueba094742014-02-28 11:47:21 -0600209 {
akmhoque5a44dd42014-03-12 18:11:32 -0500210 pnlsr.getKeyManager().addCertificate(cert, seqNum, true);
akmhoqueba094742014-02-28 11:47:21 -0600211 }
akmhoquefa8ee9b2014-03-14 09:06:24 -0500212 else
213 {
214 pnlsr.getKeyManager().addCertificate(cert, seqNum, false);
215 }
216
217 pnlsr.getKeyManager().printCertStore();
akmhoque5a44dd42014-03-12 18:11:32 -0500218 }
akmhoqueba094742014-02-28 11:47:21 -0600219}//namespace nlsr