connect: replace inet_ntop with getnameinfo

inet_ntop is not protocol independent.
getnameinfo(3) is part of POSIX and is available when getaddrinfo(3) is.
This code is only compiled when NO_IPV6 isn't defined.

The old method was buggy anyway, not every ipv6 address was converted
properly because the buffer (addr) was too small.

Signed-off-by: Benjamin Kramer <benny.kra@googlemail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Benjamin Kramer 2009-04-24 14:16:41 +02:00 committed by Junio C Hamano
parent 911198f6c0
commit 785a985749

View File

@ -177,18 +177,11 @@ static enum protocol get_protocol(const char *name)
static const char *ai_name(const struct addrinfo *ai) static const char *ai_name(const struct addrinfo *ai)
{ {
static char addr[INET_ADDRSTRLEN]; static char addr[NI_MAXHOST];
if ( AF_INET == ai->ai_family ) { if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
struct sockaddr_in *in; NI_NUMERICHOST) != 0)
in = (struct sockaddr_in *)ai->ai_addr;
inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
} else if ( AF_INET6 == ai->ai_family ) {
struct sockaddr_in6 *in;
in = (struct sockaddr_in6 *)ai->ai_addr;
inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
} else {
strcpy(addr, "(unknown)"); strcpy(addr, "(unknown)");
}
return addr; return addr;
} }