blob: 75aaf1ebe2b25f208ed618cdcdd336321f72ce8c [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 Afanasyevb989b122013-07-10 17:15:46 -0700107 (2 + 0)/* selectors */ +
108 (2 + 0)/* options */);
109
110 NS_LOG_INFO ("Serialize size = " << size);
111 return size;
112}
113
114void
115Interest::Serialize (Buffer::Iterator start) const
116{
117 start.WriteU8 (0x80); // version
118 start.WriteU8 (0x00); // packet type
119
120 start.WriteU16 (GetSerializedSize () - 4);
121
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700122 start.WriteU32 (m_interest->GetNonce ());
123 start.WriteU8 (m_interest->GetScope ());
124 start.WriteU8 (m_interest->GetNack ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700125
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700126 NS_ASSERT_MSG (0 <= m_interest->GetInterestLifetime ().ToInteger (Time::S) && m_interest->GetInterestLifetime ().ToInteger (Time::S) < 65535,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700127 "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535");
128
129 // rounding timestamp value to seconds
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700130 start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700131
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700132 NdnSim::SerializeName (start, m_interest->GetName ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700133
134 start.WriteU16 (0); // no selectors
135 start.WriteU16 (0); // no options
136}
137
138uint32_t
139Interest::Deserialize (Buffer::Iterator start)
140{
141 Buffer::Iterator i = start;
142
143 if (i.ReadU8 () != 0x80)
144 throw new InterestException ();
145
146 if (i.ReadU8 () != 0x00)
147 throw new InterestException ();
148
149 i.ReadU16 (); // length, don't need it right now
150
151 m_interest->SetNonce (i.ReadU32 ());
152 m_interest->SetScope (i.ReadU8 ());
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700153 m_interest->SetNack (i.ReadU8 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700154
155 m_interest->SetInterestLifetime (Seconds (i.ReadU16 ()));
156
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700157 m_interest->SetName (NdnSim::DeserializeName (i));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700158
159 i.ReadU16 ();
160 i.ReadU16 ();
161
162 NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
163
164 return i.GetDistanceFrom (start);
165}
166
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700167void
168Interest::Print (std::ostream &os) const
169{
170 m_interest->Print (os);
171}
172
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700173///////////////////////////////////////////////////////////////////////////////////////////////////
174///////////////////////////////////////////////////////////////////////////////////////////////////
175///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
178TypeId
179Data::GetTypeId (void)
180{
181 static TypeId tid = TypeId ("ns3::ndn::Data::ndnSIM")
182 .SetGroupName ("Ndn")
183 .SetParent<Header> ()
184 .AddConstructor<Data> ()
185 ;
186 return tid;
187}
188
189TypeId
190Data::GetInstanceTypeId (void) const
191{
192 return GetTypeId ();
193}
194
195
196Data::Data ()
197 : m_data (Create<ndn::ContentObject> ())
198{
199}
200
201Data::Data (Ptr<ndn::ContentObject> data)
202 : m_data (data)
203{
204}
205
206Ptr<ndn::ContentObject>
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700207Data::GetData ()
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700208{
209 return m_data;
210}
211
212Ptr<Packet>
213Data::ToWire (Ptr<const ndn::ContentObject> data)
214{
215 Ptr<const Packet> p = data->GetWire ();
216 if (!p)
217 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700218 Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
219 Data wireEncoding (ConstCast<ndn::ContentObject> (data));
220 packet->AddHeader (wireEncoding);
221 data->SetWire (packet);
222
223 p = packet;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700224 }
225
226 return p->Copy ();
227}
228
229Ptr<ndn::ContentObject>
230Data::FromWire (Ptr<Packet> packet)
231{
232 Ptr<ndn::ContentObject> data = Create<ndn::ContentObject> ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700233 data->SetWire (packet->Copy ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700234
235 Data wireEncoding (data);
236 packet->RemoveHeader (wireEncoding);
237
238 data->SetPayload (packet);
239
240 return data;
241}
242
243uint32_t
244Data::GetSerializedSize () const
245{
246 uint32_t size = 1 + 1 + 2 +
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700247 ((2 + 2) +
248 NdnSim::SerializedSizeName (m_data->GetName ()) +
249 (2 + 2 + 4 + 2 + 2 + (2 + 0)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700250 if (m_data->GetSignature () != 0)
251 size += 4;
252
253 NS_LOG_INFO ("Serialize size = " << size);
254 return size;
255}
256
257void
258Data::Serialize (Buffer::Iterator start) const
259{
260 start.WriteU8 (0x80); // version
261 start.WriteU8 (0x01); // packet type
262 start.WriteU16 (GetSerializedSize () - 4); // length
263
264 if (m_data->GetSignature () != 0)
265 {
266 start.WriteU16 (6); // signature length
267 start.WriteU16 (0xFF00); // "fake" simulator signature
268 start.WriteU32 (m_data->GetSignature ());
269 }
270 else
271 {
272 start.WriteU16 (2); // signature length
273 start.WriteU16 (0); // empty signature
274 }
275
276 // name
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700277 NdnSim::SerializeName (start, m_data->GetName ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700278
279 // content
280 // for now assume that contentdata length is zero
281 start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0));
282 start.WriteU16 (4 + 2 + 2 + (2 + 0));
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700283 start.WriteU32 (static_cast<uint32_t> (m_data->GetTimestamp ().ToInteger (Time::S)));
284 start.WriteU16 (static_cast<uint16_t> (m_data->GetFreshness ().ToInteger (Time::S)));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700285 start.WriteU16 (0); // reserved
286 start.WriteU16 (0); // Length (ContentInfoOptions)
287
288 // that's it folks
289}
290
291uint32_t
292Data::Deserialize (Buffer::Iterator start)
293{
294 Buffer::Iterator i = start;
295
296 if (i.ReadU8 () != 0x80)
297 throw new ContentObjectException ();
298
299 if (i.ReadU8 () != 0x01)
300 throw new ContentObjectException ();
301
302 i.ReadU16 (); // length
303
304 uint32_t signatureLength = i.ReadU16 ();
305 if (signatureLength == 6)
306 {
307 if (i.ReadU16 () != 0xFF00) // signature type
308 throw new ContentObjectException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700309 m_data->SetSignature (i.ReadU32 ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700310 }
311 else if (signatureLength == 2)
312 {
313 if (i.ReadU16 () != 0) // signature type
314 throw new ContentObjectException ();
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700315 m_data->SetSignature (0);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700316 }
317 else
318 throw new ContentObjectException ();
319
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700320 m_data->SetName (NdnSim::DeserializeName (i));
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700321
322 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0))) // content length
323 throw new ContentObjectException ();
324
325 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0))) // Length (content Info)
326 throw new ContentObjectException ();
327
328 m_data->SetTimestamp (Seconds (i.ReadU32 ()));
329 m_data->SetFreshness (Seconds (i.ReadU16 ()));
330
331 if (i.ReadU16 () != 0) // Reserved
332 throw new ContentObjectException ();
333 if (i.ReadU16 () != 0) // Length (ContentInfoOptions)
334 throw new ContentObjectException ();
335
336 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
337 "Something wrong with ContentObject::Deserialize");
338
339 return i.GetDistanceFrom (start);
340}
341
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700342void
343Data::Print (std::ostream &os) const
344{
345 m_data->Print (os);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700346}
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700347
348} // ndnSIM
349} // wire
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700350
351NDN_NAMESPACE_END