A Discrete-Event Network Simulator
API
packet.h
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
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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
22 
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/callback.h"
33 #include "ns3/assert.h"
34 #include "ns3/ptr.h"
35 #include "ns3/deprecated.h"
36 
37 namespace ns3 {
38 
39 // Forward declaration
40 class Address;
41 
54 {
55 public:
60  class Item
61  {
62 public:
66  TypeId GetTypeId (void) const;
72  uint32_t GetStart (void) const;
78  uint32_t GetEnd (void) const;
87  void GetTag (Tag &tag) const;
88 private:
89  friend class ByteTagIterator;
97  Item (TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer);
98 
100  uint32_t m_start;
101  uint32_t m_end;
103  };
107  bool HasNext (void) const;
111  Item Next (void);
112 private:
113  friend class Packet;
120 };
121 
129 {
130 public:
134  class Item
135  {
136 public:
140  TypeId GetTypeId (void) const;
149  void GetTag (Tag &tag) const;
150 private:
151  friend class PacketTagIterator;
156  Item (const struct PacketTagList::TagData *data);
158  };
162  bool HasNext (void) const;
166  Item Next (void);
167 private:
168  friend class Packet;
173  PacketTagIterator (const struct PacketTagList::TagData *head);
175 };
176 
226 class Packet : public SimpleRefCount<Packet>
227 {
228 public:
229 
234  Packet ();
239  Packet (const Packet &o);
245  Packet &operator = (const Packet &o);
257  Packet (uint32_t size);
270  Packet (uint8_t const*buffer, uint32_t size, bool magic);
281  Packet (uint8_t const*buffer, uint32_t size);
292  Ptr<Packet> CreateFragment (uint32_t start, uint32_t length) const;
299  inline uint32_t GetSize (void) const;
310  void AddHeader (const Header & header);
319  uint32_t RemoveHeader (Header &header);
328  uint32_t PeekHeader (Header &header) const;
339  void AddTrailer (const Trailer &trailer);
348  uint32_t RemoveTrailer (Trailer &trailer);
357  uint32_t PeekTrailer (Trailer &trailer);
358 
367  void AddAtEnd (Ptr<const Packet> packet);
373  void AddPaddingAtEnd (uint32_t size);
382  void RemoveAtEnd (uint32_t size);
391  void RemoveAtStart (uint32_t size);
392 
403  uint32_t CopyData (uint8_t *buffer, uint32_t size) const;
404 
413  void CopyData (std::ostream *os, uint32_t size) const;
414 
424  Ptr<Packet> Copy (void) const;
425 
445  uint64_t GetUid (void) const;
446 
457  void Print (std::ostream &os) const;
458 
466  std::string ToString (void) const;
467 
481 
491  static void EnablePrinting (void);
502  static void EnableChecking (void);
503 
515  uint32_t GetSerializedSize (void) const;
516 
525  uint32_t Serialize (uint8_t* buffer, uint32_t maxSize) const;
526 
543  void AddByteTag (const Tag &tag) const;
549  ByteTagIterator GetByteTagIterator (void) const;
559  bool FindFirstMatchingByteTag (Tag &tag) const;
560 
564  void RemoveAllByteTags (void);
565 
572  void PrintByteTags (std::ostream &os) const;
573 
583  void AddPacketTag (const Tag &tag) const;
592  bool RemovePacketTag (Tag &tag);
603  bool ReplacePacketTag (Tag & tag);
611  bool PeekPacketTag (Tag &tag) const;
615  void RemoveAllPacketTags (void);
616 
625  void PrintPacketTags (std::ostream &os) const;
626 
635 
648  void SetNixVector (Ptr<NixVector> nixVector);
656  Ptr<NixVector> GetNixVector (void) const;
657 
663  typedef void (* TracedCallback) (const Ptr<const Packet> packet);
664 
671  typedef void (* PacketAddressTracedCallback)
672  (const Ptr<const Packet> packet, const Address &address);
673 
680  typedef void (* PacketSizeTracedCallback)
681  (const uint32_t oldSize, const uint32_t newSize);
682 
683 private:
691  Packet (const Buffer &buffer, const ByteTagList &byteTagList,
692  const PacketTagList &packetTagList, const PacketMetadata &metadata);
693 
694  uint32_t Deserialize (uint8_t const*buffer, uint32_t size);
695 
700 
701  /* Please see comments above about nix-vector */
703 
704  static uint32_t m_globalUid;
705 };
706 
714 std::ostream& operator<< (std::ostream& os, const Packet &packet);
715 
757 } // namespace ns3
758 
759 /****************************************************
760  * Implementation of inline methods for performance
761  ****************************************************/
762 
763 namespace ns3 {
764 
765 uint32_t
766 Packet::GetSize (void) const
767 {
768  return m_buffer.GetSize ();
769 }
770 
771 } // namespace ns3
772 
773 #endif /* PACKET_H */
bool HasNext(void) const
Definition: packet.cc:65
Protocol header serialization and deserialization.
Definition: header.h:42
PacketMetadata m_metadata
the packet's metadata
Definition: packet.h:699
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:819
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:871
uint32_t Deserialize(uint8_t const *buffer, uint32_t size)
Definition: packet.cc:721
const struct PacketTagList::TagData * m_data
the tag data
Definition: packet.h:157
automatically resized byte buffer
Definition: buffer.h:92
def start()
Definition: core.py:1482
Forward calls to a chain of Callback.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:836
List of the packet tags stored in a packet.
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:380
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:603
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:68
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:704
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
Buffer m_buffer
the packet buffer (it's actual contents)
Definition: packet.h:696
Ptr< NixVector > GetNixVector(void) const
Get the packet nix-vector.
Definition: packet.cc:247
Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer)
Constructor.
Definition: packet.cc:57
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:424
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:157
network packets
Definition: packet.h:226
Item Next(void)
Definition: packet.cc:94
PacketTagIterator GetPacketTagIterator(void) const
Returns an object which can be used to iterate over the list of packet tags.
Definition: packet.cc:894
a polymophic address class
Definition: address.h:90
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition: packet.h:119
Packet()
Create an empty packet with a new uid (as returned by getUid).
Definition: packet.cc:130
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:112
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:228
uint32_t PeekTrailer(Trailer &trailer)
Deserialize but does not remove a trailer from the internal buffer.
Definition: packet.cc:309
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:317
Tree node for sharing serialized tags.
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:858
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:864
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:60
bool HasNext(void) const
Definition: packet.cc:89
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:353
void(* PacketAddressTracedCallback)(const Ptr< const Packet > packet, const Address &address)
TracedCallback signature for packet and address.
Definition: packet.h:672
TagBuffer m_buffer
the buffer associated with this tag.
Definition: packet.h:102
std::string ToString(void) const
Return a string representation of the packet.
Definition: packet.cc:416
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:547
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:99
void SetNixVector(Ptr< NixVector > nixVector)
Set the packet nix-vector.
Definition: packet.cc:241
ByteTagList m_byteTagList
the ByteTag list
Definition: packet.h:697
Defines a linked list of Packet tags, including copy-on-write semantics.
uint8_t data[writeSize]
Iterator class for metadata items.
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:850
static void EnableChecking(void)
Enable packets metadata checking.
Definition: packet.cc:554
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:333
ByteTagIterator GetByteTagIterator(void) const
Retiurns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:813
void(* PacketSizeTracedCallback)(const uint32_t oldSize, const uint32_t newSize)
TracedCallback signature for changes in packet size.
Definition: packet.h:681
Item Next(void)
Definition: packet.cc:70
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:44
Iterator over the set of packet tags in a packet.
Definition: packet.h:128
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:386
const struct PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:174
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:277
uint32_t m_end
the index of the last byte tagged by this tag.
Definition: packet.h:101
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
tag a set of bytes in a packet
Definition: tag.h:36
Every class exported by the ns3 library is enclosed in the ns3 namespace.
PacketMetadata::ItemIterator BeginItem(void) const
Returns an iterator which points to the first 'item' stored in this buffer.
Definition: packet.cc:541
PacketTagIterator(const struct PacketTagList::TagData *head)
Constructor.
Definition: packet.cc:84
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:284
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:346
Iterator over the set of byte tags in a packet.
Definition: packet.h:53
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:300
PacketTagList m_packetTagList
the packet's Tag list
Definition: packet.h:698
TypeId GetTypeId(void) const
Definition: packet.cc:34
Identifies a packet tag within a packet.
Definition: packet.h:134
uint32_t GetSize(void) const
Definition: buffer.h:1070
TypeId GetTypeId(void) const
Definition: packet.cc:107
Item(const struct PacketTagList::TagData *data)
Constructor.
Definition: packet.cc:102
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:49
An iterator for iterating through a byte tag list.
Definition: byte-tag-list.h:77
read and write tag data
Definition: tag-buffer.h:51
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
Definition: packet.cc:39
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet.cc:560
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:843
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:361
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:702
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:368
uint32_t m_start
the index of the first byte tagged by this tag.
Definition: packet.h:100
ByteTagIterator(ByteTagList::Iterator i)
Copy Constructor.
Definition: packet.cc:78
tuple address
Definition: first.py:37
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:57
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
Handle packet metadata about packet headers and trailers.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:803