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
src
network
utils
ethernet-trailer.cc
Generated on Tue May 14 2013 11:08:31 for ns-3 by
1.8.1.2