bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

IPv6 support for who, pinky, logname


From: Paul Eggert
Subject: IPv6 support for who, pinky, logname
Date: Tue, 16 Nov 2004 12:40:21 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this to add IPv6 support to who, pinky, and logname:

2004-11-16  Paul Eggert  <address@hidden>

        * lib/canon-host.c: Include "strdup.h".
        (canon_host): Use getaddrinfo if available, so that IPv6 works.
        Use strdup instead of malloc/strcpy to duplicate strings.
        * m4/canon-host.m4 (gl_CANON_HOST): Check for getaddrinfo.

--- canon-host.c.~1.16.~        2003-09-10 01:29:47 -0700
+++ canon-host.c        2004-11-16 12:37:25 -0800
@@ -1,6 +1,7 @@
 /* Host name canonicalization
 
-   Copyright (C) 1995, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1995, 1999, 2000, 2002, 2003, 2004 Free Software
+   Foundation, Inc.
 
    Written by Miles Bader <address@hidden>
 
@@ -42,55 +43,73 @@
 # include <arpa/inet.h>
 #endif
 
+#include "strdup.h"
+
 /* Returns the canonical hostname associated with HOST (allocated in a static
-   buffer), or 0 if it can't be determined.  */
+   buffer), or NULL if it can't be determined.  */
 char *
-canon_host (const char *host)
+canon_host (char const *host)
 {
-#ifdef HAVE_GETHOSTBYNAME
-  struct hostent *he = gethostbyname (host);
+  char *h_addr_copy = NULL;
 
-  if (he)
-    {
+#if HAVE_GETADDRINFO
+  {
+    struct addrinfo hint = { 0 };
+    struct addrinfo *res = NULL;
+    hint.ai_flags = AI_CANONNAME;
+    if (getaddrinfo (host, NULL, &hint, &res) == 0)
+      {
+       h_addr_copy = strdup (res->ai_canonname);
+       freeaddrinfo (res);
+      }
+  }
+#elif HAVE_GETHOSTBYNAME
+  {
+    struct hostent *he = gethostbyname (host);
+
+    if (he)
+      {
 # ifdef HAVE_GETHOSTBYADDR
-      char *addr = 0;
+       char *addr = NULL;
 
-      /* Try and get an ascii version of the numeric host address.  */
-      switch (he->h_addrtype)
-       {
+       /* Try and get an ascii version of the numeric host address.  */
+       switch (he->h_addrtype)
+         {
 #  ifdef HAVE_INET_NTOA
-       case AF_INET:
-         addr = inet_ntoa (*(struct in_addr *) he->h_addr);
-         break;
+         case AF_INET:
+           addr = inet_ntoa (*(struct in_addr *) he->h_addr);
+           break;
 #  endif /* HAVE_INET_NTOA */
-       }
+         }
 
-      if (addr && strcmp (he->h_name, addr) == 0)
-       {
-         /* gethostbyname has returned a string representation of the IP
-            address, for example, "127.0.0.1".  So now, look up the host
-            name via the address.  Although it may seem reasonable to look
-            up the host name via the address, we must not pass `he->h_addr'
-            directly to gethostbyaddr because on some systems he->h_addr
-            is located in a static library buffer that is reused in the
-            gethostbyaddr call.  Make a copy and use that instead.  */
-         char *h_addr_copy = (char *) malloc (he->h_length);
-         if (h_addr_copy == NULL)
-           he = NULL;
-         else
-           {
-             memcpy (h_addr_copy, he->h_addr, he->h_length);
-             he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype);
-             free (h_addr_copy);
-           }
-       }
+       if (addr && strcmp (he->h_name, addr) == 0)
+         {
+           /* gethostbyname has returned a string representation of the IP
+              address, for example, "127.0.0.1".  So now, look up the host
+              name via the address.  Although it may seem reasonable to look
+              up the host name via the address, we must not pass `he->h_addr'
+              directly to gethostbyaddr because on some systems he->h_addr
+              is located in a static library buffer that is reused in the
+              gethostbyaddr call.  Make a copy and use that instead.  */
+           h_addr_copy = (char *) malloc (he->h_length);
+           if (h_addr_copy == NULL)
+             he = NULL;
+           else
+             {
+               memcpy (h_addr_copy, he->h_addr, he->h_length);
+               he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype);
+               free (h_addr_copy);
+             }
+         }
 # endif /* HAVE_GETHOSTBYADDR */
 
-      if (he)
-       return (char *) (he->h_name);
-    }
+       if (he)
+         h_addr_copy = strdup (he->h_name);
+      }
+  }
 #endif /* HAVE_GETHOSTBYNAME */
-  return 0;
+
+  return h_addr_copy;
 }
 
 #ifdef TEST_CANON_HOST
Index: m4/canon-host.m4
===================================================================
RCS file: /fetish/cu/m4/canon-host.m4,v
retrieving revision 1.2
diff -p -u -r1.2 canon-host.m4
--- m4/canon-host.m4    12 Sep 2003 20:01:54 -0000      1.2
+++ m4/canon-host.m4    16 Nov 2004 20:27:37 -0000
@@ -1,5 +1,5 @@
-# canon-host.m4 serial 3
-dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+# canon-host.m4 serial 4
+dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
 dnl Public License, this file may be distributed as part of a program
@@ -18,5 +18,5 @@ AC_DEFUN([gl_CANON_HOST],
   AC_SEARCH_LIBS(gethostbyname, [inet nsl])
 
   dnl These come from -lnsl on Solaris 2.5.1.
-  AC_CHECK_FUNCS(gethostbyname gethostbyaddr inet_ntoa)
+  AC_CHECK_FUNCS(getaddrinfo gethostbyname gethostbyaddr inet_ntoa)
 ])




reply via email to

[Prev in Thread] Current Thread [Next in Thread]