blob: d708812bb5a5df0d654966c42325b39a8221aa9c [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"
akmhoque05d5fcf2014-04-15 14:58:45 -050013#include "utility/nlsr_logger.hpp"
14
15#define THIS_FILE "nlsr_im.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
akmhoque05d5fcf2014-04-15 14:58:45 -050024 InterestManager::processInterest( Nlsr& pnlsr,
25 const ndn::Name& name,
26 const ndn::Interest& interest)
akmhoque5a44dd42014-03-12 18:11:32 -050027 {
28 cout << "<< I: " << interest << endl;
29 string intName=interest.getName().toUri();
30 cout << "Interest Received for Name: "<< intName <<endl;
31 nlsrTokenizer nt(intName,"/");
32 string chkString("info");
33 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060034 {
akmhoque5a44dd42014-03-12 18:11:32 -050035 string nbr=nt.getTokenString(nt.getTokenPosition(chkString)+1);
36 cout <<"Neighbor: " << nbr <<endl;
37 processInterestInfo(pnlsr,nbr,interest);
akmhoqueba094742014-02-28 11:47:21 -060038 }
akmhoque5a44dd42014-03-12 18:11:32 -050039 chkString="LSA";
40 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060041 {
akmhoque5a44dd42014-03-12 18:11:32 -050042 processInterestLsa(pnlsr,interest);
akmhoqueba094742014-02-28 11:47:21 -060043 }
akmhoque5a44dd42014-03-12 18:11:32 -050044 chkString="keys";
45 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -060046 {
akmhoque5a44dd42014-03-12 18:11:32 -050047 processInterestKeys(pnlsr,interest);
akmhoqueba094742014-02-28 11:47:21 -060048 }
akmhoque5a44dd42014-03-12 18:11:32 -050049 }
akmhoqueba094742014-02-28 11:47:21 -060050
akmhoque5a44dd42014-03-12 18:11:32 -050051 void
akmhoque05d5fcf2014-04-15 14:58:45 -050052 InterestManager::processInterestInfo(Nlsr& pnlsr, string& neighbor,
53 const ndn::Interest& interest)
akmhoque5a44dd42014-03-12 18:11:32 -050054 {
55 if ( pnlsr.getAdl().isNeighbor(neighbor) )
akmhoqueba094742014-02-28 11:47:21 -060056 {
akmhoque5a44dd42014-03-12 18:11:32 -050057 Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque05d5fcf2014-04-15 14:58:45 -050058 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
akmhoque5a44dd42014-03-12 18:11:32 -050059 data.setContent((const uint8_t*)"info", sizeof("info"));
60 pnlsr.getKeyManager().signData(data);
61 cout << ">> D: " << data << endl;
62 pnlsr.getNlsrFace()->put(data);
63 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
64 if ( status == 0 )
65 {
66 string intName=neighbor +"/"+"info"+
67 pnlsr.getConfParameter().getRouterPrefix();
68 expressInterest( pnlsr,intName,2,
69 pnlsr.getConfParameter().getInterestResendTime());
70 }
akmhoqueba094742014-02-28 11:47:21 -060071 }
akmhoque5a44dd42014-03-12 18:11:32 -050072 }
akmhoqueba094742014-02-28 11:47:21 -060073
akmhoque5a44dd42014-03-12 18:11:32 -050074 void
akmhoque05d5fcf2014-04-15 14:58:45 -050075 InterestManager::processInterestLsa(Nlsr& pnlsr,const ndn::Interest& interest)
akmhoque5a44dd42014-03-12 18:11:32 -050076 {
77 string intName=interest.getName().toUri();
78 nlsrTokenizer nt(intName,"/");
79 string chkString("LSA");
80 string origRouter=nt.getTokenString(nt.getTokenPosition(chkString)+1,
81 nt.getTokenNumber()-3);
82 string lsTypeString=nt.getToken(nt.getTokenNumber()-2);
83 string lsSeqString=nt.getToken(nt.getTokenNumber()-1);
84 cout<<"Router Name: "<<origRouter<<endl;
85 cout<<"Ls Type : "<<lsTypeString<<endl;
86 cout<<"Ls Seq : "<<lsSeqString<<endl;
87 uint8_t interestedLsType;
88 uint32_t interestedLsSeqNo;
89 try
akmhoqueba094742014-02-28 11:47:21 -060090 {
akmhoque5a44dd42014-03-12 18:11:32 -050091 interestedLsType=boost::lexical_cast<uint8_t>(lsTypeString);
92 interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeqString);
akmhoqueba094742014-02-28 11:47:21 -060093 }
akmhoque5a44dd42014-03-12 18:11:32 -050094 catch(std::exception &e)
akmhoqueba094742014-02-28 11:47:21 -060095 {
akmhoque5a44dd42014-03-12 18:11:32 -050096 return;
akmhoqueba094742014-02-28 11:47:21 -060097 }
akmhoque5a44dd42014-03-12 18:11:32 -050098 cout <<"Ls Type: "<<interestedLsType<<endl;
99 if( lsTypeString == "1" ) //Name Lsa
akmhoqueba094742014-02-28 11:47:21 -0600100 {
akmhoque5a44dd42014-03-12 18:11:32 -0500101 processInterestForNameLsa(pnlsr, interest,
102 origRouter+"/"+lsTypeString, interestedLsSeqNo);
103 }
104 else if( lsTypeString == "2" ) //Adj Lsa
105 {
106 processInterestForAdjLsa(pnlsr, interest,
107 origRouter+"/"+lsTypeString, interestedLsSeqNo);
108 }
109 else if( lsTypeString == "3" ) //Cor Lsa
110 {
111 processInterestForCorLsa(pnlsr, interest,
112 origRouter+"/"+lsTypeString, interestedLsSeqNo);
113 }
114 else
115 {
116 cout<<"Unrecognized LSA Type :("<<endl;
117 }
118 }
119
120 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500121 InterestManager::processInterestForNameLsa(Nlsr& pnlsr,
122 const ndn::Interest& interest,
akmhoque5a44dd42014-03-12 18:11:32 -0500123 string lsaKey, uint32_t interestedlsSeqNo)
124 {
125 std::pair<NameLsa&, bool> nameLsa=pnlsr.getLsdb().getNameLsa(lsaKey);
126 if( nameLsa.second )
127 {
128 if ( nameLsa.first.getLsSeqNo() >= interestedlsSeqNo )
129 {
akmhoqueba094742014-02-28 11:47:21 -0600130 Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque05d5fcf2014-04-15 14:58:45 -0500131 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
132 string content=nameLsa.first.getData();
akmhoque5a44dd42014-03-12 18:11:32 -0500133 data.setContent((const uint8_t*)content.c_str(),content.size());
akmhoqueba094742014-02-28 11:47:21 -0600134 pnlsr.getKeyManager().signData(data);
akmhoque5a44dd42014-03-12 18:11:32 -0500135 cout << ">> D: " << data << endl;
akmhoqueba094742014-02-28 11:47:21 -0600136 pnlsr.getNlsrFace()->put(data);
akmhoque5a44dd42014-03-12 18:11:32 -0500137 }
akmhoqueba094742014-02-28 11:47:21 -0600138 }
akmhoque5a44dd42014-03-12 18:11:32 -0500139 }
akmhoqueba094742014-02-28 11:47:21 -0600140
akmhoque5a44dd42014-03-12 18:11:32 -0500141 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500142 InterestManager::processInterestForAdjLsa(Nlsr& pnlsr,
143 const ndn::Interest& interest,
akmhoque5a44dd42014-03-12 18:11:32 -0500144 string lsaKey, uint32_t interestedlsSeqNo)
145 {
146 std::pair<AdjLsa&, bool> adjLsa=pnlsr.getLsdb().getAdjLsa(lsaKey);
147 if( adjLsa.second )
akmhoqueba094742014-02-28 11:47:21 -0600148 {
akmhoque5a44dd42014-03-12 18:11:32 -0500149 if ( adjLsa.first.getLsSeqNo() >= interestedlsSeqNo )
150 {
151 Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque05d5fcf2014-04-15 14:58:45 -0500152 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
153 string content=adjLsa.first.getData();
akmhoque5a44dd42014-03-12 18:11:32 -0500154 data.setContent((const uint8_t*)content.c_str(),content.size());
155 pnlsr.getKeyManager().signData(data);
156 cout << ">> D: " << data << endl;
157 pnlsr.getNlsrFace()->put(data);
158 }
akmhoqueba094742014-02-28 11:47:21 -0600159 }
akmhoque5a44dd42014-03-12 18:11:32 -0500160 }
akmhoqueba094742014-02-28 11:47:21 -0600161
akmhoque5a44dd42014-03-12 18:11:32 -0500162 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500163 InterestManager::processInterestForCorLsa(Nlsr& pnlsr,
164 const ndn::Interest& interest,
akmhoque5a44dd42014-03-12 18:11:32 -0500165 string lsaKey, uint32_t interestedlsSeqNo)
166 {
167 std::pair<CorLsa&, bool> corLsa=pnlsr.getLsdb().getCorLsa(lsaKey);
168 if( corLsa.second )
akmhoqueba094742014-02-28 11:47:21 -0600169 {
akmhoque5a44dd42014-03-12 18:11:32 -0500170 if ( corLsa.first.getLsSeqNo() >= interestedlsSeqNo )
171 {
172 Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque05d5fcf2014-04-15 14:58:45 -0500173 data.setFreshnessPeriod(time::seconds(10)); // 10 sec
174 string content=corLsa.first.getData();
akmhoque5a44dd42014-03-12 18:11:32 -0500175 data.setContent((const uint8_t*)content.c_str(),content.size());
176 pnlsr.getKeyManager().signData(data);
177 cout << ">> D: " << data << endl;
178 pnlsr.getNlsrFace()->put(data);
179 }
akmhoqueba094742014-02-28 11:47:21 -0600180 }
akmhoque5a44dd42014-03-12 18:11:32 -0500181 }
akmhoqueba094742014-02-28 11:47:21 -0600182
akmhoque5a44dd42014-03-12 18:11:32 -0500183 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500184 InterestManager::processInterestKeys(Nlsr& pnlsr,const ndn::Interest& interest)
akmhoque5a44dd42014-03-12 18:11:32 -0500185 {
186 cout<<"processInterestKeys called "<<endl;
187 string intName=interest.getName().toUri();
188 cout<<"Interest Name for Key: "<<intName<<std::endl;
189 nlsrTokenizer nt(intName,"/");
akmhoquefa8ee9b2014-03-14 09:06:24 -0500190 std::string chkString("ID-CERT");
191 std::string certName;
192 uint32_t seqNum;
193 ndn::Name dataName;
194 std::pair<ndn::shared_ptr<ndn::IdentityCertificate>, bool> chkCert;
195 if( nt.getTokenPosition(chkString) == nt.getTokenNumber()-1 )
196 {
197 certName=nt.getTokenString(0,nt.getTokenNumber()-1);
198 cout<<"Cert Name: "<<certName<<std::endl;
199 chkCert=pnlsr.getKeyManager().getCertificateFromStore(certName);
200 }
201 else
202 {
203 certName=nt.getTokenString(0,nt.getTokenNumber()-2);
204 seqNum=boost::lexical_cast<uint32_t>(nt.getToken(nt.getTokenNumber()-1));
205 cout<<"Cert Name: "<<certName<<" Seq Num: "<<seqNum<<std::endl;
206 chkCert=pnlsr.getKeyManager().getCertificateFromStore(certName,seqNum);
207 }
akmhoque5a44dd42014-03-12 18:11:32 -0500208 if( chkCert.second )
akmhoqueba094742014-02-28 11:47:21 -0600209 {
akmhoquefa8ee9b2014-03-14 09:06:24 -0500210 if(nt.getTokenPosition(chkString) == nt.getTokenNumber()-1)
211 {
212 std::string dn;
213 dataName=ndn::Name(interest.getName()).appendVersion();
214 std::pair<uint32_t, bool> seqChk =
215 pnlsr.getKeyManager().getCertificateSeqNum(certName);
216 if( seqChk.second )
217 {
218 dn=dataName.toUri()+"/"+boost::lexical_cast<std::string>(seqChk.first);
219 dataName=ndn::Name(dn);
220 }
221 else
222 {
223 dn=dataName.toUri()+"/"+boost::lexical_cast<std::string>(10);
224 dataName=ndn::Name(dn);
225 }
226
227 }
228 else
229 {
230 dataName=ndn::Name(interest.getName());
231 }
232 Data data(dataName.appendVersion());
akmhoque05d5fcf2014-04-15 14:58:45 -0500233 data.setFreshnessPeriod(time::seconds(10)); //10 sec
akmhoque5a44dd42014-03-12 18:11:32 -0500234 data.setContent(chkCert.first->wireEncode());
235 pnlsr.getKeyManager().signData(data);
236 pnlsr.getNlsrFace()->put(data);
akmhoqueba094742014-02-28 11:47:21 -0600237 }
akmhoque5a44dd42014-03-12 18:11:32 -0500238 }
akmhoqueba094742014-02-28 11:47:21 -0600239
akmhoque5a44dd42014-03-12 18:11:32 -0500240
241 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500242 InterestManager::processInterestTimedOut(Nlsr& pnlsr,
243 const ndn::Interest& interest)
akmhoque5a44dd42014-03-12 18:11:32 -0500244 {
245 cout << "Timed out interest : " << interest.getName().toUri() << endl;
246 string intName= interest.getName().toUri();
247 nlsrTokenizer nt(intName,"/");
248 string chkString("info");
249 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -0600250 {
akmhoque5a44dd42014-03-12 18:11:32 -0500251 string nbr=nt.getTokenString(0,nt.getTokenPosition(chkString)-1);
252 processInterestTimedOutInfo( pnlsr , nbr , interest);
akmhoqueba094742014-02-28 11:47:21 -0600253 }
akmhoque5a44dd42014-03-12 18:11:32 -0500254 chkString="LSA";
255 if( nt.doesTokenExist(chkString) )
akmhoqueba094742014-02-28 11:47:21 -0600256 {
akmhoque5a44dd42014-03-12 18:11:32 -0500257 processInterestTimedOutLsa(pnlsr, interest);
akmhoqueba094742014-02-28 11:47:21 -0600258 }
akmhoque5a44dd42014-03-12 18:11:32 -0500259 }
akmhoqueba094742014-02-28 11:47:21 -0600260
akmhoque5a44dd42014-03-12 18:11:32 -0500261 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500262 InterestManager::processInterestTimedOutInfo(Nlsr& pnlsr, string& neighbor,
263 const ndn::Interest& interest)
akmhoque5a44dd42014-03-12 18:11:32 -0500264 {
265 pnlsr.getAdl().incrementTimedOutInterestCount(neighbor);
266 int status=pnlsr.getAdl().getStatusOfNeighbor(neighbor);
267 int infoIntTimedOutCount=pnlsr.getAdl().getTimedOutInterestCount(neighbor);
268 cout<<"Neighbor: "<< neighbor << endl;
269 cout<<"Status: "<< status << endl;
270 cout<<"Info Interest Timed out: "<< infoIntTimedOutCount <<endl;
271 if((infoIntTimedOutCount < pnlsr.getConfParameter().getInterestRetryNumber()))
akmhoqueba094742014-02-28 11:47:21 -0600272 {
akmhoque5a44dd42014-03-12 18:11:32 -0500273 string intName=neighbor +"/"+"info"+
274 pnlsr.getConfParameter().getRouterPrefix();
275 expressInterest( pnlsr,intName,2,
276 pnlsr.getConfParameter().getInterestResendTime());
akmhoqueba094742014-02-28 11:47:21 -0600277 }
akmhoque5a44dd42014-03-12 18:11:32 -0500278 else if ( (status == 1) &&
279 (infoIntTimedOutCount == pnlsr.getConfParameter().getInterestRetryNumber()))
280 {
281 pnlsr.getAdl().setStatusOfNeighbor(neighbor,0);
282 pnlsr.incrementAdjBuildCount();
283 if ( pnlsr.getIsBuildAdjLsaSheduled() == 0 )
284 {
285 pnlsr.setIsBuildAdjLsaSheduled(1);
286 // event here
287 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
288 ndn::bind(&Lsdb::scheduledAdjLsaBuild,&pnlsr.getLsdb(),
289 boost::ref(pnlsr)));
290 }
291 }
292 }
293
294 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500295 InterestManager::processInterestTimedOutLsa(Nlsr& pnlsr,
296 const ndn::Interest& interest)
akmhoque5a44dd42014-03-12 18:11:32 -0500297 {
298 }
299
300 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500301 InterestManager::expressInterest(Nlsr& pnlsr,const string& interestNamePrefix,
akmhoque5a44dd42014-03-12 18:11:32 -0500302 int scope, int seconds)
303 {
304 cout<<"Expressing Interest :"<<interestNamePrefix<<endl;
305 ndn::Interest i((ndn::Name(interestNamePrefix)));
306 //i.setScope(scope);
akmhoque05d5fcf2014-04-15 14:58:45 -0500307 i.setInterestLifetime(time::seconds(seconds));
akmhoque5a44dd42014-03-12 18:11:32 -0500308 i.setMustBeFresh(true);
309 pnlsr.getNlsrFace()->expressInterest(i,
310 ndn::func_lib::bind(&DataManager::processContent,
311 &pnlsr.getDm(), boost::ref(pnlsr),_1, _2,boost::ref(*this)),
akmhoque05d5fcf2014-04-15 14:58:45 -0500312 ndn::func_lib::bind(&InterestManager::processInterestTimedOut,
akmhoque5a44dd42014-03-12 18:11:32 -0500313 this,boost::ref(pnlsr),_1));
314 }
315
316
317 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500318 InterestManager::sendScheduledInfoInterest(Nlsr& pnlsr, int seconds)
akmhoque5a44dd42014-03-12 18:11:32 -0500319 {
320 std::list<Adjacent> adjList=pnlsr.getAdl().getAdjList();
321 for(std::list<Adjacent>::iterator it=adjList.begin(); it!=adjList.end(); ++it)
322 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500323 string adjName=(*it).getName()+"/"+"info"+
akmhoque5a44dd42014-03-12 18:11:32 -0500324 pnlsr.getConfParameter().getRouterPrefix();
325 expressInterest( pnlsr,adjName,2,
326 pnlsr.getConfParameter().getInterestResendTime());
327 }
328 scheduleInfoInterest(pnlsr, pnlsr.getConfParameter().getInfoInterestInterval());
329 }
330
331 void
akmhoque05d5fcf2014-04-15 14:58:45 -0500332 InterestManager::scheduleInfoInterest(Nlsr& pnlsr, int seconds)
akmhoque5a44dd42014-03-12 18:11:32 -0500333 {
334 EventId eid=pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
akmhoque05d5fcf2014-04-15 14:58:45 -0500335 ndn::bind(&InterestManager::sendScheduledInfoInterest, this,
akmhoque5a44dd42014-03-12 18:11:32 -0500336 boost::ref(pnlsr),seconds));
337 }
akmhoqueba094742014-02-28 11:47:21 -0600338
339
340} //namespace nlsr