mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
turned constructors into static functions, to avoid the need of exception handling, and thus treat errors correctly on windows
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3190 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
41fb02d556
commit
13671d03ea
@ -1,7 +1,6 @@
|
|||||||
#ifndef WINDOWS_SYS
|
|
||||||
#include <stdexcept>
|
|
||||||
#endif
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <sstream>
|
||||||
#include "rstunnelitems.h"
|
#include "rstunnelitems.h"
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------------//
|
// -----------------------------------------------------------------------------------//
|
||||||
@ -55,7 +54,7 @@ uint32_t RsTunnelHandshakeItem::serial_size()
|
|||||||
RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
|
RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
|
||||||
{
|
{
|
||||||
#ifdef P3TUNNEL_DEBUG
|
#ifdef P3TUNNEL_DEBUG
|
||||||
std::cerr << "RsTunnelSerialiser::deserialise() called." << std::endl ;
|
std::cerr << "RsTunnelSerialiser::deserialise() called." << std::endl ;
|
||||||
#endif
|
#endif
|
||||||
// look what we have...
|
// look what we have...
|
||||||
|
|
||||||
@ -72,28 +71,15 @@ RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
|
|||||||
return NULL; /* wrong type */
|
return NULL; /* wrong type */
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef WINDOWS_SYS
|
switch(getRsItemSubType(rstype))
|
||||||
try
|
|
||||||
{
|
{
|
||||||
#endif
|
case RS_TUNNEL_SUBTYPE_DATA : return RsTunnelDataItem::deserialise(data,*size) ;
|
||||||
switch(getRsItemSubType(rstype))
|
case RS_TUNNEL_SUBTYPE_HANDSHAKE : return RsTunnelHandshakeItem::deserialise(data,*size) ;
|
||||||
{
|
|
||||||
case RS_TUNNEL_SUBTYPE_DATA : return new RsTunnelDataItem(data,*size) ;
|
|
||||||
case RS_TUNNEL_SUBTYPE_HANDSHAKE : return new RsTunnelHandshakeItem(data,*size) ;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
std::cerr << "Unknown packet type in Rstunnel!" << std::endl ;
|
std::cerr << "Unknown packet type in Rstunnel!" << std::endl ;
|
||||||
return NULL ;
|
return NULL ;
|
||||||
}
|
|
||||||
#ifndef WINDOWS_SYS
|
|
||||||
}
|
}
|
||||||
catch(std::exception& e)
|
|
||||||
{
|
|
||||||
std::cerr << "Exception raised: " << e.what() << std::endl ;
|
|
||||||
return NULL ;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
|
bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
|
||||||
@ -136,9 +122,7 @@ bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
|
|||||||
if ((offset + encoded_data_len) != tlvsize )
|
if ((offset + encoded_data_len) != tlvsize )
|
||||||
{
|
{
|
||||||
ok = false;
|
ok = false;
|
||||||
#ifdef P3TUNNEL_DEBUG
|
|
||||||
std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl;
|
std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef P3TUNNEL_DEBUG
|
#ifdef P3TUNNEL_DEBUG
|
||||||
@ -182,17 +166,38 @@ bool RsTunnelHandshakeItem::serialize(void *data,uint32_t& pktsize)
|
|||||||
if (offset != tlvsize )
|
if (offset != tlvsize )
|
||||||
{
|
{
|
||||||
ok = false;
|
ok = false;
|
||||||
#ifdef P3TUNNEL_DEBUG
|
|
||||||
std::cerr << "RsTunnelHandshakeItem::serialiseTransfer() Size Error! " << std::endl;
|
std::cerr << "RsTunnelHandshakeItem::serialiseTransfer() Size Error! " << std::endl;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
//deserialize in constructor
|
#ifdef P3TUNNEL_DEBUG
|
||||||
RsTunnelDataItem::RsTunnelDataItem(void *data,uint32_t pktsize) : RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA)
|
void displayRawPacket(std::ostream &out, void *data, uint32_t size)
|
||||||
{
|
{
|
||||||
|
uint32_t i;
|
||||||
|
std::ostringstream sout;
|
||||||
|
sout << "DisplayRawPacket: Size: " << size;
|
||||||
|
sout << std::hex;
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
if (i % 16 == 0)
|
||||||
|
{
|
||||||
|
sout << std::endl;
|
||||||
|
}
|
||||||
|
sout << std::setw(2) << std::setfill('0')
|
||||||
|
<< (int) (((unsigned char *) data)[i]) << ":";
|
||||||
|
}
|
||||||
|
sout << std::endl;
|
||||||
|
|
||||||
|
out << sout.str();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//deserialize in constructor
|
||||||
|
RsTunnelDataItem *RsTunnelDataItem::deserialise(void *data,uint32_t pktsize)
|
||||||
|
{
|
||||||
|
RsTunnelDataItem *item = new RsTunnelDataItem ;
|
||||||
#ifdef P3TUNNEL_DEBUG
|
#ifdef P3TUNNEL_DEBUG
|
||||||
std::cerr << "RsTunnelDataItem constructor called : deserializing packet." << std::endl ;
|
std::cerr << "RsTunnelDataItem constructor called : deserializing packet." << std::endl ;
|
||||||
#endif
|
#endif
|
||||||
@ -201,54 +206,69 @@ RsTunnelDataItem::RsTunnelDataItem(void *data,uint32_t pktsize) : RsTunnelItem(R
|
|||||||
bool ok = true ;
|
bool ok = true ;
|
||||||
|
|
||||||
#ifdef P3TUNNEL_DEBUG
|
#ifdef P3TUNNEL_DEBUG
|
||||||
std::cerr << "RsTunnelDataItem constructor rssize : " << rssize << std::endl ;
|
std::cerr << "RsTunnelDataItem constructor rssize : " << rssize << std::endl ;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, sourcePeerId);
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, item->sourcePeerId);
|
||||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, relayPeerId);
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, item->relayPeerId);
|
||||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, destPeerId);
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, item->destPeerId);
|
||||||
|
|
||||||
ok &= getRawUInt32(data, pktsize, &offset, &encoded_data_len) ;
|
ok &= getRawUInt32(data, pktsize, &offset, &item->encoded_data_len) ;
|
||||||
encoded_data = (void*)malloc(encoded_data_len) ;
|
|
||||||
memcpy(encoded_data, (void*)((unsigned char*)data+offset), encoded_data_len);
|
|
||||||
|
|
||||||
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
if(!ok) // return early to avoid calling malloc with 0 size
|
||||||
#else
|
return NULL ;
|
||||||
if ((offset + encoded_data_len) != rssize)
|
|
||||||
throw std::runtime_error("Size error while deserializing.") ;
|
item->encoded_data = (void*)malloc(item->encoded_data_len) ;
|
||||||
|
memcpy(item->encoded_data, (void*)((unsigned char*)data+offset), item->encoded_data_len);
|
||||||
|
|
||||||
|
if ((offset + item->encoded_data_len) != rssize)
|
||||||
|
{
|
||||||
|
std::cerr << "Size error while deserializing a RsTunnelHandshakeItem." << std::endl;
|
||||||
|
#ifdef P3TUNNEL_DEBUG
|
||||||
|
displayRawPacket(std::cerr,data,rssize) ;
|
||||||
|
#endif
|
||||||
|
return NULL ;
|
||||||
|
}
|
||||||
if (!ok)
|
if (!ok)
|
||||||
throw std::runtime_error("Unknown error while deserializing.") ;
|
{
|
||||||
#endif
|
std::cerr << "Error while deserializing a RstunnelDataItem." << std::endl ;
|
||||||
|
return NULL ;
|
||||||
|
}
|
||||||
|
return item ;
|
||||||
}
|
}
|
||||||
|
|
||||||
//deserialize in constructor
|
//deserialize in constructor
|
||||||
RsTunnelHandshakeItem::RsTunnelHandshakeItem(void *data,uint32_t pktsize) : RsTunnelItem(RS_TUNNEL_SUBTYPE_HANDSHAKE)
|
RsTunnelHandshakeItem *RsTunnelHandshakeItem::deserialise(void *data,uint32_t pktsize)
|
||||||
{
|
{
|
||||||
|
RsTunnelHandshakeItem *item = new RsTunnelHandshakeItem ;
|
||||||
#ifdef P3TUNNEL_DEBUG
|
#ifdef P3TUNNEL_DEBUG
|
||||||
std::cerr << "RsTunnelHandshakeItem constructor called : deserializing packet." << std::endl ;
|
std::cerr << "RsTunnelHandshakeItem constructor called : deserializing packet." << std::endl ;
|
||||||
#endif
|
#endif
|
||||||
uint32_t offset = 8; // skip the header
|
uint32_t offset = 8; // skip the header
|
||||||
uint32_t rssize = getRsItemSize(data);
|
uint32_t rssize = getRsItemSize(data);
|
||||||
bool ok = true ;
|
bool ok = true ;
|
||||||
|
|
||||||
#ifdef P3TUNNEL_DEBUG
|
#ifdef P3TUNNEL_DEBUG
|
||||||
std::cerr << "RsTunnelHandshakeItem constructor rssize : " << rssize << std::endl ;
|
std::cerr << "RsTunnelHandshakeItem constructor rssize : " << rssize << std::endl ;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, sourcePeerId);
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, item->sourcePeerId);
|
||||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, relayPeerId);
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, item->relayPeerId);
|
||||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, destPeerId);
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, item->destPeerId);
|
||||||
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_CERT_SSL, sslCertPEM);
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_CERT_SSL, item->sslCertPEM);
|
||||||
ok &= getRawUInt32(data, pktsize, &offset, &connection_accepted);
|
ok &= getRawUInt32(data, pktsize, &offset, &item->connection_accepted);
|
||||||
|
|
||||||
|
if (offset != rssize)
|
||||||
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
{
|
||||||
#else
|
std::cerr << "Size error while deserializing a RsTunnelHandshakeItem." << std::endl;
|
||||||
if (offset != rssize)
|
return NULL ;
|
||||||
throw std::runtime_error("Size error while deserializing.") ;
|
}
|
||||||
if (!ok)
|
if (!ok)
|
||||||
throw std::runtime_error("Unknown error while deserializing.") ;
|
{
|
||||||
#endif
|
std::cerr << "Unknown error while deserializing a RsTunnelHandshakeItem." << std::endl ;
|
||||||
|
return NULL ;
|
||||||
|
}
|
||||||
|
return item ;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& RsTunnelDataItem::print(std::ostream& o, uint16_t)
|
std::ostream& RsTunnelDataItem::print(std::ostream& o, uint16_t)
|
||||||
|
Loading…
Reference in New Issue
Block a user