Kea
1.9.9-git
|
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_ |
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:
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.
|
inline |
|
inline |
Return the length of the data stored in the buffer.
Definition at line 100 of file buffer.h.
Referenced by isc::dhcp::decodeEthernetHeader(), isc::dhcp::decodeIpUdpHeader(), isc::dns::Name::Name(), isc::dns::Message::parseHeader(), isc::dns::MessageImpl::parseQuestion(), isc::dns::MessageImpl::parseSection(), isc::dhcp::PktFilterLPF::receive(), isc::dns::RRClass::RRClass(), isc::dns::RRTTL::RRTTL(), and isc::dns::RRType::RRType().
|
inline |
Return the current read position.
Definition at line 102 of file buffer.h.
Referenced by isc::dns::MessageImpl::addTSIG(), isc::dns::rdata::createRdata(), isc::dhcp::decodeEthernetHeader(), isc::dhcp::decodeIpUdpHeader(), isc::dns::Name::Name(), isc::dns::Message::parseHeader(), isc::dns::MessageImpl::parseQuestion(), isc::dns::MessageImpl::parseSection(), isc::dhcp::PktFilterLPF::receive(), isc::dns::RRClass::RRClass(), isc::dns::RRTTL::RRTTL(), and isc::dns::RRType::RRType().
|
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.
Referenced by isc::dns::rdata::generic::Generic::Generic(), isc::util::io::SocketSessionReceiver::pop(), and readVector().
|
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.
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().
|
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.
Referenced by isc::dhcp::decodeIpUdpHeader(), isc::dns::MessageImpl::parseSection(), isc::util::io::SocketSessionReceiver::pop(), and isc::dns::RRTTL::RRTTL().
|
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().
|
inline |
Read specified number of bytes as a vector.
If specified buffer is too short, it will be expanded using vector::resize() method.
data | Reference to a buffer (data will be stored there). |
len | Size 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().
|
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.
position | The 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().
|
protected |
Definition at line 229 of file buffer.h.
Referenced by readData(), readUint16(), and readUint32().
|
protected |
Definition at line 230 of file buffer.h.
Referenced by readData(), readUint16(), readUint32(), and readVector().