2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* libretroshare/src/serialiser: rsbaseitems.cc
|
|
|
|
*
|
|
|
|
* RetroShare Serialiser.
|
|
|
|
*
|
|
|
|
* Copyright 2007-2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <time.h>
|
|
|
|
#include "serialiser/rsbaseserial.h"
|
|
|
|
#include "serialiser/rstlvbase.h"
|
2017-04-29 17:02:44 -04:00
|
|
|
#include "serialization/rstypeserializer.h"
|
2015-11-27 23:37:39 -05:00
|
|
|
#include "util/rsprint.h"
|
2016-01-12 21:10:11 -05:00
|
|
|
#include "util/rsmemory.h"
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
#include "gxstunnel/rsgxstunnelitems.h"
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-12-27 22:16:04 -05:00
|
|
|
//#define GXS_TUNNEL_ITEM_DEBUG 1
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2017-04-29 17:02:44 -04:00
|
|
|
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
|
2015-11-27 23:37:39 -05:00
|
|
|
std::ostream& RsGxsTunnelDHPublicKeyItem::print(std::ostream &out, uint16_t indent)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemBase(out, "RsGxsTunnelDHPublicKeyItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << " Signature Key ID: " << signature.keyId << std::endl ;
|
|
|
|
out << " Public Key ID: " << gxs_key.keyId << std::endl ;
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemEnd(out, "RsGxsTunnelMsgItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
std::ostream& RsGxsTunnelDataItem::print(std::ostream &out, uint16_t indent)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemBase(out, "RsGxsTunnelDataItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2015-11-27 23:37:39 -05:00
|
|
|
out << " message id : " << std::hex << unique_item_counter << std::dec << std::endl ;
|
|
|
|
out << " service id : " << std::hex << service_id << std::dec << std::endl ;
|
|
|
|
out << " flags : " << std::hex << flags << std::dec << std::endl ;
|
|
|
|
out << " size : " << data_size << std::endl ;
|
|
|
|
out << " data : " << RsUtil::BinToHex(data,std::min(50u,data_size)) << ((data_size>50u)?"...":"") << std::endl ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemEnd(out, "RsGxsTunnelDataItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
return out;
|
|
|
|
}
|
2015-11-27 23:37:39 -05:00
|
|
|
std::ostream& RsGxsTunnelDataAckItem::print(std::ostream &out, uint16_t indent)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemBase(out, "RsGxsTunnelDataItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2015-11-27 23:37:39 -05:00
|
|
|
out << " message id : " << std::hex << unique_item_counter << std::dec << std::endl ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemEnd(out, "RsGxsTunnelDataAckItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
return out;
|
|
|
|
}
|
2015-11-27 23:37:39 -05:00
|
|
|
std::ostream& RsGxsTunnelStatusItem::print(std::ostream &out, uint16_t indent)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemBase(out, "RsGxsTunnelDataItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2015-11-30 00:02:44 -05:00
|
|
|
out << " flags : " << std::hex << status << std::dec << std::endl ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
printRsItemEnd(out, "RsGxsTunnelStatusItem", indent);
|
2015-11-17 18:11:00 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/*************************************************************************/
|
2017-04-29 17:02:44 -04:00
|
|
|
#endif
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-26 20:40:06 -05:00
|
|
|
RsGxsTunnelDHPublicKeyItem::~RsGxsTunnelDHPublicKeyItem()
|
|
|
|
{
|
|
|
|
BN_free(public_key) ;
|
|
|
|
}
|
2015-11-27 23:37:39 -05:00
|
|
|
|
2017-04-29 17:02:44 -04:00
|
|
|
#ifdef TO_REMOVE
|
2015-11-27 23:37:39 -05:00
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
RsItem *RsGxsTunnelSerialiser::deserialise(void *data, uint32_t *pktsize)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
#ifdef GXS_TUNNEL_ITEM_DEBUG
|
|
|
|
std::cerr << "deserializing packet..."<< std::endl ;
|
2015-11-17 18:11:00 -05:00
|
|
|
#endif
|
2015-11-27 23:37:39 -05:00
|
|
|
// look what we have...
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
{
|
|
|
|
std::cerr << "GxsTunnel deserialisation: not enough size: pktsize=" << *pktsize << ", rssize=" << rssize << std::endl ;
|
|
|
|
return NULL; /* not enough data */
|
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/* ready to load */
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_GXS_TUNNEL != getRsItemService(rstype)))
|
|
|
|
{
|
|
|
|
#ifdef GXS_TUNNEL_ITEM_DEBUG
|
|
|
|
std::cerr << "GxsTunnel deserialisation: wrong type !" << std::endl ;
|
2015-11-17 18:11:00 -05:00
|
|
|
#endif
|
2015-11-27 23:37:39 -05:00
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
switch(getRsItemSubType(rstype))
|
|
|
|
{
|
|
|
|
case RS_PKT_SUBTYPE_GXS_TUNNEL_DH_PUBLIC_KEY: return deserialise_RsGxsTunnelDHPublicKeyItem(data,*pktsize) ;
|
|
|
|
case RS_PKT_SUBTYPE_GXS_TUNNEL_DATA: return deserialise_RsGxsTunnelDataItem (data,*pktsize) ;
|
|
|
|
case RS_PKT_SUBTYPE_GXS_TUNNEL_DATA_ACK: return deserialise_RsGxsTunnelDataAckItem (data,*pktsize) ;
|
|
|
|
case RS_PKT_SUBTYPE_GXS_TUNNEL_STATUS: return deserialise_RsGxsTunnelStatusItem (data,*pktsize) ;
|
|
|
|
default:
|
|
|
|
std::cerr << "Unknown packet type in chat!" << std::endl ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/*************************************************************************/
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t RsGxsTunnelDHPublicKeyItem::serial_size()
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t s = 8 ; // header
|
|
|
|
s += 4 ; // BN size
|
|
|
|
s += BN_num_bytes(public_key) ; // public_key
|
|
|
|
s += signature.TlvSize() ; // signature
|
|
|
|
s += gxs_key.TlvSize() ; // gxs_key
|
2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t RsGxsTunnelDataItem::serial_size()
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t s = 8 ; // header
|
|
|
|
s += 8 ; // counter
|
|
|
|
s += 4 ; // flags
|
|
|
|
s += 4 ; // service id
|
|
|
|
s += 4 ; // data_size
|
|
|
|
s += data_size; // data
|
2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t RsGxsTunnelDataAckItem::serial_size()
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t s = 8 ; // header
|
|
|
|
s += 8 ; // counter
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
return s ;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t RsGxsTunnelStatusItem::serial_size()
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t s = 8 ; // header
|
|
|
|
s += 4 ; // flags
|
2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
|
|
|
/*************************************************************************/
|
2017-04-29 17:02:44 -04:00
|
|
|
#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;
|
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2017-04-29 17:02:44 -04:00
|
|
|
#ifdef TO_REMOVE
|
2015-11-27 23:37:39 -05:00
|
|
|
bool RsGxsTunnelDHPublicKeyItem::serialise(void *data,uint32_t& pktsize)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size() ;
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
uint32_t s = BN_num_bytes(public_key) ;
|
|
|
|
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, s);
|
|
|
|
|
|
|
|
BN_bn2bin(public_key,&((unsigned char *)data)[offset]) ;
|
|
|
|
offset += s ;
|
|
|
|
|
|
|
|
ok &= signature.SetTlv(data, tlvsize, &offset);
|
|
|
|
ok &= gxs_key.SetTlv(data, tlvsize, &offset);
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2015-11-27 23:37:39 -05:00
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::serialiseItem() Size Error! offset=" << offset << ", tlvsize=" << tlvsize << std::endl;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
return ok ;
|
|
|
|
}
|
2017-04-29 17:02:44 -04:00
|
|
|
#endif
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2017-04-29 17:02:44 -04:00
|
|
|
void RsGxsTunnelStatusItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
|
|
|
|
{
|
|
|
|
RsTypeSerializer::serial_process<uint32_t>(j,ctx,status,"status") ;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TO_REMOVE
|
2015-11-27 23:37:39 -05:00
|
|
|
bool RsGxsTunnelStatusItem::serialise(void *data, uint32_t& pktsize)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t tlvsize = serial_size() ;
|
2015-11-17 18:11:00 -05:00
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
#ifdef GXS_TUNNEL_ITEM_DEBUG
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser serialising chat status item." << std::endl;
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
|
2015-11-17 18:11:00 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
2015-11-30 00:02:44 -05:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, status);
|
2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2015-11-27 23:37:39 -05:00
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size Error! " << std::endl;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
2015-11-27 23:37:39 -05:00
|
|
|
#ifdef GXS_TUNNEL_ITEM_DEBUG
|
2015-11-17 18:11:00 -05:00
|
|
|
std::cerr << "computed size: " << 256*((unsigned char*)data)[6]+((unsigned char*)data)[7] << std::endl ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
2017-04-29 17:02:44 -04:00
|
|
|
#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") ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2017-04-29 17:02:44 -04:00
|
|
|
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
|
2015-11-30 00:02:44 -05:00
|
|
|
bool RsGxsTunnelDataItem::serialise(void *dt, uint32_t& pktsize)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size() ;
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
2015-11-30 00:02:44 -05:00
|
|
|
ok &= setRsItemHeader(dt, tlvsize, PacketId(), tlvsize);
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
#ifdef GXS_TUNNEL_ITEM_DEBUG
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser serialising chat status item." << std::endl;
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
|
2015-11-17 18:11:00 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
2015-11-30 00:02:44 -05:00
|
|
|
ok &= setRawUInt64(dt, tlvsize, &offset, unique_item_counter);
|
|
|
|
ok &= setRawUInt32(dt, tlvsize, &offset, flags);
|
|
|
|
ok &= setRawUInt32(dt, tlvsize, &offset, service_id);
|
|
|
|
ok &= setRawUInt32(dt, tlvsize, &offset, data_size);
|
2015-11-27 23:37:39 -05:00
|
|
|
|
|
|
|
if(offset + data_size <= tlvsize)
|
|
|
|
{
|
2015-11-30 00:02:44 -05:00
|
|
|
memcpy(&((uint8_t*)dt)[offset],data,data_size) ;
|
2015-11-27 23:37:39 -05:00
|
|
|
offset += data_size ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ok = false ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2015-11-27 23:37:39 -05:00
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size Error! " << std::endl;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
2015-11-27 23:37:39 -05:00
|
|
|
bool RsGxsTunnelDataAckItem::serialise(void *data, uint32_t& pktsize)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size() ;
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
#ifdef GXS_TUNNEL_ITEM_DEBUG
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser serialising chat status item." << std::endl;
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
|
2015-11-17 18:11:00 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
2015-11-27 23:37:39 -05:00
|
|
|
ok &= setRawUInt64(data, tlvsize, &offset, unique_item_counter);
|
|
|
|
|
2015-11-17 18:11:00 -05:00
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2015-11-27 23:37:39 -05:00
|
|
|
std::cerr << "RsGxsTunnelSerialiser::serialiseItem() Size Error! " << std::endl;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/*************************************************************************/
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-28 14:55:56 -05:00
|
|
|
RsGxsTunnelDHPublicKeyItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelDHPublicKeyItem(void *data,uint32_t /*size*/)
|
2015-11-27 23:37:39 -05:00
|
|
|
{
|
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
bool ok = true ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
RsGxsTunnelDHPublicKeyItem *item = new RsGxsTunnelDHPublicKeyItem() ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t s=0 ;
|
|
|
|
/* get mandatory parts first */
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &s);
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2016-01-11 20:49:26 -05:00
|
|
|
if(s > rssize || rssize - s < offset)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
item->public_key = BN_bin2bn(&((unsigned char *)data)[offset],s,NULL) ;
|
|
|
|
offset += s ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
ok &= item->signature.GetTlv(data, rssize, &offset) ;
|
|
|
|
ok &= item->gxs_key.GetTlv(data, rssize, &offset) ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
if (offset != rssize)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
return item ;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
2016-06-02 21:21:18 -04:00
|
|
|
RsGxsTunnelDataItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelDataItem(void *dat,uint32_t size)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(dat);
|
|
|
|
bool ok = true ;
|
2016-06-02 20:23:22 -04:00
|
|
|
|
|
|
|
if(rssize > size)
|
|
|
|
{
|
2016-06-03 03:02:19 -04:00
|
|
|
std::cerr << "RsGxsTunnelDataItem::() Size error while deserializing." << std::endl ;
|
|
|
|
return NULL ;
|
2016-06-02 20:23:22 -04:00
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
RsGxsTunnelDataItem *item = new RsGxsTunnelDataItem();
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/* get mandatory parts first */
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
ok &= getRawUInt64(dat, rssize, &offset, &item->unique_item_counter);
|
|
|
|
ok &= getRawUInt32(dat, rssize, &offset, &item->flags);
|
|
|
|
ok &= getRawUInt32(dat, rssize, &offset, &item->service_id);
|
|
|
|
ok &= getRawUInt32(dat, rssize, &offset, &item->data_size);
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2016-06-02 20:23:22 -04:00
|
|
|
if(item->data_size > rssize || rssize < offset + item->data_size)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2016-06-02 20:23:22 -04:00
|
|
|
std::cerr << "RsGxsTunnelDataItem::() Size error while deserializing." << std::endl ;
|
2016-01-11 20:49:26 -05:00
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
2016-01-12 21:43:04 -05:00
|
|
|
item->data = (unsigned char*)rs_malloc(item->data_size) ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2016-01-11 20:49:26 -05:00
|
|
|
if(item->data == NULL)
|
|
|
|
{
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
2016-01-11 20:49:26 -05:00
|
|
|
|
|
|
|
memcpy(item->data,&((uint8_t*)dat)[offset],item->data_size) ;
|
|
|
|
offset += item->data_size ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
if (offset != rssize)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
return item ;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-28 14:55:56 -05:00
|
|
|
RsGxsTunnelDataAckItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelDataAckItem(void *dat,uint32_t /* size */)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(dat);
|
|
|
|
bool ok = true ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
RsGxsTunnelDataAckItem *item = new RsGxsTunnelDataAckItem();
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/* get mandatory parts first */
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
ok &= getRawUInt64(dat, rssize, &offset, &item->unique_item_counter);
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
if (offset != rssize)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Size error while deserializing." << std::endl ;
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
std::cerr << "RsGxsTunnelDHPublicKeyItem::() Unknown error while deserializing." << std::endl ;
|
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
return item ;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
|
|
|
|
2016-06-02 21:21:18 -04:00
|
|
|
RsGxsTunnelStatusItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelStatusItem(void *dat, uint32_t size)
|
2015-11-17 18:11:00 -05:00
|
|
|
{
|
2015-11-27 23:37:39 -05:00
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(dat);
|
|
|
|
bool ok = true ;
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2016-06-02 20:23:22 -04:00
|
|
|
if(rssize > size)
|
|
|
|
{
|
2016-06-03 03:02:19 -04:00
|
|
|
std::cerr << "RsGxsTunnelStatusItem::() Size error while deserializing." << std::endl ;
|
|
|
|
return NULL ;
|
2016-06-02 20:23:22 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
RsGxsTunnelStatusItem *item = new RsGxsTunnelStatusItem();
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
/* get mandatory parts first */
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-30 00:02:44 -05:00
|
|
|
ok &= getRawUInt32(dat, rssize, &offset, &item->status);
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
if (offset != rssize)
|
|
|
|
{
|
2016-06-02 20:23:22 -04:00
|
|
|
std::cerr << "RsGxsTunnelStatusItem::() Size error while deserializing." << std::endl ;
|
2015-11-27 23:37:39 -05:00
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
{
|
2016-06-02 20:23:22 -04:00
|
|
|
std::cerr << "RsGxsTunnelStatusItem::() Unknown error while deserializing." << std::endl ;
|
2015-11-27 23:37:39 -05:00
|
|
|
delete item ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
2015-11-17 18:11:00 -05:00
|
|
|
|
2015-11-27 23:37:39 -05:00
|
|
|
return item ;
|
2015-11-17 18:11:00 -05:00
|
|
|
}
|
2017-04-29 17:02:44 -04:00
|
|
|
#endif
|
2015-11-17 18:11:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|