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 | * |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 6 | * GNU 3.0 license, See the LICENSE file for more information |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 19 | #include "ndnsim/wire-ndnsim.h" |
| 20 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 21 | NS_LOG_COMPONENT_DEFINE ("ndn.wire.ndnSIM"); |
| 22 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 23 | NDN_NAMESPACE_BEGIN |
| 24 | |
| 25 | namespace wire { |
| 26 | namespace ndnSIM { |
| 27 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 28 | NS_OBJECT_ENSURE_REGISTERED (Interest); |
| 29 | NS_OBJECT_ENSURE_REGISTERED (Data); |
| 30 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 31 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 32 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 33 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 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") |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 57 | .SetParent<Header> () |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 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 | { |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 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; |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 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> (); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 90 | interest->SetWire (packet->Copy ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 91 | |
| 92 | Interest wireEncoding (interest); |
| 93 | packet->RemoveHeader (wireEncoding); |
| 94 | |
| 95 | interest->SetPayload (packet); |
| 96 | |
| 97 | return interest; |
| 98 | } |
| 99 | |
| 100 | uint32_t |
| 101 | Interest::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 Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 106 | NdnSim::SerializedSizeName (m_interest->GetName ()) + |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 107 | (2 + 0)/* selectors */ + |
| 108 | (2 + 0)/* options */); |
| 109 | |
| 110 | NS_LOG_INFO ("Serialize size = " << size); |
| 111 | return size; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | Interest::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 122 | start.WriteU32 (m_interest->GetNonce ()); |
| 123 | start.WriteU8 (m_interest->GetScope ()); |
| 124 | start.WriteU8 (m_interest->GetNack ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 126 | 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] | 127 | "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535"); |
| 128 | |
| 129 | // rounding timestamp value to seconds |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 130 | start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S))); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 132 | NdnSim::SerializeName (start, m_interest->GetName ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 133 | |
| 134 | start.WriteU16 (0); // no selectors |
| 135 | start.WriteU16 (0); // no options |
| 136 | } |
| 137 | |
| 138 | uint32_t |
| 139 | Interest::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 153 | m_interest->SetNack (i.ReadU8 ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 154 | |
| 155 | m_interest->SetInterestLifetime (Seconds (i.ReadU16 ())); |
| 156 | |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 157 | m_interest->SetName (NdnSim::DeserializeName (i)); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 158 | |
| 159 | i.ReadU16 (); |
| 160 | i.ReadU16 (); |
| 161 | |
| 162 | NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start))); |
| 163 | |
| 164 | return i.GetDistanceFrom (start); |
| 165 | } |
| 166 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 167 | void |
| 168 | Interest::Print (std::ostream &os) const |
| 169 | { |
| 170 | m_interest->Print (os); |
| 171 | } |
| 172 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 173 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 174 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 175 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 176 | |
| 177 | |
| 178 | TypeId |
| 179 | Data::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 | |
| 189 | TypeId |
| 190 | Data::GetInstanceTypeId (void) const |
| 191 | { |
| 192 | return GetTypeId (); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | Data::Data () |
| 197 | : m_data (Create<ndn::ContentObject> ()) |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | Data::Data (Ptr<ndn::ContentObject> data) |
| 202 | : m_data (data) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | Ptr<ndn::ContentObject> |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 207 | Data::GetData () |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 208 | { |
| 209 | return m_data; |
| 210 | } |
| 211 | |
| 212 | Ptr<Packet> |
| 213 | Data::ToWire (Ptr<const ndn::ContentObject> data) |
| 214 | { |
| 215 | Ptr<const Packet> p = data->GetWire (); |
| 216 | if (!p) |
| 217 | { |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 218 | 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 Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return p->Copy (); |
| 227 | } |
| 228 | |
| 229 | Ptr<ndn::ContentObject> |
| 230 | Data::FromWire (Ptr<Packet> packet) |
| 231 | { |
| 232 | Ptr<ndn::ContentObject> data = Create<ndn::ContentObject> (); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 233 | data->SetWire (packet->Copy ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 234 | |
| 235 | Data wireEncoding (data); |
| 236 | packet->RemoveHeader (wireEncoding); |
| 237 | |
| 238 | data->SetPayload (packet); |
| 239 | |
| 240 | return data; |
| 241 | } |
| 242 | |
| 243 | uint32_t |
| 244 | Data::GetSerializedSize () const |
| 245 | { |
| 246 | uint32_t size = 1 + 1 + 2 + |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 247 | ((2 + 2) + |
| 248 | NdnSim::SerializedSizeName (m_data->GetName ()) + |
| 249 | (2 + 2 + 4 + 2 + 2 + (2 + 0))); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 250 | if (m_data->GetSignature () != 0) |
| 251 | size += 4; |
| 252 | |
| 253 | NS_LOG_INFO ("Serialize size = " << size); |
| 254 | return size; |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | Data::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 Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 277 | NdnSim::SerializeName (start, m_data->GetName ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 278 | |
| 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 283 | 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 Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 285 | start.WriteU16 (0); // reserved |
| 286 | start.WriteU16 (0); // Length (ContentInfoOptions) |
| 287 | |
| 288 | // that's it folks |
| 289 | } |
| 290 | |
| 291 | uint32_t |
| 292 | Data::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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 309 | m_data->SetSignature (i.ReadU32 ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 310 | } |
| 311 | else if (signatureLength == 2) |
| 312 | { |
| 313 | if (i.ReadU16 () != 0) // signature type |
| 314 | throw new ContentObjectException (); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 315 | m_data->SetSignature (0); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | throw new ContentObjectException (); |
| 319 | |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 320 | m_data->SetName (NdnSim::DeserializeName (i)); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 321 | |
| 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 Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 342 | void |
| 343 | Data::Print (std::ostream &os) const |
| 344 | { |
| 345 | m_data->Print (os); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 346 | } |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 347 | |
| 348 | } // ndnSIM |
| 349 | } // wire |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 350 | |
| 351 | NDN_NAMESPACE_END |