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