blob: d73014e59be401e87c1033e2cc8d84f349436dac [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyev2ba8f662014-01-05 22:53:18 -080020 */
21
22#ifndef NDN_META_INFO_HPP
23#define NDN_META_INFO_HPP
24
Alexander Afanasyev15f67312014-07-22 15:11:09 -070025#include "common.hpp"
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070026#include "encoding/block.hpp"
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080027#include "encoding/encoding-buffer.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070028#include "util/time.hpp"
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070029#include "name-component.hpp"
Shock Jiangca7ea702014-10-02 11:23:25 -070030#include <list>
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080031
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080032namespace ndn {
33
34/**
35 * An MetaInfo holds the meta info which is signed inside the data packet.
Shock Jiangca7ea702014-10-02 11:23:25 -070036 *
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 Afanasyev2ba8f662014-01-05 22:53:18 -080055 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070056class MetaInfo
57{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080058public:
Shock Jiangca7ea702014-10-02 11:23:25 -070059 class Error : public tlv::Error
60 {
61 public:
62 explicit
63 Error(const std::string& what)
64 : tlv::Error(what)
65 {
66 }
67 };
68
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080069 enum {
70 TYPE_DEFAULT = 0,
71 TYPE_LINK = 1,
72 TYPE_KEY = 2
73 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070075 MetaInfo();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080076
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077 /**
78 * @brief Create from wire encoding
79 */
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070080 explicit
81 MetaInfo(const Block& block);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080082
83 template<bool T>
84 size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070085 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080086
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087 const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080088 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089
Alexander Afanasyevc348f832014-02-17 16:35:17 -080090 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070091 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070092
Alexander Afanasyevc348f832014-02-17 16:35:17 -080093 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080094 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095
96 uint32_t
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070097 getType() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080099 MetaInfo&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700100 setType(uint32_t type);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101
102 const time::milliseconds&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700103 getFreshnessPeriod() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700104
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800105 MetaInfo&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700106 setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800107
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800108 const name::Component&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700109 getFinalBlockId() const;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800110
111 MetaInfo&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700112 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700113
Shock Jiangca7ea702014-10-02 11:23:25 -0700114 /**
115 * @brief Get all app-defined MetaInfo items
116 *
117 * @note Warning: Experimental API, which may change or disappear in the future
118 *
119 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
120 * is called before *AppMetaInfo, all app-defined blocks will be lost
121 */
122 const std::list<Block>&
123 getAppMetaInfo() const;
124
125 /**
126 * @brief Set app-defined MetaInfo items
127 *
128 * This method will replace all existing app-defined MetaInfo items, if they existed.
129 *
130 * @throw Error if some block in @p info has type not in the application range
131 * (http://named-data.net/doc/ndn-tlv/types.html)
132 *
133 * @note Warning: Experimental API, which may change or disappear in the future
134 *
135 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
136 * is called before *AppMetaInfo, all app-defined blocks will be lost
137 */
138 MetaInfo&
139 setAppMetaInfo(const std::list<Block>& info);
140
141 /**
142 * @brief Add an app-defined MetaInfo item
143 *
144 * @throw Error if @p block has type not in the application range
145 * (http://named-data.net/doc/ndn-tlv/types.html)
146 *
147 * @note Warning: Experimental API, which may change or disappear in the future
148 *
149 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
150 * is called before *AppMetaInfo, all app-defined blocks will be lost
151 */
152 MetaInfo&
153 addAppMetaInfo(const Block& block);
154
155 /**
156 * @brief Remove a first app-defined MetaInfo item with type @p tlvType
157 *
158 * @return true if an item was deleted
159 *
160 * @note Warning: Experimental API, which may change or disappear in the future
161 *
162 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
163 * is called before *AppMetaInfo, all app-defined blocks will be lost
164 */
165 bool
166 removeAppMetaInfo(uint32_t tlvType);
167
168 /**
169 * @brief Find a first app-defined MetaInfo item of type @p tlvType
170 *
171 * @return NULL if an item is not found, otherwise const pointer to the item
172 *
173 * @throw Error if @p tlvType is not in the application range
174 * (http://named-data.net/doc/ndn-tlv/types.html)
175 *
176 * @note Warning: Experimental API, which may change or disappear in the future
177 *
178 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
179 * is called before *AppMetaInfo, all app-defined blocks will be lost
180 */
181 const Block*
182 findAppMetaInfo(uint32_t tlvType) const;
183
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700184public: // EqualityComparable concept
185 bool
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700186 operator==(const MetaInfo& other) const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700187
188 bool
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700189 operator!=(const MetaInfo& other) const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700190
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800191private:
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800192 uint32_t m_type;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700193 time::milliseconds m_freshnessPeriod;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800194 name::Component m_finalBlockId;
Shock Jiangca7ea702014-10-02 11:23:25 -0700195 std::list<Block> m_appMetaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800196
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800197 mutable Block m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800198};
199
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700200std::ostream&
201operator<<(std::ostream& os, const MetaInfo& info);
202
203/////////////////////////////////////////////////////////////////////////
204
205inline uint32_t
206MetaInfo::getType() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800207{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700208 return m_type;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800209}
210
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700211inline const time::milliseconds&
212MetaInfo::getFreshnessPeriod() const
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800213{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700214 return m_freshnessPeriod;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800215}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700216
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700217inline const name::Component&
218MetaInfo::getFinalBlockId() const
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800219{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700220 return m_finalBlockId;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800221}
222
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700223inline bool
224MetaInfo::operator==(const MetaInfo& other) const
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800225{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700226 return wireEncode() == other.wireEncode();
227}
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800228
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700229inline bool
230MetaInfo::operator!=(const MetaInfo& other) const
231{
232 return !(*this == other);
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800233}
234
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800235} // namespace ndn
236
237#endif // NDN_META_INFO_HPP