blob: 16d0c729f29c6945ce9b8c64664b766b32caa944 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#include<iostream>
2#include<cstdlib>
3
4
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_im.hpp"
10#include "nlsr_dm.hpp"
11#include "utility/nlsr_tokenizer.hpp"
12#include "nlsr_lsdb.hpp"
13
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 interestManager::processInterest( Nlsr& pnlsr,
22 const ndn::Name &name,
23 const ndn::Interest &interest)
24 {
25 cout << "<< I: " << interest << endl;
26 string intName=interest.getName().toUri();
27 cout << "Interest Received for Name: "<< intName <<endl;
28 nlsrTokenizer nt(intName,"/");
29 string chkString("info");
30 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060031 {
akmhoque5a44dd42014-03-12 18:11:32 -050032 string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1);
33 cout <<"Neighbor: " << nbr <<endl;
34 processInterestInfo(pnlsr,nbr,interest);
akmhoqueba094742014-02-28 11:47:21 -060035 }
akmhoque5a44dd42014-03-12 18:11:32 -050036 chkString="LSA";
37 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060038 {
akmhoque5a44dd42014-03-12 18:11:32 -050039 processInterestLsa(pnlsr,interest);
akmhoqueba094742014-02-28 11:47:21 -060040 }
akmhoque5a44dd42014-03-12 18:11:32 -050041 chkString="keys";
42 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060043 {
akmhoque5a44dd42014-03-12 18:11:32 -050044 processInterestKeys(pnlsr,interest);
akmhoqueba094742014-02-28 11:47:21 -060045 }
akmhoque5a44dd42014-03-12 18:11:32 -050046 }
akmhoqueba094742014-02-28 11:47:21 -060047
akmhoque5a44dd42014-03-12 18:11:32 -050048 void
49 interestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
50 const ndn::Interest &interest)
51 {
52 if ( pnlsr.getAdl().isNeighbor(neighbor) )
akmhoqueba094742014-02-28 11:47:21 -060053 {
akmhoque5a44dd42014-03-12 18:11:32 -050054 Data data(ndn::Name(interest.getName()).appendVersion());
55 data.setFreshnessPeriod(1000); // 10 sec
56 data.setContent((const uint8_t*)"info", sizeof("info"));
57 pnlsr.getKeyManager().signData(data);
58 cout << ">> D: " << data << endl;
59 pnlsr.getNlsrFace()->put(data);
60 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
61 if ( status == 0 )
62 {
63 string intName=neighbor +"/"+"info"+
64 pnlsr.getConfParameter().getRouterPrefix();
65 expressInterest( pnlsr,intName,2,
66 pnlsr.getConfParameter().getInterestResendTime());
67 }
akmhoqueba094742014-02-28 11:47:21 -060068 }
akmhoque5a44dd42014-03-12 18:11:32 -050069 }
akmhoqueba094742014-02-28 11:47:21 -060070
akmhoque5a44dd42014-03-12 18:11:32 -050071 void
72 interestManager::processInterestLsa(Nlsr& pnlsr,const ndn::Interest &interest)
73 {
74 string intName=interest.getName().toUri();
75 nlsrTokenizer nt(intName,"/");
76 string chkString("LSA");
77 string origRouter=nt.getTokenString(nt.getTokenPosition(chkString)+1,
78 nt.getTokenNumber()-3);
79 string lsTypeString=nt.getToken(nt.getTokenNumber()-2);
80 string lsSeqString=nt.getToken(nt.getTokenNumber()-1);
81 cout<<"Router Name: "<<origRouter<<endl;
82 cout<<"Ls Type : "<<lsTypeString<<endl;
83 cout<<"Ls Seq : "<<lsSeqString<<endl;
84 uint8_t interestedLsType;
85 uint32_t interestedLsSeqNo;
86 try
akmhoqueba094742014-02-28 11:47:21 -060087 {
akmhoque5a44dd42014-03-12 18:11:32 -050088 interestedLsType=boost::lexical_cast<uint8_t>(lsTypeString);
89 interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeqString);
akmhoqueba094742014-02-28 11:47:21 -060090 }
akmhoque5a44dd42014-03-12 18:11:32 -050091 catch(std::exception &e)
akmhoqueba094742014-02-28 11:47:21 -060092 {
akmhoque5a44dd42014-03-12 18:11:32 -050093 return;
akmhoqueba094742014-02-28 11:47:21 -060094 }
akmhoque5a44dd42014-03-12 18:11:32 -050095 cout <<"Ls Type: "<<interestedLsType<<endl;
96 if( lsTypeString == "1" ) //Name Lsa
akmhoqueba094742014-02-28 11:47:21 -060097 {
akmhoque5a44dd42014-03-12 18:11:32 -050098 processInterestForNameLsa(pnlsr, interest,
99 origRouter+"/"+lsTypeString, interestedLsSeqNo);
100 }
101 else if( lsTypeString == "2" ) //Adj Lsa
102 {
103 processInterestForAdjLsa(pnlsr, interest,
104 origRouter+"/"+lsTypeString, interestedLsSeqNo);
105 }
106 else if( lsTypeString == "3" ) //Cor Lsa
107 {
108 processInterestForCorLsa(pnlsr, interest,
109 origRouter+"/"+lsTypeString, interestedLsSeqNo);
110 }
111 else
112 {
113 cout<<"Unrecognized LSA Type :("<<endl;
114 }
115 }
116
117 void
118 interestManager::processInterestForNameLsa(Nlsr& pnlsr,
119 const ndn::Interest &interest,
120 string lsaKey, uint32_t interestedlsSeqNo)
121 {
122 std::pair<NameLsa&, bool> nameLsa=pnlsr.getLsdb().getNameLsa(lsaKey);
123 if( nameLsa.second )
124 {
125 if ( nameLsa.first.getLsSeqNo() >= interestedlsSeqNo )
126 {
akmhoqueba094742014-02-28 11:47:21 -0600127 Data data(ndn::Name(interest.getName()).appendVersion());
128 data.setFreshnessPeriod(1000); // 10 sec
akmhoque5a44dd42014-03-12 18:11:32 -0500129 string content=nameLsa.first.getNameLsaData();
130 data.setContent((const uint8_t*)content.c_str(),content.size());
akmhoqueba094742014-02-28 11:47:21 -0600131 pnlsr.getKeyManager().signData(data);
akmhoque5a44dd42014-03-12 18:11:32 -0500132 cout << ">> D: " << data << endl;
akmhoqueba094742014-02-28 11:47:21 -0600133 pnlsr.getNlsrFace()->put(data);
akmhoque5a44dd42014-03-12 18:11:32 -0500134 }
akmhoqueba094742014-02-28 11:47:21 -0600135 }
akmhoque5a44dd42014-03-12 18:11:32 -0500136 }
akmhoqueba094742014-02-28 11:47:21 -0600137
akmhoque5a44dd42014-03-12 18:11:32 -0500138 void
139 interestManager::processInterestForAdjLsa(Nlsr& pnlsr,
140 const ndn::Interest &interest,
141 string lsaKey, uint32_t interestedlsSeqNo)
142 {
143 std::pair<AdjLsa&, bool> adjLsa=pnlsr.getLsdb().getAdjLsa(lsaKey);
144 if( adjLsa.second )
akmhoqueba094742014-02-28 11:47:21 -0600145 {
akmhoque5a44dd42014-03-12 18:11:32 -0500146 if ( adjLsa.first.getLsSeqNo() >= interestedlsSeqNo )
147 {
148 Data data(ndn::Name(interest.getName()).appendVersion());
149 data.setFreshnessPeriod(1000); // 10 sec
150 string content=adjLsa.first.getAdjLsaData();
151 data.setContent((const uint8_t*)content.c_str(),content.size());
152 pnlsr.getKeyManager().signData(data);
153 cout << ">> D: " << data << endl;
154 pnlsr.getNlsrFace()->put(data);
155 }
akmhoqueba094742014-02-28 11:47:21 -0600156 }
akmhoque5a44dd42014-03-12 18:11:32 -0500157 }
akmhoqueba094742014-02-28 11:47:21 -0600158
akmhoque5a44dd42014-03-12 18:11:32 -0500159 void
160 interestManager::processInterestForCorLsa(Nlsr& pnlsr,
161 const ndn::Interest &interest,
162 string lsaKey, uint32_t interestedlsSeqNo)
163 {
164 std::pair<CorLsa&, bool> corLsa=pnlsr.getLsdb().getCorLsa(lsaKey);
165 if( corLsa.second )
akmhoqueba094742014-02-28 11:47:21 -0600166 {
akmhoque5a44dd42014-03-12 18:11:32 -0500167 if ( corLsa.first.getLsSeqNo() >= interestedlsSeqNo )
168 {
169 Data data(ndn::Name(interest.getName()).appendVersion());
170 data.setFreshnessPeriod(1000); // 10 sec
171 string content=corLsa.first.getCorLsaData();
172 data.setContent((const uint8_t*)content.c_str(),content.size());
173 pnlsr.getKeyManager().signData(data);
174 cout << ">> D: " << data << endl;
175 pnlsr.getNlsrFace()->put(data);
176 }
akmhoqueba094742014-02-28 11:47:21 -0600177 }
akmhoque5a44dd42014-03-12 18:11:32 -0500178 }
akmhoqueba094742014-02-28 11:47:21 -0600179
akmhoque5a44dd42014-03-12 18:11:32 -0500180 void
181 interestManager::processInterestKeys(Nlsr& pnlsr,const ndn::Interest &interest)
182 {
183 cout<<"processInterestKeys called "<<endl;
184 string intName=interest.getName().toUri();
185 cout<<"Interest Name for Key: "<<intName<<std::endl;
186 nlsrTokenizer nt(intName,"/");
akmhoquefa8ee9b2014-03-14 09:06:24 -0500187 std::string chkString("ID-CERT");
188 std::string certName;
189 uint32_t seqNum;
190 ndn::Name dataName;
191 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert;
192 if( nt.getTokenPosition(chkString) == nt.getTokenNumber()-1 )
193 {
194 certName=nt.getTokenString(0,nt.getTokenNumber()-1);
195 cout<<"Cert Name: "<<certName<<std::endl;
196 chkCert=pnlsr.getKeyManager().getCertificateFromStore(certName);
197 }
198 else
199 {
200 certName=nt.getTokenString(0,nt.getTokenNumber()-2);
201 seqNum=boost::lexical_cast<uint32_t>(nt.getToken(nt.getTokenNumber()-1));
202 cout<<"Cert Name: "<<certName<<" Seq Num: "<<seqNum<<std::endl;
203 chkCert=pnlsr.getKeyManager().getCertificateFromStore(certName,seqNum);
204 }
akmhoque5a44dd42014-03-12 18:11:32 -0500205 if( chkCert.second )
akmhoqueba094742014-02-28 11:47:21 -0600206 {
akmhoquefa8ee9b2014-03-14 09:06:24 -0500207 if(nt.getTokenPosition(chkString) == nt.getTokenNumber()-1)
208 {
209 std::string dn;
210 dataName=ndn::Name(interest.getName()).appendVersion();
211 std::pair<uint32_t, bool> seqChk =
212 pnlsr.getKeyManager().getCertificateSeqNum(certName);
213 if( seqChk.second )
214 {
215 dn=dataName.toUri()+"/"+boost::lexical_cast<std::string>(seqChk.first);
216 dataName=ndn::Name(dn);
217 }
218 else
219 {
220 dn=dataName.toUri()+"/"+boost::lexical_cast<std::string>(10);
221 dataName=ndn::Name(dn);
222 }
223
224 }
225 else
226 {
227 dataName=ndn::Name(interest.getName());
228 }
229 Data data(dataName.appendVersion());
akmhoque5a44dd42014-03-12 18:11:32 -0500230 data.setFreshnessPeriod(1000); //10 sec
231 data.setContent(chkCert.first->wireEncode());
232 pnlsr.getKeyManager().signData(data);
233 pnlsr.getNlsrFace()->put(data);
akmhoqueba094742014-02-28 11:47:21 -0600234 }
akmhoque5a44dd42014-03-12 18:11:32 -0500235 }
akmhoqueba094742014-02-28 11:47:21 -0600236
akmhoque5a44dd42014-03-12 18:11:32 -0500237
238 void
239 interestManager::processInterestTimedOut(Nlsr& pnlsr,
240 const ndn::Interest &interest)
241 {
242 cout << "Timed out interest : " << interest.getName().toUri() << endl;
243 string intName= interest.getName().toUri();
244 nlsrTokenizer nt(intName,"/");
245 string chkString("info");
246 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -0600247 {
akmhoque5a44dd42014-03-12 18:11:32 -0500248 string nbr=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
249 processInterestTimedOutInfo( pnlsr , nbr , interest);
akmhoqueba094742014-02-28 11:47:21 -0600250 }
akmhoque5a44dd42014-03-12 18:11:32 -0500251 chkString="LSA";
252 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -0600253 {
akmhoque5a44dd42014-03-12 18:11:32 -0500254 processInterestTimedOutLsa(pnlsr, interest);
akmhoqueba094742014-02-28 11:47:21 -0600255 }
akmhoque5a44dd42014-03-12 18:11:32 -0500256 }
akmhoqueba094742014-02-28 11:47:21 -0600257
akmhoque5a44dd42014-03-12 18:11:32 -0500258 void
259 interestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
260 const ndn::Interest &interest)
261 {
262 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
263 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
264 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
265 cout<<"Neighbor: "<< neighbor << endl;
266 cout<<"Status: "<< status << endl;
267 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
268 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
akmhoqueba094742014-02-28 11:47:21 -0600269 {
akmhoque5a44dd42014-03-12 18:11:32 -0500270 string intName=neighbor +"/"+"info"+
271 pnlsr.getConfParameter().getRouterPrefix();
272 expressInterest( pnlsr,intName,2,
273 pnlsr.getConfParameter().getInterestResendTime());
akmhoqueba094742014-02-28 11:47:21 -0600274 }
akmhoque5a44dd42014-03-12 18:11:32 -0500275 else if ( (status == 1) &&
276 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
277 {
278 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
279 pnlsr.incrementAdjBuildCount();
280 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
281 {
282 pnlsr.setIsBuildAdjLsaSheduled(1);
283 // event here
284 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
285 ndn::bind(&Lsdb::scheduledAdjLsaBuild,&pnlsr.getLsdb(),
286 boost::ref(pnlsr)));
287 }
288 }
289 }
290
291 void
292 interestManager::processInterestTimedOutLsa(Nlsr& pnlsr,
293 const ndn::Interest &interest)
294 {
295 }
296
297 void
298 interestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
299 int scope, int seconds)
300 {
301 cout<<"Expressing Interest :"<<interestNamePrefix<<endl;
302 ndn::Interest i((ndn::Name(interestNamePrefix)));
303 //i.setScope(scope);
304 i.setInterestLifetime(seconds*1000);
305 i.setMustBeFresh(true);
306 pnlsr.getNlsrFace()->expressInterest(i,
307 ndn::func_lib::bind(&DataManager::processContent,
308 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2,boost::ref(*this)),
309 ndn::func_lib::bind(&interestManager::processInterestTimedOut,
310 this,boost::ref(pnlsr),_1));
311 }
312
313
314 void
315 interestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
316 {
317 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
318 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it)
319 {
320 string adjName=(*it).getAdjacentName()+"/"+"info"+
321 pnlsr.getConfParameter().getRouterPrefix();
322 expressInterest( pnlsr,adjName,2,
323 pnlsr.getConfParameter().getInterestResendTime());
324 }
325 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
326 }
327
328 void
329 interestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
330 {
331 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
332 ndn::bind(&interestManager::sendScheduledInfoInterest, this,
333 boost::ref(pnlsr),seconds));
334 }
akmhoqueba094742014-02-28 11:47:21 -0600335
336
337} //namespace nlsr