A Discrete-Event Network Simulator
API
packet.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005,2006 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #ifndef PACKET_H
20 #define PACKET_H
21 
22 #include "buffer.h"
23 #include "byte-tag-list.h"
24 #include "header.h"
25 #include "nix-vector.h"
26 #include "packet-metadata.h"
27 #include "packet-tag-list.h"
28 #include "tag.h"
29 #include "trailer.h"
30 
31 #include "ns3/assert.h"
32 #include "ns3/callback.h"
33 #include "ns3/mac48-address.h"
34 #include "ns3/ptr.h"
35 
36 #include <stdint.h>
37 
38 namespace ns3
39 {
40 
41 // Forward declaration
42 class Address;
43 
56 {
57  public:
62  class Item
63  {
64  public:
68  TypeId GetTypeId() const;
74  uint32_t GetStart() const;
80  uint32_t GetEnd() const;
89  void GetTag(Tag& tag) const;
90 
91  private:
93  friend class ByteTagIterator;
101  Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer);
102 
104  uint32_t m_start;
105  uint32_t m_end;
107  };
108 
112  bool HasNext() const;
116  Item Next();
117 
118  private:
120  friend class Packet;
127 };
128 
136 {
137  public:
141  class Item
142  {
143  public:
147  TypeId GetTypeId() const;
156  void GetTag(Tag& tag) const;
157 
158  private:
160  friend class PacketTagIterator;
167  };
168 
172  bool HasNext() const;
176  Item Next();
177 
178  private:
180  friend class Packet;
187 };
188 
238 class Packet : public SimpleRefCount<Packet>
239 {
240  public:
245  Packet();
250  Packet(const Packet& o);
256  Packet& operator=(const Packet& o);
268  Packet(uint32_t size);
281  Packet(const uint8_t* buffer, uint32_t size, bool magic);
292  Packet(const uint8_t* buffer, uint32_t size);
303  Ptr<Packet> CreateFragment(uint32_t start, uint32_t length) const;
310  inline uint32_t GetSize() const;
321  void AddHeader(const Header& header);
331  uint32_t RemoveHeader(Header& header);
343  uint32_t RemoveHeader(Header& header, uint32_t size);
352  uint32_t PeekHeader(Header& header) const;
364  uint32_t PeekHeader(Header& header, uint32_t size) const;
375  void AddTrailer(const Trailer& trailer);
384  uint32_t RemoveTrailer(Trailer& trailer);
393  uint32_t PeekTrailer(Trailer& trailer);
394 
403  void AddAtEnd(Ptr<const Packet> packet);
409  void AddPaddingAtEnd(uint32_t size);
418  void RemoveAtEnd(uint32_t size);
427  void RemoveAtStart(uint32_t size);
428 
439  uint32_t CopyData(uint8_t* buffer, uint32_t size) const;
440 
449  void CopyData(std::ostream* os, uint32_t size) const;
450 
460  Ptr<Packet> Copy() const;
461 
481  uint64_t GetUid() const;
482 
493  void Print(std::ostream& os) const;
494 
502  std::string ToString() const;
503 
517 
527  static void EnablePrinting();
538  static void EnableChecking();
539 
551  uint32_t GetSerializedSize() const;
552 
561  uint32_t Serialize(uint8_t* buffer, uint32_t maxSize) const;
562 
579  void AddByteTag(const Tag& tag) const;
580 
600  void AddByteTag(const Tag& tag, uint32_t start, uint32_t end) const;
616  bool FindFirstMatchingByteTag(Tag& tag) const;
617 
621  void RemoveAllByteTags();
622 
629  void PrintByteTags(std::ostream& os) const;
630 
640  void AddPacketTag(const Tag& tag) const;
649  bool RemovePacketTag(Tag& tag);
660  bool ReplacePacketTag(Tag& tag);
668  bool PeekPacketTag(Tag& tag) const;
672  void RemoveAllPacketTags();
673 
682  void PrintPacketTags(std::ostream& os) const;
683 
692 
709  void SetNixVector(Ptr<NixVector> nixVector) const;
718 
724  typedef void (*TracedCallback)(Ptr<const Packet> packet);
725 
732  typedef void (*AddressTracedCallback)(Ptr<const Packet> packet, const Address& address);
733 
741  typedef void (*TwoAddressTracedCallback)(const Ptr<const Packet> packet,
742  const Address& srcAddress,
743  const Address& destAddress);
744 
752 
759  typedef void (*SizeTracedCallback)(uint32_t oldSize, uint32_t newSize);
760 
767  typedef void (*SinrTracedCallback)(Ptr<const Packet> packet, double sinr);
768 
769  private:
777  Packet(const Buffer& buffer,
778  const ByteTagList& byteTagList,
779  const PacketTagList& packetTagList,
780  const PacketMetadata& metadata);
781 
788  uint32_t Deserialize(const uint8_t* buffer, uint32_t size);
789 
794 
795  /* Please see comments above about nix-vector */
797 
798  static uint32_t m_globalUid;
799 };
800 
808 std::ostream& operator<<(std::ostream& os, const Packet& packet);
809 
851 } // namespace ns3
852 
853 /****************************************************
854  * Implementation of inline methods for performance
855  ****************************************************/
856 
857 namespace ns3
858 {
859 
860 uint32_t
862 {
863  return m_buffer.GetSize();
864 }
865 
866 } // namespace ns3
867 
868 #endif /* PACKET_H */
a polymophic address class
Definition: address.h:101
automatically resized byte buffer
Definition: buffer.h:94
uint32_t GetSize() const
Definition: buffer.h:1068
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:63
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:103
uint32_t m_start
the index of the first byte tagged by this tag.
Definition: packet.h:104
uint32_t GetEnd() const
The index is an offset from the start of the packet.
Definition: packet.cc:48
TagBuffer m_buffer
the buffer associated with this tag.
Definition: packet.h:106
Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer)
Constructor.
Definition: packet.cc:63
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:54
uint32_t GetStart() const
The index is an offset from the start of the packet.
Definition: packet.cc:42
uint32_t m_end
the index of the last byte tagged by this tag.
Definition: packet.h:105
TypeId GetTypeId() const
Definition: packet.cc:36
Iterator over the set of byte tags in a packet.
Definition: packet.h:56
ByteTagIterator(ByteTagList::Iterator i)
Copy Constructor.
Definition: packet.cc:87
bool HasNext() const
Definition: packet.cc:72
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition: packet.h:126
An iterator for iterating through a byte tag list.
Definition: byte-tag-list.h:75
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:66
Protocol header serialization and deserialization.
Definition: header.h:44
an EUI-48 address
Definition: mac48-address.h:46
network packets
Definition: packet.h:239
void(* SizeTracedCallback)(uint32_t oldSize, uint32_t newSize)
TracedCallback signature for changes in packet size.
Definition: packet.h:759
PacketTagIterator GetPacketTagIterator() const
Returns an object which can be used to iterate over the list of packet tags.
Definition: packet.cc:1020
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:967
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
Buffer m_buffer
the packet buffer (it's actual contents)
Definition: packet.h:790
static void EnableChecking()
Enable packets metadata checking.
Definition: packet.cc:603
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:354
void(* Mac48AddressTracedCallback)(Ptr< const Packet > packet, Mac48Address mac)
TracedCallback signature for packet and Mac48Address.
Definition: packet.h:751
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:336
PacketMetadata::ItemIterator BeginItem() const
Returns an iterator which points to the first 'item' stored in this buffer.
Definition: packet.cc:590
void SetNixVector(Ptr< NixVector > nixVector) const
Set the packet nix-vector.
Definition: packet.cc:256
ByteTagList m_byteTagList
the ByteTag list
Definition: packet.h:791
void(* AddressTracedCallback)(Ptr< const Packet > packet, const Address &address)
TracedCallback signature for packet and Address.
Definition: packet.h:732
void(* TwoAddressTracedCallback)(const Ptr< const Packet > packet, const Address &srcAddress, const Address &destAddress)
TracedCallback signature for packet and source/destination addresses.
Definition: packet.h:741
Ptr< NixVector > GetNixVector() const
Get the packet nix-vector.
Definition: packet.cc:262
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:418
void(* SinrTracedCallback)(Ptr< const Packet > packet, double sinr)
TracedCallback signature for packet and SINR.
Definition: packet.h:767
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:798
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:376
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Deserializes a packet.
Definition: packet.cc:805
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
Definition: packet.cc:610
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:384
PacketTagList m_packetTagList
the packet's Tag list
Definition: packet.h:792
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:997
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:165
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:943
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:663
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
Packet()
Create an empty packet with a new uid (as returned by getUid).
Definition: packet.cc:139
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:960
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:305
void RemoveAllPacketTags()
Remove all packet tags.
Definition: packet.cc:990
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:238
uint32_t PeekTrailer(Trailer &trailer)
Deserialize but does not remove a trailer from the internal buffer.
Definition: packet.cc:346
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:456
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:915
std::string ToString() const
Return a string representation of the packet.
Definition: packet.cc:448
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:324
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:367
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:983
ByteTagIterator GetByteTagIterator() const
Returns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:937
PacketMetadata m_metadata
the packet's metadata
Definition: packet.h:793
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:796
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:975
Iterator class for metadata items.
Handle packet metadata about packet headers and trailers.
Identifies a packet tag within a packet.
Definition: packet.h:142
Item(const PacketTagList::TagData *data)
Constructor.
Definition: packet.cc:112
TypeId GetTypeId() const
Definition: packet.cc:118
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:124
const PacketTagList::TagData * m_data
the tag data
Definition: packet.h:166
Iterator over the set of packet tags in a packet.
Definition: packet.h:136
const PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:186
bool HasNext() const
Definition: packet.cc:98
PacketTagIterator(const PacketTagList::TagData *head)
Constructor.
Definition: packet.cc:92
List of the packet tags stored in a packet.
A template-based reference counting class.
read and write tag data
Definition: tag-buffer.h:52
tag a set of bytes in a packet
Definition: tag.h:39
Forward calls to a chain of Callback.
Protocol trailer serialization and deserialization.
Definition: trailer.h:41
a unique identifier for an interface.
Definition: type-id.h:59
address
Definition: first.py:47
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:159
mac
Definition: third.py:92
Defines a linked list of Packet tags, including copy-on-write semantics.
uint8_t data[writeSize]
Tree node for sharing serialized tags.