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

Converts between RipPacket and binary (network byte order) RIP data. More...

#include <RipPacketSerializer.h>

Inheritance diagram for inet::RipPacketSerializer:
inet::FieldsChunkSerializer inet::ChunkSerializer

Public Member Functions

 RipPacketSerializer ()
 
- 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 RipPacket and binary (network byte order) RIP data.

Constructor & Destructor Documentation

◆ RipPacketSerializer()

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

Member Function Documentation

◆ deserialize()

const Ptr< Chunk > inet::RipPacketSerializer::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.

45 {
46  auto ripPacket = makeShared<RipPacket>();
47 
48  ripPacket->setCommand((inet::RipCommand)stream.readUint8());
49  int ripVer = stream.readUint8();
50  if (ripVer != 2) {
51  // TODO add RIP v1 support
52  ripPacket->markIncorrect();
53  }
54 
55  int numEntries = stream.readUint16Be();
56  ripPacket->setEntryArraySize(numEntries);
57 
58  for (int i = 0; i < numEntries; ++i) {
59 
60  RipEntry entry = {};
61 
62  entry.addressFamilyId = (inet::RipAf)stream.readUint16Be();
63  // TODO Valid addressFamilyId values: 0, 2, 0xFFFF
64  // 0 and 2 means IPv4, 0xFFFF means Authentication packet
65  entry.routeTag = stream.readUint16Be();
66  entry.address = stream.readIpv4Address();
67  Ipv4Address netmask = stream.readIpv4Address();
68  entry.prefixLength = netmask.getNetmaskLength();
69  if (netmask != Ipv4Address::makeNetmask(entry.prefixLength))
70  ripPacket->markIncorrect(); // netmask can not be converted into prefixLength //TODO should replace prefixLength to netmask
71  entry.nextHop = stream.readIpv4Address();
72  entry.metric = stream.readUint32Be();
73 
74  ripPacket->setEntry(i, entry);
75  }
76 
77  return ripPacket;
78 }

◆ serialize()

void inet::RipPacketSerializer::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.

23 {
24  const auto& ripPacket = staticPtrCast<const RipPacket>(chunk);
25 
26  stream.writeUint8(ripPacket->getCommand());
27  stream.writeUint8(2); // RIP version
28 
29  int numEntries = ripPacket->getEntryArraySize();
30  stream.writeUint16Be(numEntries);
31 
32  // iterate over each entry and write to stream
33  for (int i = 0; i < numEntries; ++i) {
34  const RipEntry& entry = ripPacket->getEntry(i);
35  stream.writeUint16Be(entry.addressFamilyId);
36  stream.writeUint16Be(entry.routeTag);
37  stream.writeIpv4Address(entry.address.toIpv4());
38  stream.writeIpv4Address(Ipv4Address::makeNetmask(entry.prefixLength));
39  stream.writeIpv4Address(entry.nextHop.toIpv4());
40  stream.writeUint32Be(entry.metric);
41  }
42 }

The documentation for this class was generated from the following files:
inet::RipCommand
RipCommand
Enum generated from inet/routing/rip/RipPacket.msg:22 by opp_msgtool.
Definition: RipPacket_m.h:67
inet::RipAf
RipAf
Enum generated from inet/routing/rip/RipPacket.msg:30 by opp_msgtool.
Definition: RipPacket_m.h:89
inet::Ipv4Address::makeNetmask
static Ipv4Address makeNetmask(int length)
Creates and returns a netmask with the given length.
Definition: Ipv4Address.h:329