Skip to content

Commit 00bce77

Browse files
devzero2000peff
authored andcommitted
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 <[email protected]> Helped-by: Jeff King <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent b05c2f9 commit 00bce77

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

ident.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,35 @@ static int add_mailname_host(struct strbuf *buf)
7070
return 0;
7171
}
7272

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+
7399
static void add_domainname(struct strbuf *out)
74100
{
75101
char buf[1024];
76-
struct hostent *he;
77102

78103
if (gethostname(buf, sizeof(buf))) {
79104
warning("cannot get host name: %s", strerror(errno));
@@ -82,9 +107,7 @@ static void add_domainname(struct strbuf *out)
82107
}
83108
if (strchr(buf, '.'))
84109
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)
88111
strbuf_addf(out, "%s.(none)", buf);
89112
}
90113

0 commit comments

Comments
 (0)