Changed: Use booleans to know if we already added a loopback address

--HG--
branch : develop
This commit is contained in:
kervala 2016-04-02 13:31:59 +02:00
parent 3c8228df15
commit 3219ce959b

View file

@ -700,13 +700,6 @@ std::vector<CInetAddress> CInetAddress::localAddresses()
throw ESocket( "Unable to get local hostname" ); 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 // 2. Get address list
vector<CInetAddress> vect; vector<CInetAddress> vect;
@ -726,6 +719,12 @@ std::vector<CInetAddress> CInetAddress::localAddresses()
struct addrinfo *p = res; struct addrinfo *p = res;
// for loopback ipv4
bool IPv4LoopbackAdded = false;
// for loopback ipv6
bool IPv6LoopbackAdded = false;
// process all addresses // process all addresses
while (p != NULL) while (p != NULL)
{ {
@ -733,10 +732,14 @@ std::vector<CInetAddress> CInetAddress::localAddresses()
if (p->ai_family == AF_INET) if (p->ai_family == AF_INET)
{ {
// loopback ipv4 // loopback ipv4
if(!psin_addrIPv4){ // add loopback address only once if (!IPv4LoopbackAdded)
struct in_addr *psin_addrIPv4 = new in_addr; {
psin_addrIPv4->s_addr = htonl(INADDR_LOOPBACK); // add loopback address only once
vect.push_back(CInetAddress(psin_addrIPv4, localhost)); 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; struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
@ -747,10 +750,13 @@ std::vector<CInetAddress> CInetAddress::localAddresses()
else if (p->ai_family == AF_INET6) else if (p->ai_family == AF_INET6)
{ {
// loopback ipv6 // loopback ipv6
if(!psin_addrIPv6){ // add loopback address only once if (!IPv6LoopbackAdded)
struct in6_addr aLoopback6 = IN6ADDR_LOOPBACK_INIT; {
psin_addrIPv6 = &aLoopback6; // add loopback address only once
vect.push_back(CInetAddress(psin_addrIPv6, localhost)); 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; struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;