Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -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 "data.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 23 | #include "encoding/block-helpers.hpp" |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 24 | #include "util/crypto.hpp" |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 25 | #include "util/concepts.hpp" |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 29 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Data>)); |
| 30 | BOOST_CONCEPT_ASSERT((WireEncodable<Data>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireDecodable<Data>)); |
| 32 | static_assert(std::is_base_of<tlv::Error, Data::Error>::value, |
| 33 | "Data::Error must inherit from tlv::Error"); |
| 34 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 35 | Data::Data() |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 36 | : m_content(tlv::Content) // empty content |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | Data::Data(const Name& name) |
| 41 | : m_name(name) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | Data::Data(const Block& wire) |
| 46 | { |
| 47 | wireDecode(wire); |
| 48 | } |
| 49 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 50 | template<encoding::Tag TAG> |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 51 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 52 | Data::wireEncode(EncodingImpl<TAG>& encoder, bool unsignedPortion/* = false*/) const |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 53 | { |
| 54 | size_t totalLength = 0; |
| 55 | |
| 56 | // Data ::= DATA-TLV TLV-LENGTH |
| 57 | // Name |
| 58 | // MetaInfo |
| 59 | // Content |
| 60 | // Signature |
| 61 | |
| 62 | // (reverse encoding) |
| 63 | |
| 64 | if (!unsignedPortion && !m_signature) |
| 65 | { |
| 66 | throw Error("Requested wire format, but data packet has not been signed yet"); |
| 67 | } |
| 68 | |
| 69 | if (!unsignedPortion) |
| 70 | { |
| 71 | // SignatureValue |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 72 | totalLength += encoder.prependBlock(m_signature.getValue()); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // SignatureInfo |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 76 | totalLength += encoder.prependBlock(m_signature.getInfo()); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 77 | |
| 78 | // Content |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 79 | totalLength += encoder.prependBlock(getContent()); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 80 | |
| 81 | // MetaInfo |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 82 | totalLength += getMetaInfo().wireEncode(encoder); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 83 | |
| 84 | // Name |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 85 | totalLength += getName().wireEncode(encoder); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 86 | |
| 87 | if (!unsignedPortion) |
| 88 | { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 89 | totalLength += encoder.prependVarNumber(totalLength); |
| 90 | totalLength += encoder.prependVarNumber(tlv::Data); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 91 | } |
| 92 | return totalLength; |
| 93 | } |
| 94 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 95 | |
| 96 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 97 | Data::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block, |
| 98 | bool unsignedPortion) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 99 | |
| 100 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 101 | Data::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block, |
| 102 | bool unsignedPortion) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 103 | |
| 104 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 105 | const Block& |
| 106 | Data::wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const |
| 107 | { |
| 108 | size_t totalLength = encoder.size(); |
| 109 | totalLength += encoder.appendBlock(signatureValue); |
| 110 | |
| 111 | totalLength += encoder.prependVarNumber(totalLength); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 112 | encoder.prependVarNumber(tlv::Data); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 113 | |
| 114 | const_cast<Data*>(this)->wireDecode(encoder.block()); |
| 115 | return m_wire; |
| 116 | } |
| 117 | |
| 118 | const Block& |
| 119 | Data::wireEncode() const |
| 120 | { |
| 121 | if (m_wire.hasWire()) |
| 122 | return m_wire; |
| 123 | |
| 124 | EncodingEstimator estimator; |
| 125 | size_t estimatedSize = wireEncode(estimator); |
| 126 | |
| 127 | EncodingBuffer buffer(estimatedSize, 0); |
| 128 | wireEncode(buffer); |
| 129 | |
| 130 | const_cast<Data*>(this)->wireDecode(buffer.block()); |
| 131 | return m_wire; |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | Data::wireDecode(const Block& wire) |
| 136 | { |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 137 | m_fullName.clear(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 138 | m_wire = wire; |
| 139 | m_wire.parse(); |
| 140 | |
| 141 | // Data ::= DATA-TLV TLV-LENGTH |
| 142 | // Name |
| 143 | // MetaInfo |
| 144 | // Content |
| 145 | // Signature |
| 146 | |
| 147 | // Name |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 148 | m_name.wireDecode(m_wire.get(tlv::Name)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 149 | |
| 150 | // MetaInfo |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 151 | m_metaInfo.wireDecode(m_wire.get(tlv::MetaInfo)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 152 | |
| 153 | // Content |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 154 | m_content = m_wire.get(tlv::Content); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 155 | |
| 156 | /////////////// |
| 157 | // Signature // |
| 158 | /////////////// |
| 159 | |
| 160 | // SignatureInfo |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 161 | m_signature.setInfo(m_wire.get(tlv::SignatureInfo)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 162 | |
| 163 | // SignatureValue |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 164 | Block::element_const_iterator val = m_wire.find(tlv::SignatureValue); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 165 | if (val != m_wire.elements_end()) |
| 166 | m_signature.setValue(*val); |
| 167 | } |
| 168 | |
| 169 | Data& |
| 170 | Data::setName(const Name& name) |
| 171 | { |
| 172 | onChanged(); |
| 173 | m_name = name; |
| 174 | |
| 175 | return *this; |
| 176 | } |
| 177 | |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 178 | const Name& |
| 179 | Data::getFullName() const |
| 180 | { |
| 181 | if (m_fullName.empty()) { |
| 182 | if (!m_wire.hasWire()) { |
| 183 | throw Error("Full name requested, but Data packet does not have wire format " |
| 184 | "(e.g., not signed)"); |
| 185 | } |
| 186 | m_fullName = m_name; |
Alexander Afanasyev | afee0a5 | 2014-11-07 11:15:34 -0800 | [diff] [blame] | 187 | m_fullName.appendImplicitSha256Digest(crypto::sha256(m_wire.wire(), m_wire.size())); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return m_fullName; |
| 191 | } |
| 192 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 193 | Data& |
| 194 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 195 | { |
| 196 | onChanged(); |
| 197 | m_metaInfo = metaInfo; |
| 198 | |
| 199 | return *this; |
| 200 | } |
| 201 | |
| 202 | Data& |
| 203 | Data::setContentType(uint32_t type) |
| 204 | { |
| 205 | onChanged(); |
| 206 | m_metaInfo.setType(type); |
| 207 | |
| 208 | return *this; |
| 209 | } |
| 210 | |
| 211 | Data& |
| 212 | Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod) |
| 213 | { |
| 214 | onChanged(); |
| 215 | m_metaInfo.setFreshnessPeriod(freshnessPeriod); |
| 216 | |
| 217 | return *this; |
| 218 | } |
| 219 | |
| 220 | Data& |
| 221 | Data::setFinalBlockId(const name::Component& finalBlockId) |
| 222 | { |
| 223 | onChanged(); |
| 224 | m_metaInfo.setFinalBlockId(finalBlockId); |
| 225 | |
| 226 | return *this; |
| 227 | } |
| 228 | |
| 229 | const Block& |
| 230 | Data::getContent() const |
| 231 | { |
| 232 | if (m_content.empty()) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 233 | m_content = dataBlock(tlv::Content, reinterpret_cast<const uint8_t*>(0), 0); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 234 | |
| 235 | if (!m_content.hasWire()) |
| 236 | m_content.encode(); |
| 237 | return m_content; |
| 238 | } |
| 239 | |
| 240 | Data& |
| 241 | Data::setContent(const uint8_t* content, size_t contentLength) |
| 242 | { |
| 243 | onChanged(); |
| 244 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 245 | m_content = dataBlock(tlv::Content, content, contentLength); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 246 | |
| 247 | return *this; |
| 248 | } |
| 249 | |
| 250 | Data& |
| 251 | Data::setContent(const ConstBufferPtr& contentValue) |
| 252 | { |
| 253 | onChanged(); |
| 254 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 255 | m_content = Block(tlv::Content, contentValue); // not a real wire encoding yet |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 256 | |
| 257 | return *this; |
| 258 | } |
| 259 | |
| 260 | Data& |
| 261 | Data::setContent(const Block& content) |
| 262 | { |
| 263 | onChanged(); |
| 264 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 265 | if (content.type() == tlv::Content) |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 266 | m_content = content; |
| 267 | else { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 268 | m_content = Block(tlv::Content, content); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return *this; |
| 272 | } |
| 273 | |
| 274 | Data& |
| 275 | Data::setSignature(const Signature& signature) |
| 276 | { |
| 277 | onChanged(); |
| 278 | m_signature = signature; |
| 279 | |
| 280 | return *this; |
| 281 | } |
| 282 | |
| 283 | Data& |
| 284 | Data::setSignatureValue(const Block& value) |
| 285 | { |
| 286 | onChanged(); |
| 287 | m_signature.setValue(value); |
| 288 | |
| 289 | return *this; |
| 290 | } |
| 291 | |
| 292 | // |
| 293 | |
| 294 | Data& |
| 295 | Data::setIncomingFaceId(uint64_t incomingFaceId) |
| 296 | { |
| 297 | getLocalControlHeader().setIncomingFaceId(incomingFaceId); |
| 298 | // ! do not reset Data's wire ! |
| 299 | |
| 300 | return *this; |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | Data::onChanged() |
| 305 | { |
| 306 | // The values have changed, so the wire format is invalidated |
| 307 | |
| 308 | // !!!Note!!! Signature is not invalidated and it is responsibility of |
| 309 | // the application to do proper re-signing if necessary |
| 310 | |
| 311 | m_wire.reset(); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 312 | m_fullName.clear(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | bool |
| 316 | Data::operator==(const Data& other) const |
| 317 | { |
| 318 | return getName() == other.getName() && |
| 319 | getMetaInfo() == other.getMetaInfo() && |
| 320 | getContent() == other.getContent() && |
| 321 | getSignature() == other.getSignature(); |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | Data::operator!=(const Data& other) const |
| 326 | { |
| 327 | return !(*this == other); |
| 328 | } |
| 329 | |
| 330 | std::ostream& |
| 331 | operator<<(std::ostream& os, const Data& data) |
| 332 | { |
| 333 | os << "Name: " << data.getName() << "\n"; |
| 334 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 335 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
| 336 | os << "Signature: (type: " << data.getSignature().getType() << |
| 337 | ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
| 338 | os << std::endl; |
| 339 | |
| 340 | return os; |
| 341 | } |
| 342 | |
| 343 | } // namespace ndn |