NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndnsim.cc
1 
2 /*
3  * Copyright (c) 2013, Regents of the University of California
4  * Alexander Afanasyev
5  *
6  * GNU 3.0 license, See the LICENSE file for more information
7  *
8  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9  */
10 
11 #include "ndnsim.h"
12 
13 using namespace std;
14 
15 #include <ns3/header.h>
16 #include <ns3/packet.h>
17 #include <ns3/log.h>
18 
19 #include "ndnsim/wire-ndnsim.h"
20 
21 NS_LOG_COMPONENT_DEFINE ("ndn.wire.ndnSIM");
22 
23 NDN_NAMESPACE_BEGIN
24 
25 namespace wire {
26 namespace ndnSIM {
27 
28 NS_OBJECT_ENSURE_REGISTERED (Interest);
29 NS_OBJECT_ENSURE_REGISTERED (Data);
30 
34 
35 Interest::Interest ()
36  : m_interest (Create<ndn::Interest> ())
37 {
38 }
39 
40 Interest::Interest (Ptr<ndn::Interest> interest)
41  : m_interest (interest)
42 {
43 }
44 
45 Ptr<ndn::Interest>
46 Interest::GetInterest ()
47 {
48  return m_interest;
49 }
50 
51 
52 TypeId
53 Interest::GetTypeId (void)
54 {
55  static TypeId tid = TypeId ("ns3::ndn::Interest::ndnSIM")
56  .SetGroupName ("Ndn")
57  .SetParent<Header> ()
58  .AddConstructor<Interest> ()
59  ;
60  return tid;
61 }
62 
63 TypeId
64 Interest::GetInstanceTypeId (void) const
65 {
66  return GetTypeId ();
67 }
68 
69 Ptr<Packet>
70 Interest::ToWire (Ptr<const ndn::Interest> interest)
71 {
72  Ptr<const Packet> p = interest->GetWire ();
73  if (!p)
74  {
75  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;
81  }
82 
83  return p->Copy ();
84 }
85 
86 Ptr<ndn::Interest>
87 Interest::FromWire (Ptr<Packet> packet)
88 {
89  Ptr<ndn::Interest> interest = Create<ndn::Interest> ();
90  Ptr<Packet> wire = packet->Copy ();
91 
92  Interest wireEncoding (interest);
93  packet->RemoveHeader (wireEncoding);
94 
95  interest->SetPayload (packet);
96  interest->SetWire (wire);
97 
98  return interest;
99 }
100 
101 uint32_t
102 Interest::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*/ +
107  NdnSim::SerializedSizeName (m_interest->GetName ()) +
108 
109  (2 +
110  (m_interest->GetExclude () == 0 ? 0 : (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ())))
111  )/* selectors */ +
112 
113  (2 + 0)/* options */);
114 
115  NS_LOG_INFO ("Serialize size = " << size);
116  return size;
117 }
118 
119 void
120 Interest::Serialize (Buffer::Iterator start) const
121 {
122  start.WriteU8 (0x80); // version
123  start.WriteU8 (0x00); // packet type
124 
125  start.WriteU16 (GetSerializedSize () - 4);
126 
127  start.WriteU32 (m_interest->GetNonce ());
128  start.WriteU8 (m_interest->GetScope ());
129  start.WriteU8 (m_interest->GetNack ());
130 
131  NS_ASSERT_MSG (0 <= m_interest->GetInterestLifetime ().ToInteger (Time::S) && m_interest->GetInterestLifetime ().ToInteger (Time::S) < 65535,
132  "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535");
133 
134  // rounding timestamp value to seconds
135  start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S)));
136 
137  NdnSim::SerializeName (start, m_interest->GetName ());
138 
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  }
149 
150  start.WriteU16 (0); // no options
151 }
152 
153 uint32_t
154 Interest::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 ());
168  m_interest->SetNack (i.ReadU8 ());
169 
170  m_interest->SetInterestLifetime (Seconds (i.ReadU16 ()));
171 
172  m_interest->SetName (NdnSim::DeserializeName (i));
173 
174  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  }
182  i.ReadU16 ();
183 
184  NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
185 
186  return i.GetDistanceFrom (start);
187 }
188 
189 void
190 Interest::Print (std::ostream &os) const
191 {
192  m_interest->Print (os);
193 }
194 
198 
199 
200 TypeId
201 Data::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 
211 TypeId
212 Data::GetInstanceTypeId (void) const
213 {
214  return GetTypeId ();
215 }
216 
217 
218 Data::Data ()
219  : m_data (Create<ndn::Data> ())
220 {
221 }
222 
223 Data::Data (Ptr<ndn::Data> data)
224  : m_data (data)
225 {
226 }
227 
228 Ptr<ndn::Data>
229 Data::GetData ()
230 {
231  return m_data;
232 }
233 
234 Ptr<Packet>
235 Data::ToWire (Ptr<const ndn::Data> data)
236 {
237  Ptr<const Packet> p = data->GetWire ();
238  if (!p)
239  {
240  Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
241  Data wireEncoding (ConstCast<ndn::Data> (data));
242  packet->AddHeader (wireEncoding);
243  data->SetWire (packet);
244 
245  p = packet;
246  }
247 
248  return p->Copy ();
249 }
250 
251 Ptr<ndn::Data>
252 Data::FromWire (Ptr<Packet> packet)
253 {
254  Ptr<ndn::Data> data = Create<ndn::Data> ();
255  Ptr<Packet> wire = packet->Copy ();
256 
257  Data wireEncoding (data);
258  packet->RemoveHeader (wireEncoding);
259 
260  data->SetPayload (packet);
261  data->SetWire (wire);
262 
263  return data;
264 }
265 
266 uint32_t
267 Data::GetSerializedSize () const
268 {
269  uint32_t size = 1 + 1 + 2 +
270  ((2 + 2) +
271  NdnSim::SerializedSizeName (m_data->GetName ()) +
272  (2 + 2 + 4 + 2 + 2 + (2 + 0)));
273  if (m_data->GetSignature () != 0)
274  size += 4;
275 
276  NS_LOG_INFO ("Serialize size = " << size);
277  return size;
278 }
279 
280 void
281 Data::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
300  NdnSim::SerializeName (start, m_data->GetName ());
301 
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));
306  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)));
308  start.WriteU16 (0); // reserved
309  start.WriteU16 (0); // Length (ContentInfoOptions)
310 
311  // that's it folks
312 }
313 
314 uint32_t
315 Data::Deserialize (Buffer::Iterator start)
316 {
317  Buffer::Iterator i = start;
318 
319  if (i.ReadU8 () != 0x80)
320  throw new DataException ();
321 
322  if (i.ReadU8 () != 0x01)
323  throw new DataException ();
324 
325  i.ReadU16 (); // length
326 
327  uint32_t signatureLength = i.ReadU16 ();
328  if (signatureLength == 6)
329  {
330  if (i.ReadU16 () != 0xFF00) // signature type
331  throw new DataException ();
332  m_data->SetSignature (i.ReadU32 ());
333  }
334  else if (signatureLength == 2)
335  {
336  if (i.ReadU16 () != 0) // signature type
337  throw new DataException ();
338  m_data->SetSignature (0);
339  }
340  else
341  throw new DataException ();
342 
343  m_data->SetName (NdnSim::DeserializeName (i));
344 
345  if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
346  throw new DataException ();
347 
348  if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
349  throw new DataException ();
350 
351  m_data->SetTimestamp (Seconds (i.ReadU32 ()));
352  m_data->SetFreshness (Seconds (i.ReadU16 ()));
353 
354  if (i.ReadU16 () != 0) // Reserved
355  throw new DataException ();
356  if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
357  throw new DataException ();
358 
359  NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
360  "Something wrong with Data::Deserialize");
361 
362  return i.GetDistanceFrom (start);
363 }
364 
365 void
366 Data::Print (std::ostream &os) const
367 {
368  m_data->Print (os);
369 }
370 
371 } // ndnSIM
372 } // wire
373 
374 NDN_NAMESPACE_END
Class for Interest parsing exception.
Definition: ndn-interest.h:271
Class for Data parsing exception.
Definition: ndn-data.h:203
Routines to serialize/deserialize Interest packet in ndnSIM format.
Definition: ndnsim.h:74
Routines to serialize/deserialize Data packet in ndnSIM format.
Definition: ndnsim.h:135