Package: hostname
Version: 2.13
Severity: minor
Tags: patch

Hello,

Here is a patch which makes hostname try an IPv6 lookup in case the IPv4
one fails. This is needed to make hostname work on IPv6-only hosts.

This is a quick minimal non-instrusive half-fix only. I'd like to
implement the following behaviour if you and/or upstream are instereted:
    - by default, use both IPv4 and IPv6 for finding addresses/aliases;
    - allow the user to restrict operation to a given address space by
      using some command-line flags (such as -4/-6).  This would allow
      scripts to trust hostname to give them IPv4 or IPv6 addresses
      only.

What do you think ?

-- 
Jeremie Koenig <[EMAIL PROTECTED]>
diff -ru hostname-2.13.orig/hostname.c hostname-2.13/hostname.c
--- hostname-2.13.orig/hostname.c       2003-12-18 03:11:33.000000000 +0100
+++ hostname-2.13/hostname.c    2005-04-07 16:08:24.000000000 +0200
@@ -123,9 +123,13 @@
        register char *p,**alias;
        struct in_addr **ip;
        int flag = 0;   
+       char abuf[INET6_ADDRSTRLEN];
+
        if (opt_v)
                fprintf(stderr,NLS_CATGETS(catfd, hostnameSet, 
hostname_verb_res, "Resolving `%s' ...\n"),hname);
-       if (!(hp = gethostbyname(hname))) {
+
+       if (!(hp = gethostbyname(hname)) && !(hp = gethostbyname2(hname,
+                                       AF_INET6))) {
                herror(program_name);
                NLS_CATCLOSE(catfd)
                exit(1);
@@ -143,7 +147,7 @@
                ip=(struct in_addr **)hp->h_addr_list;
                while(ip[0])
                        fprintf(stderr,NLS_CATGETS(catfd, hostnameSet, 
hostname_verb_ipn, "Result: h_addr_list=`%s'\n"),
-                               inet_ntoa(**ip++));
+                               inet_ntop(hp->h_addrtype, *ip++, abuf, 
sizeof(abuf)));
        }
 
        if (!(p = strchr(hp->h_name, '.')) && (c == 'd')) return;
@@ -162,7 +166,8 @@
                        while (hp->h_addr_list[0]) {
                                if(flag)
                                        printf(" ");
-                               printf("%s ", inet_ntoa(*(struct in_addr *) * 
hp->h_addr_list++));
+                               printf("%s", inet_ntop(hp->h_addrtype,
+                                       *hp->h_addr_list++, abuf,sizeof(abuf)));
                                flag = 1;
                        }
                        printf("\n");

Reply via email to