View | Details | Raw Unified | Return to bug 751
Collapse All | Expand All

(-)a/src/devices/csma/csma-net-device.cc (+8 lines)
 Lines 421-426    Link Here 
421
  header.SetLengthType (lengthType);
428
  header.SetLengthType (lengthType);
422
  p->AddHeader (header);
429
  p->AddHeader (header);
423
430
431
  if (Node::ChecksumEnabled ())
432
    {
433
      trailer.EnableFcs (true);
434
    }
424
  trailer.CalcFcs (p);
435
  trailer.CalcFcs (p);
425
  p->AddTrailer (trailer);
436
  p->AddTrailer (trailer);
426
}
437
}
 Lines 747-752    Link Here 
747
758
748
  EthernetTrailer trailer;
759
  EthernetTrailer trailer;
749
  packet->RemoveTrailer (trailer);
760
  packet->RemoveTrailer (trailer);
761
  if (Node::ChecksumEnabled ())
762
    {
763
      trailer.EnableFcs (true);
764
    }
750
  trailer.CheckFcs (packet);
765
  trailer.CheckFcs (packet);
751
766
752
  EthernetHeader header (false);
767
  EthernetHeader header (false);
(-)a/src/node/ethernet-trailer.cc (-16 / +44 lines)
 Lines 29-45    Link Here 
29
29
30
NS_OBJECT_ENSURE_REGISTERED (EthernetTrailer);
30
NS_OBJECT_ENSURE_REGISTERED (EthernetTrailer);
31
31
32
bool EthernetTrailer::m_calcFcs = false;
33
34
EthernetTrailer::EthernetTrailer ()
32
EthernetTrailer::EthernetTrailer ()
35
{
33
  : m_calcFcs (false),
36
  Init();
34
    m_fcs (0)
37
}
35
{}
38
39
void EthernetTrailer::Init()
40
{
41
  m_fcs = 0;
42
}
43
36
44
void 
37
void 
45
EthernetTrailer::EnableFcs (bool enable)
38
EthernetTrailer::EnableFcs (bool enable)
 Lines 50-70    Link Here 
50
bool
43
bool
51
EthernetTrailer::CheckFcs (Ptr<Packet> p) const
44
EthernetTrailer::CheckFcs (Ptr<Packet> p) const
52
{
45
{
46
  int len = p->GetSize ();
47
  uint8_t *buffer;
48
  uint32_t crc;
49
53
  if (!m_calcFcs)
50
  if (!m_calcFcs)
54
    {
51
    {
55
      return true;
52
      return true;
56
    } 
57
  else 
58
    {
59
      NS_LOG_WARN ("FCS calculation is not yet enabled");
60
      return false;
61
    }
53
    }
54
55
  buffer = new uint8_t[len];
56
  p->CopyData (buffer, len);
57
  crc = DoCalcFcs (buffer, len);
58
  delete[] buffer;
59
  return (m_fcs == crc);
62
}
60
}
63
61
64
void
62
void
65
EthernetTrailer::CalcFcs (Ptr<Packet> p)
63
EthernetTrailer::CalcFcs (Ptr<Packet> p)
66
{
64
{
67
  NS_LOG_WARN ("FCS calculation is not yet enabled");
65
  int len = p->GetSize ();
66
  uint8_t *buffer;
67
68
  if (!m_calcFcs)
69
    {
70
      return;
71
    }
72
73
  buffer = new uint8_t[len];
74
  p->CopyData (buffer, len);
75
  m_fcs = DoCalcFcs (buffer, len);
76
  delete[] buffer;
68
}
77
}
69
78
70
void
79
void
 Lines 130-133    Link Here 
130
  return size;
139
  return size;
131
}
140
}
132
141
142
// This code is copied from /lib/crc32.c in the linux kernel.
143
// It assumes little endian ordering.
144
uint32_t
145
EthernetTrailer::DoCalcFcs (uint8_t *buffer, size_t len) const
146
{
147
  uint32_t crc = 0xffffffff;
148
  int i;
149
150
  while (len--)
151
    {
152
      crc ^= *buffer++;
153
      for (i = 0; i < 8; i++)
154
        {
155
	  crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
156
	}
157
    }
158
  return ~crc;
159
}
160
133
}; // namespace ns3
161
}; // namespace ns3
(-)a/src/node/ethernet-trailer.h (-10 / +6 lines)
 Lines 22-32    Link Here 
22
#define ETHERNET_TRAILER_H
22
#define ETHERNET_TRAILER_H
23
23
24
#include "ns3/trailer.h"
24
#include "ns3/trailer.h"
25
#include "ns3/packet.h"
25
#include <string>
26
#include <string>
26
27
27
namespace ns3 {
28
namespace ns3 {
28
29
29
class Packet;
30
30
31
/**
31
/**
32
 * \ingroup node
32
 * \ingroup node
 Lines 46-55    Link Here 
46
  EthernetTrailer ();
46
  EthernetTrailer ();
47
47
48
  /**
48
  /**
49
   * \brief Enable or disabled FCS checking and calculations
49
   * \brief Enable or disable FCS checking and calculations
50
   * \param enable If true, enables FCS calculations.
50
   * \param enable If true, enables FCS calculations.
51
   */
51
   */
52
  static void EnableFcs (bool enable);
52
  void EnableFcs (bool enable);
53
  /**
53
  /**
54
   * \brief Updates the Fcs Field to the correct FCS
54
   * \brief Updates the Fcs Field to the correct FCS
55
   * \param p Reference to a packet on which the FCS should be
55
   * \param p Reference to a packet on which the FCS should be
 Lines 89-107    Link Here 
89
  virtual void Serialize (Buffer::Iterator end) const;
89
  virtual void Serialize (Buffer::Iterator end) const;
90
  virtual uint32_t Deserialize (Buffer::Iterator end);
90
  virtual uint32_t Deserialize (Buffer::Iterator end);
91
private:
91
private:
92
93
  /**
94
   * Initializes the trailer parameters during construction.
95
   */
96
  void Init (void);
97
98
  /**
92
  /**
99
   * Enabled FCS calculations. If false, fcs is set to 0 and checkFCS
93
   * Enabled FCS calculations. If false, fcs is set to 0 and checkFCS
100
   * returns true.
94
   * returns true.
101
   */
95
   */
102
  static bool m_calcFcs;
96
  bool m_calcFcs;
103
  uint32_t m_fcs; /// Value of the fcs contained in the trailer
97
  uint32_t m_fcs; /// Value of the fcs contained in the trailer
104
98
99
  uint32_t DoCalcFcs (uint8_t *buffer, size_t len) const;
100
105
};
101
};
106
102
107
} // namespace ns3
103
} // namespace ns3

Return to bug 751