2011-07-09 20:39:15 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/dht: p3bitdht.h
|
|
|
|
*
|
|
|
|
* BitDht interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2009-2010 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 "dht/p3bitdht.h"
|
|
|
|
|
|
|
|
#include "bitdht/bdstddht.h"
|
|
|
|
|
|
|
|
#include "tcponudp/udprelay.h"
|
|
|
|
#include "tcponudp/udpstunner.h"
|
|
|
|
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
/***
|
|
|
|
*
|
|
|
|
* #define DEBUG_BITDHT 1
|
|
|
|
* #define DEBUG_BITDHT_TRANSLATE 1
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
/******************************************************************************************
|
|
|
|
********************************* Existing Interface *************************************
|
|
|
|
******************************************************************************************/
|
|
|
|
/* pqiNetAssistConnect - external interface functions */
|
|
|
|
/* add / remove peers */
|
|
|
|
/*****
|
|
|
|
* At the moment, findPeer, dropPeer are the only way that peer info enters the dht.
|
|
|
|
* This will obviously change, and we will have a list of Friends and FoF,
|
|
|
|
* but for now we need to expect that this function will add unknown pids.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define USE_OLD_DHT_INTERFACE 1
|
|
|
|
|
2011-07-18 20:06:09 -04:00
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
bool p3BitDht::findPeer(std::string pid)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_BITDHT
|
|
|
|
std::cerr << "p3BitDht::findPeer(" << pid << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-11-22 08:24:42 -05:00
|
|
|
bdNodeId nid;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
{
|
2011-11-22 08:24:42 -05:00
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalRsPeer_locked(pid);
|
2011-07-09 20:39:15 -04:00
|
|
|
if (!dpd)
|
|
|
|
{
|
2011-11-22 08:24:42 -05:00
|
|
|
dpd = addInternalPeer_locked(pid, RSDHT_PEERTYPE_FRIEND);
|
|
|
|
if (!dpd)
|
|
|
|
{
|
|
|
|
/* ERROR */
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2011-11-22 08:24:42 -05:00
|
|
|
std::cerr << "p3BitDht::findPeer() ERROR installing InternalPeer";
|
|
|
|
std::cerr << std::endl;
|
2011-07-10 15:50:12 -04:00
|
|
|
#endif
|
2011-11-22 08:24:42 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* new entry... what do we need to set? */
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_SEARCHING;
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2011-11-22 08:24:42 -05:00
|
|
|
std::cerr << "p3BitDht::findPeer() Installed new DhtPeer with pid => NodeId: ";
|
2011-07-10 15:50:12 -04:00
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-22 08:24:42 -05:00
|
|
|
/* old entry */
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2011-11-22 08:24:42 -05:00
|
|
|
std::cerr << "p3BitDht::findPeer() Reactivating DhtPeer with pid => NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
2011-07-10 15:50:12 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-11-22 08:24:42 -05:00
|
|
|
|
|
|
|
if (dpd->mDhtState != RSDHT_PEERDHT_NOT_ACTIVE)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_BITDHT
|
|
|
|
std::cerr << "p3BitDht::findPeer() WARNING DhtState is Already Active!";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(dpd->mDhtId.id));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* flag as searching */
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_SEARCHING;
|
|
|
|
#ifdef DEBUG_BITDHT
|
|
|
|
std::cerr << "p3BitDht::findPeer() Marking Old Peer as SEARCHING";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
}
|
2011-11-22 08:24:42 -05:00
|
|
|
|
|
|
|
nid = dpd->mDhtId.id;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_BITDHT
|
2011-11-22 08:24:42 -05:00
|
|
|
std::cerr << "p3BitDht::findPeer() calling AddFindNode() with pid => NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &nid);
|
|
|
|
std::cerr << std::endl;
|
2011-07-09 20:39:15 -04:00
|
|
|
#endif
|
|
|
|
|
2011-11-22 08:24:42 -05:00
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
/* add in peer */
|
2011-07-10 15:50:12 -04:00
|
|
|
mUdpBitDht->addFindNode(&nid, BITDHT_QFLAGS_DO_IDLE | BITDHT_QFLAGS_UPDATES);
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3BitDht::dropPeer(std::string pid)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_BITDHT
|
|
|
|
std::cerr << "p3BitDht::dropPeer(" << pid << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2011-11-22 08:24:42 -05:00
|
|
|
bdNodeId nid;
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
{
|
2011-11-22 08:24:42 -05:00
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalRsPeer_locked(pid);
|
|
|
|
if (!dpd)
|
|
|
|
{
|
2012-01-20 12:50:19 -05:00
|
|
|
#ifdef DEBUG_BITDHT
|
2011-11-22 08:24:42 -05:00
|
|
|
std::cerr << "p3BitDht::dropPeer(" << pid << ") HACK TO INCLUDE FRIEND AS NON-ACTIVE PEER";
|
|
|
|
std::cerr << std::endl;
|
2012-01-20 12:50:19 -05:00
|
|
|
#endif
|
2011-11-22 08:24:42 -05:00
|
|
|
|
|
|
|
//addFriend(pid);
|
|
|
|
dpd = addInternalPeer_locked(pid, RSDHT_PEERTYPE_FOF);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flag as searching */
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_NOT_ACTIVE;
|
|
|
|
|
|
|
|
nid = dpd->mDhtId.id;
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2011-11-22 08:24:42 -05:00
|
|
|
std::cerr << "p3BitDht::dropPeer() calling removeFindNode() with pid => NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &nid);
|
|
|
|
std::cerr << std::endl;
|
2011-07-09 20:39:15 -04:00
|
|
|
#endif
|
2011-11-22 08:24:42 -05:00
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
/* remove in peer */
|
|
|
|
mUdpBitDht->removeFindNode(&nid);
|
|
|
|
|
|
|
|
/* not removing from translation */
|
|
|
|
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
********************************* Basic Peer Details *************************************
|
|
|
|
******************************************************************************************/
|
|
|
|
|
2012-02-17 05:03:38 -05:00
|
|
|
int p3BitDht::addBadPeer(const struct sockaddr_in &addr, uint32_t /*reason*/, uint32_t /*flags*/, uint32_t /*age*/)
|
2011-11-24 19:58:01 -05:00
|
|
|
{
|
|
|
|
//mUdpBitDht->updateKnownPeer(&id, 0, bdflags);
|
2011-12-08 15:15:08 -05:00
|
|
|
|
|
|
|
if (mDhtStunner)
|
|
|
|
{
|
|
|
|
mDhtStunner->dropStunPeer(addr);
|
|
|
|
}
|
|
|
|
if (mProxyStunner)
|
|
|
|
{
|
|
|
|
mProxyStunner->dropStunPeer(addr);
|
|
|
|
}
|
|
|
|
|
2011-11-24 19:58:01 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-22 08:24:42 -05:00
|
|
|
int p3BitDht::addKnownPeer(const std::string &pid, const struct sockaddr_in &addr, uint32_t flags)
|
|
|
|
{
|
|
|
|
|
|
|
|
int p3type = 0;
|
|
|
|
int bdflags = 0;
|
|
|
|
bdId id;
|
|
|
|
bool isOwnId = false;
|
|
|
|
|
|
|
|
switch(flags & NETASSIST_KNOWN_PEER_TYPE_MASK)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_WHITELIST:
|
|
|
|
p3type = RSDHT_PEERTYPE_OTHER;
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_WHITELIST;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_FOF:
|
|
|
|
p3type = RSDHT_PEERTYPE_FOF;
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_FOF;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_FRIEND:
|
|
|
|
p3type = RSDHT_PEERTYPE_FRIEND;
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_FRIEND;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_RELAY:
|
|
|
|
p3type = RSDHT_PEERTYPE_OTHER;
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_RELAY_SERVER;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_SELF:
|
|
|
|
p3type = RSDHT_PEERTYPE_OTHER;
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_SELF;
|
|
|
|
isOwnId = true;
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & NETASSIST_KNOWN_PEER_ONLINE)
|
|
|
|
{
|
|
|
|
bdflags |= BD_FRIEND_ENTRY_ONLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isOwnId)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
DhtPeerDetails *dpd = addInternalPeer_locked(pid, p3type);
|
|
|
|
|
|
|
|
|
|
|
|
if (bdflags & BD_FRIEND_ENTRY_ONLINE)
|
|
|
|
{
|
|
|
|
/* can we update the address? */
|
|
|
|
//dpd->mDhtId.addr = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
id.id = dpd->mDhtId.id;
|
|
|
|
id.addr = addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// shouldn't use own id without mutex - but it is static!
|
|
|
|
id.id = mOwnDhtId;
|
|
|
|
id.addr = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUdpBitDht->updateKnownPeer(&id, 0, bdflags);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-01-13 20:54:17 -05:00
|
|
|
|
|
|
|
/* Total duplicate of above function - can't be helped, should merge somehow */
|
|
|
|
int p3BitDht::addKnownNode(const bdId *id, uint32_t flags)
|
|
|
|
{
|
|
|
|
int bdflags = 0;
|
|
|
|
|
|
|
|
switch(flags & NETASSIST_KNOWN_PEER_TYPE_MASK)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_WHITELIST:
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_WHITELIST;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_FOF:
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_FOF;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_FRIEND:
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_FRIEND;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_RELAY:
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_RELAY_SERVER;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NETASSIST_KNOWN_PEER_SELF:
|
|
|
|
bdflags = BITDHT_PEER_STATUS_DHT_SELF;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & NETASSIST_KNOWN_PEER_ONLINE)
|
|
|
|
{
|
|
|
|
bdflags |= BD_FRIEND_ENTRY_ONLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUdpBitDht->updateKnownPeer(id, 0, bdflags);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-22 08:24:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
2011-07-09 20:39:15 -04:00
|
|
|
int p3BitDht::addFriend(const std::string pid)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
return (NULL != addInternalPeer_locked(pid, RSDHT_PEERTYPE_FRIEND));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::addFriendOfFriend(const std::string pid)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
return (NULL != addInternalPeer_locked(pid, RSDHT_PEERTYPE_FOF));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::addOther(const std::string pid)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
return (NULL != addInternalPeer_locked(pid, RSDHT_PEERTYPE_OTHER));
|
|
|
|
}
|
2011-11-22 08:24:42 -05:00
|
|
|
#endif
|
2011-07-09 20:39:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::removePeer(const std::string pid)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
return removeInternalPeer_locked(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
********************************* Basic Peer Details *************************************
|
|
|
|
******************************************************************************************/
|
|
|
|
|
2012-02-17 05:03:38 -05:00
|
|
|
DhtPeerDetails *p3BitDht::addInternalPeer_locked(const std::string pid, uint32_t type)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
/* create the data structure */
|
|
|
|
if (!havePeerTranslation_locked(pid))
|
|
|
|
{
|
|
|
|
storeTranslation_locked(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
bdNodeId id;
|
|
|
|
if (!lookupNodeId_locked(pid, &id))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&id, RSDHT_PEERTYPE_ANY);
|
|
|
|
if (!dpd)
|
|
|
|
{
|
|
|
|
DhtPeerDetails newdpd;
|
|
|
|
mPeers[id] = newdpd;
|
|
|
|
dpd = findInternalDhtPeer_locked(&id, RSDHT_PEERTYPE_ANY);
|
2011-07-18 20:06:09 -04:00
|
|
|
|
|
|
|
dpd->mDhtId.id = id;
|
|
|
|
dpd->mRsId = pid;
|
|
|
|
dpd->mDhtState = RSDHT_PEERDHT_NOT_ACTIVE;
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* what do we need to reset? */
|
|
|
|
dpd->mPeerType = type;
|
|
|
|
|
|
|
|
return dpd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::removeInternalPeer_locked(const std::string pid)
|
|
|
|
{
|
|
|
|
bdNodeId id;
|
|
|
|
if (!lookupNodeId_locked(pid, &id))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<bdNodeId, DhtPeerDetails>::iterator it = mPeers.find(id);
|
|
|
|
if (it == mPeers.end())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPeers.erase(it);
|
|
|
|
|
|
|
|
// remove the translation?
|
|
|
|
removeTranslation_locked(pid);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* indexed by bdNodeId, as this is the more used call interface */
|
2012-02-17 05:03:38 -05:00
|
|
|
DhtPeerDetails *p3BitDht::findInternalDhtPeer_locked(const bdNodeId *id, uint32_t type)
|
2011-07-09 20:39:15 -04:00
|
|
|
{
|
|
|
|
std::map<bdNodeId, DhtPeerDetails>::iterator it = mPeers.find(*id);
|
|
|
|
if (it == mPeers.end())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (type)
|
|
|
|
{
|
|
|
|
if (it->second.mPeerType != type)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* interface to get with alt id */
|
|
|
|
DhtPeerDetails *p3BitDht::findInternalRsPeer_locked(const std::string &pid)
|
|
|
|
{
|
|
|
|
/* create the data structure */
|
|
|
|
if (!havePeerTranslation_locked(pid))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bdNodeId id;
|
|
|
|
if (!lookupNodeId_locked(pid, &id))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DhtPeerDetails *dpd = findInternalDhtPeer_locked(&id,RSDHT_PEERTYPE_ANY);
|
|
|
|
|
|
|
|
return dpd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
|
|
|
*************************** Fundamental Node Translation *********************************
|
|
|
|
******************************************************************************************/
|
|
|
|
|
|
|
|
bool p3BitDht::havePeerTranslation_locked(const std::string &pid)
|
|
|
|
{
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::havePeerTranslation_locked() for : " << pid;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::map<std::string, bdNodeId>::iterator it;
|
|
|
|
it = mTransToNodeId.find(pid);
|
|
|
|
if (it == mTransToNodeId.end())
|
|
|
|
{
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::havePeerTranslation_locked() failed Missing translation";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::havePeerTranslation_locked() Found NodeId: ";
|
2011-07-10 14:19:46 -04:00
|
|
|
bdStdPrintNodeId(std::cerr, &(it->second));
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::lookupNodeId_locked(const std::string pid, bdNodeId *id)
|
|
|
|
{
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::lookupNodeId_locked() for : " << pid;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::map<std::string, bdNodeId>::iterator it;
|
|
|
|
it = mTransToNodeId.find(pid);
|
|
|
|
if (it == mTransToNodeId.end())
|
|
|
|
{
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::lookupNodeId_locked() failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*id = it->second;
|
|
|
|
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::lookupNodeId_locked() Found NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3BitDht::lookupRsId_locked(const bdNodeId *id, std::string &pid)
|
|
|
|
{
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::lookupRsId_locked() for : ";
|
|
|
|
bdStdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::map<bdNodeId, std::string>::iterator nit;
|
|
|
|
nit = mTransToRsId.find(*id);
|
|
|
|
if (nit == mTransToRsId.end())
|
|
|
|
{
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::lookupRsId_locked() failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pid = nit->second;
|
|
|
|
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::lookupRsId_locked() Found Matching RsId: " << pid;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::storeTranslation_locked(const std::string pid)
|
|
|
|
{
|
2012-01-20 12:50:19 -05:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::storeTranslation_locked(" << pid << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bdNodeId nid;
|
|
|
|
calculateNodeId(pid, &nid);
|
|
|
|
|
2012-01-20 12:50:19 -05:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::storeTranslation_locked() Converts to NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(nid));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mTransToNodeId[pid] = nid;
|
|
|
|
mTransToRsId[nid] = pid;
|
|
|
|
|
2012-01-20 12:50:19 -05:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::storeTranslation_locked() Success";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::removeTranslation_locked(const std::string pid)
|
|
|
|
{
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::removeTranslation_locked(" << pid << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::map<std::string, bdNodeId>::iterator it = mTransToNodeId.find(pid);
|
|
|
|
it = mTransToNodeId.find(pid);
|
|
|
|
if (it == mTransToNodeId.end())
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::removeTranslation_locked() ERROR MISSING TransToNodeId";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
/* missing */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bdNodeId nid = it->second;
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::removeTranslation_locked() Found Translation: NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &(nid));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
std::map<bdNodeId, std::string>::iterator nit;
|
|
|
|
nit = mTransToRsId.find(nid);
|
|
|
|
if (nit == mTransToRsId.end())
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::removeTranslation_locked() ERROR MISSING TransToRsId";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
/* inconsistent!!! */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTransToNodeId.erase(it);
|
|
|
|
mTransToRsId.erase(nit);
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::removeTranslation_locked() SUCCESS";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Adding a little bit of fixed text...
|
|
|
|
* This allows us to ensure that only compatible peers will find each other
|
|
|
|
*/
|
|
|
|
|
|
|
|
const uint8_t RS_DHT_VERSION_LEN = 17;
|
|
|
|
const uint8_t rs_dht_version_data[RS_DHT_VERSION_LEN] = "RS_VERSION_0.5.1";
|
|
|
|
|
|
|
|
/******************** Conversion Functions **************************/
|
|
|
|
int p3BitDht::calculateNodeId(const std::string pid, bdNodeId *id)
|
|
|
|
{
|
|
|
|
/* generate node id from pid */
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << "p3BitDht::calculateNodeId() " << pid;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* use a hash to make it impossible to reverse */
|
|
|
|
|
|
|
|
uint8_t sha_hash[SHA_DIGEST_LENGTH];
|
|
|
|
memset(sha_hash,0,SHA_DIGEST_LENGTH*sizeof(uint8_t)) ;
|
|
|
|
SHA_CTX *sha_ctx = new SHA_CTX;
|
|
|
|
SHA1_Init(sha_ctx);
|
|
|
|
|
|
|
|
SHA1_Update(sha_ctx, rs_dht_version_data, RS_DHT_VERSION_LEN);
|
|
|
|
SHA1_Update(sha_ctx, pid.c_str(), pid.length());
|
|
|
|
SHA1_Final(sha_hash, sha_ctx);
|
|
|
|
|
|
|
|
for(int i = 0; i < SHA_DIGEST_LENGTH && (i < BITDHT_KEY_LEN); i++)
|
|
|
|
{
|
|
|
|
id->data[i] = sha_hash[i];
|
|
|
|
}
|
|
|
|
delete sha_ctx;
|
|
|
|
|
2011-07-10 15:50:12 -04:00
|
|
|
#ifdef DEBUG_BITDHT_TRANSLATE
|
2011-07-09 20:39:15 -04:00
|
|
|
std::cerr << " => ";
|
|
|
|
bdStdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-07-18 20:06:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
/******************** Conversion Functions **************************/
|
|
|
|
|
|
|
|
DhtPeerDetails::DhtPeerDetails()
|
|
|
|
{
|
|
|
|
mDhtState = RSDHT_PEERDHT_NOT_ACTIVE;
|
|
|
|
|
|
|
|
mDhtState = RSDHT_PEERDHT_SEARCHING;
|
|
|
|
mDhtUpdateTS = time(NULL);
|
|
|
|
|
|
|
|
mPeerReqStatusMsg = "Just Added";
|
|
|
|
mPeerReqState = RSDHT_PEERREQ_STOPPED;
|
|
|
|
mPeerReqMode = 0;
|
|
|
|
//mPeerReqProxyId;
|
|
|
|
mPeerReqTS = time(NULL);
|
|
|
|
|
|
|
|
mExclusiveProxyLock = false;
|
|
|
|
|
|
|
|
mPeerCbMsg = "No CB Yet";
|
|
|
|
mPeerCbMode = 0;
|
|
|
|
mPeerCbPoint = 0;
|
|
|
|
//mPeerCbProxyId = 0;
|
|
|
|
//mPeerCbDestId = 0;
|
|
|
|
mPeerCbTS = 0;
|
|
|
|
|
|
|
|
mPeerConnectState = RSDHT_PEERCONN_DISCONNECTED;
|
|
|
|
mPeerConnectMsg = "Disconnected";
|
|
|
|
mPeerConnectMode = 0;
|
|
|
|
//dpd->mPeerConnectProxyId;
|
|
|
|
mPeerConnectPoint = 0;
|
|
|
|
|
|
|
|
mPeerConnectUdpTS = 0;
|
|
|
|
mPeerConnectTS = 0;
|
|
|
|
mPeerConnectClosedTS = 0;
|
|
|
|
|
|
|
|
bdsockaddr_clear(&(mPeerConnectAddr));
|
|
|
|
}
|
|
|
|
|