Make port mapper names even more unique, and trial bind before binding TCP now which should work on Linux and others.

This commit is contained in:
Adam Ierymenko 2016-04-07 09:51:14 -07:00
parent 9085a04b16
commit 569de22481

View File

@ -571,9 +571,10 @@ public:
if (port == 0) { if (port == 0) {
unsigned int randp = 0; unsigned int randp = 0;
Utils::getSecureRandom(&randp,sizeof(randp)); Utils::getSecureRandom(&randp,sizeof(randp));
port = 40000 + (randp % 25500); port = 20000 + (randp % 45500);
} }
if (_trialBind(port)) {
struct sockaddr_in in4; struct sockaddr_in in4;
memset(&in4,0,sizeof(in4)); memset(&in4,0,sizeof(in4));
in4.sin_family = AF_INET; in4.sin_family = AF_INET;
@ -600,6 +601,9 @@ public:
_phy.close(_v6TcpControlSocket,false); _phy.close(_v6TcpControlSocket,false);
port = 0; port = 0;
} }
} else {
port = 0;
}
} }
if (_ports[0] == 0) if (_ports[0] == 0)
@ -675,7 +679,7 @@ public:
// private address port number. // private address port number.
_ports[1] = 20000 + ((unsigned int)_node->address() % 45500); _ports[1] = 20000 + ((unsigned int)_node->address() % 45500);
for(int i=0;;++i) { for(int i=0;;++i) {
if (i > 256) { if (i > 1000) {
_ports[1] = 0; _ports[1] = 0;
break; break;
} else if (++_ports[1] >= 65536) { } else if (++_ports[1] >= 65536) {
@ -689,9 +693,10 @@ public:
// If we're running uPnP/NAT-PMP, bind a *third* port for that. We can't // If we're running uPnP/NAT-PMP, bind a *third* port for that. We can't
// use the other two ports for that because some NATs do really funky // use the other two ports for that because some NATs do really funky
// stuff with ports that are explicitly mapped that breaks things. // stuff with ports that are explicitly mapped that breaks things.
if (_ports[1]) {
_ports[2] = _ports[1]; _ports[2] = _ports[1];
for(int i=0;;++i) { for(int i=0;;++i) {
if (i > 256) { if (i > 1000) {
_ports[2] = 0; _ports[2] = 0;
break; break;
} else if (++_ports[2] >= 65536) { } else if (++_ports[2] >= 65536) {
@ -702,9 +707,10 @@ public:
} }
if (_ports[2]) { if (_ports[2]) {
char uniqueName[64]; char uniqueName[64];
Utils::snprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx",_node->address()); Utils::snprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]);
_portMapper = new PortMapper(_ports[2],uniqueName); _portMapper = new PortMapper(_ports[2],uniqueName);
} }
}
#endif #endif
for(int i=0;i<3;++i) for(int i=0;i<3;++i)