blob: 096dc7ba4f8dd7fcfd7f007d8e3ef51107811522 [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 Afanasyev4aac5572012-08-09 10:49:55 -070034#include "ndn-name-components.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 Afanasyevb4fee8b2012-06-06 12:54:26 -070043 * @brief NDN InterestHeader and routines to serialize/deserialize
44 *
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 Afanasyev2b4c9472012-08-09 15:00:38 -070086class InterestHeader : public SimpleRefCount<InterestHeader, 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 Afanasyev2b4c9472012-08-09 15:00:38 -070094 InterestHeader ();
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070095
96 /**
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070097 * @brief Copy constructor
98 */
99 InterestHeader (const InterestHeader &interest);
100
101 /**
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700102 * \brief Set interest name
103 *
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700104 * Sets name of the interest. For example, SetName( ndnNameComponents("prefix")("postfix") );
105 * @param[in] name const pointer to ndnNameComponents object that contains an interest name
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700106 **/
107 void
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700108 SetName (Ptr<NameComponents> name);
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700109
Ilya Moiseenko332add02011-12-24 17:21:25 -0800110 /**
111 * \brief Get interest name
112 *
113 * Gets name of the interest.
114 **/
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115 const NameComponents&
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700116 GetName () const;
117
Ilya Moiseenko332add02011-12-24 17:21:25 -0800118 /**
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700119 * @brief Get smart pointer to the interest name (to avoid extra memory usage)
120 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121 Ptr<const NameComponents>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700122 GetNamePtr () const;
123
124 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -0800125 * \brief Set Scope
126 * Scope limits where the Interest may propagate.
127 * Scope 0 prevents propagation beyond the local ccnd (even to other applications on the same host).
128 * Scope 1 limits propagation to the applications on the originating host.
129 * Scope 2 limits propagation to no further than the next host.
130 * Other values are not defined, and will cause the Interest message to be dropped.
131 * Note that this is not a hop count - the value is not decremented as the interest is forwarded.
132 * @param[in] scope interest scope
133 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700134 void
135 SetScope (int8_t scope);
136
Ilya Moiseenko332add02011-12-24 17:21:25 -0800137 /**
138 * \brief Get Scope value
139 * Scope limits where the Interest may propagate.
140 * Scope 0 prevents propagation beyond the local ccnd (even to other applications on the same host).
141 * Scope 1 limits propagation to the applications on the originating host.
142 * Scope 2 limits propagation to no further than the next host.
143 * Other values are not defined, and will cause the Interest message to be dropped.
144 * Note that this is not a hop count - the value is not decremented as the interest is forwarded.
145 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700146 int8_t
147 GetScope () const;
148
Ilya Moiseenko332add02011-12-24 17:21:25 -0800149 /**
150 * \brief Set InterestLifetime
151 * InterestLifetime indicates the (approximate) time remaining before the interest times out.
152 * The timeout is relative to the arrival time of the interest at the current node.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700153 * \see http://www.ndn.org/releases/latest/doc/technical/InterestMessage.html for more information.
Ilya Moiseenko332add02011-12-24 17:21:25 -0800154 * @param[in] time interest lifetime
155 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700156 void
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700157 SetInterestLifetime (Time time);
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700158
Ilya Moiseenko332add02011-12-24 17:21:25 -0800159 /**
160 * \brief Get InterestLifetime value
161 * InterestLifetime indicates the (approximate) time remaining before the interest times out.
162 * The timeout is relative to the arrival time of the interest at the current node.
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700163 * \see http://www.ndn.org/releases/latest/doc/technical/InterestMessage.html for more information.
Ilya Moiseenko332add02011-12-24 17:21:25 -0800164 */
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700165 Time
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700166 GetInterestLifetime () const;
167
Ilya Moiseenko332add02011-12-24 17:21:25 -0800168 /**
169 * \brief Set Nonce
170 * Nonce carries a randomly-genenerated bytestring that is used to detect and discard duplicate Interest messages.
171 * @param[in] nonce Unique packet identification number
172 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700173 void
174 SetNonce (uint32_t nonce);
175
Ilya Moiseenko332add02011-12-24 17:21:25 -0800176 /**
177 * \brief Get Nonce value
178 * Nonce carries a randomly-genenerated bytestring that is used to detect and discard duplicate Interest messages.
179 *
180 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700181 uint32_t
182 GetNonce () const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800183
184 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700185 * @brief NACK Type
186 * Specifies the type of Interest packet
Ilya Moiseenko332add02011-12-24 17:21:25 -0800187 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800188 enum
189 {
190 NORMAL_INTEREST = 0,
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800191 NACK_LOOP = 10,
192 NACK_CONGESTION = 11,
193 NACK_GIVEUP_PIT = 12,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800194 };
195
Ilya Moiseenko332add02011-12-24 17:21:25 -0800196 /**
197 * \brief Mark the Interest as a Negative Acknowledgement
198 * Three types of NACKs are supported
199 * 1. NACK_LOOP
200 * 2. NACK_CONGESTION
201 * 3. NACK_GIVEUP_PIT
202 * @param[in] nackType NACK_LOOP or NACK_CONGESTION or NACK_GIVEUP_PIT or NORMAL_INTEREST
203 */
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700204 void
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800205 SetNack (uint8_t nackType); //using reserved field
Ilya Moiseenko75d9bf52011-10-28 13:18:32 -0700206
Ilya Moiseenko332add02011-12-24 17:21:25 -0800207 /**
208 * \brief Get NACK type
209 * Returns NACK_LOOP, NACK_CONGESTION or NACK_GIVEUP_PIT.
210 * Otherwise, in case of normal interest packet, returns NORMAL_INTEREST (equals 0).
211 */
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800212 uint8_t
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800213 GetNack () const;
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700214
215 //////////////////////////////////////////////////////////////////
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700216
217 static TypeId GetTypeId (void); ///< @brief Get TypeId of the class
218 virtual TypeId GetInstanceTypeId (void) const; ///< @brief Get TypeId of the instance
Ilya Moiseenko332add02011-12-24 17:21:25 -0800219
220 /**
221 * \brief Print Interest packet
222 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700223 virtual void Print (std::ostream &os) const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800224
225 /**
226 * \brief Get the size of Interest packet
227 * Returns the Interest packet size after serialization
228 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700229 virtual uint32_t GetSerializedSize (void) const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800230
231 /**
232 * \brief Serialize Interest packet
233 * Serializes Interest packet into Buffer::Iterator
234 * @param[in] start buffer to contain serialized Interest packet
235 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700236 virtual void Serialize (Buffer::Iterator start) const;
Ilya Moiseenko332add02011-12-24 17:21:25 -0800237
238 /**
239 * \brief Deserialize Interest packet
240 * Deserializes Buffer::Iterator into Interest packet
241 * @param[in] start buffer that contains serialized Interest packet
242 */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700243 virtual uint32_t Deserialize (Buffer::Iterator start);
244
Alexander Afanasyev663d63f2012-09-09 11:55:36 -0700245 /**
246 * @brief Cheat for python bindings
247 */
248 static Ptr<InterestHeader>
249 GetInterest (Ptr<Packet> packet);
250
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700251private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700252 Ptr<NameComponents> m_name; ///< Interest name
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800253 uint8_t m_scope; ///< 0xFF not set, 0 local scope, 1 this host, 2 immediate neighborhood
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700254 Time m_interestLifetime; ///< InterestLifetime
255 uint32_t m_nonce; ///< Nonce. not used if zero
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800256 uint8_t m_nackType; ///< Negative Acknowledgement type
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700257};
258
Ilya Moiseenko332add02011-12-24 17:21:25 -0800259/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700260 * @ingroup ndn-exceptions
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700261 * @brief Class for Interest parsing exception
Ilya Moiseenko332add02011-12-24 17:21:25 -0800262 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700263class InterestHeaderException {};
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700264
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700265} // namespace ndn
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700266} // namespace ns3
267
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700268#endif // _NDN_INTEREST_HEADER_H_