2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
#include "dht/p3bitdht.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
|
|
#include "bitdht/bdiface.h"
|
|
|
|
#include "bitdht/bdstddht.h"
|
|
|
|
#include "bitdht/bdmanager.h"
|
|
|
|
|
|
|
|
//#include "udp/udplayer.h"
|
|
|
|
|
|
|
|
#include "util/rsnet.h"
|
|
|
|
#include "tcponudp/tou.h"
|
|
|
|
#include "tcponudp/udpstunner.h"
|
|
|
|
#include "tcponudp/udprelay.h"
|
|
|
|
|
|
|
|
#include "pqi/p3netmgr.h"
|
2011-07-14 10:56:33 -04:00
|
|
|
#include "pqi/pqimonitor.h"
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#define PEERNET_CONNECT_TIMEOUT 180 // Should be BIGGER than Higher level (but okay if not!)
|
2011-07-17 19:41:30 -04:00
|
|
|
|
|
|
|
#define MIN_DETERMINISTIC_SWITCH_PERIOD 60
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-10 20:52:12 -04:00
|
|
|
/***
|
|
|
|
*
|
|
|
|
* #define DEBUG_BITDHT_COMMON 1 // These are the things that are called regularly (annoying for debugging specifics)
|
2011-07-18 20:06:09 -04:00
|
|
|
* #define DEBUG_PEERNET 1
|
2011-07-10 20:52:12 -04:00
|
|
|
*
|
|
|
|
**/
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
************************************* Dht Callback ***************************************
|
|
|
|
******************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**** dht NodeCallback ****
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* In the old version, we used this to callback mConnCb->peerStatus()
|
|
|
|
* We might want to drop this, and concentrate on the connection stuff.
|
|
|
|
*
|
|
|
|
* -> if an new dht peer, then pass to Stunners.
|
|
|
|
* -> do we care if we know them? not really!
|
|
|
|
*/
|
|
|
|
|
|
|
|
int p3BitDht::NodeCallback(const bdId *id, uint32_t peerflags)
|
|
|
|
{
|
|
|
|
// No need for Mutex, and Stunners are self protected.
|
|
|
|
|
|
|
|
// These are the flags that will be returned by p3BitDht & RS peers with the new DHT.
|
|
|
|
// if (peerflags & BITDHT_PEER_STATUS_DHT_ENGINE_VERSION) // Change to this later...
|
|
|
|
if ((peerflags & BITDHT_PEER_STATUS_DHT_ENGINE) &&
|
|
|
|
(peerflags & BITDHT_PEER_STATUS_DHT_APPL))
|
|
|
|
{
|
|
|
|
|
|
|
|
/* pass off to the Stunners
|
|
|
|
* but only if they need them.
|
|
|
|
* ideally don't pass to both peers... (XXX do later)
|
|
|
|
*/
|
|
|
|
|
2011-07-13 05:12:31 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
if (id->id == mOwnDhtId)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_BITDHT_COMMON
|
|
|
|
std::cerr << "p3BitDht::NodeCallback() Skipping own id";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
if ((mProxyStunner) && (mProxyStunner->needStunPeers()))
|
|
|
|
{
|
2011-07-10 20:52:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_COMMON
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::NodeCallback() Passing BitDHT Peer to DhtStunner: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
mProxyStunner->addStunPeer(id->addr, "");
|
|
|
|
}
|
|
|
|
/* else */ // removed else until we have lots of peers.
|
|
|
|
|
|
|
|
if ((mDhtStunner) && (mDhtStunner->needStunPeers()))
|
|
|
|
{
|
2011-07-10 20:52:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_COMMON
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::NodeCallback() Passing BitDHT Peer to DhtStunner: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
mDhtStunner->addStunPeer(id->addr, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**** dht PeerCallback ****
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* In the old version, we used this to callback mConnCb->peerConnectRequest()
|
|
|
|
* we need to continue doing this, to maintain compatibility.
|
|
|
|
*
|
|
|
|
* -> update Dht Status, and trigger connect if ONLINE or UNREACHABLE
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::PeerCallback(const bdId *id, uint32_t status)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
//std::ostringstream str;
|
|
|
|
//bdStdPrintNodeId(str, &(id->id));
|
|
|
|
//std::string strId = str.str();
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
//std::cerr << "p3BitDht::dhtPeerCallback()";
|
|
|
|
//std::cerr << std::endl;
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(id->id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
|
|
|
|
if (!dpd)
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "p3BitDht::PeerCallback() ERROR Unknown Peer: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << " status: " << status;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(status)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_NOT_ACTIVE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_QUERY_FAILURE:
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_FAILURE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_QUERY_PEER_OFFLINE:
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_OFFLINE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_QUERY_PEER_UNREACHABLE:
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_UNREACHABLE;
|
|
|
|
UnreachablePeerCallback_locked(id, status, dpd);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_QUERY_PEER_ONLINE:
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_ONLINE;
|
|
|
|
OnlinePeerCallback_locked(id, status, dpd);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
dpd->mDhtUpdateTS = now;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
int p3BitDht::OnlinePeerCallback_locked(const bdId *id, uint32_t /*status*/, DhtPeerDetails *dpd)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-08-12 09:42:30 -04:00
|
|
|
/* remove unused parameter warnings */
|
|
|
|
(void) id;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
if ((dpd->mPeerConnectState != RSDHT_PEERCONN_DISCONNECTED) ||
|
|
|
|
(dpd->mPeerReqState == RSDHT_PEERREQ_RUNNING))
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::OnlinePeerCallback_locked() WARNING Ignoring Callback: connection already underway: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool connectOk = true;
|
2011-07-14 10:56:33 -04:00
|
|
|
bool doTCPCallback = false;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* work out network state */
|
|
|
|
uint32_t connectFlags = dpd->mConnectLogic.connectCb(CSB_CONNECT_DIRECT,
|
2011-07-17 09:48:37 -04:00
|
|
|
mNetMgr->getNetworkMode(), mNetMgr->getNatHoleMode(), mNetMgr->getNatTypeMode());
|
2011-08-12 09:42:30 -04:00
|
|
|
// bool useProxyPort = (connectFlags & CSB_ACTION_PROXY_PORT);
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
switch(connectFlags & CSB_ACTION_MASK_MODE)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case CSB_ACTION_WAIT:
|
|
|
|
{
|
|
|
|
connectOk = false;
|
|
|
|
}
|
|
|
|
break;
|
2011-07-14 10:56:33 -04:00
|
|
|
case CSB_ACTION_TCP_CONN:
|
|
|
|
{
|
|
|
|
connectOk = false;
|
|
|
|
doTCPCallback = true;
|
|
|
|
}
|
2011-07-15 07:13:20 -04:00
|
|
|
break;
|
2011-07-09 20:39:15 -04:00
|
|
|
case CSB_ACTION_DIRECT_CONN:
|
|
|
|
{
|
|
|
|
|
|
|
|
connectOk = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CSB_ACTION_PROXY_CONN:
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "dhtPeerCallback: ERROR ConnectLogic returned PROXY";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
connectOk = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CSB_ACTION_RELAY_CONN:
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "dhtPeerCallback: ERROR ConnectLogic returned RELAY";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
connectOk = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connectOk)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtPeerCallback. Peer Online, triggering Direct Connection for: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
|
|
|
ca.mType = PEERNET_ACTION_TYPE_CONNECT;
|
|
|
|
ca.mMode = BITDHT_CONNECT_MODE_DIRECT;
|
|
|
|
ca.mDestId = *id;
|
|
|
|
ca.mAnswer = BITDHT_CONNECT_ERROR_NONE;
|
|
|
|
|
|
|
|
mActions.push_back(ca);
|
|
|
|
}
|
2011-07-14 10:56:33 -04:00
|
|
|
|
|
|
|
/* might need to make this an ACTION - leave for now */
|
|
|
|
if (doTCPCallback)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-14 10:56:33 -04:00
|
|
|
std::cerr << "dhtPeerCallback. Peer Online, triggering TCP Connection for: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-14 10:56:33 -04:00
|
|
|
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
|
|
|
ca.mType = PEERNET_ACTION_TYPE_TCPATTEMPT;
|
|
|
|
ca.mMode = BITDHT_CONNECT_MODE_DIRECT;
|
|
|
|
ca.mDestId = *id;
|
|
|
|
ca.mAnswer = BITDHT_CONNECT_ERROR_NONE;
|
|
|
|
|
|
|
|
//ConnectCalloutTCPAttempt(dpd->mRsId, id->addr);
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
int p3BitDht::UnreachablePeerCallback_locked(const bdId *id, uint32_t /*status*/, DhtPeerDetails *dpd)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
if ((dpd->mPeerConnectState != RSDHT_PEERCONN_DISCONNECTED) ||
|
|
|
|
(dpd->mPeerReqState == RSDHT_PEERREQ_RUNNING))
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::UnreachablePeerCallback_locked() WARNING Ignoring Callback: connection already underway: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtPeerCallback. Peer Unreachable, triggering Proxy | Relay Connection for: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
bool proxyOk = false;
|
|
|
|
bool connectOk = true;
|
|
|
|
|
|
|
|
/* work out network state */
|
|
|
|
uint32_t connectFlags = dpd->mConnectLogic.connectCb(CSB_CONNECT_UNREACHABLE,
|
2011-07-17 09:48:37 -04:00
|
|
|
mNetMgr->getNetworkMode(), mNetMgr->getNatHoleMode(), mNetMgr->getNatTypeMode());
|
2011-07-09 20:39:15 -04:00
|
|
|
bool useProxyPort = (connectFlags & CSB_ACTION_PROXY_PORT);
|
|
|
|
|
|
|
|
switch(connectFlags & CSB_ACTION_MASK_MODE)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case CSB_ACTION_WAIT:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-15 07:13:20 -04:00
|
|
|
std::cerr << "dhtPeerCallback. Request to Wait ... so no Connection Attempt for ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
connectOk = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CSB_ACTION_DIRECT_CONN:
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "dhtPeerCallback: ERROR ConnectLogic returned DIRECT";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
connectOk = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CSB_ACTION_PROXY_CONN:
|
|
|
|
{
|
|
|
|
proxyOk = true;
|
|
|
|
connectOk = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CSB_ACTION_RELAY_CONN:
|
|
|
|
{
|
|
|
|
proxyOk = false;
|
|
|
|
connectOk = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connectOk)
|
|
|
|
{
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
|
|
|
ca.mType = PEERNET_ACTION_TYPE_CONNECT;
|
|
|
|
ca.mDestId = *id;
|
|
|
|
|
|
|
|
dpd->mConnectLogic.storeProxyPortChoice(connectFlags, useProxyPort);
|
|
|
|
|
|
|
|
if (proxyOk)
|
|
|
|
{
|
|
|
|
ca.mMode = BITDHT_CONNECT_MODE_PROXY;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtPeerCallback. Trying Proxy Connection.";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtPeerCallback. Trying Relay Connection.";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
ca.mMode = BITDHT_CONNECT_MODE_RELAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
ca.mAnswer = BITDHT_CONNECT_ERROR_NONE;
|
|
|
|
mActions.push_back(ca);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-15 07:13:20 -04:00
|
|
|
std::cerr << "dhtPeerCallback. Cancelled Connection Attempt for ";
|
2011-07-09 20:39:15 -04:00
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
int p3BitDht::ValueCallback(const bdNodeId */*id*/, std::string /*key*/, uint32_t /*status*/)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
std::cerr << "p3BitDht::ValueCallback() ERROR Does nothing!";
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::ConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
2011-07-17 19:41:30 -04:00
|
|
|
uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::ConnectCallback()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "srcId: ";
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "proxyId: ";
|
|
|
|
bdStdPrintId(std::cerr, proxyId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "destId: ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
std::cerr << " mode: " << mode;
|
|
|
|
std::cerr << " param: " << param;
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << " point: " << point;
|
|
|
|
std::cerr << " cbtype: " << cbtype;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
/* we handle MID and START/END points differently... this is biggest difference.
|
|
|
|
* so handle first.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bdId peerId;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
switch(point)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case BD_PROXY_CONNECTION_UNKNOWN_POINT:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
std::cerr << "p3BitDht::dhtConnectCallback() ERROR UNKNOWN point, ignoring Callback";
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case BD_PROXY_CONNECTION_START_POINT:
|
|
|
|
peerId = *destId;
|
|
|
|
break;
|
|
|
|
case BD_PROXY_CONNECTION_END_POINT:
|
|
|
|
peerId = *srcId;
|
|
|
|
break;
|
|
|
|
case BD_PROXY_CONNECTION_MID_POINT:
|
|
|
|
{
|
|
|
|
/* AS a mid point, we can receive.... AUTH,PENDING,PROXY,FAILED */
|
|
|
|
|
|
|
|
switch(cbtype)
|
|
|
|
{
|
|
|
|
case BITDHT_CONNECT_CB_AUTH:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Proxy Connection Requested Between:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 19:41:30 -04:00
|
|
|
|
|
|
|
uint32_t bandwidth = 0;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
int connectionAllowed = BITDHT_CONNECT_ERROR_GENERIC;
|
2011-07-17 19:41:30 -04:00
|
|
|
if (checkProxyAllowed(srcId, destId, mode, bandwidth))
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ANSWER_OKAY;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Connection Allowed";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ERROR_AUTH_DENIED;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Connection Denied";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
|
|
|
ca.mType = PEERNET_ACTION_TYPE_AUTHORISE;
|
|
|
|
ca.mMode = mode;
|
|
|
|
ca.mProxyId = *proxyId;
|
|
|
|
ca.mSrcId = *srcId;
|
|
|
|
ca.mDestId = *destId;
|
|
|
|
ca.mPoint = point;
|
|
|
|
ca.mAnswer = connectionAllowed;
|
2011-07-17 19:41:30 -04:00
|
|
|
ca.mDelayOrBandwidth = bandwidth;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
mActions.push_back(ca);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BITDHT_CONNECT_CB_PENDING:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Proxy Connection Pending:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BITDHT_CONNECT_CB_PROXY:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Proxy Connection Starting:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BITDHT_CONNECT_CB_FAILED:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Proxy Connection Failed:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
|
|
|
|
std::cerr << " ErrorCode: " << errcode;
|
|
|
|
int errsrc = errcode & BITDHT_CONNECT_ERROR_MASK_SOURCE;
|
|
|
|
int errtype = errcode & BITDHT_CONNECT_ERROR_MASK_TYPE;
|
|
|
|
|
|
|
|
std::cerr << " ErrorSrc: " << errsrc;
|
|
|
|
std::cerr << " ErrorType: " << errtype;
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
if (mode == BITDHT_CONNECT_MODE_RELAY)
|
|
|
|
{
|
|
|
|
removeRelayConnection(srcId, destId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case BITDHT_CONNECT_CB_START:
|
|
|
|
{
|
|
|
|
std::cerr << "dhtConnectionCallback() ERROR unexpected Proxy ConnectionCallback:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* End the MID Point stuff */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we get here, we are an endpoint (peer specified in peerId) */
|
2011-07-27 09:56:35 -04:00
|
|
|
|
|
|
|
/* translate id into string for exclusive mode */
|
|
|
|
std::ostringstream idstr;
|
|
|
|
bdStdPrintNodeId(idstr, &(peerId.id));
|
|
|
|
std::string pid = idstr.str();
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
switch(cbtype)
|
|
|
|
{
|
|
|
|
case BITDHT_CONNECT_CB_AUTH:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Connection Requested By: ";
|
|
|
|
bdStdPrintId(std::cerr, &(peerId));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
int connectionAllowed = BITDHT_CONNECT_ERROR_GENERIC;
|
|
|
|
if (checkConnectionAllowed(&(peerId), mode))
|
|
|
|
{
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ANSWER_OKAY;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Connection Allowed";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ERROR_AUTH_DENIED;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Connection Denied";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
|
|
|
ca.mType = PEERNET_ACTION_TYPE_AUTHORISE;
|
|
|
|
ca.mProxyId = *proxyId;
|
|
|
|
ca.mSrcId = *srcId;
|
|
|
|
ca.mDestId = *destId;
|
2011-07-17 09:48:37 -04:00
|
|
|
ca.mDelayOrBandwidth = 0;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* Check Proxy ExtAddress Status (but only if connection is Allowed) */
|
|
|
|
if ((connectionAllowed == BITDHT_CONNECT_ANSWER_OKAY) &&
|
|
|
|
(mode == BITDHT_CONNECT_MODE_PROXY))
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Checking Address for Proxy";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
struct sockaddr_in extaddr;
|
|
|
|
uint8_t extStable = 0;
|
|
|
|
sockaddr_clear(&extaddr);
|
|
|
|
|
|
|
|
bool connectOk = false;
|
|
|
|
bool proxyPort = false;
|
2011-07-17 09:48:37 -04:00
|
|
|
bool exclusivePort = false;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback(): Proxy... deciding which port to use.";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(peerId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (dpd)
|
|
|
|
{
|
|
|
|
proxyPort = dpd->mConnectLogic.shouldUseProxyPort(
|
2011-07-17 09:48:37 -04:00
|
|
|
mNetMgr->getNetworkMode(), mNetMgr->getNatHoleMode(), mNetMgr->getNatTypeMode());
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
dpd->mConnectLogic.storeProxyPortChoice(0, proxyPort);
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: Setting ProxyPort: ";
|
|
|
|
std::cerr << " UseProxyPort? " << proxyPort;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-17 12:24:45 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: Checking ConnectLogic.NetState: ";
|
|
|
|
std::cerr << dpd->mConnectLogic.calcNetState(mNetMgr->getNetworkMode(), mNetMgr->getNatHoleMode(), mNetMgr->getNatTypeMode());
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 12:24:45 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
if (proxyPort)
|
|
|
|
{
|
2011-07-17 12:24:45 -04:00
|
|
|
exclusivePort = (CSB_NETSTATE_EXCLUSIVENAT == dpd->mConnectLogic.calcNetState(
|
|
|
|
mNetMgr->getNetworkMode(), mNetMgr->getNatHoleMode(), mNetMgr->getNatTypeMode()));
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (exclusivePort)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: we Require Exclusive Proxy Port for connection";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
2011-07-17 12:24:45 -04:00
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 12:24:45 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: Dont need Exclusive Proxy Port for connection";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 12:24:45 -04:00
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
connectOk = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction: Connect Proxy: ERROR Cannot find PeerStatus";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpStunner *stunner = mProxyStunner;
|
|
|
|
if (!proxyPort)
|
|
|
|
{
|
|
|
|
stunner = mDhtStunner;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((connectOk) && (stunner) && (stunner->externalAddr(extaddr, extStable)))
|
|
|
|
{
|
|
|
|
if (extStable)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Proxy Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(peerId));
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << " is Ok as we have Stable Own External Proxy Address";
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
if (point == BD_PROXY_CONNECTION_END_POINT)
|
|
|
|
{
|
|
|
|
ca.mDestId.addr = extaddr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ca.mSrcId.addr = extaddr;
|
|
|
|
std::cerr << "dhtConnectionCallback() ERROR Proxy Auth as SrcId";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
|
|
|
/* check if we require exclusive use of the proxy port */
|
|
|
|
if (exclusivePort)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: Attempting to Grab ExclusiveLock of UdpStunner";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-27 09:56:35 -04:00
|
|
|
|
|
|
|
int stun_age = mProxyStunner->grabExclusiveMode(pid);
|
2011-07-17 19:41:30 -04:00
|
|
|
if (stun_age > 0)
|
2011-07-17 09:48:37 -04:00
|
|
|
{
|
2011-07-17 19:41:30 -04:00
|
|
|
int delay = 0;
|
|
|
|
if (stun_age < MIN_DETERMINISTIC_SWITCH_PERIOD)
|
|
|
|
{
|
|
|
|
delay = MIN_DETERMINISTIC_SWITCH_PERIOD - stun_age;
|
|
|
|
}
|
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
/* great we got it! */
|
|
|
|
ca.mDelayOrBandwidth = delay;
|
2011-07-17 19:41:30 -04:00
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 19:41:30 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: GotExclusiveLock With Delay: " << delay;
|
|
|
|
std::cerr << " for stable port";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 19:41:30 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(peerId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (dpd)
|
|
|
|
{
|
|
|
|
dpd->mExclusiveProxyLock = true;
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: Success at grabbing ExclusiveLock of UdpStunner";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction: Connect Proxy: ERROR Cannot find PeerStatus";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ERROR_TEMPUNAVAIL;
|
2011-07-27 09:56:35 -04:00
|
|
|
mProxyStunner->releaseExclusiveMode(pid,false);
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* failed to get exclusive mode - must wait */
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ERROR_TEMPUNAVAIL;
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "dhtConnectionCallback: Failed to Grab ExclusiveLock, Returning TEMPUNAVAIL";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-17 09:48:37 -04:00
|
|
|
if (exclusivePort)
|
|
|
|
{
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ERROR_TEMPUNAVAIL;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Proxy Connection";
|
|
|
|
std::cerr << " is Discarded, as External Proxy Address is Not Stable! (EXCLUSIVE MODE)";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ERROR_UNREACHABLE;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Proxy Connection";
|
|
|
|
std::cerr << " is Discarded, as Own External Proxy Address is Not Stable!";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connectionAllowed = BITDHT_CONNECT_ERROR_TEMPUNAVAIL;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() ERROR Proxy Connection ";
|
|
|
|
std::cerr << " is Discarded, as Failed to get Own External Proxy Address.";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ca.mMode = mode;
|
|
|
|
ca.mPoint = point;
|
|
|
|
ca.mAnswer = connectionAllowed;
|
|
|
|
|
|
|
|
mActions.push_back(ca);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_CONNECT_CB_START:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Connection Starting with: ";
|
|
|
|
bdStdPrintId(std::cerr, &(peerId));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
|
|
|
ca.mType = PEERNET_ACTION_TYPE_START;
|
|
|
|
ca.mMode = mode;
|
|
|
|
ca.mProxyId = *proxyId;
|
|
|
|
ca.mSrcId = *srcId;
|
|
|
|
ca.mDestId = *destId;
|
|
|
|
ca.mPoint = point;
|
2011-07-17 19:41:30 -04:00
|
|
|
ca.mDelayOrBandwidth = param;
|
2011-07-09 20:39:15 -04:00
|
|
|
ca.mAnswer = BITDHT_CONNECT_ERROR_NONE;
|
|
|
|
|
|
|
|
mActions.push_back(ca);
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_CONNECT_CB_FAILED:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Connection Attempt Failed with:";
|
|
|
|
bdStdPrintId(std::cerr, &(peerId));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "dhtConnectionCallback() Proxy:";
|
|
|
|
bdStdPrintId(std::cerr, proxyId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "dhtConnectionCallback() ";
|
|
|
|
std::cerr << " ErrorCode: " << errcode;
|
|
|
|
int errsrc = errcode & BITDHT_CONNECT_ERROR_MASK_SOURCE;
|
|
|
|
int errtype = errcode & BITDHT_CONNECT_ERROR_MASK_TYPE;
|
|
|
|
|
|
|
|
std::cerr << " ErrorSrc: " << errsrc;
|
|
|
|
std::cerr << " ErrorType: " << errtype;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(peerId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (dpd)
|
|
|
|
{
|
|
|
|
dpd->mPeerCbMsg = "ERROR : ";
|
|
|
|
dpd->mPeerCbMsg += decodeConnectionError(errcode);
|
|
|
|
dpd->mPeerCbMode = mode;
|
|
|
|
dpd->mPeerCbPoint = point;
|
|
|
|
dpd->mPeerCbProxyId = *proxyId;
|
|
|
|
dpd->mPeerCbDestId = peerId;
|
|
|
|
dpd->mPeerCbTS = now;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "dhtConnectionCallback() ";
|
|
|
|
std::cerr << "ERROR Unknown Peer";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_CONNECT_CB_REQUEST:
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "dhtConnectionCallback() Local Connection Request Feedback:";
|
|
|
|
bdStdPrintId(std::cerr, &(peerId));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "dhtConnectionCallback() Proxy:";
|
|
|
|
bdStdPrintId(std::cerr, proxyId);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
if (point != BD_PROXY_CONNECTION_START_POINT)
|
|
|
|
{
|
|
|
|
std::cerr << "dhtConnectionCallback() ERROR not START_POINT";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(peerId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (dpd)
|
|
|
|
{
|
|
|
|
if (errcode)
|
|
|
|
{
|
2011-07-17 12:24:45 -04:00
|
|
|
ReleaseProxyExclusiveMode_locked(dpd, false);
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
dpd->mPeerReqStatusMsg = "STOPPED: ";
|
|
|
|
dpd->mPeerReqStatusMsg += decodeConnectionError(errcode);
|
|
|
|
dpd->mPeerReqState = RSDHT_PEERREQ_STOPPED;
|
|
|
|
dpd->mPeerReqTS = now;
|
|
|
|
|
|
|
|
int updatecode = CSB_UPDATE_FAILED_ATTEMPT;
|
|
|
|
int errtype = errcode & BITDHT_CONNECT_ERROR_MASK_TYPE;
|
|
|
|
switch(errtype)
|
|
|
|
{
|
|
|
|
/* fatal errors */
|
|
|
|
case BITDHT_CONNECT_ERROR_AUTH_DENIED:
|
|
|
|
{
|
|
|
|
updatecode = CSB_UPDATE_AUTH_DENIED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move on errors */
|
|
|
|
case BITDHT_CONNECT_ERROR_UNREACHABLE: // sym NAT.
|
|
|
|
case BITDHT_CONNECT_ERROR_UNSUPPORTED: // mode unavailable.
|
|
|
|
{
|
|
|
|
updatecode = CSB_UPDATE_MODE_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-07-16 09:01:08 -04:00
|
|
|
|
|
|
|
/* retry attempts */
|
|
|
|
case BITDHT_CONNECT_ERROR_TEMPUNAVAIL:
|
|
|
|
case BITDHT_CONNECT_ERROR_DUPLICATE:
|
|
|
|
{
|
|
|
|
updatecode = CSB_UPDATE_RETRY_ATTEMPT;
|
|
|
|
}
|
|
|
|
break;
|
2011-07-09 20:39:15 -04:00
|
|
|
/* standard failed attempts */
|
|
|
|
case BITDHT_CONNECT_ERROR_GENERIC:
|
|
|
|
case BITDHT_CONNECT_ERROR_PROTOCOL:
|
|
|
|
case BITDHT_CONNECT_ERROR_TIMEOUT:
|
|
|
|
case BITDHT_CONNECT_ERROR_NOADDRESS:
|
|
|
|
case BITDHT_CONNECT_ERROR_OVERLOADED:
|
|
|
|
/* CB_REQUEST errors */
|
|
|
|
case BITDHT_CONNECT_ERROR_TOOMANYRETRY:
|
|
|
|
case BITDHT_CONNECT_ERROR_OUTOFPROXY:
|
|
|
|
{
|
|
|
|
updatecode = CSB_UPDATE_FAILED_ATTEMPT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* user cancelled ... no update */
|
|
|
|
case BITDHT_CONNECT_ERROR_USER:
|
|
|
|
{
|
|
|
|
updatecode = CSB_UPDATE_NONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (updatecode)
|
|
|
|
{
|
|
|
|
dpd->mConnectLogic.updateCb(updatecode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // a new connection attempt.
|
|
|
|
{
|
|
|
|
dpd->mPeerReqStatusMsg = "Connect Attempt";
|
|
|
|
dpd->mPeerReqState = RSDHT_PEERREQ_RUNNING;
|
|
|
|
dpd->mPeerReqMode = mode;
|
|
|
|
dpd->mPeerReqProxyId = *proxyId;
|
|
|
|
dpd->mPeerReqTS = now;
|
|
|
|
|
|
|
|
// This also is flagged into the instant Cb info.
|
|
|
|
dpd->mPeerCbMsg = "Local Connect Attempt";
|
|
|
|
dpd->mPeerCbMode = mode;
|
|
|
|
dpd->mPeerCbPoint = point;
|
|
|
|
dpd->mPeerCbProxyId = *proxyId;
|
|
|
|
dpd->mPeerCbDestId = peerId;
|
|
|
|
dpd->mPeerCbTS = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "dhtConnectionCallback() ERROR Cannot find PeerStatus";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case BITDHT_CONNECT_CB_PENDING:
|
|
|
|
case BITDHT_CONNECT_CB_PROXY:
|
|
|
|
{
|
|
|
|
std::cerr << "dhtConnectionCallback() ERROR unexpected ConnectionCallback:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
******************************** Dht Actions / Monitoring ********************************
|
|
|
|
******************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* tick stuff that isn't in its own thread */
|
|
|
|
|
|
|
|
int p3BitDht::tick()
|
|
|
|
{
|
|
|
|
doActions();
|
|
|
|
monitorConnections();
|
|
|
|
|
|
|
|
minuteTick();
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET_COMMON
|
2011-08-12 09:42:30 -04:00
|
|
|
time_t now = time(NULL);
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::tick() TIME: " << ctime(&now) << std::endl;
|
|
|
|
std::cerr.flush();
|
2011-07-10 20:52:12 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MINUTE_IN_SECS 60
|
|
|
|
|
|
|
|
int p3BitDht::minuteTick()
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
int deltaT = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
deltaT = now-mMinuteTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deltaT > MINUTE_IN_SECS)
|
|
|
|
{
|
|
|
|
mRelay->checkRelays();
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
mMinuteTS = now;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::doActions()
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET_COMMON
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::doActions()" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
while(mActions.size() > 0)
|
|
|
|
{
|
|
|
|
PeerAction action;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
if (mActions.size() < 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
action = mActions.front();
|
|
|
|
mActions.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(action.mType)
|
|
|
|
{
|
|
|
|
case PEERNET_ACTION_TYPE_CONNECT:
|
|
|
|
{
|
|
|
|
/* connect attempt */
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction. Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " mode: " << action.mMode;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
bool doConnectionRequest = false;
|
2011-07-15 07:13:20 -04:00
|
|
|
bool connectionReqFailed = false;
|
2011-07-17 09:48:37 -04:00
|
|
|
bool grabbedExclusivePort = false;
|
|
|
|
|
2011-07-27 09:56:35 -04:00
|
|
|
/* translate id into string for exclusive mode */
|
|
|
|
std::ostringstream idstr;
|
|
|
|
bdStdPrintNodeId(idstr, &(action.mDestId.id));
|
|
|
|
std::string pid = idstr.str();
|
|
|
|
|
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
// Parameters that will be used for the Connect Request.
|
|
|
|
struct sockaddr_in connAddr; // We zero this address. (DHT Layer handles most cases)
|
|
|
|
sockaddr_clear(&connAddr);
|
|
|
|
uint32_t connStart = 1; /* > 0 indicates GO (number indicates required startup delay) */
|
|
|
|
uint32_t connDelay = 0;
|
|
|
|
|
|
|
|
uint32_t failReason = CSB_UPDATE_MODE_UNAVAILABLE;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
if ((action.mMode == BITDHT_CONNECT_MODE_DIRECT) ||
|
|
|
|
(action.mMode == BITDHT_CONNECT_MODE_RELAY))
|
|
|
|
{
|
2011-07-17 09:48:37 -04:00
|
|
|
doConnectionRequest = true;
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else if (action.mMode == BITDHT_CONNECT_MODE_PROXY)
|
|
|
|
{
|
|
|
|
struct sockaddr_in extaddr;
|
|
|
|
uint8_t extStable = 0;
|
|
|
|
sockaddr_clear(&extaddr);
|
|
|
|
bool proxyPort = true;
|
2011-07-17 09:48:37 -04:00
|
|
|
bool exclusivePort = false;
|
2011-07-09 20:39:15 -04:00
|
|
|
bool connectOk = false;
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction: Proxy... deciding which port to use.";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(action.mDestId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (dpd)
|
|
|
|
{
|
|
|
|
connectOk = true;
|
|
|
|
proxyPort = dpd->mConnectLogic.getProxyPortChoice();
|
2011-07-17 09:48:37 -04:00
|
|
|
if (proxyPort)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 12:24:45 -04:00
|
|
|
std::cerr << "PeerAction: Using ProxyPort. Checking ConnectLogic.NetState: ";
|
|
|
|
std::cerr << dpd->mConnectLogic.calcNetState(mNetMgr->getNetworkMode(),
|
|
|
|
mNetMgr->getNatHoleMode(), mNetMgr->getNatTypeMode());
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 12:24:45 -04:00
|
|
|
|
|
|
|
exclusivePort = (CSB_NETSTATE_EXCLUSIVENAT == dpd->mConnectLogic.calcNetState(
|
|
|
|
mNetMgr->getNetworkMode(), mNetMgr->getNatHoleMode(), mNetMgr->getNatTypeMode()));
|
|
|
|
|
|
|
|
if (exclusivePort)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 12:24:45 -04:00
|
|
|
std::cerr << "PeerAction: NetState indicates Exclusive Mode";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 12:24:45 -04:00
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction: Connect Proxy: ERROR Cannot find PeerStatus";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UdpStunner *stunner = mProxyStunner;
|
|
|
|
if (!proxyPort)
|
|
|
|
{
|
|
|
|
stunner = mDhtStunner;
|
|
|
|
}
|
|
|
|
|
2011-07-27 09:56:35 -04:00
|
|
|
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
if ((connectOk) && (stunner) && (stunner->externalAddr(extaddr, extStable)))
|
|
|
|
{
|
|
|
|
if (extStable)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction: Proxy Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " is OkGo as we have Stable Own External Proxy Address";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
|
|
|
|
/* check if we require exclusive use of the proxy port */
|
|
|
|
if (exclusivePort)
|
2011-07-15 07:13:20 -04:00
|
|
|
{
|
2011-07-27 09:56:35 -04:00
|
|
|
int stun_age = mProxyStunner->grabExclusiveMode(pid);
|
2011-07-17 19:41:30 -04:00
|
|
|
if (stun_age > 0)
|
2011-07-17 09:48:37 -04:00
|
|
|
{
|
2011-07-17 19:41:30 -04:00
|
|
|
int delay = 0;
|
|
|
|
if (stun_age < MIN_DETERMINISTIC_SWITCH_PERIOD)
|
|
|
|
{
|
|
|
|
delay = MIN_DETERMINISTIC_SWITCH_PERIOD - stun_age;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 19:41:30 -04:00
|
|
|
std::cerr << "PeerAction: Stunner has indicated a Delay of " << delay;
|
|
|
|
std::cerr << " to ensure a stable Port!";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 19:41:30 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
/* great we got it! */
|
|
|
|
connAddr = extaddr;
|
|
|
|
connDelay = delay;
|
|
|
|
doConnectionRequest = true;
|
|
|
|
grabbedExclusivePort = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* failed to get exclusive mode - must wait */
|
|
|
|
connectionReqFailed = true;
|
|
|
|
failReason = CSB_UPDATE_RETRY_ATTEMPT;
|
|
|
|
}
|
2011-07-15 07:13:20 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-17 09:48:37 -04:00
|
|
|
/* stable and non-exclusive - go for it */
|
|
|
|
connAddr = extaddr;
|
|
|
|
connStart = 1;
|
|
|
|
doConnectionRequest = true;
|
2011-07-15 07:13:20 -04:00
|
|
|
}
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "PeerAction: WARNING Proxy Connection Attempt to: ";
|
2011-07-09 20:39:15 -04:00
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " is Discarded, as Own External Proxy Address is Not Stable!";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-15 07:13:20 -04:00
|
|
|
connectionReqFailed = true;
|
2011-07-17 09:48:37 -04:00
|
|
|
if (exclusivePort)
|
|
|
|
{
|
|
|
|
failReason = CSB_UPDATE_RETRY_ATTEMPT;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "PeerAction: As Exclusive Mode, Port Will stabilise => RETRY";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failReason = CSB_UPDATE_MODE_UNAVAILABLE;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "PeerAction: Not Exclusive Mode, => MODE UNAVAILABLE";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
}
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction: ERROR Proxy Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " is Discarded, as Failed to get Own External Proxy Address.";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
connectionReqFailed = true;
|
|
|
|
failReason = CSB_UPDATE_RETRY_ATTEMPT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doConnectionRequest)
|
|
|
|
{
|
2011-07-17 19:41:30 -04:00
|
|
|
if (mUdpBitDht->ConnectionRequest(&connAddr, &(action.mDestId.id), action.mMode, connDelay, connStart))
|
2011-07-17 09:48:37 -04:00
|
|
|
{
|
2011-07-09 20:39:15 -04:00
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "PeerAction: Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " has gone ahead";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(action.mDestId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (dpd)
|
|
|
|
{
|
2011-07-17 09:48:37 -04:00
|
|
|
dpd->mPeerReqStatusMsg = "Connect Request";
|
|
|
|
dpd->mPeerReqState = RSDHT_PEERREQ_RUNNING;
|
|
|
|
dpd->mPeerReqMode = action.mMode;
|
|
|
|
dpd->mPeerReqTS = now;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
if (grabbedExclusivePort)
|
|
|
|
{
|
|
|
|
dpd->mExclusiveProxyLock = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction: Connect ERROR Cannot find PeerStatus";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-17 09:48:37 -04:00
|
|
|
connectionReqFailed = true;
|
|
|
|
failReason = CSB_UPDATE_MODE_UNAVAILABLE;
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 07:13:20 -04:00
|
|
|
if (connectionReqFailed)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-15 07:13:20 -04:00
|
|
|
std::cerr << "PeerAction: Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " is Discarded, as Mode is Unavailable";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-15 07:13:20 -04:00
|
|
|
|
2011-07-27 09:56:35 -04:00
|
|
|
//if (grabbedExclusivePort)
|
|
|
|
//{
|
|
|
|
// mProxyStunner->releaseExclusiveMode(pid,false);
|
|
|
|
//}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-15 07:13:20 -04:00
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(action.mDestId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (dpd)
|
|
|
|
{
|
2011-07-17 09:48:37 -04:00
|
|
|
dpd->mConnectLogic.updateCb(failReason);
|
2011-07-15 07:13:20 -04:00
|
|
|
|
|
|
|
dpd->mPeerReqStatusMsg = "Req Mode Unavailable";
|
|
|
|
dpd->mPeerReqState = RSDHT_PEERREQ_STOPPED;
|
|
|
|
dpd->mPeerReqMode = action.mMode;
|
|
|
|
dpd->mPeerReqTS = now;
|
2011-07-27 09:56:35 -04:00
|
|
|
|
|
|
|
if (grabbedExclusivePort)
|
|
|
|
{
|
|
|
|
ReleaseProxyExclusiveMode_locked(dpd, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction: ERROR Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " has no Internal Dht Peer!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (grabbedExclusivePort)
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction: ERROR ERROR, we grabd Exclusive Port to do this, trying emergency release";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
mProxyStunner->releaseExclusiveMode(pid,false);
|
|
|
|
}
|
2011-07-15 07:13:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PEERNET_ACTION_TYPE_AUTHORISE:
|
|
|
|
{
|
|
|
|
/* connect attempt */
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction. Authorise Connection between: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mSrcId));
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " mode: " << action.mMode;
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << " delay/bandwidth: " << action.mDelayOrBandwidth;
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
int delay = 0;
|
|
|
|
int bandwidth = 0;
|
|
|
|
if (action.mMode == BITDHT_CONNECT_MODE_RELAY)
|
|
|
|
{
|
|
|
|
bandwidth = action.mDelayOrBandwidth;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delay = action.mDelayOrBandwidth;
|
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
mUdpBitDht->ConnectionAuth(&(action.mSrcId), &(action.mProxyId), &(action.mDestId),
|
2011-07-17 19:41:30 -04:00
|
|
|
action.mMode, action.mPoint, bandwidth, delay, action.mAnswer);
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
// Only feedback to the gui if we are at END.
|
|
|
|
if (action.mPoint == BD_PROXY_CONNECTION_END_POINT)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(action.mSrcId.id), RSDHT_PEERTYPE_ANY);
|
|
|
|
if (dpd)
|
|
|
|
{
|
|
|
|
if (action.mAnswer)
|
|
|
|
{
|
|
|
|
dpd->mPeerCbMsg = "WE DENIED AUTH: ERROR : ";
|
|
|
|
dpd->mPeerCbMsg += decodeConnectionError(action.mAnswer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dpd->mPeerCbMsg = "We AUTHED";
|
|
|
|
}
|
|
|
|
dpd->mPeerCbMode = action.mMode;
|
|
|
|
dpd->mPeerCbPoint = action.mPoint;
|
|
|
|
dpd->mPeerCbProxyId = action.mProxyId;
|
|
|
|
dpd->mPeerCbDestId = action.mSrcId;
|
|
|
|
dpd->mPeerCbTS = now;
|
|
|
|
}
|
|
|
|
// Not an error if AUTH_DENIED - cos we don't know them! (so won't be in peerList).
|
|
|
|
else if (action.mAnswer | BITDHT_CONNECT_ERROR_AUTH_DENIED)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction Authorise Connection ";
|
|
|
|
std::cerr << "Denied Unknown Peer";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction Authorise Connection ";
|
|
|
|
std::cerr << "ERROR Unknown Peer & !DENIED ???";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PEERNET_ACTION_TYPE_START:
|
|
|
|
{
|
|
|
|
/* connect attempt */
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction. Start Connection between: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mSrcId));
|
|
|
|
std::cerr << " and ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " mode: " << action.mMode;
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << " delay/bandwidth: " << action.mDelayOrBandwidth;
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
initiateConnection(&(action.mSrcId), &(action.mProxyId), &(action.mDestId),
|
2011-07-17 19:41:30 -04:00
|
|
|
action.mMode, action.mPoint, action.mDelayOrBandwidth);
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PEERNET_ACTION_TYPE_RESTARTREQ:
|
|
|
|
{
|
|
|
|
/* connect attempt */
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction. Restart Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " mode: " << action.mMode;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
struct sockaddr_in laddr;
|
|
|
|
sockaddr_clear(&laddr);
|
|
|
|
uint32_t start = 1;
|
2011-07-17 19:41:30 -04:00
|
|
|
uint32_t delay = 0;
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
mUdpBitDht->ConnectionRequest(&laddr, &(action.mDestId.id), action.mMode, delay, start);
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PEERNET_ACTION_TYPE_KILLREQ:
|
|
|
|
{
|
|
|
|
/* connect attempt */
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "PeerAction. Kill Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " mode: " << action.mMode;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
struct sockaddr_in laddr;
|
|
|
|
sockaddr_clear(&laddr);
|
|
|
|
uint32_t start = 0;
|
2011-07-17 19:41:30 -04:00
|
|
|
uint32_t delay = 0;
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
mUdpBitDht->ConnectionRequest(&laddr, &(action.mDestId.id), action.mMode, delay, start);
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
case PEERNET_ACTION_TYPE_TCPATTEMPT:
|
|
|
|
{
|
|
|
|
/* connect attempt */
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-14 10:56:33 -04:00
|
|
|
std::cerr << "PeerAction. TCP Connection Attempt to: ";
|
|
|
|
bdStdPrintId(std::cerr, &(action.mDestId));
|
|
|
|
std::cerr << " mode: " << action.mMode;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-14 10:56:33 -04:00
|
|
|
|
|
|
|
std::string peerRsId;
|
|
|
|
bool foundPeerId = false;
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(action.mDestId.id), RSDHT_PEERTYPE_ANY);
|
|
|
|
if (dpd)
|
|
|
|
{
|
|
|
|
peerRsId = dpd->mRsId;
|
|
|
|
foundPeerId = true;
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-14 10:56:33 -04:00
|
|
|
std::cerr << "PeerAction. TCP Connection Attempt. DoingCallback for RsID: ";
|
|
|
|
std::cerr << peerRsId;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-14 10:56:33 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "PeerAction. TCP Connection Attempt. ERROR unknown peer";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundPeerId)
|
|
|
|
{
|
|
|
|
ConnectCalloutTCPAttempt(peerRsId, action.mDestId.addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************** Connection Logic ***************************/
|
|
|
|
|
|
|
|
/* Proxies!.
|
|
|
|
*
|
|
|
|
* We can allow all PROXY connections, as it is just a couple of messages,
|
|
|
|
* however, we should be smart about how many RELAY connections are allowed.
|
|
|
|
*
|
|
|
|
* e.g. Allow 20 Relay connections.
|
|
|
|
* 15 for friends, 5 for randoms.
|
|
|
|
*
|
|
|
|
* Can also validate addresses with own secure connections.
|
|
|
|
*/
|
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
int p3BitDht::checkProxyAllowed(const bdId *srcId, const bdId *destId, int mode, uint32_t &bandwidth)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::checkProxyAllowed()";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
// Dont think that a mutex is required here! But might be so just lock to ensure that it is possible.
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == BITDHT_CONNECT_MODE_PROXY)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::checkProxyAllowed() Allowing all PROXY connections, OKAY";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
bandwidth = 0; // unlimited as p2p.
|
2011-07-09 20:39:15 -04:00
|
|
|
return 1;
|
|
|
|
//return CONNECTION_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != BITDHT_CONNECT_MODE_RELAY)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::checkProxyAllowed() unknown Connect Mode DENIED";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
/* will install the Relay Here... so that we reserve the Relay Space for later.
|
|
|
|
* decide on relay bandwidth limitation as well
|
|
|
|
*/
|
|
|
|
if (installRelayConnection(srcId, destId, bandwidth))
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::checkProxyAllowed() Successfully added Relay, Connection OKAY";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
// CONNECT_OKAY.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::checkProxyAllowed() Failed to install Relay, Connection DENIED";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
//return CONNECT_MODE_OVERLOADED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
//return CONNECT_MODE_NOTAVAILABLE;
|
|
|
|
//return CONNECT_MODE_OVERLOADED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::checkConnectionAllowed(const bdId *peerId, int mode)
|
|
|
|
{
|
2011-08-12 09:42:30 -04:00
|
|
|
(void) mode;
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::checkConnectionAllowed() to: ";
|
|
|
|
bdStdPrintId(std::cerr,peerId);
|
|
|
|
std::cerr << " mode: " << mode;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
/* check if they are in our friend list */
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(peerId->id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (!dpd)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::checkConnectionAllowed() Peer Not Friend, DENIED";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* store as failed connection attempt */
|
|
|
|
std::map<bdNodeId, DhtPeerDetails>::iterator it;
|
|
|
|
it = mFailedPeers.find(peerId->id);
|
|
|
|
if (it == mFailedPeers.end())
|
|
|
|
{
|
|
|
|
mFailedPeers[peerId->id] = DhtPeerDetails();
|
|
|
|
it = mFailedPeers.find(peerId->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flag as failed */
|
|
|
|
it->second.mDhtId = *peerId;
|
|
|
|
it->second.mDhtState = RSDHT_PEERDHT_NOT_ACTIVE;
|
|
|
|
it->second.mDhtUpdateTS = now;
|
|
|
|
|
|
|
|
it->second.mPeerType = RSDHT_PEERTYPE_OTHER;
|
|
|
|
it->second.mPeerCbMsg = "Denied Non-Friend";
|
|
|
|
|
|
|
|
it->second.mPeerReqStatusMsg = "Denied Non-Friend";
|
|
|
|
it->second.mPeerReqState = RSDHT_PEERREQ_STOPPED;
|
|
|
|
it->second.mPeerReqTS = now;
|
|
|
|
it->second.mPeerReqMode = 0;
|
|
|
|
//it->second.mPeerProxyId;
|
|
|
|
it->second.mPeerReqTS = now;
|
|
|
|
|
|
|
|
it->second.mPeerCbMsg = "Denied Non-Friend";
|
|
|
|
|
|
|
|
it->second.mPeerConnectMsg = "Denied Non-Friend";
|
|
|
|
it->second.mPeerConnectState = RSDHT_PEERCONN_DISCONNECTED;
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
//return NOT_FRIEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* are a friend */
|
|
|
|
|
|
|
|
if (dpd->mPeerConnectState == RSDHT_PEERCONN_CONNECTED)
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::checkConnectionAllowed() ERROR Peer Already Connected, DENIED";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
// STATUS UPDATE DONE IN ACTION.
|
|
|
|
//it->second.mPeerStatusMsg = "2nd Connection Attempt!";
|
|
|
|
//it->second.mPeerUpdateTS = now;
|
|
|
|
return 0;
|
|
|
|
//return ALREADY_CONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
//return CONNECTION_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* So initiateConnection has to call out to other bits of RS.
|
|
|
|
* critical information is:
|
|
|
|
* Peer RsId, Peer Address, Connect Mode (includes Proxy/OrNot).
|
2011-07-13 18:23:40 -04:00
|
|
|
|
|
|
|
*
|
|
|
|
* What do we need: ACTIVE / PASSIVE / UNSPEC
|
|
|
|
* + Min Delay Time,
|
2011-07-09 20:39:15 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
void p3BitDht::ConnectCalloutTCPAttempt(const std::string &peerId, struct sockaddr_in raddr)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-07-14 10:56:33 -04:00
|
|
|
struct sockaddr_in proxyaddr;
|
|
|
|
struct sockaddr_in srcaddr;
|
|
|
|
|
|
|
|
sockaddr_clear(&proxyaddr);
|
|
|
|
sockaddr_clear(&srcaddr);
|
|
|
|
|
|
|
|
uint32_t source = RS_CB_DHT;
|
|
|
|
uint32_t connectFlags = RS_CB_FLAG_ORDER_UNSPEC | RS_CB_FLAG_MODE_TCP;
|
|
|
|
uint32_t delay = 0;
|
|
|
|
uint32_t bandwidth = 0;
|
|
|
|
|
|
|
|
mConnCb->peerConnectRequest(peerId, raddr, proxyaddr, srcaddr, source, connectFlags, delay, bandwidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void p3BitDht::ConnectCalloutDirectOrProxy(const std::string &peerId, struct sockaddr_in raddr, uint32_t connectFlags, uint32_t delay)
|
|
|
|
{
|
|
|
|
struct sockaddr_in proxyaddr;
|
|
|
|
struct sockaddr_in srcaddr;
|
|
|
|
|
|
|
|
sockaddr_clear(&proxyaddr);
|
|
|
|
sockaddr_clear(&srcaddr);
|
|
|
|
|
|
|
|
uint32_t source = RS_CB_DHT;
|
|
|
|
uint32_t bandwidth = 0;
|
|
|
|
|
|
|
|
mConnCb->peerConnectRequest(peerId, raddr, proxyaddr, srcaddr, source, connectFlags, delay, bandwidth);
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
2011-07-14 10:56:33 -04:00
|
|
|
|
|
|
|
void p3BitDht::ConnectCalloutRelay(const std::string &peerId,
|
|
|
|
struct sockaddr_in srcaddr, struct sockaddr_in proxyaddr, struct sockaddr_in destaddr,
|
|
|
|
uint32_t connectFlags, uint32_t bandwidth)
|
|
|
|
{
|
|
|
|
uint32_t source = RS_CB_DHT;
|
|
|
|
uint32_t delay = 0;
|
|
|
|
|
|
|
|
mConnCb->peerConnectRequest(peerId, destaddr, proxyaddr, srcaddr, source, connectFlags, delay, bandwidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//virtual void 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) = 0;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
void p3BitDht::initiateConnection(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
|
|
|
uint32_t mode, uint32_t loc, uint32_t delayOrBandwidth)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::initiateConnection()";
|
2011-07-16 09:01:08 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t srcId: ";
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "\t proxyId: ";
|
|
|
|
bdStdPrintId(std::cerr, proxyId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "\t destId: ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
std::cerr << "\t Mode: " << mode << " loc: " << loc;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t DelayOrBandwidth: " << delayOrBandwidth;
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
bdId peerConnectId;
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
uint32_t connectFlags = 0;
|
2011-07-17 19:41:30 -04:00
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
uint32_t delay = 0;
|
|
|
|
uint32_t bandwidth = 0;
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
/* determine who the actual destination is.
|
|
|
|
* as we always specify the remote address, this is all we need.
|
|
|
|
*/
|
|
|
|
if (loc == BD_PROXY_CONNECTION_START_POINT)
|
|
|
|
{
|
|
|
|
peerConnectId = *destId;
|
2011-07-14 10:56:33 -04:00
|
|
|
/* lowest is active */
|
|
|
|
if (srcId < destId)
|
|
|
|
{
|
|
|
|
connectFlags |= RS_CB_FLAG_ORDER_ACTIVE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connectFlags |= RS_CB_FLAG_ORDER_PASSIVE;
|
|
|
|
}
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else if (loc == BD_PROXY_CONNECTION_END_POINT)
|
|
|
|
{
|
|
|
|
peerConnectId = *srcId;
|
2011-07-14 10:56:33 -04:00
|
|
|
/* lowest is active (we are now dest - in this case) */
|
|
|
|
if (destId < srcId)
|
|
|
|
{
|
|
|
|
connectFlags |= RS_CB_FLAG_ORDER_ACTIVE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connectFlags |= RS_CB_FLAG_ORDER_PASSIVE;
|
|
|
|
}
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::initiateConnection() ERROR, NOT either START or END";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
/* ERROR */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::initiateConnection() Connecting to ";
|
|
|
|
bdStdPrintId(std::cerr, &peerConnectId);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
uint32_t touConnectMode = 0;
|
|
|
|
std::string rsId;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&(peerConnectId.id), RSDHT_PEERTYPE_FRIEND);
|
|
|
|
/* grab a socket */
|
|
|
|
if (!dpd)
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::initiateConnection() ERROR Peer not found";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dpd->mPeerConnectState != RSDHT_PEERCONN_DISCONNECTED)
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::initiateConnection() ERROR Peer is not Disconnected";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rsId = dpd->mRsId;
|
|
|
|
|
|
|
|
/* start the connection */
|
|
|
|
/* These Socket Modes must match the TOU Stack - or it breaks. */
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case BITDHT_CONNECT_MODE_DIRECT:
|
|
|
|
touConnectMode = RSDHT_TOU_MODE_DIRECT;
|
2011-07-14 10:56:33 -04:00
|
|
|
connectFlags |= RS_CB_FLAG_MODE_UDP_DIRECT;
|
2011-07-17 19:41:30 -04:00
|
|
|
delay = delayOrBandwidth;
|
2011-07-09 20:39:15 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_CONNECT_MODE_PROXY:
|
|
|
|
{
|
|
|
|
bool useProxyPort = dpd->mConnectLogic.getProxyPortChoice();
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::initiateConnection() Peer Mode Proxy... deciding which port to use.";
|
|
|
|
std::cerr << " UseProxyPort? " << useProxyPort;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
delay = delayOrBandwidth;
|
2011-07-09 20:39:15 -04:00
|
|
|
if (useProxyPort)
|
|
|
|
{
|
|
|
|
touConnectMode = RSDHT_TOU_MODE_PROXY;
|
2011-07-14 10:56:33 -04:00
|
|
|
connectFlags |= RS_CB_FLAG_MODE_UDP_PROXY;
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
touConnectMode = RSDHT_TOU_MODE_DIRECT;
|
2011-07-14 10:56:33 -04:00
|
|
|
connectFlags |= RS_CB_FLAG_MODE_UDP_DIRECT;
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_CONNECT_MODE_RELAY:
|
|
|
|
touConnectMode = RSDHT_TOU_MODE_RELAY;
|
2011-07-16 09:01:08 -04:00
|
|
|
connectFlags |= RS_CB_FLAG_MODE_UDP_RELAY;
|
2011-07-17 19:41:30 -04:00
|
|
|
bandwidth = delayOrBandwidth;
|
2011-07-09 20:39:15 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dpd->mPeerConnectProxyId = *proxyId;
|
|
|
|
dpd->mPeerConnectPeerId = peerConnectId;
|
|
|
|
|
|
|
|
|
|
|
|
/* store results in Status */
|
|
|
|
dpd->mPeerConnectMsg = "UDP started";
|
|
|
|
dpd->mPeerConnectState = RSDHT_PEERCONN_UDP_STARTED;
|
|
|
|
dpd->mPeerConnectUdpTS = time(NULL);
|
|
|
|
dpd->mPeerConnectMode = mode;
|
|
|
|
dpd->mPeerConnectPoint = loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finally we call out to start the connection (Outside of Mutex) */
|
2011-07-14 10:56:33 -04:00
|
|
|
|
|
|
|
if ((mode == BITDHT_CONNECT_MODE_DIRECT) || (mode == BITDHT_CONNECT_MODE_PROXY))
|
|
|
|
{
|
|
|
|
ConnectCalloutDirectOrProxy(rsId, peerConnectId.addr, connectFlags, delay);
|
|
|
|
}
|
|
|
|
else if (mode == BITDHT_CONNECT_MODE_RELAY)
|
|
|
|
{
|
|
|
|
if (loc == BD_PROXY_CONNECTION_START_POINT)
|
|
|
|
{
|
|
|
|
ConnectCalloutRelay(rsId, srcId->addr, proxyId->addr, destId->addr, connectFlags, bandwidth);
|
|
|
|
}
|
|
|
|
else // END_POINT
|
|
|
|
{
|
|
|
|
/* reverse order connection call */
|
|
|
|
ConnectCalloutRelay(rsId, destId->addr, proxyId->addr, srcId->addr, connectFlags, bandwidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
int p3BitDht::installRelayConnection(const bdId *srcId, const bdId *destId, uint32_t &bandwidth)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
/* work out if either srcId or DestId is a friend */
|
|
|
|
int relayClass = UDP_RELAY_CLASS_GENERAL;
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::ostringstream str;
|
|
|
|
bdStdPrintNodeId(str, &(srcId->id));
|
|
|
|
std::string strId1 = str.str();
|
|
|
|
|
|
|
|
str.clear();
|
|
|
|
bdStdPrintNodeId(str, &(destId->id));
|
|
|
|
std::string strId2 = str.str();
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* grab a socket */
|
|
|
|
DhtPeerDetails *dpd_src = findInternalDhtPeer_locked(&(srcId->id), RSDHT_PEERTYPE_ANY);
|
|
|
|
DhtPeerDetails *dpd_dest = findInternalDhtPeer_locked(&(destId->id), RSDHT_PEERTYPE_ANY);
|
|
|
|
|
|
|
|
if ((dpd_src) && (dpd_src->mPeerType == RSDHT_PEERTYPE_FRIEND))
|
|
|
|
{
|
|
|
|
relayClass = UDP_RELAY_CLASS_FRIENDS;
|
|
|
|
}
|
|
|
|
else if ((dpd_dest) && (dpd_dest->mPeerType == RSDHT_PEERTYPE_FRIEND))
|
|
|
|
{
|
|
|
|
relayClass = UDP_RELAY_CLASS_FRIENDS;
|
|
|
|
}
|
|
|
|
else if ((dpd_src) && (dpd_src->mPeerType == RSDHT_PEERTYPE_FOF))
|
|
|
|
{
|
|
|
|
relayClass = UDP_RELAY_CLASS_FOF;
|
|
|
|
}
|
|
|
|
else if ((dpd_dest) && (dpd_dest->mPeerType == RSDHT_PEERTYPE_FOF))
|
|
|
|
{
|
|
|
|
relayClass = UDP_RELAY_CLASS_FOF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relayClass = UDP_RELAY_CLASS_GENERAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* will install the Relay Here... so that we reserve the Relay Space for later. */
|
|
|
|
UdpRelayAddrSet relayAddrs(&(srcId->addr), &(destId->addr));
|
2011-07-17 19:41:30 -04:00
|
|
|
if (mRelay->addUdpRelay(&relayAddrs, relayClass, bandwidth))
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::installRelayConnection() Successfully added Relay, Connection OKAY";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
// CONNECT_OKAY.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::installRelayConnection() Failed to install Relay, Connection DENIED";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
//return CONNECT_MODE_OVERLOADED;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::removeRelayConnection(const bdId *srcId, const bdId *destId)
|
|
|
|
{
|
|
|
|
//RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
|
|
|
|
UdpRelayAddrSet relayAddrs(&(srcId->addr), &(destId->addr));
|
|
|
|
if (mRelay->removeUdpRelay(&relayAddrs))
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::removeRelayConnection() Successfully removed Relay";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::removeRelayConnection() ERROR Failed to remove Relay";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************** UDP Connections *****************************/
|
|
|
|
|
|
|
|
|
|
|
|
void p3BitDht::monitorConnections()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
std::map<bdNodeId, DhtPeerDetails>::iterator it;
|
|
|
|
|
|
|
|
for(it = mPeers.begin(); it != mPeers.end(); it++)
|
|
|
|
{
|
|
|
|
/* ignore ones which aren't friends */
|
|
|
|
if (it->second.mPeerType != RSDHT_PEERTYPE_FRIEND)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it->second.mPeerConnectState == RSDHT_PEERCONN_UDP_STARTED)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::monitorConnections() Connection in progress to: ";
|
|
|
|
|
|
|
|
bdStdPrintNodeId(std::cerr, &(it->second.mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
if (now - it->second.mPeerConnectUdpTS > PEERNET_CONNECT_TIMEOUT)
|
|
|
|
{
|
2011-07-17 19:41:30 -04:00
|
|
|
/* This CAN happen ;( */
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-17 19:41:30 -04:00
|
|
|
std::cerr << "p3BitDht::monitorConnections() WARNING InProgress Connection Failed: ";
|
2011-07-09 20:39:15 -04:00
|
|
|
bdStdPrintNodeId(std::cerr, &(it->second.mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
UdpConnectionFailed_locked(&(it->second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void p3BitDht::Feedback_Connected(std::string pid)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
DhtPeerDetails *dpd = findInternalRsPeer_locked(pid);
|
|
|
|
|
|
|
|
if (!dpd)
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "p3BitDht::Feedback_Connected() ERROR peer not found: " << pid;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sanity checking */
|
|
|
|
if (dpd->mPeerConnectState != RSDHT_PEERCONN_UDP_STARTED)
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "p3BitDht::Feedback_Connected() ERROR not in UDP_STARTED mode for: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::monitorConnections() InProgress Connection Now Active: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* switch state! */
|
|
|
|
dpd->mPeerConnectState = RSDHT_PEERCONN_CONNECTED;
|
|
|
|
dpd->mPeerConnectTS = time(NULL);
|
|
|
|
|
|
|
|
dpd->mConnectLogic.updateCb(CSB_UPDATE_CONNECTED);
|
|
|
|
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << "Connected in " << dpd->mPeerConnectTS - dpd->mPeerConnectUdpTS;
|
|
|
|
msg << " secs";
|
|
|
|
dpd->mPeerConnectMsg = msg.str();
|
|
|
|
|
|
|
|
// Remove the Connection Request.
|
|
|
|
if (dpd->mPeerReqState == RSDHT_PEERREQ_RUNNING)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::monitorConnections() Request Active, Stopping Request";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
|
|
|
ca.mType = PEERNET_ACTION_TYPE_KILLREQ;
|
|
|
|
ca.mMode = dpd->mPeerConnectMode;
|
|
|
|
//ca.mProxyId = *proxyId;
|
|
|
|
//ca.mSrcId = *srcId;
|
|
|
|
ca.mDestId = dpd->mPeerConnectPeerId;
|
|
|
|
//ca.mPoint = point;
|
|
|
|
ca.mAnswer = BITDHT_CONNECT_ERROR_NONE;
|
|
|
|
|
|
|
|
mActions.push_back(ca);
|
|
|
|
|
|
|
|
// What the Action should do...
|
|
|
|
//struct sockaddr_in tmpaddr;
|
|
|
|
//bdsockaddr_clear(&tmpaddr);
|
|
|
|
//int start = 0;
|
|
|
|
//mUdpBitDht->ConnectionRequest(&tmpaddr, &(it->second.mPeerConnectPeerId.id), it->second.mPeerConnectMode, start);
|
|
|
|
}
|
|
|
|
// only an error if we initiated the connection.
|
|
|
|
else if (dpd->mPeerConnectPoint == BD_PROXY_CONNECTION_START_POINT)
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::monitorConnections() ERROR Request not active, can't stop";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-17 12:24:45 -04:00
|
|
|
ReleaseProxyExclusiveMode_locked(dpd, true);
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void p3BitDht::Feedback_ConnectionFailed(std::string pid)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
DhtPeerDetails *dpd = findInternalRsPeer_locked(pid);
|
|
|
|
|
|
|
|
if (!dpd)
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "p3BitDht::Feedback_Connected() ERROR peer not found: " << pid;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-15 12:05:37 -04:00
|
|
|
std::cerr << "p3BitDht::Feedback_ConnectionFailed() UDP Connection Failed: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-15 12:05:37 -04:00
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
return UdpConnectionFailed_locked(dpd);
|
|
|
|
}
|
2011-07-15 12:05:37 -04:00
|
|
|
|
|
|
|
void p3BitDht::Feedback_ConnectionClosed(std::string pid)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********** LOCKED MUTEX ***************/
|
|
|
|
DhtPeerDetails *dpd = findInternalRsPeer_locked(pid);
|
|
|
|
|
|
|
|
if (!dpd)
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
std::cerr << "p3BitDht::Feedback_Connected() ERROR peer not found: " << pid;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-15 12:05:37 -04:00
|
|
|
std::cerr << "p3BitDht::Feedback_ConnectionClosed() Active Connection Closed: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-15 12:05:37 -04:00
|
|
|
|
|
|
|
return UdpConnectionFailed_locked(dpd);
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
void p3BitDht::UdpConnectionFailed_locked(DhtPeerDetails *dpd)
|
|
|
|
{
|
2011-07-15 12:05:37 -04:00
|
|
|
if (dpd->mPeerConnectState == RSDHT_PEERCONN_UDP_STARTED)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-15 12:05:37 -04:00
|
|
|
std::cerr << "p3BitDht::UdpConnectionFailed_locked() UDP Connection Failed: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-15 12:05:37 -04:00
|
|
|
|
2011-07-17 19:41:30 -04:00
|
|
|
/* shut it down */
|
|
|
|
|
|
|
|
/* ONLY need to update ConnectLogic - if it was our Attempt Running */
|
|
|
|
if (dpd->mPeerReqState == RSDHT_PEERREQ_RUNNING)
|
|
|
|
{
|
|
|
|
dpd->mConnectLogic.updateCb(CSB_UPDATE_FAILED_ATTEMPT);
|
|
|
|
}
|
2011-07-15 12:05:37 -04:00
|
|
|
dpd->mPeerConnectState = RSDHT_PEERCONN_DISCONNECTED;
|
|
|
|
dpd->mPeerConnectMsg = "UDP Failed";
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-15 12:05:37 -04:00
|
|
|
std::cerr << "p3BitDht::UdpConnectionFailed_locked() Active Connection Closed: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-15 12:05:37 -04:00
|
|
|
|
|
|
|
dpd->mConnectLogic.updateCb(CSB_UPDATE_DISCONNECTED);
|
|
|
|
|
|
|
|
dpd->mPeerConnectState = RSDHT_PEERCONN_DISCONNECTED;
|
|
|
|
dpd->mPeerConnectClosedTS = time(NULL);
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << "Closed, Alive for: " << dpd->mPeerConnectClosedTS - dpd->mPeerConnectTS;
|
|
|
|
msg << " secs";
|
|
|
|
dpd->mPeerConnectMsg = msg.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
if (dpd->mPeerReqState == RSDHT_PEERREQ_RUNNING)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-27 09:56:35 -04:00
|
|
|
std::cerr << "p3BitDht::UdpConnectionFailed_locked() ";
|
|
|
|
std::cerr << "Request Active (Paused)... Killing for next Attempt";
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
/* Push Back PeerAction */
|
|
|
|
PeerAction ca;
|
2011-07-15 12:05:37 -04:00
|
|
|
ca.mType = PEERNET_ACTION_TYPE_KILLREQ;
|
2011-07-09 20:39:15 -04:00
|
|
|
ca.mMode = dpd->mPeerConnectMode;
|
|
|
|
//ca.mProxyId = *proxyId;
|
|
|
|
//ca.mSrcId = *srcId;
|
|
|
|
ca.mDestId = dpd->mPeerConnectPeerId;
|
|
|
|
//ca.mPoint = point;
|
|
|
|
ca.mAnswer = BITDHT_CONNECT_ERROR_NONE;
|
|
|
|
|
|
|
|
mActions.push_back(ca);
|
|
|
|
}
|
|
|
|
// only an error if we initiated the connection.
|
|
|
|
else if (dpd->mPeerConnectPoint == BD_PROXY_CONNECTION_START_POINT)
|
|
|
|
{
|
2011-07-27 09:56:35 -04:00
|
|
|
std::cerr << "p3BitDht::UdpConnectionFailed_locked() ";
|
|
|
|
std::cerr << "ERROR Request not active, can't stop";
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-17 12:24:45 -04:00
|
|
|
ReleaseProxyExclusiveMode_locked(dpd, true);
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-17 12:24:45 -04:00
|
|
|
void p3BitDht::ReleaseProxyExclusiveMode_locked(DhtPeerDetails *dpd, bool addrChgLikely)
|
2011-07-17 09:48:37 -04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_BITDHT_COMMON
|
2011-07-27 09:56:35 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "p3BitDht::ReleaseProxyExclusiveMode_locked()";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
2011-07-27 09:56:35 -04:00
|
|
|
|
|
|
|
/* translate id into string for exclusive mode */
|
|
|
|
std::ostringstream idstr;
|
|
|
|
bdStdPrintNodeId(idstr, &(dpd->mDhtId.id));
|
|
|
|
std::string pid = idstr.str();
|
|
|
|
|
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
if (dpd->mExclusiveProxyLock)
|
|
|
|
{
|
2011-07-27 09:56:35 -04:00
|
|
|
if (mProxyStunner->releaseExclusiveMode(pid, addrChgLikely))
|
2011-07-17 09:48:37 -04:00
|
|
|
{
|
|
|
|
dpd->mExclusiveProxyLock = false;
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-27 09:56:35 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "p3BitDht::ReleaseProxyExclusiveMode_locked() Lock released by Connection to peer: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dpd->mExclusiveProxyLock = false;
|
|
|
|
|
|
|
|
std::cerr << "p3BitDht::ReleaseProxyExclusiveMode_locked() ERROR ProxyStunner is not Locked";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_BITDHT_COMMON
|
2011-07-27 09:56:35 -04:00
|
|
|
#endif
|
2011-07-17 09:48:37 -04:00
|
|
|
std::cerr << "p3BitDht::ReleaseProxyExclusiveMode_locked() Don't Have a Lock";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
void p3BitDht::ConnectionFeedback(std::string pid, int mode)
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3BitDht::ConnectionFeedback() peer: " << pid;
|
|
|
|
std::cerr << " mode: " << mode;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-13 18:23:40 -04:00
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case NETMGR_DHT_FEEDBACK_CONNECTED:
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3BitDht::ConnectionFeedback() HAVE CONNECTED (tcp?/udp) to peer: " << pid;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-13 18:23:40 -04:00
|
|
|
|
|
|
|
Feedback_Connected(pid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NETMGR_DHT_FEEDBACK_CONN_FAILED:
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3BitDht::ConnectionFeedback() UDP CONNECTION FAILED to peer: " << pid;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-13 18:23:40 -04:00
|
|
|
Feedback_ConnectionFailed(pid);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NETMGR_DHT_FEEDBACK_CONN_CLOSED:
|
2011-07-18 20:06:09 -04:00
|
|
|
#ifdef DEBUG_PEERNET
|
2011-07-13 18:23:40 -04:00
|
|
|
std::cerr << "p3BitDht::ConnectionFeedback() CONNECTION (tcp?/udp) CLOSED to peer: " << pid;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 20:06:09 -04:00
|
|
|
#endif
|
2011-07-13 18:23:40 -04:00
|
|
|
Feedback_ConnectionClosed(pid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
|