5 #include <sys/socket.h>
6 #include <netinet/in.h>
11 #include "stdio_impl.h"
13 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
14 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
15 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
16 int __res_send(const unsigned char *, int, unsigned char *, int);
18 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
21 static char *itoa(char *p, unsigned x) {
31 static void mkptr4(char *s, const unsigned char *ip)
33 sprintf(s, "%d.%d.%d.%d.in-addr.arpa",
34 ip[3], ip[2], ip[1], ip[0]);
37 static void mkptr6(char *s, const unsigned char *ip)
39 static const char xdigits[] = "0123456789abcdef";
41 for (i=15; i>=0; i--) {
42 *s++ = xdigits[ip[i]&15]; *s++ = '.';
43 *s++ = xdigits[ip[i]>>4]; *s++ = '.';
45 strcpy(s, "ip6.arpa");
48 static char *reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family)
50 char line[512], *p, *z;
51 unsigned char _buf[1032], atmp[16];
53 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
55 if (family == AF_INET) {
56 memcpy(atmp+12, a, 4);
57 memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
60 while (fgets(line, sizeof line, f)) {
61 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
63 for (p=line; *p && !isspace(*p); p++);
65 if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0)
68 if (iplit.family == AF_INET) {
69 memcpy(iplit.addr+12, iplit.addr, 4);
70 memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
74 if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid)
77 for (; *p && isspace(*p); p++);
78 for (z=p; *z && !isspace(*z); z++);
81 memcpy(buf, p, z-p+1);
89 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
92 if (rr != RR_PTR) return 0;
93 if (__dn_expand(packet, (const unsigned char *)packet + 512,
94 data, tmp, sizeof tmp) > 0)
100 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
101 char *restrict node, socklen_t nodelen,
102 char *restrict serv, socklen_t servlen,
106 char buf[256], num[3*sizeof(int)+1];
107 int af = sa->sa_family;
113 a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
114 if (sl != sizeof(struct sockaddr_in)) return EAI_FAMILY;
119 a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
120 if (sl != sizeof(struct sockaddr_in6)) return EAI_FAMILY;
121 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
125 scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
131 if (node && nodelen) {
133 if (!(flags & NI_NUMERICHOST)) {
134 reverse_hosts(buf, a, scopeid, af);
136 if (!*buf && !(flags & NI_NUMERICHOST)) {
137 unsigned char query[18+PTR_MAX], reply[512];
138 int qlen = __res_mkquery(0, ptr, 1, RR_PTR,
139 0, 0, 0, query, sizeof query);
140 int rlen = __res_send(query, qlen, reply, sizeof reply);
143 __dns_parse(reply, rlen, dns_parse_callback, buf);
146 if (flags & NI_NAMEREQD) return EAI_NONAME;
147 inet_ntop(af, a, buf, sizeof buf);
149 char *p = 0, tmp[IF_NAMESIZE+1];
150 if (!(flags & NI_NUMERICSCOPE) &&
151 (IN6_IS_ADDR_LINKLOCAL(a) ||
152 IN6_IS_ADDR_MC_LINKLOCAL(a)))
153 p = if_indextoname(scopeid, tmp+1);
155 p = itoa(num, scopeid);
160 if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
164 if (serv && servlen) {
165 char *p = itoa(num, ntohs(((struct sockaddr_in *)sa)->sin_port));
166 if (strlen(p) >= servlen)