A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
26
NS_LOG_COMPONENT_DEFINE
(
"EthernetTrailer"
);
27
28
namespace
ns3 {
29
30
NS_OBJECT_ENSURE_REGISTERED
(EthernetTrailer);
31
32
EthernetTrailer::EthernetTrailer
()
33
: m_calcFcs (false),
34
m_fcs (0)
35
{
36
NS_LOG_FUNCTION
(
this
);
37
}
38
39
void
40
EthernetTrailer::EnableFcs
(
bool
enable)
41
{
42
NS_LOG_FUNCTION
(
this
<< enable);
43
m_calcFcs
= enable;
44
}
45
46
bool
47
EthernetTrailer::CheckFcs
(
Ptr<const Packet>
p)
const
48
{
49
NS_LOG_FUNCTION
(
this
<< p);
50
int
len = p->
GetSize
();
51
uint8_t *buffer;
52
uint32_t crc;
53
54
if
(!
m_calcFcs
)
55
{
56
return
true
;
57
}
58
59
buffer =
new
uint8_t[len];
60
p->
CopyData
(buffer, len);
61
crc =
DoCalcFcs
(buffer, len);
62
delete
[] buffer;
63
return
(
m_fcs
== crc);
64
}
65
66
void
67
EthernetTrailer::CalcFcs
(
Ptr<const Packet>
p)
68
{
69
NS_LOG_FUNCTION
(
this
<< p);
70
int
len = p->
GetSize
();
71
uint8_t *buffer;
72
73
if
(!
m_calcFcs
)
74
{
75
return
;
76
}
77
78
buffer =
new
uint8_t[len];
79
p->
CopyData
(buffer, len);
80
m_fcs
=
DoCalcFcs
(buffer, len);
81
delete
[] buffer;
82
}
83
84
void
85
EthernetTrailer::SetFcs
(uint32_t fcs)
86
{
87
NS_LOG_FUNCTION
(
this
<< fcs);
88
m_fcs
= fcs;
89
}
90
91
uint32_t
92
EthernetTrailer::GetFcs
(
void
)
93
{
94
NS_LOG_FUNCTION
(
this
);
95
return
m_fcs
;
96
}
97
98
uint32_t
99
EthernetTrailer::GetTrailerSize
(
void
)
const
100
{
101
NS_LOG_FUNCTION
(
this
);
102
return
GetSerializedSize
();
103
}
104
105
TypeId
106
EthernetTrailer::GetTypeId
(
void
)
107
{
108
static
TypeId
tid =
TypeId
(
"ns3::EthernetTrailer"
)
109
.
SetParent
<
Trailer
> ()
110
.AddConstructor<EthernetTrailer> ()
111
;
112
return
tid;
113
}
114
TypeId
115
EthernetTrailer::GetInstanceTypeId
(
void
)
const
116
{
117
return
GetTypeId
();
118
}
119
void
120
EthernetTrailer::Print
(std::ostream &os)
const
121
{
122
NS_LOG_FUNCTION
(
this
<< &os);
123
os <<
"fcs="
<<
m_fcs
;
124
}
125
uint32_t
126
EthernetTrailer::GetSerializedSize
(
void
)
const
127
{
128
NS_LOG_FUNCTION
(
this
);
129
return
4;
130
}
131
132
void
133
EthernetTrailer::Serialize
(
Buffer::Iterator
end)
const
134
{
135
NS_LOG_FUNCTION
(
this
<< &end);
136
Buffer::Iterator
i = end;
137
i.
Prev
(
GetSerializedSize
());
138
139
i.
WriteU32
(
m_fcs
);
140
}
141
uint32_t
142
EthernetTrailer::Deserialize
(
Buffer::Iterator
end)
143
{
144
NS_LOG_FUNCTION
(
this
<< &end);
145
Buffer::Iterator
i = end;
146
uint32_t size =
GetSerializedSize
();
147
i.
Prev
(size);
148
149
m_fcs
= i.
ReadU32
();
150
151
return
size;
152
}
153
154
// This code is copied from /lib/crc32.c in the linux kernel.
155
// It assumes little endian ordering.
156
uint32_t
157
EthernetTrailer::DoCalcFcs
(uint8_t
const
*buffer,
size_t
len)
const
158
{
159
NS_LOG_FUNCTION
(
this
<< &buffer << len);
160
uint32_t crc = 0xffffffff;
161
int
i;
162
163
while
(len--)
164
{
165
crc ^= *buffer++;
166
for
(i = 0; i < 8; i++)
167
{
168
crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
169
}
170
}
171
return
~crc;
172
}
173
174
}
// namespace ns3
ns3::Buffer::Iterator::ReadU32
uint32_t ReadU32(void)
Definition:
buffer.cc:997
ns3::Ptr< const Packet >
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:311
ns3::EthernetTrailer::CalcFcs
void CalcFcs(Ptr< const Packet > p)
Updates the Fcs Field to the correct FCS.
Definition:
ethernet-trailer.cc:67
ns3::EthernetTrailer::SetFcs
void SetFcs(uint32_t fcs)
Sets the FCS to a new value.
Definition:
ethernet-trailer.cc:85
ns3::EthernetTrailer::GetTrailerSize
uint32_t GetTrailerSize() const
Definition:
ethernet-trailer.cc:99
ns3::Packet::GetSize
uint32_t GetSize(void) const
Definition:
packet.h:650
ns3::EthernetTrailer::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
ethernet-trailer.cc:126
ns3::EthernetTrailer::m_fcs
uint32_t m_fcs
Definition:
ethernet-trailer.h:105
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:98
ns3::EthernetTrailer::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Definition:
ethernet-trailer.cc:115
ns3::EthernetTrailer::CheckFcs
bool CheckFcs(Ptr< const Packet > p) const
Definition:
ethernet-trailer.cc:47
ns3::EthernetTrailer::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator end)
Definition:
ethernet-trailer.cc:142
ns3::Buffer::Iterator::Prev
void Prev(void)
Definition:
buffer.h:672
ns3::EthernetTrailer::Print
virtual void Print(std::ostream &os) const
Definition:
ethernet-trailer.cc:120
ns3::EthernetTrailer::GetTypeId
static TypeId GetTypeId(void)
Definition:
ethernet-trailer.cc:106
ns3::EthernetTrailer::EthernetTrailer
EthernetTrailer()
Construct a null ethernet trailer.
Definition:
ethernet-trailer.cc:32
ns3::NS_OBJECT_ENSURE_REGISTERED
NS_OBJECT_ENSURE_REGISTERED(AntennaModel)
ns3::EthernetTrailer::m_calcFcs
bool m_calcFcs
Definition:
ethernet-trailer.h:104
ns3::EthernetTrailer::Serialize
virtual void Serialize(Buffer::Iterator end) const
Definition:
ethernet-trailer.cc:133
ns3::Trailer
Protocol trailer serialization and deserialization.
Definition:
trailer.h:40
NS_LOG_COMPONENT_DEFINE
NS_LOG_COMPONENT_DEFINE("EthernetTrailer")
ns3::EthernetTrailer::EnableFcs
void EnableFcs(bool enable)
Enable or disable FCS checking and calculations.
Definition:
ethernet-trailer.cc:40
ns3::Packet::CopyData
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Definition:
packet.cc:381
ethernet-trailer.h
ns3::EthernetTrailer::DoCalcFcs
uint32_t DoCalcFcs(uint8_t const *buffer, size_t len) const
Value of the fcs contained in the trailer.
Definition:
ethernet-trailer.cc:157
ns3::Buffer::Iterator::WriteU32
void WriteU32(uint32_t data)
Definition:
buffer.cc:903
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Definition:
type-id.cc:610
ns3::EthernetTrailer::GetFcs
uint32_t GetFcs()
Definition:
ethernet-trailer.cc:92
src
network
utils
ethernet-trailer.cc
Generated on Sun Apr 20 2014 11:15:00 for ns-3 by
1.8.6