Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 1 | /** -*- 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 13 | using namespace std; |
| 14 | |
| 15 | #include <ns3/header.h> |
| 16 | #include <ns3/packet.h> |
| 17 | #include <ns3/log.h> |
| 18 | |
| 19 | NS_LOG_COMPONENT_DEFINE ("ndn.wire.ndnSIM"); |
| 20 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 21 | NDN_NAMESPACE_BEGIN |
| 22 | |
| 23 | namespace wire { |
| 24 | namespace ndnSIM { |
| 25 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 26 | NS_OBJECT_ENSURE_REGISTERED (Interest); |
| 27 | NS_OBJECT_ENSURE_REGISTERED (Data); |
| 28 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 29 | |
| 30 | class Name |
| 31 | { |
| 32 | public: |
| 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 43 | Ptr<ndn::Name> |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 44 | 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 70 | i.WriteU16 (static_cast<uint16_t> (GetSerializedSize ()-2)); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 71 | |
| 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 | |
| 103 | private: |
| 104 | Ptr<ndn::Name> m_name; |
| 105 | }; |
| 106 | |
| 107 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 108 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 109 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | Interest::Interest () |
| 112 | : m_interest (Create<ndn::Interest> ()) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | Interest::Interest (Ptr<ndn::Interest> interest) |
| 117 | : m_interest (interest) |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | Ptr<ndn::Interest> |
| 122 | Interest::GetInterest () |
| 123 | { |
| 124 | return m_interest; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | TypeId |
| 129 | Interest::GetTypeId (void) |
| 130 | { |
| 131 | static TypeId tid = TypeId ("ns3::ndn::Interest::ndnSIM") |
| 132 | .SetGroupName ("Ndn") |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 133 | .SetParent<Header> () |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 134 | .AddConstructor<Interest> () |
| 135 | ; |
| 136 | return tid; |
| 137 | } |
| 138 | |
| 139 | TypeId |
| 140 | Interest::GetInstanceTypeId (void) const |
| 141 | { |
| 142 | return GetTypeId (); |
| 143 | } |
| 144 | |
| 145 | Ptr<Packet> |
| 146 | Interest::ToWire (Ptr<const ndn::Interest> interest) |
| 147 | { |
| 148 | Ptr<const Packet> p = interest->GetWire (); |
| 149 | if (!p) |
| 150 | { |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 151 | 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 Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return p->Copy (); |
| 160 | } |
| 161 | |
| 162 | Ptr<ndn::Interest> |
| 163 | Interest::FromWire (Ptr<Packet> packet) |
| 164 | { |
| 165 | Ptr<ndn::Interest> interest = Create<ndn::Interest> (); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 166 | interest->SetWire (packet->Copy ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 167 | |
| 168 | Interest wireEncoding (interest); |
| 169 | packet->RemoveHeader (wireEncoding); |
| 170 | |
| 171 | interest->SetPayload (packet); |
| 172 | |
| 173 | return interest; |
| 174 | } |
| 175 | |
| 176 | uint32_t |
| 177 | Interest::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 182 | (Name (ConstCast<ndn::Name> (m_interest->GetNamePtr ())).GetSerializedSize ()) + |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 183 | (2 + 0)/* selectors */ + |
| 184 | (2 + 0)/* options */); |
| 185 | |
| 186 | NS_LOG_INFO ("Serialize size = " << size); |
| 187 | return size; |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | Interest::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 198 | start.WriteU32 (m_interest->GetNonce ()); |
| 199 | start.WriteU8 (m_interest->GetScope ()); |
| 200 | start.WriteU8 (m_interest->GetNack ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 201 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 202 | NS_ASSERT_MSG (0 <= m_interest->GetInterestLifetime ().ToInteger (Time::S) && m_interest->GetInterestLifetime ().ToInteger (Time::S) < 65535, |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 203 | "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535"); |
| 204 | |
| 205 | // rounding timestamp value to seconds |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 206 | start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S))); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 207 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 208 | uint32_t offset = Name (ConstCast<ndn::Name> (m_interest->GetNamePtr ())).Serialize (start); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 209 | start.Next (offset); |
| 210 | |
| 211 | start.WriteU16 (0); // no selectors |
| 212 | start.WriteU16 (0); // no options |
| 213 | } |
| 214 | |
| 215 | uint32_t |
| 216 | Interest::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 230 | m_interest->SetNack (i.ReadU8 ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 231 | |
| 232 | m_interest->SetInterestLifetime (Seconds (i.ReadU16 ())); |
| 233 | |
| 234 | Name name; |
| 235 | uint32_t offset = name.Deserialize (i); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 236 | m_interest->SetName (name.GetName ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 237 | 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 247 | void |
| 248 | Interest::Print (std::ostream &os) const |
| 249 | { |
| 250 | m_interest->Print (os); |
| 251 | } |
| 252 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 253 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 254 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 255 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 256 | |
| 257 | |
| 258 | TypeId |
| 259 | Data::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 | |
| 269 | TypeId |
| 270 | Data::GetInstanceTypeId (void) const |
| 271 | { |
| 272 | return GetTypeId (); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | Data::Data () |
| 277 | : m_data (Create<ndn::ContentObject> ()) |
| 278 | { |
| 279 | } |
| 280 | |
| 281 | Data::Data (Ptr<ndn::ContentObject> data) |
| 282 | : m_data (data) |
| 283 | { |
| 284 | } |
| 285 | |
| 286 | Ptr<ndn::ContentObject> |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 287 | Data::GetData () |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 288 | { |
| 289 | return m_data; |
| 290 | } |
| 291 | |
| 292 | Ptr<Packet> |
| 293 | Data::ToWire (Ptr<const ndn::ContentObject> data) |
| 294 | { |
| 295 | Ptr<const Packet> p = data->GetWire (); |
| 296 | if (!p) |
| 297 | { |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 298 | 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 Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return p->Copy (); |
| 307 | } |
| 308 | |
| 309 | Ptr<ndn::ContentObject> |
| 310 | Data::FromWire (Ptr<Packet> packet) |
| 311 | { |
| 312 | Ptr<ndn::ContentObject> data = Create<ndn::ContentObject> (); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 313 | data->SetWire (packet->Copy ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 314 | |
| 315 | Data wireEncoding (data); |
| 316 | packet->RemoveHeader (wireEncoding); |
| 317 | |
| 318 | data->SetPayload (packet); |
| 319 | |
| 320 | return data; |
| 321 | } |
| 322 | |
| 323 | uint32_t |
| 324 | Data::GetSerializedSize () const |
| 325 | { |
| 326 | uint32_t size = 1 + 1 + 2 + |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 327 | ((2 + 2) + (Name (ConstCast<ndn::Name> (m_data->GetNamePtr ())).GetSerializedSize ()) + (2 + 2 + 4 + 2 + 2 + (2 + 0))); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 328 | if (m_data->GetSignature () != 0) |
| 329 | size += 4; |
| 330 | |
| 331 | NS_LOG_INFO ("Serialize size = " << size); |
| 332 | return size; |
| 333 | } |
| 334 | |
| 335 | void |
| 336 | Data::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 355 | uint32_t offset = Name (ConstCast<ndn::Name> (m_data->GetNamePtr ())).Serialize (start); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 356 | 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 362 | 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 Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 364 | start.WriteU16 (0); // reserved |
| 365 | start.WriteU16 (0); // Length (ContentInfoOptions) |
| 366 | |
| 367 | // that's it folks |
| 368 | } |
| 369 | |
| 370 | uint32_t |
| 371 | Data::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 388 | m_data->SetSignature (i.ReadU32 ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 389 | } |
| 390 | else if (signatureLength == 2) |
| 391 | { |
| 392 | if (i.ReadU16 () != 0) // signature type |
| 393 | throw new ContentObjectException (); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 394 | m_data->SetSignature (0); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 395 | } |
| 396 | else |
| 397 | throw new ContentObjectException (); |
| 398 | |
| 399 | Name name; |
| 400 | uint32_t offset = name.Deserialize (i); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 401 | m_data->SetName (name.GetName ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 402 | 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 424 | void |
| 425 | Data::Print (std::ostream &os) const |
| 426 | { |
| 427 | m_data->Print (os); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 428 | } |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 429 | |
| 430 | } // ndnSIM |
| 431 | } // wire |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 432 | |
| 433 | NDN_NAMESPACE_END |