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