Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 22 Nov 2009 16:51:44 +0000 (UTC)
From:      Hajimu UMEMOTO <ume@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r199655 - head/usr.bin/w
Message-ID:  <200911221651.nAMGpiIO023873@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ume
Date: Sun Nov 22 16:51:44 2009
New Revision: 199655
URL: http://svn.freebsd.org/changeset/base/199655

Log:
  When -n is specified, attempt to turn hostnames found in utmp into IP
  addresses, again.  However, change a hostname into an IP address, only
  when a host has just one A/AAAA RR.
  
  Requested by:	candy__at__kgc.co.jp
  MFC after:	2 weeks

Modified:
  head/usr.bin/w/w.c

Modified: head/usr.bin/w/w.c
==============================================================================
--- head/usr.bin/w/w.c	Sun Nov 22 16:13:16 2009	(r199654)
+++ head/usr.bin/w/w.c	Sun Nov 22 16:51:44 2009	(r199655)
@@ -348,6 +348,7 @@ main(int argc, char *argv[])
 
 	for (ep = ehead; ep != NULL; ep = ep->next) {
 		char host_buf[UT_HOSTSIZE + 1];
+		struct addrinfo hints, *res;
 		struct sockaddr_storage ss;
 		struct sockaddr *sa = (struct sockaddr *)&ss;
 		struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
@@ -365,23 +366,42 @@ main(int argc, char *argv[])
 			else
 				x_suffix = NULL;
 		}
+
+		isaddr = 0;
+		memset(&ss, '\0', sizeof(ss));
+		if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
+			lsin6->sin6_len = sizeof(*lsin6);
+			lsin6->sin6_family = AF_INET6;
+			isaddr = 1;
+		} else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
+			lsin->sin_len = sizeof(*lsin);
+			lsin->sin_family = AF_INET;
+			isaddr = 1;
+		}
 		if (!nflag) {
 			/* Attempt to change an IP address into a name */
-			isaddr = 0;
-			memset(&ss, '\0', sizeof(ss));
-			if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
-				lsin6->sin6_len = sizeof(*lsin6);
-				lsin6->sin6_family = AF_INET6;
-				isaddr = 1;
-			} else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
-				lsin->sin_len = sizeof(*lsin);
-				lsin->sin_family = AF_INET;
-				isaddr = 1;
-			}
 			if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
 			    sa->sa_len) == HOSTNAME_FOUND)
 				p = fn;
+		} else if (!isaddr) {
+			/*
+			 * If a host has only one A/AAAA RR, change a
+			 * name into an IP address
+			 */
+			memset(&hints, 0, sizeof(hints));
+			hints.ai_flags = AI_PASSIVE;
+			hints.ai_family = AF_UNSPEC;
+			hints.ai_socktype = SOCK_STREAM;
+			if (getaddrinfo(p, NULL, &hints, &res) == 0) {
+				if (res->ai_next == NULL &&
+				    getnameinfo(res->ai_addr, res->ai_addrlen,
+					fn, sizeof(fn), NULL, 0,
+					NI_NUMERICHOST) == 0)
+					p = fn;
+				freeaddrinfo(res);
+			}
 		}
+
 		if (x_suffix) {
 			(void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
 			p = buf;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200911221651.nAMGpiIO023873>