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

#include <IPv4Serializer.h>

Inheritance diagram for inet::serializer::IPv4OptionDefaultSerializer:
inet::serializer::IPv4OptionSerializerBase

Public Member Functions

 IPv4OptionDefaultSerializer (const char *name=nullptr)
 
void serializeOption (const TLVOptionBase *option, Buffer &b, Context &c) override
 
TLVOptionBasedeserializeOption (Buffer &b, Context &c) override
 
- Public Member Functions inherited from inet::serializer::IPv4OptionSerializerBase
 IPv4OptionSerializerBase (const char *name=nullptr)
 

Constructor & Destructor Documentation

inet::serializer::IPv4OptionDefaultSerializer::IPv4OptionDefaultSerializer ( const char *  name = nullptr)
inline
39 : IPv4OptionSerializerBase(name) {}
IPv4OptionSerializerBase(const char *name=nullptr)
Definition: IPv4Serializer.h:31

Member Function Documentation

TLVOptionBase * inet::serializer::IPv4OptionDefaultSerializer::deserializeOption ( Buffer b,
Context c 
)
overridevirtual

Implements inet::serializer::IPv4OptionSerializerBase.

209 {
210  unsigned int pos = b.getPos();
211  unsigned char type = b.readByte();
212  unsigned char length = 1;
213 
214  switch (type) {
215  case IPOPTION_END_OF_OPTIONS: // EOL
216  return new IPv4OptionEnd();
217 
218  case IPOPTION_NO_OPTION: // NOP
219  return new IPv4OptionNop();
220 
221  case IPOPTION_STREAM_ID:
222  length = b.readByte();
223  if (length == 4) {
224  auto *option = new IPv4OptionStreamId();
225  option->setType(type);
226  option->setLength(length);
227  option->setStreamId(b.readUint16());
228  return option;
229  }
230  break;
231 
232  case IPOPTION_TIMESTAMP: {
233  length = b.readByte();
234  uint8_t pointer = b.readByte();
235  uint8_t flagbyte = b.readByte();
236  uint8_t overflow = flagbyte >> 4;
237  int flag = -1;
238  int bytes = 0;
239  switch (flagbyte & 0x0f) {
240  case 0: flag = IP_TIMESTAMP_TIMESTAMP_ONLY; bytes = 4; break;
241  case 1: flag = IP_TIMESTAMP_WITH_ADDRESS; bytes = 8; break;
242  case 3: flag = IP_TIMESTAMP_SENDER_INIT_ADDRESS; bytes = 8; break;
243  default: break;
244  }
245  if (flag != -1 && length > 4 && bytes && ((length-4) % bytes) == 0 && pointer >= 5 && ((pointer-5) % bytes) == 0) {
246  auto *option = new IPv4OptionTimestamp();
247  option->setType(type);
248  option->setLength(length);
249  option->setFlag(flag);
250  option->setOverflow(overflow);
251  option->setRecordTimestampArraySize((length - 4) / bytes);
252  if (bytes == 8)
253  option->setRecordAddressArraySize((length - 4) / bytes);
254  option->setNextIdx((pointer-5) / bytes);
255  for (unsigned int count = 0; count < option->getRecordAddressArraySize(); count++) {
256  if (bytes == 8)
257  option->setRecordAddress(count, b.readIPv4Address());
258  option->setRecordTimestamp(count, SimTime(b.readUint32(), SIMTIME_MS));
259  }
260  return option;
261  }
262  break;
263  }
264 
268  length = b.readByte();
269  uint8_t pointer = b.readByte();
270  if (length > 3 && (length % 4) == 3 && pointer >= 4 && (pointer % 4) == 0) {
271  auto *option = new IPv4OptionRecordRoute();
272  option->setType(type);
273  option->setLength(length);
274  option->setRecordAddressArraySize((length - 3) / 4);
275  option->setNextAddressIdx((pointer-4) / 4);
276  for (unsigned int count = 0; count < option->getRecordAddressArraySize(); count++) {
277  option->setRecordAddress(count, b.readIPv4Address());
278  }
279  return option;
280  }
281  break;
282  }
283 
285  case IPOPTION_SECURITY:
286  default:
287  break;
288  } // switch
289 
290  auto *option = new TLVOptionRaw();
291  b.seek(pos);
292  type = b.readByte();
293  length = b.readByte();
294  option->setType(type);
295  option->setLength(length);
296  if (length > 2)
297  option->setBytesArraySize(length - 2);
298  for (unsigned int i = 2; i < length; i++)
299  option->setBytes(i-2, b.readByte());
300  return option;
301 }
Definition: IPv4Datagram_m.h:112
Definition: IPv4Datagram_m.h:107
int count(const std::vector< T > &v, const T &a)
Definition: stlutils.h:58
Definition: IPv4Datagram_m.h:111
Definition: IPv4Datagram_m.h:105
Definition: IPv4Datagram_m.h:109
Definition: IPv4Datagram_m.h:106
Definition: IPv4Datagram_m.h:133
Definition: IPv4Datagram_m.h:108
Definition: IPv4Datagram_m.h:113
Definition: IPv4Datagram_m.h:135
Definition: IPv4Datagram_m.h:110
Definition: IPv4Datagram_m.h:134
value< double, units::m > b
Definition: Units.h:1054
void inet::serializer::IPv4OptionDefaultSerializer::serializeOption ( const TLVOptionBase option,
Buffer b,
Context c 
)
overridevirtual

