switched GRouter to new serialization

This commit is contained in:
csoler 2017-04-25 21:21:46 +02:00
parent 0d7df568b4
commit 77e809ea4b
7 changed files with 343 additions and 195 deletions

View File

@ -1,65 +1,71 @@
#include "util/rsprint.h"
#include "serialiser/rsbaseserial.h"
#include "serialiser/rstlvbase.h"
#include "grouteritems.h"
/**********************************************************************************************/
/* SERIALISATION */
/**********************************************************************************************/
bool RsGRouterItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvsize, uint32_t& offset) const
{
tlvsize = serial_size() ;
offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
if(!setRsItemHeader(data, tlvsize, PacketId(), tlvsize))
{
std::cerr << "RsFileTransferItem::serialise_header(): ERROR. Not enough size!" << std::endl;
return false ;
}
#ifdef RSSERIAL_DEBUG
std::cerr << "RsFileItemSerialiser::serialiseData() Header: " << ok << std::endl;
#endif
offset += 8;
return true ;
}
/**********************************************************************************************/
/* SERIALISER STUFF */
/**********************************************************************************************/
RsItem *RsGRouterSerialiser::deserialise(void *data, uint32_t *pktsize)
RsItem *RsGRouterSerialiser::create_item(uint16_t service_id,uint8_t subtype) const
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if(RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype) || RS_SERVICE_TYPE_GROUTER != getRsItemService(rstype))
{
if(RS_SERVICE_TYPE_GROUTER != service_id)
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
switch(subtype)
{
case RS_PKT_SUBTYPE_GROUTER_DATA: return deserialise_RsGRouterGenericDataItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK: return deserialise_RsGRouterTransactionChunkItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN: return deserialise_RsGRouterTransactionAcknItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT: return deserialise_RsGRouterSignedReceiptItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES: return deserialise_RsGRouterMatrixCluesItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK: return deserialise_RsGRouterMatrixTrackItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST: return deserialise_RsGRouterMatrixFriendListItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO: return deserialise_RsGRouterRoutingInfoItem (data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_DATA: return new RsGRouterGenericDataItem ();
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK: return new RsGRouterTransactionChunkItem();
case RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN: return new RsGRouterTransactionAcknItem ();
case RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT: return new RsGRouterSignedReceiptItem ();
case RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES: return new RsGRouterMatrixCluesItem ();
case RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK: return new RsGRouterMatrixTrackItem ();
case RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST: return new RsGRouterMatrixFriendListItem();
case RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO: return new RsGRouterRoutingInfoItem ();
default:
std::cerr << "RsGRouterSerialiser::deserialise(): Could not de-serialise item. SubPacket id = " << std::hex << getRsItemSubType(rstype) << " id = " << rstype << std::dec << std::endl;
return NULL;
}
return NULL;
}
void RsGRouterTransactionChunkItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint64_t>(j,ctx,propagation_id,"propagation_id") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,chunk_start ,"chunk_start") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,chunk_size ,"chunk_size") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,total_size ,"total_size") ;
// Hack for backward compatibility (the chunk size is not directly next to the chunk data)
if(j == RsItem::DESERIALIZE)
{
if(chunk_size > ctx.mSize || ctx.mOffset > ctx.mSize - chunk_size) // better than if(chunk_size + offset > size)
{
std::cerr << __PRETTY_FUNCTION__ << ": Cannot read beyond item size. Serialisation error!" << std::endl;
ctx.mOk = false ;
return ;
}
if( NULL == (chunk_data = (uint8_t*)rs_malloc(chunk_size)))
{
ctx.mOk = false ;
return ;
}
memcpy(chunk_data,&((uint8_t*)ctx.mData)[ctx.mOffset],chunk_size) ;
ctx.mOffset += chunk_size ;
}
else if(j== RsItem::SERIALIZE)
{
memcpy(&((uint8_t*)ctx.mData)[ctx.mOffset],chunk_data,chunk_size) ;
ctx.mOffset += chunk_size ;
}
else if(j== RsItem::SIZE_ESTIMATE)
ctx.mOffset += chunk_size ;
else
std::cerr << " [Binary data] " << ", length=" << chunk_size << " data=" << RsUtil::BinToHex((uint8_t*)chunk_data,std::min(50u,chunk_size)) << ((chunk_size>50)?"...":"") << std::endl;
}
#ifdef TO_REMOVE
RsGRouterTransactionChunkItem *RsGRouterSerialiser::deserialise_RsGRouterTransactionChunkItem(void *data, uint32_t tlvsize) const
{
uint32_t offset = 8; // skip the header
@ -104,6 +110,14 @@ RsGRouterTransactionChunkItem *RsGRouterSerialiser::deserialise_RsGRouterTransac
return item;
}
#endif
void RsGRouterTransactionAcknItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint64_t>(j,ctx,propagation_id,"propagation_id") ;
}
#ifdef TO_REMOVE
RsGRouterTransactionAcknItem *RsGRouterSerialiser::deserialise_RsGRouterTransactionAcknItem(void *data, uint32_t tlvsize) const
{
uint32_t offset = 8; // skip the header
@ -124,6 +138,41 @@ RsGRouterTransactionAcknItem *RsGRouterSerialiser::deserialise_RsGRouterTransact
return item;
}
#endif
void RsGRouterGenericDataItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint64_t>(j,ctx,routing_id,"routing_id") ;
RsTypeSerializer::serial_process (j,ctx,destination_key,"destination_key") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,service_id,"service_id") ;
RsTypeSerializer::TlvMemBlock_proxy prox(data_bytes,data_size) ;
RsTypeSerializer::serial_process(j,ctx,prox,"data") ;
if(ctx.mFlags & RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE)
return ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,duplication_factor,"duplication_factor") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,flags,"flags") ;
if(j == RsItem::DESERIALIZE) // make sure the duplication factor is not altered by friends. In the worst case, the item will duplicate a bit more.
{
if(duplication_factor < 1)
{
duplication_factor = 1 ;
std::cerr << "(II) correcting GRouter item duplication factor from 0 to 1, to ensure backward compat." << std::endl;
}
if(duplication_factor > GROUTER_MAX_DUPLICATION_FACTOR)
{
std::cerr << "(WW) correcting GRouter item duplication factor of " << duplication_factor << ". This is very unexpected." << std::endl;
duplication_factor = GROUTER_MAX_DUPLICATION_FACTOR ;
}
}
}
#ifdef TO_REMOVE
RsGRouterGenericDataItem *RsGRouterSerialiser::deserialise_RsGRouterGenericDataItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
@ -191,7 +240,23 @@ RsGRouterGenericDataItem *RsGRouterSerialiser::deserialise_RsGRouterGenericDataI
return item;
}
#endif
void RsGRouterSignedReceiptItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process<uint64_t> (j,ctx,routing_id,"routing_id") ;
RsTypeSerializer::serial_process<uint32_t> (j,ctx,flags,"flags") ;
RsTypeSerializer::serial_process (j,ctx,destination_key,"destination_key") ;
RsTypeSerializer::serial_process<uint32_t> (j,ctx,service_id,"service_id") ;
RsTypeSerializer::serial_process (j,ctx,data_hash,"data_hash") ;
if(ctx.mFlags & RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE)
return ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,signature,"signature") ;
}
#ifdef TO_REMOVE
RsGRouterSignedReceiptItem *RsGRouterSerialiser::deserialise_RsGRouterSignedReceiptItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
@ -216,7 +281,76 @@ RsGRouterSignedReceiptItem *RsGRouterSerialiser::deserialise_RsGRouterSignedRece
return item;
}
#endif
void RsGRouterRoutingInfoItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process (j,ctx,peerId,"peerId") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,data_status,"data_status") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_status,"tunnel_status") ;
RsTypeSerializer::serial_process<time_t> (j,ctx,received_time_TS,"received_time_TS") ;
RsTypeSerializer::serial_process<time_t> (j,ctx,last_sent_TS,"last_sent_TS") ;
RsTypeSerializer::serial_process<time_t> (j,ctx,last_tunnel_request_TS,"last_tunnel_request_TS") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,sending_attempts,"sending_attempts") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,client_id,"client_id") ;
RsTypeSerializer::serial_process (j,ctx,item_hash,"item_hash") ;
RsTypeSerializer::serial_process (j,ctx,tunnel_hash,"tunnel_hash") ;
RsTypeSerializer::serial_process<uint32_t>(j,ctx,routing_flags,"routing_flags") ;
RsTypeSerializer::serial_process<RsTlvItem>(j,ctx,incoming_routes,"incoming_routes") ;
// Hack for backward compatibility. Normally we should need a single commandline to serialise/deserialise a single item here.
// But the full item is serialised, so we need the header.
if(j == RsItem::DESERIALIZE)
{
data_item = new RsGRouterGenericDataItem() ;
ctx.mOffset += 8 ;
data_item->serial_process(j,ctx) ;
if(ctx.mOffset < ctx.mSize)
{
receipt_item = new RsGRouterSignedReceiptItem();
ctx.mOffset += 8 ;
receipt_item->serial_process(j,ctx) ;
}
else
receipt_item = NULL ;
}
else if(j == RsItem::SERIALIZE)
{
uint32_t remaining_size = ctx.mSize - ctx.mOffset;
ctx.mOk = ctx.mOk && RsGRouterSerialiser().serialise(data_item,ctx.mData,&remaining_size) ;
ctx.mOffset += RsGRouterSerialiser().size(data_item) ;
if(receipt_item != NULL)
{
remaining_size = ctx.mSize - ctx.mOffset;
ctx.mOk = ctx.mOk && RsGRouterSerialiser().serialise(data_item,ctx.mData,&remaining_size);
ctx.mOffset += RsGRouterSerialiser().size(data_item) ;
}
}
else if(j == RsItem::PRINT)
{
std::cerr << " [Serialized data] " << std::endl;
if(receipt_item != NULL)
std::cerr << " [Receipt item ]" << std::endl;
}
else if(j == RsItem::SIZE_ESTIMATE)
{
ctx.mOffset += RsGRouterSerialiser().size(data_item) ;
if(receipt_item != NULL)
ctx.mOffset += RsGRouterSerialiser().size(receipt_item) ;
}
}
#ifdef TO_REMOVE
RsGRouterRoutingInfoItem *RsGRouterSerialiser::deserialise_RsGRouterRoutingInfoItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
@ -274,6 +408,14 @@ RsGRouterRoutingInfoItem *RsGRouterSerialiser::deserialise_RsGRouterRoutingInfoI
return item;
}
#endif
void RsGRouterMatrixFriendListItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,reverse_friend_indices,"reverse_friend_indices") ;
}
#ifdef TO_REMOVE
RsGRouterMatrixFriendListItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixFriendListItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
@ -299,7 +441,16 @@ RsGRouterMatrixFriendListItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixF
return item;
}
#endif
void RsGRouterMatrixTrackItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,provider_id,"provider_id") ;
RsTypeSerializer::serial_process(j,ctx,message_id,"message_id") ;
RsTypeSerializer::serial_process<time_t>(j,ctx,time_stamp,"time_stamp") ;
}
#ifdef TO_REMOVE
RsGRouterMatrixTrackItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixTrackItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
@ -321,6 +472,22 @@ RsGRouterMatrixTrackItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixTrackI
return item;
}
#endif
void RsGRouterMatrixCluesItem::serial_process(SerializeJob j,SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,destination_key,"destination_key") ;
RsTypeSerializer::serial_process(j,ctx,clues,"clues") ;
}
template<> void RsTypeSerializer::serial_process(RsItem::SerializeJob j,SerializeContext& ctx,RoutingMatrixHitEntry& s,const std::string& name)
{
RsTypeSerializer::serial_process<uint32_t>(j,ctx,s.friend_id,name+":friend_id") ;
RsTypeSerializer::serial_process<float> (j,ctx,s.weight,name+":weight") ;
RsTypeSerializer::serial_process<time_t> (j,ctx,s.time_stamp,name+":time_stamp") ;
}
#ifdef TO_REMOVE
RsGRouterMatrixCluesItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixCluesItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
@ -356,6 +523,8 @@ RsGRouterMatrixCluesItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixCluesI
return item;
}
#endif
RsGRouterGenericDataItem *RsGRouterGenericDataItem::duplicate() const
{
RsGRouterGenericDataItem *item = new RsGRouterGenericDataItem ;
@ -393,6 +562,8 @@ RsGRouterSignedReceiptItem *RsGRouterSignedReceiptItem::duplicate() const
return item ;
}
#ifdef TO_REMOVE
uint32_t RsGRouterGenericDataItem::serial_size() const
{
uint32_t s = 8 ; // header
@ -896,3 +1067,4 @@ std::ostream& RsGRouterMatrixFriendListItem::print(std::ostream& o, uint16_t)
return o ;
}
#endif

