2007-12-11 20:43:17 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services: p3disc.cc
|
|
|
|
*
|
|
|
|
* Services for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-02-22 12:36:39 -05:00
|
|
|
#include "rsiface/rsiface.h"
|
2009-07-21 17:25:39 -04:00
|
|
|
#include "rsiface/rsinit.h" /* for PGPSSL flag */
|
2008-12-23 11:23:54 -05:00
|
|
|
#include "rsiface/rspeers.h"
|
2007-12-11 20:43:17 -05:00
|
|
|
#include "services/p3disc.h"
|
|
|
|
|
2010-01-13 15:56:55 -05:00
|
|
|
#include "pqi/authssl.h"
|
2010-01-13 16:05:38 -05:00
|
|
|
#include "pqi/authgpg.h"
|
2008-01-25 02:49:28 -05:00
|
|
|
#include "pqi/p3connmgr.h"
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
const uint32_t AUTODISC_LDI_SUBTYPE_PING = 0x01;
|
|
|
|
const uint32_t AUTODISC_LDI_SUBTYPE_RPLY = 0x02;
|
|
|
|
|
|
|
|
#include <sstream>
|
2008-02-26 11:52:50 -05:00
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
#include "util/rsdebug.h"
|
2008-02-26 11:52:50 -05:00
|
|
|
#include "util/rsprint.h"
|
2009-06-28 16:57:02 -04:00
|
|
|
#include "util/rsversion.h"
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
const int pqidisczone = 2482;
|
|
|
|
|
|
|
|
static int convertTDeltaToTRange(double tdelta);
|
|
|
|
static int convertTRangeToTDelta(int trange);
|
|
|
|
|
|
|
|
// Operating System specific includes.
|
|
|
|
#include "pqi/pqinetwork.h"
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* DISC FLAGS */
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
const uint32_t P3DISC_FLAGS_USE_DISC = 0x0001;
|
|
|
|
const uint32_t P3DISC_FLAGS_USE_DHT = 0x0002;
|
2008-12-23 11:23:54 -05:00
|
|
|
const uint32_t P3DISC_FLAGS_EXTERNAL_ADDR = 0x0004;
|
2008-02-28 10:58:54 -05:00
|
|
|
const uint32_t P3DISC_FLAGS_STABLE_UDP = 0x0008;
|
|
|
|
const uint32_t P3DISC_FLAGS_PEER_ONLINE = 0x0010;
|
|
|
|
const uint32_t P3DISC_FLAGS_OWN_DETAILS = 0x0020;
|
2008-12-23 11:23:54 -05:00
|
|
|
const uint32_t P3DISC_FLAGS_PEER_TRUSTS_ME= 0x0040;
|
2009-06-28 16:57:02 -04:00
|
|
|
const uint32_t P3DISC_FLAGS_ASK_VERSION = 0x0080;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
|
2008-03-02 09:25:59 -05:00
|
|
|
/*****
|
|
|
|
* #define P3DISC_DEBUG 1
|
|
|
|
****/
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-04 16:40:34 -05:00
|
|
|
/*********** NOTE ***************
|
|
|
|
*
|
|
|
|
* Only need Mutexs for neighbours information
|
|
|
|
*/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/******************************************************************************************
|
|
|
|
****************************** NEW DISCOVERY *******************************************
|
|
|
|
******************************************************************************************
|
|
|
|
*****************************************************************************************/
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 15:56:55 -05:00
|
|
|
p3disc::p3disc(p3ConnectMgr *cm, pqipersongrp *pqih)
|
|
|
|
:p3Service(RS_SERVICE_TYPE_DISC), mConnMgr(cm), mPqiPersonGrp(pqih)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-02-04 16:40:34 -05:00
|
|
|
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
addSerialType(new RsDiscSerialiser());
|
|
|
|
|
|
|
|
mRemoteDisc = true;
|
|
|
|
mLocalDisc = false;
|
2009-12-22 05:41:19 -05:00
|
|
|
lastSentHeartbeatTime = 0;
|
2009-07-31 14:31:26 -04:00
|
|
|
|
|
|
|
//add own version to versions map
|
2010-01-13 15:58:58 -05:00
|
|
|
versions[AuthSSL::getAuthSSL()->OwnId()] = RsUtil::retroshareVersion();
|
2007-12-11 20:43:17 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3disc::tick()
|
|
|
|
{
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
static int count = 0;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
if (++count % 10 == 0)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
idServers();
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
#endif
|
2009-12-22 05:41:19 -05:00
|
|
|
//send a heartbeat to all connected peers
|
|
|
|
if (time(NULL) - lastSentHeartbeatTime > HEARTBEAT_REPEAT_TIME) {
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "p3disc::tick() sending heartbeat to all peers" << std::endl;
|
|
|
|
#endif
|
|
|
|
lastSentHeartbeatTime = time(NULL);
|
|
|
|
std::list <std::string> peers;
|
|
|
|
mConnMgr->getOnlineList(peers);
|
|
|
|
for (std::list<std::string>::const_iterator pit = peers.begin(); pit != peers.end(); ++pit) {
|
|
|
|
sendHeartbeat(*pit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return handleIncoming();
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
int p3disc::handleIncoming()
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
|
|
|
RsItem *item = NULL;
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "p3disc::handleIncoming()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-04 16:40:34 -05:00
|
|
|
bool discOn;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
discOn = mRemoteDisc;
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
// if off discard item.
|
2008-02-03 01:29:02 -05:00
|
|
|
if (!mRemoteDisc)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
|
|
|
while(NULL != (item = recvItem()))
|
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
2007-12-11 20:43:17 -05:00
|
|
|
std::ostringstream out;
|
2008-02-03 01:29:02 -05:00
|
|
|
out << "p3disc::handleIncoming()";
|
2007-12-11 20:43:17 -05:00
|
|
|
out << " Deleting - Cos RemoteDisc Off!" << std::endl;
|
|
|
|
|
|
|
|
item -> print(out);
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nhandled = 0;
|
|
|
|
// While messages read
|
|
|
|
while(NULL != (item = recvItem()))
|
|
|
|
{
|
2009-06-23 17:36:02 -04:00
|
|
|
RsDiscOwnItem *dio = NULL;
|
2007-12-11 20:43:17 -05:00
|
|
|
RsDiscReply *dri = NULL;
|
2009-05-25 07:38:47 -04:00
|
|
|
RsDiscIssuer *dii = NULL;
|
2009-06-28 16:57:02 -04:00
|
|
|
RsDiscVersion *dvi = NULL;
|
2009-12-22 05:41:19 -05:00
|
|
|
RsDiscHeartbeat *dta = NULL;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "p3disc::handleIncoming() Received Message!" << std::endl;
|
|
|
|
item -> print(std::cerr);
|
|
|
|
std::cerr << std::endl;
|
2008-02-03 01:29:02 -05:00
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
// if discovery reply then respond if haven't already.
|
2010-01-13 16:33:54 -05:00
|
|
|
if (NULL != (dri = dynamic_cast<RsDiscReply *> (item))) {
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
recvPeerDetails(dri);
|
2009-06-23 17:36:02 -04:00
|
|
|
nhandled++;
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
2009-06-23 17:36:02 -04:00
|
|
|
#ifdef RS_USE_PGPSSL
|
2010-01-13 16:33:54 -05:00
|
|
|
else if (NULL != (dii = dynamic_cast<RsDiscIssuer *> (item))) {
|
2009-05-25 07:38:47 -04:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
//recvPeerIssuerMsg(dii);
|
2009-06-23 17:36:02 -04:00
|
|
|
nhandled++;
|
2009-05-25 07:38:47 -04:00
|
|
|
}
|
2009-06-23 17:36:02 -04:00
|
|
|
#endif
|
2010-01-13 16:33:54 -05:00
|
|
|
else if (NULL != (dvi = dynamic_cast<RsDiscVersion *> (item))) {
|
2009-06-28 16:57:02 -04:00
|
|
|
recvPeerVersionMsg(dvi);
|
|
|
|
nhandled++;
|
|
|
|
}
|
2010-01-13 16:33:54 -05:00
|
|
|
else if (NULL != (dio = dynamic_cast<RsDiscOwnItem *> (item))) /* Ping */ {
|
|
|
|
//recvPeerOwnMsg(dio);
|
2009-06-23 17:36:02 -04:00
|
|
|
nhandled++;
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
2010-01-13 16:33:54 -05:00
|
|
|
else if (NULL != (dta = dynamic_cast<RsDiscHeartbeat *> (item))) {
|
2009-12-22 05:41:19 -05:00
|
|
|
recvHeartbeatMsg(dta);
|
|
|
|
return 1;
|
|
|
|
}
|
2009-06-23 17:36:02 -04:00
|
|
|
delete item;
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
return nhandled;
|
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
/************* from pqiMonitor *******************/
|
|
|
|
void p3disc::statusChange(const std::list<pqipeer> &plist)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "p3disc::statusChange()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
std::list<pqipeer>::const_iterator pit;
|
|
|
|
/* if any have switched to 'connected' then we notify */
|
2010-01-13 16:33:54 -05:00
|
|
|
for(pit = plist.begin(); pit != plist.end(); pit++) {
|
|
|
|
if ((pit->state & RS_PEER_S_FRIEND) && (pit->actions & RS_PEER_CONNECTED)) {
|
2009-12-18 16:45:45 -05:00
|
|
|
/* send their own details to them. Usefull for ext ip address detection */
|
2010-01-13 16:33:54 -05:00
|
|
|
// sendPeerDetails(pit->id, pit->id);
|
|
|
|
// /* send our details to them */
|
|
|
|
sendOwnVersion(pit->id);
|
|
|
|
sendAllInfoToPeer(pit->id);
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
void p3disc::sendAllInfoToPeer(std::string id)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
/* get a peer lists */
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "p3disc::respondToPeer() id: " << id;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
std::list<std::string> friendIds;
|
|
|
|
std::list<std::string>::iterator friendIdsIt;
|
|
|
|
std::set<std::string> gpgIds;;
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
rsPeers->getFriendList(friendIds);
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
/* send them a list of all friend's details */
|
2010-01-13 16:33:54 -05:00
|
|
|
for(friendIdsIt = friendIds.begin(); friendIdsIt != friendIds.end(); friendIdsIt++) {
|
2008-02-03 01:29:02 -05:00
|
|
|
/* get details */
|
|
|
|
peerConnectState detail;
|
2010-01-13 16:33:54 -05:00
|
|
|
if (!mConnMgr->getFriendNetStatus(*friendIdsIt, detail)) {
|
2008-02-03 01:29:02 -05:00
|
|
|
/* major error! */
|
|
|
|
continue;
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
if (!(detail.visState & RS_VIS_STATE_NODISC)) {
|
|
|
|
gpgIds.insert(detail.gpg_id);
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
2009-12-20 08:26:23 -05:00
|
|
|
}
|
2009-12-18 18:35:40 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
//add own info
|
|
|
|
gpgIds.insert(rsPeers->getGPGOwnId());
|
2009-12-18 12:50:53 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
//send details for each gpg Ids
|
|
|
|
std::set<std::string>::iterator gpgIdsIt;
|
|
|
|
for (gpgIdsIt = gpgIds.begin(); gpgIdsIt != gpgIds.end(); gpgIdsIt++) {
|
|
|
|
sendPeerDetails(id, *gpgIdsIt);
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* (dest (to), source (cert)) */
|
2010-01-13 16:33:54 -05:00
|
|
|
void p3disc::sendPeerDetails(std::string to, std::string about) {
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2009-02-22 12:36:39 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
2008-02-03 01:29:02 -05:00
|
|
|
std::ostringstream out;
|
|
|
|
out << "p3disc::sendPeerDetails()";
|
|
|
|
out << " Sending details of: " << about;
|
|
|
|
out << " to: " << to << std::endl;
|
|
|
|
std::cerr << out.str() << std::endl;
|
2009-02-22 12:36:39 -05:00
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
about = rsPeers->getGPGId(about);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
// Construct a message
|
|
|
|
RsDiscReply *di = new RsDiscReply();
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
// Fill the message
|
|
|
|
// Set Target as input cert.
|
|
|
|
di -> PeerId(to);
|
|
|
|
di -> aboutId = about;
|
2010-01-13 16:33:54 -05:00
|
|
|
di -> certGPG = AuthGPG::getAuthGPG()->SaveCertificateToString(about);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2009-12-20 08:26:23 -05:00
|
|
|
// set the ip addresse list.
|
2010-01-13 16:33:54 -05:00
|
|
|
std::list<std::string> sslChilds;
|
|
|
|
rsPeers->getSSLChildListOfGPGId(about, sslChilds);
|
|
|
|
for (std::list<std::string>::iterator sslChildIt = sslChilds.begin(); sslChildIt != sslChilds.end(); sslChildIt++) {
|
|
|
|
peerConnectState detail;
|
|
|
|
if (!mConnMgr->getFriendNetStatus(*sslChildIt, detail)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
RsPeerNetItem *rsPeerNetItem = new RsPeerNetItem();
|
|
|
|
rsPeerNetItem->clear();
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
rsPeerNetItem->pid = detail.id;
|
|
|
|
rsPeerNetItem->gpg_id = detail.gpg_id;
|
|
|
|
rsPeerNetItem->location = detail.location;
|
|
|
|
rsPeerNetItem->netMode = detail.netMode;
|
|
|
|
rsPeerNetItem->visState = detail.visState;
|
|
|
|
rsPeerNetItem->lastContact = detail.lastcontact;
|
|
|
|
rsPeerNetItem->currentlocaladdr = detail.currentlocaladdr;
|
|
|
|
rsPeerNetItem->currentremoteaddr = detail.currentserveraddr;
|
|
|
|
rsPeerNetItem->ipAddressList = detail.getIpAddressList();
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
di->rsPeerList.push_back(*rsPeerNetItem);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
//send own details
|
|
|
|
if (about == rsPeers->getGPGOwnId()) {
|
|
|
|
peerConnectState detail;
|
|
|
|
if (mConnMgr->getOwnNetStatus(detail)) {
|
|
|
|
RsPeerNetItem *rsPeerNetItem = new RsPeerNetItem();
|
|
|
|
rsPeerNetItem->clear();
|
|
|
|
rsPeerNetItem->pid = detail.id;
|
|
|
|
rsPeerNetItem->gpg_id = detail.gpg_id;
|
|
|
|
rsPeerNetItem->location = detail.location;
|
|
|
|
rsPeerNetItem->netMode = detail.netMode;
|
|
|
|
rsPeerNetItem->visState = detail.visState;
|
|
|
|
rsPeerNetItem->lastContact = time(NULL);
|
|
|
|
rsPeerNetItem->currentlocaladdr = detail.currentlocaladdr;
|
|
|
|
rsPeerNetItem->currentremoteaddr = detail.currentserveraddr;
|
|
|
|
rsPeerNetItem->ipAddressList = detail.getIpAddressList();
|
|
|
|
|
|
|
|
di->rsPeerList.push_back(*rsPeerNetItem);
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 12:36:39 -05:00
|
|
|
|
2010-01-13 16:22:52 -05:00
|
|
|
// Send off message
|
2009-12-18 12:51:40 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
di->print(std::cerr, 5);
|
|
|
|
#endif
|
|
|
|
sendItem(di);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2009-05-25 07:38:47 -04:00
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "Sent DI Message" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
void p3disc::sendOwnVersion(std::string to)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "p3disc::sendOwnVersion()";
|
|
|
|
out << " Sending rs version to: " << to << std::endl;
|
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
RsDiscVersion *di = new RsDiscVersion();
|
|
|
|
di->PeerId(to);
|
|
|
|
di->version = RsUtil::retroshareVersion();
|
|
|
|
|
|
|
|
/* send the message */
|
|
|
|
sendItem(di);
|
|
|
|
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "Sent DI Message" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2009-05-25 07:38:47 -04:00
|
|
|
|
2009-12-22 05:41:19 -05:00
|
|
|
void p3disc::sendHeartbeat(std::string to)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "p3disc::sendHeartbeat()";
|
|
|
|
out << " Sending tick to : " << to << std::endl;
|
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
RsDiscHeartbeat *di = new RsDiscHeartbeat();
|
|
|
|
di->PeerId(to);
|
|
|
|
|
|
|
|
/* send the message */
|
|
|
|
sendItem(di);
|
|
|
|
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "Sent tick Message" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
void p3disc::recvPeerDetails(RsDiscReply *item)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
|
|
|
|
2009-02-22 12:36:39 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
2010-01-13 16:33:54 -05:00
|
|
|
std::cerr << "p3disc::recvPeerFriendMsg() From: " << item->PeerId() << " About " << item->aboutId << std::endl;
|
2009-02-22 12:36:39 -05:00
|
|
|
#endif
|
2010-01-13 16:33:54 -05:00
|
|
|
std::string certGpgId;
|
|
|
|
AuthGPG::getAuthGPG()->LoadCertificateFromString(item->certGPG, certGpgId);
|
|
|
|
if (item->aboutId != certGpgId) {
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "p3disc::recvPeerFriendMsg() Error : about id is not the same as gpg id." << std::endl;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
for (std::list<RsPeerNetItem>::iterator pitem = item->rsPeerList.begin(); pitem != item->rsPeerList.end(); pitem++) {
|
|
|
|
//don't update dummy friends
|
|
|
|
if (rsPeers->isDummyFriend(pitem->pid)) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-26 11:14:13 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
addDiscoveryData(item->PeerId(), pitem->pid, pitem->currentlocaladdr, pitem->currentremoteaddr, 0, time(NULL));
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "pp3disc::recvPeerFriendMsg() Peer Config Item:" << std::endl;
|
|
|
|
pitem->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
if (pitem->pid != rsPeers->getOwnId()) {
|
|
|
|
mConnMgr->addFriend(pitem->pid, pitem->gpg_id, pitem->netMode, pitem->visState, 0);
|
|
|
|
RsPeerDetails storedDetails;
|
|
|
|
rsPeers->getPeerDetails(pitem->pid, storedDetails);
|
|
|
|
if ( (!(storedDetails.state & RS_PEER_CONNECTED) && storedDetails.lastConnect < (pitem->lastContact - 10000))
|
|
|
|
|| item->PeerId() == pitem->pid) { //update if it's fresh info or if it's from the peer itself
|
|
|
|
//their info is fresher than ours (there is a 10000 seconds margin), update ours
|
|
|
|
mConnMgr->setLocalAddress(pitem->pid, pitem->currentlocaladdr);
|
|
|
|
mConnMgr->setExtAddress(pitem->pid, pitem->currentremoteaddr);
|
|
|
|
mConnMgr->setVisState(pitem->pid, pitem->visState);
|
|
|
|
mConnMgr->setNetworkMode(pitem->pid, pitem->netMode);
|
|
|
|
if (storedDetails.location == "") {
|
|
|
|
mConnMgr->setLocation(pitem->pid, pitem->location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pitem->currentremoteaddr.sin_addr.s_addr != 0 && pitem->currentremoteaddr.sin_port != 0 &&
|
|
|
|
pitem->currentremoteaddr.sin_addr.s_addr != 1 && pitem->currentremoteaddr.sin_port != 1 &&
|
|
|
|
std::string(inet_ntoa(pitem->currentremoteaddr.sin_addr)) != "1.1.1.1" &&
|
|
|
|
(!isLoopbackNet(&pitem->currentremoteaddr.sin_addr)) &&
|
|
|
|
(!isPrivateNet(&pitem->currentremoteaddr.sin_addr))
|
2009-12-22 13:43:04 -05:00
|
|
|
) {
|
|
|
|
//the current server address given by the peer looks nice, let's use it for our own ext address if needed
|
|
|
|
sockaddr_in tempAddr;
|
|
|
|
if (!mConnMgr->getExtFinderExtAddress(tempAddr) && !mConnMgr->getUpnpExtAddress(tempAddr)) {
|
|
|
|
//don't change the port, just the ip
|
2010-01-13 16:33:54 -05:00
|
|
|
pitem->currentremoteaddr.sin_port = mConnMgr->ownState.currentserveraddr.sin_port;
|
|
|
|
mConnMgr->setExtAddress(mConnMgr->getOwnId(), pitem->currentremoteaddr);
|
2009-12-22 13:43:04 -05:00
|
|
|
}
|
|
|
|
}
|
2010-01-13 16:33:54 -05:00
|
|
|
}
|
|
|
|
//allways update address list
|
|
|
|
mConnMgr->setAddressList(pitem->pid, pitem->ipAddressList);
|
2009-12-22 13:43:04 -05:00
|
|
|
|
2009-12-14 13:11:19 -05:00
|
|
|
}
|
2009-02-22 12:36:39 -05:00
|
|
|
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_NEIGHBOURS, NOTIFY_TYPE_MOD);
|
2009-06-28 16:57:02 -04:00
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
/* cleanup (handled by caller) */
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-13 16:33:54 -05:00
|
|
|
//void p3disc::recvPeerIssuerMsg(RsDiscIssuer *item)
|
|
|
|
//{
|
|
|
|
//
|
|
|
|
//#ifdef P3DISC_DEBUG
|
|
|
|
// std::cerr << "p3disc::recvPeerIssuerMsg() From: " << item->PeerId();
|
|
|
|
// std::cerr << std::endl;
|
|
|
|
// std::cerr << "p3disc::recvPeerIssuerMsg() Cert: " << item->issuerCert;
|
|
|
|
// std::cerr << std::endl;
|
|
|
|
//#endif
|
|
|
|
//
|
|
|
|
// /* tells us their exact address (mConnectMgr can ignore if it looks wrong) */
|
|
|
|
//
|
|
|
|
// /* load certificate */
|
|
|
|
// std::string gpgId;
|
|
|
|
// bool loaded = AuthGPG::getAuthGPG()->LoadCertificateFromString(item->issuerCert, gpgId);
|
|
|
|
//
|
|
|
|
// /* cleanup (handled by caller) */
|
|
|
|
//
|
|
|
|
// return;
|
|
|
|
//}
|
2009-05-25 07:38:47 -04:00
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
void p3disc::recvPeerVersionMsg(RsDiscVersion *item)
|
|
|
|
{
|
|
|
|
#ifdef P3DISC_DEBUG
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "p3disc::recvPeerVersionMsg() From: " << item->PeerId();
|
|
|
|
std::cerr << std::endl;
|
2009-06-28 16:57:02 -04:00
|
|
|
#endif
|
|
|
|
|
2009-12-22 05:41:19 -05:00
|
|
|
// dont need protection
|
|
|
|
versions[item->PeerId()] = item->version;
|
2009-06-28 16:57:02 -04:00
|
|
|
|
2009-12-22 05:41:19 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3disc::recvHeartbeatMsg(RsDiscHeartbeat *item)
|
|
|
|
{
|
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "p3disc::recvHeartbeatMsg() From: " << item->PeerId();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mPqiPersonGrp->getPeer(item->PeerId())->receiveHeartbeat();
|
|
|
|
|
|
|
|
return;
|
2009-06-28 16:57:02 -04:00
|
|
|
}
|
2009-05-25 07:38:47 -04:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/*************************************************************************************/
|
|
|
|
/* Storing Network Graph */
|
|
|
|
/*************************************************************************************/
|
2009-02-22 12:36:39 -05:00
|
|
|
int p3disc::addDiscoveryData(std::string fromId, std::string aboutId, struct sockaddr_in laddr, struct sockaddr_in raddr, uint32_t flags, time_t ts)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-02-04 16:40:34 -05:00
|
|
|
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-04 16:40:34 -05:00
|
|
|
/* Store Network information */
|
2008-02-03 01:29:02 -05:00
|
|
|
std::map<std::string, autoneighbour>::iterator it;
|
|
|
|
if (neighbours.end() == (it = neighbours.find(aboutId)))
|
|
|
|
{
|
|
|
|
/* doesn't exist */
|
|
|
|
autoneighbour an;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* an data */
|
|
|
|
an.id = aboutId;
|
|
|
|
an.validAddrs = false;
|
|
|
|
an.discFlags = 0;
|
|
|
|
an.ts = 0;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
neighbours[aboutId] = an;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
it = neighbours.find(aboutId);
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* it always valid */
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* just update packet */
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
autoserver as;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
as.id = fromId;
|
|
|
|
as.localAddr = laddr;
|
|
|
|
as.remoteAddr = raddr;
|
|
|
|
as.discFlags = flags;
|
|
|
|
as.ts = ts;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
bool authDetails = (as.id == it->second.id);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* KEY decision about address */
|
2009-06-28 16:57:02 -04:00
|
|
|
if ((authDetails) ||
|
2008-02-03 01:29:02 -05:00
|
|
|
((!(it->second.authoritative)) && (as.ts > it->second.ts)))
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
/* copy details to an */
|
|
|
|
it->second.authoritative = authDetails;
|
|
|
|
it->second.ts = as.ts;
|
|
|
|
it->second.validAddrs = true;
|
|
|
|
it->second.localAddr = as.localAddr;
|
|
|
|
it->second.remoteAddr = as.remoteAddr;
|
|
|
|
it->second.discFlags = as.discFlags;
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
(it->second).neighbour_of[fromId] = as;
|
|
|
|
|
|
|
|
/* do we update network address info??? */
|
|
|
|
return 1;
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/*************************************************************************************/
|
|
|
|
/* Extracting Network Graph Details */
|
|
|
|
/*************************************************************************************/
|
2008-03-04 16:31:11 -05:00
|
|
|
bool p3disc::potentialproxies(std::string id, std::list<std::string> &proxyIds)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
/* find id -> and extract the neighbour_of ids */
|
2008-02-04 16:40:34 -05:00
|
|
|
|
|
|
|
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
std::map<std::string, autoneighbour>::iterator it;
|
|
|
|
std::map<std::string, autoserver>::iterator sit;
|
|
|
|
if (neighbours.end() == (it = neighbours.find(id)))
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
return false;
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
for(sit = it->second.neighbour_of.begin();
|
|
|
|
sit != it->second.neighbour_of.end(); sit++)
|
|
|
|
{
|
|
|
|
proxyIds.push_back(sit->first);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
void p3disc::getversions(std::map<std::string, std::string> &versions)
|
|
|
|
{
|
|
|
|
versions = this->versions;
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
int p3disc::idServers()
|
|
|
|
{
|
2008-02-04 16:40:34 -05:00
|
|
|
RsStackMutex stack(mDiscMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
std::map<std::string, autoneighbour>::iterator nit;
|
|
|
|
std::map<std::string, autoserver>::iterator sit;
|
|
|
|
int cts = time(NULL);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
std::ostringstream out;
|
|
|
|
out << "::::AutoDiscovery Neighbours::::" << std::endl;
|
|
|
|
for(nit = neighbours.begin(); nit != neighbours.end(); nit++)
|
|
|
|
{
|
|
|
|
out << "Neighbour: " << (nit->second).id;
|
|
|
|
out << std::endl;
|
|
|
|
out << "-> LocalAddr: ";
|
|
|
|
out << inet_ntoa(nit->second.localAddr.sin_addr);
|
|
|
|
out << ":" << ntohs(nit->second.localAddr.sin_port) << std::endl;
|
|
|
|
out << "-> RemoteAddr: ";
|
|
|
|
out << inet_ntoa(nit->second.remoteAddr.sin_addr);
|
|
|
|
out << ":" << ntohs(nit->second.remoteAddr.sin_port) << std::endl;
|
|
|
|
out << " Last Contact: ";
|
|
|
|
out << cts - (nit->second.ts) << " sec ago";
|
|
|
|
out << std::endl;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
out << " -->DiscFlags: 0x" << std::hex << nit->second.discFlags;
|
|
|
|
out << std::dec << std::endl;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
for(sit = (nit->second.neighbour_of).begin();
|
2008-02-03 01:29:02 -05:00
|
|
|
sit != (nit->second.neighbour_of).end(); sit++)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
out << "\tConnected via: " << (sit->first);
|
2007-12-11 20:43:17 -05:00
|
|
|
out << std::endl;
|
2008-02-03 01:29:02 -05:00
|
|
|
out << "\t\tLocalAddr: ";
|
|
|
|
out << inet_ntoa(sit->second.localAddr.sin_addr);
|
2008-02-04 16:40:34 -05:00
|
|
|
out <<":"<< ntohs(sit->second.localAddr.sin_port);
|
2007-12-11 20:43:17 -05:00
|
|
|
out << std::endl;
|
2008-02-03 01:29:02 -05:00
|
|
|
out << "\t\tRemoteAddr: ";
|
|
|
|
out << inet_ntoa(sit->second.remoteAddr.sin_addr);
|
|
|
|
out <<":"<< ntohs(sit->second.remoteAddr.sin_port);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
out << std::endl;
|
|
|
|
out << "\t\tLast Contact:";
|
|
|
|
out << cts - (sit->second.ts) << " sec ago";
|
|
|
|
out << std::endl;
|
|
|
|
out << "\t\tDiscFlags: 0x" << std::hex << (sit->second.discFlags);
|
|
|
|
out << std::dec << std::endl;
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
#ifdef P3DISC_DEBUG
|
|
|
|
std::cerr << "p3disc::idServers()" << std::endl;
|
|
|
|
std::cerr << out.str();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
// tdelta -> trange.
|
|
|
|
// -inf...<0 0 (invalid)
|
|
|
|
// 0.. <9 1
|
|
|
|
// 9...<99 2
|
|
|
|
// 99...<999 3
|
|
|
|
// 999...<9999 4
|
|
|
|
// etc...
|
|
|
|
|
|
|
|
int convertTDeltaToTRange(double tdelta)
|
|
|
|
{
|
|
|
|
if (tdelta < 0)
|
|
|
|
return 0;
|
|
|
|
int trange = 1 + (int) log10(tdelta + 1.0);
|
|
|
|
return trange;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// trange -> tdelta
|
|
|
|
// -inf...0 -1 (invalid)
|
|
|
|
// 1 8
|
|
|
|
// 2 98
|
|
|
|
// 3 998
|
|
|
|
// 4 9998
|
|
|
|
// etc...
|
|
|
|
|
|
|
|
int convertTRangeToTDelta(int trange)
|
|
|
|
{
|
|
|
|
if (trange <= 0)
|
|
|
|
return -1;
|
2009-06-28 16:57:02 -04:00
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
return (int) (pow(10.0, trange) - 1.5); // (int) xxx98.5 -> xxx98
|
|
|
|
}
|
|
|
|
|