ident.c: add support for IPv6
Add IPv6 support by implementing name resolution with the protocol agnostic getaddrinfo(3) API. The old gethostbyname(3) code is still available when git is compiled with NO_IPV6. Signed-off-by: Elia Pinto <gitter.spiros@gmail.com> Helped-by: Jeff King <peff@peff.net> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Jeff King <peff@peff.net>
Elia Pinto committed
Nov 27, 2015 at 14:08 UTC
00bce77fe5b30720f4031f048abf42517b0da0ba
1 file changed
+27
-4
ident.c
+27
-4
@@ -70,10 +70,35 @@ static int add_mailname_host(struct strbuf *buf)
70
return 0;
71
}
72
73
+static int canonical_name(const char *host, struct strbuf *out)
74
+{
75
+ int status = -1;
76
+
77
+#ifndef NO_IPV6
78
+ struct addrinfo hints, *ai;
79
+ memset (&hints, '\0', sizeof (hints));
80
+ hints.ai_flags = AI_CANONNAME;
81
+ if (!getaddrinfo(host, NULL, &hints, &ai)) {
82
+ if (ai && strchr(ai->ai_canonname, '.')) {
83
+ strbuf_addstr(out, ai->ai_canonname);
84
+ status = 0;
85
+ }
86
+ freeaddrinfo(ai);
87
+ }
88
+#else
89
+ struct hostent *he = gethostbyname(buf);
90
+ if (he && strchr(he->h_name, '.')) {
91
+ strbuf_addstr(out, he->h_name);
92
+ status = 0;
93
+ }
94
+#endif /* NO_IPV6 */
95
+
96
+ return status;
97
+}
98
+
99
static void add_domainname(struct strbuf *out)
100
{
101
char buf[1024];
76
- struct hostent *he;
102
103
if (gethostname(buf, sizeof(buf))) {
104
warning("cannot get host name: %s", strerror(errno));
@@ -82,9 +107,7 @@ static void add_domainname(struct strbuf *out)
107
}
108
if (strchr(buf, '.'))
109
strbuf_addstr(out, buf);
85
- else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
86
- strbuf_addstr(out, he->h_name);
87
- else
110
+ else if (canonical_name(buf, out) < 0)
111
strbuf_addf(out, "%s.(none)", buf);
112
}
113