2010-07-10 11:48:24 +00:00
|
|
|
#ifndef BITDHT_NODE_H
|
|
|
|
#define BITDHT_NODE_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bitdht/bdnode.h
|
|
|
|
*
|
|
|
|
* 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-07-18 16:28:51 +00:00
|
|
|
#include "bitdht/bdpeer.h"
|
|
|
|
#include "bitdht/bdquery.h"
|
|
|
|
#include "bitdht/bdstore.h"
|
|
|
|
#include "bitdht/bdobj.h"
|
|
|
|
#include "bitdht/bdhash.h"
|
2010-09-29 19:59:31 +00:00
|
|
|
#include "bitdht/bdhistory.h"
|
2010-07-10 11:48:24 +00:00
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
#include "bitdht/bdconnection.h"
|
|
|
|
#include "bitdht/bdaccount.h"
|
|
|
|
|
2011-11-22 13:16:03 +00:00
|
|
|
#include "bitdht/bdfriendlist.h"
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
class bdFilter;
|
|
|
|
|
2010-07-10 11:48:24 +00:00
|
|
|
|
|
|
|
#define BD_QUERY_NEIGHBOURS 1
|
|
|
|
#define BD_QUERY_HASH 2
|
|
|
|
|
|
|
|
/**********************************
|
|
|
|
* Running a node....
|
|
|
|
*
|
|
|
|
* run().
|
|
|
|
* loops through and checks out of date peers.
|
|
|
|
* handles searches.
|
|
|
|
* prints out dht Table.
|
|
|
|
*
|
|
|
|
|
|
|
|
The node handles the i/o traffic from peers.
|
|
|
|
It
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ping, return
|
|
|
|
peers, return
|
|
|
|
hash store, return
|
|
|
|
hash get, return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
respond queue.
|
|
|
|
|
|
|
|
query queue.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
input -> call into recvFunction()
|
|
|
|
output -> call back to Udp().
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*********/
|
|
|
|
|
|
|
|
class bdNodeNetMsg
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
bdNodeNetMsg(char *data, int size, struct sockaddr_in *addr);
|
|
|
|
~bdNodeNetMsg();
|
|
|
|
|
|
|
|
void print(std::ostream &out);
|
|
|
|
|
|
|
|
char *data;
|
|
|
|
int mSize;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
class bdNodePublisher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/* simplified outgoing msg functions (for the managers) */
|
|
|
|
virtual void send_ping(bdId *id) = 0; /* message out */
|
|
|
|
virtual void send_query(bdId *id, bdNodeId *targetNodeId) = 0; /* message out */
|
|
|
|
virtual void send_connect_msg(bdId *id, int msgtype,
|
2011-07-17 23:29:53 +00:00
|
|
|
bdId *srcAddr, bdId *destAddr, int mode, int param, int status) = 0;
|
2011-06-29 10:46:11 +00:00
|
|
|
|
|
|
|
// internal Callback -> normally continues to callbackConnect().
|
|
|
|
virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
2011-07-17 23:29:53 +00:00
|
|
|
int mode, int point, int param, int cbtype, int errcode) = 0;
|
2011-06-29 10:46:11 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class bdNode: public bdNodePublisher
|
2010-07-10 11:48:24 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
bdNode(bdNodeId *id, std::string dhtVersion, std::string bootfile,
|
|
|
|
bdDhtFunctions *fns);
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
void init(); /* sets up the self referential classes (mQueryMgr & mConnMgr) */
|
|
|
|
|
|
|
|
void setNodeOptions(uint32_t optFlags);
|
2011-12-01 17:19:30 +00:00
|
|
|
uint32_t setNodeDhtMode(uint32_t dhtFlags);
|
2011-06-29 10:46:11 +00:00
|
|
|
|
2010-10-17 20:55:32 +00:00
|
|
|
/* startup / shutdown node */
|
|
|
|
void restartNode();
|
|
|
|
void shutdownNode();
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
void getOwnId(bdNodeId *id);
|
|
|
|
|
2010-07-10 11:48:24 +00:00
|
|
|
// virtual so manager can do callback.
|
|
|
|
// peer flags defined in bdiface.h
|
|
|
|
virtual void addPeer(const bdId *id, uint32_t peerflags);
|
|
|
|
|
|
|
|
void printState();
|
2011-06-29 10:46:11 +00:00
|
|
|
void checkPotentialPeer(bdId *id, bdId *src);
|
|
|
|
void addPotentialPeer(bdId *id, bdId *src);
|
2010-07-10 11:48:24 +00:00
|
|
|
|
2010-10-17 20:55:32 +00:00
|
|
|
void iterationOff();
|
2010-07-10 11:48:24 +00:00
|
|
|
void iteration();
|
|
|
|
void processRemoteQuery();
|
|
|
|
void updateStore();
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
/* simplified outgoing msg functions (for the managers) */
|
|
|
|
virtual void send_ping(bdId *id); /* message out */
|
|
|
|
virtual void send_query(bdId *id, bdNodeId *targetNodeId); /* message out */
|
|
|
|
virtual void send_connect_msg(bdId *id, int msgtype,
|
2011-07-17 23:29:53 +00:00
|
|
|
bdId *srcAddr, bdId *destAddr, int mode, int param, int status);
|
2010-07-10 11:48:24 +00:00
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
// This is implemented in bdManager.
|
|
|
|
// virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
2011-07-17 23:29:53 +00:00
|
|
|
// int mode, int point, int param, int cbtype, int errcode);
|
2011-06-29 10:46:11 +00:00
|
|
|
|
|
|
|
/* interaction with outside world (Accessed by controller to deliver us msgs) */
|
2010-07-10 11:48:24 +00:00
|
|
|
int outgoingMsg(struct sockaddr_in *addr, char *msg, int *len);
|
|
|
|
void incomingMsg(struct sockaddr_in *addr, char *msg, int len);
|
|
|
|
|
2011-12-20 02:03:25 +00:00
|
|
|
// For Relay Mode switching.
|
|
|
|
void dropRelayServers();
|
|
|
|
void pingRelayServers();
|
2011-06-29 10:46:11 +00:00
|
|
|
|
|
|
|
// Below is internal Management of incoming / outgoing messages.
|
|
|
|
private:
|
|
|
|
|
2010-07-10 11:48:24 +00:00
|
|
|
/* internal interaction with network */
|
|
|
|
void sendPkt(char *msg, int len, struct sockaddr_in addr);
|
|
|
|
void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
|
|
|
|
|
|
|
|
|
|
|
/* output functions (send msg) */
|
|
|
|
void msgout_ping(bdId *id, bdToken *transId);
|
|
|
|
void msgout_pong(bdId *id, bdToken *transId);
|
|
|
|
void msgout_find_node(bdId *id, bdToken *transId, bdNodeId *query);
|
|
|
|
void msgout_reply_find_node(bdId *id, bdToken *transId,
|
|
|
|
std::list<bdId> &peers);
|
|
|
|
void msgout_get_hash(bdId *id, bdToken *transId, bdNodeId *info_hash);
|
|
|
|
void msgout_reply_hash(bdId *id, bdToken *transId,
|
|
|
|
bdToken *token, std::list<std::string> &values);
|
|
|
|
void msgout_reply_nearest(bdId *id, bdToken *transId,
|
|
|
|
bdToken *token, std::list<bdId> &peers);
|
|
|
|
|
|
|
|
void msgout_post_hash(bdId *id, bdToken *transId, bdNodeId *info_hash,
|
|
|
|
uint32_t port, bdToken *token);
|
|
|
|
void msgout_reply_post(bdId *id, bdToken *transId);
|
|
|
|
|
|
|
|
|
|
|
|
/* input functions (once mesg is parsed) */
|
|
|
|
void msgin_ping(bdId *id, bdToken *token);
|
|
|
|
void msgin_pong(bdId *id, bdToken *transId, bdToken *versionId);
|
|
|
|
|
|
|
|
void msgin_find_node(bdId *id, bdToken *transId, bdNodeId *query);
|
|
|
|
void msgin_reply_find_node(bdId *id, bdToken *transId,
|
|
|
|
std::list<bdId> &entries);
|
|
|
|
|
|
|
|
void msgin_get_hash(bdId *id, bdToken *transId, bdNodeId *nodeid);
|
|
|
|
void msgin_reply_hash(bdId *id, bdToken *transId,
|
|
|
|
bdToken *token, std::list<std::string> &values);
|
|
|
|
void msgin_reply_nearest(bdId *id, bdToken *transId,
|
|
|
|
bdToken *token, std::list<bdId> &nodes);
|
|
|
|
|
|
|
|
void msgin_post_hash(bdId *id, bdToken *transId,
|
|
|
|
bdNodeId *info_hash, uint32_t port, bdToken *token);
|
|
|
|
void msgin_reply_post(bdId *id, bdToken *transId);
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
void msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype,
|
2011-07-17 23:29:53 +00:00
|
|
|
bdId *srcAddr, bdId *destAddr, int mode, int param, int status);
|
2011-06-29 10:46:11 +00:00
|
|
|
void msgin_connect_genmsg(bdId *id, bdToken *transId, int msgtype,
|
2011-07-17 23:29:53 +00:00
|
|
|
bdId *srcAddr, bdId *destAddr, int mode, int param, int status);
|
2011-06-29 10:46:11 +00:00
|
|
|
|
2010-07-10 11:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* token handling */
|
|
|
|
void genNewToken(bdToken *token);
|
|
|
|
int queueQuery(bdId *id, bdNodeId *query, bdToken *transId, uint32_t query_type);
|
|
|
|
|
|
|
|
/* transId handling */
|
|
|
|
void genNewTransId(bdToken *token);
|
2013-08-26 04:29:27 +00:00
|
|
|
void registerOutgoingMsg(bdId *id, bdToken *transId, uint32_t msgType, bdNodeId *aboutId);
|
|
|
|
uint32_t registerIncomingMsg(bdId *id, bdToken *transId, uint32_t msgType, bdNodeId *aboutId);
|
2012-10-25 22:36:22 +00:00
|
|
|
|
2010-07-10 11:48:24 +00:00
|
|
|
void cleanupTransIdRegister();
|
|
|
|
|
|
|
|
|
|
|
|
void doStats();
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
/********** Variables **********/
|
|
|
|
private:
|
2010-07-10 11:48:24 +00:00
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
/**** Some Variables are Protected to allow inherited classes to use *****/
|
2010-08-01 18:19:27 +00:00
|
|
|
protected:
|
2010-07-10 11:48:24 +00:00
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
bdSpace mNodeSpace;
|
|
|
|
|
|
|
|
bdQueryManager *mQueryMgr;
|
|
|
|
bdConnectManager *mConnMgr;
|
|
|
|
bdFilter *mFilterPeers;
|
2010-07-10 11:48:24 +00:00
|
|
|
|
|
|
|
bdNodeId mOwnId;
|
|
|
|
bdId mLikelyOwnId; // Try to workout own id address.
|
2011-06-29 10:46:11 +00:00
|
|
|
std::string mDhtVersion;
|
2010-08-01 18:19:27 +00:00
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
bdAccount mAccount;
|
2010-07-10 11:48:24 +00:00
|
|
|
bdStore mStore;
|
|
|
|
|
|
|
|
bdDhtFunctions *mFns;
|
|
|
|
bdHashSpace mHashSpace;
|
2011-06-29 10:46:11 +00:00
|
|
|
|
2011-11-22 13:16:03 +00:00
|
|
|
bdFriendList mFriendList;
|
2011-11-25 00:48:33 +00:00
|
|
|
bdPeerQueue mBadPeerQueue;
|
2011-11-22 13:16:03 +00:00
|
|
|
|
2012-10-25 22:36:22 +00:00
|
|
|
bdHistory mHistory; /* for understanding the DHT */
|
|
|
|
|
2011-06-29 10:46:11 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
uint32_t mNodeOptionFlags;
|
2011-12-01 17:19:30 +00:00
|
|
|
uint32_t mNodeDhtMode;
|
2011-12-20 02:03:25 +00:00
|
|
|
|
2011-12-01 17:19:30 +00:00
|
|
|
uint32_t mMaxAllowedMsgs;
|
2011-12-20 02:03:25 +00:00
|
|
|
uint32_t mRelayMode;
|
2011-06-29 10:46:11 +00:00
|
|
|
|
2010-07-10 11:48:24 +00:00
|
|
|
|
|
|
|
std::list<bdRemoteQuery> mRemoteQueries;
|
|
|
|
|
|
|
|
std::list<bdId> mPotentialPeers;
|
|
|
|
|
|
|
|
std::list<bdNodeNetMsg *> mOutgoingMsgs;
|
|
|
|
std::list<bdNodeNetMsg *> mIncomingMsgs;
|
2011-12-01 17:19:30 +00:00
|
|
|
|
2010-07-10 11:48:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // BITDHT_NODE_H
|