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
38namespace ns3
39{
40
41// Forward declaration
42class 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
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;
165 Item(const struct PacketTagList::TagData* data);
167 };
168
172 bool HasNext() const;
176 Item Next();
177
178 private:
180 friend class Packet;
185 PacketTagIterator(const struct PacketTagList::TagData* head);
186 const struct PacketTagList::TagData*
188};
189
239class Packet : public SimpleRefCount<Packet>
240{
241 public:
246 Packet();
251 Packet(const Packet& o);
257 Packet& operator=(const Packet& o);
269 Packet(uint32_t size);
282 Packet(const uint8_t* buffer, uint32_t size, bool magic);
293 Packet(const uint8_t* buffer, uint32_t size);
311 inline uint32_t GetSize() const;
322 void AddHeader(const Header& header);
344 uint32_t RemoveHeader(Header& header, uint32_t size);
353 uint32_t PeekHeader(Header& header) const;
365 uint32_t PeekHeader(Header& header, uint32_t size) const;
376 void AddTrailer(const Trailer& trailer);
394 uint32_t PeekTrailer(Trailer& trailer);
395
404 void AddAtEnd(Ptr<const Packet> packet);
410 void AddPaddingAtEnd(uint32_t size);
419 void RemoveAtEnd(uint32_t size);
428 void RemoveAtStart(uint32_t size);
429
440 uint32_t CopyData(uint8_t* buffer, uint32_t size) const;
441
450 void CopyData(std::ostream* os, uint32_t size) const;
451
461 Ptr<Packet> Copy() const;
462
482 uint64_t GetUid() const;
483
494 void Print(std::ostream& os) const;
495
503 std::string ToString() const;
504
518
528 static void EnablePrinting();
539 static void EnableChecking();
540
553
562 uint32_t Serialize(uint8_t* buffer, uint32_t maxSize) const;
563
580 void AddByteTag(const Tag& tag) const;
581
601 void AddByteTag(const Tag& tag, uint32_t start, uint32_t end) const;
617 bool FindFirstMatchingByteTag(Tag& tag) const;
618
622 void RemoveAllByteTags();
623
630 void PrintByteTags(std::ostream& os) const;
631
641 void AddPacketTag(const Tag& tag) const;
650 bool RemovePacketTag(Tag& tag);
661 bool ReplacePacketTag(Tag& tag);
669 bool PeekPacketTag(Tag& tag) const;
673 void RemoveAllPacketTags();
674
683 void PrintPacketTags(std::ostream& os) const;
684
693
710 void SetNixVector(Ptr<NixVector> nixVector) const;
719
725 typedef void (*TracedCallback)(Ptr<const Packet> packet);
726
734
742 typedef void (*TwoAddressTracedCallback)(const Ptr<const Packet> packet,
743 const Address& srcAddress,
744 const Address& destAddress);
745
753
760 typedef void (*SizeTracedCallback)(uint32_t oldSize, uint32_t newSize);
761
768 typedef void (*SinrTracedCallback)(Ptr<const Packet> packet, double sinr);
769
770 private:
778 Packet(const Buffer& buffer,
779 const ByteTagList& byteTagList,
780 const PacketTagList& packetTagList,
781 const PacketMetadata& metadata);
782
789 uint32_t Deserialize(const uint8_t* buffer, uint32_t size);
790
795
796 /* Please see comments above about nix-vector */
798
800};
801
809std::ostream& operator<<(std::ostream& os, const Packet& packet);
810
852} // namespace ns3
853
854/****************************************************
855 * Implementation of inline methods for performance
856 ****************************************************/
857
858namespace ns3
859{
860
863{
864 return m_buffer.GetSize();
865}
866
867} // namespace ns3
868
869#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: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:240
void(* SizeTracedCallback)(uint32_t oldSize, uint32_t newSize)
TracedCallback signature for changes in packet size.
Definition: packet.h:760
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:791
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:752
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:792
void(* AddressTracedCallback)(Ptr< const Packet > packet, const Address &address)
TracedCallback signature for packet and Address.
Definition: packet.h:733
void(* TwoAddressTracedCallback)(const Ptr< const Packet > packet, const Address &srcAddress, const Address &destAddress)
TracedCallback signature for packet and source/destination addresses.
Definition: packet.h:742
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:768
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:862
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:799
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:793
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:794
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:797
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:142
TypeId GetTypeId() const
Definition: packet.cc:118
const struct PacketTagList::TagData * m_data
the tag data
Definition: packet.h:166
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:136
const struct 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 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:59
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.