Kea  1.9.9-git
isc::util::InputBuffer Class Reference

The InputBuffer class is a buffer abstraction for manipulating read-only data. More...

#include <buffer.h>

Public Member Functions

void readVector (std::vector< uint8_t > &data, size_t len)
 Read specified number of bytes as a vector. More...
 
Constructors and Destructor
 InputBuffer (const void *data, size_t len)
 Constructor from variable length of data. More...
 
Getter Methods
size_t getLength () const
 Return the length of the data stored in the buffer. More...
 
size_t getPosition () const
 Return the current read position. More...
 
Setter Methods
void setPosition (size_t position)
 Set the read position of the buffer to the given value. More...
 
Methods for reading data from the buffer.
uint8_t readUint8 ()
 Read an unsigned 8-bit integer from the buffer and return it. More...
 
uint16_t readUint16 ()
 Read an unsigned 16-bit integer in network byte order from the buffer, convert it to host byte order, and return it. More...
 
uint32_t readUint32 ()
 Read an unsigned 32-bit integer in network byte order from the buffer, convert it to host byte order, and return it. More...
 
void readData (void *data, size_t len)
 Read data of the specified length from the buffer and copy it to the caller supplied buffer. More...
 

Protected Attributes

const uint8_t * data_
 
size_t len_
 

Detailed Description

The InputBuffer class is a buffer abstraction for manipulating read-only data.

The main purpose of this class is to provide a safe placeholder for examining wire-format data received from a network.

Applications normally use this class only in a limited situation: as an interface between legacy I/O operation (such as receiving data from a BSD socket) and the rest of the Kea DNS library. One common usage of this class for an application would therefore be something like this:

unsigned char buf[1024];
struct sockaddr addr;
socklen_t addrlen = sizeof(addr);
int cc = recvfrom(s, buf, sizeof(buf), 0, &addr, &addrlen);
InputBuffer buffer(buf, cc);
// pass the buffer to a DNS message object to parse the message

Other Kea DNS classes will then use methods of this class to get access to the data, but the application normally doesn't have to care about the details.

An InputBuffer object internally holds a reference to the given data, rather than make a local copy of the data. Also, it does not have an ownership of the given data. It is application's responsibility to ensure the data remains valid throughout the lifetime of the InputBuffer object. Likewise, this object generally assumes the data isn't modified throughout its lifetime; if the application modifies the data while this object retains a reference to it, the result is undefined. The application will also be responsible for releasing the data when it's not needed if it was dynamically acquired.

This is a deliberate design choice: although it's safer to make a local copy of the given data on construction, it would cause unacceptable performance overhead, especially considering that a DNS message can be as large as a few KB. Alternatively, we could allow the object to allocate memory internally and expose it to the application to store network data in it. This is also a bad design, however, in that we would effectively break the abstraction employed in the class, and do so by publishing "read-only" stuff as a writable memory region. Since there doesn't seem to be a perfect solution, we have adopted what we thought a "least bad" one.

Methods for reading data from the buffer generally work like an input stream: it begins with the head of the data, and once some length of data is read from the buffer, the next read operation will take place from the head of the unread data. An object of this class internally holds (a notion of) where the next read operation should start. We call it the read position in this document.

Definition at line 81 of file buffer.h.

Constructor & Destructor Documentation

isc::util::InputBuffer::InputBuffer ( const void *  data,
size_t  len 
)
inline

Constructor from variable length of data.

It is caller's responsibility to ensure that the data is valid as long as the buffer exists.

Parameters
dataA pointer to the data stored in the buffer.
lenThe length of the data in bytes.

Definition at line 92 of file buffer.h.

Member Function Documentation

void isc::util::InputBuffer::readData ( void *  data,
size_t  len 
)
inline

Read data of the specified length from the buffer and copy it to the caller supplied buffer.

The data is copied as stored in the buffer; no conversion is performed. If the remaining length of the buffer is smaller than the specified length, an exception of class isc::dns::InvalidBufferPosition will be thrown.

Definition at line 186 of file buffer.h.

References data_, and len_.

Referenced by isc::dns::rdata::generic::Generic::Generic(), isc::util::io::SocketSessionReceiver::pop(), and readVector().

uint16_t isc::util::InputBuffer::readUint16 ( )
inline

Read an unsigned 16-bit integer in network byte order from the buffer, convert it to host byte order, and return it.

If the remaining length of the buffer is smaller than 16-bit, an exception of class isc::dns::InvalidBufferPosition will be thrown.

Definition at line 142 of file buffer.h.

References data_, and len_.

Referenced by isc::dhcp::decodeIpUdpHeader(), isc::dhcp_ddns::NameChangeRequest::fromFormat(), isc::dns::Message::parseHeader(), isc::dns::MessageImpl::parseQuestion(), isc::dns::MessageImpl::parseSection(), isc::util::io::SocketSessionReceiver::pop(), isc::dns::RRClass::RRClass(), and isc::dns::RRType::RRType().

uint32_t isc::util::InputBuffer::readUint32 ( )
inline

Read an unsigned 32-bit integer in network byte order from the buffer, convert it to host byte order, and return it.

If the remaining length of the buffer is smaller than 32-bit, an exception of class isc::dns::InvalidBufferPosition will be thrown.

Definition at line 162 of file buffer.h.

References data_, and len_.

Referenced by isc::dhcp::decodeIpUdpHeader(), isc::dns::MessageImpl::parseSection(), isc::util::io::SocketSessionReceiver::pop(), and isc::dns::RRTTL::RRTTL().

uint8_t isc::util::InputBuffer::readUint8 ( )
inline

Read an unsigned 8-bit integer from the buffer and return it.

If the remaining length of the buffer is smaller than 8-bit, an exception of class isc::dns::InvalidBufferPosition will be thrown.

Definition at line 130 of file buffer.h.

Referenced by isc::dhcp::decodeIpUdpHeader(), and isc::dns::Name::Name().

void isc::util::InputBuffer::readVector ( std::vector< uint8_t > &  data,
size_t  len 
)
inline

Read specified number of bytes as a vector.

If specified buffer is too short, it will be expanded using vector::resize() method.

Parameters
dataReference to a buffer (data will be stored there).
lenSize specified number of bytes to read in a vector.

Definition at line 204 of file buffer.h.

References len_, and readData().

Referenced by isc::dhcp::decodeEthernetHeader(), isc::dhcp_ddns::NameChangeRequest::fromFormat(), and isc::dhcp::PktFilterLPF::receive().

+ Here is the call graph for this function:

void isc::util::InputBuffer::setPosition ( size_t  position)
inline

Set the read position of the buffer to the given value.

The new position must be in the valid range of the buffer; otherwise an exception of class isc::dns::InvalidBufferPosition will be thrown.

Parameters
positionThe new position (offset from the beginning of the buffer).

Definition at line 115 of file buffer.h.

Referenced by isc::dhcp::decodeEthernetHeader(), isc::dhcp::decodeIpUdpHeader(), isc::dns::Message::fromWire(), and isc::dns::Name::Name().

Member Data Documentation

const uint8_t* isc::util::InputBuffer::data_
protected

Definition at line 229 of file buffer.h.

Referenced by readData(), readUint16(), and readUint32().

size_t isc::util::InputBuffer::len_
protected

Definition at line 230 of file buffer.h.

Referenced by readData(), readUint16(), readUint32(), and readVector().


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