akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 1 | #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" |
akmhoque | eb764c5 | 2014-03-11 16:01:09 -0500 | [diff] [blame] | 12 | #include "security/nlsr_km.hpp" |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 13 | #include "utility/nlsr_logger.hpp" |
| 14 | |
| 15 | #define THIS_FILE "nlsr_dm.cpp" |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 16 | |
| 17 | namespace nlsr |
| 18 | { |
| 19 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 20 | using namespace std; |
| 21 | using namespace ndn; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 22 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 23 | void |
| 24 | DataManager::processContent(Nlsr& pnlsr, const ndn::Interest &interest, |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 25 | const ndn::Data & data, InterestManager& im) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 26 | { |
| 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) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 32 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 33 | 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"; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 41 | if( nt.doesTokenExist(chkString) ) |
| 42 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 43 | string dataContent((char *)data.getContent().value()); |
| 44 | processContentInfo(pnlsr,dataName,dataContent); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 45 | } |
| 46 | chkString="LSA"; |
| 47 | if( nt.doesTokenExist(chkString) ) |
| 48 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 49 | string dataContent((char *)data.getContent().value()); |
| 50 | processContentLsa(pnlsr, dataName, dataContent); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 51 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 52 | } |
| 53 | else |
| 54 | { |
| 55 | std::cout<<"Unverified Data Content. Discarded"<<std::endl; |
| 56 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 57 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 58 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 59 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 60 | 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 |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 86 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 87 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 97 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 98 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 99 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 100 | 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 |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 112 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 113 | interestedLsSeqNo=boost::lexical_cast<uint32_t>(lsSeNoString); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 114 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 115 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 139 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 140 | void |
| 141 | DataManager::processContentNameLsa(Nlsr& pnlsr, string lsaKey, |
| 142 | uint32_t lsSeqNo, string& dataContent) |
| 143 | { |
| 144 | if ( pnlsr.getLsdb().isNameLsaNew(lsaKey,lsSeqNo)) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 145 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 146 | NameLsa nameLsa; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 147 | if( nameLsa.initializeFromContent(dataContent) ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 148 | { |
| 149 | pnlsr.getLsdb().installNameLsa(pnlsr, nameLsa); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | cout<<"LSA data decoding error :("<<endl; |
| 154 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 155 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 156 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 157 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 158 | void |
| 159 | DataManager::processContentAdjLsa(Nlsr& pnlsr, string lsaKey, |
| 160 | uint32_t lsSeqNo, string& dataContent) |
| 161 | { |
| 162 | if ( pnlsr.getLsdb().isAdjLsaNew(lsaKey,lsSeqNo)) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 163 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 164 | AdjLsa adjLsa; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 165 | if( adjLsa.initializeFromContent(dataContent) ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 166 | { |
| 167 | pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | cout<<"LSA data decoding error :("<<endl; |
| 172 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 173 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 174 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 175 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 176 | void |
| 177 | DataManager::processContentCorLsa(Nlsr& pnlsr, string lsaKey, |
| 178 | uint32_t lsSeqNo, string& dataContent) |
| 179 | { |
| 180 | if ( pnlsr.getLsdb().isCorLsaNew(lsaKey,lsSeqNo)) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 181 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 182 | CorLsa corLsa; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 183 | if( corLsa.initializeFromContent(dataContent) ) |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 184 | { |
| 185 | pnlsr.getLsdb().installCorLsa(pnlsr, corLsa); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | cout<<"LSA data decoding error :("<<endl; |
| 190 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 191 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void |
| 195 | DataManager::processContentKeys(Nlsr& pnlsr, const ndn::Data& data) |
| 196 | { |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 197 | std::cout<<" processContentKeys called "<<endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 198 | ndn::shared_ptr<ndn::IdentityCertificate> cert= |
| 199 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 200 | cert->wireDecode(data.getContent().blockFromValue()); |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 201 | std::cout<<*(cert)<<endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 202 | 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; |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 208 | if ( pnlsr.getKeyManager().verify(pnlsr, *(cert))) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 209 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 210 | pnlsr.getKeyManager().addCertificate(cert, seqNum, true); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 211 | } |
akmhoque | fa8ee9b | 2014-03-14 09:06:24 -0500 | [diff] [blame] | 212 | else |
| 213 | { |
| 214 | pnlsr.getKeyManager().addCertificate(cert, seqNum, false); |
| 215 | } |
| 216 | |
| 217 | pnlsr.getKeyManager().printCertStore(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 218 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 219 | }//namespace nlsr |