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
vincent@3852
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
vincent@3852
     2
/*
vincent@3852
     3
 * Copyright (c) 2007-2008 Louis Pasteur University
vincent@3852
     4
 *
vincent@3852
     5
 * This program is free software; you can redistribute it and/or modify
vincent@3852
     6
 * it under the terms of the GNU General Public License version 2 as
vincent@3852
     7
 * published by the Free Software Foundation;
vincent@3852
     8
 *
vincent@3852
     9
 * This program is distributed in the hope that it will be useful,
vincent@3852
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
vincent@3852
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
vincent@3852
    12
 * GNU General Public License for more details.
vincent@3852
    13
 *
vincent@3852
    14
 * You should have received a copy of the GNU General Public License
vincent@3852
    15
 * along with this program; if not, write to the Free Software
vincent@3852
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
vincent@3852
    17
 *
vincent@3852
    18
 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
vincent@3852
    19
 */
vincent@3852
    20
vincent@3852
    21
#include "ns3/assert.h"
vincent@3852
    22
#include "ns3/log.h"
vincent@3852
    23
#include "ns3/header.h"
vincent@3852
    24
#include "address-utils.h"
vincent@3852
    25
#include "ipv6-header.h"
vincent@3852
    26
vincent@3852
    27
NS_LOG_COMPONENT_DEFINE ("Ipv6Header");
vincent@3852
    28
vincent@3852
    29
