A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "packet.h"
9
10#include "ns3/assert.h"
11#include "ns3/log.h"
12#include "ns3/simulator.h"
13
14#include <cstdarg>
15#include <string>
16
17namespace ns3
18{
19
21
23
24TypeId
26{
27 return m_tid;
28}
29
32{
33 return m_start;
34}
35
38{
39 return m_end;
40}
41
42void
44{
45 if (tag.GetInstanceTypeId() != GetTypeId())
46 {
47 NS_FATAL_ERROR("The tag you provided is not of the right type.");
48 }
49 tag.Deserialize(m_buffer);
50}
51
53 : m_tid(tid),
54 m_start(start),
55 m_end(end),
56 m_buffer(buffer)
57{
58}
59
60bool
62{
63 return m_current.HasNext();
64}
65
75
80
82 : m_current(head)
83{
84}
85
86bool
88{
89 return m_current != nullptr;
90}
91
100
105
106TypeId
108{
109 return m_data->tid;
110}
111
112void
114{
115 NS_ASSERT(tag.GetInstanceTypeId() == m_data->tid);
116 tag.Deserialize(TagBuffer((uint8_t*)m_data->data, (uint8_t*)m_data->data + m_data->size));
117}
118
121{
122 // we need to invoke the copy constructor directly
123 // rather than calling Create because the copy constructor
124 // is private.
125 return Ptr<Packet>(new Packet(*this), false);
126}
127
129 : m_buffer(),
130 m_byteTagList(),
131 m_packetTagList(),
132 /* The upper 32 bits of the packet id in
133 * metadata is for the system id. For non-
134 * distributed simulations, this is simply
135 * zero. The lower 32 bits are for the
136 * global UID
137 */
138 m_metadata(static_cast<uint64_t>(Simulator::GetSystemId()) << 32 | m_globalUid, 0),
139 m_nixVector(nullptr)
140{
141 m_globalUid++;
142}
143
145 : m_buffer(o.m_buffer),
146 m_byteTagList(o.m_byteTagList),
147 m_packetTagList(o.m_packetTagList),
148 m_metadata(o.m_metadata)
149{
150 o.m_nixVector ? m_nixVector = o.m_nixVector->Copy() : m_nixVector = nullptr;
151}
152
153Packet&
155{
156 if (this == &o)
157 {
158 return *this;
159 }
160 m_buffer = o.m_buffer;
164 o.m_nixVector ? m_nixVector = o.m_nixVector->Copy() : m_nixVector = nullptr;
165 return *this;
166}
167
169 : m_buffer(size),
170 m_byteTagList(),
171 m_packetTagList(),
172 /* The upper 32 bits of the packet id in
173 * metadata is for the system id. For non-
174 * distributed simulations, this is simply
175 * zero. The lower 32 bits are for the
176 * global UID
177 */
178 m_metadata(static_cast<uint64_t>(Simulator::GetSystemId()) << 32 | m_globalUid, size),
179 m_nixVector(nullptr)
180{
181 m_globalUid++;
182}
183
184Packet::Packet(const uint8_t* buffer, uint32_t size, bool magic)
185 : m_buffer(0, false),
186 m_byteTagList(),
187 m_packetTagList(),
188 m_metadata(0, 0),
189 m_nixVector(nullptr)
190{
191 NS_ASSERT(magic);
192 Deserialize(buffer, size);
193}
194
195Packet::Packet(const uint8_t* buffer, uint32_t size)
196 : m_buffer(),
197 m_byteTagList(),
198 m_packetTagList(),
199 /* The upper 32 bits of the packet id in
200 * metadata is for the system id. For non-
201 * distributed simulations, this is simply
202 * zero. The lower 32 bits are for the
203 * global UID
204 */
205 m_metadata(static_cast<uint64_t>(Simulator::GetSystemId()) << 32 | m_globalUid, size),
206 m_nixVector(nullptr)
207{
208 m_globalUid++;
209 m_buffer.AddAtStart(size);
211 i.Write(buffer, size);
212}
213
214Packet::Packet(const Buffer& buffer,
215 const ByteTagList& byteTagList,
216 const PacketTagList& packetTagList,
217 const PacketMetadata& metadata)
218 : m_buffer(buffer),
219 m_byteTagList(byteTagList),
220 m_packetTagList(packetTagList),
221 m_metadata(metadata),
222 m_nixVector(nullptr)
223{
224}
225
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 =
239 Ptr<Packet>(new Packet(buffer, byteTagList, m_packetTagList, metadata), false);
240 ret->SetNixVector(GetNixVector());
241 return ret;
242}
243
244void
246{
247 m_nixVector = nixVector;
248}
249
252{
253 return m_nixVector;
254}
255
256void
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);
264 header.Serialize(m_buffer.Begin());
265 m_metadata.AddHeader(header, size);
266}
267
270{
272 end = m_buffer.Begin();
273 end.Next(size);
274 uint32_t deserialized = header.Deserialize(m_buffer.Begin(), end);
275 NS_LOG_FUNCTION(this << header.GetInstanceTypeId().GetName() << deserialized);
276 m_buffer.RemoveAtStart(deserialized);
277 m_byteTagList.Adjust(-deserialized);
278 m_metadata.RemoveHeader(header, deserialized);
279 return deserialized;
280}
281
284{
285 uint32_t deserialized = header.Deserialize(m_buffer.Begin());
286 NS_LOG_FUNCTION(this << header.GetInstanceTypeId().GetName() << deserialized);
287 m_buffer.RemoveAtStart(deserialized);
288 m_byteTagList.Adjust(-deserialized);
289 m_metadata.RemoveHeader(header, deserialized);
290 return deserialized;
291}
292
295{
296 uint32_t deserialized = header.Deserialize(m_buffer.Begin());
297 NS_LOG_FUNCTION(this << header.GetInstanceTypeId().GetName() << deserialized);
298 return deserialized;
299}
300
303{
305 end = m_buffer.Begin();
306 end.Next(size);
307 uint32_t deserialized = header.Deserialize(m_buffer.Begin(), end);
308 NS_LOG_FUNCTION(this << header.GetInstanceTypeId().GetName() << deserialized);
309 return deserialized;
310}
311
312void
314{
315 uint32_t size = trailer.GetSerializedSize();
316 NS_LOG_FUNCTION(this << trailer.GetInstanceTypeId().GetName() << size);
318 m_buffer.AddAtEnd(size);
320 trailer.Serialize(end);
321 m_metadata.AddTrailer(trailer, size);
322}
323
326{
327 uint32_t deserialized = trailer.Deserialize(m_buffer.End());
328 NS_LOG_FUNCTION(this << trailer.GetInstanceTypeId().GetName() << deserialized);
329 m_buffer.RemoveAtEnd(deserialized);
330 m_metadata.RemoveTrailer(trailer, deserialized);
331 return deserialized;
332}
333
336{
337 uint32_t deserialized = trailer.Deserialize(m_buffer.End());
338 NS_LOG_FUNCTION(this << trailer.GetInstanceTypeId().GetName() << deserialized);
339 return deserialized;
340}
341
342void
344{
345 NS_LOG_FUNCTION(this << packet << packet->GetSize());
347 ByteTagList copy = packet->m_byteTagList;
348 copy.AddAtStart(0);
349 copy.Adjust(GetSize());
350 m_byteTagList.Add(copy);
351 m_buffer.AddAtEnd(packet->m_buffer);
352 m_metadata.AddAtEnd(packet->m_metadata);
353}
354
355void
363
364void
366{
367 NS_LOG_FUNCTION(this << size);
368 m_buffer.RemoveAtEnd(size);
370}
371
372void
374{
375 NS_LOG_FUNCTION(this << size);
377 m_byteTagList.Adjust(-size);
379}
380
381void
387
389Packet::CopyData(uint8_t* buffer, uint32_t size) const
390{
391 return m_buffer.CopyData(buffer, size);
392}
393
394void
395Packet::CopyData(std::ostream* os, uint32_t size) const
396{
397 return m_buffer.CopyData(os, size);
398}
399
400uint64_t
402{
403 return m_metadata.GetUid();
404}
405
406void
407Packet::PrintByteTags(std::ostream& os) const
408{
410 while (i.HasNext())
411 {
412 ByteTagIterator::Item item = i.Next();
413 os << item.GetTypeId().GetName() << " [" << item.GetStart() << "-" << item.GetEnd() << "]";
414 Callback<ObjectBase*> constructor = item.GetTypeId().GetConstructor();
415 if (constructor.IsNull())
416 {
417 if (i.HasNext())
418 {
419 os << " ";
420 }
421 continue;
422 }
423 Tag* tag = dynamic_cast<Tag*>(constructor());
424 NS_ASSERT(tag != nullptr);
425 os << " ";
426 item.GetTag(*tag);
427 tag->Print(os);
428 if (i.HasNext())
429 {
430 os << " ";
431 }
432 delete tag;
433 }
434}
435
436std::string
438{
439 std::ostringstream oss;
440 Print(oss);
441 return oss.str();
442}
443
444void
445Packet::Print(std::ostream& os) const
446{
448 while (i.HasNext())
449 {
450 PacketMetadata::Item item = i.Next();
451 if (item.isFragment)
452 {
453 switch (item.type)
454 {
456 os << "Payload";
457 break;
460 os << item.tid.GetName();
461 break;
462 }
463 os << " Fragment [" << item.currentTrimmedFromStart << ":"
464 << (item.currentTrimmedFromStart + item.currentSize) << "]";
465 }
466 else
467 {
468 switch (item.type)
469 {
471 os << "Payload (size=" << item.currentSize << ")";
472 break;
475 os << item.tid.GetName() << " (";
476 {
478 Callback<ObjectBase*> constructor = item.tid.GetConstructor();
479 NS_ASSERT(!constructor.IsNull());
480 ObjectBase* instance = constructor();
481 NS_ASSERT(instance != nullptr);
482 auto chunk = dynamic_cast<Chunk*>(instance);
483 NS_ASSERT(chunk != nullptr);
485 {
486 Buffer::Iterator end = item.current;
487 end.Next(item.currentSize); // move from start
488 chunk->Deserialize(item.current, end);
489 }
490 else if (item.type == PacketMetadata::Item::TRAILER)
491 {
492 Buffer::Iterator start = item.current;
493 start.Prev(item.currentSize); // move from end
494 chunk->Deserialize(start, item.current);
495 }
496 else
497 {
498 chunk->Deserialize(item.current);
499 }
500 chunk->Print(os);
501 delete chunk;
502 }
503 os << ")";
504 break;
505 }
506 }
507 if (i.HasNext())
508 {
509 os << " ";
510 }
511 }
512#if 0
513 // The code below will work only if headers and trailers
514 // define the right attributes which is not the case for
515 // now. So, as a temporary measure, we use the
516 // headers' and trailers' Print method as shown above.
518 while (i.HasNext ())
519 {
520 PacketMetadata::Item item = i.Next ();
521 if (item.isFragment)
522 {
523 switch (item.type) {
525 os << "Payload";
526 break;
529 os << item.tid.GetName ();
530 break;
531 }
532 os << " Fragment [" << item.currentTrimmedFromStart<<":"
533 << (item.currentTrimmedFromStart + item.currentSize) << "]";
534 }
535 else
536 {
537 switch (item.type) {
539 os << "Payload (size=" << item.currentSize << ")";
540 break;
543 os << item.tid.GetName () << "(";
544 {
545 NS_ASSERT (item.tid.HasConstructor ());
546 Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
547 NS_ASSERT (constructor.IsNull ());
548 ObjectBase *instance = constructor ();
549 NS_ASSERT (instance != 0);
550 Chunk *chunk = dynamic_cast<Chunk *> (instance);
551 NS_ASSERT (chunk != 0);
552 chunk->Deserialize (item.current);
553 for (uint32_t j = 0; j < item.tid.GetAttributeN (); j++)
554 {
555 std::string attrName = item.tid.GetAttributeName (j);
556 std::string value;
557 bool ok = chunk->GetAttribute (attrName, value);
558 NS_ASSERT (ok);
559 os << attrName << "=" << value;
560 if ((j + 1) < item.tid.GetAttributeN ())
561 {
562 os << ",";
563 }
564 }
565 }
566 os << ")";
567 break;
568 }
569 }
570 if (i.HasNext ())
571 {
572 os << " ";
573 }
574 }
575#endif
576}
577
580{
582}
583
584void
590
591void
597
600{
601 uint32_t size = 0;
602
603 if (m_nixVector)
604 {
605 // increment total size by the size of the nix-vector
606 // ensuring 4-byte boundary
607 size += ((m_nixVector->GetSerializedSize() + 3) & (~3));
608
609 // add 4-bytes for entry of total length of nix-vector
610 size += 4;
611 }
612 else
613 {
614 // if no nix-vector, still have to add 4-bytes
615 // to account for the entry of total size for
616 // nix-vector in the buffer
617 size += 4;
618 }
619
620 // increment total size by size of packet tag list
621 // ensuring 4-byte boundary
622 size += ((m_packetTagList.GetSerializedSize() + 3) & (~3));
623
624 // add 4-bytes for entry of total length of packet tag list
625 size += 4;
626
627 // increment total size by size of byte tag list
628 // ensuring 4-byte boundary
629 size += ((m_byteTagList.GetSerializedSize() + 3) & (~3));
630
631 // add 4-bytes for entry of total length of byte tag list
632 size += 4;
633
634 // increment total size by size of meta-data
635 // ensuring 4-byte boundary
636 size += ((m_metadata.GetSerializedSize() + 3) & (~3));
637
638 // add 4-bytes for entry of total length of meta-data
639 size += 4;
640
641 // increment total size by size of buffer
642 // ensuring 4-byte boundary
643 size += ((m_buffer.GetSerializedSize() + 3) & (~3));
644
645 // add 4-bytes for entry of total length of buffer
646 size += 4;
647
648 return size;
649}
650
652Packet::Serialize(uint8_t* buffer, uint32_t maxSize) const
653{
654 auto p = reinterpret_cast<uint32_t*>(buffer);
655 uint32_t size = 0;
656
657 // if nix-vector exists, serialize it
658 if (m_nixVector)
659 {
661 size += nixSize;
662 if (size > maxSize)
663 {
664 return 0;
665 }
666
667 // put the total length of nix-vector in the
668 // buffer. this includes 4-bytes for total
669 // length itself
670 *p++ = nixSize + 4;
671
672 // serialize the nix-vector
673 uint32_t serialized = m_nixVector->Serialize(p, nixSize);
674 if (!serialized)
675 {
676 return 0;
677 }
678
679 // increment p by nixSize bytes
680 // ensuring 4-byte boundary
681 p += ((nixSize + 3) & (~3)) / 4;
682 }
683 else
684 {
685 // no nix vector, set zero length,
686 // ie 4-bytes, since it must include
687 // length for itself
688 size += 4;
689 if (size > maxSize)
690 {
691 return 0;
692 }
693
694 *p++ = 4;
695 }
696
697 // Serialize byte tag list
699 size += byteTagSize;
700 if (size > maxSize)
701 {
702 return 0;
703 }
704
705 // put the total length of byte tag list in the
706 // buffer. this includes 4-bytes for total
707 // length itself
708 *p++ = byteTagSize + 4;
709
710 // serialize the byte tag list
711 uint32_t serialized = m_byteTagList.Serialize(p, byteTagSize);
712 if (!serialized)
713 {
714 return 0;
715 }
716
717 // increment p by byteTagSize bytes
718 // ensuring 4-byte boundary
719 p += ((byteTagSize + 3) & (~3)) / 4;
720
721 // Serialize packet tag list
723 size += packetTagSize;
724 if (size > maxSize)
725 {
726 return 0;
727 }
728
729 // put the total length of packet tag list in the
730 // buffer. this includes 4-bytes for total
731 // length itself
732 *p++ = packetTagSize + 4;
733
734 // serialize the packet tag list
735 serialized = m_packetTagList.Serialize(p, packetTagSize);
736 if (!serialized)
737 {
738 return 0;
739 }
740
741 // increment p by packetTagSize bytes
742 // ensuring 4-byte boundary
743 p += ((packetTagSize + 3) & (~3)) / 4;
744
745 // Serialize Metadata
747 size += metaSize;
748 if (size > maxSize)
749 {
750 return 0;
751 }
752
753 // put the total length of metadata in the
754 // buffer. this includes 4-bytes for total
755 // length itself
756 *p++ = metaSize + 4;
757
758 // serialize the metadata
759 serialized = m_metadata.Serialize(reinterpret_cast<uint8_t*>(p), metaSize);
760 if (!serialized)
761 {
762 return 0;
763 }
764
765 // increment p by metaSize bytes
766 // ensuring 4-byte boundary
767 p += ((metaSize + 3) & (~3)) / 4;
768
769 // Serialize the packet contents
771 size += bufSize;
772 if (size > maxSize)
773 {
774 return 0;
775 }
776
777 // put the total length of the buffer in the
778 // buffer. this includes 4-bytes for total
779 // length itself
780 *p++ = bufSize + 4;
781
782 // serialize the buffer
783 serialized = m_buffer.Serialize(reinterpret_cast<uint8_t*>(p), bufSize);
784 if (!serialized)
785 {
786 return 0;
787 }
788
789 // Serialized successfully
790 return 1;
791}
792
794Packet::Deserialize(const uint8_t* buffer, uint32_t size)
795{
796 NS_LOG_FUNCTION(this);
797
798 auto p = reinterpret_cast<const uint32_t*>(buffer);
799
800 // read nix-vector
802 uint32_t nixSize = *p++;
803
804 // if size less than nixSize, the buffer
805 // will be overrun, assert
806 NS_ASSERT(size >= nixSize);
807
808 if (nixSize > 4)
809 {
811 uint32_t nixDeserialized = nix->Deserialize(p, nixSize);
812 if (!nixDeserialized)
813 {
814 // nix-vector not deserialized
815 // completely
816 return 0;
817 }
818 m_nixVector = nix;
819 // increment p by nixSize ensuring
820 // 4-byte boundary
821 p += ((((nixSize - 4) + 3) & (~3)) / 4);
822 }
823 size -= nixSize;
824
825 // read byte tags
826 uint32_t byteTagSize = *p++;
827
828 // if size less than byteTagSize, the buffer
829 // will be overrun, assert
830 NS_ASSERT(size >= byteTagSize);
831
832 uint32_t byteTagDeserialized = m_byteTagList.Deserialize(p, byteTagSize);
833 if (!byteTagDeserialized)
834 {
835 // byte tags not deserialized completely
836 return 0;
837 }
838 // increment p by byteTagSize ensuring
839 // 4-byte boundary
840 p += ((((byteTagSize - 4) + 3) & (~3)) / 4);
841 size -= byteTagSize;
842
843 // read packet tags
844 uint32_t packetTagSize = *p++;
845
846 // if size less than packetTagSize, the buffer
847 // will be overrun, assert
848 NS_ASSERT(size >= packetTagSize);
849
850 uint32_t packetTagDeserialized = m_packetTagList.Deserialize(p, packetTagSize);
851 if (!packetTagDeserialized)
852 {
853 // packet tags not deserialized completely
854 return 0;
855 }
856 // increment p by packetTagSize ensuring
857 // 4-byte boundary
858 p += ((((packetTagSize - 4) + 3) & (~3)) / 4);
859 size -= packetTagSize;
860
861 // read metadata
862 uint32_t metaSize = *p++;
863
864 // if size less than metaSize, the buffer
865 // will be overrun, assert
866 NS_ASSERT(size >= metaSize);
867
868 uint32_t metadataDeserialized =
869 m_metadata.Deserialize(reinterpret_cast<const uint8_t*>(p), metaSize);
870 if (!metadataDeserialized)
871 {
872 // meta-data not deserialized
873 // completely
874 return 0;
875 }
876 // increment p by metaSize ensuring
877 // 4-byte boundary
878 p += ((((metaSize - 4) + 3) & (~3)) / 4);
879 size -= metaSize;
880
881 // read buffer contents
882 uint32_t bufSize = *p++;
883
884 // if size less than bufSize, the buffer
885 // will be overrun, assert
886 NS_ASSERT(size >= bufSize);
887
888 uint32_t bufferDeserialized =
889 m_buffer.Deserialize(reinterpret_cast<const uint8_t*>(p), bufSize);
890 if (!bufferDeserialized)
891 {
892 // buffer not deserialized
893 // completely
894 return 0;
895 }
896 size -= bufSize;
897
898 // return zero if did not deserialize the
899 // number of expected bytes
900 return (size == 0);
901}
902
903void
904Packet::AddByteTag(const Tag& tag) const
905{
907 auto list = const_cast<ByteTagList*>(&m_byteTagList);
908 TagBuffer buffer = list->Add(tag.GetInstanceTypeId(), tag.GetSerializedSize(), 0, GetSize());
909 tag.Serialize(buffer);
910}
911
912void
913Packet::AddByteTag(const Tag& tag, uint32_t start, uint32_t end) const
914{
916 NS_ABORT_MSG_IF(end < start, "Invalid byte range");
917 auto list = const_cast<ByteTagList*>(&m_byteTagList);
918 TagBuffer buffer = list->Add(tag.GetInstanceTypeId(),
919 tag.GetSerializedSize(),
920 static_cast<int32_t>(start),
921 static_cast<int32_t>(end));
922 tag.Serialize(buffer);
923}
924
930
931bool
933{
934 TypeId tid = tag.GetInstanceTypeId();
936 while (i.HasNext())
937 {
938 ByteTagIterator::Item item = i.Next();
939 if (tid == item.GetTypeId())
940 {
941 item.GetTag(tag);
942 return true;
943 }
944 }
945 return false;
946}
947
948void
949Packet::AddPacketTag(const Tag& tag) const
950{
952 m_packetTagList.Add(tag);
953}
954
955bool
957{
959 bool found = m_packetTagList.Remove(tag);
960 return found;
961}
962
963bool
965{
967 bool found = m_packetTagList.Replace(tag);
968 return found;
969}
970
971bool
973{
974 bool found = m_packetTagList.Peek(tag);
975 return found;
976}
977
978void
984
985void
986Packet::PrintPacketTags(std::ostream& os) const
987{
989 while (i.HasNext())
990 {
991 PacketTagIterator::Item item = i.Next();
993 Callback<ObjectBase*> constructor = item.GetTypeId().GetConstructor();
994 NS_ASSERT(!constructor.IsNull());
995 ObjectBase* instance = constructor();
996 Tag* tag = dynamic_cast<Tag*>(instance);
997 NS_ASSERT(tag != nullptr);
998 item.GetTag(*tag);
999 tag->Print(os);
1000 delete tag;
1001 if (i.HasNext())
1002 {
1003 os << " ";
1004 }
1005 }
1006}
1007
1013
1014std::ostream&
1015operator<<(std::ostream& os, const Packet& packet)
1016{
1017 packet.Print(os);
1018 return os;
1019}
1020
1021} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
void Write(const uint8_t *buffer, uint32_t size)
Definition buffer.cc:937
void Prev()
go backward by one byte
Definition buffer.h:849
void Next()
go forward by one byte
Definition buffer.h:842
automatically resized byte buffer
Definition buffer.h:83
uint32_t GetSerializedSize() const
Return the number of bytes required for serialization.
Definition buffer.cc:555
Buffer CreateFragment(uint32_t start, uint32_t length) const
Definition buffer.cc:518
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:702
void RemoveAtEnd(uint32_t end)
Definition buffer.cc:482
uint32_t GetSize() const
Definition buffer.h:1057
void AddAtStart(uint32_t start)
Definition buffer.cc:303
Buffer::Iterator Begin() const
Definition buffer.h:1063
void AddAtEnd(uint32_t end)
Definition buffer.cc:349
Buffer::Iterator End() const
Definition buffer.h:1070
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Definition buffer.cc:637
void RemoveAtStart(uint32_t start)
Definition buffer.cc:436
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Definition buffer.cc:571
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition packet.h:52
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition packet.h:92
uint32_t GetEnd() const
The index is an offset from the start of the packet.
Definition packet.cc:37
Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer)
Constructor.
Definition packet.cc:52
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition packet.cc:43
uint32_t GetStart() const
The index is an offset from the start of the packet.
Definition packet.cc:31
TypeId GetTypeId() const
Definition packet.cc:25
Iterator over the set of byte tags in a packet.
Definition packet.h:45
ByteTagIterator(ByteTagList::Iterator i)
Copy Constructor.
Definition packet.cc:76
bool HasNext() const
Definition packet.cc:61
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition packet.h:115
An iterator for iterating through a byte tag list.
ByteTagList::Iterator::Item Next()
Returns the next Item from the ByteTagList.
uint32_t GetOffsetStart() const
Returns the offset from the start of the virtual byte buffer to the ByteTagList.
bool HasNext() const
Used to determine if the iterator is at the end of the byteTagList.
keep track of the byte tags stored in a packet.
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Deserialize tag list from the provided buffer.
void AddAtEnd(int32_t appendOffset)
Make sure that all offsets are smaller than appendOffset which represents the location where new byte...
void Adjust(int32_t adjustment)
Adjust the offsets stored internally by the adjustment delta.
ByteTagList::Iterator Begin(int32_t offsetStart, int32_t offsetEnd) const
void RemoveAll()
Removes all of the tags from the ByteTagList.
TagBuffer Add(TypeId tid, uint32_t bufferSize, int32_t start, int32_t end)
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Serialize the tag list into a byte buffer.
void AddAtStart(int32_t prependOffset)
Make sure that all offsets are bigger than prependOffset which represents the location where new byte...
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
Callback template class.
Definition callback.h:422
bool IsNull() const
Check for null implementation.
Definition callback.h:555
abstract base class for ns3::Header and ns3::Trailer
Definition chunk.h:25
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Protocol header serialization and deserialization.
Definition header.h:33
virtual uint32_t GetSerializedSize() const =0
uint32_t Deserialize(Buffer::Iterator start) override=0
virtual void Serialize(Buffer::Iterator start) const =0
uint32_t GetSerializedSize() const
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Ptr< NixVector > Copy() const
Definition nix-vector.cc:58
Anchor the ns-3 type and attribute system.
virtual TypeId GetInstanceTypeId() const =0
Get the most derived TypeId for this Object.
void GetAttribute(std::string name, AttributeValue &value, bool permissive=false) const
Get the value of an attribute, raising fatal errors if unsuccessful.
network packets
Definition packet.h:228
PacketTagIterator GetPacketTagIterator() const
Returns an object which can be used to iterate over the list of packet tags.
Definition packet.cc:1009
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition packet.cc:956
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition packet.cc:283
Buffer m_buffer
the packet buffer (it's actual contents)
Definition packet.h:779
static void EnableChecking()
Enable packets metadata checking.
Definition packet.cc:592
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition packet.cc:343
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition packet.cc:325
PacketMetadata::ItemIterator BeginItem() const
Returns an iterator which points to the first 'item' stored in this buffer.
Definition packet.cc:579
void SetNixVector(Ptr< NixVector > nixVector) const
Set the packet nix-vector.
Definition packet.cc:245
ByteTagList m_byteTagList
the ByteTag list
Definition packet.h:780
Ptr< NixVector > GetNixVector() const
Get the packet nix-vector.
Definition packet.cc:251
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:407
void AddHeader(const Header &header)
Add header to this packet.
Definition packet.cc:257
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
static uint32_t m_globalUid
Global counter of packets Uid.
Definition packet.h:787
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition packet.cc:389
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition packet.cc:365
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Deserializes a packet.
Definition packet.cc:794
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
Definition packet.cc:599
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition packet.cc:373
PacketTagList m_packetTagList
the packet's Tag list
Definition packet.h:781
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition packet.cc:120
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition packet.cc:986
Packet & operator=(const Packet &o)
Basic assignment.
Definition packet.cc:154
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition packet.cc:932
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition packet.cc:652
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
Packet()
Create an empty packet with a new uid (as returned by getUid).
Definition packet.cc:128
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition packet.cc:949
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition packet.cc:294
void RemoveAllPacketTags()
Remove all packet tags.
Definition packet.cc:979
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:335
void Print(std::ostream &os) const
Print the packet contents.
Definition packet.cc:445
uint64_t GetUid() const
Returns the packet's Uid.
Definition packet.cc:401
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition packet.cc:904
std::string ToString() const
Return a string representation of the packet.
Definition packet.cc:437
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition packet.cc:313
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition packet.cc:356
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition packet.cc:972
ByteTagIterator GetByteTagIterator() const
Returns an iterator over the set of byte tags included in this packet.
Definition packet.cc:926
PacketMetadata m_metadata
the packet's metadata
Definition packet.h:782
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition packet.h:785
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition packet.cc:964
Iterator class for metadata items.
bool HasNext() const
Checks if there is another metadata item.
Item Next()
Retrieve the next metadata item.
Handle packet metadata about packet headers and trailers.
uint32_t GetSerializedSize() const
Get the metadata serialized size.
PacketMetadata CreateFragment(uint32_t start, uint32_t end) const
Creates a fragment.
void AddHeader(const Header &header, uint32_t size)
Add an header.
uint64_t GetUid() const
Get the packet Uid.
void AddAtEnd(const PacketMetadata &o)
Add a metadata at the metadata start.
ItemIterator BeginItem(Buffer buffer) const
Initialize the item iterator to the buffer begin.
static void EnableChecking()
Enable the packet metadata checking.
void RemoveAtEnd(uint32_t end)
Remove a chunk of metadata at the metadata end.
void RemoveHeader(const Header &header, uint32_t size)
Remove an header.
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Deserialization from raw uint8_t*.
void AddPaddingAtEnd(uint32_t end)
Add some padding at the end.
void RemoveAtStart(uint32_t start)
Remove a chunk of metadata at the metadata start.
void RemoveTrailer(const Trailer &trailer, uint32_t size)
Remove a trailer.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
void AddTrailer(const Trailer &trailer, uint32_t size)
Add a trailer.
static void Enable()
Enable the packet metadata.
Identifies a packet tag within a packet.
Definition packet.h:131
Item(const PacketTagList::TagData *data)
Constructor.
Definition packet.cc:101
TypeId GetTypeId() const
Definition packet.cc:107
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition packet.cc:113
Iterator over the set of packet tags in a packet.
Definition packet.h:125
const PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition packet.h:175
bool HasNext() const
Definition packet.cc:87
friend class Packet
Friend class.
Definition packet.h:169
PacketTagIterator(const PacketTagList::TagData *head)
Constructor.
Definition packet.cc:81
List of the packet tags stored in a packet.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
void Add(const Tag &tag) const
Add a tag to the head of this branch.
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Deserialize tag list from the provided buffer.
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Serialize the tag list into a byte buffer.
void RemoveAll()
Remove all tags from this list (up to the first merge).
bool Replace(Tag &tag)
Replace the value of a tag.
bool Peek(Tag &tag) const
Find a tag and return its value.
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
const PacketTagList::TagData * Head() const
Smart pointer class similar to boost::intrusive_ptr.
Control the scheduling of simulation events.
Definition simulator.h:57
read and write tag data
Definition tag-buffer.h:41
tag a set of bytes in a packet
Definition tag.h:28
virtual uint32_t GetSerializedSize() const =0
virtual void Serialize(TagBuffer i) const =0
virtual void Print(std::ostream &os) const =0
virtual void Deserialize(TagBuffer i)=0
Protocol trailer serialization and deserialization.
Definition trailer.h:30
virtual void Serialize(Buffer::Iterator start) const =0
uint32_t Deserialize(Buffer::Iterator end) override=0
virtual uint32_t GetSerializedSize() const =0
a unique identifier for an interface.
Definition type-id.h:48
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
Definition type-id.cc:1154
bool HasConstructor() const
Check if this TypeId has a constructor.
Definition type-id.cc:1084
std::size_t GetAttributeN() const
Get the number of attributes.
Definition type-id.cc:1170
std::string GetName() const
Get the name.
Definition type-id.cc:1061
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
uint8_t data[writeSize]
An item specifies an individual tag within a byte buffer.
TagBuffer buf
the data for the tag as generated by Tag::Serialize
int32_t end
offset to the end of the tag from the virtual byte buffer
int32_t start
offset to the start of the tag from the virtual byte buffer
structure describing a packet metadata item
ItemType type
metadata type
TypeId tid
TypeId of Header or Trailer.
bool isFragment
true: this is a fragmented header, trailer, or, payload.
Buffer::Iterator current
an iterator which can be fed to Deserialize.
uint32_t currentTrimmedFromStart
how many bytes were trimmed from the start of a fragment.
uint32_t currentSize
size of item.
Tree node for sharing serialized tags.
TagData * next
Pointer to next in list.
uint32_t prev