From e4b8ea0a87eb2f3ce7a9bbb9bcff5d8225a68fce Mon Sep 17 00:00:00 2001
From: Pavel Raiskup <praiskup@redhat.com>
Date: Thu, 5 Dec 2013 13:58:30 +0100
Subject: [PATCH] socket: speedup also ipv6 connection

Call getaddrinfo() with AI_NUMERICHOST if it is ipv4/ipv6 address.
For that reason the inet_pton() is better than inet_addr().
---
 socket.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/socket.c b/socket.c
index da574e2..01ea56c 100644
--- a/socket.c
+++ b/socket.c
@@ -230,7 +230,15 @@ static BOOL format_sockerr(char *errmsg, size_t buflen, int errnum, const char *
 		snprintf(errmsg, buflen, "%s failed for [%s:%d] ", cmd, host, portno);
 	return ret;
 }
- 
+
+static int
+is_numeric_address(const char *hostname)
+{
+	char unused[16];
+	return (inet_pton(AF_INET, hostname, &unused) ||
+		inet_pton(AF_INET6, hostname, &unused));
+}
+
 char
 SOCK_connect_to(SocketClass *self, unsigned short port, char *hostname, long timeout)
 {
@@ -271,8 +279,9 @@ SOCK_connect_to(SocketClass *self, unsigned short port, char *hostname, long tim
 		rest.ai_socktype = SOCK_STREAM;
 		rest.ai_family = AF_UNSPEC;
 		snprintf(portstr, sizeof(portstr), "%d", port);
-		if (inet_addr(hostname) != INADDR_NONE)
-			rest.ai_flags |= AI_NUMERICHOST;	
+		if (is_numeric_address(hostname))
+			/* don't resolve address in getaddrinfo() if not necessary */
+			rest.ai_flags |= AI_NUMERICHOST;
 		ret = getaddrinfo_ptr(hostname, portstr, &rest, &addrs);
 		if (ret || !addrs)
 		{
-- 
1.8.4.2

