INET Framework for OMNeT++/OMNEST
inet::IcmpHeaderSerializer Class Reference

Converts between IcmpHeader and binary (network byte order) ICMP header. More...

#include <IcmpHeaderSerializer.h>

Inheritance diagram for inet::IcmpHeaderSerializer:
inet::FieldsChunkSerializer inet::ChunkSerializer

Public Member Functions

 IcmpHeaderSerializer ()
 
- Public Member Functions inherited from inet::FieldsChunkSerializer
virtual void serialize (MemoryOutputStream &stream, const Ptr< const Chunk > &chunk, b offset, b length) const override
 Serializes a chunk into a stream by writing the bytes representing the chunk at the end of the stream. More...
 
virtual const Ptr< Chunkdeserialize (MemoryInputStream &stream, const std::type_info &typeInfo) const override
 Deserializes a chunk from a stream by reading the bytes at the current position of the stream. More...
 

Protected Member Functions

virtual void serialize (MemoryOutputStream &stream, const Ptr< const Chunk > &chunk) const override
 Serializes a chunk into a stream by writing all bytes representing the chunk at the end of the stream. More...
 
virtual const Ptr< Chunkdeserialize (MemoryInputStream &stream) const override
 Deserializes a chunk from a stream by reading the bytes at the current position of the stream. More...
 

Additional Inherited Members

- Static Public Attributes inherited from inet::ChunkSerializer
static b totalSerializedLength = b(0)
 
static b totalDeserializedLength = b(0)
 

Detailed Description

Converts between IcmpHeader and binary (network byte order) ICMP header.

Constructor & Destructor Documentation

◆ IcmpHeaderSerializer()

inet::IcmpHeaderSerializer::IcmpHeaderSerializer ( )
inline
25 : FieldsChunkSerializer() {}

Member Function Documentation

◆ deserialize()

const Ptr< Chunk > inet::IcmpHeaderSerializer::deserialize ( MemoryInputStream stream) const
overrideprotectedvirtual

Deserializes a chunk from a stream by reading the bytes at the current position of the stream.

The current stream position is updated according to the length of the returned chunk.

Implements inet::FieldsChunkSerializer.

59 {
60  auto icmpHeader = makeShared<IcmpHeader>();
61  IcmpType type = static_cast<IcmpType>(stream.readByte());
62  icmpHeader->setType(type);
63  icmpHeader->setCode(stream.readByte());
64  icmpHeader->setChksum(stream.readUint16Be());
65  icmpHeader->setCrcMode(CRC_COMPUTED);
66  switch (type) {
67  case ICMP_ECHO_REQUEST: {
68  auto echoRq = makeShared<IcmpEchoRequest>();
69  echoRq->setType(type);
70  echoRq->setCode(icmpHeader->getCode());
71  echoRq->setChksum(icmpHeader->getChksum());
72  echoRq->setCrcMode(CRC_COMPUTED);
73  echoRq->setIdentifier(stream.readUint16Be());
74  echoRq->setSeqNumber(stream.readUint16Be());
75  icmpHeader = echoRq;
76  break;
77  }
78  case ICMP_ECHO_REPLY: {
79  auto echoReply = makeShared<IcmpEchoReply>();
80  echoReply->setType(type);
81  echoReply->setCode(icmpHeader->getCode());
82  echoReply->setChksum(icmpHeader->getChksum());
83  echoReply->setCrcMode(CRC_COMPUTED);
84  echoReply->setIdentifier(stream.readUint16Be());
85  echoReply->setSeqNumber(stream.readUint16Be());
86  icmpHeader = echoReply;
87  break;
88  }
90  if (icmpHeader->getCode() == ICMP_DU_FRAGMENTATION_NEEDED) {
91  auto icmpPtb = makeShared<IcmpPtb>();
92  icmpPtb->setType(type);
93  icmpPtb->setCode(icmpHeader->getCode());
94  icmpPtb->setChksum(icmpHeader->getChksum());
95  icmpPtb->setCrcMode(CRC_COMPUTED);
96  icmpPtb->setUnused(stream.readUint16Be());
97  icmpPtb->setMtu(stream.readUint16Be());
98  icmpHeader = icmpPtb;
99  }
100  else
101  stream.readUint32Be(); // unused
102  break;
103  case ICMP_TIME_EXCEEDED:
104  stream.readUint32Be(); // unused
105  break;
106  default:
107  EV_ERROR << "Can not parse ICMP packet: type " << type << " not supported.";
108  icmpHeader->markImproperlyRepresented();
109  break;
110  }
111  return icmpHeader;
112 }

