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

Converts between UDPPacket and binary (network byte order) UDP header. More...

#include <UDPSerializer.h>

Inheritance diagram for inet::serializer::UDPSerializer:
inet::serializer::SerializerBase

Public Member Functions

 UDPSerializer (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 a cPacket 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 UDPPacket and binary (network byte order) UDP header.

Constructor & Destructor Documentation

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

Member Function Documentation

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

Puts a packet sniffed from the wire into an EtherFrame.

Implements inet::serializer::SerializerBase.

79 {
80  ASSERT(b.getPos() == 0);
81  UDPPacket *pkt = new UDPPacket("parsed-udp");
82  pkt->setSourcePort(b.readUint16());
83  pkt->setDestinationPort(b.readUint16());
84  unsigned int length = b.readUint16();
85  pkt->setTotalLengthField(length);
86  uint16_t chksum = b.readUint16();
87  if (length > UDP_HEADER_BYTES) {
88  unsigned int payloadLength = length - UDP_HEADER_BYTES;
89  Buffer subBuffer(b, payloadLength);
90  cPacket *encapPacket = serializers.byteArraySerializer.deserializePacket(subBuffer, c);
91  b.accessNBytes(payloadLength);
92  pkt->encapsulate(encapPacket);
93  }
94  if (chksum != 0 && c.l3AddressesPtr && c.l3AddressesLength)
95  chksum = TCPIPchecksum::checksum(IP_PROT_UDP, b._getBuf(), b.getPos(), c.l3AddressesPtr, c.l3AddressesLength);
96  else chksum = 0;
97  if (length < UDP_HEADER_BYTES || chksum != 0 || pkt->getByteLength() != length)
98  pkt->setBitError(true);
99  return pkt;
100 }
static ByteArraySerializer byteArraySerializer
Definition: SerializerBase.h:113
static uint16_t checksum(const void *addr, unsigned int count)
Definition: TCPIPchecksum.h:44
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
Definition: UDPPacket_m.h:48
SerializerRegistrationList serializers("default")
List of packet serializers (SerializerBase)
Definition: SerializerBase.h:146
Definition: IPProtocolId_m.h:83
cPacket * deserializePacket(const Buffer &b, Context &context)
Definition: SerializerBase.cc:42
value< double, units::m > b
Definition: Units.h:1054
void inet::serializer::UDPSerializer::serialize ( const cPacket *  pkt,
Buffer b,
Context context 
)
overrideprotectedvirtual

Serializes a cPacket for transmission on the wire.

Returns the length of data written into buffer.

Implements inet::serializer::SerializerBase.

58 {
59  ASSERT(b.getPos() == 0);
60  const UDPPacket *pkt = check_and_cast<const UDPPacket *>(_pkt);
61  int packetLength = pkt->getByteLength();
62  b.writeUint16(pkt->getSourcePort());
63  b.writeUint16(pkt->getDestinationPort());
64  b.writeUint16(pkt->getTotalLengthField());
65  unsigned int chksumPos = b.getPos();
66  b.writeUint16(0); // place for checksum
67  const cPacket *encapPkt = pkt->getEncapsulatedPacket();
68  if (encapPkt) {
70  }
71  else {
72  b.fillNBytes(packetLength - UDP_HEADER_BYTES, 0); // payload place
73  }
74  unsigned int endPos = b.getPos();
75  b.writeUint16To(chksumPos, TCPIPchecksum::checksum(IP_PROT_UDP, b._getBuf(), endPos, c.l3AddressesPtr, c.l3AddressesLength));
76 }
Definition: SerializerBase.h:36
static uint16_t checksum(const void *addr, unsigned int count)
Definition: TCPIPchecksum.h:44
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
Definition: UDPPacket_m.h:48
static void lookupAndSerialize(const cPacket *pkt, Buffer &b, Context &context, ProtocolGroup group, int id, unsigned int maxLength=(unsigned int)(-1))
Definition: SerializerBase.cc:88
Definition: IPProtocolId_m.h:83
value< double, units::m > b
Definition: Units.h:1054

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