| 1 |
#ifndef _PROTO_PKT |
| 2 |
#define _PROTO_PKT |
| 3 |
|
| 4 |
#include "protoDefs.h" // for UINT32, etc types |
| 5 |
|
| 6 |
#ifdef WIN32 |
| 7 |
#include "winsock2.h" // for ntohl(), htonl(), etc |
| 8 |
#else |
| 9 |
#include <netinet/in.h> // for ntohl(), htonl(), etc |
| 10 |
#endif // if/else WIN32/UNIX |
| 11 |
#include <string.h> // for memcpy() |
| 12 |
|
| 13 |
/** |
| 14 |
* @class ProtoPkt |
| 15 |
* |
| 16 |
* @brief This is a base class that maintains a uffer for "packet" |
| 17 |
* (or message) building and parsing. |
| 18 |
* |
| 19 |
* Generally, classes will be derived |
| 20 |
* from this base class to create classes for |
| 21 |
* protocol-specific packet/message |
| 22 |
* building and parsing (For examples, see |
| 23 |
* ProtoPktIP, ProtoPktRTP, etc) |
| 24 |
*/ |
| 25 |
|
| 26 |
class ProtoPkt |
| 27 |
{ |
| 28 |
public: |
| 29 |
ProtoPkt(void* bufferPtr = NULL, unsigned int numBytes = 0, bool freeOnDestruct = false); |
| 30 |
virtual ~ProtoPkt(); |
| 31 |
|
| 32 |
bool AllocateBuffer(unsigned int numBytes) |
| 33 |
{ |
| 34 |
unsigned int len = numBytes / sizeof(UINT32); |
| 35 |
len += (0 == (len % sizeof(UINT32))) ? 0 : 1; |
| 36 |
if (NULL != buffer_allocated) delete[] buffer_allocated; |
| 37 |
buffer_ptr = buffer_allocated = new UINT32[len]; |
| 38 |
buffer_bytes = (NULL != buffer_ptr) ? numBytes : 0; |
| 39 |
pkt_length = 0; |
| 40 |
return (NULL != buffer_ptr); |
| 41 |
} |
| 42 |
void AttachBuffer(void* bufferPtr, unsigned int numBytes, bool freeOnDestruct = false) |
| 43 |
{ |
| 44 |
buffer_ptr = (0 != numBytes) ? (UINT32*)bufferPtr : NULL; |
| 45 |
buffer_bytes = (NULL != bufferPtr) ? numBytes : 0; |
| 46 |
pkt_length = 0; |
| 47 |
if (NULL != buffer_allocated) delete[] buffer_allocated; |
| 48 |
buffer_allocated = freeOnDestruct ? (UINT32*)bufferPtr : NULL; |
| 49 |
} |
| 50 |
UINT32* DetachBuffer() |
| 51 |
{ |
| 52 |
UINT32* theBuffer = buffer_ptr; |
| 53 |
buffer_allocated = buffer_ptr = NULL; |
| 54 |
pkt_length = buffer_bytes = 0; |
| 55 |
return theBuffer; |
| 56 |
} |
| 57 |
bool InitFromBuffer(unsigned int packetLength, |
| 58 |
void* bufferPtr = NULL, |
| 59 |
unsigned int numBytes = 0, |
| 60 |
bool freeOnDestruct = false) |
| 61 |
{ |
| 62 |
if (NULL != bufferPtr) AttachBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 63 |
bool result = (packetLength <= buffer_bytes); |
| 64 |
pkt_length = result ? packetLength : 0; |
| 65 |
return result; |
| 66 |
} |
| 67 |
|
| 68 |
unsigned int GetBufferLength() const |
| 69 |
{return buffer_bytes;} |
| 70 |
|
| 71 |
void SetLength(unsigned int bytes) |
| 72 |
{pkt_length = bytes;} |
| 73 |
unsigned int GetLength() const |
| 74 |
{return pkt_length;} |
| 75 |
|
| 76 |
// These methods get/set fields by byte offsets, using |
| 77 |
// alignment check and different access methods to |
| 78 |
// guarantee safety regardless of alignment. |
| 79 |
|
| 80 |
const void* GetBuffer() const |
| 81 |
{return (void*)buffer_ptr;} |
| 82 |
const void* GetBuffer(unsigned int byteOffset) const |
| 83 |
{return (char*)buffer_ptr + byteOffset;} |
| 84 |
void* AccessBuffer() |
| 85 |
{return (void*)buffer_ptr;} |
| 86 |
void* AccessBuffer(unsigned int offset) |
| 87 |
{return (char*)buffer_ptr + offset;} |
| 88 |
|
| 89 |
// These methods get/set fields by _byte_ offsets |
| 90 |
UINT8 GetUINT8(unsigned int byteOffset) const |
| 91 |
{return ((UINT8*)buffer_ptr)[byteOffset];} |
| 92 |
UINT8 GetUINT8Bits(unsigned int byteOffset, unsigned int bitOffset, unsigned int bitLength = 8) const; |
| 93 |
UINT8& AccessUINT8(unsigned int byteOffset) const |
| 94 |
{return ((UINT8*)buffer_ptr)[byteOffset];} |
| 95 |
void SetUINT8(unsigned int byteOffset, UINT8 value) |
| 96 |
{((UINT8*)buffer_ptr)[byteOffset] = value;} |
| 97 |
void SetUINT8Bits(unsigned int byteOffset, unsigned int bitOffset, UINT8 value, unsigned int bitLength = 8); |
| 98 |
|
| 99 |
UINT16 GetUINT16(unsigned int byteOffset) const |
| 100 |
{return GetUINT16(GetBuffer(byteOffset));} |
| 101 |
UINT16 GetUINT16Bits(unsigned int byteOffset, unsigned int bitOffset, unsigned int bitLength = 16) const; |
| 102 |
void SetUINT16(unsigned int byteOffset, UINT16 value) |
| 103 |
{SetUINT16(AccessBuffer(byteOffset), value);} |
| 104 |
void SetUINT16Bits(unsigned int byteOffset, unsigned int bitsOffset, UINT16 value, unsigned int bitLength = 16); |
| 105 |
|
| 106 |
UINT32 GetUINT32(unsigned int byteOffset) const |
| 107 |
{return GetUINT32(GetBuffer(byteOffset));} |
| 108 |
UINT32 GetUINT32Bits(unsigned int byteOffset, unsigned int bitOffset, unsigned int bitLength = 32) const; |
| 109 |
void SetUINT32(unsigned int byteOffset, UINT32 value) |
| 110 |
{SetUINT32(AccessBuffer(byteOffset), value);} |
| 111 |
void SetUINT32Bits(unsigned int byteOffset, unsigned int bitOffset, UINT32 value, unsigned int bitLength = 32); |
| 112 |
|
| 113 |
// These methods get/set fields by _word_ offsets |
| 114 |
UINT16 GetWord16(unsigned int wordOffset) const |
| 115 |
{return GetUINT16((UINT16*)buffer_ptr + wordOffset);} |
| 116 |
void SetWord16(unsigned int wordOffset, UINT16 value) |
| 117 |
{SetUINT16((UINT16*)buffer_ptr + wordOffset, value);} |
| 118 |
UINT32 GetWord32(unsigned int wordOffset) const |
| 119 |
{return GetUINT32((UINT32*)buffer_ptr + wordOffset);} |
| 120 |
void SetWord32(unsigned int wordOffset, UINT32 value) |
| 121 |
{SetUINT32((UINT32*)buffer_ptr + wordOffset, value);} |
| 122 |
|
| 123 |
const void* GetBuffer16(unsigned int wordOffset) const |
| 124 |
{return (void*)((const UINT16*)buffer_ptr + wordOffset);} |
| 125 |
void* AccessBuffer16(unsigned int wordOffset) |
| 126 |
{return (void*)((const UINT16*)buffer_ptr + wordOffset);} |
| 127 |
|
| 128 |
const void* GetBuffer32(unsigned int wordOffset) const |
| 129 |
{return (void*)((const UINT32*)buffer_ptr + wordOffset);} |
| 130 |
void* AccessBuffer32(unsigned int wordOffset) |
| 131 |
{return (void*)((const UINT32*)buffer_ptr + wordOffset);} |
| 132 |
|
| 133 |
// Pointer alignment checks to determine safe access strategy |
| 134 |
static inline bool IsAligned16(const void* pointer) |
| 135 |
{return (0 == ((uintptr_t)pointer & 1));} |
| 136 |
|
| 137 |
static inline bool IsAligned32(const void* pointer) |
| 138 |
{return (0 == ((uintptr_t)pointer & 3));} |
| 139 |
|
| 140 |
// These methods get/set fields by pointer directly |
| 141 |
// TBD - benchmark this code that does its own |
| 142 |
// alignment checking versus just calling memcpy() always. |
| 143 |
static inline UINT16 GetUINT16(const void* ptr) |
| 144 |
{ |
| 145 |
if (IsAligned16(ptr)) |
| 146 |
{ |
| 147 |
return ntohs(*((const UINT16*)ptr)); |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
UINT16 value; |
| 152 |
memcpy(&value, ptr, 2); |
| 153 |
return ntohs(value); |
| 154 |
} |
| 155 |
} |
| 156 |
static inline void SetUINT16(void* ptr, UINT16 value) |
| 157 |
{ |
| 158 |
if (IsAligned16(ptr)) |
| 159 |
{ |
| 160 |
*((UINT16*)ptr) = htons(value); |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
value = htons(value); |
| 165 |
memcpy(ptr, &value, 2); |
| 166 |
} |
| 167 |
} |
| 168 |
static inline UINT32 GetUINT32(const void* ptr) |
| 169 |
{ |
| 170 |
if (IsAligned32(ptr)) |
| 171 |
{ |
| 172 |
return ntohl(*((const UINT32*)ptr)); |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
UINT32 value; |
| 177 |
memcpy(&value, ptr, 4); |
| 178 |
return ntohl(value); |
| 179 |
} |
| 180 |
} |
| 181 |
static inline void SetUINT32(void* ptr, UINT32 value) |
| 182 |
{ |
| 183 |
if (IsAligned32(ptr)) |
| 184 |
{ |
| 185 |
*((UINT32*)ptr) = htonl(value); |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
value = htonl(value); |
| 190 |
memcpy(ptr, &value, 4); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
bool FreeOnDestruct() const |
| 195 |
{return (NULL != buffer_allocated);} |
| 196 |
|
| 197 |
protected: |
| 198 |
// Note if externally allocated, these pointers may |
| 199 |
// _not_ be 32-bit aligned, but access routines above |
| 200 |
// safely allow for this. |
| 201 |
UINT32* buffer_ptr; |
| 202 |
UINT32* buffer_allocated; |
| 203 |
unsigned int buffer_bytes; |
| 204 |
unsigned int pkt_length; |
| 205 |
|
| 206 |
}; // end class ProtoPkt |
| 207 |
|
| 208 |
#endif // _PROTO_PKT |