A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ethernet-trailer.cc
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 
33  : m_calcFcs (false),
34  m_fcs (0)
35 {
36  NS_LOG_FUNCTION (this);
37 }
38 
39 void
41 {
42  NS_LOG_FUNCTION (this << enable);
43  m_calcFcs = enable;
44 }
45 
46 bool
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
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
86 {
87  NS_LOG_FUNCTION (this << fcs);
88  m_fcs = fcs;
89 }
90 
91 uint32_t
93 {
94  NS_LOG_FUNCTION (this);
95  return m_fcs;
96 }
97 
98 uint32_t
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
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
127 {
128  NS_LOG_FUNCTION (this);
129  return 4;
130 }
131 
132 void
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
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
uint32_t ReadU32(void)
Definition: buffer.cc:997
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
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 GetTrailerSize() const
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
uint32_t GetSize(void) const
Definition: packet.h:620
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
virtual uint32_t Deserialize(Buffer::Iterator end)
void Prev(void)
Definition: buffer.h:672
virtual void Print(std::ostream &os) const
EthernetTrailer()
Construct a null ethernet trailer.
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
Definition: packet.cc:398
uint32_t DoCalcFcs(uint8_t const *buffer, size_t len) const
Value of the fcs contained in the trailer.
void WriteU32(uint32_t data)
Definition: buffer.cc:903
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471