View File

@ -63,14 +63,7 @@ class RsGRouterItem: public RsItem
virtual ~RsGRouterItem() {}
virtual bool serialise(void *data,uint32_t& size) const = 0 ;
virtual uint32_t serial_size() const = 0 ;
virtual void clear() = 0 ;
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0;
protected:
bool serialise_header(void *data, uint32_t& pktsize, uint32_t& tlvsize, uint32_t& offset) const;
};
/***********************************************************************************/
@ -100,14 +93,11 @@ public:
RsGRouterAbstractMsgItem(uint8_t pkt_subtype) : RsGRouterItem(pkt_subtype) {}
virtual ~RsGRouterAbstractMsgItem() {}
virtual uint32_t signed_data_size() const = 0 ;
virtual bool serialise_signed_data(void *data,uint32_t size) const = 0 ;
GRouterMsgPropagationId routing_id ;
GRouterKeyId destination_key ;
GRouterServiceId service_id ;
RsTlvKeySignature signature ; // signs mid+destination_key+state
uint32_t flags ; // packet was delivered, not delivered, bounced, etc
uint32_t flags ; // packet was delivered, not delivered, bounced, etc
};
class RsGRouterGenericDataItem: public RsGRouterAbstractMsgItem, public RsGRouterNonCopyableObject
@ -116,15 +106,13 @@ class RsGRouterGenericDataItem: public RsGRouterAbstractMsgItem, public RsGRoute
RsGRouterGenericDataItem() : RsGRouterAbstractMsgItem(RS_PKT_SUBTYPE_GROUTER_DATA) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
virtual ~RsGRouterGenericDataItem() { clear() ; }
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear()
{
free(data_bytes);
data_bytes=NULL;
}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
RsGRouterGenericDataItem *duplicate() const ;
@ -132,35 +120,23 @@ class RsGRouterGenericDataItem: public RsGRouterAbstractMsgItem, public RsGRoute
//
uint32_t data_size ;
uint8_t *data_bytes;
uint32_t duplication_factor ; // number of duplicates allowed. Should be capped at each de-serialise operation!
// utility methods for signing data
virtual uint32_t signed_data_size() const ;
virtual bool serialise_signed_data(void *data, uint32_t size) const ;
};
class RsGRouterSignedReceiptItem: public RsGRouterAbstractMsgItem
{
public:
RsGRouterSignedReceiptItem() : RsGRouterAbstractMsgItem(RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
virtual ~RsGRouterSignedReceiptItem() {}
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual ~RsGRouterSignedReceiptItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
RsGRouterSignedReceiptItem *duplicate() const ;
// packet data
//
Sha1CheckSum data_hash ; // avoids an attacker to re-use a given signed receipt. This is the hash of the enceypted data.
// utility methods for signing data
virtual uint32_t signed_data_size() const ;
virtual bool serialise_signed_data(void *data, uint32_t size) const ;
Sha1CheckSum data_hash ; // avoids an attacker to re-use a given signed receipt. This is the hash of the enceypted data.
};
// Low-level data items
@ -170,62 +146,55 @@ class RsGRouterTransactionItem: public RsGRouterItem
public:
RsGRouterTransactionItem(uint8_t pkt_subtype) : RsGRouterItem(pkt_subtype) {}
virtual ~RsGRouterTransactionItem() {}
virtual ~RsGRouterTransactionItem() {}
virtual bool serialise(void *data,uint32_t& size) const =0;
virtual uint32_t serial_size() const =0;
virtual void clear() =0;
virtual RsGRouterTransactionItem *duplicate() const = 0 ;
virtual RsGRouterTransactionItem *duplicate() const = 0 ;
};
class RsGRouterTransactionChunkItem: public RsGRouterTransactionItem, public RsGRouterNonCopyableObject
{
public:
RsGRouterTransactionChunkItem() : RsGRouterTransactionItem(RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
public:
RsGRouterTransactionChunkItem() : RsGRouterTransactionItem(RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
virtual ~RsGRouterTransactionChunkItem() { free(chunk_data) ; }
virtual ~RsGRouterTransactionChunkItem() { free(chunk_data) ; }
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear() {}
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
virtual RsGRouterTransactionItem *duplicate() const
{
RsGRouterTransactionChunkItem *item = new RsGRouterTransactionChunkItem ;
*item = *this ; // copy all fields
item->chunk_data = (uint8_t*)rs_malloc(chunk_size) ; // deep copy memory chunk
virtual RsGRouterTransactionItem *duplicate() const
{
RsGRouterTransactionChunkItem *item = new RsGRouterTransactionChunkItem ;
*item = *this ; // copy all fields
item->chunk_data = (uint8_t*)rs_malloc(chunk_size) ; // deep copy memory chunk
if(item->chunk_data == NULL)
return NULL ;
memcpy(item->chunk_data,chunk_data,chunk_size) ;
return item ;
}
if(item->chunk_data == NULL)
return NULL ;
GRouterMsgPropagationId propagation_id ;
uint32_t chunk_start ;
uint32_t chunk_size ;
uint32_t total_size ;
uint8_t *chunk_data ;
memcpy(item->chunk_data,chunk_data,chunk_size) ;
return item ;
}
GRouterMsgPropagationId propagation_id ;
uint32_t chunk_start ;
uint32_t chunk_size ;
uint32_t total_size ;
uint8_t *chunk_data ;
};
class RsGRouterTransactionAcknItem: public RsGRouterTransactionItem
{
public:
RsGRouterTransactionAcknItem() : RsGRouterTransactionItem(RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
virtual ~RsGRouterTransactionAcknItem() {}
public:
RsGRouterTransactionAcknItem() : RsGRouterTransactionItem(RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
virtual ~RsGRouterTransactionAcknItem() {}
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
virtual void clear() {}
virtual RsGRouterTransactionItem *duplicate() const { return new RsGRouterTransactionAcknItem(*this) ; }
virtual RsGRouterTransactionItem *duplicate() const { return new RsGRouterTransactionAcknItem(*this) ; }
GRouterMsgPropagationId propagation_id ;
GRouterMsgPropagationId propagation_id ;
};
// Items for saving the routing matrix information.
@ -236,11 +205,8 @@ class RsGRouterMatrixCluesItem: public RsGRouterItem
RsGRouterMatrixCluesItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES)
{ setPriorityLevel(0) ; } // this item is never sent through the network
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
// packet data
//
@ -254,17 +220,15 @@ class RsGRouterMatrixTrackItem: public RsGRouterItem
RsGRouterMatrixTrackItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK)
{ setPriorityLevel(0) ; } // this item is never sent through the network
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
// packet data
//
RsGxsMessageId message_id ;
RsPeerId provider_id ;
time_t time_stamp ;
RsPeerId provider_id ;
time_t time_stamp ;
};
class RsGRouterMatrixFriendListItem: public RsGRouterItem
{
@ -272,11 +236,8 @@ class RsGRouterMatrixFriendListItem: public RsGRouterItem
RsGRouterMatrixFriendListItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST)
{ setPriorityLevel(0) ; } // this item is never sent through the network
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
virtual void clear() {}
// packet data
//
@ -291,8 +252,7 @@ class RsGRouterRoutingInfoItem: public RsGRouterItem, public GRouterRoutingInfo,
virtual ~RsGRouterRoutingInfoItem() { clear() ; }
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void serial_process(SerializeJob j,SerializeContext& ctx);
virtual void clear()
{
@ -302,47 +262,18 @@ class RsGRouterRoutingInfoItem: public RsGRouterItem, public GRouterRoutingInfo,
data_item = NULL ;
receipt_item = NULL ;
}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
};
/***********************************************************************************/
/* Serialisation */
/***********************************************************************************/
class RsGRouterSerialiser: public RsSerialType
class RsGRouterSerialiser: public RsServiceSerializer
{
public:
RsGRouterSerialiser() : RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_GROUTER) {}
RsGRouterSerialiser(SerializationFlags flags = SERIALIZATION_FLAG_NONE) : RsServiceSerializer(RS_SERVICE_TYPE_GROUTER,SerializeContext::FORMAT_BINARY,flags) {}
virtual uint32_t size (RsItem *item)
{
RsGRouterItem *gitem = dynamic_cast<RsGRouterItem *>(item);
if (!gitem)
{
return 0;
}
return gitem->serial_size() ;
}
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
{
RsGRouterItem *gitem = dynamic_cast<RsGRouterItem *>(item);
if (!gitem)
{
return false;
}
return gitem->serialise(data,*size) ;
}
virtual RsItem *deserialise (void *data, uint32_t *size) ;
private:
RsGRouterGenericDataItem *deserialise_RsGRouterGenericDataItem(void *data,uint32_t size) const ;
RsGRouterTransactionChunkItem *deserialise_RsGRouterTransactionChunkItem(void *data,uint32_t size) const ;
RsGRouterTransactionAcknItem *deserialise_RsGRouterTransactionAcknItem(void *data,uint32_t size) const ;
RsGRouterSignedReceiptItem *deserialise_RsGRouterSignedReceiptItem(void *data,uint32_t size) const ;
RsGRouterMatrixCluesItem *deserialise_RsGRouterMatrixCluesItem(void *data,uint32_t size) const ;
RsGRouterMatrixTrackItem *deserialise_RsGRouterMatrixTrackItem(void *data,uint32_t size) const ;
RsGRouterMatrixFriendListItem *deserialise_RsGRouterMatrixFriendListItem(void *data,uint32_t size) const ;
RsGRouterRoutingInfoItem *deserialise_RsGRouterRoutingInfoItem(void *data,uint32_t size) const ;
virtual RsItem *create_item(uint16_t service,uint8_t subtype) const ;
};

