Index | Thread | Search

From:
Florian Obser <florian@openbsd.org>
Subject:
rpcinfo(8): get rid of inet_aton(3).
To:
tech <tech@openbsd.org>
Date:
Sun, 11 Aug 2024 13:58:21 +0200

Download raw body.

Thread
OK?

(I don't think if (res->ai_family != AF_INET) can actually happen, happy
to leave that out.)

diff --git rpcinfo.c rpcinfo.c
index 9b48a2108ee..91d99144c2c 100644
--- rpcinfo.c
+++ rpcinfo.c
@@ -689,16 +689,25 @@ fail:
 void
 get_inet_address(struct sockaddr_in *addr, char *host)
 {
-	struct hostent *hp;
+	struct addrinfo hints, *res;
+	int error;
 
-	bzero((char *)addr, sizeof *addr);
-	if (inet_aton(host, &addr->sin_addr) == 0) {
-		if ((hp = gethostbyname(host)) == NULL) {
-			fprintf(stderr, "rpcinfo: %s is unknown host\n",
-			    host);
-			exit(1);
-		}
-		bcopy(hp->h_addr, (char *)&addr->sin_addr, hp->h_length);
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_INET;
+
+	if ((error = getaddrinfo(host, NULL, &hints, &res))) {
+		fprintf(stderr, "rpcinfo: %s is unknown host: %s\n",
+		    host, gai_strerror(error));
+		exit(1);
 	}
+
+	if (res->ai_family != AF_INET) {
+		fprintf(stderr, "rpcinfo: %s is unknown host\n",
+		    host);
+		exit(1);
+	}
+
 	addr->sin_family = AF_INET;
+	addr->sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
+	freeaddrinfo(res);
 }

-- 
In my defence, I have been left unsupervised.