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 | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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" |
| 25 | |
| 26 | namespace ndn { |
| 27 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Data>)); |
| 29 | BOOST_CONCEPT_ASSERT((WireEncodable<Data>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Data>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 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 | { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 66 | BOOST_THROW_EXCEPTION(Error("Requested wire format, but data packet has not been signed yet")); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 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 | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 97 | Data::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder, |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 98 | bool unsignedPortion) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 99 | |
| 100 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 101 | Data::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder, |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | 9bd4d98 | 2015-05-13 14:31:19 +0200 | [diff] [blame] | 111 | 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()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 183 | BOOST_THROW_EXCEPTION(Error("Full name requested, but Data packet does not have wire format " |
| 184 | "(e.g., not signed)")); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 185 | } |
| 186 | m_fullName = m_name; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 187 | m_fullName.appendImplicitSha256Digest(crypto::computeSha256Digest(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()) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 233 | m_content = makeEmptyBlock(tlv::Content); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 234 | |
| 235 | if (!m_content.hasWire()) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 236 | m_content.encode(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 237 | return m_content; |
| 238 | } |
| 239 | |
| 240 | Data& |
| 241 | Data::setContent(const uint8_t* content, size_t contentLength) |
| 242 | { |
| 243 | onChanged(); |
| 244 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 245 | m_content = makeBinaryBlock(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 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 292 | void |
| 293 | Data::onChanged() |
| 294 | { |
| 295 | // The values have changed, so the wire format is invalidated |
| 296 | |
| 297 | // !!!Note!!! Signature is not invalidated and it is responsibility of |
| 298 | // the application to do proper re-signing if necessary |
| 299 | |
| 300 | m_wire.reset(); |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 301 | m_fullName.clear(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | bool |
| 305 | Data::operator==(const Data& other) const |
| 306 | { |
| 307 | return getName() == other.getName() && |
| 308 | getMetaInfo() == other.getMetaInfo() && |
| 309 | getContent() == other.getContent() && |
| 310 | getSignature() == other.getSignature(); |
| 311 | } |
| 312 | |
| 313 | bool |
| 314 | Data::operator!=(const Data& other) const |
| 315 | { |
| 316 | return !(*this == other); |
| 317 | } |
| 318 | |
| 319 | std::ostream& |
| 320 | operator<<(std::ostream& os, const Data& data) |
| 321 | { |
| 322 | os << "Name: " << data.getName() << "\n"; |
| 323 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 324 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
| 325 | os << "Signature: (type: " << data.getSignature().getType() << |
| 326 | ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
| 327 | os << std::endl; |
| 328 | |
| 329 | return os; |
| 330 | } |
| 331 | |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [diff] [blame] | 332 | #ifdef NDN_LP_KEEP_LOCAL_CONTROL_HEADER |
| 333 | |
| 334 | // Permit deprecated usage for gcc only. |
| 335 | // clang allows deprecated usage in deprecated functions, so it doesn't need this directive. |
| 336 | #pragma GCC diagnostic push |
| 337 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 338 | |
| 339 | lp::LocalControlHeaderFacade |
| 340 | Data::getLocalControlHeader() |
| 341 | { |
| 342 | return lp::LocalControlHeaderFacade(*this); |
| 343 | } |
| 344 | |
| 345 | const lp::LocalControlHeaderFacade |
| 346 | Data::getLocalControlHeader() const |
| 347 | { |
| 348 | return lp::LocalControlHeaderFacade(const_cast<Data&>(*this)); |
| 349 | } |
| 350 | |
| 351 | uint64_t |
| 352 | Data::getIncomingFaceId() const |
| 353 | { |
| 354 | return getLocalControlHeader().getIncomingFaceId(); |
| 355 | } |
| 356 | |
| 357 | Data& |
| 358 | Data::setIncomingFaceId(uint64_t incomingFaceId) |
| 359 | { |
| 360 | getLocalControlHeader().setIncomingFaceId(incomingFaceId); |
| 361 | return *this; |
| 362 | } |
| 363 | |
| 364 | lp::LocalControlHeaderFacade::CachingPolicy |
| 365 | Data::getCachingPolicy() const |
| 366 | { |
| 367 | return getLocalControlHeader().getCachingPolicy(); |
| 368 | } |
| 369 | |
| 370 | Data& |
| 371 | Data::setCachingPolicy(lp::LocalControlHeaderFacade::CachingPolicy cachingPolicy) |
| 372 | { |
| 373 | getLocalControlHeader().setCachingPolicy(cachingPolicy); |
| 374 | return *this; |
| 375 | } |
| 376 | |
| 377 | #pragma GCC diagnostic pop |
| 378 | |
| 379 | #endif // NDN_LP_KEEP_LOCAL_CONTROL_HEADER |
| 380 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 381 | } // namespace ndn |