Added dynamic local and external ip reconfiguration. I still need to reset connexions when changing ips.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1796 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2009-11-06 22:12:58 +00:00
parent ef78f90225
commit ad96c86f68
4 changed files with 131 additions and 29 deletions

View File

@ -290,12 +290,13 @@ void p3ConnectMgr::netReset()
return; return;
} }
std::cerr << "p3ConnectMgr::netReset() shutdown" << std::endl; std::cerr << "p3ConnectMgr::netReset() shutdown" << std::endl;
shutdown(); /* blocking shutdown call */ shutdown(); /* blocking shutdown call */
// Will initiate a new call for determining the external ip.
mExtAddrFinder->reset() ;
std::cerr << "p3ConnectMgr::netReset() reset NetStatus" << std::endl; std::cerr << "p3ConnectMgr::netReset() reset NetStatus" << std::endl;
{ {
RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/ RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/
@ -2931,6 +2932,69 @@ bool p3ConnectMgr::setVisState(std::string id, uint32_t visState)
/*******************************************************************/ /*******************************************************************/
bool p3ConnectMgr::checkNetAddress()
{
in_addr_t old_in_addr = ownState.localaddr.sin_addr.s_addr;
int old_in_port = ownState.localaddr.sin_port;
{
RsStackMutex stack(connMtx); /****** STACK LOCK MUTEX *******/
// GetPreferredInterface now chooses the best local ilterface for you. So it's the default function to use now.
//
ownState.localaddr.sin_addr = getPreferredInterface() ;
if(ownState.localaddr.sin_addr.s_addr != 0)
{
if (netFlagLocalOk != true)
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::checkNetAddress() changing netFlagOk to true.";
std::cerr << std::endl;
#endif
netFlagLocalOk = true;
IndicateConfigChanged();
}
}
if(isLoopbackNet(&(ownState.localaddr.sin_addr)))
mNetStatus = RS_NET_LOOPBACK;
int port = ntohs(ownState.localaddr.sin_port);
if ((port < PQI_MIN_PORT) || (port > PQI_MAX_PORT))
ownState.localaddr.sin_port = htons(PQI_DEFAULT_PORT);
/* if localaddr = serveraddr, then ensure that the ports
* are the same (modify server)... this mismatch can
* occur when the local port is changed....
*/
if (ownState.localaddr.sin_addr.s_addr == ownState.serveraddr.sin_addr.s_addr)
ownState.serveraddr.sin_port = ownState.localaddr.sin_port;
// ensure that address family is set, otherwise windows Barfs.
ownState.localaddr.sin_family = AF_INET;
ownState.serveraddr.sin_family = AF_INET;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::checkNetAddress() Final Local Address: ";
std::cerr << inet_ntoa(ownState.localaddr.sin_addr);
std::cerr << ":" << ntohs(ownState.localaddr.sin_port);
std::cerr << std::endl;
#endif
}
if ((old_in_addr != ownState.localaddr.sin_addr.s_addr) || (old_in_port != ownState.localaddr.sin_port))
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::checkNetAddress() local address changed, resetting network." << std::endl;
#endif
netReset();
}
return 1;
}
#ifdef TO_REMOVE
bool p3ConnectMgr::checkNetAddress() bool p3ConnectMgr::checkNetAddress()
{ {
std::list<std::string> addrs = getLocalInterfaces(); std::list<std::string> addrs = getLocalInterfaces();
@ -2991,36 +3055,35 @@ bool p3ConnectMgr::checkNetAddress()
mNetStatus = RS_NET_LOOPBACK; mNetStatus = RS_NET_LOOPBACK;
} }
int port = ntohs(ownState.localaddr.sin_port); int port = ntohs(ownState.localaddr.sin_port);
if ((port < PQI_MIN_PORT) || (port > PQI_MAX_PORT)) if ((port < PQI_MIN_PORT) || (port > PQI_MAX_PORT))
{ {
ownState.localaddr.sin_port = htons(PQI_DEFAULT_PORT); ownState.localaddr.sin_port = htons(PQI_DEFAULT_PORT);
} }
/* if localaddr = serveraddr, then ensure that the ports /* if localaddr = serveraddr, then ensure that the ports
* are the same (modify server)... this mismatch can * are the same (modify server)... this mismatch can
* occur when the local port is changed.... * occur when the local port is changed....
*/ */
if (ownState.localaddr.sin_addr.s_addr == if (ownState.localaddr.sin_addr.s_addr == ownState.serveraddr.sin_addr.s_addr)
ownState.serveraddr.sin_addr.s_addr) {
{ ownState.serveraddr.sin_port = ownState.localaddr.sin_port;
ownState.serveraddr.sin_port = }
ownState.localaddr.sin_port;
}
// ensure that address family is set, otherwise windows Barfs. // ensure that address family is set, otherwise windows Barfs.
ownState.localaddr.sin_family = AF_INET; ownState.localaddr.sin_family = AF_INET;
ownState.serveraddr.sin_family = AF_INET; ownState.serveraddr.sin_family = AF_INET;
#ifdef CONN_DEBUG #ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::checkNetAddress() Final Local Address: "; std::cerr << "p3ConnectMgr::checkNetAddress() Final Local Address: ";
std::cerr << inet_ntoa(ownState.localaddr.sin_addr); std::cerr << inet_ntoa(ownState.localaddr.sin_addr);
std::cerr << ":" << ntohs(ownState.localaddr.sin_port); std::cerr << ":" << ntohs(ownState.localaddr.sin_port);
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
} }
if ((old_in_addr != ownState.localaddr.sin_addr.s_addr) || (old_in_port != ownState.localaddr.sin_port)) { if ((old_in_addr != ownState.localaddr.sin_addr.s_addr) || (old_in_port != ownState.localaddr.sin_port))
{
#ifdef CONN_DEBUG #ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::checkNetAddress() local address changed, resetting network." << std::endl; std::cerr << "p3ConnectMgr::checkNetAddress() local address changed, resetting network." << std::endl;
#endif #endif
@ -3029,6 +3092,7 @@ bool p3ConnectMgr::checkNetAddress()
return 1; return 1;
} }
#endif
/************************* p3config functions **********************/ /************************* p3config functions **********************/

