A Discrete-Event Network Simulator
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
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 #ifndef PACKET_H
21 #define PACKET_H
23 #include <stdint.h>
24 #include "buffer.h"
25 #include "header.h"
26 #include "trailer.h"
27 #include "packet-metadata.h"
28 #include "tag.h"
29 #include "byte-tag-list.h"
30 #include "packet-tag-list.h"
31 #include "nix-vector.h"
32 #include "ns3/mac48-address.h"
33 #include "ns3/callback.h"
34 #include "ns3/assert.h"
35 #include "ns3/ptr.h"
36 #include "ns3/deprecated.h"
38 namespace ns3 {
40 // Forward declaration
41 class Address;
55 {
56 public:
61  class Item
62  {
63 public:
67  TypeId GetTypeId (void) const;
73  uint32_t GetStart (void) const;
79  uint32_t GetEnd (void) const;
88  void GetTag (Tag &tag) const;
89 private:
91  friend class ByteTagIterator;
99  Item (TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer);
102  uint32_t m_start;
103  uint32_t m_end;
105  };
109  bool HasNext (void) const;
113  Item Next (void);
114 private:
116  friend class Packet;
123 };
132 {
133 public:
137  class Item
138  {
139 public:
143  TypeId GetTypeId (void) const;
152  void GetTag (Tag &tag) const;
153 private:
155  friend class PacketTagIterator;
160  Item (const struct PacketTagList::TagData *data);
162  };
166  bool HasNext (void) const;
170  Item Next (void);
171 private:
173  friend class Packet;
178  PacketTagIterator (const struct PacketTagList::TagData *head);
180 };
231 class Packet : public SimpleRefCount<Packet>
232 {
233 public:
239  Packet ();
244  Packet (const Packet &o);
250  Packet &operator = (const Packet &o);
262  Packet (uint32_t size);
275  Packet (uint8_t const*buffer, uint32_t size, bool magic);
286  Packet (uint8_t const*buffer, uint32_t size);
297  Ptr<Packet> CreateFragment (uint32_t start, uint32_t length) const;
304  inline uint32_t GetSize (void) const;
315  void AddHeader (const Header & header);
325  uint32_t RemoveHeader (Header &header);
337  uint32_t RemoveHeader (Header &header, uint32_t size);
346  uint32_t PeekHeader (Header &header) const;
358  uint32_t PeekHeader (Header &header, uint32_t size) const;
369  void AddTrailer (const Trailer &trailer);
378  uint32_t RemoveTrailer (Trailer &trailer);
387  uint32_t PeekTrailer (Trailer &trailer);
397  void AddAtEnd (Ptr<const Packet> packet);
403  void AddPaddingAtEnd (uint32_t size);
412  void RemoveAtEnd (uint32_t size);
421  void RemoveAtStart (uint32_t size);
433  uint32_t CopyData (uint8_t *buffer, uint32_t size) const;
443  void CopyData (std::ostream *os, uint32_t size) const;
454  Ptr<Packet> Copy (void) const;
475  uint64_t GetUid (void) const;
487  void Print (std::ostream &os) const;
496  std::string ToString (void) const;
521  static void EnablePrinting (void);
532  static void EnableChecking (void);
545  uint32_t GetSerializedSize (void) const;
555  uint32_t Serialize (uint8_t* buffer, uint32_t maxSize) const;
573  void AddByteTag (const Tag &tag) const;
594  void AddByteTag (const Tag &tag, uint32_t start, uint32_t end) const;
600  ByteTagIterator GetByteTagIterator (void) const;
610  bool FindFirstMatchingByteTag (Tag &tag) const;
615  void RemoveAllByteTags (void);
623  void PrintByteTags (std::ostream &os) const;
634  void AddPacketTag (const Tag &tag) const;
643  bool RemovePacketTag (Tag &tag);
654  bool ReplacePacketTag (Tag & tag);
662  bool PeekPacketTag (Tag &tag) const;
666  void RemoveAllPacketTags (void);
676  void PrintPacketTags (std::ostream &os) const;
703  void SetNixVector (Ptr<NixVector> nixVector) const;
711  Ptr<NixVector> GetNixVector (void) const;
718  typedef void (* TracedCallback) (Ptr<const Packet> packet);
726  typedef void (* AddressTracedCallback)
727  (Ptr<const Packet> packet, const Address &address);
736  typedef void (* TwoAddressTracedCallback)
737  (const Ptr<const Packet> packet, const Address &srcAddress, const Address &destAddress);
745  typedef void (* Mac48AddressTracedCallback)
754  typedef void (* SizeTracedCallback)
755  (uint32_t oldSize, uint32_t newSize);
763  typedef void (* SinrTracedCallback)
764  (Ptr<const Packet> packet, double sinr);
767 private:
775  Packet (const Buffer &buffer, const ByteTagList &byteTagList,
776  const PacketTagList &packetTagList, const PacketMetadata &metadata);
784  uint32_t Deserialize (uint8_t const*buffer, uint32_t size);
791  /* Please see comments above about nix-vector */
794  static uint32_t m_globalUid;
795 };
804 std::ostream& operator<< (std::ostream& os, const Packet &packet);
847 } // namespace ns3
849 /****************************************************
850  * Implementation of inline methods for performance
851  ****************************************************/
853 namespace ns3 {
855 uint32_t
856 Packet::GetSize (void) const
857 {
858  return m_buffer.GetSize ();
859 }
861 } // namespace ns3
863 #endif /* PACKET_H */
a polymophic address class
Definition: address.h:91
automatically resized byte buffer
Definition: buffer.h:93
uint32_t GetSize(void) const
Definition: buffer.h:1063
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:62
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:101
uint32_t m_start
the index of the first byte tagged by this tag.
Definition: packet.h:102
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:44
TagBuffer m_buffer
the buffer associated with this tag.
Definition: packet.h:104
TypeId GetTypeId(void) const
Definition: packet.cc:34
Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer)
Definition: packet.cc:57
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:49
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:39
uint32_t m_end
the index of the last byte tagged by this tag.
Definition: packet.h:103
Iterator over the set of byte tags in a packet.
Definition: packet.h:55
ByteTagIterator(ByteTagList::Iterator i)
Copy Constructor.
Definition: packet.cc:78
bool HasNext(void) const
Definition: packet.cc:65
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition: packet.h:122
Item Next(void)
Definition: packet.cc:70
An iterator for iterating through a byte tag list.
Definition: byte-tag-list.h:73
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:64
Protocol header serialization and deserialization.
Definition: header.h:43
an EUI-48 address
Definition: mac48-address.h:44
network packets
Definition: packet.h:232
void(* SizeTracedCallback)(uint32_t oldSize, uint32_t newSize)
TracedCallback signature for changes in packet size.
Definition: packet.h:755
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
Buffer m_buffer
the packet buffer (it's actual contents)
Definition: packet.h:786
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
void(* Mac48AddressTracedCallback)(Ptr< const Packet > packet, Mac48Address mac)
TracedCallback signature for packet and Mac48Address.
Definition: packet.h:746
uint32_t Deserialize(uint8_t const *buffer, uint32_t size)
Deserializes a packet.
Definition: packet.cc:800
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:318
void SetNixVector(Ptr< NixVector > nixVector) const
Set the packet nix-vector.
Definition: packet.cc:244
ByteTagList m_byteTagList
the ByteTag list
Definition: packet.h:787
void(* AddressTracedCallback)(Ptr< const Packet > packet, const Address &address)
TracedCallback signature for packet and Address.
Definition: packet.h:727
Ptr< NixVector > GetNixVector(void) const
Get the packet nix-vector.
Definition: packet.cc:250
void(* TwoAddressTracedCallback)(const Ptr< const Packet > packet, const Address &srcAddress, const Address &destAddress)
TracedCallback signature for packet and source/destination addresses.
Definition: packet.h:737
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:396
void(* SinrTracedCallback)(Ptr< const Packet > packet, double sinr)
TracedCallback signature for packet and SINR.
Definition: packet.h:764
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:794
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:355
std::string ToString(void) const
Return a string representation of the packet.
Definition: packet.cc:426
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:984
PacketTagIterator GetPacketTagIterator(void) const
Returns an object which can be used to iterate over the list of packet tags.
Definition: packet.cc:1014
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:362
PacketTagList m_packetTagList
the packet's Tag list
Definition: packet.h:788
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:991
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:156
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:939
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:638
ByteTagIterator GetByteTagIterator(void) const
Returns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:933
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
static void EnableChecking(void)
Enable packets metadata checking.
Definition: packet.cc:579
Create an empty packet with a new uid (as returned by getUid).
Definition: packet.cc:129
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:956
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
PacketMetadata::ItemIterator BeginItem(void) const
Returns an iterator which points to the first 'item' stored in this buffer.
Definition: packet.cc:566
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:327
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:434
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:912
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:347
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet.cc:585
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:978
PacketMetadata m_metadata
the packet's metadata
Definition: packet.h:789
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:792
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:970
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
Iterator class for metadata items.
Handle packet metadata about packet headers and trailers.
Identifies a packet tag within a packet.
Definition: packet.h:138
const struct PacketTagList::TagData * m_data
the tag data
Definition: packet.h:161
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:112
Item(const struct PacketTagList::TagData *data)
Definition: packet.cc:102
TypeId GetTypeId(void) const
Definition: packet.cc:107
Iterator over the set of packet tags in a packet.
Definition: packet.h:132
Item Next(void)
Definition: packet.cc:94
const struct PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:179
PacketTagIterator(const struct PacketTagList::TagData *head)
Definition: packet.cc:84
bool HasNext(void) const
Definition: packet.cc:89
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:37
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
Definition: first.py:44
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:139
Definition: third.py:99
def start()
Definition: core.py:1853
Defines a linked list of Packet tags, including copy-on-write semantics.
uint8_t data[writeSize]
Tree node for sharing serialized tags.