akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <cstdlib> |
| 3 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 4 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
| 5 | #include <ndn-cxx/security/identity-certificate.hpp> |
| 6 | #include <ndn-cxx/util/io.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 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 | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 63 | int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 64 | int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount( |
| 65 | neighbor); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | //debugging purpose start |
| 67 | std::cout << "Before Updates: " << std::endl; |
| 68 | std::cout << "Neighbor : " << neighbor << std::endl; |
| 69 | std::cout << "Status: " << oldStatus << std::endl; |
| 70 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
| 71 | //debugging purpose end |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame^] | 72 | m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1); |
| 73 | m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0); |
| 74 | int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor); |
| 75 | infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount( |
| 76 | neighbor); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | //debugging purpose |
| 78 | std::cout << "After Updates: " << std::endl; |
| 79 | std::cout << "Neighbor : " << neighbor << std::endl; |
| 80 | std::cout << "Status: " << newStatus << std::endl; |
| 81 | std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl; |
| 82 | //debugging purpose end |
| 83 | if ((oldStatus - newStatus) != 0) // change in Adjacency list |
| 84 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 85 | m_nlsr.incrementAdjBuildCount(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | /* Need to schedule event for Adjacency LSA building */ |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 87 | if (m_nlsr.getIsBuildAdjLsaSheduled() == 0) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 89 | m_nlsr.setIsBuildAdjLsaSheduled(1); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 90 | // event here |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 91 | m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5), |
| 92 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, m_nlsr.getLsdb(), |
| 93 | boost::ref(m_nlsr))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 99 | DataManager::processContentLsa(const string& dataName, string& dataContent) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 100 | { |
| 101 | Tokenizer 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 |
| 109 | { |
| 110 | interestedLsSeqNo = boost::lexical_cast<uint32_t>(lsSeNoString); |
| 111 | } |
| 112 | catch (std::exception& e) |
| 113 | { |
| 114 | return; |
| 115 | } |
| 116 | if (lsTypeString == "1") //Name Lsa |
| 117 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 118 | processContentNameLsa(origRouter + "/" + lsTypeString, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 119 | interestedLsSeqNo, dataContent); |
| 120 | } |
| 121 | else if (lsTypeString == "2") //Adj Lsa |
| 122 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 123 | processContentAdjLsa(origRouter + "/" + lsTypeString, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | interestedLsSeqNo, dataContent); |
| 125 | } |
| 126 | else if (lsTypeString == "3") //Cor Lsa |
| 127 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 128 | processContentCorLsa(origRouter + "/" + lsTypeString, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | interestedLsSeqNo, dataContent); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | cout << "Unrecognized LSA Type :(" << endl; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 138 | DataManager::processContentNameLsa(const string& lsaKey, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | uint32_t lsSeqNo, string& dataContent) |
| 140 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 141 | if (m_nlsr.getLsdb().isNameLsaNew(lsaKey, lsSeqNo)) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 142 | { |
| 143 | NameLsa nameLsa; |
| 144 | if (nameLsa.initializeFromContent(dataContent)) |
| 145 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 146 | m_nlsr.getLsdb().installNameLsa(m_nlsr, nameLsa); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 147 | } |
| 148 | else |
| 149 | { |
| 150 | std::cout << "LSA data decoding error :(" << std::endl; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 156 | DataManager::processContentAdjLsa(const string& lsaKey, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | uint32_t lsSeqNo, string& dataContent) |
| 158 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 159 | if (m_nlsr.getLsdb().isAdjLsaNew(lsaKey, lsSeqNo)) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 160 | { |
| 161 | AdjLsa adjLsa; |
| 162 | if (adjLsa.initializeFromContent(dataContent)) |
| 163 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 164 | m_nlsr.getLsdb().installAdjLsa(m_nlsr, adjLsa); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 165 | } |
| 166 | else |
| 167 | { |
| 168 | std::cout << "LSA data decoding error :(" << std::endl; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 174 | DataManager::processContentCorLsa(const string& lsaKey, |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | uint32_t lsSeqNo, string& dataContent) |
| 176 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 177 | if (m_nlsr.getLsdb().isCoordinateLsaNew(lsaKey, lsSeqNo)) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 178 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 179 | CoordinateLsa corLsa; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 180 | if (corLsa.initializeFromContent(dataContent)) |
| 181 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 182 | m_nlsr.getLsdb().installCoordinateLsa(m_nlsr, corLsa); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
| 186 | std::cout << "LSA data decoding error :(" << std::endl; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 192 | DataManager::processContentKeys(const ndn::Data& data) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 193 | { |
| 194 | std::cout << " processContentKeys called " << std::endl; |
| 195 | ndn::shared_ptr<ndn::IdentityCertificate> cert = |
| 196 | ndn::make_shared<ndn::IdentityCertificate>(); |
| 197 | cert->wireDecode(data.getContent().blockFromValue()); |
| 198 | std::cout << *(cert) << std::endl; |
| 199 | std::string dataName = data.getName().toUri(); |
| 200 | Tokenizer 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 | std::cout << "Cert Name: " << certName << " Seq Num: " << seqNum << std::endl; |
Yingdi Yu | 0c21782 | 2014-04-24 14:22:42 -0700 | [diff] [blame] | 205 | // if (m_nlsr.getKeyManager().verify(m_nlsr, *(cert))) |
| 206 | // { |
| 207 | // m_nlsr.getKeyManager().addCertificate(cert, seqNum, true); |
| 208 | // } |
| 209 | // else |
| 210 | // { |
| 211 | // m_nlsr.getKeyManager().addCertificate(cert, seqNum, false); |
| 212 | // } |
| 213 | // m_nlsr.getKeyManager().printCertStore(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 214 | } |
| 215 | }//namespace nlsr |