network packets More...
#include <packet.h>
Public Member Functions | |
void | Ref (void) const |
void | Unref (void) const |
Ptr< Packet > | Copy (void) const |
Packet () | |
Packet (const Packet &o) | |
Packet & | operator= (const Packet &o) |
Packet (uint32_t size) | |
Packet (uint8_t const *buffer, uint32_t size) | |
Ptr< Packet > | CreateFragment (uint32_t start, uint32_t length) const |
uint32_t | GetSize (void) const |
void | AddHeader (const Header &header) |
uint32_t | RemoveHeader (Header &header) |
uint32_t | PeekHeader (Header &header) const |
void | AddTrailer (const Trailer &trailer) |
uint32_t | RemoveTrailer (Trailer &trailer) |
uint32_t | PeekTrailer (Trailer &trailer) |
void | PrintTags (std::ostream &os) const |
void | AddAtEnd (Ptr< const Packet > packet) |
void | AddPaddingAtEnd (uint32_t size) |
void | RemoveAtEnd (uint32_t size) |
void | RemoveAtStart (uint32_t size) |
uint8_t const * | PeekData (void) const |
uint32_t | CopyData (uint8_t *buffer, uint32_t size) const |
uint32_t | GetUid (void) const |
void | Print (std::ostream &os) const |
PacketMetadata::ItemIterator | BeginItem (void) const |
Buffer | Serialize (void) const |
void | Deserialize (Buffer buffer) |
void | AddTag (const Tag &tag) const |
TagIterator | GetTagIterator (void) const |
bool | FindFirstMatchingTag (Tag &tag) const |
void | RemoveAllTags (void) |
Static Public Member Functions | |
static void | EnableMetadata (void) NS_DEPRECATED |
static void | EnablePrinting (void) |
static void | EnableChecking (void) |
Private Member Functions | |
Packet (const Buffer &buffer, const TagList &tagList, const PacketMetadata &metadata) | |
Private Attributes | |
Buffer | m_buffer |
TagList | m_tagList |
PacketMetadata | m_metadata |
uint32_t | m_refCount |
Static Private Attributes | |
static uint32_t | m_globalUid = 0 |
network packets
Each network packet contains a byte buffer, a set of tags, and metadata.
Implementing a new type of Header or Trailer for a new protocol is pretty easy and is a matter of creating a subclass of the ns3::Header or of the ns3::Trailer base class, and implementing the methods described in their respective API documentation.
Implementing a new type of Tag requires roughly the same amount of work and this work is described in the ns3::Tag API documentation.
The performance aspects of the Packet API are discussed in Packet Performance
Definition at line 140 of file packet.h.
ns3::Packet::Packet | ( | ) |
Create an empty packet with a new uid (as returned by getUid).
Definition at line 103 of file packet.cc.
References m_globalUid.
Referenced by Copy(), and CreateFragment().
ns3::Packet::Packet | ( | uint32_t | size | ) |
Create a packet with a zero-filled payload. The memory necessary for the payload is not allocated: it will be allocated at any later point if you attempt to fragment this packet or to access the zero-filled bytes. The packet is allocated with a new uid (as returned by getUid).
size | the size of the zero-filled payload |
Definition at line 132 of file packet.cc.
References m_globalUid.
ns3::Packet::Packet | ( | uint8_t const * | buffer, | |
uint32_t | size | |||
) |
Create a packet with payload filled with the content of this buffer. The input data is copied: the input buffer is untouched.
buffer | the data to store in the packet. | |
size | the size of the input buffer. |
Definition at line 140 of file packet.cc.
References ns3::Buffer::AddAtStart(), ns3::Buffer::Begin(), m_buffer, m_globalUid, and ns3::Buffer::Iterator::Write().
ns3::Packet::Packet | ( | const Buffer & | buffer, | |
const TagList & | tagList, | |||
const PacketMetadata & | metadata | |||
) | [private] |
Concatenate the input packet at the end of the current packet. This does not alter the uid of either packet.
packet | packet to concatenate |
Definition at line 243 of file packet.cc.
References ns3::TagList::Add(), ns3::PacketMetadata::AddAtEnd(), ns3::TagList::AddAtEnd(), ns3::Buffer::AddAtEnd(), ns3::TagList::AddAtStart(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), m_buffer, m_metadata, m_tagList, and NS_LOG_FUNCTION.
void ns3::Packet::AddHeader | ( | const Header & | header | ) |
Add header to this packet. This method invokes the Header::GetSerializedSize and Header::Serialize methods to reserve space in the buffer and request the header to serialize itself in the packet buffer.
header | a reference to the header to add to this packet. |
Definition at line 179 of file packet.cc.
References ns3::TagList::AddAtStart(), ns3::Buffer::AddAtStart(), ns3::PacketMetadata::AddHeader(), ns3::Buffer::Begin(), ns3::Buffer::GetCurrentStartOffset(), ns3::Header::GetSerializedSize(), m_buffer, m_metadata, m_tagList, NS_LOG_FUNCTION, and ns3::Header::Serialize().
Referenced by ns3::OlsrHeaderTest::RunTests().
void ns3::Packet::AddPaddingAtEnd | ( | uint32_t | size | ) |
size | number of padding bytes to add. |
Definition at line 259 of file packet.cc.
References ns3::TagList::AddAtEnd(), ns3::Buffer::AddAtEnd(), ns3::PacketMetadata::AddPaddingAtEnd(), ns3::Buffer::GetCurrentEndOffset(), m_buffer, m_metadata, m_tagList, and NS_LOG_FUNCTION.
void ns3::Packet::AddTag | ( | const Tag & | tag | ) | const |
tag | the new tag to add to this packet |
Tag each byte included in this packet with the new tag.
Note that adding a tag is a const operation which is pretty un-intuitive. The rationale is that the content and behavior of a packet is _not_ changed when a tag is added to a packet: any code which was not aware of the new tag is going to work just the same if the new tag is added. The real reason why adding a tag was made a const operation is to allow a trace sink which gets a packet to tag the packet, even if the packet is const (and most trace sources should use const packets because it would be totally evil to allow a trace sink to modify the content of a packet).
Definition at line 547 of file packet.cc.
References ns3::TagList::Add(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), ns3::ObjectBase::GetInstanceTypeId(), ns3::Tag::GetSerializedSize(), m_buffer, m_tagList, NS_LOG_FUNCTION, and ns3::Tag::Serialize().
Referenced by ns3::PacketTest::RunTests().
void ns3::Packet::AddTrailer | ( | const Trailer & | trailer | ) |
Add trailer to this packet. This method invokes the Trailer::GetSerializedSize and Trailer::Serialize methods to reserve space in the buffer and request the trailer to serialize itself in the packet buffer.
trailer | a reference to the trailer to add to this packet. |
Definition at line 210 of file packet.cc.
References ns3::TagList::AddAtEnd(), ns3::Buffer::AddAtEnd(), ns3::PacketMetadata::AddTrailer(), ns3::Buffer::End(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), ns3::Trailer::GetSerializedSize(), m_buffer, m_metadata, m_tagList, NS_LOG_FUNCTION, and ns3::Trailer::Serialize().
PacketMetadata::ItemIterator ns3::Packet::BeginItem | ( | void | ) | const |
Definition at line 466 of file packet.cc.
References ns3::PacketMetadata::BeginItem(), m_buffer, and m_metadata.
uint32_t ns3::Packet::CopyData | ( | uint8_t * | buffer, | |
uint32_t | size | |||
) | const |
buffer | a pointer to a byte buffer where the packet data should be copied. | |
size | the size of the byte buffer. |
No more than size bytes will be copied by this function.
Definition at line 300 of file packet.cc.
References ns3::Buffer::Begin(), ns3::Buffer::Iterator::IsEnd(), m_buffer, and ns3::Buffer::Iterator::ReadU8().
Create a new packet which contains a fragment of the original packet. The returned packet shares the same uid as this packet.
start | offset from start of packet to start of fragment to create | |
length | length of fragment to create |
Definition at line 160 of file packet.cc.
References ns3::PacketMetadata::CreateFragment(), ns3::Buffer::CreateFragment(), ns3::Buffer::GetSize(), m_buffer, m_metadata, m_tagList, NS_ASSERT, NS_LOG_FUNCTION, and Packet().
void ns3::Packet::Deserialize | ( | Buffer | buffer | ) |
buffer | a byte buffer |
This method reads a byte buffer as created by Packet::Serialize and restores the state of the Packet to what it was prior to calling Serialize.
This method will trigger calls to the Deserialize method of each tag stored in this packet.
This method will typically be used by parallel simulations where the simulated system is partitioned and each partition runs on a different CPU.
Definition at line 521 of file packet.cc.
References ns3::Buffer::Begin(), ns3::PacketMetadata::Deserialize(), ns3::Buffer::GetSize(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::Buffer::Iterator::ReadU32(), ns3::Buffer::RemoveAtEnd(), and ns3::Buffer::RemoveAtStart().
void ns3::Packet::EnableChecking | ( | void | ) | [static] |
The packet metadata is also used to perform extensive sanity checks at runtime when performing operations on a Packet. For example, this metadata is used to verify that when you remove a header from a packet, this same header was actually present at the front of the packet. These errors will be detected and will abort the program.
Definition at line 486 of file packet.cc.
References NS_LOG_FUNCTION_NOARGS.
Referenced by EnableMetadata().
void ns3::Packet::EnableMetadata | ( | void | ) | [static] |
Definition at line 472 of file packet.cc.
References EnableChecking(), and NS_LOG_FUNCTION_NOARGS.
void ns3::Packet::EnablePrinting | ( | void | ) | [static] |
By default, packets do not keep around enough metadata to perform the operations requested by the Print methods. If you want to be able to invoke any of the two Print methods, you need to invoke this method at least once during the simulation setup and before any packet is created.
Definition at line 479 of file packet.cc.
References ns3::PacketMetadata::Enable(), and NS_LOG_FUNCTION_NOARGS.
Referenced by ns3::YansWifiPhyHelper::EnableAscii(), ns3::Ns2ExtWifiPhyHelper::EnableAscii(), and ns3::EmuHelper::EnableAscii().
bool ns3::Packet::FindFirstMatchingTag | ( | Tag & | tag | ) | const |
tag | the tag to search in this packet |
If the requested tag type is found, it is copied in the user's provided tag instance.
Definition at line 563 of file packet.cc.
References ns3::ObjectBase::GetInstanceTypeId(), ns3::TagIterator::Item::GetTag(), GetTagIterator(), ns3::TagIterator::Item::GetTypeId(), ns3::TagIterator::HasNext(), and ns3::TagIterator::Next().
uint32_t ns3::Packet::GetSize | ( | void | ) | const |
Definition at line 173 of file packet.cc.
References ns3::Buffer::GetSize(), and m_buffer.
Referenced by ns3::OlsrHeaderTest::RunTests().
TagIterator ns3::Packet::GetTagIterator | ( | void | ) | const |
Definition at line 557 of file packet.cc.
References ns3::TagList::Begin(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), m_buffer, and m_tagList.
Referenced by FindFirstMatchingTag(), and PrintTags().
uint32_t ns3::Packet::GetUid | ( | void | ) | const |
A packet is allocated a new uid when it is created empty or with zero-filled payload.
Note: This uid is an internal uid and cannot be counted on to provide an accurate counter of how many "simulated packets" of a particular protocol are in the system. It is not trivial to make this uid into such a counter, because of questions such as what should the uid be when the packet is sent over broadcast media, or when fragmentation occurs. If a user wants to trace actual packet counts, he or she should look at e.g. the IP ID field or transport sequence numbers, or other packet or frame counters at other protocol layers.
Definition at line 313 of file packet.cc.
References ns3::PacketMetadata::GetUid(), and m_metadata.
Definition at line 120 of file packet.cc.
References m_buffer, m_metadata, and m_tagList.
uint8_t const * ns3::Packet::PeekData | ( | void | ) | const |
If you try to change the content of the buffer returned by this method, you will die.
Definition at line 294 of file packet.cc.
References m_buffer, and ns3::Buffer::PeekData().
uint32_t ns3::Packet::PeekHeader | ( | Header & | header | ) | const |
Deserialize but does _not_ remove the header from the internal buffer. This method invokes Header::Deserialize.
header | a reference to the header to read from the internal buffer. |
Definition at line 203 of file packet.cc.
References ns3::Buffer::Begin(), ns3::Header::Deserialize(), m_buffer, and NS_LOG_FUNCTION.
uint32_t ns3::Packet::PeekTrailer | ( | Trailer & | trailer | ) |
Deserialize but does _not_ remove a trailer from the internal buffer. This method invokes the Trailer::Deserialize method.
trailer | a reference to the trailer to read from the internal buffer. |
Definition at line 235 of file packet.cc.
References ns3::Trailer::Deserialize(), ns3::Buffer::End(), m_buffer, and NS_LOG_FUNCTION.
void ns3::Packet::Print | ( | std::ostream & | os | ) | const |
os | output stream in which the data should be printed. |
Iterate over the headers and trailers present in this packet, from the first header to the last trailer and invoke, for each of them, the user-provided method Header::DoPrint or Trailer::DoPrint methods.
Definition at line 349 of file packet.cc.
References ns3::PacketMetadata::BeginItem(), ns3::PacketMetadata::Item::current, ns3::PacketMetadata::Item::currentSize, ns3::PacketMetadata::Item::currentTrimedFromStart, ns3::Chunk::Deserialize(), ns3::ObjectBase::GetAttribute(), ns3::TypeId::GetAttributeN(), ns3::TypeId::GetAttributeName(), ns3::TypeId::GetConstructor(), ns3::TypeId::GetName(), ns3::TypeId::HasConstructor(), ns3::PacketMetadata::ItemIterator::HasNext(), ns3::PacketMetadata::Item::HEADER, ns3::PacketMetadata::Item::isFragment, m_buffer, m_metadata, ns3::PacketMetadata::ItemIterator::Next(), NS_ASSERT, ns3::PacketMetadata::Item::PAYLOAD, ns3::Chunk::Print(), ns3::PacketMetadata::Item::tid, ns3::PacketMetadata::Item::TRAILER, and ns3::PacketMetadata::Item::type.
void ns3::Packet::PrintTags | ( | std::ostream & | os | ) | const |
os | output stream in which the data should be printed. |
Iterate over the tags present in this packet, and invoke the Print method of each tag stored in the packet.
Definition at line 319 of file packet.cc.
References ns3::TypeId::GetConstructor(), ns3::TagIterator::Item::GetEnd(), ns3::TypeId::GetName(), ns3::TagIterator::Item::GetStart(), ns3::TagIterator::Item::GetTag(), GetTagIterator(), ns3::TagIterator::Item::GetTypeId(), ns3::TagIterator::HasNext(), ns3::Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 >::IsNull(), ns3::TagIterator::Next(), NS_ASSERT, and ns3::Tag::Print().
void ns3::Packet::Ref | ( | void | ) | const |
Definition at line 80 of file packet.cc.
References m_refCount.
void ns3::Packet::RemoveAllTags | ( | void | ) |
Remove all the tags stored in this packet.
Definition at line 287 of file packet.cc.
References m_tagList, NS_LOG_FUNCTION, and ns3::TagList::RemoveAll().
void ns3::Packet::RemoveAtEnd | ( | uint32_t | size | ) |
Remove size bytes from the end of the current packet It is safe to remove more bytes that what is present in the packet.
size | number of bytes from remove |
Definition at line 272 of file packet.cc.
References m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::PacketMetadata::RemoveAtEnd(), and ns3::Buffer::RemoveAtEnd().
void ns3::Packet::RemoveAtStart | ( | uint32_t | size | ) |
Remove size bytes from the start of the current packet. It is safe to remove more bytes that what is present in the packet.
size | number of bytes from remove |
Definition at line 279 of file packet.cc.
References m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::PacketMetadata::RemoveAtStart(), and ns3::Buffer::RemoveAtStart().
uint32_t ns3::Packet::RemoveHeader | ( | Header & | header | ) |
Deserialize and remove the header from the internal buffer. This method invokes Header::Deserialize.
header | a reference to the header to remove from the internal buffer. |
Definition at line 194 of file packet.cc.
References ns3::Buffer::Begin(), ns3::Header::Deserialize(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::Buffer::RemoveAtStart(), and ns3::PacketMetadata::RemoveHeader().
Referenced by ns3::OlsrHeaderTest::RunTests().
uint32_t ns3::Packet::RemoveTrailer | ( | Trailer & | trailer | ) |
Remove a deserialized trailer from the internal buffer. This method invokes the Deserialize method.
trailer | a reference to the trailer to remove from the internal buffer. |
Definition at line 226 of file packet.cc.
References ns3::Trailer::Deserialize(), ns3::Buffer::End(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::Buffer::RemoveAtEnd(), and ns3::PacketMetadata::RemoveTrailer().
Buffer ns3::Packet::Serialize | ( | void | ) | const |
This method creates a serialized representation of a Packet object ready to be transmitted over a network to another system. This serialized representation contains a copy of the packet byte buffer, the tag list, and the packet metadata (if there is one).
This method will trigger calls to the Serialize and GetSerializedSize methods of each tag stored in this packet.
This method will typically be used by parallel simulations where the simulated system is partitioned and each partition runs on a different CPU.
Definition at line 493 of file packet.cc.
References ns3::Buffer::AddAtEnd(), ns3::Buffer::AddAtStart(), ns3::Buffer::Begin(), ns3::Buffer::CreateFullCopy(), ns3::PacketMetadata::GetSerializedSize(), ns3::Buffer::GetSize(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::PacketMetadata::Serialize(), and ns3::Buffer::Iterator::WriteU32().
void ns3::Packet::Unref | ( | void | ) | const |
Definition at line 85 of file packet.cc.
References m_refCount.
Buffer ns3::Packet::m_buffer [private] |
Definition at line 414 of file packet.h.
Referenced by AddAtEnd(), AddHeader(), AddPaddingAtEnd(), AddTag(), AddTrailer(), BeginItem(), CopyData(), CreateFragment(), Deserialize(), GetSize(), GetTagIterator(), operator=(), Packet(), PeekData(), PeekHeader(), PeekTrailer(), Print(), RemoveAtEnd(), RemoveAtStart(), RemoveHeader(), RemoveTrailer(), and Serialize().
uint32_t ns3::Packet::m_globalUid = 0 [static, private] |
PacketMetadata ns3::Packet::m_metadata [private] |
Definition at line 416 of file packet.h.
Referenced by AddAtEnd(), AddHeader(), AddPaddingAtEnd(), AddTrailer(), BeginItem(), CreateFragment(), Deserialize(), GetUid(), operator=(), Print(), RemoveAtEnd(), RemoveAtStart(), RemoveHeader(), RemoveTrailer(), and Serialize().
uint32_t ns3::Packet::m_refCount [mutable, private] |
TagList ns3::Packet::m_tagList [private] |
Definition at line 415 of file packet.h.
Referenced by AddAtEnd(), AddHeader(), AddPaddingAtEnd(), AddTag(), AddTrailer(), CreateFragment(), GetTagIterator(), operator=(), and RemoveAllTags().