- added functions to add/remove virtual peers in turtle client service

- removed all explicit connections between turtle and FT
- added tunnel and invitation management in p3ChatServer (still needs PGP encryption)



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-GenericTunneling@6300 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2013-04-10 08:52:52 +00:00
parent e9a51455eb
commit f12ad9583d
8 changed files with 191 additions and 79 deletions

View File

@ -488,6 +488,15 @@ RsTurtleGenericTunnelItem *ftServer::deserialiseItem(void *data,uint32_t size) c
}
}
void ftServer::addVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id)
{
mFtController->addFileSource(hash,virtual_peer_id) ;
}
void ftServer::removeVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id)
{
mFtController->removeFileSource(hash,virtual_peer_id) ;
}
bool ftServer::handleTunnelRequest(const std::string& hash,const std::string& peer_id,std::string& description_info_string)
{
FileInfo info ;

View File

@ -118,6 +118,9 @@ class ftServer: public RsFiles, public ftDataSend, public RsTurtleClientService,
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const std::string& hash,const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ;
virtual RsTurtleGenericTunnelItem *deserialiseItem(void *data,uint32_t size) const ;
void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
void removeVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
/***************************************************************/
/*************** Control Interface *****************************/
/************** (Implements RsFiles) ***************************/

View File

@ -2230,7 +2230,7 @@ int RsServer::StartupRetroShare()
mConnMgr->setP3tunnel(tn);
#endif
p3turtle *tr = new p3turtle(mLinkMgr,ftserver) ;
p3turtle *tr = new p3turtle(mLinkMgr) ;
rsTurtle = tr ;
pqih -> addService(tr);

View File

@ -24,6 +24,8 @@
*/
#include <math.h>
#include "openssl/rand.h"
#include "pgp/rscertificate.h"
#include "util/rsdir.h"
#include "util/rsaes.h"
#include "util/rsrandom.h"
@ -54,6 +56,8 @@ static const time_t MAX_DELAY_BETWEEN_LOBBY_KEEP_ALIVE = 120 ; // send keep al
static const time_t MAX_KEEP_PUBLIC_LOBBY_RECORD = 60 ; // keep inactive lobbies records for 60 secs max.
static const time_t MIN_DELAY_BETWEEN_PUBLIC_LOBBY_REQ = 20 ; // don't ask for lobby list more than once every 30 secs.
static const time_t DISTANT_CHAT_CLEANING_PERIOD = 60 ; // don't ask for lobby list more than once every 30 secs.
p3ChatService::p3ChatService(p3LinkMgr *lm, p3HistoryMgr *historyMgr)
:p3Service(RS_SERVICE_TYPE_CHAT), p3Config(CONFIG_TYPE_CHAT), mChatMtx("p3ChatService"), mLinkMgr(lm) , mHistoryMgr(historyMgr)
{
@ -81,15 +85,21 @@ int p3ChatService::tick()
if(receivedItems())
receiveChatQueue();
static time_t last_clean_time = 0 ;
static time_t last_clean_time_lobby = 0 ;
static time_t last_clean_time_dchat = 0 ;
time_t now = time(NULL) ;
if(last_clean_time + LOBBY_CACHE_CLEANING_PERIOD < now)
if(last_clean_time_lobby + LOBBY_CACHE_CLEANING_PERIOD < now)
{
cleanLobbyCaches() ;
last_clean_time = now ;
last_clean_time_lobby = now ;
}
if(last_clean_time_dchat + DISTANT_CHAT_CLEANING_PERIOD < now)
{
cleanDistantChatInvites() ;
last_clean_time_dchat = now ;
}
return 0;
}
@ -2752,7 +2762,9 @@ void p3ChatService::cleanLobbyCaches()
bool p3ChatService::handleTunnelRequest(const std::string& hash,const std::string& peer_id,std::string& description_info_string)
{
std::map<TurtleFileHash,DistantChatInvite>::const_iterator it = _distant_chat_invites.find(hash) ;
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
std::map<TurtleFileHash,DistantChatInvite>::iterator it = _distant_chat_invites.find(hash) ;
std::cerr << "p3ChatService::handleTunnelRequest: received tunnel request for hash " << hash << std::endl;
@ -2763,6 +2775,39 @@ bool p3ChatService::handleTunnelRequest(const std::string& hash,const std::strin
return true ;
}
void p3ChatService::addVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id)
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
std::map<TurtleFileHash,DistantChatInvite>::iterator it = _distant_chat_invites.find(hash) ;
if(it == _distant_chat_invites.end())
{
std::cerr << "(EE) Cannot add virtual peer for hash " << hash << ": no chat invite found for that hash." << std::endl;
return ;
}
memcpy(_distant_chat_peers[virtual_peer_id].aes_key,it->second.aes_key,8) ;
time_t now = time(NULL) ;
_distant_chat_peers[virtual_peer_id].last_contact = now ;
it->second.last_hit_time = now ;
}
void p3ChatService::removeVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id)
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
std::map<std::string,DistantChatPeerInfo>::iterator it = _distant_chat_peers.find(virtual_peer_id) ;
if(it == _distant_chat_peers.end())
{
std::cerr << "(EE) Cannot remove virtual peer " << virtual_peer_id << ": not found in chat list!!" << std::endl;
return ;
}
_distant_chat_peers.erase(it) ;
}
void p3ChatService::receiveTurtleData( RsTurtleGenericTunnelItem *gitem,const std::string& hash,
const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction)
{
@ -2892,4 +2937,77 @@ void p3ChatService::sendTurtleData(RsChatItem *item, const std::string& virtual_
mTurtle->sendTurtleData(virtual_peer_id,gitem) ;
}
bool p3ChatService::createDistantChatInvite(PGPIdType pgp_id,time_t time_of_validity,TurtleFileHash& hash)
{
// create the invite
time_t now = time(NULL) ;
DistantChatInvite invite ;
invite.time_of_validity = now + time_of_validity ;
invite.time_of_creation = now ;
invite.last_hit_time = now ;
RAND_bytes( (unsigned char *)&invite.aes_key[0],16 ) ; // generate a random AES encryption key
// Create a random hash for that invite.
//
unsigned char hash_bytes[16] ;
RAND_bytes( hash_bytes, 16) ;
hash = SSLIdType(hash_bytes).toStdString() ;
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
_distant_chat_invites[hash] = invite ;
}
std::cerr << "Created new distant chat invite: " << std::endl;
std::cerr << " creation time stamp = " << invite.time_of_creation << std::endl;
std::cerr << " validity time stamp = " << invite.time_of_validity << std::endl;
std::cerr << " hash = " ;
std::cerr << " encryption key = " ;
static const char outl[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' } ;
for(uint32_t j = 0; j < 16; j++) { std::cerr << outl[ (invite.aes_key[j]>>4) ] ; std::cerr << outl[ invite.aes_key[j] & 0xf ] ; }
std::cerr << std::endl;
return true ;
}
void p3ChatService::cleanDistantChatInvites()
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
time_t now = time(NULL) ;
std::cerr << "p3ChatService::cleanDistantChatInvites: " << std::endl;
for(std::map<TurtleFileHash,DistantChatInvite>::iterator it(_distant_chat_invites.begin());it!=_distant_chat_invites.end(); )
if(it->second.time_of_validity < now)
{
std::cerr << " Removing hash " << it->first << std::endl;
std::map<TurtleFileHash,DistantChatInvite>::iterator tmp(it) ;
++it ;
_distant_chat_invites.erase(tmp) ;
}
else
{
++it ;
std::cerr << " Keeping hash " << it->first << std::endl;
}
}

