Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "link.hpp" |
| 23 | #include "interest.hpp" |
| 24 | #include "encoding/block-helpers.hpp" |
| 25 | #include "util/crypto.hpp" |
| 26 | #include "security/key-chain.hpp" |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame^] | 28 | #include <boost/range/adaptor/reversed.hpp> |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | |
| 32 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Link>)); |
| 33 | BOOST_CONCEPT_ASSERT((WireEncodable<Link>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 34 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Link>)); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 35 | BOOST_CONCEPT_ASSERT((WireDecodable<Link>)); |
| 36 | static_assert(std::is_base_of<Data::Error, Link::Error>::value, |
| 37 | "Link::Error should inherit from Data::Error"); |
| 38 | |
| 39 | Link::Link(const Block& block) |
| 40 | { |
| 41 | wireDecode(block); |
| 42 | } |
| 43 | |
| 44 | Link::Link(const Name& name) |
| 45 | : Data(name) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | Link::Link(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> links) |
| 50 | : Data(name) |
| 51 | { |
| 52 | m_delegations.insert(links); |
| 53 | encodeContent(); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | Link::addDelegation(uint32_t preference, const Name& name) |
| 58 | { |
| 59 | this->removeDelegationNoEncode(name); |
| 60 | m_delegations.insert({preference, name}); |
| 61 | encodeContent(); |
| 62 | } |
| 63 | |
| 64 | bool |
| 65 | Link::removeDelegation(const Name& name) |
| 66 | { |
| 67 | bool hasRemovedDelegation = this->removeDelegationNoEncode(name); |
| 68 | if (hasRemovedDelegation) { |
| 69 | encodeContent(); |
| 70 | } |
| 71 | return hasRemovedDelegation; |
| 72 | } |
| 73 | |
| 74 | const Link::DelegationSet& |
| 75 | Link::getDelegations() const |
| 76 | { |
| 77 | return m_delegations; |
| 78 | } |
| 79 | |
| 80 | template<encoding::Tag TAG> |
| 81 | size_t |
| 82 | Link::encodeContent(EncodingImpl<TAG>& encoder) const |
| 83 | { |
| 84 | // LinkContent ::= CONTENT-TYPE TLV-LENGTH |
| 85 | // Delegation+ |
| 86 | |
| 87 | // Delegation ::= LINK-DELEGATION-TYPE TLV-LENGTH |
| 88 | // Preference |
| 89 | // Name |
| 90 | |
| 91 | // Preference ::= LINK-PREFERENCE-TYPE TLV-LENGTH |
| 92 | // nonNegativeInteger |
| 93 | |
| 94 | size_t totalLength = 0; |
| 95 | for (const auto& delegation : m_delegations | boost::adaptors::reversed) { |
| 96 | size_t delegationLength = 0; |
| 97 | delegationLength += std::get<1>(delegation).wireEncode(encoder); |
| 98 | delegationLength += prependNonNegativeIntegerBlock(encoder, tlv::LinkPreference, |
| 99 | std::get<0>(delegation)); |
| 100 | delegationLength += encoder.prependVarNumber(delegationLength); |
| 101 | delegationLength += encoder.prependVarNumber(tlv::LinkDelegation); |
| 102 | totalLength += delegationLength; |
| 103 | } |
| 104 | totalLength += encoder.prependVarNumber(totalLength); |
| 105 | totalLength += encoder.prependVarNumber(tlv::Content); |
| 106 | return totalLength; |
| 107 | } |
| 108 | |
| 109 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 110 | Link::encodeContent<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 111 | |
| 112 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 113 | Link::encodeContent<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 114 | |
| 115 | void |
| 116 | Link::encodeContent() |
| 117 | { |
| 118 | onChanged(); |
| 119 | |
| 120 | EncodingEstimator estimator; |
| 121 | size_t estimatedSize = encodeContent(estimator); |
| 122 | |
| 123 | EncodingBuffer buffer(estimatedSize, 0); |
| 124 | encodeContent(buffer); |
| 125 | |
| 126 | setContentType(tlv::ContentType_Link); |
| 127 | setContent(buffer.block()); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | Link::decodeContent() |
| 132 | { |
| 133 | // LinkContent ::= CONTENT-TYPE TLV-LENGTH |
| 134 | // Delegation+ |
| 135 | |
| 136 | // Delegation ::= LINK-DELEGATION-TYPE TLV-LENGTH |
| 137 | // Preference |
| 138 | // Name |
| 139 | |
| 140 | // Preference ::= LINK-PREFERENCE-TYPE TLV-LENGTH |
| 141 | // nonNegativeInteger |
| 142 | |
| 143 | if (getContentType() != tlv::ContentType_Link) |
| 144 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 145 | BOOST_THROW_EXCEPTION(Error("Expected Content Type Link")); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | const Block& content = getContent(); |
| 149 | content.parse(); |
| 150 | |
| 151 | for (auto& delegation : content.elements()) { |
| 152 | delegation.parse(); |
| 153 | Block::element_const_iterator val = delegation.elements_begin(); |
| 154 | if (val == delegation.elements_end()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 155 | BOOST_THROW_EXCEPTION(Error("Unexpected Link Encoding")); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 156 | } |
| 157 | uint32_t preference; |
| 158 | try { |
| 159 | preference = static_cast<uint32_t>(readNonNegativeInteger(*val)); |
| 160 | } |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame^] | 161 | catch (const tlv::Error&) { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 162 | BOOST_THROW_EXCEPTION(Error("Missing Preference field in Link Encoding")); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 163 | } |
| 164 | ++val; |
| 165 | if (val == delegation.elements_end()) { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 166 | BOOST_THROW_EXCEPTION(Error("Missing Name field in Link Encoding")); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 167 | } |
| 168 | Name name(*val); |
| 169 | m_delegations.insert({preference, name}); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | Link::wireDecode(const Block& wire) |
| 175 | { |
| 176 | Data::wireDecode(wire); |
| 177 | decodeContent(); |
| 178 | } |
| 179 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 180 | std::tuple<uint32_t, Name> |
| 181 | Link::getDelegationFromWire(const Block& block, size_t index) |
| 182 | { |
| 183 | block.parse(); |
| 184 | const Block& contentBlock = block.get(tlv::Content); |
| 185 | contentBlock.parse(); |
| 186 | const Block& delegationBlock = contentBlock.elements().at(index); |
| 187 | delegationBlock.parse(); |
| 188 | if (delegationBlock.type() != tlv::LinkDelegation) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 189 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE, expecting LinkDelegation")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 190 | } |
| 191 | return std::make_tuple( |
| 192 | static_cast<uint32_t>( |
| 193 | readNonNegativeInteger(delegationBlock.get(tlv::LinkPreference))), |
| 194 | Name(delegationBlock.get(tlv::Name))); |
| 195 | } |
| 196 | |
| 197 | ssize_t |
| 198 | Link::findDelegationFromWire(const Block& block, const Name& delegationName) |
| 199 | { |
| 200 | block.parse(); |
| 201 | const Block& contentBlock = block.get(tlv::Content); |
| 202 | contentBlock.parse(); |
| 203 | size_t counter = 0; |
| 204 | for (auto&& delegationBlock : contentBlock.elements()) { |
| 205 | delegationBlock.parse(); |
| 206 | if (delegationBlock.type() != tlv::LinkDelegation) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 207 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE, expecting LinkDelegation")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 208 | } |
| 209 | Name name(delegationBlock.get(tlv::Name)); |
| 210 | if (name == delegationName) { |
| 211 | return counter; |
| 212 | } |
| 213 | ++counter; |
| 214 | } |
| 215 | return INVALID_SELECTED_DELEGATION_INDEX; |
| 216 | } |
| 217 | |
| 218 | ssize_t |
| 219 | Link::countDelegationsFromWire(const Block& block) |
| 220 | { |
| 221 | block.parse(); |
| 222 | const Block& contentBlock = block.get(tlv::Content); |
| 223 | contentBlock.parse(); |
| 224 | return contentBlock.elements_size(); |
| 225 | } |
| 226 | |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 227 | bool |
| 228 | Link::removeDelegationNoEncode(const Name& name) |
| 229 | { |
| 230 | bool hasRemoved = false; |
| 231 | auto i = m_delegations.begin(); |
| 232 | while (i != m_delegations.end()) { |
| 233 | if (i->second == name) { |
| 234 | hasRemoved = true; |
| 235 | i = m_delegations.erase(i); |
| 236 | } |
| 237 | else { |
| 238 | ++i; |
| 239 | } |
| 240 | } |
| 241 | return hasRemoved; |
| 242 | } |
| 243 | |
| 244 | } // namespace ndn |