1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#ifndef _mrd_packet_buffer_h_
#define _mrd_packet_buffer_h_
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
struct ip6_hdr;
class interface;
/*!
* \class std_packet_buffer mrd/packet_buffer.h
* \brief Implements a packet buffer with pre-reserve head-space for
* fast encapsulation.
*/
template <int BS, int EBS>
class std_packet_buffer {
public:
std_packet_buffer()
: source(0), rlength(-1), read_offset(0), send_offset(0) {
memset(pb_buf, 0, sizeof(pb_buf));
}
template <class T> T *header(int offset = 0) { return (T *)(pb_buf + EBS + offset); }
void *pheader(int offset = 0) { return pb_buf + EBS + offset; }
ip6_hdr *ip6_header() { return (ip6_hdr *)(pb_buf + EBS + read_offset); }
int recvfrom(int sock, sockaddr *sa, socklen_t *salen) {
rlength = ::recvfrom(sock, pb_buf + EBS, BS - EBS, 0, sa, salen);
read_offset = 0;
return rlength;
}
int sendto(int sock, const sockaddr *sa, socklen_t salen) {
int res = ::sendto(sock, pb_buf + EBS + send_offset, rlength, 0, sa, salen);
send_offset = 0;
return res;
}
void full_resize(int length) {
rlength = length;
}
void set_send_offset(int length) {
send_offset -= length;
rlength += length;
}
uint8_t *buffer() { return pb_buf + EBS; }
const uint8_t *buffer() const { return pb_buf + EBS; }
uint32_t bufferlen() const { return BS - EBS; }
interface *source;
int rlength;
int read_offset, send_offset;
int auxhdr_off, auxhdr_len;
private:
uint8_t pb_buf[BS];
};
typedef std_packet_buffer<4096, 256> packet_buffer;
class encoding_buffer {
public:
encoding_buffer(int);
~encoding_buffer();
bool check_startup();
bool require(int len) const { return (m_head + len) <= m_tail; }
bool tail_require(int len) const { return (m_tail + len) <= m_end; }
void *eat(int);
void *put(int);
uint8_t *head() const { return m_head; }
uint8_t *tail() const { return m_tail; }
uint32_t data_length() const { return m_tail - m_head; }
uint32_t available_length() const { return m_end - m_tail; }
void advance_head(int);
void advance_tail(int);
void compact();
void clear();
bool empty() const { return m_head == m_tail; }
/* Helpers */
template <class T> T &eat() { return *((T *)eat(sizeof(T))); }
template <class T> T &put() { return *((T *)put(sizeof(T))); }
int neatl() { return ntohl(eat<int>()); }
uint32_t neatu32() { return ntohl(eat<uint32_t>()); }
uint8_t neatu8() { return eat<uint8_t>(); }
uint16_t neatu16() { return ntohs(eat<uint16_t>()); }
private:
uint8_t *m_buffer, *m_end;
uint8_t *m_head, *m_tail;
};
#endif
|