src/node/ipv6-header.cc
changeset 3852 9cf7ad0cac85
child 4731 510db8599bfb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/node/ipv6-header.cc	Fri Nov 07 11:36:15 2008 -0800
     1.3 @@ -0,0 +1,187 @@
     1.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     1.5 +/*
     1.6 + * Copyright (c) 2007-2008 Louis Pasteur University
     1.7 + *
     1.8 + * This program is free software; you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License version 2 as
    1.10 + * published by the Free Software Foundation;
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program; if not, write to the Free Software
    1.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 + *
    1.21 + * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
    1.22 + */
    1.23 +
    1.24 +#include "ns3/assert.h"
    1.25 +#include "ns3/log.h"
    1.26 +#include "ns3/header.h"
    1.27 +#include "address-utils.h"
    1.28 +#include "ipv6-header.h"
    1.29 +
    1.30 +NS_LOG_COMPONENT_DEFINE ("Ipv6Header");
    1.31 +
    1.32 +namespace ns3 {
    1.33 +
    1.34 +NS_OBJECT_ENSURE_REGISTERED (Ipv6Header);
    1.35 +
    1.36 +Ipv6Header::Ipv6Header ()
    1.37 +: m_version (6),
    1.38 +  m_trafficClass (0),
    1.39 +  m_flowLabel (1),
    1.40 +  m_payloadLength (0),
    1.41 +  m_nextHeader (0),
    1.42 +  m_hopLimit (0)
    1.43 +{
    1.44 +
    1.45 +  SetSourceAddress (Ipv6Address("::"));
    1.46 +  SetDestinationAddress(Ipv6Address ("::"));
    1.47 +}
    1.48 +
    1.49 +void Ipv6Header::SetTrafficClass (uint8_t traffic)
    1.50 +{
    1.51 +  m_trafficClass = traffic;
    1.52 +}
    1.53 +
    1.54 +uint8_t Ipv6Header::GetTrafficClass () const
    1.55 +{
    1.56 +  return m_trafficClass;
    1.57 +}
    1.58 +
    1.59 +void Ipv6Header::SetFlowLabel (uint32_t flow)
    1.60 +{
    1.61 +  m_flowLabel = flow;
    1.62 +}
    1.63 +
    1.64 +uint32_t Ipv6Header::GetFlowLabel () const
    1.65 +{
    1.66 +  return m_flowLabel;
    1.67 +}
    1.68 +
    1.69 +void Ipv6Header::SetPayloadLength (uint16_t len)
    1.70 +{
    1.71 +  m_payloadLength = len;
    1.72 +}
    1.73 +
    1.74 +uint16_t Ipv6Header::GetPayloadLength () const
    1.75 +{
    1.76 +  return m_payloadLength;
    1.77 +}
    1.78 +
    1.79 +void Ipv6Header::SetNextHeader (uint8_t next)
    1.80 +{
    1.81 +  m_nextHeader = next;
    1.82 +}
    1.83 +
    1.84 +uint8_t Ipv6Header::GetNextHeader () const
    1.85 +{
    1.86 +  return m_nextHeader;
    1.87 +}
    1.88 +
    1.89 +void Ipv6Header::SetHopLimit (uint8_t limit)
    1.90 +{
    1.91 +  m_hopLimit = limit;
    1.92 +}
    1.93 +
    1.94 +uint8_t Ipv6Header::GetHopLimit () const
    1.95 +{
    1.96 +  return m_hopLimit;
    1.97 +}
    1.98 +
    1.99 +void Ipv6Header::SetSourceAddress (Ipv6Address src)
   1.100 +{
   1.101 +  m_sourceAddress = src;
   1.102 +}
   1.103 +
   1.104 +Ipv6Address Ipv6Header::GetSourceAddress () const
   1.105 +{
   1.106 +  return m_sourceAddress;
   1.107 +}
   1.108 +
   1.109 +void Ipv6Header::SetDestinationAddress (Ipv6Address dst)
   1.110 +{
   1.111 +  m_destinationAddress = dst;
   1.112 +}
   1.113 +
   1.114 +Ipv6Address Ipv6Header::GetDestinationAddress () const
   1.115 +{
   1.116 +  return m_destinationAddress;
   1.117 +}
   1.118 +
   1.119 +TypeId Ipv6Header::GetTypeId (void)
   1.120 +{
   1.121 +  static TypeId tid = TypeId ("ns3::Ipv6Header")
   1.122 +    .SetParent<Header> ()
   1.123 +    .AddConstructor<Ipv6Header> ()
   1.124 +    ;
   1.125 +  return tid;
   1.126 +}
   1.127 +
   1.128 +TypeId Ipv6Header::GetInstanceTypeId (void) const
   1.129 +{
   1.130 +  return GetTypeId ();
   1.131 +}
   1.132 +
   1.133 +void Ipv6Header::Print (std::ostream& os) const
   1.134 +{
   1.135 +  os << "("
   1.136 +    "Version " << m_version << " "
   1.137 +    << "Traffic class 0x" << std::hex << m_trafficClass << std::dec << " "
   1.138 +    << "Flow Label 0x" << std::hex << m_flowLabel << std::dec << " "
   1.139 +    << "Payload Length " << m_payloadLength << " "
   1.140 +    << "Next Header " << std::dec << (uint32_t) m_nextHeader << " "
   1.141 +    << "Hop Limit " << std::dec << (uint32_t)m_hopLimit << " )"
   1.142 +    <<  m_sourceAddress << " > " <<  m_destinationAddress
   1.143 +    ;
   1.144 +}
   1.145 +
   1.146 +uint32_t Ipv6Header::GetSerializedSize () const
   1.147 +{
   1.148 +  return 10 * 4;
   1.149 +}
   1.150 +
   1.151 +void Ipv6Header::Serialize (Buffer::Iterator start) const
   1.152 +{
   1.153 +  Buffer::Iterator i = start;
   1.154 +  uint32_t vTcFl = 0; /* version, Traffic Class and Flow Label fields */
   1.155 +
   1.156 +  vTcFl= (6 << 28) | (m_trafficClass << 20) | (m_flowLabel);
   1.157 +
   1.158 +  i.WriteHtonU32(vTcFl);
   1.159 +  i.WriteHtonU16(m_payloadLength);
   1.160 +  i.WriteU8(m_nextHeader);
   1.161 +  i.WriteU8(m_hopLimit);
   1.162 +
   1.163 +  WriteTo(i, m_sourceAddress);
   1.164 +  WriteTo(i, m_destinationAddress);
   1.165 +}
   1.166 +
   1.167 +uint32_t Ipv6Header::Deserialize (Buffer::Iterator start)
   1.168 +{
   1.169 +  Buffer::Iterator i = start;
   1.170 +  uint32_t vTcFl = 0;
   1.171 +
   1.172 +  vTcFl = i.ReadNtohU32();
   1.173 +  m_version = vTcFl >> 28;
   1.174 +
   1.175 +  NS_ASSERT((m_version) == 6);
   1.176 +
   1.177 +  m_trafficClass = (uint8_t)((vTcFl >> 20) & 0x000000ff);
   1.178 +  m_flowLabel = vTcFl & 0xfff00000;
   1.179 +  m_payloadLength = i.ReadNtohU16();
   1.180 +  m_nextHeader = i.ReadU8();
   1.181 +  m_hopLimit = i.ReadU8();
   1.182 +
   1.183 +  ReadFrom(i, m_sourceAddress);
   1.184 +  ReadFrom(i, m_destinationAddress);
   1.185 +
   1.186 +  return GetSerializedSize();
   1.187 +}
   1.188 +
   1.189 +} /* namespace ns3 */
   1.190 +