A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
ethernet-trailer.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 "ns3/assert.h"
22
#include "ns3/log.h"
23
#include "ns3/trailer.h"
24
#include "
ethernet-trailer.h
"
25
#include "
crc32.h
"
26
27
namespace
ns3
{
28
29
NS_LOG_COMPONENT_DEFINE
(
"EthernetTrailer"
);
30
31
NS_OBJECT_ENSURE_REGISTERED
(EthernetTrailer);
32
33
EthernetTrailer::EthernetTrailer
()
34
: m_calcFcs (false),
35
m_fcs (0)
36
{
37
NS_LOG_FUNCTION
(
this
);
38
}
39
40
void
41
EthernetTrailer::EnableFcs
(
bool
enable)
42
{
43
NS_LOG_FUNCTION
(
this
<< enable);
44
m_calcFcs
= enable;
45
}
46
47
bool
48
EthernetTrailer::CheckFcs
(
Ptr<const Packet>
p)
const
49
{
50
NS_LOG_FUNCTION
(
this
<< p);
51
int
len = p->
GetSize
();
52
uint8_t *buffer;
53
uint32_t crc;
54
55
if
(!
m_calcFcs
)
56
{
57
return
true
;
58
}
59
60
buffer =
new
uint8_t[len];
61
p->
CopyData
(buffer, len);
62
crc =
CRC32Calculate
(buffer, len);
63
delete
[] buffer;
64
return
(
m_fcs
== crc);
65
}
66
67
void
68
EthernetTrailer::CalcFcs
(
Ptr<const Packet>
p)
69
{
70
NS_LOG_FUNCTION
(
this
<< p);
71
int
len = p->
GetSize
();
72
uint8_t *buffer;
73
74
if
(!
m_calcFcs
)
75
{
76
return
;
77
}
78
79
buffer =
new
uint8_t[len];
80
p->
CopyData
(buffer, len);
81
m_fcs
=
CRC32Calculate
(buffer, len);
82
delete
[] buffer;
83
}
84
85
void
86
EthernetTrailer::SetFcs
(uint32_t fcs)
87
{
88
NS_LOG_FUNCTION
(
this
<< fcs);
89
m_fcs
= fcs;
90
}
91
92
uint32_t
93
EthernetTrailer::GetFcs
(
void
)
const
94
{
95
NS_LOG_FUNCTION
(
this
);
96
return
m_fcs
;
97
}
98
99
uint32_t
100
EthernetTrailer::GetTrailerSize
(
void
)
const
101
{
102
NS_LOG_FUNCTION
(
this
);
103
return
GetSerializedSize
();
104
}
105
106
TypeId
107
EthernetTrailer::GetTypeId
(
void
)
108
{
109
static
TypeId
tid =
TypeId
(
"ns3::EthernetTrailer"
)
110
.
SetParent
<
Trailer
> ()
111
.SetGroupName(
"Network"
)
112
.AddConstructor<
EthernetTrailer
> ()
113
;
114
return
tid;
115
}
116
TypeId
117
EthernetTrailer::GetInstanceTypeId
(
void
)
const
118
{
119
return
GetTypeId
();
120
}
121
void
122
EthernetTrailer::Print
(std::ostream &os)
const
123
{
124
NS_LOG_FUNCTION
(
this
<< &os);
125
os <<
"fcs="
<<
m_fcs
;
126
}
127
uint32_t
128
EthernetTrailer::GetSerializedSize
(
void
)
const
129
{
130
NS_LOG_FUNCTION
(
this
);
131
return
4;
132
}
133
134
void
135
EthernetTrailer::Serialize
(
Buffer::Iterator
end)
const
136
{
137
NS_LOG_FUNCTION
(
this
<< &end);
138
Buffer::Iterator
i = end;
139
i.
Prev
(
GetSerializedSize
());
140
141
i.
WriteU32
(
m_fcs
);
142
}
143
uint32_t
144
EthernetTrailer::Deserialize
(
Buffer::Iterator
end)
145
{
146
NS_LOG_FUNCTION
(
this
<< &end);
147
Buffer::Iterator
i = end;
148
uint32_t size =
GetSerializedSize
();
149
i.
Prev
(size);
150
151
m_fcs
= i.
ReadU32
();
152
153
return
size;
154
}
155
156
}
// namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition:
object-base.h:45
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition:
packet.h:852
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::EthernetTrailer::EnableFcs
void EnableFcs(bool enable)
Enable or disable FCS checking and calculations.
Definition:
ethernet-trailer.cc:41
ns3::EthernetTrailer::GetFcs
uint32_t GetFcs() const
Definition:
ethernet-trailer.cc:93
ns3::Packet::CopyData
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition:
packet.cc:378
ns3::EthernetTrailer::SetFcs
void SetFcs(uint32_t fcs)
Sets the FCS to a new value.
Definition:
ethernet-trailer.cc:86
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:923
ns3::EthernetTrailer::m_calcFcs
bool m_calcFcs
Enabled FCS calculations.
Definition:
ethernet-trailer.h:108
ns3::Ptr< const Packet >
crc32.h
ethernet-trailer.h
ns3::CRC32Calculate
uint32_t CRC32Calculate(const uint8_t *data, int length)
Calculates the CRC-32 for a given input.
Definition:
crc32.cc:71
ns3::EthernetTrailer::CheckFcs
bool CheckFcs(Ptr< const Packet > p) const
Calculate an FCS on the provided packet and check this value against the FCS found when the trailer w...
Definition:
ethernet-trailer.cc:48
ns3::EthernetTrailer::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator end)
Definition:
ethernet-trailer.cc:144
ns3::EthernetTrailer::m_fcs
uint32_t m_fcs
Value of the fcs contained in the trailer.
Definition:
ethernet-trailer.h:109
ns3::Buffer::Iterator::ReadU32
uint32_t ReadU32(void)
Definition:
buffer.cc:973
ns3::EthernetTrailer::EthernetTrailer
EthernetTrailer()
Construct a null ethernet trailer.
Definition:
ethernet-trailer.cc:33
ns3::EthernetTrailer::Print
virtual void Print(std::ostream &os) const
Definition:
ethernet-trailer.cc:122
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:99
ns3::EthernetTrailer::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
ethernet-trailer.cc:128
ns3::EthernetTrailer::CalcFcs
void CalcFcs(Ptr< const Packet > p)
Updates the Fcs Field to the correct FCS.
Definition:
ethernet-trailer.cc:68
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition:
log-macros-enabled.h:244
ns3::EthernetTrailer::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition:
ethernet-trailer.cc:117
ns3::EthernetTrailer::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
ethernet-trailer.cc:107
ns3::EthernetTrailer::GetTrailerSize
uint32_t GetTrailerSize() const
Definition:
ethernet-trailer.cc:100
ns3::Buffer::Iterator::Prev
void Prev(void)
go backward by one byte
Definition:
buffer.h:851
ns3::Buffer::Iterator::WriteU32
void WriteU32(uint32_t data)
Definition:
buffer.cc:879
ns3::EthernetTrailer
Packet trailer for Ethernet.
Definition:
ethernet-trailer.h:40
ns3::EthernetTrailer::Serialize
virtual void Serialize(Buffer::Iterator end) const
Definition:
ethernet-trailer.cc:135
ns3::Trailer
Protocol trailer serialization and deserialization.
Definition:
trailer.h:41
src
network
utils
ethernet-trailer.cc
Generated on Fri Oct 1 2021 17:03:30 for ns-3 by
1.8.20