A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ethernet-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
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: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
19  */
20 
21 #include <iomanip>
22 #include <iostream>
23 #include "ns3/assert.h"
24 #include "ns3/log.h"
25 #include "ns3/header.h"
26 #include "ethernet-header.h"
27 #include "address-utils.h"
28 
29 NS_LOG_COMPONENT_DEFINE ("EthernetHeader");
30 
31 namespace ns3 {
32 
33 NS_OBJECT_ENSURE_REGISTERED (EthernetHeader)
34  ;
35 
37  : m_enPreambleSfd (hasPreamble),
38  m_lengthType (0)
39 {
40  NS_LOG_FUNCTION (this << hasPreamble);
41 }
42 
44  : m_enPreambleSfd (false),
45  m_lengthType (0)
46 {
47  NS_LOG_FUNCTION (this);
48 }
49 
50 void
51 EthernetHeader::SetLengthType (uint16_t lengthType)
52 {
53  NS_LOG_FUNCTION (this << lengthType);
54  m_lengthType = lengthType;
55 }
56 uint16_t
58 {
59  NS_LOG_FUNCTION (this);
60  return m_lengthType;
61 }
62 
63 void
64 EthernetHeader::SetPreambleSfd (uint64_t preambleSfd)
65 {
66  NS_LOG_FUNCTION (this << preambleSfd);
67  m_preambleSfd = preambleSfd;
68 }
69 uint64_t
71 {
72  NS_LOG_FUNCTION (this);
73  return m_preambleSfd;
74 }
75 
76 void
78 {
79  NS_LOG_FUNCTION (this << source);
80  m_source = source;
81 }
84 {
85  NS_LOG_FUNCTION (this);
86  return m_source;
87 }
88 
89 void
91 {
92  NS_LOG_FUNCTION (this << dst);
93  m_destination = dst;
94 }
97 {
98  NS_LOG_FUNCTION (this);
99  return m_destination;
100 }
101 
104 {
105  NS_LOG_FUNCTION (this);
106  return LENGTH;
107 }
108 
109 uint32_t
111 {
112  NS_LOG_FUNCTION (this);
113  return GetSerializedSize ();
114 }
115 
116 
117 TypeId
119 {
120  static TypeId tid = TypeId ("ns3::EthernetHeader")
121  .SetParent<Header> ()
122  .AddConstructor<EthernetHeader> ()
123  ;
124  return tid;
125 }
126 TypeId
128 {
129  return GetTypeId ();
130 }
131 void
132 EthernetHeader::Print (std::ostream &os) const
133 {
134  NS_LOG_FUNCTION (this << &os);
135  // ethernet, right ?
136  if (m_enPreambleSfd)
137  {
138  os << "preamble/sfd=" << m_preambleSfd << ",";
139  }
140 
141  os << " length/type=0x" << std::hex << m_lengthType << std::dec
142  << ", source=" << m_source
143  << ", destination=" << m_destination;
144 }
145 uint32_t
147 {
148  NS_LOG_FUNCTION (this);
149  if (m_enPreambleSfd)
150  {
152  }
153  else
154  {
155  return LENGTH_SIZE + 2*MAC_ADDR_SIZE;
156  }
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION (this << &start);
164 
165  if (m_enPreambleSfd)
166  {
168  }
169  WriteTo (i, m_destination);
170  WriteTo (i, m_source);
172 }
173 uint32_t
175 {
176  NS_LOG_FUNCTION (this << &start);
178 
179  if (m_enPreambleSfd)
180  {
181  m_enPreambleSfd = i.ReadU64 ();
182  }
183 
184  ReadFrom (i, m_destination);
185  ReadFrom (i, m_source);
186  m_lengthType = i.ReadNtohU16 ();
187 
188  return GetSerializedSize ();
189 }
190 
191 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
static TypeId GetTypeId(void)
virtual void Print(std::ostream &os) const
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
NS_LOG_COMPONENT_DEFINE("EthernetHeader")
static const int PREAMBLE_SIZE
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
uint16_t GetLengthType(void) const
void SetSource(Mac48Address source)
iterator in a Buffer instance
Definition: buffer.h:98
uint32_t GetHeaderSize() const
virtual void Serialize(Buffer::Iterator start) const
Mac48Address m_destination
Source address.
void WriteHtonU16(uint16_t data)
Definition: buffer.h:726
void WriteU64(uint64_t data)
Definition: buffer.cc:915
void SetPreambleSfd(uint64_t preambleSfd)
EthernetHeader()
Construct a null ethernet header By default, does not add or remove an ethernet preamble.
Basic ethernet packet, no tags, type/length field indicates packet length or IP/ARP packet...
an EUI-48 address
Definition: mac48-address.h:41
uint64_t GetPreambleSfd() const
uint64_t ReadU64(void)
Definition: buffer.cc:1014
virtual uint32_t Deserialize(Buffer::Iterator start)
Mac48Address GetSource(void) const
Mac48Address GetDestination(void) const
ethernet_header_t
Types of ethernet packets.
virtual TypeId GetInstanceTypeId(void) const
uint16_t m_lengthType
Value of the Preamble/SFD fields.
virtual uint32_t GetSerializedSize(void) const
Mac48Address m_source
Length or type of the packet.
static const int MAC_ADDR_SIZE
size of the length_type header field
static const int LENGTH_SIZE
size of the preamble_sfd header field
void SetLengthType(uint16_t size)
bool m_enPreambleSfd
size of src/dest addr header fields
ethernet_header_t GetPacketType(void) const
uint16_t ReadNtohU16(void)
Definition: buffer.h:767
void SetDestination(Mac48Address destination)
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611