5 #include <sys/socket.h>
6 #include <netinet/in.h>
10 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
11 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
12 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
13 int __res_send(const unsigned char *, int, unsigned char *, int);
15 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
18 static char *itoa(char *p, unsigned x) {
28 static void mkptr4(char *s, const unsigned char *ip)
30 sprintf(s, "%d.%d.%d.%d.in-addr.arpa",
31 ip[3], ip[2], ip[1], ip[0]);
34 static void mkptr6(char *s, const unsigned char *ip)
36 static const char xdigits[] = "0123456789abcdef";
38 for (i=15; i>=0; i--) {
39 *s++ = xdigits[ip[i]&15]; *s++ = '.';
40 *s++ = xdigits[ip[i]>>4]; *s++ = '.';
42 strcpy(s, "ip6.arpa");
45 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
48 if (rr != RR_PTR) return 0;
49 if (__dn_expand(packet, (const unsigned char *)packet + 512,
50 data, tmp, sizeof tmp) > 0)
56 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
57 char *restrict node, socklen_t nodelen,
58 char *restrict serv, socklen_t servlen,
62 char buf[256], num[3*sizeof(int)+1];
63 int af = sa->sa_family;
69 a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
70 if (sl != sizeof(struct sockaddr_in)) return EAI_FAMILY;
74 a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
75 if (sl != sizeof(struct sockaddr_in6)) return EAI_FAMILY;
76 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
85 if (node && nodelen) {
87 if (!(flags & NI_NUMERICHOST)) {
88 unsigned char query[18+PTR_MAX], reply[512];
89 int qlen = __res_mkquery(0, ptr, 1, RR_PTR,
90 0, 0, 0, query, sizeof query);
91 int rlen = __res_send(query, qlen, reply, sizeof reply);
94 __dns_parse(reply, rlen, dns_parse_callback, buf);
97 if (flags & NI_NAMEREQD) return EAI_NONAME;
98 inet_ntop(af, a, buf, sizeof buf);
100 (x = ((struct sockaddr_in6 *)sa)->sin6_scope_id)) {
101 char *p = 0, tmp[IF_NAMESIZE+1];
102 if (!(flags & NI_NUMERICSCOPE) &&
103 (IN6_IS_ADDR_LINKLOCAL(a) ||
104 IN6_IS_ADDR_MC_LINKLOCAL(a)))
105 p = if_indextoname(x, tmp+1);
112 if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
116 if (serv && servlen) {
117 char *p = itoa(num, ntohs(((struct sockaddr_in *)sa)->sin_port));
118 if (strlen(p) >= servlen)