A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  ByteTagList byteTagList = m_byteTagList;
233  byteTagList.Adjust (-start);
234  NS_ASSERT (m_buffer.GetSize () >= start + length);
235  uint32_t end = m_buffer.GetSize () - (start + length);
236  PacketMetadata metadata = m_metadata.CreateFragment (start, end);
237  // again, call the constructor directly rather than
238  // through Create because it is private.
239  Ptr<Packet> ret = Ptr<Packet> (new Packet (buffer, byteTagList, m_packetTagList, metadata), false);
240  ret->SetNixVector (GetNixVector ());
241  return ret;
242 }
243 
244 void
246 {
247  m_nixVector = nixVector;
248 }
249 
252 {
253  return m_nixVector;
254 }
255 
256 void
257 Packet::AddHeader (const Header &header)
258 {
259  uint32_t size = header.GetSerializedSize ();
260  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << size);
261  m_buffer.AddAtStart (size);
262  m_byteTagList.Adjust (size);
263  m_byteTagList.AddAtStart (size);
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_byteTagList.Adjust (-deserialized);
274  m_metadata.RemoveHeader (header, deserialized);
275  return deserialized;
276 }
277 uint32_t
278 Packet::PeekHeader (Header &header) const
279 {
280  uint32_t deserialized = header.Deserialize (m_buffer.Begin ());
281  NS_LOG_FUNCTION (this << header.GetInstanceTypeId ().GetName () << deserialized);
282  return deserialized;
283 }
284 void
285 Packet::AddTrailer (const Trailer &trailer)
286 {
287  uint32_t size = trailer.GetSerializedSize ();
288  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << size);
290  m_buffer.AddAtEnd (size);
291  Buffer::Iterator end = m_buffer.End ();
292  trailer.Serialize (end);
293  m_metadata.AddTrailer (trailer, size);
294 }
295 uint32_t
297 {
298  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
299  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
300  m_buffer.RemoveAtEnd (deserialized);
301  m_metadata.RemoveTrailer (trailer, deserialized);
302  return deserialized;
303 }
304 uint32_t
306 {
307  uint32_t deserialized = trailer.Deserialize (m_buffer.End ());
308  NS_LOG_FUNCTION (this << trailer.GetInstanceTypeId ().GetName () << deserialized);
309  return deserialized;
310 }
311 
312 void
314 {
315  NS_LOG_FUNCTION (this << packet << packet->GetSize ());
317  ByteTagList copy = packet->m_byteTagList;
318  copy.AddAtStart (0);
319  copy.Adjust (GetSize ());
320  m_byteTagList.Add (copy);
321  m_buffer.AddAtEnd (packet->m_buffer);
322  m_metadata.AddAtEnd (packet->m_metadata);
323 }
324 void
325 Packet::AddPaddingAtEnd (uint32_t size)
326 {
327  NS_LOG_FUNCTION (this << size);
329  m_buffer.AddAtEnd (size);
331 }
332 void
333 Packet::RemoveAtEnd (uint32_t size)
334 {
335  NS_LOG_FUNCTION (this << size);
336  m_buffer.RemoveAtEnd (size);
337  m_metadata.RemoveAtEnd (size);
338 }
339 void
340 Packet::RemoveAtStart (uint32_t size)
341 {
342  NS_LOG_FUNCTION (this << size);
343  m_buffer.RemoveAtStart (size);
344  m_byteTagList.Adjust (-size);
345  m_metadata.RemoveAtStart (size);
346 }
347 
348 void
350 {
351  NS_LOG_FUNCTION (this);
353 }
354 
355 uint32_t
356 Packet::CopyData (uint8_t *buffer, uint32_t size) const
357 {
358  return m_buffer.CopyData (buffer, size);
359 }
360 
361 void
362 Packet::CopyData (std::ostream *os, uint32_t size) const
363 {
364  return m_buffer.CopyData (os, size);
365 }
366 
367 uint64_t
368 Packet::GetUid (void) const
369 {
370  return m_metadata.GetUid ();
371 }
372 
373 void
374 Packet::PrintByteTags (std::ostream &os) const
375 {
377  while (i.HasNext ())
378  {
379  ByteTagIterator::Item item = i.Next ();
380  os << item.GetTypeId ().GetName () << " [" << item.GetStart () << "-" << item.GetEnd () << "]";
381  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
382  if (constructor.IsNull ())
383  {
384  if (i.HasNext ())
385  {
386  os << " ";
387  }
388  continue;
389  }
390  Tag *tag = dynamic_cast<Tag *> (constructor ());
391  NS_ASSERT (tag != 0);
392  os << " ";
393  item.GetTag (*tag);
394  tag->Print (os);
395  if (i.HasNext ())
396  {
397  os << " ";
398  }
399  delete tag;
400  }
401 }
402 
403 std::string
405 {
406  std::ostringstream oss;
407  Print (oss);
408  return oss.str();
409 }
410 
411 void
412 Packet::Print (std::ostream &os) const
413 {
415  while (i.HasNext ())
416  {
417  PacketMetadata::Item item = i.Next ();
418  if (item.isFragment)
419  {
420  switch (item.type) {
422  os << "Payload";
423  break;
426  os << item.tid.GetName ();
427  break;
428  }
429  os << " Fragment [" << item.currentTrimedFromStart<<":"
430  << (item.currentTrimedFromStart + item.currentSize) << "]";
431  }
432  else
433  {
434  switch (item.type) {
436  os << "Payload (size=" << item.currentSize << ")";
437  break;
440  os << item.tid.GetName () << " (";
441  {
442  NS_ASSERT (item.tid.HasConstructor ());
443  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
444  NS_ASSERT (!constructor.IsNull ());
445  ObjectBase *instance = constructor ();
446  NS_ASSERT (instance != 0);
447  Chunk *chunk = dynamic_cast<Chunk *> (instance);
448  NS_ASSERT (chunk != 0);
449  chunk->Deserialize (item.current);
450  chunk->Print (os);
451  delete chunk;
452  }
453  os << ")";
454  break;
455  }
456  }
457  if (i.HasNext ())
458  {
459  os << " ";
460  }
461  }
462 #if 0
463  // The code below will work only if headers and trailers
464  // define the right attributes which is not the case for
465  // now. So, as a temporary measure, we use the
466  // headers' and trailers' Print method as shown above.
468  while (i.HasNext ())
469  {
470  PacketMetadata::Item item = i.Next ();
471  if (item.isFragment)
472  {
473  switch (item.type) {
475  os << "Payload";
476  break;
479  os << item.tid.GetName ();
480  break;
481  }
482  os << " Fragment [" << item.currentTrimedFromStart<<":"
483  << (item.currentTrimedFromStart + item.currentSize) << "]";
484  }
485  else
486  {
487  switch (item.type) {
489  os << "Payload (size=" << item.currentSize << ")";
490  break;
493  os << item.tid.GetName () << "(";
494  {
495  NS_ASSERT (item.tid.HasConstructor ());
496  Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
497  NS_ASSERT (constructor.IsNull ());
498  ObjectBase *instance = constructor ();
499  NS_ASSERT (instance != 0);
500  Chunk *chunk = dynamic_cast<Chunk *> (instance);
501  NS_ASSERT (chunk != 0);
502  chunk->Deserialize (item.current);
503  for (uint32_t j = 0; j < item.tid.GetAttributeN (); j++)
504  {
505  std::string attrName = item.tid.GetAttributeName (j);
506  std::string value;
507  bool ok = chunk->GetAttribute (attrName, value);
508  NS_ASSERT (ok);
509  os << attrName << "=" << value;
510  if ((j + 1) < item.tid.GetAttributeN ())
511  {
512  os << ",";
513  }
514  }
515  }
516  os << ")";
517  break;
518  }
519  }
520  if (i.HasNext ())
521  {
522  os << " ";
523  }
524  }
525 #endif
526 }
527 
529 Packet::BeginItem (void) const
530 {
531  return m_metadata.BeginItem (m_buffer);
532 }
533 
534 void
536 {
539 }
540 
541 void
543 {
546 }
547 
548 uint32_t Packet::GetSerializedSize (void) const
549 {
550  uint32_t size = 0;
551 
552  if (m_nixVector)
553  {
554  // increment total size by the size of the nix-vector
555  // ensuring 4-byte boundary
556  size += ((m_nixVector->GetSerializedSize () + 3) & (~3));
557 
558  // add 4-bytes for entry of total length of nix-vector
559  size += 4;
560  }
561  else
562  {
563  // if no nix-vector, still have to add 4-bytes
564  // to account for the entry of total size for
565  // nix-vector in the buffer
566  size += 4;
567  }
568 
569  //Tag size
571  //size += m_tags.GetSerializedSize ();
572 
573  // increment total size by size of meta-data
574  // ensuring 4-byte boundary
575  size += ((m_metadata.GetSerializedSize () + 3) & (~3));
576 
577  // add 4-bytes for entry of total length of meta-data
578  size += 4;
579 
580  // increment total size by size of buffer
581  // ensuring 4-byte boundary
582  size += ((m_buffer.GetSerializedSize () + 3) & (~3));
583 
584  // add 4-bytes for entry of total length of buffer
585  size += 4;
586 
587  return size;
588 }
589 
590 uint32_t
591 Packet::Serialize (uint8_t* buffer, uint32_t maxSize) const
592 {
593  uint32_t* p = reinterpret_cast<uint32_t *> (buffer);
594  uint32_t size = 0;
595 
596  // if nix-vector exists, serialize it
597  if (m_nixVector)
598  {
599  uint32_t nixSize = m_nixVector->GetSerializedSize ();
600  if (size + nixSize <= maxSize)
601  {
602  // put the total length of nix-vector in the
603  // buffer. this includes 4-bytes for total
604  // length itself
605  *p++ = nixSize + 4;
606  size += nixSize;
607 
608  // serialize the nix-vector
609  uint32_t serialized =
610  m_nixVector->Serialize (p, nixSize);
611  if (serialized)
612  {
613  // increment p by nixSize bytes
614  // ensuring 4-byte boundary
615  p += ((nixSize+3) & (~3)) / 4;
616  }
617  else
618  {
619  return 0;
620  }
621  }
622  else
623  {
624  return 0;
625  }
626  }
627  else
628  {
629  // no nix vector, set zero length,
630  // ie 4-bytes, since it must include
631  // length for itself
632  if (size + 4 <= maxSize)
633  {
634  size += 4;
635  *p++ = 4;
636  }
637  else
638  {
639  return 0;
640  }
641  }
642 
643  // Serialize Tags
645 
646  // Serialize Metadata
647  uint32_t metaSize = m_metadata.GetSerializedSize ();
648  if (size + metaSize <= maxSize)
649  {
650  // put the total length of metadata in the
651  // buffer. this includes 4-bytes for total
652  // length itself
653  *p++ = metaSize + 4;
654  size += metaSize;
655 
656  // serialize the metadata
657  uint32_t serialized =
658  m_metadata.Serialize (reinterpret_cast<uint8_t *> (p), metaSize);
659  if (serialized)
660  {
661  // increment p by metaSize bytes
662  // ensuring 4-byte boundary
663  p += ((metaSize+3) & (~3)) / 4;
664  }
665  else
666  {
667  return 0;
668  }
669  }
670  else
671  {
672  return 0;
673  }
674 
675  // Serialize the packet contents
676  uint32_t bufSize = m_buffer.GetSerializedSize ();
677  if (size + bufSize <= maxSize)
678  {
679  // put the total length of the buffer in the
680  // buffer. this includes 4-bytes for total
681  // length itself
682  *p++ = bufSize + 4;
683  size += bufSize;
684 
685  // serialize the buffer
686  uint32_t serialized =
687  m_buffer.Serialize (reinterpret_cast<uint8_t *> (p), bufSize);
688  if (serialized)
689  {
690  // increment p by bufSize bytes
691  // ensuring 4-byte boundary
692  p += ((bufSize+3) & (~3)) / 4;
693  }
694  else
695  {
696  return 0;
697  }
698  }
699  else
700  {
701  return 0;
702  }
703 
704  // Serialized successfully
705  return 1;
706 }
707 
708 uint32_t
709 Packet::Deserialize (const uint8_t* buffer, uint32_t size)
710 {
711  NS_LOG_FUNCTION (this);
712 
713  const uint32_t* p = reinterpret_cast<const uint32_t *> (buffer);
714 
715  // read nix-vector
717  uint32_t nixSize = *p++;
718 
719  // if size less than nixSize, the buffer
720  // will be overrun, assert
721  NS_ASSERT (size >= nixSize);
722 
723  size -= nixSize;
724 
725  if (nixSize > 4)
726  {
727  Ptr<NixVector> nix = Create<NixVector> ();
728  uint32_t nixDeserialized = nix->Deserialize (p, nixSize);
729  if (!nixDeserialized)
730  {
731  // nix-vector not deserialized
732  // completely
733  return 0;
734  }
735  m_nixVector = nix;
736  // increment p by nixSize ensuring
737  // 4-byte boundary
738  p += ((((nixSize - 4) + 3) & (~3)) / 4);
739  }
740 
741  // read tags
743  //uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ());
744  //buffer.RemoveAtStart (tagsDeserialized);
745 
746  // read metadata
747  uint32_t metaSize = *p++;
748 
749  // if size less than metaSize, the buffer
750  // will be overrun, assert
751  NS_ASSERT (size >= metaSize);
752 
753  size -= metaSize;
754 
755  uint32_t metadataDeserialized =
756  m_metadata.Deserialize (reinterpret_cast<const uint8_t *> (p), metaSize);
757  if (!metadataDeserialized)
758  {
759  // meta-data not deserialized
760  // completely
761  return 0;
762  }
763  // increment p by metaSize ensuring
764  // 4-byte boundary
765  p += ((((metaSize - 4) + 3) & (~3)) / 4);
766 
767  // read buffer contents
768  uint32_t bufSize = *p++;
769 
770  // if size less than bufSize, the buffer
771  // will be overrun, assert
772  NS_ASSERT (size >= bufSize);
773 
774  size -= bufSize;
775 
776  uint32_t bufferDeserialized =
777  m_buffer.Deserialize (reinterpret_cast<const uint8_t *> (p), bufSize);
778  if (!bufferDeserialized)
779  {
780  // buffer not deserialized
781  // completely
782  return 0;
783  }
784 
785  // return zero if did not deserialize the
786  // number of expected bytes
787  return (size == 0);
788 }
789 
790 void
791 Packet::AddByteTag (const Tag &tag) const
792 {
793  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
794  ByteTagList *list = const_cast<ByteTagList *> (&m_byteTagList);
795  TagBuffer buffer = list->Add (tag.GetInstanceTypeId (), tag.GetSerializedSize (),
796  0,
797  GetSize ());
798  tag.Serialize (buffer);
799 }
802 {
803  return ByteTagIterator (m_byteTagList.Begin (0, GetSize ()));
804 }
805 
806 bool
808 {
809  TypeId tid = tag.GetInstanceTypeId ();
811  while (i.HasNext ())
812  {
813  ByteTagIterator::Item item = i.Next ();
814  if (tid == item.GetTypeId ())
815  {
816  item.GetTag (tag);
817  return true;
818  }
819  }
820  return false;
821 }
822 
823 void
824 Packet::AddPacketTag (const Tag &tag) const
825 {
826  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
827  m_packetTagList.Add (tag);
828 }
829 
830 bool
832 {
833  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
834  bool found = m_packetTagList.Remove (tag);
835  return found;
836 }
837 bool
839 {
840  NS_LOG_FUNCTION (this << tag.GetInstanceTypeId ().GetName () << tag.GetSerializedSize ());
841  bool found = m_packetTagList.Replace (tag);
842  return found;
843 }
844 
845 bool
847 {
848  bool found = m_packetTagList.Peek (tag);
849  return found;
850 }
851 void
853 {
854  NS_LOG_FUNCTION (this);
856 }
857 
858 void
859 Packet::PrintPacketTags (std::ostream &os) const
860 {
862  while (i.HasNext ())
863  {
864  PacketTagIterator::Item item = i.Next ();
865  NS_ASSERT (item.GetTypeId ().HasConstructor ());
866  Callback<ObjectBase *> constructor = item.GetTypeId ().GetConstructor ();
867  NS_ASSERT (!constructor.IsNull ());
868  ObjectBase *instance = constructor ();
869  Tag *tag = dynamic_cast<Tag *> (instance);
870  NS_ASSERT (tag != 0);
871  item.GetTag (*tag);
872  tag->Print (os);
873  delete tag;
874  if (i.HasNext ())
875  {
876  os << " ";
877  }
878  }
879 }
880 
883 {
885 }
886 
887 std::ostream& operator<< (std::ostream& os, const Packet &packet)
888 {
889  packet.Print (os);
890  return os;
891 }
892 
893 } // 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:725
uint32_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:969
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:807
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:859
Control the scheduling of simulation events.
Definition: simulator.h:70
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:709
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
structure describing a packet metadata item
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:824
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:368
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:591
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:730
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:792
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:722
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Anchor the ns-3 type and attribute system.
Definition: object-base.h:68
Ptr< NixVector > GetNixVector(void) const
Get the packet nix-vector.
Definition: packet.cc:251
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:412
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:157
network packets
Definition: packet.h:227
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:882
iterator in a Buffer instance
Definition: buffer.h:98
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:953
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition: packet.h:120
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
Check if this TypeId has a constructor.
Definition: type-id.cc:903
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:305
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:313
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:649
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:846
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:852
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:340
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:404
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:535
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:100
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:245
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:723
uint8_t data[writeSize]
Iterator class for metadata items.
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:838
uint32_t GetSerializedSize(void) const
Definition: nix-vector.cc:214
Buffer::Iterator End(void) const
Definition: buffer.h:1069
static void EnableChecking(void)
Enable packets metadata checking.
Definition: packet.cc:542
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:325
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
Retiurns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:801
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:715
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:129
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:374
const struct PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:175
Buffer::Iterator Begin(void) const
Definition: buffer.h:1063
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:122
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:278
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.
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:529
PacketTagIterator(const struct PacketTagList::TagData *head)
Constructor.
Definition: packet.cc:84
enum ns3::PacketMetadata::Item::@79 type
metadata type
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:285
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:333
friend class Packet
Definition: packet.h:169
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:296
std::string GetName(void) const
Get the name.
Definition: type-id.cc:881
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:724
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:229
Identifies a packet tag within a packet.
Definition: packet.h:135
uint32_t GetSize(void) const
Definition: buffer.h:1057
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
Size of serialization buffer data.
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:548
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:831
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:349
virtual uint32_t GetSerializedSize(void) const =0
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:728
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:953
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:356
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:257
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:791
An item specifies an individual tag within a byte buffer.
Definition: byte-tag-list.h:81