2009-02-28 13:19:00 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services: p3turtle.h
|
|
|
|
*
|
|
|
|
* Services for RetroShare.
|
|
|
|
*
|
2009-03-12 17:07:00 -04:00
|
|
|
* Copyright 2009 by Cyril Soler
|
2009-02-28 13:19:00 -05:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
2009-03-12 17:07:00 -04:00
|
|
|
* Please report all bugs and problems to "csoler@users.sourceforge.net".
|
2009-02-28 13:19:00 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-04-13 16:26:13 -04:00
|
|
|
//====================================== General setup of the router ===================================//
|
|
|
|
//
|
2009-02-28 13:19:00 -05:00
|
|
|
// This class implements the turtle hopping router. It basically serves as
|
|
|
|
// - a cache of turtle tunnels which are the communicating ways between distant peers.
|
|
|
|
// - turtle tunnels are either end-point tunnels, or transitory points, in which case items are just
|
|
|
|
// re-serialized and passed on along the tunnel.
|
|
|
|
// - turtle tunnels are dug on request when calling diggTurtleTunnel(const std::string& hash)
|
|
|
|
// this command lets a trace in each peer along the tunnel of where
|
|
|
|
// packets come from and where they should go. Doing so, once a tunnel is
|
|
|
|
// dug, packets are directly forwarded to the correct peer.
|
|
|
|
// - an entry point for search request from the interface
|
|
|
|
// - search results, as they come back, are forwarded upwards with some additional info:
|
2009-06-13 15:28:47 -04:00
|
|
|
// - depth // depth of the file. setup to 1 for immediate friends and 2 for long distance friends.
|
2009-02-28 13:19:00 -05:00
|
|
|
// - hash // hash of the file found
|
|
|
|
// - name // name of the file found
|
|
|
|
// - search request id. //
|
|
|
|
//
|
2009-04-13 16:26:13 -04:00
|
|
|
// - when downloading:
|
|
|
|
// - for a given hash, a set of starting tunnels is maintained. Transitory
|
|
|
|
// tunnels are also maintained for other hashes as requested by distant
|
|
|
|
// peers.
|
|
|
|
//
|
|
|
|
//============================================= Operations =============================================//
|
|
|
|
//
|
|
|
|
// A download session works as follows:
|
|
|
|
// Initiation:
|
|
|
|
// 1 - the user searches for files (turtle search), and selects one and clicks download.
|
|
|
|
// 2 - In parallel:
|
|
|
|
// - the ft module gets a request, and searches for peers to provide this using its search modules.
|
|
|
|
// - the turtle router is informed that a turtle download will happen with the given hash, so
|
|
|
|
// it initiates tunnels for this hash.
|
|
|
|
// In a loop:
|
|
|
|
// 3 - the ft module asks the hash to the turtle searchModule, and sends file requests to the pqi
|
|
|
|
// interface of this module.
|
|
|
|
// 4 - the turtle pqi interface forwards these requests to the turtle router, which sends them to
|
|
|
|
// the correct peers, selecting randomly among all the possible tunnels for this hash.
|
|
|
|
// 5 - when a file data packet gets back, the turtle router forwards it back to the file transfer module.
|
|
|
|
//
|
|
|
|
//================================ connexion to the file transfer module ===============================//
|
|
|
|
//
|
|
|
|
// The turtle router should provide the ft module with the necessary interface for asking files, and
|
|
|
|
// retreiving data:
|
|
|
|
// - a search module that responds with a given fake peer id for hash request for which it has tunnels.
|
|
|
|
// - a pqi interface to ask for file data
|
|
|
|
// - p3turtle sends back file data packets to the file transfer module
|
|
|
|
//
|
|
|
|
//========================================== Tunnel usage rules ========================================//
|
|
|
|
//
|
2009-06-13 15:28:47 -04:00
|
|
|
// Tunnels should be used according to their capacity. This is an unsolved problem as for now.
|
2009-04-13 16:26:13 -04:00
|
|
|
//
|
|
|
|
//======================================= Tunnel maintenance rules =====================================//
|
|
|
|
//
|
|
|
|
// P3turtle should derive from pqihandler, just as p3disc, so that newly connected peers should trigger
|
2009-06-13 15:28:47 -04:00
|
|
|
// asking for new tunnels, and disconnecting peers should produce a close tunnel packet. To simplify this,
|
|
|
|
// I maintain a time stamp in tunnels, that is updated each time a file data packet travels in the tunnel.
|
|
|
|
// Doing so, if a tunnel is not used for some time, it just disapears. Additional rules apply:
|
2009-04-13 16:26:13 -04:00
|
|
|
//
|
|
|
|
// - when a peer A connects:
|
|
|
|
// - initiate new tunnels for all active file hashes (go through the list of hashes) by
|
|
|
|
// asking to A, for the same hash and the same source. Only report tunnels for which the destination
|
|
|
|
// endpoint is different, which should not happen in fact, because of bouncing gards.
|
|
|
|
//
|
|
|
|
// - when a peer A disconnects.
|
2009-06-13 15:28:47 -04:00
|
|
|
// - do nothing.
|
2009-04-13 16:26:13 -04:00
|
|
|
//
|
|
|
|
// - when receive open tunnel from A
|
|
|
|
// - check whether it's a bouncing request. If yes, give up.
|
|
|
|
// - check hash against local files.
|
|
|
|
// if > 0
|
|
|
|
// return tunnel ok item. No need to go forward, as sub tunnels are not useful.
|
|
|
|
// else
|
|
|
|
// forward request to peers, notting source and hashes.
|
|
|
|
//
|
|
|
|
// - when receive tunnel ok from A
|
|
|
|
// - no need to check whether we already have this tunnel, as bouncing gards prevent this.
|
|
|
|
// - leave a trace for the tunnel, and send (forward) backward.
|
|
|
|
//
|
|
|
|
// Ids management:
|
|
|
|
// - tunnel ids should be identical for requests between 2 same peers for the same file hash.
|
2009-06-13 15:28:47 -04:00
|
|
|
// - tunnel ids should be asymetric
|
|
|
|
// - tunnel requests should never be identical, to allow searching multiple times for the same string.
|
2009-04-13 16:26:13 -04:00
|
|
|
// So:
|
|
|
|
// - when issuing an open tunnel order,
|
|
|
|
// - a random request id is generated and used for packet routing
|
|
|
|
// - a partial tunnel id is build, which is unique to the pair (source,file hash)
|
|
|
|
// - when tunnel_ok is sent back, the tunnel id is completed so that it is unique to the
|
|
|
|
// triplet (source, destination, file hash).
|
|
|
|
//
|
|
|
|
// For these needs, tunnels are represented by:
|
|
|
|
// - their file hash. Each tunnel is only designed for transferring a single and same file.
|
|
|
|
// - their local endpoints id. These are the ids of the peers in direction to the source and destination.
|
|
|
|
// - the tunnel id, which is unique to the triple hash+global source+global destination.
|
|
|
|
// - there is a difference between source and destination in tunnels. The source is the file asker, the
|
|
|
|
// destination is the file provider. This helps sorting tunnels.
|
|
|
|
// - a timestamp, used for cleaning unused tunnels.
|
|
|
|
//
|
|
|
|
// The turtle router has:
|
|
|
|
// - a list of search requests and where to bounce them back.
|
|
|
|
// - a list of tunnel digging requests and where to bounce them, back.
|
|
|
|
// - a list of active file hashes, for which is should constantly maintain tunnels.
|
|
|
|
// - a list of active tunnels, some being transitory, some being endpoints.
|
|
|
|
//
|
|
|
|
// Turtle router entries:
|
|
|
|
// - a function for performing turtle search
|
2009-06-13 15:28:47 -04:00
|
|
|
// - a function for handling tunnels for a given file hash.
|
2009-04-13 16:26:13 -04:00
|
|
|
//
|
|
|
|
// Questions:
|
|
|
|
// - should tunnels be re-used ? nope. The only useful case would be when two peers are exchanging files, which happens quite rarely.
|
2009-12-28 16:11:00 -05:00
|
|
|
//
|
2009-04-13 16:26:13 -04:00
|
|
|
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
#ifndef MRK_PQI_TURTLE_H
|
|
|
|
#define MRK_PQI_TURTLE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2011-04-10 15:22:15 -04:00
|
|
|
#include <set>
|
2009-02-28 13:19:00 -05:00
|
|
|
|
|
|
|
#include "pqi/pqinetwork.h"
|
|
|
|
#include "pqi/pqi.h"
|
|
|
|
#include "pqi/pqimonitor.h"
|
2012-09-23 15:58:08 -04:00
|
|
|
#include "ft/ftcontroller.h"
|
|
|
|
#include "pqi/p3cfgmgr.h"
|
2009-02-28 13:19:00 -05:00
|
|
|
#include "services/p3service.h"
|
2009-05-26 17:42:45 -04:00
|
|
|
#include "ft/ftsearch.h"
|
2010-08-06 05:40:23 -04:00
|
|
|
#include "retroshare/rsturtle.h"
|
2009-05-26 17:42:45 -04:00
|
|
|
#include "rsturtleitem.h"
|
2011-05-26 18:11:06 -04:00
|
|
|
#include "turtlestatistics.h"
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2011-03-15 19:09:31 -04:00
|
|
|
//#define TUNNEL_STATISTICS
|
|
|
|
|
2009-05-26 17:42:45 -04:00
|
|
|
class ftServer ;
|
2011-07-09 14:39:34 -04:00
|
|
|
class p3LinkMgr;
|
2009-05-26 17:42:45 -04:00
|
|
|
class ftDataMultiplex;
|
2009-06-03 14:47:14 -04:00
|
|
|
class RsSerialiser;
|
2009-12-28 16:11:00 -05:00
|
|
|
|
2009-03-12 17:07:00 -04:00
|
|
|
static const int TURTLE_MAX_SEARCH_DEPTH = 6 ;
|
|
|
|
|
2009-04-19 15:59:54 -04:00
|
|
|
// This class is used to keep trace of requests (searches and tunnels).
|
|
|
|
//
|
|
|
|
class TurtleRequestInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TurtlePeerId origin ; // where the request came from.
|
|
|
|
uint32_t time_stamp ; // last time the tunnel was actually used. Used for cleaning old tunnels.
|
2011-04-07 16:16:43 -04:00
|
|
|
int depth ; // depth of the request. Used to optimize tunnel length.
|
2011-04-10 15:22:15 -04:00
|
|
|
std::set<uint32_t> responses; // responses to this request. Useful to avoid spamming tunnel responses.
|
2009-04-19 15:59:54 -04:00
|
|
|
};
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
class TurtleTunnel
|
|
|
|
{
|
|
|
|
public:
|
2010-06-19 08:11:44 -04:00
|
|
|
/* For all tunnels */
|
|
|
|
|
2009-04-13 16:26:13 -04:00
|
|
|
TurtlePeerId local_src ; // where packets come from. Direction to the source.
|
|
|
|
TurtlePeerId local_dst ; // where packets should go. Direction to the destination.
|
|
|
|
uint32_t time_stamp ; // last time the tunnel was actually used. Used for cleaning old tunnels.
|
2010-06-19 08:11:44 -04:00
|
|
|
uint32_t transfered_bytes ; // total bytes transferred in this tunnel.
|
|
|
|
float speed_Bps ; // speed of the traffic through the tunnel
|
|
|
|
|
|
|
|
/* For ending/starting tunnels only. */
|
|
|
|
|
|
|
|
TurtleFileHash hash; // Hash of the file for this tunnel
|
|
|
|
TurtleVirtualPeerId vpid; // Virtual peer id for this tunnel.
|
2009-02-28 13:19:00 -05:00
|
|
|
};
|
|
|
|
|
2009-04-19 15:59:54 -04:00
|
|
|
// This class keeps trace of the activity for the file hashes the turtle router is asked to monitor.
|
|
|
|
//
|
|
|
|
class TurtleFileHashInfo
|
|
|
|
{
|
|
|
|
public:
|
2009-05-26 17:42:45 -04:00
|
|
|
std::vector<TurtleTunnelId> tunnels ; // list of active tunnel ids for this file hash
|
2009-04-19 15:59:54 -04:00
|
|
|
TurtleRequestId last_request ; // last request for the tunnels of this hash
|
2009-05-26 17:42:45 -04:00
|
|
|
|
|
|
|
TurtleFileName name ;
|
2011-03-15 19:09:31 -04:00
|
|
|
time_t last_digg_time ;
|
2009-05-26 17:42:45 -04:00
|
|
|
uint64_t size ;
|
2009-04-19 15:59:54 -04:00
|
|
|
};
|
|
|
|
|
2009-06-03 14:47:14 -04:00
|
|
|
// Subclassing:
|
|
|
|
//
|
|
|
|
// Class | Brings what | Usage
|
|
|
|
// -----------+------------------+------------------------------------------------------
|
|
|
|
// p3Service | sendItem() | handle packet sending/receiving to/from friend peers.
|
|
|
|
// pqiMonitor | configChanged() | handle who's connecting/disconnecting to dig new tunnels
|
|
|
|
// RsTurtle | start/stop file()| brings interface for turtle service
|
|
|
|
// ftSearch | search() | used to allow searching for monitored files.
|
|
|
|
// p3Config | ConfigChanged() | used to load/save .cfg file for turtle variales.
|
|
|
|
// -----------+------------------+------------------------------------------------------
|
|
|
|
//
|
2012-11-01 06:06:12 -04:00
|
|
|
class p3turtle: public p3Service, public RsTurtle, public p3Config
|
2009-02-28 13:19:00 -05:00
|
|
|
{
|
|
|
|
public:
|
2011-07-09 14:39:34 -04:00
|
|
|
p3turtle(p3LinkMgr *lm,ftServer *m);
|
2009-02-28 13:19:00 -05:00
|
|
|
|
|
|
|
// Lauches a search request through the pipes, and immediately returns
|
|
|
|
// the request id, which will be further used by the gui to store results
|
|
|
|
// as they come back.
|
|
|
|
//
|
2009-04-13 16:26:13 -04:00
|
|
|
virtual TurtleSearchRequestId turtleSearch(const std::string& string_to_match) ;
|
2009-08-25 08:04:43 -04:00
|
|
|
virtual TurtleSearchRequestId turtleSearch(const LinearizedExpression& expr) ;
|
2009-03-13 17:14:30 -04:00
|
|
|
|
2009-06-03 14:47:14 -04:00
|
|
|
// Initiates tunnel handling for the given file hash. tunnels. Launches
|
|
|
|
// an exception if an error occurs during the initialization process. The
|
|
|
|
// turtle router itself does not initiate downloads, it only maintains
|
|
|
|
// tunnels for the given hash. The download should be driven by the file
|
|
|
|
// transfer module. Maybe this function can do the whole thing:
|
2009-04-13 16:26:13 -04:00
|
|
|
// - initiate tunnel handling
|
|
|
|
// - send the file request to the file transfer module
|
|
|
|
// - populate the file transfer module with the adequate pqi interface and search module.
|
2009-03-13 17:14:30 -04:00
|
|
|
//
|
2009-06-03 14:47:14 -04:00
|
|
|
// This function should be called in addition to ftServer::FileRequest() so that the turtle router
|
|
|
|
// automatically provide tunnels for the file to download.
|
|
|
|
//
|
|
|
|
virtual void monitorFileTunnels(const std::string& name,const std::string& file_hash,uint64_t size) ;
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// This should be called when canceling a file download, so that the turtle router stops
|
|
|
|
/// handling tunnels for this file.
|
|
|
|
///
|
2009-06-03 14:47:14 -04:00
|
|
|
virtual void stopMonitoringFileTunnels(const std::string& file_hash) ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// get info about tunnels
|
2009-08-16 16:10:53 -04:00
|
|
|
virtual void getInfo(std::vector<std::vector<std::string> >&,
|
|
|
|
std::vector<std::vector<std::string> >&,
|
2011-05-23 17:45:25 -04:00
|
|
|
std::vector<TurtleRequestDisplayInfo >&,
|
|
|
|
std::vector<TurtleRequestDisplayInfo >&) const ;
|
2009-08-16 16:10:53 -04:00
|
|
|
|
2011-05-26 18:11:06 -04:00
|
|
|
virtual void getTrafficStatistics(TurtleTrafficStatisticsInfo& info) const ;
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
/************* from pqiMonitor *******************/
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// This function does many things:
|
|
|
|
/// - It handles incoming and outgoing packets
|
|
|
|
/// - it sorts search requests and forwards search results upward.
|
|
|
|
/// - it cleans unused (tunnel+search) requests.
|
|
|
|
/// - it maintains the pool of tunnels, for each request file hash.
|
|
|
|
///
|
2009-03-13 17:14:30 -04:00
|
|
|
virtual int tick();
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2009-06-03 14:47:14 -04:00
|
|
|
/************* from p3Config *******************/
|
|
|
|
virtual RsSerialiser *setupSerialiser() ;
|
2010-12-18 14:35:07 -05:00
|
|
|
virtual bool saveList(bool& cleanup, std::list<RsItem*>&) ;
|
2012-02-10 17:23:06 -05:00
|
|
|
virtual bool loadList(std::list<RsItem*>& /*load*/) ;
|
2009-06-03 14:47:14 -04:00
|
|
|
|
2009-05-26 17:42:45 -04:00
|
|
|
/************* Communication with ftserver *******************/
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Does the turtle router manages tunnels to this peer ? (this is not a
|
|
|
|
/// real id, but a fake one, that the turtle router is capable of connecting with a tunnel id).
|
2010-01-26 15:40:21 -05:00
|
|
|
virtual bool isTurtlePeer(const std::string& peer_id) const ;
|
2009-05-26 17:42:45 -04:00
|
|
|
|
2012-02-10 17:23:06 -05:00
|
|
|
/// sets/gets the max number of forwarded tunnel requests per second.
|
|
|
|
virtual void setMaxTRForwardRate(int max_tr_up_rate) ;
|
|
|
|
virtual int getMaxTRForwardRate() const ;
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Examines the peer id, finds the turtle tunnel in it, and respond yes if the tunnel is ok and operational.
|
2009-05-26 17:42:45 -04:00
|
|
|
bool isOnline(const std::string& peer_id) const ;
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Returns a unique peer id, corresponding to the given tunnel.
|
2009-05-26 17:42:45 -04:00
|
|
|
std::string getTurtlePeerId(TurtleTunnelId tid) const ;
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// returns the list of virtual peers for all tunnels.
|
2009-05-26 17:42:45 -04:00
|
|
|
void getVirtualPeersList(std::list<pqipeer>& list) ;
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Send a data request into the correct tunnel for the given file hash
|
2009-05-26 17:42:45 -04:00
|
|
|
void sendDataRequest(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t offset, uint32_t chunksize) ;
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Send file data into the correct tunnel for the given file hash
|
2009-05-26 17:42:45 -04:00
|
|
|
void sendFileData(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t baseoffset, uint32_t chunksize, void *data) ;
|
2010-01-11 11:00:42 -05:00
|
|
|
|
|
|
|
/// Send a request for the chunk map of this file to the given peer
|
2010-07-25 15:04:31 -04:00
|
|
|
void sendChunkMapRequest(const std::string& peerId, const std::string& hash,bool is_client) ;
|
2010-01-11 11:00:42 -05:00
|
|
|
|
|
|
|
/// Send a chunk map of this file to the given peer
|
2010-07-25 15:04:31 -04:00
|
|
|
void sendChunkMap(const std::string& peerId, const std::string& hash,const CompressedChunkMap& cmap,bool is_client) ;
|
2010-01-11 11:00:42 -05:00
|
|
|
|
2010-07-21 19:14:10 -04:00
|
|
|
/// Send a request for the crc32 map of this file to the given peer
|
|
|
|
void sendCRC32MapRequest(const std::string& peerId, const std::string& hash) ;
|
|
|
|
|
|
|
|
/// Send a crc32 map of this file to the given peer
|
|
|
|
void sendCRC32Map(const std::string& peerId, const std::string& hash,const CRC32Map& cmap) ;
|
|
|
|
|
2012-03-15 15:55:43 -04:00
|
|
|
/// Send a request for the CRC of a single chunk of this file to the given peer
|
|
|
|
void sendSingleChunkCRCRequest(const std::string& peerId, const std::string& hash,uint32_t chunk_number) ;
|
|
|
|
|
|
|
|
/// Send a crc32 map of this file to the given peer
|
|
|
|
void sendSingleChunkCRC(const std::string& peerId, const std::string& hash,uint32_t chunk_number,const Sha1CheckSum& sum) ;
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
private:
|
2009-04-13 16:26:13 -04:00
|
|
|
//--------------------------- Admin/Helper functions -------------------------//
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Generates a cyphered combination of ownId() and file hash
|
|
|
|
uint32_t generatePersonalFilePrint(const TurtleFileHash&,bool) ;
|
|
|
|
|
|
|
|
/// Generates a random uint32_t number.
|
|
|
|
uint32_t generateRandomRequestId() ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Auto cleaning of unused tunnels, search requests and tunnel requests.
|
|
|
|
void autoWash() ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2009-04-13 16:26:13 -04:00
|
|
|
//------------------------------ Tunnel handling -----------------------------//
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// initiates tunnels from here to any peers having the given file hash
|
|
|
|
TurtleRequestId diggTunnel(const TurtleFileHash& hash) ;
|
|
|
|
|
|
|
|
/// adds info related to a new virtual peer.
|
2010-12-07 16:45:12 -05:00
|
|
|
void locked_addDistantPeer(const TurtleFileHash&, TurtleTunnelId) ;
|
2009-04-13 16:26:13 -04:00
|
|
|
|
2010-06-19 08:11:44 -04:00
|
|
|
/// estimates the speed of the traffic into tunnels.
|
|
|
|
void estimateTunnelSpeeds() ;
|
|
|
|
|
2009-04-13 16:26:13 -04:00
|
|
|
//----------------------------- Routing functions ----------------------------//
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Handle tunnel digging for current file hashes
|
|
|
|
void manageTunnels() ;
|
|
|
|
|
2010-03-12 14:39:23 -05:00
|
|
|
/// Closes a given tunnel. Should be called with mutex set.
|
|
|
|
/// The hashes and peers to remove (by calling
|
|
|
|
/// ftController::removeFileSource() are happended to the supplied vector
|
|
|
|
/// so that they can be removed off the turtle mutex.
|
|
|
|
void locked_closeTunnel(TurtleTunnelId tid,std::vector<std::pair<TurtleFileHash,TurtleVirtualPeerId> >& peers_to_remove) ;
|
2010-01-02 16:30:19 -05:00
|
|
|
|
|
|
|
/// Main routing function
|
|
|
|
int handleIncoming();
|
|
|
|
|
|
|
|
/// Generic routing function for all tunnel packets that derive from RsTurtleGenericTunnelItem
|
|
|
|
void routeGenericTunnelItem(RsTurtleGenericTunnelItem *item) ;
|
2009-03-12 17:07:00 -04:00
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// specific routing functions for handling particular packets.
|
|
|
|
void handleSearchRequest(RsTurtleSearchRequestItem *item);
|
2009-03-12 17:07:00 -04:00
|
|
|
void handleSearchResult(RsTurtleSearchResultItem *item);
|
2009-04-13 16:26:13 -04:00
|
|
|
void handleTunnelRequest(RsTurtleOpenTunnelItem *item);
|
|
|
|
void handleTunnelResult(RsTurtleTunnelOkItem *item);
|
2009-05-26 17:42:45 -04:00
|
|
|
void handleRecvFileRequest(RsTurtleFileRequestItem *item);
|
|
|
|
void handleRecvFileData(RsTurtleFileDataItem *item);
|
2010-01-11 11:00:42 -05:00
|
|
|
void handleRecvFileMapRequest(RsTurtleFileMapRequestItem*);
|
2009-12-28 16:11:00 -05:00
|
|
|
void handleRecvFileMap(RsTurtleFileMapItem*);
|
2010-07-21 19:14:10 -04:00
|
|
|
void handleRecvFileCRC32MapRequest(RsTurtleFileCrcRequestItem*);
|
|
|
|
void handleRecvFileCRC32Map(RsTurtleFileCrcItem*);
|
2012-03-15 15:55:43 -04:00
|
|
|
void handleRecvChunkCRCRequest(RsTurtleChunkCrcRequestItem*);
|
|
|
|
void handleRecvChunkCRC(RsTurtleChunkCrcItem*);
|
2009-03-12 17:07:00 -04:00
|
|
|
|
2009-04-13 16:26:13 -04:00
|
|
|
//------ Functions connecting the turtle router to other components.----------//
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Performs a search calling local cache and search structure.
|
2009-04-13 16:26:13 -04:00
|
|
|
void performLocalSearch(const std::string& match_string,std::list<TurtleFileInfo>& result) ;
|
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// Returns a search result upwards (possibly to the gui)
|
2009-03-12 17:07:00 -04:00
|
|
|
void returnSearchResult(RsTurtleSearchResultItem *item) ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2012-11-01 06:06:12 -04:00
|
|
|
/// Returns true if the file with given hash is hosted locally, and accessible in anonymous mode the supplied peer.
|
|
|
|
virtual bool performLocalHashSearch(const TurtleFileHash& hash,const std::string& client_peer_id,FileInfo& info) ;
|
2009-04-13 16:26:13 -04:00
|
|
|
|
|
|
|
//--------------------------- Local variables --------------------------------//
|
|
|
|
|
2009-03-13 17:14:30 -04:00
|
|
|
/* data */
|
2011-07-09 14:39:34 -04:00
|
|
|
p3LinkMgr *mLinkMgr;
|
2009-05-26 17:42:45 -04:00
|
|
|
ftServer *_ft_server ;
|
|
|
|
ftController *_ft_controller ;
|
2009-03-13 17:14:30 -04:00
|
|
|
|
2009-05-26 17:42:45 -04:00
|
|
|
mutable RsMutex mTurtleMtx;
|
2009-03-13 17:14:30 -04:00
|
|
|
|
2010-01-02 16:30:19 -05:00
|
|
|
/// keeps trace of who emmitted a given search request
|
|
|
|
std::map<TurtleSearchRequestId,TurtleRequestInfo> _search_requests_origins ;
|
|
|
|
|
|
|
|
/// keeps trace of who emmitted a tunnel request
|
|
|
|
std::map<TurtleTunnelRequestId,TurtleRequestInfo> _tunnel_requests_origins ;
|
|
|
|
|
|
|
|
/// stores adequate tunnels for each file hash locally managed
|
|
|
|
std::map<TurtleFileHash,TurtleFileHashInfo> _incoming_file_hashes ;
|
|
|
|
|
|
|
|
/// stores file info for each file we provide.
|
|
|
|
std::map<TurtleFileHash,FileInfo> _outgoing_file_hashes ;
|
|
|
|
|
|
|
|
/// local tunnels, stored by ids (Either transiting or ending).
|
|
|
|
std::map<TurtleTunnelId,TurtleTunnel > _local_tunnels ;
|
|
|
|
|
|
|
|
/// Peers corresponding to each tunnel.
|
|
|
|
std::map<TurtleVirtualPeerId,TurtleTunnelId> _virtual_peers ;
|
|
|
|
|
|
|
|
/// Hashes marked to be deleted.
|
|
|
|
std::vector<TurtleFileHash> _hashes_to_remove ;
|
2009-05-26 17:42:45 -04:00
|
|
|
|
2009-03-12 16:08:02 -04:00
|
|
|
time_t _last_clean_time ;
|
2009-04-19 15:59:54 -04:00
|
|
|
time_t _last_tunnel_management_time ;
|
2009-06-14 14:59:42 -04:00
|
|
|
time_t _last_tunnel_campaign_time ;
|
2010-06-19 08:11:44 -04:00
|
|
|
time_t _last_tunnel_speed_estimate_time ;
|
2009-04-19 15:59:54 -04:00
|
|
|
|
2009-05-26 17:42:45 -04:00
|
|
|
std::list<pqipeer> _online_peers;
|
2010-01-02 16:30:19 -05:00
|
|
|
|
|
|
|
/// used to force digging new tunnels
|
|
|
|
bool _force_digg_new_tunnels ;
|
|
|
|
|
2011-04-07 16:16:43 -04:00
|
|
|
/// used as a bias to introduce randomness in a consistent way, for
|
|
|
|
/// altering tunnel request depths, and tunnel re-routing actions.
|
|
|
|
///
|
|
|
|
uint32_t _random_bias ;
|
|
|
|
|
2011-05-26 18:11:06 -04:00
|
|
|
// Used to collect statistics on turtle traffic.
|
|
|
|
//
|
|
|
|
TurtleTrafficStatisticsInfoOp _traffic_info ; // used for recording speed
|
|
|
|
TurtleTrafficStatisticsInfoOp _traffic_info_buffer ; // used as a buffer to collect bytes
|
|
|
|
|
2012-02-10 17:23:06 -05:00
|
|
|
float _max_tr_up_rate ;
|
|
|
|
|
2009-04-19 15:59:54 -04:00
|
|
|
#ifdef P3TURTLE_DEBUG
|
2010-01-02 16:30:19 -05:00
|
|
|
// debug function
|
2009-04-19 15:59:54 -04:00
|
|
|
void dumpState() ;
|
|
|
|
#endif
|
2011-03-15 19:09:31 -04:00
|
|
|
#ifdef TUNNEL_STATISTICS
|
|
|
|
void TS_dumpState();
|
|
|
|
#endif
|
2009-02-28 13:19:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|