Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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 | |
| 77 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 78 | KeyLocator::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 79 | |
| 80 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 81 | KeyLocator::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 82 | |
| 83 | const Block& |
| 84 | KeyLocator::wireEncode() const |
| 85 | { |
| 86 | if (m_wire.hasWire()) |
| 87 | return m_wire; |
| 88 | |
| 89 | EncodingEstimator estimator; |
| 90 | size_t estimatedSize = wireEncode(estimator); |
| 91 | |
| 92 | EncodingBuffer buffer(estimatedSize, 0); |
| 93 | wireEncode(buffer); |
| 94 | |
| 95 | m_wire = buffer.block(); |
| 96 | return m_wire; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | KeyLocator::wireDecode(const Block& wire) |
| 101 | { |
| 102 | if (wire.type() != tlv::KeyLocator) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 103 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type during KeyLocator decoding")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 104 | |
| 105 | m_wire = wire; |
| 106 | m_wire.parse(); |
| 107 | |
| 108 | if (m_wire.elements().empty()) { |
| 109 | m_type = KeyLocator_None; |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | switch (m_wire.elements_begin()->type()) { |
| 114 | case tlv::Name: |
| 115 | m_type = KeyLocator_Name; |
| 116 | m_name.wireDecode(*m_wire.elements_begin()); |
| 117 | break; |
| 118 | case tlv::KeyDigest: |
| 119 | m_type = KeyLocator_KeyDigest; |
| 120 | m_keyDigest = *m_wire.elements_begin(); |
| 121 | break; |
| 122 | default: |
| 123 | m_type = KeyLocator_Unknown; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | KeyLocator& |
| 129 | KeyLocator::clear() |
| 130 | { |
| 131 | m_wire.reset(); |
| 132 | m_type = KeyLocator_None; |
| 133 | m_name.clear(); |
| 134 | m_keyDigest.reset(); |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | const Name& |
| 139 | KeyLocator::getName() const |
| 140 | { |
| 141 | if (m_type != KeyLocator_Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 142 | BOOST_THROW_EXCEPTION(Error("KeyLocator type is not Name")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 143 | |
| 144 | return m_name; |
| 145 | } |
| 146 | |
| 147 | KeyLocator& |
| 148 | KeyLocator::setName(const Name& name) |
| 149 | { |
| 150 | this->clear(); |
| 151 | m_type = KeyLocator_Name; |
| 152 | m_name = name; |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | const Block& |
| 157 | KeyLocator::getKeyDigest() const |
| 158 | { |
| 159 | if (m_type != KeyLocator_KeyDigest) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 160 | BOOST_THROW_EXCEPTION(Error("KeyLocator type is not KeyDigest")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 161 | |
| 162 | return m_keyDigest; |
| 163 | } |
| 164 | |
| 165 | KeyLocator& |
| 166 | KeyLocator::setKeyDigest(const Block& keyDigest) |
| 167 | { |
| 168 | if (keyDigest.type() != tlv::KeyDigest) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 169 | BOOST_THROW_EXCEPTION(Error("expecting KeyDigest block")); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 170 | |
| 171 | this->clear(); |
| 172 | m_type = KeyLocator_KeyDigest; |
| 173 | m_keyDigest = keyDigest; |
| 174 | return *this; |
| 175 | } |
| 176 | |
| 177 | KeyLocator& |
| 178 | KeyLocator::setKeyDigest(const ConstBufferPtr& keyDigest) |
| 179 | { |
| 180 | // WARNING: ConstBufferPtr is shared_ptr<const Buffer> |
| 181 | // This function takes a constant reference of a shared pointer. |
| 182 | // It MUST NOT change the reference count of that shared pointer. |
| 183 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 184 | return this->setKeyDigest(makeBinaryBlock(tlv::KeyDigest, keyDigest->get(), keyDigest->size())); |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | bool |
| 188 | KeyLocator::operator==(const KeyLocator& other) const |
| 189 | { |
| 190 | return wireEncode() == other.wireEncode(); |
| 191 | } |
| 192 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame^] | 193 | std::ostream& |
| 194 | operator<<(std::ostream& os, const KeyLocator& keyLocator) |
| 195 | { |
| 196 | switch (keyLocator.getType()) { |
| 197 | case KeyLocator::KeyLocator_Name: { |
| 198 | return os << "Name=" << keyLocator.getName(); |
| 199 | } |
| 200 | case KeyLocator::KeyLocator_KeyDigest: { |
| 201 | const size_t MAX_DIGEST_OCTETS_TO_SHOW = 5; |
| 202 | const Block& digest = keyLocator.getKeyDigest(); |
| 203 | os << "KeyDigest=" << toHex(digest.value(), digest.value_size()).substr(0, MAX_DIGEST_OCTETS_TO_SHOW * 2); |
| 204 | if (digest.value_size() > MAX_DIGEST_OCTETS_TO_SHOW) { |
| 205 | os << "..."; |
| 206 | } |
| 207 | return os; |
| 208 | } |
| 209 | case KeyLocator::KeyLocator_None: { |
| 210 | return os << "None"; |
| 211 | } |
| 212 | case KeyLocator::KeyLocator_Unknown: { |
| 213 | return os << "Unknown"; |
| 214 | } |
| 215 | } |
| 216 | return os << "Unknown"; |
| 217 | } |
| 218 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame] | 219 | } // namespace ndn |