added config target for turtle hoping, pushed router dev a little further.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1074 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2009-03-12 20:08:02 +00:00
parent 32c12542dc
commit 8d08034380
3 changed files with 187 additions and 313 deletions

View File

@ -25,7 +25,7 @@ win32-x-g++ {
QMAKE_CXXFLAGS *= -Wmissing-include-dirs
QMAKE_CC = i586-mingw32msvc-g++
QMAKE_LIB = i586-mingw32msvc-ar
DEFINES *= STATICLIB
DEFINES *= STATICLIB WIN32
INCLUDEPATH *= /usr/i586-mingw32msvc/include ${HOME}/.wine/drive_c/pthreads/include/
}
@ -49,8 +49,8 @@ win32 {
DEFINES *= PQI_USE_XPGP MINIUPNPC_VERSION=10
SSL_DIR = ../../../../openssl-0.9.7g-xpgp-0.1c
UPNPC_DIR = ../../../../miniupnpc-1.0
SSL_DIR=../../../../src/openssl-0.9.7g-xpgp-0.1c
UPNPC_DIR=../../../../src/miniupnpc-1.0
INCLUDEPATH += . $${SSL_DIR}/include $${UPNPC_DIR}
@ -315,3 +315,13 @@ SOURCES = \
util/rsnet.cc \
util/rsprint.cc \
util/rsthreads.cc
# To compile for turtle hopping. I'm using this flag to avoid conflict while developping.
# Just do a
# qmake CONFIG=turtle
turtle {
SOURCES += services/p3turtle.cc
HEADERS += services/p3turtle.h
DEFINES *= TURTLE_HOPPING
}

View File

@ -1,9 +1,9 @@
/*
* libretroshare/src/services: p3disc.cc
* libretroshare/src/services: p3turtle.cc
*
* Services for RetroShare.
*
* Copyright 2004-2008 by Robert Fernie.
* Copyright 2009 by Cyril Soler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -19,17 +19,17 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
* Please report all bugs and problems to "csoler@users.sourceforge.net".
*
*/
#include "rsiface/rsiface.h"
#include "rsiface/rspeers.h"
#include "services/p3disc.h"
#include "pqi/p3authmgr.h"
#include "pqi/p3connmgr.h"
#include "p3turtle.h"
#include <iostream>
#include <errno.h>
@ -48,9 +48,7 @@ const uint8_t RS_TURTLE_SUBTYPE_SEARCH_RESULT = 0x02;
/* DISC FLAGS */
/*****
* #define P3DISC_DEBUG 1
****/
#define P3TURTLE_DEBUG 1
/*********** NOTE ***************
*
@ -64,7 +62,7 @@ const uint8_t RS_TURTLE_SUBTYPE_SEARCH_RESULT = 0x02;
p3turtle::p3turtle(p3AuthMgr *am, p3ConnectMgr *cm) :p3Service(RS_SERVICE_TYPE_TURTLE), mAuthMgr(am), mConnMgr(cm)
{
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
addSerialType(new RsTurtleSerialiser());
}
@ -74,20 +72,51 @@ int p3turtle::tick()
handleIncoming(); // handle incoming packets
handleOutgoing(); // handle outgoing packets
autoclean() ; // clean old/unused tunnels and search requests.
// Clean every 10 sec.
time_t now = time(NULL) ;
if(now > 10+_last_clean_time)
{
autoclean() ; // clean old/unused tunnels and search requests.
_last_clean_time = now ;
}
}
int p3turtle::autoclean()
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
// look for tunnels and stored temportary info that have not been used for a while.
}
TurtleRequestId p3turtle::performSearch(const std::string& string_to_match)
{
// generate a new search id.
TurtleRequestId = generateRandomRequestId() ;
// form a request packet
//
RsTurtleSearchRequestItem *item = new RsTurtleSearchRequestItem ;
item->match_string = string_to_match ;
item->request_id = TurtleRequestId ;
item->depth = 0 ;
// send it
handleSearchRequest(item) ;
delete item ;
}
int p3turtle::handleIncoming()
{
#ifdef DEBUG_TURTLE
#ifdef P3TURTLE_DEBUG
std::cerr << "p3turtle::handleIncoming()";
std::cerr << std::endl;
#endif
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
}
int nhandled = 0;
// While messages read
//
@ -115,48 +144,74 @@ int p3turtle::handleIncoming()
return nhandled;
}
void p3turtle::handleSearchRequest(const RsSearchRequestItem *item)
void p3turtle::handleSearchRequest(RsTurtleSearchRequestItem *item)
{
// take a look at the item:
// - If the item destimation is
#ifdef P3TURTLE_DEBUG
std::cerr << "Received search request: " << std::endl ;
item->print() ;
#endif
// If the item contains an already handled search request, give up. This
// happens when the same search request gets relayed by different peers
//
if(requests_origins.find(item->request_id) != requests_origins.end())
{
#ifdef P3TURTLE_DEBUG
std::cerr << " This is a bouncing request. Ignoring and deleting it." << std::endl ;
#endif
return ;
}
// This is a new request. Let's add it to the request map, and forward it to
// open peers.
requests_origins[item->request_id] = item->peerId() ;
// Perform local search. If something found, forward the search result back.
// If it's not for us, perform a local search. If something found, forward the search result back.
std::map<FileHash,FileName> result ;
performLocalSearch(item->match_string,result) ;
if(!result.empty())
if(item->peerId() != own_peer_id)
{
// do something
#ifdef P3TURTLE_DEBUG
std::cerr << " Request not from us. Performing local search" << std::endl ;
#endif
std::map<TurtleFileHash,FileName> result ;
performLocalSearch(item->match_string,result) ;
// forward item back
RsTurtleSearchResultItem *res_item = new RsTurtleSearchResultItem ;
if(!result.empty())
{
// do something
res_item->depth = 0 ;
res_item->result = result ;
res_item->request_id = item->request_id ;
res_item->peer_id = item->peer_id ; // send back to the same guy
// forward item back
RsTurtleSearchResultItem *res_item = new RsTurtleSearchResultItem ;
sendItem(res_item) ;
// perhaps we should chop search results items into several items of finite size ?
res_item->depth = 0 ;
res_item->result = result ;
res_item->request_id = item->request_id ;
res_item->peer_id = item->peer_id ; // send back to the same guy
#ifdef P3TURTLE_DEBUG
std::cerr << " " << result.size() << " matches found. Sending back to origin." << std::endl ;
#endif
sendItem(res_item) ;
}
}
// If search depth not too large, also forward this search request to all other peers.
//
if(item->depth < TURTLE_MAX_SEARCH_DEPTH)
for(std::map<>::const_iterator it(openned_peers.begin());it!=openned_peers.end();++it)
{
std::list<std::string> onlineIds ;
mConnMgr->getOnlineList(onlineIds);
for(std::map<>::const_iterator it(inlineIds.begin());it!=inlineIds.end();++it)
if(*it != item->peerId())
{
#ifdef P3TURTLE_DEBUG
std::cerr << " Forwarding request to peer = " << *it << std::endl ;
#endif
// Copy current item and modify it.
RsTurtleSearchRequestItem *fwd_item = new RsTurtleSearchRequestItem(item) ;
@ -165,14 +220,22 @@ void p3turtle::handleSearchRequest(const RsSearchRequestItem *item)
sendItem(fwd_item) ;
}
}
#ifdef P3TURTLE_DEBUG
else
std::cout << " Dropping this item, as search depth is " << item->depth << std::endl ;
#endif
}
void p3turtle::handleSearchResult(const RsSearchResultItem *item)
void p3turtle::handleSearchResult(const RsTurtleSearchResultItem *item)
{
// Find who actually sent the corresponding request.
//
std::map<TurtleRequestId,TurtlePeerId>::const_iterator it = requests_origins.find(item->request_id) ;
#ifdef P3TURTLE_DEBUG
std::cerr << "Received search result:" << std::endl ;
item->print() ;
#endif
if(it == requests_origins.end())
{
// This is an error: how could we receive a search result corresponding to a search item we
@ -189,7 +252,10 @@ void p3turtle::handleSearchResult(const RsSearchResultItem *item)
returnSearchResult(item) ; // Yes, so send upward.
else
{ // Nope, so forward it back.
RsSearchResultItem *fwd_item = new RsSearchResultItem(item) ; // copy the item
#ifdef P3TURTLE_DEBUG
std::cerr << " Forwarding result back to " << it->second << std::endl;
#endif
RsTurtleSearchResultItem *fwd_item = new RsTurtleSearchResultItem(item) ; // copy the item
++(fwd_item->depth) ; // increase depth
@ -201,9 +267,41 @@ void p3turtle::handleSearchResult(const RsSearchResultItem *item)
}
}
void RsTurtleSearchRequestItem::print() const
{
std::cout << "Search request:" << std::endl ;
std::cout << " match string: \"" << match_string << "\"" << std::endl ;
std::cout << " Req. Id: " << request_id << std::endl ;
std::cout << " Depth : " << depth << std::endl ;
}
void RsTurtleSearchResultItem::print() const
{
std::cout << "Search result:" << std::endl ;
std::cout << " Peer id: " << peer_id << std::endl ;
std::cout << " Depth : " << depth << std::endl ;
std::cout << " Req. Id: " << request_id << std::endl ;
std::cout << " Files:" << std::endl ;
for(std::map<TurtleFileHash,FileName>::const_iterator it(result.begin());it!=result.end();++it)
std::cout << " " << it->first << " " << it->Second << std::endl ;
}
void p3turtle::returnSearchResult(RsTurtleSearchResultItem *item)
{
// just cout for now, but it should be notified to the gui
std::cerr << "Received result for search request: " << std::endl ;
item->print() ;
}
/************* from pqiMonitor *******************/
void p3turtle::statusChange(const std::list<pqipeer> &plist)
{
// Do we shutdown tunnels whne peers are down, or automatically find a new tunnel ?
// I'll see that later...
#ifdef TO_DO
/* get a list of all online peers */
std::list<std::string> onlineIds;
@ -223,273 +321,3 @@ void p3turtle::statusChange(const std::list<pqipeer> &plist)
#endif
}
#ifdef A_VIRER
void p3disc::respondToPeer(std::string id)
{
/* get a peer lists */
#ifdef P3DISC_DEBUG
std::cerr << "p3disc::respondToPeer() id: " << id;
std::cerr << std::endl;
#endif
std::list<std::string> friendIds;
std::list<std::string> onlineIds;
std::list<std::string>::iterator it;
mConnMgr->getFriendList(friendIds);
mConnMgr->getOnlineList(onlineIds);
/* Check that they have DISC on */
{
/* get details */
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(id, detail))
{
/* major error! */
return;
}
if (detail.visState & RS_VIS_STATE_NODISC)
{
/* don't have DISC enabled */
return;
}
}
/* send them a list of all friend's details */
for(it = friendIds.begin(); it != friendIds.end(); it++)
{
/* get details */
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(*it, detail))
{
/* major error! */
continue;
}
if (!(detail.visState & RS_VIS_STATE_NODISC))
{
sendPeerDetails(id, *it); /* (dest (to), source (cert)) */
}
}
/* send their details to all online peers */
for(it = onlineIds.begin(); it != onlineIds.end(); it++)
{
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(*it, detail))
{
/* major error! */
continue;
}
if (!(detail.visState & RS_VIS_STATE_NODISC))
{
sendPeerDetails(*it, id); /* (dest (to), source (cert)) */
}
}
}
/*************************************************************************************/
/* Output Network Msgs */
/*************************************************************************************/
void p3disc::sendOwnDetails(std::string to)
{
/* setup:
* IP local / external
* availability (TCP LOCAL / EXT, UDP ...)
*/
// Then send message.
{
#ifdef P3DISC_DEBUG
std::ostringstream out;
out << "p3disc::sendOwnDetails()";
out << "Constructing a RsDiscItem Message!" << std::endl;
out << "Sending to: " << to;
std::cerr << out.str() << std::endl;
#endif
}
// Construct a message
RsDiscItem *di = new RsDiscItem();
/* components:
* laddr
* saddr
* contact_tf
* discFlags
*/
peerConnectState detail;
if (!mConnMgr->getOwnNetStatus(detail))
{
/* major error! */
return;
}
// Fill the message
di -> PeerId(to);
di -> laddr = detail.localaddr;
di -> saddr = detail.serveraddr;
di -> contact_tf = 0;
/* construct disc flags */
di -> discFlags = 0;
if (!(detail.visState & RS_VIS_STATE_NODISC))
{
di->discFlags |= P3DISC_FLAGS_USE_DISC;
}
if (!(detail.visState & RS_VIS_STATE_NODHT))
{
di->discFlags |= P3DISC_FLAGS_USE_DHT;
}
if ((detail.netMode & RS_NET_MODE_EXT) ||
(detail.netMode & RS_NET_MODE_UPNP))
{
di->discFlags |= P3DISC_FLAGS_EXTERNAL_ADDR;
}
else if (detail.netMode & RS_NET_MODE_UDP)
{
di->discFlags |= P3DISC_FLAGS_STABLE_UDP;
}
di->discFlags |= P3DISC_FLAGS_OWN_DETAILS;
/* send msg */
sendItem(di);
}
/* (dest (to), source (cert)) */
void p3disc::sendPeerDetails(std::string to, std::string about)
{
/* setup:
* Certificate.
* IP local / external
* availability ...
* last connect (0) if online.
*/
/* send it off */
{
#ifdef P3DISC_DEBUG
std::ostringstream out;
out << "p3disc::sendPeerDetails()";
out << " Sending details of: " << about;
out << " to: " << to << std::endl;
std::cerr << out.str() << std::endl;
#endif
}
peerConnectState detail;
if (!mConnMgr->getFriendNetStatus(about, detail))
{
/* major error! */
return;
}
// Construct a message
RsDiscReply *di = new RsDiscReply();
// Fill the message
// Set Target as input cert.
di -> PeerId(to);
di -> aboutId = about;
// set the server address.
di -> laddr = detail.localaddr;
di -> saddr = detail.serveraddr;
if (detail.state & RS_PEER_S_CONNECTED)
{
di -> contact_tf = 0;
}
else
{
di -> contact_tf = convertTDeltaToTRange(time(NULL) - detail.lastcontact);
}
/* construct disc flags */
di->discFlags = 0;
/* NOTE we should not be sending packet if NODISC is set....
* checked elsewhere... so don't check.
*/
di->discFlags |= P3DISC_FLAGS_USE_DISC;
if (!(detail.visState & RS_VIS_STATE_NODHT))
{
di->discFlags |= P3DISC_FLAGS_USE_DHT;
}
if (detail.netMode & RS_NET_MODE_EXT)
{
di->discFlags |= P3DISC_FLAGS_EXTERNAL_ADDR;
}
else if (detail.netMode & RS_NET_MODE_UDP)
{
di->discFlags |= P3DISC_FLAGS_STABLE_UDP;
}
if (detail.state & RS_PEER_S_CONNECTED)
{
di->discFlags |= P3DISC_FLAGS_PEER_ONLINE;
}
// Add 3rd party trust info
// We look at peers that trust 'to', by looking into 'to''s tigners list. The problem is that
// signers are accessible through their names instead of their id, so there is ambiguity if too peers
// have the same names. @DrBob: that would be cool to save signers using their ids...
//
RsPeerDetails pd ;
std::string name = rsPeers->getPeerName(about) ;
if(rsPeers->getPeerDetails(to,pd))
for(std::list<std::string>::const_iterator it(pd.signers.begin());it!=pd.signers.end();++it)
if(*it == name)
{
di->discFlags |= P3DISC_FLAGS_PEER_TRUSTS_ME;
#ifdef P3DISC_DEBUG
std::cerr << " Peer " << about << "(" << name << ")" << " is trusting " << to << ", sending info." << std::endl ;
#endif
}
uint32_t certLen = 0;
unsigned char **binptr = (unsigned char **) &(di -> certDER.bin_data);
mAuthMgr->SaveCertificateToBinary(about, binptr, &certLen);
#ifdef P3DISC_DEBUG
std::cerr << "Saved certificate to binary in p3discReply. Length=" << certLen << std::endl ;
#endif
if (certLen > 0)
{
di -> certDER.bin_len = certLen;
#ifdef P3DISC_DEBUG
std::cerr << "Cert Encoded(" << certLen << ")" << std::endl;
#endif
}
else
{
#ifdef P3DISC_DEBUG
std::cerr << "Failed to Encode Cert" << std::endl;
#endif
di -> certDER.bin_len = 0;
}
// Send off message
sendItem(di);
#ifdef P3DISC_DEBUG
std::cerr << "Sent DI Message" << std::endl;
#endif
}
#endif

