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/deprecated.h"
34 #include "ns3/mac48-address.h"
35 #include "ns3/ptr.h"
36 
37 #include <stdint.h>
38 
39 namespace ns3
40 {
41 
42 // Forward declaration
43 class Address;
44 
57 {
58  public:
63  class Item
64  {
65  public:
69  TypeId GetTypeId() const;
75  uint32_t GetStart() const;
81  uint32_t GetEnd() const;
90  void GetTag(Tag& tag) const;
91 
92  private:
94  friend class ByteTagIterator;
102  Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer);
103 
105  uint32_t m_start;
106  uint32_t m_end;
108  };
109 
113  bool HasNext() const;
117  Item Next();
118 
119  private:
121  friend class Packet;
128 };
129 
137 {
138  public:
142  class Item
143  {
144  public:
148  TypeId GetTypeId() const;
157  void GetTag(Tag& tag) const;
158 
159  private:
161  friend class PacketTagIterator;
166  Item(const struct PacketTagList::TagData* data);
168  };
169 
173  bool HasNext() const;
177  Item Next();
178 
179  private:
181  friend class Packet;
186  PacketTagIterator(const struct PacketTagList::TagData* head);
187  const struct PacketTagList::TagData*
189 };
190 
240 class Packet : public SimpleRefCount<Packet>
241 {
242  public:
247  Packet();
252  Packet(const Packet& o);
258  Packet& operator=(const Packet& o);
270  Packet(uint32_t size);
283  Packet(const uint8_t* buffer, uint32_t size, bool magic);
294  Packet(const uint8_t* buffer, uint32_t size);
305  Ptr<Packet> CreateFragment(uint32_t start, uint32_t length) const;
312  inline uint32_t GetSize() const;
323  void AddHeader(const Header& header);
333  uint32_t RemoveHeader(Header& header);
345  uint32_t RemoveHeader(Header& header, uint32_t size);
354  uint32_t PeekHeader(Header& header) const;
366  uint32_t PeekHeader(Header& header, uint32_t size) const;
377  void AddTrailer(const Trailer& trailer);
386  uint32_t RemoveTrailer(Trailer& trailer);
395  uint32_t PeekTrailer(Trailer& trailer);
396 
405  void AddAtEnd(Ptr<const Packet> packet);
411  void AddPaddingAtEnd(uint32_t size);
420  void RemoveAtEnd(uint32_t size);
429  void RemoveAtStart(uint32_t size);
430 
441  uint32_t CopyData(uint8_t* buffer, uint32_t size) const;
442 
451  void CopyData(std::ostream* os, uint32_t size) const;
452 
462  Ptr<Packet> Copy() const;
463 
483  uint64_t GetUid() const;
484 
495  void Print(std::ostream& os) const;
496 
504  std::string ToString() const;
505 
519 
529  static void EnablePrinting();
540  static void EnableChecking();
541 
553  uint32_t GetSerializedSize() const;
554 
563  uint32_t Serialize(uint8_t* buffer, uint32_t maxSize) const;
564 
581  void AddByteTag(const Tag& tag) const;
582 
602  void AddByteTag(const Tag& tag, uint32_t start, uint32_t end) const;
618  bool FindFirstMatchingByteTag(Tag& tag) const;
619 
623  void RemoveAllByteTags();
624 
631  void PrintByteTags(std::ostream& os) const;
632 
642  void AddPacketTag(const Tag& tag) const;
651  bool RemovePacketTag(Tag& tag);
662  bool ReplacePacketTag(Tag& tag);
670  bool PeekPacketTag(Tag& tag) const;
674  void RemoveAllPacketTags();
675 
684  void PrintPacketTags(std::ostream& os) const;
685 
694 
711  void SetNixVector(Ptr<NixVector> nixVector) const;
720 
726  typedef void (*TracedCallback)(Ptr<const Packet> packet);
727 
734  typedef void (*AddressTracedCallback)(Ptr<const Packet> packet, const Address& address);
735 
743  typedef void (*TwoAddressTracedCallback)(const Ptr<const Packet> packet,
744  const Address& srcAddress,
745  const Address& destAddress);
746 
754 
761  typedef void (*SizeTracedCallback)(uint32_t oldSize, uint32_t newSize);
762 
769  typedef void (*SinrTracedCallback)(Ptr<const Packet> packet, double sinr);
770 
771  private:
779  Packet(const Buffer& buffer,
780  const ByteTagList& byteTagList,
781  const PacketTagList& packetTagList,
782  const PacketMetadata& metadata);
783 
790  uint32_t Deserialize(const uint8_t* buffer, uint32_t size);
791 
796 
797  /* Please see comments above about nix-vector */
799 
800  static uint32_t m_globalUid;
801 };
802 
810 std::ostream& operator<<(std::ostream& os, const Packet& packet);
811 
853 } // namespace ns3
854 
855 /****************************************************
856  * Implementation of inline methods for performance
857  ****************************************************/
858 
859 namespace ns3
860 {
861 
862 uint32_t
864 {
865  return m_buffer.GetSize();
866 }
867 
868 } // namespace ns3
869 
870 #endif /* PACKET_H */
a polymophic address class
Definition: address.h:100
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:64
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:104
uint32_t m_start
the index of the first byte tagged by this tag.
Definition: packet.h:105
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:107
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:106
TypeId GetTypeId() const
Definition: packet.cc:36
Iterator over the set of byte tags in a packet.
Definition: packet.h:57
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:127
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:241
void(* SizeTracedCallback)(uint32_t oldSize, uint32_t newSize)
TracedCallback signature for changes in packet size.
Definition: packet.h:761
PacketTagIterator GetPacketTagIterator() const
Returns an object which can be used to iterate over the list of packet tags.
Definition: packet.cc:1039
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:986
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:792
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:753
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:793
void(* AddressTracedCallback)(Ptr< const Packet > packet, const Address &address)
TracedCallback signature for packet and Address.
Definition: packet.h:734
void(* TwoAddressTracedCallback)(const Ptr< const Packet > packet, const Address &srcAddress, const Address &destAddress)
TracedCallback signature for packet and source/destination addresses.
Definition: packet.h:743
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:769
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:863
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:800
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:824
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:794
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:1016
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:962
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:979
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:1009
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:934
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:1002
ByteTagIterator GetByteTagIterator() const
Returns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:956
PacketMetadata m_metadata
the packet's metadata
Definition: packet.h:795
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:798
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:994
Iterator class for metadata items.
Handle packet metadata about packet headers and trailers.
Identifies a packet tag within a packet.
Definition: packet.h:143
TypeId GetTypeId() const
Definition: packet.cc:118
const struct PacketTagList::TagData * m_data
the tag data
Definition: packet.h:167
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:124
Item(const struct PacketTagList::TagData *data)
Constructor.
Definition: packet.cc:112
Iterator over the set of packet tags in a packet.
Definition: packet.h:137
const struct PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:187
bool HasNext() const
Definition: packet.cc:98
PacketTagIterator(const struct 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:60
address
Definition: first.py:40
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:129
mac
Definition: third.py:85
Defines a linked list of Packet tags, including copy-on-write semantics.
uint8_t data[writeSize]
Tree node for sharing serialized tags.