Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_META_INFO_HPP |
| 23 | #define NDN_META_INFO_HPP |
| 24 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 25 | #include "common.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 26 | #include "encoding/block.hpp" |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 27 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 28 | #include "util/time.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 29 | #include "name-component.hpp" |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 30 | #include <list> |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 32 | namespace ndn { |
| 33 | |
| 34 | /** |
| 35 | * An MetaInfo holds the meta info which is signed inside the data packet. |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 36 | * |
| 37 | * The class allows experimentation with application-defined meta information blocks, |
| 38 | * which slightly violates NDN-TLV specification. When using the application-defined |
| 39 | * meta information blocks be aware that this may result in packet drop (NFD and |
| 40 | * previous versions of ndn-cxx will gracefully accept such packet). |
| 41 | * |
| 42 | * The following definition of MetaInfo block is assumed in this implementation (compared |
| 43 | * to the NDN-TLV spec, definition extended to allow optional AppMetaInfo TLV blocks): |
| 44 | * |
| 45 | * MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 46 | * ContentType? |
| 47 | * FreshnessPeriod? |
| 48 | * FinalBlockId? |
| 49 | * AppMetaInfo* |
| 50 | * |
| 51 | * AppMetaInfo ::= any TLV block with type in the restricted application range [128, 252] |
| 52 | * |
| 53 | * Note that AppMetaInfo blocks are application-defined and must have TLV type from |
| 54 | * the restricted application range [128, 252]. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 55 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 56 | class MetaInfo |
| 57 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 58 | public: |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 59 | class Error : public tlv::Error |
| 60 | { |
| 61 | public: |
| 62 | explicit |
| 63 | Error(const std::string& what) |
| 64 | : tlv::Error(what) |
| 65 | { |
| 66 | } |
| 67 | }; |
| 68 | |
Junxiao Shi | cad32e3 | 2014-11-03 20:32:36 -0700 | [diff] [blame] | 69 | /** \brief ContentType codes |
| 70 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 71 | enum { |
Junxiao Shi | cad32e3 | 2014-11-03 20:32:36 -0700 | [diff] [blame] | 72 | /** @brief indicates content is the actual data bits |
| 73 | */ |
| 74 | TYPE_BLOB = 0, |
| 75 | |
| 76 | /** @brief indicates content is another name which identifies actual data content |
| 77 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 78 | TYPE_LINK = 1, |
Junxiao Shi | cad32e3 | 2014-11-03 20:32:36 -0700 | [diff] [blame] | 79 | |
| 80 | /** @brief indicates content is a public key |
| 81 | */ |
| 82 | TYPE_KEY = 2, |
| 83 | |
| 84 | /** @brief indicates a producer generated NACK |
| 85 | * @warning Experimental. Not defined in NDN-TLV spec. |
| 86 | */ |
| 87 | TYPE_NACK = 3, |
| 88 | |
| 89 | TYPE_DEFAULT = TYPE_BLOB |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 90 | }; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 92 | MetaInfo(); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 94 | /** |
| 95 | * @brief Create from wire encoding |
| 96 | */ |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 97 | explicit |
| 98 | MetaInfo(const Block& block); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 99 | |
| 100 | template<bool T> |
| 101 | size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 102 | wireEncode(EncodingImpl<T>& block) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 104 | const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 105 | wireEncode() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 106 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 107 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 108 | wireDecode(const Block& wire); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 109 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 110 | /////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 111 | // Getters/setters |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 112 | |
| 113 | uint32_t |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 114 | getType() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 116 | MetaInfo& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 117 | setType(uint32_t type); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 118 | |
| 119 | const time::milliseconds& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 120 | getFreshnessPeriod() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 122 | MetaInfo& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 123 | setFreshnessPeriod(const time::milliseconds& freshnessPeriod); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 125 | const name::Component& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 126 | getFinalBlockId() const; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 127 | |
| 128 | MetaInfo& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 129 | setFinalBlockId(const name::Component& finalBlockId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 130 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 131 | /** |
| 132 | * @brief Get all app-defined MetaInfo items |
| 133 | * |
| 134 | * @note Warning: Experimental API, which may change or disappear in the future |
| 135 | * |
| 136 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 137 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 138 | */ |
| 139 | const std::list<Block>& |
| 140 | getAppMetaInfo() const; |
| 141 | |
| 142 | /** |
| 143 | * @brief Set app-defined MetaInfo items |
| 144 | * |
| 145 | * This method will replace all existing app-defined MetaInfo items, if they existed. |
| 146 | * |
| 147 | * @throw Error if some block in @p info has type not in the application range |
| 148 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 149 | * |
| 150 | * @note Warning: Experimental API, which may change or disappear in the future |
| 151 | * |
| 152 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 153 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 154 | */ |
| 155 | MetaInfo& |
| 156 | setAppMetaInfo(const std::list<Block>& info); |
| 157 | |
| 158 | /** |
| 159 | * @brief Add an app-defined MetaInfo item |
| 160 | * |
| 161 | * @throw Error if @p block has type not in the application range |
| 162 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 163 | * |
| 164 | * @note Warning: Experimental API, which may change or disappear in the future |
| 165 | * |
| 166 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 167 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 168 | */ |
| 169 | MetaInfo& |
| 170 | addAppMetaInfo(const Block& block); |
| 171 | |
| 172 | /** |
| 173 | * @brief Remove a first app-defined MetaInfo item with type @p tlvType |
| 174 | * |
| 175 | * @return true if an item was deleted |
| 176 | * |
| 177 | * @note Warning: Experimental API, which may change or disappear in the future |
| 178 | * |
| 179 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 180 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 181 | */ |
| 182 | bool |
| 183 | removeAppMetaInfo(uint32_t tlvType); |
| 184 | |
| 185 | /** |
| 186 | * @brief Find a first app-defined MetaInfo item of type @p tlvType |
| 187 | * |
| 188 | * @return NULL if an item is not found, otherwise const pointer to the item |
| 189 | * |
| 190 | * @throw Error if @p tlvType is not in the application range |
| 191 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 192 | * |
| 193 | * @note Warning: Experimental API, which may change or disappear in the future |
| 194 | * |
| 195 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 196 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 197 | */ |
| 198 | const Block* |
| 199 | findAppMetaInfo(uint32_t tlvType) const; |
| 200 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 201 | public: // EqualityComparable concept |
| 202 | bool |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 203 | operator==(const MetaInfo& other) const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 204 | |
| 205 | bool |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 206 | operator!=(const MetaInfo& other) const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 207 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 208 | private: |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 209 | uint32_t m_type; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 210 | time::milliseconds m_freshnessPeriod; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 211 | name::Component m_finalBlockId; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 212 | std::list<Block> m_appMetaInfo; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 214 | mutable Block m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 217 | std::ostream& |
| 218 | operator<<(std::ostream& os, const MetaInfo& info); |
| 219 | |
| 220 | ///////////////////////////////////////////////////////////////////////// |
| 221 | |
| 222 | inline uint32_t |
| 223 | MetaInfo::getType() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 224 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 225 | return m_type; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 228 | inline const time::milliseconds& |
| 229 | MetaInfo::getFreshnessPeriod() const |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 230 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 231 | return m_freshnessPeriod; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 232 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 233 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 234 | inline const name::Component& |
| 235 | MetaInfo::getFinalBlockId() const |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 236 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 237 | return m_finalBlockId; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 240 | inline bool |
| 241 | MetaInfo::operator==(const MetaInfo& other) const |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 242 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 243 | return wireEncode() == other.wireEncode(); |
| 244 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 245 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 246 | inline bool |
| 247 | MetaInfo::operator!=(const MetaInfo& other) const |
| 248 | { |
| 249 | return !(*this == other); |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 252 | } // namespace ndn |
| 253 | |
| 254 | #endif // NDN_META_INFO_HPP |