2018-05-22 16:03:11 -04:00
/*******************************************************************************
* libretroshare / src / ft : ftserver . h *
* *
* libretroshare : retroshare core library *
* *
* Copyright 2008 by Robert Fernie < retroshare @ lunamutt . com > *
* *
* This program is free software : you can redistribute it and / or modify *
2018-05-28 16:03:39 -04:00
* it under the terms of the GNU Lesser General Public License as *
2018-05-22 16:03:11 -04:00
* 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 *
2018-05-28 16:03:39 -04:00
* GNU Lesser General Public License for more details . *
2018-05-22 16:03:11 -04:00
* *
2018-05-28 16:03:39 -04:00
* You should have received a copy of the GNU Lesser General Public License *
2018-05-22 16:03:11 -04:00
* along with this program . If not , see < https : //www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-07-02 09:19:59 -04:00
# ifndef FT_SERVER_HEADER
# define FT_SERVER_HEADER
2009-07-12 09:22:31 -04:00
/*
2008-07-02 09:19:59 -04:00
* ftServer .
*
* Top level File Transfer interface .
* ( replaces old filedexserver )
*
* sets up the whole File Transfer class structure .
* sets up the File Indexing side of cache system too .
*
* provides rsFiles interface for external control .
*
*/
# include <map>
# include <list>
# include <iostream>
2018-08-25 11:58:04 -04:00
# include <functional>
# include <chrono>
2008-07-02 09:19:59 -04:00
2008-08-03 08:45:53 -04:00
# include "ft/ftdata.h"
2013-04-01 17:18:58 -04:00
# include "turtle/turtleclientservice.h"
2013-10-01 04:11:15 -04:00
# include "services/p3service.h"
2010-08-06 05:40:23 -04:00
# include "retroshare/rsfiles.h"
2008-07-02 09:19:59 -04:00
2008-08-03 08:45:53 -04:00
# include "pqi/pqi.h"
2008-07-02 09:19:59 -04:00
# include "pqi/p3cfgmgr.h"
class p3ConnectMgr ;
2016-08-13 04:15:02 -04:00
class p3FileDatabase ;
2008-07-02 09:19:59 -04:00
2008-08-09 13:03:24 -04:00
class ftFiStore ;
class ftFiMonitor ;
2008-07-02 09:19:59 -04:00
2008-07-23 18:01:59 -04:00
class ftController ;
class ftExtraList ;
2008-08-03 08:45:53 -04:00
class ftFileSearch ;
2008-07-02 09:19:59 -04:00
2008-08-03 08:45:53 -04:00
class ftDataMultiplex ;
2009-05-26 17:42:45 -04:00
class p3turtle ;
2008-08-03 08:45:53 -04:00
2011-07-09 14:39:34 -04:00
class p3PeerMgr ;
2014-03-28 23:57:44 -04:00
class p3ServiceControl ;
2016-07-30 15:52:42 -04:00
class p3FileDatabase ;
2011-07-09 14:39:34 -04:00
2017-04-24 08:14:34 -04:00
class ftServer : public p3Service , public RsFiles , public ftDataSend , public RsTurtleClientService , public RsServiceSerializer
2008-07-02 09:19:59 -04:00
{
2016-07-30 15:52:42 -04:00
public :
2008-07-02 09:19:59 -04:00
2016-07-30 15:52:42 -04:00
/***************************************************************/
/******************** Setup ************************************/
/***************************************************************/
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
ftServer ( p3PeerMgr * peerMgr , p3ServiceControl * serviceCtrl ) ;
virtual RsServiceInfo getServiceInfo ( ) ;
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
/* Assign important variables */
void setConfigDirectory ( std : : string path ) ;
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
/* add Config Items (Extra, Controller) */
void addConfigComponents ( p3ConfigMgr * mgr ) ;
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
const RsPeerId & OwnId ( ) ;
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
/* Final Setup (once everything is assigned) */
void SetupFtServer ( ) ;
2016-08-13 04:15:02 -04:00
virtual void connectToTurtleRouter ( p3turtle * p ) ;
virtual void connectToFileDatabase ( p3FileDatabase * b ) ;
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
// Implements RsTurtleClientService
//
2018-06-16 16:39:35 -04:00
uint16_t serviceId ( ) const { return RS_SERVICE_TYPE_FILE_TRANSFER ; }
2016-07-30 15:52:42 -04:00
virtual bool handleTunnelRequest ( const RsFileHash & hash , const RsPeerId & peer_id ) ;
2018-06-06 17:15:29 -04:00
virtual void receiveTurtleData ( const RsTurtleGenericTunnelItem * item , const RsFileHash & hash , const RsPeerId & virtual_peer_id , RsTurtleGenericTunnelItem : : Direction direction ) ;
2018-11-14 14:20:27 -05:00
virtual void ftReceiveSearchResult ( RsTurtleFTSearchResultItem * item ) ; // We dont use TurtleClientService::receiveSearchResult() because of backward compatibility.
2017-04-16 13:59:22 -04:00
virtual RsItem * create_item ( uint16_t service , uint8_t item_type ) const ;
2017-04-24 08:14:34 -04:00
virtual RsServiceSerializer * serializer ( ) { return this ; }
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
void addVirtualPeer ( const TurtleFileHash & , const TurtleVirtualPeerId & , RsTurtleGenericTunnelItem : : Direction dir ) ;
void removeVirtualPeer ( const TurtleFileHash & , const TurtleVirtualPeerId & ) ;
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
/***************************************************************/
/*************** Control Interface *****************************/
/************** (Implements RsFiles) ***************************/
/***************************************************************/
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
void StartupThreads ( ) ;
void StopThreads ( ) ;
2013-04-10 04:52:52 -04:00
2016-07-30 15:52:42 -04:00
// member access
2013-04-01 17:18:58 -04:00
2016-07-30 15:52:42 -04:00
ftDataMultiplex * getMultiplexer ( ) const { return mFtDataplex ; }
ftController * getController ( ) const { return mFtController ; }
2013-10-01 04:11:15 -04:00
2016-07-30 15:52:42 -04:00
/**
2015-03-08 09:46:07 -04:00
* @ see RsFiles : : getFileData
*/
2016-07-30 15:52:42 -04:00
bool getFileData ( const RsFileHash & hash , uint64_t offset , uint32_t & requested_size , uint8_t * data ) ;
/***
* Control of Downloads
* * */
virtual bool alreadyHaveFile ( const RsFileHash & hash , FileInfo & info ) ;
virtual bool FileRequest ( const std : : string & fname , const RsFileHash & hash , uint64_t size , const std : : string & dest , TransferRequestFlags flags , const std : : list < RsPeerId > & srcIds ) ;
virtual bool FileCancel ( const RsFileHash & hash ) ;
virtual bool FileControl ( const RsFileHash & hash , uint32_t flags ) ;
virtual bool FileClearCompleted ( ) ;
virtual bool setDestinationDirectory ( const RsFileHash & hash , const std : : string & new_path ) ;
virtual bool setDestinationName ( const RsFileHash & hash , const std : : string & new_name ) ;
virtual bool setChunkStrategy ( const RsFileHash & hash , FileChunksInfo : : ChunkStrategy s ) ;
virtual void setDefaultChunkStrategy ( FileChunksInfo : : ChunkStrategy ) ;
virtual FileChunksInfo : : ChunkStrategy defaultChunkStrategy ( ) ;
virtual uint32_t freeDiskSpaceLimit ( ) const ;
virtual void setFreeDiskSpaceLimit ( uint32_t size_in_mb ) ;
2016-10-29 12:18:02 -04:00
virtual void setDefaultEncryptionPolicy ( uint32_t policy ) ; // RS_FILE_CTRL_ENCRYPTION_POLICY_STRICT/PERMISSIVE
virtual uint32_t defaultEncryptionPolicy ( ) ;
2017-05-08 16:00:51 -04:00
virtual void setMaxUploadSlotsPerFriend ( uint32_t n ) ;
virtual uint32_t getMaxUploadSlotsPerFriend ( ) ;
2017-06-26 17:35:01 -04:00
virtual void setFilePermDirectDL ( uint32_t perm ) ;
virtual uint32_t filePermDirectDL ( ) ;
2016-07-30 15:52:42 -04:00
2018-08-25 11:58:04 -04:00
/// @see RsFiles
virtual bool turtleSearchRequest (
const std : : string & matchString ,
const std : : function < void ( const std : : list < TurtleFileInfo > & results ) > & multiCallback ,
2018-10-06 19:34:05 -04:00
rstime_t maxWait = 300 ) ;
2018-08-25 11:58:04 -04:00
2018-06-06 17:15:29 -04:00
virtual TurtleSearchRequestId turtleSearch ( const std : : string & string_to_match ) ;
virtual TurtleSearchRequestId turtleSearch ( const RsRegularExpression : : LinearizedExpression & expr ) ;
2016-07-30 15:52:42 -04:00
/***
* Control of Downloads Priority .
* * */
virtual uint32_t getQueueSize ( ) ;
virtual void setQueueSize ( uint32_t s ) ;
virtual bool changeQueuePosition ( const RsFileHash & hash , QueueMove queue_mv ) ;
virtual bool changeDownloadSpeed ( const RsFileHash & hash , int speed ) ;
virtual bool getDownloadSpeed ( const RsFileHash & hash , int & speed ) ;
virtual bool clearDownload ( const RsFileHash & hash ) ;
//virtual void getDwlDetails(std::list<DwlDetails> & details);
/***
* Download / Upload Details
* * */
virtual void FileDownloads ( std : : list < RsFileHash > & hashs ) ;
virtual bool FileUploads ( std : : list < RsFileHash > & hashs ) ;
virtual bool FileDetails ( const RsFileHash & hash , FileSearchFlags hintflags , FileInfo & info ) ;
virtual bool FileDownloadChunksDetails ( const RsFileHash & hash , FileChunksInfo & info ) ;
virtual bool FileUploadChunksDetails ( const RsFileHash & hash , const RsPeerId & peer_id , CompressedChunkMap & map ) ;
2016-10-30 10:33:05 -04:00
virtual bool isEncryptedSource ( const RsPeerId & virtual_peer_id ) ;
2016-07-30 15:52:42 -04:00
/***
* Extra List Access
* * */
virtual bool ExtraFileAdd ( std : : string fname , const RsFileHash & hash , uint64_t size , uint32_t period , TransferRequestFlags flags ) ;
2018-09-27 10:53:08 -04:00
virtual bool ExtraFileRemove ( const RsFileHash & hash ) ;
2019-08-01 09:20:54 -04:00
virtual bool ExtraFileHash ( std : : string localpath , rstime_t period , TransferRequestFlags flags ) ;
2016-07-30 15:52:42 -04:00
virtual bool ExtraFileStatus ( std : : string localpath , FileInfo & info ) ;
virtual bool ExtraFileMove ( std : : string fname , const RsFileHash & hash , uint64_t size , std : : string destpath ) ;
/***
* Directory Listing / Search Interface
* * */
virtual int RequestDirDetails ( void * ref , DirDetails & details , FileSearchFlags flags ) ;
2018-10-09 16:58:17 -04:00
/// @see RsFiles::RequestDirDetails
virtual bool requestDirDetails (
DirDetails & details , std : : uintptr_t handle = 0 ,
FileSearchFlags flags = RS_FILE_HINTS_LOCAL ) ;
2016-09-15 04:41:40 -04:00
virtual bool findChildPointer ( void * ref , int row , void * & result , FileSearchFlags flags ) ;
2016-07-30 15:52:42 -04:00
virtual uint32_t getType ( void * ref , FileSearchFlags flags ) ;
virtual int SearchKeywords ( std : : list < std : : string > keywords , std : : list < DirDetails > & results , FileSearchFlags flags ) ;
virtual int SearchKeywords ( std : : list < std : : string > keywords , std : : list < DirDetails > & results , FileSearchFlags flags , const RsPeerId & peer_id ) ;
2016-09-13 06:05:22 -04:00
virtual int SearchBoolExp ( RsRegularExpression : : Expression * exp , std : : list < DirDetails > & results , FileSearchFlags flags ) ;
virtual int SearchBoolExp ( RsRegularExpression : : Expression * exp , std : : list < DirDetails > & results , FileSearchFlags flags , const RsPeerId & peer_id ) ;
2016-11-11 14:25:11 -05:00
virtual int getSharedDirStatistics ( const RsPeerId & pid , SharedDirStats & stats ) ;
2016-07-30 15:52:42 -04:00
2018-08-16 12:49:36 -04:00
virtual int banFile ( const RsFileHash & real_file_hash , const std : : string & filename , uint64_t file_size ) ;
virtual int unbanFile ( const RsFileHash & real_file_hash ) ;
virtual bool getPrimaryBannedFilesList ( std : : map < RsFileHash , BannedFileEntry > & banned_files ) ;
2018-08-22 15:57:56 -04:00
virtual bool isHashBanned ( const RsFileHash & hash ) ;
2018-08-16 12:49:36 -04:00
2016-07-30 15:52:42 -04:00
/***
* Utility Functions
* * */
virtual bool ConvertSharedFilePath ( std : : string path , std : : string & fullpath ) ;
virtual void ForceDirectoryCheck ( ) ;
virtual void updateSinceGroupPermissionsChanged ( ) ;
virtual bool InDirectoryCheck ( ) ;
virtual bool copyFile ( const std : : string & source , const std : : string & dest ) ;
/***
* Directory Handling
* * */
2016-09-11 11:52:12 -04:00
virtual void requestDirUpdate ( void * ref ) ; // triggers the update of the given reference. Used when browsing.
2018-08-22 19:39:26 -04:00
virtual bool setDownloadDirectory ( const std : : string & path ) ;
virtual bool setPartialsDirectory ( const std : : string & path ) ;
2016-07-30 15:52:42 -04:00
virtual std : : string getDownloadDirectory ( ) ;
virtual std : : string getPartialsDirectory ( ) ;
virtual bool getSharedDirectories ( std : : list < SharedDirInfo > & dirs ) ;
2016-10-31 11:28:26 -04:00
virtual bool setSharedDirectories ( const std : : list < SharedDirInfo > & dirs ) ;
2016-07-30 15:52:42 -04:00
virtual bool addSharedDirectory ( const SharedDirInfo & dir ) ;
virtual bool updateShareFlags ( const SharedDirInfo & dir ) ; // updates the flags. The directory should already exist !
virtual bool removeSharedDirectory ( std : : string dir ) ;
2017-09-10 13:58:57 -04:00
virtual bool getIgnoreLists ( std : : list < std : : string > & ignored_prefixes , std : : list < std : : string > & ignored_suffixes , uint32_t & ignore_flags ) ;
virtual void setIgnoreLists ( const std : : list < std : : string > & ignored_prefixes , const std : : list < std : : string > & ignored_suffixes , uint32_t ignore_flags ) ;
2016-07-30 15:52:42 -04:00
virtual bool getShareDownloadDirectory ( ) ;
virtual bool shareDownloadDirectory ( bool share ) ;
virtual void setWatchPeriod ( int minutes ) ;
virtual int watchPeriod ( ) const ;
2016-09-18 12:34:39 -04:00
virtual void setWatchEnabled ( bool b ) ;
virtual bool watchEnabled ( ) ;
2016-11-24 17:42:56 -05:00
virtual bool followSymLinks ( ) const ;
virtual void setFollowSymLinks ( bool b ) ;
2017-09-10 16:07:36 -04:00
virtual void togglePauseHashingProcess ( ) ;
virtual bool hashingProcessPaused ( ) ;
2016-07-30 15:52:42 -04:00
2017-09-24 11:53:06 -04:00
virtual void setMaxShareDepth ( int depth ) ;
virtual int maxShareDepth ( ) const ;
virtual bool ignoreDuplicates ( ) ;
virtual void setIgnoreDuplicates ( bool ignore ) ;
2018-08-16 12:49:36 -04:00
static bool encryptHash ( const RsFileHash & hash , RsFileHash & hash_of_hash ) ;
2016-07-30 15:52:42 -04:00
/***************************************************************/
/*************** Data Transfer Interface ***********************/
/***************************************************************/
public :
2016-11-02 15:51:42 -04:00
virtual bool activateTunnels ( const RsFileHash & hash , uint32_t default_encryption_policy , TransferRequestFlags flags , bool onoff ) ;
2016-10-29 11:59:03 -04:00
2016-07-30 15:52:42 -04:00
virtual bool sendData ( const RsPeerId & peerId , const RsFileHash & hash , uint64_t size , uint64_t offset , uint32_t chunksize , void * data ) ;
virtual bool sendDataRequest ( const RsPeerId & peerId , const RsFileHash & hash , uint64_t size , uint64_t offset , uint32_t chunksize ) ;
virtual bool sendChunkMapRequest ( const RsPeerId & peer_id , const RsFileHash & hash , bool is_client ) ;
virtual bool sendChunkMap ( const RsPeerId & peer_id , const RsFileHash & hash , const CompressedChunkMap & cmap , bool is_client ) ;
virtual bool sendSingleChunkCRCRequest ( const RsPeerId & peer_id , const RsFileHash & hash , uint32_t chunk_number ) ;
virtual bool sendSingleChunkCRC ( const RsPeerId & peer_id , const RsFileHash & hash , uint32_t chunk_number , const Sha1CheckSum & crc ) ;
2016-10-19 15:30:37 -04:00
static void deriveEncryptionKey ( const RsFileHash & hash , uint8_t * key ) ;
2016-10-19 16:49:51 -04:00
bool encryptItem ( RsTurtleGenericTunnelItem * clear_item , const RsFileHash & hash , RsTurtleGenericDataItem * & encrypted_item ) ;
2018-06-06 17:15:29 -04:00
bool decryptItem ( const RsTurtleGenericDataItem * encrypted_item , const RsFileHash & hash , RsTurtleGenericTunnelItem * & decrypted_item ) ;
2016-10-19 15:30:37 -04:00
2016-07-30 15:52:42 -04:00
/*************** Internal Transfer Fns *************************/
virtual int tick ( ) ;
/* Configuration */
bool addConfiguration ( p3ConfigMgr * cfgmgr ) ;
bool ResumeTransfers ( ) ;
/*************************** p3 Config Overload ********************/
protected :
int handleIncoming ( ) ;
bool handleCacheData ( ) ;
2016-10-24 18:08:27 -04:00
/*!
* \ brief sendTurtleItem
* Sends the given item into a turtle tunnel , possibly encrypting it if the type of tunnel requires it , which is known from the hash itself .
* \ param peerId Peer id to send to ( this is a virtual peer id from turtle service )
* \ param hash hash of the file . If the item needs to be encrypted
* \ param item item to send .
* \ return
* true if everything goes right
*/
bool sendTurtleItem ( const RsPeerId & peerId , const RsFileHash & hash , RsTurtleGenericTunnelItem * item ) ;
// fnds out what is the real hash of encrypted hash hash
bool findRealHash ( const RsFileHash & hash , RsFileHash & real_hash ) ;
2016-10-30 06:36:00 -04:00
bool findEncryptedHash ( const RsPeerId & virtual_peer_id , RsFileHash & encrypted_hash ) ;
2016-10-24 18:08:27 -04:00
2017-05-08 16:00:51 -04:00
bool checkUploadLimit ( const RsPeerId & pid , const RsFileHash & hash ) ;
2016-07-30 15:52:42 -04:00
private :
/**** INTERNAL FUNCTIONS ***/
//virtual int reScanDirs();
//virtual int check_dBUpdate();
private :
/* no need for Mutex protection -
* as each component is protected independently .
*/
p3PeerMgr * mPeerMgr ;
p3ServiceControl * mServiceCtrl ;
p3FileDatabase * mFileDatabase ;
ftController * mFtController ;
ftExtraList * mFtExtra ;
ftDataMultiplex * mFtDataplex ;
p3turtle * mTurtleRouter ;
ftFileSearch * mFtSearch ;
RsMutex srvMutex ;
std : : string mConfigPath ;
std : : string mDownloadPath ;
std : : string mPartialsPath ;
2016-10-24 18:08:27 -04:00
std : : map < RsFileHash , RsFileHash > mEncryptedHashes ; // This map is such that sha1(it->second) = it->first
2016-10-30 06:36:00 -04:00
std : : map < RsPeerId , RsFileHash > mEncryptedPeerIds ; // This map holds the hash to be used with each peer id
2018-10-06 19:34:05 -04:00
std : : map < RsPeerId , std : : map < RsFileHash , rstime_t > > mUploadLimitMap ;
2018-08-25 11:58:04 -04:00
/** Store search callbacks with timeout*/
std : : map <
TurtleRequestId ,
std : : pair <
std : : function < void ( const std : : list < TurtleFileInfo > & results ) > ,
std : : chrono : : system_clock : : time_point >
> mSearchCallbacksMap ;
RsMutex mSearchCallbacksMapMutex ;
/// Cleanup mSearchCallbacksMap
void cleanTimedOutSearches ( ) ;
2008-07-02 09:19:59 -04:00
} ;
# endif