|
|
| 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) |
|
|
| 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 |
|
|
| 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 |