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"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
for(it = int_peers.entries.begin(); it != int_peers.entries.end(); it++)
|
|
|
|
{
|
|
|
|
RsDhtPeer peer;
|
|
|
|
|
|
|
|
convertBdPeerToRsDhtPeer(peer, *it);
|
|
|
|
peer.mBucket = lvl;
|
|
|
|
|
|
|
|
peers.push_back(peer);
|
|
|
|
}
|
|
|
|
return (int_peers.entries.size() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::getNetPeerList(std::list<std::string> &peerIds)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
2011-07-09 20:39:15 -04:00
|
|
|
std::map<bdNodeId, DhtPeerDetails>::iterator it;
|
2011-07-06 09:04:50 -04:00
|
|
|
for(it = mPeers.begin(); it != mPeers.end(); it++)
|
|
|
|
{
|
2011-07-09 20:39:15 -04:00
|
|
|
peerIds.push_back(it->second.mRsId);
|
2011-07-06 09:04:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::getNetPeerStatus(std::string peerId, RsDhtNetPeer &status)
|
|
|
|
{
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
for(it = int_relayEnds.begin(); it != int_relayEnds.end(); it++)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
for(it = int_relayProxies.begin(); it != int_relayProxies.end(); it++)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
|
|
|
|
struct sockaddr_in extAddr;
|
|
|
|
uint8_t extStable;
|
|
|
|
|
|
|
|
if (mDhtStunner->externalAddr(extAddr, extStable))
|
|
|
|
{
|
2012-03-17 21:33:25 -04:00
|
|
|
out << " DhtExtAddr: " << rs_inet_ntoa(extAddr.sin_addr);
|
2011-07-18 20:06:09 -04:00
|
|
|
out << ":" << ntohs(extAddr.sin_port);
|
|
|
|
|
|
|
|
if (extStable)
|
|
|
|
{
|
|
|
|
out << " (Stable) ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << " (Unstable) ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << " DhtExtAddr: Unknown ";
|
|
|
|
}
|
|
|
|
if (mProxyStunner->externalAddr(extAddr, extStable))
|
|
|
|
{
|
2012-03-17 21:33:25 -04:00
|
|
|
out << " ProxyExtAddr: " << rs_inet_ntoa(extAddr.sin_addr);
|
2011-07-18 20:06:09 -04:00
|
|
|
out << ":" << ntohs(extAddr.sin_port);
|
|
|
|
|
|
|
|
if (extStable)
|
|
|
|
{
|
|
|
|
out << " (Stable) ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << " (Unstable) ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << " ProxyExtAddr: Unknown ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
bdStdPrintNodeId(out, &(int_peer.mPeerId.id));
|
|
|
|
|
|
|
|
peer.mDhtId = out.str();
|
|
|
|
|
|
|
|
std::ostringstream addr;
|
|
|
|
addr << rs_inet_ntoa(int_peer.mPeerId.addr.sin_addr) << ":" << ntohs(int_peer.mPeerId.addr.sin_port);
|
|
|
|
peer.mAddr = addr.str();
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2011-09-28 12:01:22 -04:00
|
|
|
bdStdPrintId(out, &(details.mDhtId));
|
2011-07-06 09:04:50 -04:00
|
|
|
|
|
|
|
status.mDhtId = out.str();
|
|
|
|
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:
|
|
|
|
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;
|
|
|
|
std::ostringstream out2;
|
|
|
|
bdStdPrintId(out2, &(details.mPeerConnectProxyId));
|
|
|
|
status.mPeerConnectProxyId = out2.str();
|
|
|
|
|
|
|
|
status.mCbPeerMsg = details.mPeerCbMsg;
|
|
|
|
|
2011-07-06 09:04:50 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void convertUdpRelayEndtoRsDhtRelayEnd(RsDhtRelayEnd &end, const UdpRelayEnd &int_end)
|
|
|
|
{
|
2011-07-16 09:01:08 -04:00
|
|
|
{
|
|
|
|
std::ostringstream addr;
|
|
|
|
addr << rs_inet_ntoa(int_end.mLocalAddr.sin_addr) << ":" << ntohs(int_end.mLocalAddr.sin_port);
|
|
|
|
end.mLocalAddr = addr.str();
|
|
|
|
}
|
2011-07-06 09:04:50 -04:00
|
|
|
|
2011-07-16 09:01:08 -04:00
|
|
|
{
|
|
|
|
std::ostringstream addr;
|
|
|
|
addr << rs_inet_ntoa(int_end.mProxyAddr.sin_addr) << ":" << ntohs(int_end.mProxyAddr.sin_port);
|
|
|
|
end.mProxyAddr = addr.str();
|
|
|
|
}
|
2011-07-06 09:04:50 -04:00
|
|
|
|
2011-07-16 09:01:08 -04:00
|
|
|
{
|
|
|
|
std::ostringstream addr;
|
|
|
|
addr << rs_inet_ntoa(int_end.mRemoteAddr.sin_addr) << ":" << ntohs(int_end.mRemoteAddr.sin_port);
|
|
|
|
end.mRemoteAddr = addr.str();
|
|
|
|
}
|
2011-07-06 09:04:50 -04:00
|
|
|
|
|
|
|
end.mCreateTS = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void convertUdpRelayProxytoRsDhtRelayProxy(RsDhtRelayProxy &proxy, const UdpRelayProxy &int_proxy)
|
|
|
|
{
|
2011-07-16 09:01:08 -04:00
|
|
|
{
|
|
|
|
std::ostringstream addr;
|
|
|
|
addr << rs_inet_ntoa(int_proxy.mAddrs.mSrcAddr.sin_addr) << ":" << ntohs(int_proxy.mAddrs.mSrcAddr.sin_port);
|
|
|
|
proxy.mSrcAddr = addr.str();
|
|
|
|
}
|
2011-07-06 09:04:50 -04:00
|
|
|
|
2011-07-16 09:01:08 -04:00
|
|
|
{
|
|
|
|
std::ostringstream addr;
|
|
|
|
addr << rs_inet_ntoa(int_proxy.mAddrs.mDestAddr.sin_addr) << ":" << ntohs(int_proxy.mAddrs.mDestAddr.sin_port);
|
|
|
|
proxy.mDestAddr = addr.str();
|
|
|
|
}
|
2011-07-06 09:04:50 -04:00
|
|
|
|
|
|
|
proxy.mBandwidth = int_proxy.mBandwidth;
|
|
|
|
proxy.mRelayClass = int_proxy.mRelayClass;
|
|
|
|
proxy.mLastTS = int_proxy.mLastTS;
|
|
|
|
proxy.mCreateTS = 0;
|
|
|
|
|
|
|
|
//proxy.mDataSize = int_proxy.mDataSize;
|
|
|
|
//proxy.mLastBandwidthTS = int_proxy.mLastBandwidthTS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|