View File

@ -1263,13 +1263,13 @@ bool p3GRouter::locked_sendTransactionData(const RsPeerId& pid,const RsGRouterTr
#ifdef GROUTER_DEBUG
std::cerr << " sending to tunnel vpid " << pid << std::endl;
#endif
uint32_t turtle_data_size = trans_item.serial_size() ;
uint32_t turtle_data_size = RsGRouterSerialiser().size(const_cast<RsGRouterTransactionItem*>(&trans_item)) ;
uint8_t *turtle_data = (uint8_t*)rs_malloc(turtle_data_size) ;
if(turtle_data == NULL)
return false ;
if(!trans_item.serialise(turtle_data,turtle_data_size))
if(!RsGRouterSerialiser().serialise(const_cast<RsGRouterTransactionItem*>(&trans_item),turtle_data,&turtle_data_size))
{
std::cerr << " ERROR: cannot serialise RsGRouterTransactionChunkItem." << std::endl;
@ -1428,7 +1428,7 @@ bool p3GRouter::sliceDataItem(RsGRouterAbstractMsgItem *item,std::list<RsGRouter
item->print(std::cerr, 2) ;
#endif
uint32_t size = item->serial_size();
uint32_t size = RsGRouterSerialiser().size(item);
RsTemporaryMemory data(size) ; // data will be freed on return, whatever the route taken.
@ -1438,7 +1438,7 @@ bool p3GRouter::sliceDataItem(RsGRouterAbstractMsgItem *item,std::list<RsGRouter
throw ;
}
if(!item->serialise(data,size))
if(!RsGRouterSerialiser().serialise(item,data,&size))
{
std::cerr << " ERROR: cannot serialise." << std::endl;
throw ;
@ -1617,12 +1617,15 @@ void p3GRouter::handleIncomingReceiptItem(RsGRouterSignedReceiptItem *receipt_it
Sha1CheckSum p3GRouter::computeDataItemHash(RsGRouterGenericDataItem *data_item)
{
uint32_t total_size = data_item->signed_data_size() + data_item->signature.TlvSize() ;
RsGRouterSerialiser signature_serializer(RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE | RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER);
uint32_t signed_data_size = signature_serializer.size(data_item);
uint32_t total_size = signed_data_size + data_item->signature.TlvSize() ;
RsTemporaryMemory mem(total_size) ;
uint32_t offset = 0 ;
data_item->serialise_signed_data(mem,total_size) ;
offset += data_item->signed_data_size() ;
signature_serializer.serialise(data_item,mem,&total_size) ;
offset += signed_data_size ;
data_item->signature.SetTlv(mem, total_size,&offset) ;
@ -1948,28 +1951,45 @@ bool p3GRouter::signDataItem(RsGRouterAbstractMsgItem *item,const RsGxsId& signi
{
try
{
#ifdef GROUTER_DEBUG
//#ifdef GROUTER_DEBUG
std::cerr << "p3GRouter::signDataItem()" << std::endl;
std::cerr << " Key ID = " << signing_id << std::endl;
std::cerr << " Getting key material..." << std::endl;
#endif
uint32_t data_size = item->signed_data_size() ;
RsTemporaryMemory data(data_size) ;
//#endif
RsGRouterSerialiser signature_serializer(RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE | RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER) ;
uint32_t data_size = signature_serializer.size(item) ;
RsTemporaryMemory data(data_size) ;
if(data == NULL)
throw std::runtime_error("Cannot allocate memory for signing data.") ;
if(!item->serialise_signed_data(data,data_size))
if(!signature_serializer.serialise(item,data,&data_size))
throw std::runtime_error("Cannot serialise signed data.") ;
uint32_t error_status ;
uint32_t error_status ;
if(!mGixs->signData(data,data_size,signing_id,item->signature,error_status))
throw std::runtime_error("Cannot sign for id " + signing_id.toStdString() + ". Signature call failed.") ;
std::cerr << "GRouter::signing data" << std::endl;
std::cerr << " size: " << data_size << std::endl;
std::cerr << " data: " << RsUtil::BinToHex(data,data_size) << std::endl;
#ifdef GROUTER_DEBUG
if(!mGixs->signData(data,data_size,signing_id,item->signature,error_status))
throw std::runtime_error("Cannot sign for id " + signing_id.toStdString() + ". Signature call failed.") ;
//#ifdef GROUTER_DEBUG
std::cerr << "Created signature for data hash: " << RsDirUtil::sha1sum(data,data_size) << " and key id=" << signing_id << std::endl;
#endif
//#endif
// Check signature
RsIdentityUsage::UsageCode info;
uint32_t error;
if(verifySignedDataItem(item,info,error))
std::cerr << "Signature checks." << std::endl;
else
{
std::cerr << "(EE) Cannot verify own signed item. Something's wrong." << std::endl;
return false ;
}
return true ;
}
catch(std::exception& e)
@ -1989,15 +2009,16 @@ bool p3GRouter::verifySignedDataItem(RsGRouterAbstractMsgItem *item,const RsIden
std::cerr << "(WW) received global router message from banned identity " << item->signature.keyId << ". Rejecting the message." << std::endl;
return false ;
}
RsGRouterSerialiser signature_serializer(RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE | RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER);
uint32_t data_size = item->signed_data_size() ;
RsTemporaryMemory data(data_size) ;
uint32_t data_size = signature_serializer.size(item) ;
RsTemporaryMemory data(data_size) ;
if(data == NULL)
throw std::runtime_error("Cannot allocate data.") ;
if(data == NULL)
throw std::runtime_error("Cannot allocate data.") ;
if(!item->serialise_signed_data(data,data_size))
throw std::runtime_error("Cannot serialise signed data.") ;
if(!signature_serializer.serialise(item,data,&data_size))
throw std::runtime_error("Cannot serialise signed data.") ;
RsIdentityUsage use(RS_SERVICE_TYPE_GROUTER,info) ;

View File

@ -524,7 +524,7 @@ int pqiSSLstore::readPkt(RsItem **item_out)
int extralen = getRsItemSize(block) - blen;
int totallen = extralen+blen;
if(totallen > 1024*1024)
if(totallen > 1024*1024 || totallen<blen)
{
free(block);
std::cerr << "pqiSSLstore: ERROR: trying to realloc memory for packet of length" << totallen << ", that exceeds the limit of 1MB" << std::endl ;

View File

@ -132,7 +132,7 @@ class RsItem: public RsMemoryManagement::SmallObject
std::cerr << "(EE) RsItem::serial_process() called by an item using new serialization classes, but not derived! " << std::endl;
}
private:
protected:
uint32_t type;
RsPeerId peerId;
uint8_t _priority_level ;

View File

@ -4,19 +4,26 @@
#include "serialization/rsserializer.h"
#include "serialization/rstypeserializer.h"
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_NONE ( 0x0000 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_CONFIG ( 0x0001 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE ( 0x0002 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_NONE ( 0x0000 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_CONFIG ( 0x0001 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_SIGNATURE ( 0x0002 );
const SerializationFlags RsGenericSerializer::SERIALIZATION_FLAG_SKIP_HEADER ( 0x0004 );
RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size)
{
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER)
{
std::cerr << "(EE) Cannot deserialise item with flags SERIALIZATION_FLAG_SKIP_HEADER. Check your code!" << std::endl;
return NULL ;
}
uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ;
RsItem *item = create_item(getRsItemService(rstype),getRsItemSubType(rstype)) ;
if(!item)
{
std::cerr << "(EE) " << typeid(*this).name() << ": cannot deserialise unknown item subtype " << std::hex << getRsItemSubType(rstype) << std::dec << std::endl;
std::cerr << "(EE) " << typeid(*this).name() << ": cannot deserialise unknown item subtype " << std::hex << (int)getRsItemSubType(rstype) << std::dec << std::endl;
std::cerr << "(EE) Data is: " << RsUtil::BinToHex(static_cast<uint8_t*>(data),std::min(50u,*size)) << ((*size>50)?"...":"") << std::endl;
return NULL ;
}
@ -40,13 +47,19 @@ RsItem *RsServiceSerializer::deserialise(void *data, uint32_t *size)
}
RsItem *RsConfigSerializer::deserialise(void *data, uint32_t *size)
{
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER)
{
std::cerr << "(EE) Cannot deserialise item with flags SERIALIZATION_FLAG_SKIP_HEADER. Check your code!" << std::endl;
return NULL ;
}
uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ;
RsItem *item = create_item(getRsItemType(rstype),getRsItemSubType(rstype)) ;
if(!item)
{
std::cerr << "(EE) " << typeid(*this).name() << ": cannot deserialise unknown item subtype " << std::hex << getRsItemSubType(rstype) << std::dec << std::endl;
std::cerr << "(EE) " << typeid(*this).name() << ": cannot deserialise unknown item subtype " << std::hex << (int)getRsItemSubType(rstype) << std::dec << std::endl;
std::cerr << "(EE) Data is: " << RsUtil::BinToHex(static_cast<uint8_t*>(data),std::min(50u,*size)) << ((*size>50)?"...":"") << std::endl;
return NULL ;
}
@ -77,12 +90,19 @@ bool RsGenericSerializer::serialise(RsItem *item,void *data,uint32_t *size)
if(tlvsize > *size)
throw std::runtime_error("Cannot serialise: not enough room.") ;
if(!setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize))
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER)
ctx.mOffset = 0;
else
{
std::cerr << "RsSerializer::serialise_item(): ERROR. Not enough size!" << std::endl;
return false ;
if(!setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize))
{
std::cerr << "RsSerializer::serialise_item(): ERROR. Not enough size!" << std::endl;
return false ;
}
ctx.mOffset = 8;
}
ctx.mOffset = 8;
ctx.mSize = tlvsize;
item->serial_process(RsItem::SERIALIZE,ctx) ;
@ -99,7 +119,10 @@ uint32_t RsGenericSerializer::size(RsItem *item)
{
SerializeContext ctx(NULL,0,mFormat,mFlags);
ctx.mOffset = 8 ; // header size
if(mFlags & SERIALIZATION_FLAG_SKIP_HEADER)
ctx.mOffset = 0;
else
ctx.mOffset = 8 ; // header size
item->serial_process(RsItem::SIZE_ESTIMATE, ctx) ;
return ctx.mOffset ;

View File

@ -24,6 +24,7 @@ public:
static const SerializationFlags SERIALIZATION_FLAG_NONE ; // 0x0000
static const SerializationFlags SERIALIZATION_FLAG_CONFIG ; // 0x0001
static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE ; // 0x0002
static const SerializationFlags SERIALIZATION_FLAG_SKIP_HEADER ; // 0x0004
// The following functions overload RsSerialType. They *should not* need to be further overloaded.