attempt to fix error in setting external port when guessing external IP

This commit is contained in:
csoler 2021-02-04 22:22:21 +01:00
parent 6c83b5cd27
commit 251d022ae1
2 changed files with 49 additions and 38 deletions

View File

@ -379,8 +379,10 @@ void p3NetMgrIMPL::netStartup()
break; break;
case RS_NET_MODE_TRY_LOOPBACK: case RS_NET_MODE_TRY_LOOPBACK:
std::cerr << "p3NetMgrIMPL::netStartup() TRY_LOOPBACK mode"; #ifdef NETMGR_DEBUG_RESET
std::cerr << "p3NetMgrIMPL::netStartup() TRY_LOOPBACK mode";
std::cerr << std::endl; std::cerr << std::endl;
#endif
mNetMode |= RS_NET_MODE_HIDDEN; mNetMode |= RS_NET_MODE_HIDDEN;
mNetStatus = RS_NET_LOOPBACK; mNetStatus = RS_NET_LOOPBACK;
break; break;
@ -468,8 +470,10 @@ void p3NetMgrIMPL::netStatusTick()
if(netStatus <= RS_NET_UPNP_SETUP && mUseExtAddrFinder) if(netStatus <= RS_NET_UPNP_SETUP && mUseExtAddrFinder)
{ {
sockaddr_storage tmpip; sockaddr_storage tmpip = mLocalAddr; // copies local port and correctly inits the IP family
sockaddr_storage_setport(tmpip, guessNewExtPort()); #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "Asking ExtAddrFinder for IP. Initializing port with " << sockaddr_storage_port(tmpip) << std::endl;
#endif
if(mExtAddrFinder->hasValidIP(tmpip) && sockaddr_storage_ipv6_to_ipv4(tmpip) && !sockaddr_storage_same(tmpip,mExtAddr)) if(mExtAddrFinder->hasValidIP(tmpip) && sockaddr_storage_ipv6_to_ipv4(tmpip) && !sockaddr_storage_same(tmpip,mExtAddr))
{ {
@ -743,7 +747,10 @@ void p3NetMgrIMPL::netExtCheck()
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() checking ExtAddrFinder" << std::endl; std::cerr << "p3NetMgrIMPL::netExtCheck() checking ExtAddrFinder" << std::endl;
#endif #endif
bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip); sockaddr_storage tmpip = mLocalAddr; // copies local port and correctly inits the IP family
bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip);
if (extFinderOk && sockaddr_storage_ipv6_to_ipv4(tmpip)) if (extFinderOk && sockaddr_storage_ipv6_to_ipv4(tmpip))
{ {
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
@ -783,11 +790,10 @@ void p3NetMgrIMPL::netExtCheck()
uint8_t isstable; // unused uint8_t isstable; // unused
sockaddr_storage tmpaddr; sockaddr_storage tmpaddr;
if ( mPeerMgr->getExtAddressReportedByFriends(tmpaddr, isstable) && if ( mPeerMgr->getExtAddressReportedByFriends(tmpaddr, isstable) && sockaddr_storage_ipv6_to_ipv4(tmpaddr) )
sockaddr_storage_ipv6_to_ipv4(tmpaddr) )
{ {
#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder" << std::endl; std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied by friends" << std::endl;
#endif #endif
sockaddr_storage_setport(tmpaddr, guessNewExtPort()); sockaddr_storage_setport(tmpaddr, guessNewExtPort());
@ -988,7 +994,7 @@ void p3NetMgrIMPL::netExtCheck()
bool p3NetMgrIMPL::checkNetAddress() bool p3NetMgrIMPL::checkNetAddress()
{ {
bool addrChanged = false; bool addrChanged = false;
bool validAddr = false; bool validAddr = false;
sockaddr_storage prefAddr; sockaddr_storage prefAddr;
sockaddr_storage oldAddr; sockaddr_storage oldAddr;
@ -1087,25 +1093,27 @@ bool p3NetMgrIMPL::checkNetAddress()
/* Using same port as external may make some NAT happier */ /* Using same port as external may make some NAT happier */
uint16_t port = sockaddr_storage_port(mExtAddr); uint16_t port = sockaddr_storage_port(mExtAddr);
/* Avoid to automatically set a local port to a reserved one < 1024 /* Avoid to automatically set a local port to a reserved one < 1024
* that needs special permissions or root access. * that needs special permissions or root access.
* This do not impede the user to set a reserved port manually, * This do not impede the user to set a reserved port manually,
* which make sense in some cases. */ * which make sense in some cases. */
while (port < 1025)
std::cerr << __PRETTY_FUNCTION__ << " local port is 0. Ext port is " << port ;
while (port < 1025)
port = static_cast<uint16_t>(RsRandom::random_u32()); port = static_cast<uint16_t>(RsRandom::random_u32());
sockaddr_storage_setport(mLocalAddr, htons(port)); std::cerr << " new ext port is " << port << ": using these for local/ext ports" << std::endl;
addrChanged = true;
RsWarn() << __PRETTY_FUNCTION__ << " local port was 0, corrected " sockaddr_storage_setport(mLocalAddr, port);
<<"to: " << port << std::endl; sockaddr_storage_setport(mExtAddr, port); // this accounts for when the port was updated
addrChanged = true;
} }
} // RS_STACK_MUTEX(mNetMtx); } // RS_STACK_MUTEX(mNetMtx);
if (addrChanged) if (addrChanged)
{ {
RsInfo() << __PRETTY_FUNCTION__ << " local address changed, resetting" RsInfo() << __PRETTY_FUNCTION__ << " local address changed, resetting network." << std::endl;
<<" network." << std::endl;
if(rsEvents) if(rsEvents)
{ {
@ -1115,10 +1123,10 @@ bool p3NetMgrIMPL::checkNetAddress()
rsEvents->postEvent(ev); rsEvents->postEvent(ev);
} }
if (mPeerMgr) mPeerMgr->UpdateOwnAddress(mLocalAddr, mExtAddr); if (mPeerMgr) mPeerMgr->UpdateOwnAddress(mLocalAddr, mExtAddr);
netReset(); netReset();
} }
return true; return true;
} }
@ -1771,19 +1779,19 @@ void p3NetMgrIMPL::updateNetStateBox_temporal()
{ {
RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/
uint32_t netstate = mNetStateBox.getNetStateMode(); auto netstate = mNetStateBox.getNetStateMode();
uint32_t netMode = mNetStateBox.getNetworkMode(); auto netMode = mNetStateBox.getNetworkMode();
uint32_t natType = mNetStateBox.getNatTypeMode(); auto natType = mNetStateBox.getNatTypeMode();
uint32_t natHole = mNetStateBox.getNatHoleMode(); auto natHole = mNetStateBox.getNatHoleMode();
uint32_t connect = mNetStateBox.getConnectModes(); auto connect = mNetStateBox.getConnectModes();
#ifdef SUSPENDED
auto netstatestr = NetStateNetStateString(netstate);
auto connectstr = NetStateConnectModesString(connect);
auto natholestr = NetStateNatHoleString(natHole);
auto nattypestr = NetStateNatTypeString(natType);
auto netmodestr = NetStateNetworkModeString(netMode);
std::string netstatestr = NetStateNetStateString(netstate); std::cerr << "p3NetMgrIMPL::updateNetStateBox_temporal() NetStateBox Thinking";
std::string connectstr = NetStateConnectModesString(connect);
std::string natholestr = NetStateNatHoleString(natHole);
std::string nattypestr = NetStateNatTypeString(natType);
std::string netmodestr = NetStateNetworkModeString(netMode);
std::cerr << "p3NetMgrIMPL::updateNetStateBox_temporal() NetStateBox Thinking";
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << "\tNetState: " << netstatestr; std::cerr << "\tNetState: " << netstatestr;
std::cerr << std::endl; std::cerr << std::endl;
@ -1794,7 +1802,8 @@ void p3NetMgrIMPL::updateNetStateBox_temporal()
std::cerr << "\tNatHole: " << natholestr; std::cerr << "\tNatHole: " << natholestr;
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << "\tNatType: " << nattypestr; std::cerr << "\tNatType: " << nattypestr;
std::cerr << std::endl; std::cerr << std::endl;
#endif
} }
#endif #endif
@ -1826,8 +1835,8 @@ void p3NetMgrIMPL::updateNatSetting()
#ifdef NETMGR_DEBUG_STATEBOX #ifdef NETMGR_DEBUG_STATEBOX
std::cerr << "p3NetMgrIMPL::updateNetStateBox_temporal() NatType Change!"; std::cerr << "p3NetMgrIMPL::updateNetStateBox_temporal() NatType Change!";
std::cerr << "\tNatType: " << NetStateNatTypeString(natType); // std::cerr << "\tNatType: " << NetStateNatTypeString(natType);
std::cerr << "\tNatHole: " << NetStateNatHoleString(natHole); // std::cerr << "\tNatHole: "k << NetStateNatHoleString(natHole);
std::cerr << std::endl; std::cerr << std::endl;
#endif #endif
@ -1964,7 +1973,9 @@ void p3NetMgrIMPL::updateNetStateBox_startup()
/* ExtAddrFinder */ /* ExtAddrFinder */
if (mUseExtAddrFinder) if (mUseExtAddrFinder)
{ {
bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip); tmpip = mLocalAddr;
bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip);
if (extFinderOk) if (extFinderOk)
{ {
sockaddr_storage_setport(tmpip, guessNewExtPort()); sockaddr_storage_setport(tmpip, guessNewExtPort());

View File

@ -250,7 +250,7 @@ bool ExtAddrFinder::hasValidIP(struct sockaddr_storage &addr)
#ifdef EXTADDRSEARCH_DEBUG #ifdef EXTADDRSEARCH_DEBUG
std::cerr << "ExtAddrFinder: Has stored ip: responding with this ip." << std::endl ; std::cerr << "ExtAddrFinder: Has stored ip: responding with this ip." << std::endl ;
#endif #endif
addr = mAddr; sockaddr_storage_copyip(addr,mAddr); // just copy the IP so we dont erase the port.
} }
} }
rstime_t delta; rstime_t delta;