From 587804cab7d06ff28acd567745dc06205482b156 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Apr 2016 13:32:28 +0200 Subject: [PATCH] Merge with develop --- code/nel/include/nel/net/inet_address.h | 2 +- code/nel/src/net/inet_address.cpp | 82 ++++++++++++++----------- code/nel/src/net/unified_network.cpp | 23 ++++--- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/code/nel/include/nel/net/inet_address.h b/code/nel/include/nel/net/inet_address.h index fa75d47f9..b7dba0614 100644 --- a/code/nel/include/nel/net/inet_address.h +++ b/code/nel/include/nel/net/inet_address.h @@ -144,7 +144,7 @@ public: bool is127001 () const; /// Returns true if this CInetAddress is a loop back address - bool CInetAddress::isloopbackIPAddress () const; + bool isLoopbackIPAddress () const; /// Creates a CInetAddress object with local host address, port=0 static CInetAddress localHost(); diff --git a/code/nel/src/net/inet_address.cpp b/code/nel/src/net/inet_address.cpp index aaa43ab29..093bf8dcb 100644 --- a/code/nel/src/net/inet_address.cpp +++ b/code/nel/src/net/inet_address.cpp @@ -92,18 +92,18 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) BOOLEAN IN6_IS_ADDR_UNSPECIFIED(CONST IN6_ADDR *a) { - // - // We can't use the in6addr_any variable, since that would - // require existing callers to link with a specific library. - // - return (BOOLEAN)((a->s6_words[0] == 0) && - (a->s6_words[1] == 0) && - (a->s6_words[2] == 0) && - (a->s6_words[3] == 0) && - (a->s6_words[4] == 0) && - (a->s6_words[5] == 0) && - (a->s6_words[6] == 0) && - (a->s6_words[7] == 0)); + // + // We can't use the in6addr_any variable, since that would + // require existing callers to link with a specific library. + // + return (BOOLEAN)((a->s6_words[0] == 0) && + (a->s6_words[1] == 0) && + (a->s6_words[2] == 0) && + (a->s6_words[3] == 0) && + (a->s6_words[4] == 0) && + (a->s6_words[5] == 0) && + (a->s6_words[6] == 0) && + (a->s6_words[7] == 0)); } #endif @@ -700,13 +700,6 @@ std::vector CInetAddress::localAddresses() throw ESocket( "Unable to get local hostname" ); } - // for loopback ipv4 - struct in_addr *psin_addrIPv4 = NULL; - - // for loopback ipv6 - struct in6_addr *psin_addrIPv6 = NULL; - - // 2. Get address list vector vect; @@ -721,21 +714,32 @@ std::vector CInetAddress::localAddresses() if (status) { // will come here if the local hostname (/etc/hostname in Linux) is not the real name - throw ESocket( (string("Hostname resolution failed for ")+string(localhost)).c_str() ); + throw ESocket( toString("Hostname resolution failed for %s", localhost).c_str() ); } struct addrinfo *p = res; + // for loopback ipv4 + bool IPv4LoopbackAdded = false; + + // for loopback ipv6 + bool IPv6LoopbackAdded = false; + // process all addresses while (p != NULL) { // check address family - if (p->ai_family == AF_INET){ // ipv4 + if (p->ai_family == AF_INET) + { // loopback ipv4 - if(!psin_addrIPv4){ // add loopback address only once - struct in_addr *psin_addrIPv4 = new in_addr; - psin_addrIPv4->s_addr = htonl(INADDR_LOOPBACK); - vect.push_back(CInetAddress(psin_addrIPv4, localhost)); + if (!IPv4LoopbackAdded) + { + // add loopback address only once + struct in_addr psin_addrIPv4; + psin_addrIPv4.s_addr = htonl(INADDR_LOOPBACK); + vect.push_back(CInetAddress(&psin_addrIPv4, localhost)); + + IPv4LoopbackAdded = true; } struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; @@ -743,12 +747,16 @@ std::vector CInetAddress::localAddresses() vect.push_back( CInetAddress( &ipv4->sin_addr, localhost ) ); } - else if (p->ai_family == AF_INET6){ // ipv6 + else if (p->ai_family == AF_INET6) + { // loopback ipv6 - if(!psin_addrIPv6){ // add loopback address only once - struct in6_addr aLoopback6 = IN6ADDR_LOOPBACK_INIT; - psin_addrIPv6 = &aLoopback6; - vect.push_back(CInetAddress(psin_addrIPv6, localhost)); + if (!IPv6LoopbackAdded) + { + // add loopback address only once + struct in6_addr psin_addrIPv6 = IN6ADDR_LOOPBACK_INIT; + vect.push_back(CInetAddress(&psin_addrIPv6, localhost)); + + IPv6LoopbackAdded = true; } struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; @@ -765,7 +773,7 @@ std::vector CInetAddress::localAddresses() if(vect.empty()) { - throw ESocket( (string("No network card detected for ")+string(localhost)).c_str() ); + throw ESocket(toString("No network card detected for %s", localhost).c_str() ); } return vect; @@ -776,14 +784,14 @@ bool CInetAddress::is127001 () const return (internalIPAddress () == htonl(0x7F000001)); } -bool CInetAddress::isloopbackIPAddress () const +bool CInetAddress::isLoopbackIPAddress () const { - const char *sIPAddress = ipAddress().c_str(); + std::string sIPAddress = ipAddress(); - return (strcmp(sIPAddress, "::") == 0) || - (strcmp(sIPAddress, "::1") == 0) || - (strcmp(sIPAddress, "127.0.0.1") == 0) || - (strcmp(sIPAddress, "0:0:0:0:0:0:0:1") == 0); + return (sIPAddress.compare("::") == 0) || + (sIPAddress.compare("::1") == 0) || + (sIPAddress.compare("127.0.0.1") == 0) || + (sIPAddress.compare("0:0:0:0:0:0:0:1") == 0); } diff --git a/code/nel/src/net/unified_network.cpp b/code/nel/src/net/unified_network.cpp index 1dedbb119..ee882f547 100644 --- a/code/nel/src/net/unified_network.cpp +++ b/code/nel/src/net/unified_network.cpp @@ -831,8 +831,9 @@ void CUnifiedNetwork::addService(const string &name, const vector uint j = 0; - if (!addr[i].isloopbackIPAddress ()){ // it 's loopback ip address, it s ok - + if (!addr[i].isLoopbackIPAddress()) + { + // it's loopback ip address, it's ok for (j = 0; j < laddr.size (); j++) { if (laddr[j].internalNetAddress () == addr[i].internalNetAddress ()) @@ -1293,8 +1294,11 @@ uint8 CUnifiedNetwork::findConnectionId (TServiceId sid, uint8 nid) if (connectionId >= _IdCnx[sid.get()].Connections.size() || !_IdCnx[sid.get()].Connections[connectionId].valid() || !_IdCnx[sid.get()].Connections[connectionId].CbNetBase->connected()) { - if(nid != 0xFF) // not a default network. There's a problem with the selected connectionID, so try to find a valid one + if (nid != 0xFF) + { + // not a default network. There's a problem with the selected connectionID, so try to find a valid one nlwarning ("HNETL5: Can't find selected connection id %hu to send message to %s because connection is not valid or connected, find a valid connection id", (uint16)connectionId, _IdCnx[sid.get()].ServiceName.c_str ()); + } for (connectionId = 0; connectionId < _IdCnx[sid.get()].Connections.size(); connectionId++) { @@ -1302,13 +1306,18 @@ uint8 CUnifiedNetwork::findConnectionId (TServiceId sid, uint8 nid) { // we found one at last, use this one //nldebug ("HNETL5: Ok, we found a valid connectionid, use %hu", (uint16)connectionId); - if(nid < _IdCnx[sid.get()].NetworkConnectionAssociations.size()){ + if (nid < _IdCnx[sid.get()].NetworkConnectionAssociations.size()) + { _IdCnx[sid.get()].NetworkConnectionAssociations[nid] = connectionId; // we set the preferred networkConnectionAssociation - } else { - if(nid == 0xFF){ + } + else + { + if (nid == 0xFF) + { _IdCnx[sid.get()].DefaultNetwork = connectionId; } } + nlwarning ("HNETL5: selected connection id %hu from network %hu to send message to %s", (uint16)connectionId, (uint16)nid, _IdCnx[sid.get()].ServiceName.c_str ()); break; } @@ -1789,7 +1798,7 @@ bool CUnifiedNetwork::isServiceLocal (TServiceId sid) { for (uint j = 0; j < _IdCnx[sid.get()].ExtAddress.size(); j++) { - if (_IdCnx[sid.get()].ExtAddress[j].isloopbackIPAddress ()) + if (_IdCnx[sid.get()].ExtAddress[j].isLoopbackIPAddress ()) return true; if (_IdCnx[sid.get()].ExtAddress[j].internalIPAddress () == laddr[i].internalIPAddress ())