2008-01-25 01:36:40 -05:00
/*
* libretroshare / src / pqi : p3connmgr . h
*
* 3 P / PQI network interface for RetroShare .
*
* Copyright 2007 - 2008 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_CONNECTION_MANAGER_HEADER
# define MRK_PQI_CONNECTION_MANAGER_HEADER
# include "pqi/pqimonitor.h"
# include "pqi/p3authmgr.h"
2009-11-11 11:43:51 -05:00
# include "serialiser/rsconfigitems.h"
2008-01-25 01:36:40 -05:00
2008-08-16 11:02:24 -04:00
//#include "pqi/p3dhtmgr.h"
//#include "pqi/p3upnpmgr.h"
# include "pqi/pqiassist.h"
2009-12-13 16:59:26 -05:00
# include "services/p3tunnel.h"
2008-08-16 11:02:24 -04:00
2008-01-25 01:36:40 -05:00
# include "pqi/p3cfgmgr.h"
# include "util/rsthreads.h"
2009-04-03 05:21:20 -04:00
class ExtAddrFinder ;
2008-02-07 11:18:34 -05:00
/* RS_VIS_STATE_XXXX
* determines how public this peer wants to be . . .
*
* STD = advertise to Peers / DHT checking etc
* GRAY = share with friends / but not DHT
* DARK = hidden from all
* BROWN ? = hidden from friends / but on DHT
*/
2008-01-25 01:36:40 -05:00
const uint32_t RS_VIS_STATE_NODISC = 0x0001 ;
const uint32_t RS_VIS_STATE_NODHT = 0x0002 ;
const uint32_t RS_VIS_STATE_STD = 0x0000 ;
const uint32_t RS_VIS_STATE_GRAY = RS_VIS_STATE_NODHT ;
const uint32_t RS_VIS_STATE_DARK = RS_VIS_STATE_NODISC | RS_VIS_STATE_NODHT ;
const uint32_t RS_VIS_STATE_BROWN = RS_VIS_STATE_NODISC ;
2008-02-07 11:18:34 -05:00
2008-02-26 11:14:13 -05:00
/* Startup Modes (confirmed later) */
const uint32_t RS_NET_MODE_TRYMODE = 0x00f0 ;
const uint32_t RS_NET_MODE_TRY_EXT = 0x0010 ;
const uint32_t RS_NET_MODE_TRY_UPNP = 0x0020 ;
const uint32_t RS_NET_MODE_TRY_UDP = 0x0040 ;
/* Actual State */
2008-02-28 10:58:54 -05:00
const uint32_t RS_NET_MODE_ACTUAL = 0x000f ;
2008-02-26 11:14:13 -05:00
2008-02-28 10:58:54 -05:00
const uint32_t RS_NET_MODE_UNKNOWN = 0x0000 ;
const uint32_t RS_NET_MODE_EXT = 0x0001 ;
const uint32_t RS_NET_MODE_UPNP = 0x0002 ;
const uint32_t RS_NET_MODE_UDP = 0x0004 ;
const uint32_t RS_NET_MODE_UNREACHABLE = 0x0008 ;
2008-01-25 01:36:40 -05:00
/* order of attempts ... */
const uint32_t RS_NET_CONN_TCP_ALL = 0x000f ;
const uint32_t RS_NET_CONN_UDP_ALL = 0x00f0 ;
2009-12-13 16:59:26 -05:00
const uint32_t RS_NET_CONN_TUNNEL = 0x0f00 ;
2008-01-25 01:36:40 -05:00
const uint32_t RS_NET_CONN_TCP_LOCAL = 0x0001 ;
const uint32_t RS_NET_CONN_TCP_EXTERNAL = 0x0002 ;
2009-11-11 11:45:46 -05:00
const uint32_t RS_NET_CONN_TCP_UNKNOW_TOPOLOGY = 0x0003 ;
2009-12-13 16:59:26 -05:00
const uint32_t RS_NET_CONN_UDP = 0x0010 ;
2008-10-18 12:02:06 -04:00
/* 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 */
2008-01-25 01:36:40 -05:00
2008-02-03 01:29:02 -05:00
/* flags of peerStatus */
const uint32_t RS_NET_FLAGS_USE_DISC = 0x0001 ;
2008-12-23 11:23:54 -05:00
const uint32_t RS_NET_FLAGS_USE_DHT = 0x0002 ;
const uint32_t RS_NET_FLAGS_ONLINE = 0x0004 ;
2008-02-03 01:29:02 -05:00
const uint32_t RS_NET_FLAGS_EXTERNAL_ADDR = 0x0008 ;
2008-02-28 10:58:54 -05:00
const uint32_t RS_NET_FLAGS_STABLE_UDP = 0x0010 ;
2008-12-23 11:23:54 -05:00
const uint32_t RS_NET_FLAGS_TRUSTS_ME = 0x0020 ;
2008-02-03 01:29:02 -05:00
2008-03-26 11:35:09 -04:00
const uint32_t RS_TCP_STD_TIMEOUT_PERIOD = 5 ; /* 5 seconds! */
2008-01-25 01:36:40 -05:00
class peerAddrInfo
{
public :
peerAddrInfo ( ) ; /* init */
bool found ;
uint32_t type ;
struct sockaddr_in laddr , raddr ;
time_t ts ;
} ;
class peerConnectAddress
{
public :
peerConnectAddress ( ) ; /* init */
struct sockaddr_in addr ;
2008-02-26 21:32:20 -05:00
uint32_t delay ; /* to stop simultaneous connects */
uint32_t period ; /* UDP only */
2008-01-25 01:36:40 -05:00
uint32_t type ;
time_t ts ;
} ;
class peerConnectState
{
2009-11-11 11:47:10 -05:00
private :
std : : list < IpAddressTimed > ipAddressList ;
2008-01-25 01:36:40 -05:00
public :
peerConnectState ( ) ; /* init */
std : : string id ;
uint32_t netMode ; /* EXT / UPNP / UDP / INVALID */
2008-02-07 11:18:34 -05:00
uint32_t visState ; /* STD, GRAY, DARK */
2008-01-25 01:36:40 -05:00
2009-11-11 11:45:46 -05:00
//used to store friends ip lists
2009-11-11 11:47:10 -05:00
void sortIpAddressListBySeenTime ( ) ; //Sort the ip list ordering by seen time
void purgeIpAddressList ( ) ; //purge old addresses to keep a small list
std : : list < IpAddressTimed > getIpAddressList ( ) ; //return the sorted ant purged list.
void updateIpAddressList ( IpAddressTimed ipTimed ) ;
void updateIpAddressList ( std : : list < IpAddressTimed > ipTimedList ) ;
void printIpAddressList ( ) ;
2009-11-11 11:43:51 -05:00
2009-12-15 11:56:58 -05:00
static void sortIpAddressListBySeenTime ( std : : list < IpAddressTimed > & ipTimedList ) ; //Sort the ip list ordering by seen time
static bool compare_seen_time ( IpAddressTimed first , IpAddressTimed second ) ;
static bool is_same_address ( IpAddressTimed first , IpAddressTimed second ) ;
static void printIpAddressList ( std : : list < IpAddressTimed > ipTimedList ) ;
static bool extractExtAddress ( std : : list < IpAddressTimed > ipAddressList , IpAddressTimed & resultAddress ) ; //extract the last seen external address from the list
//used to store current ip (for config and connection management)
2009-11-11 11:43:51 -05:00
struct sockaddr_in currentlocaladdr ; /* Mandatory */
struct sockaddr_in currentserveraddr ; /* Mandatory */
2008-01-25 01:36:40 -05:00
2008-02-07 11:18:34 -05:00
time_t lastcontact ;
2008-01-25 01:36:40 -05:00
2008-02-07 11:18:34 -05:00
/***** Below here not stored permanently *****/
2008-03-26 11:35:09 -04:00
uint32_t connecttype ; // RS_NET_CONN_TCP_ALL / RS_NET_CONN_UDP_ALL
2008-03-02 09:25:59 -05:00
time_t lastavailable ;
time_t lastattempt ;
2008-01-25 01:36:40 -05:00
2008-02-07 11:18:34 -05:00
std : : string name ;
uint32_t state ;
uint32_t actions ;
2008-01-25 01:36:40 -05:00
uint32_t source ; /* most current source */
peerAddrInfo dht ;
peerAddrInfo disc ;
peerAddrInfo peer ;
/* a list of connect attempts to make (in order) */
bool inConnAttempt ;
2009-11-11 11:43:51 -05:00
peerConnectAddress currentConnAddrAttempt ;
2008-01-25 01:36:40 -05:00
std : : list < peerConnectAddress > connAddrs ;
} ;
2009-08-04 19:22:44 -04:00
std : : string textPeerConnectState ( peerConnectState & state ) ;
2008-01-25 01:36:40 -05:00
class p3ConnectMgr : public pqiConnectCb , public p3Config
{
public :
p3ConnectMgr ( p3AuthMgr * authMgr ) ;
void tick ( ) ;
/*************** Setup ***************************/
2008-08-16 11:02:24 -04:00
void addNetAssistConnect ( uint32_t type , pqiNetAssistConnect * ) ;
void addNetAssistFirewall ( uint32_t type , pqiNetAssistFirewall * ) ;
2008-01-25 01:36:40 -05:00
bool checkNetAddress ( ) ; /* check our address is sensible */
2009-08-04 19:22:44 -04:00
void addNetListener ( pqiNetListener * listener ) ;
2008-01-25 01:36:40 -05:00
/*************** External Control ****************/
2008-04-09 08:54:15 -04:00
bool shutdown ( ) ; /* blocking shutdown call */
2008-01-25 01:36:40 -05:00
bool retryConnect ( std : : string id ) ;
bool getUPnPState ( ) ;
bool getUPnPEnabled ( ) ;
bool getDHTEnabled ( ) ;
2009-04-05 09:04:18 -04:00
bool getIPServersEnabled ( ) { return use_extr_addr_finder ; }
void setIPServersEnabled ( bool b ) ;
void getIPServersList ( std : : list < std : : string > & ip_servers ) ;
2009-12-13 16:59:26 -05:00
void setTunnelConnection ( bool b ) ;
bool getTunnelConnection ( ) { return allow_tunnel_connection ; }
2009-04-05 09:04:18 -04:00
2009-10-29 20:41:24 -04:00
bool getNetStatusLocalOk ( ) ;
2008-10-18 12:02:06 -04:00
bool getNetStatusUpnpOk ( ) ;
bool getNetStatusDhtOk ( ) ;
2009-10-29 20:41:24 -04:00
bool getNetStatusStunOk ( ) ;
bool getNetStatusExtraAddressCheckOk ( ) ;
2008-10-18 12:02:06 -04:00
2008-02-07 11:18:34 -05:00
void setOwnNetConfig ( uint32_t netMode , uint32_t visState ) ;
2008-01-25 01:36:40 -05:00
bool setLocalAddress ( std : : string id , struct sockaddr_in addr ) ;
bool setExtAddress ( std : : string id , struct sockaddr_in addr ) ;
2009-11-11 11:44:51 -05:00
bool setAddressList ( std : : string id , std : : list < IpAddressTimed > IpAddressTimedList ) ;
2008-01-25 01:36:40 -05:00
bool setNetworkMode ( std : : string id , uint32_t netMode ) ;
2008-02-07 11:18:34 -05:00
bool setVisState ( std : : string id , uint32_t visState ) ;
2008-01-25 01:36:40 -05:00
/* add/remove friends */
2008-02-07 11:18:34 -05:00
bool addFriend ( std : : string id , uint32_t netMode = RS_NET_MODE_UDP ,
uint32_t visState = RS_VIS_STATE_STD , time_t lastContact = 0 ) ;
2008-01-25 01:36:40 -05:00
bool removeFriend ( std : : string ) ;
bool addNeighbour ( std : : string ) ;
/*************** External Control ****************/
/* access to network details (called through Monitor) */
const std : : string getOwnId ( ) ;
bool getOwnNetStatus ( peerConnectState & state ) ;
bool isFriend ( std : : string id ) ;
2008-08-17 11:23:11 -04:00
bool isOnline ( std : : string id ) ;
2008-01-25 01:36:40 -05:00
bool getFriendNetStatus ( std : : string id , peerConnectState & state ) ;
bool getOthersNetStatus ( std : : string id , peerConnectState & state ) ;
void getOnlineList ( std : : list < std : : string > & peers ) ;
void getFriendList ( std : : list < std : : string > & peers ) ;
void getOthersList ( std : : list < std : : string > & peers ) ;
/**************** handle monitors *****************/
void addMonitor ( pqiMonitor * mon ) ;
void removeMonitor ( pqiMonitor * mon ) ;
/******* overloaded from pqiConnectCb *************/
virtual void peerStatus ( std : : string id ,
2009-11-11 11:49:11 -05:00
struct sockaddr_in laddr , struct sockaddr_in raddr , std : : list < IpAddressTimed > ipAddressList ,
2008-02-03 01:29:02 -05:00
uint32_t type , uint32_t flags , uint32_t source ) ;
2008-02-26 21:32:20 -05:00
virtual void peerConnectRequest ( std : : string id ,
struct sockaddr_in raddr , uint32_t source ) ;
2008-02-25 18:56:23 -05:00
virtual void stunStatus ( std : : string id , struct sockaddr_in raddr , uint32_t type , uint32_t flags ) ;
2008-01-25 01:36:40 -05:00
/****************** Connections *******************/
2008-02-26 21:32:20 -05:00
bool connectAttempt ( std : : string id , struct sockaddr_in & addr ,
uint32_t & delay , uint32_t & period , uint32_t & type ) ;
2009-12-14 13:11:19 -05:00
bool connectResult ( std : : string id , bool success , uint32_t flags , struct sockaddr_in remote_peer_address ) ;
2009-12-13 16:59:26 -05:00
bool doNextAttempt ( std : : string id ) ;
p3tunnel * getP3tunnel ( ) ;
void setP3tunnel ( p3tunnel * p3tun ) ;
2008-01-25 01:36:40 -05:00
protected :
2008-08-16 11:02:24 -04:00
/****************** Internal Interface *******************/
virtual bool enableNetAssistFirewall ( bool on ) ;
virtual bool netAssistFirewallEnabled ( ) ;
virtual bool netAssistFirewallActive ( ) ;
virtual bool netAssistFirewallShutdown ( ) ;
virtual bool enableNetAssistConnect ( bool on ) ;
virtual bool netAssistConnectEnabled ( ) ;
virtual bool netAssistConnectActive ( ) ;
virtual bool netAssistConnectShutdown ( ) ;
2009-12-14 13:11:19 -05:00
/* Assist Firewall */
2008-08-16 11:02:24 -04:00
bool netAssistExtAddress ( struct sockaddr_in & extAddr ) ;
bool netAssistFirewallPorts ( uint16_t iport , uint16_t eport ) ;
/* Assist Connect */
virtual bool netAssistFriend ( std : : string id , bool on ) ;
virtual bool netAssistAddStun ( std : : string id ) ;
virtual bool netAssistStun ( bool on ) ;
virtual bool netAssistNotify ( std : : string id ) ;
virtual bool netAssistSetAddress ( struct sockaddr_in & laddr ,
struct sockaddr_in & eaddr ,
uint32_t mode ) ;
2008-01-25 01:36:40 -05:00
/* Internal Functions */
2009-08-04 19:22:44 -04:00
void netReset ( ) ;
2008-03-02 09:25:59 -05:00
void statusTick ( ) ;
2008-01-25 01:36:40 -05:00
void netTick ( ) ;
void netStartup ( ) ;
/* startup the bits */
void netDhtInit ( ) ;
void netUdpInit ( ) ;
void netStunInit ( ) ;
2008-10-18 12:02:06 -04:00
void netStatusReset ( ) ;
2008-01-25 01:36:40 -05:00
void netInit ( ) ;
void netExtInit ( ) ;
void netExtCheck ( ) ;
void netUpnpInit ( ) ;
void netUpnpCheck ( ) ;
2009-11-12 17:19:45 -05:00
void netExtFinderAddressCheck ( ) ;
2008-03-02 09:25:59 -05:00
void netUnreachableCheck ( ) ;
2008-01-25 01:36:40 -05:00
/* Udp / Stun functions */
void udpStunPeer ( std : : string id , struct sockaddr_in & addr ) ;
void stunInit ( ) ;
bool stunCheck ( ) ;
void stunCollect ( std : : string id , struct sockaddr_in addr , uint32_t flags ) ;
2008-03-03 09:01:52 -05:00
bool addBootstrapStunPeers ( ) ;
2008-01-25 01:36:40 -05:00
2009-10-29 20:41:24 -04:00
void networkConsistencyCheck ( ) ;
2008-01-25 01:36:40 -05:00
/* monitor control */
void tickMonitors ( ) ;
2008-03-02 09:25:59 -05:00
/* connect attempts */
bool retryConnectTCP ( std : : string id ) ;
bool retryConnectNotify ( std : : string id ) ;
2008-01-25 01:36:40 -05:00
2009-10-29 20:41:24 -04:00
bool getUpnpExtAddress ( struct sockaddr_in & addr ) ;
bool getStunExtAddress ( struct sockaddr_in & addr ) ;
bool getExtFinderExtAddress ( struct sockaddr_in & addr ) ;
2008-01-25 01:36:40 -05:00
/* temporary for testing */
2009-04-09 17:13:48 -04:00
//virtual void loadConfiguration() { return; }
2008-01-25 01:36:40 -05:00
protected :
/*****************************************************************/
/*********************** p3config ******************************/
/* Key Functions to be overloaded for Full Configuration */
virtual RsSerialiser * setupSerialiser ( ) ;
virtual std : : list < RsItem * > saveList ( bool & cleanup ) ;
virtual bool loadList ( std : : list < RsItem * > load ) ;
/*****************************************************************/
#if 0
void setupOwnNetConfig ( RsPeerConfigItem * item ) ;
void addPeer ( RsPeerConfigItem * item ) ;
# endif
private :
p3AuthMgr * mAuthMgr ;
2008-08-16 11:02:24 -04:00
2009-12-13 16:59:26 -05:00
p3tunnel * mP3tunnel ;
2008-08-16 11:02:24 -04:00
std : : map < uint32_t , pqiNetAssistFirewall * > mFwAgents ;
std : : map < uint32_t , pqiNetAssistConnect * > mDhts ;
2008-01-25 01:36:40 -05:00
RsMutex connMtx ; /* protects below */
2009-08-04 19:22:44 -04:00
std : : list < pqiNetListener * > mNetListeners ;
2008-01-25 01:36:40 -05:00
time_t mNetInitTS ;
uint32_t mNetStatus ;
2008-03-05 11:32:18 -05:00
2008-01-25 01:36:40 -05:00
uint32_t mStunStatus ;
2008-03-05 11:32:18 -05:00
uint32_t mStunFound ;
bool mStunMoreRequired ;
2008-01-25 01:36:40 -05:00
bool mStatusChanged ;
std : : list < pqiMonitor * > clients ;
2009-04-03 05:21:20 -04:00
ExtAddrFinder * mExtAddrFinder ;
2009-04-05 09:04:18 -04:00
bool use_extr_addr_finder ;
2009-12-13 16:59:26 -05:00
bool allow_tunnel_connection ;
2008-01-25 01:36:40 -05:00
/* external Address determination */
2009-10-29 20:41:24 -04:00
//bool mUpnpAddrValid, mStunAddrValid;
//bool mStunAddrStable;
//struct sockaddr_in mUpnpExtAddr;
2008-01-25 01:36:40 -05:00
struct sockaddr_in mStunExtAddr ;
2008-10-18 12:02:06 -04:00
/* network status flags (read by rsiface) */
2009-10-29 20:41:24 -04:00
bool netFlagLocalOk ;
2008-10-18 12:02:06 -04:00
bool netFlagUpnpOk ;
bool netFlagDhtOk ;
2009-10-29 20:41:24 -04:00
bool netFlagStunOk ;
bool netFlagExtraAddressCheckOk ;
2008-10-18 12:02:06 -04:00
2009-10-29 20:41:24 -04:00
/* old network status flags in order to detect changes */
bool oldnetFlagLocalOk ;
bool oldnetFlagUpnpOk ;
bool oldnetFlagDhtOk ;
bool oldnetFlagStunOk ;
bool oldnetFlagExtraAddressCheckOk ;
2009-10-29 20:35:59 -04:00
2008-01-25 01:36:40 -05:00
protected :
void addPeer ( std : : string id , std : : string name ) ; /* tmp fn */
peerConnectState ownState ;
std : : list < std : : string > mStunList ;
std : : map < std : : string , peerConnectState > mFriendList ;
std : : map < std : : string , peerConnectState > mOthersList ;
} ;
# endif // MRK_PQI_CONNECTION_MANAGER_HEADER