A Discrete-Event Network Simulator
API
ethernet-header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
18  */
19 
20 #include "ethernet-header.h"
21 
22 #include "address-utils.h"
23 
24 #include "ns3/assert.h"
25 #include "ns3/header.h"
26 #include "ns3/log.h"
27 
28 #include <iomanip>
29 #include <iostream>
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("EthernetHeader");
35 
36 NS_OBJECT_ENSURE_REGISTERED(EthernetHeader);
37 
39  : m_enPreambleSfd(hasPreamble),
40  m_lengthType(0)
41 {
42  NS_LOG_FUNCTION(this << hasPreamble);
43 }
44 
46  : m_enPreambleSfd(false),
47  m_lengthType(0)
48 {
49  NS_LOG_FUNCTION(this);
50 }
51 
52 void
53 EthernetHeader::SetLengthType(uint16_t lengthType)
54 {
55  NS_LOG_FUNCTION(this << lengthType);
56  m_lengthType = lengthType;
57 }
58 
59 uint16_t
61 {
62  NS_LOG_FUNCTION(this);
63  return m_lengthType;
64 }
65 
66 void
67 EthernetHeader::SetPreambleSfd(uint64_t preambleSfd)
68 {
69  NS_LOG_FUNCTION(this << preambleSfd);
70  m_preambleSfd = preambleSfd;
71 }
72 
73 uint64_t
75 {
76  NS_LOG_FUNCTION(this);
77  return m_preambleSfd;
78 }
79 
80 void
82 {
83  NS_LOG_FUNCTION(this << source);
84  m_source = source;
85 }
86 
89 {
90  NS_LOG_FUNCTION(this);
91  return m_source;
92 }
93 
94 void
96 {
97  NS_LOG_FUNCTION(this << dst);
98  m_destination = dst;
99 }
100 
103 {
104  NS_LOG_FUNCTION(this);
105  return m_destination;
106 }
107 
110 {
111  NS_LOG_FUNCTION(this);
112  return LENGTH;
113 }
114 
115 uint32_t
117 {
118  NS_LOG_FUNCTION(this);
119  return GetSerializedSize();
120 }
121 
122 TypeId
124 {
125  static TypeId tid = TypeId("ns3::EthernetHeader")
126  .SetParent<Header>()
127  .SetGroupName("Network")
128  .AddConstructor<EthernetHeader>();
129  return tid;
130 }
131 
132 TypeId
134 {
135  return GetTypeId();
136 }
137 
138 void
139 EthernetHeader::Print(std::ostream& os) const
140 {
141  NS_LOG_FUNCTION(this << &os);
142  // ethernet, right ?
143  if (m_enPreambleSfd)
144  {
145  os << "preamble/sfd=" << m_preambleSfd << ",";
146  }
147 
148  os << " length/type=0x" << std::hex << m_lengthType << std::dec << ", source=" << m_source
149  << ", destination=" << m_destination;
150 }
151 
152 uint32_t
154 {
155  NS_LOG_FUNCTION(this);
156  if (m_enPreambleSfd)
157  {
158  return PREAMBLE_SIZE + LENGTH_SIZE + 2 * MAC_ADDR_SIZE;
159  }
160  else
161  {
162  return LENGTH_SIZE + 2 * MAC_ADDR_SIZE;
163  }
164 }
165 
166 void
168 {
169  NS_LOG_FUNCTION(this << &start);
171 
172  if (m_enPreambleSfd)
173  {
175  }
177  WriteTo(i, m_source);
179 }
180 
181 uint32_t
183 {
184  NS_LOG_FUNCTION(this << &start);
186 
187  if (m_enPreambleSfd)
188  {
189  m_enPreambleSfd = i.ReadU64();
190  }
191 
193  ReadFrom(i, m_source);
195 
196  return GetSerializedSize();
197 }
198 
199 } // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
void WriteU64(uint64_t data)
Definition: buffer.cc:881
uint64_t ReadU64()
Definition: buffer.cc:984
void WriteHtonU16(uint16_t data)
Definition: buffer.h:915
uint16_t ReadNtohU16()
Definition: buffer.h:954
Packet header for Ethernet.
uint16_t GetLengthType() const
uint16_t m_lengthType
Length or type of the packet.
uint32_t GetHeaderSize() const
bool m_enPreambleSfd
If false, the preamble/sfd are not serialised/deserialised.
uint32_t GetSerializedSize() const override
Mac48Address m_destination
Destination address.
static TypeId GetTypeId()
Get the type ID.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetDestination(Mac48Address destination)
static const int PREAMBLE_SIZE
size of the preamble_sfd header field
static const int MAC_ADDR_SIZE
size of src/dest addr header fields
Mac48Address m_source
Source address.
Mac48Address GetDestination() const
void Serialize(Buffer::Iterator start) const override
void SetLengthType(uint16_t size)
void SetSource(Mac48Address source)
EthernetHeader()
Construct a null ethernet header By default, does not add or remove an ethernet preamble.
ethernet_header_t GetPacketType() const
void Print(std::ostream &os) const override
uint64_t m_preambleSfd
Value of the Preamble/SFD fields.
void SetPreambleSfd(uint64_t preambleSfd)
Mac48Address GetSource() const
static const int LENGTH_SIZE
size of the length_type header field
uint64_t GetPreambleSfd() const
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
an EUI-48 address
Definition: mac48-address.h:46
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
ethernet_header_t
Types of ethernet packets.
@ LENGTH
Basic ethernet packet, no tags, type/length field indicates packet length or IP/ARP packet.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.