namespace ns3 {
vincent@3852
    30
vincent@3852
    31
NS_OBJECT_ENSURE_REGISTERED (Ipv6Header);
vincent@3852
    32
vincent@3852
    33
Ipv6Header::Ipv6Header ()
vincent@3852
    34
: m_version (6),
vincent@3852
    35
  m_trafficClass (0),
vincent@3852
    36
  m_flowLabel (1),
vincent@3852
    37
  m_payloadLength (0),
vincent@3852
    38
  m_nextHeader (0),
vincent@3852
    39
  m_hopLimit (0)
vincent@3852
    40
{
vincent@3852
    41
vincent@3852
    42
  SetSourceAddress (Ipv6Address("::"));
vincent@3852
    43
  SetDestinationAddress(Ipv6Address ("::"));
vincent@3852
    44
}
vincent@3852
    45
vincent@3852
    46
void Ipv6Header::SetTrafficClass (uint8_t traffic)
vincent@3852
    47
{
vincent@3852
    48
  m_trafficClass = traffic;
vincent@3852
    49
}
vincent@3852
    50
vincent@3852
    51
uint8_t Ipv6Header::GetTrafficClass () const
vincent@3852
    52
{
vincent@3852
    53
  return m_trafficClass;
vincent@3852
    54
}
vincent@3852
    55
vincent@3852
    56
void Ipv6Header::SetFlowLabel (uint32_t flow)
vincent@3852
    57
{
vincent@3852
    58
  m_flowLabel = flow;
vincent@3852
    59
}
vincent@3852
    60
vincent@3852
    61
uint32_t Ipv6Header::GetFlowLabel () const
vincent@3852
    62
{
vincent@3852
    63
  return m_flowLabel;
vincent@3852
    64
}
vincent@3852
    65
vincent@3852
    66
void Ipv6Header::SetPayloadLength (uint16_t len)
vincent@3852
    67
{
vincent@3852
    68
  m_payloadLength = len;
vincent@3852
    69
}
vincent@3852
    70
vincent@3852
    71
uint16_t Ipv6Header::GetPayloadLength () const
vincent@3852
    72
{
vincent@3852
    73
  return m_payloadLength;
vincent@3852
    74
}
vincent@3852
    75
vincent@3852
    76
void Ipv6Header::SetNextHeader (uint8_t next)
vincent@3852
    77
{
vincent@3852
    78
  m_nextHeader = next;
vincent@3852
    79
}
vincent@3852
    80
vincent@3852
    81
uint8_t Ipv6Header::GetNextHeader () const
vincent@3852
    82
{
vincent@3852
    83
  return m_nextHeader;
vincent@3852
    84
}
vincent@3852
    85
vincent@3852
    86
void Ipv6Header::SetHopLimit (uint8_t limit)
vincent@3852
    87
{
vincent@3852
    88
  m_hopLimit = limit;
vincent@3852
    89
}
vincent@3852
    90
vincent@3852
    91
uint8_t Ipv6Header::GetHopLimit () const
vincent@3852
    92
{
vincent@3852
    93
  return m_hopLimit;
vincent@3852
    94
}
vincent@3852
    95
vincent@3852
    96
void Ipv6Header::SetSourceAddress (Ipv6Address src)
vincent@3852
    97
{
vincent@3852
    98
  m_sourceAddress = src;
vincent@3852
    99
}
vincent@3852
   100
vincent@3852
   101
Ipv6Address Ipv6Header::GetSourceAddress () const
vincent@3852
   102
{
vincent@3852
   103
  return m_sourceAddress;
vincent@3852
   104
}
vincent@3852
   105
vincent@3852
   106
void Ipv6Header::SetDestinationAddress (Ipv6Address dst)
vincent@3852
   107
{
vincent@3852
   108
  m_destinationAddress = dst;
vincent@3852
   109
}
vincent@3852
   110
vincent@3852
   111
Ipv6Address Ipv6Header::GetDestinationAddress () const
vincent@3852
   112
{
vincent@3852
   113
  return m_destinationAddress;
vincent@3852
   114
}
vincent@3852
   115
vincent@3852
   116
TypeId Ipv6Header::GetTypeId (void)
vincent@3852
   117
{
vincent@3852
   118
  static TypeId tid = TypeId ("ns3::Ipv6Header")
vincent@3852
   119
    .SetParent<Header> ()
vincent@3852
   120
    .AddConstructor<Ipv6Header> ()
vincent@3852
   121
    ;
vincent@3852
   122
  return tid;
vincent@3852
   123
}
vincent@3852
   124
vincent@3852
   125
TypeId Ipv6Header::GetInstanceTypeId (void) const
vincent@3852
   126
{
vincent@3852
   127
  return GetTypeId ();
vincent@3852
   128
}
vincent@3852
   129
vincent@3852
   130
void Ipv6Header::Print (std::ostream& os) const
vincent@3852
   131
{
vincent@3852
   132
  os << "("
vincent@3852
   133
    "Version " << m_version << " "
vincent@3852
   134
    << "Traffic class 0x" << std::hex << m_trafficClass << std::dec << " "
vincent@3852
   135
    << "Flow Label 0x" << std::hex << m_flowLabel << std::dec << " "
vincent@3852
   136
    << "Payload Length " << m_payloadLength << " "
vincent@3852
   137
    << "Next Header " << std::dec << (uint32_t) m_nextHeader << " "
vincent@3852
   138
    << "Hop Limit " << std::dec << (uint32_t)m_hopLimit << " )"
vincent@3852
   139
    <<  m_sourceAddress << " > " <<  m_destinationAddress
vincent@3852
   140
    ;
vincent@3852
   141
}
vincent@3852
   142
vincent@3852
   143
uint32_t Ipv6Header::GetSerializedSize () const
vincent@3852
   144
{
vincent@3852
   145
  return 10 * 4;
vincent@3852
   146
}
vincent@3852
   147
vincent@3852
   148
void Ipv6Header::Serialize (Buffer::Iterator start) const
vincent@3852
   149
{
vincent@3852
   150
  Buffer::Iterator i = start;
vincent@3852
   151
  uint32_t vTcFl = 0; /* version, Traffic Class and Flow Label fields */
vincent@3852
   152
vincent@3852
   153
  vTcFl= (6 << 28) | (m_trafficClass << 20) | (m_flowLabel);
vincent@3852
   154
vincent@3852
   155
  i.WriteHtonU32(vTcFl);
vincent@3852
   156
  i.WriteHtonU16(m_payloadLength);
vincent@3852
   157
  i.WriteU8(m_nextHeader);
vincent@3852
   158
  i.WriteU8(m_hopLimit);
vincent@3852
   159
vincent@3852
   160
  WriteTo(i, m_sourceAddress);
vincent@3852
   161
  WriteTo(i, m_destinationAddress);
vincent@3852
   162
}
vincent@3852
   163
vincent@3852
   164
uint32_t Ipv6Header::Deserialize (Buffer::Iterator start)
vincent@3852
   165
{
vincent@3852
   166
  Buffer::Iterator i = start;
vincent@3852
   167
  uint32_t vTcFl = 0;
vincent@3852
   168
vincent@3852
   169
  vTcFl = i.ReadNtohU32();
vincent@3852
   170
  m_version = vTcFl >> 28;
vincent@3852
   171
vincent@3852
   172
  NS_ASSERT((m_version) == 6);
vincent@3852
   173
vincent@3852
   174
  m_trafficClass = (uint8_t)((vTcFl >> 20) & 0x000000ff);
vincent@3852
   175
  m_flowLabel = vTcFl & 0xfff00000;
vincent@3852
   176
  m_payloadLength = i.ReadNtohU16();
vincent@3852
   177
  m_nextHeader = i.ReadU8();
vincent@3852
   178
  m_hopLimit = i.ReadU8();
vincent@3852
   179
vincent@3852
   180
  ReadFrom(i, m_sourceAddress);
vincent@3852
   181
  ReadFrom(i, m_destinationAddress);
vincent@3852
   182
vincent@3852
   183
  return GetSerializedSize();
vincent@3852
   184
}
vincent@3852
   185
vincent@3852
   186
} /* namespace ns3 */
vincent@3852
   187