From ad96c86f68ce29e11bc002d8e121e2c1b6cae3de Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 6 Nov 2009 22:12:58 +0000 Subject: [PATCH] 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 --- libretroshare/src/pqi/p3connmgr.cc | 118 +++++++++++++++----- libretroshare/src/pqi/pqinetwork.cc | 22 +++- libretroshare/src/tcponudp/extaddrfinder.cc | 18 ++- libretroshare/src/tcponudp/extaddrfinder.h | 2 + 4 files changed, 131 insertions(+), 29 deletions(-) diff --git a/libretroshare/src/pqi/p3connmgr.cc b/libretroshare/src/pqi/p3connmgr.cc index 2ff28c415..6f81106c1 100644 --- a/libretroshare/src/pqi/p3connmgr.cc +++ b/libretroshare/src/pqi/p3connmgr.cc @@ -290,12 +290,13 @@ void p3ConnectMgr::netReset() return; } - - std::cerr << "p3ConnectMgr::netReset() shutdown" << std::endl; shutdown(); /* blocking shutdown call */ + // Will initiate a new call for determining the external ip. + mExtAddrFinder->reset() ; + std::cerr << "p3ConnectMgr::netReset() reset NetStatus" << std::endl; { 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() { std::list addrs = getLocalInterfaces(); @@ -2991,36 +3055,35 @@ bool p3ConnectMgr::checkNetAddress() 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); - } + 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; - } + /* 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; + // 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 +#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)) { + 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 @@ -3029,6 +3092,7 @@ bool p3ConnectMgr::checkNetAddress() return 1; } +#endif /************************* p3config functions **********************/ diff --git a/libretroshare/src/pqi/pqinetwork.cc b/libretroshare/src/pqi/pqinetwork.cc index 3b0c4b2e7..e69c2a5c7 100644 --- a/libretroshare/src/pqi/pqinetwork.cc +++ b/libretroshare/src/pqi/pqinetwork.cc @@ -179,6 +179,20 @@ std::list getLocalInterfaces() out << std::endl; 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); } } @@ -533,6 +547,9 @@ struct in_addr getPreferredInterface() // returns best addr. for(it = addrs.begin(); it != addrs.end(); it++) { 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!). if (addr.s_addr == 0) { @@ -562,13 +579,16 @@ struct in_addr getPreferredInterface() // returns best addr. { if (!found_ext) { + std::cerr << "Found external address " << (void*)addr.s_addr << std::endl ; found_ext = true; addr_ext = addr; - return addr_ext; } } } + if(found_ext) // external address is best. + return addr_ext ; + if (found_priv) return addr_priv; diff --git a/libretroshare/src/tcponudp/extaddrfinder.cc b/libretroshare/src/tcponudp/extaddrfinder.cc index 62be9524b..649be1e98 100644 --- a/libretroshare/src/tcponudp/extaddrfinder.cc +++ b/libretroshare/src/tcponudp/extaddrfinder.cc @@ -231,12 +231,28 @@ bool ExtAddrFinder::hasValidIP(struct sockaddr_in *addr) 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() { #ifdef EXTADDRSEARCH_DEBUG std::cerr << "ExtAddrFinder: Deleting ExtAddrFinder." << std::endl ; #endif - while(!*_searching) + while(*_searching) #ifdef WIN32 Sleep(1000) ; #else diff --git a/libretroshare/src/tcponudp/extaddrfinder.h b/libretroshare/src/tcponudp/extaddrfinder.h index fc1b3f52e..55b0cd8d6 100644 --- a/libretroshare/src/tcponudp/extaddrfinder.h +++ b/libretroshare/src/tcponudp/extaddrfinder.h @@ -17,6 +17,8 @@ class ExtAddrFinder void start_request() ; + void reset() ; + private: friend void* doExtAddrSearch(void *p) ;