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  ;
33 
35  : m_calcFcs (false),
36  m_fcs (0)
37 {
38  NS_LOG_FUNCTION (this);
39 }
40 
41 void
43 {
44  NS_LOG_FUNCTION (this << enable);
45  m_calcFcs = enable;
46 }
47 
48 bool
50 {
51  NS_LOG_FUNCTION (this << p);
52  int len = p->GetSize ();
53  uint8_t *buffer;
54  uint32_t crc;
55 
56  if (!m_calcFcs)
57  {
58  return true;
59  }
60 
61  buffer = new uint8_t[len];
62  p->CopyData (buffer, len);
63  crc = CRC32Calculate (buffer, len);
64  delete[] buffer;
65  return (m_fcs == crc);
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION (this << p);
72  int len = p->GetSize ();
73  uint8_t *buffer;
74 
75  if (!m_calcFcs)
76  {
77  return;
78  }
79 
80  buffer = new uint8_t[len];
81  p->CopyData (buffer, len);
82  m_fcs = CRC32Calculate (buffer, len);
83  delete[] buffer;
84 }
85 
86 void
88 {
89  NS_LOG_FUNCTION (this << fcs);
90  m_fcs = fcs;
91 }
92 
93 uint32_t
95 {
96  NS_LOG_FUNCTION (this);
97  return m_fcs;
98 }
99 
100 uint32_t
102 {
103  NS_LOG_FUNCTION (this);
104  return GetSerializedSize ();
105 }
106 
107 TypeId
109 {
110  static TypeId tid = TypeId ("ns3::EthernetTrailer")
111  .SetParent<Trailer> ()
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
uint32_t ReadU32(void)
Definition: buffer.cc:997
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
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)
Definition: crc32.cc:68
uint32_t GetTrailerSize() const
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
uint32_t GetSize(void) const
Definition: packet.h:650
virtual uint32_t GetSerializedSize(void) const
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:672
virtual void Print(std::ostream &os) const
static TypeId GetTypeId(void)
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
NS_LOG_COMPONENT_DEFINE("EthernetTrailer")
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:903
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611