From 11cc3fce0400f976f53bd7d11eb8ca0a4134091a Mon Sep 17 00:00:00 2001 From: drbob Date: Wed, 6 Jul 2011 13:04:50 +0000 Subject: [PATCH] Added External DHT Interface to display Connection Information. * added retroshare/rsdht.h * extended p3bitdht to provide RsDht interface. (p3bitdht_interface.cc) * added UdpRelay to networking stack. * started expansion of p3bitdht to handle connections. * added header to rsthreads. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4399 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dht/p3bitdht.cc | 19 ++ libretroshare/src/dht/p3bitdht.h | 47 +++- libretroshare/src/dht/p3bitdht_interface.cc | 262 ++++++++++++++++++++ libretroshare/src/libretroshare.pro | 7 +- libretroshare/src/retroshare/rsdht.h | 136 ++++++++++ libretroshare/src/rsserver/rsinit.cc | 15 +- libretroshare/src/util/rsthreads.h | 1 + 7 files changed, 479 insertions(+), 8 deletions(-) create mode 100644 libretroshare/src/dht/p3bitdht_interface.cc create mode 100644 libretroshare/src/retroshare/rsdht.h diff --git a/libretroshare/src/dht/p3bitdht.cc b/libretroshare/src/dht/p3bitdht.cc index c1894b4ab..b3d043d88 100644 --- a/libretroshare/src/dht/p3bitdht.cc +++ b/libretroshare/src/dht/p3bitdht.cc @@ -29,6 +29,9 @@ #include "bitdht/bdstddht.h" #include "pqi/p3connmgr.h" +#include "tcponudp/udprelay.h" +#include "tcponudp/udpstunner.h" + #include @@ -37,6 +40,9 @@ * */ +/**** EXTERNAL INTERFACE DHT POINTER *****/ +RsDht *rsDht = NULL; + class p3BdCallback: public BitDhtCallback { public: @@ -74,6 +80,10 @@ virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdI p3BitDht::p3BitDht(std::string id, pqiConnectCb *cb, UdpStack *udpstack, std::string bootstrapfile) :pqiNetAssistConnect(id, cb), dhtMtx("p3BitDht") { + mDhtStunner = NULL; + mProxyStunner = NULL; + mRelay = NULL; + std::string dhtVersion = "RS51"; // should come from elsewhere! bdNodeId ownId; @@ -122,6 +132,15 @@ p3BitDht::~p3BitDht() delete mUdpBitDht; } + +void p3BitDht::setupConnectBits(UdpStunner *dhtStunner, UdpStunner *proxyStunner, UdpRelayReceiver *relay) +{ + mDhtStunner = dhtStunner; + mProxyStunner = proxyStunner; + mRelay = relay; +} + + void p3BitDht::start() { #ifdef DEBUG_BITDHT diff --git a/libretroshare/src/dht/p3bitdht.h b/libretroshare/src/dht/p3bitdht.h index 869a7a621..e41d44020 100644 --- a/libretroshare/src/dht/p3bitdht.h +++ b/libretroshare/src/dht/p3bitdht.h @@ -28,6 +28,7 @@ #define MRK_P3_BITDHT_H #include "pqi/pqiassist.h" +#include "retroshare/rsdht.h" #include #include @@ -39,14 +40,54 @@ #include "udp/udpbitdht.h" #include "bitdht/bdiface.h" -class p3BitDht: public pqiNetAssistConnect + +class DhtPeerDetails +{ + public: + + bdId mDhtId; + std::string mRsId; + +}; + +class UdpRelayReceiver; +class UdpStunner; + + +class p3BitDht: public pqiNetAssistConnect, public RsDht { public: p3BitDht(std::string id, pqiConnectCb *cb, UdpStack *udpstack, std::string bootstrapfile); + virtual ~p3BitDht(); + +/*********************************************************************************************** + ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* +************************************************************************************************/ + +virtual uint32_t getNetState(uint32_t type); +virtual int getDhtPeers(int lvl, std::list &peers); +virtual int getNetPeerList(std::list &peerIds); +virtual int getNetPeerStatus(std::string peerId, RsDhtNetPeer &status); + +virtual int getRelayEnds(std::list &relayEnds); +virtual int getRelayProxies(std::list &relayProxies); + +//virtual int getNetFailedPeer(std::string peerId, PeerStatus &status); + +/*********************************************************************************************** + ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* +************************************************************************************************/ + + + void setupConnectBits(UdpStunner *dhtStunner, UdpStunner *proxyStunner, UdpRelayReceiver *relay); + + + + void start(); /* starts up the bitdht thread */ /* pqiNetAssist - external interface functions */ @@ -94,12 +135,16 @@ int ValueCallback(const bdNodeId *id, std::string key, uint32_t status); int removeTranslation(const std::string pid); UdpBitDht *mUdpBitDht; /* has own mutex, is static except for creation/destruction */ + UdpStunner *mDhtStunner; + UdpStunner *mProxyStunner; + UdpRelayReceiver *mRelay; RsMutex dhtMtx; /* translation maps */ std::map mTransToNodeId; std::map mTransToRsId; + std::map mPeers; }; #endif /* MRK_P3_BITDHT_H */ diff --git a/libretroshare/src/dht/p3bitdht_interface.cc b/libretroshare/src/dht/p3bitdht_interface.cc new file mode 100644 index 000000000..6e63b6861 --- /dev/null +++ b/libretroshare/src/dht/p3bitdht_interface.cc @@ -0,0 +1,262 @@ +/* + * 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" +#include "bitdht/bdstddht.h" + +#include + +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) ********* +************************************************************************************************/ + +uint32_t p3BitDht::getNetState(uint32_t type) +{ + + return 1; +} + +int p3BitDht::getDhtPeers(int lvl, std::list &peers) +{ + /* this function we can actually implement! */ + bdBucket int_peers; + std::list::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 &peerIds) +{ + RsStackMutex stack(dhtMtx); /*********** LOCKED **********/ + std::map::iterator it; + for(it = mPeers.begin(); it != mPeers.end(); it++) + { + peerIds.push_back(it->first); + } + + return 1; +} + +int p3BitDht::getNetPeerStatus(std::string peerId, RsDhtNetPeer &status) +{ + + RsStackMutex stack(dhtMtx); /*********** LOCKED **********/ + + std::map::iterator it; + it = mPeers.find(peerId); + if (it == mPeers.end()) + { + return 0; + } + + convertDhtPeerDetailsToRsDhtNetPeer(status, it->second); + return 1; +} + + +int p3BitDht::getRelayEnds(std::list &relayEnds) +{ + /* no need for mutex as mRelayReceiver is protected */ + if (!mRelay) + { + return 0; + } + + std::list int_relayEnds; + std::list::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 &relayProxies) +{ + /* no need for mutex as mRelayReceiver is protected */ + if (!mRelay) + { + return 0; + } + + std::list int_relayProxies; + std::list::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 + +/*********************************************************************************************** + ********** 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; + bdStdPrintNodeId(out, &(details.mDhtId.id)); + + status.mDhtId = out.str(); + status.mRsId = details.mRsId; + + return; +} + +void convertUdpRelayEndtoRsDhtRelayEnd(RsDhtRelayEnd &end, const UdpRelayEnd &int_end) +{ + std::ostringstream addr; + addr << rs_inet_ntoa(int_end.mLocalAddr.sin_addr) << ":" << ntohs(int_end.mLocalAddr.sin_port); + end.mLocalAddr = addr.str(); + + addr.clear(); + addr << rs_inet_ntoa(int_end.mProxyAddr.sin_addr) << ":" << ntohs(int_end.mProxyAddr.sin_port); + end.mProxyAddr = addr.str(); + + + addr.clear(); + addr << rs_inet_ntoa(int_end.mRemoteAddr.sin_addr) << ":" << ntohs(int_end.mRemoteAddr.sin_port); + end.mRemoteAddr = addr.str(); + + end.mCreateTS = 0; + return; +} + +void convertUdpRelayProxytoRsDhtRelayProxy(RsDhtRelayProxy &proxy, const UdpRelayProxy &int_proxy) +{ + std::ostringstream addr; + addr << rs_inet_ntoa(int_proxy.mAddrs.mSrcAddr.sin_addr) << ":" << ntohs(int_proxy.mAddrs.mSrcAddr.sin_port); + proxy.mSrcAddr = addr.str(); + + addr.clear(); + addr << rs_inet_ntoa(int_proxy.mAddrs.mDestAddr.sin_addr) << ":" << ntohs(int_proxy.mAddrs.mDestAddr.sin_port); + proxy.mDestAddr = addr.str(); + + 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; + +} + + diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 346fbd37d..89b967f4d 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -65,8 +65,8 @@ debug { bitdht { HEADERS += dht/p3bitdht.h -SOURCES += dht/p3bitdht.cc - +SOURCES += dht/p3bitdht.cc \ + dht/p3bitdht_interface.cc HEADERS += tcponudp/udppeer.h \ tcponudp/bio_tou.h \ @@ -139,7 +139,8 @@ PUBLIC_HEADERS = retroshare/rsblogs.h \ retroshare/rsrank.h \ retroshare/rsstatus.h \ retroshare/rsturtle.h \ - retroshare/rstypes.h + retroshare/rstypes.h \ + retroshare/rsdht.h HEADERS += plugins/pluginmanager.h \ plugins/dlfcn_win32.h \ diff --git a/libretroshare/src/retroshare/rsdht.h b/libretroshare/src/retroshare/rsdht.h new file mode 100644 index 000000000..c0c282163 --- /dev/null +++ b/libretroshare/src/retroshare/rsdht.h @@ -0,0 +1,136 @@ +#ifndef RETROSHARE_DHT_GUI_INTERFACE_H +#define RETROSHARE_DHT_GUI_INTERFACE_H + +/* + * libretroshare/src/rsiface: rsdht.h + * + * RetroShare C++ Interface. + * + * Copyright 2011-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 +#include +#include + +/* The Main Interface Class - for information about your Peers */ +class RsDht; +extern RsDht *rsDht; + + +//std::ostream &operator<<(std::ostream &out, const RsPhotoShowDetails &detail); +//std::ostream &operator<<(std::ostream &out, const RsPhotoDetails &detail); + + +#define RS_DHT_NETSTART_NETWORKMODE 0x0001 +#define RS_DHT_NETSTART_NATTYPE 0x0002 +#define RS_DHT_NETSTART_NATHOLE 0x0003 +#define RS_DHT_NETSTART_CONNECTMODES 0x0004 +#define RS_DHT_NETSTART_NETSTATE 0x0005 + +class RsDhtPeer +{ + public: + RsDhtPeer(); + + int mBucket; + std::string mDhtId; + std::string mAddr; + time_t mLastSendTime; + time_t mLastRecvTime; + time_t mFoundTime; + uint32_t mPeerFlags; + uint32_t mExtraFlags; +}; + +class RsDhtNetPeer +{ + public: + RsDhtNetPeer(); + + std::string mDhtId; + std::string mRsId; + +}; + +class RsDhtRelayEnd +{ + public: + + RsDhtRelayEnd(); + std::string mLocalAddr; + std::string mProxyAddr; + std::string mRemoteAddr; + time_t mCreateTS; +}; + +class RsDhtRelayProxy +{ + public: + RsDhtRelayProxy(); + + std::string mSrcAddr; + std::string mDestAddr; + + double mBandwidth; + int mRelayClass; + time_t mLastTS; + time_t mCreateTS; + + //uint32_t mDataSize; + //time_t mLastBandwidthTS; + +}; + + +class RsDht +{ + public: + + RsDht() { return; } +virtual ~RsDht() { return; } + +virtual uint32_t getNetState(uint32_t type) = 0; +virtual int getDhtPeers(int lvl, std::list &peers) = 0; +virtual int getNetPeerList(std::list &peerIds) = 0; +virtual int getNetPeerStatus(std::string peerId, RsDhtNetPeer &status) = 0; + +virtual int getRelayEnds(std::list &relayEnds) = 0; +virtual int getRelayProxies(std::list &relayProxies) = 0; + +//virtual int getNetFailedPeer(std::string peerId, PeerStatus &status); + +#if 0 +virtual std::string getPeerStatusString(); +virtual std::string getPeerAddressString(); +virtual std::string getDhtStatusString(); + +virtual int get_dht_queries(std::map &queries); +virtual int get_query_status(std::string id, bdQuerySummary &query); + +virtual int get_peer_status(std::string peerId, PeerStatus &status); + +virtual int get_net_failedpeers(std::list &peerIds); +virtual int get_failedpeer_status(std::string peerId, PeerStatus &status); +#endif + +}; + +#endif diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 499b5b74f..ad73b892f 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1900,12 +1900,14 @@ int RsServer::StartupRetroShare() // NEXT BITDHT. p3BitDht *mBitDht = new p3BitDht(ownId, mConnMgr, mUdpStack, bootstrapfile); + /* install external Pointer for Interface */ + rsDht = mBitDht; // NEXT THE RELAY (NEED to keep a reference for installing RELAYS) - //UdpRelayReceiver *mRelayRecver = new UdpRelayReceiver(mUdpStack); - //udpReceivers[2] = mRelayRecver; /* RELAY Connections (DHT Port) */ - //udpTypes[2] = TOU_RECEIVER_TYPE_UDPRELAY; - //mUdpStack->addReceiver(udpReceivers[2]); + UdpRelayReceiver *mRelayRecver = new UdpRelayReceiver(mUdpStack); + udpReceivers[2] = mRelayRecver; /* RELAY Connections (DHT Port) */ + udpTypes[2] = TOU_RECEIVER_TYPE_UDPRELAY; + mUdpStack->addReceiver(udpReceivers[2]); // LAST ON THIS STACK IS STANDARD DIRECT TOU udpReceivers[0] = new UdpPeerReceiver(mUdpStack); /* standard DIRECT Connections (DHT Port) */ @@ -1938,6 +1940,11 @@ int RsServer::StartupRetroShare() // REAL INITIALISATION - WITH THREE MODES - FOR LATER. //tou_init((void **) udpReceivers, udpTypes, 3); + //mBitDht->setupConnectBits(mDhtStunner, mProxyStunner, mRelayRecver); + mBitDht->setupConnectBits(mDhtStunner, NULL, mRelayRecver); +#else + /* install NULL Pointer for rsDht Interface */ + rsDht = NULL; #endif diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index f3edd84b7..9bea693cf 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -33,6 +33,7 @@ #include #include +#include /* RsIface Thread Wrappers */