blob: 2152d9911008d6dc1ab060200527332d740f6284 [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 *
6 * BSD license, See the doc/LICENSE file for more information
7 *
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
19NS_LOG_COMPONENT_DEFINE ("ndn.wire.ndnSIM");
20
Alexander Afanasyevb989b122013-07-10 17:15:46 -070021NDN_NAMESPACE_BEGIN
22
23namespace wire {
24namespace ndnSIM {
25
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070026NS_OBJECT_ENSURE_REGISTERED (Interest);
27NS_OBJECT_ENSURE_REGISTERED (Data);
28
Alexander Afanasyevb989b122013-07-10 17:15:46 -070029
30class Name
31{
32public:
33 Name ()
34 : m_name (Create<ndn::Name> ())
35 {
36 }
37
38 Name (Ptr<ndn::Name> name)
39 : m_name (name)
40 {
41 }
42
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070043 Ptr<ndn::Name>
Alexander Afanasyevb989b122013-07-10 17:15:46 -070044 GetName ()
45 {
46 return m_name;
47 }
48
49 size_t
50 GetSerializedSize () const
51 {
52 size_t nameSerializedSize = 2;
53
54 for (std::list<std::string>::const_iterator i = m_name->begin ();
55 i != m_name->end ();
56 i++)
57 {
58 nameSerializedSize += 2 + i->size ();
59 }
60 NS_ASSERT_MSG (nameSerializedSize < 30000, "Name is too long (> 30kbytes)");
61
62 return nameSerializedSize;
63 }
64
65 uint32_t
66 Serialize (Buffer::Iterator start) const
67 {
68 Buffer::Iterator i = start;
69
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070070 i.WriteU16 (static_cast<uint16_t> (GetSerializedSize ()-2));
Alexander Afanasyevb989b122013-07-10 17:15:46 -070071
72 for (std::list<std::string>::const_iterator item = m_name->begin ();
73 item != m_name->end ();
74 item++)
75 {
76 i.WriteU16 (static_cast<uint16_t> (item->size ()));
77 i.Write (reinterpret_cast<const uint8_t*> (item->c_str ()), item->size ());
78 }
79
80 return i.GetDistanceFrom (start);
81 }
82
83 uint32_t
84 Deserialize (Buffer::Iterator start)
85 {
86 Buffer::Iterator i = start;
87
88 uint16_t nameLength = i.ReadU16 ();
89 while (nameLength > 0)
90 {
91 uint16_t length = i.ReadU16 ();
92 nameLength = nameLength - 2 - length;
93
94 uint8_t tmp[length];
95 i.Read (tmp, length);
96
97 m_name->Add (string (reinterpret_cast<const char*> (tmp), length));
98 }
99
100 return i.GetDistanceFrom (start);
101 }
102
103private:
104 Ptr<ndn::Name> m_name;
105};
106
107///////////////////////////////////////////////////////////////////////////////////////////////////
108///////////////////////////////////////////////////////////////////////////////////////////////////
109///////////////////////////////////////////////////////////////////////////////////////////////////
110
111Interest::Interest ()
112 : m_interest (Create<ndn::Interest> ())
113{
114}
115
116Interest::Interest (Ptr<ndn::Interest> interest)
117 : m_interest (interest)
118{
119}
120
121Ptr<ndn::Interest>
122Interest::GetInterest ()
123{
124 return m_interest;
125}
126
127
128TypeId
129Interest::GetTypeId (void)
130{
131 static TypeId tid = TypeId ("ns3::ndn::Interest::ndnSIM")
132 .SetGroupName ("Ndn")
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700133 .SetParent<Header> ()
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700134 .AddConstructor<Interest> ()
135 ;
136 return tid;
137}
138
139TypeId
140Interest::GetInstanceTypeId (void) const
141{
142 return GetTypeId ();
143}
144
145Ptr<Packet>
146Interest::ToWire (Ptr<const ndn::Interest> interest)
147{
148 Ptr<const Packet> p = interest->GetWire ();
149 if (!p)
150 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700151 Ptr<Packet> packet = Create<Packet> (*interest->GetPayload ());
152 Interest wireEncoding (ConstCast<ndn::Interest> (interest));
153 packet->AddHeader (wireEncoding);
154 interest->SetWire (packet);
155
156 p = packet;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700157 }
158
159 return p->Copy ();
160}
161
162Ptr<ndn::Interest>
163Interest::FromWire (Ptr<Packet> packet)
164{
165 Ptr<ndn::Interest> interest = Create<ndn::Interest> ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700166 interest->SetWire (packet->Copy ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700167
168 Interest wireEncoding (interest);
169 packet->RemoveHeader (wireEncoding);
170
171 interest->SetPayload (packet);
172
173 return interest;
174}
175
176uint32_t
177Interest::GetSerializedSize (void) const
178{
179 size_t size =
180 1/*version*/ + 1 /*type*/ + 2/*length*/ +
181 (4/*nonce*/ + 1/*scope*/ + 1/*nack type*/ + 2/*timestamp*/ +
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700182 (Name (ConstCast<ndn::Name> (m_interest->GetNamePtr ())).GetSerializedSize ()) +
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700183 (2 + 0)/* selectors */ +
184 (2 + 0)/* options */);
185
186 NS_LOG_INFO ("Serialize size = " << size);
187 return size;
188}
189
190void
191Interest::Serialize (Buffer::Iterator start) const
192{
193 start.WriteU8 (0x80); // version
194 start.WriteU8 (0x00); // packet type
195
196 start.WriteU16 (GetSerializedSize () - 4);
197
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700198 start.WriteU32 (m_interest->GetNonce ());
199 start.WriteU8 (m_interest->GetScope ());
200 start.WriteU8 (m_interest->GetNack ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700201
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700202 NS_ASSERT_MSG (0 <= m_interest->GetInterestLifetime ().ToInteger (Time::S) && m_interest->GetInterestLifetime ().ToInteger (Time::S) < 65535,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700203 "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535");
204
205 // rounding timestamp value to seconds
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700206 start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700207
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700208 uint32_t offset = Name (ConstCast<ndn::Name> (m_interest->GetNamePtr ())).Serialize (start);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700209 start.Next (offset);
210
211 start.WriteU16 (0); // no selectors
212 start.WriteU16 (0); // no options
213}
214
215uint32_t
216Interest::Deserialize (Buffer::Iterator start)
217{
218 Buffer::Iterator i = start;
219
220 if (i.ReadU8 () != 0x80)
221 throw new InterestException ();
222
223 if (i.ReadU8 () != 0x00)
224 throw new InterestException ();
225
226 i.ReadU16 (); // length, don't need it right now
227
228 m_interest->SetNonce (i.ReadU32 ());
229 m_interest->SetScope (i.ReadU8 ());
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700230 m_interest->SetNack (i.ReadU8 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700231
232 m_interest->SetInterestLifetime (Seconds (i.ReadU16 ()));
233
234 Name name;
235 uint32_t offset = name.Deserialize (i);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700236 m_interest->SetName (name.GetName ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700237 i.Next (offset);
238
239 i.ReadU16 ();
240 i.ReadU16 ();
241
242 NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
243
244 return i.GetDistanceFrom (start);
245}
246
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700247void
248Interest::Print (std::ostream &os) const
249{
250 m_interest->Print (os);
251}
252
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700253///////////////////////////////////////////////////////////////////////////////////////////////////
254///////////////////////////////////////////////////////////////////////////////////////////////////
255///////////////////////////////////////////////////////////////////////////////////////////////////
256
257
258TypeId
259Data::GetTypeId (void)
260{
261 static TypeId tid = TypeId ("ns3::ndn::Data::ndnSIM")
262 .SetGroupName ("Ndn")
263 .SetParent<Header> ()
264 .AddConstructor<Data> ()
265 ;
266 return tid;
267}
268
269TypeId
270Data::GetInstanceTypeId (void) const
271{
272 return GetTypeId ();
273}
274
275
276Data::Data ()
277 : m_data (Create<ndn::ContentObject> ())
278{
279}
280
281Data::Data (Ptr<ndn::ContentObject> data)
282 : m_data (data)
283{
284}
285
286Ptr<ndn::ContentObject>
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700287Data::GetData ()
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700288{
289 return m_data;
290}
291
292Ptr<Packet>
293Data::ToWire (Ptr<const ndn::ContentObject> data)
294{
295 Ptr<const Packet> p = data->GetWire ();
296 if (!p)
297 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700298 Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
299 Data wireEncoding (ConstCast<ndn::ContentObject> (data));
300 packet->AddHeader (wireEncoding);
301 data->SetWire (packet);
302
303 p = packet;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700304 }
305
306 return p->Copy ();
307}
308
309Ptr<ndn::ContentObject>
310Data::FromWire (Ptr<Packet> packet)
311{
312 Ptr<ndn::ContentObject> data = Create<ndn::ContentObject> ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700313 data->SetWire (packet->Copy ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700314
315 Data wireEncoding (data);
316 packet->RemoveHeader (wireEncoding);
317
318 data->SetPayload (packet);
319
320 return data;
321}
322
323uint32_t
324Data::GetSerializedSize () const
325{
326 uint32_t size = 1 + 1 + 2 +
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700327 ((2 + 2) + (Name (ConstCast<ndn::Name> (m_data->GetNamePtr ())).GetSerializedSize ()) + (2 + 2 + 4 + 2 + 2 + (2 + 0)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700328 if (m_data->GetSignature () != 0)
329 size += 4;
330
331 NS_LOG_INFO ("Serialize size = " << size);
332 return size;
333}
334
335void
336Data::Serialize (Buffer::Iterator start) const
337{
338 start.WriteU8 (0x80); // version
339 start.WriteU8 (0x01); // packet type
340 start.WriteU16 (GetSerializedSize () - 4); // length
341
342 if (m_data->GetSignature () != 0)
343 {
344 start.WriteU16 (6); // signature length
345 start.WriteU16 (0xFF00); // "fake" simulator signature
346 start.WriteU32 (m_data->GetSignature ());
347 }
348 else
349 {
350 start.WriteU16 (2); // signature length
351 start.WriteU16 (0); // empty signature
352 }
353
354 // name
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700355 uint32_t offset = Name (ConstCast<ndn::Name> (m_data->GetNamePtr ())).Serialize (start);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700356 start.Next (offset);
357
358 // content
359 // for now assume that contentdata length is zero
360 start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0));
361 start.WriteU16 (4 + 2 + 2 + (2 + 0));
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700362 start.WriteU32 (static_cast<uint32_t> (m_data->GetTimestamp ().ToInteger (Time::S)));
363 start.WriteU16 (static_cast<uint16_t> (m_data->GetFreshness ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700364 start.WriteU16 (0); // reserved
365 start.WriteU16 (0); // Length (ContentInfoOptions)
366
367 // that's it folks
368}
369
370uint32_t
371Data::Deserialize (Buffer::Iterator start)
372{
373 Buffer::Iterator i = start;
374
375 if (i.ReadU8 () != 0x80)
376 throw new ContentObjectException ();
377
378 if (i.ReadU8 () != 0x01)
379 throw new ContentObjectException ();
380
381 i.ReadU16 (); // length
382
383 uint32_t signatureLength = i.ReadU16 ();
384 if (signatureLength == 6)
385 {
386 if (i.ReadU16 () != 0xFF00) // signature type
387 throw new ContentObjectException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700388 m_data->SetSignature (i.ReadU32 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700389 }
390 else if (signatureLength == 2)
391 {
392 if (i.ReadU16 () != 0) // signature type
393 throw new ContentObjectException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700394 m_data->SetSignature (0);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700395 }
396 else
397 throw new ContentObjectException ();
398
399 Name name;
400 uint32_t offset = name.Deserialize (i);
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700401 m_data->SetName (name.GetName ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700402 i.Next (offset);
403
404 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
405 throw new ContentObjectException ();
406
407 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
408 throw new ContentObjectException ();
409
410 m_data->SetTimestamp (Seconds (i.ReadU32 ()));
411 m_data->SetFreshness (Seconds (i.ReadU16 ()));
412
413 if (i.ReadU16 () != 0) // Reserved
414 throw new ContentObjectException ();
415 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
416 throw new ContentObjectException ();
417
418 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
419 "Something wrong with ContentObject::Deserialize");
420
421 return i.GetDistanceFrom (start);
422}
423
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700424void
425Data::Print (std::ostream &os) const
426{
427 m_data->Print (os);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700428}
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700429
430} // ndnSIM
431} // wire
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700432
433NDN_NAMESPACE_END