2018-05-30 21:19:13 +02:00
/*******************************************************************************
* libretroshare / src / turtle : rsturtleitem . h *
* *
* libretroshare : retroshare core library *
* *
* Copyright 2009 - 2018 by Cyril Soler < csoler @ users . sourceforge . net > *
* *
* This program is free software : you can redistribute it and / or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation , either version 3 of the *
* License , or ( at your option ) any later version . *
* *
* This program 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 Lesser General Public License for more details . *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program . If not , see < https : //www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-05-26 21:42:45 +00:00
# pragma once
2017-04-26 11:40:46 +02:00
# include "rsitems/rsserviceids.h"
# include "rsitems/rsitem.h"
# include "rsitems/itempriorities.h"
2009-05-26 21:42:45 +00:00
# include "serialiser/rsserial.h"
# include "serialiser/rstlvbase.h"
# include "serialiser/rsbaseserial.h"
2017-04-26 11:40:46 +02:00
2010-08-06 09:40:23 +00:00
# include "retroshare/rsturtle.h"
# include "retroshare/rsexpr.h"
# include "retroshare/rstypes.h"
2009-05-26 21:42:45 +00:00
# include "turtle/turtletypes.h"
2017-04-30 16:05:37 +02:00
# include "serialiser/rsserializer.h"
2017-04-15 18:46:44 +02:00
2009-08-25 12:04:43 +00:00
const uint8_t RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST = 0x01 ;
2018-05-05 18:41:41 +02:00
const uint8_t RS_TURTLE_SUBTYPE_FT_SEARCH_RESULT = 0x02 ;
2009-08-25 12:04:43 +00:00
const uint8_t RS_TURTLE_SUBTYPE_OPEN_TUNNEL = 0x03 ;
const uint8_t RS_TURTLE_SUBTYPE_TUNNEL_OK = 0x04 ;
const uint8_t RS_TURTLE_SUBTYPE_FILE_REQUEST = 0x07 ;
const uint8_t RS_TURTLE_SUBTYPE_FILE_DATA = 0x08 ;
2017-04-15 18:46:44 +02:00
const uint8_t RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST = 0x09 ;
2013-04-01 21:18:58 +00:00
const uint8_t RS_TURTLE_SUBTYPE_GENERIC_DATA = 0x0a ;
2018-06-06 23:15:29 +02:00
const uint8_t RS_TURTLE_SUBTYPE_GENERIC_SEARCH_REQUEST = 0x0b ;
const uint8_t RS_TURTLE_SUBTYPE_GENERIC_SEARCH_RESULT = 0x0c ;
2017-04-15 18:46:44 +02:00
const uint8_t RS_TURTLE_SUBTYPE_FILE_MAP = 0x10 ;
const uint8_t RS_TURTLE_SUBTYPE_FILE_MAP_REQUEST = 0x11 ;
2018-06-06 23:15:29 +02:00
// const uint8_t RS_TURTLE_SUBTYPE_FILE_CRC = 0x12 ; // unused
// const uint8_t RS_TURTLE_SUBTYPE_FILE_CRC_REQUEST = 0x13 ;
2017-04-15 18:46:44 +02:00
const uint8_t RS_TURTLE_SUBTYPE_CHUNK_CRC = 0x14 ;
const uint8_t RS_TURTLE_SUBTYPE_CHUNK_CRC_REQUEST = 0x15 ;
2009-08-25 12:04:43 +00:00
2018-05-31 15:41:54 +02:00
class TurtleSearchRequestInfo ;
2009-08-25 12:04:43 +00:00
/***********************************************************************************/
/* Basic Turtle Item Class */
/***********************************************************************************/
2009-05-26 21:42:45 +00:00
class RsTurtleItem : public RsItem
{
public :
RsTurtleItem ( uint8_t turtle_subtype ) : RsItem ( RS_PKT_VERSION_SERVICE , RS_SERVICE_TYPE_TURTLE , turtle_subtype ) { }
} ;
2009-08-25 12:04:43 +00:00
/***********************************************************************************/
/* Turtle Search Item classes */
2010-01-02 21:30:19 +00:00
/* Specific packets */
2009-08-25 12:04:43 +00:00
/***********************************************************************************/
2018-05-31 15:41:54 +02:00
// Class hierarchy is
//
// RsTurtleItem
// |
// +---- RsTurtleSearchRequestItem
// | |
// | +---- RsTurtleFileSearchRequestItem
2018-06-16 22:39:35 +02:00
// | | |
// | | +---- RsTurtleStringSearchRequestItem
// | | |
// | | +---- RsTurtleReqExpSearchRequestItem
2018-05-31 15:41:54 +02:00
// | |
2018-06-16 22:39:35 +02:00
// | +---- RsTurtleGenericSearchRequestItem
2018-05-31 15:41:54 +02:00
// |
// +---- RsTurtleSearchResultItem
// |
// +---- RsTurtleFTSearchResultItem
// |
2018-06-16 22:39:35 +02:00
// +---- RsTurtleGenericSearchResultItem
2018-05-31 15:41:54 +02:00
//
2009-05-26 21:42:45 +00:00
2018-05-31 15:41:54 +02:00
class RsTurtleSearchResultItem ;
2009-05-26 21:42:45 +00:00
class RsTurtleSearchRequestItem : public RsTurtleItem
{
public :
2015-06-19 11:55:01 +00:00
RsTurtleSearchRequestItem ( uint32_t subtype ) : RsTurtleItem ( subtype ) , request_id ( 0 ) , depth ( 0 ) { setPriorityLevel ( QOS_PRIORITY_RS_TURTLE_SEARCH_REQUEST ) ; }
2018-05-31 15:41:54 +02:00
virtual ~ RsTurtleSearchRequestItem ( ) { }
2018-05-05 18:41:41 +02:00
2017-04-15 18:46:44 +02:00
virtual RsTurtleSearchRequestItem * clone ( ) const = 0 ; // used for cloning in routing methods
2009-08-25 12:04:43 +00:00
2017-09-15 15:03:06 +03:00
virtual std : : string GetKeywords ( ) = 0 ;
2018-07-05 17:40:06 +02:00
virtual uint16_t serviceId ( ) const = 0 ;
2018-05-31 15:41:54 +02:00
2009-05-26 21:42:45 +00:00
uint32_t request_id ; // randomly generated request id.
uint16_t depth ; // Used for limiting search depth.
2009-08-25 12:04:43 +00:00
} ;
2018-05-31 15:41:54 +02:00
class RsTurtleFileSearchRequestItem : public RsTurtleSearchRequestItem
2018-05-05 18:41:41 +02:00
{
public :
2018-05-31 15:41:54 +02:00
RsTurtleFileSearchRequestItem ( uint32_t subtype ) : RsTurtleSearchRequestItem ( subtype ) { }
virtual ~ RsTurtleFileSearchRequestItem ( ) { }
2009-05-26 21:42:45 +00:00
2018-07-05 17:40:06 +02:00
virtual uint16_t serviceId ( ) const { return RS_SERVICE_TYPE_FILE_TRANSFER ; }
2018-05-31 15:41:54 +02:00
virtual void search ( std : : list < TurtleFileInfo > & ) const = 0 ;
} ;
2009-05-26 21:42:45 +00:00
2018-05-31 15:41:54 +02:00
class RsTurtleStringSearchRequestItem : public RsTurtleFileSearchRequestItem
2009-08-25 12:04:43 +00:00
{
public :
2018-05-31 15:41:54 +02:00
RsTurtleStringSearchRequestItem ( ) : RsTurtleFileSearchRequestItem ( RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST ) { }
virtual ~ RsTurtleStringSearchRequestItem ( ) { }
2018-06-16 22:39:35 +02:00
virtual void search ( std : : list < TurtleFileInfo > & ) const ;
2018-05-31 15:41:54 +02:00
2009-08-25 12:04:43 +00:00
std : : string match_string ; // string to match
2017-09-15 15:03:06 +03:00
std : : string GetKeywords ( ) { return match_string ; }
2018-05-31 15:41:54 +02:00
2009-08-25 12:04:43 +00:00
virtual RsTurtleSearchRequestItem * clone ( ) const { return new RsTurtleStringSearchRequestItem ( * this ) ; }
2009-05-26 21:42:45 +00:00
2017-04-15 18:46:44 +02:00
void clear ( ) { match_string . clear ( ) ; }
2009-08-25 12:04:43 +00:00
protected :
2017-04-26 11:40:46 +02:00
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
2009-08-25 12:04:43 +00:00
} ;
2018-05-31 15:41:54 +02:00
class RsTurtleRegExpSearchRequestItem : public RsTurtleFileSearchRequestItem
2009-08-25 12:04:43 +00:00
{
public :
2018-05-31 15:41:54 +02:00
RsTurtleRegExpSearchRequestItem ( ) : RsTurtleFileSearchRequestItem ( RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST ) { }
virtual ~ RsTurtleRegExpSearchRequestItem ( ) { }
2009-08-25 12:04:43 +00:00
2016-09-13 12:05:22 +02:00
RsRegularExpression : : LinearizedExpression expr ; // Reg Exp in linearised mode
2009-05-26 21:42:45 +00:00
2017-11-14 14:34:22 +03:00
std : : string GetKeywords ( )
{
RsRegularExpression : : Expression * ex = RsRegularExpression : : LinearizedExpression : : toExpr ( expr ) ;
std : : string exs = ex - > toStdString ( ) ;
delete ex ;
return exs ;
}
2009-08-25 12:04:43 +00:00
2018-06-16 22:39:35 +02:00
virtual void search ( std : : list < TurtleFileInfo > & ) const ;
2018-05-31 15:41:54 +02:00
virtual RsTurtleSearchRequestItem * clone ( ) const { return new RsTurtleRegExpSearchRequestItem ( * this ) ; }
2017-04-15 18:46:44 +02:00
void clear ( ) { expr = RsRegularExpression : : LinearizedExpression ( ) ; }
2009-05-26 21:42:45 +00:00
protected :
2017-04-26 11:40:46 +02:00
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
2009-05-26 21:42:45 +00:00
} ;
2018-06-06 23:15:29 +02:00
class RsTurtleGenericSearchRequestItem : public RsTurtleSearchRequestItem
2009-05-26 21:42:45 +00:00
{
public :
2018-06-06 23:15:29 +02:00
RsTurtleGenericSearchRequestItem ( ) : RsTurtleSearchRequestItem ( RS_TURTLE_SUBTYPE_GENERIC_SEARCH_REQUEST ) { }
virtual ~ RsTurtleGenericSearchRequestItem ( ) { clear ( ) ; }
2009-08-25 12:04:43 +00:00
2018-06-06 23:15:29 +02:00
uint16_t service_id ; // service to search
uint32_t search_data_len ;
2018-07-05 17:40:06 +02:00
uint8_t request_type ; // type of request. This is used to limit the number of responses.
2018-06-06 23:15:29 +02:00
unsigned char * search_data ;
2018-05-31 15:41:54 +02:00
2018-07-05 17:40:06 +02:00
std : : string GetKeywords ( ) ;
virtual uint16_t serviceId ( ) const { return service_id ; }
2018-05-31 15:41:54 +02:00
2018-06-06 23:15:29 +02:00
virtual RsTurtleSearchRequestItem * clone ( ) const ;
2018-07-05 17:40:06 +02:00
virtual uint32_t requestType ( ) const { return request_type ; }
2018-06-06 23:15:29 +02:00
void clear ( ) { free ( search_data ) ; search_data = NULL ; search_data_len = 0 ; }
2017-04-15 18:46:44 +02:00
2009-08-25 12:04:43 +00:00
protected :
2017-04-26 11:40:46 +02:00
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
2018-06-02 18:12:27 +02:00
2018-06-06 23:15:29 +02:00
private :
RsTurtleGenericSearchRequestItem ( const RsTurtleGenericSearchRequestItem & ) : RsTurtleSearchRequestItem ( RS_TURTLE_SUBTYPE_GENERIC_SEARCH_REQUEST ) { } // make the object non copi-able.
RsTurtleGenericSearchRequestItem & operator = ( const RsTurtleGenericSearchRequestItem & ) { return * this ; }
2018-06-02 18:12:27 +02:00
} ;
2018-05-31 15:41:54 +02:00
class RsTurtleSearchResultItem : public RsTurtleItem
2009-08-25 12:04:43 +00:00
{
public :
2018-05-31 15:41:54 +02:00
RsTurtleSearchResultItem ( uint8_t subtype ) : RsTurtleItem ( subtype ) , request_id ( 0 ) , depth ( 0 ) { setPriorityLevel ( QOS_PRIORITY_RS_TURTLE_SEARCH_RESULT ) ; }
2009-08-25 12:04:43 +00:00
2018-05-31 15:41:54 +02:00
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.
2009-05-26 21:42:45 +00:00
2018-05-31 15:41:54 +02:00
virtual uint32_t count ( ) const = 0 ;
virtual void pop ( ) = 0 ;
2009-08-25 12:04:43 +00:00
2018-05-31 15:41:54 +02:00
virtual void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) = 0 ;
virtual RsTurtleSearchResultItem * duplicate ( ) const = 0 ;
} ;
class RsTurtleFTSearchResultItem : public RsTurtleSearchResultItem
{
public :
RsTurtleFTSearchResultItem ( ) : RsTurtleSearchResultItem ( RS_TURTLE_SUBTYPE_FT_SEARCH_RESULT ) { }
std : : list < TurtleFileInfo > result ;
void clear ( ) { result . clear ( ) ; }
uint32_t count ( ) const { return result . size ( ) ; }
virtual void pop ( ) { result . pop_back ( ) ; }
virtual RsTurtleSearchResultItem * duplicate ( ) const { return new RsTurtleFTSearchResultItem ( * this ) ; }
protected :
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
} ;
2018-06-06 23:15:29 +02:00
class RsTurtleGenericSearchResultItem : public RsTurtleSearchResultItem
2018-05-31 15:41:54 +02:00
{
public :
2018-06-06 23:15:29 +02:00
RsTurtleGenericSearchResultItem ( ) : RsTurtleSearchResultItem ( RS_TURTLE_SUBTYPE_GENERIC_SEARCH_RESULT ) { }
virtual ~ RsTurtleGenericSearchResultItem ( ) { }
2018-05-31 15:41:54 +02:00
2018-06-06 23:15:29 +02:00
uint32_t count ( ) const { return result_data_len / 50 ; } // This is a blind size estimate. We should probably use the actual size to limit search results.
virtual void pop ( ) { }
2018-06-02 18:12:27 +02:00
2018-06-06 23:15:29 +02:00
unsigned char * result_data ;
uint32_t result_data_len ;
2009-05-26 21:42:45 +00:00
2018-06-06 23:15:29 +02:00
virtual RsTurtleSearchResultItem * duplicate ( ) const ;
void clear ( ) { free ( result_data ) ; result_data = NULL ; result_data_len = 0 ; }
2018-06-02 18:12:27 +02:00
protected :
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
} ;
2018-05-31 15:41:54 +02:00
2009-08-25 12:04:43 +00:00
/***********************************************************************************/
/* Turtle Tunnel Item classes */
/***********************************************************************************/
2009-05-26 21:42:45 +00:00
class RsTurtleOpenTunnelItem : public RsTurtleItem
{
public :
2015-06-19 11:55:01 +00:00
RsTurtleOpenTunnelItem ( ) : RsTurtleItem ( RS_TURTLE_SUBTYPE_OPEN_TUNNEL ) , request_id ( 0 ) , partial_tunnel_id ( 0 ) , depth ( 0 ) { setPriorityLevel ( QOS_PRIORITY_RS_TURTLE_OPEN_TUNNEL ) ; }
2009-05-26 21:42:45 +00:00
2018-06-06 23:15:29 +02:00
TurtleFileHash file_hash ; // hash to match
uint32_t request_id ; // randomly generated request id.
2009-05-26 21:42:45 +00:00
uint32_t partial_tunnel_id ; // uncomplete tunnel id. Will be completed at destination.
2018-06-06 23:15:29 +02:00
uint16_t depth ; // Used for limiting search depth.
2009-05-26 21:42:45 +00:00
2017-04-15 18:46:44 +02:00
void clear ( ) { file_hash . clear ( ) ; }
2009-05-26 21:42:45 +00:00
protected :
2017-04-26 11:40:46 +02:00
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
2009-05-26 21:42:45 +00:00
} ;
class RsTurtleTunnelOkItem : public RsTurtleItem
{
public :
2015-06-19 11:55:01 +00:00
RsTurtleTunnelOkItem ( ) : RsTurtleItem ( RS_TURTLE_SUBTYPE_TUNNEL_OK ) , tunnel_id ( 0 ) , request_id ( 0 ) { setPriorityLevel ( QOS_PRIORITY_RS_TURTLE_TUNNEL_OK ) ; }
2009-05-26 21:42:45 +00:00
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.
2017-04-15 18:46:44 +02:00
void clear ( ) { }
2009-05-26 21:42:45 +00:00
protected :
2017-04-26 11:40:46 +02:00
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
2009-05-26 21:42:45 +00:00
} ;
2010-01-02 21:30:19 +00:00
/***********************************************************************************/
/* Generic turtle packets for tunnels */
/***********************************************************************************/
class RsTurtleGenericTunnelItem : public RsTurtleItem
{
public :
2015-06-19 11:55:01 +00:00
RsTurtleGenericTunnelItem ( uint8_t sub_packet_id ) : RsTurtleItem ( sub_packet_id ) , direction ( 0 ) , tunnel_id ( 0 ) { setPriorityLevel ( QOS_PRIORITY_RS_TURTLE_GENERIC_ITEM ) ; }
2017-03-09 20:59:01 +01:00
virtual ~ RsTurtleGenericTunnelItem ( ) { }
2010-01-02 21:30:19 +00:00
2010-01-11 16:00:42 +00:00
typedef uint32_t Direction ;
static const Direction DIRECTION_CLIENT = 0x001 ;
static const Direction DIRECTION_SERVER = 0x002 ;
2010-01-02 21:30:19 +00:00
/// Does this packet stamps tunnels when it passes through ?
/// This is used for keeping trace weither tunnels are active or not.
2018-05-31 15:41:54 +02:00
2010-01-02 21:30:19 +00:00
virtual bool shouldStampTunnel ( ) const = 0 ;
2018-05-31 15:41:54 +02:00
/// All tunnels derived from RsTurtleGenericTunnelItem should have a tunnel id to
2010-01-02 21:30:19 +00:00
/// indicate which tunnel they are travelling through.
2018-05-31 15:41:54 +02:00
2013-04-06 09:21:01 +00:00
virtual TurtleTunnelId tunnelId ( ) const { return tunnel_id ; }
2010-01-02 21:30:19 +00:00
/// Indicate weither the packet is a client packet (goign back to the
/// client) or a server packet (going to the server. Typically file
/// requests are server packets, whereas file data are client packets.
2018-05-31 15:41:54 +02:00
2013-04-06 09:21:01 +00:00
virtual Direction travelingDirection ( ) const { return direction ; }
virtual void setTravelingDirection ( Direction d ) { direction = d ; }
2013-04-06 13:58:27 +00:00
Direction direction ; // This does not need to be serialised. It's only used by the client services, optionnally,
// and is set by the turtle router according to which direction the item travels.
2018-05-31 15:41:54 +02:00
2013-04-06 13:58:27 +00:00
uint32_t tunnel_id ; // Id of the tunnel to travel through
2010-01-02 21:30:19 +00:00
} ;
2009-08-25 12:04:43 +00:00
/***********************************************************************************/
2013-04-06 09:21:01 +00:00
/* Specific Turtle Transfer items */
2009-08-25 12:04:43 +00:00
/***********************************************************************************/
2009-05-26 21:42:45 +00:00
2013-04-06 13:58:27 +00:00
// This item can be used by any service to pass-on arbitrary data into a tunnel.
2013-04-06 09:21:01 +00:00
//
2013-04-01 21:18:58 +00:00
class RsTurtleGenericDataItem : public RsTurtleGenericTunnelItem
{
public :
2015-06-19 11:55:01 +00:00
RsTurtleGenericDataItem ( ) : RsTurtleGenericTunnelItem ( RS_TURTLE_SUBTYPE_GENERIC_DATA ) , data_size ( 0 ) , data_bytes ( 0 ) { setPriorityLevel ( QOS_PRIORITY_RS_TURTLE_FILE_REQUEST ) ; }
2013-04-06 13:58:27 +00:00
virtual ~ RsTurtleGenericDataItem ( ) { if ( data_bytes ! = NULL ) free ( data_bytes ) ; }
2013-04-01 21:18:58 +00:00
virtual bool shouldStampTunnel ( ) const { return true ; }
2013-04-06 09:21:01 +00:00
uint32_t data_size ;
2013-04-06 13:58:27 +00:00
void * data_bytes ;
2013-04-01 21:18:58 +00:00
2017-04-15 18:46:44 +02:00
void clear ( )
{
free ( data_bytes ) ;
data_bytes = NULL ;
data_size = 0 ;
}
2013-04-06 09:21:01 +00:00
protected :
2017-04-26 11:40:46 +02:00
void serial_process ( RsGenericSerializer : : SerializeJob j , RsGenericSerializer : : SerializeContext & ctx ) ;
2010-07-21 23:14:10 +00:00
} ;
2009-08-25 12:04:43 +00:00
/***********************************************************************************/
/* Turtle Serialiser class */
/***********************************************************************************/
2017-04-24 14:14:34 +02:00
class RsTurtleSerialiser : public RsServiceSerializer
2009-05-26 21:42:45 +00:00
{
public :
2017-04-24 14:14:34 +02:00
RsTurtleSerialiser ( ) : RsServiceSerializer ( RS_SERVICE_TYPE_TURTLE ) { }
2017-04-15 18:46:44 +02:00
2017-04-16 19:59:22 +02:00
virtual RsItem * create_item ( uint16_t service , uint8_t item_subtype ) const ;
2013-04-06 09:21:01 +00:00
// This is used by the turtle router to add services to its serialiser.
// Client services are only used for deserialising, since the serialisation is
// performed using the overloaded virtual functions above.
//
void registerClientService ( RsTurtleClientService * service ) { _client_services . push_back ( service ) ; }
private :
std : : vector < RsTurtleClientService * > _client_services ;
2009-05-26 21:42:45 +00:00
} ;