6 #include <sys/socket.h>
7 #include <netinet/in.h>
13 #include "stdio_impl.h"
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 void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family)
47 char line[512], *p, *z;
48 unsigned char _buf[1032], atmp[16];
50 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
52 if (family == AF_INET) {
53 memcpy(atmp+12, a, 4);
54 memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
57 while (fgets(line, sizeof line, f)) {
58 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
60 for (p=line; *p && !isspace(*p); p++);
62 if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0)
65 if (iplit.family == AF_INET) {
66 memcpy(iplit.addr+12, iplit.addr, 4);
67 memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
71 if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid)
74 for (; *p && isspace(*p); p++);
75 for (z=p; *z && !isspace(*z); z++);
78 memcpy(buf, p, z-p+1);
85 static void reverse_services(char *buf, int port, int dgram)
88 char line[128], *p, *z;
89 unsigned char _buf[1032];
90 FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
92 while (fgets(line, sizeof line, f)) {
93 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
95 for (p=line; *p && !isspace(*p); p++);
98 svport = strtoul(p, &z, 10);
100 if (svport != port || z==p) continue;
101 if (dgram && strncmp(z, "/udp", 4)) continue;
102 if (!dgram && strncmp(z, "/tcp", 4)) continue;
103 if (p-line > 32) continue;
105 memcpy(buf, line, p-line);
111 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
113 if (rr != RR_PTR) return 0;
114 if (__dn_expand(packet, (const unsigned char *)packet + 512,
121 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
122 char *restrict node, socklen_t nodelen,
123 char *restrict serv, socklen_t servlen,
127 char buf[256], num[3*sizeof(int)+1];
128 int af = sa->sa_family;
134 a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
135 if (sl < sizeof(struct sockaddr_in)) return EAI_FAMILY;
140 a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
141 if (sl < sizeof(struct sockaddr_in6)) return EAI_FAMILY;
142 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
146 scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
152 if (node && nodelen) {
154 if (!(flags & NI_NUMERICHOST)) {
155 reverse_hosts(buf, a, scopeid, af);
157 if (!*buf && !(flags & NI_NUMERICHOST)) {
158 unsigned char query[18+PTR_MAX], reply[512];
159 int qlen = __res_mkquery(0, ptr, 1, RR_PTR,
160 0, 0, 0, query, sizeof query);
161 query[3] = 0; /* don't need AD flag */
162 int rlen = __res_send(query, qlen, reply, sizeof reply);
165 __dns_parse(reply, rlen, dns_parse_callback, buf);
168 if (flags & NI_NAMEREQD) return EAI_NONAME;
169 inet_ntop(af, a, buf, sizeof buf);
171 char *p = 0, tmp[IF_NAMESIZE+1];
172 if (!(flags & NI_NUMERICSCOPE) &&
173 (IN6_IS_ADDR_LINKLOCAL(a) ||
174 IN6_IS_ADDR_MC_LINKLOCAL(a)))
175 p = if_indextoname(scopeid, tmp+1);
177 p = itoa(num, scopeid);
182 if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
186 if (serv && servlen) {
188 int port = ntohs(((struct sockaddr_in *)sa)->sin_port);
190 if (!(flags & NI_NUMERICSERV))
191 reverse_services(buf, port, flags & NI_DGRAM);
194 if (strlen(p) >= servlen)