Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -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 "key-locator.hpp" |
| 23 | #include "encoding/block-helpers.hpp" |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 24 | #include "util/string-helper.hpp" |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>)); |
| 29 | BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<KeyLocator>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>)); |
| 32 | static_assert(std::is_base_of<tlv::Error, KeyLocator::Error>::value, |
| 33 | "KeyLocator::Error must inherit from tlv::Error"); |
| 34 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 35 | KeyLocator::KeyLocator() |
| 36 | : m_type(KeyLocator_None) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | KeyLocator::KeyLocator(const Block& wire) |
| 41 | { |
| 42 | wireDecode(wire); |
| 43 | } |
| 44 | |
| 45 | KeyLocator::KeyLocator(const Name& name) |
| 46 | { |
| 47 | setName(name); |
| 48 | } |
| 49 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 50 | template<encoding::Tag TAG> |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 51 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 52 | KeyLocator::wireEncode(EncodingImpl<TAG>& encoder) const |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 53 | { |
| 54 | // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH (Name | KeyDigest) |
| 55 | // KeyDigest ::= KEY-DIGEST-TYPE TLV-LENGTH BYTE+ |
| 56 | |
| 57 | size_t totalLength = 0; |
| 58 | |
| 59 | switch (m_type) { |
| 60 | case KeyLocator_None: |
| 61 | break; |
| 62 | case KeyLocator_Name: |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 63 | totalLength += m_name.wireEncode(encoder); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 64 | break; |
| 65 | case KeyLocator_KeyDigest: |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 66 | totalLength += encoder.prependBlock(m_keyDigest); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 67 | break; |
| 68 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 69 | BOOST_THROW_EXCEPTION(Error("Unsupported KeyLocator type")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 72 | totalLength += encoder.prependVarNumber(totalLength); |
| 73 | totalLength += encoder.prependVarNumber(tlv::KeyLocator); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 74 | return totalLength; |
| 75 | } |
| 76 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 77 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(KeyLocator); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 78 | |
| 79 | const Block& |
| 80 | KeyLocator::wireEncode() const |
| 81 | { |
| 82 | if (m_wire.hasWire()) |
| 83 | return m_wire; |
| 84 | |
| 85 | EncodingEstimator estimator; |
| 86 | size_t estimatedSize = wireEncode(estimator); |
| 87 | |
| 88 | EncodingBuffer buffer(estimatedSize, 0); |
| 89 | wireEncode(buffer); |
| 90 | |
| 91 | m_wire = buffer.block(); |
| 92 | return m_wire; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | KeyLocator::wireDecode(const Block& wire) |
| 97 | { |
| 98 | if (wire.type() != tlv::KeyLocator) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 99 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type during KeyLocator decoding")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 100 | |
| 101 | m_wire = wire; |
| 102 | m_wire.parse(); |
| 103 | |
| 104 | if (m_wire.elements().empty()) { |
| 105 | m_type = KeyLocator_None; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | switch (m_wire.elements_begin()->type()) { |
| 110 | case tlv::Name: |
| 111 | m_type = KeyLocator_Name; |
| 112 | m_name.wireDecode(*m_wire.elements_begin()); |
| 113 | break; |
| 114 | case tlv::KeyDigest: |
| 115 | m_type = KeyLocator_KeyDigest; |
| 116 | m_keyDigest = *m_wire.elements_begin(); |
| 117 | break; |
| 118 | default: |
| 119 | m_type = KeyLocator_Unknown; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | KeyLocator& |
| 125 | KeyLocator::clear() |
| 126 | { |
| 127 | m_wire.reset(); |
| 128 | m_type = KeyLocator_None; |
| 129 | m_name.clear(); |
| 130 | m_keyDigest.reset(); |
| 131 | return *this; |
| 132 | } |
| 133 | |
| 134 | const Name& |
| 135 | KeyLocator::getName() const |
| 136 | { |
| 137 | if (m_type != KeyLocator_Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 138 | BOOST_THROW_EXCEPTION(Error("KeyLocator type is not Name")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 139 | |
| 140 | return m_name; |
| 141 | } |
| 142 | |
| 143 | KeyLocator& |
| 144 | KeyLocator::setName(const Name& name) |
| 145 | { |
| 146 | this->clear(); |
| 147 | m_type = KeyLocator_Name; |
| 148 | m_name = name; |
| 149 | return *this; |
| 150 | } |
| 151 | |
| 152 | const Block& |
| 153 | KeyLocator::getKeyDigest() const |
| 154 | { |
| 155 | if (m_type != KeyLocator_KeyDigest) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 156 | BOOST_THROW_EXCEPTION(Error("KeyLocator type is not KeyDigest")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 157 | |
| 158 | return m_keyDigest; |
| 159 | } |
| 160 | |
| 161 | KeyLocator& |
| 162 | KeyLocator::setKeyDigest(const Block& keyDigest) |
| 163 | { |
| 164 | if (keyDigest.type() != tlv::KeyDigest) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 165 | BOOST_THROW_EXCEPTION(Error("expecting KeyDigest block")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 166 | |
| 167 | this->clear(); |
| 168 | m_type = KeyLocator_KeyDigest; |
| 169 | m_keyDigest = keyDigest; |
| 170 | return *this; |
| 171 | } |
| 172 | |
| 173 | KeyLocator& |
| 174 | KeyLocator::setKeyDigest(const ConstBufferPtr& keyDigest) |
| 175 | { |
| 176 | // WARNING: ConstBufferPtr is shared_ptr<const Buffer> |
| 177 | // This function takes a constant reference of a shared pointer. |
| 178 | // It MUST NOT change the reference count of that shared pointer. |
| 179 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 180 | return this->setKeyDigest(makeBinaryBlock(tlv::KeyDigest, keyDigest->data(), keyDigest->size())); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | bool |
| 184 | KeyLocator::operator==(const KeyLocator& other) const |
| 185 | { |
| 186 | return wireEncode() == other.wireEncode(); |
| 187 | } |
| 188 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 189 | std::ostream& |
| 190 | operator<<(std::ostream& os, const KeyLocator& keyLocator) |
| 191 | { |
| 192 | switch (keyLocator.getType()) { |
| 193 | case KeyLocator::KeyLocator_Name: { |
| 194 | return os << "Name=" << keyLocator.getName(); |
| 195 | } |
| 196 | case KeyLocator::KeyLocator_KeyDigest: { |
| 197 | const size_t MAX_DIGEST_OCTETS_TO_SHOW = 5; |
| 198 | const Block& digest = keyLocator.getKeyDigest(); |
| 199 | os << "KeyDigest=" << toHex(digest.value(), digest.value_size()).substr(0, MAX_DIGEST_OCTETS_TO_SHOW * 2); |
| 200 | if (digest.value_size() > MAX_DIGEST_OCTETS_TO_SHOW) { |
| 201 | os << "..."; |
| 202 | } |
| 203 | return os; |
| 204 | } |
| 205 | case KeyLocator::KeyLocator_None: { |
| 206 | return os << "None"; |
| 207 | } |
| 208 | case KeyLocator::KeyLocator_Unknown: { |
| 209 | return os << "Unknown"; |
| 210 | } |
| 211 | } |
| 212 | return os << "Unknown"; |
| 213 | } |
| 214 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 215 | } // namespace ndn |