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 void 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);
88 static void reverse_services(char *buf
, int port
, int dgram
)
91 char line
[128], *p
, *z
;
92 unsigned char _buf
[1032];
93 FILE _f
, *f
= __fopen_rb_ca("/etc/services", &_f
, _buf
, sizeof _buf
);
95 while (fgets(line
, sizeof line
, f
)) {
96 if ((p
=strchr(line
, '#'))) *p
++='\n', *p
=0;
98 for (p
=line
; *p
&& !isspace(*p
); p
++);
101 svport
= strtoul(p
, &z
, 10);
103 if (svport
!= port
|| z
==p
) continue;
104 if (dgram
&& strncmp(z
, "/udp", 4)) continue;
105 if (!dgram
&& strncmp(z
, "/tcp", 4)) continue;
106 if (p
-line
> 32) continue;
108 memcpy(buf
, line
, p
-line
);
114 static int dns_parse_callback(void *c
, int rr
, const void *data
, int len
, const void *packet
)
116 if (rr
!= RR_PTR
) return 0;
117 if (__dn_expand(packet
, (const unsigned char *)packet
+ 512,
124 int getnameinfo(const struct sockaddr
*restrict sa
, socklen_t sl
,
125 char *restrict node
, socklen_t nodelen
,
126 char *restrict serv
, socklen_t servlen
,
130 char buf
[256], num
[3*sizeof(int)+1];
131 int af
= sa
->sa_family
;
137 a
= (void *)&((struct sockaddr_in
*)sa
)->sin_addr
;
138 if (sl
< sizeof(struct sockaddr_in
)) return EAI_FAMILY
;
143 a
= (void *)&((struct sockaddr_in6
*)sa
)->sin6_addr
;
144 if (sl
< sizeof(struct sockaddr_in6
)) return EAI_FAMILY
;
145 if (memcmp(a
, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
149 scopeid
= ((struct sockaddr_in6
*)sa
)->sin6_scope_id
;
155 if (node
&& nodelen
) {
157 if (!(flags
& NI_NUMERICHOST
)) {
158 reverse_hosts(buf
, a
, scopeid
, af
);
160 if (!*buf
&& !(flags
& NI_NUMERICHOST
)) {
161 unsigned char query
[18+PTR_MAX
], reply
[512];
162 int qlen
= __res_mkquery(0, ptr
, 1, RR_PTR
,
163 0, 0, 0, query
, sizeof query
);
164 int rlen
= __res_send(query
, qlen
, reply
, sizeof reply
);
167 __dns_parse(reply
, rlen
, dns_parse_callback
, buf
);
170 if (flags
& NI_NAMEREQD
) return EAI_NONAME
;
171 inet_ntop(af
, a
, buf
, sizeof buf
);
173 char *p
= 0, tmp
[IF_NAMESIZE
+1];
174 if (!(flags
& NI_NUMERICSCOPE
) &&
175 (IN6_IS_ADDR_LINKLOCAL(a
) ||
176 IN6_IS_ADDR_MC_LINKLOCAL(a
)))
177 p
= if_indextoname(scopeid
, tmp
+1);
179 p
= itoa(num
, scopeid
);
184 if (strlen(buf
) >= nodelen
) return EAI_OVERFLOW
;
188 if (serv
&& servlen
) {
190 int port
= ntohs(((struct sockaddr_in
*)sa
)->sin_port
);
192 if (!(flags
& NI_NUMERICSERV
))
193 reverse_services(buf
, port
, flags
& NI_DGRAM
);
196 if (strlen(p
) >= servlen
)