From 45ac4e8df477c596fae45de2232534e4bfccf741 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 2 Apr 2016 13:31:59 +0200 Subject: [PATCH] Changed: Use booleans to know if we already added a loopback address --- 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;