svn commit: r199655 - head/usr.bin/w

Juli Mallett jmallett at FreeBSD.org
Wed Nov 25 01:19:09 UTC 2009


I'm not sure this is right.  While the manpage implies that doing no
resolution means that it will show numeric addresses, since that is
not correct, there are two ways to go.  As you have done, you can
choose to try to resolve hostnames to IP addresses.  You could also do
as the code before did and do no resolution.  I frequently use 'w -n'
on systems with slow nameservers to see who is logged in without
triggering a bunch of DNS traffic that I then have to wait for.
Trying to resolve the name to a numeric address presents that problem.
 Since it is not possible to always show a numeric address (if, for
example, the hostname is no longer valid) I'm not sure that this is
the right direction to try to go with 'w -n'.

On Sun, Nov 22, 2009 at 08:51, Hajimu UMEMOTO <ume at freebsd.org> wrote:
> 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;
>


More information about the svn-src-all mailing list