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 
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 }
37 
38 void
40 {
41  m_calcFcs = enable;
42 }
43 
44 bool
46 {
47  int len = p->GetSize ();
48  uint8_t *buffer;
49  uint32_t crc;
50 
51  if (!m_calcFcs)
52  {
53  return true;
54  }
55 
56  buffer = new uint8_t[len];
57  p->CopyData (buffer, len);
58  crc = DoCalcFcs (buffer, len);
59  delete[] buffer;
60  return (m_fcs == crc);
61 }
62 
63 void
65 {
66  int len = p->GetSize ();
67  uint8_t *buffer;
68 
69  if (!m_calcFcs)
70  {
71  return;
72  }
73 
74  buffer = new uint8_t[len];
75  p->CopyData (buffer, len);
76  m_fcs = DoCalcFcs (buffer, len);
77  delete[] buffer;
78 }
79 
80 void
82 {
83  m_fcs = fcs;
84 }
85 
86 uint32_t
88 {
89  return m_fcs;
90 }
91 
92 uint32_t
94 {
95  return GetSerializedSize ();
96 }
97 
98 TypeId
100 {
101  static TypeId tid = TypeId ("ns3::EthernetTrailer")
102  .SetParent<Trailer> ()
103  .AddConstructor<EthernetTrailer> ()
104  ;
105  return tid;
106 }
107 TypeId
109 {
110  return GetTypeId ();
111 }
112 void
113 EthernetTrailer::Print (std::ostream &os) const
114 {
115  os << "fcs=" << m_fcs;
116 }
117 uint32_t
119 {
120  return 4;
121 }
122 
123 void
125 {
126  Buffer::Iterator i = end;
127  i.Prev (GetSerializedSize ());
128 
129  i.WriteU32 (m_fcs);
130 }
131 uint32_t
133 {
134  Buffer::Iterator i = end;
135  uint32_t size = GetSerializedSize ();
136  i.Prev (size);
137 
138  m_fcs = i.ReadU32 ();
139 
140  return size;
141 }
142 
143 // This code is copied from /lib/crc32.c in the linux kernel.
144 // It assumes little endian ordering.
145 uint32_t
146 EthernetTrailer::DoCalcFcs (uint8_t const *buffer, size_t len) const
147 {
148  uint32_t crc = 0xffffffff;
149  int i;
150 
151  while (len--)
152  {
153  crc ^= *buffer++;
154  for (i = 0; i < 8; i++)
155  {
156  crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
157  }
158  }
159  return ~crc;
160 }
161 
162 } // namespace ns3