blob: f1bbaf5ee59cb664d46b9d97615096dd8ab02aa0 [file] [log] [blame]
Alexander Afanasyevb989b122013-07-10 17:15:46 -07001/** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 *
Alexander Afanasyeva89bc102013-07-16 10:17:31 -07006 * GNU 3.0 license, See the LICENSE file for more information
Alexander Afanasyevb989b122013-07-10 17:15:46 -07007 *
8 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9 */
10
11#include "ndnsim.h"
12
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070013using namespace std;
14
15#include <ns3/header.h>
16#include <ns3/packet.h>
17#include <ns3/log.h>
18
Alexander Afanasyeva89bc102013-07-16 10:17:31 -070019#include "ndnsim/wire-ndnsim.h"
20
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070021NS_LOG_COMPONENT_DEFINE ("ndn.wire.ndnSIM");
22
Alexander Afanasyevb989b122013-07-10 17:15:46 -070023NDN_NAMESPACE_BEGIN
24
25namespace wire {
26namespace ndnSIM {
27
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070028NS_OBJECT_ENSURE_REGISTERED (Interest);
29NS_OBJECT_ENSURE_REGISTERED (Data);
30
Alexander Afanasyevb989b122013-07-10 17:15:46 -070031///////////////////////////////////////////////////////////////////////////////////////////////////
32///////////////////////////////////////////////////////////////////////////////////////////////////
33///////////////////////////////////////////////////////////////////////////////////////////////////
34
35Interest::Interest ()
36 : m_interest (Create<ndn::Interest> ())
37{
38}
39
40Interest::Interest (Ptr<ndn::Interest> interest)
41 : m_interest (interest)
42{
43}
44
45Ptr<ndn::Interest>
46Interest::GetInterest ()
47{
48 return m_interest;
49}
50
51
52TypeId
53Interest::GetTypeId (void)
54{
55 static TypeId tid = TypeId ("ns3::ndn::Interest::ndnSIM")
56 .SetGroupName ("Ndn")
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070057 .SetParent<Header> ()
Alexander Afanasyevb989b122013-07-10 17:15:46 -070058 .AddConstructor<Interest> ()
59 ;
60 return tid;
61}
62
63TypeId
64Interest::GetInstanceTypeId (void) const
65{
66 return GetTypeId ();
67}
68
69Ptr<Packet>
70Interest::ToWire (Ptr<const ndn::Interest> interest)
71{
72 Ptr<const Packet> p = interest->GetWire ();
73 if (!p)
74 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070075 Ptr<Packet> packet = Create<Packet> (*interest->GetPayload ());
76 Interest wireEncoding (ConstCast<ndn::Interest> (interest));
77 packet->AddHeader (wireEncoding);
78 interest->SetWire (packet);
79
80 p = packet;
Alexander Afanasyevb989b122013-07-10 17:15:46 -070081 }
82
83 return p->Copy ();
84}
85
86Ptr<ndn::Interest>
87Interest::FromWire (Ptr<Packet> packet)
88{
89 Ptr<ndn::Interest> interest = Create<ndn::Interest> ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070090 interest->SetWire (packet->Copy ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -070091
92 Interest wireEncoding (interest);
93 packet->RemoveHeader (wireEncoding);
94
95 interest->SetPayload (packet);
96
97 return interest;
98}
99
100uint32_t
101Interest::GetSerializedSize (void) const
102{
103 size_t size =
104 1/*version*/ + 1 /*type*/ + 2/*length*/ +
105 (4/*nonce*/ + 1/*scope*/ + 1/*nack type*/ + 2/*timestamp*/ +
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700106 NdnSim::SerializedSizeName (m_interest->GetName ()) +
Alexander Afanasyevabb493a2013-07-19 15:31:33 -0700107
108 (2 +
109 (m_interest->GetExclude () == 0 ? 0 : (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ())))
110 )/* selectors */ +
111
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700112 (2 + 0)/* options */);
113
114 NS_LOG_INFO ("Serialize size = " << size);
115 return size;
116}
117
118void
119Interest::Serialize (Buffer::Iterator start) const
120{
121 start.WriteU8 (0x80); // version
122 start.WriteU8 (0x00); // packet type
123
124 start.WriteU16 (GetSerializedSize () - 4);
125
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700126 start.WriteU32 (m_interest->GetNonce ());
127 start.WriteU8 (m_interest->GetScope ());
128 start.WriteU8 (m_interest->GetNack ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700129
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700130 NS_ASSERT_MSG (0 <= m_interest->GetInterestLifetime ().ToInteger (Time::S) && m_interest->GetInterestLifetime ().ToInteger (Time::S) < 65535,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700131 "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535");
132
133 // rounding timestamp value to seconds
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700134 start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700135
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700136 NdnSim::SerializeName (start, m_interest->GetName ());
Alexander Afanasyevabb493a2013-07-19 15:31:33 -0700137
138 if (m_interest->GetExclude () == 0)
139 {
140 start.WriteU16 (0); // no selectors
141 }
142 else
143 {
144 start.WriteU16 (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ()));
145 start.WriteU8 (0x01);
146 NdnSim::SerializeExclude (start, *m_interest->GetExclude ());
147 }
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700148
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700149 start.WriteU16 (0); // no options
150}
151
152uint32_t
153Interest::Deserialize (Buffer::Iterator start)
154{
155 Buffer::Iterator i = start;
156
157 if (i.ReadU8 () != 0x80)
158 throw new InterestException ();
159
160 if (i.ReadU8 () != 0x00)
161 throw new InterestException ();
162
163 i.ReadU16 (); // length, don't need it right now
164
165 m_interest->SetNonce (i.ReadU32 ());
166 m_interest->SetScope (i.ReadU8 ());
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700167 m_interest->SetNack (i.ReadU8 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700168
169 m_interest->SetInterestLifetime (Seconds (i.ReadU16 ()));
170
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700171 m_interest->SetName (NdnSim::DeserializeName (i));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700172
Alexander Afanasyevabb493a2013-07-19 15:31:33 -0700173 uint32_t selectorsLen = i.ReadU16 ();
174 if (selectorsLen > 0)
175 {
176 if (i.ReadU8 () != 0x01) // exclude filter only
177 throw InterestException ();
178
179 m_interest->SetExclude (NdnSim::DeserializeExclude (i));
180 }
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700181 i.ReadU16 ();
182
183 NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
184
185 return i.GetDistanceFrom (start);
186}
187
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700188void
189Interest::Print (std::ostream &os) const
190{
191 m_interest->Print (os);
192}
193
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700194///////////////////////////////////////////////////////////////////////////////////////////////////
195///////////////////////////////////////////////////////////////////////////////////////////////////
196///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
199TypeId
200Data::GetTypeId (void)
201{
202 static TypeId tid = TypeId ("ns3::ndn::Data::ndnSIM")
203 .SetGroupName ("Ndn")
204 .SetParent<Header> ()
205 .AddConstructor<Data> ()
206 ;
207 return tid;
208}
209
210TypeId
211Data::GetInstanceTypeId (void) const
212{
213 return GetTypeId ();
214}
215
216
217Data::Data ()
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700218 : m_data (Create<ndn::Data> ())
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700219{
220}
221
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700222Data::Data (Ptr<ndn::Data> data)
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700223 : m_data (data)
224{
225}
226
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700227Ptr<ndn::Data>
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700228Data::GetData ()
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700229{
230 return m_data;
231}
232
233Ptr<Packet>
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700234Data::ToWire (Ptr<const ndn::Data> data)
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700235{
236 Ptr<const Packet> p = data->GetWire ();
237 if (!p)
238 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700239 Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700240 Data wireEncoding (ConstCast<ndn::Data> (data));
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700241 packet->AddHeader (wireEncoding);
242 data->SetWire (packet);
243
244 p = packet;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700245 }
246
247 return p->Copy ();
248}
249
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700250Ptr<ndn::Data>
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700251Data::FromWire (Ptr<Packet> packet)
252{
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700253 Ptr<ndn::Data> data = Create<ndn::Data> ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700254 data->SetWire (packet->Copy ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700255
256 Data wireEncoding (data);
257 packet->RemoveHeader (wireEncoding);
258
259 data->SetPayload (packet);
260
261 return data;
262}
263
264uint32_t
265Data::GetSerializedSize () const
266{
267 uint32_t size = 1 + 1 + 2 +
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700268 ((2 + 2) +
269 NdnSim::SerializedSizeName (m_data->GetName ()) +
270 (2 + 2 + 4 + 2 + 2 + (2 + 0)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700271 if (m_data->GetSignature () != 0)
272 size += 4;
273
274 NS_LOG_INFO ("Serialize size = " << size);
275 return size;
276}
277
278void
279Data::Serialize (Buffer::Iterator start) const
280{
281 start.WriteU8 (0x80); // version
282 start.WriteU8 (0x01); // packet type
283 start.WriteU16 (GetSerializedSize () - 4); // length
284
285 if (m_data->GetSignature () != 0)
286 {
287 start.WriteU16 (6); // signature length
288 start.WriteU16 (0xFF00); // "fake" simulator signature
289 start.WriteU32 (m_data->GetSignature ());
290 }
291 else
292 {
293 start.WriteU16 (2); // signature length
294 start.WriteU16 (0); // empty signature
295 }
296
297 // name
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700298 NdnSim::SerializeName (start, m_data->GetName ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700299
300 // content
301 // for now assume that contentdata length is zero
302 start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0));
303 start.WriteU16 (4 + 2 + 2 + (2 + 0));
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700304 start.WriteU32 (static_cast<uint32_t> (m_data->GetTimestamp ().ToInteger (Time::S)));
305 start.WriteU16 (static_cast<uint16_t> (m_data->GetFreshness ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700306 start.WriteU16 (0); // reserved
307 start.WriteU16 (0); // Length (ContentInfoOptions)
308
309 // that's it folks
310}
311
312uint32_t
313Data::Deserialize (Buffer::Iterator start)
314{
315 Buffer::Iterator i = start;
316
317 if (i.ReadU8 () != 0x80)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700318 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700319
320 if (i.ReadU8 () != 0x01)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700321 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700322
323 i.ReadU16 (); // length
324
325 uint32_t signatureLength = i.ReadU16 ();
326 if (signatureLength == 6)
327 {
328 if (i.ReadU16 () != 0xFF00) // signature type
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700329 throw new DataException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700330 m_data->SetSignature (i.ReadU32 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700331 }
332 else if (signatureLength == 2)
333 {
334 if (i.ReadU16 () != 0) // signature type
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700335 throw new DataException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700336 m_data->SetSignature (0);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700337 }
338 else
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700339 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700340
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700341 m_data->SetName (NdnSim::DeserializeName (i));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700342
343 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700344 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700345
346 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700347 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700348
349 m_data->SetTimestamp (Seconds (i.ReadU32 ()));
350 m_data->SetFreshness (Seconds (i.ReadU16 ()));
351
352 if (i.ReadU16 () != 0) // Reserved
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700353 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700354 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700355 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700356
357 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700358 "Something wrong with Data::Deserialize");
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700359
360 return i.GetDistanceFrom (start);
361}
362
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700363void
364Data::Print (std::ostream &os) const
365{
366 m_data->Print (os);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700367}
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700368
369} // ndnSIM
370} // wire
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700371
372NDN_NAMESPACE_END