summaryrefslogtreecommitdiff
blob: abc85659df021e725d4e5d5c01b2f7a713b6ee86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
From d894c8c42415fb8e5148dc0ba073078acb99a607 Mon Sep 17 00:00:00 2001
From: Mike Frysinger <vapier@gentoo.org>
Date: Mon, 14 Apr 2008 00:40:29 -0400
Subject: [PATCH] Patch by pasi.valminen@hut.fi to fix FQDN handling with AAAA records

http://bugs.gentoo.org/42650
---
 hostname.c |   71 +++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/hostname.c b/hostname.c
index c4c5aa0..e8ed156 100644
--- a/hostname.c
+++ b/hostname.c
@@ -36,6 +36,10 @@
 #include <getopt.h>
 #include <string.h>
 #include <netdb.h>
+#ifdef IPV6
+#include <sys/socket.h> /* for PF_INET6 */
+#include <sys/types.h>  /* for inet_ntop */
+#endif /* IPV6 */
 #include <errno.h>
 #include <sys/param.h>
 #include <netinet/in.h>
@@ -125,15 +129,23 @@ static void setdname(char *dname)
 static void showhname(char *hname, int c)
 {
     struct hostent *hp;
+#ifdef IPV6
+    struct in6_addr **ip6;
+#endif /* IPV6 */
     register char *p, **alias;
     struct in_addr **ip;
 
     if (opt_v)
 	fprintf(stderr, _("Resolving `%s' ...\n"), hname);
-    if (!(hp = gethostbyname(hname))) {
+    if (
+#ifdef IPV6
+        !(hp = gethostbyname2(hname, PF_INET6)) && 
+#endif /* IPV6 */
+        !(hp = gethostbyname(hname))) {
 	herror(program_name);
 	exit(1);
     }
+
     if (opt_v) {
 	fprintf(stderr, _("Result: h_name=`%s'\n"),
 		hp->h_name);
@@ -142,11 +154,28 @@ static void showhname(char *hname, int c)
 	while (alias[0])
 	    fprintf(stderr, _("Result: h_aliases=`%s'\n"),
 		    *alias++);
-
-	ip = (struct in_addr **) hp->h_addr_list;
-	while (ip[0])
-	    fprintf(stderr, _("Result: h_addr_list=`%s'\n"),
-		    inet_ntoa(**ip++));
+#ifdef IPV6
+        if(hp->h_addrtype == PF_INET6) {
+            char addr[INET6_ADDRSTRLEN + 1];
+	    addr[INET6_ADDRSTRLEN] = '\0';
+            ip6 = (struct in6_addr **) hp->h_addr_list;
+            while(ip6[0]) {
+                if(inet_ntop(PF_INET6, *ip6++, addr, INET6_ADDRSTRLEN))
+                    fprintf(stderr, _("Result: h_addr_list=`%s'\n"), addr);
+                else if(errno == EAFNOSUPPORT)
+                    fprintf(stderr, _("%s: protocol family not supported\n"),
+                            program_name);
+                else if(errno == ENOSPC)
+                    fprintf(stderr, _("%s: name too long\n"), program_name);
+            }
+        } else 
+#endif /* IPV6 */
+	{
+	    ip = (struct in_addr **) hp->h_addr_list;
+	    while (ip[0])
+	        fprintf(stderr, _("Result: h_addr_list=`%s'\n"),
+		        inet_ntoa(**ip++));
+        }
     }
     if (!(p = strchr(hp->h_name, '.')) && (c == 'd'))
 	return;
@@ -158,10 +187,32 @@ static void showhname(char *hname, int c)
 	printf("\n");
 	break;
     case 'i':
-	while (hp->h_addr_list[0])
-	    printf("%s ", inet_ntoa(*(struct in_addr *) *hp->h_addr_list++));
-	printf("\n");
-	break;
+#ifdef IPV6
+        if(hp->h_addrtype == PF_INET6) {
+            char addr[INET6_ADDRSTRLEN + 1];
+            addr[INET6_ADDRSTRLEN] = '\0';
+            while(hp->h_addr_list[0]) {
+                if(inet_ntop(PF_INET6, (struct in6_addr *)*hp->h_addr_list++,
+                             addr, INET6_ADDRSTRLEN))
+                    printf("%s ", addr);
+                else if(errno == EAFNOSUPPORT) {
+                    fprintf(stderr, _("\n%s: protocol family not supported\n"),
+                            program_name);
+                    exit(1);
+                } else if(errno == ENOSPC) {
+                    fprintf(stderr, _("\n%s: name too long\n"), program_name);
+                    exit(1);
+                }
+                printf("\n");
+            }
+        } else
+#endif /* IPV6 */
+        {
+	    while (hp->h_addr_list[0])
+	        printf("%s ", inet_ntoa(*(struct in_addr *)*hp->h_addr_list++));
+            printf("\n");
+        }
+        break;
     case 'd':
 	printf("%s\n", ++p);
 	break;
-- 
1.5.5