A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ethernet-trailer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
7 */
8
9#include "ethernet-trailer.h"
10
11#include "crc32.h"
12
13#include "ns3/assert.h"
14#include "ns3/log.h"
15#include "ns3/trailer.h"
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("EthernetTrailer");
21
22NS_OBJECT_ENSURE_REGISTERED(EthernetTrailer);
23
25 : m_calcFcs(false),
26 m_fcs(0)
27{
28 NS_LOG_FUNCTION(this);
29}
30
31void
33{
34 NS_LOG_FUNCTION(this << enable);
35 m_calcFcs = enable;
36}
37
38bool
40{
41 NS_LOG_FUNCTION(this << p);
42 int len = p->GetSize();
43 uint8_t* buffer;
44 uint32_t crc;
45
46 if (!m_calcFcs)
47 {
48 return true;
49 }
50
51 buffer = new uint8_t[len];
52 p->CopyData(buffer, len);
53 crc = CRC32Calculate(buffer, len);
54 delete[] buffer;
55 return (m_fcs == crc);
56}
57
58void
60{
61 NS_LOG_FUNCTION(this << p);
62 int len = p->GetSize();
63 uint8_t* buffer;
64
65 if (!m_calcFcs)
66 {
67 return;
68 }
69
70 buffer = new uint8_t[len];
71 p->CopyData(buffer, len);
72 m_fcs = CRC32Calculate(buffer, len);
73 delete[] buffer;
74}
75
76void
78{
79 NS_LOG_FUNCTION(this << fcs);
80 m_fcs = fcs;
81}
82
85{
86 NS_LOG_FUNCTION(this);
87 return m_fcs;
88}
89
96
99{
100 static TypeId tid = TypeId("ns3::EthernetTrailer")
102 .SetGroupName("Network")
103 .AddConstructor<EthernetTrailer>();
104 return tid;
105}
106
107TypeId
109{
110 return GetTypeId();
111}
112
113void
114EthernetTrailer::Print(std::ostream& os) const
115{
116 NS_LOG_FUNCTION(this << &os);
117 os << "fcs=" << m_fcs;
118}
119
122{
123 NS_LOG_FUNCTION(this);
124 return 4;
125}
126
127void
129{
130 NS_LOG_FUNCTION(this << &end);
131 Buffer::Iterator i = end;
133
134 i.WriteU32(m_fcs);
135}
136
139{
140 NS_LOG_FUNCTION(this << &end);
141 Buffer::Iterator i = end;
143 i.Prev(size);
144
145 m_fcs = i.ReadU32();
146
147 return size;
148}
149
150} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
void WriteU32(uint32_t data)
Definition buffer.cc:857
uint32_t ReadU32()
Definition buffer.cc:955
void Prev()
go backward by one byte
Definition buffer.h:849
Packet trailer for Ethernet.
uint32_t GetFcs() const
uint32_t Deserialize(Buffer::Iterator end) override
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
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...
uint32_t GetSerializedSize() const override
void Serialize(Buffer::Iterator end) const override
uint32_t GetTrailerSize() const
void EnableFcs(bool enable)
Enable or disable FCS checking and calculations.
EthernetTrailer()
Construct a null ethernet trailer.
bool m_calcFcs
Enabled FCS calculations.
void CalcFcs(Ptr< const Packet > p)
Updates the Fcs Field to the correct FCS.
uint32_t m_fcs
Value of the fcs contained in the trailer.
static TypeId GetTypeId()
Get the type ID.
void SetFcs(uint32_t fcs)
Sets the FCS to a new value.
Smart pointer class similar to boost::intrusive_ptr.
Protocol trailer serialization and deserialization.
Definition trailer.h:30
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t CRC32Calculate(const uint8_t *data, int length)
Calculates the CRC-32 for a given input.
Definition crc32.cc:60