dwicthed GxsTunnel items to new serialization

This commit is contained in:
csoler 2017-04-29 23:02:44 +02:00
parent 1159c863a2
commit 4f92f483fa
4 changed files with 115 additions and 45 deletions

View File

@ -1185,7 +1185,9 @@ bool p3GxsTunnelService::locked_sendClearTunnelData(RsGxsTunnelDHPublicKeyItem *
//
RsTurtleGenericDataItem *gitem = new RsTurtleGenericDataItem ;
uint32_t rssize = item->serial_size() ;
RsGxsTunnelSerialiser ser ;
uint32_t rssize = ser.size(item);
gitem->data_size = rssize + 8 ;
gitem->data_bytes = rs_malloc(rssize+8) ;
@ -1198,7 +1200,7 @@ bool p3GxsTunnelService::locked_sendClearTunnelData(RsGxsTunnelDHPublicKeyItem *
// by convention, we use a IV of 0 for unencrypted data.
memset(gitem->data_bytes,0,8) ;
if(!item->serialise(&((uint8_t*)gitem->data_bytes)[8],rssize))
if(!ser.serialise(item,&((uint8_t*)gitem->data_bytes)[8],&rssize))
{
std::cerr << "(EE) Could not serialise item!!!" << std::endl;
delete gitem ;
@ -1220,10 +1222,12 @@ bool p3GxsTunnelService::locked_sendClearTunnelData(RsGxsTunnelDHPublicKeyItem *
bool p3GxsTunnelService::locked_sendEncryptedTunnelData(RsGxsTunnelItem *item)
{
uint32_t rssize = item->serial_size();
RsGxsTunnelSerialiser ser;
uint32_t rssize = ser.size(item);
RsTemporaryMemory buff(rssize) ;
if(!item->serialise(buff,rssize))
if(!ser.serialise(item,buff,&rssize))
{
std::cerr << "(EE) GxsTunnelService::sendEncryptedTunnelData(): Could not serialise item!" << std::endl;
return false;

View File

@ -28,6 +28,7 @@
#include <time.h>
#include "serialiser/rsbaseserial.h"
#include "serialiser/rstlvbase.h"
#include "serialization/rstypeserializer.h"
#include "util/rsprint.h"
#include "util/rsmemory.h"
@ -35,6 +36,23 @@
//#define GXS_TUNNEL_ITEM_DEBUG 1
RsItem *RsGxsTunnelSerialiser::create_item(uint16_t service,uint8_t item_subtype) const
{
if(service != RS_SERVICE_TYPE_GXS_TUNNEL)
return NULL ;
switch(item_subtype)
{
case RS_PKT_SUBTYPE_GXS_TUNNEL_DATA: return new RsGxsTunnelDataItem();
case RS_PKT_SUBTYPE_GXS_TUNNEL_DATA_ACK: return new RsGxsTunnelDataAckItem();
case RS_PKT_SUBTYPE_GXS_TUNNEL_DH_PUBLIC_KEY: return new RsGxsTunnelDHPublicKeyItem();
case RS_PKT_SUBTYPE_GXS_TUNNEL_STATUS: return new RsGxsTunnelStatusItem();
default:
return NULL ;
}
}
#ifdef TO_REMOVE
std::ostream& RsGxsTunnelDHPublicKeyItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsGxsTunnelDHPublicKeyItem", indent);
@ -87,12 +105,14 @@ std::ostream& RsGxsTunnelStatusItem::print(std::ostream &out, uint16_t indent)
}
/*************************************************************************/
#endif
RsGxsTunnelDHPublicKeyItem::~RsGxsTunnelDHPublicKeyItem()
{
BN_free(public_key) ;
}
#ifdef TO_REMOVE
/*************************************************************************/
RsItem *RsGxsTunnelSerialiser::deserialise(void *data, uint32_t *pktsize)
@ -176,7 +196,54 @@ uint32_t RsGxsTunnelStatusItem::serial_size()
return s ;
}
/*************************************************************************/
#endif
void RsGxsTunnelDHPublicKeyItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process (j,ctx,public_key,"public_key") ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,gxs_key,"gxs_key") ;
}
template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, BIGNUM * const & member)
{
uint32_t s = BN_num_bytes(member) ;
if(size < offset + 4 + s)
return false ;
bool ok = true ;
ok &= setRawUInt32(data, size, &offset, s);
BN_bn2bin(member,&((unsigned char *)data)[offset]) ;
offset += s ;
return ok;
}
template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, BIGNUM *& member)
{
uint32_t s=0 ;
bool ok = true ;
ok &= getRawUInt32(data, size, &offset, &s);
if(s > size || size - s < offset)
return false ;
member = BN_bin2bn(&((unsigned char *)data)[offset],s,NULL) ;
offset += s ;
return ok;
}
template<> uint32_t RsTypeSerializer::serial_size(BIGNUM * const & member)
{
return 4 + BN_num_bytes(member) ;
}
template<> void RsTypeSerializer::print_data(const std::string& name,BIGNUM * const & /* member */)
{
std::cerr << "[BIGNUM] : " << name << std::endl;
}
#ifdef TO_REMOVE
bool RsGxsTunnelDHPublicKeyItem::serialise(void *data,uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
@ -211,7 +278,14 @@ bool RsGxsTunnelDHPublicKeyItem::serialise(void *data,uint32_t& pktsize)
}
return ok ;
}
#endif
void RsGxsTunnelStatusItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,status,"status") ;
}
#ifdef TO_REMOVE
bool RsGxsTunnelStatusItem::serialise(void *data, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
@ -249,7 +323,23 @@ bool RsGxsTunnelStatusItem::serialise(void *data, uint32_t& pktsize)
return ok;
}
#endif
void RsGxsTunnelDataItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint64_t>(j,ctx,unique_item_counter,"unique_item_counter") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,flags ,"flags") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,service_id ,"service_id") ;
RsTypeSerializer::TlvMemBlock_proxy mem(data,data_size) ;
RsTypeSerializer::serial_process(j,ctx,mem,"data") ;
}
void RsGxsTunnelDataAckItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint64_t>(j,ctx,unique_item_counter,"unique_item_counter") ;
}
#ifdef TO_REMOVE
bool RsGxsTunnelDataItem::serialise(void *dt, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
@ -489,6 +579,7 @@ RsGxsTunnelStatusItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelStatusItem(
return item ;
}
#endif

View File

@ -60,10 +60,6 @@ class RsGxsTunnelItem: public RsItem
virtual ~RsGxsTunnelItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ;
virtual bool serialise(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() = 0 ; // deserialise is handled using a constructor
};
/*!
@ -81,10 +77,8 @@ public:
virtual ~RsGxsTunnelDataItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
uint64_t unique_item_counter; // this allows to make the item unique
uint32_t flags; // mainly NEEDS_HACK?
@ -103,10 +97,8 @@ class RsGxsTunnelStatusItem: public RsGxsTunnelItem
RsGxsTunnelStatusItem(void *data,uint32_t size) ; // deserialization
virtual ~RsGxsTunnelStatusItem() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
uint32_t status ;
};
@ -120,12 +112,10 @@ class RsGxsTunnelDataAckItem: public RsGxsTunnelItem
RsGxsTunnelDataAckItem(void *data,uint32_t size) ; // deserialization
virtual ~RsGxsTunnelDataAckItem() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
uint64_t unique_item_counter ; // unique identifier for that item
uint64_t unique_item_counter ; // unique identifier for that item
};
@ -139,10 +129,8 @@ class RsGxsTunnelDHPublicKeyItem: public RsGxsTunnelItem
RsGxsTunnelDHPublicKeyItem(void *data,uint32_t size) ; // deserialization
virtual ~RsGxsTunnelDHPublicKeyItem() ;
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
// Private data to DH public key item
//
@ -157,24 +145,11 @@ class RsGxsTunnelDHPublicKeyItem: public RsGxsTunnelItem
const RsGxsTunnelDHPublicKeyItem& operator=(const RsGxsTunnelDHPublicKeyItem&) { return *this ;}
};
class RsGxsTunnelSerialiser: public RsSerialType
class RsGxsTunnelSerialiser: public RsServiceSerializer
{
public:
RsGxsTunnelSerialiser() :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_GXS_TUNNEL) {}
RsGxsTunnelSerialiser() :RsServiceSerializer(RS_SERVICE_TYPE_GXS_TUNNEL) {}
virtual uint32_t size (RsItem *item)
{
return static_cast<RsGxsTunnelItem *>(item)->serial_size() ;
}
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
{
return static_cast<RsGxsTunnelItem *>(item)->serialise(data,*size) ;
}
RsItem *deserialise(void *data, uint32_t *pktsize);
private:
static RsGxsTunnelDataAckItem *deserialise_RsGxsTunnelDataAckItem (void *data, uint32_t size) ;
static RsGxsTunnelDataItem *deserialise_RsGxsTunnelDataItem (void *data, uint32_t size) ;
static RsGxsTunnelStatusItem *deserialise_RsGxsTunnelStatusItem (void *data, uint32_t size) ;
static RsGxsTunnelDHPublicKeyItem *deserialise_RsGxsTunnelDHPublicKeyItem(void *data, uint32_t size) ;
virtual RsItem *create_item(uint16_t service,uint8_t item_subtype) const ;
};

View File

@ -339,15 +339,15 @@ class RsTypeSerializer
}
protected:
template<class T> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const T& member);
template<class T> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, T& member);
template<class T> static uint32_t serial_size(const T& /* member */);
template<class T> static void print_data(const std::string& name,const T& /* member */);
template<typename T> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const T& member);
template<typename T> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, T& member);
template<typename T> static uint32_t serial_size(const T& /* member */);
template<typename T> static void print_data(const std::string& name,const T& /* member */);
template<class T> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, uint16_t type_id,const T& member);
template<class T> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset,uint16_t type_id, T& member);
template<class T> static uint32_t serial_size(uint16_t type_id,const T& /* member */);
template<class T> static void print_data(const std::string& name,uint16_t type_id,const T& /* member */);
template<typename T> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, uint16_t type_id,const T& member);
template<typename T> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset,uint16_t type_id, T& member);
template<typename T> static uint32_t serial_size(uint16_t type_id,const T& /* member */);
template<typename T> static void print_data(const std::string& name,uint16_t type_id,const T& /* member */);
template<uint32_t ID_SIZE_IN_BYTES,bool UPPER_CASE,uint32_t UNIQUE_IDENTIFIER> static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const t_RsGenericIdType<ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER>& member);
template<uint32_t ID_SIZE_IN_BYTES,bool UPPER_CASE,uint32_t UNIQUE_IDENTIFIER> static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, t_RsGenericIdType<ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER>& member);