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