Implements inet::serializer::IPv4OptionSerializerBase.

Referenced by inet::serializer::IPv4Serializer::serializeOptions().

64 {
65  unsigned short type = option->getType();
66  unsigned short length = option->getLength(); // length >= 1
67 
68  b.writeByte(type);
69  if (length > 1)
70  b.writeByte(length);
71 
72  auto *opt = dynamic_cast<const TLVOptionRaw *>(option);
73  if (opt) {
74  unsigned int datalen = opt->getBytesArraySize();
75  ASSERT(length == 2 + datalen);
76  for (unsigned int i = 0; i < datalen; i++)
77  b.writeByte(opt->getBytes(i));
78  return;
79  }
80 
81  switch (type) {
83  check_and_cast<const IPv4OptionEnd *>(option);
84  ASSERT(length == 1);
85  break;
86 
87  case IPOPTION_NO_OPTION:
88  check_and_cast<const IPv4OptionNop *>(option);
89  ASSERT(length == 1);
90  break;
91 
92  case IPOPTION_STREAM_ID: {
93  auto *opt = check_and_cast<const IPv4OptionStreamId *>(option);
94  ASSERT(length == 4);
95  b.writeUint16(opt->getStreamId());
96  break;
97  }
98 
99  case IPOPTION_TIMESTAMP: {
100  auto *opt = check_and_cast<const IPv4OptionTimestamp *>(option);
101  int bytes = (opt->getFlag() == IP_TIMESTAMP_TIMESTAMP_ONLY) ? 4 : 8;
102  ASSERT(length == 4 + bytes * opt->getRecordTimestampArraySize());
103  uint8_t pointer = 5 + opt->getNextIdx() * bytes;
104  b.writeByte(pointer);
105  uint8_t flagbyte = opt->getOverflow() << 4 | opt->getFlag();
106  b.writeByte(flagbyte);
107  for (unsigned int count = 0; count < opt->getRecordTimestampArraySize(); count++) {
108  if (bytes == 8)
109  b.writeIPv4Address(opt->getRecordAddress(count));
110  b.writeUint32(opt->getRecordTimestamp(count).inUnit(SIMTIME_MS));
111  }
112  break;
113  }
114 
118  auto *opt = check_and_cast<const IPv4OptionRecordRoute *>(option);
119  ASSERT(length == 3 + 4 * opt->getRecordAddressArraySize());
120  uint8_t pointer = 4 + opt->getNextAddressIdx() * 4;
121  b.writeByte(pointer);
122  for (unsigned int count = 0; count < opt->getRecordAddressArraySize(); count++) {
123  b.writeIPv4Address(opt->getRecordAddress(count));
124  }
125  break;
126  }
128  case IPOPTION_SECURITY:
129  default: {
130  throw cRuntimeError("Unknown IPv4Option type=%d (not in an TLVOptionRaw option)", type);
131  break;
132  }
133  }
134 }
Definition: IPv4Datagram_m.h:112
Definition: IPv4Datagram_m.h:107
int count(const std::vector< T > &v, const T &a)
Definition: stlutils.h:58
Definition: IPv4Datagram_m.h:111
Definition: IPv4Datagram_m.h:105
Definition: IPv4Datagram_m.h:109
Definition: IPv4Datagram_m.h:106
Definition: IPv4Datagram_m.h:133
Definition: IPv4Datagram_m.h:108
Definition: IPv4Datagram_m.h:113
Definition: IPv4Datagram_m.h:110
value< double, units::m > b
Definition: Units.h:1054

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