From ab6489f9fe1d54c4799bb9b1897f63682f710529 Mon Sep 17 00:00:00 2001
From: beck <>
Date: Sun, 30 Apr 2017 05:43:05 +0000
Subject: Make BIO_get_host_ip just yet another getaddrinfo wrapper

---
 src/lib/libcrypto/bio/b_sock.c | 47 ++++++++++++++++++------------------------
 1 file changed, 20 insertions(+), 27 deletions(-)

(limited to 'src/lib')

diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c
index b8aba39578..af3e35e66b 100644
--- a/src/lib/libcrypto/bio/b_sock.c
+++ b/src/lib/libcrypto/bio/b_sock.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: b_sock.c,v 1.65 2017/04/30 05:09:22 beck Exp $ */
+/* $OpenBSD: b_sock.c,v 1.66 2017/04/30 05:43:05 beck Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -78,36 +78,29 @@
 int
 BIO_get_host_ip(const char *str, unsigned char *ip)
 {
-	int i;
-	int err = 1;
-	struct hostent *he;
-
-	if (inet_pton(AF_INET, str, ip) == 1)
-		return (1);
+	struct addrinfo *res = NULL;
+	struct addrinfo hints = {
+		.ai_family = AF_INET,
+		.ai_socktype = SOCK_STREAM,
+		.ai_flags = AI_PASSIVE,
+	};
+	uint32_t *iap = (in_addr_t *)ip;
+	int error;
 
-	/* do a gethostbyname */
-	CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
-	he = BIO_gethostbyname(str);
-	if (he == NULL) {
-		BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP);
-		goto err;
+	if (str == NULL) {
+		ERR_asprintf_error_data("NULL host provided");
+		return (0);
 	}
 
-	if (he->h_addrtype != AF_INET) {
-		BIOerror(BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
-		goto err;
+	if ((error = getaddrinfo(str, NULL, &hints, &res)) != 0) {
+		BIOerror(BIO_R_BAD_HOSTNAME_LOOKUP);
+		ERR_asprintf_error_data("getaddrinfo: host='%s' : %s'", str,
+		    gai_strerror(error));
+		return (0);
 	}
-	for (i = 0; i < 4; i++)
-		ip[i] = he->h_addr_list[0][i];
-	err = 0;
-
-err:
-	CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
-	if (err) {
-		ERR_asprintf_error_data("host=%s", str);
-		return 0;
-	} else
-		return 1;
+	*iap = (uint32_t)(((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr);
+	freeaddrinfo(res);
+	return (1);
 }
 
 int
-- 
cgit v1.2.3-55-g6feb