A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ethernet-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
7 */
8
9#include "ethernet-header.h"
10
11#include "address-utils.h"
12
13#include "ns3/assert.h"
14#include "ns3/header.h"
15#include "ns3/log.h"
16
17#include <iomanip>
18#include <iostream>
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("EthernetHeader");
24
25NS_OBJECT_ENSURE_REGISTERED(EthernetHeader);
26
28 : m_enPreambleSfd(hasPreamble),
29 m_lengthType(0)
30{
31 NS_LOG_FUNCTION(this << hasPreamble);
32}
33
35 : m_enPreambleSfd(false),
36 m_lengthType(0)
37{
38 NS_LOG_FUNCTION(this);
39}
40
41void
43{
44 NS_LOG_FUNCTION(this << lengthType);
45 m_lengthType = lengthType;
46}
47
48uint16_t
50{
51 NS_LOG_FUNCTION(this);
52 return m_lengthType;
53}
54
55void
56EthernetHeader::SetPreambleSfd(uint64_t preambleSfd)
57{
58 NS_LOG_FUNCTION(this << preambleSfd);
59 m_preambleSfd = preambleSfd;
60}
61
62uint64_t
68
69void
71{
72 NS_LOG_FUNCTION(this << source);
73 m_source = source;
74}
75
78{
79 NS_LOG_FUNCTION(this);
80 return m_source;
81}
82
83void
89
96
99{
100 NS_LOG_FUNCTION(this);
101 return LENGTH;
102}
103
106{
107 NS_LOG_FUNCTION(this);
108 return GetSerializedSize();
109}
110
111TypeId
113{
114 static TypeId tid = TypeId("ns3::EthernetHeader")
115 .SetParent<Header>()
116 .SetGroupName("Network")
117 .AddConstructor<EthernetHeader>();
118 return tid;
119}
120
121TypeId
123{
124 return GetTypeId();
125}
126
127void
128EthernetHeader::Print(std::ostream& os) const
129{
130 NS_LOG_FUNCTION(this << &os);
131 // ethernet, right ?
132 if (m_enPreambleSfd)
133 {
134 os << "preamble/sfd=" << m_preambleSfd << ",";
135 }
136
137 os << " length/type=0x" << std::hex << m_lengthType << std::dec << ", source=" << m_source
138 << ", destination=" << m_destination;
139}
140
143{
144 NS_LOG_FUNCTION(this);
145 if (m_enPreambleSfd)
146 {
148 }
149 else
150 {
151 return LENGTH_SIZE + 2 * MAC_ADDR_SIZE;
152 }
153}
154
155void
157{
158 NS_LOG_FUNCTION(this << &start);
159 Buffer::Iterator i = start;
160
161 if (m_enPreambleSfd)
162 {
164 }
166 WriteTo(i, m_source);
168}
169
172{
173 NS_LOG_FUNCTION(this << &start);
174 Buffer::Iterator i = start;
175
176 if (m_enPreambleSfd)
177 {
179 }
180
182 ReadFrom(i, m_source);
184
185 return GetSerializedSize();
186}
187
188} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
void WriteU64(uint64_t data)
Definition buffer.cc:870
uint64_t ReadU64()
Definition buffer.cc:973
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint16_t ReadNtohU16()
Definition buffer.h:943
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
uint32_t Deserialize(Buffer::Iterator start) 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:33
an EUI-48 address
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:35
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.