View File

@ -57,14 +57,17 @@ class p3ConnectMgr;
typedef uint32_t TurtleRequestId ;
typedef std::string TurtlePeerId ;
typedef std::string TurtleFileHash ;
typedef std::string TurtleFileName ;
class RsTurtleItem: public RsItem
{
public:
virtual void serialize(void *& data,uint32_t& size) {} // isn't it better that items can (de)serialize themselves ?
virtual void deserialize(const void *data,uint32_t size) {}
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 ;
virtual int size() const ;
virtual void print() const = 0 ;
};
class RsTurtleSearchResultItem: public RsTurtleItem
@ -75,9 +78,13 @@ class RsTurtleSearchResultItem: public RsTurtleItem
TurtleRequestId request_id ; // randomly generated request id.
std::map<FileHash,FileName> result ;
// uint8_t hash[20] ; // sha1 hash of the file found
// std::string filename ; // name of the file found
std::map<TurtleFileHash,TurtleFileName> result ;
virtual void print() const ;
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 RsTurtleSearchRequestItem: public RsTurtleItem
@ -86,6 +93,31 @@ class RsTurtleSearchRequestItem: public RsTurtleItem
std::string match_string ; // string to match
uint32_t request_id ; // randomly generated request id.
uint16_t depth ; // Used for limiting search depth.
virtual void print() const = 0 ;
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) ;
};
class TurtleTunnel
@ -121,18 +153,22 @@ class p3turtle: public p3Service, public pqiMonitor
private:
inline uint32_t generateRandomRequestId() const { return lrand48() ; }
void autoclean() ;
/* Network Input */
int handleIncoming();
int handleOutgoing();
void recvSearchRequest(RsTurtleSearchRequestItem *item);
void handleSearchRequest(RsTurtleSearchRequestItem *item);
std::map<TurtleRequestId,TurtlePeerId> request_origins ; // keeps trace of who emmitted a given request
std::map<FileHash,TurtleTunnel> file_tunnels ; // stores adequate tunnels for each file hash.
std::map<TurtleFileHash,TurtleTunnel> file_tunnels ; // stores adequate tunnels for each file hash.
p3AuthMgr *mAuthMgr;
p3ConnectMgr *mConnMgr;
time_t _last_clean_time ;
/* data */
RsMutex mTurtleMtx;
};