blob: 4668a7952d84b7baa80e94b4b7cd52711b0db03d [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 Afanasyevdfa69852013-12-05 15:50:44 -080090 Ptr<Packet> wire = packet->Copy ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -070091
92 Interest wireEncoding (interest);
93 packet->RemoveHeader (wireEncoding);
94
95 interest->SetPayload (packet);
Alexander Afanasyevdfa69852013-12-05 15:50:44 -080096 interest->SetWire (wire);
Alexander Afanasyevb989b122013-07-10 17:15:46 -070097
98 return interest;
99}
100
101uint32_t
102Interest::GetSerializedSize (void) const
103{
104 size_t size =
105 1/*version*/ + 1 /*type*/ + 2/*length*/ +
106 (4/*nonce*/ + 1/*scope*/ + 1/*nack type*/ + 2/*timestamp*/ +
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700107 NdnSim::SerializedSizeName (m_interest->GetName ()) +
Alexander Afanasyevabb493a2013-07-19 15:31:33 -0700108
109 (2 +
110 (m_interest->GetExclude () == 0 ? 0 : (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ())))
111 )/* selectors */ +
112
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700113 (2 + 0)/* options */);
114
115 NS_LOG_INFO ("Serialize size = " << size);
116 return size;
117}
118
119void
120Interest::Serialize (Buffer::Iterator start) const
121{
122 start.WriteU8 (0x80); // version
123 start.WriteU8 (0x00); // packet type
124
125 start.WriteU16 (GetSerializedSize () - 4);
126
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700127 start.WriteU32 (m_interest->GetNonce ());
128 start.WriteU8 (m_interest->GetScope ());
129 start.WriteU8 (m_interest->GetNack ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700130
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700131 NS_ASSERT_MSG (0 <= m_interest->GetInterestLifetime ().ToInteger (Time::S) && m_interest->GetInterestLifetime ().ToInteger (Time::S) < 65535,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700132 "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535");
133
134 // rounding timestamp value to seconds
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700135 start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700136
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700137 NdnSim::SerializeName (start, m_interest->GetName ());
Alexander Afanasyevabb493a2013-07-19 15:31:33 -0700138
139 if (m_interest->GetExclude () == 0)
140 {
141 start.WriteU16 (0); // no selectors
142 }
143 else
144 {
145 start.WriteU16 (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ()));
146 start.WriteU8 (0x01);
147 NdnSim::SerializeExclude (start, *m_interest->GetExclude ());
148 }
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700149
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700150 start.WriteU16 (0); // no options
151}
152
153uint32_t
154Interest::Deserialize (Buffer::Iterator start)
155{
156 Buffer::Iterator i = start;
157
158 if (i.ReadU8 () != 0x80)
159 throw new InterestException ();
160
161 if (i.ReadU8 () != 0x00)
162 throw new InterestException ();
163
164 i.ReadU16 (); // length, don't need it right now
165
166 m_interest->SetNonce (i.ReadU32 ());
167 m_interest->SetScope (i.ReadU8 ());
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700168 m_interest->SetNack (i.ReadU8 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700169
170 m_interest->SetInterestLifetime (Seconds (i.ReadU16 ()));
171
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700172 m_interest->SetName (NdnSim::DeserializeName (i));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700173
Alexander Afanasyevabb493a2013-07-19 15:31:33 -0700174 uint32_t selectorsLen = i.ReadU16 ();
175 if (selectorsLen > 0)
176 {
177 if (i.ReadU8 () != 0x01) // exclude filter only
178 throw InterestException ();
179
180 m_interest->SetExclude (NdnSim::DeserializeExclude (i));
181 }
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700182 i.ReadU16 ();
183
184 NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
185
186 return i.GetDistanceFrom (start);
187}
188
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700189void
190Interest::Print (std::ostream &os) const
191{
192 m_interest->Print (os);
193}
194
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700195///////////////////////////////////////////////////////////////////////////////////////////////////
196///////////////////////////////////////////////////////////////////////////////////////////////////
197///////////////////////////////////////////////////////////////////////////////////////////////////
198
199
200TypeId
201Data::GetTypeId (void)
202{
203 static TypeId tid = TypeId ("ns3::ndn::Data::ndnSIM")
204 .SetGroupName ("Ndn")
205 .SetParent<Header> ()
206 .AddConstructor<Data> ()
207 ;
208 return tid;
209}
210
211TypeId
212Data::GetInstanceTypeId (void) const
213{
214 return GetTypeId ();
215}
216
217
218Data::Data ()
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700219 : m_data (Create<ndn::Data> ())
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700220{
221}
222
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700223Data::Data (Ptr<ndn::Data> data)
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700224 : m_data (data)
225{
226}
227
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700228Ptr<ndn::Data>
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700229Data::GetData ()
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700230{
231 return m_data;
232}
233
234Ptr<Packet>
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700235Data::ToWire (Ptr<const ndn::Data> data)
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700236{
237 Ptr<const Packet> p = data->GetWire ();
238 if (!p)
239 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700240 Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700241 Data wireEncoding (ConstCast<ndn::Data> (data));
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700242 packet->AddHeader (wireEncoding);
243 data->SetWire (packet);
244
245 p = packet;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700246 }
247
248 return p->Copy ();
249}
250
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700251Ptr<ndn::Data>
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700252Data::FromWire (Ptr<Packet> packet)
253{
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700254 Ptr<ndn::Data> data = Create<ndn::Data> ();
Alexander Afanasyevdfa69852013-12-05 15:50:44 -0800255 Ptr<Packet> wire = packet->Copy ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700256
257 Data wireEncoding (data);
258 packet->RemoveHeader (wireEncoding);
259
260 data->SetPayload (packet);
Alexander Afanasyevdfa69852013-12-05 15:50:44 -0800261 data->SetWire (wire);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700262
263 return data;
264}
265
266uint32_t
267Data::GetSerializedSize () const
268{
269 uint32_t size = 1 + 1 + 2 +
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700270 ((2 + 2) +
271 NdnSim::SerializedSizeName (m_data->GetName ()) +
272 (2 + 2 + 4 + 2 + 2 + (2 + 0)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700273 if (m_data->GetSignature () != 0)
274 size += 4;
275
276 NS_LOG_INFO ("Serialize size = " << size);
277 return size;
278}
279
280void
281Data::Serialize (Buffer::Iterator start) const
282{
283 start.WriteU8 (0x80); // version
284 start.WriteU8 (0x01); // packet type
285 start.WriteU16 (GetSerializedSize () - 4); // length
286
287 if (m_data->GetSignature () != 0)
288 {
289 start.WriteU16 (6); // signature length
290 start.WriteU16 (0xFF00); // "fake" simulator signature
291 start.WriteU32 (m_data->GetSignature ());
292 }
293 else
294 {
295 start.WriteU16 (2); // signature length
296 start.WriteU16 (0); // empty signature
297 }
298
299 // name
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700300 NdnSim::SerializeName (start, m_data->GetName ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700301
302 // content
303 // for now assume that contentdata length is zero
304 start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0));
305 start.WriteU16 (4 + 2 + 2 + (2 + 0));
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700306 start.WriteU32 (static_cast<uint32_t> (m_data->GetTimestamp ().ToInteger (Time::S)));
307 start.WriteU16 (static_cast<uint16_t> (m_data->GetFreshness ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700308 start.WriteU16 (0); // reserved
309 start.WriteU16 (0); // Length (ContentInfoOptions)
310
311 // that's it folks
312}
313
314uint32_t
315Data::Deserialize (Buffer::Iterator start)
316{
317 Buffer::Iterator i = start;
318
319 if (i.ReadU8 () != 0x80)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700320 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700321
322 if (i.ReadU8 () != 0x01)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700323 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700324
325 i.ReadU16 (); // length
326
327 uint32_t signatureLength = i.ReadU16 ();
328 if (signatureLength == 6)
329 {
330 if (i.ReadU16 () != 0xFF00) // signature type
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700331 throw new DataException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700332 m_data->SetSignature (i.ReadU32 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700333 }
334 else if (signatureLength == 2)
335 {
336 if (i.ReadU16 () != 0) // signature type
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700337 throw new DataException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700338 m_data->SetSignature (0);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700339 }
340 else
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700341 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700342
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700343 m_data->SetName (NdnSim::DeserializeName (i));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700344
345 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700346 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700347
348 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700349 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700350
351 m_data->SetTimestamp (Seconds (i.ReadU32 ()));
352 m_data->SetFreshness (Seconds (i.ReadU16 ()));
353
354 if (i.ReadU16 () != 0) // Reserved
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700355 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700356 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700357 throw new DataException ();
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700358
359 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700360 "Something wrong with Data::Deserialize");
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700361
362 return i.GetDistanceFrom (start);
363}
364
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700365void
366Data::Print (std::ostream &os) const
367{
368 m_data->Print (os);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700369}
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700370
371} // ndnSIM
372} // wire
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700373
374NDN_NAMESPACE_END