View File

@ -303,11 +303,16 @@ class p3ChatService: public p3Service, public p3Config, public pqiMonitor, publi
// Members related to anonymous distant chat. //
// ===========================================================//
public:
void connectToTurtleRouter(p3turtle *) ;
private:
struct DistantChatInvite
{
unsigned char aes_key[16] ;
time_t validity_time_stamp ;
std::string virtual_peer_id ;
time_t time_of_validity ;
time_t time_of_creation ;
time_t last_hit_time ;
};
struct DistantChatPeerInfo
{
@ -333,6 +338,8 @@ class p3ChatService: public p3Service, public p3Config, public pqiMonitor, publi
virtual bool handleTunnelRequest(const std::string& hash,const std::string& peer_id,std::string& description_info_string) ;
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const std::string& hash,const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ;
void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
void removeVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
// Utility functions
@ -340,8 +347,6 @@ class p3ChatService: public p3Service, public p3Config, public pqiMonitor, publi
void sendTurtleData(RsChatItem *,const std::string& virtual_peer_id) ;
p3turtle *mTurtle ;
public:
void connectToTurtleRouter(p3turtle *) ;
};
class p3ChatService::StateStringInfo

View File

@ -99,7 +99,7 @@ static const int MAX_TR_FORWARD_PER_SEC_UPPER_LIMIT = 30 ;
static const int MAX_TR_FORWARD_PER_SEC_LOWER_LIMIT = 10 ;
static const int DISTANCE_SQUEEZING_POWER = 8 ;
p3turtle::p3turtle(p3LinkMgr *lm,ftServer *fs)
p3turtle::p3turtle(p3LinkMgr *lm)
:p3Service(RS_SERVICE_TYPE_TURTLE), p3Config(CONFIG_TYPE_TURTLE), mLinkMgr(lm), mTurtleMtx("p3turtle")
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
@ -107,9 +107,6 @@ p3turtle::p3turtle(p3LinkMgr *lm,ftServer *fs)
_turtle_routing_enabled = true ;
_turtle_routing_session_enabled = true;
_ft_server = fs ;
_ft_controller = fs->getController() ;
_random_bias = RSRandom::random_u32() ;
_serialiser = new RsTurtleSerialiser() ;
@ -261,31 +258,6 @@ int p3turtle::tick()
// -----------------------------------------------------------------------------------//
//
#ifdef TO_REMOVE
// This method handles peer connexion/deconnexion
// If A connects, new tunnels should be initiated from A
// If A disconnects, the tunnels passed through A should be closed.
//
void p3turtle::statusChange(const std::list<pqipeer> &plist) // derived from pqiMonitor
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
// We actually do not shut down tunnels when peers get down: Tunnels that
// are not working properly get automatically removed after some time.
// save the list of active peers. This is useful for notifying the ftContoller
_online_peers = plist ;
std::cerr << "p3turtle: status change triggered. Saving list of " << plist.size() << " peers." << std::endl ;
/* if any have switched to 'connected' then we force digging new tunnels */
for(std::list<pqipeer>::const_iterator pit = plist.begin(); pit != plist.end(); pit++)
if ((pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_CONNECTED))
_force_digg_new_tunnels = true ;
}
#endif
// adds a virtual peer to the list that is communicated ot ftController.
//
void p3turtle::locked_addDistantPeer(const TurtleFileHash&,TurtleTunnelId tid)
@ -520,11 +492,25 @@ void p3turtle::autoWash()
// File hashes can only be removed by calling the 'stopMonitoringFileTunnels()' command.
// All calls to _ft_controller are done off-mutex, to avoir cross-lock
if(_ft_controller != NULL)
for(uint32_t i=0;i<peers_to_remove.size();++i)
_ft_controller->removeFileSource(peers_to_remove[i].first,peers_to_remove[i].second) ;
for(uint32_t i=0;i<peers_to_remove.size();++i)
{
RsTurtleClientService *service = NULL ;
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
std::map<TurtleFileHash,TurtleHashInfo>::iterator it(_incoming_file_hashes.find(peers_to_remove[i].first)) ;
if(it != _incoming_file_hashes.end())
service = it->second.service ;
else
std::cerr << "p3turtle::autowash(): ERROR. No service associated to hash " << peers_to_remove[i].first << ": this is super weird." << std::endl;
}
// All calls to services are done off-mutex, to avoir cross-lock
//
if(service != NULL)
service->removeVirtualPeer(peers_to_remove[i].first,peers_to_remove[i].second) ;
}
}
void p3turtle::locked_closeTunnel(TurtleTunnelId tid,std::vector<std::pair<TurtleFileHash,TurtleVirtualPeerId> >& sources_to_remove)
@ -777,8 +763,6 @@ int p3turtle::handleIncoming()
case RS_TURTLE_SUBTYPE_TUNNEL_OK : handleTunnelResult(dynamic_cast<RsTurtleTunnelOkItem *>(item)) ;
break ;
case RS_TURTLE_SUBTYPE_GENERIC_DATA : handleRecvGenericDataItem(dynamic_cast<RsTurtleGenericDataItem *>(item)) ;
break ;
default:
std::cerr << "p3turtle::handleIncoming: Unknown packet subtype " << item->PacketSubType() << std::endl ;
}
@ -1085,21 +1069,21 @@ void p3turtle::handleRecvGenericTunnelItem(RsTurtleGenericTunnelItem *item)
service->receiveTurtleData(item,hash,vpid,item->travelingDirection()) ;
}
void p3turtle::handleRecvGenericDataItem(RsTurtleGenericDataItem *item)
{
#ifdef P3TURTLE_DEBUG
std::cerr << "p3Turtle: received Generic Data item:" << std::endl ;
item->print(std::cerr,1) ;
#endif
std::string virtual_peer_id ;
std::string hash ;
RsTurtleClientService *service ;
if(!getTunnelServiceInfo(item->tunnelId(),virtual_peer_id,hash,service))
return ;
service->receiveTurtleData(item->data_bytes,item->data_size,hash,virtual_peer_id,item->travelingDirection()) ;
}
//void p3turtle::handleRecvGenericDataItem(RsTurtleGenericDataItem *item)
//{
//#ifdef P3TURTLE_DEBUG
// std::cerr << "p3Turtle: received Generic Data item:" << std::endl ;
// item->print(std::cerr,1) ;
//#endif
// std::string virtual_peer_id ;
// std::string hash ;
// RsTurtleClientService *service ;
//
// if(!getTunnelServiceInfo(item->tunnelId(),virtual_peer_id,hash,service))
// return ;
//
// service->receiveTurtleData(item->data_bytes,item->data_size,hash,virtual_peer_id,item->travelingDirection()) ;
//}
bool p3turtle::getTunnelServiceInfo(TurtleTunnelId tunnel_id,std::string& vpid,std::string& hash,RsTurtleClientService *& service)
{
@ -1543,6 +1527,7 @@ void p3turtle::handleTunnelResult(RsTurtleTunnelOkItem *item)
bool new_tunnel = false ;
TurtleFileHash new_hash ;
std::string new_vpid ;
RsTurtleClientService *service = NULL ;
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
@ -1634,6 +1619,7 @@ void p3turtle::handleTunnelResult(RsTurtleTunnelOkItem *item)
//
new_tunnel = true ;
new_hash = it->first ;
service = it->second.service ;
locked_addDistantPeer(new_hash,item->tunnel_id) ;
new_vpid = _local_tunnels[item->tunnel_id].vpid ; // save it for off-mutex usage.
@ -1657,11 +1643,8 @@ void p3turtle::handleTunnelResult(RsTurtleTunnelOkItem *item)
// notify the file transfer controller for the new file source. This should be done off-mutex
// so we deported this code here.
//
if(new_tunnel && _ft_controller != NULL)
{
_ft_controller->addFileSource(new_hash,new_vpid) ;
_ft_controller->statusChange(_online_peers) ;
}
if(new_tunnel && service != NULL)
service->addVirtualPeer(new_hash,new_vpid) ;
}
// -----------------------------------------------------------------------------------//
@ -2093,8 +2076,8 @@ void p3turtle::dumpState()
for(std::map<TurtleVirtualPeerId,TurtleTunnelId>::const_iterator it(_virtual_peers.begin());it!=_virtual_peers.end();++it)
std::cerr << " id=" << it->first << ", tunnel=" << (void*)(it->second) << std::endl ;
std::cerr << " Online peers: " << std::endl ;
for(std::list<pqipeer>::const_iterator it(_online_peers.begin());it!=_online_peers.end();++it)
std::cerr << " id=" << it->id << ", name=" << it->name << ", state=" << it->state << ", actions=" << it->actions << std::endl ;
// for(std::list<pqipeer>::const_iterator it(_online_peers.begin());it!=_online_peers.end();++it)
// std::cerr << " id=" << it->id << ", name=" << it->name << ", state=" << it->state << ", actions=" << it->actions << std::endl ;
}
#endif

