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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
// - depth // depth of the file. This is here for debug bug will disapear for anonymity.
|
|
|
|
// - peer id // peer id owning the file. This is here for debug bug will disapear for anonymity.
|
|
|
|
// - hash // hash of the file found
|
|
|
|
// - name // name of the file found
|
|
|
|
// - search request id. //
|
|
|
|
//
|
|
|
|
#ifndef MRK_PQI_TURTLE_H
|
|
|
|
#define MRK_PQI_TURTLE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include "pqi/pqinetwork.h"
|
|
|
|
#include "pqi/pqi.h"
|
|
|
|
#include "pqi/pqimonitor.h"
|
|
|
|
#include "services/p3service.h"
|
|
|
|
|
|
|
|
class p3AuthMgr;
|
|
|
|
class p3ConnectMgr;
|
|
|
|
|
2009-03-12 17:07:00 -04:00
|
|
|
const uint8_t RS_TURTLE_SUBTYPE_SEARCH_REQUEST = 0x01 ;
|
|
|
|
const uint8_t RS_TURTLE_SUBTYPE_SEARCH_RESULT = 0x02 ;
|
|
|
|
|
|
|
|
static const int TURTLE_MAX_SEARCH_DEPTH = 6 ;
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
typedef uint32_t TurtleRequestId ;
|
|
|
|
typedef std::string TurtlePeerId ;
|
2009-03-12 16:08:02 -04:00
|
|
|
typedef std::string TurtleFileHash ;
|
|
|
|
typedef std::string TurtleFileName ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
|
|
|
class RsTurtleItem: public RsItem
|
|
|
|
{
|
|
|
|
public:
|
2009-03-12 17:07:00 -04:00
|
|
|
RsTurtleItem(uint8_t turtle_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TURTLE,turtle_subtype) {}
|
|
|
|
|
2009-03-12 16:08:02 -04:00
|
|
|
virtual bool serialize(void *data,uint32_t& size) = 0 ; // isn't it better that items can (de)serialize themselves ?
|
|
|
|
virtual RsItem *deserialize(const void *data,uint32_t size) = 0 ;
|
|
|
|
virtual uint32_t serial_size() const = 0 ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2009-03-12 17:07:00 -04:00
|
|
|
virtual void clear() {}
|
2009-02-28 13:19:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class RsTurtleSearchResultItem: public RsTurtleItem
|
|
|
|
{
|
|
|
|
public:
|
2009-03-12 17:07:00 -04:00
|
|
|
RsTurtleSearchResultItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_SEARCH_RESULT) {}
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
uint16_t depth ;
|
|
|
|
uint8_t peer_id[16]; // peer id. This will eventually be obfuscated in some way.
|
|
|
|
|
|
|
|
TurtleRequestId request_id ; // randomly generated request id.
|
|
|
|
|
2009-03-12 16:08:02 -04:00
|
|
|
std::map<TurtleFileHash,TurtleFileName> result ;
|
2009-03-12 17:07:00 -04:00
|
|
|
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
2009-03-12 16:08:02 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool serialize(void *data,uint32_t& size) ;
|
|
|
|
virtual RsItem *deserialize(const void *data,uint32_t size) ;
|
|
|
|
virtual uint32_t serial_size() const ;
|
2009-02-28 13:19:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class RsTurtleSearchRequestItem: public RsTurtleItem
|
|
|
|
{
|
|
|
|
public:
|
2009-03-12 17:07:00 -04:00
|
|
|
RsTurtleSearchRequestItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_SEARCH_REQUEST) {}
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
std::string match_string ; // string to match
|
|
|
|
uint32_t request_id ; // randomly generated request id.
|
|
|
|
uint16_t depth ; // Used for limiting search depth.
|
2009-03-12 16:08:02 -04:00
|
|
|
|
2009-03-12 17:07:00 -04:00
|
|
|
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
2009-03-12 16:08:02 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool serialize(void *data,uint32_t& size) ;
|
|
|
|
virtual RsItem *deserialize(const void *data,uint32_t size) ;
|
|
|
|
virtual uint32_t serial_size() const ;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Class responsible for serializing/deserializing all turtle items.
|
|
|
|
//
|
|
|
|
class RsTurtleSerialiser: public RsSerialType
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RsTurtleSerialiser() : RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_TURTLE) {}
|
|
|
|
|
|
|
|
virtual uint32_t size (RsItem *item)
|
|
|
|
{
|
|
|
|
return static_cast<RsTurtleItem *>(item)->serial_size() ;
|
|
|
|
}
|
|
|
|
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
|
|
|
|
{
|
|
|
|
return static_cast<RsTurtleItem *>(item)->serialize(data,*size) ;
|
|
|
|
}
|
|
|
|
virtual RsItem * deserialise (void *data, uint32_t *size) ;
|
2009-02-28 13:19:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class TurtleTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TurtlePeerId in ; // where packets come from
|
|
|
|
TurtlePeerId out ; // where packets should go
|
|
|
|
uint32_t time_stamp ; // last time the tunnel was actually used. Used for cleaning old tunnels.
|
|
|
|
};
|
|
|
|
|
|
|
|
class p3turtle: public p3Service, public pqiMonitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
p3turtle(p3AuthMgr *am, p3ConnectMgr *cm);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
TurtleRequestId performSearch(const std::string& string_to_match) ;
|
|
|
|
|
|
|
|
/************* from pqiMonitor *******************/
|
|
|
|
// Informs the turtle router that some peers are (dis)connected. This should initiate digging new tunnels,
|
|
|
|
// and closing other tunnels.
|
|
|
|
//
|
|
|
|
virtual void statusChange(const std::list<pqipeer> &plist);
|
|
|
|
|
|
|
|
/************* from pqiMonitor *******************/
|
|
|
|
|
|
|
|
// Handles incoming and outgoing packets, sort search requests and
|
|
|
|
// forward info upward.
|
|
|
|
int tick();
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline uint32_t generateRandomRequestId() const { return lrand48() ; }
|
2009-03-12 17:07:00 -04:00
|
|
|
void autoWash() ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
|
|
|
/* Network Input */
|
|
|
|
int handleIncoming();
|
2009-03-12 16:08:02 -04:00
|
|
|
int handleOutgoing();
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2009-03-12 17:07:00 -04:00
|
|
|
// Performs a search calling local cache and search structure.
|
|
|
|
void performLocalSearch(const std::string& s,std::map<TurtleFileHash,TurtleFileName>& result) {}
|
|
|
|
|
2009-03-12 16:08:02 -04:00
|
|
|
void handleSearchRequest(RsTurtleSearchRequestItem *item);
|
2009-03-12 17:07:00 -04:00
|
|
|
void handleSearchResult(RsTurtleSearchResultItem *item);
|
|
|
|
|
|
|
|
// returns a search result upwards (possibly to the gui)
|
|
|
|
void returnSearchResult(RsTurtleSearchResultItem *item) ;
|
2009-02-28 13:19:00 -05:00
|
|
|
|
2009-03-12 17:07:00 -04:00
|
|
|
std::map<TurtleRequestId,TurtlePeerId> requests_origins ; // keeps trace of who emmitted a given request
|
|
|
|
std::map<TurtleFileHash,TurtleTunnel> file_tunnels ; // stores adequate tunnels for each file hash.
|
2009-02-28 13:19:00 -05:00
|
|
|
|
|
|
|
p3AuthMgr *mAuthMgr;
|
|
|
|
p3ConnectMgr *mConnMgr;
|
|
|
|
|
2009-03-12 16:08:02 -04:00
|
|
|
time_t _last_clean_time ;
|
|
|
|
|
2009-02-28 13:19:00 -05:00
|
|
|
/* data */
|
|
|
|
RsMutex mTurtleMtx;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|