2011-07-09 05:36:37 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/pqi: p3linkmgr.h
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2007-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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MRK_PQI_LINK_MANAGER_HEADER
|
|
|
|
#define MRK_PQI_LINK_MANAGER_HEADER
|
|
|
|
|
|
|
|
#include "pqi/pqimonitor.h"
|
|
|
|
#include "pqi/pqiipset.h"
|
|
|
|
|
|
|
|
//#include "pqi/p3dhtmgr.h"
|
|
|
|
//#include "pqi/p3upnpmgr.h"
|
|
|
|
#include "pqi/pqiassist.h"
|
|
|
|
|
|
|
|
#include "pqi/p3cfgmgr.h"
|
|
|
|
|
|
|
|
#include "util/rsthreads.h"
|
|
|
|
|
|
|
|
class ExtAddrFinder ;
|
|
|
|
class DNSResolver ;
|
|
|
|
|
|
|
|
/* order of attempts ... */
|
|
|
|
const uint32_t RS_NET_CONN_TCP_ALL = 0x000f;
|
|
|
|
const uint32_t RS_NET_CONN_UDP_ALL = 0x00f0;
|
|
|
|
const uint32_t RS_NET_CONN_TUNNEL = 0x0f00;
|
|
|
|
|
|
|
|
const uint32_t RS_NET_CONN_TCP_LOCAL = 0x0001;
|
|
|
|
const uint32_t RS_NET_CONN_TCP_EXTERNAL = 0x0002;
|
|
|
|
const uint32_t RS_NET_CONN_TCP_UNKNOW_TOPOLOGY = 0x0004;
|
|
|
|
const uint32_t RS_NET_CONN_UDP_DHT_SYNC = 0x0010;
|
|
|
|
const uint32_t RS_NET_CONN_UDP_PEER_SYNC = 0x0020; /* coming soon */
|
|
|
|
|
|
|
|
/* extra flags */
|
|
|
|
// not sure if needed yet.
|
|
|
|
//const uint32_t RS_NET_CONN_PEERAGE = 0x0f00;
|
|
|
|
//const uint32_t RS_NET_CONN_SERVER = 0x0100; /* TCP only */
|
|
|
|
//const uint32_t RS_NET_CONN_PEER = 0x0200; /* all UDP */
|
|
|
|
|
|
|
|
const uint32_t RS_TCP_STD_TIMEOUT_PERIOD = 5; /* 5 seconds! */
|
|
|
|
|
|
|
|
class peerAddrInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
peerAddrInfo(); /* init */
|
|
|
|
|
|
|
|
bool found;
|
|
|
|
uint32_t type;
|
|
|
|
pqiIpAddrSet addrs;
|
|
|
|
time_t ts;
|
|
|
|
};
|
|
|
|
|
|
|
|
class peerConnectAddress
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
peerConnectAddress(); /* init */
|
|
|
|
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
uint32_t delay; /* to stop simultaneous connects */
|
|
|
|
uint32_t period; /* UDP only */
|
|
|
|
uint32_t type;
|
2011-07-13 18:23:40 -04:00
|
|
|
uint32_t flags; /* CB FLAGS defined in pqimonitor.h */
|
2011-07-09 05:36:37 -04:00
|
|
|
time_t ts;
|
|
|
|
};
|
|
|
|
|
|
|
|
class peerConnectState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
peerConnectState(); /* init */
|
|
|
|
|
|
|
|
std::string id;
|
|
|
|
//std::string gpg_id;
|
|
|
|
|
|
|
|
//uint32_t netMode; /* EXT / UPNP / UDP / INVALID */
|
2011-07-09 14:39:34 -04:00
|
|
|
//uint32_t visState; /* STD, GRAY, DARK */
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
//struct sockaddr_in localaddr, serveraddr;
|
|
|
|
|
|
|
|
//used to store current ip (for config and connection management)
|
|
|
|
//struct sockaddr_in currentlocaladdr; /* Mandatory */
|
|
|
|
//struct sockaddr_in currentserveraddr; /* Mandatory */
|
|
|
|
//std::string dyndns;
|
|
|
|
|
|
|
|
|
|
|
|
/* list of addresses from various sources */
|
|
|
|
//pqiIpAddrSet ipAddrs;
|
|
|
|
|
|
|
|
/***** Below here not stored permanently *****/
|
|
|
|
|
2011-07-09 20:41:39 -04:00
|
|
|
bool dhtVisible;
|
|
|
|
|
2011-07-10 20:52:12 -04:00
|
|
|
//time_t lastcontact;
|
2011-07-09 14:39:34 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
uint32_t connecttype; // RS_NET_CONN_TCP_ALL / RS_NET_CONN_UDP_ALL
|
|
|
|
time_t lastavailable;
|
|
|
|
time_t lastattempt;
|
|
|
|
|
|
|
|
std::string name;
|
2011-07-09 14:39:34 -04:00
|
|
|
|
|
|
|
//std::string location;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
uint32_t state;
|
|
|
|
uint32_t actions;
|
|
|
|
|
|
|
|
uint32_t source; /* most current source */
|
|
|
|
peerAddrInfo dht;
|
|
|
|
peerAddrInfo disc;
|
|
|
|
peerAddrInfo peer;
|
|
|
|
|
|
|
|
/* a list of connect attempts to make (in order) */
|
|
|
|
bool inConnAttempt;
|
|
|
|
peerConnectAddress currentConnAddrAttempt;
|
|
|
|
std::list<peerConnectAddress> connAddrs;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class p3tunnel;
|
|
|
|
class RsPeerGroupItem;
|
|
|
|
class RsGroupInfo;
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
class p3PeerMgr;
|
|
|
|
class p3NetMgr;
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
class p3PeerMgrIMPL;
|
|
|
|
class p3NetMgrIMPL;
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
std::string textPeerConnectState(peerConnectState &state);
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/*******
|
|
|
|
* Virtual Interface to allow testing
|
|
|
|
*
|
|
|
|
*/
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
class p3LinkMgr: public pqiConnectCb
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
p3LinkMgr() { return; }
|
|
|
|
virtual ~p3LinkMgr() { return; }
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
virtual const std::string getOwnId() = 0;
|
|
|
|
virtual bool isOnline(const std::string &ssl_id) = 0;
|
|
|
|
virtual void getOnlineList(std::list<std::string> &ssl_peers) = 0;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/**************** handle monitors *****************/
|
|
|
|
virtual void addMonitor(pqiMonitor *mon) = 0;
|
|
|
|
virtual void removeMonitor(pqiMonitor *mon) = 0;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/****************** Connections *******************/
|
|
|
|
virtual bool connectAttempt(const std::string &id, struct sockaddr_in &addr,
|
2011-07-13 18:23:40 -04:00
|
|
|
uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags) = 0;
|
2011-07-13 10:22:25 -04:00
|
|
|
virtual bool connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address) = 0;
|
|
|
|
virtual bool retryConnect(const std::string &id) = 0;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/* Network Addresses */
|
|
|
|
virtual bool setLocalAddress(struct sockaddr_in addr) = 0;
|
|
|
|
virtual struct sockaddr_in getLocalAddress() = 0;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
virtual void getFriendList(std::list<std::string> &ssl_peers) = 0; // ONLY used by p3peers.cc USE p3PeerMgr instead.
|
|
|
|
virtual int getOnlineCount() = 0; // ONLY used by p3peers.cc
|
|
|
|
virtual int getFriendCount() = 0; // ONLY used by p3serverconfig.cc & p3peers.cc
|
|
|
|
virtual bool getFriendNetStatus(const std::string &id, peerConnectState &state) = 0; // ONLY used by p3peers.cc
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
virtual void setTunnelConnection(bool b) = 0; // ONLY used by p3peermgr.cc & p3peers.cc MOVE => p3PeerMgr
|
|
|
|
virtual bool getTunnelConnection() = 0; // ONLY used by p3peermgr.cc & p3peers.cc MOVE => p3PeerMgr
|
2011-07-09 20:41:39 -04:00
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/******* overloaded from pqiConnectCb *************/
|
|
|
|
// THESE MUSTn't BE specfied HERE - as overloaded from pqiConnectCb.
|
|
|
|
//virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs,
|
|
|
|
// uint32_t type, uint32_t flags, uint32_t source) = 0;
|
2011-07-13 18:23:40 -04:00
|
|
|
//virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr,
|
|
|
|
// uint32_t source, uint32_t flags, uint32_t delay) = 0;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/****************************************************************************/
|
|
|
|
/****************************************************************************/
|
|
|
|
/****************************************************************************/
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
};
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
class p3LinkMgrIMPL: public p3LinkMgr
|
|
|
|
{
|
|
|
|
public:
|
2011-07-09 14:39:34 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/************************************************************************************************/
|
|
|
|
/* EXTERNAL INTERFACE */
|
|
|
|
/************************************************************************************************/
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
virtual const std::string getOwnId();
|
|
|
|
virtual bool isOnline(const std::string &ssl_id);
|
|
|
|
virtual void getOnlineList(std::list<std::string> &ssl_peers);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**************** handle monitors *****************/
|
2011-07-13 10:22:25 -04:00
|
|
|
virtual void addMonitor(pqiMonitor *mon);
|
|
|
|
virtual void removeMonitor(pqiMonitor *mon);
|
|
|
|
|
|
|
|
/****************** Connections *******************/
|
|
|
|
virtual bool connectAttempt(const std::string &id, struct sockaddr_in &addr,
|
2011-07-13 18:23:40 -04:00
|
|
|
uint32_t &delay, uint32_t &period, uint32_t &type, uint32_t &flags);
|
2011-07-13 10:22:25 -04:00
|
|
|
virtual bool connectResult(const std::string &id, bool success, uint32_t flags, struct sockaddr_in remote_peer_address);
|
|
|
|
virtual bool retryConnect(const std::string &id);
|
|
|
|
|
|
|
|
/* Network Addresses */
|
|
|
|
virtual bool setLocalAddress(struct sockaddr_in addr);
|
|
|
|
virtual struct sockaddr_in getLocalAddress();
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
/******* overloaded from pqiConnectCb *************/
|
|
|
|
virtual void peerStatus(std::string id, const pqiIpAddrSet &addrs,
|
|
|
|
uint32_t type, uint32_t flags, uint32_t source);
|
2011-07-13 18:23:40 -04:00
|
|
|
virtual void peerConnectRequest(std::string id, struct sockaddr_in raddr,
|
|
|
|
uint32_t source, uint32_t flags, uint32_t delay);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
/************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/
|
|
|
|
|
|
|
|
virtual void getFriendList(std::list<std::string> &ssl_peers); // ONLY used by p3peers.cc USE p3PeerMgr instead.
|
|
|
|
virtual int getOnlineCount(); // ONLY used by p3peers.cc
|
|
|
|
virtual int getFriendCount(); // ONLY used by p3serverconfig.cc & p3peers.cc
|
|
|
|
virtual bool getFriendNetStatus(const std::string &id, peerConnectState &state); // ONLY used by p3peers.cc
|
|
|
|
|
|
|
|
virtual void setTunnelConnection(bool b); // ONLY used by p3peermgr.cc & p3peers.cc MOVE => p3PeerMgr
|
|
|
|
virtual bool getTunnelConnection(); // ONLY used by p3peermgr.cc & p3peers.cc MOVE => p3PeerMgr
|
|
|
|
|
|
|
|
/************************************************************************************************/
|
|
|
|
/* Extra IMPL Functions (used by p3PeerMgr, p3NetMgr + Setup) */
|
|
|
|
/************************************************************************************************/
|
|
|
|
|
|
|
|
p3LinkMgrIMPL(p3PeerMgrIMPL *peerMgr, p3NetMgrIMPL *netMgr);
|
|
|
|
|
|
|
|
void tick();
|
|
|
|
|
|
|
|
/* THIS COULD BE ADDED TO INTERFACE */
|
|
|
|
void setFriendVisibility(const std::string &id, bool isVisible);
|
|
|
|
|
|
|
|
/* add/remove friends */
|
|
|
|
int addFriend(const std::string &ssl_id, bool isVisible);
|
|
|
|
int removeFriend(const std::string &ssl_id);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
void printPeerLists(std::ostream &out);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/* THESE CAN PROBABLY BE REMOVED */
|
|
|
|
//bool shutdown(); /* blocking shutdown call */
|
|
|
|
//bool getOwnNetStatus(peerConnectState &state);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
protected:
|
|
|
|
/****************** Internal Interface *******************/
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
/* Internal Functions */
|
|
|
|
void statusTick();
|
|
|
|
|
|
|
|
/* monitor control */
|
|
|
|
void tickMonitors();
|
|
|
|
|
|
|
|
/* connect attempts UDP */
|
2011-07-13 18:23:40 -04:00
|
|
|
bool tryConnectUDP(const std::string &id, struct sockaddr_in &rUdpAddr, uint32_t flags, uint32_t delay);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
/* connect attempts TCP */
|
|
|
|
bool retryConnectTCP(const std::string &id);
|
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
void locked_ConnectAttempt_SpecificAddress(peerConnectState *peer, struct sockaddr_in *remoteAddr);
|
2011-07-09 14:39:34 -04:00
|
|
|
void locked_ConnectAttempt_CurrentAddresses(peerConnectState *peer, struct sockaddr_in *localAddr, struct sockaddr_in *serverAddr);
|
|
|
|
void locked_ConnectAttempt_HistoricalAddresses(peerConnectState *peer, const pqiIpAddrSet &ipAddrs);
|
|
|
|
void locked_ConnectAttempt_AddDynDNS(peerConnectState *peer, std::string dyndns, uint16_t dynPort);
|
2011-07-09 05:36:37 -04:00
|
|
|
void locked_ConnectAttempt_AddTunnel(peerConnectState *peer);
|
|
|
|
|
|
|
|
bool locked_ConnectAttempt_Complete(peerConnectState *peer);
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
bool locked_CheckPotentialAddr(const struct sockaddr_in *addr, time_t age);
|
2011-07-13 18:23:40 -04:00
|
|
|
bool addAddressIfUnique(std::list<peerConnectAddress> &addrList, peerConnectAddress &pca, bool pushFront);
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
// These should have there own Mutex Protection,
|
|
|
|
//p3tunnel *mP3tunnel;
|
|
|
|
DNSResolver *mDNSResolver ;
|
|
|
|
|
2011-07-13 10:22:25 -04:00
|
|
|
p3PeerMgrIMPL *mPeerMgr;
|
|
|
|
p3NetMgrIMPL *mNetMgr;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
RsMutex mLinkMtx; /* protects below */
|
|
|
|
|
|
|
|
uint32_t mRetryPeriod;
|
|
|
|
|
|
|
|
bool mStatusChanged;
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
struct sockaddr_in mLocalAddress;
|
|
|
|
|
2011-07-09 05:36:37 -04:00
|
|
|
std::list<pqiMonitor *> clients;
|
|
|
|
|
|
|
|
bool mAllowTunnelConnection;
|
|
|
|
|
|
|
|
/* external Address determination */
|
|
|
|
//bool mUpnpAddrValid, mStunAddrValid;
|
|
|
|
//struct sockaddr_in mUpnpExtAddr;
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
//peerConnectState mOwnState;
|
2011-07-09 05:36:37 -04:00
|
|
|
|
|
|
|
std::map<std::string, peerConnectState> mFriendList;
|
|
|
|
std::map<std::string, peerConnectState> mOthersList;
|
|
|
|
|
|
|
|
std::list<RsPeerGroupItem *> groupList;
|
|
|
|
uint32_t lastGroupId;
|
|
|
|
|
|
|
|
/* relatively static list of banned ip addresses */
|
|
|
|
std::list<struct in_addr> mBannedIpList;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MRK_PQI_LINK_MANAGER_HEADER
|