blob: c5808f749b515898d93f65f5a4df41f3424c562a [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -07002/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#ifndef _NDN_INTEREST_HEADER_H_
23#define _NDN_INTEREST_HEADER_H_
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070024
Alexander Afanasyev2536e202011-08-12 14:13:10 -070025#include "ns3/integer.h"
26#include "ns3/header.h"
Alexander Afanasyeve275cf82012-04-18 14:25:02 -070027#include "ns3/simple-ref-count.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070028#include "ns3/nstime.h"
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070029
30#include <string>
31#include <vector>
32#include <list>
33
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070034#include "ndn-name.h"
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036namespace ns3 {
Alexander Afanasyev663d63f2012-09-09 11:55:36 -070037
38class Packet;
39
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ndn {
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070041
42/**
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070043 * @brief NDN Interest and routines to serialize/deserialize
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070044 *
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080045 * Optimized and simplified formatting of Interest packets
46 *
47 * Interest ::= Nonce
48 * Scope
49 * InterestLifetime
50 * Name
51 * Selectors
52 * Options
53 *
54 * Minumum size of the Interest packet: 1 + 4 + 2 + 1 + (2 + 0) + (2 + 0) + (2 + 0) = 14
55 *
56 * Maximum size of the Interest packet: 1 + 4 + 2 + 1 + (2 + 65535) + (2 + 65535) + (2 + 65535) = 196619
57 *
58 * ::
59 *
60 * 0 1 2 3
61 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * | Nonce |
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * | Scope | Reserved | InterestLifetime |
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * | Length | |
68 * |-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
69 * ~ ~
70 * ~ Name ~
71 * | |
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * | Length | |
74 * |-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
75 * ~ ~
76 * ~ Selectors ~
77 * | |
78 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 * | Length | |
80 * |-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
81 * ~ ~
82 * ~ Options ~
83 * | |
84 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 * **/
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070086class Interest : public SimpleRefCount<Interest, Header>
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070087{
88public:
89 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080090 * \brief Constructor
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070091 *
92 * Creates a null header
93 **/
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070094 Interest ();
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070095
96 /**
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070097 * @brief Copy constructor
98 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070099 Interest (const Interest &interest);
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700100
101 /**
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700102 * \brief Set interest name
103 *
Alexander Afanasyevcc50d982013-03-30 19:09:10 -0700104 * @param name smart pointer to Name
105 *
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700106 **/
107 void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700108 SetName (Ptr<Name> name);
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700109
Ilya Moiseenko332add02011-12-24 17:21:25 -0800110 /**
Alexander Afanasyevcc50d982013-03-30 19:09:10 -0700111 * \brief Another variant to set interest name
112 *
113 * @param name const reference to Name object
114 *
115 **/
116 void
117 SetName (const Name &name);
118
119 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -0800120 * \brief Get interest name
121 *
122 * Gets name of the interest.
123 **/
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700124 const Name&
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700125 GetName () const;
126
Ilya Moiseenko332add02011-12-24 17:21:25 -0800127 /**
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700128 * @brief Get smart pointer to the interest name (to avoid extra memory usage)
129 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700130 Ptr<const Name>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700131 GetNamePtr () const;
132
133 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -0800134 * \brief Set Scope
135 * Scope limits where the Interest may propagate.
136 * Scope 0 prevents propagation beyond the local ccnd (even to other applications on the same host).
137 * Scope 1 limits propagation to the applications on the originating host.
138 * Scope 2 limits propagation to no further than the next host.
139 * Other values are not defined, and will cause the Interest message to be dropped.
140 * Note that this is not a hop count - the value is not decremented as the interest is forwarded.
141 * @param[in] scope interest scope
142 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700143 void
144 SetScope (int8_t scope);
145
Ilya Moiseenko332add02011-12-24 17:21:25 -0800146 /**
147 * \brief Get Scope value
148 * Scope limits where the Interest may propagate.
149 * Scope 0 prevents propagation beyond the local ccnd (even to other applications on the same host).
150 * Scope 1 limits propagation to the applications on the originating host.
151 * Scope 2 limits propagation to no further than the next host.
152 * Other values are not defined, and will cause the Interest message to be dropped.
153 * Note that this is not a hop count - the value is not decremented as the interest is forwarded.
154 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700155 int8_t
156 GetScope () const;
157
Ilya Moiseenko332add02011-12-24 17:21:25 -0800158 /**
159 * \brief Set InterestLifetime
160 * InterestLifetime indicates the (approximate) time remaining before the interest times out.
161 * The timeout is relative to the arrival time of the interest at the current node.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700162 * \see http://www.ndn.org/releases/latest/doc/technical/InterestMessage.html for more information.
Ilya Moiseenko332add02011-12-24 17:21:25 -0800163 * @param[in] time interest lifetime
164 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700165 void
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700166 SetInterestLifetime (Time time);
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700167
Ilya Moiseenko332add02011-12-24 17:21:25 -0800168 /**
169 * \brief Get InterestLifetime value
170 * InterestLifetime indicates the (approximate) time remaining before the interest times out.
171 * The timeout is relative to the arrival time of the interest at the current node.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700172 * \see http://www.ndn.org/releases/latest/doc/technical/InterestMessage.html for more information.
Ilya Moiseenko332add02011-12-24 17:21:25 -0800173 */
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700174 Time
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700175 GetInterestLifetime () const;
176
Ilya Moiseenko332add02011-12-24 17:21:25 -0800177 /**
178 * \brief Set Nonce
179 * Nonce carries a randomly-genenerated bytestring that is used to detect and discard duplicate Interest messages.
180 * @param[in] nonce Unique packet identification number
181 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700182 void
183 SetNonce (uint32_t nonce);
184
Ilya Moiseenko332add02011-12-24 17:21:25 -0800185 /**
186 * \brief Get Nonce value
187 * Nonce carries a randomly-genenerated bytestring that is used to detect and discard duplicate Interest messages.
188 *
189 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700190 uint32_t
191 GetNonce () const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800192
193 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700194 * @brief NACK Type
195 * Specifies the type of Interest packet
Ilya Moiseenko332add02011-12-24 17:21:25 -0800196 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800197 enum
198 {
199 NORMAL_INTEREST = 0,
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800200 NACK_LOOP = 10,
201 NACK_CONGESTION = 11,
202 NACK_GIVEUP_PIT = 12,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800203 };
204
Ilya Moiseenko332add02011-12-24 17:21:25 -0800205 /**
206 * \brief Mark the Interest as a Negative Acknowledgement
207 * Three types of NACKs are supported
208 * 1. NACK_LOOP
209 * 2. NACK_CONGESTION
210 * 3. NACK_GIVEUP_PIT
211 * @param[in] nackType NACK_LOOP or NACK_CONGESTION or NACK_GIVEUP_PIT or NORMAL_INTEREST
212 */
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700213 void
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800214 SetNack (uint8_t nackType); //using reserved field
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700215
Ilya Moiseenko332add02011-12-24 17:21:25 -0800216 /**
217 * \brief Get NACK type
218 * Returns NACK_LOOP, NACK_CONGESTION or NACK_GIVEUP_PIT.
219 * Otherwise, in case of normal interest packet, returns NORMAL_INTEREST (equals 0).
220 */
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800221 uint8_t
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800222 GetNack () const;
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700223
224 //////////////////////////////////////////////////////////////////
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700225
226 static TypeId GetTypeId (void); ///< @brief Get TypeId of the class
227 virtual TypeId GetInstanceTypeId (void) const; ///< @brief Get TypeId of the instance
Ilya Moiseenko332add02011-12-24 17:21:25 -0800228
229 /**
230 * \brief Print Interest packet
231 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700232 virtual void Print (std::ostream &os) const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800233
234 /**
235 * \brief Get the size of Interest packet
236 * Returns the Interest packet size after serialization
237 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700238 virtual uint32_t GetSerializedSize (void) const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800239
240 /**
241 * \brief Serialize Interest packet
242 * Serializes Interest packet into Buffer::Iterator
243 * @param[in] start buffer to contain serialized Interest packet
244 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700245 virtual void Serialize (Buffer::Iterator start) const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800246
247 /**
248 * \brief Deserialize Interest packet
249 * Deserializes Buffer::Iterator into Interest packet
250 * @param[in] start buffer that contains serialized Interest packet
251 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700252 virtual uint32_t Deserialize (Buffer::Iterator start);
253
Alexander Afanasyev663d63f2012-09-09 11:55:36 -0700254 /**
255 * @brief Cheat for python bindings
256 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700257 static Ptr<Interest>
Alexander Afanasyev663d63f2012-09-09 11:55:36 -0700258 GetInterest (Ptr<Packet> packet);
259
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700260private:
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700261 Ptr<Name> m_name; ///< Interest name
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800262 uint8_t m_scope; ///< 0xFF not set, 0 local scope, 1 this host, 2 immediate neighborhood
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700263 Time m_interestLifetime; ///< InterestLifetime
264 uint32_t m_nonce; ///< Nonce. not used if zero
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800265 uint8_t m_nackType; ///< Negative Acknowledgement type
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700266};
267
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700268typedef Interest InterestHeader;
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700269
Ilya Moiseenko332add02011-12-24 17:21:25 -0800270/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700271 * @ingroup ndn-exceptions
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700272 * @brief Class for Interest parsing exception
Ilya Moiseenko332add02011-12-24 17:21:25 -0800273 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700274class InterestException {};
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700275
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700276} // namespace ndn
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700277} // namespace ns3
278
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700279#endif // _NDN_INTEREST_HEADER_H_