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

Buffer for serializer/deserializer. More...

#include <Buffer.h>

Public Member Functions

 Buffer (const Buffer &base, unsigned int maxLength)
 
 Buffer (void *buf, unsigned int bufLen)
 
void seek (unsigned int newpos) const
 
unsigned int getPos () const
 
unsigned int getRemainingSize () const
 
unsigned int getRemainingSize (unsigned int reservedSize) const
 
bool hasError () const
 
void setError () const
 
unsigned char readByte () const
 
void readNBytes (unsigned int length, void *dest) const
 
uint16_t readUint16 () const
 
uint32_t readUint32 () const
 
uint64_t readUint64 () const
 
MACAddress readMACAddress () const
 
IPv4Address readIPv4Address () const
 
IPv6Address readIPv6Address () const
 
void writeByte (unsigned char data)
 
void writeByteTo (unsigned int position, unsigned char data)
 
void writeNBytes (unsigned int length, const void *src)
 
void writeNBytes (Buffer &inputBuffer, unsigned int length)
 
void fillNBytes (unsigned int length, unsigned char data)
 
void writeUint16 (uint16_t data)
 
void writeUint16To (unsigned int position, uint16_t data)
 
void writeUint32 (uint32_t data)
 
void writeUint64 (uint64_t data)
 
void writeMACAddress (const MACAddress &addr)
 
void writeIPv4Address (IPv4Address addr)
 
void writeIPv6Address (const IPv6Address &addr)
 
void * accessNBytes (unsigned int length)
 
const void * accessNBytes (unsigned int length) const
 
unsigned char * _getBuf () const
 
unsigned int _getBufSize () const
 

Protected Attributes

unsigned char * buf = nullptr
 
unsigned int bufsize = 0
 
unsigned int pos = 0
 
bool errorFound = false
 

Detailed Description

Buffer for serializer/deserializer.

Constructor & Destructor Documentation

inet::serializer::Buffer::Buffer ( const Buffer base,
unsigned int  maxLength 
)
26 {
27  buf = base.buf + base.pos;
28  bufsize = std::min(base.bufsize - base.pos, maxLength);
29 }
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SCTPAssociation.h:270
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
inet::serializer::Buffer::Buffer ( void *  buf,
unsigned int  bufLen 
)
inline
47 : buf(static_cast<unsigned char *>(buf)), bufsize(bufLen) {}
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41

Member Function Documentation

void * inet::serializer::Buffer::accessNBytes ( unsigned int  length)

Referenced by inet::serializer::IPv6Serializer::deserialize(), inet::serializer::ICMPSerializer::deserialize(), inet::serializer::UDPSerializer::deserialize(), inet::serializer::IGMPSerializer::deserialize(), inet::serializer::SCTPSerializer::deserialize(), inet::serializer::Ieee80211Serializer::deserialize(), inet::serializer::TCPSerializer::deserialize(), inet::serializer::EthernetSerializer::deserialize(), inet::serializer::IPv4Serializer::deserialize(), inet::serializer::DefaultSerializer::deserialize(), inet::serializer::ByteArraySerializer::deserialize(), inet::serializer::SerializerBase::lookupAndDeserialize(), inet::serializer::SerializerBase::lookupAndSerialize(), inet::serializer::IPv6Serializer::serialize(), inet::serializer::Ieee80211PhySerializer::serialize(), inet::serializer::IGMPSerializer::serialize(), inet::serializer::ICMPSerializer::serialize(), inet::serializer::SCTPSerializer::serialize(), inet::serializer::TCPSerializer::serialize(), inet::serializer::IPv4Serializer::serialize(), and inet::serializer::ByteArraySerializer::serialize().

