2011-07-09 09:36:37 +00:00
|
|
|
/*
|
|
|
|
* libretroshare/src/pqi: p3peermgr.cc
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2007-2011 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util/rsnet.h"
|
|
|
|
#include "pqi/authgpg.h"
|
|
|
|
#include "pqi/authssl.h"
|
|
|
|
|
|
|
|
#include "pqi/p3peermgr.h"
|
2011-07-09 18:39:34 +00:00
|
|
|
#include "pqi/p3linkmgr.h"
|
|
|
|
#include "pqi/p3netmgr.h"
|
2011-09-29 09:20:09 +00:00
|
|
|
#include "pqi/p3historymgr.h"
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
//#include "pqi/p3dhtmgr.h" // Only need it for constants.
|
|
|
|
//#include "tcponudp/tou.h"
|
|
|
|
//#include "util/extaddrfinder.h"
|
|
|
|
//#include "util/dnsresolver.h"
|
|
|
|
|
|
|
|
#include "util/rsprint.h"
|
2012-04-14 22:38:24 +00:00
|
|
|
#include "util/rsstring.h"
|
2011-08-06 12:27:23 +00:00
|
|
|
#include "util/rsdebug.h"
|
|
|
|
const int p3peermgrzone = 9531;
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
#include "serialiser/rsconfigitems.h"
|
|
|
|
#include "pqi/pqinotify.h"
|
2011-08-07 21:11:00 +00:00
|
|
|
|
|
|
|
#include "retroshare/rsiface.h" // Needed for rsicontrol (should remove this dependancy)
|
|
|
|
#include "retroshare/rspeers.h" // Needed for Group Parameters.
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
/* Network setup States */
|
|
|
|
|
|
|
|
const uint32_t RS_NET_NEEDS_RESET = 0x0000;
|
|
|
|
const uint32_t RS_NET_UNKNOWN = 0x0001;
|
|
|
|
const uint32_t RS_NET_UPNP_INIT = 0x0002;
|
|
|
|
const uint32_t RS_NET_UPNP_SETUP = 0x0003;
|
|
|
|
const uint32_t RS_NET_EXT_SETUP = 0x0004;
|
|
|
|
const uint32_t RS_NET_DONE = 0x0005;
|
|
|
|
const uint32_t RS_NET_LOOPBACK = 0x0006;
|
|
|
|
const uint32_t RS_NET_DOWN = 0x0007;
|
|
|
|
|
|
|
|
const uint32_t MIN_TIME_BETWEEN_NET_RESET = 5;
|
|
|
|
|
|
|
|
const uint32_t PEER_IP_CONNECT_STATE_MAX_LIST_SIZE = 4;
|
|
|
|
|
2011-08-08 12:02:17 +00:00
|
|
|
#define VERY_OLD_PEER (90 * 24 * 3600) // 90 days.
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
/****
|
2011-07-11 12:11:58 +00:00
|
|
|
* #define PEER_DEBUG 1
|
2011-07-09 09:36:37 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
#define MAX_AVAIL_PERIOD 230 //times a peer stay in available state when not connected
|
|
|
|
#define MIN_RETRY_PERIOD 140
|
|
|
|
|
|
|
|
void printConnectState(std::ostream &out, peerState &peer);
|
|
|
|
|
|
|
|
peerState::peerState()
|
|
|
|
:id("unknown"),
|
|
|
|
gpg_id("unknown"),
|
2011-07-09 18:39:34 +00:00
|
|
|
netMode(RS_NET_MODE_UNKNOWN), visState(RS_VIS_STATE_STD), lastcontact(0)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-07-09 18:39:34 +00:00
|
|
|
sockaddr_clear(&localaddr);
|
|
|
|
sockaddr_clear(&serveraddr);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string textPeerConnectState(peerState &state)
|
|
|
|
{
|
2012-04-14 22:38:24 +00:00
|
|
|
std::string out = "Id: " + state.id + "\n";
|
|
|
|
rs_sprintf_append(out, "NetMode: %lu\n", state.netMode);
|
|
|
|
rs_sprintf_append(out, "VisState: %lu\n", state.visState);
|
|
|
|
rs_sprintf_append(out, "laddr: %s:%u\n", rs_inet_ntoa(state.localaddr.sin_addr).c_str(), ntohs(state.localaddr.sin_port));
|
|
|
|
rs_sprintf_append(out, "eaddr: %s:%u\n", rs_inet_ntoa(state.serveraddr.sin_addr).c_str(), ntohs(state.serveraddr.sin_port));
|
|
|
|
|
|
|
|
return out;
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-26 18:12:19 +00:00
|
|
|
p3PeerMgrIMPL::p3PeerMgrIMPL( const std::string& ssl_own_id,
|
|
|
|
const std::string& gpg_own_id,
|
|
|
|
const std::string& gpg_own_name,
|
|
|
|
const std::string& ssl_own_location)
|
2011-07-09 09:36:37 +00:00
|
|
|
:p3Config(CONFIG_TYPE_PEERS), mPeerMtx("p3PeerMgr"), mStatusChanged(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-09-29 09:20:09 +00:00
|
|
|
mLinkMgr = NULL;
|
|
|
|
mNetMgr = NULL;
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
/* setup basics of own state */
|
2012-12-26 18:12:19 +00:00
|
|
|
mOwnState.id = ssl_own_id ;
|
|
|
|
mOwnState.gpg_id = gpg_own_id ;
|
|
|
|
mOwnState.name = gpg_own_name ;
|
|
|
|
mOwnState.location = ssl_own_location ;
|
2011-07-30 12:18:56 +00:00
|
|
|
mOwnState.netMode = RS_NET_MODE_UPNP; // Default to UPNP.
|
2011-08-07 21:11:00 +00:00
|
|
|
mOwnState.visState = 0;
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
lastGroupId = 1;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-09 09:36:37 +00:00
|
|
|
std::cerr << "p3PeerMgr() Startup" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-14 22:55:14 +00:00
|
|
|
void p3PeerMgrIMPL::setManagers(p3LinkMgrIMPL *linkMgr, p3NetMgrIMPL *netMgr)
|
2011-07-09 18:39:34 +00:00
|
|
|
{
|
|
|
|
mLinkMgr = linkMgr;
|
|
|
|
mNetMgr = netMgr;
|
|
|
|
}
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
void p3PeerMgrIMPL::setOwnNetworkMode(uint32_t netMode)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setOwnNetworkMode() :";
|
2011-07-09 18:39:34 +00:00
|
|
|
std::cerr << " Existing netMode: " << mOwnState.netMode;
|
|
|
|
std::cerr << " Input netMode: " << netMode;
|
2011-07-09 09:36:37 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mOwnState.netMode = (netMode & RS_NET_MODE_ACTUAL);
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass on Flags to NetMgr.
|
2011-07-10 00:41:39 +00:00
|
|
|
mNetMgr->setNetworkMode((netMode & RS_NET_MODE_ACTUAL));
|
2011-07-09 18:39:34 +00:00
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
void p3PeerMgrIMPL::setOwnVisState(uint32_t visState)
|
2011-07-09 18:39:34 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2012-04-14 22:38:24 +00:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "p3PeerMgr::setOwnVisState() Existing vis: %lu Input vis: %lu", mOwnState.visState, visState);
|
|
|
|
rslog(RSL_WARNING, p3peermgrzone, out);
|
2011-08-07 21:11:00 +00:00
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-08-07 21:11:00 +00:00
|
|
|
std::cerr << out.str() << std::endl;
|
2011-07-09 18:39:34 +00:00
|
|
|
#endif
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
mOwnState.visState = visState;
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass on Flags to NetMgr.
|
2011-07-10 00:41:39 +00:00
|
|
|
mNetMgr->setVisState(visState);
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
void p3PeerMgrIMPL::tick()
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
|
2011-08-08 12:02:17 +00:00
|
|
|
static const time_t INTERVAL_BETWEEN_LOCATION_CLEANING = 1860 ; // Remove unused locations every 31 minutes.
|
|
|
|
static time_t last_friends_check = time(NULL) + INTERVAL_BETWEEN_LOCATION_CLEANING; // first cleaning after 1 hour.
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
time_t now = time(NULL) ;
|
|
|
|
|
2011-08-07 21:11:00 +00:00
|
|
|
if(now - last_friends_check > INTERVAL_BETWEEN_LOCATION_CLEANING)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::tick(): cleaning unused locations." << std::endl ;
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-08-07 21:11:00 +00:00
|
|
|
rslog(RSL_WARNING, p3peermgrzone, "p3PeerMgr::tick() removeUnusedLocations()");
|
2011-08-06 12:27:23 +00:00
|
|
|
|
2011-08-07 21:11:00 +00:00
|
|
|
removeUnusedLocations() ;
|
2011-07-09 09:36:37 +00:00
|
|
|
last_friends_check = now ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************** Network Status *********************************
|
|
|
|
* Configuration Loading / Saving.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
const std::string p3PeerMgrIMPL::getOwnId()
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
return AuthSSL::getAuthSSL()->OwnId();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::getOwnNetStatus(peerState &state)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
state = mOwnState;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::isFriend(const std::string &id)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG_COMMON
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::isFriend(" << id << ") called" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
bool ret = (mFriendList.end() != mFriendList.find(id));
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG_COMMON
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::isFriend(" << id << ") returning : " << ret << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-07 21:11:00 +00:00
|
|
|
bool p3PeerMgrIMPL::getPeerName(const std::string &ssl_id, std::string &name)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
it = mFriendList.find(ssl_id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = it->second.name + " (" + it->second.location + ")";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-05 15:41:24 +00:00
|
|
|
bool p3PeerMgrIMPL::getGpgId(const std::string &ssl_id, std::string &gpgId)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
it = mFriendList.find(ssl_id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpgId = it->second.gpg_id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-08 22:15:19 +00:00
|
|
|
// Placeholder until we implement this functionality.
|
2012-02-17 10:03:38 +00:00
|
|
|
uint32_t p3PeerMgrIMPL::getConnectionType(const std::string &/*sslId*/)
|
2012-01-08 22:15:19 +00:00
|
|
|
{
|
|
|
|
return RS_NET_CONN_TYPE_FRIEND;
|
|
|
|
}
|
2011-08-07 21:11:00 +00:00
|
|
|
|
2012-02-25 01:48:56 +00:00
|
|
|
int p3PeerMgrIMPL::getFriendCount(bool ssl, bool online)
|
|
|
|
{
|
|
|
|
if (online) {
|
|
|
|
// count only online id's
|
|
|
|
std::list<std::string> onlineIds;
|
|
|
|
mLinkMgr->getOnlineList(onlineIds);
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::set<std::string> gpgIds;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); ++it) {
|
|
|
|
if (online && std::find(onlineIds.begin(), onlineIds.end(), it->first) == onlineIds.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ssl) {
|
|
|
|
// count ssl id's only
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
// count unique gpg id's
|
|
|
|
gpgIds.insert(it->second.gpg_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssl ? count : gpgIds.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl) {
|
|
|
|
// count all ssl id's
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
return mFriendList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// count all gpg id's
|
|
|
|
std::list<std::string> gpgIds;
|
|
|
|
AuthGPG::getAuthGPG()->getGPGAcceptedList(gpgIds);
|
|
|
|
|
|
|
|
// add own gpg id, if we have more than one location
|
|
|
|
std::list<std::string> ownSslIds;
|
|
|
|
getAssociatedPeers(AuthGPG::getAuthGPG()->getGPGOwnId(), ownSslIds);
|
|
|
|
|
|
|
|
return gpgIds.size() + ((ownSslIds.size() > 0) ? 1 : 0);
|
|
|
|
}
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::getFriendNetStatus(const std::string &id, peerState &state)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::getOthersNetStatus(const std::string &id, peerState &state)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
it = mOthersList.find(id);
|
|
|
|
if (it == mOthersList.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
#if 0
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
void p3PeerMgrIMPL::getFriendList(std::list<std::string> &peers)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
peers.push_back(it->first);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
#endif
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
#if 0
|
2011-07-13 14:22:25 +00:00
|
|
|
void p3PeerMgrIMPL::getOthersList(std::list<std::string> &peers)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mOthersList.begin(); it != mOthersList.end(); it++)
|
|
|
|
{
|
|
|
|
peers.push_back(it->first);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
int p3PeerMgrIMPL::getConnectAddresses(const std::string &id,
|
2011-07-10 00:41:39 +00:00
|
|
|
struct sockaddr_in &lAddr, struct sockaddr_in &eAddr,
|
|
|
|
pqiIpAddrSet &histAddrs, std::string &dyndns)
|
|
|
|
{
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
/* ERROR */
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::getConnectAddresses() ERROR unknown Peer";
|
2011-07-10 00:41:39 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lAddr = it->second.localaddr;
|
|
|
|
eAddr = it->second.serveraddr;
|
|
|
|
histAddrs = it->second.ipAddrs;
|
|
|
|
dyndns = it->second.dyndns;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::haveOnceConnected()
|
2011-07-11 18:48:56 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.lastcontact > 0)
|
|
|
|
{
|
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::haveOnceConnected() lastcontact: ";
|
2011-07-11 18:48:56 +00:00
|
|
|
std::cerr << time(NULL) - it->second.lastcontact << " for id: " << it->first;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::haveOnceConnected() all Last Contacts = 0";
|
2011-07-11 18:48:56 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
|
|
/*******************************************************************/
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
bool p3PeerMgrIMPL::addFriend(const std::string& input_id, const std::string& input_gpg_id, uint32_t netMode, uint32_t visState, time_t lastContact,ServicePermissionFlags service_flags)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
bool notifyLinkMgr = false;
|
2012-09-16 14:43:38 +00:00
|
|
|
std::string id = input_id ;
|
|
|
|
std::string gpg_id = input_gpg_id ;
|
2011-08-06 12:27:23 +00:00
|
|
|
|
2012-09-16 14:43:38 +00:00
|
|
|
rslog(RSL_WARNING, p3peermgrzone, "p3PeerMgr::addFriend() id: " + id);
|
2011-08-06 12:27:23 +00:00
|
|
|
|
2012-09-16 14:43:38 +00:00
|
|
|
// For safety, make sure ssl_id is lower case and GPG id is upper case.
|
|
|
|
//
|
|
|
|
for(uint32_t i=0;i<id.length();++i)
|
|
|
|
if(id[i] >= 'A' && id[i] <= 'F')
|
|
|
|
id[i] += 'a' - 'A' ;
|
|
|
|
|
|
|
|
for(uint32_t i=0;i<gpg_id.length();++i)
|
|
|
|
if(gpg_id[i] >= 'a' && gpg_id[i] <= 'f')
|
|
|
|
gpg_id[i] += 'A' - 'a' ;
|
2011-08-06 12:27:23 +00:00
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
if (id == AuthSSL::getAuthSSL()->OwnId())
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::addFriend() cannot add own id as a friend." << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
/* (1) already exists */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* so four possibilities
|
|
|
|
* (1) already exists as friend -> do nothing.
|
|
|
|
* (2) is in others list -> move over.
|
|
|
|
* (3) is non-existant -> create new one.
|
|
|
|
*/
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::addFriend() " << id << "; gpg_id : " << gpg_id << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() != mFriendList.find(id))
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::addFriend() Already Exists" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
/* (1) already exists */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Authentication is now tested at connection time, we don't store the ssl cert anymore
|
|
|
|
//
|
|
|
|
if (!AuthGPG::getAuthGPG()->isGPGAccepted(gpg_id) && gpg_id != AuthGPG::getAuthGPG()->getGPGOwnId())
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::addFriend() gpg is not accepted" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
/* no auth */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* check if it is in others */
|
|
|
|
if (mOthersList.end() != (it = mOthersList.find(id)))
|
|
|
|
{
|
|
|
|
/* (2) in mOthersList -> move over */
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::addFriend() Move from Others" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
mFriendList[id] = it->second;
|
|
|
|
mOthersList.erase(it);
|
|
|
|
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
|
|
|
|
/* setup connectivity parameters */
|
|
|
|
it->second.visState = visState;
|
|
|
|
it->second.netMode = netMode;
|
|
|
|
it->second.lastcontact = lastContact;
|
|
|
|
|
|
|
|
mStatusChanged = true;
|
|
|
|
|
|
|
|
notifyLinkMgr = true;
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::addFriend() Creating New Entry" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* create a new entry */
|
|
|
|
peerState pstate;
|
|
|
|
|
|
|
|
pstate.id = id;
|
|
|
|
pstate.gpg_id = gpg_id;
|
|
|
|
pstate.name = AuthGPG::getAuthGPG()->getGPGName(gpg_id);
|
|
|
|
|
|
|
|
pstate.visState = visState;
|
|
|
|
pstate.netMode = netMode;
|
|
|
|
pstate.lastcontact = lastContact;
|
|
|
|
|
|
|
|
/* addr & timestamps -> auto cleared */
|
|
|
|
|
|
|
|
mFriendList[id] = pstate;
|
|
|
|
|
|
|
|
mStatusChanged = true;
|
|
|
|
|
|
|
|
notifyLinkMgr = true;
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notifyLinkMgr)
|
|
|
|
{
|
2011-07-10 00:41:39 +00:00
|
|
|
mLinkMgr->addFriend(id, !(visState & RS_VIS_STATE_NODHT));
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
setServicePermissionFlags(gpg_id,service_flags) ;
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
|
|
|
printPeerLists(std::cerr);
|
|
|
|
mLinkMgr->printPeerLists(std::cerr);
|
|
|
|
#endif
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::removeFriend(const std::string &id)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::removeFriend() for id : " << id << std::endl;
|
|
|
|
std::cerr << "p3PeerMgrIMPL::removeFriend() mFriendList.size() : " << mFriendList.size() << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-06 12:27:23 +00:00
|
|
|
rslog(RSL_WARNING, p3peermgrzone, "p3PeerMgr::removeFriend() id: " + id);
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
std::list<std::string> sslid_toRemove; // This is a list of SSLIds.
|
|
|
|
std::list<std::string> pgpid_toRemove; // This is a list of SSLIds.
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* move to othersList */
|
|
|
|
bool success = false;
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
//remove ssl and gpg_ids
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.id == id || it->second.gpg_id == id) {
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::removeFriend() friend found in the list." << id << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
peerState peer = it->second;
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
sslid_toRemove.push_back(it->second.id);
|
|
|
|
pgpid_toRemove.push_back(it->second.gpg_id);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
mOthersList[id] = peer;
|
|
|
|
mStatusChanged = true;
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
std::list<std::string>::iterator rit;
|
2012-12-01 19:22:22 +00:00
|
|
|
for(rit = sslid_toRemove.begin(); rit != sslid_toRemove.end(); rit++)
|
2011-07-11 12:11:58 +00:00
|
|
|
if (mFriendList.end() != (it = mFriendList.find(*rit)))
|
2011-07-09 09:36:37 +00:00
|
|
|
mFriendList.erase(it);
|
2012-12-01 19:22:22 +00:00
|
|
|
|
|
|
|
std::map<std::string,ServicePermissionFlags>::iterator it2 ;
|
|
|
|
|
|
|
|
for(rit = pgpid_toRemove.begin(); rit != pgpid_toRemove.end(); rit++)
|
|
|
|
if (mFriendsPermissionFlags.end() != (it2 = mFriendsPermissionFlags.find(*rit)))
|
|
|
|
mFriendsPermissionFlags.erase(it2);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::removeFriend() new mFriendList.size() : " << mFriendList.size() << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
std::list<std::string>::iterator rit;
|
2012-12-01 19:22:22 +00:00
|
|
|
for(rit = sslid_toRemove.begin(); rit != sslid_toRemove.end(); rit++)
|
2011-07-11 12:11:58 +00:00
|
|
|
{
|
|
|
|
mLinkMgr->removeFriend(*rit);
|
|
|
|
}
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
/* remove id from all groups */
|
|
|
|
std::list<std::string> peerIds;
|
|
|
|
peerIds.push_back(id);
|
|
|
|
|
|
|
|
assignPeersToGroup("", peerIds, false);
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
|
|
|
printPeerLists(std::cerr);
|
|
|
|
mLinkMgr->printPeerLists(std::cerr);
|
|
|
|
#endif
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
return !sslid_toRemove.empty();
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
void p3PeerMgrIMPL::printPeerLists(std::ostream &out)
|
2011-07-11 12:11:58 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
out << "p3PeerMgrIMPL::printPeerLists() Friend List";
|
2011-07-11 12:11:58 +00:00
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
out << "\t SSL ID: " << it->second.id;
|
|
|
|
out << "\t GPG ID: " << it->second.gpg_id;
|
|
|
|
out << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
out << "p3PeerMgrIMPL::printPeerLists() Others List";
|
2011-07-11 12:11:58 +00:00
|
|
|
out << std::endl;
|
|
|
|
for(it = mOthersList.begin(); it != mOthersList.end(); it++)
|
|
|
|
{
|
|
|
|
out << "\t SSL ID: " << it->second.id;
|
|
|
|
out << "\t GPG ID: " << it->second.gpg_id;
|
|
|
|
out << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
#if 0
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::addNeighbour(std::string id)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::addNeighbour() not implemented anymore." << id << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* so three possibilities
|
|
|
|
* (1) already exists as friend -> do nothing.
|
|
|
|
* (2) already in others list -> do nothing.
|
|
|
|
* (3) is non-existant -> create new one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == mFriendList.find(id))
|
|
|
|
{
|
|
|
|
/* (1) already exists */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOthersList.end() == mOthersList.find(id))
|
|
|
|
{
|
|
|
|
/* (2) already exists */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check with the AuthMgr if its valid */
|
|
|
|
if (!AuthSSL::getAuthSSL()->isAuthenticated(id))
|
|
|
|
{
|
|
|
|
/* no auth */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get details from AuthMgr */
|
|
|
|
sslcert detail;
|
|
|
|
if (!AuthSSL::getAuthSSL()->getCertDetails(id, detail))
|
|
|
|
{
|
|
|
|
/* no details */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new entry */
|
|
|
|
peerState pstate;
|
|
|
|
|
|
|
|
pstate.id = id;
|
|
|
|
pstate.name = detail.name;
|
|
|
|
|
|
|
|
pstate.state = 0;
|
|
|
|
pstate.actions = 0; //RS_PEER_NEW;
|
|
|
|
pstate.visState = RS_VIS_STATE_STD;
|
|
|
|
pstate.netMode = RS_NET_MODE_UNKNOWN;
|
|
|
|
|
|
|
|
/* addr & timestamps -> auto cleared */
|
|
|
|
mOthersList[id] = pstate;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
|
|
/*******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
**********************************************************************
|
|
|
|
******************** External Setup **********************************
|
|
|
|
**********************************************************************
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
|
2011-08-02 19:05:27 +00:00
|
|
|
/* This function should only be called from NetMgr,
|
|
|
|
* as it doesn't call back to there.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_in &localAddr, const struct sockaddr_in &extAddr)
|
|
|
|
{
|
|
|
|
std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress(";
|
|
|
|
std::cerr << rs_inet_ntoa(localAddr.sin_addr) << ":" << htons(localAddr.sin_port);
|
|
|
|
std::cerr << ", ";
|
|
|
|
std::cerr << rs_inet_ntoa(extAddr.sin_addr) << ":" << htons(extAddr.sin_port);
|
|
|
|
std::cerr << ")" << std::endl;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
//update ip address list
|
|
|
|
pqiIpAddress ipAddressTimed;
|
|
|
|
ipAddressTimed.mAddr = localAddr;
|
|
|
|
ipAddressTimed.mSeenTime = time(NULL);
|
|
|
|
mOwnState.ipAddrs.updateLocalAddrs(ipAddressTimed);
|
|
|
|
|
|
|
|
mOwnState.localaddr = localAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
//update ip address list
|
|
|
|
pqiIpAddress ipAddressTimed;
|
|
|
|
ipAddressTimed.mAddr = extAddr;
|
|
|
|
ipAddressTimed.mSeenTime = time(NULL);
|
|
|
|
mOwnState.ipAddrs.updateExtAddrs(ipAddressTimed);
|
|
|
|
|
2011-11-22 13:24:42 +00:00
|
|
|
/* Attempted Fix to MANUAL FORWARD Mode....
|
|
|
|
* don't update the server address - if we are in this mode
|
|
|
|
*
|
|
|
|
* It is okay - if they get it wrong, as we put the address in the address list anyway.
|
|
|
|
* This should keep people happy, and allow for misconfiguration!
|
|
|
|
*/
|
|
|
|
|
2012-02-11 12:18:56 +00:00
|
|
|
if (mOwnState.netMode & RS_NET_MODE_TRY_EXT)
|
|
|
|
{
|
|
|
|
/**** THIS CASE SHOULD NOT BE TRIGGERED ****/
|
|
|
|
std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress() Disabling Update of Server Port ";
|
|
|
|
std::cerr << " as MANUAL FORWARD Mode (ERROR - SHOULD NOT BE TRIGGERED: TRY_EXT_MODE)";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "Address is Now: ";
|
|
|
|
std::cerr << rs_inet_ntoa(mOwnState.serveraddr.sin_addr);
|
|
|
|
std::cerr << ":" << htons(mOwnState.serveraddr.sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else if (mOwnState.netMode & RS_NET_MODE_EXT)
|
2011-11-22 13:24:42 +00:00
|
|
|
{
|
|
|
|
mOwnState.serveraddr.sin_addr.s_addr = extAddr.sin_addr.s_addr;
|
|
|
|
std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress() Disabling Update of Server Port ";
|
|
|
|
std::cerr << " as MANUAL FORWARD Mode";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "Address is Now: ";
|
|
|
|
std::cerr << rs_inet_ntoa(mOwnState.serveraddr.sin_addr);
|
|
|
|
std::cerr << ":" << htons(mOwnState.serveraddr.sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mOwnState.serveraddr = extAddr;
|
|
|
|
}
|
2011-08-02 19:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
mLinkMgr->setLocalAddress(localAddr);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::setLocalAddress(const std::string &id, struct sockaddr_in addr)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
if (id == AuthSSL::getAuthSSL()->OwnId())
|
|
|
|
{
|
|
|
|
bool changed = false;
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
2011-07-09 18:39:34 +00:00
|
|
|
if (mOwnState.localaddr.sin_addr.s_addr != addr.sin_addr.s_addr ||
|
|
|
|
mOwnState.localaddr.sin_port != addr.sin_port)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
mOwnState.localaddr = addr;
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
{
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
mNetMgr->setLocalAddress(addr);
|
2011-07-10 01:49:39 +00:00
|
|
|
mLinkMgr->setLocalAddress(addr);
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres info : peer id not found in friend list id: " << id << std::endl;
|
2011-07-11 12:11:58 +00:00
|
|
|
#endif
|
2011-07-09 09:36:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "it" points to peer */
|
2011-07-09 18:39:34 +00:00
|
|
|
it->second.localaddr = addr;
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
//update ip address list
|
|
|
|
IpAddressTimed ipAddressTimed;
|
|
|
|
ipAddressTimed.ipAddr = addr;
|
|
|
|
ipAddressTimed.seenTime = time(NULL);
|
|
|
|
it->second.updateIpAddressList(ipAddressTimed);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::setExtAddress(const std::string &id, struct sockaddr_in addr)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
if (id == AuthSSL::getAuthSSL()->OwnId())
|
|
|
|
{
|
2011-07-09 18:39:34 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
mOwnState.serveraddr = addr;
|
|
|
|
}
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
mNetMgr->setExtAddress(addr);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres info : peer id not found in friend list id: " << id << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "it" points to peer */
|
2011-07-09 18:39:34 +00:00
|
|
|
it->second.serveraddr = addr;
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
//update ip address list
|
|
|
|
IpAddressTimed ipAddressTimed;
|
|
|
|
ipAddressTimed.ipAddr = addr;
|
|
|
|
ipAddressTimed.seenTime = time(NULL);
|
|
|
|
it->second.updateIpAddressList(ipAddressTimed);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::setDynDNS(const std::string &id, const std::string &dyndns)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
if (id == AuthSSL::getAuthSSL()->OwnId())
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
mOwnState.dyndns = dyndns;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setDynDNS() cannot add dyn dns info : peer id not found in friend list id: " << id << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "it" points to peer */
|
|
|
|
it->second.dyndns = dyndns;
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::updateAddressList(const std::string& id, const pqiIpAddrSet &addrs)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setAddressList() called for id : " << id << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check if it is our own ip */
|
|
|
|
if (id == getOwnId())
|
|
|
|
{
|
|
|
|
mOwnState.ipAddrs.updateAddrs(addrs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres info : peer id not found in friend list. id: " << id << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "it" points to peer */
|
|
|
|
it->second.ipAddrs.updateAddrs(addrs);
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setLocalAddress() Updated Address for: " << id;
|
2011-07-09 09:36:37 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
it->second.ipAddrs.printAddrs(std::cerr);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::updateCurrentAddress(const std::string& id, const pqiIpAddress &addr)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::updateCurrentAddress() called for id : " << id << std::endl;
|
2011-07-09 18:39:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* cannot be own id */
|
|
|
|
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-07-09 18:39:34 +00:00
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::updateCurrentAddress() ERROR peer id not found: " << id << std::endl;
|
2011-07-09 18:39:34 +00:00
|
|
|
return false;
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
2011-07-09 18:39:34 +00:00
|
|
|
}
|
2011-07-09 09:36:37 +00:00
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
if (isPrivateNet(&(addr.mAddr.sin_addr)))
|
|
|
|
{
|
|
|
|
it->second.ipAddrs.updateLocalAddrs(addr);
|
|
|
|
it->second.localaddr = addr.mAddr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->second.ipAddrs.updateExtAddrs(addr);
|
|
|
|
it->second.serveraddr = addr.mAddr;
|
|
|
|
}
|
2011-07-11 00:52:12 +00:00
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::updatedCurrentAddress() Updated Address for: " << id;
|
2011-07-09 18:39:34 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
it->second.ipAddrs.printAddrs(std::cerr);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return true;
|
2011-07-11 00:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::updateLastContact(const std::string& id)
|
2011-07-11 00:52:12 +00:00
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::updateLastContact() called for id : " << id << std::endl;
|
2011-07-11 00:52:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* cannot be own id */
|
|
|
|
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
|
|
|
{
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::updateLastContact() ERROR peer id not found: " << id << std::endl;
|
2011-07-11 00:52:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-09 18:39:34 +00:00
|
|
|
}
|
2011-07-11 00:52:12 +00:00
|
|
|
|
|
|
|
it->second.lastcontact = time(NULL);
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::setNetworkMode(const std::string &id, uint32_t netMode)
|
2011-07-09 18:39:34 +00:00
|
|
|
{
|
|
|
|
if (id == AuthSSL::getAuthSSL()->OwnId())
|
|
|
|
{
|
|
|
|
setOwnNetworkMode(netMode);
|
2011-07-09 09:36:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "it" points to peer */
|
|
|
|
it->second.netMode = netMode;
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::setLocation(const std::string &id, const std::string &location)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setLocation() called for id : " << id << "; with location " << location << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
if (id == AuthSSL::getAuthSSL()->OwnId())
|
|
|
|
{
|
|
|
|
mOwnState.location = location;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id))) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
it->second.location = location;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::setVisState(const std::string &id, uint32_t visState)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
2011-08-07 21:11:00 +00:00
|
|
|
{
|
2012-04-14 22:38:24 +00:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "p3PeerMgr::setVisState(%s, %lu)", id.c_str(), visState);
|
|
|
|
rslog(RSL_WARNING, p3peermgrzone, out);
|
2011-08-07 21:11:00 +00:00
|
|
|
}
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
if (id == AuthSSL::getAuthSSL()->OwnId())
|
|
|
|
{
|
2011-07-09 18:39:34 +00:00
|
|
|
setOwnVisState(visState);
|
2011-07-09 09:36:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dht_state ;
|
|
|
|
bool isFriend = false;
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check if it is a friend */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
if (mOthersList.end() == (it = mOthersList.find(id)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isFriend = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "it" points to peer */
|
|
|
|
it->second.visState = visState;
|
|
|
|
dht_state = it->second.visState & RS_VIS_STATE_NODHT ;
|
2011-07-30 12:18:56 +00:00
|
|
|
|
|
|
|
std::cerr << "p3PeerMgrIMPL::setVisState(" << id << ", " << std::hex << visState << std::dec << ") ";
|
|
|
|
std::cerr << " NAME: " << it->second.name;
|
|
|
|
|
|
|
|
if (it->second.visState & RS_VIS_STATE_NODHT)
|
|
|
|
{
|
|
|
|
std::cerr << " NO-DHT ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << " DHT-OK ";
|
|
|
|
}
|
|
|
|
if (it->second.visState & RS_VIS_STATE_NODISC)
|
|
|
|
{
|
|
|
|
std::cerr << " NO-DISC ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << " DISC-OK ";
|
|
|
|
}
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
if(isFriend)
|
|
|
|
{
|
|
|
|
/* toggle DHT state */
|
|
|
|
if(dht_state)
|
|
|
|
{
|
2011-07-30 12:18:56 +00:00
|
|
|
|
|
|
|
std::cerr << "p3PeerMgrIMPL::setVisState() setFriendVisibility => false";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
/* hidden from DHT world */
|
2011-07-09 18:39:34 +00:00
|
|
|
mLinkMgr->setFriendVisibility(id, false);
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-30 12:18:56 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::setVisState() setFriendVisibility => true";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
mLinkMgr->setFriendVisibility(id, true);
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
**********************************************************************
|
|
|
|
******************** p3Config functions ******************************
|
|
|
|
**********************************************************************
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/* Key Functions to be overloaded for Full Configuration */
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
RsSerialiser *p3PeerMgrIMPL::setupSerialiser()
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
rss->addSerialType(new RsPeerConfigSerialiser());
|
|
|
|
rss->addSerialType(new RsGeneralConfigSerialiser()) ;
|
|
|
|
|
|
|
|
return rss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list<RsItem *>& saveData)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
/* create a list of current peers */
|
|
|
|
cleanup = false;
|
2011-07-09 18:39:34 +00:00
|
|
|
bool useExtAddrFinder = mNetMgr->getIPServersEnabled();
|
|
|
|
bool allowTunnelConnection = mLinkMgr->getTunnelConnection();
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
mPeerMtx.lock(); /****** MUTEX LOCKED *******/
|
|
|
|
|
|
|
|
RsPeerNetItem *item = new RsPeerNetItem();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
item->pid = getOwnId();
|
|
|
|
item->gpg_id = mOwnState.gpg_id;
|
|
|
|
item->location = mOwnState.location;
|
2011-07-30 12:18:56 +00:00
|
|
|
#if 0
|
2011-07-09 09:36:37 +00:00
|
|
|
if (mOwnState.netMode & RS_NET_MODE_TRY_EXT)
|
|
|
|
{
|
|
|
|
item->netMode = RS_NET_MODE_EXT;
|
|
|
|
}
|
|
|
|
else if (mOwnState.netMode & RS_NET_MODE_TRY_UPNP)
|
|
|
|
{
|
|
|
|
item->netMode = RS_NET_MODE_UPNP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->netMode = RS_NET_MODE_UDP;
|
|
|
|
}
|
2011-07-30 12:18:56 +00:00
|
|
|
#endif
|
|
|
|
item->netMode = mOwnState.netMode;
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
item->visState = mOwnState.visState;
|
|
|
|
item->lastContact = mOwnState.lastcontact;
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
item->currentlocaladdr = mOwnState.localaddr;
|
|
|
|
item->currentremoteaddr = mOwnState.serveraddr;
|
2011-07-09 09:36:37 +00:00
|
|
|
item->dyndns = mOwnState.dyndns;
|
|
|
|
mOwnState.ipAddrs.mLocal.loadTlv(item->localAddrList);
|
|
|
|
mOwnState.ipAddrs.mExt.loadTlv(item->extAddrList);
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::saveList() Own Config Item:" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
item->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
saveData.push_back(item);
|
|
|
|
saveCleanupList.push_back(item);
|
|
|
|
|
|
|
|
/* iterate through all friends and save */
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
item = new RsPeerNetItem();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
item->pid = it->first;
|
2011-07-09 18:39:34 +00:00
|
|
|
item->gpg_id = (it->second).gpg_id;
|
|
|
|
item->location = (it->second).location;
|
|
|
|
item->netMode = (it->second).netMode;
|
2011-07-09 09:36:37 +00:00
|
|
|
item->visState = (it->second).visState;
|
|
|
|
item->lastContact = (it->second).lastcontact;
|
2011-07-09 18:39:34 +00:00
|
|
|
item->currentlocaladdr = (it->second).localaddr;
|
|
|
|
item->currentremoteaddr = (it->second).serveraddr;
|
|
|
|
item->dyndns = (it->second).dyndns;
|
|
|
|
(it->second).ipAddrs.mLocal.loadTlv(item->localAddrList);
|
|
|
|
(it->second).ipAddrs.mExt.loadTlv(item->extAddrList);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
saveData.push_back(item);
|
|
|
|
saveCleanupList.push_back(item);
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::saveList() Peer Config Item:" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
item->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
RsPeerServicePermissionItem *sitem = new RsPeerServicePermissionItem ;
|
|
|
|
|
|
|
|
for(std::map<std::string,ServicePermissionFlags>::const_iterator it(mFriendsPermissionFlags.begin());it!=mFriendsPermissionFlags.end();++it)
|
|
|
|
{
|
|
|
|
sitem->pgp_ids.push_back(it->first) ;
|
|
|
|
sitem->service_flags.push_back(it->second) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveData.push_back(sitem) ;
|
|
|
|
saveCleanupList.push_back(sitem);
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
// Now save config for network digging strategies
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet ;
|
|
|
|
|
|
|
|
RsTlvKeyValue kv;
|
|
|
|
kv.key = "USE_EXTR_IP_FINDER" ;
|
2011-07-09 18:39:34 +00:00
|
|
|
kv.value = (useExtAddrFinder)?"TRUE":"FALSE" ;
|
2011-07-09 09:36:37 +00:00
|
|
|
vitem->tlvkvs.pairs.push_back(kv) ;
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
|
|
|
std::cout << "Pushing item for use_extr_addr_finder = " << useExtAddrFinder << std::endl ;
|
|
|
|
#endif
|
2011-07-09 09:36:37 +00:00
|
|
|
saveData.push_back(vitem);
|
|
|
|
saveCleanupList.push_back(vitem);
|
|
|
|
|
|
|
|
// Now save config for network digging strategies
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *vitem2 = new RsConfigKeyValueSet ;
|
|
|
|
|
|
|
|
RsTlvKeyValue kv2;
|
|
|
|
kv2.key = "ALLOW_TUNNEL_CONNECTION" ;
|
2011-07-09 18:39:34 +00:00
|
|
|
kv2.value = (allowTunnelConnection)?"TRUE":"FALSE" ;
|
2011-07-09 09:36:37 +00:00
|
|
|
vitem2->tlvkvs.pairs.push_back(kv2) ;
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
|
|
|
std::cout << "Pushing item for allow_tunnel_connection = " << allowTunnelConnection << std::endl ;
|
|
|
|
#endif
|
2011-07-09 09:36:37 +00:00
|
|
|
saveData.push_back(vitem2);
|
|
|
|
saveCleanupList.push_back(vitem2);
|
|
|
|
|
|
|
|
/* save groups */
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem *>::iterator groupIt;
|
|
|
|
for (groupIt = groupList.begin(); groupIt != groupList.end(); groupIt++) {
|
|
|
|
saveData.push_back(*groupIt); // no delete
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
void p3PeerMgrIMPL::saveDone()
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
/* clean up the save List */
|
|
|
|
std::list<RsItem *>::iterator it;
|
|
|
|
for(it = saveCleanupList.begin(); it != saveCleanupList.end(); it++)
|
|
|
|
{
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveCleanupList.clear();
|
|
|
|
|
|
|
|
/* unlock mutex */
|
|
|
|
mPeerMtx.unlock(); /****** MUTEX UNLOCKED *******/
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::loadList(std::list<RsItem *>& load)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
// DEFAULTS.
|
|
|
|
bool useExtAddrFinder = true;
|
|
|
|
bool allowTunnelConnection = true;
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
if (load.size() == 0) {
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::loadList() list is empty, it may be a configuration problem." << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::loadList() Item Count: " << load.size() << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string ownId = getOwnId();
|
|
|
|
|
|
|
|
/* load the list of peers */
|
|
|
|
std::list<RsItem *>::iterator it;
|
|
|
|
for(it = load.begin(); it != load.end(); it++)
|
|
|
|
{
|
|
|
|
RsPeerNetItem *pitem = dynamic_cast<RsPeerNetItem *>(*it);
|
|
|
|
if (pitem)
|
|
|
|
{
|
|
|
|
if (pitem->pid == ownId)
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::loadList() Own Config Item:" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
pitem->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* add ownConfig */
|
2011-07-09 18:39:34 +00:00
|
|
|
setOwnNetworkMode(pitem->netMode);
|
|
|
|
setOwnVisState(pitem->visState);
|
|
|
|
|
|
|
|
mOwnState.gpg_id = AuthGPG::getAuthGPG()->getGPGOwnId();
|
|
|
|
mOwnState.location = AuthSSL::getAuthSSL()->getOwnLocation();
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::loadList() Peer Config Item:" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
pitem->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* ************* */
|
2012-12-01 19:22:22 +00:00
|
|
|
addFriend(pitem->pid, pitem->gpg_id, pitem->netMode, pitem->visState, pitem->lastContact, RS_SERVICE_PERM_ALL);
|
2011-07-09 18:39:34 +00:00
|
|
|
setLocation(pitem->pid, pitem->location);
|
|
|
|
}
|
|
|
|
|
|
|
|
setLocalAddress(pitem->pid, pitem->currentlocaladdr);
|
|
|
|
setExtAddress(pitem->pid, pitem->currentremoteaddr);
|
|
|
|
setDynDNS (pitem->pid, pitem->dyndns);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
/* convert addresses */
|
|
|
|
pqiIpAddrSet addrs;
|
|
|
|
addrs.mLocal.extractFromTlv(pitem->localAddrList);
|
|
|
|
addrs.mExt.extractFromTlv(pitem->extAddrList);
|
2011-07-09 18:39:34 +00:00
|
|
|
|
|
|
|
updateAddressList(pitem->pid, addrs);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
delete(*it);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *vitem = dynamic_cast<RsConfigKeyValueSet *>(*it) ;
|
|
|
|
if (vitem)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::loadList() General Variable Config Item:" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
vitem->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
std::list<RsTlvKeyValue>::iterator kit;
|
|
|
|
for(kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); kit++) {
|
|
|
|
if(kit->key == "USE_EXTR_IP_FINDER") {
|
2011-07-09 18:39:34 +00:00
|
|
|
useExtAddrFinder = (kit->value == "TRUE");
|
|
|
|
std::cerr << "setting use_extr_addr_finder to " << useExtAddrFinder << std::endl ;
|
2011-07-09 09:36:37 +00:00
|
|
|
} else if (kit->key == "ALLOW_TUNNEL_CONNECTION") {
|
2011-07-09 18:39:34 +00:00
|
|
|
allowTunnelConnection = (kit->value == "TRUE");
|
|
|
|
std::cerr << "setting allow_tunnel_connection to " << allowTunnelConnection << std::endl ;
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(*it);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsPeerGroupItem *gitem = dynamic_cast<RsPeerGroupItem *>(*it) ;
|
|
|
|
if (gitem)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-11 12:11:58 +00:00
|
|
|
#ifdef PEER_DEBUG
|
2011-07-13 14:22:25 +00:00
|
|
|
std::cerr << "p3PeerMgrIMPL::loadList() Peer group item:" << std::endl;
|
2011-07-09 09:36:37 +00:00
|
|
|
gitem->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
groupList.push_back(gitem); // don't delete
|
|
|
|
|
|
|
|
if ((gitem->flag & RS_GROUP_FLAG_STANDARD) == 0) {
|
|
|
|
/* calculate group id */
|
|
|
|
uint32_t groupId = atoi(gitem->id.c_str());
|
|
|
|
if (groupId > lastGroupId) {
|
|
|
|
lastGroupId = groupId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-01 19:22:22 +00:00
|
|
|
RsPeerServicePermissionItem *sitem = dynamic_cast<RsPeerServicePermissionItem*>(*it) ;
|
|
|
|
|
|
|
|
if(sitem)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::cerr << "Loaded service permission item: " << std::endl;
|
|
|
|
|
|
|
|
for(uint32_t i=0;i<sitem->pgp_ids.size();++i)
|
|
|
|
if(AuthGPG::getAuthGPG()->isGPGAccepted(sitem->pgp_ids[i]))
|
|
|
|
{
|
|
|
|
mFriendsPermissionFlags[sitem->pgp_ids[i]] = sitem->service_flags[i] ;
|
|
|
|
std::cerr << " " << sitem->pgp_ids[i] << " - " << sitem->service_flags[i] << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
std::cerr << " " << sitem->pgp_ids[i] << " - Not a friend!" << std::endl;
|
|
|
|
}
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* set missing groupIds */
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* Standard groups */
|
|
|
|
const int standardGroupCount = 5;
|
|
|
|
const char *standardGroup[standardGroupCount] = { RS_GROUP_ID_FRIENDS, RS_GROUP_ID_FAMILY, RS_GROUP_ID_COWORKERS, RS_GROUP_ID_OTHERS, RS_GROUP_ID_FAVORITES };
|
|
|
|
bool foundStandardGroup[standardGroupCount] = { false, false, false, false, false };
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem *>::iterator groupIt;
|
|
|
|
for (groupIt = groupList.begin(); groupIt != groupList.end(); groupIt++) {
|
|
|
|
if ((*groupIt)->flag & RS_GROUP_FLAG_STANDARD) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < standardGroupCount; i++) {
|
|
|
|
if ((*groupIt)->id == standardGroup[i]) {
|
|
|
|
foundStandardGroup[i] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= standardGroupCount) {
|
|
|
|
/* No more a standard group, remove the flag standard */
|
|
|
|
(*groupIt)->flag &= ~RS_GROUP_FLAG_STANDARD;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32_t groupId = atoi((*groupIt)->id.c_str());
|
|
|
|
if (groupId == 0) {
|
2012-04-14 22:38:24 +00:00
|
|
|
rs_sprintf((*groupIt)->id, "%lu", lastGroupId++);
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize standard groups */
|
|
|
|
for (int i = 0; i < standardGroupCount; i++) {
|
|
|
|
if (foundStandardGroup[i] == false) {
|
|
|
|
RsPeerGroupItem *gitem = new RsPeerGroupItem;
|
|
|
|
gitem->id = standardGroup[i];
|
|
|
|
gitem->name = standardGroup[i];
|
|
|
|
gitem->flag |= RS_GROUP_FLAG_STANDARD;
|
|
|
|
groupList.push_back(gitem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
mNetMgr->setIPServersEnabled(useExtAddrFinder);
|
|
|
|
mLinkMgr->setTunnelConnection(allowTunnelConnection);
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
#if 0
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
void printConnectState(std::ostream &out, peerState &peer)
|
|
|
|
{
|
|
|
|
|
|
|
|
out << "Friend: " << peer.name << " Id: " << peer.id << " State: " << peer.state;
|
|
|
|
if (peer.state & RS_PEER_S_FRIEND)
|
|
|
|
out << " S:RS_PEER_S_FRIEND";
|
|
|
|
if (peer.state & RS_PEER_S_ONLINE)
|
|
|
|
out << " S:RS_PEER_S_ONLINE";
|
|
|
|
if (peer.state & RS_PEER_S_CONNECTED)
|
|
|
|
out << " S:RS_PEER_S_CONNECTED";
|
|
|
|
out << " Actions: " << peer.actions;
|
|
|
|
if (peer.actions & RS_PEER_NEW)
|
|
|
|
out << " A:RS_PEER_NEW";
|
|
|
|
if (peer.actions & RS_PEER_MOVED)
|
|
|
|
out << " A:RS_PEER_MOVED";
|
|
|
|
if (peer.actions & RS_PEER_CONNECTED)
|
|
|
|
out << " A:RS_PEER_CONNECTED";
|
|
|
|
if (peer.actions & RS_PEER_DISCONNECTED)
|
|
|
|
out << " A:RS_PEER_DISCONNECTED";
|
|
|
|
if (peer.actions & RS_PEER_CONNECT_REQ)
|
|
|
|
out << " A:RS_PEER_CONNECT_REQ";
|
|
|
|
|
|
|
|
out << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
#endif
|
|
|
|
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
**********************************************************************
|
|
|
|
************************** Groups ************************************
|
|
|
|
**********************************************************************
|
|
|
|
**********************************************************************/
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::addGroup(RsGroupInfo &groupInfo)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
RsPeerGroupItem *groupItem = new RsPeerGroupItem;
|
|
|
|
groupItem->set(groupInfo);
|
|
|
|
|
2012-04-14 22:38:24 +00:00
|
|
|
rs_sprintf(groupItem->id, "%lu", ++lastGroupId);
|
2011-07-09 09:36:37 +00:00
|
|
|
|
|
|
|
// remove standard flag
|
|
|
|
groupItem->flag &= ~RS_GROUP_FLAG_STANDARD;
|
|
|
|
|
|
|
|
groupItem->PeerId(getOwnId());
|
|
|
|
|
|
|
|
groupList.push_back(groupItem);
|
2012-11-24 14:48:31 +00:00
|
|
|
|
|
|
|
groupInfo.id = groupItem->id;
|
2011-07-09 09:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_GROUPLIST, NOTIFY_TYPE_ADD);
|
|
|
|
|
|
|
|
IndicateConfigChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::editGroup(const std::string &groupId, RsGroupInfo &groupInfo)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
if (groupId.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem*>::iterator groupIt;
|
|
|
|
for (groupIt = groupList.begin(); groupIt != groupList.end(); groupIt++) {
|
|
|
|
if ((*groupIt)->id == groupId) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupIt != groupList.end()) {
|
|
|
|
if ((*groupIt)->flag & RS_GROUP_FLAG_STANDARD) {
|
|
|
|
// can't edit standard groups
|
|
|
|
} else {
|
|
|
|
changed = true;
|
|
|
|
(*groupIt)->set(groupInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_GROUPLIST, NOTIFY_TYPE_MOD);
|
|
|
|
|
|
|
|
IndicateConfigChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::removeGroup(const std::string &groupId)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
if (groupId.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem*>::iterator groupIt;
|
|
|
|
for (groupIt = groupList.begin(); groupIt != groupList.end(); groupIt++) {
|
|
|
|
if ((*groupIt)->id == groupId) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupIt != groupList.end()) {
|
|
|
|
if ((*groupIt)->flag & RS_GROUP_FLAG_STANDARD) {
|
|
|
|
// can't remove standard groups
|
|
|
|
} else {
|
|
|
|
changed = true;
|
|
|
|
delete(*groupIt);
|
|
|
|
groupList.erase(groupIt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_GROUPLIST, NOTIFY_TYPE_DEL);
|
|
|
|
|
|
|
|
IndicateConfigChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::getGroupInfo(const std::string &groupId, RsGroupInfo &groupInfo)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
if (groupId.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem*>::iterator groupIt;
|
|
|
|
for (groupIt = groupList.begin(); groupIt != groupList.end(); groupIt++) {
|
|
|
|
if ((*groupIt)->id == groupId) {
|
|
|
|
(*groupIt)->get(groupInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::getGroupInfoList(std::list<RsGroupInfo> &groupInfoList)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem*>::iterator groupIt;
|
|
|
|
for (groupIt = groupList.begin(); groupIt != groupList.end(); groupIt++) {
|
|
|
|
RsGroupInfo groupInfo;
|
|
|
|
(*groupIt)->get(groupInfo);
|
|
|
|
groupInfoList.push_back(groupInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// groupId == "" && assign == false -> remove from all groups
|
2011-07-13 14:22:25 +00:00
|
|
|
bool p3PeerMgrIMPL::assignPeersToGroup(const std::string &groupId, const std::list<std::string> &peerIds, bool assign)
|
2011-07-09 09:36:37 +00:00
|
|
|
{
|
|
|
|
if (groupId.empty() && assign == true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peerIds.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem*>::iterator groupIt;
|
|
|
|
for (groupIt = groupList.begin(); groupIt != groupList.end(); groupIt++) {
|
|
|
|
if (groupId.empty() || (*groupIt)->id == groupId) {
|
|
|
|
RsPeerGroupItem *groupItem = *groupIt;
|
|
|
|
|
|
|
|
std::list<std::string>::const_iterator peerIt;
|
|
|
|
for (peerIt = peerIds.begin(); peerIt != peerIds.end(); peerIt++) {
|
|
|
|
std::list<std::string>::iterator peerIt1 = std::find(groupItem->peerIds.begin(), groupItem->peerIds.end(), *peerIt);
|
|
|
|
if (assign) {
|
|
|
|
if (peerIt1 == groupItem->peerIds.end()) {
|
|
|
|
groupItem->peerIds.push_back(*peerIt);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (peerIt1 != groupItem->peerIds.end()) {
|
|
|
|
groupItem->peerIds.erase(peerIt1);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupId.empty() == false) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_GROUPLIST, NOTIFY_TYPE_MOD);
|
|
|
|
|
|
|
|
IndicateConfigChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
2011-08-07 21:11:00 +00:00
|
|
|
|
|
|
|
|
2012-12-01 19:22:22 +00:00
|
|
|
/**********************************************************************
|
|
|
|
**********************************************************************
|
|
|
|
******************** Service permission stuff ************************
|
|
|
|
**********************************************************************
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
ServicePermissionFlags p3PeerMgrIMPL::servicePermissionFlags_sslid(const std::string& ssl_id)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerState>::const_iterator it = mFriendList.find(ssl_id);
|
|
|
|
|
|
|
|
if(it == mFriendList.end())
|
|
|
|
return RS_SERVICE_PERM_ALL ;
|
|
|
|
else
|
|
|
|
return servicePermissionFlags(it->second.gpg_id) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ServicePermissionFlags p3PeerMgrIMPL::servicePermissionFlags(const std::string& pgp_id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
std::map<std::string,ServicePermissionFlags>::const_iterator it = mFriendsPermissionFlags.find( pgp_id ) ;
|
|
|
|
|
|
|
|
if(it == mFriendsPermissionFlags.end())
|
|
|
|
return RS_SERVICE_PERM_ALL ;
|
|
|
|
else
|
|
|
|
return it->second ;
|
|
|
|
}
|
|
|
|
void p3PeerMgrIMPL::setServicePermissionFlags(const std::string& pgp_id, const ServicePermissionFlags& flags)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
// Check that we have a PGP id. This should not be necessary, but because
|
|
|
|
// we use std::string, anything can get passed down here.
|
|
|
|
//
|
|
|
|
if(pgp_id.length() != 16)
|
|
|
|
{
|
|
|
|
std::cerr << "Bad parameter passed to setServicePermissionFlags(): " << pgp_id << std::endl;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFriendsPermissionFlags[pgp_id] = flags ;
|
|
|
|
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
|
|
|
|
}
|
|
|
|
|
2011-08-07 21:11:00 +00:00
|
|
|
/**********************************************************************
|
|
|
|
**********************************************************************
|
|
|
|
******************** Stuff moved from p3peers ************************
|
|
|
|
**********************************************************************
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
bool p3PeerMgrIMPL::removeAllFriendLocations(const std::string &gpgid)
|
|
|
|
{
|
|
|
|
std::list<std::string> sslIds;
|
|
|
|
if (!getAssociatedPeers(gpgid, sslIds))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
for(it = sslIds.begin(); it != sslIds.end(); it++)
|
|
|
|
{
|
|
|
|
removeFriend(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3PeerMgrIMPL::getAssociatedPeers(const std::string &gpg_id, std::list<std::string> &ids)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
#ifdef P3PEERS_DEBUG
|
|
|
|
std::cerr << "p3PeerMgr::getAssociatedPeers() for id : " << gpg_id << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.gpg_id == gpg_id)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
ids.push_back(it->first);
|
|
|
|
|
|
|
|
#ifdef P3PEERS_DEBUG
|
|
|
|
std::cerr << "p3PeerMgr::getAssociatedPeers() found ssl id : " << it->first << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (count > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This only removes SSL certs, that are old... Can end up with no Certs per GPG Id
|
|
|
|
* We are removing the concept of a "DummyId" - There is no need for it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool isDummyFriend(std::string id)
|
|
|
|
{
|
|
|
|
bool ret = (id.substr(0,5) == "dummy");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3PeerMgrIMPL::removeUnusedLocations()
|
|
|
|
{
|
|
|
|
std::list<std::string> toRemove;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
#ifdef P3PEERS_DEBUG
|
|
|
|
std::cerr << "p3PeerMgr::removeUnusedLocations()" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
std::map<std::string, peerState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (now - it->second.lastcontact > VERY_OLD_PEER)
|
|
|
|
{
|
|
|
|
toRemove.push_back(it->first);
|
|
|
|
|
|
|
|
#ifdef P3PEERS_DEBUG
|
|
|
|
std::cerr << "p3PeerMgr::removeUnusedLocations() removing Old SSL Id: " << it->first << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDummyFriend(it->first))
|
|
|
|
{
|
|
|
|
toRemove.push_back(it->first);
|
|
|
|
|
|
|
|
#ifdef P3PEERS_DEBUG
|
|
|
|
std::cerr << "p3PeerMgr::removeUnusedLocations() removing Dummy Id: " << it->first << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
|
|
|
|
for(it = toRemove.begin(); it != toRemove.end(); it++)
|
|
|
|
{
|
|
|
|
removeFriend(*it);
|
|
|
|
}
|
2011-08-08 11:27:40 +00:00
|
|
|
|
|
|
|
return true;
|
2011-08-07 21:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|