◆ serialize()

void inet::IcmpHeaderSerializer::serialize ( MemoryOutputStream stream,
const Ptr< const Chunk > &  chunk 
) const
overrideprotectedvirtual

Serializes a chunk into a stream by writing all bytes representing the chunk at the end of the stream.

Implements inet::FieldsChunkSerializer.

20 {
21  const auto& icmpHeader = staticPtrCast<const IcmpHeader>(chunk);
22  stream.writeByte(icmpHeader->getType());
23  stream.writeByte(icmpHeader->getCode());
24  stream.writeUint16Be(icmpHeader->getChksum());
25  switch (icmpHeader->getType()) {
26  case ICMP_ECHO_REQUEST: {
27  const auto& icmpEchoRq = CHK(dynamicPtrCast<const IcmpEchoRequest>(chunk));
28  stream.writeUint16Be(icmpEchoRq->getIdentifier());
29  stream.writeUint16Be(icmpEchoRq->getSeqNumber());
30  break;
31  }
32  case ICMP_ECHO_REPLY: {
33  const auto& icmpEchoReply = CHK(dynamicPtrCast<const IcmpEchoReply>(chunk));
34  stream.writeUint16Be(icmpEchoReply->getIdentifier());
35  stream.writeUint16Be(icmpEchoReply->getSeqNumber());
36  break;
37  }
39  if (icmpHeader->getCode() == ICMP_DU_FRAGMENTATION_NEEDED) {
40  if (const auto& icmpPtb = dynamicPtrCast<const IcmpPtb>(chunk)) {
41  stream.writeUint16Be(icmpPtb->getUnused()); // unused
42  stream.writeUint16Be(icmpPtb->getMtu()); // next hop MTU
43  }
44  else
45  stream.writeUint32Be(0);
46  }
47  else
48  stream.writeUint32Be(0); // unused
49  break;
50  case ICMP_TIME_EXCEEDED:
51  stream.writeUint32Be(0); // unused
52  break;
53  default:
54  throw cRuntimeError("Can not serialize ICMP packet: type %d not supported.", icmpHeader->getType());
55  }
56 }

The documentation for this class was generated from the following files:
CHK
#define CHK(x)
Definition: INETDefs.h:87
inet::ICMP_DESTINATION_UNREACHABLE
@ ICMP_DESTINATION_UNREACHABLE
Definition: IcmpHeader_m.h:77
inet::ICMP_DU_FRAGMENTATION_NEEDED
@ ICMP_DU_FRAGMENTATION_NEEDED
Definition: IcmpHeader_m.h:190
inet::CRC_COMPUTED
@ CRC_COMPUTED
Definition: CrcMode_m.h:59
inet::ICMP_ECHO_REQUEST
@ ICMP_ECHO_REQUEST
Definition: IcmpHeader_m.h:80
inet::IcmpType
IcmpType
Enum generated from inet/networklayer/ipv4/IcmpHeader.msg:18 by opp_msgtool.
Definition: IcmpHeader_m.h:76
inet::ICMP_TIME_EXCEEDED
@ ICMP_TIME_EXCEEDED
Definition: IcmpHeader_m.h:83
type
removed type
Definition: IUdp-gates.txt:7
inet::ICMP_ECHO_REPLY
@ ICMP_ECHO_REPLY
Definition: IcmpHeader_m.h:85