2010-07-10 07:48:24 -04:00
|
|
|
/*
|
|
|
|
* bitdht/bdmanager.cc
|
|
|
|
*
|
|
|
|
* BitDHT: An Flexible DHT library.
|
|
|
|
*
|
|
|
|
* Copyright 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 3 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 "bitdht@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*******
|
|
|
|
* Node Manager.
|
|
|
|
******/
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
* 1) Maintains a list of ids to search for.
|
|
|
|
* 2) Sets up initial search for own node.
|
|
|
|
* 3) Checks on status of queries.
|
|
|
|
* 4) Callback on successful searches.
|
|
|
|
*
|
|
|
|
* This is pretty specific to RS requirements.
|
|
|
|
****/
|
|
|
|
|
2010-07-18 12:28:51 -04:00
|
|
|
#include "bitdht/bdiface.h"
|
|
|
|
#include "bitdht/bdstddht.h"
|
|
|
|
#include "bitdht/bdmanager.h"
|
|
|
|
#include "bitdht/bdmsgs.h"
|
|
|
|
#include "bitdht/bencode.h"
|
2011-06-29 06:46:11 -04:00
|
|
|
#include "bitdht/bdquerymgr.h"
|
2011-12-08 15:00:20 -05:00
|
|
|
#include "bitdht/bdfilter.h"
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iomanip>
|
2011-07-20 13:56:49 -04:00
|
|
|
#include <string.h>
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2010-07-18 12:28:51 -04:00
|
|
|
#include "util/bdnet.h"
|
2011-06-29 06:46:11 -04:00
|
|
|
#include "util/bdrandom.h"
|
2012-04-19 16:44:32 -04:00
|
|
|
#include "util/bdstring.h"
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
/***
|
|
|
|
* #define DEBUG_MGR 1
|
|
|
|
* #define DEBUG_MGR_PKT 1
|
|
|
|
***/
|
|
|
|
|
2012-01-08 05:50:33 -05:00
|
|
|
//#define DEBUG_MGR 1
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
//#define LOCAL_NET_FLAG (BITDHT_PEER_STATUS_DHT_APPL)
|
|
|
|
#define LOCAL_NET_FLAG (BITDHT_PEER_STATUS_DHT_ENGINE)
|
|
|
|
// This is eventually what we want.
|
|
|
|
//#define LOCAL_NET_FLAG (BITDHT_PEER_STATUS_DHT_ENGINE_VERSION)
|
|
|
|
|
2011-07-16 08:57:11 -04:00
|
|
|
#define QUERY_UPDATE_PERIOD 8 // under refresh period - so it'll happen at the MAX_REFRESH_PERIOD
|
2011-07-05 20:14:58 -04:00
|
|
|
|
2010-10-17 16:55:32 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string bootfile, bdDhtFunctions *fns)
|
|
|
|
:bdNode(id, dhtVersion, bootfile, fns)
|
|
|
|
{
|
2010-10-17 16:55:32 -04:00
|
|
|
mMode = BITDHT_MGR_STATE_OFF;
|
2010-07-10 07:48:24 -04:00
|
|
|
mFns = fns;
|
2010-10-06 15:01:13 -04:00
|
|
|
mModeTS = 0 ;
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2010-10-17 16:55:32 -04:00
|
|
|
mNetworkSize = 0;
|
|
|
|
mBdNetworkSize = 0;
|
|
|
|
|
2012-12-01 06:51:33 -05:00
|
|
|
std::string bfilter = "edff727f3a49f55c0504ad99d4282f7a26b3f69b59ebc6ca496879c6805a0aa567dffb755f17fdfd44dd24180bf2b61ebfbe68e9a53e79d7893f002140882daf7efbfed66f36eb170064208286040001fbefbbbbef1fa7fdf4a21128d050a208cd3a529a7efdc672c8255130e022b134bc6c77dfbf455d054349c575774d427b";
|
2011-06-29 12:06:33 -04:00
|
|
|
|
|
|
|
mBloomFilter.setFilterBits(bfilter);
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::bdNodeManager() ID: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-10-17 16:55:32 -04:00
|
|
|
int bdNodeManager::stopDht()
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
/* clean up node */
|
|
|
|
shutdownNode();
|
|
|
|
|
|
|
|
/* flag queries as inactive */
|
|
|
|
/* check if exists already */
|
|
|
|
std::map<bdNodeId, bdQueryPeer>::iterator it;
|
|
|
|
for(it = mActivePeers.begin(); it != mActivePeers.end(); it++)
|
|
|
|
{
|
|
|
|
it->second.mStatus = BITDHT_QUERY_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set state flag */
|
|
|
|
mMode = BITDHT_MGR_STATE_OFF;
|
|
|
|
mModeTS = now;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdNodeManager::startDht()
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
/* set startup mode */
|
|
|
|
restartNode();
|
|
|
|
|
|
|
|
mMode = BITDHT_MGR_STATE_STARTUP;
|
|
|
|
mModeTS = now;
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
mStartTS = now;
|
|
|
|
mSearchingDone = false;
|
|
|
|
mSearchTS = now;
|
|
|
|
|
2010-10-17 16:55:32 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STOPPED, STARTING, ACTIVE, FAILED */
|
|
|
|
int bdNodeManager::stateDht()
|
|
|
|
{
|
|
|
|
return mMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t bdNodeManager::statsNetworkSize()
|
|
|
|
{
|
|
|
|
return mNetworkSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* same version as us! */
|
|
|
|
uint32_t bdNodeManager::statsBDVersionSize()
|
|
|
|
{
|
|
|
|
return mBdNetworkSize;
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2011-12-01 12:19:30 -05:00
|
|
|
uint32_t bdNodeManager::setDhtMode(uint32_t dhtFlags)
|
|
|
|
{
|
|
|
|
/* handle options here? */
|
|
|
|
setNodeDhtMode(dhtFlags);
|
|
|
|
|
|
|
|
return dhtFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-21 07:23:56 -04:00
|
|
|
bool bdNodeManager::setAttachMode(bool on)
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
setNodeOptions(BITDHT_OPTIONS_MAINTAIN_UNSTABLE_PORT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setNodeOptions(0);
|
|
|
|
}
|
|
|
|
return on;
|
|
|
|
}
|
|
|
|
|
2011-11-22 08:16:03 -05:00
|
|
|
/* Friend Tracking */
|
2012-02-17 05:03:38 -05:00
|
|
|
void bdNodeManager::addBadPeer(const struct sockaddr_in &/*addr*/, uint32_t /*source*/, uint32_t /*reason*/, uint32_t /*age*/)
|
2011-11-24 19:48:33 -05:00
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::addBadPeer() not implemented yet!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-11-22 08:16:03 -05:00
|
|
|
void bdNodeManager::updateKnownPeer(const bdId *id, uint32_t /* type */, uint32_t flags)
|
|
|
|
{
|
|
|
|
mFriendList.updatePeer(id, flags);
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
void bdNodeManager::addFindNode(bdNodeId *id, uint32_t qflags)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::addFindNode() ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* check if exists already */
|
|
|
|
std::map<bdNodeId, bdQueryPeer>::iterator it;
|
|
|
|
it = mActivePeers.find(*id);
|
|
|
|
if (it != mActivePeers.end())
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::addFindNode() Found existing....";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add to map */
|
|
|
|
bdQueryPeer peer;
|
|
|
|
peer.mId.id = (*id);
|
2010-08-01 14:19:27 -04:00
|
|
|
peer.mStatus = BITDHT_QUERY_READY; //QUERYING;
|
2010-07-10 07:48:24 -04:00
|
|
|
peer.mQFlags = qflags;
|
2010-10-04 17:37:07 -04:00
|
|
|
|
|
|
|
peer.mDhtAddr.sin_addr.s_addr = 0;
|
|
|
|
peer.mDhtAddr.sin_port = 0;
|
|
|
|
|
2011-07-05 20:14:58 -04:00
|
|
|
peer.mCallbackTS = 0;
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
mActivePeers[*id] = peer;
|
|
|
|
#ifdef DEBUG_MGR
|
2010-08-01 14:19:27 -04:00
|
|
|
std::cerr << "bdNodeManager::addFindNode() Added QueryPeer as READY....";
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-08-01 14:19:27 -04:00
|
|
|
//addQuery(id, qflags | BITDHT_QFLAGS_DISGUISE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finds a queued query, and starts it */
|
|
|
|
void bdNodeManager::startQueries()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::startQueries() ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* check if exists already */
|
|
|
|
std::map<bdNodeId, bdQueryPeer>::iterator it;
|
|
|
|
for(it = mActivePeers.begin(); it != mActivePeers.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.mStatus == BITDHT_QUERY_READY)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::startQueries() Found READY Query.";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-09-29 15:59:31 -04:00
|
|
|
it->second.mStatus = BITDHT_QUERY_QUERYING;
|
2010-08-01 14:19:27 -04:00
|
|
|
|
|
|
|
uint32_t qflags = it->second.mQFlags | BITDHT_QFLAGS_DISGUISE;
|
2011-06-29 06:46:11 -04:00
|
|
|
mQueryMgr->addQuery(&(it->first), qflags);
|
2010-10-19 18:03:38 -04:00
|
|
|
|
|
|
|
// add all queries at the same time!
|
|
|
|
//return;
|
2010-08-01 14:19:27 -04:00
|
|
|
}
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void bdNodeManager::removeFindNode(bdNodeId *id)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::removeFindNode() ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
std::map<bdNodeId, bdQueryPeer>::iterator it;
|
|
|
|
it = mActivePeers.find(*id);
|
|
|
|
if (it == mActivePeers.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup any actions */
|
2011-06-29 06:46:11 -04:00
|
|
|
mQueryMgr->clearQuery(&(it->first));
|
2010-07-10 07:48:24 -04:00
|
|
|
//clearPing(&(it->first));
|
|
|
|
|
|
|
|
/* remove from map */
|
|
|
|
mActivePeers.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bdNodeManager::iteration()
|
|
|
|
{
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t modeAge = now - mModeTS;
|
|
|
|
switch(mMode)
|
|
|
|
{
|
2010-10-17 16:55:32 -04:00
|
|
|
case BITDHT_MGR_STATE_OFF:
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): OFF";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
case BITDHT_MGR_STATE_STARTUP:
|
|
|
|
/* 10 seconds startup .... then switch to ACTIVE */
|
2010-08-01 14:19:27 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
if (modeAge > MAX_STARTUP_TIME)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
2010-07-31 14:01:24 -04:00
|
|
|
std::cerr << "bdNodeManager::iteration(): STARTUP -> REFRESH";
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
bdNodeId id;
|
|
|
|
getOwnId(&id);
|
2011-06-29 06:46:11 -04:00
|
|
|
mQueryMgr->addQuery(&id, BITDHT_QFLAGS_DO_IDLE | BITDHT_QFLAGS_DISGUISE);
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2010-08-01 14:19:27 -04:00
|
|
|
mMode = BITDHT_MGR_STATE_FINDSELF;
|
|
|
|
mModeTS = now;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_STATE_FINDSELF:
|
2010-10-20 14:36:25 -04:00
|
|
|
/* 60 seconds further startup .... then switch to ACTIVE
|
|
|
|
* if we reach TRANSITION_OP_SPACE_SIZE before this time, transition immediately...
|
|
|
|
* if, after 60 secs, we haven't reached MIN_OP_SPACE_SIZE, restart....
|
|
|
|
*/
|
|
|
|
|
2011-07-05 19:53:40 -04:00
|
|
|
#define TRANSITION_OP_SPACE_SIZE 50 /* 1 query / sec, should take 12-15 secs */
|
2012-01-11 05:24:37 -05:00
|
|
|
#define MAX_FINDSELF_TIME 30
|
|
|
|
#define MIN_OP_SPACE_SIZE 10
|
2011-07-05 19:53:40 -04:00
|
|
|
// testing parameters.
|
2012-01-11 05:24:37 -05:00
|
|
|
//#define MAX_FINDSELF_TIME 10
|
|
|
|
//#define MIN_OP_SPACE_SIZE 2 // for testing. self + oneother.
|
2010-08-01 14:19:27 -04:00
|
|
|
|
2010-10-17 16:55:32 -04:00
|
|
|
{
|
|
|
|
uint32_t nodeSpaceSize = mNodeSpace.calcSpaceSize();
|
|
|
|
|
2010-08-01 14:19:27 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-10-17 16:55:32 -04:00
|
|
|
std::cerr << "bdNodeManager::iteration() Finding Oneself: ";
|
|
|
|
std::cerr << "NodeSpace Size:" << nodeSpaceSize;
|
2010-08-01 14:19:27 -04:00
|
|
|
std::cerr << std::endl;
|
2011-01-20 17:18:42 -05:00
|
|
|
#endif
|
2010-08-01 14:19:27 -04:00
|
|
|
|
2010-10-20 14:36:25 -04:00
|
|
|
if (nodeSpaceSize > TRANSITION_OP_SPACE_SIZE)
|
2010-10-17 16:55:32 -04:00
|
|
|
{
|
|
|
|
mMode = BITDHT_MGR_STATE_REFRESH;
|
|
|
|
mModeTS = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modeAge > MAX_FINDSELF_TIME)
|
|
|
|
{
|
2011-06-29 06:46:11 -04:00
|
|
|
if (nodeSpaceSize >= MIN_OP_SPACE_SIZE)
|
2010-10-20 14:36:25 -04:00
|
|
|
{
|
|
|
|
mMode = BITDHT_MGR_STATE_REFRESH;
|
|
|
|
mModeTS = now;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mMode = BITDHT_MGR_STATE_FAILED;
|
|
|
|
mModeTS = now;
|
|
|
|
}
|
2010-10-17 16:55:32 -04:00
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
2010-08-01 14:19:27 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_STATE_ACTIVE:
|
2011-07-05 20:14:58 -04:00
|
|
|
if (modeAge >= MAX_REFRESH_TIME)
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
2010-07-31 14:01:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): ACTIVE -> REFRESH";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
mMode = BITDHT_MGR_STATE_REFRESH;
|
|
|
|
mModeTS = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_STATE_REFRESH:
|
|
|
|
{
|
2010-07-31 14:01:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): REFRESH -> ACTIVE";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
/* select random ids, and perform searchs to refresh space */
|
|
|
|
mMode = BITDHT_MGR_STATE_ACTIVE;
|
|
|
|
mModeTS = now;
|
|
|
|
|
2010-08-01 14:19:27 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): Starting Query";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
startQueries();
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): Updating Stores";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
updateStore();
|
|
|
|
|
2011-12-08 15:00:20 -05:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): Cleaning up Filter (should do less frequently)";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mFilterPeers->cleanupFilter();
|
|
|
|
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-11-17 13:10:56 -05:00
|
|
|
std::cerr << "bdNodeManager::iteration(): Do App Search";
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-11-17 13:10:56 -05:00
|
|
|
/* run a random search for ourselves, from own App DHT peer */
|
|
|
|
QueryRandomLocalNet();
|
|
|
|
|
2012-01-11 05:24:37 -05:00
|
|
|
#define SEARCH_MAX_SIZE 10
|
2011-06-29 06:46:11 -04:00
|
|
|
if (mBdNetworkSize < SEARCH_MAX_SIZE)
|
|
|
|
{
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2011-06-29 06:46:11 -04:00
|
|
|
std::cerr << "Local Netsize: " << mBdNetworkSize << " to small...searching";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 19:52:23 -04:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
/* if the network size is very small */
|
|
|
|
SearchForLocalNet();
|
|
|
|
mSearchingDone = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!mSearchingDone)
|
|
|
|
{
|
|
|
|
mSearchingDone = true;
|
|
|
|
mSearchTS = now;
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2011-06-29 06:46:11 -04:00
|
|
|
std::cerr << "Completed LocalNet Search in : " << mSearchTS-mStartTS;
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 19:52:23 -04:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
}
|
2010-11-17 13:10:56 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): REFRESH ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
status(); /* calculates mNetworkSize */
|
|
|
|
|
2011-06-29 12:06:33 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2011-06-29 06:46:11 -04:00
|
|
|
mAccount.printStats(std::cerr);
|
2011-06-29 12:06:33 -04:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
/* Finally, Fail, and restart if we lose all peers */
|
2011-06-29 12:06:33 -04:00
|
|
|
uint32_t nodeSpaceSize = mNodeSpace.calcSpaceSize();
|
|
|
|
if (nodeSpaceSize < MIN_OP_SPACE_SIZE)
|
2011-06-29 06:46:11 -04:00
|
|
|
{
|
2011-06-29 12:06:33 -04:00
|
|
|
std::cerr << "bdNodeManager::iteration(): SpaceSize to Small: " << nodeSpaceSize;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "bdNodeManager::iteration(): REFRESH ==> FAILED";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
mMode = BITDHT_MGR_STATE_FAILED;
|
|
|
|
mModeTS = now;
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_MGR_STATE_QUIET:
|
|
|
|
{
|
2010-10-17 16:55:32 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::iteration(): QUIET";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-10-17 16:55:32 -04:00
|
|
|
default:
|
|
|
|
case BITDHT_MGR_STATE_FAILED:
|
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::iteration(): FAILED ==> STARTUP";
|
|
|
|
std::cerr << std::endl;
|
2010-10-19 18:03:38 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-10-17 16:55:32 -04:00
|
|
|
#endif
|
|
|
|
stopDht();
|
|
|
|
startDht();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2010-10-17 16:55:32 -04:00
|
|
|
if (mMode == BITDHT_MGR_STATE_OFF)
|
|
|
|
{
|
|
|
|
bdNode::iterationOff();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* tick parent */
|
|
|
|
bdNode::iteration();
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
|
2010-11-17 13:10:56 -05:00
|
|
|
/* NB: This is a bit of a hack, the code is duplicated from bdnode & bdquery.
|
|
|
|
* should use fn calls into their functions for good generality
|
|
|
|
*/
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
#define RANDOM_SEARCH_FRAC (0.1)
|
|
|
|
|
|
|
|
int bdNodeManager::QueryRandomLocalNet()
|
2010-11-17 13:10:56 -05:00
|
|
|
{
|
|
|
|
bdId id;
|
|
|
|
bdNodeId targetNodeId;
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
uint32_t withFlag = LOCAL_NET_FLAG;
|
2010-11-17 13:10:56 -05:00
|
|
|
if (mNodeSpace.findRandomPeerWithFlag(id, withFlag))
|
|
|
|
{
|
2011-06-29 06:46:11 -04:00
|
|
|
/* if we've got a very small network size... then ask them about a random peer.
|
|
|
|
* (so we get there 159/158 boxes!
|
|
|
|
*/
|
|
|
|
bool isRandom = false;
|
|
|
|
if ((mBdNetworkSize < SEARCH_MAX_SIZE) || (RANDOM_SEARCH_FRAC > bdRandom::random_f32()))
|
|
|
|
{
|
|
|
|
bdStdRandomNodeId(&targetNodeId);
|
|
|
|
isRandom = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* calculate mid point */
|
|
|
|
mFns->bdRandomMidId(&mOwnId, &(id.id), &targetNodeId);
|
|
|
|
}
|
2010-11-17 13:10:56 -05:00
|
|
|
|
|
|
|
/* do standard find_peer message */
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
mQueryMgr->addWorthyPeerSource(&id); /* Tell BitDHT that we really want to ping their peers */
|
|
|
|
send_query(&id, &targetNodeId);
|
2010-11-17 13:10:56 -05:00
|
|
|
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-11-17 13:10:56 -05:00
|
|
|
std::cerr << "bdNodeManager::QueryRandomLocalNet() Querying : ";
|
|
|
|
mFns->bdPrintId(std::cerr, &id);
|
|
|
|
std::cerr << " searching for : ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &targetNodeId);
|
|
|
|
std::cerr << std::endl;
|
2011-06-29 12:06:33 -04:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
if (isRandom)
|
|
|
|
{
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2011-06-29 06:46:11 -04:00
|
|
|
std::cerr << "bdNodeManager::QueryRandomLocalNet() Search is Random!";
|
|
|
|
std::cerr << std::endl;
|
2011-06-29 12:06:33 -04:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2010-11-17 13:10:56 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-11-17 13:10:56 -05:00
|
|
|
std::cerr << "bdNodeManager::QueryRandomLocalNet() No LocalNet Peer Found";
|
|
|
|
std::cerr << std::endl;
|
2011-06-29 12:06:33 -04:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void bdNodeManager::SearchForLocalNet()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::SearchForLocalNet()";
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 19:52:23 -04:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
/* Check how many "Search Queries" we've got going. */
|
|
|
|
|
|
|
|
/* check queries */
|
|
|
|
std::map<bdNodeId, bdQueryStatus>::iterator it;
|
|
|
|
std::map<bdNodeId, bdQueryStatus> queryStatus;
|
|
|
|
|
|
|
|
|
|
|
|
mQueryMgr->QueryStatus(queryStatus);
|
|
|
|
|
|
|
|
int numSearchQueries = 0;
|
|
|
|
for(it = queryStatus.begin(); it != queryStatus.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->second.mQFlags & BITDHT_QFLAGS_INTERNAL)
|
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::SearchForLocalNet() Existing Internal Search: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
numSearchQueries++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_SEARCH_QUERIES 5
|
|
|
|
|
|
|
|
for(;numSearchQueries < MAX_SEARCH_QUERIES; numSearchQueries++)
|
|
|
|
{
|
|
|
|
/* install a new query */
|
|
|
|
bdNodeId targetNodeId;
|
2011-06-29 12:06:33 -04:00
|
|
|
|
|
|
|
/* get something that filter approves of */
|
|
|
|
bool filterOk = false;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
#define MAX_FILTER_ATTEMPTS 3000
|
|
|
|
|
|
|
|
for(i = 0; (!filterOk) && (i < MAX_FILTER_ATTEMPTS); i++)
|
|
|
|
{
|
|
|
|
bdStdRandomNodeId(&targetNodeId);
|
2012-04-19 16:44:32 -04:00
|
|
|
std::string tststr;
|
|
|
|
bdStdPrintNodeId(tststr, &targetNodeId, false);
|
2011-06-29 12:06:33 -04:00
|
|
|
|
2012-04-19 16:44:32 -04:00
|
|
|
if (mBloomFilter.test(tststr))
|
2011-06-29 12:06:33 -04:00
|
|
|
{
|
|
|
|
filterOk = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filterOk)
|
|
|
|
{
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2011-06-29 12:06:33 -04:00
|
|
|
std::cerr << "bdNodeManager::SearchForLocalNet() " << i << " Attempts to find OkNode: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &targetNodeId);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 19:52:23 -04:00
|
|
|
#endif
|
2011-06-29 12:06:33 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2011-06-29 12:06:33 -04:00
|
|
|
std::cerr << "bdNodeManager::SearchForLocalNet() Failed to Find FilterOk this time: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &targetNodeId);
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 19:52:23 -04:00
|
|
|
#endif
|
2011-06-29 12:06:33 -04:00
|
|
|
}
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
uint32_t qflags = BITDHT_QFLAGS_INTERNAL | BITDHT_QFLAGS_DISGUISE;
|
|
|
|
mQueryMgr->addQuery(&targetNodeId, qflags);
|
|
|
|
|
2011-07-18 19:52:23 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2011-06-29 06:46:11 -04:00
|
|
|
std::cerr << "bdNodeManager::SearchForLocalNet() Adding New Internal Search: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(targetNodeId));
|
|
|
|
std::cerr << std::endl;
|
2011-07-18 19:52:23 -04:00
|
|
|
#endif
|
2010-11-17 13:10:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
int bdNodeManager::status()
|
|
|
|
{
|
|
|
|
/* do status of bdNode */
|
2012-12-01 06:51:33 -05:00
|
|
|
//#ifdef DEBUG_MGR
|
2010-07-10 07:48:24 -04:00
|
|
|
printState();
|
2012-12-01 06:51:33 -05:00
|
|
|
//#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
checkStatus();
|
2011-11-24 19:48:33 -05:00
|
|
|
checkBadPeerStatus();
|
2010-10-17 16:55:32 -04:00
|
|
|
|
|
|
|
/* update the network numbers */
|
|
|
|
mNetworkSize = mNodeSpace.calcNetworkSize();
|
|
|
|
mBdNetworkSize = mNodeSpace.calcNetworkSizeWithFlag(
|
2011-06-29 06:46:11 -04:00
|
|
|
LOCAL_NET_FLAG);
|
2010-10-31 09:58:54 -04:00
|
|
|
|
2012-12-01 06:51:33 -05:00
|
|
|
//#ifdef DEBUG_MGR
|
2010-10-31 09:58:54 -04:00
|
|
|
std::cerr << "BitDHT NetworkSize: " << mNetworkSize << std::endl;
|
|
|
|
std::cerr << "BitDHT App NetworkSize: " << mBdNetworkSize << std::endl;
|
2012-12-01 06:51:33 -05:00
|
|
|
//#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bdNodeManager::checkStatus()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::checkStatus()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check queries */
|
|
|
|
std::map<bdNodeId, bdQueryStatus>::iterator it;
|
|
|
|
std::map<bdNodeId, bdQueryStatus> queryStatus;
|
|
|
|
|
|
|
|
|
2011-07-05 20:14:58 -04:00
|
|
|
mQueryMgr->QueryStatus(queryStatus);
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
for(it = queryStatus.begin(); it != queryStatus.end(); it++)
|
|
|
|
{
|
|
|
|
bool doPing = false;
|
|
|
|
bool doRemove = false;
|
|
|
|
bool doCallback = false;
|
2010-10-04 17:37:07 -04:00
|
|
|
bool doSaveAddress = false;
|
2010-07-10 07:48:24 -04:00
|
|
|
uint32_t callbackStatus = 0;
|
|
|
|
|
|
|
|
switch(it->second.mStatus)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case BITDHT_QUERY_QUERYING:
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
2010-10-07 15:25:40 -04:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Query in Progress id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_QUERY_FAILURE:
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
2010-10-07 15:25:40 -04:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Query Failed: id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
|
|
|
// BAD.
|
|
|
|
doRemove = true;
|
|
|
|
doCallback = true;
|
|
|
|
callbackStatus = BITDHT_MGR_QUERY_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_QUERY_FOUND_CLOSEST:
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
2010-10-07 15:25:40 -04:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Found Closest: id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
2010-10-07 15:25:40 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
doRemove = true;
|
|
|
|
doCallback = true;
|
|
|
|
callbackStatus = BITDHT_MGR_QUERY_PEER_OFFLINE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_QUERY_PEER_UNREACHABLE:
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
2010-10-07 15:25:40 -04:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() the Peer Online but Unreachable: id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
2010-10-07 15:25:40 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
doRemove = true;
|
|
|
|
doCallback = true;
|
|
|
|
callbackStatus = BITDHT_MGR_QUERY_PEER_UNREACHABLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_QUERY_SUCCESS:
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
2010-10-07 15:25:40 -04:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Found Query: id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
|
|
|
//foundId =
|
|
|
|
doRemove = true;
|
|
|
|
doCallback = true;
|
2010-10-04 17:37:07 -04:00
|
|
|
doSaveAddress = true;
|
2010-07-10 07:48:24 -04:00
|
|
|
callbackStatus = BITDHT_MGR_QUERY_PEER_ONLINE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove done queries */
|
|
|
|
if (doRemove)
|
|
|
|
{
|
|
|
|
if (it->second.mQFlags & BITDHT_QFLAGS_DO_IDLE)
|
|
|
|
{
|
|
|
|
doRemove = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doRemove)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::checkStatus() Removing query: id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
mQueryMgr->clearQuery(&(it->first));
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIND in activePeers */
|
|
|
|
std::map<bdNodeId, bdQueryPeer>::iterator pit;
|
|
|
|
pit = mActivePeers.find(it->first);
|
|
|
|
|
|
|
|
if (pit == mActivePeers.end())
|
|
|
|
{
|
|
|
|
/* only internal! - disable Callback / Ping */
|
|
|
|
doPing = false;
|
|
|
|
doCallback = false;
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::checkStatus() Internal: no cb for id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2010-10-07 15:25:40 -04:00
|
|
|
else
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
2010-10-07 15:25:40 -04:00
|
|
|
if (pit->second.mStatus == it->second.mStatus)
|
|
|
|
{
|
|
|
|
/* status is unchanged */
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-10-07 15:25:40 -04:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Status unchanged for : ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << " status: " << it->second.mStatus;
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
2011-07-05 20:14:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
/* now we check if we've done a callback before... */
|
|
|
|
if (it->second.mQFlags & BITDHT_QFLAGS_UPDATES)
|
|
|
|
{
|
|
|
|
if (now - pit->second.mCallbackTS > QUERY_UPDATE_PERIOD)
|
|
|
|
{
|
|
|
|
// keep flags.
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::checkStatus() Doing Update Callback for";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << " status: " << it->second.mStatus;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no callback this time */
|
|
|
|
doPing = false;
|
|
|
|
doCallback = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
doPing = false;
|
|
|
|
doCallback = false;
|
|
|
|
}
|
2010-10-07 15:25:40 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-10-07 15:25:40 -04:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Updating External Status for : ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << " to: " << it->second.mStatus;
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
2010-10-07 15:25:40 -04:00
|
|
|
/* update status */
|
|
|
|
pit->second.mStatus = it->second.mStatus;
|
2010-10-04 17:37:07 -04:00
|
|
|
}
|
2010-10-07 15:25:40 -04:00
|
|
|
|
|
|
|
if (doSaveAddress)
|
2010-10-04 17:37:07 -04:00
|
|
|
{
|
2010-10-07 15:25:40 -04:00
|
|
|
if (it->second.mResults.size() > 0)
|
|
|
|
{
|
|
|
|
pit->second.mDhtAddr = it->second.mResults.front().addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pit->second.mDhtAddr.sin_addr.s_addr = 0;
|
|
|
|
pit->second.mDhtAddr.sin_port = 0;
|
|
|
|
}
|
2010-10-04 17:37:07 -04:00
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2010-11-22 19:02:54 -05:00
|
|
|
/* add successful queries to ping list */
|
|
|
|
if (doPing)
|
|
|
|
{
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-11-22 19:02:54 -05:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Starting Ping (TODO): id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
2010-11-22 19:02:54 -05:00
|
|
|
/* add first matching peer */
|
|
|
|
//addPeerPing(foundId);
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
|
2010-11-22 19:02:54 -05:00
|
|
|
/* callback on new successful queries */
|
|
|
|
if (doCallback)
|
|
|
|
{
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
2010-11-22 19:02:54 -05:00
|
|
|
std::cerr << "bdNodeManager::checkStatus() Doing Callback: id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, &(it->first));
|
|
|
|
std::cerr << std::endl;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
2011-07-05 20:14:58 -04:00
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
pit->second.mCallbackTS = now;
|
2010-11-22 19:02:54 -05:00
|
|
|
bdId id(it->first,pit->second.mDhtAddr);
|
|
|
|
doPeerCallback(&id, callbackStatus);
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
bdNodeManager::checkPingStatus()
|
|
|
|
{
|
|
|
|
|
|
|
|
/* check queries */
|
|
|
|
std::map<bdNodeId, bdPingStatus>::iterator it;
|
|
|
|
std::map<bdNodeId, bdPingStatus> pingStatus;
|
|
|
|
|
|
|
|
PingStatus(pingStatus);
|
|
|
|
|
|
|
|
for(it = pingStatus.begin(); it != pingStatus.end(); it++)
|
|
|
|
{
|
|
|
|
switch(it->second.mStatus)
|
|
|
|
{
|
|
|
|
case BITDHT_QUERY_QUERYING:
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_QUERY_FAILURE:
|
|
|
|
{
|
|
|
|
// BAD.
|
|
|
|
doRemove = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_QUERY_FOUND_CLOSEST:
|
|
|
|
{
|
|
|
|
|
|
|
|
doRemove = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITDHT_QUERY_SUCCESS:
|
|
|
|
{
|
|
|
|
foundId =
|
|
|
|
doRemove = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove done queries */
|
|
|
|
if (doRemove)
|
|
|
|
{
|
|
|
|
clearQuery(it->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add successful queries to ping list */
|
|
|
|
if (doPing)
|
|
|
|
{
|
|
|
|
/* add first matching peer */
|
|
|
|
addPeerPing(foundId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* callback on new successful queries */
|
|
|
|
if (doCallback)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-24 19:48:33 -05:00
|
|
|
int bdNodeManager::checkBadPeerStatus()
|
|
|
|
{
|
|
|
|
bdId id;
|
|
|
|
uint32_t flags;
|
|
|
|
std::string nullstr;
|
|
|
|
|
|
|
|
while(mBadPeerQueue.popPeer(&id, flags))
|
|
|
|
{
|
|
|
|
doInfoCallback(&id, BITDHT_INFO_CB_TYPE_BADPEER, flags, nullstr);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
int bdNodeManager::SearchOutOfDate()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::SearchOutOfDate()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::map<bdNodeId, bdQueryPeer>::iterator it;
|
|
|
|
|
|
|
|
/* search for out-of-date peers */
|
|
|
|
for(it = mActivePeers.begin(); it != mActivePeers.end(); it++)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
if (old)
|
|
|
|
{
|
|
|
|
addQuery(it->first);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***** Functions to Call down to bdNodeManager ****/
|
|
|
|
/* Request DHT Peer Lookup */
|
|
|
|
/* Request Keyword Lookup */
|
|
|
|
|
2010-09-29 15:59:31 -04:00
|
|
|
void bdNodeManager::findDhtValue(bdNodeId * /*id*/, std::string /*key*/, uint32_t /*mode*/)
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::findDhtValue() TODO";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***** Get Results Details *****/
|
2010-10-04 17:37:07 -04:00
|
|
|
int bdNodeManager::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from)
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::getDhtPeerAddress() Id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << " ... ? TODO" << std::endl;
|
2010-09-29 15:59:31 -04:00
|
|
|
#else
|
|
|
|
(void) id;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
|
|
|
|
2010-10-04 17:37:07 -04:00
|
|
|
std::map<bdNodeId, bdQueryPeer>::iterator pit;
|
|
|
|
pit = mActivePeers.find(*id);
|
|
|
|
|
|
|
|
std::cerr << "bdNodeManager::getDhtPeerAddress() Id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (pit != mActivePeers.end())
|
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::getDhtPeerAddress() Found ActiveQuery";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (pit->second.mStatus == BITDHT_QUERY_SUCCESS)
|
|
|
|
{
|
|
|
|
from = pit->second.mDhtAddr;
|
|
|
|
|
|
|
|
std::cerr << "bdNodeManager::getDhtPeerAddress() Found Peer Address:";
|
2012-03-17 21:33:25 -04:00
|
|
|
std::cerr << bdnet_inet_ntoa(from.sin_addr) << ":" << htons(from.sin_port);
|
2010-10-04 17:37:07 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
|
2010-10-04 17:37:07 -04:00
|
|
|
int bdNodeManager::getDhtValue(const bdNodeId *id, std::string key, std::string & /*value*/)
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::getDhtValue() Id: ";
|
|
|
|
mFns->bdPrintNodeId(std::cerr, id);
|
|
|
|
std::cerr << " key: " << key;
|
|
|
|
std::cerr << " ... ? TODO" << std::endl;
|
2010-09-29 15:59:31 -04:00
|
|
|
#else
|
|
|
|
(void) id;
|
|
|
|
(void) key;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
int bdNodeManager::getDhtBucket(const int idx, bdBucket &bucket)
|
|
|
|
{
|
|
|
|
return mNodeSpace.getDhtBucket(idx, bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdNodeManager::getDhtQueries(std::map<bdNodeId, bdQueryStatus> &queries)
|
|
|
|
{
|
|
|
|
mQueryMgr->QueryStatus(queries);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdNodeManager::getDhtQueryStatus(const bdNodeId *id, bdQuerySummary &query)
|
|
|
|
{
|
|
|
|
return mQueryMgr->QuerySummary(id, query);
|
|
|
|
}
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
/***** Add / Remove Callback Clients *****/
|
|
|
|
void bdNodeManager::addCallback(BitDhtCallback *cb)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::addCallback()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* search list */
|
|
|
|
std::list<BitDhtCallback *>::iterator it;
|
|
|
|
it = std::find(mCallbacks.begin(), mCallbacks.end(), cb);
|
|
|
|
if (it == mCallbacks.end())
|
|
|
|
{
|
|
|
|
/* add it */
|
|
|
|
mCallbacks.push_back(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bdNodeManager::removeCallback(BitDhtCallback *cb)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::removeCallback()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* search list */
|
|
|
|
std::list<BitDhtCallback *>::iterator it;
|
|
|
|
it = std::find(mCallbacks.begin(), mCallbacks.end(), cb);
|
|
|
|
if (it == mCallbacks.end())
|
|
|
|
{
|
|
|
|
/* not found! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
it = mCallbacks.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void bdNodeManager::addPeer(const bdId *id, uint32_t peerflags)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::addPeer() Overloaded (doing Callback)";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
doNodeCallback(id, peerflags);
|
|
|
|
|
|
|
|
// call parent.
|
|
|
|
bdNode::addPeer(id, peerflags);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void bdNodeManager::doNodeCallback(const bdId *id, uint32_t peerflags)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_MGR
|
2010-10-01 15:43:54 -04:00
|
|
|
std::cerr << "bdNodeManager::doNodeCallback() ";
|
|
|
|
mFns->bdPrintId(std::cerr, id);
|
|
|
|
std::cerr << "peerflags: " << peerflags;
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-10-01 15:43:54 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
/* search list */
|
|
|
|
std::list<BitDhtCallback *>::iterator it;
|
|
|
|
for(it = mCallbacks.begin(); it != mCallbacks.end(); it++)
|
|
|
|
{
|
|
|
|
(*it)->dhtNodeCallback(id, peerflags);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-22 19:02:54 -05:00
|
|
|
void bdNodeManager::doPeerCallback(const bdId *id, uint32_t status)
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
2010-10-01 15:43:54 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::doPeerCallback()";
|
2011-06-29 06:46:11 -04:00
|
|
|
mFns->bdPrintId(std::cerr, id);
|
2010-10-01 15:43:54 -04:00
|
|
|
std::cerr << "status: " << status;
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-10-01 15:43:54 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
/* search list */
|
|
|
|
std::list<BitDhtCallback *>::iterator it;
|
|
|
|
for(it = mCallbacks.begin(); it != mCallbacks.end(); it++)
|
|
|
|
{
|
|
|
|
(*it)->dhtPeerCallback(id, status);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-22 19:02:54 -05:00
|
|
|
void bdNodeManager::doValueCallback(const bdNodeId *id, std::string key, uint32_t status)
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::doValueCallback()";
|
|
|
|
std::cerr << std::endl;
|
2010-10-01 15:43:54 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_MGR
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
|
|
|
/* search list */
|
|
|
|
std::list<BitDhtCallback *>::iterator it;
|
|
|
|
for(it = mCallbacks.begin(); it != mCallbacks.end(); it++)
|
|
|
|
{
|
2010-11-22 19:02:54 -05:00
|
|
|
(*it)->dhtValueCallback(id, key, status);
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
|
2011-11-24 19:48:33 -05:00
|
|
|
void bdNodeManager::doInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info)
|
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::doInfoCallback()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
#endif
|
|
|
|
/* search list */
|
|
|
|
std::list<BitDhtCallback *>::iterator it;
|
|
|
|
for(it = mCallbacks.begin(); it != mCallbacks.end(); it++)
|
|
|
|
{
|
|
|
|
(*it)->dhtInfoCallback(id, type, flags, info);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-20 13:04:44 -04:00
|
|
|
#define BITDHT_IDENTITY_STRING_V1 "d1:"
|
|
|
|
#define BITDHT_IDENTITY_SIZE_V1 3
|
|
|
|
#define BITDHT_PACKET_MIN_SIZE 4
|
2011-06-29 06:46:11 -04:00
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
/******************* Internals *************************/
|
2010-09-29 15:59:31 -04:00
|
|
|
int bdNodeManager::isBitDhtPacket(char *data, int size, struct sockaddr_in & from)
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
{
|
2011-07-20 13:04:44 -04:00
|
|
|
|
|
|
|
/* use a very simple initial check */
|
|
|
|
if (size < BITDHT_PACKET_MIN_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (0 == strncmp(data, BITDHT_IDENTITY_STRING_V1, BITDHT_IDENTITY_SIZE_V1));
|
|
|
|
|
|
|
|
/* Below is the old version! */
|
|
|
|
|
|
|
|
|
2010-07-10 07:48:24 -04:00
|
|
|
#ifdef DEBUG_MGR_PKT
|
|
|
|
std::cerr << "bdNodeManager::isBitDhtPacket() *******************************";
|
|
|
|
std::cerr << " from " << inet_ntoa(from.sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(from.sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
{
|
|
|
|
/* print the fucker... only way to catch bad ones */
|
2012-04-19 16:44:32 -04:00
|
|
|
std::string out;
|
2010-07-10 07:48:24 -04:00
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (isascii(data[i]))
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
out += data[i];
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
bd_sprintf_append(out, "[%02lx]", (uint32_t) data[i]);
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
if ((i % 16 == 0) && (i != 0))
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
out += "\n";
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 16:44:32 -04:00
|
|
|
std::cerr << out;
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
std::cerr << "bdNodeManager::isBitDhtPacket() *******************************";
|
|
|
|
std::cerr << std::endl;
|
2010-09-29 15:59:31 -04:00
|
|
|
#else
|
|
|
|
(void) from;
|
2010-07-10 07:48:24 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* try to parse it! */
|
|
|
|
/* convert to a be_node */
|
|
|
|
be_node *node = be_decoden(data, size);
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
/* invalid decode */
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdNodeManager::isBitDhtPacket() be_decode failed. dropping";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "bdNodeManager::BadPacket ******************************";
|
|
|
|
std::cerr << " from " << inet_ntoa(from.sin_addr);
|
|
|
|
std::cerr << ":" << ntohs(from.sin_port);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
{
|
|
|
|
/* print the fucker... only way to catch bad ones */
|
2012-04-19 16:44:32 -04:00
|
|
|
std::string out;
|
2010-07-10 07:48:24 -04:00
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (isascii(data[i]))
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
out += data[i];
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
bd_sprintf_append(out, "[%02lx]", (uint32_t) data[i]);
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
if ((i % 16 == 0) && (i != 0))
|
|
|
|
{
|
2012-04-19 16:44:32 -04:00
|
|
|
out += "\n";
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 16:44:32 -04:00
|
|
|
std::cerr << out;
|
2010-07-10 07:48:24 -04:00
|
|
|
}
|
|
|
|
std::cerr << "bdNodeManager::BadPacket ******************************";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find message type */
|
|
|
|
uint32_t beType = beMsgType(node);
|
|
|
|
int ans = (beType != BITDHT_MSG_TYPE_UNKNOWN);
|
|
|
|
be_free(node);
|
|
|
|
|
|
|
|
#ifdef DEBUG_MGR_PKT
|
|
|
|
if (ans)
|
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::isBitDhtPacket() YES";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::isBitDhtPacket() NO: Unknown Type";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bdDebugCallback::~bdDebugCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
int bdDebugCallback::dhtPeerCallback(const bdId *id, uint32_t status)
|
2010-07-10 07:48:24 -04:00
|
|
|
{
|
2011-08-12 08:43:24 -04:00
|
|
|
/* remove unused parameter warnings */
|
|
|
|
(void) status;
|
|
|
|
|
2011-01-20 17:18:42 -05:00
|
|
|
#ifdef DEBUG_MGR
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << "bdDebugCallback::dhtPeerCallback() Id: ";
|
2011-01-20 17:18:42 -05:00
|
|
|
#endif
|
2011-06-29 06:46:11 -04:00
|
|
|
bdStdPrintId(std::cerr, id);
|
2011-01-20 17:18:42 -05:00
|
|
|
#ifdef DEBUG_MGR
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << " status: " << std::hex << status << std::dec << std::endl;
|
2011-01-20 17:18:42 -05:00
|
|
|
#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdDebugCallback::dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status)
|
|
|
|
{
|
2011-08-12 08:43:24 -04:00
|
|
|
/* remove unused parameter warnings */
|
|
|
|
(void) key;
|
|
|
|
(void) status;
|
|
|
|
|
2011-01-20 17:18:42 -05:00
|
|
|
#ifdef DEBUG_MGR
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << "bdDebugCallback::dhtValueCallback() Id: ";
|
2011-01-20 17:18:42 -05:00
|
|
|
#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
bdStdPrintNodeId(std::cerr, id);
|
2011-01-20 17:18:42 -05:00
|
|
|
#ifdef DEBUG_MGR
|
2010-07-10 07:48:24 -04:00
|
|
|
std::cerr << " key: " << key;
|
|
|
|
std::cerr << " status: " << std::hex << status << std::dec << std::endl;
|
2011-01-20 17:18:42 -05:00
|
|
|
#endif
|
2010-07-10 07:48:24 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************* Connection Stuff ********************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-17 19:29:53 -04:00
|
|
|
bool bdNodeManager::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start)
|
2011-06-29 06:46:11 -04:00
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::ConnectionRequest()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-17 19:29:53 -04:00
|
|
|
return mConnMgr->requestConnection(laddr, target, mode, delay, start);
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
|
2011-07-17 19:29:53 -04:00
|
|
|
void bdNodeManager::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t bandwidth, uint32_t delay, uint32_t answer)
|
2011-06-29 06:46:11 -04:00
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::ConnectionAuth()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (answer == BITDHT_CONNECT_ANSWER_OKAY)
|
|
|
|
{
|
2011-07-17 19:29:53 -04:00
|
|
|
mConnMgr->AuthConnectionOk(srcId, proxyId, destId, mode, loc, bandwidth, delay);
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mConnMgr->AuthConnectionNo(srcId, proxyId, destId, mode, loc, answer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bdNodeManager::ConnectionOptions(uint32_t allowedModes, uint32_t flags)
|
|
|
|
{
|
|
|
|
mConnMgr->setConnectionOptions(allowedModes, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***** Connections Requests *****/
|
|
|
|
|
|
|
|
// Overloaded from bdnode for external node callback.
|
2011-07-17 19:29:53 -04:00
|
|
|
void bdNodeManager::callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int point, int param, int cbtype, int errcode)
|
2011-06-29 06:46:11 -04:00
|
|
|
{
|
|
|
|
std::cerr << "bdNodeManager::callbackConnect()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
#endif
|
|
|
|
/* search list */
|
|
|
|
std::list<BitDhtCallback *>::iterator it;
|
|
|
|
for(it = mCallbacks.begin(); it != mCallbacks.end(); it++)
|
|
|
|
{
|
2011-07-17 19:29:53 -04:00
|
|
|
(*it)->dhtConnectCallback(srcId, proxyId, destId, mode, point, param, cbtype, errcode);
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdDebugCallback::dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
2011-07-17 19:29:53 -04:00
|
|
|
uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode)
|
2011-06-29 06:46:11 -04:00
|
|
|
{
|
2011-08-12 08:43:24 -04:00
|
|
|
/* remove unused parameter warnings */
|
|
|
|
(void) srcId;
|
|
|
|
(void) proxyId;
|
|
|
|
(void) destId;
|
|
|
|
(void) mode;
|
|
|
|
(void) point;
|
|
|
|
(void) param;
|
|
|
|
(void) cbtype;
|
|
|
|
(void) errcode;
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdDebugCallback::dhtConnectCallback() Type: " << cbtype;
|
|
|
|
std::cerr << " errCode: " << errcode;
|
|
|
|
std::cerr << " srcId: ";
|
|
|
|
bdStdPrintId(std::cerr, srcId);
|
|
|
|
std::cerr << " proxyId: ";
|
|
|
|
bdStdPrintId(std::cerr, proxyId);
|
|
|
|
std::cerr << " destId: ";
|
|
|
|
bdStdPrintId(std::cerr, destId);
|
|
|
|
std::cerr << " mode: " << mode;
|
2011-07-17 19:29:53 -04:00
|
|
|
std::cerr << " param: " << param;
|
2011-06-29 06:46:11 -04:00
|
|
|
std::cerr << " point: " << point << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-01-07 06:28:00 -05:00
|
|
|
|
|
|
|
int bdDebugCallback::dhtInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info)
|
|
|
|
{
|
|
|
|
/* remove unused parameter warnings */
|
|
|
|
(void) id;
|
|
|
|
(void) type;
|
|
|
|
(void) flags;
|
|
|
|
(void) info;
|
|
|
|
|
|
|
|
#ifdef DEBUG_MGR
|
|
|
|
std::cerr << "bdDebugCallback::dhtInfoCallback() Type: " << type;
|
|
|
|
std::cerr << " Id: ";
|
|
|
|
bdStdPrintId(std::cerr, id);
|
|
|
|
std::cerr << " flags: " << flags;
|
|
|
|
std::cerr << " info: " << info;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|