A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
51 {
52 public:
57  class Item
58  {
59 public:
63  TypeId GetTypeId (void) const;
69  uint32_t GetStart (void) const;
75  uint32_t GetEnd (void) const;
84  void GetTag (Tag &tag) const;
85 private:
86  friend class ByteTagIterator;
87  Item (TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer);
89  uint32_t m_start;
90  uint32_t m_end;
92  };
96  bool HasNext (void) const;
100  Item Next (void);
101 private:
102  friend class Packet;
105 };
106 
114 {
115 public:
119  class Item
120  {
121 public:
125  TypeId GetTypeId (void) const;
134  void GetTag (Tag &tag) const;
135 private:
136  friend class PacketTagIterator;
137  Item (const struct PacketTagList::TagData *data);
139  };
143  bool HasNext (void) const;
147  Item Next (void);
148 private:
149  friend class Packet;
150  PacketTagIterator (const struct PacketTagList::TagData *head);
152 };
153 
203 class Packet : public SimpleRefCount<Packet>
204 {
205 public:
206 
211  Packet ();
212  Packet (const Packet &o);
213  Packet &operator = (const Packet &o);
224  Packet (uint32_t size);
235  Packet (uint8_t const*buffer, uint32_t size, bool magic);
244  Packet (uint8_t const*buffer, uint32_t size);
253  Ptr<Packet> CreateFragment (uint32_t start, uint32_t length) const;
258  inline uint32_t GetSize (void) const;
267  void AddHeader (const Header & header);
275  uint32_t RemoveHeader (Header &header);
283  uint32_t PeekHeader (Header &header) const;
292  void AddTrailer (const Trailer &trailer);
300  uint32_t RemoveTrailer (Trailer &trailer);
308  uint32_t PeekTrailer (Trailer &trailer);
309 
316  void AddAtEnd (Ptr<const Packet> packet);
320  void AddPaddingAtEnd (uint32_t size);
328  void RemoveAtEnd (uint32_t size);
336  void RemoveAtStart (uint32_t size);
337 
351  uint8_t const *PeekData (void) const NS_DEPRECATED;
352 
363  uint32_t CopyData (uint8_t *buffer, uint32_t size) const;
364 
373  void CopyData (std::ostream *os, uint32_t size) const;
374 
382  Ptr<Packet> Copy (void) const;
383 
401  uint64_t GetUid (void) const;
402 
411  void Print (std::ostream &os) const;
412 
422 
430  static void EnablePrinting (void);
439  static void EnableChecking (void);
440 
449  uint32_t GetSerializedSize (void) const;
450 
459  uint32_t Serialize (uint8_t* buffer, uint32_t maxSize) const;
460 
477  void AddByteTag (const Tag &tag) const;
481  ByteTagIterator GetByteTagIterator (void) const;
489  bool FindFirstMatchingByteTag (Tag &tag) const;
490 
494  void RemoveAllByteTags (void);
495 
502  void PrintByteTags (std::ostream &os) const;
503 
513  void AddPacketTag (const Tag &tag) const;
522  bool RemovePacketTag (Tag &tag);
533  bool ReplacePacketTag (Tag & tag);
541  bool PeekPacketTag (Tag &tag) const;
545  void RemoveAllPacketTags (void);
546 
555  void PrintPacketTags (std::ostream &os) const;
556 
562 
579  Ptr<NixVector> GetNixVector (void) const;
580 
581 private:
582  Packet (const Buffer &buffer, const ByteTagList &byteTagList,
583  const PacketTagList &packetTagList, const PacketMetadata &metadata);
584 
585  uint32_t Deserialize (uint8_t const*buffer, uint32_t size);
586 
591 
592  /* Please see comments above about nix-vector */
594 
595  static uint32_t m_globalUid;
596 };
597 
598 std::ostream& operator<< (std::ostream& os, const Packet &packet);
599 
641 } // namespace ns3
642 
643 /****************************************************
644  * Implementation of inline methods for performance
645  ****************************************************/
646 
647 namespace ns3 {
648 
649 uint32_t
650 Packet::GetSize (void) const
651 {
652  return m_buffer.GetSize ();
653 }
654 
655 } // namespace ns3
656 
657 #endif /* PACKET_H */
bool HasNext(void) const
Definition: packet.cc:65
Protocol header serialization and deserialization.
Definition: header.h:42
PacketMetadata m_metadata
Definition: packet.h:590
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
bool FindFirstMatchingByteTag(Tag &tag) const
Definition: packet.cc:824
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:876
uint32_t Deserialize(uint8_t const *buffer, uint32_t size)
Definition: packet.cc:726
const struct PacketTagList::TagData * m_data
Definition: packet.h:138
automatically resized byte buffer
Definition: buffer.h:92
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
List of the packet tags stored in a packet.
uint64_t GetUid(void) const
A packet is allocated a new uid when it is created empty or with zero-filled payload.
Definition: packet.cc:393
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:608
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:68
static uint32_t m_globalUid
Definition: packet.h:595
uint32_t GetSize(void) const
Definition: packet.h:650
Buffer m_buffer
Definition: packet.h:587
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)
Definition: packet.cc:57
void Print(std::ostream &os) const
Definition: packet.cc:429
Packet & operator=(const Packet &o)
Definition: packet.cc:157
network packets
Definition: packet.h:203
Item Next(void)
Definition: packet.cc:94
PacketTagIterator GetPacketTagIterator(void) const
Definition: packet.cc:899
ByteTagList::Iterator m_current
Definition: packet.h:104
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.
uint8_t const * PeekData(void) const NS_DEPRECATED
Definition: packet.cc:368
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:863
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:869
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:57
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
#define NS_DEPRECATED
Definition: deprecated.h:7
static void EnablePrinting(void)
By default, packets do not keep around enough metadata to perform the operations requested by the Pri...
Definition: packet.cc:552
void SetNixVector(Ptr< NixVector >)
Set the packet nix-vector.
Definition: packet.cc:241
ByteTagList m_byteTagList
Definition: packet.h:588
Defines a linked list of Packet tags, including copy-on-write semantics.
uint8_t data[writeSize]
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:855
static void EnableChecking(void)
The packet metadata is also used to perform extensive sanity checks at runtime when performing operat...
Definition: packet.cc:559
void AddPaddingAtEnd(uint32_t size)
Definition: packet.cc:333
ByteTagIterator GetByteTagIterator(void) const
Definition: packet.cc:818
Item Next(void)
Definition: packet.cc:70
uint32_t GetEnd(void) const
Definition: packet.cc:44
Iterator over the set of packet tags in a packet.
Definition: packet.h:113
void PrintByteTags(std::ostream &os) const
Definition: packet.cc:399
const struct PacketTagList::TagData * m_current
Definition: packet.h:151
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
Ptr< Packet > Copy(void) const
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
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
tag a set of bytes in a packet
Definition: tag.h:36
PacketMetadata::ItemIterator BeginItem(void) const
Definition: packet.cc:546
PacketTagIterator(const struct PacketTagList::TagData *head)
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 It is safe to remove more bytes than are present...
Definition: packet.cc:346
Iterator over the set of byte tags in a packet.
Definition: packet.h:50
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:300
PacketTagList m_packetTagList
Definition: packet.h:589
TypeId GetTypeId(void) const
Definition: packet.cc:34
Identifies a packet tag within a packet.
Definition: packet.h:119
uint32_t GetSize(void) const
Definition: buffer.h:869
TypeId GetTypeId(void) const
Definition: packet.cc:107
Item(const struct PacketTagList::TagData *data)
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
read and write tag data
Definition: tag-buffer.h:51
uint32_t GetStart(void) const
Definition: packet.cc:39
uint32_t GetSerializedSize(void) const
Definition: packet.cc:565
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:848
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:361
Ptr< NixVector > m_nixVector
Definition: packet.h:593
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:381
ByteTagIterator(ByteTagList::Iterator i)
Definition: packet.cc:78
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:49
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:808