View File

@ -216,7 +216,7 @@ class TurtleHashInfo
class p3turtle: public p3Service, public RsTurtle, public p3Config
{
public:
p3turtle(p3LinkMgr *lm,ftServer *m);
p3turtle(p3LinkMgr *lm) ;
// Enables/disable the service. Still ticks, but does nothing. Default is true.
//
@ -352,7 +352,6 @@ class p3turtle: public p3Service, public RsTurtle, public p3Config
/// specific routing functions for handling particular packets.
void handleRecvGenericTunnelItem(RsTurtleGenericTunnelItem *item);
void handleRecvGenericDataItem(RsTurtleGenericDataItem *item);
bool getTunnelServiceInfo(TurtleTunnelId, std::string& virtual_peer_id, std::string& hash, RsTurtleClientService*&) ;
// following functions should go to ftServer
@ -376,8 +375,6 @@ class p3turtle: public p3Service, public RsTurtle, public p3Config
/* data */
p3LinkMgr *mLinkMgr;
ftServer *_ft_server ;
ftController *_ft_controller ;
RsTurtleSerialiser *_serialiser ;
mutable RsMutex mTurtleMtx;

View File

@ -64,14 +64,6 @@ class RsTurtleClientService
//
// By default (if not overloaded), the method will just free the data, as any subclass should do as well.
//
virtual void receiveTurtleData(void *data,uint32_t size,const std::string& hash,const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction)
{
std::cerr << "!!!!!! Received Data from turtle router, but the client service is not handling it !!!!!!!!!!" << std::endl ;
}
// This method does the exact same job, but the item is already de-serialized. It is kept for
// compatibility reasons only. New services should not use it, only the file transfer should.
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const std::string& hash,const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction)
{
std::cerr << "!!!!!! Received Data from turtle router, but the client service is not handling it !!!!!!!!!!" << std::endl ;
@ -83,6 +75,11 @@ class RsTurtleClientService
// router: RsTurtleGenericDataItem
virtual RsTurtleGenericTunnelItem *deserialiseItem(void *data, uint32_t size) const { return NULL ; }
// These methods are called by the turtle router to add/remove virtual peers when tunnels are created/deleted
//
virtual void addVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id) = 0 ;
virtual void removeVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id) = 0 ;
};