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

Converts between IGMPMessage and binary (network byte order) IGMP header. More...

#include <IGMPSerializer.h>

Inheritance diagram for inet::serializer::IGMPSerializer:
inet::serializer::SerializerBase

Public Member Functions

 IGMPSerializer (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 IGMPMessage and binary (network byte order) IGMP header.

Constructor & Destructor Documentation

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

Member Function Documentation

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

Puts a packet sniffed from the wire into an EtherFrame.

Implements inet::serializer::SerializerBase.

127 {
128  unsigned int startPos = b.getPos();
129  const void *igmp = b.accessNBytes(0);
130  unsigned char type = b.readByte();
131  unsigned char code = b.readByte();
132  uint16_t chksum = b.readUint16(); (void)chksum;
133 
134  cPacket *packet = nullptr;
135 
136  switch (type) {
138  if (code == 0) {
139  IGMPv1Query *pkt = new IGMPv1Query();
140  packet = pkt;
141  pkt->setGroupAddress(b.readIPv4Address());
142  pkt->setByteLength(8);
143  }
144  else if (b._getBufSize() - startPos == 8) { // RFC 3376 Section 7.1
145  IGMPv2Query *pkt = new IGMPv2Query();
146  packet = pkt;
147  pkt->setMaxRespTime(code);
148  pkt->setGroupAddress(b.readIPv4Address());
149  pkt->setByteLength(8);
150  }
151  else {
152  IGMPv3Query *pkt = new IGMPv3Query();
153  packet = pkt;
154  pkt->setMaxRespCode(code);
155  pkt->setGroupAddress(b.readIPv4Address());
156  unsigned char x = b.readByte(); //
157  pkt->setSuppressRouterProc((x & 0x8) != 0);
158  pkt->setRobustnessVariable(x & 7);
159  pkt->setQueryIntervalCode(b.readByte());
160  unsigned int vs = b.readUint16();
161  for (unsigned int i = 0; i < vs && !b.hasError(); i++)
162  pkt->getSourceList()[i] = b.readIPv4Address();
163  pkt->setByteLength(b.getPos() - startPos);
164  }
165  break;
166 
168  {
169  IGMPv1Report *pkt;
170  packet = pkt = new IGMPv1Report();
171  pkt->setGroupAddress(b.readIPv4Address());
172  pkt->setByteLength(8);
173  }
174  break;
175 
177  {
178  IGMPv2Report *pkt;
179  packet = pkt = new IGMPv2Report();
180  pkt->setGroupAddress(b.readIPv4Address());
181  pkt->setByteLength(8);
182  }
183  break;
184 
185  case IGMPV2_LEAVE_GROUP:
186  {
187  IGMPv2Leave *pkt;
188  packet = pkt = new IGMPv2Leave();
189  pkt->setGroupAddress(b.readIPv4Address());
190  pkt->setByteLength(8);
191  }
192  break;
193 
195  {
196  IGMPv3Report *pkt;
197  packet = pkt = new IGMPv3Report();
198  b.readUint16(); //reserved
199  unsigned int s = b.readUint16();
200  pkt->setGroupRecordArraySize(s);
201  unsigned int i;
202  for (i = 0; i < s && !b.hasError(); i++) {
203  GroupRecord gr;
204  gr.recordType = b.readByte();
205  uint16_t auxDataLen = 4 * b.readByte();
206  unsigned int gac = b.readUint16();
207  gr.groupAddress = b.readIPv4Address();
208  for (unsigned int j = 0; j < gac && !b.hasError(); j++) {
209  gr.sourceList.push_back(b.readIPv4Address());
210  }
211  pkt->setGroupRecord(i, gr);
212  b.accessNBytes(auxDataLen);
213  }
214  if (i < s) {
215  pkt->setGroupRecordArraySize(i);
216  }
217  pkt->setByteLength(b.getPos() - startPos);
218  }
219  break;
220 
221  default:
222  EV_ERROR << "IGMPSerializer: can not create IGMP packet: type " << type << " not supported\n";
223  b.seek(startPos);
225  packet->setBitError(true);
226  break;
227  }
228 
229  ASSERT(packet);
230  if (b.hasError() || TCPIPchecksum::checksum(igmp, packet->getByteLength()) != 0)
231  packet->setBitError(true);
232  return packet;
233 }
Definition: IGMPMessage_m.h:62
Definition: IGMPMessage_m.h:61
static ByteArraySerializer byteArraySerializer
Definition: SerializerBase.h:113
static uint16_t checksum(const void *addr, unsigned int count)
Definition: TCPIPchecksum.h:44
Definition: IGMPMessage_m.h:60
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
Definition: IGMPMessage_m.h:58
Definition: IGMPMessage_m.h:59
value< double, units::s > s
Definition: Units.h:1049
cPacket * deserializePacket(const Buffer &b, Context &context)
Definition: SerializerBase.cc:42
value< double, units::m > b
Definition: Units.h:1054
void inet::serializer::IGMPSerializer::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.

45 {
46  unsigned int startPos = b.getPos();
47  const IGMPMessage *pkt = check_and_cast<const IGMPMessage *>(_pkt);
48  const void *igmp = b.accessNBytes(0);
49 
50  switch (pkt->getType())
51  {
52  case IGMP_MEMBERSHIP_QUERY: {
53  b.writeByte(IGMP_MEMBERSHIP_QUERY); // type
54  b.writeByte(0); // code
55  b.writeUint16(0); // chksum
56  b.writeIPv4Address(check_and_cast<const IGMPQuery*>(pkt)->getGroupAddress());
57  if (dynamic_cast<const IGMPv3Query*>(pkt))
58  {
59  const IGMPv3Query* v3pkt = static_cast<const IGMPv3Query*>(pkt);
60  b.writeByteTo(1, v3pkt->getMaxRespCode());
61  ASSERT(v3pkt->getRobustnessVariable() <= 7);
62  b.writeByte((v3pkt->getSuppressRouterProc() ? 0x8 : 0) | v3pkt->getRobustnessVariable());
63  b.writeByte(v3pkt->getQueryIntervalCode());
64  unsigned int vs = v3pkt->getSourceList().size();
65  b.writeUint16(vs);
66  for (unsigned int i = 0; i < vs; i++)
67  b.writeIPv4Address(v3pkt->getSourceList()[i]);
68  }
69  else if (dynamic_cast<const IGMPv2Query*>(pkt))
70  {
71  b.writeByteTo(1, static_cast<const IGMPv2Query*>(pkt)->getMaxRespTime());
72  }
73  break;
74  }
75 
77  b.writeByte(IGMPV1_MEMBERSHIP_REPORT); // type
78  b.writeByte(0); // code
79  b.writeUint16(0); // chksum
80  b.writeIPv4Address(check_and_cast<const IGMPv1Report*>(pkt)->getGroupAddress());
81  break;
82 
84  b.writeByte(IGMPV2_MEMBERSHIP_REPORT); // type
85  b.writeByte(0); // code
86  b.writeUint16(0); // chksum
87  b.writeIPv4Address(check_and_cast<const IGMPv2Report*>(pkt)->getGroupAddress());
88  break;
89 
90  case IGMPV2_LEAVE_GROUP:
91  b.writeByte(IGMPV2_LEAVE_GROUP); // type
92  b.writeByte(0); // code
93  b.writeUint16(0); // chksum
94  b.writeIPv4Address(check_and_cast<const IGMPv2Leave*>(pkt)->getGroupAddress());
95  break;
96 
98  const IGMPv3Report* v3pkt = check_and_cast<const IGMPv3Report*>(pkt);
99  b.writeByte(IGMPV3_MEMBERSHIP_REPORT); // type
100  b.writeByte(0); // code
101  b.writeUint16(0); // chksum
102  b.writeUint16(0); // reserved
103  unsigned int s = v3pkt->getGroupRecordArraySize();
104  b.writeUint16(s); // number of groups
105  for (unsigned int i = 0; i < s; i++) {
106  // serialize one group:
107  const GroupRecord& gr = v3pkt->getGroupRecord(i);
108  b.writeByte(gr.recordType);
109  b.writeByte(0); // aux data len: RFC 3376 Section 4.2.6
110  b.writeUint16(gr.sourceList.size());
111  b.writeIPv4Address(gr.groupAddress);
112  for (auto src: gr.sourceList) {
113  b.writeIPv4Address(src);
114  }
115  // write auxiliary data
116  }
117  break;
118  }
119 
120  default:
121  throw cRuntimeError("Can not serialize IGMP packet (%s): type %d not supported.", pkt->getClassName(), pkt->getType());
122  }
123  b.writeUint16To(2, TCPIPchecksum::checksum(igmp, b.getPos() - startPos));
124 }
Definition: IGMPMessage_m.h:62
Definition: IGMPMessage_m.h:61
static uint16_t checksum(const void *addr, unsigned int count)
Definition: TCPIPchecksum.h:44
Definition: IGMPMessage_m.h:60
Definition: IGMPMessage_m.h:58
Definition: IGMPMessage_m.h:59
value< double, units::s > s
Definition: Units.h:1049
value< double, units::m > b
Definition: Units.h:1054

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