A Discrete-Event Network Simulator
API
 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 #include "crc32.h"
26 
27 NS_LOG_COMPONENT_DEFINE ("EthernetTrailer");
28 
29 namespace ns3 {
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  .AddConstructor<EthernetTrailer> ()
112  ;
113  return tid;
114 }
115 TypeId
117 {
118  return GetTypeId ();
119 }
120 void
121 EthernetTrailer::Print (std::ostream &os) const
122 {
123  NS_LOG_FUNCTION (this << &os);
124  os << "fcs=" << m_fcs;
125 }
126 uint32_t
128 {
129  NS_LOG_FUNCTION (this);
130  return 4;
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION (this << &end);
137  Buffer::Iterator i = end;
138  i.Prev (GetSerializedSize ());
139 
140  i.WriteU32 (m_fcs);
141 }
142 uint32_t
144 {
145  NS_LOG_FUNCTION (this << &end);
146  Buffer::Iterator i = end;
147  uint32_t size = GetSerializedSize ();
148  i.Prev (size);
149 
150  m_fcs = i.ReadU32 ();
151 
152  return size;
153 }
154 
155 } // namespace ns3
uint32_t ReadU32(void)
Definition: buffer.cc:1001
#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 the class in the ns-3 factory.
Definition: object-base.h:38
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
uint32_t GetTrailerSize() const
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
virtual uint32_t GetSerializedSize(void) const
uint32_t m_fcs
Value of the fcs contained in the trailer.
iterator in a Buffer instance
Definition: buffer.h:98
virtual TypeId GetInstanceTypeId(void) const
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 uint32_t Deserialize(Buffer::Iterator end)
void Prev(void)
go backward by one byte
Definition: buffer.h:858
virtual void Print(std::ostream &os) const
static TypeId GetTypeId(void)
Get the type ID.
EthernetTrailer()
Construct a null ethernet trailer.
bool m_calcFcs
Enabled FCS calculations.
virtual void Serialize(Buffer::Iterator end) const
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
void EnableFcs(bool enable)
Enable or disable FCS checking and calculations.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:381
void WriteU32(uint32_t data)
Definition: buffer.cc:907
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610