2011-07-09 05:36:37 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/pqi: p3linkmgr.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 "pqi/p3linkmgr.h"
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
#include "pqi/p3peermgr.h"
|
|
|
|
#include "pqi/p3netmgr.h"
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
#include "pqi/authssl.h"
|
|
|
|
#include "pqi/p3dhtmgr.h" // Only need it for constants.
|
|
|
|
#include "tcponudp/tou.h"
|
|
|
|
#include "util/extaddrfinder.h"
|
|
|
|
#include "util/dnsresolver.h"
|
|
|
|
#include "util/rsnet.h"
|
|
|
|
#include "pqi/authgpg.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "util/rsprint.h"
|
|
|
|
#include "util/rsdebug.h"
|
|
|
|
const int p3connectzone = 3431;
|
|
|
|
|
|
|
|
#include "serialiser/rsconfigitems.h"
|
|
|
|
#include "pqi/pqinotify.h"
|
|
|
|
#include "retroshare/rsiface.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
/* Network setup States */
|
|
|
|
|
|
|
|
|
|
|
|
/****
|
2011-07-10 14:19:46 -04:00
|
|
|
* #define LINKMGR_DEBUG 1
|
2011-07-10 20:52:12 -04:00
|
|
|
* #define LINKMGR_DEBUG_CONNFAIL 1
|
2011-07-11 08:11:58 -04:00
|
|
|
* #define LINKMGR_DEBUG_ACTIONS 1
|
2011-07-09 05:36:37 -04:00
|
|
|
***/
|
|
|
|
|
2011-07-18 04:37:11 -04:00
|
|
|
/****
|
|
|
|
* #define DISABLE_UDP_CONNECTIONS 1
|
|
|
|
***/
|
2011-07-10 14:19:46 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
/****
|
|
|
|
* #define P3CONNMGR_NO_TCP_CONNECTIONS 1
|
|
|
|
***/
|
|
|
|
/****
|
|
|
|
* #define P3CONNMGR_NO_AUTO_CONNECTION 1
|
|
|
|
***/
|
|
|
|
|
2011-07-18 20:17:54 -04:00
|
|
|
//#define P3CONNMGR_NO_TCP_CONNECTIONS 1
|
2011-07-18 20:06:09 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
const uint32_t P3CONNMGR_TCP_DEFAULT_DELAY = 3; /* 2 Seconds? is it be enough! */
|
|
|
|
const uint32_t P3CONNMGR_UDP_DEFAULT_DELAY = 3; /* 2 Seconds? is it be enough! */
|
|
|
|
|
|
|
|
const uint32_t P3CONNMGR_TCP_DEFAULT_PERIOD = 10;
|
2011-07-18 20:06:09 -04:00
|
|
|
const uint32_t P3CONNMGR_UDP_DEFAULT_PERIOD = 30; // this represents how long it stays at the default TTL (4), before rising.
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
#define MAX_AVAIL_PERIOD 230 //times a peer stay in available state when not connected
|
|
|
|
#define MIN_RETRY_PERIOD 140
|
|
|
|
|
|
|
|
#define MAX_RANDOM_ATTEMPT_OFFSET 6 // seconds.
|
|
|
|
|
|
|
|
void printConnectState(std::ostream &out, peerConnectState &peer);
|
|
|
|
|
|
|
|
peerConnectAddress::peerConnectAddress()
|
|
|
|
:delay(0), period(0), type(0), ts(0)
|
|
|
|
{
|
|
|
|
sockaddr_clear(&addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
peerAddrInfo::peerAddrInfo()
|
|
|
|
:found(false), type(0), ts(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
peerConnectState::peerConnectState()
|
|
|
|
:id("unknown"),
|
|
|
|
connecttype(0),
|
|
|
|
lastavailable(0),
|
|
|
|
lastattempt(0),
|
2011-07-09 14:39:34 -04:00
|
|
|
name(""),
|
2011-07-09 05:36:37 -04:00
|
|
|
state(0), actions(0),
|
|
|
|
source(0),
|
|
|
|
inConnAttempt(0)
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
//sockaddr_clear(¤tlocaladdr);
|
|
|
|
//sockaddr_clear(¤tserveraddr);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string textPeerConnectState(peerConnectState &state)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "Id: " << state.id << std::endl;
|
2011-07-09 14:39:34 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
std::string output = out.str();
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
/*********
|
|
|
|
* NOTES:
|
|
|
|
*
|
|
|
|
* p3LinkMgr doesn't store anything. All configuration is handled by p3PeerMgr.
|
|
|
|
*
|
|
|
|
* p3LinkMgr recvs the Discovery / Dht / Status updates.... tries the address.
|
|
|
|
* at success the address is pushed to p3PeerMgr for storage.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
p3LinkMgrIMPL::p3LinkMgrIMPL(p3PeerMgrIMPL *peerMgr, p3NetMgrIMPL *netMgr)
|
2011-07-09 14:39:34 -04:00
|
|
|
:mPeerMgr(peerMgr), mNetMgr(netMgr), mLinkMtx("p3LinkMgr"),mStatusChanged(false)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
mAllowTunnelConnection = false;
|
2011-07-09 14:39:34 -04:00
|
|
|
mDNSResolver = new DNSResolver();
|
2011-07-09 05:36:37 -04:00
|
|
|
mRetryPeriod = MIN_RETRY_PERIOD;
|
|
|
|
|
|
|
|
lastGroupId = 1;
|
|
|
|
|
|
|
|
/* setup Banned Ip Address - static for now
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct in_addr bip;
|
|
|
|
memset(&bip, 0, sizeof(bip));
|
|
|
|
bip.s_addr = 1;
|
|
|
|
|
|
|
|
mBannedIpList.push_back(bip);
|
|
|
|
}
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "p3LinkMgr() Startup" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::setTunnelConnection(bool b)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
mAllowTunnelConnection = b;
|
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::getTunnelConnection()
|
2011-07-09 14:39:34 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
return mAllowTunnelConnection;
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::setLocalAddress(struct sockaddr_in addr)
|
2011-07-09 21:49:39 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
mLocalAddress = addr;
|
2011-07-19 14:55:32 -04:00
|
|
|
|
|
|
|
return true ;
|
2011-07-09 21:49:39 -04:00
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
struct sockaddr_in p3LinkMgrIMPL::getLocalAddress()
|
2011-07-09 21:49:39 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
return mLocalAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::isOnline(const std::string &ssl_id)
|
2011-07-09 21:49:39 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
it = mFriendList.find(ssl_id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::getOnlineList(std::list<std::string> &ssl_peers)
|
2011-07-09 14:39:34 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
|
|
|
ssl_peers.push_back(it->first);
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
2011-07-09 14:39:34 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::getFriendList(std::list<std::string> &ssl_peers)
|
2011-07-09 14:39:34 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
ssl_peers.push_back(it->first);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
2011-07-09 14:39:34 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
2011-08-07 17:11:00 -04:00
|
|
|
bool p3LinkMgrIMPL::getPeerName(const std::string &ssl_id, std::string &name)
|
|
|
|
{
|
|
|
|
return mPeerMgr->getPeerName(ssl_id, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
int p3LinkMgrIMPL::getFriendCount()
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
2011-07-09 14:39:34 -04:00
|
|
|
|
|
|
|
return mFriendList.size();
|
|
|
|
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
int p3LinkMgrIMPL::getOnlineCount()
|
2011-07-09 14:39:34 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::getFriendNetStatus(const std::string &id, peerConnectState &state)
|
2011-07-09 21:49:39 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::setFriendVisibility(const std::string &id, bool isVisible)
|
2011-07-09 20:41:39 -04:00
|
|
|
{
|
|
|
|
/* set visibility */
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
2011-07-09 20:41:39 -04:00
|
|
|
it = mFriendList.find(id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
/* */
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::setFriendVisibility() ERROR peer unknown: " << id;
|
2011-07-09 20:41:39 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
if (it->second.dhtVisible == isVisible)
|
|
|
|
{
|
|
|
|
/* no change in state */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
it->second.dhtVisible = isVisible;
|
|
|
|
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
|
|
|
/* dont worry about it */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
/* switch the NetAssistOn/Off */
|
|
|
|
mNetMgr->netAssistFriend(id, isVisible);
|
|
|
|
}
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::tick()
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
statusTick();
|
|
|
|
tickMonitors();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::statusTick()
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
/* iterate through peers ...
|
|
|
|
* if been available for long time ... remove flag
|
|
|
|
* if last attempt a while - retryConnect.
|
|
|
|
* etc.
|
|
|
|
*/
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_TICK
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::statusTick()" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
std::list<std::string> retryIds;
|
|
|
|
std::list<std::string>::iterator it2;
|
|
|
|
//std::list<std::string> dummyToRemove;
|
|
|
|
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t oldavail = now - MAX_AVAIL_PERIOD;
|
|
|
|
time_t retry = now - mRetryPeriod;
|
|
|
|
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** LOCK MUTEX ******/
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((it->second.state & RS_PEER_S_ONLINE) &&
|
|
|
|
(it->second.lastavailable < oldavail))
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_TICK
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::statusTick() ONLINE TIMEOUT for: ";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << it->first;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
it->second.state &= (~RS_PEER_S_ONLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it->second.lastattempt < retry)
|
|
|
|
{
|
|
|
|
retryIds.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef P3CONNMGR_NO_AUTO_CONNECTION
|
|
|
|
|
|
|
|
for(it2 = retryIds.begin(); it2 != retryIds.end(); it2++)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_TICK
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::statusTick() RETRY TIMEOUT for: ";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << *it2;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* retry it! */
|
|
|
|
retryConnect(*it2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************** Network Status *********************************
|
|
|
|
* Configuration Loading / Saving.
|
|
|
|
*/
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::addMonitor(pqiMonitor *mon)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::list<pqiMonitor *>::iterator it;
|
|
|
|
it = std::find(clients.begin(), clients.end(), mon);
|
|
|
|
if (it != clients.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mon->setLinkMgr(this);
|
|
|
|
clients.push_back(mon);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::removeMonitor(pqiMonitor *mon)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::list<pqiMonitor *>::iterator it;
|
|
|
|
it = std::find(clients.begin(), clients.end(), mon);
|
|
|
|
if (it == clients.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(*it)->setLinkMgr(NULL);
|
|
|
|
clients.erase(it);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::tickMonitors()
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
bool doStatusChange = false;
|
|
|
|
std::list<pqipeer> actionList;
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
if (mStatusChanged)
|
|
|
|
{
|
2011-07-11 08:11:58 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_ACTIONS
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::tickMonitors() StatusChanged! List:" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
/* assemble list */
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.actions)
|
|
|
|
{
|
|
|
|
/* add in */
|
|
|
|
pqipeer peer;
|
|
|
|
peer.id = it->second.id;
|
|
|
|
peer.name = it->second.name;
|
|
|
|
peer.state = it->second.state;
|
|
|
|
peer.actions = it->second.actions;
|
|
|
|
|
|
|
|
/* reset action */
|
|
|
|
it->second.actions = 0;
|
|
|
|
|
|
|
|
actionList.push_back(peer);
|
|
|
|
|
2011-07-11 08:11:58 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_ACTIONS
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Friend: " << peer.name << " Id: " << peer.id << " State: " << peer.state;
|
|
|
|
if (peer.state & RS_PEER_S_FRIEND)
|
|
|
|
std::cerr << " S:RS_PEER_S_FRIEND";
|
|
|
|
if (peer.state & RS_PEER_S_ONLINE)
|
|
|
|
std::cerr << " S:RS_PEER_S_ONLINE";
|
|
|
|
if (peer.state & RS_PEER_S_CONNECTED)
|
|
|
|
std::cerr << " S:RS_PEER_S_CONNECTED";
|
|
|
|
std::cerr << " Actions: " << peer.actions;
|
|
|
|
if (peer.actions & RS_PEER_NEW)
|
|
|
|
std::cerr << " A:RS_PEER_NEW";
|
|
|
|
if (peer.actions & RS_PEER_MOVED)
|
|
|
|
std::cerr << " A:RS_PEER_MOVED";
|
|
|
|
if (peer.actions & RS_PEER_CONNECTED)
|
|
|
|
std::cerr << " A:RS_PEER_CONNECTED";
|
|
|
|
if (peer.actions & RS_PEER_DISCONNECTED)
|
|
|
|
std::cerr << " A:RS_PEER_DISCONNECTED";
|
|
|
|
if (peer.actions & RS_PEER_CONNECT_REQ)
|
|
|
|
std::cerr << " A:RS_PEER_CONNECT_REQ";
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* notify GUI */
|
|
|
|
if (peer.actions & RS_PEER_CONNECTED)
|
|
|
|
{
|
|
|
|
pqiNotify *notify = getPqiNotify();
|
|
|
|
if (notify)
|
|
|
|
{
|
|
|
|
notify->AddPopupMessage(RS_POPUP_CONNECT, peer.id,"", "Online: ");
|
|
|
|
notify->AddFeedItem(RS_FEED_ITEM_PEER_CONNECT, peer.id, "", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 08:11:58 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
/* do the Others as well! */
|
|
|
|
for(it = mOthersList.begin(); it != mOthersList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.actions)
|
|
|
|
{
|
|
|
|
/* add in */
|
|
|
|
pqipeer peer;
|
|
|
|
peer.id = it->second.id;
|
|
|
|
peer.name = it->second.name;
|
|
|
|
peer.state = it->second.state;
|
|
|
|
peer.actions = it->second.actions;
|
|
|
|
|
|
|
|
/* reset action */
|
|
|
|
it->second.actions = 0;
|
|
|
|
|
2011-07-11 08:11:58 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_ACTIONS
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Other: " << peer.name << " Id: " << peer.id << " State: " << peer.state;
|
|
|
|
if (peer.state & RS_PEER_S_FRIEND)
|
|
|
|
std::cerr << " S:RS_PEER_S_FRIEND";
|
|
|
|
if (peer.state & RS_PEER_S_ONLINE)
|
|
|
|
std::cerr << " S:RS_PEER_S_ONLINE";
|
|
|
|
if (peer.state & RS_PEER_S_CONNECTED)
|
|
|
|
std::cerr << " S:RS_PEER_S_CONNECTED";
|
|
|
|
std::cerr << " Actions: " << peer.actions;
|
|
|
|
if (peer.actions & RS_PEER_NEW)
|
|
|
|
std::cerr << " A:RS_PEER_NEW";
|
|
|
|
if (peer.actions & RS_PEER_MOVED)
|
|
|
|
std::cerr << " A:RS_PEER_MOVED";
|
|
|
|
if (peer.actions & RS_PEER_CONNECTED)
|
|
|
|
std::cerr << " A:RS_PEER_CONNECTED";
|
|
|
|
if (peer.actions & RS_PEER_DISCONNECTED)
|
|
|
|
std::cerr << " A:RS_PEER_DISCONNECTED";
|
|
|
|
if (peer.actions & RS_PEER_CONNECT_REQ)
|
|
|
|
std::cerr << " A:RS_PEER_CONNECT_REQ";
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
actionList.push_back(peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mStatusChanged = false;
|
|
|
|
doStatusChange = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
} /****** UNLOCK STACK MUTEX ******/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* NOTE - clients is accessed without mutex protection!!!!
|
|
|
|
* At the moment this is okay - as they are only added at the start.
|
|
|
|
* IF this changes ---- must fix with second Mutex.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (doStatusChange)
|
|
|
|
{
|
2011-07-11 08:11:58 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_ACTIONS
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Sending to " << clients.size() << " monitorClients" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* send to all monitors */
|
|
|
|
std::list<pqiMonitor *>::iterator mit;
|
|
|
|
for(mit = clients.begin(); mit != clients.end(); mit++)
|
|
|
|
{
|
|
|
|
(*mit)->statusChange(actionList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
// hack for too many connections
|
|
|
|
|
|
|
|
/* notify all monitors */
|
|
|
|
std::list<pqiMonitor *>::iterator mit;
|
|
|
|
for(mit = clients.begin(); mit != clients.end(); mit++) {
|
|
|
|
(*mit)->statusChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
#endif
|
2011-07-11 08:11:58 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* Now Cleanup OthersList (served its purpose (MOVE Action)) */
|
|
|
|
mOthersList.clear();
|
|
|
|
}
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
const std::string p3LinkMgrIMPL::getOwnId()
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
return AuthSSL::getAuthSSL()->OwnId();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
bool p3LinkMgrIMPL::connectAttempt(const std::string &id, struct sockaddr_in &raddr,
|
|
|
|
struct sockaddr_in &proxyaddr,
|
|
|
|
struct sockaddr_in &srcaddr,
|
|
|
|
uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags, uint32_t &bandwidth)
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectAttempt() FAILED Not in FriendList! id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it->second.connAddrs.size() < 1)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectAttempt() FAILED No ConnectAddresses id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectAttempt() Already FLAGGED as connected!!!!" << std::endl;
|
|
|
|
std::cerr << "p3LinkMgrIMPL::connectAttempt() But allowing anyway!!!" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-08-06 08:27:23 -04:00
|
|
|
|
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectAttempt() ERROR ALREADY CONNECTED");
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
2011-08-06 08:27:23 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectAttempt() called id: " + id);
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
it->second.lastattempt = time(NULL);
|
|
|
|
it->second.inConnAttempt = true;
|
|
|
|
it->second.currentConnAddrAttempt = it->second.connAddrs.front();
|
|
|
|
it->second.connAddrs.pop_front();
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
raddr = it->second.currentConnAddrAttempt.addr;
|
2011-07-09 05:36:37 -04:00
|
|
|
delay = it->second.currentConnAddrAttempt.delay;
|
|
|
|
period = it->second.currentConnAddrAttempt.period;
|
|
|
|
type = it->second.currentConnAddrAttempt.type;
|
2011-07-13 18:23:40 -04:00
|
|
|
flags = it->second.currentConnAddrAttempt.flags;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
proxyaddr = it->second.currentConnAddrAttempt.proxyaddr;
|
|
|
|
srcaddr = it->second.currentConnAddrAttempt.srcaddr;
|
|
|
|
bandwidth = it->second.currentConnAddrAttempt.bandwidth;
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectAttempt() found an address: id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << " laddr: " << rs_inet_ntoa(addr.sin_addr) << " lport: " << ntohs(addr.sin_port) << " delay: " << delay << " period: " << period;
|
|
|
|
std::cerr << " type: " << type << std::endl;
|
|
|
|
#endif
|
2011-07-14 10:56:33 -04:00
|
|
|
if (raddr.sin_addr.s_addr == 0 || raddr.sin_port == 0) {
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectAttempt() WARNING: address or port is null" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << " type: " << type << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
* Update state,
|
|
|
|
* trigger retry if necessary,
|
|
|
|
*
|
|
|
|
* remove from DHT?
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 15:50:12 -04:00
|
|
|
bool doDhtAssist = false ;
|
2011-07-09 14:39:34 -04:00
|
|
|
bool updatePeerAddr = false;
|
2011-07-10 20:52:12 -04:00
|
|
|
bool updateLastContact = false;
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectResult() called Connect!: id: " + id);
|
2011-07-09 14:39:34 -04:00
|
|
|
if (success)
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectResult() called with SUCCESS.");
|
2011-07-09 14:39:34 -04:00
|
|
|
} else
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectResult() called with FAILED.");
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
if (id == getOwnId())
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectResult() Failed, connecting to own id: ");
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* check for existing */
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() ERROR, missing Friend " << " id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
/* update address (should also come through from DISC) */
|
2011-07-10 20:52:12 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_CONNFAIL
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Connect!: id: " << id << std::endl;
|
2011-07-10 20:52:12 -04:00
|
|
|
std::cerr << " Success: " << success << " flags: " << flags << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Connect!: id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << " Success: " << success << " flags: " << flags << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectResult() Success");
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
/* change state */
|
|
|
|
it->second.state |= RS_PEER_S_CONNECTED;
|
|
|
|
it->second.actions |= RS_PEER_CONNECTED;
|
|
|
|
it->second.connecttype = flags;
|
|
|
|
|
2011-07-10 20:52:12 -04:00
|
|
|
updateLastContact = true; /* time of connect */
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
/* only update the peer's address if we were in a connect attempt.
|
|
|
|
* Otherwise, they connected to us, and the address will be a
|
|
|
|
* random port of their outgoing TCP socket
|
|
|
|
*
|
|
|
|
* NB even if we received the connection, the IP address is likely to okay.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//used to send back to the peer it's own ext address
|
|
|
|
//it->second.currentserveraddr = remote_peer_address;
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
// THIS TEST IS A Bit BAD XXX, we should update their address anyway...
|
|
|
|
// This means we only update connections that we've made.. so maybe not too bad?
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
if ((it->second.inConnAttempt) &&
|
|
|
|
(it->second.currentConnAddrAttempt.addr.sin_addr.s_addr
|
|
|
|
== remote_peer_address.sin_addr.s_addr) &&
|
|
|
|
(it->second.currentConnAddrAttempt.addr.sin_port
|
|
|
|
== remote_peer_address.sin_port))
|
2011-07-09 14:39:34 -04:00
|
|
|
{
|
|
|
|
updatePeerAddr = true;
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() adding current peer address in list." << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove other attempts */
|
|
|
|
it->second.inConnAttempt = false;
|
|
|
|
it->second.connAddrs.clear();
|
|
|
|
mStatusChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-10 20:52:12 -04:00
|
|
|
#ifdef LINKMGR_DEBUG_CONNFAIL
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Disconnect/Fail: flags: " << flags << " id: " << id;
|
2011-07-10 20:52:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (it->second.inConnAttempt)
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Likely Connect Fail, as inConnAttempt Flag is set";
|
2011-07-10 20:52:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Likely DISCONNECT, as state set to Connected";
|
2011-07-10 20:52:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
it->second.inConnAttempt = false;
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Disconnect/Fail: id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << " Success: " << success << " flags: " << flags << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* if currently connected -> flag as failed */
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
|
|
|
it->second.state &= (~RS_PEER_S_CONNECTED);
|
|
|
|
it->second.actions |= RS_PEER_DISCONNECTED;
|
|
|
|
mStatusChanged = true;
|
|
|
|
|
2011-07-10 20:52:12 -04:00
|
|
|
updateLastContact = true; /* time of disconnect */
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (it->second.connAddrs.size() >= 1)
|
|
|
|
{
|
|
|
|
it->second.actions |= RS_PEER_CONNECT_REQ;
|
|
|
|
mStatusChanged = true;
|
|
|
|
}
|
2011-07-10 15:50:12 -04:00
|
|
|
|
|
|
|
if (it->second.dhtVisible)
|
|
|
|
{
|
|
|
|
doDhtAssist = true;
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
2011-07-09 14:39:34 -04:00
|
|
|
|
|
|
|
if (updatePeerAddr)
|
|
|
|
{
|
|
|
|
pqiIpAddress raddr;
|
|
|
|
raddr.mAddr = remote_peer_address;
|
|
|
|
raddr.mSeenTime = time(NULL);
|
|
|
|
raddr.mSrc = 0;
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Success and we initiated connection... Updating Address";
|
2011-07-10 15:50:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
mPeerMgr->updateCurrentAddress(id, raddr);
|
|
|
|
}
|
2011-07-10 15:50:12 -04:00
|
|
|
|
2011-07-10 20:52:12 -04:00
|
|
|
if (updateLastContact)
|
|
|
|
{
|
|
|
|
mPeerMgr->updateLastContact(id);
|
|
|
|
}
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
/* inform NetAssist of result. This is slightly duplicating below, as we switch it on/off
|
|
|
|
* in a second anyway. However, the FAILURE of UDP connection, must be informed.
|
|
|
|
*
|
|
|
|
* actually the way the DHT works at the moment, both forms of feedback are required.
|
|
|
|
* this handles connection requests, the other searches. As they are independent... do both.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (flags == RS_NET_CONN_UDP_ALL)
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
#endif
|
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Sending Feedback for UDP connection";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
#endif
|
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() UDP Update CONNECTED to: " << id;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
mNetMgr->netAssistStatusUpdate(id, NETMGR_DHT_FEEDBACK_CONNECTED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
#endif
|
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() UDP Update FAILED to: " << id;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
/* have no differentiation between failure and closed? */
|
|
|
|
mNetMgr->netAssistStatusUpdate(id, NETMGR_DHT_FEEDBACK_CONN_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Success switching off DhtAssist for friend: " << id;
|
2011-07-10 15:50:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* always switch it off now */
|
2011-11-22 08:24:42 -05:00
|
|
|
mNetMgr->netAssistFriend(id,false);
|
|
|
|
|
2011-12-01 12:21:52 -05:00
|
|
|
/* inform NetMgr that we know this peers address: but only if external address */
|
|
|
|
if (isExternalNet(&(remote_peer_address.sin_addr)))
|
|
|
|
{
|
|
|
|
mNetMgr->netAssistKnownPeer(id,remote_peer_address,
|
|
|
|
NETASSIST_KNOWN_PEER_FRIEND | NETASSIST_KNOWN_PEER_ONLINE);
|
|
|
|
}
|
2011-07-10 15:50:12 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (doDhtAssist)
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Fail, Enabling DhtAssist for: " << id;
|
2011-07-10 15:50:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
mNetMgr->netAssistFriend(id,true) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::connectResult() Fail, No DhtAssist, as No DHT visibility for: " << id;
|
2011-07-10 15:50:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2011-11-22 08:24:42 -05:00
|
|
|
|
|
|
|
/* inform NetMgr that this peer is offline */
|
|
|
|
mNetMgr->netAssistKnownPeer(id,remote_peer_address, NETASSIST_KNOWN_PEER_FRIEND | NETASSIST_KNOWN_PEER_OFFLINE);
|
2011-07-10 15:50:12 -04:00
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
return success;
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************** Feedback ...... *********************************
|
|
|
|
* From various sources
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::peerStatus(std::string id, const pqiIpAddrSet &addrs,
|
2011-07-09 05:36:37 -04:00
|
|
|
uint32_t type, uint32_t flags, uint32_t source)
|
|
|
|
{
|
|
|
|
/* HACKED UP FIX ****/
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
bool isFriend = true;
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
peerAddrInfo details;
|
|
|
|
details.type = type;
|
|
|
|
details.found = true;
|
|
|
|
details.addrs = addrs;
|
|
|
|
details.ts = now;
|
2011-07-09 14:39:34 -04:00
|
|
|
|
|
|
|
bool updateNetConfig = (source == RS_CB_PERSON);
|
|
|
|
uint32_t peerVisibility = 0;
|
|
|
|
uint32_t peerNetMode = 0;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
uint32_t ownNetMode = mNetMgr->getNetworkMode();
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
/* Log */
|
|
|
|
std::ostringstream out;
|
2011-07-13 10:22:25 -04:00
|
|
|
out << "p3LinkMgrIMPL::peerStatus()" << " id: " << id;
|
2011-07-09 05:36:37 -04:00
|
|
|
out << " type: " << type << " flags: " << flags;
|
|
|
|
out << " source: " << source;
|
|
|
|
out << std::endl;
|
|
|
|
addrs.printAddrs(out);
|
|
|
|
|
|
|
|
rslog(RSL_WARNING, p3connectzone, out.str());
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << out.str();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look up the id */
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
|
|
|
/* check Others list */
|
|
|
|
isFriend = false;
|
|
|
|
it = mOthersList.find(id);
|
|
|
|
if (it == mOthersList.end())
|
|
|
|
{
|
|
|
|
/* not found - ignore */
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() Peer Not Found - Ignore" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() Peer is in mOthersList" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() Current Peer State:" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
printConnectState(std::cerr, it->second);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* update the status */
|
|
|
|
|
|
|
|
/* if source is DHT */
|
|
|
|
if (source == RS_CB_DHT)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() Update From DHT:";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
/* DHT can tell us about
|
|
|
|
* 1) connect type (UDP/TCP/etc)
|
|
|
|
* 2) local/external address
|
|
|
|
*/
|
|
|
|
it->second.source = RS_CB_DHT;
|
|
|
|
it->second.dht = details;
|
|
|
|
|
|
|
|
/* If we get a info -> then they are online */
|
|
|
|
it->second.state |= RS_PEER_S_ONLINE;
|
|
|
|
it->second.lastavailable = now;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (source == RS_CB_DISC)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() Update From DISC:";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
/* DISC can tell us about
|
|
|
|
* 1) connect type (UDP/TCP/etc)
|
|
|
|
* 2) local/external addresses
|
|
|
|
*/
|
|
|
|
it->second.source = RS_CB_DISC;
|
|
|
|
it->second.disc = details;
|
|
|
|
|
|
|
|
|
|
|
|
if (flags & RS_NET_FLAGS_ONLINE)
|
|
|
|
{
|
|
|
|
it->second.actions |= RS_PEER_ONLINE;
|
|
|
|
it->second.state |= RS_PEER_S_ONLINE;
|
|
|
|
it->second.lastavailable = now;
|
|
|
|
mStatusChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (source == RS_CB_PERSON)
|
|
|
|
{
|
|
|
|
/* PERSON can tell us about
|
|
|
|
* 1) online / offline
|
|
|
|
* 2) connect address
|
|
|
|
* -> update all!
|
|
|
|
*/
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() Update From PERSON:";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
it->second.source = RS_CB_PERSON;
|
|
|
|
it->second.peer = details;
|
|
|
|
|
|
|
|
it->second.state |= RS_PEER_S_ONLINE;
|
|
|
|
it->second.lastavailable = now;
|
|
|
|
|
|
|
|
/* must be online to recv info (should be connected too!)
|
|
|
|
* but no need for action as should be connected already
|
2011-07-09 14:39:34 -04:00
|
|
|
*
|
|
|
|
* One problem with these states... is that we will never find them via DHT
|
|
|
|
* if these flags are switched off here... If we get a connection attempt via DHT
|
|
|
|
* we should switch the DHT search back on.
|
2011-07-09 05:36:37 -04:00
|
|
|
*/
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
peerNetMode = 0; //it->second.netMode &= (~RS_NET_MODE_ACTUAL); /* clear actual flags */
|
2011-07-09 05:36:37 -04:00
|
|
|
if (flags & RS_NET_FLAGS_EXTERNAL_ADDR)
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
peerNetMode = RS_NET_MODE_EXT;
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
else if (flags & RS_NET_FLAGS_STABLE_UDP)
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
peerNetMode = RS_NET_MODE_UDP;
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
peerNetMode = RS_NET_MODE_UNREACHABLE;
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* always update VIS status */
|
|
|
|
if (flags & RS_NET_FLAGS_USE_DISC)
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
peerVisibility &= (~RS_VIS_STATE_NODISC);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
peerVisibility |= RS_VIS_STATE_NODISC;
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & RS_NET_FLAGS_USE_DHT)
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
peerVisibility &= (~RS_VIS_STATE_NODHT);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
peerVisibility |= RS_VIS_STATE_NODHT;
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
2011-07-09 14:39:34 -04:00
|
|
|
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine Reachability (only advisory) */
|
2011-07-09 14:39:34 -04:00
|
|
|
if (ownNetMode & RS_NET_MODE_UDP)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
if ((details.type & RS_NET_CONN_UDP_DHT_SYNC) ||
|
|
|
|
(details.type & RS_NET_CONN_TCP_EXTERNAL))
|
|
|
|
{
|
|
|
|
/* reachable! */
|
|
|
|
it->second.state &= (~RS_PEER_S_UNREACHABLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* unreachable */
|
|
|
|
it->second.state |= RS_PEER_S_UNREACHABLE;
|
|
|
|
}
|
|
|
|
}
|
2011-07-09 14:39:34 -04:00
|
|
|
else if (ownNetMode & RS_NET_MODE_UNREACHABLE)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
if (details.type & RS_NET_CONN_TCP_EXTERNAL)
|
|
|
|
{
|
|
|
|
/* reachable! */
|
|
|
|
it->second.state &= (~RS_PEER_S_UNREACHABLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* unreachable */
|
|
|
|
it->second.state |= RS_PEER_S_UNREACHABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->second.state &= (~RS_PEER_S_UNREACHABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isFriend)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() NOT FRIEND " << " id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::peerStatus() NO CONNECT (not friend)");
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if already connected -> done */
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() PEER ONLINE ALREADY " << " id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Log */
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::peerStatus() NO CONNECT (already connected!)");
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
} /****** STACK UNLOCK MUTEX *******/
|
2011-07-09 14:39:34 -04:00
|
|
|
|
|
|
|
bool newAddrs = mPeerMgr->updateAddressList(id, addrs);
|
|
|
|
if (updateNetConfig)
|
|
|
|
{
|
|
|
|
mPeerMgr -> setVisState(id, peerVisibility);
|
|
|
|
mPeerMgr -> setNetworkMode(id, peerNetMode);
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus()" << " id: " << id;
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << " type: " << type << " flags: " << flags;
|
|
|
|
std::cerr << " source: " << source << std::endl;
|
|
|
|
std::cerr << " addrs: " << std::endl;
|
|
|
|
addrs.printAddrs(std::cerr);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef P3CONNMGR_NO_AUTO_CONNECTION
|
|
|
|
|
|
|
|
#ifndef P3CONNMGR_NO_TCP_CONNECTIONS
|
|
|
|
if (newAddrs)
|
|
|
|
{
|
|
|
|
retryConnectTCP(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // P3CONNMGR_NO_TCP_CONNECTIONS
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
#endif // P3CONNMGR_NO_AUTO_CONNECTION
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerStatus() Resulting Peer State:" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
printConnectState(std::cerr, it->second);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
/* This has become very unwieldy - as extra arguments are required for UDP connections */
|
|
|
|
void p3LinkMgrIMPL::peerConnectRequest(std::string id, struct sockaddr_in raddr, struct sockaddr_in proxyaddr, struct sockaddr_in srcaddr,
|
|
|
|
uint32_t source, uint32_t flags, uint32_t delay, uint32_t bandwidth)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerConnectRequest() id: " << id;
|
|
|
|
std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port);
|
2011-07-14 10:56:33 -04:00
|
|
|
std::cerr << " proxyaddr: " << rs_inet_ntoa(proxyaddr.sin_addr) << ":" << ntohs(proxyaddr.sin_port);
|
|
|
|
std::cerr << " srcaddr: " << rs_inet_ntoa(srcaddr.sin_addr) << ":" << ntohs(srcaddr.sin_port);
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << " source: " << source;
|
|
|
|
std::cerr << " flags: " << flags;
|
|
|
|
std::cerr << " delay: " << delay;
|
2011-07-14 10:56:33 -04:00
|
|
|
std::cerr << " bandwidth: " << bandwidth;
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Log */
|
|
|
|
std::ostringstream out;
|
2011-07-14 10:56:33 -04:00
|
|
|
out << "p3LinkMgrIMPL::peerConnectRequest() id: " << id;
|
|
|
|
out << " raddr: " << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port);
|
|
|
|
std::cerr << " proxyaddr: " << rs_inet_ntoa(proxyaddr.sin_addr) << ":" << ntohs(proxyaddr.sin_port);
|
|
|
|
std::cerr << " srcaddr: " << rs_inet_ntoa(srcaddr.sin_addr) << ":" << ntohs(srcaddr.sin_port);
|
|
|
|
out << " source: " << source;
|
2011-07-13 18:23:40 -04:00
|
|
|
out << " flags: " << flags;
|
|
|
|
out << " delay: " << delay;
|
2011-07-14 10:56:33 -04:00
|
|
|
out << " bandwidth: " << bandwidth;
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************** TCP PART *****************************/
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerConnectRequest() (From DHT Only)" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (source == RS_CB_DHT)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
if (flags & RS_CB_FLAG_MODE_TCP)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
|
|
|
|
#ifndef P3CONNMGR_NO_TCP_CONNECTIONS
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerConnectRequest() DHT says Online ==> so try TCP";
|
|
|
|
std::cerr << std::endl;
|
2011-07-10 14:19:46 -04:00
|
|
|
#endif
|
2011-07-13 18:23:40 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
std::cerr << "p3LinkMgrIMPL::peerConnectRequest() ERROR Peer is not Friend" << std::endl;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if already connected -> done */
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
std::cerr << "p3LinkMgrIMPL::peerConnectRequest() ERROR Peer Already Connected" << std::endl;
|
|
|
|
#endif
|
2011-08-06 08:27:23 -04:00
|
|
|
return;
|
2011-07-13 18:23:40 -04:00
|
|
|
}
|
|
|
|
/* setup specific attempt for DHT found address. */
|
|
|
|
locked_ConnectAttempt_SpecificAddress(&(it->second), &raddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
retryConnect(id);
|
2011-07-18 20:06:09 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* UDP Attempt! */
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
std::cerr << "p3LinkMgrIMPL::peerConnectRequest() DHT says CONNECT ==> tryConnectUDP()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-14 10:56:33 -04:00
|
|
|
tryConnectUDP(id, raddr, proxyaddr, srcaddr, flags, delay, bandwidth);
|
2011-07-13 18:23:40 -04:00
|
|
|
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
return;
|
2011-07-13 18:23:40 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // IS THIS USED???
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::peerConnectRequest() ERROR source OTHER ==> NOOP" << std::endl;
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************/
|
|
|
|
/*******************************************************************/
|
|
|
|
/*************** External Control ****************/
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::retryConnect(const std::string &id)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
/* push all available addresses onto the connect addr stack */
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnect() id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef P3CONNMGR_NO_TCP_CONNECTIONS
|
|
|
|
|
|
|
|
retryConnectTCP(id);
|
|
|
|
|
|
|
|
#endif // P3CONNMGR_NO_TCP_CONNECTIONS
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
bool p3LinkMgrIMPL::tryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr,
|
|
|
|
struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr,
|
|
|
|
uint32_t flags, uint32_t delay, uint32_t bandwidth)
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-14 11:01:41 -04:00
|
|
|
|
|
|
|
#ifdef DISABLE_UDP_CONNECTIONS
|
|
|
|
|
|
|
|
std::cerr << "p3LinkMgrIMPL::tryConnectUDP() CONNECTIONS DISABLED FOR NOW... id: " << id << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "p3LinkMgrIMPL::tryConnectUDP() PARAMS id: " << id;
|
|
|
|
std::cerr << " raddr: " << rs_inet_ntoa(rUdpAddr.sin_addr) << ":" << ntohs(rUdpAddr.sin_port);
|
|
|
|
std::cerr << " proxyaddr: " << rs_inet_ntoa(proxyaddr.sin_addr) << ":" << ntohs(proxyaddr.sin_port);
|
|
|
|
std::cerr << " srcaddr: " << rs_inet_ntoa(srcaddr.sin_addr) << ":" << ntohs(srcaddr.sin_port);
|
|
|
|
std::cerr << " flags: " << flags;
|
|
|
|
std::cerr << " delay: " << delay;
|
|
|
|
std::cerr << " bandwidth: " << bandwidth;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
mNetMgr->netAssistStatusUpdate(id, NETMGR_DHT_FEEDBACK_CONN_FAILED);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* push all available addresses onto the connect addr stack */
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-14 11:01:41 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::tryConnectUDP() id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (id == getOwnId()) {
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::retryConnectUDP() Failed, connecting to own id: ");
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look up the id */
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectUDP() Peer is not Friend" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if already connected -> done */
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectUDP() Peer Already Connected" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
if (it->second.connecttype & RS_NET_CONN_TUNNEL) {
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectUDP() Peer Connected through a tunnel connection, let's try a normal connection." << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
} else {
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectUDP() Peer Connected no more connection attempts" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Explicit Request to start the UDP connection */
|
|
|
|
if (isValidNet(&(rUdpAddr.sin_addr)))
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Adding udp connection attempt: ";
|
|
|
|
std::cerr << "Addr: " << rs_inet_ntoa(rUdpAddr.sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(rUdpAddr.sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
peerConnectAddress pca;
|
|
|
|
pca.addr = rUdpAddr;
|
|
|
|
pca.type = RS_NET_CONN_UDP_PEER_SYNC;
|
2011-07-13 18:23:40 -04:00
|
|
|
pca.delay = delay;
|
2011-07-09 05:36:37 -04:00
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = P3CONNMGR_UDP_DEFAULT_PERIOD;
|
2011-07-13 18:23:40 -04:00
|
|
|
pca.flags = flags;
|
2011-07-14 10:56:33 -04:00
|
|
|
|
|
|
|
pca.proxyaddr = proxyaddr;
|
|
|
|
pca.srcaddr = srcaddr;
|
|
|
|
pca.bandwidth = bandwidth;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
// Push address to the front... so it happens quickly (before any timings are lost).
|
|
|
|
addAddressIfUnique(it->second.connAddrs, pca, true);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* finish it off */
|
|
|
|
return locked_ConnectAttempt_Complete(&(it->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::retryConnectTCP(const std::string &id)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
/* Check if we should retry first */
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
/* push all available addresses onto the connect addr stack...
|
|
|
|
* with the following exceptions:
|
|
|
|
* - check local address, see if it is the same network as us
|
|
|
|
- check address age. don't add old ones
|
|
|
|
*/
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() id: " << id << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
|
|
|
|
if (id == getOwnId())
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::retryConnectTCP() Failed, connecting to own id: ");
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look up the id */
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
if (mFriendList.end() == (it = mFriendList.find(id)))
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer is not Friend" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if already connected -> done */
|
|
|
|
if (it->second.state & RS_PEER_S_CONNECTED)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer Already Connected" << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
if (it->second.connecttype & RS_NET_CONN_TUNNEL)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer Connected through a tunnel connection, let's try a normal connection." << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Peer Connected no more connection attempts" << std::endl;
|
2011-07-09 14:39:34 -04:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /****** END of LOCKED ******/
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() Getting Address from PeerMgr for : " << id;
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
/* If we reach here, must retry .... extract the required info from p3PeerMgr */
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
struct sockaddr_in lAddr;
|
|
|
|
struct sockaddr_in eAddr;
|
|
|
|
pqiIpAddrSet histAddrs;
|
|
|
|
std::string dyndns;
|
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
if (mPeerMgr->getConnectAddresses(id, lAddr, eAddr, histAddrs, dyndns))
|
2011-07-09 14:39:34 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
if (mFriendList.end() != (it = mFriendList.find(id)))
|
|
|
|
{
|
|
|
|
|
|
|
|
locked_ConnectAttempt_CurrentAddresses(&(it->second), &lAddr, &eAddr);
|
|
|
|
locked_ConnectAttempt_HistoricalAddresses(&(it->second), histAddrs);
|
|
|
|
|
|
|
|
uint16_t dynPort = ntohs(eAddr.sin_port);
|
|
|
|
if (!dynPort)
|
|
|
|
dynPort = ntohs(lAddr.sin_port);
|
|
|
|
if (dynPort)
|
|
|
|
{
|
|
|
|
locked_ConnectAttempt_AddDynDNS(&(it->second), dyndns, dynPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
//locked_ConnectAttempt_AddTunnel(&(it->second));
|
|
|
|
|
|
|
|
/* finish it off */
|
|
|
|
return locked_ConnectAttempt_Complete(&(it->second));
|
|
|
|
}
|
2011-07-10 14:19:46 -04:00
|
|
|
else
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " << id;
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::retryConnectTCP() ERROR failed to addresses from PeerMgr for: " << id;
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-09 14:39:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_TCP_ADDR_AGE (3600 * 24 * 14) // two weeks in seconds.
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::locked_CheckPotentialAddr(const struct sockaddr_in *addr, time_t age)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr(";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << rs_inet_ntoa(addr->sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(addr->sin_port);
|
|
|
|
std::cerr << ", " << age << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if it is old - quick rejection
|
|
|
|
*/
|
|
|
|
if (age > MAX_TCP_ADDR_AGE)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - TOO OLD";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid = isValidNet(&(addr->sin_addr));
|
|
|
|
// bool isLoopback = isLoopbackNet(&(addr->sin_addr));
|
|
|
|
// bool isPrivate = isPrivateNet(&(addr->sin_addr));
|
|
|
|
bool isExternal = isExternalNet(&(addr->sin_addr));
|
|
|
|
|
|
|
|
/* if invalid - quick rejection */
|
|
|
|
if (!isValid)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - INVALID";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it is on the ban list - ignore */
|
|
|
|
/* checks - is it the dreaded 1.0.0.0 */
|
|
|
|
|
|
|
|
std::list<struct in_addr>::const_iterator it;
|
|
|
|
for(it = mBannedIpList.begin(); it != mBannedIpList.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->s_addr == addr->sin_addr.s_addr)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - ON BANNED IPLIST";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* if it is an external address, we'll accept it.
|
|
|
|
* - even it is meant to be a local address.
|
|
|
|
*/
|
|
|
|
if (isExternal)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() ACCEPTING - EXTERNAL";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get here, it is private or loopback
|
|
|
|
* - can only connect to these addresses if we are on the same subnet.
|
|
|
|
- check net against our local address.
|
|
|
|
*/
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() Checking sameNet against: ";
|
2011-07-09 14:39:34 -04:00
|
|
|
std::cerr << rs_inet_ntoa(mLocalAddress.sin_addr);
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
if (sameNet(&(mLocalAddress.sin_addr), &(addr->sin_addr)))
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() ACCEPTING - PRIVATE & sameNET";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - PRIVATE & !sameNET";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* else it fails */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
void p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, struct sockaddr_in *remoteAddr)
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
if ((remoteAddr) && (locked_CheckPotentialAddr(remoteAddr, 0)))
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses() ";
|
|
|
|
std::cerr << "Adding tcp connection attempt: ";
|
|
|
|
std::cerr << "Addr: " << rs_inet_ntoa(remoteAddr->sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(remoteAddr->sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
peerConnectAddress pca;
|
|
|
|
pca.addr = *remoteAddr;
|
|
|
|
pca.type = RS_NET_CONN_TCP_EXTERNAL;
|
|
|
|
pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY;
|
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD;
|
2011-07-14 10:56:33 -04:00
|
|
|
sockaddr_clear(&(pca.proxyaddr));
|
|
|
|
sockaddr_clear(&(pca.srcaddr));
|
|
|
|
pca.bandwidth = 0;
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
addAddressIfUnique(peer->connAddrs, pca, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, struct sockaddr_in *localAddr, struct sockaddr_in *serverAddr)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses()";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
// Just push all the addresses onto the stack.
|
|
|
|
/* try "current addresses" first */
|
2011-07-09 14:39:34 -04:00
|
|
|
if ((localAddr) && (locked_CheckPotentialAddr(localAddr, 0)))
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses() ";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Adding tcp connection attempt: ";
|
2011-07-09 14:39:34 -04:00
|
|
|
std::cerr << "Current Local Addr: " << rs_inet_ntoa(localAddr->sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(localAddr->sin_port);
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
peerConnectAddress pca;
|
2011-07-09 14:39:34 -04:00
|
|
|
pca.addr = *localAddr;
|
2011-07-09 05:36:37 -04:00
|
|
|
pca.type = RS_NET_CONN_TCP_LOCAL;
|
|
|
|
pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY;
|
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD;
|
2011-07-14 10:56:33 -04:00
|
|
|
sockaddr_clear(&(pca.proxyaddr));
|
|
|
|
sockaddr_clear(&(pca.srcaddr));
|
|
|
|
pca.bandwidth = 0;
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
addAddressIfUnique(peer->connAddrs, pca, false);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
if ((serverAddr) && (locked_CheckPotentialAddr(serverAddr, 0)))
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses() ";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Adding tcp connection attempt: ";
|
2011-07-09 14:39:34 -04:00
|
|
|
std::cerr << "Current Ext Addr: " << rs_inet_ntoa(serverAddr->sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(serverAddr->sin_port);
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
peerConnectAddress pca;
|
2011-07-09 14:39:34 -04:00
|
|
|
pca.addr = *serverAddr;
|
2011-07-09 05:36:37 -04:00
|
|
|
pca.type = RS_NET_CONN_TCP_EXTERNAL;
|
|
|
|
pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY;
|
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD;
|
2011-07-14 10:56:33 -04:00
|
|
|
sockaddr_clear(&(pca.proxyaddr));
|
|
|
|
sockaddr_clear(&(pca.srcaddr));
|
|
|
|
pca.bandwidth = 0;
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
addAddressIfUnique(peer->connAddrs, pca, false);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses(peerConnectState *peer, const pqiIpAddrSet &ipAddrs)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
/* now try historical addresses */
|
|
|
|
/* try local addresses first */
|
2011-07-09 14:39:34 -04:00
|
|
|
std::list<pqiIpAddress>::const_iterator ait;
|
2011-07-09 05:36:37 -04:00
|
|
|
time_t now = time(NULL);
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses()";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
for(ait = ipAddrs.mLocal.mAddrs.begin();
|
|
|
|
ait != ipAddrs.mLocal.mAddrs.end(); ait++)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
if (locked_CheckPotentialAddr(&(ait->mAddr), now - ait->mSeenTime))
|
|
|
|
{
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses() ";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Adding tcp connection attempt: ";
|
|
|
|
std::cerr << "Local Addr: " << rs_inet_ntoa(ait->mAddr.sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(ait->mAddr.sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
peerConnectAddress pca;
|
|
|
|
pca.addr = ait->mAddr;
|
|
|
|
pca.type = RS_NET_CONN_TCP_LOCAL;
|
|
|
|
pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY;
|
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD;
|
2011-07-14 10:56:33 -04:00
|
|
|
sockaddr_clear(&(pca.proxyaddr));
|
|
|
|
sockaddr_clear(&(pca.srcaddr));
|
|
|
|
pca.bandwidth = 0;
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
addAddressIfUnique(peer->connAddrs, pca, false);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
for(ait = ipAddrs.mExt.mAddrs.begin();
|
|
|
|
ait != ipAddrs.mExt.mAddrs.end(); ait++)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
if (locked_CheckPotentialAddr(&(ait->mAddr), now - ait->mSeenTime))
|
|
|
|
{
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses() ";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Adding tcp connection attempt: ";
|
|
|
|
std::cerr << "Ext Addr: " << rs_inet_ntoa(ait->mAddr.sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(ait->mAddr.sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
peerConnectAddress pca;
|
|
|
|
pca.addr = ait->mAddr;
|
|
|
|
pca.type = RS_NET_CONN_TCP_EXTERNAL;
|
|
|
|
pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY;
|
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD;
|
2011-07-14 10:56:33 -04:00
|
|
|
sockaddr_clear(&(pca.proxyaddr));
|
|
|
|
sockaddr_clear(&(pca.srcaddr));
|
|
|
|
pca.bandwidth = 0;
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
addAddressIfUnique(peer->connAddrs, pca, false);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std::string dyndns, uint16_t port)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
/* try dyndns address too */
|
2011-07-10 14:19:46 -04:00
|
|
|
struct in_addr addr;
|
|
|
|
if (!dyndns.empty() && port)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() Looking up DynDNS address: " << dyndns << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-07-10 14:19:46 -04:00
|
|
|
if(mDNSResolver->getIPAddressFromString(dyndns, addr))
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() ";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << "Adding tcp connection attempt: ";
|
|
|
|
std::cerr << "DynDNS Addr: " << rs_inet_ntoa(addr);
|
|
|
|
std::cerr << ":" << ntohs(port);
|
|
|
|
std::cerr << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
2011-07-10 14:19:46 -04:00
|
|
|
peerConnectAddress pca;
|
|
|
|
pca.addr.sin_family = AF_INET;
|
|
|
|
pca.addr.sin_addr.s_addr = addr.s_addr;
|
|
|
|
pca.addr.sin_port = htons(port);
|
|
|
|
pca.type = RS_NET_CONN_TCP_EXTERNAL;
|
|
|
|
//for the delay, we add a random time and some more time when the friend list is big
|
|
|
|
pca.delay = P3CONNMGR_TCP_DEFAULT_DELAY;
|
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = P3CONNMGR_TCP_DEFAULT_PERIOD;
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
sockaddr_clear(&(pca.proxyaddr));
|
|
|
|
sockaddr_clear(&(pca.srcaddr));
|
|
|
|
pca.bandwidth = 0;
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
/* check address validity */
|
|
|
|
if (locked_CheckPotentialAddr(&(pca.addr), 0))
|
|
|
|
{
|
2012-01-06 17:03:46 -05:00
|
|
|
addAddressIfUnique(peer->connAddrs, pca, true);
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
2011-07-10 14:19:46 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() DNSResolver hasn't found addr yet";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() Address(" << dyndns << ") or Port(" << port << ") NULL ignoring";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::locked_ConnectAttempt_AddTunnel(peerConnectState *peer)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-09 14:39:34 -04:00
|
|
|
if (!(peer->state & RS_PEER_S_CONNECTED) && mAllowTunnelConnection)
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << "Adding TUNNEL Connection Attempt";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 14:39:34 -04:00
|
|
|
peerConnectAddress pca;
|
|
|
|
pca.type = RS_NET_CONN_TUNNEL;
|
|
|
|
pca.ts = time(NULL);
|
|
|
|
pca.period = 0;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
sockaddr_clear(&pca.addr);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
sockaddr_clear(&(pca.proxyaddr));
|
|
|
|
sockaddr_clear(&(pca.srcaddr));
|
|
|
|
pca.bandwidth = 0;
|
|
|
|
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
addAddressIfUnique(peer->connAddrs, pca, false);
|
2011-07-09 14:39:34 -04:00
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
bool p3LinkMgrIMPL::addAddressIfUnique(std::list<peerConnectAddress> &addrList, peerConnectAddress &pca, bool pushFront)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
/* iterate through the list, and make sure it isn't already
|
|
|
|
* in the list
|
|
|
|
*/
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::addAddressIfUnique() Checking Address: " << rs_inet_ntoa(pca.addr.sin_addr);
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
std::list<peerConnectAddress>::iterator it;
|
|
|
|
for(it = addrList.begin(); it != addrList.end(); it++)
|
|
|
|
{
|
|
|
|
if ((pca.addr.sin_addr.s_addr == it->addr.sin_addr.s_addr) &&
|
|
|
|
(pca.addr.sin_port == it->addr.sin_port) &&
|
|
|
|
(pca.type == it->type))
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::addAddressIfUnique() Discarding Duplicate Address";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
/* already */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::addAddressIfUnique() Adding New Address";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
if (pushFront)
|
|
|
|
{
|
|
|
|
addrList.push_front(pca);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addrList.push_back(pca);
|
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
bool p3LinkMgrIMPL::locked_ConnectAttempt_Complete(peerConnectState *peer)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
/* flag as last attempt to prevent loop */
|
|
|
|
//add a random perturbation between 0 and 2 sec.
|
2011-07-09 14:39:34 -04:00
|
|
|
peer->lastattempt = time(NULL) + rand() % MAX_RANDOM_ATTEMPT_OFFSET;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
if (peer->inConnAttempt)
|
|
|
|
{
|
2011-07-09 05:36:37 -04:00
|
|
|
/* -> it'll automatically use the addresses we added */
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_Complete() Already in CONNECT ATTEMPT";
|
2011-07-09 05:36:37 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::locked_ConnectAttempt_Complete() Remaining ConnAddr Count: " << peer->connAddrs.size();
|
2011-07-09 14:39:34 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start a connection attempt */
|
2011-07-09 14:39:34 -04:00
|
|
|
if (peer->connAddrs.size() > 0)
|
2011-07-09 05:36:37 -04:00
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-09 14:39:34 -04:00
|
|
|
std::ostringstream out;
|
2011-07-13 10:22:25 -04:00
|
|
|
out << "p3LinkMgrIMPL::locked_ConnectAttempt_Complete() Started CONNECT ATTEMPT! " ;
|
2011-07-10 14:19:46 -04:00
|
|
|
out << std::endl;
|
2011-07-13 10:22:25 -04:00
|
|
|
out << "p3LinkMgrIMPL::locked_ConnectAttempt_Complete() ConnAddr Count: " << peer->connAddrs.size();
|
2011-07-09 14:39:34 -04:00
|
|
|
rslog(RSL_DEBUG_ALERT, p3connectzone, out.str());
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << out.str() << std::endl;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
peer->actions |= RS_PEER_CONNECT_REQ;
|
|
|
|
mStatusChanged = true;
|
2011-07-09 05:36:37 -04:00
|
|
|
return true;
|
2011-07-09 14:39:34 -04:00
|
|
|
}
|
2011-07-09 05:36:37 -04:00
|
|
|
else
|
|
|
|
{
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-09 14:39:34 -04:00
|
|
|
std::ostringstream out;
|
2011-07-13 10:22:25 -04:00
|
|
|
out << "p3LinkMgrIMPL::locked_ConnectAttempt_Complete() No addr in the connect attempt list. Not suitable for CONNECT ATTEMPT! ";
|
2011-07-09 14:39:34 -04:00
|
|
|
rslog(RSL_DEBUG_ALERT, p3connectzone, out.str());
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << out.str() << std::endl;
|
2011-07-09 14:39:34 -04:00
|
|
|
#endif
|
2011-07-09 05:36:37 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
/***********************************************************************************************************
|
|
|
|
************************************* Handling of Friends *************************************************
|
|
|
|
***********************************************************************************************************/
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
int p3LinkMgrIMPL::addFriend(const std::string &id, bool isVisible)
|
2011-07-09 20:41:39 -04:00
|
|
|
{
|
2011-08-06 08:27:23 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgr::addFriend() id: " + id);
|
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::addFriend(" << id << "," << isVisible << ")";
|
2011-07-10 14:19:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
|
|
|
|
if (it != mFriendList.end())
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::addFriend() ERROR, friend already exists : " << id;
|
2011-07-09 20:41:39 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
peerConnectState pcs;
|
|
|
|
pcs.dhtVisible = isVisible;
|
2011-07-10 14:19:46 -04:00
|
|
|
pcs.id = id;
|
2011-07-11 08:11:58 -04:00
|
|
|
pcs.name = "NoName";
|
2011-07-10 14:19:46 -04:00
|
|
|
pcs.state = RS_PEER_S_FRIEND;
|
|
|
|
pcs.actions = RS_PEER_NEW;
|
2011-07-09 20:41:39 -04:00
|
|
|
|
|
|
|
mFriendList[id] = pcs;
|
2011-07-11 08:11:58 -04:00
|
|
|
|
|
|
|
mStatusChanged = true;
|
2011-07-09 20:41:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mNetMgr->netAssistFriend(id, isVisible);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
int p3LinkMgrIMPL::removeFriend(const std::string &id)
|
2011-07-09 20:41:39 -04:00
|
|
|
{
|
2011-08-06 08:27:23 -04:00
|
|
|
rslog(RSL_WARNING, p3connectzone, "p3LinkMgr::removeFriend() id: " + id);
|
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
{
|
2011-07-11 08:11:58 -04:00
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
|
|
|
#ifdef LINKMGR_DEBUG
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::removeFriend(" << id << ")";
|
2011-07-09 20:41:39 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-11 08:11:58 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
it = mFriendList.find(id);
|
|
|
|
|
|
|
|
if (it == mFriendList.end())
|
|
|
|
{
|
2011-07-13 10:22:25 -04:00
|
|
|
std::cerr << "p3LinkMgrIMPL::removeFriend() ERROR, friend not there : " << id;
|
2011-07-11 08:11:58 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to OthersList (so remove can be handled via the action) */
|
|
|
|
peerConnectState peer = it->second;
|
|
|
|
|
|
|
|
peer.state &= (~RS_PEER_S_FRIEND);
|
|
|
|
peer.state &= (~RS_PEER_S_CONNECTED);
|
|
|
|
peer.state &= (~RS_PEER_S_ONLINE);
|
|
|
|
peer.actions = RS_PEER_MOVED;
|
|
|
|
peer.inConnAttempt = false;
|
|
|
|
mOthersList[id] = peer;
|
|
|
|
|
|
|
|
mStatusChanged = true;
|
|
|
|
|
|
|
|
mFriendList.erase(it);
|
2011-07-09 20:41:39 -04:00
|
|
|
}
|
2011-07-11 08:11:58 -04:00
|
|
|
|
|
|
|
mNetMgr->netAssistFriend(id, false);
|
2011-07-09 20:41:39 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void p3LinkMgrIMPL::printPeerLists(std::ostream &out)
|
2011-07-11 08:11:58 -04:00
|
|
|
{
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
out << "p3LinkMgrIMPL::printPeerLists() Friend List";
|
2011-07-11 08:11:58 -04:00
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
std::map<std::string, peerConnectState>::iterator it;
|
|
|
|
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
|
|
|
{
|
|
|
|
out << "\t SSL ID: " << it->second.id;
|
|
|
|
out << "\t State: " << it->second.state;
|
|
|
|
out << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
out << "p3LinkMgrIMPL::printPeerLists() Others List";
|
2011-07-11 08:11:58 -04:00
|
|
|
out << std::endl;
|
|
|
|
for(it = mOthersList.begin(); it != mOthersList.end(); it++)
|
|
|
|
{
|
|
|
|
out << "\t SSL ID: " << it->second.id;
|
|
|
|
out << "\t State: " << it->second.state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2011-07-09 20:41:39 -04:00
|
|
|
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
void printConnectState(std::ostream &out, peerConnectState &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 20:41:39 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
|