5 #include <sys/socket.h>
6 #include <netinet/in.h>
12 #include "stdio_impl.h"
14 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
17 static char *itoa(char *p, unsigned x) {
27 static void mkptr4(char *s, const unsigned char *ip)
29 sprintf(s, "%d.%d.%d.%d.in-addr.arpa",
30 ip[3], ip[2], ip[1], ip[0]);
33 static void mkptr6(char *s, const unsigned char *ip)
35 static const char xdigits[] = "0123456789abcdef";
37 for (i=15; i>=0; i--) {
38 *s++ = xdigits[ip[i]&15]; *s++ = '.';
39 *s++ = xdigits[ip[i]>>4]; *s++ = '.';
41 strcpy(s, "ip6.arpa");
44 static void reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family)
46 char line[512], *p, *z;
47 unsigned char _buf[1032], atmp[16];
49 FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
51 if (family == AF_INET) {
52 memcpy(atmp+12, a, 4);
53 memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
56 while (fgets(line, sizeof line, f)) {
57 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
59 for (p=line; *p && !isspace(*p); p++);
61 if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0)
64 if (iplit.family == AF_INET) {
65 memcpy(iplit.addr+12, iplit.addr, 4);
66 memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
70 if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid)
73 for (; *p && isspace(*p); p++);
74 for (z=p; *z && !isspace(*z); z++);
77 memcpy(buf, p, z-p+1);
84 static void reverse_services(char *buf, int port, int dgram)
87 char line[128], *p, *z;
88 unsigned char _buf[1032];
89 FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
91 while (fgets(line, sizeof line, f)) {
92 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
94 for (p=line; *p && !isspace(*p); p++);
97 svport = strtoul(p, &z, 10);
99 if (svport != port || z==p) continue;
100 if (dgram && strncmp(z, "/udp", 4)) continue;
101 if (!dgram && strncmp(z, "/tcp", 4)) continue;
102 if (p-line > 32) continue;
104 memcpy(buf, line, p-line);
110 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
112 if (rr != RR_PTR) return 0;
113 if (__dn_expand(packet, (const unsigned char *)packet + 512,
120 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
121 char *restrict node, socklen_t nodelen,
122 char *restrict serv, socklen_t servlen,
126 char buf[256], num[3*sizeof(int)+1];
127 int af = sa->sa_family;
133 a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
134 if (sl < sizeof(struct sockaddr_in)) return EAI_FAMILY;
139 a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
140 if (sl < sizeof(struct sockaddr_in6)) return EAI_FAMILY;
141 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
145 scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
151 if (node && nodelen) {
153 if (!(flags & NI_NUMERICHOST)) {
154 reverse_hosts(buf, a, scopeid, af);
156 if (!*buf && !(flags & NI_NUMERICHOST)) {
157 unsigned char query[18+PTR_MAX], reply[512];
158 int qlen = __res_mkquery(0, ptr, 1, RR_PTR,
159 0, 0, 0, query, sizeof query);
160 int rlen = __res_send(query, qlen, reply, sizeof reply);
163 __dns_parse(reply, rlen, dns_parse_callback, buf);
166 if (flags & NI_NAMEREQD) return EAI_NONAME;
167 inet_ntop(af, a, buf, sizeof buf);
169 char *p = 0, tmp[IF_NAMESIZE+1];
170 if (!(flags & NI_NUMERICSCOPE) &&
171 (IN6_IS_ADDR_LINKLOCAL(a) ||
172 IN6_IS_ADDR_MC_LINKLOCAL(a)))
173 p = if_indextoname(scopeid, tmp+1);
175 p = itoa(num, scopeid);
180 if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
184 if (serv && servlen) {
186 int port = ntohs(((struct sockaddr_in *)sa)->sin_port);
188 if (!(flags & NI_NUMERICSERV))
189 reverse_services(buf, port, flags & NI_DGRAM);
192 if (strlen(p) >= servlen)