INET Framework for OMNeT++/OMNEST
inet::serializer::EthernetSerializer Class Reference

Converts between EtherFrame and binary (network byte order) Ethernet header. More...

#include <EthernetSerializer.h>

Inheritance diagram for inet::serializer::EthernetSerializer:
inet::serializer::SerializerBase

Public Member Functions

 EthernetSerializer (const char *name=nullptr)
 
- Public Member Functions inherited from inet::serializer::SerializerBase
 SerializerBase (const char *name=nullptr)
 
void serializePacket (const cPacket *pkt, Buffer &b, Context &context)
 
cPacket * deserializePacket (const Buffer &b, Context &context)
 

Protected Member Functions

virtual void serialize (const cPacket *pkt, Buffer &b, Context &context) override
 Serializes an EtherFrame for transmission on the wire. More...
 
virtual cPacket * deserialize (const Buffer &b, Context &context) override
 Puts a packet sniffed from the wire into an EtherFrame. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from inet::serializer::SerializerBase
static SerializerBaselookupSerializer (const cPacket *pkt, Context &context, ProtocolGroup group, int id)
 
static void lookupAndSerialize (const cPacket *pkt, Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
 
static SerializerBaselookupDeserializer (Context &context, ProtocolGroup group, int id)
 
static cPacket * lookupAndDeserialize (const Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
 

Detailed Description

Converts between EtherFrame and binary (network byte order) Ethernet header.

Constructor & Destructor Documentation

inet::serializer::EthernetSerializer::EthernetSerializer ( const char *  name = nullptr)
inline
44 : SerializerBase(name) {}
SerializerBase(const char *name=nullptr)
Definition: SerializerBase.h:84

Member Function Documentation

cPacket * inet::serializer::EthernetSerializer::deserialize ( const Buffer b,
Context context 
)
overrideprotectedvirtual

Puts a packet sniffed from the wire into an EtherFrame.

Implements inet::serializer::SerializerBase.

102 {
103  ASSERT(b.getPos() == 0);
104  EtherFrame *etherPacket = nullptr;
105  ProtocolGroup protocolGroup = UNKNOWN;
106  int protocolType = -1;
107 
108  int frameLength = b.getRemainingSize();
109  unsigned int payloadLength = 0;
110  MACAddress destAddr = b.readMACAddress();
111  MACAddress srcAddr = b.readMACAddress();
112  uint16_t typeOrLength = b.readUint16();
113 
114  // detect and create the real packet type.
115  if (typeOrLength >= 0x0600 || typeOrLength == 0) {
116  EthernetIIFrame *ethernetIIPacket = new EthernetIIFrame();
117  ethernetIIPacket->setEtherType(typeOrLength);
118  protocolGroup = ETHERTYPE;
119  protocolType = typeOrLength;
120  etherPacket = ethernetIIPacket;
121  payloadLength = b.getRemainingSize(4);
122  }
123  else {
124  payloadLength = typeOrLength;
125  EtherFrameWithLLC *ethLLC = nullptr;
126  uint8_t ssap = b.readByte();
127  uint8_t dsap = b.readByte();
128  uint8_t ctrl = b.readByte();
129  if (dsap == 0xAA && ssap == 0xAA) { // snap frame
130  EtherFrameWithSNAP *ethSnap = new EtherFrameWithSNAP();
131  ethSnap->setOrgCode(((uint32_t)b.readByte() << 16) + b.readUint16());
132  protocolGroup = ETHERTYPE;
133  protocolType = b.readUint16();
134  ethSnap->setLocalcode(protocolType);
135  ethLLC = ethSnap;
136  }
137  else
138  ethLLC = new EtherFrameWithLLC();
139  ethLLC->setDsap(dsap);
140  ethLLC->setSsap(ssap);
141  ethLLC->setControl(ctrl);
142  etherPacket = ethLLC;
143  }
144 
145  etherPacket->setDest(destAddr);
146  etherPacket->setSrc(srcAddr);
147  etherPacket->setByteLength(b.getPos() + 4); // +4 for trailing crc
148 
149  cPacket *encapPacket = SerializerBase::lookupAndDeserialize(b, c, protocolGroup, protocolType, payloadLength);
150  ASSERT(encapPacket);
151  etherPacket->encapsulate(encapPacket);
152  etherPacket->setName(encapPacket->getName());
153  if (b.getRemainingSize() > 4) { // padding
154  etherPacket->addByteLength(b.getRemainingSize() - 4);
155  b.accessNBytes(b.getRemainingSize() - 4);
156  }
157  uint32_t calculatedFcs = ethernetCRC(b._getBuf(), b.getPos());
158  uint32_t receivedFcs = b.readUint32();
159  EV_DEBUG << "Calculated FCS: " << calculatedFcs << ", received FCS: " << receivedFcs << endl;
160  if (receivedFcs != calculatedFcs)
161  etherPacket->setBitError(true);
162  if (etherPacket->getByteLength() != frameLength)
163  etherPacket->setBitError(true);
164  return etherPacket;
165 }
Definition: SerializerBase.h:36
Definition: SerializerBase.h:39
uint32_t ethernetCRC(const unsigned char *buf, unsigned int bufsize)
Definition: EthernetCRC.cc:67
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
static cPacket * lookupAndDeserialize(const Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
Definition: SerializerBase.cc:99
value< double, units::m > b
Definition: Units.h:1054
ProtocolGroup
Definition: SerializerBase.h:35
void inet::serializer::EthernetSerializer::serialize ( const cPacket *  pkt,
Buffer b,
Context context 
)
overrideprotectedvirtual

Serializes an EtherFrame for transmission on the wire.

Implements inet::serializer::SerializerBase.

40 {
41  ASSERT(b.getPos() == 0);
42  const EtherFrame *ethPkt = check_and_cast<const EtherFrame *>(pkt);
43  b.writeMACAddress(ethPkt->getDest());
44  b.writeMACAddress(ethPkt->getSrc());
45  if (dynamic_cast<const EthernetIIFrame *>(pkt)) {
46  const EthernetIIFrame *frame = static_cast<const EthernetIIFrame *>(pkt);
47  uint16_t ethType = frame->getEtherType();
48  b.writeUint16(ethType);
49  cPacket *encapPkt = frame->getEncapsulatedPacket();
50  SerializerBase::lookupAndSerialize(encapPkt, b, c, ETHERTYPE, ethType, b.getRemainingSize(4));
51  }
52  else if (dynamic_cast<const EtherFrameWithLLC *>(pkt)) {
53  const EtherFrameWithLLC *frame = static_cast<const EtherFrameWithLLC *>(pkt);
54  cPacket *encapPkt = frame->getEncapsulatedPacket();
55  unsigned int payloadLengthPos = b.getPos();
56  b.writeUint16(0xFFFF);
57  b.writeByte(frame->getSsap());
58  b.writeByte(frame->getDsap());
59  b.writeByte(frame->getControl());
60  if (dynamic_cast<const EtherFrameWithSNAP *>(pkt)) {
61  const EtherFrameWithSNAP *frame = static_cast<const EtherFrameWithSNAP *>(pkt);
62  b.writeByte(frame->getOrgCode() >> 16);
63  b.writeByte(frame->getOrgCode() >> 8);
64  b.writeByte(frame->getOrgCode());
65  b.writeUint16(frame->getLocalcode());
66  unsigned int payloadLength = b.getRemainingSize(4);
67  if (frame->getOrgCode() == 0) {
68  b.writeUint16To(payloadLengthPos, payloadLength);
69  SerializerBase::lookupAndSerialize(encapPkt, b, c, ETHERTYPE, frame->getLocalcode(), payloadLength);
70  }
71  else {
72  //TODO
73  b.writeUint16To(payloadLengthPos, payloadLength);
74  SerializerBase::lookupAndSerialize(encapPkt, b, c, UNKNOWN, frame->getLocalcode(), payloadLength);
75  }
76  }
77  else if (typeid(*frame) == typeid(EtherFrameWithLLC)) {
78  unsigned int payloadLength = b.getRemainingSize(4);
79  b.writeUint16To(payloadLengthPos, payloadLength);
80  SerializerBase::lookupAndSerialize(encapPkt, b, c, UNKNOWN, 0, payloadLength);
81  }
82  else {
83  throw cRuntimeError("Serializer not found for '%s'", pkt->getClassName());
84  }
85  }
86  else if (dynamic_cast<const EtherPauseFrame *>(pkt)) {
87  const EtherPauseFrame *frame = static_cast<const EtherPauseFrame *>(pkt);
88  b.writeUint16(0x8808);
89  b.writeUint16(0x0001);
90  b.writeUint16(frame->getPauseTime());
91  }
92  else {
93  throw cRuntimeError("Serializer not found for '%s'", pkt->getClassName());
94  }
95  if (b.getPos() + 4 < pkt->getByteLength())
96  b.fillNBytes((pkt->getByteLength() - 4) - b.getPos(), 0);
97  uint32_t fcs = ethernetCRC(b._getBuf(), b.getPos());
98  b.writeUint32(fcs);
99 }
Definition: SerializerBase.h:36
Definition: SerializerBase.h:39
uint32_t ethernetCRC(const unsigned char *buf, unsigned int bufsize)
Definition: EthernetCRC.cc:67
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
static void lookupAndSerialize(const cPacket *pkt, Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
Definition: SerializerBase.cc:88
value< double, units::m > b
Definition: Units.h:1054

The documentation for this class was generated from the following files: