From a410f2fcc1f720aca49702daa0aaeecec6d85f69 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Apr 2016 13:28:42 +0200 Subject: [PATCH 1/5] Changed: Better camel case syntax --HG-- branch : develop --- code/nel/include/nel/net/inet_address.h | 2 +- code/nel/src/net/inet_address.cpp | 2 +- code/nel/src/net/unified_network.cpp | 7 ++++--- 3 files changed, 6 insertions(+), 5 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..66650d79c 100644 --- a/code/nel/src/net/inet_address.cpp +++ b/code/nel/src/net/inet_address.cpp @@ -776,7 +776,7 @@ bool CInetAddress::is127001 () const return (internalIPAddress () == htonl(0x7F000001)); } -bool CInetAddress::isloopbackIPAddress () const +bool CInetAddress::isLoopbackIPAddress () const { const char *sIPAddress = ipAddress().c_str(); diff --git a/code/nel/src/net/unified_network.cpp b/code/nel/src/net/unified_network.cpp index 1dedbb119..9ce5dc7a5 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 ()) @@ -1789,7 +1790,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 ()) From c30bf20ccc27443490c7da865d4a2ca5753b509a Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Apr 2016 13:29:42 +0200 Subject: [PATCH 2/5] Changed: Minor changes --HG-- branch : develop --- code/nel/src/net/inet_address.cpp | 30 +++++++++++++++------------- code/nel/src/net/unified_network.cpp | 16 +++++++++++---- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/code/nel/src/net/inet_address.cpp b/code/nel/src/net/inet_address.cpp index 66650d79c..f7b199d74 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 @@ -730,7 +730,8 @@ std::vector CInetAddress::localAddresses() 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; @@ -743,7 +744,8 @@ 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; diff --git a/code/nel/src/net/unified_network.cpp b/code/nel/src/net/unified_network.cpp index 9ce5dc7a5..ee882f547 100644 --- a/code/nel/src/net/unified_network.cpp +++ b/code/nel/src/net/unified_network.cpp @@ -1294,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++) { @@ -1303,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; } From 7dd635960a9209333dace69bc73c43821fccb490 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Apr 2016 13:30:08 +0200 Subject: [PATCH 3/5] Changed: Use toString for formatting --HG-- branch : develop --- code/nel/src/net/inet_address.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/nel/src/net/inet_address.cpp b/code/nel/src/net/inet_address.cpp index f7b199d74..e5b0280ab 100644 --- a/code/nel/src/net/inet_address.cpp +++ b/code/nel/src/net/inet_address.cpp @@ -721,7 +721,7 @@ 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; @@ -767,7 +767,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; From 3c8228df1534df1e048a030e584a9b261732fdc6 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Apr 2016 13:31:04 +0200 Subject: [PATCH 4/5] Fixed: Empty const char* because std::string destructor called --HG-- branch : develop --- code/nel/src/net/inet_address.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/code/nel/src/net/inet_address.cpp b/code/nel/src/net/inet_address.cpp index e5b0280ab..f5d22ebe3 100644 --- a/code/nel/src/net/inet_address.cpp +++ b/code/nel/src/net/inet_address.cpp @@ -780,12 +780,12 @@ bool CInetAddress::is127001 () 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); } From 3219ce959bd11e2982da0f0e23189b578bac315e Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Apr 2016 13:31:59 +0200 Subject: [PATCH 5/5] Changed: Use booleans to know if we already added a loopback address --HG-- branch : develop --- code/nel/src/net/inet_address.cpp | 36 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/code/nel/src/net/inet_address.cpp b/code/nel/src/net/inet_address.cpp index f5d22ebe3..093bf8dcb 100644 --- a/code/nel/src/net/inet_address.cpp +++ b/code/nel/src/net/inet_address.cpp @@ -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; @@ -726,6 +719,12 @@ std::vector CInetAddress::localAddresses() struct addrinfo *p = res; + // for loopback ipv4 + bool IPv4LoopbackAdded = false; + + // for loopback ipv6 + bool IPv6LoopbackAdded = false; + // process all addresses while (p != NULL) { @@ -733,10 +732,14 @@ std::vector CInetAddress::localAddresses() 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; @@ -747,10 +750,13 @@ std::vector CInetAddress::localAddresses() 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;