mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-29 00:27:27 -04:00
* Major tweak to the serialiser: shifted size to 32bits to handle bigger packets. This will break compatibility.
* Modified tlvitems to support future expansions. * added random tlv test functios. * added dummy Wide Directory functions to rsdir (for the future;) * Fixed Mutex Bug in fimonitor. * added netiface_test for hiberation / net change testing. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1620 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
05676e4399
commit
0f99826623
19 changed files with 1588 additions and 319 deletions
|
@ -29,37 +29,6 @@
|
|||
#include "serialiser/rstlvbase.h"
|
||||
#include "serialiser/rsbaseserial.h"
|
||||
|
||||
/*******************************************************************
|
||||
* These are the general TLV (un)packing routines.
|
||||
*
|
||||
* Data is Serialised into the following format
|
||||
*
|
||||
* -----------------------------------------
|
||||
* | TLV TYPE (2 bytes)| TLV LEN (2 bytes) |
|
||||
* -----------------------------------------
|
||||
* | |
|
||||
* | Data .... |
|
||||
* | |
|
||||
* -----------------------------------------
|
||||
*
|
||||
* Size is the total size of the TLV Field (including the 4 byte header)
|
||||
*
|
||||
* Like the lowlevel packing routines. They are usually
|
||||
* created in pairs - one to pack the data, the other to unpack.
|
||||
*
|
||||
* GetTlvXXX(void *data, uint32_t size, uint32_t *offset, XXX *out);
|
||||
* SetTlvXXX(void *data, uint32_t size, uint32_t *offset, XXX *in);
|
||||
*
|
||||
*
|
||||
* data - the base pointer to the serialised data.
|
||||
* size - size of the memory pointed to by data.
|
||||
* *offset - where we want to (un)pack the data.
|
||||
* This is incremented by the datasize.
|
||||
*
|
||||
* *in / *out - the data to (un)pack.
|
||||
*
|
||||
******************************************************************/
|
||||
|
||||
//*********************
|
||||
|
||||
// A facility func
|
||||
|
@ -71,21 +40,18 @@ inline void* right_shift_void_pointer(void* p, uint32_t len) {
|
|||
|
||||
#define TLV_BASE_DEBUG 1
|
||||
|
||||
|
||||
const uint32_t TYPE_FIELD_BYTES = 2;
|
||||
|
||||
/**** Basic TLV Functions ****/
|
||||
uint16_t GetTlvSize(void *data) {
|
||||
if (!data)
|
||||
return 0;
|
||||
|
||||
uint16_t len;
|
||||
uint32_t len;
|
||||
|
||||
void * from =right_shift_void_pointer(data, sizeof(uint16_t));
|
||||
void * from =right_shift_void_pointer(data, TLV_HEADER_TYPE_SIZE);
|
||||
|
||||
memcpy((void *)&len, from , sizeof(uint16_t));
|
||||
memcpy((void *)&len, from , TLV_HEADER_LEN_SIZE);
|
||||
|
||||
len = ntohs(len);
|
||||
len = ntohl(len);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
@ -96,7 +62,7 @@ uint16_t GetTlvType(void *data) {
|
|||
|
||||
uint16_t type;
|
||||
|
||||
memcpy((void*)&type, data, TYPE_FIELD_BYTES);
|
||||
memcpy((void*)&type, data, TLV_HEADER_TYPE_SIZE);
|
||||
|
||||
type = ntohs(type);
|
||||
|
||||
|
@ -106,48 +72,95 @@ uint16_t GetTlvType(void *data) {
|
|||
|
||||
//tested
|
||||
bool SetTlvBase(void *data, uint32_t size, uint32_t *offset, uint16_t type,
|
||||
uint16_t len) {
|
||||
uint32_t len) {
|
||||
if (!data)
|
||||
return false;
|
||||
if (!offset)
|
||||
return false;
|
||||
if (size < *offset +4)
|
||||
if (size < *offset + TLV_HEADER_SIZE)
|
||||
return false;
|
||||
|
||||
uint16_t type_n = htons(type);
|
||||
|
||||
//copy type_n to (data+*offset)
|
||||
void* to = right_shift_void_pointer(data, *offset);
|
||||
memcpy(to , (void*)&type_n, sizeof(uint16_t));
|
||||
memcpy(to , (void*)&type_n, TLV_HEADER_TYPE_SIZE);
|
||||
|
||||
uint16_t len_n =htons(len);
|
||||
uint32_t len_n =htonl(len);
|
||||
//copy len_n to (data + *offset +2)
|
||||
to = right_shift_void_pointer(to, sizeof(uint16_t));
|
||||
memcpy((void *)to, (void*)&len_n, sizeof(uint16_t));
|
||||
to = right_shift_void_pointer(to, TLV_HEADER_TYPE_SIZE);
|
||||
memcpy((void *)to, (void*)&len_n, TLV_HEADER_LEN_SIZE);
|
||||
|
||||
*offset += sizeof(uint16_t)*2;
|
||||
*offset += TLV_HEADER_SIZE;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SetTlvType(void *data, uint32_t size, uint16_t type)
|
||||
{
|
||||
if (!data)
|
||||
return false;
|
||||
|
||||
if(size < TLV_HEADER_SIZE )
|
||||
return false;
|
||||
|
||||
uint16_t type_n = htons(type);
|
||||
memcpy(data , (void*)&type_n, TLV_HEADER_TYPE_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
//tested
|
||||
bool SetTlvSize(void *data, uint32_t size, uint16_t len) {
|
||||
bool SetTlvSize(void *data, uint32_t size, uint32_t len) {
|
||||
if (!data)
|
||||
return false;
|
||||
|
||||
if(size < sizeof(uint16_t)*2 )
|
||||
if(size < TLV_HEADER_SIZE )
|
||||
return false;
|
||||
|
||||
uint16_t len_n = htons(len);
|
||||
uint32_t len_n = htonl(len);
|
||||
|
||||
void * to = (void*)((uint8_t *) data + sizeof(uint16_t));
|
||||
void * to = (void*)((uint8_t *) data + TLV_HEADER_TYPE_SIZE);
|
||||
|
||||
memcpy(to, (void*) &len_n, sizeof(uint16_t));
|
||||
memcpy(to, (void*) &len_n, TLV_HEADER_LEN_SIZE);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/* Step past unknown TLV TYPE */
|
||||
bool SkipUnknownTlv(void *data, uint32_t size, uint32_t *offset)
|
||||
{
|
||||
if (!data)
|
||||
return false;
|
||||
|
||||
if (size < *offset + TLV_HEADER_SIZE)
|
||||
return false;
|
||||
|
||||
/* extract the type and size */
|
||||
void *tlvstart = right_shift_void_pointer(data, *offset);
|
||||
uint16_t tlvtype = GetTlvType(tlvstart);
|
||||
uint32_t tlvsize = GetTlvSize(tlvstart);
|
||||
|
||||
/* check that there is size */
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
if (size < tlvend)
|
||||
{
|
||||
#ifdef TLV_BASE_DEBUG
|
||||
std::cerr << "SkipUnknownTlv() FAILED - not enough space." << std::endl;
|
||||
std::cerr << "SkipUnknownTlv() size: " << size << std::endl;
|
||||
std::cerr << "SkipUnknownTlv() tlvsize: " << tlvsize << std::endl;
|
||||
std::cerr << "SkipUnknownTlv() tlvend: " << tlvend << std::endl;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = true;
|
||||
/* step past this tlv item */
|
||||
*offset = tlvend;
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
/**** Generic TLV Functions ****
|
||||
* This have the same data (int or string for example),
|
||||
* but they can have different types eg. a string could represent a name or a path,
|
||||
|
@ -159,7 +172,7 @@ bool SetTlvUInt32(void *data, uint32_t size, uint32_t *offset, uint16_t type,
|
|||
{
|
||||
if (!data)
|
||||
return false;
|
||||
uint16_t tlvsize = GetTlvUInt32Size(); /* this will always be 8 bytes */
|
||||
uint32_t tlvsize = GetTlvUInt32Size(); /* this will always be 8 bytes */
|
||||
uint32_t tlvend = *offset + tlvsize; /* where the data will extend to */
|
||||
if (size < tlvend)
|
||||
{
|
||||
|
@ -207,13 +220,13 @@ bool GetTlvUInt32(void *data, uint32_t size, uint32_t *offset,
|
|||
if (!data)
|
||||
return false;
|
||||
|
||||
if (size < *offset + 4)
|
||||
if (size < *offset + TLV_HEADER_SIZE)
|
||||
return false;
|
||||
|
||||
/* extract the type and size */
|
||||
void *tlvstart = right_shift_void_pointer(data, *offset);
|
||||
uint16_t tlvtype = GetTlvType(tlvstart);
|
||||
uint16_t tlvsize = GetTlvSize(tlvstart);
|
||||
uint32_t tlvsize = GetTlvSize(tlvstart);
|
||||
|
||||
/* check that there is size */
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
|
@ -238,7 +251,7 @@ bool GetTlvUInt32(void *data, uint32_t size, uint32_t *offset,
|
|||
return false;
|
||||
}
|
||||
|
||||
*offset += 4; /* step past header */
|
||||
*offset += TLV_HEADER_SIZE; /* step past header */
|
||||
|
||||
bool ok = true;
|
||||
ok &= getRawUInt32(data, tlvend, offset, in);
|
||||
|
@ -248,20 +261,20 @@ bool GetTlvUInt32(void *data, uint32_t size, uint32_t *offset,
|
|||
|
||||
|
||||
uint32_t GetTlvUInt64Size() {
|
||||
return 4 + 8;
|
||||
return TLV_HEADER_SIZE + 8;
|
||||
}
|
||||
|
||||
uint32_t GetTlvUInt32Size() {
|
||||
return 4 + 4;
|
||||
return TLV_HEADER_SIZE + 4;
|
||||
}
|
||||
|
||||
uint32_t GetTlvUInt16Size() {
|
||||
return 4 + sizeof(uint16_t);
|
||||
return TLV_HEADER_SIZE + sizeof(uint16_t);
|
||||
|
||||
}
|
||||
|
||||
uint32_t GetTlvUInt8Size() {
|
||||
return 4 + sizeof(uint8_t);
|
||||
return TLV_HEADER_SIZE + sizeof(uint8_t);
|
||||
|
||||
}
|
||||
|
||||
|
@ -271,7 +284,7 @@ bool SetTlvUInt64(void *data, uint32_t size, uint32_t *offset, uint16_t type,
|
|||
{
|
||||
if (!data)
|
||||
return false;
|
||||
uint16_t tlvsize = GetTlvUInt64Size();
|
||||
uint32_t tlvsize = GetTlvUInt64Size();
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
if (size < tlvend)
|
||||
{
|
||||
|
@ -317,13 +330,13 @@ bool GetTlvUInt64(void *data, uint32_t size, uint32_t *offset,
|
|||
if (!data)
|
||||
return false;
|
||||
|
||||
if (size < *offset + 4)
|
||||
if (size < *offset + TLV_HEADER_SIZE)
|
||||
return false;
|
||||
|
||||
/* extract the type and size */
|
||||
void *tlvstart = right_shift_void_pointer(data, *offset);
|
||||
uint16_t tlvtype = GetTlvType(tlvstart);
|
||||
uint16_t tlvsize = GetTlvSize(tlvstart);
|
||||
uint32_t tlvsize = GetTlvSize(tlvstart);
|
||||
|
||||
/* check that there is size */
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
|
@ -348,7 +361,7 @@ bool GetTlvUInt64(void *data, uint32_t size, uint32_t *offset,
|
|||
return false;
|
||||
}
|
||||
|
||||
*offset += 4; /* step past header */
|
||||
*offset += TLV_HEADER_SIZE; /* step past header */
|
||||
|
||||
bool ok = true;
|
||||
ok &= getRawUInt64(data, tlvend, offset, in);
|
||||
|
@ -362,7 +375,7 @@ bool SetTlvString(void *data, uint32_t size, uint32_t *offset,
|
|||
{
|
||||
if (!data)
|
||||
return false;
|
||||
uint16_t tlvsize = GetTlvStringSize(out);
|
||||
uint32_t tlvsize = GetTlvStringSize(out);
|
||||
uint32_t tlvend = *offset + tlvsize; /* where the data will extend to */
|
||||
|
||||
if (size < tlvend)
|
||||
|
@ -381,7 +394,7 @@ bool SetTlvString(void *data, uint32_t size, uint32_t *offset,
|
|||
|
||||
void * to = right_shift_void_pointer(data, *offset);
|
||||
|
||||
uint16_t strlen = tlvsize - 4;
|
||||
uint16_t strlen = tlvsize - TLV_HEADER_SIZE;
|
||||
memcpy(to, out.c_str(), strlen);
|
||||
|
||||
*offset += strlen;
|
||||
|
@ -396,7 +409,7 @@ bool GetTlvString(void *data, uint32_t size, uint32_t *offset,
|
|||
if (!data)
|
||||
return false;
|
||||
|
||||
if (size < *offset + 4)
|
||||
if (size < *offset + TLV_HEADER_SIZE)
|
||||
{
|
||||
#ifdef TLV_BASE_DEBUG
|
||||
std::cerr << "GetTlvString() FAILED - not enough space" << std::endl;
|
||||
|
@ -409,7 +422,7 @@ bool GetTlvString(void *data, uint32_t size, uint32_t *offset,
|
|||
/* extract the type and size */
|
||||
void *tlvstart = right_shift_void_pointer(data, *offset);
|
||||
uint16_t tlvtype = GetTlvType(tlvstart);
|
||||
uint16_t tlvsize = GetTlvSize(tlvstart);
|
||||
uint32_t tlvsize = GetTlvSize(tlvstart);
|
||||
|
||||
/* check that there is size */
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
|
@ -435,7 +448,7 @@ bool GetTlvString(void *data, uint32_t size, uint32_t *offset,
|
|||
}
|
||||
|
||||
char *strdata = (char *) right_shift_void_pointer(tlvstart, 4);
|
||||
uint16_t strsize = tlvsize - 4; /* remove the header */
|
||||
uint16_t strsize = tlvsize - TLV_HEADER_SIZE; /* remove the header */
|
||||
in = std::string(strdata, strsize);
|
||||
|
||||
*offset += tlvsize; /* step along */
|
||||
|
@ -443,7 +456,7 @@ bool GetTlvString(void *data, uint32_t size, uint32_t *offset,
|
|||
}
|
||||
|
||||
uint32_t GetTlvStringSize(const std::string &in) {
|
||||
return 4 + in.size();
|
||||
return TLV_HEADER_SIZE + in.size();
|
||||
}
|
||||
|
||||
|
||||
|
@ -458,7 +471,7 @@ bool SetTlvWideString(void *data, uint32_t size, uint32_t *offset,
|
|||
{
|
||||
if (!data)
|
||||
return false;
|
||||
uint16_t tlvsize = GetTlvWideStringSize(out);
|
||||
uint32_t tlvsize = GetTlvWideStringSize(out);
|
||||
uint32_t tlvend = *offset + tlvsize; /* where the data will extend to */
|
||||
|
||||
if (size < tlvend)
|
||||
|
@ -475,10 +488,10 @@ bool SetTlvWideString(void *data, uint32_t size, uint32_t *offset,
|
|||
bool ok = true;
|
||||
ok &= SetTlvBase(data, tlvend, offset, type, tlvsize);
|
||||
|
||||
uint16_t strlen = out.length();
|
||||
uint32_t strlen = out.length();
|
||||
|
||||
/* Must convert manually to ensure its always the same! */
|
||||
for(uint16_t i = 0; i < strlen; i++)
|
||||
for(uint32_t i = 0; i < strlen; i++)
|
||||
{
|
||||
uint32_t widechar = out[i];
|
||||
ok &= setRawUInt32(data, tlvend, offset, widechar);
|
||||
|
@ -493,7 +506,7 @@ bool GetTlvWideString(void *data, uint32_t size, uint32_t *offset,
|
|||
if (!data)
|
||||
return false;
|
||||
|
||||
if (size < *offset + 4)
|
||||
if (size < *offset + TLV_HEADER_SIZE)
|
||||
{
|
||||
#ifdef TLV_BASE_DEBUG
|
||||
std::cerr << "GetTlvWideString() FAILED - not enough space" << std::endl;
|
||||
|
@ -506,7 +519,7 @@ bool GetTlvWideString(void *data, uint32_t size, uint32_t *offset,
|
|||
/* extract the type and size */
|
||||
void *tlvstart = right_shift_void_pointer(data, *offset);
|
||||
uint16_t tlvtype = GetTlvType(tlvstart);
|
||||
uint16_t tlvsize = GetTlvSize(tlvstart);
|
||||
uint32_t tlvsize = GetTlvSize(tlvstart);
|
||||
|
||||
/* check that there is size */
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
|
@ -534,11 +547,11 @@ bool GetTlvWideString(void *data, uint32_t size, uint32_t *offset,
|
|||
|
||||
bool ok = true;
|
||||
/* remove the header, calc string length */
|
||||
*offset += 4;
|
||||
uint16_t strlen = (tlvsize - 4) / RS_WCHAR_SIZE;
|
||||
*offset += TLV_HEADER_SIZE;
|
||||
uint16_t strlen = (tlvsize - TLV_HEADER_SIZE) / RS_WCHAR_SIZE;
|
||||
|
||||
/* Must convert manually to ensure its always the same! */
|
||||
for(uint16_t i = 0; i < strlen; i++)
|
||||
for(uint32_t i = 0; i < strlen; i++)
|
||||
{
|
||||
uint32_t widechar;
|
||||
ok &= getRawUInt32(data, tlvend, offset, &widechar);
|
||||
|
@ -548,7 +561,7 @@ bool GetTlvWideString(void *data, uint32_t size, uint32_t *offset,
|
|||
}
|
||||
|
||||
uint32_t GetTlvWideStringSize(std::wstring &in) {
|
||||
return 4 + in.size() * RS_WCHAR_SIZE;
|
||||
return TLV_HEADER_SIZE + in.size() * RS_WCHAR_SIZE;
|
||||
}
|
||||
|
||||
|
||||
|
@ -557,7 +570,7 @@ bool SetTlvIpAddrPortV4(void *data, uint32_t size, uint32_t *offset,
|
|||
if (!data)
|
||||
return false;
|
||||
|
||||
uint16_t tlvsize = GetTlvIpAddrPortV4Size();
|
||||
uint32_t tlvsize = GetTlvIpAddrPortV4Size();
|
||||
uint32_t tlvend = *offset + tlvsize; /* where the data will extend to */
|
||||
|
||||
if (size < tlvend)
|
||||
|
@ -589,7 +602,7 @@ bool GetTlvIpAddrPortV4(void *data, uint32_t size, uint32_t *offset,
|
|||
if (!data)
|
||||
return false;
|
||||
|
||||
if (size < *offset + 4)
|
||||
if (size < *offset + TLV_HEADER_SIZE)
|
||||
{
|
||||
#ifdef TLV_BASE_DEBUG
|
||||
std::cerr << "GetIpAddrPortV4() FAILED - not enough space" << std::endl;
|
||||
|
@ -602,7 +615,7 @@ bool GetTlvIpAddrPortV4(void *data, uint32_t size, uint32_t *offset,
|
|||
/* extract the type and size */
|
||||
void *tlvstart = right_shift_void_pointer(data, *offset);
|
||||
uint16_t tlvtype = GetTlvType(tlvstart);
|
||||
uint16_t tlvsize = GetTlvSize(tlvstart);
|
||||
uint32_t tlvsize = GetTlvSize(tlvstart);
|
||||
|
||||
/* check that there is size */
|
||||
uint32_t tlvend = *offset + tlvsize;
|
||||
|
@ -627,7 +640,7 @@ bool GetTlvIpAddrPortV4(void *data, uint32_t size, uint32_t *offset,
|
|||
return false;
|
||||
}
|
||||
|
||||
*offset += 4; /* skip header */
|
||||
*offset += TLV_HEADER_SIZE; /* skip header */
|
||||
|
||||
bool ok = true;
|
||||
|
||||
|
@ -646,6 +659,6 @@ bool GetTlvIpAddrPortV4(void *data, uint32_t size, uint32_t *offset,
|
|||
}
|
||||
|
||||
uint32_t GetTlvIpAddrPortV4Size() {
|
||||
return 4 + 4 + 2; /* header + 4 (IP) + 2 (Port) */
|
||||
return TLV_HEADER_SIZE + 4 + 2; /* header + 4 (IP) + 2 (Port) */
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue