src/dsr/model/dsr-fs-header.h
changeset 8751 efad81f3cb47
child 8752 2da1fab73114
equal deleted inserted replaced
8750:b3db7d51f260 8751:efad81f3cb47
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2011 Yufei Cheng
       
     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: Yufei Cheng   <yfcheng@ittc.ku.edu>
       
    19  *
       
    20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
       
    21  * ResiliNets Research Group  http://wiki.ittc.ku.edu/resilinets
       
    22  * Information and Telecommunication Technology Center (ITTC)
       
    23  * and Department of Electrical Engineering and Computer Science
       
    24  * The University of Kansas Lawrence, KS USA.
       
    25  *
       
    26  * Work supported in part by NSF FIND (Future Internet Design) Program
       
    27  * under grant CNS-0626918 (Postmodern Internet Architecture),
       
    28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
       
    29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
       
    30  */
       
    31 
       
    32 #ifndef DSR_FS_HEADER_H
       
    33 #define DSR_FS_HEADER_H
       
    34 
       
    35 #include <vector>
       
    36 #include <list>
       
    37 #include <ostream>
       
    38 
       
    39 #include "ns3/header.h"
       
    40 #include "ns3/ipv4-address.h"
       
    41 #include "dsr-option-header.h"
       
    42 
       
    43 namespace ns3 {
       
    44 namespace dsr {
       
    45 /**
       
    46  * \class DsrHeader
       
    47  * \brief Header for Dsr Routing.
       
    48  */
       
    49 
       
    50 /**
       
    51 * \ingroup dsr
       
    52 * \brief Dsr fixed size header Format
       
    53   \verbatim
       
    54    |      0        |      1        |      2        |      3        |
       
    55    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
       
    56    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    57    |  Next Header |F|     Reservd    |       Payload Length       |
       
    58    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    59    |                            Options                           |
       
    60    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    61   \endverbatim
       
    62 */
       
    63 
       
    64 /**
       
    65 * \ingroup dsr
       
    66 * \brief The modified version of Dsr fixed size header Format
       
    67   \verbatim
       
    68    |      0        |      1        |      2        |      3        |
       
    69    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
       
    70    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    71    |  Next Header |F|  Message Type  |       Payload Length       |
       
    72    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    73    |             Source Id           |            Dest Id         |
       
    74    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    75    |                            Options                           |
       
    76    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    77   \endverbatim
       
    78 */
       
    79 
       
    80 enum DsrMessageType
       
    81 {
       
    82   DSR_CONTROL_PACKET = 1,
       
    83   DSR_DATA_PACKET = 2
       
    84 };
       
    85 
       
    86 class DsrFsHeader : public Header
       
    87 {
       
    88 public:
       
    89   /**
       
    90    * \brief Get the type identificator.
       
    91    * \return type identificator
       
    92    */
       
    93   static TypeId GetTypeId ();
       
    94   /**
       
    95    * \brief Get the instance type ID.
       
    96    * \return instance type ID
       
    97    */
       
    98   virtual TypeId GetInstanceTypeId () const;
       
    99   /**
       
   100    * \brief Constructor.
       
   101    */
       
   102   DsrFsHeader ();
       
   103   /**
       
   104    * \brief Destructor.
       
   105    */
       
   106   virtual ~DsrFsHeader ();
       
   107   /**
       
   108    * \brief Set the "Next header" field.
       
   109    * \param nextHeader the next header number
       
   110    */
       
   111   void SetNextHeader (uint8_t protocol);
       
   112   /**
       
   113    * \brief Get the next header.
       
   114    * \return the next header number
       
   115    */
       
   116   uint8_t GetNextHeader () const;
       
   117   /**
       
   118    * brief Set the message type of the header.
       
   119    * \param message type the message type of the header
       
   120    */
       
   121   void SetMessageType (uint8_t messageType);
       
   122   /**
       
   123    * brief Get the message type of the header.
       
   124    * \return message type the message type of the header
       
   125    */
       
   126   uint8_t GetMessageType () const;
       
   127   /**
       
   128    * brief Set the source id of the header.
       
   129    * \param source id the source id of the header
       
   130    */
       
   131   void SetSourceId (uint16_t sourceId);
       
   132   /**
       
   133    * brief Get the source id of the header.
       
   134    * \return source id the source id of the header
       
   135    */
       
   136   uint16_t GetSourceId () const;
       
   137   /**
       
   138    * brief Set the dest id of the header.
       
   139    * \param dest id the dest id of the header
       
   140    */
       
   141   void SetDestId (uint16_t destId);
       
   142   /**
       
   143    * brief Get the dest id of the header.
       
   144    * \return dest id the dest id of the header
       
   145    */
       
   146   uint16_t GetDestId () const;
       
   147   /**
       
   148    * brief Set the payload length of the header.
       
   149    * \param length the payload length of the header in bytes
       
   150    */
       
   151   void SetPayloadLength (uint16_t length);
       
   152   /**
       
   153    * \brief Get the payload length of the header.
       
   154    * \return the payload length of the header
       
   155    */
       
   156   uint16_t GetPayloadLength () const;
       
   157   /**
       
   158    * \brief Print some informations about the packet.
       
   159    * \param os output stream
       
   160    * \return info about this packet
       
   161    */
       
   162   virtual void Print (std::ostream &os) const;
       
   163   /**
       
   164    * \brief Get the serialized size of the packet.
       
   165    * \return size
       
   166    */
       
   167   virtual uint32_t GetSerializedSize () const;
       
   168   /**
       
   169    * \brief Serialize the packet.
       
   170    * \param start Buffer iterator
       
   171    */
       
   172   virtual void Serialize (Buffer::Iterator start) const;
       
   173   /**
       
   174    * \brief Deserialize the packet.
       
   175    * \param start Buffer iterator
       
   176    * \return size of the packet
       
   177    */
       
   178   virtual uint32_t Deserialize (Buffer::Iterator start);
       
   179 
       
   180 private:
       
   181   /**
       
   182    * \brief The "next header" field.
       
   183    */
       
   184   uint8_t m_nextHeader;
       
   185   /**
       
   186    * \brief The type of the message.
       
   187    */
       
   188   uint8_t m_messageType;
       
   189   /**
       
   190    * \brief The "payload length" field.
       
   191    */
       
   192   uint16_t m_payloadLen;
       
   193   /**
       
   194    * \brief The source node id
       
   195    */
       
   196   uint16_t m_sourceId;
       
   197   /**
       
   198    * \brief The destination node id
       
   199    */
       
   200   uint16_t m_destId;
       
   201   /**
       
   202    * \brief The data of the extension.
       
   203    */
       
   204   Buffer m_data;
       
   205 };
       
   206 
       
   207 /**
       
   208  * \class OptionField
       
   209  * \brief Option field for an DsrFsHeader
       
   210  * Enables adding options to an DsrFsHeader
       
   211  *
       
   212  * Implementor's note: Make sure to add the result of
       
   213  * OptionField::GetSerializedSize () to your DsrFsHeader::GetSerializedSize ()
       
   214  * return value. Call OptionField::Serialize and OptionField::Deserialize at the
       
   215  * end of your corresponding DsrFsHeader methods.
       
   216  */
       
   217 class DsrOptionField
       
   218 {
       
   219 public:
       
   220   /**
       
   221    * \brief Constructor.
       
   222    * \param optionsOffset option offset
       
   223    */
       
   224   DsrOptionField (uint32_t optionsOffset);
       
   225   /**
       
   226    * \brief Destructor.
       
   227    */
       
   228   ~DsrOptionField ();
       
   229   /**
       
   230    * \brief Get the serialized size of the packet.
       
   231    * \return size
       
   232    */
       
   233   uint32_t GetSerializedSize () const;
       
   234   /**
       
   235    * \brief Serialize all added options.
       
   236    * \param start Buffer iterator
       
   237    */
       
   238   void Serialize (Buffer::Iterator start) const;
       
   239   /**
       
   240    * \brief Deserialize the packet.
       
   241    * \param start Buffer iterator
       
   242    * \param length length
       
   243    * \return size of the packet
       
   244    */
       
   245   uint32_t Deserialize (Buffer::Iterator start, uint32_t length);
       
   246   /**
       
   247    * \brief Serialize the option, prepending pad1 or padn option as necessary
       
   248    * \param option the option header to serialize
       
   249    */
       
   250   void AddDsrOption (DsrOptionHeader const& option);
       
   251   /**
       
   252    * \brief Get the offset where the options begin, measured from the start of
       
   253    * the extension header.
       
   254    * \return the offset from the start of the extension header
       
   255    */
       
   256   uint32_t GetDsrOptionsOffset ();
       
   257   /**
       
   258    * \brief Get the buffer.
       
   259    * \return buffer
       
   260    */
       
   261   Buffer GetDsrOptionBuffer ();
       
   262 
       
   263 private:
       
   264   /**
       
   265    * \brief Calculate padding.
       
   266    * \param alignment alignment
       
   267    */
       
   268   uint32_t CalculatePad (DsrOptionHeader::Alignment alignment) const;
       
   269   /**
       
   270    * \brief Data payload.
       
   271    */
       
   272   Buffer m_optionData;
       
   273   /**
       
   274    * \brief Offset.
       
   275    */
       
   276   uint32_t m_optionsOffset;
       
   277 };
       
   278 
       
   279 /**
       
   280  * \class DsrRoutingHeader
       
   281  * \brief Header of Dsr Routing
       
   282  */
       
   283 class DsrRoutingHeader : public DsrFsHeader,
       
   284                          public DsrOptionField
       
   285 {
       
   286 public:
       
   287   /**
       
   288    * \brief Get the type identificator.
       
   289    * \return type identificator
       
   290    */
       
   291   static TypeId GetTypeId ();
       
   292   /**
       
   293    * \brief Get the instance type ID.
       
   294    * \return instance type ID
       
   295    */
       
   296   virtual TypeId GetInstanceTypeId () const;
       
   297   /**
       
   298    * \brief Constructor.
       
   299    */
       
   300   DsrRoutingHeader ();
       
   301   /**
       
   302    * \brief Destructor.
       
   303    */
       
   304   virtual ~DsrRoutingHeader ();
       
   305   /**
       
   306    * \brief Print some informations about the packet.
       
   307    * \param os output stream
       
   308    * \return info about this packet
       
   309    */
       
   310   virtual void Print (std::ostream &os) const;
       
   311   /**
       
   312    * \brief Get the serialized size of the packet.
       
   313    * \return size
       
   314    */
       
   315   virtual uint32_t GetSerializedSize () const;
       
   316   /**
       
   317    * \brief Serialize the packet.
       
   318    * \param start Buffer iterator
       
   319    */
       
   320   virtual void Serialize (Buffer::Iterator start) const;
       
   321   /**
       
   322    * \brief Deserialize the packet.
       
   323    * \param start Buffer iterator
       
   324    * \return size of the packet
       
   325    */
       
   326   virtual uint32_t Deserialize (Buffer::Iterator start);
       
   327 };
       
   328 
       
   329 static inline std::ostream & operator<< (std::ostream& os, const DsrRoutingHeader & dsr)
       
   330 {
       
   331   dsr.Print (os);
       
   332   return os;
       
   333 }
       
   334 
       
   335 }  // namespace dsr
       
   336 }  // namespace ns3
       
   337 
       
   338 #endif /* DSR_FS_HEADER_H */
       
   339