2 * socktoa - return a numeric host name from a sockaddr_storage structure
6 #ifdef HAVE_SYS_SOCKET_H
7 #include <sys/socket.h>
9 #ifdef HAVE_NETINET_IN_H
10 #include <netinet/in.h>
13 #include <arpa/inet.h>
18 #include "lib_strbuf.h"
19 #include "ntp_stdlib.h"
21 #include "ntp_debug.h"
26 const sockaddr_u *sock
29 const char svc[] = "ntp";
33 struct addrinfo hints;
34 struct addrinfo * alist;
40 /* reverse the address to purported DNS name */
42 gni_flags = NI_DGRAM | NI_NAMEREQD;
43 if (getnameinfo(&sock->sa, SOCKLEN(sock), pbuf, LIB_BUFLENGTH,
45 return stoa(sock); /* use address */
47 TRACE(1, ("%s reversed to %s\n", stoa(sock), pbuf));
50 * Resolve the reversed name and make sure the reversed address
51 * is among the results.
54 hints.ai_family = AF(sock);
55 hints.ai_protocol = IPPROTO_UDP;
56 hints.ai_socktype = SOCK_DGRAM;
60 a_info = getaddrinfo(pbuf, svc, &hints, &alist);
61 if (a_info == EAI_NONAME
63 || a_info == EAI_NODATA
66 hints.ai_flags = AI_CANONNAME;
68 hints.ai_flags |= AI_ADDRCONFIG;
70 a_info = getaddrinfo(pbuf, svc, &hints, &alist);
73 /* Some older implementations don't like AI_ADDRCONFIG. */
74 if (a_info == EAI_BADFLAGS) {
75 hints.ai_flags &= ~AI_ADDRCONFIG;
76 a_info = getaddrinfo(pbuf, svc, &hints, &alist);
82 INSIST(alist != NULL);
84 for (ai = alist; ai != NULL; ai = ai->ai_next) {
86 * Make a convenience sockaddr_u copy from ai->ai_addr
87 * because casting from sockaddr * to sockaddr_u * is
88 * risking alignment problems on platforms where
89 * sockaddr_u has stricter alignment than sockaddr,
93 octets = min(sizeof(addr), ai->ai_addrlen);
94 memcpy(&addr, ai->ai_addr, octets);
95 if (SOCK_EQ(sock, &addr))
101 return pbuf; /* forward check passed */
104 TRACE(1, ("%s forward check lookup fail: %s\n", pbuf,
105 gai_strerror(a_info)));
107 snprintf(pliar, LIB_BUFLENGTH, "%s (%s)", stoa(sock), pbuf);