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
118 }
119 
120 
122 Packet::Copy (void) const
123 {
124  // we need to invoke the copy constructor directly
125  // rather than calling Create because the copy constructor
126  // is private.
127  return Ptr<Packet> (new Packet (*this), false);
128 }
129 
131  : m_buffer (),
132  m_byteTagList (),
133  m_packetTagList (),
134  /* The upper 32 bits of the packet id in
135  * metadata is for the system id. For non-
136  * distributed simulations, this is simply
137  * zero. The lower 32 bits are for the
138  * global UID
139  */
140  m_metadata (static_cast<uint64_t> (Simulator::GetSystemId ()) << 32 | m_globalUid, 0),
141  m_nixVector (0)
142 {
143  m_globalUid++;
144 }
145 
147  : m_buffer (o.m_buffer),
148  m_byteTagList (o.m_byteTagList),
149  m_packetTagList (o.m_packetTagList),
150  m_metadata (o.m_metadata)
151 {
153  : m_nixVector = 0;
154 }
155 
156 Packet &
158 {
159  if (this == &o)
160  {
161  return *this;
162  }
163  m_buffer = o.m_buffer;
168  : m_nixVector = 0;
169  return *this;
170 }
171 
172 Packet::Packet (uint32_t size)
173  : m_buffer (size),
174  m_byteTagList (),
175  m_packetTagList (),
176  /* The upper 32 bits of the packet id in
177  * metadata is for the system id. For non-
178  * distributed simulations, this is simply
179  * zero. The lower 32 bits are for the
180  * global UID
181  */
182  m_metadata (static_cast<uint64_t> (Simulator::GetSystemId ()) << 32 | m_globalUid, size),
183  m_nixVector (0)
184 {
185  m_globalUid++;
186 }
187 Packet::Packet (uint8_t const *buffer, uint32_t size, bool magic)
188  : m_buffer (0, false),
189  m_byteTagList (),
190  m_packetTagList (),
191  m_metadata (0,0),
192  m_nixVector (0)
193 {
194  NS_ASSERT (magic);
195  Deserialize (buffer, size);
196 }
197 
198 Packet::Packet (uint8_t const*buffer, uint32_t size)
199  : m_buffer (),
200  m_byteTagList (),
201  m_packetTagList (),
202  /* The upper 32 bits of the packet id in
203  * metadata is for the system id. For non-
204  * distributed simulations, this is simply
205  * zero. The lower 32 bits are for the
206  * global UID
207  */
208  m_metadata (static_cast<uint64_t> (Simulator::GetSystemId ()) << 32 | m_globalUid, size),
209  m_nixVector (0)
210 {
211  m_globalUid++;
212  m_buffer.AddAtStart (size);
214  i.Write (buffer, size);
215 }
216 
217 Packet::Packet (const Buffer &buffer, const ByteTagList &byteTagList,
218  const PacketTagList &packetTagList, const PacketMetadata &metadata)
219  : m_buffer (buffer),
220  m_byteTagList (byteTagList),
221  m_packetTagList (packetTagList),
222  m_metadata (metadata),
223  m_nixVector (0)
224 {
225 }
226 
228 Packet::CreateFragment (uint32_t start, uint32_t length) const
229 {
230  NS_LOG_FUNCTION (this << start << length);
231  Buffer buffer = m_buffer.CreateFragment (start, length);
232  NS_ASSERT (m_buffer.GetSize () >= start + length);
233  uint32_t end = m_buffer.GetSize () - (start + length);
234  PacketMetadata metadata = m_metadata.CreateFragment (start, end);
235  // again, call the constructor directly rather than
236  // through Create because it is private.
237  return Ptr<Packet> (new Packet (buffer, m_byteTagList, m_packetTagList, metadata), false);
238 }
239 
240 void
242 {
243  m_nixVector = nixVector;
244 }
245 
248 {
249  return m_nixVector;
250 }
251 
252 void
253 Packet::AddHeader (const Header &header)
254 {
255  uint32_t size = header.GetSerializedSize ();
256  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << size);
257  uint32_t orgStart = m_buffer.GetCurrentStartOffset ();
258  bool resized = m_buffer.AddAtStart (size);
259  if (resized)
260  {
262  m_buffer.GetCurrentStartOffset () + size);
263  }
264  header.Serialize (m_buffer.Begin ());
265  m_metadata.AddHeader (header, size);
266 }
267 uint32_t
269 {
270  uint32_t deserialized = header.Deserialize (m_buffer.Begin ());
271  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
272  m_buffer.RemoveAtStart (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);
288  uint32_t orgStart = m_buffer.GetCurrentStartOffset ();
289  bool resized = m_buffer.AddAtEnd (size);
290  if (resized)
291  {
293  m_buffer.GetCurrentEndOffset () - size);
294  }
295  Buffer::Iterator end = m_buffer.End ();
296  trailer.Serialize (end);
297  m_metadata.AddTrailer (trailer, size);
298 }
299 uint32_t
301 {
302  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
303  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
304  m_buffer.RemoveAtEnd (deserialized);
305  m_metadata.RemoveTrailer (trailer, deserialized);
306  return deserialized;
307 }
308 uint32_t
310 {
311  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
312  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
313  return deserialized;
314 }
315 
316 void
318 {
319  NS_LOG_FUNCTION (this << packet << packet->GetSize ());
320  uint32_t aStart = m_buffer.GetCurrentStartOffset ();
321  uint32_t bEnd = packet->m_buffer.GetCurrentEndOffset ();
322  m_buffer.AddAtEnd (packet->m_buffer);
323  uint32_t appendPrependOffset = m_buffer.GetCurrentEndOffset () - packet->m_buffer.GetSize ();
325  appendPrependOffset);
326  ByteTagList copy = packet->m_byteTagList;
327  copy.AddAtStart (m_buffer.GetCurrentEndOffset () - bEnd,
328  appendPrependOffset);
329  m_byteTagList.Add (copy);
330  m_metadata.AddAtEnd (packet->m_metadata);
331 }
332 void
333 Packet::AddPaddingAtEnd (uint32_t size)
334 {
335  NS_LOG_FUNCTION (this << size);
336  uint32_t orgEnd = m_buffer.GetCurrentEndOffset ();
337  bool resized = m_buffer.AddAtEnd (size);
338  if (resized)
339  {
341  m_buffer.GetCurrentEndOffset () - size);
342  }
344 }
345 void
346 Packet::RemoveAtEnd (uint32_t size)
347 {
348  NS_LOG_FUNCTION (this << size);
349  m_buffer.RemoveAtEnd (size);
350  m_metadata.RemoveAtEnd (size);
351 }
352 void
353 Packet::RemoveAtStart (uint32_t size)
354 {
355  NS_LOG_FUNCTION (this << size);
356  m_buffer.RemoveAtStart (size);
357  m_metadata.RemoveAtStart (size);
358 }
359 
360 void
362 {
363  NS_LOG_FUNCTION (this);
365 }
366 
367 uint32_t
368 Packet::CopyData (uint8_t *buffer, uint32_t size) const
369 {
370  return m_buffer.CopyData (buffer, size);
371 }
372 
373 void
374 Packet::CopyData (std::ostream *os, uint32_t size) const
375 {
376  return m_buffer.CopyData (os, size);
377 }
378 
379 uint64_t
380 Packet::GetUid (void) const
381 {
382  return m_metadata.GetUid ();
383 }
384 
385 void
386 Packet::PrintByteTags (std::ostream &os) const
387 {
389  while (i.HasNext ())
390  {
391  ByteTagIterator::Item item = i.Next ();
392  os << item.GetTypeId ().GetName () << " [" << item.GetStart () << "-" << item.GetEnd () << "]";
393  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
394  if (constructor.IsNull ())
395  {
396  if (i.HasNext ())
397  {
398  os << " ";
399  }
400  continue;
401  }
402  Tag *tag = dynamic_cast<Tag *> (constructor ());
403  NS_ASSERT (tag != 0);
404  os << " ";
405  item.GetTag (*tag);
406  tag->Print (os);
407  if (i.HasNext ())
408  {
409  os << " ";
410  }
411  delete tag;
412  }
413 }
414 
415 std::string
417 {
418  std::ostringstream oss;
419  Print (oss);
420  return oss.str();
421 }
422 
423 void
424 Packet::Print (std::ostream &os) const
425 {
427  while (i.HasNext ())
428  {
429  PacketMetadata::Item item = i.Next ();
430  if (item.isFragment)
431  {
432  switch (item.type) {
434  os << "Payload";
435  break;
438  os << item.tid.GetName ();
439  break;
440  }
441  os << " Fragment [" << item.currentTrimedFromStart<<":"
442  << (item.currentTrimedFromStart + item.currentSize) << "]";
443  }
444  else
445  {
446  switch (item.type) {
448  os << "Payload (size=" << item.currentSize << ")";
449  break;
452  os << item.tid.GetName () << " (";
453  {
454  NS_ASSERT (item.tid.HasConstructor ());
455  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
456  NS_ASSERT (!constructor.IsNull ());
457  ObjectBase *instance = constructor ();
458  NS_ASSERT (instance != 0);
459  Chunk *chunk = dynamic_cast<Chunk *> (instance);
460  NS_ASSERT (chunk != 0);
461  chunk->Deserialize (item.current);
462  chunk->Print (os);
463  delete chunk;
464  }
465  os << ")";
466  break;
467  }
468  }
469  if (i.HasNext ())
470  {
471  os << " ";
472  }
473  }
474 #if 0
475  // The code below will work only if headers and trailers
476  // define the right attributes which is not the case for
477  // now. So, as a temporary measure, we use the
478  // headers' and trailers' Print method as shown above.
480  while (i.HasNext ())
481  {
482  PacketMetadata::Item item = i.Next ();
483  if (item.isFragment)
484  {
485  switch (item.type) {
487  os << "Payload";
488  break;
491  os << item.tid.GetName ();
492  break;
493  }
494  os << " Fragment [" << item.currentTrimedFromStart<<":"
495  << (item.currentTrimedFromStart + item.currentSize) << "]";
496  }
497  else
498  {
499  switch (item.type) {
501  os << "Payload (size=" << item.currentSize << ")";
502  break;
505  os << item.tid.GetName () << "(";
506  {
507  NS_ASSERT (item.tid.HasConstructor ());
508  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
509  NS_ASSERT (constructor.IsNull ());
510  ObjectBase *instance = constructor ();
511  NS_ASSERT (instance != 0);
512  Chunk *chunk = dynamic_cast<Chunk *> (instance);
513  NS_ASSERT (chunk != 0);
514  chunk->Deserialize (item.current);
515  for (uint32_t j = 0; j < item.tid.GetAttributeN (); j++)
516  {
517  std::string attrName = item.tid.GetAttributeName (j);
518  std::string value;
519  bool ok = chunk->GetAttribute (attrName, value);
520  NS_ASSERT (ok);
521  os << attrName << "=" << value;
522  if ((j + 1) < item.tid.GetAttributeN ())
523  {
524  os << ",";
525  }
526  }
527  }
528  os << ")";
529  break;
530  }
531  }
532  if (i.HasNext ())
533  {
534  os << " ";
535  }
536  }
537 #endif
538 }
539 
541 Packet::BeginItem (void) const
542 {
543  return m_metadata.BeginItem (m_buffer);
544 }
545 
546 void
548 {
551 }
552 
553 void
555 {
558 }
559 
560 uint32_t Packet::GetSerializedSize (void) const
561 {
562  uint32_t size = 0;
563 
564  if (m_nixVector)
565  {
566  // increment total size by the size of the nix-vector
567  // ensuring 4-byte boundary
568  size += ((m_nixVector->GetSerializedSize () + 3) & (~3));
569 
570  // add 4-bytes for entry of total length of nix-vector
571  size += 4;
572  }
573  else
574  {
575  // if no nix-vector, still have to add 4-bytes
576  // to account for the entry of total size for
577  // nix-vector in the buffer
578  size += 4;
579  }
580 
581  //Tag size
583  //size += m_tags.GetSerializedSize ();
584 
585  // increment total size by size of meta-data
586  // ensuring 4-byte boundary
587  size += ((m_metadata.GetSerializedSize () + 3) & (~3));
588 
589  // add 4-bytes for entry of total length of meta-data
590  size += 4;
591 
592  // increment total size by size of buffer
593  // ensuring 4-byte boundary
594  size += ((m_buffer.GetSerializedSize () + 3) & (~3));
595 
596  // add 4-bytes for entry of total length of buffer
597  size += 4;
598 
599  return size;
600 }
601 
602 uint32_t
603 Packet::Serialize (uint8_t* buffer, uint32_t maxSize) const
604 {
605  uint32_t* p = reinterpret_cast<uint32_t *> (buffer);
606  uint32_t size = 0;
607 
608  // if nix-vector exists, serialize it
609  if (m_nixVector)
610  {
611  uint32_t nixSize = m_nixVector->GetSerializedSize ();
612  if (size + nixSize <= maxSize)
613  {
614  // put the total length of nix-vector in the
615  // buffer. this includes 4-bytes for total
616  // length itself
617  *p++ = nixSize + 4;
618  size += nixSize;
619 
620  // serialize the nix-vector
621  uint32_t serialized =
622  m_nixVector->Serialize (p, nixSize);
623  if (serialized)
624  {
625  // increment p by nixSize bytes
626  // ensuring 4-byte boundary
627  p += ((nixSize+3) & (~3)) / 4;
628  }
629  else
630  {
631  return 0;
632  }
633  }
634  else
635  {
636  return 0;
637  }
638  }
639  else
640  {
641  // no nix vector, set zero length,
642  // ie 4-bytes, since it must include
643  // length for itself
644  if (size + 4 <= maxSize)
645  {
646  size += 4;
647  *p++ = 4;
648  }
649  else
650  {
651  return 0;
652  }
653  }
654 
655  // Serialize Tags
657 
658  // Serialize Metadata
659  uint32_t metaSize = m_metadata.GetSerializedSize ();
660  if (size + metaSize <= maxSize)
661  {
662  // put the total length of metadata in the
663  // buffer. this includes 4-bytes for total
664  // length itself
665  *p++ = metaSize + 4;
666  size += metaSize;
667 
668  // serialize the metadata
669  uint32_t serialized =
670  m_metadata.Serialize (reinterpret_cast<uint8_t *> (p), metaSize);
671  if (serialized)
672  {
673  // increment p by metaSize bytes
674  // ensuring 4-byte boundary
675  p += ((metaSize+3) & (~3)) / 4;
676  }
677  else
678  {
679  return 0;
680  }
681  }
682  else
683  {
684  return 0;
685  }
686 
687  // Serialize the packet contents
688  uint32_t bufSize = m_buffer.GetSerializedSize ();
689  if (size + bufSize <= maxSize)
690  {
691  // put the total length of the buffer in the
692  // buffer. this includes 4-bytes for total
693  // length itself
694  *p++ = bufSize + 4;
695  size += bufSize;
696 
697  // serialize the buffer
698  uint32_t serialized =
699  m_buffer.Serialize (reinterpret_cast<uint8_t *> (p), bufSize);
700  if (serialized)
701  {
702  // increment p by bufSize bytes
703  // ensuring 4-byte boundary
704  p += ((bufSize+3) & (~3)) / 4;
705  }
706  else
707  {
708  return 0;
709  }
710  }
711  else
712  {
713  return 0;
714  }
715 
716  // Serialized successfully
717  return 1;
718 }
719 
720 uint32_t
721 Packet::Deserialize (const uint8_t* buffer, uint32_t size)
722 {
723  NS_LOG_FUNCTION (this);
724 
725  const uint32_t* p = reinterpret_cast<const uint32_t *> (buffer);
726 
727  // read nix-vector
729  uint32_t nixSize = *p++;
730 
731  // if size less than nixSize, the buffer
732  // will be overrun, assert
733  NS_ASSERT (size >= nixSize);
734 
735  size -= nixSize;
736 
737  if (nixSize > 4)
738  {
739  Ptr<NixVector> nix = Create<NixVector> ();
740  uint32_t nixDeserialized = nix->Deserialize (p, nixSize);
741  if (!nixDeserialized)
742  {
743  // nix-vector not deserialized
744  // completely
745  return 0;
746  }
747  m_nixVector = nix;
748  // increment p by nixSize ensuring
749  // 4-byte boundary
750  p += ((((nixSize - 4) + 3) & (~3)) / 4);
751  }
752 
753  // read tags
755  //uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ());
756  //buffer.RemoveAtStart (tagsDeserialized);
757 
758  // read metadata
759  uint32_t metaSize = *p++;
760 
761  // if size less than metaSize, the buffer
762  // will be overrun, assert
763  NS_ASSERT (size >= metaSize);
764 
765  size -= metaSize;
766 
767  uint32_t metadataDeserialized =
768  m_metadata.Deserialize (reinterpret_cast<const uint8_t *> (p), metaSize);
769  if (!metadataDeserialized)
770  {
771  // meta-data not deserialized
772  // completely
773  return 0;
774  }
775  // increment p by metaSize ensuring
776  // 4-byte boundary
777  p += ((((metaSize - 4) + 3) & (~3)) / 4);
778 
779  // read buffer contents
780  uint32_t bufSize = *p++;
781 
782  // if size less than bufSize, the buffer
783  // will be overrun, assert
784  NS_ASSERT (size >= bufSize);
785 
786  size -= bufSize;
787 
788  uint32_t bufferDeserialized =
789  m_buffer.Deserialize (reinterpret_cast<const uint8_t *> (p), bufSize);
790  if (!bufferDeserialized)
791  {
792  // buffer not deserialized
793  // completely
794  return 0;
795  }
796 
797  // return zero if did not deserialize the
798  // number of expected bytes
799  return (size == 0);
800 }
801 
802 void
803 Packet::AddByteTag (const Tag &tag) const
804 {
805  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
806  ByteTagList *list = const_cast<ByteTagList *> (&m_byteTagList);
807  TagBuffer buffer = list->Add (tag.GetInstanceTypeId (), tag.GetSerializedSize (),
810  tag.Serialize (buffer);
811 }
814 {
816 }
817 
818 bool
820 {
821  TypeId tid = tag.GetInstanceTypeId ();
823  while (i.HasNext ())
824  {
825  ByteTagIterator::Item item = i.Next ();
826  if (tid == item.GetTypeId ())
827  {
828  item.GetTag (tag);
829  return true;
830  }
831  }
832  return false;
833 }
834 
835 void
836 Packet::AddPacketTag (const Tag &tag) const
837 {
838  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
839  m_packetTagList.Add (tag);
840 }
841 
842 bool
844 {
845  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
846  bool found = m_packetTagList.Remove (tag);
847  return found;
848 }
849 bool
851 {
852  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
853  bool found = m_packetTagList.Replace (tag);
854  return found;
855 }
856 
857 bool
859 {
860  bool found = m_packetTagList.Peek (tag);
861  return found;
862 }
863 void
865 {
866  NS_LOG_FUNCTION (this);
868 }
869 
870 void
871 Packet::PrintPacketTags (std::ostream &os) const
872 {
874  while (i.HasNext ())
875  {
876  PacketTagIterator::Item item = i.Next ();
877  NS_ASSERT (item.GetTypeId ().HasConstructor ());
878  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
879  NS_ASSERT (!constructor.IsNull ());
880  ObjectBase *instance = constructor ();
881  Tag *tag = dynamic_cast<Tag *> (instance);
882  NS_ASSERT (tag != 0);
883  item.GetTag (*tag);
884  tag->Print (os);
885  delete tag;
886  if (i.HasNext ())
887  {
888  os << " ";
889  }
890  }
891 }
892 
895 {
897 }
898 
899 std::ostream& operator<< (std::ostream& os, const Packet &packet)
900 {
901  packet.Print (os);
902  return os;
903 }
904 
905 } // 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:268
PacketMetadata m_metadata
the packet's metadata
Definition: packet.h:699
uint32_t GetAttributeN(void) const
Definition: type-id.cc:780
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:819
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
Size of serialization buffer data.
#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:871
Control the scheduling of simulation events.
Definition: simulator.h:70
Callback template class.
Definition: callback.h:984
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:501
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)
Definition: packet.cc:721
void RemoveAtStart(uint32_t start)
Definition: buffer.cc:456
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:1482
structure describing a packet metadata item
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:836
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:380
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:603
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:68
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1078
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:704
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:766
uint32_t GetSerializedSize(void) const
Return the number of bytes required for serialization.
Definition: buffer.cc:574
#define NS_FATAL_ERROR(msg)
Fatal error handling.
Definition: fatal-error.h:100
uint32_t GetSerializedSize(void) const
Get the metadata serialized size.
Buffer m_buffer
the packet buffer (it's actual contents)
Definition: packet.h:696
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Anchor the ns-3 type and attribute system.
Definition: object-base.h:68
enum ns3::PacketMetadata::Item::@81 type
metadata type
Ptr< NixVector > GetNixVector(void) const
Get the packet nix-vector.
Definition: packet.cc:247
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:424
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:157
network packets
Definition: packet.h:226
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:894
iterator in a Buffer instance
Definition: buffer.h:98
Callback< ObjectBase * > GetConstructor(void) const
Definition: type-id.cc:764
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition: packet.h:119
Packet()
Create an empty packet with a new uid (as returned by getUid).
Definition: packet.cc:130
void Add(Tag const &tag) const
Add a tag to the head of this branch.
bool HasConstructor(void) const
Definition: type-id.cc:714
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:228
uint32_t PeekTrailer(Trailer &trailer)
Deserialize but does not remove a trailer from the internal buffer.
Definition: packet.cc:309
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:317
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:664
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:858
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:864
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:60
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:353
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:416
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:547
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:99
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:241
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:697
uint8_t data[writeSize]
Iterator class for metadata items.
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:850
uint32_t GetSerializedSize(void) const
Definition: nix-vector.cc:214
Buffer::Iterator End(void) const
Definition: buffer.h:1082
static void EnableChecking(void)
Enable packets metadata checking.
Definition: packet.cc:554
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:333
int32_t start
offset to the start of the tag from the virtual byte buffer
Definition: byte-tag-list.h:90
virtual void Serialize(Buffer::Iterator start) const =0
ByteTagIterator GetByteTagIterator(void) const
Retiurns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:813
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:743
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
int32_t GetCurrentEndOffset(void) const
Returns the current buffer end offset.
Definition: buffer.cc:714
Iterator over the set of packet tags in a packet.
Definition: packet.h:128
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:386
void AddAtEnd(int32_t adjustment, int32_t appendOffset)
Adjust the offsets stored internally by the adjustment delta and make sure that all offsets are small...
const struct PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:174
Buffer::Iterator Begin(void) const
Definition: buffer.h:1076
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
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
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:541
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:346
friend class Packet
Definition: packet.h:168
Iterator over the set of byte tags in a packet.
Definition: packet.h:53
virtual void Deserialize(TagBuffer i)=0
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:300
std::string GetName(void) const
Definition: type-id.cc:692
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:698
int32_t end
offset to the end of the tag from the virtual byte buffer
Definition: byte-tag-list.h:91
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:228
Identifies a packet tag within a packet.
Definition: packet.h:134
uint32_t GetSize(void) const
Definition: buffer.h:1070
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:88
Buffer CreateFragment(uint32_t start, uint32_t length) const
Definition: buffer.cc:537
bool AddAtEnd(uint32_t end)
Definition: buffer.cc:360
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:594
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:92
An iterator for iterating through a byte tag list.
Definition: byte-tag-list.h:77
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:560
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:843
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:361
virtual uint32_t GetSerializedSize(void) const =0
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:702
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:982
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:368
virtual void Serialize(Buffer::Iterator start) const =0
ByteTagIterator(ByteTagList::Iterator i)
Copy Constructor.
Definition: packet.cc:78
bool AddAtStart(uint32_t start)
Definition: buffer.cc:309
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.
int32_t GetCurrentStartOffset(void) const
Returns the current buffer start offset.
Definition: buffer.cc:708
void RemoveAll(void)
Remove all tags from this list (up to the first merge).
a unique identifier for an interface.
Definition: type-id.h:57
abstract base class for ns3::Header and ns3::Trailer
Definition: chunk.h:14
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
virtual void Print(std::ostream &os) const =0
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
void AddAtStart(int32_t adjustment, int32_t prependOffset)
Adjust the offsets stored internally by the adjustment delta and make sure that all offsets are bigge...
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:803
An item specifies an individual tag within a byte buffer.
Definition: byte-tag-list.h:86