193 {
194  if (pos + length <= bufsize) {
195  void *ret = buf + pos;
196  pos += length;
197  return ret;
198  }
199  pos = bufsize;
200  errorFound = true;
201  return nullptr;
202 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
const void* inet::serializer::Buffer::accessNBytes ( unsigned int  length) const
inline

Referenced by accessNBytes().

85 { return const_cast<Buffer *>(this)->accessNBytes(length); } // returns nullptr when haven't got enough space
void * accessNBytes(unsigned int length)
Definition: Buffer.cc:192
Buffer(const Buffer &base, unsigned int maxLength)
Definition: Buffer.cc:25
unsigned int inet::serializer::Buffer::getPos ( ) const
inline

Referenced by inet::serializer::IPv6Serializer::deserialize(), inet::serializer::IGMPSerializer::deserialize(), inet::serializer::UDPSerializer::deserialize(), inet::serializer::ICMPSerializer::deserialize(), inet::serializer::ICMPv6Serializer::deserialize(), inet::serializer::Ieee80211Serializer::deserialize(), inet::serializer::EthernetSerializer::deserialize(), inet::serializer::ARPSerializer::deserialize(), inet::serializer::EtherPhySerializer::deserialize(), inet::serializer::IPv4Serializer::deserialize(), inet::serializer::IPv4OptionDefaultSerializer::deserializeOption(), inet::serializer::IPv4Serializer::deserializeOptions(), inet::serializer::SerializerBase::deserializePacket(), inet::tcp::TCP_NSC::handleIpInputMessage(), inet::tcp::TCP_lwIP::handleIpInputMessage(), inet::serializer::SerializerBase::lookupAndDeserialize(), inet::serializer::SerializerBase::lookupAndSerialize(), inet::serializer::ARPSerializer::readIPv4Address(), inet::serializer::ARPSerializer::readMACAddress(), inet::sctp::SCTP::send_to_ip(), inet::serializer::Ieee80211PhySerializer::serialize(), inet::serializer::IPv6Serializer::serialize(), inet::serializer::IGMPSerializer::serialize(), inet::serializer::UDPSerializer::serialize(), inet::serializer::ICMPv6Serializer::serialize(), inet::serializer::ICMPSerializer::serialize(), inet::physicallayer::APSKPhyFrameSerializer::serialize(), inet::serializer::EthernetSerializer::serialize(), inet::serializer::Ieee80211Serializer::serialize(), inet::serializer::TCPSerializer::serialize(), inet::serializer::ARPSerializer::serialize(), inet::serializer::EtherPhySerializer::serialize(), inet::serializer::IPv4Serializer::serialize(), inet::serializer::SerializerBase::serializePacket(), inet::PcapDump::writeFrame(), and inet::PcapDump::writeIPv6Frame().

51 { return pos; }
unsigned int pos
Definition: Buffer.h:42
unsigned int inet::serializer::Buffer::getRemainingSize ( unsigned int  reservedSize) const
inline
53 { return bufsize - pos > reservedSize ? (bufsize - pos) - reservedSize : 0; }
unsigned int pos
Definition: Buffer.h:42
unsigned int bufsize
Definition: Buffer.h:41
IPv4Address inet::serializer::Buffer::readIPv4Address ( ) const
inline
IPv6Address inet::serializer::Buffer::readIPv6Address ( ) const

Referenced by inet::serializer::ICMPv6Serializer::deserialize().

220 {
221  uint32_t d[4];
222  for (auto & elem : d)
223  elem = readUint32();
224  return IPv6Address(d[0], d[1], d[2], d[3]);
225 }
uint32_t readUint32() const
Definition: Buffer.cc:61
MACAddress inet::serializer::Buffer::readMACAddress ( ) const

Referenced by inet::serializer::ICMPv6Serializer::deserialize(), inet::serializer::Ieee80211Serializer::deserialize(), inet::serializer::EthernetSerializer::deserialize(), inet::serializer::Ieee80211Serializer::parseDataOrMgmtFrame(), and inet::serializer::ARPSerializer::readMACAddress().

205 {
206  MACAddress addr;
207  for (int i = 0; i < MAC_ADDRESS_SIZE; i++)
208  addr.setAddressByte(i, readByte());
209  return addr;
210 }
unsigned char readByte() const
Definition: Buffer.cc:31
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
void inet::serializer::Buffer::readNBytes ( unsigned int  length,
void *  dest 
) const

Referenced by inet::serializer::Ieee80211Serializer::deserialize(), inet::serializer::TCPSerializer::deserialize(), and inet::serializer::EtherPhySerializer::deserialize().

41 {
42  unsigned char *dest = static_cast<unsigned char *>(_dest);
43  while (length--) {
44  if (pos >= bufsize) {
45  errorFound = true;
46  *dest++ = 0;
47  } else
48  *dest++ = buf[pos++];
49  }
50 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
uint32_t inet::serializer::Buffer::readUint32 ( ) const

Referenced by inet::serializer::ICMPSerializer::deserialize(), inet::serializer::ICMPv6Serializer::deserialize(), inet::serializer::Ieee80211Serializer::deserialize(), inet::serializer::EthernetSerializer::deserialize(), inet::serializer::IPv4OptionDefaultSerializer::deserializeOption(), inet::serializer::TCPSerializer::deserializeOption(), and readIPv6Address().

62 {
63  uint32_t ret = 0;
64  if (pos < bufsize) ret += ((uint32_t)(buf[pos++]) << 24);
65  if (pos < bufsize) ret += ((uint32_t)(buf[pos++]) << 16);
66  if (pos < bufsize) ret += ((uint32_t)(buf[pos++]) << 8);
67  if (pos < bufsize) ret += ((uint32_t)(buf[pos++]));
68  else errorFound = true;
69  return ret;
70 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
uint64_t inet::serializer::Buffer::readUint64 ( ) const

Referenced by inet::serializer::Ieee80211Serializer::deserialize().

73 {
74  uint64_t ret = 0;
75  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]) << 56);
76  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]) << 48);
77  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]) << 40);
78  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]) << 32);
79  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]) << 24);
80  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]) << 16);
81  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]) << 8);
82  if (pos < bufsize) ret += ((uint64_t)(buf[pos++]));
83  else errorFound = true;
84  return ret;
85 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
void inet::serializer::Buffer::writeByteTo ( unsigned int  position,
unsigned char  data 
)

Referenced by inet::serializer::IPv6Serializer::serialize(), and inet::serializer::IGMPSerializer::serialize().

