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 
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. */
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 
566 
567  /* Please see comments above about nix-vector */
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 */