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
268 {
269  uint32_t deserialized = header.Deserialize (m_buffer.Begin ());
270  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
271  m_buffer.RemoveAtStart (deserialized);
272  m_byteTagList.Adjust (-deserialized);
273  m_metadata.RemoveHeader (header, deserialized);
274  return deserialized;
275 }
276 uint32_t
277 Packet::PeekHeader (Header &header) const
278 {
279  uint32_t deserialized = header.Deserialize (m_buffer.Begin ());
280  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
281  return deserialized;
282 }
283 void
284 Packet::AddTrailer (const Trailer &trailer)
285 {
286  uint32_t size = trailer.GetSerializedSize ();
287  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << size);
289  m_buffer.AddAtEnd (size);
290  Buffer::Iterator end = m_buffer.End ();
291  trailer.Serialize (end);
292  m_metadata.AddTrailer (trailer, size);
293 }
294 uint32_t
296 {
297  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
298  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
299  m_buffer.RemoveAtEnd (deserialized);
300  m_metadata.RemoveTrailer (trailer, deserialized);
301  return deserialized;
302 }
303 uint32_t
305 {
306  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
307  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
308  return deserialized;
309 }
310 
311 void
313 {
314  NS_LOG_FUNCTION (this << packet << packet->GetSize ());
316  ByteTagList copy = packet->m_byteTagList;
317  copy.AddAtStart (0);
318  copy.Adjust (GetSize ());
319  m_byteTagList.Add (copy);
320  m_buffer.AddAtEnd (packet->m_buffer);
321  m_metadata.AddAtEnd (packet->m_metadata);
322 }
323 void
324 Packet::AddPaddingAtEnd (uint32_t size)
325 {
326  NS_LOG_FUNCTION (this << size);
328  m_buffer.AddAtEnd (size);
330 }
331 void
332 Packet::RemoveAtEnd (uint32_t size)
333 {
334  NS_LOG_FUNCTION (this << size);
335  m_buffer.RemoveAtEnd (size);
336  m_metadata.RemoveAtEnd (size);
337 }
338 void
339 Packet::RemoveAtStart (uint32_t size)
340 {
341  NS_LOG_FUNCTION (this << size);
342  m_buffer.RemoveAtStart (size);
343  m_byteTagList.Adjust (-size);
344  m_metadata.RemoveAtStart (size);
345 }
346 
347 void
349 {
350  NS_LOG_FUNCTION (this);
352 }
353 
354 uint32_t
355 Packet::CopyData (uint8_t *buffer, uint32_t size) const
356 {
357  return m_buffer.CopyData (buffer, size);
358 }
359 
360 void
361 Packet::CopyData (std::ostream *os, uint32_t size) const
362 {
363  return m_buffer.CopyData (os, size);
364 }
365 
366 uint64_t
367 Packet::GetUid (void) const
368 {
369  return m_metadata.GetUid ();
370 }
371 
372 void
373 Packet::PrintByteTags (std::ostream &os) const
374 {
376  while (i.HasNext ())
377  {
378  ByteTagIterator::Item item = i.Next ();
379  os << item.GetTypeId ().GetName () << " [" << item.GetStart () << "-" << item.GetEnd () << "]";
380  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
381  if (constructor.IsNull ())
382  {
383  if (i.HasNext ())
384  {
385  os << " ";
386  }
387  continue;
388  }
389  Tag *tag = dynamic_cast<Tag *> (constructor ());
390  NS_ASSERT (tag != 0);
391  os << " ";
392  item.GetTag (*tag);
393  tag->Print (os);
394  if (i.HasNext ())
395  {
396  os << " ";
397  }
398  delete tag;
399  }
400 }
401 
402 std::string
404 {
405  std::ostringstream oss;
406  Print (oss);
407  return oss.str();
408 }
409 
410 void
411 Packet::Print (std::ostream &os) const
412 {
414  while (i.HasNext ())
415  {
416  PacketMetadata::Item item = i.Next ();
417  if (item.isFragment)
418  {
419  switch (item.type) {
421  os << "Payload";
422  break;
425  os << item.tid.GetName ();
426  break;
427  }
428  os << " Fragment [" << item.currentTrimedFromStart<<":"
429  << (item.currentTrimedFromStart + item.currentSize) << "]";
430  }
431  else
432  {
433  switch (item.type) {
435  os << "Payload (size=" << item.currentSize << ")";
436  break;
439  os << item.tid.GetName () << " (";
440  {
441  NS_ASSERT (item.tid.HasConstructor ());
442  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
443  NS_ASSERT (!constructor.IsNull ());
444  ObjectBase *instance = constructor ();
445  NS_ASSERT (instance != 0);
446  Chunk *chunk = dynamic_cast<Chunk *> (instance);
447  NS_ASSERT (chunk != 0);
448  chunk->Deserialize (item.current);
449  chunk->Print (os);
450  delete chunk;
451  }
452  os << ")";
453  break;
454  }
455  }
456  if (i.HasNext ())
457  {
458  os << " ";
459  }
460  }
461 #if 0
462  // The code below will work only if headers and trailers
463  // define the right attributes which is not the case for
464  // now. So, as a temporary measure, we use the
465  // headers' and trailers' Print method as shown above.
467  while (i.HasNext ())
468  {
469  PacketMetadata::Item item = i.Next ();
470  if (item.isFragment)
471  {
472  switch (item.type) {
474  os << "Payload";
475  break;
478  os << item.tid.GetName ();
479  break;
480  }
481  os << " Fragment [" << item.currentTrimedFromStart<<":"
482  << (item.currentTrimedFromStart + item.currentSize) << "]";
483  }
484  else
485  {
486  switch (item.type) {
488  os << "Payload (size=" << item.currentSize << ")";
489  break;
492  os << item.tid.GetName () << "(";
493  {
494  NS_ASSERT (item.tid.HasConstructor ());
495  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
496  NS_ASSERT (constructor.IsNull ());
497  ObjectBase *instance = constructor ();
498  NS_ASSERT (instance != 0);
499  Chunk *chunk = dynamic_cast<Chunk *> (instance);
500  NS_ASSERT (chunk != 0);
501  chunk->Deserialize (item.current);
502  for (uint32_t j = 0; j < item.tid.GetAttributeN (); j++)
503  {
504  std::string attrName = item.tid.GetAttributeName (j);
505  std::string value;
506  bool ok = chunk->GetAttribute (attrName, value);
507  NS_ASSERT (ok);
508  os << attrName << "=" << value;
509  if ((j + 1) < item.tid.GetAttributeN ())
510  {
511  os << ",";
512  }
513  }
514  }
515  os << ")";
516  break;
517  }
518  }
519  if (i.HasNext ())
520  {
521  os << " ";
522  }
523  }
524 #endif
525 }
526 
528 Packet::BeginItem (void) const
529 {
530  return m_metadata.BeginItem (m_buffer);
531 }
532 
533 void
535 {
538 }
539 
540 void
542 {
545 }
546 
547 uint32_t Packet::GetSerializedSize (void) const
548 {
549  uint32_t size = 0;
550 
551  if (m_nixVector)
552  {
553  // increment total size by the size of the nix-vector
554  // ensuring 4-byte boundary
555  size += ((m_nixVector->GetSerializedSize () + 3) & (~3));
556 
557  // add 4-bytes for entry of total length of nix-vector
558  size += 4;
559  }
560  else
561  {
562  // if no nix-vector, still have to add 4-bytes
563  // to account for the entry of total size for
564  // nix-vector in the buffer
565  size += 4;
566  }
567 
568  //Tag size
570  //size += m_tags.GetSerializedSize ();
571 
572  // increment total size by size of meta-data
573  // ensuring 4-byte boundary
574  size += ((m_metadata.GetSerializedSize () + 3) & (~3));
575 
576  // add 4-bytes for entry of total length of meta-data
577  size += 4;
578 
579  // increment total size by size of buffer
580  // ensuring 4-byte boundary
581  size += ((m_buffer.GetSerializedSize () + 3) & (~3));
582 
583  // add 4-bytes for entry of total length of buffer
584  size += 4;
585 
586  return size;
587 }
588 
589 uint32_t
590 Packet::Serialize (uint8_t* buffer, uint32_t maxSize) const
591 {
592  uint32_t* p = reinterpret_cast<uint32_t *> (buffer);
593  uint32_t size = 0;
594 
595  // if nix-vector exists, serialize it
596  if (m_nixVector)
597  {
598  uint32_t nixSize = m_nixVector->GetSerializedSize ();
599  if (size + nixSize <= maxSize)
600  {
601  // put the total length of nix-vector in the
602  // buffer. this includes 4-bytes for total
603  // length itself
604  *p++ = nixSize + 4;
605  size += nixSize;
606 
607  // serialize the nix-vector
608  uint32_t serialized =
609  m_nixVector->Serialize (p, nixSize);
610  if (serialized)
611  {
612  // increment p by nixSize bytes
613  // ensuring 4-byte boundary
614  p += ((nixSize+3) & (~3)) / 4;
615  }
616  else
617  {
618  return 0;
619  }
620  }
621  else
622  {
623  return 0;
624  }
625  }
626  else
627  {
628  // no nix vector, set zero length,
629  // ie 4-bytes, since it must include
630  // length for itself
631  if (size + 4 <= maxSize)
632  {
633  size += 4;
634  *p++ = 4;
635  }
636  else
637  {
638  return 0;
639  }
640  }
641 
642  // Serialize Tags
644 
645  // Serialize Metadata
646  uint32_t metaSize = m_metadata.GetSerializedSize ();
647  if (size + metaSize <= maxSize)
648  {
649  // put the total length of metadata in the
650  // buffer. this includes 4-bytes for total
651  // length itself
652  *p++ = metaSize + 4;
653  size += metaSize;
654 
655  // serialize the metadata
656  uint32_t serialized = m_metadata.Serialize (reinterpret_cast<uint8_t *> (p), metaSize);
657  if (serialized)
658  {
659  // increment p by metaSize bytes
660  // ensuring 4-byte boundary
661  p += ((metaSize+3) & (~3)) / 4;
662  }
663  else
664  {
665  return 0;
666  }
667  }
668  else
669  {
670  return 0;
671  }
672 
673  // Serialize the packet contents
674  uint32_t bufSize = m_buffer.GetSerializedSize ();
675  if (size + bufSize <= maxSize)
676  {
677  // put the total length of the buffer in the
678  // buffer. this includes 4-bytes for total
679  // length itself
680  *p++ = bufSize + 4;
681 
682  // serialize the buffer
683  uint32_t serialized = m_buffer.Serialize (reinterpret_cast<uint8_t *> (p), bufSize);
684  if (!serialized)
685  {
686  return 0;
687  }
688  }
689  else
690  {
691  return 0;
692  }
693 
694  // Serialized successfully
695  return 1;
696 }
697 
698 uint32_t
699 Packet::Deserialize (const uint8_t* buffer, uint32_t size)
700 {
701  NS_LOG_FUNCTION (this);
702 
703  const uint32_t* p = reinterpret_cast<const uint32_t *> (buffer);
704 
705  // read nix-vector
707  uint32_t nixSize = *p++;
708 
709  // if size less than nixSize, the buffer
710  // will be overrun, assert
711  NS_ASSERT (size >= nixSize);
712 
713  size -= nixSize;
714 
715  if (nixSize > 4)
716  {
717  Ptr<NixVector> nix = Create<NixVector> ();
718  uint32_t nixDeserialized = nix->Deserialize (p, nixSize);
719  if (!nixDeserialized)
720  {
721  // nix-vector not deserialized
722  // completely
723  return 0;
724  }
725  m_nixVector = nix;
726  // increment p by nixSize ensuring
727  // 4-byte boundary
728  p += ((((nixSize - 4) + 3) & (~3)) / 4);
729  }
730 
731  // read tags
733  //uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ());
734  //buffer.RemoveAtStart (tagsDeserialized);
735 
736  // read metadata
737  uint32_t metaSize = *p++;
738 
739  // if size less than metaSize, the buffer
740  // will be overrun, assert
741  NS_ASSERT (size >= metaSize);
742 
743  size -= metaSize;
744 
745  uint32_t metadataDeserialized =
746  m_metadata.Deserialize (reinterpret_cast<const uint8_t *> (p), metaSize);
747  if (!metadataDeserialized)
748  {
749  // meta-data not deserialized
750  // completely
751  return 0;
752  }
753  // increment p by metaSize ensuring
754  // 4-byte boundary
755  p += ((((metaSize - 4) + 3) & (~3)) / 4);
756 
757  // read buffer contents
758  uint32_t bufSize = *p++;
759 
760  // if size less than bufSize, the buffer
761  // will be overrun, assert
762  NS_ASSERT (size >= bufSize);
763 
764  size -= bufSize;
765 
766  uint32_t bufferDeserialized =
767  m_buffer.Deserialize (reinterpret_cast<const uint8_t *> (p), bufSize);
768  if (!bufferDeserialized)
769  {
770  // buffer not deserialized
771  // completely
772  return 0;
773  }
774 
775  // return zero if did not deserialize the
776  // number of expected bytes
777  return (size == 0);
778 }
779 
780 void
781 Packet::AddByteTag (const Tag &tag) const
782 {
783  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
784  ByteTagList *list = const_cast<ByteTagList *> (&m_byteTagList);
785  TagBuffer buffer = list->Add (tag.GetInstanceTypeId (), tag.GetSerializedSize (),
786  0,
787  GetSize ());
788  tag.Serialize (buffer);
789 }
792 {
793  return ByteTagIterator (m_byteTagList.Begin (0, GetSize ()));
794 }
795 
796 bool
798 {
799  TypeId tid = tag.GetInstanceTypeId ();
801  while (i.HasNext ())
802  {
803  ByteTagIterator::Item item = i.Next ();
804  if (tid == item.GetTypeId ())
805  {
806  item.GetTag (tag);
807  return true;
808  }
809  }
810  return false;
811 }
812 
813 void
814 Packet::AddPacketTag (const Tag &tag) const
815 {
816  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
817  m_packetTagList.Add (tag);
818 }
819 
820 bool
822 {
823  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
824  bool found = m_packetTagList.Remove (tag);
825  return found;
826 }
827 bool
829 {
830  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
831  bool found = m_packetTagList.Replace (tag);
832  return found;
833 }
834 
835 bool
837 {
838  bool found = m_packetTagList.Peek (tag);
839  return found;
840 }
841 void
843 {
844  NS_LOG_FUNCTION (this);
846 }
847 
848 void
849 Packet::PrintPacketTags (std::ostream &os) const
850 {
852  while (i.HasNext ())
853  {
854  PacketTagIterator::Item item = i.Next ();
855  NS_ASSERT (item.GetTypeId ().HasConstructor ());
856  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
857  NS_ASSERT (!constructor.IsNull ());
858  ObjectBase *instance = constructor ();
859  Tag *tag = dynamic_cast<Tag *> (instance);
860  NS_ASSERT (tag != 0);
861  item.GetTag (*tag);
862  tag->Print (os);
863  delete tag;
864  if (i.HasNext ())
865  {
866  os << " ";
867  }
868  }
869 }
870 
873 {
875 }
876 
877 std::ostream& operator<< (std::ostream& os, const Packet &packet)
878 {
879  packet.Print (os);
880  return os;
881 }
882 
883 } // namespace ns3
bool HasNext(void) const
Definition: packet.cc:65
Protocol header serialization and deserialization.
Definition: header.h:42
void RemoveAtEnd(uint32_t end)
Remove a chunk of metadata at the metadata end.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:267
PacketMetadata m_metadata
the packet's metadata
Definition: packet.h:729
uint32_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1068
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:797
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:849
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:486
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:699
void RemoveAtStart(uint32_t start)
Definition: buffer.cc:441
virtual uint32_t GetSerializedSize(void) const =0
const struct PacketTagList::TagData * Head(void) const
uint64_t GetUid(void) const
Get the packet Uid.
Item Next(void)
Retrieve the next metadata item.
automatically resized byte buffer
Definition: buffer.h:92
Ptr< NixVector > Copy(void) const
Definition: nix-vector.cc:71
def start()
Definition: core.py:1790
structure describing a packet metadata item
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:814
List of the packet tags stored in a packet.
bool isFragment
true: this is a fragmented header, trailer, or, payload.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:367
TypeId tid
TypeId of Header or Trailer.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:590
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:63
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:734
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
bool HasNext(void) const
Used to determine if the iterator is at the end of the byteTagList.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
TagBuffer Add(TypeId tid, uint32_t bufferSize, int32_t start, int32_t end)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:796
uint32_t GetSerializedSize(void) const
Return the number of bytes required for serialization.
Definition: buffer.cc:559
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
uint32_t GetSerializedSize(void) const
Get the metadata serialized size.
Buffer m_buffer
the packet buffer (it's actual contents)
Definition: packet.h:726
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Anchor the ns-3 type and attribute system.
Definition: object-base.h:119
Ptr< NixVector > GetNixVector(void) const
Get the packet nix-vector.
Definition: packet.cc:250
Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer)
Constructor.
Definition: packet.cc:57
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:411
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:156
network packets
Definition: packet.h:231
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
bool Peek(Tag &tag) const
Find a tag and return its value.
Item Next(void)
Definition: packet.cc:94
void AddHeader(Header const &header, uint32_t size)
Add an header.
PacketTagIterator GetPacketTagIterator(void) const
Returns an object which can be used to iterate over the list of packet tags.
Definition: packet.cc:872
iterator in a Buffer instance
Definition: buffer.h:98
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1052
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
void Add(Tag const &tag) const
Add a tag to the head of this branch.
bool HasConstructor(void) const
Check if this TypeId has a constructor.
Definition: type-id.cc:990
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:112
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
uint32_t PeekTrailer(Trailer &trailer)
Deserialize but does not remove a trailer from the internal buffer.
Definition: packet.cc:304
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:312
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:651
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:836
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:842
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.
bool HasNext(void) const
Definition: packet.cc:89
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:339
void AddPaddingAtEnd(uint32_t end)
Add some padding at the end.
bool Replace(Tag &tag)
Replace the value of a tag.
std::string ToString(void) const
Return a string representation of the packet.
Definition: packet.cc:403
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:534
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:101
PacketMetadata CreateFragment(uint32_t start, uint32_t end) const
Creates a fragment.
void SetNixVector(Ptr< NixVector > nixVector)
Set the packet nix-vector.
Definition: packet.cc:244
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Definition: nix-vector.cc:225
virtual void Print(std::ostream &os) const =0
Print the object contents.
ByteTagList m_byteTagList
the ByteTag list
Definition: packet.h:727
uint8_t data[writeSize]
enum ns3::PacketMetadata::Item::ItemType type
metadata type
Iterator class for metadata items.
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:828
uint32_t GetSerializedSize(void) const
Definition: nix-vector.cc:214
Buffer::Iterator End(void) const
Definition: buffer.h:1075
static void EnableChecking(void)
Enable packets metadata checking.
Definition: packet.cc:541
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:324
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
ByteTagIterator GetByteTagIterator(void) const
Returns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:791
bool HasNext(void) const
Checks if there is another metadata item.
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:718
virtual uint32_t Deserialize(Buffer::Iterator end)=0
Item Next(void)
Definition: packet.cc:70
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:44
Iterator over the set of packet tags in a packet.
Definition: packet.h:131
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:373
const struct PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:179
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
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.
#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
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:277
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual uint32_t GetSerializedSize(void) const =0
PacketMetadata::ItemIterator BeginItem(void) const
Returns an iterator which points to the first 'item' stored in this buffer.
Definition: packet.cc:528
PacketTagIterator(const struct PacketTagList::TagData *head)
Constructor.
Definition: packet.cc:84
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:284
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:332
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:295
std::string GetName(void) const
Get the name.
Definition: type-id.cc:968
uint32_t GetOffsetStart(void) const
Returns the offset from the start of the virtual byte buffer to the ByteTagList.
PacketTagList m_packetTagList
the packet's Tag list
Definition: packet.h:728
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*.
TypeId GetTypeId(void) const
Definition: packet.cc:34
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
Identifies a packet tag within a packet.
Definition: packet.h:137
uint32_t GetSize(void) const
Definition: buffer.h:1063
void RemoveHeader(Header const &header, uint32_t size)
Remove an header.
TypeId GetTypeId(void) const
Definition: packet.cc:107
TypeId tid
type of the tag
Definition: byte-tag-list.h:83
Buffer CreateFragment(uint32_t start, uint32_t length) const
Definition: buffer.cc:522
uint32_t currentSize
size of item.
Item(const struct PacketTagList::TagData *data)
Constructor.
Definition: packet.cc:102
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:49
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Definition: buffer.cc:579
ByteTagList::Iterator Begin(int32_t offsetStart, int32_t offsetEnd) const
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
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:39
void RemoveTrailer(Trailer const &trailer, uint32_t size)
Remove a trailer.
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet.cc:547
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:821
void AddAtStart(int32_t prependOffset)
Make sure that all offsets are bigger than prependOffset which represents the location where new byte...
ItemIterator BeginItem(Buffer buffer) const
Initialize the item iterator to the buffer begin.
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:348
virtual uint32_t GetSerializedSize(void) const =0
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:732
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Definition: nix-vector.cc:282
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:956
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:355
virtual void Serialize(Buffer::Iterator start) const =0
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 trimed from the start of a fragment.
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
abstract base class for ns3::Header and ns3::Trailer
Definition: chunk.h:34
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
virtual void Print(std::ostream &os) const =0
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
Handle packet metadata about packet headers and trailers.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:781
An item specifies an individual tag within a byte buffer.
Definition: byte-tag-list.h:81