Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 2 | /* |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "index.hpp" |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 21 | |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 22 | #include <ndn-cxx/util/sha256.hpp> |
| 23 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 24 | |
| 25 | namespace repo { |
| 26 | |
| 27 | /** @brief determines if entry can satisfy interest |
| 28 | * @param hash SHA256 hash of PublisherPublicKeyLocator if exists in interest, otherwise ignored |
| 29 | */ |
| 30 | static bool |
| 31 | matchesSimpleSelectors(const Interest& interest, ndn::ConstBufferPtr& hash, |
| 32 | const Index::Entry& entry) |
| 33 | { |
| 34 | const Name& fullName = entry.getName(); |
| 35 | |
| 36 | if (!interest.getName().isPrefixOf(fullName)) |
| 37 | return false; |
| 38 | |
| 39 | size_t nSuffixComponents = fullName.size() - interest.getName().size(); |
| 40 | if (interest.getMinSuffixComponents() >= 0 && |
| 41 | nSuffixComponents < static_cast<size_t>(interest.getMinSuffixComponents())) |
| 42 | return false; |
| 43 | if (interest.getMaxSuffixComponents() >= 0 && |
| 44 | nSuffixComponents > static_cast<size_t>(interest.getMaxSuffixComponents())) |
| 45 | return false; |
| 46 | |
| 47 | if (!interest.getExclude().empty() && |
| 48 | entry.getName().size() > interest.getName().size() && |
| 49 | interest.getExclude().isExcluded(entry.getName()[interest.getName().size()])) |
| 50 | return false; |
| 51 | if (!interest.getPublisherPublicKeyLocator().empty()) |
| 52 | { |
| 53 | if (*entry.getKeyLocatorHash() != *hash) |
| 54 | return false; |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 59 | Index::Index(size_t nMaxPackets) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 60 | : m_maxPackets(nMaxPackets) |
| 61 | , m_size(0) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | |
| 66 | bool |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 67 | Index::insert(const Data& data, int64_t id) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 68 | { |
| 69 | if (isFull()) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 70 | BOOST_THROW_EXCEPTION(Error("The Index is Full. Cannot Insert Any Data!")); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 71 | Entry entry(data, id); |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 72 | bool isInserted = m_indexContainer.insert(entry).second; |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 73 | if (isInserted) |
| 74 | ++m_size; |
| 75 | return isInserted; |
| 76 | } |
| 77 | |
| 78 | bool |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 79 | Index::insert(const Name& fullName, int64_t id, |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 80 | const ndn::ConstBufferPtr& keyLocatorHash) |
| 81 | { |
| 82 | if (isFull()) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 83 | BOOST_THROW_EXCEPTION(Error("The Index is Full. Cannot Insert Any Data!")); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 84 | Entry entry(fullName, keyLocatorHash, id); |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 85 | bool isInserted = m_indexContainer.insert(entry).second; |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 86 | if (isInserted) |
| 87 | ++m_size; |
| 88 | return isInserted; |
| 89 | } |
| 90 | |
| 91 | std::pair<int64_t,Name> |
| 92 | Index::find(const Interest& interest) const |
| 93 | { |
| 94 | Name name = interest.getName(); |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 95 | IndexContainer::const_iterator result = m_indexContainer.lower_bound(name); |
| 96 | if (result != m_indexContainer.end()) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 97 | { |
| 98 | return selectChild(interest, result); |
| 99 | } |
| 100 | else |
| 101 | { |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 102 | return std::make_pair(0, Name()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | std::pair<int64_t,Name> |
| 107 | Index::find(const Name& name) const |
| 108 | { |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 109 | IndexContainer::const_iterator result = m_indexContainer.lower_bound(name); |
| 110 | if (result != m_indexContainer.end()) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 111 | { |
| 112 | return findFirstEntry(name, result); |
| 113 | } |
| 114 | else |
| 115 | { |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 116 | return std::make_pair(0, Name()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | bool |
| 121 | Index::hasData(const Data& data) const |
| 122 | { |
| 123 | Index::Entry entry(data, -1); // the id number is useless |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 124 | IndexContainer::const_iterator result = m_indexContainer.find(entry); |
| 125 | return result != m_indexContainer.end(); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 126 | |
| 127 | } |
| 128 | |
| 129 | std::pair<int64_t,Name> |
| 130 | Index::findFirstEntry(const Name& prefix, |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 131 | IndexContainer::const_iterator startingPoint) const |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 132 | { |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 133 | BOOST_ASSERT(startingPoint != m_indexContainer.end()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 134 | if (prefix.isPrefixOf(startingPoint->getName())) |
| 135 | { |
| 136 | return std::make_pair(startingPoint->getId(), startingPoint->getName()); |
| 137 | } |
| 138 | else |
| 139 | { |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 140 | return std::make_pair(0, Name()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | bool |
| 145 | Index::erase(const Name& fullName) |
| 146 | { |
| 147 | Entry entry(fullName); |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 148 | IndexContainer::const_iterator findIterator = m_indexContainer.find(entry); |
| 149 | if (findIterator != m_indexContainer.end()) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 150 | { |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 151 | m_indexContainer.erase(findIterator); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 152 | m_size--; |
| 153 | return true; |
| 154 | } |
| 155 | else |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | const ndn::ConstBufferPtr |
| 160 | Index::computeKeyLocatorHash(const KeyLocator& keyLocator) |
| 161 | { |
| 162 | const Block& block = keyLocator.wireEncode(); |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 163 | ndn::ConstBufferPtr keyLocatorHash = ndn::util::Sha256::computeDigest(block.wire(), block.size()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 164 | return keyLocatorHash; |
| 165 | } |
| 166 | |
| 167 | std::pair<int64_t,Name> |
| 168 | Index::selectChild(const Interest& interest, |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 169 | IndexContainer::const_iterator startingPoint) const |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 170 | { |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 171 | BOOST_ASSERT(startingPoint != m_indexContainer.end()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 172 | bool isLeftmost = (interest.getChildSelector() <= 0); |
| 173 | ndn::ConstBufferPtr hash; |
| 174 | if (!interest.getPublisherPublicKeyLocator().empty()) |
| 175 | { |
| 176 | KeyLocator keyLocator = interest.getPublisherPublicKeyLocator(); |
| 177 | const Block& block = keyLocator.wireEncode(); |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame^] | 178 | hash = ndn::util::Sha256::computeDigest(block.wire(), block.size()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (isLeftmost) |
| 182 | { |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 183 | for (IndexContainer::const_iterator it = startingPoint; |
| 184 | it != m_indexContainer.end(); ++it) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 185 | { |
| 186 | if (!interest.getName().isPrefixOf(it->getName())) |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 187 | return std::make_pair(0, Name()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 188 | if (matchesSimpleSelectors(interest, hash, (*it))) |
| 189 | return std::make_pair(it->getId(), it->getName()); |
| 190 | } |
| 191 | } |
| 192 | else |
| 193 | { |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 194 | IndexContainer::const_iterator boundary = m_indexContainer.lower_bound(interest.getName()); |
| 195 | if (boundary == m_indexContainer.end() || !interest.getName().isPrefixOf(boundary->getName())) |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 196 | return std::make_pair(0, Name()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 197 | Name successor = interest.getName().getSuccessor(); |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 198 | IndexContainer::const_iterator last = interest.getName().size() == 0 ? |
| 199 | m_indexContainer.end() : m_indexContainer.lower_bound(interest.getName().getSuccessor()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 200 | while (true) |
| 201 | { |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 202 | IndexContainer::const_iterator prev = last; |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 203 | --prev; |
| 204 | if (prev == boundary) |
| 205 | { |
| 206 | bool isMatch = matchesSimpleSelectors(interest, hash, (*prev)); |
| 207 | if (isMatch) |
| 208 | { |
| 209 | return std::make_pair(prev->getId(), prev->getName()); |
| 210 | } |
| 211 | else |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 212 | return std::make_pair(0, Name()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 213 | } |
WeiqiShi | a79c778 | 2014-12-26 09:42:10 +0800 | [diff] [blame] | 214 | IndexContainer::const_iterator first = |
| 215 | m_indexContainer.lower_bound(prev->getName().getPrefix(interest.getName().size() + 1)); |
| 216 | IndexContainer::const_iterator match = |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 217 | std::find_if(first, last, bind(&matchesSimpleSelectors, interest, hash, _1)); |
| 218 | if (match != last) |
| 219 | { |
| 220 | return std::make_pair(match->getId(), match->getName()); |
| 221 | } |
| 222 | last = first; |
| 223 | } |
| 224 | } |
Alexander Afanasyev | b7e8a81 | 2014-07-23 01:36:47 -0700 | [diff] [blame] | 225 | return std::make_pair(0, Name()); |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 228 | Index::Entry::Entry(const Data& data, int64_t id) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 229 | : m_name(data.getFullName()) |
| 230 | , m_id(id) |
| 231 | { |
| 232 | const ndn::Signature& signature = data.getSignature(); |
| 233 | if (signature.hasKeyLocator()) |
| 234 | m_keyLocatorHash = computeKeyLocatorHash(signature.getKeyLocator()); |
| 235 | } |
| 236 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 237 | Index::Entry::Entry(const Name& fullName, const KeyLocator& keyLocator, int64_t id) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 238 | : m_name(fullName) |
| 239 | , m_keyLocatorHash(computeKeyLocatorHash(keyLocator)) |
| 240 | , m_id(id) |
| 241 | { |
| 242 | } |
| 243 | |
| 244 | Index::Entry::Entry(const Name& fullName, |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 245 | const ndn::ConstBufferPtr& keyLocatorHash, int64_t id) |
Weiqi Shi | 28a90fb | 2014-07-09 10:28:55 -0700 | [diff] [blame] | 246 | : m_name(fullName) |
| 247 | , m_keyLocatorHash(keyLocatorHash) |
| 248 | , m_id(id) |
| 249 | { |
| 250 | } |
| 251 | |
| 252 | Index::Entry::Entry(const Name& name) |
| 253 | : m_name(name) |
| 254 | { |
| 255 | } |
| 256 | |
| 257 | } // namespace repo |