netbuffer.h
Go to the documentation of this file.
#ifndef WIBBLE_SYS_NETBUFFER_H
#define WIBBLE_SYS_NETBUFFER_H
/*
* Variable-size, reference-counted memory buffer used to access network
* packets
*
* Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <wibble/sys/buffer.h>
#include <wibble/exception.h>
namespace wibble {
namespace sys {
class NetBuffer : public Buffer
{
public:
size_t cursor;
public:
NetBuffer() throw () : Buffer(), cursor(0) {}
NetBuffer(size_t size) : Buffer(size), cursor(0) {}
NetBuffer(void* buf, size_t size, bool own = true)
: Buffer(buf, size, own), cursor(0) {}
NetBuffer(const void* buf, size_t size)
: Buffer(buf, size), cursor(0) {}
NetBuffer(const Buffer& buf) throw () : Buffer(buf), cursor(0) {}
NetBuffer(const NetBuffer& buf) throw ()
: Buffer(buf), cursor(buf.cursor) {}
NetBuffer& operator=(const Buffer& buf)
{
Buffer::operator=(buf);
cursor = 0;
return *this;
}
NetBuffer& operator=(const NetBuffer& buf)
{
Buffer::operator=(buf);
cursor = buf.cursor;
return *this;
}
const void* data(size_t ofs = 0) const throw () { return static_cast<const char*>(Buffer::data()) + cursor + ofs; }
void* data(size_t ofs = 0) throw () { return static_cast<char*>(Buffer::data()) + cursor + ofs; }
size_t size() const throw () { return Buffer::size() - cursor; }
template<class T>
bool fits(size_t ofs = 0) const throw ()
{
return cursor + ofs + sizeof(T) < size();
}
template<class T>
const T* cast(size_t ofs = 0) const throw (wibble::exception::Consistency)
{
if (cursor + ofs + sizeof(T) >= size())
throw wibble::exception::Consistency("reading from buffer", "tried to read past the end of the buffer");
return static_cast<const T*>(data(ofs));
}
NetBuffer operator+(size_t ofs) throw (wibble::exception::Consistency)
{
return after(ofs);
}
const NetBuffer after(size_t ofs) const throw (wibble::exception::Consistency)
{
NetBuffer res(*this);
res.skip(ofs);
return res;
}
template<class T>
const NetBuffer after() const throw (wibble::exception::Consistency)
{
NetBuffer res(*this);
res.skip(sizeof(T));
return res;
}
NetBuffer& operator+=(size_t ofs) throw (wibble::exception::Consistency)
{
skip(ofs);
return *this;
}
template<class T>
void skip() throw (wibble::exception::Consistency)
{
skip(sizeof(T));
}
void skip(size_t t) throw (wibble::exception::Consistency)
{
if (cursor + t >= size())
throw wibble::exception::Consistency("reading from buffer", "tried to skip past the end of the buffer");
cursor += t;
}
};
}
}
// vim:set ts=4 sw=4:
#endif
wibble::sys::Buffer
Variable-size, reference-counted memory buffer.
Definition: buffer.h:33
wibble::sys::NetBuffer::after
const NetBuffer after(size_t ofs) const
Return another NetBuffer starting ofs bytes from the beginning of this one.
Definition: netbuffer.h:111
wibble::sys::NetBuffer::data
void * data(size_t ofs=0)
Return a pointer to the buffer.
Definition: netbuffer.h:74
wibble::sys::NetBuffer::cast
const T * cast(size_t ofs=0) const
Access the buffer contents as a structure T at the given offset.
Definition: netbuffer.h:93
wibble::sys::NetBuffer::skip
void skip()
Move the starting point of this buffer sizeof(T) bytes from the beginning.
Definition: netbuffer.h:144
wibble::sys::NetBuffer::NetBuffer
NetBuffer()
Definition: netbuffer.h:45
wibble::sys::NetBuffer::NetBuffer
NetBuffer(size_t size)
Definition: netbuffer.h:46
wibble::exception::Consistency
Exception thrown when some consistency check fails.
Definition: exception.h:254
wibble::sys::NetBuffer::fits
bool fits(size_t ofs=0) const
Check if the buffer is long enough to contain a structure T at the given offset.
Definition: netbuffer.h:84
exception.h
wibble::sys::NetBuffer::NetBuffer
NetBuffer(void *buf, size_t size, bool own=true)
Definition: netbuffer.h:47
wibble::sys::NetBuffer::operator=
NetBuffer & operator=(const NetBuffer &buf)
Definition: netbuffer.h:63
wibble::sys::NetBuffer::size
size_t size() const
Return the buffer size.
Definition: netbuffer.h:77
wibble::sys::NetBuffer::NetBuffer
NetBuffer(const NetBuffer &buf)
Definition: netbuffer.h:53
wibble::sys::NetBuffer::operator=
NetBuffer & operator=(const Buffer &buf)
Definition: netbuffer.h:56
wibble::sys::NetBuffer::data
const void * data(size_t ofs=0) const
Return a pointer to the buffer.
Definition: netbuffer.h:71
wibble::sys::NetBuffer::after
const NetBuffer after() const
Return another NetBuffer starting just after sizeof(T) from the beginning of this one...
Definition: netbuffer.h:123
wibble::sys::Buffer::size
size_t size() const
Return the buffer size.
Definition: buffer.h:149
wibble::sys::NetBuffer::cursor
size_t cursor
Offset in bytes of the NetBuffer start, from the beginning of the memory area we manage.
Definition: netbuffer.h:42
wibble::sys::NetBuffer::operator+
NetBuffer operator+(size_t ofs)
Return another NetBuffer starting ofs bytes from the beginning of this one.
Definition: netbuffer.h:103
wibble::sys::NetBuffer::NetBuffer
NetBuffer(const Buffer &buf)
Definition: netbuffer.h:52
wibble::sys::NetBuffer::skip
void skip(size_t t)
Move the starting point of this buffer ofs bytes from the beginning.
Definition: netbuffer.h:152
wibble::sys::Buffer::data
void * data()
Return a pointer to the buffer.
Definition: buffer.h:143
wibble::sys::NetBuffer::operator+=
NetBuffer & operator+=(size_t ofs)
Move the starting point of this buffer ofs bytes from the beginning.
Definition: netbuffer.h:133
buffer.h
wibble
Definition: amorph.h:17
wibble::sys::NetBuffer
Buffer whose starting can be moved back and forth, useful to decapsulate stacked network packets...
Definition: netbuffer.h:35
wibble::sys::Buffer::operator=
Buffer & operator=(const Buffer &buf)
Definition: buffer.h:132
wibble::sys::NetBuffer::NetBuffer
NetBuffer(const void *buf, size_t size)
Definition: netbuffer.h:49
wibble::sys::Buffer::Buffer
Buffer()
Create a 0-lenght buffer.
Definition: buffer.h:74