00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007-2008 Louis Pasteur University 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr> 00019 */ 00020 00021 #include "ns3/assert.h" 00022 #include "ns3/log.h" 00023 #include "ns3/header.h" 00024 #include "address-utils.h" 00025 #include "ipv6-header.h" 00026 00027 NS_LOG_COMPONENT_DEFINE ("Ipv6Header"); 00028 00029 namespace ns3 { 00030 00031 NS_OBJECT_ENSURE_REGISTERED (Ipv6Header); 00032 00033 Ipv6Header::Ipv6Header () 00034 : m_version (6), 00035 m_trafficClass (0), 00036 m_flowLabel (1), 00037 m_payloadLength (0), 00038 m_nextHeader (0), 00039 m_hopLimit (0) 00040 { 00041 00042 SetSourceAddress (Ipv6Address("::")); 00043 SetDestinationAddress(Ipv6Address ("::")); 00044 } 00045 00046 void Ipv6Header::SetTrafficClass (uint8_t traffic) 00047 { 00048 m_trafficClass = traffic; 00049 } 00050 00051 uint8_t Ipv6Header::GetTrafficClass () const 00052 { 00053 return m_trafficClass; 00054 } 00055 00056 void Ipv6Header::SetFlowLabel (uint32_t flow) 00057 { 00058 m_flowLabel = flow; 00059 } 00060 00061 uint32_t Ipv6Header::GetFlowLabel () const 00062 { 00063 return m_flowLabel; 00064 } 00065 00066 void Ipv6Header::SetPayloadLength (uint16_t len) 00067 { 00068 m_payloadLength = len; 00069 } 00070 00071 uint16_t Ipv6Header::GetPayloadLength () const 00072 { 00073 return m_payloadLength; 00074 } 00075 00076 void Ipv6Header::SetNextHeader (uint8_t next) 00077 { 00078 m_nextHeader = next; 00079 } 00080 00081 uint8_t Ipv6Header::GetNextHeader () const 00082 { 00083 return m_nextHeader; 00084 } 00085 00086 void Ipv6Header::SetHopLimit (uint8_t limit) 00087 { 00088 m_hopLimit = limit; 00089 } 00090 00091 uint8_t Ipv6Header::GetHopLimit () const 00092 { 00093 return m_hopLimit; 00094 } 00095 00096 void Ipv6Header::SetSourceAddress (Ipv6Address src) 00097 { 00098 m_sourceAddress = src; 00099 } 00100 00101 Ipv6Address Ipv6Header::GetSourceAddress () const 00102 { 00103 return m_sourceAddress; 00104 } 00105 00106 void Ipv6Header::SetDestinationAddress (Ipv6Address dst) 00107 { 00108 m_destinationAddress = dst; 00109 } 00110 00111 Ipv6Address Ipv6Header::GetDestinationAddress () const 00112 { 00113 return m_destinationAddress; 00114 } 00115 00116 TypeId Ipv6Header::GetTypeId (void) 00117 { 00118 static TypeId tid = TypeId ("ns3::Ipv6Header") 00119 .SetParent<Header> () 00120 .AddConstructor<Ipv6Header> () 00121 ; 00122 return tid; 00123 } 00124 00125 TypeId Ipv6Header::GetInstanceTypeId (void) const 00126 { 00127 return GetTypeId (); 00128 } 00129 00130 void Ipv6Header::Print (std::ostream& os) const 00131 { 00132 os << "(" 00133 "Version " << m_version << " " 00134 << "Traffic class 0x" << std::hex << m_trafficClass << std::dec << " " 00135 << "Flow Label 0x" << std::hex << m_flowLabel << std::dec << " " 00136 << "Payload Length " << m_payloadLength << " " 00137 << "Next Header " << std::dec << (uint32_t) m_nextHeader << " " 00138 << "Hop Limit " << std::dec << (uint32_t)m_hopLimit << " )" 00139 << m_sourceAddress << " > " << m_destinationAddress 00140 ; 00141 } 00142 00143 uint32_t Ipv6Header::GetSerializedSize () const 00144 { 00145 return 10 * 4; 00146 } 00147 00148 void Ipv6Header::Serialize (Buffer::Iterator start) const 00149 { 00150 Buffer::Iterator i = start; 00151 uint32_t vTcFl = 0; /* version, Traffic Class and Flow Label fields */ 00152 00153 vTcFl= (6 << 28) | (m_trafficClass << 20) | (m_flowLabel); 00154 00155 i.WriteHtonU32(vTcFl); 00156 i.WriteHtonU16(m_payloadLength); 00157 i.WriteU8(m_nextHeader); 00158 i.WriteU8(m_hopLimit); 00159 00160 WriteTo(i, m_sourceAddress); 00161 WriteTo(i, m_destinationAddress); 00162 } 00163 00164 uint32_t Ipv6Header::Deserialize (Buffer::Iterator start) 00165 { 00166 Buffer::Iterator i = start; 00167 uint32_t vTcFl = 0; 00168 00169 vTcFl = i.ReadNtohU32(); 00170 m_version = vTcFl >> 28; 00171 00172 NS_ASSERT((m_version) == 6); 00173 00174 m_trafficClass = (uint8_t)((vTcFl >> 20) & 0x000000ff); 00175 m_flowLabel = vTcFl & 0xfff00000; 00176 m_payloadLength = i.ReadNtohU16(); 00177 m_nextHeader = i.ReadU8(); 00178 m_hopLimit = i.ReadU8(); 00179 00180 ReadFrom(i, m_sourceAddress); 00181 ReadFrom(i, m_destinationAddress); 00182 00183 return GetSerializedSize(); 00184 } 00185 00186 } /* namespace ns3 */ 00187