View File

@ -179,6 +179,20 @@ std::list<std::string> getLocalInterfaces()
out << std::endl; out << std::endl;
pqioutput(PQL_DEBUG_BASIC, pqinetzone, out.str()); pqioutput(PQL_DEBUG_BASIC, pqinetzone, out.str());
// Now check wether the interface is up and running. If not, we don't use it!!
//
if(ioctl(sock,SIOCGIFFLAGS,&ifreq) != 0)
{
std::cerr << "Could not get flags from interface " << ifptr -> if_name << std::endl ;
continue ;
}
#ifdef NET_DEBUG
std::cout << out.str() ;
std::cout << "flags = " << ifreq.ifr_flags << std::endl ;
#endif
if((ifreq.ifr_flags & IFF_UP) == 0) continue ;
if((ifreq.ifr_flags & IFF_RUNNING) == 0) continue ;
addrs.push_back(astr); addrs.push_back(astr);
} }
} }
@ -533,6 +547,9 @@ struct in_addr getPreferredInterface() // returns best addr.
for(it = addrs.begin(); it != addrs.end(); it++) for(it = addrs.begin(); it != addrs.end(); it++)
{ {
inet_aton((*it).c_str(), &addr); inet_aton((*it).c_str(), &addr);
std::cout << "Examining addr = " << (void*)addr.s_addr << std::endl ;
// for windows silliness (returning 0.0.0.0 as valid addr!). // for windows silliness (returning 0.0.0.0 as valid addr!).
if (addr.s_addr == 0) if (addr.s_addr == 0)
{ {
@ -562,13 +579,16 @@ struct in_addr getPreferredInterface() // returns best addr.
{ {
if (!found_ext) if (!found_ext)
{ {
std::cerr << "Found external address " << (void*)addr.s_addr << std::endl ;
found_ext = true; found_ext = true;
addr_ext = addr; addr_ext = addr;
return addr_ext;
} }
} }
} }
if(found_ext) // external address is best.
return addr_ext ;
if (found_priv) if (found_priv)
return addr_priv; return addr_priv;

View File

@ -231,12 +231,28 @@ bool ExtAddrFinder::hasValidIP(struct sockaddr_in *addr)
return *_found ; return *_found ;
} }
void ExtAddrFinder::reset()
{
while(*_searching)
#ifdef WIN32
Sleep(1000) ;
#else
sleep(1) ;
#endif
RsStackMutex mut(_addrMtx) ;
*_found = false ;
*_searching = false ;
*mFoundTS = time(NULL) - MAX_IP_STORE;
}
ExtAddrFinder::~ExtAddrFinder() ExtAddrFinder::~ExtAddrFinder()
{ {
#ifdef EXTADDRSEARCH_DEBUG #ifdef EXTADDRSEARCH_DEBUG
std::cerr << "ExtAddrFinder: Deleting ExtAddrFinder." << std::endl ; std::cerr << "ExtAddrFinder: Deleting ExtAddrFinder." << std::endl ;
#endif #endif
while(!*_searching) while(*_searching)
#ifdef WIN32 #ifdef WIN32
Sleep(1000) ; Sleep(1000) ;
#else #else

View File

@ -17,6 +17,8 @@ class ExtAddrFinder
void start_request() ; void start_request() ;
void reset() ;
private: private:
friend void* doExtAddrSearch(void *p) ; friend void* doExtAddrSearch(void *p) ;