A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
packet.h
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);
88  TypeId m_tid;
89  uint32_t m_start;
90  uint32_t m_end;
91  TagBuffer m_buffer;
92  };
96  bool HasNext (void) const;
100  Item Next (void);
101 private:
102  friend class Packet;
104  ByteTagList::Iterator m_current;
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);
138  const struct PacketTagList::TagData *m_data;
139  };
143  bool HasNext (void) const;
147  Item Next (void);
148 private:
149  friend class Packet;
150  PacketTagIterator (const struct PacketTagList::TagData *head);
151  const struct PacketTagList::TagData *m_current;
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 
348  uint8_t const *PeekData (void) const NS_DEPRECATED;
349 
358  uint32_t CopyData (uint8_t *buffer, uint32_t size) const;
359 
366  void CopyData (std::ostream *os, uint32_t size) const;
367 
375  Ptr<Packet> Copy (void) const;
376 
394  uint64_t GetUid (void) const;
395 
404  void Print (std::ostream &os) const;
405 
415 
423  static void EnablePrinting (void);
432  static void EnableChecking (void);
433 
442  uint32_t GetSerializedSize (void) const;
443 
444  /*
445  * \param buffer a raw byte buffer to which the packet will be serialized
446  * \param maxSize the max size of the buffer for bounds checking
447  *
448  * A packet is completely serialized and placed into the raw byte buffer
449  *
450  * \returns zero if buffer size was too small
451  */
452  uint32_t Serialize (uint8_t* buffer, uint32_t maxSize) const;
453 
471  void AddByteTag (const Tag &tag) const;
475  ByteTagIterator GetByteTagIterator (void) const;
483  bool FindFirstMatchingByteTag (Tag &tag) const;
484 
488  void RemoveAllByteTags (void);
489 
496  void PrintByteTags (std::ostream &os) const;
497 
508  void AddPacketTag (const Tag &tag) const;
517  bool RemovePacketTag (Tag &tag);
525  bool PeekPacketTag (Tag &tag) const;
529  void RemoveAllPacketTags (void);
530 
539  void PrintPacketTags (std::ostream &os) const;
540 
546 
547  /* Note: These functions support a temporary solution
548  * to a specific problem in this generic class, i.e.
549  * how to associate something specific like nix-vector
550  * with a packet. This design methodology
551  * should _not_ be followed, and is only here as an
552  * impetus to fix this general issue. */
553  void SetNixVector (Ptr<NixVector>);
554  Ptr<NixVector> GetNixVector (void) const;
555 
556 private:
557  Packet (const Buffer &buffer, const ByteTagList &byteTagList,
558  const PacketTagList &packetTagList, const PacketMetadata &metadata);
559 
560  uint32_t Deserialize (uint8_t const*buffer, uint32_t size);
561 
562  Buffer m_buffer;
563  ByteTagList m_byteTagList;
564  PacketTagList m_packetTagList;
565  PacketMetadata m_metadata;
566 
567  /* Please see comments above about nix-vector */
568  Ptr<NixVector> m_nixVector;
569 
570  static uint32_t m_globalUid;
571 };
572 
573 std::ostream& operator<< (std::ostream& os, const Packet &packet);
574 
615 } // namespace ns3
616 
617 namespace ns3 {
618 
619 uint32_t
620 Packet::GetSize (void) const
621 {
622  return m_buffer.GetSize ();
623 }
624 
625 } // namespace ns3
626 
627 #endif /* PACKET_H */
bool HasNext(void) const
Definition: packet.cc:69
Protocol header serialization and deserialization.
Definition: header.h:42
uint32_t RemoveHeader(Header &header)
Definition: packet.cc:285
bool FindFirstMatchingByteTag(Tag &tag) const
Definition: packet.cc:850
void PrintPacketTags(std::ostream &os) const
Definition: packet.cc:895
automatically resized byte buffer
Definition: buffer.h:92
void AddPacketTag(const Tag &tag) const
Definition: packet.cc:868
uint64_t GetUid(void) const
Definition: packet.cc:412
keep track of the tags stored in a packet.
Definition: byte-tag-list.h:68
uint32_t GetSize(void) const
Definition: packet.h:620
void Print(std::ostream &os) const
Definition: packet.cc:450
network packets
Definition: packet.h:203
PacketTagIterator GetPacketTagIterator(void) const
Definition: packet.cc:919
void GetTag(Tag &tag) const
Definition: packet.cc:122
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Definition: packet.cc:243
uint32_t PeekTrailer(Trailer &trailer)
Definition: packet.cc:326
void AddAtEnd(Ptr< const Packet > packet)
Definition: packet.cc:334
uint8_t const * PeekData(void) const NS_DEPRECATED
Definition: packet.cc:385
bool PeekPacketTag(Tag &tag) const
Definition: packet.cc:881
void RemoveAllPacketTags(void)
Definition: packet.cc:888
bool HasNext(void) const
Definition: packet.cc:96
void RemoveAtStart(uint32_t size)
Definition: packet.cc:370
static void EnablePrinting(void)
Definition: packet.cc:575
static void EnableChecking(void)
Definition: packet.cc:582
void AddPaddingAtEnd(uint32_t size)
Definition: packet.cc:350
ByteTagIterator GetByteTagIterator(void) const
Definition: packet.cc:843
Item Next(void)
Definition: packet.cc:74
uint32_t GetEnd(void) const
Definition: packet.cc:45
Iterator over the set of 'packet' tags in a packet.
Definition: packet.h:113
void PrintByteTags(std::ostream &os) const
Definition: packet.cc:419
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
Ptr< Packet > Copy(void) const
Definition: packet.cc:131
uint32_t PeekHeader(Header &header) const
Definition: packet.cc:294
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:568
void AddTrailer(const Trailer &trailer)
Definition: packet.cc:301
void RemoveAtEnd(uint32_t size)
Definition: packet.cc:363
Iterator over the set of tags in a packet.
Definition: packet.h:50
uint32_t RemoveTrailer(Trailer &trailer)
Definition: packet.cc:317
TypeId GetTypeId(void) const
Definition: packet.cc:34
uint32_t GetSize(void) const
Definition: buffer.h:869
TypeId GetTypeId(void) const
Definition: packet.cc:117
void GetTag(Tag &tag) const
Definition: packet.cc:51
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:588
bool RemovePacketTag(Tag &tag)
Definition: packet.cc:874
void RemoveAllByteTags(void)
Definition: packet.cc:378
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Definition: packet.cc:398
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:44
void AddHeader(const Header &header)
Definition: packet.cc:270
handle packet metadata about packet headers and trailers
void AddByteTag(const Tag &tag) const
Definition: packet.cc:833