[PATCH] ypbind: replace gethostbyname() with getaddrinfo()

Ashwani Kumar Kamal <[email protected]>
Newsgroups gmane.os.freebsd.devel.hackers
Message-ID <[email protected]>
Replace legacy gethostbyname() usage in yp_restricted_mode()
with getaddrinfo(), preserving IPv4-only semantics.
This avoids use of struct hostent and modernizes the
name resolution API.

Signed-off-by: Ashwani Kumar Kamal <[email protected]>
---
 usr.sbin/ypbind/ypbind.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/usr.sbin/ypbind/ypbind.c b/usr.sbin/ypbind/ypbind.c
index 6ed45eeb8dc6..8a6da584f5d1 100644
--- a/usr.sbin/ypbind/ypbind.c
+++ b/usr.sbin/ypbind/ypbind.c
@@ -993,7 +993,7 @@ verify(struct in_addr addr)
 void
 yp_restricted_mode(char *args)
 {
-	struct hostent *h;
+	struct addrinfo hints, *res;
 	int i = 0;
 	char *s;
 
@@ -1004,10 +1004,13 @@ yp_restricted_mode(char *args)
 
 	/* Get the addresses of the servers. */
 	while ((s = strsep(&args, ",")) != NULL && i < RESTRICTED_SERVERS) {
-		if ((h = gethostbyname(s)) == NULL)
+		memset(&hints, 0, sizeof(hints));
+		hints.ai_family = AF_INET;
+		if (getaddrinfo(s, NULL, &hints, &res) != 0)
 			return;
-		bcopy (h->h_addr_list[0], &restricted_addrs[i],
+		bcopy (&((struct sockaddr_in *)res->ai_addr)->sin_addr, &restricted_addrs[i],
 		    sizeof(struct in_addr));
+		freeaddrinfo(res);
 		i++;
 	}
 
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.