97 {
98  if (position >= bufsize) {
99  errorFound = true;
100  return;
101  }
102  buf[position] = data;
103 }
bool errorFound
Definition: Buffer.h:43
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
void inet::serializer::Buffer::writeIPv4Address ( IPv4Address  addr)
inline
void inet::serializer::Buffer::writeIPv6Address ( const IPv6Address addr)
inline

Referenced by inet::serializer::IPv6Serializer::serialize(), and inet::serializer::ICMPv6Serializer::serialize().

81 { for (int i = 0; i < 4; i++) { writeUint32(addr.words()[i]); } }
void writeUint32(uint32_t data)
Definition: Buffer.cc:156
void inet::serializer::Buffer::writeMACAddress ( const MACAddress addr)

Referenced by inet::serializer::ICMPv6Serializer::serialize(), inet::serializer::EthernetSerializer::serialize(), inet::serializer::Ieee80211Serializer::serialize(), and inet::serializer::ARPSerializer::serialize().

213 {
214  unsigned char buff[MAC_ADDRESS_SIZE];
215  addr.getAddressBytes(buff);
217 }
void writeNBytes(unsigned int length, const void *src)
Definition: Buffer.cc:105
#define MAC_ADDRESS_SIZE
Definition: MACAddress.h:27
void inet::serializer::Buffer::writeNBytes ( unsigned int  length,
const void *  src 
)

Referenced by inet::serializer::Ieee80211Serializer::serialize(), inet::serializer::TCPSerializer::serialize(), inet::serializer::IPv4Serializer::serialize(), and writeMACAddress().

106 {
107  const unsigned char *src = static_cast<const unsigned char *>(_src);
108  while (pos < bufsize && length > 0) {
109  buf[pos++] = *src++;
110  length--;
111  }
112  if (length)
113  errorFound = true;
114 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
void inet::serializer::Buffer::writeNBytes ( Buffer inputBuffer,
unsigned int  length 
)
117 {
118  while (pos < bufsize && length > 0) {
119  buf[pos++] = inputBuffer.readByte();
120  length--;
121  }
122  if (length)
123  errorFound = true;
124 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
void inet::serializer::Buffer::writeUint16To ( unsigned int  position,
uint16_t  data 
)

Referenced by inet::serializer::ICMPSerializer::serialize(), inet::serializer::UDPSerializer::serialize(), inet::serializer::IGMPSerializer::serialize(), inet::serializer::ICMPv6Serializer::serialize(), inet::serializer::EthernetSerializer::serialize(), and inet::serializer::TCPSerializer::serialize().

147 {
148  if (position < bufsize)
149  buf[position++] = (uint8_t)(data >> 8);
150  if (position < bufsize)
151  buf[position++] = (uint8_t)data;
152  else
153  errorFound = true;
154 }
bool errorFound
Definition: Buffer.h:43
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
void inet::serializer::Buffer::writeUint32 ( uint32_t  data)

Referenced by inet::serializer::IPv6Serializer::serialize(), inet::serializer::ICMPSerializer::serialize(), inet::serializer::ICMPv6Serializer::serialize(), inet::serializer::EthernetSerializer::serialize(), inet::serializer::Ieee80211Serializer::serialize(), inet::serializer::IPv4OptionDefaultSerializer::serializeOption(), and inet::serializer::TCPSerializer::serializeOption().

157 {
158  if (pos < bufsize)
159  buf[pos++] = (uint8_t)(data >> 24);
160  if (pos < bufsize)
161  buf[pos++] = (uint8_t)(data >> 16);
162  if (pos < bufsize)
163  buf[pos++] = (uint8_t)(data >> 8);
164  if (pos < bufsize)
165  buf[pos++] = (uint8_t)data;
166  else
167  errorFound = true;
168 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41
void inet::serializer::Buffer::writeUint64 ( uint64_t  data)

Referenced by inet::serializer::Ieee80211Serializer::serialize().

171 {
172  if (pos < bufsize)
173  buf[pos++] = (uint8_t)(data >> 56);
174  if (pos < bufsize)
175  buf[pos++] = (uint8_t)(data >> 48);
176  if (pos < bufsize)
177  buf[pos++] = (uint8_t)(data >> 40);
178  if (pos < bufsize)
179  buf[pos++] = (uint8_t)(data >> 32);
180  if (pos < bufsize)
181  buf[pos++] = (uint8_t)(data >> 24);
182  if (pos < bufsize)
183  buf[pos++] = (uint8_t)(data >> 16);
184  if (pos < bufsize)
185  buf[pos++] = (uint8_t)(data >> 8);
186  if (pos < bufsize)
187  buf[pos++] = (uint8_t)data;
188  else
189  errorFound = true;
190 }
bool errorFound
Definition: Buffer.h:43
unsigned int pos
Definition: Buffer.h:42
unsigned char * buf
Definition: Buffer.h:40
unsigned int bufsize
Definition: Buffer.h:41

Member Data Documentation

unsigned int inet::serializer::Buffer::bufsize = 0
protected
unsigned int inet::serializer::Buffer::pos = 0
mutableprotected

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