src/node/ipv6-header.cc
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
child 4731 510db8599bfb
permissions -rw-r--r--
Initial IPv6 capability
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2007-2008 Louis Pasteur University
     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: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
    19  */
    20 
    21 #include "ns3/assert.h"
    22 #include "ns3/log.h"
    23 #include "ns3/header.h"
    24 #include "address-utils.h"
    25 #include "ipv6-header.h"
    26 
    27 NS_LOG_COMPONENT_DEFINE ("Ipv6Header");
    28 
    29 namespace ns3 {
    30 
    31 NS_OBJECT_ENSURE_REGISTERED (Ipv6Header);
    32 
    33 Ipv6Header::Ipv6Header ()
    34 : m_version (6),
    35   m_trafficClass (0),
    36   m_flowLabel (1),
    37   m_payloadLength (0),
    38   m_nextHeader (0),
    39   m_hopLimit (0)
    40 {
    41 
    42   SetSourceAddress (Ipv6Address("::"));
    43   SetDestinationAddress(Ipv6Address ("::"));
    44 }
    45 
    46 void Ipv6Header::SetTrafficClass (uint8_t traffic)
    47 {
    48   m_trafficClass = traffic;
    49 }
    50 
    51 uint8_t Ipv6Header::GetTrafficClass () const
    52 {
    53   return m_trafficClass;
    54 }
    55 
    56 void Ipv6Header::SetFlowLabel (uint32_t flow)
    57 {
    58   m_flowLabel = flow;
    59 }
    60 
    61 uint32_t Ipv6Header::GetFlowLabel () const
    62 {
    63   return m_flowLabel;
    64 }
    65 
    66 void Ipv6Header::SetPayloadLength (uint16_t len)
    67 {
    68   m_payloadLength = len;
    69 }
    70 
    71 uint16_t Ipv6Header::GetPayloadLength () const
    72 {
    73   return m_payloadLength;
    74 }
    75 
    76 void Ipv6Header::SetNextHeader (uint8_t next)
    77 {
    78   m_nextHeader = next;
    79 }
    80 
    81 uint8_t Ipv6Header::GetNextHeader () const
    82 {
    83   return m_nextHeader;
    84 }
    85 
    86 void Ipv6Header::SetHopLimit (uint8_t limit)
    87 {
    88   m_hopLimit = limit;
    89 }
    90 
    91 uint8_t Ipv6Header::GetHopLimit () const
    92 {
    93   return m_hopLimit;
    94 }
    95 
    96 void Ipv6Header::SetSourceAddress (Ipv6Address src)
    97 {
    98   m_sourceAddress = src;
    99 }
   100 
   101 Ipv6Address Ipv6Header::GetSourceAddress () const
   102 {
   103   return m_sourceAddress;
   104 }
   105 
   106 void Ipv6Header::SetDestinationAddress (Ipv6Address dst)
   107 {
   108   m_destinationAddress = dst;
   109 }
   110 
   111 Ipv6Address Ipv6Header::GetDestinationAddress () const
   112 {
   113   return m_destinationAddress;
   114 }
   115 
   116 TypeId Ipv6Header::GetTypeId (void)
   117 {
   118   static TypeId tid = TypeId ("ns3::Ipv6Header")
   119     .SetParent<Header> ()
   120     .AddConstructor<Ipv6Header> ()
   121     ;
   122   return tid;
   123 }
   124 
   125 TypeId Ipv6Header::GetInstanceTypeId (void) const
   126 {
   127   return GetTypeId ();
   128 }
   129 
   130 void Ipv6Header::Print (std::ostream& os) const
   131 {
   132   os << "("
   133     "Version " << m_version << " "
   134     << "Traffic class 0x" << std::hex << m_trafficClass << std::dec << " "
   135     << "Flow Label 0x" << std::hex << m_flowLabel << std::dec << " "
   136     << "Payload Length " << m_payloadLength << " "
   137     << "Next Header " << std::dec << (uint32_t) m_nextHeader << " "
   138     << "Hop Limit " << std::dec << (uint32_t)m_hopLimit << " )"
   139     <<  m_sourceAddress << " > " <<  m_destinationAddress
   140     ;
   141 }
   142 
   143 uint32_t Ipv6Header::GetSerializedSize () const
   144 {
   145   return 10 * 4;
   146 }
   147 
   148 void Ipv6Header::Serialize (Buffer::Iterator start) const
   149 {
   150   Buffer::Iterator i = start;
   151   uint32_t vTcFl = 0; /* version, Traffic Class and Flow Label fields */
   152 
   153   vTcFl= (6 << 28) | (m_trafficClass << 20) | (m_flowLabel);
   154 
   155   i.WriteHtonU32(vTcFl);
   156   i.WriteHtonU16(m_payloadLength);
   157   i.WriteU8(m_nextHeader);
   158   i.WriteU8(m_hopLimit);
   159 
   160   WriteTo(i, m_sourceAddress);
   161   WriteTo(i, m_destinationAddress);
   162 }
   163 
   164 uint32_t Ipv6Header::Deserialize (Buffer::Iterator start)
   165 {
   166   Buffer::Iterator i = start;
   167   uint32_t vTcFl = 0;
   168 
   169   vTcFl = i.ReadNtohU32();
   170   m_version = vTcFl >> 28;
   171 
   172   NS_ASSERT((m_version) == 6);
   173 
   174   m_trafficClass = (uint8_t)((vTcFl >> 20) & 0x000000ff);
   175   m_flowLabel = vTcFl & 0xfff00000;
   176   m_payloadLength = i.ReadNtohU16();
   177   m_nextHeader = i.ReadU8();
   178   m_hopLimit = i.ReadU8();
   179 
   180   ReadFrom(i, m_sourceAddress);
   181   ReadFrom(i, m_destinationAddress);
   182 
   183   return GetSerializedSize();
   184 }
   185 
   186 } /* namespace ns3 */
   187