A Discrete-Event Network Simulator
API
packet.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "packet.h"
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 #include "ns3/simulator.h"
24 #include <string>
25 #include <cstdarg>
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("Packet");
30 
31 uint32_t Packet::m_globalUid = 0;
32 
33 TypeId
35 {
36  return m_tid;
37 }
38 uint32_t
40 {
41  return m_start;
42 }
43 uint32_t
45 {
46  return m_end;
47 }
48 void
50 {
51  if (tag.GetInstanceTypeId () != GetTypeId ())
52  {
53  NS_FATAL_ERROR ("The tag you provided is not of the right type.");
54  }
55  tag.Deserialize (m_buffer);
56 }
57 ByteTagIterator::Item::Item (TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer)
58  : m_tid (tid),
59  m_start (start),
60  m_end (end),
61  m_buffer (buffer)
62 {
63 }
64 bool
66 {
67  return m_current.HasNext ();
68 }
71 {
73  return ByteTagIterator::Item (i.tid,
76  i.buf);
77 }
79  : m_current (i)
80 {
81 }
82 
83 
85  : m_current (head)
86 {
87 }
88 bool
90 {
91  return m_current != 0;
92 }
95 {
96  NS_ASSERT (HasNext ());
97  const struct PacketTagList::TagData *prev = m_current;
99  return PacketTagIterator::Item (prev);
100 }
101 
103  : m_data (data)
104 {
105 }
106 TypeId
108 {
109  return m_data->tid;
110 }
111 void
113 {
114  NS_ASSERT (tag.GetInstanceTypeId () == m_data->tid);
115  tag.Deserialize (TagBuffer ((uint8_t*)m_data->data,
116  (uint8_t*)m_data->data + m_data->size));
117 }
118 
119 
121 Packet::Copy (void) const
122 {
123  // we need to invoke the copy constructor directly
124  // rather than calling Create because the copy constructor
125  // is private.
126  return Ptr<Packet> (new Packet (*this), false);
127 }
128 
130  : m_buffer (),
131  m_byteTagList (),
132  m_packetTagList (),
133  /* The upper 32 bits of the packet id in
134  * metadata is for the system id. For non-
135  * distributed simulations, this is simply
136  * zero. The lower 32 bits are for the
137  * global UID
138  */
139  m_metadata (static_cast<uint64_t> (Simulator::GetSystemId ()) << 32 | m_globalUid, 0),
140  m_nixVector (0)
141 {
142  m_globalUid++;
143 }
144 
146  : m_buffer (o.m_buffer),
147  m_byteTagList (o.m_byteTagList),
148  m_packetTagList (o.m_packetTagList),
149  m_metadata (o.m_metadata)
150 {
152  : m_nixVector = 0;
153 }
154 
155 Packet &
157 {
158  if (this == &o)
159  {
160  return *this;
161  }
162  m_buffer = o.m_buffer;
167  : m_nixVector = 0;
168  return *this;
169 }
170 
171 Packet::Packet (uint32_t size)
172  : m_buffer (size),
173  m_byteTagList (),
174  m_packetTagList (),
175  /* The upper 32 bits of the packet id in
176  * metadata is for the system id. For non-
177  * distributed simulations, this is simply
178  * zero. The lower 32 bits are for the
179  * global UID
180  */
181  m_metadata (static_cast<uint64_t> (Simulator::GetSystemId ()) << 32 | m_globalUid, size),
182  m_nixVector (0)
183 {
184  m_globalUid++;
185 }
186 Packet::Packet (uint8_t const *buffer, uint32_t size, bool magic)
187  : m_buffer (0, false),
188  m_byteTagList (),
189  m_packetTagList (),
190  m_metadata (0,0),
191  m_nixVector (0)
192 {
193  NS_ASSERT (magic);
194  Deserialize (buffer, size);
195 }
196 
197 Packet::Packet (uint8_t const*buffer, uint32_t size)
198  : m_buffer (),
199  m_byteTagList (),
200  m_packetTagList (),
201  /* The upper 32 bits of the packet id in
202  * metadata is for the system id. For non-
203  * distributed simulations, this is simply
204  * zero. The lower 32 bits are for the
205  * global UID
206  */
207  m_metadata (static_cast<uint64_t> (Simulator::GetSystemId ()) << 32 | m_globalUid, size),
208  m_nixVector (0)
209 {
210  m_globalUid++;
211  m_buffer.AddAtStart (size);
213  i.Write (buffer, size);
214 }
215 
216 Packet::Packet (const Buffer &buffer, const ByteTagList &byteTagList,
217  const PacketTagList &packetTagList, const PacketMetadata &metadata)
218  : m_buffer (buffer),
219  m_byteTagList (byteTagList),
220  m_packetTagList (packetTagList),
221  m_metadata (metadata),
222  m_nixVector (0)
223 {
224 }
225 
227 Packet::CreateFragment (uint32_t start, uint32_t length) const
228 {
229  NS_LOG_FUNCTION (this << start << length);
230  Buffer buffer = m_buffer.CreateFragment (start, length);
231  ByteTagList byteTagList = m_byteTagList;
232  byteTagList.Adjust (-start);
233  NS_ASSERT (m_buffer.GetSize () >= start + length);
234  uint32_t end = m_buffer.GetSize () - (start + length);
235  PacketMetadata metadata = m_metadata.CreateFragment (start, end);
236  // again, call the constructor directly rather than
237  // through Create because it is private.
238  Ptr<Packet> ret = Ptr<Packet> (new Packet (buffer, byteTagList, m_packetTagList, metadata), false);
239  ret->SetNixVector (GetNixVector ());
240  return ret;
241 }
242 
243 void
245 {
246  m_nixVector = nixVector;
247 }
248 
251 {
252  return m_nixVector;
253 }
254 
255 void
256 Packet::AddHeader (const Header &header)
257 {
258  uint32_t size = header.GetSerializedSize ();
259  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << size);
260  m_buffer.AddAtStart (size);
261  m_byteTagList.Adjust (size);
262  m_byteTagList.AddAtStart (size);
263  header.Serialize (m_buffer.Begin ());
264  m_metadata.AddHeader (header, size);
265 }
266 uint32_t
267 Packet::RemoveHeader (Header &header, uint32_t size)
268 {
269  Buffer::Iterator end;
270  end = m_buffer.Begin ();
271  end.Next (size);
272  uint32_t deserialized = header.Deserialize (m_buffer.Begin (), end);
273  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
274  m_buffer.RemoveAtStart (deserialized);
275  m_byteTagList.Adjust (-deserialized);
276  m_metadata.RemoveHeader (header, deserialized);
277  return deserialized;
278 }
279 uint32_t
281 {
282  uint32_t deserialized = header.Deserialize (m_buffer.Begin ());
283  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
284  m_buffer.RemoveAtStart (deserialized);
285  m_byteTagList.Adjust (-deserialized);
286  m_metadata.RemoveHeader (header, deserialized);
287  return deserialized;
288 }
289 uint32_t
290 Packet::PeekHeader (Header &header) const
291 {
292  uint32_t deserialized = header.Deserialize (m_buffer.Begin ());
293  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
294  return deserialized;
295 }
296 uint32_t
297 Packet::PeekHeader (Header &header, uint32_t size) const
298 {
299  Buffer::Iterator end;
300  end = m_buffer.Begin ();
301  end.Next (size);
302  uint32_t deserialized = header.Deserialize (m_buffer.Begin (), end);
303  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
304  return deserialized;
305 }
306 void
307 Packet::AddTrailer (const Trailer &trailer)
308 {
309  uint32_t size = trailer.GetSerializedSize ();
310  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << size);
312  m_buffer.AddAtEnd (size);
313  Buffer::Iterator end = m_buffer.End ();
314  trailer.Serialize (end);
315  m_metadata.AddTrailer (trailer, size);
316 }
317 uint32_t
319 {
320  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
321  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
322  m_buffer.RemoveAtEnd (deserialized);
323  m_metadata.RemoveTrailer (trailer, deserialized);
324  return deserialized;
325 }
326 uint32_t
328 {
329  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
330  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
331  return deserialized;
332 }
333 
334 void
336 {
337  NS_LOG_FUNCTION (this << packet << packet->GetSize ());
339  ByteTagList copy = packet->m_byteTagList;
340  copy.AddAtStart (0);
341  copy.Adjust (GetSize ());
342  m_byteTagList.Add (copy);
343  m_buffer.AddAtEnd (packet->m_buffer);
344  m_metadata.AddAtEnd (packet->m_metadata);
345 }
346 void
347 Packet::AddPaddingAtEnd (uint32_t size)
348 {
349  NS_LOG_FUNCTION (this << size);
351  m_buffer.AddAtEnd (size);
353 }
354 void
355 Packet::RemoveAtEnd (uint32_t size)
356 {
357  NS_LOG_FUNCTION (this << size);
358  m_buffer.RemoveAtEnd (size);
359  m_metadata.RemoveAtEnd (size);
360 }
361 void
362 Packet::RemoveAtStart (uint32_t size)
363 {
364  NS_LOG_FUNCTION (this << size);
365  m_buffer.RemoveAtStart (size);
366  m_byteTagList.Adjust (-size);
367  m_metadata.RemoveAtStart (size);
368 }
369 
370 void
372 {
373  NS_LOG_FUNCTION (this);
375 }
376 
377 uint32_t
378 Packet::CopyData (uint8_t *buffer, uint32_t size) const
379 {
380  return m_buffer.CopyData (buffer, size);
381 }
382 
383 void
384 Packet::CopyData (std::ostream *os, uint32_t size) const
385 {
386  return m_buffer.CopyData (os, size);
387 }
388 
389 uint64_t
390 Packet::GetUid (void) const
391 {
392  return m_metadata.GetUid ();
393 }
394 
395 void
396 Packet::PrintByteTags (std::ostream &os) const
397 {
399  while (i.HasNext ())
400  {
401  ByteTagIterator::Item item = i.Next ();
402  os << item.GetTypeId ().GetName () << " [" << item.GetStart () << "-" << item.GetEnd () << "]";
403  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
404  if (constructor.IsNull ())
405  {
406  if (i.HasNext ())
407  {
408  os << " ";
409  }
410  continue;
411  }
412  Tag *tag = dynamic_cast<Tag *> (constructor ());
413  NS_ASSERT (tag != 0);
414  os << " ";
415  item.GetTag (*tag);
416  tag->Print (os);
417  if (i.HasNext ())
418  {
419  os << " ";
420  }
421  delete tag;
422  }
423 }
424 
425 std::string
427 {
428  std::ostringstream oss;
429  Print (oss);
430  return oss.str();
431 }
432 
433 void
434 Packet::Print (std::ostream &os) const
435 {
437  while (i.HasNext ())
438  {
439  PacketMetadata::Item item = i.Next ();
440  if (item.isFragment)
441  {
442  switch (item.type) {
444  os << "Payload";
445  break;
448  os << item.tid.GetName ();
449  break;
450  }
451  os << " Fragment [" << item.currentTrimedFromStart<<":"
452  << (item.currentTrimedFromStart + item.currentSize) << "]";
453  }
454  else
455  {
456  switch (item.type) {
458  os << "Payload (size=" << item.currentSize << ")";
459  break;
462  os << item.tid.GetName () << " (";
463  {
464  NS_ASSERT (item.tid.HasConstructor ());
465  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
466  NS_ASSERT (!constructor.IsNull ());
467  ObjectBase *instance = constructor ();
468  NS_ASSERT (instance != 0);
469  Chunk *chunk = dynamic_cast<Chunk *> (instance);
470  NS_ASSERT (chunk != 0);
472  {
473  Buffer::Iterator end = item.current;
474  end.Next (item.currentSize); // move from start
475  chunk->Deserialize (item.current, end);
476  }
477  else if (item.type == PacketMetadata::Item::TRAILER)
478  {
480  start.Prev (item.currentSize); // move from end
481  chunk->Deserialize (start, item.current);
482  }
483  else
484  {
485  chunk->Deserialize (item.current);
486  }
487  chunk->Print (os);
488  delete chunk;
489  }
490  os << ")";
491  break;
492  }
493  }
494  if (i.HasNext ())
495  {
496  os << " ";
497  }
498  }
499 #if 0
500  // The code below will work only if headers and trailers
501  // define the right attributes which is not the case for
502  // now. So, as a temporary measure, we use the
503  // headers' and trailers' Print method as shown above.
505  while (i.HasNext ())
506  {
507  PacketMetadata::Item item = i.Next ();
508  if (item.isFragment)
509  {
510  switch (item.type) {
512  os << "Payload";
513  break;
516  os << item.tid.GetName ();
517  break;
518  }
519  os << " Fragment [" << item.currentTrimedFromStart<<":"
520  << (item.currentTrimedFromStart + item.currentSize) << "]";
521  }
522  else
523  {
524  switch (item.type) {
526  os << "Payload (size=" << item.currentSize << ")";
527  break;
530  os << item.tid.GetName () << "(";
531  {
532  NS_ASSERT (item.tid.HasConstructor ());
533  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
534  NS_ASSERT (constructor.IsNull ());
535  ObjectBase *instance = constructor ();
536  NS_ASSERT (instance != 0);
537  Chunk *chunk = dynamic_cast<Chunk *> (instance);
538  NS_ASSERT (chunk != 0);
539  chunk->Deserialize (item.current);
540  for (uint32_t j = 0; j < item.tid.GetAttributeN (); j++)
541  {
542  std::string attrName = item.tid.GetAttributeName (j);
543  std::string value;
544  bool ok = chunk->GetAttribute (attrName, value);
545  NS_ASSERT (ok);
546  os << attrName << "=" << value;
547  if ((j + 1) < item.tid.GetAttributeN ())
548  {
549  os << ",";
550  }
551  }
552  }
553  os << ")";
554  break;
555  }
556  }
557  if (i.HasNext ())
558  {
559  os << " ";
560  }
561  }
562 #endif
563 }
564 
566 Packet::BeginItem (void) const
567 {
568  return m_metadata.BeginItem (m_buffer);
569 }
570 
571 void
573 {
576 }
577 
578 void
580 {
583 }
584 
585 uint32_t Packet::GetSerializedSize (void) const
586 {
587  uint32_t size = 0;
588 
589  if (m_nixVector)
590  {
591  // increment total size by the size of the nix-vector
592  // ensuring 4-byte boundary
593  size += ((m_nixVector->GetSerializedSize () + 3) & (~3));
594 
595  // add 4-bytes for entry of total length of nix-vector
596  size += 4;
597  }
598  else
599  {
600  // if no nix-vector, still have to add 4-bytes
601  // to account for the entry of total size for
602  // nix-vector in the buffer
603  size += 4;
604  }
605 
606  //Tag size
608  //size += m_tags.GetSerializedSize ();
609 
610  // increment total size by size of meta-data
611  // ensuring 4-byte boundary
612  size += ((m_metadata.GetSerializedSize () + 3) & (~3));
613 
614  // add 4-bytes for entry of total length of meta-data
615  size += 4;
616 
617  // increment total size by size of buffer
618  // ensuring 4-byte boundary
619  size += ((m_buffer.GetSerializedSize () + 3) & (~3));
620 
621  // add 4-bytes for entry of total length of buffer
622  size += 4;
623 
624  return size;
625 }
626 
627 uint32_t
628 Packet::Serialize (uint8_t* buffer, uint32_t maxSize) const
629 {
630  uint32_t* p = reinterpret_cast<uint32_t *> (buffer);
631  uint32_t size = 0;
632 
633  // if nix-vector exists, serialize it
634  if (m_nixVector)
635  {
636  uint32_t nixSize = m_nixVector->GetSerializedSize ();
637  if (size + nixSize <= maxSize)
638  {
639  // put the total length of nix-vector in the
640  // buffer. this includes 4-bytes for total
641  // length itself
642  *p++ = nixSize + 4;
643  size += nixSize;
644 
645  // serialize the nix-vector
646  uint32_t serialized =
647  m_nixVector->Serialize (p, nixSize);
648  if (serialized)
649  {
650  // increment p by nixSize bytes
651  // ensuring 4-byte boundary
652  p += ((nixSize+3) & (~3)) / 4;
653  }
654  else
655  {
656  return 0;
657  }
658  }
659  else
660  {
661  return 0;
662  }
663  }
664  else
665  {
666  // no nix vector, set zero length,
667  // ie 4-bytes, since it must include
668  // length for itself
669  if (size + 4 <= maxSize)
670  {
671  size += 4;
672  *p++ = 4;
673  }
674  else
675  {
676  return 0;
677  }
678  }
679 
680  // Serialize Tags
682 
683  // Serialize Metadata
684  uint32_t metaSize = m_metadata.GetSerializedSize ();
685  if (size + metaSize <= maxSize)
686  {
687  // put the total length of metadata in the
688  // buffer. this includes 4-bytes for total
689  // length itself
690  *p++ = metaSize + 4;
691  size += metaSize;
692 
693  // serialize the metadata
694  uint32_t serialized = m_metadata.Serialize (reinterpret_cast<uint8_t *> (p), metaSize);
695  if (serialized)
696  {
697  // increment p by metaSize bytes
698  // ensuring 4-byte boundary
699  p += ((metaSize+3) & (~3)) / 4;
700  }
701  else
702  {
703  return 0;
704  }
705  }
706  else
707  {
708  return 0;
709  }
710 
711  // Serialize the packet contents
712  uint32_t bufSize = m_buffer.GetSerializedSize ();
713  if (size + bufSize <= maxSize)
714  {
715  // put the total length of the buffer in the
716  // buffer. this includes 4-bytes for total
717  // length itself
718  *p++ = bufSize + 4;
719 
720  // serialize the buffer
721  uint32_t serialized = m_buffer.Serialize (reinterpret_cast<uint8_t *> (p), bufSize);
722  if (!serialized)
723  {
724  return 0;
725  }
726  }
727  else
728  {
729  return 0;
730  }
731 
732  // Serialized successfully
733  return 1;
734 }
735 
736 uint32_t
737 Packet::Deserialize (const uint8_t* buffer, uint32_t size)
738 {
739  NS_LOG_FUNCTION (this);
740 
741  const uint32_t* p = reinterpret_cast<const uint32_t *> (buffer);
742 
743  // read nix-vector
745  uint32_t nixSize = *p++;
746 
747  // if size less than nixSize, the buffer
748  // will be overrun, assert
749  NS_ASSERT (size >= nixSize);
750 
751  size -= nixSize;
752 
753  if (nixSize > 4)
754  {
755  Ptr<NixVector> nix = Create<NixVector> ();
756  uint32_t nixDeserialized = nix->Deserialize (p, nixSize);
757  if (!nixDeserialized)
758  {
759  // nix-vector not deserialized
760  // completely
761  return 0;
762  }
763  m_nixVector = nix;
764  // increment p by nixSize ensuring
765  // 4-byte boundary
766  p += ((((nixSize - 4) + 3) & (~3)) / 4);
767  }
768 
769  // read tags
771  //uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ());
772  //buffer.RemoveAtStart (tagsDeserialized);
773 
774  // read metadata
775  uint32_t metaSize = *p++;
776 
777  // if size less than metaSize, the buffer
778  // will be overrun, assert
779  NS_ASSERT (size >= metaSize);
780 
781  size -= metaSize;
782 
783  uint32_t metadataDeserialized =
784  m_metadata.Deserialize (reinterpret_cast<const uint8_t *> (p), metaSize);
785  if (!metadataDeserialized)
786  {
787  // meta-data not deserialized
788  // completely
789  return 0;
790  }
791  // increment p by metaSize ensuring
792  // 4-byte boundary
793  p += ((((metaSize - 4) + 3) & (~3)) / 4);
794 
795  // read buffer contents
796  uint32_t bufSize = *p++;
797 
798  // if size less than bufSize, the buffer
799  // will be overrun, assert
800  NS_ASSERT (size >= bufSize);
801 
802  size -= bufSize;
803 
804  uint32_t bufferDeserialized =
805  m_buffer.Deserialize (reinterpret_cast<const uint8_t *> (p), bufSize);
806  if (!bufferDeserialized)
807  {
808  // buffer not deserialized
809  // completely
810  return 0;
811  }
812 
813  // return zero if did not deserialize the
814  // number of expected bytes
815  return (size == 0);
816 }
817 
818 void
819 Packet::AddByteTag (const Tag &tag) const
820 {
821  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
822  ByteTagList *list = const_cast<ByteTagList *> (&m_byteTagList);
823  TagBuffer buffer = list->Add (tag.GetInstanceTypeId (), tag.GetSerializedSize (),
824  0,
825  GetSize ());
826  tag.Serialize (buffer);
827 }
828 void
829 Packet::AddByteTag (const Tag &tag, uint32_t start, uint32_t end) const
830 {
831  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
832  NS_ABORT_MSG_IF (end < start, "Invalid byte range");
833  ByteTagList *list = const_cast<ByteTagList *> (&m_byteTagList);
834  TagBuffer buffer = list->Add (tag.GetInstanceTypeId (), tag.GetSerializedSize (),
835  static_cast<int32_t> (start),
836  static_cast<int32_t> (end));
837  tag.Serialize (buffer);
838 }
841 {
842  return ByteTagIterator (m_byteTagList.Begin (0, GetSize ()));
843 }
844 
845 bool
847 {
848  TypeId tid = tag.GetInstanceTypeId ();
850  while (i.HasNext ())
851  {
852  ByteTagIterator::Item item = i.Next ();
853  if (tid == item.GetTypeId ())
854  {
855  item.GetTag (tag);
856  return true;
857  }
858  }
859  return false;
860 }
861 
862 void
863 Packet::AddPacketTag (const Tag &tag) const
864 {
865  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
866  m_packetTagList.Add (tag);
867 }
868 
869 bool
871 {
872  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
873  bool found = m_packetTagList.Remove (tag);
874  return found;
875 }
876 bool
878 {
879  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
880  bool found = m_packetTagList.Replace (tag);
881  return found;
882 }
883 
884 bool
886 {
887  bool found = m_packetTagList.Peek (tag);
888  return found;
889 }
890 void
892 {
893  NS_LOG_FUNCTION (this);
895 }
896 
897 void
898 Packet::PrintPacketTags (std::ostream &os) const
899 {
901  while (i.HasNext ())
902  {
903  PacketTagIterator::Item item = i.Next ();
904  NS_ASSERT (item.GetTypeId ().HasConstructor ());
905  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
906  NS_ASSERT (!constructor.IsNull ());
907  ObjectBase *instance = constructor ();
908  Tag *tag = dynamic_cast<Tag *> (instance);
909  NS_ASSERT (tag != 0);
910  item.GetTag (*tag);
911  tag->Print (os);
912  delete tag;
913  if (i.HasNext ())
914  {
915  os << " ";
916  }
917  }
918 }
919 
922 {
924 }
925 
926 std::ostream& operator<< (std::ostream& os, const Packet &packet)
927 {
928  packet.Print (os);
929  return os;
930 }
931 
932 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
void RemoveAtEnd(uint32_t end)
Remove a chunk of metadata at the metadata end.
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:846
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
PacketMetadata m_metadata
the packet&#39;s metadata
Definition: packet.h:785
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1053
TypeId GetTypeId(void) const
Definition: packet.cc:34
std::string GetName(void) const
Get the name.
Definition: type-id.cc:969
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:434
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
bool HasConstructor(void) const
Check if this TypeId has a constructor.
Definition: type-id.cc:991
Control the scheduling of simulation events.
Definition: simulator.h:68
Callback template class.
Definition: callback.h:1176
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:483
struct TagData * next
Pointer to next in list.
Buffer::Iterator current
an iterator which can be fed to Deserialize.
uint32_t Deserialize(uint8_t const *buffer, uint32_t size)
Deserializes a packet.
Definition: packet.cc:737
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
void RemoveAtStart(uint32_t start)
Definition: buffer.cc:438
bool HasNext(void) const
Definition: packet.cc:65
virtual uint32_t GetSerializedSize(void) const =0
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet.cc:585
TypeId GetTypeId(void) const
Definition: packet.cc:107
Item Next(void)
Retrieve the next metadata item.
automatically resized byte buffer
Definition: buffer.h:92
def start()
Definition: core.py:1855
structure describing a packet metadata item
List of the packet tags stored in a packet.
bool isFragment
true: this is a fragmented header, trailer, or, payload.
TypeId tid
TypeId of Header or Trailer.
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:63
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
Definition: packet.cc:227
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:790
static void EnableChecking(void)
Enable the packet metadata checking.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
TagBuffer Add(TypeId tid, uint32_t bufferSize, int32_t start, int32_t end)
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:39
void Adjust(int32_t adjustment)
Adjust the offsets stored internally by the adjustment delta.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
Buffer m_buffer
the packet buffer (it&#39;s actual contents)
Definition: packet.h:782
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Anchor the ns-3 type and attribute system.
Definition: object-base.h:119
Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer)
Constructor.
Definition: packet.cc:57
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:156
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:898
network packets
Definition: packet.h:231
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Item Next(void)
Definition: packet.cc:94
void AddHeader(Header const &header, uint32_t size)
Add an header.
iterator in a Buffer instance
Definition: buffer.h:98
const struct PacketTagList::TagData * Head(void) const
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition: packet.h:122
Packet()
Create an empty packet with a new uid (as returned by getUid).
Definition: packet.cc:129
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
uint32_t PeekTrailer(Trailer &trailer)
Deserialize but does not remove a trailer from the internal buffer.
Definition: packet.cc:327
void AddAtEnd(int32_t appendOffset)
Make sure that all offsets are smaller than appendOffset which represents the location where new byte...
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
void RemoveAll(void)
Removes all of the tags from the ByteTagList.
Tree node for sharing serialized tags.
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:648
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:891
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:61
void AddTrailer(Trailer const &trailer, uint32_t size)
Add a trailer.
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:362
void AddPaddingAtEnd(uint32_t end)
Add some padding at the end.
bool Replace(Tag &tag)
Replace the value of a tag.
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Definition: nix-vector.cc:225
ByteTagList::Iterator Begin(int32_t offsetStart, int32_t offsetEnd) const
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
bool HasNext(void) const
Definition: packet.cc:89
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:101
void SetNixVector(Ptr< NixVector > nixVector)
Set the packet nix-vector.
Definition: packet.cc:244
ItemIterator BeginItem(Buffer buffer) const
Initialize the item iterator to the buffer begin.
virtual void Print(std::ostream &os) const =0
Print the object contents.
ByteTagList m_byteTagList
the ByteTag list
Definition: packet.h:783
uint8_t data[writeSize]
enum ns3::PacketMetadata::Item::ItemType type
metadata type
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
Definition: buffer.cc:715
Iterator class for metadata items.
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:877
uint32_t GetSerializedSize(void) const
Definition: nix-vector.cc:214
uint32_t GetSerializedSize(void) const
Return the number of bytes required for serialization.
Definition: buffer.cc:556
static void EnableChecking(void)
Enable packets metadata checking.
Definition: packet.cc:579
std::string ToString(void) const
Return a string representation of the packet.
Definition: packet.cc:426
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:347
void Next(void)
go forward by one byte
Definition: buffer.h:845
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
int32_t start
offset to the start of the tag from the virtual byte buffer
Definition: byte-tag-list.h:85
virtual void Serialize(Buffer::Iterator start) const =0
virtual uint32_t Deserialize(Buffer::Iterator end)=0
Item Next(void)
Definition: packet.cc:70
Iterator over the set of packet tags in a packet.
Definition: packet.h:131
const struct PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:179
void AddAtEnd(PacketMetadata const &o)
Add a metadata at the metadata start.
struct ByteTagList::Iterator::Item Next(void)
Returns the next Item from the ByteTagList.
bool HasNext(void) const
Checks if there is another metadata item.
#define list
virtual uint32_t Deserialize(Buffer::Iterator start)=0
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
void AddAtEnd(uint32_t end)
Definition: buffer.cc:354
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
virtual void Serialize(TagBuffer i) const =0
tag a set of bytes in a packet
Definition: tag.h:36
static void Enable(void)
Enable the packet metadata.
PacketTagIterator GetPacketTagIterator(void) const
Returns an object which can be used to iterate over the list of packet tags.
Definition: packet.cc:921
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual uint32_t GetSerializedSize(void) const =0
PacketMetadata CreateFragment(uint32_t start, uint32_t end) const
Creates a fragment.
PacketTagIterator(const struct PacketTagList::TagData *head)
Constructor.
Definition: packet.cc:84
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:355
friend class Packet
Friend class.
Definition: packet.h:173
Iterator over the set of byte tags in a packet.
Definition: packet.h:54
virtual void Deserialize(TagBuffer i)=0
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:318
bool Peek(Tag &tag) const
Find a tag and return its value.
PacketTagList m_packetTagList
the packet&#39;s Tag list
Definition: packet.h:784
Ptr< NixVector > Copy(void) const
Definition: nix-vector.cc:71
int32_t end
offset to the end of the tag from the virtual byte buffer
Definition: byte-tag-list.h:86
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Deserialization from raw uint8_t*.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Definition: buffer.cc:576
void PrintByteTags(std::ostream &os) const
Iterate over the byte tags present in this packet, and invoke the Print method of each tag stored in ...
Definition: packet.cc:396
Identifies a packet tag within a packet.
Definition: packet.h:137
PacketMetadata::ItemIterator BeginItem(void) const
Returns an iterator which points to the first &#39;item&#39; stored in this buffer.
Definition: packet.cc:566
void Add(Tag const &tag) const
Add a tag to the head of this branch.
void RemoveHeader(Header const &header, uint32_t size)
Remove an header.
Buffer::Iterator End(void) const
Definition: buffer.h:1075
TypeId tid
type of the tag
Definition: byte-tag-list.h:83
uint32_t currentSize
size of item.
Item(const struct PacketTagList::TagData *data)
Constructor.
Definition: packet.cc:102
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:44
TagBuffer buf
the data for the tag as generated by Tag::Serialize
Definition: byte-tag-list.h:87
An iterator for iterating through a byte tag list.
Definition: byte-tag-list.h:72
read and write tag data
Definition: tag-buffer.h:51
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:112
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:863
void RemoveTrailer(Trailer const &trailer, uint32_t size)
Remove a trailer.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1069
virtual TypeId GetInstanceTypeId(void) const =0
Get the most derived TypeId for this Object.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:870
void AddAtStart(int32_t prependOffset)
Make sure that all offsets are bigger than prependOffset which represents the location where new byte...
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
virtual uint32_t GetSerializedSize(void) const =0
uint64_t GetUid(void) const
Get the packet Uid.
Ptr< NixVector > GetNixVector(void) const
Get the packet nix-vector.
Definition: packet.cc:250
Ptr< NixVector > m_nixVector
the packet&#39;s Nix vector
Definition: packet.h:788
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Definition: nix-vector.cc:282
uint32_t GetSize(void) const
Definition: buffer.h:1063
Buffer CreateFragment(uint32_t start, uint32_t length) const
Definition: buffer.cc:519
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:953
uint32_t GetSerializedSize(void) const
Get the metadata serialized size.
virtual void Serialize(Buffer::Iterator start) const =0
uint32_t GetOffsetStart(void) const
Returns the offset from the start of the virtual byte buffer to the ByteTagList.
ByteTagIterator(ByteTagList::Iterator i)
Copy Constructor.
Definition: packet.cc:78
void RemoveAtStart(uint32_t start)
Remove a chunk of metadata at the metadata start.
uint32_t currentTrimedFromStart
how many bytes were trimmed from the start of a fragment.
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:885
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:49
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
void RemoveAll(void)
Remove all tags from this list (up to the first merge).
a unique identifier for an interface.
Definition: type-id.h:58
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:819
abstract base class for ns3::Header and ns3::Trailer
Definition: chunk.h:34
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
ByteTagIterator GetByteTagIterator(void) const
Returns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:840
virtual void Print(std::ostream &os) const =0
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:628
bool HasNext(void) const
Used to determine if the iterator is at the end of the byteTagList.
Handle packet metadata about packet headers and trailers.
An item specifies an individual tag within a byte buffer.
Definition: byte-tag-list.h:81