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