2011-07-06 09:04:50 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/dht: p3bitdht.h
|
|
|
|
*
|
|
|
|
* BitDht interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2009-2011 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "util/rsnet.h"
|
|
|
|
#include "dht/p3bitdht.h"
|
|
|
|
|
|
|
|
#include "tcponudp/udprelay.h"
|
2011-07-18 20:06:09 -04:00
|
|
|
#include "tcponudp/udpstunner.h"
|
2011-07-06 09:04:50 -04:00
|
|
|
#include "bitdht/bdstddht.h"
|
|
|
|
|
|
|
|
|
|
|
|
void convertBdPeerToRsDhtPeer(RsDhtPeer &peer, const bdPeer &int_peer);
|
|
|
|
void convertDhtPeerDetailsToRsDhtNetPeer(RsDhtNetPeer &status, const DhtPeerDetails &details);
|
|
|
|
void convertUdpRelayEndtoRsDhtRelayEnd(RsDhtRelayEnd &end, const UdpRelayEnd &int_end);
|
|
|
|
void convertUdpRelayProxytoRsDhtRelayProxy(RsDhtRelayProxy &proxy, const UdpRelayProxy &int_proxy);
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************************
|
|
|
|
********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) *********
|
|
|
|
************************************************************************************************/
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
uint32_t p3BitDht::getNetState(uint32_t /*type*/)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::getDhtPeers(int lvl, std::list<RsDhtPeer> &peers)
|
|
|
|
{
|
|
|
|
/* this function we can actually implement! */
|
|
|
|
bdBucket int_peers;
|
|
|
|
std::list<bdPeer>::iterator it;
|
|
|
|
mUdpBitDht->getDhtBucket(lvl, int_peers);
|
|
|
|
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = int_peers.entries.begin(); it != int_peers.entries.end(); ++it)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
|
|
|
RsDhtPeer peer;
|
|
|
|
|
|
|
|
convertBdPeerToRsDhtPeer(peer, *it);
|
|
|
|
peer.mBucket = lvl;
|
|
|
|
|
|
|
|
peers.push_back(peer);
|
|
|
|
}
|
|
|
|
return (int_peers.entries.size() > 0);
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
int p3BitDht::getNetPeerList(std::list<RsPeerId> &peerIds)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
2011-07-09 20:39:15 -04:00
|
|
|
std::map<bdNodeId, DhtPeerDetails>::iterator it;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = mPeers.begin(); it != mPeers.end(); ++it)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
2011-07-09 20:39:15 -04:00
|
|
|
peerIds.push_back(it->second.mRsId);
|
2011-07-06 09:04:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
int p3BitDht::getNetPeerStatus(const RsPeerId& peerId, RsDhtNetPeer &status)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
DhtPeerDetails *dpd = findInternalRsPeer_locked(peerId);
|
|
|
|
if (!dpd)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
convertDhtPeerDetailsToRsDhtNetPeer(status, *dpd);
|
2011-07-06 09:04:50 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::getRelayEnds(std::list<RsDhtRelayEnd> &relayEnds)
|
|
|
|
{
|
|
|
|
/* no need for mutex as mRelayReceiver is protected */
|
|
|
|
if (!mRelay)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<UdpRelayEnd> int_relayEnds;
|
|
|
|
std::list<UdpRelayEnd>::iterator it;
|
|
|
|
mRelay->getRelayEnds(int_relayEnds);
|
|
|
|
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = int_relayEnds.begin(); it != int_relayEnds.end(); ++it)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
|
|
|
RsDhtRelayEnd end;
|
|
|
|
convertUdpRelayEndtoRsDhtRelayEnd(end, *it);
|
|
|
|
|
|
|
|
relayEnds.push_back(end);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::getRelayProxies(std::list<RsDhtRelayProxy> &relayProxies)
|
|
|
|
{
|
|
|
|
/* no need for mutex as mRelayReceiver is protected */
|
|
|
|
if (!mRelay)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<UdpRelayProxy> int_relayProxies;
|
|
|
|
std::list<UdpRelayProxy>::iterator it;
|
|
|
|
mRelay->getRelayProxies(int_relayProxies);
|
|
|
|
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = int_relayProxies.begin(); it != int_relayProxies.end(); ++it)
|
2011-07-06 09:04:50 -04:00
|
|
|
{
|
|
|
|
RsDhtRelayProxy proxy;
|
|
|
|
convertUdpRelayProxytoRsDhtRelayProxy(proxy, *it);
|
|
|
|
relayProxies.push_back(proxy);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
int p3BitDht::getNetFailedPeer(std::string peerId, PeerStatus &status)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
std::string p3BitDht::getUdpAddressString()
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
std::string out;
|
2016-06-21 08:02:52 -04:00
|
|
|
#ifdef RS_USE_DHT_STUNNER
|
2011-07-18 20:06:09 -04:00
|
|
|
struct sockaddr_in extAddr;
|
|
|
|
uint8_t extStable;
|
2016-06-21 08:02:52 -04:00
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
if (mDhtStunner->externalAddr(extAddr, extStable))
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
rs_sprintf_append(out, " DhtExtAddr: %s:%u", rs_inet_ntoa(extAddr.sin_addr).c_str(), ntohs(extAddr.sin_port));
|
2011-07-18 20:06:09 -04:00
|
|
|
|
|
|
|
if (extStable)
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
out += " (Stable) ";
|
2011-07-18 20:06:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
out += " (Unstable) ";
|
2011-07-18 20:06:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
out += " DhtExtAddr: Unknown ";
|
2011-07-18 20:06:09 -04:00
|
|
|
}
|
|
|
|
if (mProxyStunner->externalAddr(extAddr, extStable))
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
rs_sprintf_append(out, " ProxyExtAddr: %s:%u", rs_inet_ntoa(extAddr.sin_addr).c_str(), ntohs(extAddr.sin_port));
|
2011-07-18 20:06:09 -04:00
|
|
|
|
|
|
|
if (extStable)
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
out += " (Stable) ";
|
2011-07-18 20:06:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
out += " (Unstable) ";
|
2011-07-18 20:06:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-17 20:00:59 -04:00
|
|
|
out += " ProxyExtAddr: Unknown ";
|
2011-07-18 20:06:09 -04:00
|
|
|
}
|
2016-06-21 07:55:23 -04:00
|
|
|
#else // RS_USE_DHT_STUNNER
|
|
|
|
// whitespaces taken from above
|
|
|
|
out = " DhtExtAddr: Unknown ProxyExtAddr: Unknown ";
|
|
|
|
#endif // RS_USE_DHT_STUNNER
|
2012-04-17 20:00:59 -04:00
|
|
|
return out;
|
2011-07-18 20:06:09 -04:00
|
|
|
}
|
|
|
|
|
2012-01-19 11:23:57 -05:00
|
|
|
void p3BitDht::updateDataRates()
|
|
|
|
{
|
|
|
|
uint32_t relayRead = 0;
|
|
|
|
uint32_t relayWrite = 0;
|
|
|
|
uint32_t relayRelay = 0;
|
|
|
|
uint32_t dhtRead = 0;
|
|
|
|
uint32_t dhtWrite = 0;
|
|
|
|
|
|
|
|
mRelay->getDataTransferred(relayRead, relayWrite, relayRelay);
|
|
|
|
mUdpBitDht->getDataTransferred(dhtRead, dhtWrite);
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
float period = now - mLastDataRateUpdate;
|
|
|
|
|
|
|
|
#define RATE_FACTOR (0.75)
|
|
|
|
|
|
|
|
mRelayReadRate *= RATE_FACTOR;
|
|
|
|
mRelayReadRate += (1.0 - RATE_FACTOR) * (relayRead / period);
|
|
|
|
|
|
|
|
mRelayWriteRate *= RATE_FACTOR;
|
|
|
|
mRelayWriteRate += (1.0 - RATE_FACTOR) * (relayWrite / period);
|
|
|
|
|
|
|
|
mRelayRelayRate *= RATE_FACTOR;
|
|
|
|
mRelayRelayRate += (1.0 - RATE_FACTOR) * (relayRelay / period);
|
|
|
|
|
|
|
|
mDhtReadRate *= RATE_FACTOR;
|
|
|
|
mDhtReadRate += (1.0 - RATE_FACTOR) * (dhtRead / period);
|
|
|
|
|
|
|
|
mDhtWriteRate *= RATE_FACTOR;
|
|
|
|
mDhtWriteRate += (1.0 - RATE_FACTOR) * (dhtWrite / period);
|
|
|
|
|
|
|
|
mLastDataRateUpdate = now;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3BitDht::clearDataRates()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
mRelayReadRate = 0;
|
|
|
|
mRelayWriteRate = 0;
|
|
|
|
mRelayRelayRate = 0;
|
|
|
|
mDhtReadRate = 0;
|
|
|
|
mDhtWriteRate = 0;
|
|
|
|
|
|
|
|
mLastDataRateUpdate = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* in kB/s */
|
|
|
|
void p3BitDht::getDhtRates(float &read, float &write)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
read = mDhtReadRate / 1024.0;
|
|
|
|
write = mDhtWriteRate / 1024.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3BitDht::getRelayRates(float &read, float &write, float &relay)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
read = mRelayReadRate / 1024.0;
|
|
|
|
write = mRelayWriteRate / 1024.0;
|
|
|
|
relay = mRelayRelayRate / 1024.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-06 09:04:50 -04:00
|
|
|
/***********************************************************************************************
|
|
|
|
********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) *********
|
|
|
|
************************************************************************************************/
|
|
|
|
|
|
|
|
void convertBdPeerToRsDhtPeer(RsDhtPeer &peer, const bdPeer &int_peer)
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
bdStdPrintNodeId(peer.mDhtId, &(int_peer.mPeerId.id), false);
|
2011-07-06 09:04:50 -04:00
|
|
|
|
2012-04-18 17:20:37 -04:00
|
|
|
rs_sprintf(peer.mAddr, "%s:%u", rs_inet_ntoa(int_peer.mPeerId.addr.sin_addr).c_str(), ntohs(int_peer.mPeerId.addr.sin_port));
|
2011-07-06 09:04:50 -04:00
|
|
|
|
|
|
|
peer.mLastSendTime = int_peer.mLastSendTime;
|
|
|
|
peer.mLastRecvTime = int_peer.mLastRecvTime;
|
|
|
|
peer.mFoundTime = int_peer.mFoundTime;
|
|
|
|
peer.mPeerFlags = int_peer.mPeerFlags;
|
|
|
|
peer.mExtraFlags = int_peer.mExtraFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void convertDhtPeerDetailsToRsDhtNetPeer(RsDhtNetPeer &status, const DhtPeerDetails &details)
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
bdStdPrintId(status.mDhtId, &(details.mDhtId), false);
|
2011-07-06 09:04:50 -04:00
|
|
|
|
|
|
|
status.mRsId = details.mRsId;
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
status.mPeerType = details.mPeerType;
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
status.mDhtState = details.mDhtState;
|
|
|
|
|
|
|
|
status.mConnectState = details.mConnectLogic.connectState();
|
|
|
|
|
|
|
|
status.mPeerReqState = details.mPeerReqState;
|
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
status.mExclusiveProxyLock = details.mExclusiveProxyLock;
|
|
|
|
|
2011-07-10 14:19:46 -04:00
|
|
|
status.mPeerConnectState = details.mPeerConnectState;
|
|
|
|
|
|
|
|
switch(details.mPeerConnectMode)
|
|
|
|
{
|
|
|
|
default:
|
2013-08-22 03:26:13 -04:00
|
|
|
status.mPeerConnectMode = RSDHT_TOU_MODE_NONE;
|
|
|
|
break;
|
2011-07-10 14:19:46 -04:00
|
|
|
case BITDHT_CONNECT_MODE_DIRECT:
|
|
|
|
status.mPeerConnectMode = RSDHT_TOU_MODE_DIRECT;
|
|
|
|
break;
|
|
|
|
case BITDHT_CONNECT_MODE_PROXY:
|
|
|
|
status.mPeerConnectMode = RSDHT_TOU_MODE_PROXY;
|
|
|
|
break;
|
|
|
|
case BITDHT_CONNECT_MODE_RELAY:
|
|
|
|
status.mPeerConnectMode = RSDHT_TOU_MODE_RELAY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//status.mPeerConnectProxyId = details.mPeerConnectProxyId;
|
2012-04-19 16:44:32 -04:00
|
|
|
bdStdPrintId(status.mPeerConnectProxyId, &(details.mPeerConnectProxyId), false);
|
2011-07-10 14:19:46 -04:00
|
|
|
|
|
|
|
status.mCbPeerMsg = details.mPeerCbMsg;
|
|
|
|
|
2011-07-06 09:04:50 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void convertUdpRelayEndtoRsDhtRelayEnd(RsDhtRelayEnd &end, const UdpRelayEnd &int_end)
|
|
|
|
{
|
2012-04-18 17:20:37 -04:00
|
|
|
rs_sprintf(end.mLocalAddr, "%s:%u", rs_inet_ntoa(int_end.mLocalAddr.sin_addr).c_str(), ntohs(int_end.mLocalAddr.sin_port));
|
|
|
|
rs_sprintf(end.mProxyAddr, "%s:%u", rs_inet_ntoa(int_end.mProxyAddr.sin_addr).c_str(), ntohs(int_end.mProxyAddr.sin_port));
|
|
|
|
rs_sprintf(end.mRemoteAddr, "%s:%u", rs_inet_ntoa(int_end.mRemoteAddr.sin_addr).c_str(), ntohs(int_end.mRemoteAddr.sin_port));
|
2011-07-06 09:04:50 -04:00
|
|
|
|
|
|
|
end.mCreateTS = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void convertUdpRelayProxytoRsDhtRelayProxy(RsDhtRelayProxy &proxy, const UdpRelayProxy &int_proxy)
|
|
|
|
{
|
2012-04-18 17:20:37 -04:00
|
|
|
rs_sprintf(proxy.mSrcAddr, "%s:%u", rs_inet_ntoa(int_proxy.mAddrs.mSrcAddr.sin_addr).c_str(), ntohs(int_proxy.mAddrs.mSrcAddr.sin_port));
|
|
|
|
rs_sprintf(proxy.mDestAddr, "%s:%u", rs_inet_ntoa(int_proxy.mAddrs.mDestAddr.sin_addr).c_str(), ntohs(int_proxy.mAddrs.mDestAddr.sin_port));
|
2011-07-06 09:04:50 -04:00
|
|
|
|
2012-04-18 17:20:37 -04:00
|
|
|
proxy.mBandwidth = int_proxy.mBandwidth;
|
|
|
|
proxy.mRelayClass = int_proxy.mRelayClass;
|
|
|
|
proxy.mLastTS = int_proxy.mLastTS;
|
2011-07-06 09:04:50 -04:00
|
|
|
proxy.mCreateTS = 0;
|
|
|
|
|
2012-04-18 17:20:37 -04:00
|
|
|
//proxy.mDataSize = int_proxy.mDataSize;
|
|
|
|
//proxy.mLastBandwidthTS = int_proxy.mLastBandwidthTS;
|
2011-07-06 09:04:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RsDhtPeer::RsDhtPeer()
|
|
|
|
{
|
|
|
|
mBucket = 0;
|
|
|
|
|
|
|
|
//std::string mDhtId;
|
|
|
|
mLastSendTime = 0;
|
|
|
|
mLastRecvTime = 0;
|
|
|
|
mFoundTime = 0;
|
|
|
|
mPeerFlags = 0;
|
|
|
|
mExtraFlags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RsDhtNetPeer::RsDhtNetPeer()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RsDhtRelayEnd::RsDhtRelayEnd()
|
|
|
|
{
|
|
|
|
mCreateTS = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RsDhtRelayProxy::RsDhtRelayProxy()
|
|
|
|
{
|
|
|
|
mBandwidth = 0;
|
|
|
|
mRelayClass = 0;
|
|
|
|
mLastTS = 0;
|
|
|
|
mCreateTS = 0;
|
|
|
|
|
|
|
|
//uint32_t mDataSize;
|
|
|
|
//time_t mLastBandwidthTS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|