The InputBuffer
class is a buffer abstraction for manipulating read-only data.
More...
Public Member Functions | |
Constructors and Destructor | |
InputBuffer (const void *data, size_t len) | |
Constructor from variable length of data. | |
Getter Methods | |
size_t | getLength () const |
Return the length of the data stored in the buffer. | |
size_t | getPosition () const |
Return the current read position. | |
Setter Methods | |
void | setPosition (size_t position) |
Set the read position of the buffer to the given value. | |
Methods for reading data from the buffer. | |
uint8_t | readUint8 () |
Read an unsigned 8-bit integer from the buffer and return it. | |
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. | |
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. | |
void | readData (void *data, size_t len) |
Read data of the specified length from the buffer and copy it to the caller supplied buffer. |
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 BIND10 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 BIND10 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.
isc::dns::InputBuffer::InputBuffer | ( | const void * | data, | |
size_t | len | |||
) |
Constructor from variable length of data.
It is caller's responsibility to ensure that the data is valid as long as the buffer exists.
data | A pointer to the data stored in the buffer. | |
len | The length of the data in bytes. |
size_t isc::dns::InputBuffer::getLength | ( | ) | const |
Return the length of the data stored in the buffer.
Referenced by isc::dns::Name::Name().
size_t isc::dns::InputBuffer::getPosition | ( | ) | const |
Return the current read position.
Referenced by isc::dns::Name::Name().
void isc::dns::InputBuffer::readData | ( | void * | data, | |
size_t | len | |||
) |
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.
References dns_throw.
uint16_t isc::dns::InputBuffer::readUint16 | ( | ) |
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.
References dns_throw.
uint32_t isc::dns::InputBuffer::readUint32 | ( | ) |
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.
References dns_throw.
uint8_t isc::dns::InputBuffer::readUint8 | ( | ) |
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.
References dns_throw.
Referenced by isc::dns::Name::Name().
void isc::dns::InputBuffer::setPosition | ( | size_t | position | ) |
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). |
References dns_throw.
Referenced by isc::dns::Name::Name().