]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MEDIUM] use getaddrinfo to resolve names if gethostbyname fail
authorDavid du Colombier <dducolombier@exceliance.fr>
Thu, 17 Mar 2011 09:40:16 +0000 (10:40 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 23 Mar 2011 21:49:55 +0000 (22:49 +0100)
Function gethostbyname is deprecated since IEEE Std 1003.1-2008 and
was replaced by getaddrinfo (available since IEEE Std 1003.1-2004).
Contrary to gethostbyname, getaddrinfo is specified to support both
IPv4 and IPv4 addresses.
Since some libc doesn't handle getaddrinfo properly, constant
USE_GETADDRINFO must be defined at compile time to enable use of
getaddrinfo.

src/standard.c

index 033ece7bd09d0b5882d7dcfde03f10f075a7aac5..0e5c71ea36731e4419b2449ab2f111b7308460db 100644 (file)
@@ -265,8 +265,34 @@ struct sockaddr_storage *str2ip(const char *str)
                        ((struct sockaddr_in6 *)&sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
                        return &sa;
                }
-               /* unsupported address family */
        }
+#ifdef USE_GETADDRINFO
+       else {
+               struct addrinfo hints, *result;
+
+               memset(&result, 0, sizeof(result));
+               memset(&hints, 0, sizeof(hints));
+               hints.ai_family = AF_UNSPEC;
+               hints.ai_socktype = SOCK_DGRAM;
+               hints.ai_flags = AI_PASSIVE;
+               hints.ai_protocol = 0;
+
+               if (getaddrinfo(str, NULL, &hints, &result) == 0) {
+                       sa.ss_family = result->ai_family;
+                       switch (result->ai_family) {
+                       case AF_INET:
+                               memcpy((struct sockaddr_in *)&sa, result->ai_addr, result->ai_addrlen);
+                               return &sa;
+                       case AF_INET6:
+                               memcpy((struct sockaddr_in6 *)&sa, result->ai_addr, result->ai_addrlen);
+                               return &sa;
+                       }
+               }
+
+               freeaddrinfo(result);
+       }
+#endif
+       /* unsupported address family */
 
        return NULL;
 }