00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005 INRIA 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: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca> 00019 */ 00020 00021 #include <iomanip> 00022 #include <iostream> 00023 #include "ns3/assert.h" 00024 #include "ns3/log.h" 00025 #include "ns3/header.h" 00026 #include "ethernet-header.h" 00027 #include "address-utils.h" 00028 00029 NS_LOG_COMPONENT_DEFINE ("EthernetHeader"); 00030 00031 namespace ns3 { 00032 00033 NS_OBJECT_ENSURE_REGISTERED (EthernetHeader); 00034 00035 EthernetHeader::EthernetHeader (bool hasPreamble) 00036 : m_enPreambleSfd (hasPreamble), 00037 m_lengthType (0) 00038 {} 00039 00040 EthernetHeader::EthernetHeader () 00041 : m_enPreambleSfd (false), 00042 m_lengthType (0) 00043 {} 00044 00045 void 00046 EthernetHeader::SetLengthType (uint16_t lengthType) 00047 { 00048 m_lengthType = lengthType; 00049 } 00050 uint16_t 00051 EthernetHeader::GetLengthType (void) const 00052 { 00053 return m_lengthType; 00054 } 00055 00056 void 00057 EthernetHeader::SetPreambleSfd (uint64_t preambleSfd) 00058 { 00059 m_preambleSfd = preambleSfd; 00060 } 00061 uint64_t 00062 EthernetHeader::GetPreambleSfd (void) const 00063 { 00064 return m_preambleSfd; 00065 } 00066 00067 void 00068 EthernetHeader::SetSource (Mac48Address source) 00069 { 00070 m_source = source; 00071 } 00072 Mac48Address 00073 EthernetHeader::GetSource (void) const 00074 { 00075 return m_source; 00076 } 00077 00078 void 00079 EthernetHeader::SetDestination (Mac48Address dst) 00080 { 00081 m_destination = dst; 00082 } 00083 Mac48Address 00084 EthernetHeader::GetDestination (void) const 00085 { 00086 return m_destination; 00087 } 00088 00089 ethernet_header_t 00090 EthernetHeader::GetPacketType (void) const 00091 { 00092 return LENGTH; 00093 } 00094 00095 uint32_t 00096 EthernetHeader::GetHeaderSize (void) const 00097 { 00098 return GetSerializedSize(); 00099 } 00100 00101 00102 TypeId 00103 EthernetHeader::GetTypeId (void) 00104 { 00105 static TypeId tid = TypeId ("ns3::EthernetHeader") 00106 .SetParent<Header> () 00107 .AddConstructor<EthernetHeader> () 00108 ; 00109 return tid; 00110 } 00111 TypeId 00112 EthernetHeader::GetInstanceTypeId (void) const 00113 { 00114 return GetTypeId (); 00115 } 00116 void 00117 EthernetHeader::Print (std::ostream &os) const 00118 { 00119 // ethernet, right ? 00120 if (m_enPreambleSfd) 00121 { 00122 os << "preamble/sfd=" << m_preambleSfd << ","; 00123 } 00124 00125 os << " length/type=0x" << std::hex << m_lengthType << std::dec 00126 << ", source=" << m_source 00127 << ", destination=" << m_destination; 00128 } 00129 uint32_t 00130 EthernetHeader::GetSerializedSize (void) const 00131 { 00132 if (m_enPreambleSfd) 00133 { 00134 return PREAMBLE_SIZE + LENGTH_SIZE + 2*MAC_ADDR_SIZE; 00135 } 00136 else 00137 { 00138 return LENGTH_SIZE + 2*MAC_ADDR_SIZE; 00139 } 00140 } 00141 00142 void 00143 EthernetHeader::Serialize (Buffer::Iterator start) const 00144 { 00145 Buffer::Iterator i = start; 00146 00147 if (m_enPreambleSfd) 00148 { 00149 i.WriteU64(m_preambleSfd); 00150 } 00151 WriteTo (i, m_destination); 00152 WriteTo (i, m_source); 00153 i.WriteHtonU16 (m_lengthType); 00154 } 00155 uint32_t 00156 EthernetHeader::Deserialize (Buffer::Iterator start) 00157 { 00158 Buffer::Iterator i = start; 00159 00160 if (m_enPreambleSfd) 00161 { 00162 m_enPreambleSfd = i.ReadU64 (); 00163 } 00164 00165 ReadFrom (i, m_destination); 00166 ReadFrom (i, m_source); 00167 m_lengthType = i.ReadNtohU16 (); 00168 00169 return GetSerializedSize (); 00170 } 00171 00172 } // namespace ns3