A Discrete-Event Network Simulator
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 
34  : m_calcFcs (false),
35  m_fcs (0)
36 {
37  NS_LOG_FUNCTION (this);
38 }
39 
40 void
42 {
43  NS_LOG_FUNCTION (this << enable);
44  m_calcFcs = enable;
45 }
46 
47 bool
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
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
87 {
88  NS_LOG_FUNCTION (this << fcs);
89  m_fcs = fcs;
90 }
91 
92 uint32_t
94 {
95  NS_LOG_FUNCTION (this);
96  return m_fcs;
97 }
98 
99 uint32_t
101 {
102  NS_LOG_FUNCTION (this);
103  return GetSerializedSize ();
104 }
105 
106 TypeId
108 {
109  static TypeId tid = TypeId ("ns3::EthernetTrailer")
110  .SetParent<Trailer> ()
111  .SetGroupName("Network")
112  .AddConstructor<EthernetTrailer> ()
113  ;
114  return tid;
115 }
116 TypeId
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
129 {
130  NS_LOG_FUNCTION (this);
131  return 4;
132 }
133 
134 void
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
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
virtual uint32_t GetSerializedSize(void) const
uint32_t ReadU32(void)
Definition: buffer.cc:972
#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:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
void CalcFcs(Ptr< const Packet > p)
Updates the Fcs Field to the correct FCS.
void SetFcs(uint32_t fcs)
Sets the FCS to a new value.
uint32_t CRC32Calculate(const uint8_t *data, int length)
Calculates the CRC-32 for a given input.
Definition: crc32.cc:71
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
uint32_t GetTrailerSize() const
uint32_t m_fcs
Value of the fcs contained in the trailer.
iterator in a Buffer instance
Definition: buffer.h:98
uint32_t GetFcs() const
virtual uint32_t Deserialize(Buffer::Iterator end)
void Prev(void)
go backward by one byte
Definition: buffer.h:851
static TypeId GetTypeId(void)
Get the type ID.
EthernetTrailer()
Construct a null ethernet trailer.
bool m_calcFcs
Enabled FCS calculations.
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Packet trailer for Ethernet.
void EnableFcs(bool enable)
Enable or disable FCS checking and calculations.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
virtual void Serialize(Buffer::Iterator end) const
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
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...
virtual void Print(std::ostream &os) const
void WriteU32(uint32_t data)
Definition: buffer.cc:878
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923