2006-11-01 13:11:30 +01:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
2006-08-29 17:42:13 +02:00
|
|
|
/*
|
2007-09-07 08:34:46 +02:00
|
|
|
* Copyright (c) 2005,2006,2007 INRIA
|
2006-08-29 17:42:13 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
|
|
|
|
*/
|
|
|
|
|
#ifndef BUFFER_H
|
|
|
|
|
#define BUFFER_H
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2006-08-29 17:55:34 +02:00
|
|
|
namespace ns3 {
|
2006-08-29 17:42:13 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \brief automatically resized byte buffer
|
|
|
|
|
*
|
|
|
|
|
* This represents a buffer of bytes. Its size is
|
|
|
|
|
* automatically adjusted to hold any data prepended
|
|
|
|
|
* or appended by the user. Its implementation is optimized
|
|
|
|
|
* to ensure that the number of buffer resizes is minimized,
|
|
|
|
|
* by creating new Buffers of the maximum size ever used.
|
|
|
|
|
* The correct maximum size is learned at runtime during use by
|
|
|
|
|
* recording the maximum size of each packet.
|
|
|
|
|
*/
|
|
|
|
|
class Buffer {
|
|
|
|
|
public:
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \brief iterator in a Buffer instance
|
|
|
|
|
*/
|
|
|
|
|
class Iterator {
|
|
|
|
|
public:
|
2007-09-07 08:34:46 +02:00
|
|
|
Iterator ();
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* go forward by one byte
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void Next (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* go backward by one byte
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void Prev (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param delta number of bytes to go forward
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void Next (uint32_t delta);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param delta number of bytes to go backward
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void Prev (uint32_t delta);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param o the second iterator
|
|
|
|
|
* \return number of bytes included between the two iterators
|
|
|
|
|
*
|
|
|
|
|
* This method works only if the two iterators point
|
|
|
|
|
* to the same underlying buffer. Debug builds ensure
|
|
|
|
|
* this with an assert.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint32_t GetDistanceFrom (Iterator const &o) const;
|
2006-11-01 13:11:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \return true if this iterator points to the end of the byte array.
|
|
|
|
|
* false otherwise.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
bool IsEnd (void) const;
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return true if this iterator points to the start of the byte array.
|
|
|
|
|
* false otherwise.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
bool IsStart (void) const;
|
2006-11-01 13:11:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by one byte.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteU8 (uint8_t data);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
* \param len number of times data must be written in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer len times and avance the iterator position
|
|
|
|
|
* by len byte.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteU8 (uint8_t data, uint32_t len);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by two bytes. The format of the data written in the byte
|
|
|
|
|
* buffer is non-portable. We only ensure that readU16 will
|
|
|
|
|
* return exactly what we wrote with writeU16 if the program
|
|
|
|
|
* is run on the same machine.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteU16 (uint16_t data);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by four bytes. The format of the data written in the byte
|
|
|
|
|
* buffer is non-portable. We only ensure that readU32 will
|
|
|
|
|
* return exactly what we wrote with writeU32 if the program
|
|
|
|
|
* is run on the same machine.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteU32 (uint32_t data);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by eight bytes. The format of the data written in the byte
|
|
|
|
|
* buffer is non-portable. We only ensure that readU64 will
|
|
|
|
|
* return exactly what we wrote with writeU64 if the program
|
|
|
|
|
* is run on the same machine.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteU64 (uint64_t data);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by two bytes. The data is written in network order and the
|
|
|
|
|
* input data is expected to be in host order.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteHtonU16 (uint16_t data);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by four bytes. The data is written in network order and the
|
|
|
|
|
* input data is expected to be in host order.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteHtonU32 (uint32_t data);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param data data to write in buffer
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by eight bytes. The data is written in network order and the
|
|
|
|
|
* input data is expected to be in host order.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void WriteHtonU64 (uint64_t data);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param buffer a byte buffer to copy in the internal buffer.
|
|
|
|
|
* \param size number of bytes to copy.
|
|
|
|
|
*
|
|
|
|
|
* Write the data in buffer and avance the iterator position
|
|
|
|
|
* by size bytes.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void Write (uint8_t const*buffer, uint32_t size);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param start the start of the data to copy
|
|
|
|
|
* \param end the end of the data to copy
|
|
|
|
|
*
|
|
|
|
|
* Write the data delimited by start and end in internal buffer
|
|
|
|
|
* and avance the iterator position by the number of bytes
|
|
|
|
|
* copied.
|
|
|
|
|
* The input interators _must_ not point to the same Buffer as
|
|
|
|
|
* we do to avoid overlapping copies. This is enforced
|
|
|
|
|
* in debug builds by asserts.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
void Write (Iterator start, Iterator end);
|
2006-11-01 13:11:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \return the byte read in the buffer.
|
|
|
|
|
*
|
|
|
|
|
* Read data and advance the Iterator by the number of bytes
|
|
|
|
|
* read.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint8_t ReadU8 (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return the two bytes read in the buffer.
|
|
|
|
|
*
|
|
|
|
|
* Read data and advance the Iterator by the number of bytes
|
|
|
|
|
* read.
|
|
|
|
|
* The data is read in the format written by writeU16.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint16_t ReadU16 (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return the four bytes read in the buffer.
|
|
|
|
|
*
|
|
|
|
|
* Read data and advance the Iterator by the number of bytes
|
|
|
|
|
* read.
|
|
|
|
|
* The data is read in the format written by writeU32.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint32_t ReadU32 (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return the eight bytes read in the buffer.
|
|
|
|
|
*
|
|
|
|
|
* Read data and advance the Iterator by the number of bytes
|
|
|
|
|
* read.
|
|
|
|
|
* The data is read in the format written by writeU64.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint64_t ReadU64 (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return the two bytes read in the buffer.
|
|
|
|
|
*
|
|
|
|
|
* Read data and advance the Iterator by the number of bytes
|
|
|
|
|
* read.
|
|
|
|
|
* The data is read in network format and return in host format.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint16_t ReadNtohU16 (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return the four bytes read in the buffer.
|
|
|
|
|
*
|
|
|
|
|
* Read data and advance the Iterator by the number of bytes
|
|
|
|
|
* read.
|
|
|
|
|
* The data is read in network format and return in host format.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint32_t ReadNtohU32 (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return the eight bytes read in the buffer.
|
|
|
|
|
*
|
|
|
|
|
* Read data and advance the Iterator by the number of bytes
|
|
|
|
|
* read.
|
|
|
|
|
* The data is read in network format and return in host format.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint64_t ReadNtohU64 (void);
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \param buffer buffer to copy data into
|
|
|
|
|
* \param size number of bytes to copy
|
|
|
|
|
*
|
|
|
|
|
* Copy size bytes of data from the internal buffer to the
|
|
|
|
|
* input buffer and avance the Iterator by the number of
|
|
|
|
|
* bytes read.
|
|
|
|
|
*/
|
2007-09-07 19:39:47 +02:00
|
|
|
void Read (uint8_t *buffer, uint32_t size);
|
2006-11-01 13:11:30 +01:00
|
|
|
private:
|
|
|
|
|
friend class Buffer;
|
2007-09-07 18:45:00 +02:00
|
|
|
Iterator (Buffer const*buffer);
|
|
|
|
|
Iterator (Buffer const*buffer, bool);
|
|
|
|
|
void Construct (const Buffer *buffer);
|
2006-11-01 13:11:30 +01:00
|
|
|
uint32_t m_zeroStart;
|
|
|
|
|
uint32_t m_zeroEnd;
|
2007-09-07 18:45:00 +02:00
|
|
|
uint32_t m_dataStart;
|
2006-11-01 13:11:30 +01:00
|
|
|
uint32_t m_dataEnd;
|
|
|
|
|
uint32_t m_current;
|
|
|
|
|
uint8_t *m_data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \return the number of bytes stored in this buffer.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
uint32_t GetSize (void) const;
|
2006-11-01 13:11:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \return a pointer to the start of the internal
|
|
|
|
|
* byte buffer.
|
|
|
|
|
*
|
|
|
|
|
* The returned pointer points to an area of
|
|
|
|
|
* memory which is ns3::Buffer::GetSize () bytes big.
|
|
|
|
|
* Please, try to never ever use this method. It is really
|
|
|
|
|
* evil and is present only for a few specific uses.
|
|
|
|
|
*/
|
|
|
|
|
uint8_t const*PeekData (void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \param start size to reserve
|
|
|
|
|
*
|
|
|
|
|
* Add bytes at the start of the Buffer. The
|
|
|
|
|
* content of these bytes is undefined but debugging
|
|
|
|
|
* builds initialize them to 0x33.
|
|
|
|
|
* Any call to this method invalidates any Iterator
|
|
|
|
|
* pointing to this Buffer.
|
|
|
|
|
*/
|
|
|
|
|
void AddAtStart (uint32_t start);
|
|
|
|
|
/**
|
|
|
|
|
* \param end size to reserve
|
|
|
|
|
*
|
|
|
|
|
* Add bytes at the end of the Buffer. The
|
|
|
|
|
* content of these bytes is undefined but debugging
|
|
|
|
|
* builds initialize them to 0x33.
|
|
|
|
|
* Any call to this method invalidates any Iterator
|
|
|
|
|
* pointing to this Buffer.
|
|
|
|
|
*/
|
|
|
|
|
void AddAtEnd (uint32_t end);
|
|
|
|
|
/**
|
|
|
|
|
* \param start size to remove
|
|
|
|
|
*
|
|
|
|
|
* Remove bytes at the start of the Buffer.
|
|
|
|
|
* Any call to this method invalidates any Iterator
|
|
|
|
|
* pointing to this Buffer.
|
|
|
|
|
*/
|
|
|
|
|
void RemoveAtStart (uint32_t start);
|
|
|
|
|
/**
|
|
|
|
|
* \param end size to remove
|
|
|
|
|
*
|
|
|
|
|
* Remove bytes at the end of the Buffer.
|
|
|
|
|
* Any call to this method invalidates any Iterator
|
|
|
|
|
* pointing to this Buffer.
|
|
|
|
|
*/
|
|
|
|
|
void RemoveAtEnd (uint32_t end);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \param start offset from start of packet
|
|
|
|
|
* \param length
|
|
|
|
|
*
|
|
|
|
|
* \return a fragment of size length starting at offset
|
|
|
|
|
* start.
|
|
|
|
|
*/
|
|
|
|
|
Buffer CreateFragment (uint32_t start, uint32_t length) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \return an Iterator which points to the
|
|
|
|
|
* start of this Buffer.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
Buffer::Iterator Begin (void) const;
|
2006-11-01 13:11:30 +01:00
|
|
|
/**
|
|
|
|
|
* \return an Iterator which points to the
|
|
|
|
|
* end of this Buffer.
|
|
|
|
|
*/
|
2007-09-07 08:34:46 +02:00
|
|
|
Buffer::Iterator End (void) const;
|
2006-11-01 13:11:30 +01:00
|
|
|
|
2007-08-04 09:31:52 +02:00
|
|
|
Buffer CreateFullCopy (void) const;
|
2007-06-01 15:23:07 +02:00
|
|
|
|
2007-09-07 08:34:46 +02:00
|
|
|
Buffer (Buffer const &o);
|
|
|
|
|
Buffer &operator = (Buffer const &o);
|
|
|
|
|
Buffer ();
|
|
|
|
|
Buffer (uint32_t dataSize);
|
|
|
|
|
~Buffer ();
|
2006-08-29 17:42:13 +02:00
|
|
|
private:
|
2006-11-01 13:11:30 +01:00
|
|
|
|
2007-08-04 09:31:52 +02:00
|
|
|
void TransformIntoRealBuffer (void) const;
|
2007-09-10 16:45:52 +02:00
|
|
|
bool CheckInternalState (void) const;
|
|
|
|
|
void Initialize (uint32_t zeroSize);
|
|
|
|
|
uint32_t GetInternalSize (void) const;
|
|
|
|
|
uint32_t GetInternalEnd (void) const;
|
|
|
|
|
static void Recycle (struct BufferData *data);
|
|
|
|
|
static struct BufferData *Create (uint32_t size);
|
2006-11-01 13:11:30 +01:00
|
|
|
|
|
|
|
|
struct BufferData *m_data;
|
2007-09-10 16:45:52 +02:00
|
|
|
uint32_t m_maxZeroAreaStart;
|
2007-09-10 08:54:11 +02:00
|
|
|
uint32_t m_zeroAreaStart;
|
2007-09-10 16:45:52 +02:00
|
|
|
uint32_t m_zeroAreaEnd;
|
2006-11-01 13:11:30 +01:00
|
|
|
uint32_t m_start;
|
2007-09-09 10:38:22 +02:00
|
|
|
uint32_t m_end;
|
2006-08-29 17:42:13 +02:00
|
|
|
};
|
|
|
|
|
|
2007-09-07 08:34:46 +02:00
|
|
|
} // namespace ns3
|
2006-08-29 17:42:13 +02:00
|
|
|
|
|
|
|
|
#endif /* BUFFER_H */
|