mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-27 00:19:25 -05:00
moved turtle and FT to new serialization
This commit is contained in:
parent
3453a3e57d
commit
636450f14d
@ -64,7 +64,7 @@ static const time_t FILE_TRANSFER_LOW_PRIORITY_TASKS_PERIOD = 5 ; // low priorit
|
||||
|
||||
/* Setup */
|
||||
ftServer::ftServer(p3PeerMgr *pm, p3ServiceControl *sc)
|
||||
: p3Service(),
|
||||
: p3Service(),RsSerializer(RS_SERVICE_TYPE_TURTLE), // should be FT, but this is for backward compatibility
|
||||
mPeerMgr(pm), mServiceCtrl(sc),
|
||||
mFileDatabase(NULL),
|
||||
mFtController(NULL), mFtExtra(NULL),
|
||||
@ -459,14 +459,12 @@ bool ftServer::FileDetails(const RsFileHash &hash, FileSearchFlags hintflags, Fi
|
||||
return false;
|
||||
}
|
||||
|
||||
RsTurtleGenericTunnelItem *ftServer::deserialiseItem(void *data,uint32_t size) const
|
||||
RsItem *ftServer::create_item(uint16_t service,uint8_t item_type)
|
||||
{
|
||||
uint32_t rstype = getRsItemId(data);
|
||||
|
||||
#ifdef SERVER_DEBUG
|
||||
FTSERVER_DEBUG() << "p3turtle: deserialising packet: " << std::endl ;
|
||||
#endif
|
||||
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_TURTLE != getRsItemService(rstype)))
|
||||
if (RS_SERVICE_TYPE_TURTLE != service)
|
||||
{
|
||||
FTSERVER_ERROR() << " Wrong type !!" << std::endl ;
|
||||
return NULL; /* wrong type */
|
||||
@ -474,14 +472,14 @@ RsTurtleGenericTunnelItem *ftServer::deserialiseItem(void *data,uint32_t size) c
|
||||
|
||||
try
|
||||
{
|
||||
switch(getRsItemSubType(rstype))
|
||||
switch(item_type)
|
||||
{
|
||||
case RS_TURTLE_SUBTYPE_FILE_REQUEST : return new RsTurtleFileRequestItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_DATA : return new RsTurtleFileDataItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_MAP_REQUEST : return new RsTurtleFileMapRequestItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_MAP : return new RsTurtleFileMapItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_CHUNK_CRC_REQUEST : return new RsTurtleChunkCrcRequestItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_CHUNK_CRC : return new RsTurtleChunkCrcItem(data,size) ;
|
||||
case RS_TURTLE_SUBTYPE_FILE_REQUEST : return new RsTurtleFileRequestItem();
|
||||
case RS_TURTLE_SUBTYPE_FILE_DATA : return new RsTurtleFileDataItem();
|
||||
case RS_TURTLE_SUBTYPE_FILE_MAP_REQUEST : return new RsTurtleFileMapRequestItem();
|
||||
case RS_TURTLE_SUBTYPE_FILE_MAP : return new RsTurtleFileMapItem();
|
||||
case RS_TURTLE_SUBTYPE_CHUNK_CRC_REQUEST : return new RsTurtleChunkCrcRequestItem();
|
||||
case RS_TURTLE_SUBTYPE_CHUNK_CRC : return new RsTurtleChunkCrcItem();
|
||||
|
||||
default:
|
||||
return NULL ;
|
||||
@ -1189,7 +1187,8 @@ bool ftServer::encryptItem(RsTurtleGenericTunnelItem *clear_item,const RsFileHas
|
||||
FTSERVER_DEBUG() << " random nonce : " << RsUtil::BinToHex(initialization_vector,ENCRYPTED_FT_INITIALIZATION_VECTOR_SIZE) << std::endl;
|
||||
#endif
|
||||
|
||||
uint32_t total_data_size = ENCRYPTED_FT_HEADER_SIZE + ENCRYPTED_FT_INITIALIZATION_VECTOR_SIZE + ENCRYPTED_FT_EDATA_SIZE + clear_item->serial_size() + ENCRYPTED_FT_AUTHENTICATION_TAG_SIZE ;
|
||||
uint32_t item_serialized_size = size(clear_item) ;
|
||||
uint32_t total_data_size = ENCRYPTED_FT_HEADER_SIZE + ENCRYPTED_FT_INITIALIZATION_VECTOR_SIZE + ENCRYPTED_FT_EDATA_SIZE + item_serialized_size + ENCRYPTED_FT_AUTHENTICATION_TAG_SIZE ;
|
||||
|
||||
#ifdef SERVER_DEBUG
|
||||
FTSERVER_DEBUG() << " clear part size : " << clear_item->serial_size() << std::endl;
|
||||
@ -1204,7 +1203,7 @@ bool ftServer::encryptItem(RsTurtleGenericTunnelItem *clear_item,const RsFileHas
|
||||
return false ;
|
||||
|
||||
uint8_t *edata = (uint8_t*)encrypted_item->data_bytes ;
|
||||
uint32_t edata_size = clear_item->serial_size() ;
|
||||
uint32_t edata_size = item_serialized_size;
|
||||
uint32_t offset = 0;
|
||||
|
||||
edata[0] = 0xae ;
|
||||
@ -1227,7 +1226,8 @@ bool ftServer::encryptItem(RsTurtleGenericTunnelItem *clear_item,const RsFileHas
|
||||
offset += ENCRYPTED_FT_EDATA_SIZE ;
|
||||
|
||||
uint32_t ser_size = (uint32_t)((int)total_data_size - (int)offset);
|
||||
clear_item->serialize(&edata[offset], ser_size);
|
||||
|
||||
serialise(clear_item,&edata[offset], &ser_size);
|
||||
|
||||
#ifdef SERVER_DEBUG
|
||||
FTSERVER_DEBUG() << " clear item : " << RsUtil::BinToHex(&edata[offset],std::min(50,(int)total_data_size-(int)offset)) << "(...)" << std::endl;
|
||||
@ -1331,7 +1331,7 @@ bool ftServer::decryptItem(RsTurtleGenericDataItem *encrypted_item,const RsFileH
|
||||
return false ;
|
||||
}
|
||||
|
||||
decrypted_item = deserialiseItem(&edata[clear_item_offset],edata_size) ;
|
||||
decrypted_item = dynamic_cast<RsTurtleGenericTunnelItem*>(deserialise(&edata[clear_item_offset],&edata_size)) ;
|
||||
|
||||
if(decrypted_item == NULL)
|
||||
return false ;
|
||||
|
@ -68,7 +68,7 @@ class p3PeerMgr;
|
||||
class p3ServiceControl;
|
||||
class p3FileDatabase;
|
||||
|
||||
class ftServer: public p3Service, public RsFiles, public ftDataSend, public RsTurtleClientService
|
||||
class ftServer: public p3Service, public RsFiles, public ftDataSend, public RsTurtleClientService, public RsSerializer
|
||||
{
|
||||
|
||||
public:
|
||||
@ -97,7 +97,7 @@ public:
|
||||
//
|
||||
virtual bool handleTunnelRequest(const RsFileHash& hash,const RsPeerId& peer_id) ;
|
||||
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const RsFileHash& hash,const RsPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ;
|
||||
virtual RsTurtleGenericTunnelItem *deserialiseItem(void *data,uint32_t size) const ;
|
||||
virtual RsItem *create_item(uint16_t service,uint8_t item_type) ;
|
||||
|
||||
void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&,RsTurtleGenericTunnelItem::Direction dir) ;
|
||||
void removeVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
|
||||
|
@ -30,6 +30,9 @@
|
||||
#include <serialiser/itempriorities.h>
|
||||
#include <ft/ftturtlefiletransferitem.h>
|
||||
|
||||
#include <serialization/rstypeserializer.h>
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
uint32_t RsTurtleFileRequestItem::serial_size() const
|
||||
{
|
||||
uint32_t s = 0 ;
|
||||
@ -101,6 +104,15 @@ uint32_t RsTurtleChunkCrcRequestItem::serial_size() const
|
||||
|
||||
return s ;
|
||||
}
|
||||
#endif
|
||||
|
||||
void RsTurtleFileMapRequestItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id,"tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,direction,"direction") ;
|
||||
}
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
bool RsTurtleFileMapRequestItem::serialize(void *data,uint32_t& pktsize) const
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
@ -134,6 +146,7 @@ bool RsTurtleFileMapRequestItem::serialize(void *data,uint32_t& pktsize) const
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
bool RsTurtleFileMapItem::serialize(void *data,uint32_t& pktsize) const
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
@ -563,3 +576,40 @@ std::ostream& RsTurtleChunkCrcItem::print(std::ostream& o, uint16_t)
|
||||
return o ;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void RsTurtleFileMapItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id,"tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,direction,"direction") ;
|
||||
RsTypeSerializer::serial_process (j,ctx,compressed_map._map,"map") ;
|
||||
}
|
||||
void RsTurtleChunkCrcRequestItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id,"tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,chunk_number,"chunk_number") ;
|
||||
}
|
||||
void RsTurtleChunkCrcItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id,"tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,chunk_number,"chunk_number") ;
|
||||
RsTypeSerializer::serial_process (j,ctx,check_sum,"check_sum") ;
|
||||
}
|
||||
|
||||
void RsTurtleFileRequestItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id,"tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint64_t>(j,ctx,chunk_offset,"chunk_offset") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,chunk_size,"chunk_size") ;
|
||||
}
|
||||
void RsTurtleFileDataItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id,"tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint64_t>(j,ctx,chunk_offset,"chunk_offset") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,chunk_size,"chunk_size") ;
|
||||
|
||||
RsTypeSerializer::TlvMemBlock_proxy prox(chunk_data,chunk_size) ;
|
||||
|
||||
RsTypeSerializer::serial_process(j,ctx,prox,"chunk_data") ;
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,6 @@ class RsTurtleFileRequestItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleFileRequestItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_FILE_REQUEST) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_FILE_REQUEST);}
|
||||
RsTurtleFileRequestItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual bool shouldStampTunnel() const { return false ; }
|
||||
virtual Direction travelingDirection() const { return DIRECTION_SERVER ; }
|
||||
@ -42,51 +41,50 @@ class RsTurtleFileRequestItem: public RsTurtleGenericTunnelItem
|
||||
uint64_t chunk_offset ;
|
||||
uint32_t chunk_size ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
void clear() {}
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) const;
|
||||
virtual uint32_t serial_size() const;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
class RsTurtleFileDataItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleFileDataItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_FILE_DATA) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_FILE_DATA) ;}
|
||||
~RsTurtleFileDataItem() ;
|
||||
RsTurtleFileDataItem(void *data,uint32_t size) ; // deserialization
|
||||
~RsTurtleFileDataItem() { clear() ; }
|
||||
|
||||
virtual bool shouldStampTunnel() const { return true ; }
|
||||
virtual Direction travelingDirection() const { return DIRECTION_CLIENT ; }
|
||||
|
||||
void clear()
|
||||
{
|
||||
free(chunk_data);
|
||||
chunk_data = NULL ;
|
||||
chunk_size = 0 ;
|
||||
chunk_offset = 0 ;
|
||||
}
|
||||
|
||||
uint64_t chunk_offset ; // offset in the file
|
||||
uint32_t chunk_size ; // size of the file chunk
|
||||
void *chunk_data ; // actual data.
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
virtual bool serialize(void *data,uint32_t& size) const;
|
||||
virtual uint32_t serial_size() const;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
class RsTurtleFileMapRequestItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleFileMapRequestItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_FILE_MAP_REQUEST) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_FILE_MAP_REQUEST) ;}
|
||||
RsTurtleFileMapRequestItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual bool shouldStampTunnel() const { return false ; }
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
virtual bool serialize(void *data,uint32_t& size) const;
|
||||
virtual uint32_t serial_size() const;
|
||||
void clear() {}
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
class RsTurtleFileMapItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleFileMapItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_FILE_MAP) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_FILE_MAP) ;}
|
||||
RsTurtleFileMapItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual bool shouldStampTunnel() const { return false ; }
|
||||
|
||||
@ -94,34 +92,28 @@ class RsTurtleFileMapItem: public RsTurtleGenericTunnelItem
|
||||
// by default, we suppose the peer has all the chunks. This info will thus be and-ed
|
||||
// with the default file map for this source.
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
virtual bool serialize(void *data,uint32_t& size) const;
|
||||
virtual uint32_t serial_size() const;
|
||||
void clear() { compressed_map._map.clear() ;}
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
class RsTurtleChunkCrcRequestItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleChunkCrcRequestItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_CHUNK_CRC_REQUEST) { setPriorityLevel(QOS_PRIORITY_RS_CHUNK_CRC_REQUEST);}
|
||||
RsTurtleChunkCrcRequestItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual bool shouldStampTunnel() const { return false ; }
|
||||
virtual Direction travelingDirection() const { return DIRECTION_SERVER ; }
|
||||
|
||||
uint32_t chunk_number ; // id of the chunk to CRC.
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
virtual bool serialize(void *data,uint32_t& size) const;
|
||||
virtual uint32_t serial_size() const;
|
||||
void clear() {}
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
class RsTurtleChunkCrcItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleChunkCrcItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_CHUNK_CRC) { setPriorityLevel(QOS_PRIORITY_RS_CHUNK_CRC);}
|
||||
RsTurtleChunkCrcItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual bool shouldStampTunnel() const { return true ; }
|
||||
virtual Direction travelingDirection() const { return DIRECTION_CLIENT ; }
|
||||
@ -129,7 +121,6 @@ class RsTurtleChunkCrcItem: public RsTurtleGenericTunnelItem
|
||||
uint32_t chunk_number ;
|
||||
Sha1CheckSum check_sum ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
virtual bool serialize(void *data,uint32_t& size) const;
|
||||
virtual uint32_t serial_size() const;
|
||||
void clear() { check_sum.clear() ;}
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
@ -32,7 +32,7 @@
|
||||
//*********************
|
||||
|
||||
// A facility func
|
||||
inline void* right_shift_void_pointer(void* p, uint32_t len) {
|
||||
inline void* right_shift_void_pointer(const void* p, uint32_t len) {
|
||||
|
||||
return (void*)( (uint8_t*)p + len);
|
||||
}
|
||||
@ -550,7 +550,7 @@ static bool find_decoded_string(const std::string& in,const std::string& suspici
|
||||
}
|
||||
|
||||
//tested
|
||||
bool GetTlvString(void *data, uint32_t size, uint32_t *offset,
|
||||
bool GetTlvString(const void *data, uint32_t size, uint32_t *offset,
|
||||
uint16_t type, std::string &in)
|
||||
{
|
||||
if (!data)
|
||||
|
@ -275,7 +275,7 @@ uint32_t GetTlvUInt64Size();
|
||||
|
||||
|
||||
bool SetTlvString(void *data, uint32_t size, uint32_t *offset, uint16_t type, std::string out);
|
||||
bool GetTlvString(void *data, uint32_t size, uint32_t *offset, uint16_t type, std::string &in);
|
||||
bool GetTlvString(const void *data, uint32_t size, uint32_t *offset, uint16_t type, std::string &in);
|
||||
uint32_t GetTlvStringSize(const std::string &in);
|
||||
|
||||
#ifdef REMOVED_CODE
|
||||
|
@ -22,7 +22,11 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint3
|
||||
{
|
||||
return setRawUInt8(data,size,&offset,member);
|
||||
}
|
||||
template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint32_t& member)
|
||||
template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint16_t& member)
|
||||
{
|
||||
return setRawUInt16(data,size,&offset,member);
|
||||
}
|
||||
template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint32_t& member)
|
||||
{
|
||||
return setRawUInt32(data,size,&offset,member);
|
||||
}
|
||||
@ -46,7 +50,11 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz
|
||||
{
|
||||
return getRawUInt8(data,size,&offset,&member);
|
||||
}
|
||||
template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint32_t& member)
|
||||
template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint16_t& member)
|
||||
{
|
||||
return getRawUInt16(data,size,&offset,&member);
|
||||
}
|
||||
template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint32_t& member)
|
||||
{
|
||||
return getRawUInt32(data,size,&offset,&member);
|
||||
}
|
||||
@ -67,6 +75,10 @@ template<> uint32_t RsTypeSerializer::serial_size(const uint8_t& /* member*/)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
template<> uint32_t RsTypeSerializer::serial_size(const uint16_t& /* member*/)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
template<> uint32_t RsTypeSerializer::serial_size(const uint32_t& /* member*/)
|
||||
{
|
||||
return 4;
|
||||
@ -224,7 +236,7 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[],uint32_t size,uint32_
|
||||
|
||||
template<> void RsTypeSerializer::print_data(const std::string& n, const RsTypeSerializer::TlvMemBlock_proxy& s)
|
||||
{
|
||||
std::cerr << " [Binary data] " << n << ", length=" << s.second << " data=" << RsUtil::BinToHex(s.first,std::min(50u,s.second)) << ((s.second>50)?"...":"") << std::endl;
|
||||
std::cerr << " [Binary data] " << n << ", length=" << s.second << " data=" << RsUtil::BinToHex((uint8_t*)s.first,std::min(50u,s.second)) << ((s.second>50)?"...":"") << std::endl;
|
||||
}
|
||||
|
||||
//=================================================================================================//
|
||||
|
@ -26,7 +26,11 @@ class RsTypeSerializer
|
||||
public:
|
||||
// This type should be used to pass a parameter to drive the serialisation if needed.
|
||||
|
||||
typedef std::pair<uint8_t*& ,uint32_t&> TlvMemBlock_proxy;
|
||||
struct TlvMemBlock_proxy: public std::pair<void*& ,uint32_t&>
|
||||
{
|
||||
TlvMemBlock_proxy(void *& p,uint32_t& s) : std::pair<void*&,uint32_t&>(p,s) {}
|
||||
TlvMemBlock_proxy(uint8_t*& p,uint32_t& s) : std::pair<void*&,uint32_t&>(*(void**)&p,s) {}
|
||||
};
|
||||
|
||||
//=================================================================================================//
|
||||
// Generic types //
|
||||
@ -135,6 +139,59 @@ class RsTypeSerializer
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//=================================================================================================//
|
||||
// std::list<T> //
|
||||
//=================================================================================================//
|
||||
|
||||
template<typename T>
|
||||
static void serial_process(RsItem::SerializeJob j,SerializeContext& ctx,std::list<T>& v,const std::string& member_name)
|
||||
{
|
||||
switch(j)
|
||||
{
|
||||
case RsItem::SIZE_ESTIMATE:
|
||||
{
|
||||
ctx.mOffset += 4 ;
|
||||
for(typename std::list<T>::iterator it(v.begin());it!=v.end();++it)
|
||||
serial_process(j,ctx,*it ,member_name) ;
|
||||
}
|
||||
break ;
|
||||
|
||||
case RsItem::DESERIALIZE:
|
||||
{ uint32_t n=0 ;
|
||||
serial_process(j,ctx,n,"temporary size") ;
|
||||
|
||||
for(uint32_t i=0;i<n;++i)
|
||||
{
|
||||
T tmp;
|
||||
serial_process<T>(j,ctx,tmp,member_name) ;
|
||||
v.push_back(tmp);
|
||||
}
|
||||
}
|
||||
break ;
|
||||
|
||||
case RsItem::SERIALIZE:
|
||||
{
|
||||
uint32_t n=v.size();
|
||||
serial_process(j,ctx,n,"temporary size") ;
|
||||
for(typename std::list<T>::iterator it(v.begin());it!=v.end();++it)
|
||||
serial_process(j,ctx,*it ,member_name) ;
|
||||
}
|
||||
break ;
|
||||
|
||||
case RsItem::PRINT:
|
||||
{
|
||||
if(v.empty())
|
||||
std::cerr << " Empty list"<< std::endl;
|
||||
else
|
||||
std::cerr << " List of " << v.size() << " elements:" << std::endl;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//=================================================================================================//
|
||||
// t_RsFlags32<> types //
|
||||
//=================================================================================================//
|
||||
|
@ -852,12 +852,14 @@ void p3turtle::handleSearchRequest(RsTurtleSearchRequestItem *item)
|
||||
item->print(std::cerr,0) ;
|
||||
#endif
|
||||
|
||||
if(item->serial_size() > TURTLE_MAX_SEARCH_REQ_ACCEPTED_SERIAL_SIZE)
|
||||
uint32_t item_size = RsTurtleSerialiser().size(item);
|
||||
|
||||
if(item_size > TURTLE_MAX_SEARCH_REQ_ACCEPTED_SERIAL_SIZE)
|
||||
{
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << " Dropping, because the serial size exceeds the accepted limit." << std::endl ;
|
||||
#endif
|
||||
std::cerr << " Caught a turtle search item with arbitrary large size from " << item->PeerId() << " of size " << item->serial_size() << " and depth " << item->depth << ". This is not allowed => dropping." << std::endl;
|
||||
std::cerr << " Caught a turtle search item with arbitrary large size from " << item->PeerId() << " of size " << item_size << " and depth " << item->depth << ". This is not allowed => dropping." << std::endl;
|
||||
return ;
|
||||
}
|
||||
|
||||
@ -1074,7 +1076,7 @@ void p3turtle::routeGenericTunnelItem(RsTurtleGenericTunnelItem *item)
|
||||
if(item->shouldStampTunnel())
|
||||
tunnel.time_stamp = time(NULL) ;
|
||||
|
||||
tunnel.transfered_bytes += static_cast<RsTurtleItem*>(item)->serial_size() ;
|
||||
tunnel.transfered_bytes += RsTurtleSerialiser().size(item);
|
||||
|
||||
if(item->PeerId() == tunnel.local_dst)
|
||||
item->setTravelingDirection(RsTurtleGenericTunnelItem::DIRECTION_CLIENT) ;
|
||||
@ -1100,7 +1102,7 @@ void p3turtle::routeGenericTunnelItem(RsTurtleGenericTunnelItem *item)
|
||||
#endif
|
||||
item->PeerId(tunnel.local_src) ;
|
||||
|
||||
_traffic_info_buffer.unknown_updn_Bps += static_cast<RsTurtleItem*>(item)->serial_size() ;
|
||||
_traffic_info_buffer.unknown_updn_Bps += RsTurtleSerialiser().size(item) ;
|
||||
|
||||
// This has been disabled for compilation reasons. Not sure we actually need it.
|
||||
//
|
||||
@ -1118,7 +1120,7 @@ void p3turtle::routeGenericTunnelItem(RsTurtleGenericTunnelItem *item)
|
||||
#endif
|
||||
item->PeerId(tunnel.local_dst) ;
|
||||
|
||||
_traffic_info_buffer.unknown_updn_Bps += static_cast<RsTurtleItem*>(item)->serial_size() ;
|
||||
_traffic_info_buffer.unknown_updn_Bps += RsTurtleSerialiser().size(item);
|
||||
|
||||
sendItem(item) ;
|
||||
return ;
|
||||
@ -1126,7 +1128,7 @@ void p3turtle::routeGenericTunnelItem(RsTurtleGenericTunnelItem *item)
|
||||
|
||||
// item is for us. Use the locked region to record the data.
|
||||
|
||||
_traffic_info_buffer.data_dn_Bps += item->serial_size() ;
|
||||
_traffic_info_buffer.data_dn_Bps += RsTurtleSerialiser().size(item);
|
||||
}
|
||||
|
||||
// The packet was not forwarded, so it is for us. Let's treat it.
|
||||
@ -1246,7 +1248,7 @@ void p3turtle::sendTurtleData(const RsPeerId& virtual_peer_id,RsTurtleGenericTun
|
||||
|
||||
item->tunnel_id = tunnel_id ; // we should randomly select a tunnel, or something more clever.
|
||||
|
||||
uint32_t ss = item->serial_size() ;
|
||||
uint32_t ss = RsTurtleSerialiser().size(item);
|
||||
|
||||
if(item->shouldStampTunnel())
|
||||
tunnel.time_stamp = time(NULL) ;
|
||||
@ -1380,7 +1382,7 @@ void p3turtle::handleTunnelRequest(RsTurtleOpenTunnelItem *item)
|
||||
|
||||
{
|
||||
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
|
||||
_traffic_info_buffer.tr_dn_Bps += static_cast<RsTurtleItem*>(item)->serial_size() ;
|
||||
_traffic_info_buffer.tr_dn_Bps += RsTurtleSerialiser().size(item);
|
||||
|
||||
float distance_to_maximum = std::min(100.0f,_traffic_info.tr_up_Bps/(float)(TUNNEL_REQUEST_PACKET_SIZE*_max_tr_up_rate)) ;
|
||||
float corrected_distance = pow(distance_to_maximum,DISTANCE_SQUEEZING_POWER) ;
|
||||
@ -1572,7 +1574,7 @@ void p3turtle::handleTunnelRequest(RsTurtleOpenTunnelItem *item)
|
||||
|
||||
{
|
||||
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
|
||||
_traffic_info_buffer.tr_up_Bps += static_cast<RsTurtleItem*>(fwd_item)->serial_size() ;
|
||||
_traffic_info_buffer.tr_up_Bps += RsTurtleSerialiser().size(fwd_item);
|
||||
}
|
||||
|
||||
sendItem(fwd_item) ;
|
||||
|
@ -6,6 +6,8 @@
|
||||
#include "rsturtleitem.h"
|
||||
#include "turtleclientservice.h"
|
||||
|
||||
#include "serialization/rstypeserializer.h"
|
||||
|
||||
//#define P3TURTLE_DEBUG
|
||||
// -----------------------------------------------------------------------------------//
|
||||
// -------------------------------- Serialization. --------------------------------- //
|
||||
@ -16,6 +18,7 @@
|
||||
// ---------------------------------- Packet sizes -----------------------------------//
|
||||
//
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
uint32_t RsTurtleStringSearchRequestItem::serial_size() const
|
||||
{
|
||||
uint32_t s = 0 ;
|
||||
@ -102,19 +105,13 @@ uint32_t RsTurtleGenericDataItem::serial_size() const
|
||||
|
||||
return s ;
|
||||
}
|
||||
#endif
|
||||
//
|
||||
// ---------------------------------- Serialization ----------------------------------//
|
||||
//
|
||||
RsItem *RsTurtleSerialiser::deserialise(void *data, uint32_t *size)
|
||||
RsItem *RsTurtleSerialiser::create_item(uint16_t service,uint8_t item_subtype)
|
||||
{
|
||||
// look what we have...
|
||||
|
||||
/* get the type */
|
||||
uint32_t rstype = getRsItemId(data);
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << "p3turtle: deserialising packet: " << std::endl ;
|
||||
#endif
|
||||
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_TURTLE != getRsItemService(rstype)))
|
||||
if (RS_SERVICE_TYPE_TURTLE != service)
|
||||
{
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << " Wrong type !!" << std::endl ;
|
||||
@ -122,43 +119,38 @@ RsItem *RsTurtleSerialiser::deserialise(void *data, uint32_t *size)
|
||||
return NULL; /* wrong type */
|
||||
}
|
||||
|
||||
#ifndef WINDOWS_SYS
|
||||
try
|
||||
switch(getRsItemSubType(item_subtype))
|
||||
{
|
||||
#endif
|
||||
switch(getRsItemSubType(rstype))
|
||||
{
|
||||
case RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST : return new RsTurtleStringSearchRequestItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST : return new RsTurtleRegExpSearchRequestItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_SEARCH_RESULT : return new RsTurtleSearchResultItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_OPEN_TUNNEL : return new RsTurtleOpenTunnelItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_TUNNEL_OK : return new RsTurtleTunnelOkItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_GENERIC_DATA : return new RsTurtleGenericDataItem(data,*size) ;
|
||||
|
||||
default:
|
||||
break ;
|
||||
}
|
||||
// now try all client services
|
||||
//
|
||||
RsItem *item = NULL ;
|
||||
case RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST : return new RsTurtleStringSearchRequestItem();
|
||||
case RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST : return new RsTurtleRegExpSearchRequestItem();
|
||||
case RS_TURTLE_SUBTYPE_SEARCH_RESULT : return new RsTurtleSearchResultItem();
|
||||
case RS_TURTLE_SUBTYPE_OPEN_TUNNEL : return new RsTurtleOpenTunnelItem();
|
||||
case RS_TURTLE_SUBTYPE_TUNNEL_OK : return new RsTurtleTunnelOkItem();
|
||||
case RS_TURTLE_SUBTYPE_GENERIC_DATA : return new RsTurtleGenericDataItem();
|
||||
|
||||
for(uint32_t i=0;i<_client_services.size();++i)
|
||||
if((item = _client_services[i]->deserialiseItem(data,*size)) != NULL)
|
||||
return item ;
|
||||
|
||||
std::cerr << "Unknown packet type in RsTurtle (not even handled by client services)!" << std::endl ;
|
||||
return NULL ;
|
||||
#ifndef WINDOWS_SYS
|
||||
default:
|
||||
break ;
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception raised: " << e.what() << std::endl ;
|
||||
return NULL ;
|
||||
}
|
||||
#endif
|
||||
// now try all client services
|
||||
//
|
||||
RsItem *item = NULL ;
|
||||
|
||||
for(uint32_t i=0;i<_client_services.size();++i)
|
||||
if((item = _client_services[i]->create_item(service,item_subtype)) != NULL)
|
||||
return item ;
|
||||
|
||||
std::cerr << "Unknown packet type in RsTurtle (not even handled by client services)!" << std::endl ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
void RsTurtleStringSearchRequestItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_VALUE,match_string,"match_string") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,request_id,"request_id") ;
|
||||
RsTypeSerializer::serial_process<uint16_t>(j,ctx,depth ,"depth") ;
|
||||
}
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
bool RsTurtleStringSearchRequestItem::serialize(void *data,uint32_t& pktsize) const
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
@ -242,7 +234,95 @@ bool RsTurtleRegExpSearchRequestItem::serialize(void *data,uint32_t& pktsize) co
|
||||
|
||||
return ok;
|
||||
}
|
||||
#endif
|
||||
|
||||
void RsTurtleRegExpSearchRequestItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,request_id,"request_id") ;
|
||||
RsTypeSerializer::serial_process<uint16_t>(j,ctx,depth,"depth") ;
|
||||
RsTypeSerializer::serial_process(j,ctx,expr,"expr") ;
|
||||
}
|
||||
|
||||
template<> uint32_t RsTypeSerializer::serial_size(const RsRegularExpression::LinearizedExpression& r)
|
||||
{
|
||||
uint32_t s = 0 ;
|
||||
|
||||
s += 4 ; // number of strings
|
||||
|
||||
for(unsigned int i=0;i<r._strings.size();++i)
|
||||
s += GetTlvStringSize(r._strings[i]) ;
|
||||
|
||||
s += 4 ; // number of ints
|
||||
s += 4 * r._ints.size() ;
|
||||
s += 4 ; // number of tokens
|
||||
s += r._tokens.size() ; // uint8_t has size 1
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
template<> bool RsTypeSerializer::deserialize(const uint8_t data[],uint32_t size,uint32_t& offset,RsRegularExpression::LinearizedExpression& expr)
|
||||
{
|
||||
uint32_t saved_offset = offset ;
|
||||
|
||||
uint32_t n =0 ;
|
||||
bool ok = true ;
|
||||
|
||||
ok &= getRawUInt32(data,size,&offset,&n) ;
|
||||
|
||||
if(ok) expr._tokens.resize(n) ;
|
||||
|
||||
for(uint32_t i=0;i<n && ok;++i) ok &= getRawUInt8(data,size,&offset,&expr._tokens[i]) ;
|
||||
|
||||
ok &= getRawUInt32(data,size,&offset,&n) ;
|
||||
|
||||
if(ok) expr._ints.resize(n) ;
|
||||
|
||||
for(uint32_t i=0;i<n && ok;++i) ok &= getRawUInt32(data,size,&offset,&expr._ints[i]) ;
|
||||
|
||||
ok &= getRawUInt32(data,size,&offset,&n);
|
||||
|
||||
if (ok) expr._strings.resize(n);
|
||||
|
||||
for(uint32_t i=0;i<n && ok;++i) ok &= GetTlvString(data, size, &offset, TLV_TYPE_STR_VALUE, expr._strings[i]);
|
||||
|
||||
if(!ok)
|
||||
offset = saved_offset ;
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
template<> bool RsTypeSerializer::serialize(uint8_t data[],uint32_t size,uint32_t& offset,const RsRegularExpression::LinearizedExpression& expr)
|
||||
{
|
||||
uint32_t saved_offset = offset ;
|
||||
|
||||
bool ok = true ;
|
||||
|
||||
ok &= setRawUInt32(data,size,&offset,expr._tokens.size()) ;
|
||||
|
||||
for(unsigned int i=0;i<expr._tokens.size();++i) ok &= setRawUInt8(data,size,&offset,expr._tokens[i]) ;
|
||||
|
||||
ok &= setRawUInt32(data,size,&offset,expr._ints.size()) ;
|
||||
|
||||
for(unsigned int i=0;i<expr._ints.size();++i) ok &= setRawUInt32(data,size,&offset,expr._ints[i]) ;
|
||||
|
||||
ok &= setRawUInt32(data,size,&offset,expr._strings.size()) ;
|
||||
|
||||
for(unsigned int i=0;i<expr._strings.size();++i) ok &= SetTlvString(data, size, &offset, TLV_TYPE_STR_VALUE, expr._strings[i]);
|
||||
|
||||
|
||||
if(!ok)
|
||||
offset = saved_offset ;
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
template<> void RsTypeSerializer::print_data(const std::string& n, const RsRegularExpression::LinearizedExpression& expr)
|
||||
{
|
||||
std::cerr << " [RegExpr ] " << n << ", tokens=" << expr._tokens.size() << " ints=" << expr._ints.size() << " strings=" << expr._strings.size() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
RsTurtleStringSearchRequestItem::RsTurtleStringSearchRequestItem(void *data,uint32_t pktsize)
|
||||
: RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST)
|
||||
{
|
||||
@ -309,6 +389,62 @@ RsTurtleRegExpSearchRequestItem::RsTurtleRegExpSearchRequestItem(void *data,uint
|
||||
throw std::runtime_error("Unknown error while deserializing.") ;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void RsTurtleSearchResultItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,request_id,"request_id") ;
|
||||
RsTypeSerializer::serial_process<uint16_t>(j,ctx,depth ,"depth") ;
|
||||
RsTypeSerializer::serial_process (j,ctx,result ,"result") ;
|
||||
}
|
||||
|
||||
template<> uint32_t RsTypeSerializer::serial_size(const TurtleFileInfo& i)
|
||||
{
|
||||
uint32_t s = 0 ;
|
||||
|
||||
s += 8 ; // size
|
||||
s += i.hash.SIZE_IN_BYTES ;
|
||||
s += GetTlvStringSize(i.name) ;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
template<> bool RsTypeSerializer::deserialize(const uint8_t data[],uint32_t size,uint32_t& offset,TurtleFileInfo& i)
|
||||
{
|
||||
uint32_t saved_offset = offset ;
|
||||
bool ok = true ;
|
||||
|
||||
ok &= getRawUInt64(data, size, &offset, &i.size); // file size
|
||||
ok &= i.hash.deserialise(data, size, offset); // file hash
|
||||
ok &= GetTlvString(data, size, &offset, TLV_TYPE_STR_NAME, i.name); // file name
|
||||
|
||||
if(!ok)
|
||||
offset = saved_offset ;
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
template<> bool RsTypeSerializer::serialize(uint8_t data[],uint32_t size,uint32_t& offset,const TurtleFileInfo& i)
|
||||
{
|
||||
uint32_t saved_offset = offset ;
|
||||
bool ok = true ;
|
||||
|
||||
ok &= setRawUInt64(data, size, &offset, i.size); // file size
|
||||
ok &= i.hash.serialise(data, size, offset); // file hash
|
||||
ok &= SetTlvString(data, size, &offset, TLV_TYPE_STR_NAME, i.name); // file name
|
||||
|
||||
if(!ok)
|
||||
offset = saved_offset ;
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
template<> void RsTypeSerializer::print_data(const std::string& n, const TurtleFileInfo& i)
|
||||
{
|
||||
std::cerr << " [FileInfo ] " << n << " size=" << i.size << " hash=" << i.hash << ", name=" << i.name << std::endl;
|
||||
}
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
|
||||
bool RsTurtleSearchResultItem::serialize(void *data,uint32_t& pktsize) const
|
||||
{
|
||||
@ -394,7 +530,17 @@ RsTurtleSearchResultItem::RsTurtleSearchResultItem(void *data,uint32_t pktsize)
|
||||
throw std::runtime_error("Unknown error while deserializing.") ;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void RsTurtleOpenTunnelItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process (j,ctx,file_hash ,"file_hash") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,request_id ,"request_id") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,partial_tunnel_id,"partial_tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint16_t>(j,ctx,depth ,"depth") ;
|
||||
}
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
bool RsTurtleOpenTunnelItem::serialize(void *data,uint32_t& pktsize) const
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
@ -460,7 +606,15 @@ RsTurtleOpenTunnelItem::RsTurtleOpenTunnelItem(void *data,uint32_t pktsize)
|
||||
throw std::runtime_error("RsTurtleOpenTunnelItem::() unknown error while deserializing.") ;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void RsTurtleTunnelOkItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id ,"tunnel_id") ;
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,request_id,"request_id") ;
|
||||
}
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
bool RsTurtleTunnelOkItem::serialize(void *data,uint32_t& pktsize) const
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
@ -522,7 +676,18 @@ RsTurtleTunnelOkItem::RsTurtleTunnelOkItem(void *data,uint32_t pktsize)
|
||||
throw std::runtime_error("RsTurtleTunnelOkItem::() unknown error while deserializing.") ;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void RsTurtleGenericDataItem::serial_process(SerializeJob j,SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer::serial_process<uint32_t>(j,ctx,tunnel_id ,"tunnel_id") ;
|
||||
|
||||
RsTypeSerializer::TlvMemBlock_proxy prox(data_bytes,data_size) ;
|
||||
|
||||
RsTypeSerializer::serial_process(j,ctx,prox,"data bytes") ;
|
||||
}
|
||||
|
||||
#ifdef TO_REMOVE
|
||||
RsTurtleGenericDataItem::RsTurtleGenericDataItem(void *data,uint32_t pktsize)
|
||||
: RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_GENERIC_DATA)
|
||||
{
|
||||
@ -604,6 +769,7 @@ bool RsTurtleGenericDataItem::serialize(void *data,uint32_t& pktsize) const
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------//
|
||||
// ------------------------------------- IO --------------------------------------- //
|
||||
// -----------------------------------------------------------------------------------//
|
||||
@ -681,3 +847,4 @@ std::ostream& RsTurtleGenericDataItem::print(std::ostream& o, uint16_t)
|
||||
|
||||
return o ;
|
||||
}
|
||||
#endif
|
||||
|
@ -9,6 +9,8 @@
|
||||
#include "serialiser/rsserviceids.h"
|
||||
#include "turtle/turtletypes.h"
|
||||
|
||||
#include "serialization/rsserializer.h"
|
||||
|
||||
const uint8_t RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST = 0x01 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_SEARCH_RESULT = 0x02 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_OPEN_TUNNEL = 0x03 ;
|
||||
@ -17,14 +19,14 @@ const uint8_t RS_TURTLE_SUBTYPE_CLOSE_TUNNEL = 0x05 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_TUNNEL_CLOSED = 0x06 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_REQUEST = 0x07 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_DATA = 0x08 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST = 0x09 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST = 0x09 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_GENERIC_DATA = 0x0a ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_MAP = 0x10 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_MAP_REQUEST = 0x11 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_CRC = 0x12 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_CRC_REQUEST = 0x13 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_CHUNK_CRC = 0x14 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_CHUNK_CRC_REQUEST = 0x15 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_MAP = 0x10 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_MAP_REQUEST = 0x11 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_CRC = 0x12 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_CRC_REQUEST = 0x13 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_CHUNK_CRC = 0x14 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_CHUNK_CRC_REQUEST = 0x15 ;
|
||||
|
||||
/***********************************************************************************/
|
||||
/* Basic Turtle Item Class */
|
||||
@ -34,11 +36,6 @@ class RsTurtleItem: public RsItem
|
||||
{
|
||||
public:
|
||||
RsTurtleItem(uint8_t turtle_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TURTLE,turtle_subtype) {}
|
||||
|
||||
virtual bool serialize(void *data,uint32_t& size) const = 0 ; // Isn't it better that items can serialize themselves ?
|
||||
virtual uint32_t serial_size() const = 0 ; // deserialise is handled using a constructor
|
||||
|
||||
virtual void clear() {}
|
||||
};
|
||||
|
||||
/***********************************************************************************/
|
||||
@ -50,29 +47,26 @@ class RsTurtleSearchResultItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
RsTurtleSearchResultItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_SEARCH_RESULT), request_id(0), depth(0) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_SEARCH_RESULT) ;}
|
||||
RsTurtleSearchResultItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
TurtleSearchRequestId request_id ; // Randomly generated request id.
|
||||
|
||||
uint16_t depth ; // The depth of a search result is obfuscated in this way:
|
||||
// If the actual depth is 1, this field will be 1.
|
||||
// If the actual depth is > 1, this field is a larger arbitrary integer.
|
||||
|
||||
uint16_t depth ; // The depth of a search result is obfuscated in this way:
|
||||
// If the actual depth is 1, this field will be 1.
|
||||
// If the actual depth is > 1, this field is a larger arbitrary integer.
|
||||
std::list<TurtleFileInfo> result ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
void clear() { result.clear() ; }
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) const ;
|
||||
virtual uint32_t serial_size() const ;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
|
||||
};
|
||||
|
||||
class RsTurtleSearchRequestItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
RsTurtleSearchRequestItem(uint32_t subtype) : RsTurtleItem(subtype), request_id(0), depth(0) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_SEARCH_REQUEST) ;}
|
||||
virtual RsTurtleSearchRequestItem *clone() const = 0 ; // used for cloning in routing methods
|
||||
|
||||
virtual RsTurtleSearchRequestItem *clone() const = 0 ; // used for cloning in routing methods
|
||||
virtual void performLocalSearch(std::list<TurtleFileInfo>&) const = 0 ; // abstracts the search method
|
||||
|
||||
uint32_t request_id ; // randomly generated request id.
|
||||
@ -83,34 +77,31 @@ class RsTurtleStringSearchRequestItem: public RsTurtleSearchRequestItem
|
||||
{
|
||||
public:
|
||||
RsTurtleStringSearchRequestItem() : RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST) {}
|
||||
RsTurtleStringSearchRequestItem(void *data,uint32_t size) ;
|
||||
|
||||
std::string match_string ; // string to match
|
||||
|
||||
virtual RsTurtleSearchRequestItem *clone() const { return new RsTurtleStringSearchRequestItem(*this) ; }
|
||||
virtual void performLocalSearch(std::list<TurtleFileInfo>&) const ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
void clear() { match_string.clear() ; }
|
||||
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) const ;
|
||||
virtual uint32_t serial_size() const ;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
class RsTurtleRegExpSearchRequestItem: public RsTurtleSearchRequestItem
|
||||
{
|
||||
public:
|
||||
RsTurtleRegExpSearchRequestItem() : RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST) {}
|
||||
RsTurtleRegExpSearchRequestItem(void *data,uint32_t size) ;
|
||||
|
||||
RsRegularExpression::LinearizedExpression expr ; // Reg Exp in linearised mode
|
||||
|
||||
virtual RsTurtleSearchRequestItem *clone() const { return new RsTurtleRegExpSearchRequestItem(*this) ; }
|
||||
virtual void performLocalSearch(std::list<TurtleFileInfo>&) const ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
void clear() { expr = RsRegularExpression::LinearizedExpression(); }
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) const ;
|
||||
virtual uint32_t serial_size() const ;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
/***********************************************************************************/
|
||||
@ -121,34 +112,28 @@ class RsTurtleOpenTunnelItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
RsTurtleOpenTunnelItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_OPEN_TUNNEL), request_id(0), partial_tunnel_id(0), depth(0) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_OPEN_TUNNEL) ;}
|
||||
RsTurtleOpenTunnelItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
TurtleFileHash file_hash ; // hash to match
|
||||
uint32_t request_id ; // randomly generated request id.
|
||||
uint32_t partial_tunnel_id ; // uncomplete tunnel id. Will be completed at destination.
|
||||
uint16_t depth ; // Used for limiting search depth.
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
void clear() { file_hash.clear() ;}
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) const ;
|
||||
virtual uint32_t serial_size() const ;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
class RsTurtleTunnelOkItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
RsTurtleTunnelOkItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_TUNNEL_OK), tunnel_id(0), request_id(0) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_TUNNEL_OK) ;}
|
||||
RsTurtleTunnelOkItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
uint32_t tunnel_id ; // id of the tunnel. Should be identical for a tunnel between two same peers for the same hash.
|
||||
uint32_t request_id ; // randomly generated request id corresponding to the intial request.
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
void clear() {}
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) const ;
|
||||
virtual uint32_t serial_size() const ;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
/***********************************************************************************/
|
||||
@ -198,8 +183,6 @@ class RsTurtleGenericDataItem: public RsTurtleGenericTunnelItem
|
||||
{
|
||||
public:
|
||||
RsTurtleGenericDataItem() : RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_GENERIC_DATA), data_size(0), data_bytes(0) { setPriorityLevel(QOS_PRIORITY_RS_TURTLE_FILE_REQUEST);}
|
||||
RsTurtleGenericDataItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual ~RsTurtleGenericDataItem() { if(data_bytes != NULL) free(data_bytes) ; }
|
||||
|
||||
virtual bool shouldStampTunnel() const { return true ; }
|
||||
@ -207,30 +190,26 @@ class RsTurtleGenericDataItem: public RsTurtleGenericTunnelItem
|
||||
uint32_t data_size ;
|
||||
void *data_bytes ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
void clear()
|
||||
{
|
||||
free(data_bytes) ;
|
||||
data_bytes = NULL ;
|
||||
data_size = 0;
|
||||
}
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) const ;
|
||||
virtual uint32_t serial_size() const ;
|
||||
void serial_process(SerializeJob j,SerializeContext& ctx);
|
||||
};
|
||||
|
||||
/***********************************************************************************/
|
||||
/* Turtle Serialiser class */
|
||||
/***********************************************************************************/
|
||||
|
||||
class RsTurtleSerialiser: public RsSerialType
|
||||
class RsTurtleSerialiser: public RsSerializer
|
||||
{
|
||||
public:
|
||||
RsTurtleSerialiser() : RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_TURTLE) {}
|
||||
RsTurtleSerialiser() : RsSerializer(RS_SERVICE_TYPE_TURTLE) {}
|
||||
|
||||
virtual uint32_t size (RsItem *item)
|
||||
{
|
||||
return dynamic_cast<RsTurtleItem *>(item)->serial_size() ;
|
||||
}
|
||||
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
|
||||
{
|
||||
return dynamic_cast<RsTurtleItem *>(item)->serialize(data,*size) ;
|
||||
}
|
||||
virtual RsItem *deserialise (void *data, uint32_t *size) ;
|
||||
virtual RsItem *create_item(uint16_t service,uint8_t item_subtype);
|
||||
|
||||
// This is used by the turtle router to add services to its serialiser.
|
||||
// Client services are only used for deserialising, since the serialisation is
|
||||
|
@ -71,12 +71,12 @@ class RsTurtleClientService
|
||||
std::cerr << "!!!!!! Received Data from turtle router, but the client service is not handling it !!!!!!!!!!" << std::endl ;
|
||||
}
|
||||
|
||||
// Method for deserialising specific items of the client service. The
|
||||
// Method for creating specific items of the client service. The
|
||||
// method has a default behavior of not doing anything, since most client
|
||||
// services might only use the generic item already provided by the turtle
|
||||
// router: RsTurtleGenericDataItem
|
||||
|
||||
virtual RsTurtleGenericTunnelItem *deserialiseItem(void */*data*/, uint32_t /*size*/) const { return NULL ; }
|
||||
virtual RsTurtleGenericTunnelItem *create_item(uint16_t service,uint8_t item_type) const { return NULL ; }
|
||||
|
||||
// These methods are called by the turtle router to add/remove virtual peers when tunnels are created/deleted
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user