A Discrete-Event Network Simulator
API
ethernet-trailer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
18  */
19 
20 #include "ethernet-trailer.h"
21 
22 #include "crc32.h"
23 
24 #include "ns3/assert.h"
25 #include "ns3/log.h"
26 #include "ns3/trailer.h"
27 
28 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE("EthernetTrailer");
32 
33 NS_OBJECT_ENSURE_REGISTERED(EthernetTrailer);
34 
36  : m_calcFcs(false),
37  m_fcs(0)
38 {
39  NS_LOG_FUNCTION(this);
40 }
41 
42 void
44 {
45  NS_LOG_FUNCTION(this << enable);
46  m_calcFcs = enable;
47 }
48 
49 bool
51 {
52  NS_LOG_FUNCTION(this << p);
53  int len = p->GetSize();
54  uint8_t* buffer;
55  uint32_t crc;
56 
57  if (!m_calcFcs)
58  {
59  return true;
60  }
61 
62  buffer = new uint8_t[len];
63  p->CopyData(buffer, len);
64  crc = CRC32Calculate(buffer, len);
65  delete[] buffer;
66  return (m_fcs == crc);
67 }
68 
69 void
71 {
72  NS_LOG_FUNCTION(this << p);
73  int len = p->GetSize();
74  uint8_t* buffer;
75 
76  if (!m_calcFcs)
77  {
78  return;
79  }
80 
81  buffer = new uint8_t[len];
82  p->CopyData(buffer, len);
83  m_fcs = CRC32Calculate(buffer, len);
84  delete[] buffer;
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION(this << fcs);
91  m_fcs = fcs;
92 }
93 
94 uint32_t
96 {
97  NS_LOG_FUNCTION(this);
98  return m_fcs;
99 }
100 
101 uint32_t
103 {
104  NS_LOG_FUNCTION(this);
105  return GetSerializedSize();
106 }
107 
108 TypeId
110 {
111  static TypeId tid = TypeId("ns3::EthernetTrailer")
112  .SetParent<Trailer>()
113  .SetGroupName("Network")
114  .AddConstructor<EthernetTrailer>();
115  return tid;
116 }
117 
118 TypeId
120 {
121  return GetTypeId();
122 }
123 
124 void
125 EthernetTrailer::Print(std::ostream& os) const
126 {
127  NS_LOG_FUNCTION(this << &os);
128  os << "fcs=" << m_fcs;
129 }
130 
131 uint32_t
133 {
134  NS_LOG_FUNCTION(this);
135  return 4;
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION(this << &end);
142  Buffer::Iterator i = end;
143  i.Prev(GetSerializedSize());
144 
145  i.WriteU32(m_fcs);
146 }
147 
148 uint32_t
150 {
151  NS_LOG_FUNCTION(this << &end);
152  Buffer::Iterator i = end;
153  uint32_t size = GetSerializedSize();
154  i.Prev(size);
155 
156  m_fcs = i.ReadU32();
157 
158  return size;
159 }
160 
161 } // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
void WriteU32(uint32_t data)
Definition: buffer.cc:868
uint32_t ReadU32()
Definition: buffer.cc:966
void Prev()
go backward by one byte
Definition: buffer.h:860
Packet trailer for Ethernet.
uint32_t GetFcs() const
uint32_t Deserialize(Buffer::Iterator end) override
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
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...
uint32_t GetSerializedSize() const override
void Serialize(Buffer::Iterator end) const override
uint32_t GetTrailerSize() const
void EnableFcs(bool enable)
Enable or disable FCS checking and calculations.
EthernetTrailer()
Construct a null ethernet trailer.
bool m_calcFcs
Enabled FCS calculations.
void CalcFcs(Ptr< const Packet > p)
Updates the Fcs Field to the correct FCS.
uint32_t m_fcs
Value of the fcs contained in the trailer.
static TypeId GetTypeId()
Get the type ID.
void SetFcs(uint32_t fcs)
Sets the FCS to a new value.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
Protocol trailer serialization and deserialization.
Definition: trailer.h:41
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t CRC32Calculate(const uint8_t *data, int length)
Calculates the CRC-32 for a given input.
Definition: crc32.cc:71