A Discrete-Event Network Simulator
API
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 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("EthernetHeader");
32 
33 NS_OBJECT_ENSURE_REGISTERED (EthernetHeader);
34 
36  : m_enPreambleSfd (hasPreamble),
37  m_lengthType (0)
38 {
39  NS_LOG_FUNCTION (this << hasPreamble);
40 }
41 
43  : m_enPreambleSfd (false),
44  m_lengthType (0)
45 {
46  NS_LOG_FUNCTION (this);
47 }
48 
49 void
50 EthernetHeader::SetLengthType (uint16_t lengthType)
51 {
52  NS_LOG_FUNCTION (this << lengthType);
53  m_lengthType = lengthType;
54 }
55 uint16_t
57 {
58  NS_LOG_FUNCTION (this);
59  return m_lengthType;
60 }
61 
62 void
63 EthernetHeader::SetPreambleSfd (uint64_t preambleSfd)
64 {
65  NS_LOG_FUNCTION (this << preambleSfd);
66  m_preambleSfd = preambleSfd;
67 }
68 uint64_t
70 {
71  NS_LOG_FUNCTION (this);
72  return m_preambleSfd;
73 }
74 
75 void
77 {
78  NS_LOG_FUNCTION (this << source);
79  m_source = source;
80 }
83 {
84  NS_LOG_FUNCTION (this);
85  return m_source;
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION (this << dst);
92  m_destination = dst;
93 }
96 {
97  NS_LOG_FUNCTION (this);
98  return m_destination;
99 }
100 
103 {
104  NS_LOG_FUNCTION (this);
105  return LENGTH;
106 }
107 
108 uint32_t
110 {
111  NS_LOG_FUNCTION (this);
112  return GetSerializedSize ();
113 }
114 
115 
116 TypeId
118 {
119  static TypeId tid = TypeId ("ns3::EthernetHeader")
120  .SetParent<Header> ()
121  .AddConstructor<EthernetHeader> ()
122  ;
123  return tid;
124 }
125 TypeId
127 {
128  return GetTypeId ();
129 }
130 void
131 EthernetHeader::Print (std::ostream &os) const
132 {
133  NS_LOG_FUNCTION (this << &os);
134  // ethernet, right ?
135  if (m_enPreambleSfd)
136  {
137  os << "preamble/sfd=" << m_preambleSfd << ",";
138  }
139 
140  os << " length/type=0x" << std::hex << m_lengthType << std::dec
141  << ", source=" << m_source
142  << ", destination=" << m_destination;
143 }
144 uint32_t
146 {
147  NS_LOG_FUNCTION (this);
148  if (m_enPreambleSfd)
149  {
151  }
152  else
153  {
154  return LENGTH_SIZE + 2*MAC_ADDR_SIZE;
155  }
156 }
157 
158 void
160 {
161  NS_LOG_FUNCTION (this << &start);
163 
164  if (m_enPreambleSfd)
165  {
167  }
168  WriteTo (i, m_destination);
169  WriteTo (i, m_source);
171 }
172 uint32_t
174 {
175  NS_LOG_FUNCTION (this << &start);
177 
178  if (m_enPreambleSfd)
179  {
180  m_enPreambleSfd = i.ReadU64 ();
181  }
182 
183  ReadFrom (i, m_destination);
184  ReadFrom (i, m_source);
185  m_lengthType = i.ReadNtohU16 ();
186 
187  return GetSerializedSize ();
188 }
189 
190 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
static TypeId GetTypeId(void)
Get the type ID.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual void Print(std::ostream &os) const
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
def start()
Definition: core.py:1482
static const int PREAMBLE_SIZE
size of the preamble_sfd header field
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
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
Destination address.
void WriteHtonU16(uint16_t data)
Definition: buffer.h:912
uint64_t m_preambleSfd
Value of the Preamble/SFD fields.
void WriteU64(uint64_t data)
Definition: buffer.cc:919
void SetPreambleSfd(uint64_t preambleSfd)
EthernetHeader()
Construct a null ethernet header By default, does not add or remove an ethernet preamble.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Basic ethernet packet, no tags, type/length field indicates packet length or IP/ARP packet...
an EUI-48 address
Definition: mac48-address.h:43
uint64_t GetPreambleSfd() const
uint64_t ReadU64(void)
Definition: buffer.cc:1018
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
Get the most derived TypeId for this Object.
uint16_t m_lengthType
Length or type of the packet.
virtual uint32_t GetSerializedSize(void) const
Mac48Address m_source
Source address.
static const int MAC_ADDR_SIZE
size of src/dest addr header fields
static const int LENGTH_SIZE
size of the length_type header field
void SetLengthType(uint16_t size)
bool m_enPreambleSfd
If false, the preamble/sfd are not serialised/deserialised.
ethernet_header_t GetPacketType(void) const
uint16_t ReadNtohU16(void)
Definition: buffer.h:953
void SetDestination(Mac48Address destination)
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631