a93967
From 0be99f8799e90eaed4e8eeb7d5be7de81dd71360 Mon Sep 17 00:00:00 2001
a93967
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
a93967
Date: Thu, 11 Apr 2019 18:17:16 +0200
a93967
Subject: [PATCH] inet_aton: Use getaddrinfo() if possible
a93967
MIME-Version: 1.0
a93967
Content-Type: text/plain; charset=UTF-8
a93967
Content-Transfer-Encoding: 8bit
a93967
a93967
Socket::inet_aton() used gethostbyname() to process arguments that are
a93967
not an IP addres. However, gethostbyname() is not thread-safe and when
a93967
called from multiple threads a bogus value can be returned.
a93967
a93967
This patch does add any new test because a basic inet_aton() usage is
a93967
already covered and because reproducing the thread failure would
a93967
require flodding DNS servers with thousounds of request.
a93967
a93967
<https://rt.perl.org/Public/Bug/Display.html?id=97860>
a93967
<https://bugzilla.redhat.com/show_bug.cgi?id=1693293>
a93967
a93967
Signed-off-by: Petr Písař <ppisar@redhat.com>
a93967
---
a93967
 Socket.xs | 16 +++++++++++++++-
a93967
 1 file changed, 15 insertions(+), 1 deletion(-)
a93967
a93967
diff --git a/Socket.xs b/Socket.xs
a93967
index e46c93e..65244dd 100644
a93967
--- a/Socket.xs
a93967
+++ b/Socket.xs
a93967
@@ -764,19 +764,33 @@ inet_aton(host)
a93967
 	char *	host
a93967
 	CODE:
a93967
 	{
a93967
+#ifdef HAS_GETADDRINFO
a93967
+	struct addrinfo *res;
a93967
+	struct addrinfo hints = {0,};
a93967
+	hints.ai_family = AF_INET;
a93967
+	if (!getaddrinfo(host, NULL, &hints, &res)) {
a93967
+	    ST(0) = sv_2mortal(newSVpvn(
a93967
+		(char *)&(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr),
a93967
+		4
a93967
+	));
a93967
+	freeaddrinfo(res);
a93967
+	XSRETURN(1);
a93967
+    }
a93967
+#else
a93967
 	struct in_addr ip_address;
a93967
 	struct hostent * phe;
a93967
-
a93967
 	if ((*host != '\0') && inet_aton(host, &ip_address)) {
a93967
 		ST(0) = sv_2mortal(newSVpvn((char *)&ip_address, sizeof(ip_address)));
a93967
 		XSRETURN(1);
a93967
 	}
a93967
 #ifdef HAS_GETHOSTBYNAME
a93967
+	/* gethostbyname is not thread-safe */
a93967
 	phe = gethostbyname(host);
a93967
 	if (phe && phe->h_addrtype == AF_INET && phe->h_length == 4) {
a93967
 		ST(0) = sv_2mortal(newSVpvn((char *)phe->h_addr, phe->h_length));
a93967
 		XSRETURN(1);
a93967
 	}
a93967
+#endif
a93967
 #endif
a93967
 	XSRETURN_UNDEF;
a93967
 	}
a93967
-- 
a93967
2.20.1
a93967