86 #if !HAVE_DECL_GETNAMEINFO
89 #include <sys/socket.h>
95 #include <netinet/in.h>
98 #include <arpa/inet.h>
100 #if HAVE_ARPA_NAMESER_H
101 #include <arpa/nameser.h>
119 #include <inttypes.h>
124 #include <ws2tcpip.h>
135 { PF_INET6,
sizeof(
struct in6_addr), sizeof(struct sockaddr_in6),
136 offsetof(struct sockaddr_in6, sin6_addr),
137 offsetof(struct sockaddr_in6, sin6_port)
140 { PF_INET,
sizeof(
struct in_addr), sizeof(struct sockaddr_in),
141 offsetof(struct sockaddr_in, sin_addr),
142 offsetof(struct sockaddr_in, sin_port)
148 static int ip6_parsenumeric __P((
const struct sockaddr *,
const char *,
char *,
150 static int ip6_sa2str __P((
const struct sockaddr_in6 *,
char *,
size_t,
int));
154 xgetnameinfo(
const struct sockaddr *sa,
socklen_t salen,
char *host,
size_t hostlen,
char *serv,
size_t servlen,
int flags)
169 if (sa->sa_len != salen)
173 family = sa->sa_family;
175 if (
afdl[i].a_af == family) {
187 addr = (
const char *)sa +
afd->
a_off;
189 if (!serv || servlen == 0) {
201 (flags &
NI_DGRAM) ?
"udp" :
"tcp");
204 if (strlen(
sp->s_name) + 1 > servlen)
208 snprintf(numserv,
sizeof(numserv),
"%u", ntohs(
port));
209 if (strlen(numserv) + 1 > servlen)
215 switch (sa->sa_family) {
218 ntohl(((
const struct sockaddr_in *)sa)->sin_addr.s_addr);
221 v4a >>= IN_CLASSA_NSHIFT;
227 const struct sockaddr_in6 *sin6;
228 sin6 = (
const struct sockaddr_in6 *)sa;
229 switch (sin6->sin6_addr.s6_addr[0]) {
231 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
233 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
239 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
241 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
249 if (!host || hostlen == 0) {
271 if (strlen(hp->
h_name) + 1 > hostlen) {
291 if ((
error = ip6_parsenumeric(sa, addr, host,
310 ip6_parsenumeric(sa, addr, host, hostlen, flags)
311 const struct sockaddr *sa;
320 if (!
inet_ntop(AF_INET6, addr, numaddr,
sizeof(numaddr)))
323 numaddrlen = strlen(numaddr);
324 if (numaddrlen + 1 > hostlen)
328 if (((
const struct sockaddr_in6 *)sa)->sin6_scope_id) {
332 zonelen = ip6_sa2str(
333 (
const struct sockaddr_in6 *)(
const void *)sa,
334 zonebuf,
sizeof(zonebuf), flags);
337 if (zonelen + 1 + numaddrlen + 1 > hostlen)
341 memcpy(host + numaddrlen + 1, zonebuf,
344 host[numaddrlen + 1 + zonelen] =
'\0';
352 ip6_sa2str(sa6, buf, bufsiz, flags)
353 const struct sockaddr_in6 *sa6;
358 unsigned int ifindex;
359 const struct in6_addr *a6;
362 ifindex = (
unsigned int)sa6->sin6_scope_id;
363 a6 = &sa6->sin6_addr;
367 n = snprintf(buf, bufsiz,
"%u", sa6->sin6_scope_id);
368 if (n < 0 || n >= bufsiz)
376 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
377 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
378 char *p = if_indextoname(ifindex, buf);
384 n = snprintf(buf, bufsiz,
"%u", sa6->sin6_scope_id);
385 if (n < 0 || n >= bufsiz)